This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
statement.go
107 lines (90 loc) · 2.65 KB
/
statement.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 cql
import (
"context"
"database/sql/driver"
"reflect"
)
// Close a statement
func (cqlStmt *CqlStmt) Close() error {
if cqlStmt.CqlQuery != nil {
cqlStmt.CqlQuery.Release()
cqlStmt.CqlQuery = nil
}
return nil
}
// NumInput not supported
func (cqlStmt *CqlStmt) NumInput() int {
return -1
}
// Exec executes a statement with background context
func (cqlStmt *CqlStmt) Exec(args []driver.Value) (driver.Result, error) {
return cqlStmt.execContext(context.Background(), valuesToInterface(args))
}
// ExecContext executes a statement with context
func (cqlStmt *CqlStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
values, err := namedValuesToInterface(args)
if err != nil {
return nil, err
}
return cqlStmt.execContext(ctx, values)
}
// execContext executes a statement with context
func (cqlStmt *CqlStmt) execContext(ctx context.Context, values []interface{}) (driver.Result, error) {
query := cqlStmt.CqlQuery
if query == nil {
return nil, ErrQueryIsNil
}
query = query.WithContext(ctx)
if len(values) > 0 {
query = query.Bind(values...)
}
err := query.Exec()
if err != nil {
return nil, err
}
return cqlResultStruct{}, nil
}
// Query queries a statement with background context
func (cqlStmt *CqlStmt) Query(args []driver.Value) (driver.Rows, error) {
return cqlStmt.queryContext(context.Background(), valuesToInterface(args))
}
// QueryContext queries a statement with context
func (cqlStmt *CqlStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
values, err := namedValuesToInterface(args)
if err != nil {
return nil, err
}
return cqlStmt.queryContext(ctx, values)
}
// queryContext queries a statement with context
func (cqlStmt *CqlStmt) queryContext(ctx context.Context, values []interface{}) (driver.Rows, error) {
query := cqlStmt.CqlQuery
if query == nil {
return nil, ErrQueryIsNil
}
query = query.WithContext(ctx)
if len(values) > 0 {
query = query.Bind(values...)
}
iter := query.Iter()
return &cqlRowsStruct{
iter: iter,
columns: columnInfoToString(iter.Columns()),
}, nil
}
// ColumnConverter provides driver ValueConverter for statement
func (cqlStmt *CqlStmt) ColumnConverter(index int) driver.ValueConverter {
return converter{}
}
// ConvertValue coverts interface value to driver Value
func (c converter) ConvertValue(valueInterface interface{}) (driver.Value, error) {
valueDriver, err := driver.DefaultParameterConverter.ConvertValue(valueInterface)
if err == nil {
return valueDriver, nil
}
rv := reflect.ValueOf(valueInterface)
if rv.Kind() == reflect.Uint64 {
return rv.Uint(), nil
}
return valueDriver, err
}