forked from SAP/cgo-ase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
248 lines (211 loc) · 6.11 KB
/
command.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
// 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"
"errors"
"fmt"
"io"
"unsafe"
)
// Command contains the C.command and indicates if that command is dynamic.
type Command struct {
cmd *C.CS_COMMAND
isDynamic bool
}
// GenericExec is the central method through which SQL statements are
// sent to ASE.
func (conn *Connection) GenericExec(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, driver.Result, error) {
if len(args) == 0 {
cmd, err := conn.NewCommand(ctx, query)
if err != nil {
return nil, nil, err
}
rows, result, err := cmd.ConsumeResponse(ctx)
if err != nil {
return nil, nil, err
}
if rows != nil {
return rows, result, nil
}
cmd.Drop()
return nil, result, nil
}
stmt, err := conn.prepare(ctx, query)
if err != nil {
// TODO
return nil, nil, err
}
defer stmt.Close()
for i := range args {
if err := stmt.CheckNamedValue(&args[i]); err != nil {
return nil, nil, fmt.Errorf("go-ase: error checking argument: %w", err)
}
}
return stmt.exec(ctx, args)
}
// NewCommand creates a new command.
func (conn *Connection) NewCommand(ctx context.Context, query string) (*Command, error) {
return conn.exec(ctx, query)
}
// Cancel cancels the current result set.
func (cmd *Command) Cancel() error {
retval := C.ct_cancel(nil, cmd.cmd, C.CS_CANCEL_ALL)
if retval != C.CS_SUCCEED {
return makeError(retval, "Error occurred while cancelling command")
}
return nil
}
// Drop deallocates the command.
func (cmd *Command) Drop() error {
retval := C.ct_cmd_drop(cmd.cmd)
if retval != C.CS_SUCCEED {
return makeError(retval, "Failed to drop command")
}
cmd.cmd = nil
return nil
}
// exec allocates, prepares and sends a command.
//
// The return values are the command structure, a function to deallocate
// the command structure and an error, if any occurred.
func (conn *Connection) exec(ctx context.Context, query string) (*Command, error) {
cmd := &Command{}
retval := C.ct_cmd_alloc(conn.conn, &cmd.cmd)
if retval != C.CS_SUCCEED {
return nil, makeError(retval, "Failed to allocate command structure")
}
sql := C.CString(query)
defer C.free(unsafe.Pointer(sql))
// Set language command
retval = C.ct_command(cmd.cmd, C.CS_LANG_CMD, sql, C.CS_NULLTERM, C.CS_UNUSED)
if retval != C.CS_SUCCEED {
cmd.Drop()
return nil, makeError(retval, "Failed to set language command")
}
// Send command to ASE
retval = C.ct_send(cmd.cmd)
if retval != C.CS_SUCCEED {
cmd.Drop()
return nil, makeError(retval, "Failed to send command")
}
return cmd, nil
}
// dynamic initializes a Command as a prepared statement.
func (conn *Connection) dynamic(name string, query string) (*Command, error) {
cmd := &Command{}
cmd.isDynamic = true
retval := C.ct_cmd_alloc(conn.conn, &cmd.cmd)
if retval != C.CS_SUCCEED {
return nil, makeError(retval, "Failed to allocate command structure")
}
// Initialize dynamic command
q := C.CString(query)
defer C.free(unsafe.Pointer(q))
n := C.CString(name)
defer C.free(unsafe.Pointer(n))
retval = C.ct_dynamic(cmd.cmd, C.CS_PREPARE, n, C.CS_NULLTERM, q, C.CS_NULLTERM)
if retval != C.CS_SUCCEED {
return nil, makeError(retval, "Failed to initialize dynamic command")
}
// Send command to ASE
retval = C.ct_send(cmd.cmd)
if retval != C.CS_SUCCEED {
cmd.Drop()
return nil, makeError(retval, "Failed to send command")
}
return cmd, nil
}
// Response reads a single response from the command structure and
// handles it.
//
// When no more results are available this method returns io.EOF.
//
// The third return value is a CS_INT, which may be the CS_RETCODE of
// ct_results when the command failed or finished or the result type if
// the result set requires further processing.
func (cmd *Command) Response() (*Rows, *Result, C.CS_INT, error) {
var resultType C.CS_INT
retval := C.ct_results(cmd.cmd, &resultType)
switch retval {
case C.CS_SUCCEED:
// handle result type
break
case C.CS_END_RESULTS:
return nil, nil, retval, io.EOF // no more responses available, quit
case C.CS_FAIL:
cmd.Cancel()
return nil, nil, retval, makeError(retval, "Command failed")
default:
cmd.Cancel()
return nil, nil, retval, makeError(retval, "Invalid return code")
}
switch resultType {
// fetchable results
case C.CS_COMPUTE_RESULT, C.CS_CURSOR_RESULT, C.CS_PARAM_RESULT:
fallthrough
case C.CS_ROW_RESULT, C.CS_STATUS_RESULT:
rows, err := newRows(cmd)
if err != nil {
cmd.Cancel()
return nil, nil, C.CS_UNUSED, err
}
return rows, nil, C.CS_UNUSED, nil
// non-fetchable results
case C.CS_COMPUTEFMT_RESULT, C.CS_MSG_RESULT, C.CS_ROWFMT_RESULT, C.CS_DESCRIBE_RESULT:
return nil, nil, resultType, nil
// other result types
case C.CS_CMD_FAIL:
cmd.Cancel()
return nil, nil, C.CS_UNUSED, makeError(retval, "Command failed, cancelled")
case C.CS_CMD_DONE:
var rowsAffected C.CS_INT
retval := C.ct_res_info(cmd.cmd, C.CS_ROW_COUNT, unsafe.Pointer(&rowsAffected),
C.CS_UNUSED, nil)
if retval != C.CS_SUCCEED {
cmd.Cancel()
return nil, nil, C.CS_UNUSED, makeError(retval, "Failed to read affected rows")
}
return nil, &Result{int64(rowsAffected)}, C.CS_UNUSED, nil
case C.CS_CMD_SUCCEED:
return nil, nil, C.CS_UNUSED, nil
default:
cmd.Cancel()
return nil, nil, resultType, fmt.Errorf("Unknown result type: %d", resultType)
}
}
// ConsumeResponse is a wrapper around .Response that guarantees that
// all results have been read.
func (cmd *Command) ConsumeResponse(ctx context.Context) (*Rows, *Result, error) {
var resResult *Result
outer:
for {
select {
case <-ctx.Done():
break outer
default:
rows, result, _, err := cmd.Response()
if err != nil {
if errors.Is(err, io.EOF) {
break outer
}
return nil, nil, fmt.Errorf("go-ase: received error reading results: %w", err)
}
if result != nil {
resResult = result
}
if rows != nil {
return rows, result, nil
}
}
}
return nil, resResult, nil
}