-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
sqlite.go
502 lines (449 loc) · 13.7 KB
/
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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
// +build !darwin
// +build !skip_sqlite
// This package depends on go-sqlite3 which wraps the C library which I can't
// get to build for mac so it is excluded with the above build tag.
package sqlite
// Sqlite doesn't support schema so table.schema is ignored throughout
import (
"database/sql"
"errors"
"fmt"
_ "github.com/mattn/go-sqlite3"
"github.com/timabell/schema-explorer/driver_interface"
"github.com/timabell/schema-explorer/drivers"
"github.com/timabell/schema-explorer/params"
"github.com/timabell/schema-explorer/reader"
"github.com/timabell/schema-explorer/schema"
"log"
"strings"
)
var pathVal = ""
const filePathConfigKey = "file"
var driverOpts = drivers.DriverOpts{
filePathConfigKey: drivers.DriverOpt{Description: "Path to sqlite db file", Value: &pathVal},
}
func init() {
reader.RegisterReader(&drivers.Driver{Name: "sqlite", Options: driverOpts, CreateReader: newSqlite, FullName: "SQLite"})
}
type sqliteModel struct {
path string
connected bool // todo: technically it's a connection string per db so we could end up in multiple states, ignore for now
}
func newSqlite() driver_interface.DbReader {
path := driverOpts[filePathConfigKey].Value
log.Printf("Connecting to sqlite file: '%s'", *path)
return sqliteModel{path: *path, connected: false}
}
func (model sqliteModel) ReadSchema(databaseName string) (database *schema.Database, err error) {
dbc, err := getConnection(model.path)
if err != nil {
return
}
defer dbc.Close()
database = &schema.Database{
Supports: schema.SupportedFeatures{
Schema: false,
Descriptions: false,
FkNames: false, // todo: Get sqlite fk names https://stackoverflow.com/a/42365021/10245
PagingWithoutSorting: true,
},
}
// load table list
database.Tables, err = model.getTables(dbc)
if err != nil {
return
}
// add table columns
for _, table := range database.Tables {
var cols []*schema.Column
cols, err = model.getColumns(dbc, table)
if err != nil {
return
}
table.Columns = append(table.Columns, cols...)
}
// fks
for _, table := range database.Tables {
var fks []*schema.Fk
fks, err = getFks(dbc, table, database)
if err != nil {
return
}
table.Fks = fks
database.Fks = append(database.Fks, fks...)
}
// hook-up inbound fks
for _, fk := range database.Fks {
destination := database.FindTable(fk.DestinationTable)
if destination == nil {
err = fmt.Errorf("failed to find destination table for fk %s", fk)
}
destination.InboundFks = append(destination.InboundFks, fk)
for _, destCol := range fk.DestinationColumns {
destCol.InboundFks = append(destCol.InboundFks, fk)
}
}
// indexes
for _, table := range database.Tables {
var indexes []*schema.Index
indexes, err = getIndexes(dbc, table, database)
if err != nil {
return
}
table.Indexes = indexes
database.Indexes = append(database.Indexes, indexes...)
}
//log.Print(database.DebugString())
return
}
func (model sqliteModel) CanSwitchDatabase() bool {
return false
}
func (model sqliteModel) GetConfiguredDatabaseName() string {
return ""
}
func (model sqliteModel) ListDatabases() (databaseList []string, err error) {
panic("not available for sqlite")
}
func (model sqliteModel) DatabaseSelected() bool {
return true // there is only one
}
func (model sqliteModel) UpdateRowCounts(database *schema.Database) (err error) {
for _, table := range database.Tables {
rowCount, err := model.getRowCount(table)
if err != nil {
// todo: aggregate errors to return
log.Printf("Failed to get row count for %s, %s", table, err)
rowCount = -1
}
table.RowCount = &rowCount
}
return err
}
func (model sqliteModel) getTables(dbc *sql.DB) (tables []*schema.Table, err error) {
// todo: parameterise
rows, err := dbc.Query("SELECT name FROM sqlite_master WHERE type='table' AND name not like 'sqlite_%' order by name;")
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var name string
rows.Scan(&name)
tables = append(tables, &schema.Table{Name: name, Pk: &schema.Pk{}})
}
return tables, nil
}
func (model sqliteModel) getRowCount(table *schema.Table) (rowCount int, err error) {
sql := "select count(*) from \"" + table.Name + "\""
dbc, err := getConnection(model.path)
if dbc == nil {
log.Println(err)
panic("getConnection() returned nil")
}
defer dbc.Close()
rows, err := dbc.Query(sql)
if err != nil {
return 0, err
}
defer rows.Close()
rows.Next()
var count int
rows.Scan(&count)
return count, nil
}
func getConnection(path string) (dbc *sql.DB, err error) {
dbc, err = sql.Open("sqlite3", path)
if err != nil {
log.Println("connection error", err)
}
return
}
func (model sqliteModel) SetDatabase(databaseName string) {
// n/a
}
func (model sqliteModel) GetDatabaseName() string {
return "" // n/a
}
func (model sqliteModel) CheckConnection(databaseName string) (err error) {
if model.path == "" {
return errors.New("sqlite file path not set")
}
dbc, err := getConnection(model.path)
if dbc == nil {
log.Println(err)
panic("getConnection() returned nil")
}
defer dbc.Close()
err = dbc.Ping()
if err != nil {
err = errors.New("database ping failed - " + err.Error())
return
}
tables, err := model.getTables(dbc)
if err != nil {
err = errors.New("getTables() failed - " + err.Error())
return
}
if len(tables) == 0 {
// https://stackoverflow.com/q/45777113/10245
err = errors.New("no tables found. (SQLite will create an empty db if the specified file doesn't exist)")
return
}
model.connected = true
log.Println("Connected.", len(tables), "tables found")
return
}
func (model sqliteModel) Connected() bool {
return model.connected
}
func getFks(dbc *sql.DB, sourceTable *schema.Table, database *schema.Database) (fks []*schema.Fk, err error) {
// todo: parameterise
rows, err := dbc.Query("PRAGMA foreign_key_list('" + sourceTable.Name + "');")
if err != nil {
return
}
defer rows.Close()
fks = []*schema.Fk{}
for rows.Next() {
var id, seq int
var destinationTableName, sourceColumnName, destinationColumnName, onUpdate, onDelete, match string
rows.Scan(&id, &seq, &destinationTableName, &sourceColumnName, &destinationColumnName, &onUpdate, &onDelete, &match)
_, sourceColumn := sourceTable.FindColumn(sourceColumnName)
destinationTable := database.FindTable(&schema.Table{Name: destinationTableName})
_, destinationColumn := destinationTable.FindColumn(destinationColumnName)
// see if we are adding columns to an existing fk
var fk *schema.Fk
for _, existingFk := range fks {
if existingFk.Id == id {
existingFk.SourceColumns = append(existingFk.SourceColumns, sourceColumn)
existingFk.DestinationColumns = append(existingFk.DestinationColumns, destinationColumn)
fk = existingFk
break
}
}
if fk == nil {
fk = &schema.Fk{Id: id, SourceTable: sourceTable, SourceColumns: schema.ColumnList{sourceColumn}, DestinationTable: destinationTable, DestinationColumns: schema.ColumnList{destinationColumn}}
fks = append(fks, fk)
}
sourceColumn.Fks = append(sourceColumn.Fks, fk)
}
return
}
func getIndexes(dbc *sql.DB, table *schema.Table, database *schema.Database) (indexes []*schema.Index, err error) {
rows, err := dbc.Query("PRAGMA index_list('" + table.Name + "');")
if err != nil {
return
}
defer rows.Close()
indexes = []*schema.Index{}
for rows.Next() {
var seq int
var name, origin string
var unique, partial bool
rows.Scan(&seq, &name, &unique, &origin, &partial)
if strings.HasPrefix(name, "sqlite_autoindex") {
continue
}
thisIndex := schema.Index{
Name: name,
Table: table,
IsUnique: unique,
}
err = getIndexInfo(dbc, &thisIndex, table)
if err != nil {
return
}
indexes = append(indexes, &thisIndex)
}
database.Indexes = append(database.Indexes, indexes...)
return
}
func getIndexInfo(dbc *sql.DB, index *schema.Index, table *schema.Table) (err error) {
rows, err := dbc.Query("PRAGMA index_info('" + index.Name + "');")
if err != nil {
return
}
defer rows.Close()
for rows.Next() {
var seqno, cid int
var colName string
rows.Scan(&seqno, &cid, &colName)
if colName != "" {
_, col := table.FindColumn(colName)
if col == nil {
err = errors.New(fmt.Sprintf("can't find col '%s' specified in index %s", colName, index.String()))
return
}
col.Indexes = append(col.Indexes, index)
index.Columns = append(index.Columns, col)
}
}
return
}
func (model sqliteModel) GetSqlRows(databaseName string, table *schema.Table, params *params.TableParams, peekFinder *driver_interface.PeekLookup) (rows *sql.Rows, err error) {
dbc, err := getConnection(model.path)
if err != nil {
log.Print("GetRows failed to get connection")
return
}
defer dbc.Close()
sql, values := buildQuery(table, params, peekFinder)
rows, err = dbc.Query(sql, values...)
if err != nil {
log.Print("GetRows failed to get query")
log.Println(sql)
log.Println(err)
}
return
}
func (model sqliteModel) GetRowCount(databaseName string, table *schema.Table, params *params.TableParams) (rowCount int, err error) {
dbc, err := getConnection(model.path)
if err != nil {
log.Print("GetRows failed to get connection")
return
}
defer dbc.Close()
sql, values := buildQuery(table, params, &driver_interface.PeekLookup{})
sql = "select count(*) from (" + sql + ")"
rows, err := dbc.Query(sql, values...)
if err != nil {
log.Print("GetRowCount failed to get query")
log.Println(sql)
log.Println(err)
return
}
if !rows.Next() {
err = errors.New("GetRowCount query returned no rows")
return
}
rows.Scan(&rowCount)
return
}
func (model sqliteModel) GetAnalysis(databaseName string, table *schema.Table) (analysis []schema.ColumnAnalysis, err error) {
// todo, might be good to stream this all the way to the http response
dbc, err := getConnection(model.path)
if err != nil {
log.Print("GetAnalysis failed to get connection")
return
}
defer dbc.Close()
analysis = []schema.ColumnAnalysis{}
for _, col := range table.Columns {
sql := "select " + col.Name + ", count(*) qty from " + table.Name + " group by " + col.Name + " order by count(*) desc, " + col.Name + " limit 100;"
rows, err := dbc.Query(sql)
if err != nil {
log.Print("GetAnalysis failed to get query")
log.Println(sql)
log.Println(err)
return nil, err
}
var valueInfos []schema.ValueInfo
for rows.Next() {
var value interface{}
var quantity int
rows.Scan(&value, &quantity)
valueInfos = append(valueInfos, schema.ValueInfo{
Value: value,
Quantity: quantity,
})
}
analysis = append(analysis, schema.ColumnAnalysis{
Column: col,
ValueCounts: valueInfos,
})
}
return
}
func buildQuery(table *schema.Table, params *params.TableParams, peekFinder *driver_interface.PeekLookup) (sql string, values []interface{}) {
sql = "select t.*"
// peek cols
for fkIndex, fk := range peekFinder.Fks {
for _, peekCol := range fk.DestinationTable.PeekColumns {
sql = sql + fmt.Sprintf(", fk%d.[%s] fk%d_%s", fkIndex, peekCol, fkIndex, peekCol)
}
}
// inbound fk counts
for inboundFkIndex, inboundFk := range table.InboundFks {
onPredicates := []string{}
for ix, sourceCol := range inboundFk.SourceColumns {
onPredicates = append(onPredicates, fmt.Sprintf("ifk%d.[%s] = t.[%s]", inboundFkIndex, sourceCol.Name, inboundFk.DestinationColumns[ix].Name))
}
onString := strings.Join(onPredicates, " and ")
sql = sql + fmt.Sprintf(", (select count(*) from [%s] ifk%d where %s) ifk%d_count", inboundFk.SourceTable, inboundFkIndex, onString, inboundFkIndex)
}
sql = sql + " from [" + table.Name + "] t"
// peek tables
for fkIndex, fk := range peekFinder.Fks {
sql = sql + fmt.Sprintf(" left outer join [%s] fk%d on ", fk.DestinationTable.String(), fkIndex)
onPredicates := []string{}
for ix, sourceCol := range fk.SourceColumns {
onPredicates = append(onPredicates, fmt.Sprintf("t.[%s] = fk%d.[%s]", sourceCol, fkIndex, fk.DestinationColumns[ix]))
}
onString := strings.Join(onPredicates, " and ")
sql = sql + onString
}
query := params.Filter
if len(query) > 0 {
sql = sql + " where "
clauses := make([]string, 0, len(query))
values = make([]interface{}, 0, len(query))
for _, v := range query {
col := v.Field
clauses = append(clauses, "t.["+col.Name+"] = ?")
values = append(values, v.Values[0]) // todo: maybe support multiple values
}
sql = sql + strings.Join(clauses, " and ")
}
if len(params.Sort) > 0 {
var sortParts []string
for _, sortCol := range params.Sort {
sortString := "t.[" + sortCol.Column.Name + "]"
if sortCol.Descending {
sortString = sortString + " desc"
}
sortParts = append(sortParts, sortString)
}
sql = sql + " order by " + strings.Join(sortParts, ", ")
}
if params.RowLimit > 0 || params.SkipRows > 0 {
sql = sql + fmt.Sprintf(" limit %d, %d", params.SkipRows, params.RowLimit)
}
return sql, values
}
func (model sqliteModel) getColumns(dbc *sql.DB, table *schema.Table) (cols []*schema.Column, err error) {
// todo: parameterise
rows, err := dbc.Query("PRAGMA table_info('" + table.Name + "');")
if err != nil {
return
}
defer rows.Close()
cols = []*schema.Column{}
colIndex := 0
for rows.Next() {
var cid, pk int
var name, typeName string
var notNull bool
var defaultValue interface{}
rows.Scan(&cid, &name, &typeName, ¬Null, &defaultValue, &pk)
thisCol := schema.Column{
Position: colIndex,
Name: name,
Type: typeName,
IsInPrimaryKey: pk > 0,
Nullable: !notNull,
}
cols = append(cols, &thisCol)
if pk > 0 {
table.Pk.Columns = append(table.Pk.Columns, &thisCol)
}
colIndex++
}
return
}
func (model sqliteModel) SetTableDescription(database string, table string, description string) (err error) {
return
}
func (model sqliteModel) SetColumnDescription(database string, table string, column string, description string) (err error) {
return
}