-
-
Notifications
You must be signed in to change notification settings - Fork 955
Expand file tree
/
Copy pathrows.go
More file actions
273 lines (246 loc) · 6.03 KB
/
rows.go
File metadata and controls
273 lines (246 loc) · 6.03 KB
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
package pq
import (
"database/sql/driver"
"fmt"
"io"
"math"
"reflect"
"time"
"github.com/lib/pq/internal/proto"
"github.com/lib/pq/oid"
)
type noRows struct{}
var emptyRows noRows
var _ driver.Result = noRows{}
func (noRows) LastInsertId() (int64, error) { return 0, errNoLastInsertID }
func (noRows) RowsAffected() (int64, error) { return 0, errNoRowsAffected }
type (
rowsHeader struct {
colNames []string
colTyps []fieldDesc
colFmts []format
}
rows struct {
cn *conn
finish func()
rowsHeader
done bool
rb readBuf
result driver.Result
tag string
next *rowsHeader
}
)
func (rs *rows) Close() error {
if rs.finish != nil {
defer rs.finish()
}
// no need to look at cn.bad as Next() will
for {
err := rs.Next(nil)
switch err {
case nil:
case io.EOF:
// rs.Next can return io.EOF on both ReadyForQuery and
// RowDescription (used with HasNextResultSet). We need to fetch
// messages until we hit a ReadyForQuery, which is done by waiting
// for done to be set.
if rs.done {
return nil
}
default:
return err
}
}
}
func (rs *rows) Columns() []string {
return rs.colNames
}
func (rs *rows) Result() driver.Result {
if rs.result == nil {
return emptyRows
}
return rs.result
}
func (rs *rows) Tag() string {
return rs.tag
}
func (rs *rows) Next(dest []driver.Value) (resErr error) {
if rs.done {
return io.EOF
}
if err := rs.cn.err.getForNext(); err != nil {
return err
}
for {
t, err := rs.cn.recv1Buf(&rs.rb)
if err != nil {
return rs.cn.handleError(err)
}
switch t {
case proto.ErrorResponse:
resErr = parseError(&rs.rb, "")
case proto.CommandComplete, proto.EmptyQueryResponse:
if t == proto.CommandComplete {
rs.result, rs.tag, err = rs.cn.parseComplete(rs.rb.string())
if err != nil {
return rs.cn.handleError(err)
}
}
continue
case proto.ReadyForQuery:
rs.cn.processReadyForQuery(&rs.rb)
rs.done = true
if resErr != nil {
return rs.cn.handleError(resErr)
}
return io.EOF
case proto.DataRow:
n := rs.rb.int16()
if resErr != nil {
rs.cn.err.set(driver.ErrBadConn)
return fmt.Errorf("pq: unexpected DataRow after error %s", resErr)
}
if n < len(dest) {
dest = dest[:n]
}
for i := range dest {
l := rs.rb.int32()
if l == -1 {
dest[i] = nil
continue
}
dest[i], err = decode(&rs.cn.parameterStatus, rs.rb.next(l), rs.colTyps[i].OID, rs.colFmts[i])
if err != nil {
return rs.cn.handleError(err)
}
}
return rs.cn.handleError(resErr)
case proto.RowDescription:
next := parsePortalRowDescribe(&rs.rb)
rs.next = &next
return io.EOF
default:
return fmt.Errorf("pq: unexpected message after execute: %q", t)
}
}
}
func (rs *rows) HasNextResultSet() bool {
hasNext := rs.next != nil && !rs.done
return hasNext
}
func (rs *rows) NextResultSet() error {
if rs.next == nil {
return io.EOF
}
rs.rowsHeader = *rs.next
rs.next = nil
return nil
}
// ColumnTypeScanType returns the value type that can be used to scan types into.
func (rs *rows) ColumnTypeScanType(index int) reflect.Type {
return rs.colTyps[index].Type()
}
// ColumnTypeDatabaseTypeName return the database system type name.
func (rs *rows) ColumnTypeDatabaseTypeName(index int) string {
if rs.cn.parameterStatus.isRedshift {
if n, ok := redshiftTypeName[rs.colTyps[index].OID]; ok {
return n
}
}
return rs.colTyps[index].Name()
}
// ColumnTypeLength returns the length of the column type if the column is a
// variable length type. If the column is not a variable length type ok
// should return false.
func (rs *rows) ColumnTypeLength(index int) (length int64, ok bool) {
return rs.colTyps[index].Length()
}
// ColumnTypePrecisionScale should return the precision and scale for decimal
// types. If not applicable, ok should be false.
func (rs *rows) ColumnTypePrecisionScale(index int) (precision, scale int64, ok bool) {
return rs.colTyps[index].PrecisionScale()
}
const headerSize = 4
type fieldDesc struct {
// The object ID of the data type.
OID oid.Oid
// The data type size (see pg_type.typlen).
// Note that negative values denote variable-width types.
Len int
// The type modifier (see pg_attribute.atttypmod).
// The meaning of the modifier is type-specific.
Mod int
}
func (fd fieldDesc) Type() reflect.Type {
switch fd.OID {
case oid.T_int8:
return reflect.TypeFor[int64]()
case oid.T_int4:
return reflect.TypeFor[int32]()
case oid.T_int2:
return reflect.TypeFor[int16]()
case oid.T_float8:
return reflect.TypeFor[float64]()
case oid.T_float4:
return reflect.TypeFor[float32]()
case oid.T_varchar, oid.T_text, oid.T_varbit, oid.T_bit:
return reflect.TypeFor[string]()
case oid.T_bool:
return reflect.TypeFor[bool]()
case oid.T_date, oid.T_time, oid.T_timetz, oid.T_timestamp, oid.T_timestamptz:
return reflect.TypeFor[time.Time]()
case oid.T_bytea:
return reflect.TypeFor[[]byte]()
default:
return reflect.TypeFor[any]()
}
}
func (fd fieldDesc) Name() string {
return oid.TypeName[fd.OID]
}
func (fd fieldDesc) Length() (length int64, ok bool) {
switch fd.OID {
case oid.T_text, oid.T_bytea:
return math.MaxInt64, true
case oid.T_varchar, oid.T_bpchar:
return int64(fd.Mod - headerSize), true
case oid.T_varbit, oid.T_bit:
return int64(fd.Mod), true
default:
return 0, false
}
}
func (fd fieldDesc) PrecisionScale() (precision, scale int64, ok bool) {
switch fd.OID {
case oid.T_numeric, oid.T__numeric:
mod := fd.Mod - headerSize
precision = int64((mod >> 16) & 0xffff)
scale = int64(mod & 0xffff)
return precision, scale, true
default:
return 0, 0, false
}
}
var redshiftTypeName = map[oid.Oid]string{
86: "PG_SHADOW",
87: "PG_GROUP",
88: "PG_DATABASE",
90: "PG_TABLESPACE",
635: "_SPECTRUM_ARRAY",
636: "_SPECTRUM_MAP",
637: "_SPECTRUM_STRUCT",
1188: "INTERVALY2M",
1189: "_INTERVALY2M",
1190: "INTERVALD2S",
1191: "_INTERVALD2S",
2935: "HLLSKETCH",
3000: "GEOMETRY",
3001: "GEOGRAPHY",
4000: "SUPER",
4600: "USERITEM",
4601: "_USERITEM",
4602: "ROLEITEM",
4603: "_ROLEITEM",
6551: "VARBYTE",
}