forked from goccy/go-zetasqlite
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stmt.go
250 lines (210 loc) · 5.89 KB
/
stmt.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
package internal
import (
"context"
"database/sql"
"database/sql/driver"
"fmt"
ast "github.com/goccy/go-zetasql/resolved_ast"
)
var (
_ driver.Stmt = &CreateTableStmt{}
_ driver.Stmt = &CreateFunctionStmt{}
_ driver.Stmt = &DMLStmt{}
_ driver.Stmt = &QueryStmt{}
)
type CreateTableStmt struct {
stmt *sql.Stmt
conn *Conn
catalog *Catalog
spec *TableSpec
}
type CreateViewStmt struct {
stmt *sql.Stmt
conn *Conn
catalog *Catalog
spec *TableSpec
}
func (s *CreateTableStmt) Close() error {
return s.stmt.Close()
}
func (s *CreateTableStmt) NumInput() int {
return 0
}
func (s *CreateTableStmt) Exec(args []driver.Value) (driver.Result, error) {
if _, err := s.stmt.Exec(args); err != nil {
return nil, err
}
if err := s.catalog.AddNewTableSpec(context.Background(), s.conn, s.spec); err != nil {
return nil, fmt.Errorf("failed to add new table spec: %w", err)
}
return nil, nil
}
func (s *CreateTableStmt) Query(args []driver.Value) (driver.Rows, error) {
return nil, fmt.Errorf("failed to query for CreateTableStmt")
}
func newCreateTableStmt(stmt *sql.Stmt, conn *Conn, catalog *Catalog, spec *TableSpec) *CreateTableStmt {
return &CreateTableStmt{
stmt: stmt,
conn: conn,
catalog: catalog,
spec: spec,
}
}
func newCreateViewStmt(stmt *sql.Stmt, conn *Conn, catalog *Catalog, spec *TableSpec) *CreateViewStmt {
return &CreateViewStmt{
stmt: stmt,
conn: conn,
catalog: catalog,
spec: spec,
}
}
func (s *CreateViewStmt) Close() error {
return s.stmt.Close()
}
func (s *CreateViewStmt) NumInput() int {
return 0
}
func (s *CreateViewStmt) Exec(args []driver.Value) (driver.Result, error) {
if _, err := s.stmt.Exec(args); err != nil {
return nil, err
}
if err := s.catalog.AddNewTableSpec(context.Background(), s.conn, s.spec); err != nil {
return nil, fmt.Errorf("failed to add new table spec: %w", err)
}
return nil, nil
}
func (s *CreateViewStmt) Query(args []driver.Value) (driver.Rows, error) {
return nil, fmt.Errorf("failed to query for CreateViewStmt")
}
type CreateFunctionStmt struct {
conn *Conn
catalog *Catalog
spec *FunctionSpec
}
func (s *CreateFunctionStmt) Close() error {
return nil
}
func (s *CreateFunctionStmt) NumInput() int {
return 0
}
func (s *CreateFunctionStmt) Exec(args []driver.Value) (driver.Result, error) {
if err := s.catalog.AddNewFunctionSpec(context.Background(), s.conn, s.spec); err != nil {
return nil, fmt.Errorf("failed to add new function spec: %w", err)
}
return nil, nil
}
func (s *CreateFunctionStmt) Query(args []driver.Value) (driver.Rows, error) {
return nil, fmt.Errorf("failed to query for CreateFunctionStmt")
}
func newCreateFunctionStmt(conn *Conn, catalog *Catalog, spec *FunctionSpec) *CreateFunctionStmt {
return &CreateFunctionStmt{
conn: conn,
catalog: catalog,
spec: spec,
}
}
type DMLStmt struct {
stmt *sql.Stmt
args []*ast.ParameterNode
formattedQuery string
}
func newDMLStmt(stmt *sql.Stmt, args []*ast.ParameterNode, formattedQuery string) *DMLStmt {
return &DMLStmt{
stmt: stmt,
args: args,
formattedQuery: formattedQuery,
}
}
func (s *DMLStmt) CheckNamedValue(value *driver.NamedValue) error {
return nil
}
func (s *DMLStmt) Close() error {
return s.stmt.Close()
}
func (s *DMLStmt) NumInput() int {
return len(s.args)
}
func (s *DMLStmt) Exec(args []driver.Value) (driver.Result, error) {
values := make([]interface{}, 0, len(args))
for _, arg := range args {
values = append(values, arg)
}
newArgs, err := EncodeGoValues(values, s.args)
if err != nil {
return nil, err
}
result, err := s.stmt.Exec(newArgs...)
if err != nil {
return nil, fmt.Errorf(
"failed to execute query %s: args %v: %w",
s.formattedQuery,
newArgs,
err,
)
}
return result, nil
}
func (s *DMLStmt) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
return nil, fmt.Errorf("unimplemented ExecContext for DMLStmt")
}
func (s *DMLStmt) Query(args []driver.Value) (driver.Rows, error) {
return nil, fmt.Errorf("unsupported query for DMLStmt")
}
func (s *DMLStmt) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
return nil, fmt.Errorf("unsupported query for DMLStmt")
}
type QueryStmt struct {
stmt *sql.Stmt
args []*ast.ParameterNode
formattedQuery string
outputColumns []*ColumnSpec
}
func newQueryStmt(stmt *sql.Stmt, args []*ast.ParameterNode, formattedQuery string, outputColumns []*ColumnSpec) *QueryStmt {
return &QueryStmt{
stmt: stmt,
args: args,
formattedQuery: formattedQuery,
outputColumns: outputColumns,
}
}
func (s *QueryStmt) CheckNamedValue(value *driver.NamedValue) error {
return nil
}
func (s *QueryStmt) Close() error {
return s.stmt.Close()
}
func (s *QueryStmt) NumInput() int {
return len(s.args)
}
func (s *QueryStmt) OutputColumns() []*ColumnSpec {
return s.outputColumns
}
func (s *QueryStmt) Exec(args []driver.Value) (driver.Result, error) {
return nil, fmt.Errorf("unsupported exec for QueryStmt")
}
func (s *QueryStmt) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
return nil, fmt.Errorf("unsupported exec for QueryStmt")
}
func (s *QueryStmt) Query(args []driver.Value) (driver.Rows, error) {
values := make([]interface{}, 0, len(args))
for _, arg := range args {
values = append(values, arg)
}
newArgs, err := EncodeGoValues(values, s.args)
if err != nil {
return nil, err
}
rows, err := s.stmt.Query(newArgs...)
if err != nil {
return nil, fmt.Errorf(
"failed to query %s: args: %v: %w",
s.formattedQuery,
newArgs,
err,
)
}
return &Rows{rows: rows, columns: s.outputColumns}, nil
}
func (s *QueryStmt) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
return nil, fmt.Errorf("unimplemented QueryContext for QueryStmt")
}