-
Notifications
You must be signed in to change notification settings - Fork 22
/
schema.go
213 lines (197 loc) · 5.27 KB
/
schema.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package schema
import (
"database/sql"
"fmt"
)
// https://github.com/golang/go/issues/7408
//
// https://github.com/golang/go/issues/7408#issuecomment-252046876
//
// If this package were to be part of database/sql, then the API would become like:-
//
// func (db *DB) Table(schema, table string) ([]*ColumnType, error)
// func (db *DB) TableNames() ([][2]string, error)
// func (db *DB) Tables() (map[[2]string][]*ColumnType, error)
// func (db *DB) View(schema, view string) ([]*ColumnType, error)
// func (db *DB) ViewNames() ([][2]string, error)
// func (db *DB) Views() (map[[2]string][]*ColumnType, error)
//
//
// UnknownDriverError is returned when there is no matching
// database driver type name in the driverDialect table.
//
// Errors of this kind are caused by using an unsupported
// database driver/dialect, or if/when a database driver
// developer renames the type underlying calls to db.Driver().
type UnknownDriverError struct {
Driver string
}
// Error returns a formatted string description.
func (e UnknownDriverError) Error() string {
return fmt.Sprintf("unknown database driver: %s", e.Driver)
}
//
// Tables returns column type metadata for all tables in the current schema.
//
// The returned map is keyed by table name tuples.
func Tables(db *sql.DB) (map[[2]string][]*sql.ColumnType, error) {
d, err := getDialect(db)
if err != nil {
return nil, err
}
names, err := d.TableNames(db)
if err != nil {
return nil, err
}
if len(names) == 0 {
return nil, nil
}
m := make(map[[2]string][]*sql.ColumnType, len(names))
for _, n := range names {
ct, err := d.ColumnTypes(db, n[0], n[1])
if err != nil {
return nil, err
}
m[n] = ct
}
return m, nil
}
// Views returns column type metadata for all views in the current schema.
//
// The returned map is keyed by view name tuples.
func Views(db *sql.DB) (map[[2]string][]*sql.ColumnType, error) {
d, err := getDialect(db)
if err != nil {
return nil, err
}
names, err := d.ViewNames(db)
if err != nil {
return nil, err
}
if len(names) == 0 {
return nil, nil
}
m := make(map[[2]string][]*sql.ColumnType, len(names))
for _, n := range names {
ct, err := d.ColumnTypes(db, n[0], n[1])
if err != nil {
return nil, err
}
m[n] = ct
}
return m, nil
}
// TableNames returns a list of all table names.
//
// Each name consists of a [2]string tuple: schema name, table name.
func TableNames(db *sql.DB) ([][2]string, error) {
d, err := getDialect(db)
if err != nil {
return nil, err
}
return d.TableNames(db)
}
// ViewNames returns a list of all view names.
//
// Each name consists of a [2]string tuple: schema name, view name.
func ViewNames(db *sql.DB) ([][2]string, error) {
d, err := getDialect(db)
if err != nil {
return nil, err
}
return d.ViewNames(db)
}
// ColumnTypes returns the column type metadata for the given object (table or view) in the given schema.
//
// Setting schema to an empty string results in the current schema being used.
func ColumnTypes(db *sql.DB, schema, object string) ([]*sql.ColumnType, error) {
d, err := getDialect(db)
if err != nil {
return nil, err
}
return d.ColumnTypes(db, schema, object)
}
// PrimaryKey returns a list of column names making up the primary
// key for the given table in the given schema.
func PrimaryKey(db *sql.DB, schema, table string) ([]string, error) {
d, err := getDialect(db)
if err != nil {
return nil, err
}
return d.PrimaryKey(db, schema, table)
}
// fetchNames executes the given query with an optional name parameter,
// and returns a list of table/view/column names.
//
// The name parameter (if not "") is passed as a parameter to db.Query.
func fetchNames(db *sql.DB, query, schema, name string) ([]string, error) {
var rows *sql.Rows
var err error
if len(schema) > 0 {
rows, err = db.Query(query, schema, name)
} else {
rows, err = db.Query(query, name)
}
if err != nil {
return nil, err
}
defer rows.Close()
// Scan result into list of names.
var names []string
n := ""
for rows.Next() {
err = rows.Scan(&n)
if err != nil {
return nil, err
}
names = append(names, n)
}
return names, nil
}
// fetchObjectNames executes the given query
// and returns a list of table/view/column names.
func fetchObjectNames(db *sql.DB, query string) ([][2]string, error) {
var rows *sql.Rows
var err error
rows, err = db.Query(query)
if err != nil {
return nil, err
}
defer rows.Close()
// Scan result into list of names.
var names [][2]string
s := ""
n := ""
for rows.Next() {
err = rows.Scan(&s, &n)
if err != nil {
return nil, err
}
names = append(names, [2]string{s, n})
}
return names, nil
}
func getDialect(db *sql.DB) (dialect, error) {
dt := fmt.Sprintf("%T", db.Driver())
d, ok := driverDialect[dt]
if !ok {
return nil, UnknownDriverError{Driver: dt}
}
return d, nil
}
// fetchColumnTypes queries the database and returns column's type metadata
// for a single table or view.
func fetchColumnTypes(db *sql.DB, query, schema, name string, escapeIdent func(string) string) ([]*sql.ColumnType, error) {
if schema == "" {
query = fmt.Sprintf(query, escapeIdent(name))
} else {
n := fmt.Sprintf("%s.%s", escapeIdent(schema), escapeIdent(name))
query = fmt.Sprintf(query, n)
}
rows, err := db.Query(query)
if err != nil {
return nil, err
}
defer rows.Close()
return rows.ColumnTypes()
}