-
Notifications
You must be signed in to change notification settings - Fork 390
/
query.go
251 lines (211 loc) · 6.08 KB
/
query.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package sqliteutil
import (
"context"
"database/sql"
"regexp"
"sort"
"strings"
"github.com/zeebo/errs"
"storj.io/storj/shared/dbutil/dbschema"
)
type definition struct {
name string
table string
sql string
}
// QuerySchema loads the schema from sqlite database.
func QuerySchema(ctx context.Context, db dbschema.Queryer) (*dbschema.Schema, error) {
schema := &dbschema.Schema{}
tableDefinitions := make([]*definition, 0)
indexDefinitions := make([]*definition, 0)
// find tables and indexes
err := func() error {
rows, err := db.QueryContext(ctx, `
SELECT name, tbl_name, type, sql FROM sqlite_master WHERE sql NOT NULL AND name NOT LIKE 'sqlite_%'
`)
if err != nil {
return errs.Wrap(err)
}
defer func() { err = errs.Combine(err, rows.Close()) }()
for rows.Next() {
var defName, defTblName, defType, defSQL string
err := rows.Scan(&defName, &defTblName, &defType, &defSQL)
if err != nil {
return errs.Wrap(err)
}
if defType == "table" {
tableDefinitions = append(tableDefinitions, &definition{name: defName, sql: defSQL})
} else if defType == "index" {
indexDefinitions = append(indexDefinitions, &definition{name: defName, sql: defSQL, table: defTblName})
}
}
return rows.Err()
}()
if err != nil {
return nil, err
}
err = discoverTables(ctx, db, schema, tableDefinitions)
if err != nil {
return nil, err
}
err = discoverIndexes(ctx, db, schema, indexDefinitions)
if err != nil {
return nil, err
}
schema.Sort()
return schema, nil
}
func discoverTables(ctx context.Context, db dbschema.Queryer, schema *dbschema.Schema, tableDefinitions []*definition) (err error) {
for _, definition := range tableDefinitions {
if err := discoverTable(ctx, db, schema, definition); err != nil {
return err
}
}
return errs.Wrap(err)
}
func discoverTable(ctx context.Context, db dbschema.Queryer, schema *dbschema.Schema, definition *definition) (err error) {
table := schema.EnsureTable(definition.name)
tableRows, err := db.QueryContext(ctx, `PRAGMA table_info(`+definition.name+`)`)
if err != nil {
return errs.Wrap(err)
}
for tableRows.Next() {
var defaultValue sql.NullString
var index, name, columnType string
var pk int
var notNull bool
err := tableRows.Scan(&index, &name, &columnType, ¬Null, &defaultValue, &pk)
if err != nil {
return errs.Wrap(errs.Combine(tableRows.Err(), tableRows.Close(), err))
}
column := &dbschema.Column{
Name: name,
Type: columnType,
IsNullable: !notNull && pk == 0,
}
table.AddColumn(column)
if pk > 0 {
table.PrimaryKey = append(table.PrimaryKey, name)
}
}
err = errs.Combine(tableRows.Err(), tableRows.Close())
if err != nil {
return errs.Wrap(err)
}
matches := rxUnique.FindAllStringSubmatch(definition.sql, -1)
for _, match := range matches {
// TODO feel this can be done easier
var columns []string
for _, name := range strings.Split(match[1], ",") {
columns = append(columns, strings.TrimSpace(name))
}
table.Unique = append(table.Unique, columns)
}
keysRows, err := db.QueryContext(ctx, `PRAGMA foreign_key_list(`+definition.name+`)`)
if err != nil {
return errs.Wrap(err)
}
for keysRows.Next() {
var id, sec int
var tableName, from, to, onUpdate, onDelete, match string
err := keysRows.Scan(&id, &sec, &tableName, &from, &to, &onUpdate, &onDelete, &match)
if err != nil {
return errs.Wrap(errs.Combine(keysRows.Err(), keysRows.Close(), err))
}
column, found := table.FindColumn(from)
if found {
if onDelete == "NO ACTION" {
onDelete = ""
}
if onUpdate == "NO ACTION" {
onUpdate = ""
}
column.Reference = &dbschema.Reference{
Table: tableName,
Column: to,
OnUpdate: onUpdate,
OnDelete: onDelete,
}
}
}
err = errs.Combine(keysRows.Err(), keysRows.Close())
if err != nil {
return errs.Wrap(err)
}
return nil
}
func discoverIndexes(ctx context.Context, db dbschema.Queryer, schema *dbschema.Schema, indexDefinitions []*definition) (err error) {
// TODO improve indexes discovery
for _, definition := range indexDefinitions {
index := &dbschema.Index{
Name: definition.name,
Table: definition.table,
}
schema.Indexes = append(schema.Indexes, index)
indexRows, err := db.QueryContext(ctx, `PRAGMA index_info(`+definition.name+`)`)
if err != nil {
return errs.Wrap(err)
}
defer func() { err = errs.Combine(err, indexRows.Close()) }()
type indexInfo struct {
name *string
seqno int
cid int
}
var indexInfos []indexInfo
for indexRows.Next() {
var info indexInfo
err := indexRows.Scan(&info.seqno, &info.cid, &info.name)
if err != nil {
return errs.Wrap(err)
}
indexInfos = append(indexInfos, info)
}
sort.SliceStable(indexInfos, func(i, j int) bool {
return indexInfos[i].seqno < indexInfos[j].seqno
})
sqlDef := definition.sql
var parsedColumns []string
parseColumns := func() []string {
if parsedColumns != nil {
return parsedColumns
}
var base string
if matches := rxIndexExpr.FindStringSubmatchIndex(strings.ToUpper(sqlDef)); len(matches) > 0 {
base = sqlDef[matches[2]:matches[3]]
}
parsedColumns = strings.Split(base, ",")
return parsedColumns
}
for _, info := range indexInfos {
if info.name != nil {
index.Columns = append(index.Columns, *info.name)
continue
}
if info.cid == -1 {
index.Columns = append(index.Columns, "rowid")
} else if info.cid == -2 {
index.Columns = append(index.Columns, parseColumns()[info.seqno])
}
}
// unique
if strings.Contains(definition.sql, "CREATE UNIQUE INDEX") {
index.Unique = true
}
// partial
if matches := rxIndexPartial.FindStringSubmatch(definition.sql); len(matches) > 0 {
index.Partial = strings.TrimSpace(matches[1])
}
}
return errs.Wrap(err)
}
var (
// matches "UNIQUE (a,b)".
rxUnique = regexp.MustCompile(`UNIQUE\s*\((.*?)\)`)
// matches "ON table(expr)".
rxIndexExpr = regexp.MustCompile(`ON\s*[^(]*\((.*)\)`)
// matches "WHERE (partial expression)".
rxIndexPartial = regexp.MustCompile(`WHERE (.*)$`)
)