-
Notifications
You must be signed in to change notification settings - Fork 2
/
jdbc-0.2.0.tm
1159 lines (968 loc) · 39.9 KB
/
jdbc-0.2.0.tm
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
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#------------------------------------------------------------------------------
#
# Tcl DataBase Connectivity JDBC Driver
# Class definitions and Tcl-level methods for the tdbc::jdbc bridge.
#
#------------------------------------------------------------------------------
package require Tcl 8.6-
package require tdbc
# jtclsh will setup TCLLIBPATH
if { [info exists ::env(TCLLIBPATH) ] } {
if {[catch {package require java}]} {
puts "Failed loading package TclBlend..."
exit
}
} else {
# Environment variable does not find TCLLIBPATH -> no TclBlend/jtclsh
# Now try to check TclJBlend
if {[catch {package require JBlend}]} {
puts "Failed loading package TclJBlend, exit."
exit
}
}
package provide tdbc::jdbc 0.2.0
::namespace eval ::tdbc::jdbc {
namespace export connection
}
java::import java.sql.Connection
java::import java.sql.DriverManager
java::import java.sql.ResultSet
java::import java.sql.SQLWarning
java::import java.sql.Statement
java::import java.sql.ResultSetMetaData
java::import java.sql.DatabaseMetaData
java::import java.sql.Date
java::import java.sql.Time
java::import java.sql.Timestamp
java::import java.math.BigDecimal
java::import java.io.ByteArrayInputStream
java::import java.io.Reader
java::import java.io.StringReader
#------------------------------------------------------------------------------
#
# tdbc::jdbc::connection --
#
# Class representing a connection to a jdbc database.
#
#-------------------------------------------------------------------------------
::oo::class create ::tdbc::jdbc::connection {
superclass ::tdbc::connection
variable ConnectionI DatabaseMetaDataI
variable isolation
variable readonly
variable transaction
variable useprepared
constructor {className url username password args} {
next
if {[llength $args] % 2 != 0} {
set cmd [lrange [info level 0] 0 end-[llength $args]]
return -code error \
-errorcode {TDBC GENERAL_ERROR HY000 JDBC WRONGNUMARGS} \
"wrong # args, should be \"$cmd ?-option value?...\""
}
# required magic for AndroWish: needs investigation
catch { java::load $className }
catch {
# Android: use Context.getClassLoader().loadClass()
java::try {
set ctx [java::call tk.tcl.wish.AndroWish getContext]
set ldr [$ctx getClassLoader]
set cls [$ldr loadClass $className]
} catch {Exception ex} {
# nothing
} finally {
unset -nocomplain ldr
unset -nocomplain ctx
}
}
if {![info exists cls]} {
# Normal JVM: use Class.forName()
java::try {
java::call Class forName $className
} catch {Exception ex} {
# nothing
}
}
java::try {
set ConnectionI [java::call DriverManager getConnection $url $username $password]
} catch {TclException ex} {
error "tcl error: [$ex toString]"
} catch {ClassNotFoundException ex} {
error "ClassNotFoundException: [$ex toString]"
} catch {SQLException ex} {
error "SQLException: [$ex toString]"
} catch {Exception ex} {
error "Exception: [$ex toString]"
} finally {
unset -nocomplain cls
}
# for internal use
set transaction [dict create \
1 readuncommitted \
2 readcommitted \
4 repeatableread \
8 serializable]
set DatabaseMetaDataI {}
set isolation [$ConnectionI getTransactionIsolation]
set readonly [$ConnectionI isReadOnly]
set useprepared 1
if {[llength $args] > 0} {
my configure {*}$args
}
}
forward statementCreate ::tdbc::jdbc::statement create
method configure args {
if {[llength $args] == 0} {
set result -isolation
lappend result [dict get $transaction $isolation]
lappend result -readonly $readonly
return $result
} elseif {[llength $args] == 1} {
set option [lindex $args 0]
switch -exact -- $option {
-i - -is - -iso - -isol - -isola - -isolat - -isolati -
-isolatio - -isolation {
return [dict get $transaction $isolation]
}
-r - -re - -rea - -read - -reado - -readon - -readonl -
-readonly {
return $readonly
}
default {
return -code error \
-errorcode [list TDBC GENERAL_ERROR HY000 JDBC \
BADOPTION $option] \
"bad option \"$option\": must be -isolation or -readonly"
}
}
} elseif {[llength $args] % 2 != 0} {
set cmd [lrange [info level 0] 0 end-[llength $args]]
return -code error \
-errorcode [list TDBC GENERAL_ERROR HY000 \
JDBC WRONGNUMARGS] \
"wrong # args, should be \"$cmd ?-option value?...\""
}
foreach {option value} $args {
switch -exact -- $option {
-i - -is - -iso - -isol - -isola - -isolat - -isolati -
-isolatio - -isolation {
switch -exact -- $value {
readu - readun - readunc - readunco - readuncom -
readuncomm - readuncommi - readuncommit -
readuncommitt - readuncommitte - readuncommitted {
$ConnectionI setTransactionIsolation 1
set isolation [$ConnectionI getTransactionIsolation]
}
readc - readco - readcom - readcomm - readcommi -
readcommit - readcommitt - readcommitte -
readcommitted {
$ConnectionI setTransactionIsolation 2
set isolation [$ConnectionI getTransactionIsolation]
}
rep - repe - repea - repeat - repeata - repeatab -
repeatabl - repeatable - repeatabler - repeatablere -
repeatablerea - repeatablread {
$ConnectionI setTransactionIsolation 4
set isolation [$ConnectionI getTransactionIsolation]
}
s - se - ser - seri - seria - serial - seriali -
serializ - serializa - serializab - serializabl -
serializable -
reado - readon - readonl - readonly {
$ConnectionI setTransactionIsolation 8
set isolation [$ConnectionI getTransactionIsolation]
}
default {
return -code error \
-errorcode [list TDBC GENERAL_ERROR HY000 \
JDBC BADISOLATION $value] \
"bad isolation level \"$value\": should be readuncommitted, readcommitted, repeatableread, serializable, or readonly"
}
}
}
-r - -re - -rea - -read - -reado - -readon - -readonl -
-readonly {
$ConnectionI setReadOnly $value
set readonly [$ConnectionI isReadOnly]
}
default {
return -code error \
-errorcode [list TDBC GENERAL_ERROR HY000 \
JDBC BADOPTION $value] \
"bad option \"$option\": must be -isolation or -readonly"
}
}
}
return
}
# invoke close method -> destroy our object
method close {} {
set mystats [my statements]
foreach mystat $mystats {
$mystat close
}
unset mystats
$ConnectionI close
next
}
method tables {{pattern %}} {
set retval {}
set DatabaseMetaDataI [$ConnectionI getMetaData]
set result [$DatabaseMetaDataI getTables [java::null] [java::null] $pattern [java::null]]
while { [$result next] } {
set row [dict create]
dict set row "TABLE_CAT" [$result getString "TABLE_CAT"]
dict set row "TABLE_SCHEM" [$result getString "TABLE_SCHEM"]
dict set row "TABLE_NAME" [$result getString "TABLE_NAME"]
dict set row "TABLE_TYPE" [$result getString "TABLE_TYPE"]
dict set retval [dict get $row TABLE_NAME] $row
}
return $retval
}
method columns {table {pattern %}} {
set retval {}
# Setup our pattern
set pattern [string map [list \
* {[*]} \
? {[?]} \
\[ \\\[ \
\] \\\[ \
_ ? \
% *] $pattern]
set sql "select * from $table where 1 = 0"
set stmt [$ConnectionI createStatement]
set query [$stmt executeQuery $sql]
set meta [$query getMetaData]
set count [$meta getColumnCount]
for {set i 1} {$i <= $count} {incr i 1} {
set column_name [$meta getColumnLabel $i]
if {![string match $pattern $column_name]} {
continue
}
set row [dict create]
dict set row name $column_name
dict set row type [string tolower [$meta getColumnTypeName $i]]
dict set row precision [$meta getPrecision $i]
dict set row scale [$meta getScale $i]
dict set row nullable [$meta isNullable $i]
dict set retval [dict get $row name] $row
}
$query close
$stmt close
return $retval
}
method primarykeys {table} {
set retval {}
set DatabaseMetaDataI [$ConnectionI getMetaData]
set result [$DatabaseMetaDataI getPrimaryKeys [java::null] [java::null] $table]
while { [$result next] } {
set row [dict create]
dict set row "TABLE_CAT" [$result getString "TABLE_CAT"]
dict set row "TABLE_SCHEM" [$result getString "TABLE_SCHEM"]
dict set row "TABLE_NAME" [$result getString "TABLE_NAME"]
dict set row "COLUMN_NAME" [$result getString "COLUMN_NAME"]
dict set retval [dict get $row COLUMN_NAME] $row
}
return $retval
}
method foreignkeys {args} {
set retval {}
set length [llength $args]
set ftable ""
if { $length != 2 || $length%2 != 0 } {
set cmd [lrange [info level 0] 0 1]
return -code error \
-errorcode [list TDBC GENERAL_ERROR HY000 \
JDBC WRONGNUMARGS] \
"wrong # args: should be \"$cmd -foreign tableName\""
return $retval
}
foreach {key table} $args {
if {[string compare $key "-foreign"] == 0} {
set ftable $table
} else {
return $retval
}
}
set DatabaseMetaDataI [$ConnectionI getMetaData]
set result [$DatabaseMetaDataI getImportedKeys [java::null] [java::null] $ftable]
while { [$result next] } {
set row [dict create]
set fktable [$result getString "FKTABLE_NAME"]
set fkschem [$result getString "FKTABLE_SCHEM"]
set fkcolumn [$result getString "FKCOLUMN_NAME"]
set pktable [$result getString "PKTABLE_NAME"]
set pkschem [$result getString "PKTABLE_SCHEM"]
set pkcolumn [$result getString "PKCOLUMN_NAME"]
if {[string compare $ftable $fktable] == 0} {
if {[dict exists $retval foreignTable]} {
set vallist [list $fktable]
} elseif {$fktable ni $vallist} {
lappend vallist $fktable
}
dict set retval foreignTable $vallist
if {[dict exists $retval foreignSchema]} {
set vallist2 [list $fkschem]
} elseif {$fkschem ni $vallist2} {
lappend vallist2 $fkschem
}
dict set retval foreignSchema $vallist2
if {[dict exists $retval foreignColumn]} {
set listval [list $fkcolumn]
} elseif {$fkcolumn ni $listval} {
lappend listval $fkcolumn
}
dict set retval foreignColumn $listval
# Get primary key info
if {[dict exists $retval primaryTable]} {
set primaryvallist [list $pktable]
} elseif {$pktable ni $primaryvallist} {
lappend primaryvallist $pktable
}
dict set retval primaryTable $primaryvallist
if {[dict exists $retval primarySchema]} {
set primaryvallist2 [list $pkschem]
} elseif {$pkschem ni $primaryvallist2} {
lappend primaryvallist2 $pkschem
}
dict set retval primarySchema $primaryvallist2
if {[dict exists $retval primaryColumn]} {
set primarylistval [list $pkcolumn]
} elseif {$pkcolumn ni $primarylistval} {
lappend primarylistval $pkcolumn
}
dict set retval primaryColumn $primarylistval
}
}
return $retval
}
# The 'prepareCall' method gives a portable interface to prepare
# calls to stored procedures. It delegates to 'prepare' to do the
# actual work.
method preparecall {call} {
regexp {^[[:space:]]*(?:([A-Za-z_][A-Za-z_0-9]*)[[:space:]]*=)?(.*)} \
$call -> varName rest
if {$varName eq {}} {
my prepare \\{$rest\\}
} else {
my prepare \\{:$varName=$rest\\}
}
}
# The 'begintransaction' method launches a database transaction
method begintransaction {} {
$ConnectionI setAutoCommit 0
}
# The 'commit' method commits a database transaction
method commit {} {
$ConnectionI commit
$ConnectionI setAutoCommit 1
}
# The 'rollback' method abandons a database transaction
method rollback {} {
$ConnectionI rollback
$ConnectionI setAutoCommit 1
}
method prepare {sqlCode} {
set result [next $sqlCode]
return $result
}
method getDBhandle {} {
return $ConnectionI
}
method setUsePrepared {flag} {
set useprepared $flag
}
method getUsePrepared {} {
return $useprepared
}
# For debug use
method getProductName {} {
set DatabaseMetaDataI [$ConnectionI getMetaData]
set productName [$DatabaseMetaDataI getDatabaseProductName]
return $productName
}
# For debug use
method getProductVersion {} {
set DatabaseMetaDataI [$ConnectionI getMetaData]
set productVersion [$DatabaseMetaDataI getDatabaseProductVersion]
return $productVersion
}
}
#------------------------------------------------------------------------------
#
# tdbc::jdbc::statement --
#
# The class 'tdbc::jdbc::statement' models one statement against a
# database accessed through a jdbc connection
#
#------------------------------------------------------------------------------
::oo::class create ::tdbc::jdbc::statement {
superclass ::tdbc::statement
variable Params ConnectionI sql useprepared stmt
constructor {connection sqlcode} {
next
set Params {}
set ConnectionI [$connection getDBhandle]
set sql {}
set useprepared [$connection getUsePrepared]
foreach token [::tdbc::tokenize $sqlcode] {
# I have no idea how to get params meta here,
# just give a default value.
if {[string index $token 0] in {$ : @}} {
dict set Params [string range $token 1 end] \
{type varchar direction in}
append sql "?"
continue
}
append sql $token
}
java::try {
if {$useprepared} {
set stmt [$ConnectionI prepareStatement $sql]
} else {
set stmt [$ConnectionI createStatement]
}
} catch {SQLException ex} {
error "SQLException: [$ex toString]"
} catch {Exception ex} {
error "Exception: [$ex toString]"
}
}
forward resultSetCreate ::tdbc::jdbc::resultset create
method close {} {
set mysets [my resultsets]
foreach myset $mysets {
$myset close
}
unset mysets
$stmt close
next
}
# The 'params' method returns descriptions of the parameters accepted
# by the statement
method params {} {
return $Params
}
method paramtype args {
set length [llength $args]
if {$length < 2} {
set cmd [lrange [info level 0] 0 end-[llength $args]]
return -code error \
-errorcode {TDBC GENERAL_ERROR HY000 jdbc WRONGNUMARGS} \
"wrong # args: should be \"$cmd param\""
}
set parameter [lindex $args 0]
if { [catch {set value [dict get $Params $parameter]}] } {
return -code error \
-errorcode {TDBC GENERAL_ERROR HY000 jdbc BADOPTION} \
"unknown parameter \"$parameter\""
}
set count 1
if {$length > 1} {
set direction [lindex $args $count]
if {$direction in {in out inout}} {
# I don't know how to setup direction, setup to in
dict set value direction in
incr count 1
}
}
if {$length > $count} {
set type [lindex $args $count]
# Only accept these types
if {$type in {bit tinyint smallint integer bigint \
char varchar longvarchar clob real float double \
date time timestamp decimal numeric binary \
varbinary longvarbinary blob}} {
dict set value type $type
}
}
# Skip other parameters and setup
dict set Params $parameter $value
}
method getStmthandle {} {
return $stmt
}
method getSql {} {
return $sql
}
method getUsePrepared {} {
return $useprepared
}
}
#------------------------------------------------------------------------------
#
# tdbc::jdbc::resultset --
#
# The class 'tdbc::jdbc::resultset' models the result set that is
# produced by executing a statement against a jdbc database.
#
#------------------------------------------------------------------------------
::oo::class create ::tdbc::jdbc::resultset {
superclass ::tdbc::resultset
variable -set {*}{
-stmt -sql -sqltypes -ResultSetI -ResultSetMetaDataI -params -RowCount
-columns -columnCount -useprepared -TdbcHelper
}
constructor {statement args} {
next
set -stmt [$statement getStmthandle]
set -params [$statement params]
set -sql [$statement getSql]
set -useprepared [$statement getUsePrepared]
set -ResultSetI {}
set -ResultSetMetaDataI {}
set -columns {}
set -columnCount 0
set -sqltypes {}
set -TdbcHelper {}
if {[llength $args] == 0} {
set keylist [dict keys ${-params}]
set count 1
# Using java::try to catch exception -> return error is OK?
java::try {
foreach mykey $keylist {
if {[info exists ::$mykey]} {
upvar 1 $mykey mykey1
set -sqltypes [dict get [dict get ${-params} $mykey] type]
switch -exact -- ${-sqltypes} {
bit {
${-stmt} setBoolean $count $mykey1
}
tinyint {
${-stmt} setByte $count $mykey1
}
smallint {
${-stmt} setShort $count $mykey1
}
integer {
${-stmt} setInt $count $mykey1
}
bigint {
${-stmt} setLong $count $mykey1
}
real {
${-stmt} setFloat $count $mykey1
}
float - double {
${-stmt} setDouble $count $mykey1
}
char - varcahr - longvarchar {
${-stmt} setString $count $mykey1
}
clob {
# only work for type 4 JDBC driver
set myclob [java::new StringReader $mykey1]
${-stmt} setCharacterStream $count $myclob
}
date {
# Get the Date string YYYY-MM-DD, convert to Java SQL type
set mydate [java::call java.sql.Date valueOf $mykey1]
${-stmt} setDate $count $mydate
}
time {
# Get the Time string HH:mm:ss, convert to Java SQL type
set mytime [java::call java.sql.Time valueOf $mykey1]
${-stmt} setTime $count $mytime
}
timestamp {
# Get the HH:mm:ss HH:mm:ss.S string, convert to Java SQL type
set mytimestamp [java::call java.sql.Timestamp valueOf $mykey1]
${-stmt} setTimestamp $count $mytimestamp
}
decimal - numeric {
# Try convert our string to a BigDecimal object
set mynumeric [java::new java.math.BigDecimal $mykey1]
${-stmt} setBigDecimal $count $mynumeric
}
binary - varbinary - longvarbinary - blob {
# Try to use UTF-8 encoding
set charsetName [java::new String "UTF-8"]
set jstring [java::new String $mykey1]
set bytearray [java::new ByteArrayInputStream [$jstring getBytes $charsetName]]
${-stmt} setBinaryStream $count $bytearray
}
default {
# maybe it is wrong
${-stmt} setString $count $mykey1
}
}
} else {
set -sqltypes [dict get [dict get ${-params} $mykey] type]
switch -exact -- ${-sqltypes} {
bit {
set opt1 [java::field java.sql.Types BOOLEAN]
}
tinyint {
set opt1 [java::field java.sql.Types TINYINT]
}
smallint {
set opt1 [java::field java.sql.Types SMALLINT]
}
integer {
set opt1 [java::field java.sql.Types INTEGER]
}
bigint {
set opt1 [java::field java.sql.Types BIGINT]
}
real {
set opt1 [java::field java.sql.Types FLOAT]
}
float {
set opt1 [java::field java.sql.Types FLOAT]
}
double {
set opt1 [java::field java.sql.Types DOUBLE]
}
char {
set opt1 [java::field java.sql.Types CHAR]
}
varchar {
set opt1 [java::field java.sql.Types VARCHAR]
}
longvarchar {
set opt1 [java::field java.sql.Types LONGVARCHAR]
}
clob {
set opt1 [java::field java.sql.Types CLOB]
}
date {
set opt1 [java::field java.sql.Types DATE]
}
time {
set opt1 [java::field java.sql.Types TIME]
}
timestamp {
set opt1 [java::field java.sql.Types TIMESTAMP]
}
decimal {
set opt1 [java::field java.sql.Types DECIMAL]
}
numeric {
set opt1 [java::field java.sql.Types NUMERIC]
}
binary {
set opt1 [java::field java.sql.Types BINARY]
}
varbinary {
set opt1 [java::field java.sql.Types VARBINARY]
}
longvarbinary {
set opt1 [java::field java.sql.Types LONGVARBINARY]
}
blob {
set opt1 [java::field java.sql.Types BLOB]
}
default {
# maybe it is wrong
set opt1 [java::field java.sql.Types VARCHAR]
}
}
${-stmt} setNull $count $opt1
}
incr count 1
}
if {${-useprepared}} {
set result [${-stmt} execute]
} else {
set result [${-stmt} execute ${-sql}]
}
} catch {SQLException ex} {
error "SQLException: [$ex toString]"
} catch {Exception ex} {
error "Exception: [$ex toString]"
}
if { $result != 0 } {
set -ResultSetI [${-stmt} getResultSet]
set -ResultSetMetaDataI [${-ResultSetI} getMetaData]
set -columnCount [${-ResultSetMetaDataI} getColumnCount]
set -RowCount 0
} else {
set -RowCount [${-stmt} getUpdateCount]
}
} elseif {[llength $args] == 1} {
# If the dict parameter is supplied, it is searched for a key
# whose name matches the name of the bound variable
set -paramDict [lindex $args 0]
set keylist [dict keys ${-params}]
set count 1
# Using java::try to catch exception -> return error is OK?
java::try {
foreach mykey $keylist {
if {![catch {set bound [dict get ${-paramDict} $mykey]}]} {
set -sqltypes [dict get [dict get ${-params} $mykey] type]
switch -exact -- ${-sqltypes} {
bit {
${-stmt} setBoolean $count $bound
}
tinyint {
${-stmt} setByte $count $bound
}
smallint {
${-stmt} setShort $count $bound
}
integer {
${-stmt} setInt $count $bound
}
bigint {
${-stmt} setLong $count $bound
}
real {
${-stmt} setFloat $count $bound
}
float - double {
${-stmt} setDouble $count $bound
}
char - varcahr - longvarchar {
${-stmt} setString $count $bound
}
clob {
# only work for type 4 JDBC driver
set myclob [java::new StringReader $bound]
${-stmt} setCharacterStream $count $myclob
}
date {
# Get the Date string YYYY-MM-DD, convert to Java SQL type
set mydate [java::call java.sql.Date valueOf $bound]
${-stmt} setDate $count $mydate
}
time {
# Get the Time string HH:mm:ss, convert to Java SQL type
set mytime [java::call java.sql.Time valueOf $bound]
${-stmt} setTime $count $mytime
}
timestamp {
# Get the HH:mm:ss HH:mm:ss.S string, convert to Java SQL type
set mytimestamp [java::call java.sql.Timestamp valueOf $bound]
${-stmt} setTimestamp $count $mytimestamp
}
decimal - numeric {
# Try convert our string to a BigDecimal object
set mynumeric [java::new java.math.BigDecimal $bound]
${-stmt} setBigDecimal $count $mynumeric
}
binary - varbinary - longvarbinary - blob {
# Try to use UTF-8 encoding
set charsetName [java::new String "UTF-8"]
set jstring [java::new String $bound]
set bytearray [java::new ByteArrayInputStream [$jstring getBytes $charsetName]]
${-stmt} setBinaryStream $count $bytearray
}
default {
# maybe it is wrong
${-stmt} setString $count $bound
}
}
} else {
set -sqltypes [dict get [dict get ${-params} $mykey] type]
switch -exact -- ${-sqltypes} {
bit {
set opt1 [java::field java.sql.Types BOOLEAN]
}
tinyint {
set opt1 [java::field java.sql.Types TINYINT]
}
smallint {
set opt1 [java::field java.sql.Types SMALLINT]
}
integer {
set opt1 [java::field java.sql.Types INTEGER]
}
bigint {
set opt1 [java::field java.sql.Types BIGINT]
}
real {
set opt1 [java::field java.sql.Types FLOAT]
}
float {
set opt1 [java::field java.sql.Types FLOAT]
}
double {
set opt1 [java::field java.sql.Types DOUBLE]
}
char {
set opt1 [java::field java.sql.Types CHAR]
}
varchar {
set opt1 [java::field java.sql.Types VARCHAR]
}
longvarchar {
set opt1 [java::field java.sql.Types LONGVARCHAR]
}
clob {
set opt1 [java::field java.sql.Types CLOB]
}
date {
set opt1 [java::field java.sql.Types DATE]
}
time {
set opt1 [java::field java.sql.Types TIME]
}
timestamp {
set opt1 [java::field java.sql.Types TIMESTAMP]
}
decimal {
set opt1 [java::field java.sql.Types DECIMAL]
}
numeric {
set opt1 [java::field java.sql.Types NUMERIC]
}
binary {
set opt1 [java::field java.sql.Types BINARY]
}
varbinary {
set opt1 [java::field java.sql.Types VARBINARY]
}
longvarbinary {
set opt1 [java::field java.sql.Types LONGVARBINARY]
}
blob {
set opt1 [java::field java.sql.Types BLOB]
}
default {
# maybe it is wrong
set opt1 [java::field java.sql.Types VARCHAR]
}
}
${-stmt} setNull $count $opt1
}
incr count 1
}
if {${-useprepared}} {
set result [${-stmt} execute]
} else {
set result [${-stmt} execute ${-sql}]
}
} catch {SQLException ex} {
error "SQLException: [$ex toString]"
} catch {Exception e} {
error "Exception: [$ex toString]"
}
if {$result} {
set -ResultSetI [${-stmt} getResultSet]
set -ResultSetMetaDataI [${-ResultSetI} getMetaData]
set -columnCount [${-ResultSetMetaDataI} getColumnCount]