-
Notifications
You must be signed in to change notification settings - Fork 22
/
dialect_postgres.go
99 lines (86 loc) · 2.32 KB
/
dialect_postgres.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
package schema
import (
"database/sql"
)
// TODO(js) Should we be filtering out system tables, like we currently do?
const postgresAllColumns = `SELECT * FROM %s LIMIT 0`
const postgresTableNamesWithSchema = `
SELECT
table_schema,
table_name
FROM
information_schema.tables
WHERE
table_type = 'BASE TABLE' AND
table_schema NOT IN ('pg_catalog', 'information_schema')
ORDER BY
table_schema,
table_name
`
const postgresViewNamesWithSchema = `
SELECT
table_schema,
table_name
FROM
information_schema.tables
WHERE
table_type = 'VIEW' AND
table_schema NOT IN ('pg_catalog', 'information_schema')
ORDER BY
table_schema,
table_name
`
const postgresPrimaryKey = `
SELECT
kcu.column_name
FROM
information_schema.table_constraints tco
JOIN
information_schema.key_column_usage kcu
ON kcu.constraint_name = tco.constraint_name AND
kcu.constraint_schema = tco.constraint_schema AND
kcu.constraint_name = tco.constraint_name
WHERE
tco.constraint_type = 'PRIMARY KEY' AND
kcu.table_schema = current_schema() AND
kcu.table_name = $1
ORDER BY
kcu.ordinal_position
`
const postgresPrimaryKeyWithSchema = `
SELECT
kcu.column_name
FROM
information_schema.table_constraints tco
JOIN
information_schema.key_column_usage kcu
ON kcu.constraint_name = tco.constraint_name AND
kcu.constraint_schema = tco.constraint_schema AND
kcu.constraint_name = tco.constraint_name
WHERE
tco.constraint_type = 'PRIMARY KEY' AND
kcu.table_schema = $1 AND
kcu.table_name = $2
ORDER BY
kcu.ordinal_position
`
type postgresDialect struct{}
func (postgresDialect) escapeIdent(ident string) string {
// "tablename"
return escapeWithDoubleQuotes(ident)
}
func (d postgresDialect) ColumnTypes(db *sql.DB, schema, name string) ([]*sql.ColumnType, error) {
return fetchColumnTypes(db, postgresAllColumns, schema, name, d.escapeIdent)
}
func (postgresDialect) PrimaryKey(db *sql.DB, schema, name string) ([]string, error) {
if schema == "" {
return fetchNames(db, postgresPrimaryKey, "", name)
}
return fetchNames(db, postgresPrimaryKeyWithSchema, schema, name)
}
func (postgresDialect) TableNames(db *sql.DB) ([][2]string, error) {
return fetchObjectNames(db, postgresTableNamesWithSchema)
}
func (postgresDialect) ViewNames(db *sql.DB) ([][2]string, error) {
return fetchObjectNames(db, postgresViewNamesWithSchema)
}