-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlite.go
213 lines (183 loc) · 5.22 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
package loaders
import (
"regexp"
"strings"
_ "github.com/mattn/go-sqlite3"
"github.com/sundayfun/xo/internal"
"github.com/sundayfun/xo/models"
)
func init() {
internal.SchemaLoaders["sqlite3"] = internal.TypeLoader{
ProcessRelkind: SqRelkind,
ParamN: func(int) string { return "?" },
MaskFunc: func() string { return "?" },
ParseType: SqParseType,
TableList: SqTables,
ColumnList: SqTableColumns,
ForeignKeyList: func(db models.XODB, schema string, table string) ([]*models.ForeignKey, error) {
return models.SqTableForeignKeys(db, table)
},
IndexList: func(db models.XODB, schema string, table string) ([]*models.Index, error) {
return models.SqTableIndexes(db, table)
},
IndexColumnList: func(db models.XODB, schema string, table string, index string) ([]*models.IndexColumn, error) {
return models.SqIndexColumns(db, index)
},
QueryColumnList: SqQueryColumns,
}
}
// SqRelkind returns the sqlite string representation for RelType.
func SqRelkind(relType internal.RelType) string {
var s string
switch relType {
case internal.Table:
s = "table"
case internal.View:
s = "view"
default:
panic("unsupported RelType")
}
return s
}
var uRE = regexp.MustCompile(`\s*unsigned\*`)
// SqParseType parse a sqlite type into a Go type based on the column
// definition.
func SqParseType(args *internal.ArgType, dt string, nullable bool) (int, string, string) {
precision := 0
nilVal := "nil"
unsigned := false
dt = strings.ToLower(dt)
// extract precision
dt, precision, _ = args.ParsePrecision(dt)
if uRE.MatchString(dt) {
unsigned = true
uRE.ReplaceAllString(dt, "")
}
var typ string
switch dt {
case "bool", "boolean":
nilVal = "false"
typ = "bool"
if nullable {
nilVal = "sql.NullBool{}"
typ = "sql.NullBool"
}
case "int", "integer", "tinyint", "smallint", "mediumint", "bigint":
nilVal = "0"
typ = args.Int32Type
if nullable {
nilVal = "sql.NullInt64{}"
typ = "sql.NullInt64"
}
case "numeric", "real", "double", "float", "decimal":
nilVal = "0.0"
typ = "float64"
if nullable {
nilVal = "sql.NullFloat64{}"
typ = "sql.NullFloat64"
}
case "blob":
typ = "[]byte"
case "timestamp", "datetime", "date", "timestamp with time zone", "time with time zone", "time without time zone", "timestamp without time zone":
nilVal = "xoutil.SqTime{}"
typ = "xoutil.SqTime"
default:
// case "varchar", "character", "varying character", "nchar", "native character", "nvarchar", "text", "clob", "datetime", "date", "time":
nilVal = `""`
typ = "string"
if nullable {
nilVal = "sql.NullString{}"
typ = "sql.NullString"
}
}
// if unsigned ...
if internal.IntRE.MatchString(typ) && unsigned {
typ = "u" + typ
}
return precision, nilVal, typ
}
// SqTables returns the sqlite tables with the manual PK information added.
// ManualPk is true when the table's primary key is not autoincrement.
func SqTables(db models.XODB, schema string, relkind string) ([]*models.Table, error) {
var err error
// get the tables
rows, err := models.SqTables(db, relkind)
if err != nil {
return nil, err
}
// get the SQL for the Autoincrement detection
autoIncrements, err := models.SqAutoIncrements(db)
if err != nil {
// Set it to an empty set on error.
autoIncrements = []*models.SqAutoIncrement{}
}
// Add information about manual FK.
var tables []*models.Table
for _, row := range rows {
manualPk := true
// Look for a match in the table name where it contains the autoincrement
// keyword for the given table in the SQL.
for _, autoInc := range autoIncrements {
lSQL := strings.ToLower(autoInc.SQL)
if autoInc.TableName == row.TableName && strings.Contains(lSQL, "autoincrement") {
manualPk = false
} else {
cols, err := SqTableColumns(db, schema, row.TableName)
if err != nil {
return nil, err
}
for _, col := range cols {
if col.IsPrimaryKey == true {
dt := strings.ToUpper(col.DataType)
if dt == "INTEGER" {
manualPk = false
}
break
}
}
}
}
tables = append(tables, &models.Table{
TableName: row.TableName,
Type: row.Type,
ManualPk: manualPk,
})
}
return tables, nil
}
// SqTableColumns returns the sqlite table column info.
func SqTableColumns(db models.XODB, schema string, table string) ([]*models.Column, error) {
var err error
// grab
rows, err := models.SqTableColumns(db, table)
if err != nil {
return nil, err
}
// fix columns
var cols []*models.Column
for _, row := range rows {
cols = append(cols, &models.Column{
FieldOrdinal: row.FieldOrdinal,
ColumnName: row.ColumnName,
DataType: row.DataType,
NotNull: row.NotNull,
DefaultValue: row.DefaultValue,
IsPrimaryKey: row.PkColIndex != 0,
})
}
return cols, nil
}
// SqQueryColumns parses a sqlite query and generates a type for it.
func SqQueryColumns(args *internal.ArgType, inspect []string) ([]*models.Column, error) {
var err error
// create temporary view xoid
xoid := "_xo_" + internal.GenRandomID()
viewq := `CREATE TEMPORARY VIEW ` + xoid + ` AS ` + strings.Join(inspect, "\n")
models.XOLog(viewq)
_, err = args.DB.Exec(viewq)
if err != nil {
return nil, err
}
// load column information
return SqTableColumns(args.DB, "", xoid)
}