-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathstmt.go
264 lines (231 loc) · 5.61 KB
/
stmt.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
package mysql
/*
#include "cgo.h"
*/
import "C"
import (
"reflect"
"unsafe"
)
var (
c_TRUE = C.my_bool(1)
c_FALSE = C.my_bool(0)
)
// Prepared statement.
type Stmt struct {
conn *Connection
s *C.MY_STMT
sql string
bindPtr *C.MYSQL_BIND
binds []C.MYSQL_BIND
bind_pos int
}
// Prepare a statement.
func (conn *Connection) Prepare(sql string) (*Stmt, error) {
stmt := &Stmt{}
stmt.conn = conn
stmt.sql = sql
if C.my_prepare(&stmt.s, &stmt.bindPtr, &conn.c, (*C.char)(stringPointer(sql)), C.ulong(len(sql))) != 0 {
return nil, conn.lastError(sql)
}
if stmt.bindPtr != nil {
bindSlice := (*reflect.SliceHeader)(unsafe.Pointer(&stmt.binds))
bindSlice.Data = uintptr(unsafe.Pointer(stmt.bindPtr))
bindSlice.Len = int(stmt.s.param_count)
bindSlice.Cap = bindSlice.Len
}
return stmt, nil
}
// Clean bind parameters.
func (stmt *Stmt) CleanBind() {
stmt.bind_pos = 0
}
// Number of input arguments.
func (stmt *Stmt) NumInput() int {
return len(stmt.binds)
}
// Bind a int parameter.
func (stmt *Stmt) BindInt(value int32) {
stmt.binds[stmt.bind_pos] = C.MYSQL_BIND{
buffer_type: C.MYSQL_TYPE_LONG,
buffer: unsafe.Pointer(&value),
is_null: &c_FALSE,
}
stmt.bind_pos++
}
// Bind a tinyint parameter.
func (stmt *Stmt) BindTinyInt(value int8) {
stmt.binds[stmt.bind_pos] = C.MYSQL_BIND{
buffer_type: C.MYSQL_TYPE_TINY,
buffer: unsafe.Pointer(&value),
is_null: &c_FALSE,
}
stmt.bind_pos++
}
// Bind a smallint parameter.
func (stmt *Stmt) BindSmallInt(value int16) {
stmt.binds[stmt.bind_pos] = C.MYSQL_BIND{
buffer_type: C.MYSQL_TYPE_SHORT,
buffer: unsafe.Pointer(&value),
is_null: &c_FALSE,
}
stmt.bind_pos++
}
// Bind a bigint parameter.
func (stmt *Stmt) BindBigInt(value int64) {
stmt.binds[stmt.bind_pos] = C.MYSQL_BIND{
buffer_type: C.MYSQL_TYPE_LONGLONG,
buffer: unsafe.Pointer(&value),
is_null: &c_FALSE,
}
stmt.bind_pos++
}
// Bind a float parameter.
func (stmt *Stmt) BindFloat(value float32) {
stmt.binds[stmt.bind_pos] = C.MYSQL_BIND{
buffer_type: C.MYSQL_TYPE_FLOAT,
buffer: unsafe.Pointer(&value),
is_null: &c_FALSE,
}
stmt.bind_pos++
}
// Bind a double parameter.
func (stmt *Stmt) BindDouble(value float64) {
stmt.binds[stmt.bind_pos] = C.MYSQL_BIND{
buffer_type: C.MYSQL_TYPE_DOUBLE,
buffer: unsafe.Pointer(&value),
is_null: &c_FALSE,
}
stmt.bind_pos++
}
// Bind a text parameter.
func (stmt *Stmt) BindText(value string) {
stmt.binds[stmt.bind_pos] = C.MYSQL_BIND{
buffer_type: C.MYSQL_TYPE_VAR_STRING,
buffer: stringPointer(value),
buffer_length: (C.ulong)(len(value)),
is_null: &c_FALSE,
}
stmt.bind_pos++
}
// Bind a blob parameter.
func (stmt *Stmt) BindBlob(value []byte) {
stmt.binds[stmt.bind_pos] = C.MYSQL_BIND{
buffer_type: C.MYSQL_TYPE_BLOB,
buffer: bytePointer(value),
buffer_length: (C.ulong)(len(value)),
is_null: cbool(value == nil),
}
stmt.bind_pos++
}
func (stmt *Stmt) bind(paramType TypeCode, valuePtr unsafe.Pointer) {
stmt.binds[stmt.bind_pos] = C.MYSQL_BIND{
buffer_type: uint32(paramType),
buffer: valuePtr,
is_null: cbool(valuePtr == nil),
}
stmt.bind_pos++
}
// Bind parameter.
func (stmt *Stmt) Bind(value interface{}) {
switch v := value.(type) {
case int:
stmt.BindBigInt(int64(v))
case int8:
stmt.BindTinyInt(v)
case int16:
stmt.BindSmallInt(v)
case int32:
stmt.BindInt(v)
case int64:
stmt.BindBigInt(v)
case float32:
stmt.BindFloat(v)
case float64:
stmt.BindDouble(v)
case string:
stmt.BindText(v)
case []byte:
stmt.BindBlob(v)
case *int8:
stmt.bind(C.MYSQL_TYPE_TINY, unsafe.Pointer(v))
case *int16:
stmt.bind(C.MYSQL_TYPE_SHORT, unsafe.Pointer(v))
case *int32:
stmt.bind(C.MYSQL_TYPE_LONG, unsafe.Pointer(v))
case *int64:
stmt.bind(C.MYSQL_TYPE_LONGLONG, unsafe.Pointer(v))
case *float32:
stmt.bind(C.MYSQL_TYPE_FLOAT, unsafe.Pointer(v))
case *float64:
stmt.bind(C.MYSQL_TYPE_DOUBLE, unsafe.Pointer(v))
default:
panic("unknow parameter type")
}
}
func (stmt *Stmt) execute(res *stmtResult, mode C.MY_MODE) error {
if stmt.conn.IsClosed() {
return &SqlError{Num: 2006, Message: "Connection is closed"}
}
if C.my_stmt_execute(stmt.s, stmt.bindPtr, &res.c, mode) != 0 {
return stmt.lastError()
}
return nil
}
func (stmt *Stmt) query(res *stmtQueryResult, mode C.MY_MODE) error {
if err := stmt.execute(&res.stmtResult, mode); err != nil {
return err
}
res.stmt = stmt
res.fillFields()
return nil
}
// Execute statement as none-query.
func (stmt *Stmt) Execute() (Result, error) {
res := &stmtResult{}
res.s = stmt.s
if err := stmt.execute(res, C.MY_MODE_NONE); err != nil {
return nil, err
}
return res, nil
}
// Execute statement and fill result into a DataTable.
func (stmt *Stmt) QueryTable() (DataTable, error) {
res := &stmtDataTable{}
res.s = stmt.s
if err := stmt.query(&res.stmtQueryResult, C.MY_MODE_TABLE); err != nil {
return nil, err
}
defer res.close()
if err := res.fillRows(stmt); err != nil {
return nil, err
}
return res, nil
}
// Execute statement and return a result reader. NOTE: Please remember close the reader.
func (stmt *Stmt) QueryReader() (DataReader, error) {
res := &stmtDataReader{}
res.s = stmt.s
if err := stmt.query(&res.stmtQueryResult, C.MY_MODE_READER); err != nil {
return nil, err
}
return res, nil
}
// Close and dispose the statement.
func (stmt *Stmt) Close() error {
if stmt.s == nil {
return nil
}
if C.my_stmt_close(stmt.s, stmt.bindPtr) != 0 {
return stmt.lastError()
}
stmt.s = nil
stmt.binds = nil
return nil
}
func cbool(gobool bool) *C.my_bool {
if gobool {
return &c_TRUE
}
return &c_FALSE
}