Skip to content

Commit 939b33c

Browse files
committed
refactor(sqlite): update Structure to use core methods
1 parent 9b77627 commit 939b33c

File tree

1 file changed

+18
-25
lines changed

1 file changed

+18
-25
lines changed

dbee/adapters/sqlite_driver.go

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,35 @@ type sqliteDriver struct {
1313
c *builders.Client
1414
}
1515

16-
func (c *sqliteDriver) Query(ctx context.Context, query string) (core.ResultStream, error) {
16+
func (d *sqliteDriver) Query(ctx context.Context, query string) (core.ResultStream, error) {
1717
// run query, fallback to affected rows
18-
return c.c.QueryUntilNotEmpty(ctx, query, "select changes() as 'Rows Affected'")
18+
return d.c.QueryUntilNotEmpty(ctx, query, "select changes() as 'Rows Affected'")
1919
}
2020

21-
func (c *sqliteDriver) Columns(opts *core.TableOptions) ([]*core.Column, error) {
22-
return c.c.ColumnsFromQuery("SELECT name, type FROM pragma_table_info('%s')", opts.Table)
21+
func (d *sqliteDriver) Columns(opts *core.TableOptions) ([]*core.Column, error) {
22+
return d.c.ColumnsFromQuery("SELECT name, type FROM pragma_table_info('%s')", opts.Table)
2323
}
2424

25-
func (c *sqliteDriver) Structure() ([]*core.Structure, error) {
26-
query := `SELECT name FROM sqlite_schema WHERE type ='table'`
25+
func (d *sqliteDriver) Structure() ([]*core.Structure, error) {
26+
// sqlite is single schema structure, so we hardcode the name of it.
27+
query := "SELECT 'sqlite_schema' as schema, name, type FROM sqlite_schema"
2728

28-
rows, err := c.Query(context.TODO(), query)
29+
rows, err := d.Query(context.Background(), query)
2930
if err != nil {
3031
return nil, err
3132
}
3233

33-
var schema []*core.Structure
34-
for rows.HasNext() {
35-
row, err := rows.Next()
36-
if err != nil {
37-
return nil, err
34+
decodeStructureType := func(typ string) core.StructureType {
35+
switch typ {
36+
case "table":
37+
return core.StructureTypeTable
38+
case "view":
39+
return core.StructureTypeView
40+
default:
41+
return core.StructureTypeNone
3842
}
39-
40-
// We know for a fact there is only one string field (see query above)
41-
table := row[0].(string)
42-
schema = append(schema, &core.Structure{
43-
Name: table,
44-
Schema: "",
45-
Type: core.StructureTypeTable,
46-
})
4743
}
48-
49-
return schema, nil
44+
return core.GetGenericStructure(rows, decodeStructureType)
5045
}
5146

52-
func (c *sqliteDriver) Close() {
53-
c.c.Close()
54-
}
47+
func (d *sqliteDriver) Close() { d.c.Close() }

0 commit comments

Comments
 (0)