-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdialect.go
102 lines (95 loc) · 2.54 KB
/
dialect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package orm
import (
"database/sql"
"fmt"
)
type Dialect struct {
DriverName string
PlaceholderChar string
IncludeIndexInPlaceholder bool
AddTableNameInSelectColumns bool
PlaceHolderGenerator func(n int) []string
QueryListTables string
QueryTableSchema string
}
func getListOfTables(query string) func(db *sql.DB) ([]string, error) {
return func(db *sql.DB) ([]string, error) {
rows, err := db.Query(query)
if err != nil {
return nil, err
}
var tables []string
for rows.Next() {
var table string
err = rows.Scan(&table)
if err != nil {
return nil, err
}
tables = append(tables, table)
}
return tables, nil
}
}
type columnSpec struct {
//0|id|INTEGER|0||1
Name string
Type string
Nullable bool
DefaultValue sql.NullString
IsPrimaryKey bool
}
func getTableSchema(query string) func(db *sql.DB, query string) ([]columnSpec, error) {
return func(db *sql.DB, table string) ([]columnSpec, error) {
rows, err := db.Query(fmt.Sprintf(query, table))
if err != nil {
return nil, err
}
var output []columnSpec
for rows.Next() {
var cs columnSpec
var nullable string
var pk int
err = rows.Scan(&cs.Name, &cs.Type, &nullable, &cs.DefaultValue, &pk)
if err != nil {
return nil, err
}
cs.Nullable = nullable == "notnull"
cs.IsPrimaryKey = pk == 1
output = append(output, cs)
}
return output, nil
}
}
var Dialects = &struct {
MySQL *Dialect
PostgreSQL *Dialect
SQLite3 *Dialect
}{
MySQL: &Dialect{
DriverName: "mysql",
PlaceholderChar: "?",
IncludeIndexInPlaceholder: false,
AddTableNameInSelectColumns: true,
PlaceHolderGenerator: questionMarks,
QueryListTables: "SHOW TABLES",
QueryTableSchema: "DESCRIBE %s",
},
PostgreSQL: &Dialect{
DriverName: "postgres",
PlaceholderChar: "$",
IncludeIndexInPlaceholder: true,
AddTableNameInSelectColumns: true,
PlaceHolderGenerator: postgresPlaceholder,
QueryListTables: `\dt`,
QueryTableSchema: `\d %s`,
},
SQLite3: &Dialect{
DriverName: "sqlite3",
PlaceholderChar: "?",
IncludeIndexInPlaceholder: false,
AddTableNameInSelectColumns: false,
PlaceHolderGenerator: questionMarks,
QueryListTables: "SELECT name FROM sqlite_schema WHERE type='table'",
QueryTableSchema: `SELECT name,type,"notnull","dflt_value","pk" FROM PRAGMA_TABLE_INFO('%s')`,
},
}