forked from weigj/go-odbc
/
odbc.go
721 lines (660 loc) · 19.9 KB
/
odbc.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
// Copyright (c) 2011, Wei guangjing <vcc.163@gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package odbc
/*
#include <stdio.h>
#include <stdlib.h>
#ifdef __MINGW32__
#include <windef.h>
#else
typedef void* HANDLE;
#endif
#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>
*/
import "C"
import (
"unsafe"
"fmt"
"reflect"
)
const (
BUFFER_SIZE = 10 * 1024
INFO_BUFFER_LEN = 256
)
var (
Genv C.SQLHANDLE
SQL_NULL_HANDLE C.SQLHANDLE
Debug bool = false
)
type Connection struct {
Dbc C.SQLHANDLE
connected bool
}
type Statement struct {
executed bool
prepared bool
scrollable bool
handle C.SQLHANDLE
}
type ODBCError struct {
SQLState string
NativeError int
ErrorMessage string
}
func initEnv() (err *ODBCError) {
ret := C.SQLAllocHandle(C.SQL_HANDLE_ENV, SQL_NULL_HANDLE, &Genv)
if !Success(ret) {
err := FormatError(C.SQL_HANDLE_ENV, Genv, int(ret))
debugPrint("ERROR:", err)
return err
}
ret = C.SQLSetEnvAttr(C.SQLHENV(Genv), C.SQL_ATTR_ODBC_VERSION, C.SQLPOINTER(unsafe.Pointer(uintptr(C.SQL_OV_ODBC3))), C.SQLINTEGER(0))
if !Success(ret) {
err := FormatError(C.SQL_HANDLE_ENV, Genv, int(ret))
debugPrint("ERROR:", err)
return err
}
return nil
}
func Connect(dsn string, params ...interface{}) (conn *Connection, err *ODBCError) {
var h C.SQLHANDLE
ret := C.SQLAllocHandle(C.SQL_HANDLE_DBC, Genv, &h)
if !Success(ret) {
err := FormatError(C.SQL_HANDLE_DBC, h, int(ret))
debugPrint("SQLAllocHandle ERROR=", err)
return nil, err
}
var stringLength2 C.SQLSMALLINT
outBuf := make([]byte, BUFFER_SIZE*2)
outConnectionString := (*C.SQLWCHAR)(unsafe.Pointer(&outBuf[0]))
ret = C.SQLDriverConnectW(C.SQLHDBC(h),
C.SQLHWND(unsafe.Pointer(uintptr(0))),
(*C.SQLWCHAR)(unsafe.Pointer(StringToUTF16Ptr(dsn))),
C.SQL_NTS,
outConnectionString,
BUFFER_SIZE,
&stringLength2,
C.SQL_DRIVER_NOPROMPT)
if !Success(ret) {
err := FormatError(C.SQL_HANDLE_DBC, h, int(ret))
debugPrint("SQLDriverConnect ERROR=", err)
return nil, err
}
return &Connection{Dbc: h, connected: true}, nil
}
func (conn *Connection) ExecDirect(sql string) (stmt *Statement, err *ODBCError) {
if stmt, err = conn.newStmt(); err != nil {
return nil, err
}
wsql := StringToUTF16Ptr(sql)
ret := C.SQLExecDirectW(C.SQLHSTMT(stmt.handle), (*C.SQLWCHAR)(unsafe.Pointer(wsql)), C.SQL_NTS)
if !Success(ret) {
err := FormatError(C.SQL_HANDLE_STMT, stmt.handle, int(ret))
debugPrint("SQLExecDirectW Error", err)
stmt.Close()
return nil, err
}
stmt.executed = true
return stmt, nil
}
func (conn *Connection) newStmt() (*Statement, *ODBCError) {
stmt := &Statement{}
ret := C.SQLAllocHandle(C.SQL_HANDLE_STMT, conn.Dbc, &stmt.handle)
if !Success(ret) {
err := FormatError(C.SQL_HANDLE_DBC, conn.Dbc, int(ret))
debugPrint("Commit Error", err)
return nil, err
}
return stmt, nil
}
func (conn *Connection) Prepare(sql string, params ...interface{}) (*Statement, *ODBCError) {
wsql := StringToUTF16Ptr(sql)
stmt, err := conn.newStmt()
if err != nil {
return nil, err
}
ret := C.SQLPrepareW(C.SQLHSTMT(stmt.handle), (*C.SQLWCHAR)(unsafe.Pointer(wsql)), C.SQLINTEGER(len(sql)))
if !Success(ret) {
err := FormatError(C.SQL_HANDLE_STMT, stmt.handle, int(ret))
debugPrint("Prepare Error", err)
stmt.Close()
return nil, err
}
stmt.prepared = true
return stmt, nil
}
//func (conn *Connection) Query(sql string, params ...interface{}) (stmt *Statement, err *ODBCError) {
// //TODO
//}
func (conn *Connection) Commit() (err *ODBCError) {
ret := C.SQLEndTran(C.SQL_HANDLE_DBC, conn.Dbc, C.SQL_COMMIT)
if !Success(ret) {
err = FormatError(C.SQL_HANDLE_DBC, conn.Dbc, int(ret))
debugPrint("Commit Error", err)
}
return
}
func (conn *Connection) AutoCommit(b bool) (err *ODBCError) {
var n C.int
if b {
n = C.SQL_AUTOCOMMIT_ON
} else {
n = C.SQL_AUTOCOMMIT_OFF
}
ret := C.SQLSetConnectAttr(C.SQLHDBC(conn.Dbc), C.SQL_ATTR_AUTOCOMMIT, C.SQLPOINTER(unsafe.Pointer(uintptr(n))), C.SQL_IS_UINTEGER)
if !Success(ret) {
err = FormatError(C.SQL_HANDLE_DBC, conn.Dbc, int(ret))
debugPrint("Commit Error", err)
}
return
}
func (conn *Connection) BeginTransaction() (err *ODBCError) {
ret := C.SQLSetConnectAttr(C.SQLHDBC(conn.Dbc), C.SQL_ATTR_AUTOCOMMIT, C.SQLPOINTER(unsafe.Pointer(uintptr(C.SQL_AUTOCOMMIT_OFF))), C.SQL_IS_UINTEGER)
if !Success(ret) {
err = FormatError(C.SQL_HANDLE_DBC, conn.Dbc, int(ret))
debugPrint("BeginTransaction Error", err)
}
return
}
func (conn *Connection) Rollback() (err *ODBCError) {
ret := C.SQLEndTran(C.SQL_HANDLE_DBC, conn.Dbc, C.SQL_ROLLBACK)
if !Success(ret) {
err = FormatError(C.SQL_HANDLE_DBC, conn.Dbc, int(ret))
debugPrint("SQLEndTran Error", err)
}
return
}
func (conn *Connection) ServerInfo() (string, string, string, *ODBCError) {
var info_len C.SQLSMALLINT
p := make([]byte, INFO_BUFFER_LEN)
ret := C.SQLGetInfo(C.SQLHDBC(conn.Dbc), C.SQL_DATABASE_NAME, C.SQLPOINTER(unsafe.Pointer(&p[0])), INFO_BUFFER_LEN, &info_len)
if !Success(ret) {
err := FormatError(C.SQL_HANDLE_DBC, conn.Dbc, int(ret))
debugPrint("SQLGetInfo ERROR=", err)
return "", "", "", err
}
db := string(p[0:info_len])
ret = C.SQLGetInfo(C.SQLHDBC(conn.Dbc), C.SQL_DBMS_VER, C.SQLPOINTER(unsafe.Pointer(&p[0])), INFO_BUFFER_LEN, &info_len)
if !Success(ret) {
err := FormatError(C.SQL_HANDLE_DBC, conn.Dbc, int(ret))
debugPrint("SQLGetInfo ERROR=", err)
return db, "", "", err
}
ver := string(p[0:info_len])
ret = C.SQLGetInfo(C.SQLHDBC(conn.Dbc), C.SQL_SERVER_NAME, C.SQLPOINTER(unsafe.Pointer(&p[0])), INFO_BUFFER_LEN, &info_len)
if !Success(ret) {
err := FormatError(C.SQL_HANDLE_DBC, conn.Dbc, int(ret))
debugPrint("SQLGetInfo ERROR=", err)
return db, ver, "", err
}
server := string(p[0:info_len])
return db, ver, server, nil
}
func (conn *Connection) ClientInfo() (string, string, string, *ODBCError) {
var info_len C.SQLSMALLINT
p := make([]byte, INFO_BUFFER_LEN)
ret := C.SQLGetInfo(C.SQLHDBC(conn.Dbc), C.SQL_DRIVER_NAME, C.SQLPOINTER(unsafe.Pointer(&p[0])), INFO_BUFFER_LEN, &info_len)
if !Success(ret) {
err := FormatError(C.SQL_HANDLE_DBC, conn.Dbc, int(ret))
debugPrint("SQLGetInfo ERROR=", err)
return "", "", "", err
}
drv_name := string(p[0:info_len])
ret = C.SQLGetInfo(C.SQLHDBC(conn.Dbc), C.SQL_DRIVER_ODBC_VER, C.SQLPOINTER(unsafe.Pointer(&p[0])), INFO_BUFFER_LEN, &info_len)
if !Success(ret) {
err := FormatError(C.SQL_HANDLE_DBC, conn.Dbc, int(ret))
debugPrint("SQLGetInfo ERROR=", err)
return "", "", "", err
}
drv_odbc_ver := string(p[0:info_len])
ret = C.SQLGetInfo(C.SQLHDBC(conn.Dbc), C.SQL_DRIVER_VER, C.SQLPOINTER(unsafe.Pointer(&p[0])), INFO_BUFFER_LEN, &info_len)
if !Success(ret) {
err := FormatError(C.SQL_HANDLE_DBC, conn.Dbc, int(ret))
debugPrint("SQLGetInfo ERROR=", err)
return "", "", "", err
}
drv_ver := string(p[0:info_len])
return drv_name, drv_odbc_ver, drv_ver, nil
}
func (conn *Connection) Close() *ODBCError {
if conn.connected {
ret := C.SQLDisconnect(C.SQLHDBC(conn.Dbc))
if !Success(ret) {
err := FormatError(C.SQL_HANDLE_DBC, conn.Dbc, int(ret))
debugPrint("SQLDisconnect Error=", err)
return err
}
ret = C.SQLFreeHandle(C.SQL_HANDLE_DBC, conn.Dbc)
if !Success(ret) {
debugPrint("SQLFreeHandle Error=", ret)
}
conn.connected = false
}
return nil
}
func (stmt *Statement) RowsAffected() (int, *ODBCError) {
var nor C.SQLINTEGER
ret := C.SQLRowCount(C.SQLHSTMT(stmt.handle), &nor)
if !Success(ret) {
err := FormatError(C.SQL_HANDLE_STMT, stmt.handle, int(ret))
debugPrint("SQLRowCount Error", err)
return -1, err
}
return int(nor), nil
}
func (stmt *Statement) Cancel() *ODBCError {
ret := C.SQLCancel(C.SQLHSTMT(stmt.handle))
if !Success(ret) {
err := FormatError(C.SQL_HANDLE_STMT, stmt.handle, int(ret))
debugPrint("Cancel Error", err)
return err
}
return nil
}
func (stmt *Statement) Execute(params ...interface{}) *ODBCError {
if params != nil {
var cParams C.SQLSMALLINT
ret := C.SQLNumParams(C.SQLHSTMT(stmt.handle), &cParams)
if !Success(ret) {
err := FormatError(C.SQL_HANDLE_STMT, stmt.handle, int(ret))
debugPrint("SQLNumParams Error", err)
return err
}
for i := 0; i < int(cParams); i++ {
stmt.BindParam(i+1, params[i])
}
}
ret := C.SQLExecute(C.SQLHSTMT(stmt.handle))
if ret == C.SQL_NEED_DATA {
debugPrint("Execute NEED DATA")
// TODO
// send_data(stmt)
} else if ret == C.SQL_NO_DATA {
debugPrint("Execute NO DATA")
} else if !Success(ret) {
err := FormatError(C.SQL_HANDLE_STMT, stmt.handle, int(ret))
debugPrint("Execute Error", ret, err)
return err
}
stmt.executed = true
return nil
}
func (stmt *Statement) Fetch() (bool, *ODBCError) {
ret := C.SQLFetch(C.SQLHSTMT(stmt.handle))
if ret == C.SQL_NO_DATA {
return false, nil
}
if !Success(ret) {
err := FormatError(C.SQL_HANDLE_STMT, stmt.handle, int(ret))
debugPrint("Fetch Error", err)
return false, err
}
return true, nil
}
type Row struct {
Data []interface{}
}
// Get(Columnindex)
// TODO Get(ColumnName)
func (r *Row) Get(a interface{}) interface{} {
value := reflect.ValueOf(a)
switch f := value; f.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return r.Data[f.Int()]
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return r.Data[f.Uint()]
// case *reflect.StringValue:
// i := r.Meta[f.Get()]
// return r.Data[i]
}
return nil
}
func (r *Row) GetInt(a interface{}) (ret int64) {
v := r.Get(a)
value := reflect.ValueOf(v)
switch f := value; f.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
ret = int64(f.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
ret = int64(f.Uint())
}
return
}
func (r *Row) GetFloat(a interface{}) (ret float64) {
v := r.Get(a)
value := reflect.ValueOf(v)
switch f := value; f.Kind() {
case reflect.Float32, reflect.Float64:
ret = float64(f.Float())
}
return
}
//func (r *Row) GetComplex(a interface{}) (ret complex) {
// v := r.Get(a)
// value := reflect.NewValue(v)
// switch f := value.(type) {
// case *reflect.ComplexValue:
// ret = complex(f.Get())
// }
// return
//}
func (r *Row) GetString(a interface{}) (ret string) {
v := r.Get(a)
value := reflect.ValueOf(v)
switch f := value; f.Kind() {
case reflect.String:
ret = f.String()
}
return
}
func (r *Row) Length() int {
return len(r.Data)
}
func (stmt *Statement) FetchAll() (rows []*Row, err *ODBCError) {
for {
row, err := stmt.FetchOne()
if err != nil || row == nil {
break
}
rows = append(rows, row)
}
return rows, err
}
func (stmt *Statement) FetchOne() (*Row, *ODBCError) {
ok, err := stmt.Fetch()
if !ok {
return nil, err
}
n, _ := stmt.NumFields()
row := new(Row)
row.Data = make([]interface{}, n)
for i := 0; i < n; i++ {
v, _, _, _ := stmt.GetField(i)
row.Data[i] = v
}
return row, nil
}
func (stmt *Statement) GetField(field_index int) (v interface{}, ftype int, flen int, err *ODBCError) {
var field_type C.SQLINTEGER
var field_len C.SQLINTEGER
var ll C.SQLSMALLINT
ret := C.SQLColAttribute(C.SQLHSTMT(stmt.handle), C.SQLUSMALLINT(field_index+1), C.SQL_DESC_CONCISE_TYPE, C.SQLPOINTER(unsafe.Pointer(uintptr(0))), C.SQLSMALLINT(0), &ll, &field_type)
if !Success(ret) {
debugPrint("GetFiled type Error")
}
ret = C.SQLColAttribute(C.SQLHSTMT(stmt.handle), C.SQLUSMALLINT(field_index+1), C.SQL_DESC_LENGTH, C.SQLPOINTER(unsafe.Pointer(uintptr(0))), C.SQLSMALLINT(0), &ll, &field_len)
if !Success(ret) {
debugPrint("GetFiled len Error")
}
var fl C.SQLLEN = C.SQLLEN(field_len)
switch int(field_type) {
case C.SQL_BIT, C.SQL_INTEGER, C.SQL_SMALLINT, C.SQL_TINYINT:
var value C.HANDLE
ret = C.SQLGetData(C.SQLHSTMT(stmt.handle), C.SQLUSMALLINT(field_index+1), C.SQL_C_LONG, C.SQLPOINTER(unsafe.Pointer(&value)), 0, &fl)
if fl == -1 {
v = nil
} else {
v = value
}
case C.SQL_FLOAT, C.SQL_REAL:
var value C.double
ret = C.SQLGetData(C.SQLHSTMT(stmt.handle), C.SQLUSMALLINT(field_index+1), C.SQL_C_DOUBLE, C.SQLPOINTER(unsafe.Pointer(&value)), 0, &fl)
if fl == -1 {
v = nil
} else {
v = value
}
case C.SQL_WCHAR, C.SQL_WVARCHAR, C.SQL_WLONGVARCHAR:
value := make([]uint16, int(field_len)+8)
ret = C.SQLGetData(C.SQLHSTMT(stmt.handle), C.SQLUSMALLINT(field_index+1), C.SQL_C_WCHAR, C.SQLPOINTER(unsafe.Pointer(&value[0])), C.SQLINTEGER(int(field_len)+4), &fl)
s := UTF16ToString(value)
v = s
default:
value := make([]byte, int(fl)+2)
ret = C.SQLGetData(C.SQLHSTMT(stmt.handle), C.SQLUSMALLINT(field_index+1), C.SQL_C_CHAR, C.SQLPOINTER(unsafe.Pointer(&value[0])), C.SQLINTEGER(int(field_len)+4), &fl)
s := string(value[0:])
v = s
debugPrint("default type", value, fl, s)
}
if !Success(ret) {
err = FormatError(C.SQL_HANDLE_STMT, stmt.handle, int(ret))
debugPrint("GetFiled Data Error", err)
}
return v, int(field_type), int(fl), err
}
func (stmt *Statement) NumFields() (int, *ODBCError) {
var NOC C.SQLSMALLINT
ret := C.SQLNumResultCols(C.SQLHSTMT(stmt.handle), &NOC)
if !Success(ret) {
err := FormatError(C.SQL_HANDLE_STMT, stmt.handle, int(ret))
debugPrint("NumFields Error", err)
return -1, err
}
return int(NOC), nil
}
func (stmt *Statement) GetParamType(index int) (int, int, int, int, *ODBCError) {
var data_type, dec_ptr, null_ptr C.SQLSMALLINT
var size_ptr C.SQLULEN
ret := C.SQLDescribeParam(C.SQLHSTMT(stmt.handle), C.SQLUSMALLINT(index), &data_type, &size_ptr, &dec_ptr, &null_ptr)
if !Success(ret) {
err := FormatError(C.SQL_HANDLE_STMT, stmt.handle, int(ret))
debugPrint("GetParamType Error", err)
return -1, -1, -1, -1, err
}
return int(data_type), int(size_ptr), int(dec_ptr), int(null_ptr), nil
}
func (stmt *Statement) BindParam(index int, param interface{}) *ODBCError {
var ValueType C.SQLSMALLINT
var ParameterType C.SQLSMALLINT
var ColumnSize C.SQLULEN
var DecimalDigits C.SQLSMALLINT
var ParameterValuePtr C.SQLPOINTER
var BufferLength C.SQLLEN
var StrLen_or_IndPt C.SQLLEN
v := reflect.ValueOf(param)
if param == nil {
ft, _, _, _, err := stmt.GetParamType(index)
if err != nil {
return err
}
ParameterType = C.SQLSMALLINT(ft)
if ParameterType == C.SQL_UNKNOWN_TYPE {
ParameterType = C.SQL_VARCHAR
}
ValueType = C.SQL_C_DEFAULT
StrLen_or_IndPt = C.SQL_NULL_DATA
ColumnSize = 1
} else {
switch v.Kind() {
case reflect.Bool:
ParameterType = C.SQL_BIT
ValueType = C.SQL_C_BIT
var b [1]byte
if v.Bool() {
b[0] = 1
} else {
b[0] = 0
}
ParameterValuePtr = C.SQLPOINTER(unsafe.Pointer(&b[0]))
BufferLength = 1
StrLen_or_IndPt = 0
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
switch v.Type().Kind() {
case reflect.Int:
case reflect.Int8, reflect.Int16, reflect.Int32:
ParameterType = C.SQL_INTEGER
ValueType = C.SQL_C_LONG
var l C.long = C.long(v.Int())
ParameterValuePtr = C.SQLPOINTER(unsafe.Pointer(&l))
BufferLength = 4
StrLen_or_IndPt = 0
case reflect.Int64:
ParameterType = C.SQL_BIGINT
ValueType = C.SQL_C_SBIGINT
var ll C.longlong = C.longlong(v.Int())
ParameterValuePtr = C.SQLPOINTER(unsafe.Pointer(&ll))
BufferLength = 8
StrLen_or_IndPt = 0
}
case reflect.Float32, reflect.Float64:
ParameterType = C.SQL_DOUBLE
ValueType = C.SQL_C_DOUBLE
var d C.double = C.double(v.Float())
ParameterValuePtr = C.SQLPOINTER(unsafe.Pointer(&d))
BufferLength = 8
StrLen_or_IndPt = 0
case reflect.Complex64, reflect.Complex128:
case reflect.String:
var slen C.SQLUINTEGER = C.SQLUINTEGER(len(v.String()))
ParameterType = C.SQL_VARCHAR
ValueType = C.SQL_C_CHAR
s := []byte(v.String())
ParameterValuePtr = C.SQLPOINTER(unsafe.Pointer(&s[0]))
ColumnSize = slen
BufferLength = C.SQLINTEGER(slen + 1)
StrLen_or_IndPt = C.SQLINTEGER(slen)
default:
debugPrint("Not support type", v)
}
}
ret := C.SQLBindParameter(C.SQLHSTMT(stmt.handle), C.SQLUSMALLINT(index), C.SQL_PARAM_INPUT, ValueType, ParameterType, ColumnSize, DecimalDigits, ParameterValuePtr, BufferLength, &StrLen_or_IndPt)
if !Success(ret) {
err := FormatError(C.SQL_HANDLE_STMT, stmt.handle, int(ret))
debugPrint("SQLBindParameter Error", err)
return err
}
return nil
}
//func (stmt *Statement) BindCol(col int, data interface{}, buff_indicator int) *ODBCError {
// // TODO
// switch f := data.(type) {
// case bool: //1 SQL_BIT
// case int: //4
// case string:
// case float: //4
// case float64: //8
// case complex: //8
// default:
// }
// //ret := C.SQLBindCol(C.SQLHSTMT(stmt.handle), col, C.SQL_C_CHAR, &data, len(data), &buff_indicator)
// return nil
//}
func (stmt *Statement) NextResult() bool {
ret := C.SQLMoreResults(C.SQLHSTMT(stmt.handle))
if ret == C.SQL_NO_DATA {
return false
}
return true
}
func (stmt *Statement) NumRows() (int, *ODBCError) {
var NOR C.SQLLEN
ret := C.SQLRowCount(C.SQLHSTMT(stmt.handle), &NOR)
if !Success(ret) {
err := FormatError(C.SQL_HANDLE_STMT, stmt.handle, int(ret))
debugPrint("SQLRowCount Error", err)
return -1, err
}
return int(NOR), nil
}
func (stmt *Statement) HasRows() bool {
n, _ := stmt.NumRows()
return n > 0
}
type Field struct {
Name string
Type int
Size int
DecimalDigits int
Nullable int
}
func (stmt *Statement) FieldMetadata(col int) (*Field, *ODBCError) {
var BufferLength C.SQLSMALLINT = INFO_BUFFER_LEN
var NameLength C.SQLSMALLINT
var DataType C.SQLSMALLINT
var ColumnSize C.SQLULEN
var DecimalDigits C.SQLSMALLINT
var Nullable C.SQLSMALLINT
ColumnName := make([]byte, INFO_BUFFER_LEN)
ret := C.SQLDescribeCol(C.SQLHSTMT(stmt.handle),
C.SQLUSMALLINT(col),
(*C.SQLCHAR)(unsafe.Pointer(&ColumnName[0])),
BufferLength,
&NameLength,
&DataType,
&ColumnSize,
&DecimalDigits,
&Nullable)
if !Success(ret) {
err := FormatError(C.SQL_HANDLE_STMT, stmt.handle, int(ret))
debugPrint("SQLDescribeCol Error", err)
return nil, err
}
field := &Field{string(ColumnName[0:NameLength]), int(DataType), int(ColumnSize), int(DecimalDigits), int(Nullable)}
return field, nil
}
func (stmt *Statement) free() {
C.SQLFreeHandle(C.SQL_HANDLE_STMT, stmt.handle)
}
func (stmt *Statement) Close() {
stmt.free()
}
func Success(ret C.SQLRETURN) bool {
return int(ret) == C.SQL_SUCCESS || int(ret) == C.SQL_SUCCESS_WITH_INFO
}
func (err *ODBCError) String() string {
if err != nil {
return err.SQLState + " " + string(err.NativeError) + " " + err.ErrorMessage
}
return ""
}
func FormatError(ht C.SQLSMALLINT, h C.SQLHANDLE, val_ret int) (err *ODBCError) {
sqlState := make([]byte, 6)
var nativeError C.SQLINTEGER
messageText := make([]byte, BUFFER_SIZE*10)
var textLength C.SQLSMALLINT
debugPrint("FormatError", val_ret)
for {
ret := C.SQLGetDiagRec(C.SQLSMALLINT(ht),
h,
C.SQLSMALLINT(val_ret),
(*C.SQLCHAR)(unsafe.Pointer(&sqlState[0])),
&nativeError,
(*C.SQLCHAR)(unsafe.Pointer(&messageText[0])),
BUFFER_SIZE*2,
&textLength)
switch ret {
case C.SQL_INVALID_HANDLE:
debugPrint("SQL_INVALID_HANDLE")
err = &ODBCError{SQLState: string(sqlState), NativeError: int(nativeError), ErrorMessage: "SQL_INVALID_HANDLE"}
case C.SQL_NO_DATA:
case C.SQL_SUCCESS:
debugPrint("SQL_SUCCESS")
err = &ODBCError{SQLState: string(sqlState), NativeError: int(nativeError), ErrorMessage: string(messageText)}
continue
case C.SQL_SUCCESS_WITH_INFO:
debugPrint("SQLGetDiagRec => SQL_SUCCESS_WITH_INFO")
err = &ODBCError{SQLState: string(sqlState), NativeError: int(nativeError), ErrorMessage: "SQL_SUCCESS_WITH_INFO"}
case C.SQL_ERROR:
debugPrint("SQLGetDiagRec => SQL_ERROR", sqlState, nativeError, messageText)
err = &ODBCError{SQLState: string(sqlState), NativeError: int(nativeError), ErrorMessage: "SQL_ERROR"}
default:
debugPrint("SQLGetDiagRec => OTHER_ERROR")
err = &ODBCError{SQLState: string(sqlState), NativeError: int(nativeError), ErrorMessage: "OTHER_SQL_ERROR"}
}
break
}
return err
}
func debugPrint(a ...interface{}) {
if Debug {
fmt.Println(a)
}
}
func init() {
if err := initEnv(); err != nil {
panic("odbc init env error!" + err.String())
}
}