This repository has been archived by the owner on Mar 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
statement.go
206 lines (162 loc) · 5.36 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
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
package avatica
import (
"database/sql/driver"
"errors"
"math"
"time"
"github.com/Boostport/avatica/message"
"golang.org/x/net/context"
)
type stmt struct {
statementID uint32
conn *conn
parameters []*message.AvaticaParameter
handle message.StatementHandle
}
// Close closes a statement
func (s *stmt) Close() error {
if s.conn.connectionId == "" {
return driver.ErrBadConn
}
_, err := s.conn.httpClient.post(context.Background(), &message.CloseStatementRequest{
ConnectionId: s.conn.connectionId,
StatementId: s.statementID,
})
return err
}
// NumInput returns the number of placeholder parameters.
//
// If NumInput returns >= 0, the sql package will sanity check
// argument counts from callers and return errors to the caller
// before the statement's Exec or Query methods are called.
//
// NumInput may also return -1, if the driver doesn't know
// its number of placeholders. In that case, the sql package
// will not sanity check Exec or Query argument counts.
func (s *stmt) NumInput() int {
return len(s.parameters)
}
// Exec executes a query that doesn't return rows, such
// as an INSERT or UPDATE.
func (s *stmt) Exec(args []driver.Value) (driver.Result, error) {
list := driverValueToNamedValue(args)
return s.exec(context.Background(), list)
}
func (s *stmt) exec(ctx context.Context, args []namedValue) (driver.Result, error) {
if s.conn.connectionId == "" {
return nil, driver.ErrBadConn
}
msg := &message.ExecuteRequest{
StatementHandle: &s.handle,
ParameterValues: s.parametersToTypedValues(args),
FirstFrameMaxSize: s.conn.config.frameMaxSize,
HasParameterValues: true,
}
if s.conn.config.frameMaxSize <= -1 {
msg.DeprecatedFirstFrameMaxSize = math.MaxInt64
} else {
msg.DeprecatedFirstFrameMaxSize = uint64(s.conn.config.frameMaxSize)
}
res, err := s.conn.httpClient.post(ctx, msg)
if err != nil {
return nil, err
}
results := res.(*message.ExecuteResponse).Results
if len(results) <= 0 {
return nil, errors.New("empty ResultSet in ExecuteResponse")
}
// Currently there is only 1 ResultSet per response
changed := int64(results[0].UpdateCount)
return &result{
affectedRows: changed,
}, nil
}
// Query executes a query that may return rows, such as a
// SELECT.
func (s *stmt) Query(args []driver.Value) (driver.Rows, error) {
list := driverValueToNamedValue(args)
return s.query(context.Background(), list)
}
func (s *stmt) query(ctx context.Context, args []namedValue) (driver.Rows, error) {
if s.conn.connectionId == "" {
return nil, driver.ErrBadConn
}
msg := &message.ExecuteRequest{
StatementHandle: &s.handle,
ParameterValues: s.parametersToTypedValues(args),
FirstFrameMaxSize: s.conn.config.frameMaxSize,
HasParameterValues: true,
}
if s.conn.config.frameMaxSize <= -1 {
msg.DeprecatedFirstFrameMaxSize = math.MaxInt64
} else {
msg.DeprecatedFirstFrameMaxSize = uint64(s.conn.config.frameMaxSize)
}
res, err := s.conn.httpClient.post(ctx, msg)
if err != nil {
return nil, err
}
resultSet := res.(*message.ExecuteResponse).Results
return newRows(s.conn, s.statementID, resultSet), nil
}
func (s *stmt) parametersToTypedValues(vals []namedValue) []*message.TypedValue {
var result []*message.TypedValue
for i, val := range vals {
typed := message.TypedValue{}
if val.Value == nil {
typed.Null = true
} else {
switch v := val.Value.(type) {
case int64:
typed.Type = message.Rep_LONG
typed.NumberValue = v
case float64:
typed.Type = message.Rep_DOUBLE
typed.DoubleValue = v
case bool:
typed.Type = message.Rep_BOOLEAN
typed.BoolValue = v
case []byte:
typed.Type = message.Rep_BYTE_STRING
typed.BytesValue = v
case string:
if s.parameters[i].TypeName == "DECIMAL" {
typed.Type = message.Rep_BIG_DECIMAL
} else {
typed.Type = message.Rep_STRING
}
typed.StringValue = v
case time.Time:
avaticaParameter := s.parameters[i]
switch avaticaParameter.TypeName {
case "TIME", "UNSIGNED_TIME":
typed.Type = message.Rep_JAVA_SQL_TIME
// Because a location can have multiple time zones due to daylight savings,
// we need to be explicit and get the offset
zone, offset := v.Zone()
// Calculate milliseconds since 00:00:00.000
base := time.Date(v.Year(), v.Month(), v.Day(), 0, 0, 0, 0, time.FixedZone(zone, offset))
typed.NumberValue = int64(v.Sub(base).Nanoseconds() / int64(time.Millisecond))
case "DATE", "UNSIGNED_DATE":
typed.Type = message.Rep_JAVA_SQL_DATE
// Because a location can have multiple time zones due to daylight savings,
// we need to be explicit and get the offset
zone, offset := v.Zone()
// Calculate number of days since 1970/1/1
base := time.Date(1970, 1, 1, 0, 0, 0, 0, time.FixedZone(zone, offset))
typed.NumberValue = int64(v.Sub(base) / (24 * time.Hour))
case "TIMESTAMP", "UNSIGNED_TIMESTAMP":
typed.Type = message.Rep_JAVA_SQL_TIMESTAMP
// Because a location can have multiple time zones due to daylight savings,
// we need to be explicit and get the offset
zone, offset := v.Zone()
// Calculate number of milliseconds since 1970-01-01 00:00:00.000
base := time.Date(1970, 1, 1, 0, 0, 0, 0, time.FixedZone(zone, offset))
typed.NumberValue = int64(v.Sub(base).Nanoseconds() / int64(time.Millisecond))
}
}
}
result = append(result, &typed)
}
return result
}