-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstatement.go
133 lines (109 loc) · 3.08 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
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
package driver
import (
"context"
"database/sql/driver"
"regexp"
"strings"
"cloud.google.com/go/bigquery"
"github.com/samber/lo"
"github.com/sirupsen/logrus"
)
var namedParamsRegexp = regexp.MustCompile(`@[\w]+`)
type bigQueryStatement struct {
connection *bigQueryConnection
query string
}
func (statement bigQueryStatement) Close() error {
return nil
}
func (statement bigQueryStatement) NumInput() int {
params := strings.Count(statement.query, "?")
if params > 0 {
return params
}
uniqueMatches := lo.Uniq(namedParamsRegexp.FindAllString(statement.query, -1))
return len(uniqueMatches)
}
func (bigQueryStatement) CheckNamedValue(*driver.NamedValue) error {
return nil
}
func (statement *bigQueryStatement) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
query, err := statement.buildQuery(convertParameters(args))
if err != nil {
return nil, err
}
rowIterator, err := query.Read(ctx)
if err != nil {
return nil, err
}
return &bigQueryResult{rowIterator}, nil
}
func (statement *bigQueryStatement) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
query, err := statement.buildQuery(convertParameters(args))
if err != nil {
return nil, err
}
rowIterator, err := query.Read(ctx)
if err != nil {
return nil, err
}
return &bigQueryRows{
source: createSourceFromRowIterator(rowIterator),
}, nil
}
func (statement bigQueryStatement) Exec(args []driver.Value) (driver.Result, error) {
return nil, driver.ErrSkip
}
func (statement bigQueryStatement) Query(args []driver.Value) (driver.Rows, error) {
return nil, driver.ErrSkip
}
func (statement bigQueryStatement) buildQuery(args []driver.Value) (*bigquery.Query, error) {
query, err := statement.connection.query(statement.query)
if err != nil {
return nil, err
}
query.Parameters, err = statement.buildParameters(args)
if err != nil {
return nil, err
}
return query, err
}
func (statement bigQueryStatement) buildParameters(args []driver.Value) ([]bigquery.QueryParameter, error) { // nolint: unparam
if args == nil {
return nil, nil
}
var parameters []bigquery.QueryParameter
for _, arg := range args {
parameters = buildParameter(arg, parameters)
}
return parameters, nil
}
func buildParameter(arg driver.Value, parameters []bigquery.QueryParameter) []bigquery.QueryParameter {
namedValue, ok := arg.(driver.NamedValue)
if ok {
return buildParameterFromNamedValue(namedValue, parameters)
}
logrus.Debugf("-param:%s", arg)
return append(parameters, bigquery.QueryParameter{
Value: arg,
})
}
func buildParameterFromNamedValue(namedValue driver.NamedValue, parameters []bigquery.QueryParameter) []bigquery.QueryParameter {
if namedValue.Name == "" {
return append(parameters, bigquery.QueryParameter{
Value: namedValue.Value,
})
} else {
return append(parameters, bigquery.QueryParameter{
Name: namedValue.Name,
Value: namedValue.Value,
})
}
}
func convertParameters(args []driver.NamedValue) []driver.Value {
var values []driver.Value
for _, arg := range args {
values = append(values, arg)
}
return values
}