-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlite_writer.go
318 lines (266 loc) · 7.23 KB
/
sqlite_writer.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package peanut
import (
"database/sql"
"os"
"reflect"
"strings"
// Import Sqlite db driver.
_ "github.com/mattn/go-sqlite3"
)
// TODO(js) Add an option to allow different insert modes (default/ignore/update).
// TODO(js) We should not be silently overwriting things. We should return an error somehow.
// SQLiteWriter writes records to an SQLite database,
// writing each record type to an individual table
// automatically.
//
// During writing, the database file is held in a
// temporary location, and only moved into its
// final destination during a successful Close operation.
//
// Note that if an existing database with the same filename
// already exists at the given output location,
// it will be silently overwritten.
//
// The caller must call Close on successful completion
// of all writing, to ensure proper cleanup, and the
// relocation of the database from its temporary
// location during writing, to its final output.
//
// In the event of an error or cancellation, the
// caller must call Cancel before quiting, to ensure
// closure and cleanup of any partially written data.
//
// SQLiteWriter supports additional tag values to denote the primary key:
// type Shape struct {
// ShapeID string `peanut:"shape_id,pk"`
// Name string `peanut:"name"`
// NumSides int `peanut:"num_sides"`
// }
//
// type Color struct {
// ColorID string `peanut:"color_id,pk"`
// Name string `peanut:"name"`
// RBG string `peanut:"rgb"`
// }
// Compound primary keys are also supported.
//
// SQLiteWriter has no support for foreign keys, indexes, etc.
type SQLiteWriter struct {
*base
tmpFilename string // tmpFilename is the filename used by the temp file.
dstFilename string // dstFilename is the final destination filename.
insertByType map[reflect.Type]*sql.Stmt // insertByType holds prepared INSERT statements.
db *sql.DB // db is the database instance.
}
// TODO(js) Can we unify/simplify the constructors? Use pattern instead of prefix/suffix maybe? (not here, but for others)
// NewSQLiteWriter returns a new SQLiteWriter,
// using the given filename + ".sqlite" as its final output location.
func NewSQLiteWriter(filename string) *SQLiteWriter {
w := SQLiteWriter{
base: &base{},
dstFilename: filename + ".sqlite",
insertByType: make(map[reflect.Type]*sql.Stmt),
}
return &w
}
func (w *SQLiteWriter) register(x interface{}) (reflect.Type, error) {
// Register with base writer.
t, ok := w.base.register(x)
if !ok {
return t, nil
}
if err := allFieldsSupportedKinds(x); err != nil {
return nil, err
}
if len(w.base.tagsByType[t]) == 0 {
return t, nil
}
// Lazy init of database.
if w.db == nil {
filename, err := randomTempFilename("peanut-", ".sqlite")
if err != nil {
return nil, err
}
// log.Printf("Creating SQLite db %s", filename)
db, err := sql.Open("sqlite3", filename)
if err != nil {
return nil, err
}
w.db = db
w.tmpFilename = filename
}
// log.Printf("Setting up SQLite table for %s", t.Name())
ddl := w.createDDL(t)
// log.Println("DDL:", ddl)
// Execute DDL to create table.
_, err := w.db.Exec(ddl)
if err != nil {
return nil, err
}
insert := w.createInsert(t)
// log.Println("Insert:", insert)
// Create and cache prepared statement.
stmt, err := w.db.Prepare(insert)
if err != nil {
return nil, err
}
w.insertByType[t] = stmt
return t, nil
}
var kindToDBType = map[reflect.Kind]string{
reflect.String: "TEXT",
reflect.Bool: "BOOLEAN",
reflect.Float64: "REAL",
reflect.Float32: "REAL",
reflect.Int8: "INT8",
reflect.Int16: "INT16",
reflect.Int32: "INT32",
reflect.Int64: "INT64",
reflect.Int: "INT64",
reflect.Uint8: "UNSIGNED INT8",
reflect.Uint16: "UNSIGNED INT16",
reflect.Uint32: "UNSIGNED INT32",
reflect.Uint64: "UNSIGNED INT64",
reflect.Uint: "UNSIGNED INT64",
}
func (w *SQLiteWriter) createDDL(t reflect.Type) string {
// Create table using type name - quoted.
ddl := "CREATE TABLE \"" + t.Name() + "\" (\n"
// List of DDL statements to build the table definition.
var ddlLines []string
// List of primary keys.
var pks []string
hdrs := w.headersByType[t]
typs := w.typesByType[t]
tags := w.tagsByType[t]
for i := 0; i < len(typs); i++ {
// Column name - quoted.
col := "\t\"" + hdrs[i] + "\" "
// Column datatype.
col += kindToDBType[typs[i].Kind()]
// We ensure kindToDBType has necessary entries using a test,
// so no need to check for missing entries here.
// Column constraints.
col += " NOT NULL"
// Add DDL line to list.
ddlLines = append(ddlLines, col)
// Handle primary key tag.
if secondTagValue(tags[i]) == "pk" {
// Add column name to primary key list.
pks = append(pks, hdrs[i])
}
}
// Primary key.
if len(pks) > 0 {
pk := "PRIMARY KEY ("
pk += strings.Join(pks, ", ")
pk += ")"
ddlLines = append(ddlLines, pk)
}
// Join the lines of DDL together.
ddl += strings.Join(ddlLines, ",\n")
ddl += "\n)"
return ddl
}
func (w *SQLiteWriter) createInsert(t reflect.Type) string {
// TODO(js) Add an option to allow different insert modes (default/ignore/update).
s := "INSERT OR IGNORE INTO \"" + t.Name() + "\" ("
hdrs := w.headersByType[t]
s += strings.Join(hdrs, ",")
s += ") VALUES ("
q := make([]string, len(hdrs))
for i := 0; i < len(q); i++ {
q[i] = "?"
}
s += strings.Join(q, ",")
s += ")"
return s
}
// Write is called to persist records.
// Each record is written to an individual row
// in the corresponding table within the output database,
// according to the type of the given record.
func (w *SQLiteWriter) Write(x interface{}) error {
if w.closed {
return ErrClosedWriter
}
t, err := w.register(x)
if err != nil {
return err
}
if len(w.base.tagsByType[t]) == 0 {
return nil
}
// log.Printf("WriteRecord for %s", t.Name())
stmt := w.insertByType[t]
_, err = stmt.Exec(excelValuesFrom(x)...)
return err
}
// Close cleans up all used resources,
// closes the database connection,
// and moves the database to its final location.
//
// Calling Close after a previous call to
// Cancel is safe, and always results in a no-op.
func (w *SQLiteWriter) Close() error {
if w.closed {
return nil
}
w.closed = true
if w.db == nil {
return nil
}
var rerr error
// TODO(js) We should make lists of errors.
err := w.close()
if err != nil {
rerr = err
}
err = os.Rename(w.tmpFilename, w.dstFilename)
if err != nil {
rerr = err
}
return rerr
}
func (w *SQLiteWriter) close() error {
var rerr error
// TODO(js) We should make lists of errors.
for _, stmt := range w.insertByType {
var cerr error
var err error
err = stmt.Close()
if err != nil {
cerr = err
}
if cerr != nil {
rerr = cerr
}
}
err := w.db.Close()
if err != nil {
rerr = err
}
return rerr
}
// Cancel should be called in the event of an error occurring,
// to properly close any used resources,
// and delete the partially written database from its temporary location.
func (w *SQLiteWriter) Cancel() error {
if w.closed {
return nil
}
w.closed = true
if w.db == nil {
return nil
}
var rerr error
// TODO(js) We should make lists of errors.
rerr = w.close()
if w.db != nil {
err := os.Remove(w.tmpFilename)
if err != nil {
rerr = err
}
}
return rerr
}