forked from araddon/qlbridge
/
conn.go
352 lines (311 loc) · 9.85 KB
/
conn.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// Package sqlite implements a Qlbridge Datasource interface around sqlite
// that translates mysql syntax to sqlite.
package sqlite
import (
"database/sql"
"database/sql/driver"
"fmt"
"strings"
u "github.com/araddon/gou"
"github.com/dchest/siphash"
"github.com/google/btree"
"golang.org/x/net/context"
// Import driver for sqlite
_ "github.com/mattn/go-sqlite3"
"github.com/fuhongbo/qlbridge/datasource"
"github.com/fuhongbo/qlbridge/exec"
"github.com/fuhongbo/qlbridge/expr"
"github.com/fuhongbo/qlbridge/plan"
"github.com/fuhongbo/qlbridge/rel"
"github.com/fuhongbo/qlbridge/schema"
)
var (
// ensure our conn implements connection features
_ schema.ConnAll = (*qryconn)(nil)
_ schema.ConnMutation = (*qryconn)(nil)
// SourcePlanner interface {
// // given our request statement, turn that into a plan.Task.
// WalkSourceSelect(pl Planner, s *Source) (Task, error)
// }
_ plan.SourcePlanner = (*qryconn)(nil)
)
type (
// qryconn is a single-query connection in order to manage
// stateful, non-multi-threaded access to sqlite rows object.
qryconn struct {
*exec.TaskBase
stmt rel.SqlStatement
exit <-chan bool
source *Source
tbl *schema.Table
ps *plan.Source
indexCol int
rows *sql.Rows
ct uint64
cols []string
colidx map[string]int
err error
sqlInsert string
sqlUpdate string
}
)
func newQueryConn(tbl *schema.Table, source *Source) *qryconn {
m := qryconn{
tbl: tbl,
cols: tbl.Columns(),
source: source,
}
m.init()
return &m
}
func (m *qryconn) init() {
cols := make([]string, len(m.cols))
vals := make([]string, len(m.cols))
for i, col := range m.cols {
cols[i] = expr.IdentityMaybeQuote('"', col)
vals[i] = "?"
}
m.sqlInsert = fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s);", m.tbl.Name, strings.Join(cols, ", "), strings.Join(vals, ", "))
}
// Close the qryconn. Since sqlite is a NON-threadsafe db, this is very important
// as we actually hold a lock per-table during scans to prevent conflict.
func (m *qryconn) Close() error {
defer m.source.mu.Unlock()
delete(m.source.qryconns, m.tbl.Name)
if m.rows != nil {
if err := m.rows.Close(); err != nil {
return err
}
}
return nil
}
// CreateIterator creates an interator to page through each row in this query resultset.
// This qryconn is wrapping a sql rows object, paging through until empty.
func (m *qryconn) CreateIterator() schema.Iterator { return m }
// Columns gets the columns used in this query.
func (m *qryconn) Columns() []string { return m.cols }
//func (m *qryconn) Length() int { return 0 }
//func (m *conn) SetColumns(cols []string) { m.tbl.SetColumns(cols) }
// CreateMutator part of Mutator interface to allow this connection to have access
// to the full plan context to take original sql statement and pass through to sqlite.
func (m *qryconn) CreateMutator(pc interface{}) (schema.ConnMutator, error) {
if ctx, ok := pc.(*plan.Context); ok && ctx != nil {
m.TaskBase = exec.NewTaskBase(ctx)
m.stmt = ctx.Stmt
return m, nil
}
return nil, fmt.Errorf("Expected *plan.Context but got %T", pc)
}
func (m *qryconn) Next() schema.Message {
if m.rows == nil {
m.err = fmt.Errorf("wtf missing rows")
u.Errorf("could not find rows")
return nil
}
select {
case <-m.exit:
return nil
default:
for {
if !m.rows.Next() {
return nil
}
//vals := make([]driver.Value, len(m.cols))
//u.Infof("expecting %d cols", len(m.cols))
readCols := make([]interface{}, len(m.cols))
writeCols := make([]driver.Value, len(m.cols))
for i := range writeCols {
readCols[i] = &writeCols[i]
}
//cols, _ := m.rows.Columns()
//u.Debugf("sqlite result cols provides %v but expecting %d", cols, len(m.cols))
m.err = m.rows.Scan(readCols...)
if m.err != nil {
u.Warnf("err=%v", m.err)
return nil
}
//u.Debugf("read vals: %#v", writeCols)
// This seems pretty gross, isn't there a better way to do this?
for i, col := range writeCols {
//u.Debugf("%d %s %T %v", i, m.cols[i], col, col)
switch val := col.(type) {
case []uint8:
writeCols[i] = driver.Value(string(val))
}
}
msg := datasource.NewSqlDriverMessageMap(m.ct, writeCols, m.colidx)
m.ct++
//u.Infof("return item btreeP:%p itemP:%p cursorP:%p %v %v", m, item, m.cursor, msg.Id(), msg.Values())
//u.Debugf("return? %T %v", item, item.(*DriverItem).SqlDriverMessageMap)
return msg
}
}
}
// Put interface for Upsert.Put() to do single row insert based on key.
func (m *qryconn) Put(ctx context.Context, key schema.Key, row interface{}) (schema.Key, error) {
//u.Infof("%p Put(), row:%#v", m, row)
switch rowVals := row.(type) {
case []driver.Value:
if len(rowVals) != len(m.Columns()) {
u.Warnf("wrong column ct")
return nil, fmt.Errorf("Wrong number of columns, got %v expected %v", len(rowVals), len(m.Columns()))
}
id := MakeId(rowVals[m.indexCol])
row := m.source.db.QueryRow(fmt.Sprintf("SELECT * FROM %v WHERE %s = $1", m.tbl.Name, m.cols[0]), rowVals[m.indexCol])
vals := make([]driver.Value, len(m.cols))
if err := row.Scan(&vals); err != nil && err != sql.ErrNoRows {
u.Warnf("could not get current? %v", err)
return nil, err
} else if err == sql.ErrNoRows {
//u.Debugf("empty, now do insert")
//sdm := datasource.NewSqlDriverMessageMap(id, rowVals, m.tbl.FieldPositions)
ivals := make([]interface{}, len(rowVals))
for i, v := range rowVals {
ivals[i] = v
}
_, err = m.source.db.Exec(m.sqlInsert, ivals...)
if err != nil {
u.Warnf("wtf %v", err)
}
//u.Debugf("%p PUT: id:%v IdVal:%v Id():%v vals:%#v", m, id, sdm.IdVal, sdm.Id(), rowVals)
} else {
u.Debugf("found current? %v", vals)
sdm := datasource.NewSqlDriverMessageMap(id, rowVals, m.tbl.FieldPositions)
_, err = m.source.db.Exec(m.stmt.String(), nil)
if err != nil {
u.Warnf("wtf %v", err)
}
u.Debugf("%p PUT: id:%v IdVal:%v Id():%v vals:%#v", m, id, sdm.IdVal, sdm.Id(), rowVals)
}
return NewKey(id), nil
default:
u.Warnf("not implemented %T", row)
return nil, fmt.Errorf("Expected []driver.Value but got %T", row)
}
}
func (m *qryconn) PutMulti(ctx context.Context, keys []schema.Key, src interface{}) ([]schema.Key, error) {
return nil, fmt.Errorf("not implemented")
}
// Get a single row by key.
func (m *qryconn) Get(key driver.Value) (schema.Message, error) {
row := m.source.db.QueryRow(fmt.Sprintf("SELECT * FROM %v WHERE %s = $1", m.tbl.Name, m.cols[0]), key)
vals := make([]driver.Value, len(m.cols))
if err := row.Scan(&vals); err != nil {
return nil, err
}
return datasource.NewSqlDriverMessageMap(0, vals, m.colidx), nil
}
// Delete deletes a single row by key
func (m *qryconn) Delete(key driver.Value) (int, error) {
return -1, schema.ErrNotImplemented
}
// WalkSourceSelect An interface implemented by this connection allowing the planner
// to push down as much sql logic down to sqlite.
func (m *qryconn) WalkSourceSelect(planner plan.Planner, p *plan.Source) (plan.Task, error) {
sqlSelect := p.Stmt.Source
u.Infof("original %s", sqlSelect.String())
p.Stmt.Source = nil
p.Stmt.Rewrite(sqlSelect)
sqlSelect = p.Stmt.Source
u.Infof("original after From(source) rewrite %s", sqlSelect.String())
sqlSelect.RewriteAsRawSelect()
m.cols = sqlSelect.Columns.UnAliasedFieldNames()
m.colidx = sqlSelect.ColIndexes()
sqlString, _ := newRewriter(sqlSelect).rewrite()
u.Infof("after sqlite-rewrite %s", sqlSelect.String())
u.Infof("pushdown sql: %s", sqlString)
rows, err := m.source.db.Query(sqlString)
if err != nil {
u.Errorf("could not open master err=%v", err)
return nil, err
}
m.rows = rows
m.TaskBase = exec.NewTaskBase(p.Context())
p.SourceExec = true
m.ps = p
//p.Complete = true
return nil, nil
}
// DeleteExpression Delete using a Where Expression
func (m *qryconn) DeleteExpression(p interface{}, where expr.Node) (int, error) {
return -1, schema.ErrNotImplemented
/*
_, ok := p.(*plan.Delete)
if !ok {
return 0, plan.ErrNoPlan
}
deletedKeys := make([]*Key, 0)
m.bt.Ascend(func(a btree.Item) bool {
di, ok := a.(*DriverItem)
if !ok {
u.Warnf("wat? %T %#v", a, a)
return false
}
msgCtx := di.SqlDriverMessageMap
whereValue, ok := vm.Eval(msgCtx, where)
if !ok {
u.Debugf("could not evaluate where: %v", msgCtx.Values())
//return deletedCt, fmt.Errorf("Could not evaluate where clause")
return true
}
switch whereVal := whereValue.(type) {
case value.BoolValue:
if whereVal.Val() == false {
//this means do NOT delete
} else {
// Delete!
indexVal := msgCtx.Values()[m.indexCol]
deletedKeys = append(deletedKeys, NewKey(makeId(indexVal)))
}
case nil:
// ??
default:
if whereVal.Nil() {
// Doesn't match, so don't delete
} else {
u.Warnf("unknown type? %T", whereVal)
}
}
return true
})
for _, deleteKey := range deletedKeys {
if ct, err := m.Delete(deleteKey); err != nil {
u.Errorf("Could not delete key: %v", deleteKey)
} else if ct != 1 {
u.Errorf("delete should have removed 1 key %v", deleteKey)
}
}
return len(deletedKeys), nil
*/
}
func MakeId(dv driver.Value) uint64 {
switch vt := dv.(type) {
case int:
return uint64(vt)
case int64:
return uint64(vt)
case []byte:
return siphash.Hash(456729, 1111581582, vt)
case string:
return siphash.Hash(456729, 1111581582, []byte(vt))
//by := append(make([]byte,0,8), byte(r), byte(r>>8), byte(r>>16), byte(r>>24), byte(r>>32), byte(r>>40), byte(r>>48), byte(r>>56))
case datasource.KeyCol:
return MakeId(vt.Val)
}
return 0
}
// Key implements Key and Sort interfaces.
type Key struct {
Id uint64
}
func NewKey(key uint64) *Key { return &Key{key} }
func (m *Key) Key() driver.Value { return driver.Value(m.Id) }
func (m *Key) Less(than btree.Item) bool {
switch it := than.(type) {
case *Key:
return m.Id < it.Id
default:
u.Warnf("what type? %T", than)
}
return false
}