-
Notifications
You must be signed in to change notification settings - Fork 1
/
cursor.go
95 lines (85 loc) · 1.84 KB
/
cursor.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
package sysql
import (
"github.com/dean2021/go-sqlite3"
"github.com/dean2021/sysql/table"
"reflect"
)
type Cursor struct {
index int
rows table.TableRows
TablePlugin table.Table
Constraints table.Constraints
}
func (vc *Cursor) Column(c *sqlite3.SQLiteContext, col int) error {
columns := vc.TablePlugin.Columns()
value := vc.rows[vc.index][columns[col].Name]
if value != nil {
switch value.(type) {
case string:
c.ResultText(value.(string))
case uint64:
c.ResultInt64(int64(value.(uint64)))
case uint32:
c.ResultInt(int(value.(uint32)))
case uint16:
c.ResultInt(int(value.(uint16)))
case uint8:
c.ResultInt(int(value.(uint8)))
case uint:
c.ResultInt(int(value.(uint)))
case int64:
c.ResultInt64(value.(int64))
case int32:
c.ResultInt(int(value.(int32)))
case int16:
c.ResultInt(int(value.(int16)))
case int8:
c.ResultInt(int(value.(int8)))
case int:
c.ResultInt(value.(int))
case float64:
c.ResultDouble(value.(float64))
case bool:
c.ResultBool(value.(bool))
case []byte:
c.ResultBlob(value.([]byte))
default:
panic(reflect.TypeOf(value).Kind().String() + " type is not supported")
}
} else {
c.ResultNull()
}
return nil
}
func (vc *Cursor) Filter(idxNum int, idxStr string, vals []interface{}) error {
if len(vals) > 0 {
for i, expr := range vals {
vc.Constraints[i].Expr = expr
}
} else {
vc.Constraints = nil
}
var err error
var context = &table.QueryContext{
Constraints: vc.Constraints,
}
vc.rows, err = vc.TablePlugin.Generate(context)
if err != nil {
return err
}
vc.index = 0
return nil
}
func (vc *Cursor) Next() error {
vc.index++
return nil
}
func (vc *Cursor) EOF() bool {
return vc.index >= len(vc.rows)
}
func (vc *Cursor) Rowid() (int64, error) {
return int64(vc.index), nil
}
func (vc *Cursor) Close() error {
return nil
}