-
Notifications
You must be signed in to change notification settings - Fork 101
/
sqlite3.go
211 lines (202 loc) · 5.35 KB
/
sqlite3.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
package qbs
import (
"database/sql"
"reflect"
"time"
"unsafe"
)
type sqlite3 struct {
base
}
func NewSqlite3() Dialect {
d := new(sqlite3)
d.base.dialect = d
return d
}
func RegisterSqlite3(dbFileName string) {
dsn := new(DataSourceName)
dsn.DbName = dbFileName
dsn.Dialect = NewSqlite3()
RegisterWithDataSourceName(dsn)
}
func (d sqlite3) sqlType(field modelField) string {
f := field.value
fieldValue := reflect.ValueOf(f)
kind := fieldValue.Kind()
if field.nullable != reflect.Invalid {
kind = field.nullable
}
switch kind {
case reflect.Bool:
return "integer"
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return "integer"
case reflect.Float32, reflect.Float64:
return "real"
case reflect.String:
return "text"
case reflect.Slice:
if reflect.TypeOf(f).Elem().Kind() == reflect.Uint8 {
return "text"
}
case reflect.Struct:
switch fieldValue.Interface().(type) {
case time.Time:
return "text"
case sql.NullBool:
return "integer"
case sql.NullInt64:
return "integer"
case sql.NullFloat64:
return "real"
case sql.NullString:
return "text"
default:
if len(field.colType) != 0 {
switch field.colType {
case QBS_COLTYPE_INT:
return "integer"
case QBS_COLTYPE_BIGINT:
return "integer"
case QBS_COLTYPE_BOOL:
return "integer"
case QBS_COLTYPE_TIME:
return "text"
case QBS_COLTYPE_DOUBLE:
return "real"
case QBS_COLTYPE_TEXT:
return "text"
default:
panic("Qbs doesn't support column type " + field.colType + "for SQLite3")
}
}
}
}
panic("invalid sql type for field:" + field.name)
}
func (d sqlite3) setModelValue(value reflect.Value, field reflect.Value) error {
switch field.Type().Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
field.SetInt(value.Elem().Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
// reading uint from int value causes panic
switch value.Elem().Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
field.SetUint(uint64(value.Elem().Int()))
default:
field.SetUint(value.Elem().Uint())
}
case reflect.Bool:
if value.Elem().Int() == 0 {
field.SetBool(false)
} else {
field.SetBool(true)
}
case reflect.Float32, reflect.Float64:
field.SetFloat(value.Elem().Float())
case reflect.String:
if value.Elem().Kind() == reflect.Slice {
field.SetString(string(value.Elem().Bytes()))
} else {
field.SetString(value.Elem().String())
}
case reflect.Slice:
if reflect.TypeOf(value.Interface()).Elem().Kind() == reflect.Uint8 {
field.SetBytes(value.Elem().Bytes())
}
case reflect.Ptr:
d.setPtrValue(value, field)
case reflect.Struct:
switch field.Interface().(type) {
case time.Time:
var t time.Time
var err error
switch value.Elem().Kind() {
case reflect.String:
t, err = time.Parse("2006-01-02 15:04:05", value.Elem().String())
if err != nil {
return err
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
t = time.Unix(value.Elem().Int(), 0)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
t = time.Unix(int64(value.Elem().Uint()), 0)
case reflect.Slice:
t, err = time.Parse("2006-01-02 15:04:05", string(value.Elem().Bytes()))
if err != nil {
return err
}
}
v := reflect.NewAt(reflect.TypeOf(time.Time{}), unsafe.Pointer(&t))
field.Set(v.Elem())
case sql.NullBool:
b := true
if value.Elem().Int() == 0 {
b = false
}
field.Set(reflect.ValueOf(sql.NullBool{b, true}))
case sql.NullFloat64:
if f, ok := value.Elem().Interface().(float64); ok {
field.Set(reflect.ValueOf(sql.NullFloat64{f, true}))
}
case sql.NullInt64:
if i, ok := value.Elem().Interface().(int64); ok {
field.Set(reflect.ValueOf(sql.NullInt64{i, true}))
}
case sql.NullString:
str := string(value.Elem().String())
field.Set(reflect.ValueOf(sql.NullString{str, true}))
}
}
return nil
}
func (d sqlite3) indexExists(mg *Migration, tableName string, indexName string) bool {
query := "PRAGMA index_list('" + tableName + "')"
rows, err := mg.db.Query(query)
if err != nil {
panic(err)
}
defer rows.Close()
for rows.Next() {
var c0, c1, c2 string
rows.Scan(&c0, &c1, &c2)
if c1 == indexName {
return true
}
}
return false
}
func (d sqlite3) columnsInTable(mg *Migration, table interface{}) map[string]bool {
tn := tableName(table)
columns := make(map[string]bool)
query := "PRAGMA table_info('" + tn + "')"
rows, err := mg.db.Query(query)
if err != nil {
panic(err)
}
defer rows.Close()
for rows.Next() {
cols, _ := rows.Columns()
containers := make([]interface{}, 0, len(cols))
for i := 0; i < cap(containers); i++ {
var v interface{}
containers = append(containers, &v)
}
err = rows.Scan(containers...)
value := reflect.Indirect(reflect.ValueOf(containers[1]))
if err == nil {
if value.Elem().Kind() == reflect.Slice {
columns[string(value.Elem().Bytes())] = true
} else {
columns[value.Elem().String()] = true
}
}
}
return columns
}
func (d sqlite3) primaryKeySql(isString bool, size int) string {
if isString {
return "text PRIMARY KEY NOT NULL"
}
return "integer PRIMARY KEY AUTOINCREMENT NOT NULL"
}