-
Notifications
You must be signed in to change notification settings - Fork 14
/
driver.go
107 lines (86 loc) · 1.71 KB
/
driver.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
package goql
import (
"database/sql"
drv "database/sql/driver"
"fmt"
"io"
"github.com/fzerorubigd/goql/astdata"
"github.com/fzerorubigd/goql/parse"
)
type driver struct{}
type conn struct {
pkg *astdata.Package
}
type stmt struct {
pkg *astdata.Package
query *parse.Query
}
type rows struct {
cursor int
rows []string
data [][]Getter
}
func (driver) Open(name string) (drv.Conn, error) {
p, err := astdata.ParsePackage(name)
if err != nil {
return nil, err
}
return &conn{pkg: p}, nil
}
func (gc *conn) Prepare(query string) (drv.Stmt, error) {
st, err := parse.AST(query)
if err != nil {
return nil, err
}
return &stmt{pkg: gc.pkg, query: st}, nil
}
func (gc *conn) Close() error {
return nil
}
func (gc *conn) Begin() (drv.Tx, error) {
return nil, fmt.Errorf("not supported")
}
func (gs *stmt) Close() error {
return nil
}
func (gs *stmt) NumInput() int {
return gs.query.Statement.ParamCount()
}
func (gs *stmt) Exec(args []drv.Value) (drv.Result, error) {
return nil, fmt.Errorf("currently only select via query is supported")
}
func (gs *stmt) Query(args []drv.Value) (drv.Rows, error) {
var err error
params := make([]interface{}, len(args))
for i := range args {
params[i] = args[i]
}
r := &rows{}
r.rows, r.data, err = execute(gs.pkg, gs.query, params...)
if err != nil {
return nil, err
}
return r, nil
}
func (r *rows) Columns() []string {
return r.rows
}
func (r *rows) Close() error {
r.data = nil
r.rows = nil
return nil
}
func (r *rows) Next(dest []drv.Value) error {
if r.cursor >= len(r.data) {
return io.EOF
}
for i := range dest {
in := r.data[r.cursor][i].Get()
dest[i] = in
}
r.cursor++
return nil
}
func init() {
sql.Register("goql", &driver{})
}