-
Notifications
You must be signed in to change notification settings - Fork 159
/
sqlite.go
171 lines (142 loc) · 3.35 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
package main
import (
"database/sql"
"encoding/json"
"fmt"
"strings"
"github.com/multiprocessio/datastation/runner"
)
type SQLiteResultItemWriter struct {
db *sql.DB
fields []string
panelId string
rowBuffer runner.Vector[any]
convertNumbers bool
}
func openSQLiteResultItemWriter(f string, panelId string, convertNumbers bool) (runner.ResultItemWriter, error) {
var sw SQLiteResultItemWriter
sw.panelId = panelId
sw.convertNumbers = convertNumbers
sw.rowBuffer = runner.Vector[any]{}
var err error
sw.db, err = sql.Open("sqlite3_extended", f)
if err != nil {
return nil, err
}
return &sw, nil
}
func (sw *SQLiteResultItemWriter) createTable() error {
fieldType := "TEXT"
if sw.convertNumbers {
fieldType = "NUMERIC"
}
var columns []string
for _, field := range sw.fields {
columns = append(columns, `"`+field+`" `+fieldType)
}
create := "CREATE TABLE \"" + sw.panelId + "\"(" + strings.Join(columns, ", ") + ");"
_, err := sw.db.Exec(create)
return err
}
func (sw *SQLiteResultItemWriter) makeQuery(rows int) string {
var query strings.Builder
query.WriteString("INSERT INTO \"" + sw.panelId + "\" VALUES ")
for i := 0; i < rows; i++ {
if i > 0 {
query.WriteString(", ")
}
query.WriteByte('(')
for i := range sw.fields {
if i > 0 {
query.WriteString(", ")
}
query.WriteByte('?')
}
query.WriteByte(')')
}
return query.String()
}
func (sw *SQLiteResultItemWriter) flush() error {
rowsInBatch := 100
query := sw.makeQuery(rowsInBatch)
stmt, err := sw.db.Prepare(query)
if err != nil {
return err
}
rows := sw.rowBuffer.Index() / len(sw.fields)
args := sw.rowBuffer.List()
var leftover []any
batchArgs := make([]any, rowsInBatch*len(sw.fields))
for i := 0; i < rows; i += rowsInBatch {
if i+rowsInBatch > rows {
leftover = make([]any, (rows-i)*len(sw.fields))
copy(leftover, args[i*len(sw.fields):])
break
}
copy(batchArgs, args[i*len(sw.fields):(i+rowsInBatch)*len(sw.fields)])
_, err = stmt.Exec(batchArgs...)
if err != nil {
return err
}
}
err = stmt.Close()
if err != nil {
return err
}
if len(leftover) > 0 {
remainingRows := len(leftover) / len(sw.fields)
_, err := sw.db.Exec(sw.makeQuery(remainingRows), leftover...)
if err != nil {
return err
}
}
sw.rowBuffer.Reset()
return nil
}
func (sw *SQLiteResultItemWriter) WriteRow(r any, written int) error {
m, ok := r.(map[string]any)
if !ok {
return fmt.Errorf("Row must be a map, got: %#v", r)
}
if len(sw.fields) == 0 {
for key := range m {
sw.fields = append(sw.fields, key)
}
err := sw.createTable()
if err != nil {
return err
}
}
for _, field := range sw.fields {
v := m[field]
switch t := v.(type) {
case []any:
bs, err := json.Marshal(t)
if err != nil {
return err
}
v = string(bs)
}
sw.rowBuffer.Append(v)
}
// Flush data
if written > 0 && written%10000 == 0 {
return sw.flush()
}
return nil
}
func (sw *SQLiteResultItemWriter) SetNamespace(key string) error {
return fmt.Errorf("SetNamespace unimplemented")
}
func (sw *SQLiteResultItemWriter) Shape(id string, maxBytesToRead, sampleSize int) (*runner.Shape, error) {
return nil, fmt.Errorf("Shape unimplemented")
}
func (sw *SQLiteResultItemWriter) Close() error {
if sw.rowBuffer.Index() > 0 {
err := sw.flush()
if err != nil {
return err
}
}
return sw.db.Close()
}