forked from rainycape/gondola
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.go
253 lines (234 loc) · 6.7 KB
/
backend.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
package sqlite
import (
"fmt"
"reflect"
"strings"
"time"
"gnd.la/config"
"gnd.la/encoding/codec"
"gnd.la/orm/driver"
"gnd.la/orm/driver/sql"
"gnd.la/orm/index"
"gnd.la/util/generic"
"gnd.la/util/stringutil"
"gnd.la/util/structs"
_ "github.com/mattn/go-sqlite3"
)
var (
sqliteBackend = &Backend{}
transformedTypes = []reflect.Type{
reflect.TypeOf((*time.Time)(nil)),
reflect.TypeOf((*bool)(nil)),
}
)
type Backend struct {
sql.SqlBackend
}
func (b *Backend) Name() string {
return "sqlite3"
}
func (b *Backend) Tag() string {
return "sqlite"
}
func (b *Backend) Func(fname string, retType reflect.Type) (string, error) {
if fname == "now" && retType.PkgPath() == "time" && retType.Name() == "Time" {
return "(strftime('%s', 'now'))", nil
}
return b.SqlBackend.Func(fname, retType)
}
func (b *Backend) Inspect(db *sql.DB, m driver.Model) (*sql.Table, error) {
name := db.QuoteString(m.Table())
rows, err := db.Query(fmt.Sprintf("PRAGMA table_info(%s)", name))
if err != nil {
return nil, err
}
defer rows.Close()
fieldsByName := make(map[string]*sql.Field)
var fields []*sql.Field
for rows.Next() {
var cid int
var f sql.Field
var notnull int
var def *string
var pk int
if err := rows.Scan(&cid, &f.Name, &f.Type, ¬null, &def, &pk); err != nil {
return nil, err
}
f.Type = strings.ToUpper(f.Type)
if notnull != 0 {
f.AddConstraint(sql.ConstraintNotNull)
}
if def != nil {
f.Default = *def
}
if pk != 0 {
f.AddConstraint(sql.ConstraintPrimaryKey)
}
fields = append(fields, &f)
fieldsByName[f.Name] = &f
}
if err := rows.Err(); err != nil {
return nil, err
}
rows, err = db.Query(fmt.Sprintf("PRAGMA foreign_key_list(%s)", name))
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var id, seq int
var table, from, to, onUpdate, onDelete, match string
if err := rows.Scan(&id, &seq, &table, &from, &to, &onUpdate, &onDelete, &match); err != nil {
return nil, err
}
field := fieldsByName[from]
field.Constraints = append(field.Constraints, &sql.Constraint{Type: sql.ConstraintForeignKey, References: sql.MakeReference(table, to)})
}
if err := rows.Err(); err != nil {
return nil, err
}
if len(fields) > 0 {
return &sql.Table{Fields: fields}, nil
}
return nil, nil
}
func (b *Backend) HasIndex(db *sql.DB, m driver.Model, idx *index.Index, name string) (bool, error) {
rows, err := db.Query(fmt.Sprintf("PRAGMA index_info(%s)", name))
if err != nil {
return false, err
}
has := rows.Next()
rows.Close()
return has, nil
}
func (b *Backend) DefineField(db *sql.DB, m driver.Model, table *sql.Table, field *sql.Field) (string, []string, error) {
if field.HasOption(sql.OptionAutoIncrement) {
if field.Constraint(sql.ConstraintPrimaryKey) == nil {
return "", nil, fmt.Errorf("%s can only auto increment the primary key", b.Name())
}
}
def, constraints, err := b.SqlBackend.DefineField(db, m, table, field)
if err == nil {
def = strings.Replace(strings.Replace(def, "DEFAULT false", "DEFAULT 0", -1), "DEFAULT true", "DEFAULT 1", -1)
}
return def, constraints, err
}
func (b *Backend) AddFields(db *sql.DB, m driver.Model, prevTable *sql.Table, newTable *sql.Table, fields []*sql.Field) error {
rewrite := false
for _, v := range fields {
if !b.canAddField(v) {
rewrite = true
break
}
}
if rewrite {
name := db.QuoteIdentifier(m.Table())
tmpName := fmt.Sprintf("%s_%s", m.Table(), stringutil.Random(8))
quotedTmpName := db.QuoteIdentifier(tmpName)
createSql, err := newTable.SQL(db, b, m, tmpName)
if err != nil {
return err
}
if _, err := db.Exec(createSql); err != nil {
return err
}
fieldNames := generic.Map(prevTable.Fields, func(f *sql.Field) string { return f.Name }).([]string)
// The previous table might have fields that we're not part
// of the new table.
fieldSet := make(map[string]bool)
for _, v := range newTable.Fields {
fieldSet[v.Name] = true
}
fieldNames = generic.Filter(fieldNames, func(n string) bool { return fieldSet[n] }).([]string)
sqlFields := strings.Join(generic.Map(fieldNames, db.QuoteIdentifier).([]string), ", ")
copySql := fmt.Sprintf("INSERT INTO %s (%s) SELECT %s FROM %s", quotedTmpName, sqlFields, sqlFields, name)
if _, err := db.Exec(copySql); err != nil {
return err
}
if _, err := db.Exec(fmt.Sprintf("DROP TABLE %s", name)); err != nil {
return err
}
if _, err := db.Exec(fmt.Sprintf("ALTER TABLE %s RENAME TO %s", quotedTmpName, name)); err != nil {
return err
}
return nil
}
return b.SqlBackend.AddFields(db, m, prevTable, newTable, fields)
}
func (b *Backend) FieldType(typ reflect.Type, t *structs.Tag) (string, error) {
if c := codec.FromTag(t); c != nil {
if c.Binary || t.PipeName() != "" {
return "BLOB", nil
}
return "TEXT", nil
}
switch typ.Kind() {
case reflect.Bool:
return "BOOLEAN", nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return "INTEGER", nil
case reflect.Float32, reflect.Float64:
return "REAL", nil
case reflect.String:
return "TEXT", nil
case reflect.Slice:
// []byte
if typ.Elem().Kind() == reflect.Uint8 {
return "BLOB", nil
}
case reflect.Struct:
if typ.Name() == "Time" && typ.PkgPath() == "time" {
return "INTEGER", nil
}
}
return "", fmt.Errorf("can't map field type %v to a database type", typ)
}
func (b *Backend) Transforms() []reflect.Type {
return transformedTypes
}
func (b *Backend) ScanInt(val int64, goVal *reflect.Value, t *structs.Tag) error {
switch goVal.Kind() {
case reflect.Struct:
goVal.Set(reflect.ValueOf(time.Unix(val, 0).UTC()))
return nil
case reflect.Bool:
goVal.SetBool(val != 0)
return nil
}
return b.SqlBackend.ScanInt(val, goVal, t)
}
func (b *Backend) TransformOutValue(val reflect.Value) (interface{}, error) {
val = driver.Direct(val)
switch x := val.Interface().(type) {
case time.Time:
if x.IsZero() {
return nil, nil
}
return x.Unix(), nil
case bool:
if x {
return 1, nil
}
return 0, nil
}
return nil, fmt.Errorf("can't transform type %v", val.Type())
}
func (b *Backend) canAddField(f *sql.Field) bool {
// These are a supeset of the actual resctrictions, for
// simplicity. See https://www.sqlite.org/lang_altertable.html
// for more details.
return f.Constraint(sql.ConstraintPrimaryKey) != nil && f.Constraint(sql.ConstraintUnique) == nil && f.Default == ""
}
func sqliteOpener(url *config.URL) (driver.Driver, error) {
drv, err := sql.NewDriver(sqliteBackend, url)
if err == nil {
if _, err := drv.DB().Exec("PRAGMA foreign_keys = on"); err != nil {
return nil, err
}
}
return drv, err
}
func init() {
driver.Register("sqlite", sqliteOpener)
driver.Register("sqlite3", sqliteOpener)
}