-
Notifications
You must be signed in to change notification settings - Fork 36
/
sql.go
171 lines (144 loc) · 3.59 KB
/
sql.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 stdlib provides a compatability layer from gorqlite to database/sql.
package stdlib
import (
"context"
"database/sql"
"database/sql/driver"
"fmt"
"io"
"github.com/rqlite/gorqlite"
)
func init() {
sql.Register("rqlite", &Driver{})
}
type Driver struct{}
func (d *Driver) Open(name string) (driver.Conn, error) {
conn, err := gorqlite.Open(name)
if err != nil {
return nil, err
}
return &Conn{conn}, nil
}
type Conn struct {
*gorqlite.Connection
}
func (c *Conn) Prepare(query string) (driver.Stmt, error) {
return &Stmt{Stmt: query, Conn: c}, nil
}
func (c *Conn) Close() error {
c.Connection.Close()
return nil
}
func (c *Conn) Begin() (driver.Tx, error) {
return &Tx{}, nil
}
type Tx struct{}
func (tx *Tx) Commit() error {
// no-op
return nil
}
func (tx *Tx) Rollback() error {
// no-op
return nil
}
type Stmt struct {
Stmt string
Conn *Conn
}
// these aren't checked automatically anywhere else, so we check them here
var _ driver.StmtExecContext = (*Stmt)(nil)
var _ driver.StmtQueryContext = (*Stmt)(nil)
func (s *Stmt) Close() error {
return nil
}
func (s *Stmt) NumInput() int {
return -1
}
func (s *Stmt) Exec(args []driver.Value) (driver.Result, error) {
a := make([]interface{}, len(args))
for i, v := range args {
a[i] = v
}
stmt := gorqlite.ParameterizedStatement{Query: s.Stmt, Arguments: a}
wr, err := s.Conn.WriteOneParameterized(stmt)
if err != nil {
return &Result{wr}, err
}
return &Result{wr}, nil
}
func (s *Stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
a := make([]interface{}, len(args))
for _, v := range args {
if v.Name != "" {
return nil, fmt.Errorf("rqlite: driver does not support named parameters, but got one with name %q in statement %q", v.Name, s.Stmt)
}
a[v.Ordinal-1] = v.Value
}
stmt := gorqlite.ParameterizedStatement{Query: s.Stmt, Arguments: a}
wr, err := s.Conn.WriteOneParameterizedContext(ctx, stmt)
if err != nil {
return &Result{wr}, err
}
return &Result{wr}, nil
}
func (s *Stmt) Query(args []driver.Value) (driver.Rows, error) {
a := make([]interface{}, len(args))
for i, v := range args {
a[i] = v
}
stmt := gorqlite.ParameterizedStatement{Query: s.Stmt, Arguments: a}
qr, err := s.Conn.QueryOneParameterized(stmt)
if err != nil {
return &Rows{qr}, err
}
return &Rows{qr}, nil
}
func (s *Stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
a := make([]interface{}, len(args))
for _, v := range args {
if v.Name != "" {
return nil, fmt.Errorf("rqlite: driver does not support named parameters, but got one with name %q in statement %q", v.Name, s.Stmt)
}
a[v.Ordinal-1] = v.Value
}
stmt := gorqlite.ParameterizedStatement{Query: s.Stmt, Arguments: a}
qr, err := s.Conn.QueryOneParameterizedContext(ctx, stmt)
if err != nil {
return &Rows{qr}, err
}
return &Rows{qr}, nil
}
type Result struct {
gorqlite.WriteResult
}
func (r *Result) LastInsertId() (int64, error) {
return r.WriteResult.LastInsertID, r.WriteResult.Err
}
func (r *Result) RowsAffected() (int64, error) {
return r.WriteResult.RowsAffected, r.WriteResult.Err
}
type Rows struct {
gorqlite.QueryResult
}
func (r *Rows) Columns() []string {
return r.QueryResult.Columns()
}
func (r *Rows) Close() error {
return r.Err
}
func (r *Rows) Next(dest []driver.Value) error {
ok := r.QueryResult.Next()
if !ok {
return io.EOF
}
// in Next, we are copying the values into the destination
// slice, not directly scanning them
slice, err := r.QueryResult.Slice()
if err != nil {
return err
}
for i, v := range slice {
dest[i] = v
}
return nil
}