-
Notifications
You must be signed in to change notification settings - Fork 15
/
stmt.go
130 lines (107 loc) · 3.1 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
package csvq
import (
"context"
"database/sql/driver"
"strconv"
"sync/atomic"
"github.com/mithrandie/csvq/lib/parser"
"github.com/mithrandie/csvq/lib/query"
"github.com/mithrandie/csvq/lib/value"
)
const statementPrefix = "autogen_prepared_"
var counter uint64
func GenerateStatementName() string {
atomic.AddUint64(&counter, 1)
return statementPrefix + strconv.FormatUint(counter, 32)
}
type Stmt struct {
proc *query.Processor
name parser.Identifier
numInput int
}
func NewStmt(ctx context.Context, proc *query.Processor, queryString string) (driver.Stmt, error) {
name := parser.Identifier{Literal: GenerateStatementName()}
statements := []parser.Statement{
parser.StatementPreparation{
Name: name,
Statement: value.NewString(queryString),
},
}
if _, err := proc.Execute(ctx, statements); err != nil {
return nil, err
}
stmt, err := proc.Tx.PreparedStatements.Get(name)
if err != nil {
return nil, err
}
return &Stmt{
proc: proc,
name: name,
numInput: stmt.HolderNumber,
}, nil
}
func (stmt *Stmt) Close() error {
statements := []parser.Statement{
parser.DisposeStatement{
Name: stmt.name,
},
}
_, err := stmt.proc.Execute(context.Background(), statements)
return err
}
func (stmt *Stmt) NumInput() int {
return stmt.numInput
}
func (stmt *Stmt) Exec(args []driver.Value) (driver.Result, error) {
return stmt.ExecContext(context.Background(), stmt.valuesToNamedValues(args))
}
func (stmt *Stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
if err := stmt.exec(ctx, args); err != nil {
return nil, err
}
return NewResult(int64(stmt.proc.Tx.AffectedRows)), nil
}
func (stmt *Stmt) Query(args []driver.Value) (driver.Rows, error) {
return stmt.QueryContext(context.Background(), stmt.valuesToNamedValues(args))
}
func (stmt *Stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
if err := stmt.exec(ctx, args); err != nil {
return nil, err
}
return NewRows(stmt.proc.Tx.SelectedViews), nil
}
func (stmt *Stmt) valuesToNamedValues(values []driver.Value) []driver.NamedValue {
nvs := make([]driver.NamedValue, 0, len(values))
for i := range values {
nvs = append(nvs, driver.NamedValue{Ordinal: i + 1, Value: values[i]})
}
return nvs
}
func (stmt *Stmt) exec(ctx context.Context, args []driver.NamedValue) error {
values := make([]parser.ReplaceValue, 0, len(args))
for i := range args {
v, _ := stmt.ColumnConverter(i).ConvertValue(args[i].Value)
values = append(values, parser.ReplaceValue{
Value: v.(Value).PrimitiveType(),
Name: parser.Identifier{Literal: args[i].Name},
})
}
statements := []parser.Statement{
parser.ExecuteStatement{
Name: stmt.name,
Values: values,
},
}
_, err := stmt.proc.Execute(query.ContextForStoringResults(ctx), statements)
return err
}
func (stmt *Stmt) ColumnConverter(_ int) driver.ValueConverter {
return ValueConverter{}
}
func (stmt *Stmt) CheckNamedValue(nv *driver.NamedValue) error {
index := nv.Ordinal - 1
if _, err := stmt.ColumnConverter(index).ConvertValue(nv.Value); err != nil {
return err
}
return nil
}