-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathdialect_sqlite.go
70 lines (58 loc) · 1.35 KB
/
dialect_sqlite.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
package schema
import (
"database/sql"
)
// TODO(js) Can we see tables in an attached database? How are their names handled? See https://sqlite.org/lang_naming.html
const sqliteAllColumns = `SELECT * FROM %s LIMIT 0`
const sqliteTableNamesWithSchema = `
SELECT
"" AS schema,
name
FROM
sqlite_master
WHERE
type = 'table'
ORDER BY
name
`
const sqliteViewNamesWithSchema = `
SELECT
"" AS schema,
name
FROM
sqlite_master
WHERE
type = 'view'
ORDER BY
name
`
const sqlitePrimaryKey = `
SELECT
name
FROM
pragma_table_info(?)
WHERE
pk > 0
ORDER BY
pk
`
type sqliteDialect struct{}
func (sqliteDialect) escapeIdent(ident string) string {
// "tablename"
return escapeWithDoubleQuotes(ident)
}
func (d sqliteDialect) ColumnTypes(db *sql.DB, schema, name string) ([]*sql.ColumnType, error) {
return fetchColumnTypes(db, sqliteAllColumns, schema, name, d.escapeIdent)
}
func (sqliteDialect) PrimaryKey(db *sql.DB, schema, name string) ([]string, error) {
// if schema == "" {
// return fetchNames(db, sqlitePrimaryKey, "", name)
// }
return fetchNames(db, sqlitePrimaryKey, "", name)
}
func (sqliteDialect) TableNames(db *sql.DB) ([][2]string, error) {
return fetchObjectNames(db, sqliteTableNamesWithSchema)
}
func (sqliteDialect) ViewNames(db *sql.DB) ([][2]string, error) {
return fetchObjectNames(db, sqliteViewNamesWithSchema)
}