forked from goccy/go-zetasqlite
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stmt_action.go
539 lines (465 loc) · 14.5 KB
/
stmt_action.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
package internal
import (
"context"
"database/sql/driver"
"fmt"
"strings"
ast "github.com/goccy/go-zetasql/resolved_ast"
)
type StmtAction interface {
Prepare(context.Context, *Conn) (driver.Stmt, error)
ExecContext(context.Context, *Conn) (driver.Result, error)
QueryContext(context.Context, *Conn) (*Rows, error)
Cleanup(context.Context, *Conn) error
Args() []interface{}
}
type CreateTableStmtAction struct {
query string
args []interface{}
spec *TableSpec
catalog *Catalog
isAutoIndexMode bool
}
func (a *CreateTableStmtAction) Prepare(ctx context.Context, conn *Conn) (driver.Stmt, error) {
if a.spec.CreateMode == ast.CreateOrReplaceMode {
if _, err := conn.ExecContext(
ctx,
fmt.Sprintf("DROP TABLE IF EXISTS `%s`", a.spec.TableName()),
); err != nil {
return nil, err
}
}
stmt, err := conn.PrepareContext(ctx, a.spec.SQLiteSchema())
if err != nil {
return nil, fmt.Errorf("failed to prepare %s: %w", a.query, err)
}
return newCreateTableStmt(stmt, conn, a.catalog, a.spec), nil
}
func (a *CreateTableStmtAction) createIndexAutomatically(ctx context.Context, conn *Conn) error {
for _, col := range a.spec.Columns {
if !col.Type.AvailableAutoIndex() {
continue
}
indexName := fmt.Sprintf("zetasqlite_autoindex_%s_%s", col.Name, strings.Join(a.spec.NamePath, "_"))
createIndexQuery := fmt.Sprintf(
"CREATE INDEX IF NOT EXISTS %s ON `%s`(`%s`)",
indexName,
a.spec.TableName(),
col.Name,
)
if _, err := conn.ExecContext(ctx, createIndexQuery); err != nil {
return fmt.Errorf("failed to create index automatically %s: %w", createIndexQuery, err)
}
}
return nil
}
func (a *CreateTableStmtAction) exec(ctx context.Context, conn *Conn) error {
if a.spec.CreateMode == ast.CreateOrReplaceMode {
if _, err := conn.ExecContext(
ctx,
fmt.Sprintf("DROP TABLE IF EXISTS `%s`", a.spec.TableName()),
); err != nil {
return err
}
}
if _, err := conn.ExecContext(ctx, a.spec.SQLiteSchema(), a.args...); err != nil {
return fmt.Errorf("failed to exec %s: %w", a.query, err)
}
if a.isAutoIndexMode {
if err := a.createIndexAutomatically(ctx, conn); err != nil {
return err
}
}
if err := a.catalog.AddNewTableSpec(ctx, conn, a.spec); err != nil {
return fmt.Errorf("failed to add new table spec: %w", err)
}
if !a.spec.IsTemp {
conn.addTable(a.spec)
}
return nil
}
func (a *CreateTableStmtAction) ExecContext(ctx context.Context, conn *Conn) (driver.Result, error) {
if err := a.exec(ctx, conn); err != nil {
return nil, err
}
return &Result{conn: conn}, nil
}
func (a *CreateTableStmtAction) QueryContext(ctx context.Context, conn *Conn) (*Rows, error) {
if err := a.exec(ctx, conn); err != nil {
return nil, err
}
return &Rows{conn: conn}, nil
}
func (a *CreateTableStmtAction) Args() []interface{} {
return a.args
}
func (a *CreateTableStmtAction) Cleanup(ctx context.Context, conn *Conn) error {
if !a.spec.IsTemp {
return nil
}
if _, err := conn.ExecContext(
ctx,
fmt.Sprintf("DROP TABLE IF EXISTS `%s`", a.spec.TableName()),
); err != nil {
return fmt.Errorf("failed to cleanup table %s: %w", a.spec.TableName(), err)
}
if err := a.catalog.DeleteTableSpec(ctx, conn, a.spec.TableName()); err != nil {
return fmt.Errorf("failed to delete table spec: %w", err)
}
return nil
}
type CreateViewStmtAction struct {
query string
spec *TableSpec
catalog *Catalog
}
func (a *CreateViewStmtAction) Prepare(ctx context.Context, conn *Conn) (driver.Stmt, error) {
if a.spec.CreateMode == ast.CreateOrReplaceMode {
if _, err := conn.ExecContext(
ctx,
fmt.Sprintf("DROP VIEW IF EXISTS `%s`", a.spec.TableName()),
); err != nil {
return nil, err
}
}
stmt, err := conn.PrepareContext(ctx, a.spec.SQLiteSchema())
if err != nil {
return nil, fmt.Errorf("failed to prepare %s: %w", a.query, err)
}
return newCreateViewStmt(stmt, conn, a.catalog, a.spec), nil
}
func (a *CreateViewStmtAction) exec(ctx context.Context, conn *Conn) error {
if a.spec.CreateMode == ast.CreateOrReplaceMode {
if _, err := conn.ExecContext(
ctx,
fmt.Sprintf("DROP VIEW IF EXISTS `%s`", a.spec.TableName()),
); err != nil {
return err
}
}
if _, err := conn.ExecContext(ctx, a.spec.SQLiteSchema()); err != nil {
return fmt.Errorf("failed to exec %s: %w", a.query, err)
}
if err := a.catalog.AddNewTableSpec(ctx, conn, a.spec); err != nil {
return fmt.Errorf("failed to add new view spec: %w", err)
}
return nil
}
func (a *CreateViewStmtAction) ExecContext(ctx context.Context, conn *Conn) (driver.Result, error) {
if err := a.exec(ctx, conn); err != nil {
return nil, err
}
return &Result{conn: conn}, nil
}
func (a *CreateViewStmtAction) QueryContext(ctx context.Context, conn *Conn) (*Rows, error) {
if err := a.exec(ctx, conn); err != nil {
return nil, err
}
return &Rows{conn: conn}, nil
}
func (a *CreateViewStmtAction) Cleanup(ctx context.Context, conn *Conn) error {
if !a.spec.IsTemp {
conn.addTable(a.spec)
return nil
}
if _, err := conn.ExecContext(
ctx,
fmt.Sprintf("DROP VIEW IF EXISTS `%s`", a.spec.TableName()),
); err != nil {
return fmt.Errorf("failed to cleanup view %s: %w", a.spec.TableName(), err)
}
if err := a.catalog.DeleteTableSpec(ctx, conn, a.spec.TableName()); err != nil {
return fmt.Errorf("failed to delete table spec: %w", err)
}
return nil
}
func (a *CreateViewStmtAction) Args() []interface{} {
return nil
}
type CreateFunctionStmtAction struct {
spec *FunctionSpec
catalog *Catalog
funcMap map[string]*FunctionSpec
}
func (a *CreateFunctionStmtAction) Prepare(ctx context.Context, conn *Conn) (driver.Stmt, error) {
return newCreateFunctionStmt(conn, a.catalog, a.spec), nil
}
func (a *CreateFunctionStmtAction) exec(ctx context.Context, conn *Conn) error {
if err := a.catalog.AddNewFunctionSpec(ctx, conn, a.spec); err != nil {
return fmt.Errorf("failed to add new function spec: %w", err)
}
a.funcMap[a.spec.FuncName()] = a.spec
conn.addFunction(a.spec)
return nil
}
func (a *CreateFunctionStmtAction) ExecContext(ctx context.Context, conn *Conn) (driver.Result, error) {
if err := a.exec(ctx, conn); err != nil {
return nil, err
}
return &Result{conn: conn}, nil
}
func (a *CreateFunctionStmtAction) QueryContext(ctx context.Context, conn *Conn) (*Rows, error) {
if err := a.exec(ctx, conn); err != nil {
return nil, err
}
return &Rows{conn: conn}, nil
}
func (a *CreateFunctionStmtAction) Args() []interface{} {
return nil
}
func (a *CreateFunctionStmtAction) Cleanup(ctx context.Context, conn *Conn) error {
if !a.spec.IsTemp {
return nil
}
funcName := a.spec.FuncName()
if err := a.catalog.DeleteFunctionSpec(ctx, conn, funcName); err != nil {
return fmt.Errorf("failed to delete function spec: %w", err)
}
delete(a.funcMap, funcName)
return nil
}
type DropStmtAction struct {
name string
objectType string
funcMap map[string]*FunctionSpec
catalog *Catalog
query string
formattedQuery string
args []interface{}
}
func (a *DropStmtAction) exec(ctx context.Context, conn *Conn) error {
switch a.objectType {
case "TABLE":
if _, err := conn.ExecContext(ctx, a.formattedQuery, a.args...); err != nil {
return fmt.Errorf("failed to exec %s: %w", a.query, err)
}
spec := a.catalog.tableMap[a.name]
if err := a.catalog.DeleteTableSpec(ctx, conn, a.name); err != nil {
return fmt.Errorf("failed to delete table spec: %w", err)
}
conn.deleteTable(spec)
case "FUNCTION":
if err := a.catalog.DeleteFunctionSpec(ctx, conn, a.name); err != nil {
return fmt.Errorf("failed to delete function spec: %w", err)
}
conn.deleteFunction(a.funcMap[a.name])
delete(a.funcMap, a.name)
default:
return fmt.Errorf("currently unsupported DROP %s statement", a.objectType)
}
return nil
}
func (a *DropStmtAction) Prepare(ctx context.Context, conn *Conn) (driver.Stmt, error) {
return nil, nil
}
func (a *DropStmtAction) ExecContext(ctx context.Context, conn *Conn) (driver.Result, error) {
if err := a.exec(ctx, conn); err != nil {
return nil, err
}
return &Result{conn: conn}, nil
}
func (a *DropStmtAction) QueryContext(ctx context.Context, conn *Conn) (*Rows, error) {
if err := a.exec(ctx, conn); err != nil {
return nil, err
}
return &Rows{conn: conn}, nil
}
func (a *DropStmtAction) Args() []interface{} {
return nil
}
func (a *DropStmtAction) Cleanup(ctx context.Context, conn *Conn) error {
return nil
}
type DMLStmtAction struct {
query string
params []*ast.ParameterNode
args []interface{}
formattedQuery string
}
func (a *DMLStmtAction) Prepare(ctx context.Context, conn *Conn) (driver.Stmt, error) {
s, err := conn.PrepareContext(ctx, a.formattedQuery)
if err != nil {
return nil, fmt.Errorf("failed to prepare %s: %w", a.query, err)
}
return newDMLStmt(s, a.params, a.formattedQuery), nil
}
func (a *DMLStmtAction) exec(ctx context.Context, conn *Conn) (driver.Result, error) {
result, err := conn.ExecContext(ctx, a.formattedQuery, a.args...)
if err != nil {
return nil, fmt.Errorf("failed to exec %s: %w", a.formattedQuery, err)
}
return result, nil
}
func (a *DMLStmtAction) ExecContext(ctx context.Context, conn *Conn) (driver.Result, error) {
result, err := a.exec(ctx, conn)
if err != nil {
return nil, err
}
return &Result{conn: conn, result: result}, nil
}
func (a *DMLStmtAction) QueryContext(ctx context.Context, conn *Conn) (*Rows, error) {
if _, err := a.exec(ctx, conn); err != nil {
return nil, err
}
return &Rows{conn: conn}, nil
}
func (a *DMLStmtAction) Args() []interface{} {
return nil
}
func (a *DMLStmtAction) Cleanup(ctx context.Context, conn *Conn) error {
return nil
}
type QueryStmtAction struct {
query string
params []*ast.ParameterNode
args []interface{}
formattedQuery string
outputColumns []*ColumnSpec
isExplainMode bool
}
func (a *QueryStmtAction) Prepare(ctx context.Context, conn *Conn) (driver.Stmt, error) {
s, err := conn.PrepareContext(ctx, a.formattedQuery)
if err != nil {
return nil, fmt.Errorf("failed to prepare %s: %w", a.query, err)
}
return newQueryStmt(s, a.params, a.formattedQuery, a.outputColumns), nil
}
func (a *QueryStmtAction) ExecContext(ctx context.Context, conn *Conn) (driver.Result, error) {
if _, err := conn.ExecContext(ctx, a.formattedQuery, a.args...); err != nil {
return nil, fmt.Errorf("failed to query %s: %w", a.query, err)
}
return &Result{conn: conn}, nil
}
func (a *QueryStmtAction) ExplainQueryPlan(ctx context.Context, conn *Conn) error {
rows, err := conn.QueryContext(ctx, fmt.Sprintf("EXPLAIN QUERY PLAN %s", a.formattedQuery), a.args...)
if err != nil {
return fmt.Errorf("failed to explain query plan: %w", err)
}
defer rows.Close()
fmt.Println("|selectid|order|from|detail|")
fmt.Println("----------------------------")
for rows.Next() {
var (
selectID, order, from int
detail string
)
if err := rows.Scan(&selectID, &order, &from, &detail); err != nil {
return fmt.Errorf("failed to scan: %w", err)
}
fmt.Printf("|%d|%d|%d|%s|\n", selectID, order, from, detail)
}
return nil
}
func (a *QueryStmtAction) QueryContext(ctx context.Context, conn *Conn) (*Rows, error) {
if a.isExplainMode {
if err := a.ExplainQueryPlan(ctx, conn); err != nil {
return nil, err
}
return &Rows{}, nil
}
rows, err := conn.QueryContext(ctx, a.formattedQuery, a.args...)
if err != nil {
return nil, fmt.Errorf("failed to query %s: %w", a.query, err)
}
return &Rows{conn: conn, rows: rows, columns: a.outputColumns}, nil
}
func (a *QueryStmtAction) Args() []interface{} {
return nil
}
func (a *QueryStmtAction) Cleanup(ctx context.Context, conn *Conn) error {
return nil
}
type BeginStmtAction struct{}
func (a *BeginStmtAction) Prepare(ctx context.Context, conn *Conn) (driver.Stmt, error) {
return nil, nil
}
func (a *BeginStmtAction) ExecContext(ctx context.Context, conn *Conn) (driver.Result, error) {
return &Result{conn: conn}, nil
}
func (a *BeginStmtAction) QueryContext(ctx context.Context, conn *Conn) (*Rows, error) {
return &Rows{conn: conn}, nil
}
func (a *BeginStmtAction) Args() []interface{} {
return nil
}
func (a *BeginStmtAction) Cleanup(ctx context.Context, conn *Conn) error {
return nil
}
type CommitStmtAction struct{}
func (a *CommitStmtAction) Prepare(ctx context.Context, conn *Conn) (driver.Stmt, error) {
return nil, nil
}
func (a *CommitStmtAction) ExecContext(ctx context.Context, conn *Conn) (driver.Result, error) {
return &Result{conn: conn}, nil
}
func (a *CommitStmtAction) QueryContext(ctx context.Context, conn *Conn) (*Rows, error) {
return &Rows{conn: conn}, nil
}
func (a *CommitStmtAction) Args() []interface{} {
return nil
}
func (a *CommitStmtAction) Cleanup(ctx context.Context, conn *Conn) error {
return nil
}
type TruncateStmtAction struct {
query string
}
func (a *TruncateStmtAction) Prepare(ctx context.Context, conn *Conn) (driver.Stmt, error) {
return nil, nil
}
func (a *TruncateStmtAction) exec(ctx context.Context, conn *Conn) error {
if _, err := conn.ExecContext(ctx, a.query); err != nil {
return fmt.Errorf("failed to truncate %s: %w", a.query, err)
}
return nil
}
func (a *TruncateStmtAction) ExecContext(ctx context.Context, conn *Conn) (driver.Result, error) {
if err := a.exec(ctx, conn); err != nil {
return nil, err
}
return &Result{conn: conn}, nil
}
func (a *TruncateStmtAction) QueryContext(ctx context.Context, conn *Conn) (*Rows, error) {
if err := a.exec(ctx, conn); err != nil {
return nil, err
}
return &Rows{conn: conn}, nil
}
func (a *TruncateStmtAction) Args() []interface{} {
return nil
}
func (a *TruncateStmtAction) Cleanup(ctx context.Context, conn *Conn) error {
return nil
}
type MergeStmtAction struct {
stmts []string
}
func (a *MergeStmtAction) Prepare(ctx context.Context, conn *Conn) (driver.Stmt, error) {
return nil, nil
}
func (a *MergeStmtAction) exec(ctx context.Context, conn *Conn) error {
for _, stmt := range a.stmts {
if _, err := conn.ExecContext(ctx, stmt); err != nil {
return fmt.Errorf("failed to exec merge statement %s: %w", stmt, err)
}
}
return nil
}
func (a *MergeStmtAction) ExecContext(ctx context.Context, conn *Conn) (driver.Result, error) {
if err := a.exec(ctx, conn); err != nil {
return nil, err
}
return &Result{conn: conn}, nil
}
func (a *MergeStmtAction) QueryContext(ctx context.Context, conn *Conn) (*Rows, error) {
if err := a.exec(ctx, conn); err != nil {
return nil, err
}
return &Rows{conn: conn}, nil
}
func (a *MergeStmtAction) Args() []interface{} {
return nil
}
func (a *MergeStmtAction) Cleanup(ctx context.Context, conn *Conn) error {
return nil
}