forked from pubnative/mysqldriver-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.go
388 lines (339 loc) · 10.1 KB
/
query.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package mysqldriver
import (
"strconv"
"github.com/pubnative/mysqlproto-go"
)
// Rows represents result set of SELECT query
type Rows struct {
resultSet mysqlproto.ResultSet
packet []byte
offset uint64
eof bool
err error
}
// Next moves cursor to the next unread row.
// It returns false when there are no more rows left
// or an error occurred during reading rows (see LastError() function)
// This function must be called before reading first row
// and continue being called until it returns false.
// rows, _ := conn.Query("SELECT * FROM people LIMIT 2")
// rows.Next() // move cursor to the first row
// // read values from the first row
// rows.Next() // move cursor to the second row
// // read values from the second row
// rows.Next() // drain the stream
// Best practice is to call Next() function in a loop:
// rows, _ := conn.Query("SELECT * FROM people")
// for rows.Next() {
// // read values from the row
// }
// It's required to read all rows before performing another query
// because connection contains sequential stream of rows.
// rows, _ := conn.Query("SELECT name FROM dogs LIMIT 1")
// rows.Next() // move cursor to the first row
// rows.String() // dog's name
// rows, _ = conn.Query("SELECT name FROM cats LIMIT 2")
// rows.Next() // move cursor to the second row of first query
// rows.String() // still dog's name
// rows.Next() // returns false. closes the first stream of rows
// rows.Next() // move cursor to the first row of second query
// rows.String() // cat's name
// rows.Next() // returns false. closes the second stream of rows
func (r *Rows) Next() bool {
if r.eof {
return false
}
if r.err != nil {
return false
}
packet, err := r.resultSet.Row()
if err != nil {
r.err = err
r.eof = true
return false
}
if packet == nil {
r.eof = true
return false
} else {
r.packet = packet
r.offset = 0
return true
}
}
// Bytes returns value as slice of bytes.
// NULL value is represented as empty slice.
func (r *Rows) Bytes() []byte {
value, _ := r.NullBytes()
return value
}
// NullBytes returns value as a slice of bytes
// and NULL indicator. When value is NULL, second parameter is true.
func (r *Rows) NullBytes() ([]byte, bool) {
value, offset, null := mysqlproto.ReadRowValue(r.packet, r.offset)
r.offset = offset
return value, null
}
// String returns value as a string.
// NULL value is represented as an empty string.
func (r *Rows) String() string {
value, _ := r.NullString()
return value
}
// NullString returns string as a value and
// NULL indicator. When value is NULL, second parameter is true.
func (r *Rows) NullString() (string, bool) {
data, null := r.NullBytes()
return string(data), null
}
// Int returns value as an int.
// NULL value is represented as 0.
// Int method uses strconv.Atoi to convert string into int.
// (see https://golang.org/pkg/strconv/#Atoi)
func (r *Rows) Int() int {
num, _ := r.NullInt()
return num
}
// NullInt returns value as an int and NULL indicator.
// When value is NULL, second parameter is true.
// NullInt method uses strconv.Atoi to convert string into int.
// (see https://golang.org/pkg/strconv/#Atoi)
func (r *Rows) NullInt() (int, bool) {
str, null := r.NullString()
if null {
return 0, true
}
num, err := strconv.Atoi(str)
if err != nil {
r.err = err
}
return num, false
}
// Int8 returns value as an int8.
// NULL value is represented as 0.
// Int8 method uses strconv.ParseInt to convert string into int8.
// (see https://golang.org/pkg/strconv/#ParseInt)
func (r *Rows) Int8() int8 {
num, _ := r.NullInt8()
return num
}
// NullInt8 returns value as an int8 and NULL indicator.
// When value is NULL, second parameter is true.
// NullInt8 method uses strconv.ParseInt to convert string into int8.
// (see https://golang.org/pkg/strconv/#ParseInt)
func (r *Rows) NullInt8() (int8, bool) {
str, null := r.NullString()
if null {
return 0, true
}
num, err := strconv.ParseInt(str, 10, 8)
if err != nil {
r.err = err
}
return int8(num), false
}
// Int16 returns value as an int16.
// NULL value is represented as 0.
// Int16 method uses strconv.ParseInt to convert string into int16.
// (see https://golang.org/pkg/strconv/#ParseInt)
func (r *Rows) Int16() int16 {
num, _ := r.NullInt16()
return num
}
// NullInt16 returns value as an int8 and NULL indicator.
// When value is NULL, second parameter is true.
// NullInt16 method uses strconv.ParseInt to convert string into int16.
// (see https://golang.org/pkg/strconv/#ParseInt)
func (r *Rows) NullInt16() (int16, bool) {
str, null := r.NullString()
if null {
return 0, true
}
num, err := strconv.ParseInt(str, 10, 16)
if err != nil {
r.err = err
}
return int16(num), false
}
// Int32 returns value as an int32.
// NULL value is represented as 0.
// Int32 method uses strconv.ParseInt to convert string into int32.
// (see https://golang.org/pkg/strconv/#ParseInt)
func (r *Rows) Int32() int32 {
num, _ := r.NullInt32()
return num
}
// NullInt32 returns value as an int32 and NULL indicator.
// When value is NULL, second parameter is true.
// NullInt32 method uses strconv.ParseInt to convert string into int32.
// (see https://golang.org/pkg/strconv/#ParseInt)
func (r *Rows) NullInt32() (int32, bool) {
str, null := r.NullString()
if null {
return 0, true
}
num, err := strconv.ParseInt(str, 10, 32)
if err != nil {
r.err = err
}
return int32(num), false
}
// Int64 returns value as an int64.
// NULL value is represented as 0.
// Int64 method uses strconv.ParseInt to convert string into int64.
// (see https://golang.org/pkg/strconv/#ParseInt)
func (r *Rows) Int64() int64 {
num, _ := r.NullInt64()
return num
}
// NullInt64 returns value as an int64 and NULL indicator.
// When value is NULL, second parameter is true.
// NullInt64 method uses strconv.ParseInt to convert string into int64.
// (see https://golang.org/pkg/strconv/#ParseInt)
func (r *Rows) NullInt64() (int64, bool) {
str, null := r.NullString()
if null {
return 0, true
}
num, err := strconv.ParseInt(str, 10, 64)
if err != nil {
r.err = err
}
return int64(num), false
}
// Float32 returns value as a float32.
// NULL value is represented as 0.0.
// Float32 method uses strconv.ParseFloat to convert string into float32.
// (see https://golang.org/pkg/strconv/#ParseFloat)
func (r *Rows) Float32() float32 {
num, _ := r.NullFloat32()
return num
}
// NullFloat32 returns value as a float32 and NULL indicator.
// When value is NULL, second parameter is true.
// NullFloat32 method uses strconv.ParseFloat to convert string into float32.
// (see https://golang.org/pkg/strconv/#ParseFloat)
func (r *Rows) NullFloat32() (float32, bool) {
str, null := r.NullString()
if null {
return 0, true
}
num, err := strconv.ParseFloat(str, 32)
if err != nil {
r.err = err
}
return float32(num), false
}
// Float64 returns value as a float64.
// NULL value is represented as 0.0.
// Float64 method uses strconv.ParseFloat to convert string into float64.
// (see https://golang.org/pkg/strconv/#ParseFloat)
func (r *Rows) Float64() float64 {
num, _ := r.NullFloat64()
return num
}
// NullFloat64 returns value as a float64 and NULL indicator.
// When value is NULL, second parameter is true.
// NullFloat64 method uses strconv.ParseFloat to convert string into float64.
// (see https://golang.org/pkg/strconv/#ParseFloat)
func (r *Rows) NullFloat64() (float64, bool) {
str, null := r.NullString()
if null {
return 0, true
}
num, err := strconv.ParseFloat(str, 64)
if err != nil {
r.err = err
}
return num, false
}
// Bool returns value as a bool.
// NULL value is represented as false.
// Bool method uses strconv.ParseBool to convert string into bool.
// (see https://golang.org/pkg/strconv/#ParseBool)
func (r *Rows) Bool() bool {
b, _ := r.NullBool()
return b
}
// NullBool returns value as a bool and NULL indicator.
// When value is NULL, second parameter is true.
// NullBool method uses strconv.ParseBool to convert string into bool.
// (see https://golang.org/pkg/strconv/#ParseBool)
func (r *Rows) NullBool() (bool, bool) {
str, null := r.NullString()
if null {
return false, true
}
b, err := strconv.ParseBool(str)
if err != nil {
r.err = err
}
return b, false
}
// LastError returns the error if any occurred during
// reading result set of SELECT query. This method should
// be always called after reading all rows.
// rows, err := conn.Query("SELECT * FROM dogs")
// if err != nil {
// // handle error
// }
// for rows.Next() {
// // read values
// }
// if err = rows.LastError(); err != nil {
// // handle error
// }
func (r *Rows) LastError() error {
return r.err
}
// Query function is used only for SELECT query.
// For all other queries and commands see func (c Conn) Exec
func (c *Conn) Query(sql string) (*Rows, error) {
req := mysqlproto.ComQueryRequest([]byte(sql))
if _, err := c.conn.Write(req); err != nil {
c.valid = false
return nil, err
}
resultSet, err := mysqlproto.ComQueryResponse(c.conn)
if err != nil {
if _, ok := err.(mysqlproto.ERRPacket); !ok {
c.valid = false
}
return nil, err
}
return &Rows{resultSet: resultSet}, nil
}
// Exec executes queries or other commands which expect to return OK_PACKET
// including INSERT/UPDATE/DELETE queries. For SELECT query see func (Conn) Query
// okPacket, err := conn.Exec("DELETE FROM dogs WHERE id = 1")
// if err == nil {
// return nil // query was performed successfully
// }
// if errPacket, ok := err.(mysqlproto.ERRPacket); ok {
// return errPacket // retrieve more information about the error
// } else {
// return err // generic error
// }
func (c *Conn) Exec(sql string) (mysqlproto.OKPacket, error) {
req := mysqlproto.ComQueryRequest([]byte(sql))
if _, err := c.conn.Write(req); err != nil {
c.valid = false
return mysqlproto.OKPacket{}, err
}
packet, err := c.conn.NextPacket()
if err != nil {
c.valid = false
return mysqlproto.OKPacket{}, err
}
if packet.Payload[0] == mysqlproto.OK_PACKET {
pkt, err := mysqlproto.ParseOKPacket(packet.Payload, c.conn.CapabilityFlags)
return pkt, err
} else {
pkt, err := mysqlproto.ParseERRPacket(packet.Payload, c.conn.CapabilityFlags)
if err == nil {
return mysqlproto.OKPacket{}, pkt
} else {
return mysqlproto.OKPacket{}, err
}
}
}