forked from SAP/cgo-ase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
statement.go
386 lines (318 loc) · 10.3 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
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
// SPDX-FileCopyrightText: 2020 SAP SE
// SPDX-FileCopyrightText: 2021 SAP SE
// SPDX-FileCopyrightText: 2022 SAP SE
// SPDX-FileCopyrightText: 2023 SAP SE
//
// SPDX-License-Identifier: Apache-2.0
package ase
//#include <stdlib.h>
//#include "ctlib.h"
import "C"
import (
"context"
"database/sql/driver"
"encoding/binary"
"fmt"
"io"
"math"
"strings"
"sync"
"unsafe"
"github.com/SAP/go-dblib"
"github.com/SAP/go-dblib/asetypes"
)
// Interface satisfaction checks.
var (
_ driver.Stmt = (*statement)(nil)
_ driver.StmtExecContext = (*statement)(nil)
_ driver.StmtQueryContext = (*statement)(nil)
_ driver.NamedValueChecker = (*statement)(nil)
)
// statement implements the driver.Stmt interface.
type statement struct {
name string
argCount int
cmd *Command
columnTypes []ASEType
}
var (
statementCounter uint
statementCounterM = sync.Mutex{}
)
// Prepare implements the driver.Conn interface.
func (conn *Connection) Prepare(query string) (driver.Stmt, error) {
return conn.PrepareContext(context.Background(), query)
}
// PrepareContext implements the driver.ConnPrepareContext interface.
func (conn *Connection) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
return conn.prepare(ctx, query)
}
// TODO: Add doc
func (conn *Connection) prepare(ctx context.Context, query string) (*statement, error) {
stmt := &statement{}
stmt.argCount = strings.Count(query, "?")
statementCounterM.Lock()
statementCounter++
stmt.name = fmt.Sprintf("stmt%d", statementCounter)
statementCounterM.Unlock()
cmd, err := conn.dynamic(stmt.name, query)
if err != nil {
stmt.Close()
return nil, err
}
rows, _, err := cmd.ConsumeResponse(ctx)
if err != nil {
stmt.Close()
cmd.Cancel()
return nil, fmt.Errorf("error reading response: %w", err)
}
if rows != nil {
rows.Close()
return nil, fmt.Errorf("received rows when creating prepared statement")
}
stmt.cmd = cmd
if err := stmt.fillColumnTypes(); err != nil {
stmt.Close()
return nil, fmt.Errorf("Failed to retrieve argument types: %w", err)
}
return stmt, nil
}
// Close implements the driver.Stmt interface.
func (stmt *statement) Close() error {
if stmt.cmd != nil {
name := C.CString(stmt.name)
defer C.free(unsafe.Pointer(name))
retval := C.ct_dynamic(stmt.cmd.cmd, C.CS_DEALLOC, name, C.CS_NULLTERM, nil, C.CS_UNUSED)
if retval != C.CS_SUCCEED {
return makeError(retval, "C.ct_dynamic with C.CS_DEALLOC failed")
}
retval = C.ct_send(stmt.cmd.cmd)
if retval != C.CS_SUCCEED {
return makeError(retval, "C.ct_send failed")
}
var err error
for err = nil; err != io.EOF; _, _, _, err = stmt.cmd.Response() {
if err != nil {
return err
}
}
}
return nil
}
// NumInput implements the driver.Stmt interface.
func (stmt *statement) NumInput() int {
return stmt.argCount
}
// TODO: Add doc
func (stmt *statement) exec(ctx context.Context, args []driver.NamedValue) (*Rows, *Result, error) {
if len(args) != stmt.argCount {
return nil, nil, fmt.Errorf("Mismatched argument count - expected %d, got %d",
stmt.argCount, len(args))
}
name := C.CString(stmt.name)
defer C.free(unsafe.Pointer(name))
retval := C.ct_dynamic(stmt.cmd.cmd, C.CS_EXECUTE, name, C.CS_NULLTERM, nil, C.CS_UNUSED)
if retval != C.CS_SUCCEED {
return nil, nil, makeError(retval, "C.ct_dynamic with CS_EXECUTE failed")
}
for i, arg := range args {
// TODO place binding in function to achieve an earlier free
datafmt := (*C.CS_DATAFMT)(C.calloc(1, C.sizeof_CS_DATAFMT))
defer C.free(unsafe.Pointer(datafmt))
datafmt.status = C.CS_INPUTVALUE
datafmt.namelen = C.CS_NULLTERM
switch stmt.columnTypes[i] {
case IMAGE:
datafmt.datatype = (C.CS_INT)(BINARY)
default:
datafmt.datatype = (C.CS_INT)(stmt.columnTypes[i])
}
// datalen is the length of the data in bytes.
datalen := 0
dataType := stmt.columnTypes[i].ToDataType()
var ptr unsafe.Pointer
// TODO: This entire case could be moved into a function, since
// the values set here are always the same - the expected values
// for ct_param.
// This function could also check for null values early.
switch stmt.columnTypes[i] {
case BIGINT, INT, SMALLINT, TINYINT, UBIGINT, UINT, USMALLINT, USHORT, FLOAT, REAL:
bs, err := dataType.Bytes(binary.LittleEndian, arg.Value)
if err != nil {
// TODO context
return nil, nil, err
}
ptr = C.CBytes(bs)
defer C.free(ptr)
case DECIMAL, NUMERIC:
bs, err := dataType.Bytes(binary.LittleEndian, arg.Value)
if err != nil {
return nil, nil, err
}
csDec := (*C.CS_DECIMAL)(C.calloc(1, C.sizeof_CS_DECIMAL))
defer C.free(unsafe.Pointer(csDec))
csDec.precision = (C.CS_BYTE)(arg.Value.(*asetypes.Decimal).Precision)
csDec.scale = (C.CS_BYTE)(arg.Value.(*asetypes.Decimal).Scale)
for i, b := range bs {
csDec.array[i] = (C.CS_BYTE)(b)
}
ptr = unsafe.Pointer(csDec)
case MONEY, MONEY4, DATE, TIME, DATETIME4, DATETIME, BIGDATETIME, BIGTIME:
bs, err := dataType.Bytes(binary.LittleEndian, arg.Value)
if err != nil {
return nil, nil, err
}
ptr = C.CBytes(bs)
defer C.free(ptr)
case CHAR:
ptr = unsafe.Pointer(C.CString(arg.Value.(string)))
defer C.free(ptr)
datalen = len(arg.Value.(string))
datafmt.format = C.CS_FMT_NULLTERM
datafmt.maxlength = C.CS_MAX_CHAR
case TEXT, LONGCHAR:
ptr = unsafe.Pointer(C.CString(arg.Value.(string)))
defer C.free(ptr)
datalen = len(arg.Value.(string))
datafmt.format = C.CS_FMT_NULLTERM
datafmt.maxlength = (C.CS_INT)(datalen)
case VARCHAR:
varchar := (*C.CS_VARCHAR)(C.calloc(1, C.sizeof_CS_VARCHAR))
defer C.free(unsafe.Pointer(varchar))
varchar.len = (C.CS_SMALLINT)(len(arg.Value.(string)))
for i, chr := range arg.Value.(string) {
varchar.str[i] = (C.CS_CHAR)(chr)
}
ptr = unsafe.Pointer(varchar)
case BINARY, IMAGE:
ptr = C.CBytes(arg.Value.([]byte))
defer C.free(ptr)
datalen = len(arg.Value.([]byte))
// IMAGE does not support null padding
if stmt.columnTypes[i] == BINARY {
datafmt.format = C.CS_FMT_PADNULL
}
// The maximum length of slices is constrained by the
// ability to address elements by integers - hence the
// maximum length we can retrieve is MaxInt64.
datafmt.maxlength = (C.CS_INT)(math.MaxInt32)
case VARBINARY:
varbin := (*C.CS_VARBINARY)(C.calloc(1, C.sizeof_CS_VARBINARY))
defer C.free(unsafe.Pointer(varbin))
varbin.len = (C.CS_SMALLINT)(len(arg.Value.([]byte)))
for i, b := range arg.Value.([]byte) {
varbin.array[i] = (C.CS_BYTE)(b)
}
ptr = unsafe.Pointer(varbin)
case BIT:
b := (C.CS_BOOL)(0)
if arg.Value.(bool) {
b = (C.CS_BOOL)(1)
}
ptr = unsafe.Pointer(&b)
datalen = 1
case UNICHAR, UNITEXT:
bs, err := dataType.Bytes(binary.LittleEndian, arg.Value)
if err != nil {
return nil, nil, err
}
ptr = unsafe.Pointer(C.CBytes(bs))
defer C.free(ptr)
datalen = len(bs)
datafmt.format = C.CS_FMT_NULLTERM
datafmt.maxlength = (C.CS_INT)(datalen)
default:
return nil, nil, fmt.Errorf("Unhandled column type: %s", stmt.columnTypes[i])
}
var csDatalen C.CS_INT
if datalen != C.CS_UNUSED {
csDatalen = (C.CS_INT)(datalen)
} else {
csDatalen = C.CS_UNUSED
}
retval = C.ct_param(stmt.cmd.cmd, datafmt, ptr, csDatalen, 0)
if retval != C.CS_SUCCEED {
return nil, nil, makeError(retval, "C.ct_param on parameter %d failed with argument '%v'", i, arg)
}
}
retval = C.ct_send(stmt.cmd.cmd)
if retval != C.CS_SUCCEED {
return nil, nil, makeError(retval, "C.ct_send failed")
}
return stmt.cmd.ConsumeResponse(ctx)
}
// Exec implements the driver.Stmt interface.
func (stmt *statement) Exec(args []driver.Value) (driver.Result, error) {
return stmt.ExecContext(context.Background(), dblib.ValuesToNamedValues(args))
}
// ExecContext implements the driver.StmtExecContext interface.
func (stmt *statement) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
_, result, err := stmt.exec(ctx, args)
return result, err
}
// Query implements the driver.Stmt interface.
func (stmt *statement) Query(args []driver.Value) (driver.Rows, error) {
return stmt.QueryContext(context.Background(), dblib.ValuesToNamedValues(args))
}
// QueryContext implements the driver.StmtQueryContext interface.
func (stmt *statement) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
rows, _, err := stmt.exec(ctx, args)
return rows, err
}
func (stmt *statement) fillColumnTypes() error {
name := C.CString(stmt.name)
defer C.free(unsafe.Pointer(name))
// Instruct server to send data to descriptor
retval := C.ct_dynamic(stmt.cmd.cmd, C.CS_DESCRIBE_INPUT, name,
C.CS_NULLTERM, nil, C.CS_UNUSED)
if retval != C.CS_SUCCEED {
return makeError(retval, "Error when preparing input description")
}
retval = C.ct_send(stmt.cmd.cmd)
if retval != C.CS_SUCCEED {
return makeError(retval, "Error sending command to server")
}
for {
_, _, resultType, err := stmt.cmd.Response()
if err != nil {
if err == io.EOF {
break
}
return fmt.Errorf("Received error while receiving input description: %w", err)
}
if resultType != C.CS_DESCRIBE_RESULT {
continue
}
// Receive number of arguments
var paramCount C.CS_INT
retval = C.ct_res_info(stmt.cmd.cmd, C.CS_NUMDATA, unsafe.Pointer(¶mCount), C.CS_UNUSED, nil)
if retval != C.CS_SUCCEED {
return makeError(retval, "Failed to retrieve parameter count")
}
stmt.argCount = int(paramCount)
stmt.columnTypes = make([]ASEType, stmt.argCount)
for i := 0; i < stmt.argCount; i++ {
datafmt := (*C.CS_DATAFMT)(C.calloc(1, C.sizeof_CS_DATAFMT))
retval = C.ct_describe(stmt.cmd.cmd, (C.CS_INT)(i+1), datafmt)
if retval != C.CS_SUCCEED {
return makeError(retval, "Failed to retrieve description of parameter %d", i)
}
stmt.columnTypes[i] = ASEType(datafmt.datatype)
}
}
return nil
}
// CheckNamedValue implements the driver.NamedValueChecker interface.
func (stmt statement) CheckNamedValue(named *driver.NamedValue) error {
index := named.Ordinal - 1
if index > len(stmt.columnTypes) {
return fmt.Errorf("cgo-ase: ordinal %d is larger than the number of columns %d",
named.Ordinal, len(stmt.columnTypes))
}
val, err := stmt.columnTypes[index].ToDataType().ConvertValue(named.Value)
if err != nil {
return fmt.Errorf("cgo-ase: error converting value: %w", err)
}
named.Value = val
return nil
}