-
Notifications
You must be signed in to change notification settings - Fork 22
/
dialect_mysql.go
95 lines (83 loc) · 1.96 KB
/
dialect_mysql.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
package schema
import (
"database/sql"
)
const mysqlAllColumns = `SELECT * FROM %s LIMIT 0`
const mysqlTableNamesWithSchema = `
SELECT
table_schema,
table_name
FROM
information_schema.tables
WHERE
table_type = 'BASE TABLE'
ORDER BY
table_schema,
table_name
`
const mysqlViewNamesWithSchema = `
SELECT
table_schema,
table_name
FROM
information_schema.tables
WHERE
table_type = 'VIEW'
ORDER BY
table_schema,
table_name
`
const mysqlPrimaryKey = `
SELECT
sta.column_name
FROM
information_schema.tables tab
INNER JOIN
information_schema.statistics sta
ON sta.table_schema = tab.table_schema AND
sta.table_name = tab.table_name AND
sta.index_name = 'primary'
WHERE
tab.table_type = 'BASE TABLE' AND
tab.table_schema = database() AND
tab.table_name = ?
ORDER BY
sta.seq_in_index
`
const mysqlPrimaryKeyWithSchema = `
SELECT
sta.column_name
FROM
information_schema.tables tab
INNER JOIN
information_schema.statistics sta
ON sta.table_schema = tab.table_schema AND
sta.table_name = tab.table_name AND
sta.index_name = 'primary'
WHERE
tab.table_type = 'BASE TABLE' AND
tab.table_schema = ? AND
tab.table_name = ?
ORDER BY
sta.seq_in_index
`
type mysqlDialect struct{}
func (mysqlDialect) escapeIdent(ident string) string {
// `tablename`
return escapeWithBackticks(ident)
}
func (d mysqlDialect) ColumnTypes(db *sql.DB, schema, name string) ([]*sql.ColumnType, error) {
return fetchColumnTypes(db, mysqlAllColumns, schema, name, d.escapeIdent)
}
func (mysqlDialect) PrimaryKey(db *sql.DB, schema, name string) ([]string, error) {
if schema == "" {
return fetchNames(db, mysqlPrimaryKey, "", name)
}
return fetchNames(db, mysqlPrimaryKeyWithSchema, schema, name)
}
func (mysqlDialect) TableNames(db *sql.DB) ([][2]string, error) {
return fetchObjectNames(db, mysqlTableNamesWithSchema)
}
func (mysqlDialect) ViewNames(db *sql.DB) ([][2]string, error) {
return fetchObjectNames(db, mysqlViewNamesWithSchema)
}