-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathconn_result.go
167 lines (135 loc) · 3.31 KB
/
conn_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
package mysql
/*
#include "cgo.h"
*/
import "C"
import (
"unsafe"
)
const (
// NOTE(szopa): maxSize used to be 1 << 30, but that causes
// compiler errors in some situations.
maxSize = 1 << 20
)
// Result is the structure returned by the mysql library.
// When transmitted over the wire, the Rows all come back as strings
// and lose their original use Fields.Type to convert
// them back if needed, using the following functions.
type connResult struct {
m *C.MYSQL
c C.MY_RES
}
func (res *connResult) RowsAffected() int64 {
return int64(res.c.affected_rows)
}
func (res *connResult) InsertId() int64 {
return int64(res.c.insert_id)
}
func (res *connResult) close() {
C.my_close_result(res.m, &res.c)
}
type connQueryResult struct {
connResult
conn *Connection
fields []Field
}
func fetchFields(c C.MY_RES_META) []Field {
nfields := int(c.num_fields)
if nfields == 0 {
return nil
}
cfields := (*[maxSize]C.MYSQL_FIELD)(unsafe.Pointer(c.fields))
totalLength := uint64(0)
for i := 0; i < nfields; i++ {
totalLength += uint64(cfields[i].name_length)
}
fields := make([]Field, nfields)
for i := 0; i < nfields; i++ {
length := cfields[i].name_length
fname := (*[maxSize]byte)(unsafe.Pointer(cfields[i].name))[:length]
fields[i].Name = string(fname)
fields[i].Type = TypeCode(cfields[i]._type)
}
return fields
}
func fetchNext(c C.MY_RES_META, crow C.MY_ROW, isStmt bool) (row []Value, err error) {
rowPtr := (*[maxSize]*[maxSize]byte)(unsafe.Pointer(crow.mysql_row))
if rowPtr == nil {
return nil, nil
}
cfields := (*[maxSize]C.MYSQL_FIELD)(unsafe.Pointer(c.fields))
colCount := int(c.num_fields)
row = make([]Value, colCount)
lengths := (*[maxSize]uint64)(unsafe.Pointer(crow.lengths))
totalLength := uint64(0)
for i := 0; i < colCount; i++ {
totalLength += lengths[i]
}
arena := make([]byte, 0, int(totalLength))
for i := 0; i < colCount; i++ {
colLength := lengths[i]
colPtr := rowPtr[i]
if colPtr == nil {
continue
}
start := len(arena)
arena = append(arena, colPtr[:colLength]...)
row[i] = Value{isStmt, TypeCode(cfields[i]._type), arena[start : start+int(colLength)]}
}
return row, nil
}
func (res *connQueryResult) fillFields() {
res.fields = fetchFields(res.c.meta)
}
func (res *connQueryResult) fetchNext() (row []Value, err error) {
crow := C.my_fetch_next(res.m, &res.c)
if crow.has_error != 0 {
return nil, res.conn.lastError("")
}
return fetchNext(res.c.meta, crow, false)
}
func (res *connQueryResult) Fields() []Field {
return res.fields
}
func (res *connQueryResult) IndexOf(name string) int {
for i, field := range res.fields {
if field.Name == name {
return i
}
}
return -1
}
type connDataTable struct {
connQueryResult
rows [][]Value
}
func (res *connDataTable) fillRows(conn *Connection) (err error) {
rowCount := int(res.c.affected_rows)
if rowCount == 0 {
return nil
}
if rowCount < 0 {
return conn.lastError("")
}
rows := make([][]Value, rowCount)
for i := 0; i < rowCount; i++ {
rows[i], err = res.fetchNext()
if err != nil {
return err
}
}
res.rows = rows
return nil
}
func (res *connDataTable) Rows() [][]Value {
return res.rows
}
type connDataReader struct {
connQueryResult
}
func (res *connDataReader) FetchNext() ([]Value, error) {
return res.fetchNext()
}
func (res *connDataReader) Close() {
res.close()
}