-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
adodb.go
911 lines (853 loc) · 19.4 KB
/
adodb.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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
package adodb
import (
"database/sql"
"database/sql/driver"
"errors"
"io"
"math"
//"math/big"
"reflect"
"time"
"unsafe"
"github.com/go-ole/go-ole"
"github.com/go-ole/go-ole/oleutil"
"golang.org/x/net/context"
)
type releaser interface {
Release() int32
}
func fullRelease(obj releaser) {
last := int32(-1)
for {
ref := obj.Release()
if ref == 0 || ref == last {
break
}
last = ref
}
}
func init() {
sql.Register("adodb", &AdodbDriver{})
}
type AdodbDriver struct {
CursorLocation int
ConnectTimeout int64
}
type AdodbConn struct {
db *ole.IDispatch
}
type AdodbTx struct {
c *AdodbConn
}
func (tx *AdodbTx) Commit() error {
rv, err := oleutil.CallMethod(tx.c.db, "CommitTrans")
if err != nil {
return err
}
rv.Clear()
return nil
}
func (tx *AdodbTx) Rollback() error {
rv, err := oleutil.CallMethod(tx.c.db, "RollbackTrans")
if err != nil {
return err
}
rv.Clear()
return nil
}
func (c *AdodbConn) exec(ctx context.Context, query string, args []namedValue) (driver.Result, error) {
s, err := c.Prepare(query)
if err != nil {
return nil, err
}
result, err := s.(*AdodbStmt).exec(ctx, args)
s.Close()
if err != nil && err != driver.ErrSkip {
return nil, err
}
return result, nil
}
/*
func (c *AdodbConn) Query(query string, args []driver.Value) (driver.Rows, error) {
list := make([]namedValue, len(args))
for i, v := range args {
list[i] = namedValue{
Ordinal: i + 1,
Value: v,
}
}
return c.query(context.Background(), query, list)
}
func (c *AdodbConn) query(ctx context.Context, query string, args []namedValue) (driver.Rows, error) {
s, err := c.Prepare(query)
if err != nil {
return nil, err
}
rows, err := s.(*AdodbStmt).query(ctx, args)
if err != nil && err != driver.ErrSkip {
s.Close()
return nil, err
}
return rows, nil
}
*/
func (c *AdodbConn) Begin() (driver.Tx, error) {
return c.begin(context.Background())
}
func (c *AdodbConn) begin(ctx context.Context) (driver.Tx, error) {
rv, err := oleutil.CallMethod(c.db, "BeginTrans")
if err != nil {
return nil, err
}
rv.Clear()
return &AdodbTx{c: c}, nil
}
func (d *AdodbDriver) Open(dsn string) (driver.Conn, error) {
ole.CoInitialize(0)
unknown, err := oleutil.CreateObject("ADODB.Connection")
if err != nil {
return nil, err
}
db, err := unknown.QueryInterface(ole.IID_IDispatch)
unknown.Release()
if err != nil {
return nil, err
}
if d.CursorLocation != 0 {
_, err = oleutil.PutProperty(db, "CursorLocation", d.CursorLocation)
if err != nil {
return nil, err
}
}
if d.ConnectTimeout != 0 {
_, err = oleutil.PutProperty(db, "ConnectTimeout", d.ConnectTimeout)
if err != nil {
return nil, err
}
}
rc, err := oleutil.CallMethod(db, "Open", dsn)
if err != nil {
return nil, err
}
rc.Clear()
return &AdodbConn{db: db}, nil
}
func (c *AdodbConn) Close() error {
rv, err := oleutil.CallMethod(c.db, "Close")
if err != nil {
return err
}
rv.Clear()
fullRelease(c.db)
c.db = nil
ole.CoUninitialize()
return nil
}
type AdodbStmt struct {
c *AdodbConn
s *ole.IDispatch
ps *ole.IDispatch
pc int
}
func (c *AdodbConn) Prepare(query string) (driver.Stmt, error) {
return c.prepare(context.Background(), query)
}
func (c *AdodbConn) prepare(ctx context.Context, query string) (driver.Stmt, error) {
unknown, err := oleutil.CreateObject("ADODB.Command")
if err != nil {
return nil, err
}
s, err := unknown.QueryInterface(ole.IID_IDispatch)
unknown.Release()
if err != nil {
return nil, err
}
rv, err := oleutil.PutProperty(s, "ActiveConnection", c.db)
if err != nil {
return nil, err
}
rv.Clear()
rv, err = oleutil.PutProperty(s, "CommandText", query)
if err != nil {
return nil, err
}
rv.Clear()
rv, err = oleutil.PutProperty(s, "CommandType", 1)
if err != nil {
return nil, err
}
rv.Clear()
rv, err = oleutil.PutProperty(s, "Prepared", true)
if err != nil {
return nil, err
}
rv.Clear()
val, err := oleutil.GetProperty(s, "Parameters")
if err != nil {
return nil, err
}
ps := val.ToIDispatch()
val.Clear()
pc := -1
rv, err = oleutil.GetProperty(ps, "Count")
if err != nil {
rv, err = oleutil.CallMethod(ps, "Refresh")
if err == nil {
rv.Clear()
}
} else {
pc = int(rv.Val)
rv.Clear()
}
return &AdodbStmt{c: c, s: s, ps: ps, pc: pc}, nil
}
func (s *AdodbStmt) Close() error {
fullRelease(s.ps)
s.ps = nil
fullRelease(s.s)
s.s = nil
s.c = nil
return nil
}
func (s *AdodbStmt) NumInput() int {
if s.pc != -1 {
return s.pc
}
rv, err := oleutil.GetProperty(s.ps, "Count")
if err != nil {
return -1
}
s.pc = int(rv.Val)
rv.Clear()
return s.pc
}
func (s *AdodbStmt) bind(args []namedValue) error {
for i, v := range args {
var err error
var val *ole.VARIANT
if v.Name != "" {
val, err = oleutil.CallMethod(s.ps, "Item", v.Name)
} else {
val, err = oleutil.CallMethod(s.ps, "Item", int32(i))
}
if err != nil {
return err
}
item := val.ToIDispatch()
val.Clear()
t, err := oleutil.GetProperty(item, "Type")
if err != nil {
item.Release()
return err
}
rv, err := oleutil.PutProperty(item, "Value", v.Value)
if err != nil {
t.Clear()
item.Release()
return err
}
rv.Clear()
t.Clear()
item.Release()
}
return nil
}
type namedValue struct {
Name string
Ordinal int
Value driver.Value
}
func (s *AdodbStmt) Query(args []driver.Value) (driver.Rows, error) {
list := make([]namedValue, len(args))
for i, v := range args {
list[i] = namedValue{
Ordinal: i + 1,
Value: v,
}
}
return s.query(context.Background(), list)
}
func (s *AdodbStmt) query(ctx context.Context, args []namedValue) (driver.Rows, error) {
if err := s.bind(args); err != nil {
return nil, err
}
res, err := oleutil.CallMethod(s.s, "Execute")
if err != nil {
return nil, err
}
rc := res.ToIDispatch()
rc.AddRef()
res.Clear()
return &AdodbRows{s: s, rc: rc, nc: -1, cols: nil}, nil
}
func (s *AdodbStmt) Exec(args []driver.Value) (driver.Result, error) {
list := make([]namedValue, len(args))
for i, v := range args {
list[i] = namedValue{
Ordinal: i + 1,
Value: v,
}
}
return s.exec(context.Background(), list)
}
type AdodbResult struct {
n int64
}
func (r *AdodbResult) LastInsertId() (int64, error) {
return 0, errors.New("LastInsertId not supported")
}
func (r *AdodbResult) RowsAffected() (int64, error) {
return r.n, nil
}
func (s *AdodbStmt) exec(ctx context.Context, args []namedValue) (driver.Result, error) {
if err := s.bind(args); err != nil {
return nil, err
}
var rowsAffected int64
rc, err := oleutil.CallMethod(s.s, "Execute", &rowsAffected)
if err != nil {
return nil, err
}
rc.Clear()
return &AdodbResult{n: rowsAffected}, nil
}
type AdodbRows struct {
s *AdodbStmt
rc *ole.IDispatch
nc int
cols []string
}
func (rc *AdodbRows) Close() error {
rv, err := oleutil.CallMethod(rc.rc, "Close")
if err != nil {
return err
}
rv.Clear()
fullRelease(rc.rc)
rc.rc = nil
rc.s = nil
return nil
}
func (rc *AdodbRows) Columns() []string {
if rc.nc != len(rc.cols) {
unknown, err := oleutil.GetProperty(rc.rc, "Fields")
if err != nil {
return []string{}
}
fields := unknown.ToIDispatch()
unknown.Clear()
defer fields.Release()
rv, err := oleutil.GetProperty(fields, "Count")
if err != nil {
return []string{}
}
rc.nc = int(rv.Val)
rv.Clear()
rc.cols = make([]string, rc.nc)
for i := 0; i < rc.nc; i++ {
var varval ole.VARIANT
varval.VT = ole.VT_I4
varval.Val = int64(i)
val, err := oleutil.CallMethod(fields, "Item", &varval)
if err != nil {
return []string{}
}
item := val.ToIDispatch()
val.Clear()
name, err := oleutil.GetProperty(item, "Name")
if err != nil {
item.Release()
return []string{}
}
rc.cols[i] = name.ToString()
name.Clear()
item.Release()
}
}
return rc.cols
}
func (rc *AdodbRows) Next(dest []driver.Value) error {
eof, err := oleutil.GetProperty(rc.rc, "EOF")
if err != nil {
return io.EOF
}
if eof.Val != 0 {
eof.Clear()
return io.EOF
}
eof.Clear()
unknown, err := oleutil.GetProperty(rc.rc, "Fields")
if err != nil {
return err
}
fields := unknown.ToIDispatch()
unknown.Clear()
defer fields.Release()
for i := range dest {
var varval ole.VARIANT
varval.VT = ole.VT_I4
varval.Val = int64(i)
rv, err := oleutil.CallMethod(fields, "Item", &varval)
if err != nil {
return err
}
field := rv.ToIDispatch()
rv.Clear()
val, err := oleutil.GetProperty(field, "Value")
if err != nil {
field.Release()
return err
}
if val.VT == 1 { // VT_NULL
dest[i] = nil
val.Clear()
field.Release()
continue
}
typ, err := oleutil.GetProperty(field, "Type")
if err != nil {
val.Clear()
field.Release()
return err
}
sc, err := oleutil.GetProperty(field, "NumericScale")
if err != nil {
typ.Clear()
val.Clear()
field.Release()
return err
}
switch typ.Val {
case 0: // ADEMPTY
dest[i] = nil
case 2: // ADSMALLINT
dest[i] = int64(int16(val.Val))
case 3: // ADINTEGER
dest[i] = int64(int32(val.Val))
case 4: // ADSINGLE
dest[i] = float64(math.Float32frombits(uint32(val.Val)))
case 5: // ADDOUBLE
dest[i] = math.Float64frombits(uint64(val.Val))
case 6: // ADCURRENCY
dest[i] = float64(val.Val) / 10000
case 7: // ADDATE
// see http://blogs.msdn.com/b/ericlippert/archive/2003/09/16/eric-s-complete-guide-to-vt-date.aspx
d, t := math.Modf(math.Float64frombits(uint64(val.Val)))
t = math.Abs(t)
dest[i] = time.Date(1899, 12, 30+int(d), 0, 0, int(t*86400+0.5), 0, time.Local)
case 8: // ADBSTR
dest[i] = val.ToString()
case 9: // ADIDISPATCH
dest[i] = val.ToIDispatch()
case 10: // ADERROR
// TODO
case 11: // ADBOOLEAN
dest[i] = val.Val != 0
case 12: // ADVARIANT
dest[i] = val
case 13: // ADIUNKNOWN
dest[i] = val.ToIUnknown()
case 14: // ADDECIMAL
sub := math.Pow(10, float64(sc.Val))
dest[i] = float64(float64(val.Val) / sub)
case 16: // ADTINYINT
dest[i] = int8(val.Val)
case 17: // ADUNSIGNEDTINYINT
dest[i] = uint8(val.Val)
case 18: // ADUNSIGNEDSMALLINT
dest[i] = uint16(val.Val)
case 19: // ADUNSIGNEDINT
dest[i] = uint32(val.Val)
case 20: // ADBIGINT
//dest[i] = big.NewInt(val.Val)
dest[i] = int64(val.Val)
case 21: // ADUNSIGNEDBIGINT
dest[i] = uint64(val.Val)
case 72: // ADGUID
dest[i] = val.ToString()
case 128: // ADBINARY
sa := (*ole.SafeArray)(unsafe.Pointer(uintptr(val.Val)))
conv := &ole.SafeArrayConversion{sa}
elems, err := conv.TotalElements(0)
if err != nil {
return err
}
dest[i] = (*[1 << 30]byte)(unsafe.Pointer(uintptr(sa.Data)))[0:elems]
case 129: // ADCHAR
dest[i] = val.ToString() //uint8(val.Val)
case 130: // ADWCHAR
dest[i] = val.ToString() //uint16(val.Val)
case 131: // ADNUMERIC
sub := math.Pow(10, float64(sc.Val))
dest[i] = float64(float64(val.Val) / sub)
case 132: // ADUSERDEFINED
dest[i] = uintptr(val.Val)
case 133: // ADDBDATE
// see http://blogs.msdn.com/b/ericlippert/archive/2003/09/16/eric-s-complete-guide-to-vt-date.aspx
d := math.Float64frombits(uint64(val.Val))
dest[i] = time.Date(1899, 12, 30+int(d), 0, 0, 0, 0, time.Local)
case 134: // ADDBTIME
t := math.Float64frombits(uint64(val.Val))
sec, nsec := math.Modf(t * 86400)
dest[i] = time.Date(0, 1, 1, 0, 0, int(sec), int(nsec*float64(time.Second)), time.Local)
case 135: // ADDBTIMESTAMP
d, t := math.Modf(math.Float64frombits(uint64(val.Val)))
t = math.Abs(t)
sec, nsec := math.Modf(t * 86400)
dest[i] = time.Date(1899, 12, 30+int(d), 0, 0, int(sec), int(nsec*float64(time.Second)), time.Local)
case 136: // ADCHAPTER
dest[i] = val.ToString()
case 200: // ADVARCHAR
dest[i] = val.ToString()
case 201: // ADLONGVARCHAR
dest[i] = val.ToString()
case 202: // ADVARWCHAR
dest[i] = val.ToString()
case 203: // ADLONGVARWCHAR
dest[i] = val.ToString()
case 204: // ADVARBINARY
// TODO
case 205: // ADLONGVARBINARY
sa := (*ole.SafeArray)(unsafe.Pointer(uintptr(val.Val)))
conv := &ole.SafeArrayConversion{sa}
elems, err := conv.TotalElements(0)
if err != nil {
return err
}
dest[i] = (*[1 << 30]byte)(unsafe.Pointer(uintptr(sa.Data)))[0:elems]
}
if typ.Val != 12 {
val.Clear()
}
typ.Clear()
sc.Clear()
field.Release()
}
rv, err := oleutil.CallMethod(rc.rc, "MoveNext")
if err != nil {
return err
}
rv.Clear()
return nil
}
// ColumnTypeDatabaseTypeName implement RowsColumnTypeDatabaseTypeName.
func (rc *AdodbRows) ColumnTypeDatabaseTypeName(i int) string {
if i >= rc.nc {
return ""
}
unknown, err := oleutil.GetProperty(rc.rc, "Fields")
if err != nil {
return ""
}
fields := unknown.ToIDispatch()
unknown.Clear()
defer fields.Release()
var varval ole.VARIANT
varval.VT = ole.VT_I4
varval.Val = int64(i)
val, err := oleutil.CallMethod(fields, "Item", &varval)
if err != nil {
return ""
}
item := val.ToIDispatch()
val.Clear()
typ, err := oleutil.GetProperty(item, "Type")
if err != nil {
item.Release()
return ""
}
typname := ""
switch typ.Val {
case 0:
typname = "ADEMPTY"
case 2:
typname = "ADSMALLINT"
case 3:
typname = "ADINTEGER"
case 4:
typname = "ADSINGLE"
case 5:
typname = "ADDOUBLE"
case 6:
typname = "ADCURRENCY"
case 7:
typname = "ADDATE"
case 8:
typname = "ADBSTR"
case 9:
typname = "ADIDISPATCH"
case 10:
typname = "ADERROR"
case 11:
typname = "ADBOOLEAN"
case 12:
typname = "ADVARIANT"
case 13:
typname = "ADIUNKNOWN"
case 14:
typname = "ADDECIMAL"
case 16:
typname = "ADTINYINT"
case 17:
typname = "ADUNSIGNEDTINYINT"
case 18:
typname = "ADUNSIGNEDSMALLINT"
case 19:
typname = "ADUNSIGNEDINT"
case 20:
typname = "ADBIGINT"
case 21:
typname = "ADUNSIGNEDBIGINT"
case 72:
typname = "ADGUID"
case 128:
typname = "ADBINARY"
case 129:
typname = "ADCHAR"
case 130:
typname = "ADWCHAR"
case 131:
typname = "ADNUMERIC"
case 132:
typname = "ADUSERDEFINED"
case 133:
typname = "ADDBDATE"
case 134:
typname = "ADDBTIME"
case 135:
typname = "ADDBTIMESTAMP"
case 136:
typname = "ADCHAPTER"
case 200:
typname = "ADVARCHAR"
case 201:
typname = "ADLONGVARCHAR"
case 202:
typname = "ADVARWCHAR"
case 203:
typname = "ADLONGVARWCHAR"
case 204:
typname = "ADVARBINARY"
case 205:
typname = "ADLONGVARBINARY"
}
typ.Clear()
item.Release()
return typname
}
func (rc *AdodbRows) ColumnTypeLength(i int) (length int64, ok bool) {
if i >= rc.nc {
return 0, false
}
unknown, err := oleutil.GetProperty(rc.rc, "Fields")
if err != nil {
return 0, false
}
fields := unknown.ToIDispatch()
unknown.Clear()
defer fields.Release()
var varval ole.VARIANT
varval.VT = ole.VT_I4
varval.Val = int64(i)
val, err := oleutil.CallMethod(fields, "Item", &varval)
if err != nil {
return 0, false
}
item := val.ToIDispatch()
val.Clear()
siz, err := oleutil.GetProperty(item, "DefinedSize")
if err != nil {
item.Release()
return 0, false
}
sizval := siz.Val
siz.Clear()
item.Release()
return int64(sizval), true
}
// ColumnTypeNullable implement RowsColumnTypeNullable.
func (rc *AdodbRows) ColumnTypeNullable(i int) (nullable, ok bool) {
if i >= rc.nc {
return false, false
}
unknown, err := oleutil.GetProperty(rc.rc, "Fields")
if err != nil {
return false, false
}
fields := unknown.ToIDispatch()
unknown.Clear()
defer fields.Release()
var varval ole.VARIANT
varval.VT = ole.VT_I4
varval.Val = int64(i)
val, err := oleutil.CallMethod(fields, "Item", &varval)
if err != nil {
return false, false
}
item := val.ToIDispatch()
val.Clear()
att, err := oleutil.GetProperty(item, "Attributes")
if err != nil {
item.Release()
return false, false
}
attributes := att.Val
att.Clear()
item.Release()
return attributes&0x20 != 0, true
}
// ColumnTypeScanType implement RowsColumnTypeScanType.
func (rc *AdodbRows) ColumnTypeScanType(i int) reflect.Type {
if i >= rc.nc {
return reflect.TypeOf(nil)
}
unknown, err := oleutil.GetProperty(rc.rc, "Fields")
if err != nil {
return reflect.TypeOf(nil)
}
fields := unknown.ToIDispatch()
unknown.Clear()
defer fields.Release()
var varval ole.VARIANT
varval.VT = ole.VT_I4
varval.Val = int64(i)
val, err := oleutil.CallMethod(fields, "Item", &varval)
if err != nil {
return reflect.TypeOf(nil)
}
item := val.ToIDispatch()
val.Clear()
typ, err := oleutil.GetProperty(item, "Type")
if err != nil {
item.Release()
return reflect.TypeOf(nil)
}
var rt reflect.Type
switch typ.Val {
case 0: // ADEMPTY
rt = reflect.TypeOf(nil)
case 2: // ADSMALLINT
rt = reflect.TypeOf(int16(0))
case 3: // ADINTEGER
rt = reflect.TypeOf(int32(0))
case 4: // ADSINGLE
rt = reflect.TypeOf(float32(0))
case 5: // ADDOUBLE
rt = reflect.TypeOf(float64(0))
case 6: // ADCURRENCY
rt = reflect.TypeOf(float64(0))
case 7: // ADDATE
rt = reflect.TypeOf(time.Time{})
case 8: // ADBSTR
rt = reflect.TypeOf("")
case 9: // ADIDISPATCH
rt = reflect.TypeOf((*ole.IDispatch)(nil))
case 10: // ADERROR
rt = reflect.TypeOf((error)(nil))
case 11: // ADBOOLEAN
rt = reflect.TypeOf(true)
case 12: // ADVARIANT
var va ole.VARIANT
rt = reflect.TypeOf(va)
case 13: // ADIUNKNOWN
rt = reflect.TypeOf((*ole.IUnknown)(nil))
case 14: // ADDECIMAL
rt = reflect.TypeOf(float64(0))
case 16: // ADTINYINT
rt = reflect.TypeOf(int8(0))
case 17: // ADUNSIGNEDTINYINT
rt = reflect.TypeOf(uint8(0))
case 18: // ADUNSIGNEDSMALLINT
rt = reflect.TypeOf(uint16(0))
case 19: // ADUNSIGNEDINT
rt = reflect.TypeOf(uint32(0))
case 20: // ADBIGINT
//rt = reflect.TypeOf((*big.Int)(nil))
rt = reflect.TypeOf((int64)(0))
case 21: // ADUNSIGNEDBIGINT
//rt = reflect.TypeOf(nil)
rt = reflect.TypeOf((uint64)(0))
case 72: // ADGUID
var gi ole.GUID
rt = reflect.TypeOf(gi)
case 128: // ADBINARY
rt = reflect.TypeOf((*ole.SafeArray)(nil))
case 129: // ADCHAR
rt = reflect.TypeOf(byte(0))
case 130: // ADWCHAR
rt = reflect.TypeOf(rune(0))
case 131: // ADNUMERIC
rt = reflect.TypeOf(float64(0))
case 132: // ADUSERDEFINED
rt = reflect.TypeOf(uintptr(0))
case 133: // ADDBDATE
rt = reflect.TypeOf(time.Time{})
case 134: // ADDBTIME
rt = reflect.TypeOf(time.Time{})
case 135: // ADDBTIMESTAMP
rt = reflect.TypeOf(time.Time{})
case 136: // ADCHAPTER
rt = reflect.TypeOf("")
case 200: // ADVARCHAR
rt = reflect.TypeOf("")
case 201: // ADLONGVARCHAR
rt = reflect.TypeOf("")
case 202: // ADVARWCHAR
rt = reflect.TypeOf("")
case 203: // ADLONGVARWCHAR
rt = reflect.TypeOf("")
case 204: // ADVARBINARY
rt = reflect.TypeOf([]byte{})
case 205: // ADLONGVARBINARY
rt = reflect.TypeOf((*ole.SafeArray)(nil))
}
typ.Clear()
item.Release()
return rt
}
func (rc *AdodbRows) ColumnTypePrecisionScale(i int) (precision, scale int64, ok bool) {
if i >= rc.nc {
return 0, 0, false
}
unknown, err := oleutil.GetProperty(rc.rc, "Fields")
if err != nil {
return 0, 0, false
}
fields := unknown.ToIDispatch()
unknown.Clear()
defer fields.Release()
var varval ole.VARIANT
varval.VT = ole.VT_I4
varval.Val = int64(i)
val, err := oleutil.CallMethod(fields, "Item", &varval)
if err != nil {
return 0, 0, false
}
item := val.ToIDispatch()
val.Clear()
typ, err := oleutil.GetProperty(item, "Type")
if err != nil {
item.Release()
return 0, 0, false
}
if typ.Val != 131 {
item.Release()
return 0, 0, true
}
prec, err := oleutil.GetProperty(item, "Precision")
if err != nil {
item.Release()
return 0, 0, false
}
scl, err := oleutil.GetProperty(item, "NumericScale")
if err != nil {
item.Release()
return 0, 0, false
}
precval := prec.Val
sclval := scl.Val
prec.Clear()
scl.Clear()
item.Release()
return int64(precval), int64(sclval), true
}