Skip to content

Commit 9465f98

Browse files
authored
Merge pull request #78 from hymkor/push-vlwuunnoxtkp
Refactor: "dialect/*": move SQLs from main.go to *.sql using "embed"
2 parents 0f1b66b + 2a19bab commit 9465f98

13 files changed

Lines changed: 126 additions & 92 deletions

File tree

dialect/mysql/columns.sql

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
select ordinal_position as "ID",
2+
column_name as "NAME",
3+
case
4+
when character_maximum_length is not null then
5+
concat(data_type,'(',character_maximum_length,')')
6+
when datetime_precision is not null then
7+
concat(data_type,'(',datetime_precision,')')
8+
else
9+
data_type
10+
end as "TYPE",
11+
case is_nullable
12+
when "YES" then 'NULL'
13+
else 'NOT NULL'
14+
end as "NULL?"
15+
from information_schema.columns,
16+
(select ? as v1) arg
17+
where table_name = REGEXP_REPLACE(v1,'^[^\\.]*\\.','')
18+
and table_schema = case
19+
when v1 like '%.%' then regexp_replace(v1,'\\.[^\\.]*$','')
20+
else database()
21+
end
22+
order by ordinal_position

dialect/mysql/main.go

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

33
import (
4+
_ "embed"
45
"strings"
56
"time"
67

@@ -13,6 +14,12 @@ const (
1314
mySQLDateTimeTzLayout = "2006-01-02 15:04:05.999999999-07:00"
1415
)
1516

17+
//go:embed tables.sql
18+
var tablesSql string
19+
20+
//go:embed columns.sql
21+
var columnsSql string
22+
1623
var mySQLTypeNameToFormat = map[string]string{
1724
"DATETIME": dialect.DateTimeLayout,
1825
"TIMESTAMP": dialect.DateTimeLayout,
@@ -56,37 +63,9 @@ func formatValue(typeName string, value any) (string, bool) {
5663
}
5764

5865
var mySqlSpec = &dialect.Entry{
59-
Usage: `sqlbless mysql <USERNAME>:<PASSWORD>@/<DBNAME>`,
60-
SQLForColumns: `
61-
select ordinal_position as "ID",
62-
column_name as "NAME",
63-
case
64-
when character_maximum_length is not null then
65-
concat(data_type,'(',character_maximum_length,')')
66-
when datetime_precision is not null then
67-
concat(data_type,'(',datetime_precision,')')
68-
else data_type
69-
end as "TYPE",
70-
case is_nullable
71-
when "YES" then 'NULL'
72-
else 'NOT NULL'
73-
end as "NULL?"
74-
from information_schema.columns
75-
join (select ? as x) v
76-
where table_name = REGEXP_REPLACE(v.x,'^[^\\.]*\\.','')
77-
and table_schema =
78-
case
79-
when instr(v.x,'.') >= 1 then
80-
regexp_replace(v.x,'\\.[^\\.]*$','')
81-
else database()
82-
end
83-
order by ordinal_position`,
84-
SQLForTables: `
85-
select concat(table_schema,'.',table_name) as FULL_NAME,
86-
tables.* from information_schema.tables
87-
where table_type = 'BASE TABLE'
88-
and table_schema
89-
not in ('mysql', 'information_schema', 'performance_schema', 'sys')`,
66+
Usage: `sqlbless mysql <USERNAME>:<PASSWORD>@/<DBNAME>`,
67+
SQLForColumns: columnsSql,
68+
SQLForTables: tablesSql,
9069
TypeConverterFor: typeNameToConv,
9170
PlaceHolder: &dialect.PlaceHolderQuestion{},
9271
TableNameField: "FULL_NAME",

dialect/mysql/tables.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
select concat(table_schema,'.',table_name) as FULL_NAME,
2+
tables.*
3+
from information_schema.tables
4+
where table_type = 'BASE TABLE'
5+
and table_schema not in ('mysql', 'information_schema', 'performance_schema', 'sys')

dialect/oracle/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ import (
1010
"github.com/hymkor/sqlbless/dialect"
1111
)
1212

13+
//go:embed tables.sql
14+
var tablesSql string
15+
1316
//go:embed columns.sql
1417
var columnsSql string
1518

1619
var oracleSpec = &dialect.Entry{
1720
Usage: "sqlbless oracle://<USERNAME>:<PASSWORD>@<HOSTNAME>:<PORT>/<SERVICE>",
1821
SQLForColumns: columnsSql,
19-
SQLForTables: `select * from tab where tname not like 'BIN$%'`,
22+
SQLForTables: tablesSql,
2023
TypeConverterFor: typeNameToConv,
2124
TableNameField: "tname",
2225
ColumnNameField: "name",

dialect/oracle/tables.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
select *
2+
from tab
3+
where tname not like 'BIN$%'

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

dialect/sqlite/columns.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PRAGMA {schema.}table_info({table})

dialect/sqlite/main.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package sqlite
22

33
import (
44
"database/sql"
5+
_ "embed"
56
"fmt"
67
"strings"
78
"time"
@@ -12,17 +13,18 @@ import (
1213
"github.com/hymkor/sqlbless/internal/misc"
1314
)
1415

16+
//go:embed tables.sql
17+
var tablesSql string
18+
19+
//go:embed columns.sql
20+
var columnsSql string
21+
1522
var Entry = &dialect.Entry{
16-
Usage: "sqlbless sqlite3 :memory: OR <FILEPATH>",
17-
SQLForTables: `
18-
select 'main' as schema,name,rootpage,sql from sqlite_master
19-
where type = 'table'
20-
union all
21-
select 'temp' as schema,name,rootpage,sql from sqlite_temp_master
22-
where type = 'table'`,
23+
Usage: "sqlbless sqlite3 :memory: OR <FILEPATH>",
24+
SQLForTables: tablesSql,
2325
TypeConverterFor: typeNameToConv,
2426
PlaceHolder: &placeHolder{},
25-
SQLForColumns: `PRAGMA {schema.}table_info({table})`,
27+
SQLForColumns: columnsSql,
2628
TableNameField: "name",
2729
ColumnNameField: "name",
2830
IsTransactionSafe: canUseInTransaction,

0 commit comments

Comments
 (0)