-
Notifications
You must be signed in to change notification settings - Fork 15
/
rows.go
309 lines (262 loc) · 7.83 KB
/
rows.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
// 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
import (
"context"
"database/sql/driver"
"errors"
"fmt"
"io"
"reflect"
"github.com/SAP/go-dblib/tds"
)
// baseRows is used to share common code between Rows and CursorRows.
type baseRows struct {
// rowFmt is a workaround to supply the current RowFmtPackage
// dynamically, as it may change and CursorRows only has access to
// it through the Cursor.
rowFmt func() *tds.RowFmtPackage
// closed effectively disables a rows object if a valid rows object
// is required but won't receive any data.
//
// An example for this are the Direct-/GenericExec methods, which
// return both a valid Rows and a valid Result.
// In the case of update statements only the Rows would not receive
// any data and hence must be prevented from attempting to consume
// packages.
//
// Another example is for rows resulting in a cursor to close the
// underlying channel used to buffer fields and prevent consuming
// more packages.
closed bool
}
func (rows baseRows) isClosed() bool {
// This method solely exists to silence the check from
// exhaustivestruct, which reports the attribute 'closed' as unused
// despite it being used in structs embedding baseRows.
return rows.closed
}
// fmts returns the list of tds.FieldFmts in the current result set.
//
// FieldFmts marked as hidden are not returned.
func (rows baseRows) fmts() []tds.FieldFmt {
if rows.rowFmt() == nil {
return nil
}
fmts := []tds.FieldFmt{}
for _, fieldFmt := range rows.rowFmt().Fmts {
// ignore hidden columns
if fmtStatus := tds.RowFmtStatus(fieldFmt.Status()); fmtStatus&tds.TDS_ROW_HIDDEN == tds.TDS_ROW_HIDDEN {
continue
}
fmts = append(fmts, fieldFmt)
}
return fmts
}
// Columns implements the driver.Rows interface.
func (rows baseRows) Columns() []string {
fmts := rows.fmts()
if len(fmts) == 0 {
return []string{}
}
response := make([]string, len(fmts))
for i, fieldFmt := range fmts {
// TODO check if RowFmt is wide and contains column label,
// catalogue, schema, table
response[i] = fieldFmt.Name()
}
return response
}
// ColumnTypeLength implements the driver.RowsColumnTypeLength interface.
func (rows baseRows) ColumnTypeLength(index int) (int64, bool) {
if index >= len(rows.fmts()) {
return 0, false
}
return rows.fmts()[index].MaxLength(), true
}
// ColumnTypeDisplayLength returns a best guess of the maximum length
// required to display the values of the column.
func (rows baseRows) ColumnTypeDisplayLength(index int) (int64, bool) {
if index >= len(rows.fmts()) {
return 0, false
}
return rows.fmts()[index].DisplayMaxLength(), true
}
// ColumnTypeDatabaseTypeName implements the
// driver.RowsColumnTypeDatabaseTypeName interface.
func (rows baseRows) ColumnTypeDatabaseTypeName(index int) string {
if index >= len(rows.fmts()) {
return ""
}
return rows.fmts()[index].DataType().String()
}
// ColumnTypePrecisionScale implements the
// driver.RowsColumnTypePrecisionScale interface.
func (rows baseRows) ColumnTypePrecisionScale(index int) (int64, int64, bool) {
if index >= len(rows.fmts()) {
return 0, 0, false
}
type PrecisionScaler interface {
Precision() uint8
Scale() uint8
}
colType, ok := rows.fmts()[index].(PrecisionScaler)
if !ok {
return 0, 0, false
}
return int64(colType.Precision()), int64(colType.Scale()), true
}
// ColumnTypeNullable implements the
// driver.RowsColumnTypeNullable interface.
func (rows baseRows) ColumnTypeNullable(index int) (bool, bool) {
if index >= len(rows.fmts()) {
return false, false
}
if rows.fmts()[index].Status()&uint(tds.TDS_ROW_NULLALLOWED) != uint(tds.TDS_ROW_NULLALLOWED) {
return false, true
}
return true, true
}
// ColumnTypeScanType implements the
// driver.RowsColumnTypeScanType interface.
func (rows baseRows) ColumnTypeScanType(index int) reflect.Type {
if index >= len(rows.fmts()) {
return nil
}
return rows.fmts()[index].DataType().GoReflectType()
}
// Interface satisfaction checks.
var (
_ driver.Rows = (*Rows)(nil)
_ driver.RowsNextResultSet = (*Rows)(nil)
_ driver.RowsColumnTypeLength = (*Rows)(nil)
_ driver.RowsColumnTypeDatabaseTypeName = (*Rows)(nil)
_ driver.RowsColumnTypePrecisionScale = (*Rows)(nil)
_ driver.RowsColumnTypeNullable = (*Rows)(nil)
_ driver.RowsColumnTypeScanType = (*Rows)(nil)
)
// Rows implements the driver.Rows interface.
type Rows struct {
Conn *Conn
baseRows
RowFmt *tds.RowFmtPackage
hasNextResultSet bool
}
func (conn *Conn) NewRows() *Rows {
rows := new(Rows)
rows.Conn = conn
rows.rowFmt = func() *tds.RowFmtPackage { return rows.RowFmt }
return rows
}
// Close implements the driver.Rows interface.
func (rows *Rows) Close() error {
if rows.isClosed() {
return nil
}
defer func() {
rows.closed = true
}()
for {
if err := rows.NextResultSet(); err != nil {
if errors.Is(err, io.EOF) {
break
}
return fmt.Errorf("go-ase: error consuming result sets: %w", err)
}
}
return nil
}
// Next implements the driver.Rows interface.
func (rows *Rows) Next(dst []driver.Value) error {
if rows.isClosed() || (rows.rowFmt() == nil && len(dst) == 0) {
return io.EOF
}
_, err := rows.Conn.Channel.NextPackageUntil(context.Background(), true,
func(pkg tds.Package) (bool, error) {
switch typed := pkg.(type) {
case *tds.RowPackage:
if len(dst) != len(typed.DataFields) {
return true, fmt.Errorf("go-ase: received invalid number of destinations, expecting %d destinations, got %d", len(typed.DataFields), len(dst))
}
for i := range typed.DataFields {
dst[i] = typed.DataFields[i].Value()
}
return true, nil
case *tds.RowFmtPackage:
rows.RowFmt = typed
rows.hasNextResultSet = true
return false, io.EOF
case *tds.OrderByPackage, *tds.OrderBy2Package:
return false, nil
case *tds.DonePackage:
ok, err := handleDonePackage(typed)
if err != nil {
return true, fmt.Errorf("go-ase: %w", err)
}
return ok, nil
case *tds.ReturnStatusPackage:
if typed.ReturnValue != 0 {
return true, fmt.Errorf("go-ase: query failed with return status %d", typed.ReturnValue)
}
return false, nil
default:
return true, fmt.Errorf("unhandled package type %T: %v", pkg, pkg)
}
},
)
if err != nil {
// database/sql expects only an io.EOF - it doesn't check with
// errors.Is.
if errors.Is(err, io.EOF) {
return io.EOF
}
return fmt.Errorf("go-ase: error reading next row package: %w", err)
}
return nil
}
// HasNextResultSet implements the driver.RowsNextResultSet interface.
func (rows *Rows) HasNextResultSet() bool {
if !rows.hasNextResultSet {
return false
}
rows.hasNextResultSet = false
return true
}
// NextResultSet implements the driver.RowsNextResultSet interface.
func (rows *Rows) NextResultSet() error {
if rows.isClosed() {
return io.EOF
}
// discard all RowPackage until either end of communication or next
// RowFmtPackage
_, err := rows.Conn.Channel.NextPackageUntil(context.Background(), false,
func(pkg tds.Package) (bool, error) {
switch typed := pkg.(type) {
case *tds.RowFmtPackage:
rows.RowFmt = typed
rows.hasNextResultSet = true
return false, nil
case *tds.RowPackage, *tds.OrderByPackage, *tds.OrderBy2Package:
return true, nil
case *tds.DonePackage:
if typed.Status&tds.TDS_DONE_MORE == tds.TDS_DONE_MORE {
return false, nil
}
return true, fmt.Errorf("go-ase: no next result set: %w", io.EOF)
default:
return false, fmt.Errorf("unhandled package type %T: %v", pkg, pkg)
}
},
)
if err != nil {
if errors.Is(err, tds.ErrNoPackageReady) || errors.Is(err, io.EOF) {
return io.EOF
}
return fmt.Errorf("go-ase: error reading next package: %w", err)
}
return nil
}