-
Notifications
You must be signed in to change notification settings - Fork 54
/
result.go
179 lines (151 loc) · 5.07 KB
/
result.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
package protocol
import (
"database/sql/driver"
"fmt"
"reflect"
"github.com/SAP/go-hdb/driver/internal/protocol/encoding"
)
type columnOptions int8
const (
coMandatory columnOptions = 0x01
coOptional columnOptions = 0x02
)
const (
coMandatoryText = "mandatory"
coOptionalText = "optional"
)
func (k columnOptions) String() string {
var s []string
if k&coMandatory != 0 {
s = append(s, coMandatoryText)
}
if k&coOptional != 0 {
s = append(s, coOptionalText)
}
return fmt.Sprintf("%v", s)
}
// ResultsetID represents a resultset id.
type ResultsetID uint64
func (id ResultsetID) String() string { return fmt.Sprintf("%d", id) }
func (id *ResultsetID) decode(dec *encoding.Decoder) error {
*id = ResultsetID(dec.Uint64())
return dec.Error()
}
func (id ResultsetID) encode(enc *encoding.Encoder) error { enc.Uint64(uint64(id)); return nil }
func newResultFields(size int) []*ResultField {
return make([]*ResultField, size)
}
// ResultField represents a database result field.
type ResultField struct {
names *fieldNames
tableNameOfs uint32
schemaNameOfs uint32
columnNameOfs uint32
columnDisplayNameOfs uint32
prec int // length
scale int // fraction
columnOptions columnOptions
tc typeCode
}
// String implements the Stringer interface.
func (f *ResultField) String() string {
return fmt.Sprintf("columnsOptions %s typeCode %s precision %d scale %d tablename %s schemaname %s columnname %s columnDisplayname %s",
f.columnOptions,
f.tc,
f.prec,
f.scale,
f.names.name(f.tableNameOfs),
f.names.name(f.schemaNameOfs),
f.names.name(f.columnNameOfs),
f.names.name(f.columnDisplayNameOfs),
)
}
func (f *ResultField) isNullable() bool { return f.columnOptions == coOptional }
// DatabaseTypeName returns the type name of the field.
// It implements the go-hdb driver ColumnType interface.
func (f *ResultField) DatabaseTypeName() string { return f.tc.typeName() }
// DecimalSize returns the type precision and scale of the field.
// It implements the go-hdb driver ColumnType interface.
func (f *ResultField) DecimalSize() (int64, int64, bool) {
if f.tc.isDecimalType() {
return int64(f.prec), int64(f.scale), true
}
return 0, 0, false
}
// Length returns the type length of the field.
// It implements the go-hdb driver ColumnType interface.
func (f *ResultField) Length() (int64, bool) {
if f.tc.isVariableLength() {
return int64(f.prec), true
}
return 0, false
}
// Name returns the result field name.
// It implements the go-hdb driver ColumnType interface.
func (f *ResultField) Name() string { return f.names.name(f.columnDisplayNameOfs) }
// Nullable returns true if the field may be null, false otherwise.
// It implements the go-hdb driver ColumnType interface.
func (f *ResultField) Nullable() (bool, bool) { return f.isNullable(), true }
// ScanType returns the scan type of the field.
// It implements the go-hdb driver ColumnType interface.
func (f *ResultField) ScanType() reflect.Type { return f.tc.dataType().ScanType(f.isNullable()) }
func (f *ResultField) decode(dec *encoding.Decoder) {
f.columnOptions = columnOptions(dec.Int8())
f.tc = typeCode(dec.Int8())
f.scale = int(dec.Int16())
f.prec = int(dec.Int16())
dec.Skip(2) // filler
f.tableNameOfs = dec.Uint32()
f.schemaNameOfs = dec.Uint32()
f.columnNameOfs = dec.Uint32()
f.columnDisplayNameOfs = dec.Uint32()
f.names.insert(f.tableNameOfs)
f.names.insert(f.schemaNameOfs)
f.names.insert(f.columnNameOfs)
f.names.insert(f.columnDisplayNameOfs)
}
func (f *ResultField) decodeResult(dec *encoding.Decoder, readFn lobReadFn, lobChunkSize int) (any, error) {
return decodeResult(f.tc, dec, readFn, lobChunkSize, f.scale)
}
// ResultMetadata represents the metadata of a set of database result fields.
type ResultMetadata struct {
ResultFields []*ResultField
}
func (r *ResultMetadata) String() string {
return fmt.Sprintf("result fields %v", r.ResultFields)
}
func (r *ResultMetadata) decodeNumArg(dec *encoding.Decoder, numArg int) error {
r.ResultFields = newResultFields(numArg)
names := &fieldNames{}
for i := 0; i < len(r.ResultFields); i++ {
f := &ResultField{names: names}
f.decode(dec)
r.ResultFields[i] = f
}
if err := names.decode(dec); err != nil {
return err
}
return dec.Error()
}
// Resultset represents a database result set.
type Resultset struct {
ResultFields []*ResultField
FieldValues []driver.Value
DecodeErrors DecodeErrors
}
func (r *Resultset) String() string {
return fmt.Sprintf("result fields %v field values %v", r.ResultFields, r.FieldValues)
}
func (r *Resultset) decodeResult(dec *encoding.Decoder, numArg int, readFn lobReadFn, lobChunkSize int) error {
cols := len(r.ResultFields)
r.FieldValues = resizeSlice(r.FieldValues, numArg*cols)
for i := 0; i < numArg; i++ {
for j, f := range r.ResultFields {
var err error
if r.FieldValues[i*cols+j], err = f.decodeResult(dec, readFn, lobChunkSize); err != nil {
r.DecodeErrors = append(r.DecodeErrors, &DecodeError{row: i, fieldName: f.Name(), err: err}) // collect decode / conversion errors
}
}
}
return dec.Error()
}