forked from SAP/go-ase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrows.go
177 lines (154 loc) · 4.41 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
// SPDX-FileCopyrightText: 2020 SAP SE
// SPDX-FileCopyrightText: 2021 SAP SE
//
// SPDX-License-Identifier: Apache-2.0
package ase
import (
"context"
"database/sql/driver"
"errors"
"fmt"
"io"
"github.com/SAP/go-dblib/tds"
)
// Interface satisfaction checks.
var (
_ driver.Rows = (*Rows)(nil)
_ driver.RowsNextResultSet = (*Rows)(nil)
_ driver.RowsColumnTypeLength = (*Rows)(nil)
_ driver.RowsColumnTypeDatabaseTypeName = (*Rows)(nil)
)
// Rows implements the driver.Rows interface.
type Rows struct {
Conn *Conn
RowFmt *tds.RowFmtPackage
hasNextResultSet bool
}
// Columns implements the driver.Rows interface.
func (rows Rows) Columns() []string {
if rows.RowFmt == nil {
return []string{}
}
// TODO ignore hidden columns
response := make([]string, len(rows.RowFmt.Fmts))
for i, fieldFmt := range rows.RowFmt.Fmts {
// TODO check if RowFmt is wide and contains column label,
// catalogue, schema, table
response[i] = fieldFmt.Name()
}
return response
}
// Close implements the driver.Rows interface.
func (rows *Rows) Close() error {
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.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:
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 {
// 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:
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
}
// ColumnTypeLength implements the driver.RowsColumnTypeLength interface.
func (rows Rows) ColumnTypeLength(index int) (int64, bool) {
if index >= len(rows.RowFmt.Fmts) {
return 0, false
}
return rows.RowFmt.Fmts[index].MaxLength(), true
}
// ColumnTypeDatabaseTypeName implements the
// driver.RowsColumnTypeDatabaseTypeName interface.
func (rows Rows) ColumnTypeDatabaseTypeName(index int) string {
if index >= len(rows.RowFmt.Fmts) {
return ""
}
return string(rows.RowFmt.Fmts[index].DataType())
}