Skip to content

Commit 45c547d

Browse files
committed
Refactor: dialect/postgresql: move SQLs from main.go to *.sql using "embed"
1 parent 0f1b66b commit 45c547d

3 files changed

Lines changed: 50 additions & 49 deletions

File tree

dialect/postgresql/columns.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
select attname as "NAME",
2+
case
3+
when attnotnull then 'NOT NULL'
4+
else 'NULL'
5+
end as "NULL?",
6+
format_type(atttypid, atttypmod) as "TYPE"
7+
from pg_attribute
8+
where attrelid = to_regclass($1)::oid
9+
and attnum > 0
10+
and not attisdropped
11+
order by attnum

dialect/postgresql/main.go

Lines changed: 10 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package postgres
22

33
import (
4+
_ "embed"
45
"fmt"
56
"strings"
67
"time"
@@ -11,6 +12,12 @@ import (
1112
"github.com/hymkor/sqlbless/internal/misc"
1213
)
1314

15+
//go:embed tables.sql
16+
var tablesSql string
17+
18+
//go:embed columns.sql
19+
var columnsSql string
20+
1421
var postgresTypeNameToFormat = map[string][2]string{
1522
"TIMESTAMPTZ": [2]string{"TIMESTAMP WITH TIME ZONE", dialect.DateTimeTzLayout},
1623
"TIMESTAMP": [2]string{"TIMESTAMP", dialect.DateTimeLayout},
@@ -44,55 +51,9 @@ func (ph *placeHolder) Values() (result []any) {
4451
}
4552

4653
var postgresSpec = &dialect.Entry{
47-
Usage: "sqlbless postgres://<USERNAME>:<PASSWORD>@<HOSTNAME>:<PORT>/<DBNAME>?sslmode=disable",
48-
SQLForColumns: `
49-
with target as (
50-
select to_regclass($1)::oid as oid
51-
)
52-
select
53-
a.attname as "NAME",
54-
case a.attnotnull
55-
when true then 'NOT NULL'
56-
else 'NULL'
57-
end as "NULL?",
58-
format_type(a.atttypid, a.atttypmod) as "TYPE"
59-
from target t
60-
join pg_attribute a
61-
on a.attrelid = t.oid
62-
where a.attnum > 0
63-
and not a.attisdropped
64-
order by a.attnum`,
65-
SQLForTables: `
66-
select
67-
n.nspname || '.' || c.relname as full_name,
68-
n.nspname as table_schema,
69-
c.relname as table_name,
70-
case c.relkind
71-
when 'r' then 'BASE TABLE'
72-
when 'p' then 'PARTITIONED TABLE'
73-
when 'v' then 'VIEW'
74-
when 'm' then 'MATERIALIZED VIEW'
75-
when 'f' then 'FOREIGN TABLE'
76-
end as table_type,
77-
c.reltuples::bigint as estimated_rows,
78-
obj_description(c.oid,'pg_class') as remarks
79-
from pg_class c
80-
join pg_namespace n
81-
on n.oid = c.relnamespace
82-
where c.relkind in ('r','p','v','m','f')
83-
order by
84-
case n.nspname
85-
when 'pg_catalog' then 9
86-
when 'information_schema' then 8
87-
else 0
88-
end,
89-
case c.relkind
90-
when 'r' then 0
91-
when 'p' then 1
92-
else 9
93-
end,
94-
n.nspname,
95-
c.relname`,
54+
Usage: "sqlbless postgres://<USERNAME>:<PASSWORD>@<HOSTNAME>:<PORT>/<DBNAME>?sslmode=disable",
55+
SQLForColumns: columnsSql,
56+
SQLForTables: tablesSql,
9657
TypeConverterFor: postgresTypeNameToConv,
9758
PlaceHolder: &placeHolder{},
9859
TableNameField: "full_name",

dialect/postgresql/tables.sql

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
select n.nspname || '.' || c.relname as full_name,
2+
n.nspname as table_schema,
3+
c.relname as table_name,
4+
case c.relkind
5+
when 'r' then 'BASE TABLE'
6+
when 'p' then 'PARTITIONED TABLE'
7+
when 'v' then 'VIEW'
8+
when 'm' then 'MATERIALIZED VIEW'
9+
when 'f' then 'FOREIGN TABLE'
10+
end as table_type,
11+
c.reltuples::bigint as estimated_rows,
12+
obj_description(c.oid,'pg_class') as remarks
13+
from pg_class c
14+
join pg_namespace n
15+
on n.oid = c.relnamespace
16+
where c.relkind in ('r','p','v','m','f')
17+
order by
18+
case n.nspname
19+
when 'pg_catalog' then 9
20+
when 'information_schema' then 8
21+
else 0
22+
end,
23+
case c.relkind
24+
when 'r' then 0
25+
when 'p' then 1
26+
else 9
27+
end,
28+
n.nspname,
29+
c.relname

0 commit comments

Comments
 (0)