forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sqlite_meta.go
169 lines (147 loc) · 3.16 KB
/
sqlite_meta.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
package translators
import (
"fmt"
"strings"
"github.com/jmoiron/sqlx"
"github.com/markbates/pop/fizz"
)
type sqliteIndexListInfo struct {
Seq int `db:"seq"`
Name string `db:"name"`
Unique bool `db:"unique"`
Origin string `db:"origin"`
Partial string `db:"partial"`
}
type sqliteIndexInfo struct {
Seq int `db:"seqno"`
CID int `db:"cid"`
Name string `db:"name"`
}
type sqliteTableInfo struct {
CID int `db:"cid"`
Name string `db:"name"`
Type string `db:"type"`
NotNull bool `db:"notnull"`
Default interface{} `db:"dflt_value"`
PK bool `db:"pk"`
}
func (t sqliteTableInfo) ToColumn() fizz.Column {
c := fizz.Column{
Name: t.Name,
ColType: t.Type,
Primary: t.PK,
Options: fizz.Options{},
}
if !t.NotNull {
c.Options["null"] = true
}
if t.Default != nil {
c.Options["default"] = strings.TrimSuffix(strings.TrimPrefix(fmt.Sprintf("%s", t.Default), "'"), "'")
}
return c
}
type sqliteSchema struct {
URL string
db *sqlx.DB
schema map[string]*fizz.Table
}
func (p *sqliteSchema) Delete(table string) {
delete(p.schema, table)
}
func (p *sqliteSchema) TableInfo(table string) (*fizz.Table, error) {
if ti, ok := p.schema[table]; ok {
return ti, nil
}
err := p.buildSchema()
if err != nil {
return nil, err
}
if ti, ok := p.schema[table]; ok {
return ti, nil
}
return nil, fmt.Errorf("Could not find table data for %s!", table)
}
func (p *sqliteSchema) buildSchema() error {
var err error
p.db, err = sqlx.Open("sqlite3", p.URL)
if err != nil {
return err
}
defer p.db.Close()
res, err := p.db.Queryx("SELECT name FROM sqlite_master WHERE type='table';")
if err != nil {
return err
}
for res.Next() {
table := &fizz.Table{
Columns: []fizz.Column{},
Indexes: []fizz.Index{},
}
err = res.StructScan(table)
if err != nil {
return err
}
if table.Name != "sqlite_sequence" {
err = p.buildTableData(table)
if err != nil {
return err
}
}
}
return nil
}
func (p *sqliteSchema) buildTableData(table *fizz.Table) error {
prag := fmt.Sprintf("PRAGMA table_info(%s)", table.Name)
res, err := p.db.Queryx(prag)
if err != nil {
return nil
}
for res.Next() {
ti := sqliteTableInfo{}
err = res.StructScan(&ti)
if err != nil {
return err
}
table.Columns = append(table.Columns, ti.ToColumn())
}
err = p.buildTableIndexes(table)
if err != nil {
return err
}
p.schema[table.Name] = table
return nil
}
func (p *sqliteSchema) buildTableIndexes(t *fizz.Table) error {
prag := fmt.Sprintf("PRAGMA index_list(%s)", t.Name)
res, err := p.db.Queryx(prag)
if err != nil {
return err
}
for res.Next() {
li := sqliteIndexListInfo{}
err = res.StructScan(&li)
if err != nil {
return err
}
i := fizz.Index{
Name: li.Name,
Unique: li.Unique,
Columns: []string{},
}
prag = fmt.Sprintf("PRAGMA index_info(%s)", i.Name)
iires, err := p.db.Queryx(prag)
if err != nil {
return err
}
for iires.Next() {
ii := sqliteIndexInfo{}
err = iires.StructScan(&ii)
if err != nil {
return err
}
i.Columns = append(i.Columns, ii.Name)
}
t.Indexes = append(t.Indexes, i)
}
return nil
}