CallableStatementRegressionTest.java
62.9 KB
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
/*
Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.
The MySQL Connector/J is licensed under the terms of the GPLv2
<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most MySQL Connectors.
There are special exceptions to the terms and conditions of the GPLv2 as it is applied to
this software, see the FOSS License Exception
<http://www.mysql.com/about/legal/licensing/foss-exception.html>.
This program is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; version 2
of the License.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this
program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth
Floor, Boston, MA 02110-1301 USA
*/
package testsuite.regression;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.Properties;
import com.mysql.jdbc.NonRegisteringDriver;
import com.mysql.jdbc.SQLError;
import testsuite.BaseTestCase;
/**
* Tests fixes for bugs in CallableStatement code.
*/
public class CallableStatementRegressionTest extends BaseTestCase {
public CallableStatementRegressionTest(String name) {
super(name);
}
/**
* Runs all test cases in this test suite
*
* @param args
* ignored
*/
public static void main(String[] args) {
junit.textui.TestRunner.run(CallableStatementRegressionTest.class);
}
/**
* Tests fix for BUG#3539 getProcedures() does not return any procedures in
* result set
*
* @throws Exception
* if an error occurs.
*/
public void testBug3539() throws Exception {
if (!serverSupportsStoredProcedures()) {
return;
}
createProcedure("testBug3539", "()\nBEGIN\nSELECT 1;end\n");
this.rs = this.conn.getMetaData().getProcedures(null, null, "testBug3539");
assertTrue(this.rs.next());
assertTrue("testBug3539".equals(this.rs.getString(3)));
}
/**
* Tests fix for BUG#3540 getProcedureColumns doesn't work with wildcards
* for procedure name
*
* @throws Exception
* if an error occurs.
*/
public void testBug3540() throws Exception {
if (!serverSupportsStoredProcedures()) {
return;
}
createProcedure("testBug3540", "(x int, out y int)\nBEGIN\nSELECT 1;end\n");
this.rs = this.conn.getMetaData().getProcedureColumns(null, null, "testBug3540%", "%");
assertTrue(this.rs.next());
assertEquals("testBug3540", this.rs.getString(3));
assertEquals("x", this.rs.getString(4));
assertTrue(this.rs.next());
assertEquals("testBug3540", this.rs.getString(3));
assertEquals("y", this.rs.getString(4));
assertTrue(!this.rs.next());
}
/**
* Tests fix for BUG#7026 - DBMD.getProcedures() doesn't respect catalog
* parameter
*
* @throws Exception
* if the test fails.
*/
public void testBug7026() throws Exception {
if (!serverSupportsStoredProcedures()) {
return;
}
createProcedure("testBug7026", "(x int, out y int)\nBEGIN\nSELECT 1;end\n");
//
// Should be found this time.
//
this.rs = this.conn.getMetaData().getProcedures(this.conn.getCatalog(), null, "testBug7026");
assertTrue(this.rs.next());
assertTrue("testBug7026".equals(this.rs.getString(3)));
assertTrue(!this.rs.next());
//
// This time, shouldn't be found, because not associated with this (bogus) catalog
//
this.rs = this.conn.getMetaData().getProcedures("abfgerfg", null, "testBug7026");
assertTrue(!this.rs.next());
//
// Should be found this time as well, as we haven't specified a catalog.
//
this.rs = this.conn.getMetaData().getProcedures(null, null, "testBug7026");
assertTrue(this.rs.next());
assertTrue("testBug7026".equals(this.rs.getString(3)));
assertTrue(!this.rs.next());
}
/**
* Tests fix for BUG#9319 -- Stored procedures with same name in different
* databases confuse the driver when it tries to determine parameter
* counts/types.
*
* @throws Exception
* if the test fails
*/
public void testBug9319() throws Exception {
if (!serverSupportsStoredProcedures()) {
return;
}
boolean doASelect = true; // SELECT currently causes the server to hang on the last execution of this testcase, filed as BUG#9405
if (isAdminConnectionConfigured()) {
Connection db2Connection = null;
Connection db1Connection = null;
db2Connection = getAdminConnection();
db1Connection = getAdminConnection();
Statement db1st = db1Connection.createStatement();
Statement db2st = db2Connection.createStatement();
createDatabase(db2st, "db_9319_2");
db2Connection.setCatalog("db_9319_2");
createProcedure(db2st, "COMPROVAR_USUARI",
"(IN p_CodiUsuari VARCHAR(10),\nIN p_contrasenya VARCHAR(10),\nOUT p_userId INTEGER,"
+ "\nOUT p_userName VARCHAR(30),\nOUT p_administrador VARCHAR(1),\nOUT p_idioma VARCHAR(2))\nBEGIN"
+ (doASelect ? "\nselect 2;" : "\nSELECT 2 INTO p_administrador;") + "\nEND");
createDatabase(db1st, "db_9319_1");
db1Connection.setCatalog("db_9319_1");
createProcedure(db1st, "COMPROVAR_USUARI",
"(IN p_CodiUsuari VARCHAR(10),\nIN p_contrasenya VARCHAR(10),\nOUT p_userId INTEGER,"
+ "\nOUT p_userName VARCHAR(30),\nOUT p_administrador VARCHAR(1))\nBEGIN"
+ (doASelect ? "\nselect 1;" : "\nSELECT 1 INTO p_administrador;") + "\nEND");
CallableStatement cstmt = db2Connection.prepareCall("{ call COMPROVAR_USUARI(?, ?, ?, ?, ?, ?) }");
cstmt.setString(1, "abc");
cstmt.setString(2, "def");
cstmt.registerOutParameter(3, java.sql.Types.INTEGER);
cstmt.registerOutParameter(4, java.sql.Types.VARCHAR);
cstmt.registerOutParameter(5, java.sql.Types.VARCHAR);
cstmt.registerOutParameter(6, java.sql.Types.VARCHAR);
cstmt.execute();
if (doASelect) {
this.rs = cstmt.getResultSet();
assertTrue(this.rs.next());
assertEquals(2, this.rs.getInt(1));
} else {
assertEquals(2, cstmt.getInt(5));
}
cstmt = db1Connection.prepareCall("{ call COMPROVAR_USUARI(?, ?, ?, ?, ?, ?) }");
cstmt.setString(1, "abc");
cstmt.setString(2, "def");
cstmt.registerOutParameter(3, java.sql.Types.INTEGER);
cstmt.registerOutParameter(4, java.sql.Types.VARCHAR);
cstmt.registerOutParameter(5, java.sql.Types.VARCHAR);
try {
cstmt.registerOutParameter(6, java.sql.Types.VARCHAR);
fail("Should've thrown an exception");
} catch (SQLException sqlEx) {
assertEquals(SQLError.SQL_STATE_ILLEGAL_ARGUMENT, sqlEx.getSQLState());
}
cstmt = db1Connection.prepareCall("{ call COMPROVAR_USUARI(?, ?, ?, ?, ?) }");
cstmt.setString(1, "abc");
cstmt.setString(2, "def");
cstmt.registerOutParameter(3, java.sql.Types.INTEGER);
cstmt.registerOutParameter(4, java.sql.Types.VARCHAR);
cstmt.registerOutParameter(5, java.sql.Types.VARCHAR);
cstmt.execute();
if (doASelect) {
this.rs = cstmt.getResultSet();
assertTrue(this.rs.next());
assertEquals(1, this.rs.getInt(1));
} else {
assertEquals(1, cstmt.getInt(5));
}
String quoteChar = db2Connection.getMetaData().getIdentifierQuoteString();
cstmt = db2Connection.prepareCall(
"{ call " + quoteChar + db1Connection.getCatalog() + quoteChar + "." + quoteChar + "COMPROVAR_USUARI" + quoteChar + "(?, ?, ?, ?, ?) }");
cstmt.setString(1, "abc");
cstmt.setString(2, "def");
cstmt.registerOutParameter(3, java.sql.Types.INTEGER);
cstmt.registerOutParameter(4, java.sql.Types.VARCHAR);
cstmt.registerOutParameter(5, java.sql.Types.VARCHAR);
cstmt.execute();
if (doASelect) {
this.rs = cstmt.getResultSet();
assertTrue(this.rs.next());
assertEquals(1, this.rs.getInt(1));
} else {
assertEquals(1, cstmt.getInt(5));
}
}
}
/*
* public void testBug9319() throws Exception { boolean doASelect = false;
* // SELECT currently causes the server to hang on the // last execution of
* this testcase, filed as BUG#9405
*
* if (versionMeetsMinimum(5, 0, 2)) { if (isAdminConnectionConfigured()) {
* Connection db2Connection = null; Connection db1Connection = null;
*
* try { db2Connection = getAdminConnection();
*
* db2Connection.createStatement().executeUpdate( "CREATE DATABASE IF NOT
* EXISTS db_9319"); db2Connection.setCatalog("db_9319");
*
* db2Connection.createStatement().executeUpdate( "DROP PROCEDURE IF EXISTS
* COMPROVAR_USUARI");
*
* db2Connection.createStatement().executeUpdate( "CREATE PROCEDURE
* COMPROVAR_USUARI(IN p_CodiUsuari VARCHAR(10)," + "\nIN p_contrasenya
* VARCHAR(10)," + "\nOUT p_userId INTEGER," + "\nOUT p_userName
* VARCHAR(30)," + "\nOUT p_administrador VARCHAR(1)," + "\nOUT p_idioma
* VARCHAR(2))" + "\nBEGIN" + (doASelect ? "\nselect 2;" : "\nSELECT 2 INTO
* p_administrador;" ) + "\nEND");
*
* this.stmt .executeUpdate("DROP PROCEDURE IF EXISTS COMPROVAR_USUARI");
* this.stmt .executeUpdate("CREATE PROCEDURE COMPROVAR_USUARI(IN
* p_CodiUsuari VARCHAR(10)," + "\nIN p_contrasenya VARCHAR(10)," + "\nOUT
* p_userId INTEGER," + "\nOUT p_userName VARCHAR(30)," + "\nOUT
* p_administrador VARCHAR(1))" + "\nBEGIN" + (doASelect ? "\nselect 1;" :
* "\nSELECT 1 INTO p_administrador;" ) + "\nEND");
*
* CallableStatement cstmt = db2Connection .prepareCall("{ call
* COMPROVAR_USUARI(?, ?, ?, ?, ?, ?) }"); cstmt.setString(1, "abc");
* cstmt.setString(2, "def"); cstmt.registerOutParameter(3,
* java.sql.Types.INTEGER); cstmt.registerOutParameter(4,
* java.sql.Types.VARCHAR); cstmt.registerOutParameter(5,
* java.sql.Types.VARCHAR);
*
* cstmt.registerOutParameter(6, java.sql.Types.VARCHAR);
*
* cstmt.execute();
*
* if (doASelect) { this.rs = cstmt.getResultSet();
* assertTrue(this.rs.next()); assertEquals(2, this.rs.getInt(1)); } else {
* assertEquals(2, cstmt.getInt(5)); }
*
* cstmt = this.conn .prepareCall("{ call COMPROVAR_USUARI(?, ?, ?, ?, ?, ?)
* }"); cstmt.setString(1, "abc"); cstmt.setString(2, "def");
* cstmt.registerOutParameter(3, java.sql.Types.INTEGER);
* cstmt.registerOutParameter(4, java.sql.Types.VARCHAR);
* cstmt.registerOutParameter(5, java.sql.Types.VARCHAR);
*
* try { cstmt.registerOutParameter(6, java.sql.Types.VARCHAR);
* fail("Should've thrown an exception"); } catch (SQLException sqlEx) {
* assertEquals(SQLError.SQL_STATE_ILLEGAL_ARGUMENT, sqlEx .getSQLState());
* }
*
* cstmt = this.conn .prepareCall("{ call COMPROVAR_USUARI(?, ?, ?, ?, ?)
* }"); cstmt.setString(1, "abc"); cstmt.setString(2, "def");
* cstmt.registerOutParameter(3, java.sql.Types.INTEGER);
* cstmt.registerOutParameter(4, java.sql.Types.VARCHAR);
* cstmt.registerOutParameter(5, java.sql.Types.VARCHAR);
*
* cstmt.execute();
*
* if (doASelect) { this.rs = cstmt.getResultSet();
* assertTrue(this.rs.next()); assertEquals(1, this.rs.getInt(1)); } else {
* assertEquals(1, cstmt.getInt(5)); }
*
* String quoteChar =
* db2Connection.getMetaData().getIdentifierQuoteString();
*
* cstmt = db2Connection .prepareCall("{ call " + quoteChar +
* this.conn.getCatalog() + quoteChar + "." + quoteChar + "COMPROVAR_USUARI"
* + quoteChar + "(?, ?, ?, ?, ?) }"); cstmt.setString(1, "abc");
* cstmt.setString(2, "def"); cstmt.registerOutParameter(3,
* java.sql.Types.INTEGER); cstmt.registerOutParameter(4,
* java.sql.Types.VARCHAR); cstmt.registerOutParameter(5,
* java.sql.Types.VARCHAR);
*
* cstmt.execute();
*
* if (doASelect) { this.rs = cstmt.getResultSet();
* assertTrue(this.rs.next()); assertEquals(1, this.rs.getInt(1)); } else {
* assertEquals(1, cstmt.getInt(5)); } } finally { if (db2Connection !=
* null) { db2Connection.createStatement().executeUpdate( "DROP PROCEDURE IF
* EXISTS COMPROVAR_USUARI"); //
* db2Connection.createStatement().executeUpdate( // "DROP DATABASE IF
* EXISTS db_9319"); }
*
* this.stmt .executeUpdate("DROP PROCEDURE IF EXISTS COMPROVAR_USUARI"); }
* } } }
*/
/**
* Tests fix for BUG#9682 - Stored procedures with DECIMAL parameters with
* storage specifications that contained "," in them would fail.
*
* @throws Exception
* if the test fails.
*/
public void testBug9682() throws Exception {
if (!serverSupportsStoredProcedures()) {
return;
}
createProcedure("testBug9682", "(decimalParam DECIMAL(18,0))\nBEGIN\n SELECT 1;\nEND");
CallableStatement cStmt = null;
try {
cStmt = this.conn.prepareCall("Call testBug9682(?)");
cStmt.setDouble(1, 18.0);
cStmt.execute();
} finally {
if (cStmt != null) {
cStmt.close();
}
}
}
/**
* Tests fix forBUG#10310 - Driver doesn't support {?=CALL(...)} for calling
* stored functions. This involved adding support for function retrieval to
* DatabaseMetaData.getProcedures() and getProcedureColumns() as well.
*
* @throws Exception
* if the test fails.
*/
public void testBug10310() throws Exception {
if (!serverSupportsStoredProcedures()) {
return;
}
CallableStatement cStmt = null;
try {
this.stmt.executeUpdate("DROP FUNCTION IF EXISTS testBug10310");
this.stmt.executeUpdate("CREATE FUNCTION testBug10310(a float, b bigint, c int) RETURNS INT NO SQL\nBEGIN\nRETURN a;\nEND");
cStmt = this.conn.prepareCall("{? = CALL testBug10310(?,?,?)}");
cStmt.registerOutParameter(1, Types.INTEGER);
cStmt.setFloat(2, 2);
cStmt.setInt(3, 1);
cStmt.setInt(4, 1);
assertEquals(4, cStmt.getParameterMetaData().getParameterCount());
assertEquals(Types.INTEGER, cStmt.getParameterMetaData().getParameterType(1));
java.sql.DatabaseMetaData dbmd = this.conn.getMetaData();
this.rs = ((com.mysql.jdbc.DatabaseMetaData) dbmd).getFunctionColumns(this.conn.getCatalog(), null, "testBug10310", "%");
ResultSetMetaData rsmd = this.rs.getMetaData();
assertEquals(17, rsmd.getColumnCount());
assertEquals("FUNCTION_CAT", rsmd.getColumnName(1));
assertEquals("FUNCTION_SCHEM", rsmd.getColumnName(2));
assertEquals("FUNCTION_NAME", rsmd.getColumnName(3));
assertEquals("COLUMN_NAME", rsmd.getColumnName(4));
assertEquals("COLUMN_TYPE", rsmd.getColumnName(5));
assertEquals("DATA_TYPE", rsmd.getColumnName(6));
assertEquals("TYPE_NAME", rsmd.getColumnName(7));
assertEquals("PRECISION", rsmd.getColumnName(8));
assertEquals("LENGTH", rsmd.getColumnName(9));
assertEquals("SCALE", rsmd.getColumnName(10));
assertEquals("RADIX", rsmd.getColumnName(11));
assertEquals("NULLABLE", rsmd.getColumnName(12));
assertEquals("REMARKS", rsmd.getColumnName(13));
assertEquals("CHAR_OCTET_LENGTH", rsmd.getColumnName(14));
assertEquals("ORDINAL_POSITION", rsmd.getColumnName(15));
assertEquals("IS_NULLABLE", rsmd.getColumnName(16));
assertEquals("SPECIFIC_NAME", rsmd.getColumnName(17));
this.rs.close();
assertFalse(cStmt.execute());
assertEquals(2f, cStmt.getInt(1), .001);
assertEquals("java.lang.Integer", cStmt.getObject(1).getClass().getName());
assertEquals(-1, cStmt.executeUpdate());
assertEquals(2f, cStmt.getInt(1), .001);
assertEquals("java.lang.Integer", cStmt.getObject(1).getClass().getName());
cStmt.setFloat("a", 4);
cStmt.setInt("b", 1);
cStmt.setInt("c", 1);
assertFalse(cStmt.execute());
assertEquals(4f, cStmt.getInt(1), .001);
assertEquals("java.lang.Integer", cStmt.getObject(1).getClass().getName());
assertEquals(-1, cStmt.executeUpdate());
assertEquals(4f, cStmt.getInt(1), .001);
assertEquals("java.lang.Integer", cStmt.getObject(1).getClass().getName());
// Check metadata while we're at it
this.rs = dbmd.getProcedures(this.conn.getCatalog(), null, "testBug10310");
this.rs.next();
assertEquals("testBug10310", this.rs.getString("PROCEDURE_NAME"));
assertEquals(java.sql.DatabaseMetaData.procedureReturnsResult, this.rs.getShort("PROCEDURE_TYPE"));
cStmt.setNull(2, Types.FLOAT);
cStmt.setInt(3, 1);
cStmt.setInt(4, 1);
assertFalse(cStmt.execute());
assertEquals(0f, cStmt.getInt(1), .001);
assertEquals(true, cStmt.wasNull());
assertEquals(null, cStmt.getObject(1));
assertEquals(true, cStmt.wasNull());
assertEquals(-1, cStmt.executeUpdate());
assertEquals(0f, cStmt.getInt(1), .001);
assertEquals(true, cStmt.wasNull());
assertEquals(null, cStmt.getObject(1));
assertEquals(true, cStmt.wasNull());
// Check with literals, not all parameters filled!
cStmt = this.conn.prepareCall("{? = CALL testBug10310(4,5,?)}");
cStmt.registerOutParameter(1, Types.INTEGER);
cStmt.setInt(2, 1);
assertFalse(cStmt.execute());
assertEquals(4f, cStmt.getInt(1), .001);
assertEquals("java.lang.Integer", cStmt.getObject(1).getClass().getName());
assertEquals(-1, cStmt.executeUpdate());
assertEquals(4f, cStmt.getInt(1), .001);
assertEquals("java.lang.Integer", cStmt.getObject(1).getClass().getName());
assertEquals(2, cStmt.getParameterMetaData().getParameterCount());
assertEquals(Types.INTEGER, cStmt.getParameterMetaData().getParameterType(1));
assertEquals(Types.INTEGER, cStmt.getParameterMetaData().getParameterType(2));
} finally {
if (this.rs != null) {
this.rs.close();
this.rs = null;
}
if (cStmt != null) {
cStmt.close();
}
this.stmt.executeUpdate("DROP FUNCTION IF EXISTS testBug10310");
}
}
/**
* Tests fix for Bug#12417 - stored procedure catalog name is case-sensitive
* on Windows (this is actually a server bug, but we have a workaround in
* place for it now).
*
* @throws Exception
* if the test fails.
*/
public void testBug12417() throws Exception {
if (serverSupportsStoredProcedures() && isServerRunningOnWindows()) {
createProcedure("testBug12417", "()\nBEGIN\nSELECT 1;end\n");
Connection ucCatalogConn = null;
try {
ucCatalogConn = getConnectionWithProps((Properties) null);
ucCatalogConn.setCatalog(this.conn.getCatalog().toUpperCase());
ucCatalogConn.prepareCall("{call testBug12417()}");
} finally {
if (ucCatalogConn != null) {
ucCatalogConn.close();
}
}
}
}
public void testBug15121() throws Exception {
if (!this.DISABLED_testBug15121 /* needs to be fixed on server */) {
if (versionMeetsMinimum(5, 0)) {
createProcedure("p_testBug15121", "()\nBEGIN\nSELECT * from idonotexist;\nEND");
Properties props = new Properties();
props.setProperty(NonRegisteringDriver.DBNAME_PROPERTY_KEY, "");
Connection noDbConn = null;
try {
noDbConn = getConnectionWithProps(props);
StringBuilder queryBuf = new StringBuilder("{call ");
String quotedId = this.conn.getMetaData().getIdentifierQuoteString();
queryBuf.append(quotedId);
queryBuf.append(this.conn.getCatalog());
queryBuf.append(quotedId);
queryBuf.append(".p_testBug15121()}");
noDbConn.prepareCall(queryBuf.toString()).execute();
} finally {
if (noDbConn != null) {
noDbConn.close();
}
}
}
}
}
/**
* Tests fix for BUG#15464 - INOUT parameter does not store IN value.
*
* @throws Exception
* if the test fails
*/
public void testBug15464() throws Exception {
if (!serverSupportsStoredProcedures()) {
return;
}
createProcedure("testInOutParam",
"(IN p1 VARCHAR(255), INOUT p2 INT)\nbegin\n DECLARE z INT;\n" + "SET z = p2 + 1;\nSET p2 = z;\nSELECT p1;\nSELECT CONCAT('zyxw', p1);\nend\n");
CallableStatement storedProc = null;
storedProc = this.conn.prepareCall("{call testInOutParam(?, ?)}");
storedProc.setString(1, "abcd");
storedProc.setInt(2, 4);
storedProc.registerOutParameter(2, Types.INTEGER);
storedProc.execute();
assertEquals(5, storedProc.getInt(2));
}
/**
* Tests fix for BUG#17898 - registerOutParameter not working when some
* parameters pre-populated. Still waiting for feedback from JDBC experts
* group to determine what correct parameter count from getMetaData() should
* be, however.
*
* @throws Exception
* if the test fails
*/
public void testBug17898() throws Exception {
if (!serverSupportsStoredProcedures()) {
return;
}
createProcedure("testBug17898", "(param1 VARCHAR(50), OUT param2 INT)\nBEGIN\nDECLARE rtn INT;\n" + "SELECT 1 INTO rtn;\nSET param2=rtn;\nEND");
CallableStatement cstmt = this.conn.prepareCall("{CALL testBug17898('foo', ?)}");
cstmt.registerOutParameter(1, Types.INTEGER);
cstmt.execute();
assertEquals(1, cstmt.getInt(1));
cstmt.clearParameters();
cstmt.registerOutParameter("param2", Types.INTEGER);
cstmt.execute();
assertEquals(1, cstmt.getInt(1));
}
/**
* Tests fix for BUG#21462 - JDBC (and ODBC) specifications allow
* no-parenthesis CALL statements for procedures with no arguments, MySQL
* server does not.
*
* @throws Exception
* if the test fails.
*/
public void testBug21462() throws Exception {
if (!serverSupportsStoredProcedures()) {
return;
}
createProcedure("testBug21462", "() BEGIN SELECT 1; END");
CallableStatement cstmt = null;
try {
cstmt = this.conn.prepareCall("{CALL testBug21462}");
cstmt.execute();
} finally {
if (cstmt != null) {
cstmt.close();
}
}
}
/**
* Tests fix for BUG#22024 - Newlines causing whitespace to span confuse
* procedure parser when getting parameter metadata for stored procedures.
*
* @throws Exception
* if the test fails
*/
public void testBug22024() throws Exception {
if (!serverSupportsStoredProcedures()) {
return;
}
createProcedure("testBug22024_1", "(\r\n)\r\n BEGIN SELECT 1; END");
createProcedure("testBug22024_2", "(\r\na INT)\r\n BEGIN SELECT 1; END");
CallableStatement cstmt = null;
try {
cstmt = this.conn.prepareCall("{CALL testBug22024_1()}");
cstmt.execute();
cstmt = this.conn.prepareCall("{CALL testBug22024_2(?)}");
cstmt.setInt(1, 1);
cstmt.execute();
} finally {
if (cstmt != null) {
cstmt.close();
}
}
}
/**
* Tests workaround for server crash when calling stored procedures via a
* server-side prepared statement (driver now detects prepare(stored
* procedure) and substitutes client-side prepared statement).
*
* @throws Exception
* if the test fails
*/
public void testBug22297() throws Exception {
if (!serverSupportsStoredProcedures()) {
return;
}
createTable("tblTestBug2297_1", "(id varchar(20) NOT NULL default '',Income double(19,2) default NULL)");
createTable("tblTestBug2297_2", "(id varchar(20) NOT NULL default '',CreatedOn datetime default NULL)");
createProcedure("testBug22297", "(pcaseid INT) BEGIN\nSET @sql = \"DROP TEMPORARY TABLE IF EXISTS tmpOrders\";"
+ " PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;"
+ "\nSET @sql = \"CREATE TEMPORARY TABLE tmpOrders SELECT id, 100 AS Income FROM tblTestBug2297_1 GROUP BY id\"; PREPARE stmt FROM @sql;"
+ " EXECUTE stmt; DEALLOCATE PREPARE stmt;\n SELECT id, Income FROM (SELECT e.id AS id ,COALESCE(prof.Income,0) AS Income"
+ "\n FROM tblTestBug2297_2 e LEFT JOIN tmpOrders prof ON e.id = prof.id\n WHERE e.CreatedOn > '2006-08-01') AS Final ORDER BY id;\nEND");
this.stmt.executeUpdate("INSERT INTO tblTestBug2297_1 (`id`,`Income`) VALUES ('a',4094.00),('b',500.00),('c',3462.17), ('d',500.00), ('e',600.00)");
this.stmt.executeUpdate("INSERT INTO tblTestBug2297_2 (`id`,`CreatedOn`) VALUES ('d','2006-08-31 00:00:00'),('e','2006-08-31 00:00:00'),"
+ "('b','2006-08-31 00:00:00'),('c','2006-08-31 00:00:00'),('a','2006-08-31 00:00:00')");
this.pstmt = this.conn.prepareStatement("{CALL testBug22297(?)}");
this.pstmt.setInt(1, 1);
this.rs = this.pstmt.executeQuery();
String[] ids = new String[] { "a", "b", "c", "d", "e" };
int pos = 0;
while (this.rs.next()) {
assertEquals(ids[pos++], this.rs.getString(1));
assertEquals(100, this.rs.getInt(2));
}
assertTrue(this.pstmt.getClass().getName().indexOf("Server") == -1);
}
public void testHugeNumberOfParameters() throws Exception {
if (!serverSupportsStoredProcedures()) {
return;
}
StringBuilder procDef = new StringBuilder("(OUT param_0 VARCHAR(32)");
StringBuilder placeholders = new StringBuilder("?");
for (int i = 1; i < 274; i++) {
procDef.append(", OUT param_" + i + " VARCHAR(32)");
placeholders.append(",?");
}
procDef.append(")\nBEGIN\nSELECT 1;\nEND");
createProcedure("testHugeNumberOfParameters", procDef.toString());
CallableStatement cStmt = null;
try {
cStmt = this.conn.prepareCall("{call testHugeNumberOfParameters(" + placeholders.toString() + ")}");
cStmt.registerOutParameter(274, Types.VARCHAR);
cStmt.execute();
} finally {
if (cStmt != null) {
cStmt.close();
}
}
}
public void testPrepareOfMultiRs() throws Exception {
if (!serverSupportsStoredProcedures()) {
return;
}
createProcedure("p", "() begin select 1; select 2; end;");
PreparedStatement ps = null;
try {
ps = this.conn.prepareStatement("call p()");
ps.execute();
this.rs = ps.getResultSet();
assertTrue(this.rs.next());
assertEquals(1, this.rs.getInt(1));
assertTrue(ps.getMoreResults());
this.rs = ps.getResultSet();
assertTrue(this.rs.next());
assertEquals(2, this.rs.getInt(1));
assertTrue(!ps.getMoreResults());
} finally {
if (this.rs != null) {
this.rs.close();
this.rs = null;
}
if (ps != null) {
ps.close();
}
}
}
/**
* Tests fix for BUG#25379 - INOUT parameters in CallableStatements get
* doubly-escaped.
*
* @throws Exception
* if the test fails.
*/
public void testBug25379() throws Exception {
if (!serverSupportsStoredProcedures()) {
return;
}
createTable("testBug25379", "(col char(40))");
createProcedure("sp_testBug25379", "(INOUT invalue char(255))\nBEGIN" + "\ninsert into testBug25379(col) values(invalue);\nEND");
CallableStatement cstmt = this.conn.prepareCall("{call sp_testBug25379(?)}");
cstmt.setString(1, "'john'");
cstmt.executeUpdate();
assertEquals("'john'", cstmt.getString(1));
assertEquals("'john'", getSingleValue("testBug25379", "col", "").toString());
}
/**
* Tests fix for BUG#25715 - CallableStatements with OUT/INOUT parameters
* that are "binary" have extra 7 bytes (which happens to be the _binary
* introducer!)
*
* @throws Exception
* if the test fails.
*/
public void testBug25715() throws Exception {
if (!serverSupportsStoredProcedures()) {
return; // no stored procs
}
createProcedure("spbug25715", "(INOUT mblob MEDIUMBLOB) BEGIN SELECT 1 FROM DUAL WHERE 1=0;\nEND");
CallableStatement cstmt = null;
try {
cstmt = this.conn.prepareCall("{call spbug25715(?)}");
byte[] buf = new byte[65];
for (int i = 0; i < 65; i++) {
buf[i] = 1;
}
int il = buf.length;
int[] typesToTest = new int[] { Types.BIT, Types.BINARY, Types.BLOB, Types.JAVA_OBJECT, Types.LONGVARBINARY, Types.VARBINARY };
for (int i = 0; i < typesToTest.length; i++) {
cstmt.setBinaryStream("mblob", new ByteArrayInputStream(buf), buf.length);
cstmt.registerOutParameter("mblob", typesToTest[i]);
cstmt.executeUpdate();
InputStream is = cstmt.getBlob("mblob").getBinaryStream();
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
int bytesRead = 0;
byte[] readBuf = new byte[256];
while ((bytesRead = is.read(readBuf)) != -1) {
bOut.write(readBuf, 0, bytesRead);
}
byte[] fromSelectBuf = bOut.toByteArray();
int ol = fromSelectBuf.length;
assertEquals(il, ol);
}
cstmt.close();
} finally {
if (cstmt != null) {
cstmt.close();
}
}
}
protected boolean serverSupportsStoredProcedures() throws SQLException {
return versionMeetsMinimum(5, 0);
}
public void testBug26143() throws Exception {
if (!serverSupportsStoredProcedures()) {
return; // no stored procedure support
}
try {
dropProcedure("testBug26143");
this.stmt.executeUpdate("CREATE DEFINER=CURRENT_USER PROCEDURE testBug26143(I INT) COMMENT 'abcdefg'\nBEGIN\nSELECT I * 10;\nEND");
this.conn.prepareCall("{call testBug26143(?)").close();
} finally {
dropProcedure("testBug26143");
}
}
/**
* Tests fix for BUG#26959 - comments confuse procedure parser.
*
* @throws Exception
* if the test fails
*/
public void testBug26959() throws Exception {
if (!serverSupportsStoredProcedures()) {
return;
}
createProcedure("testBug26959",
"(_ACTION varchar(20),\n`/*dumb-identifier-1*/` int,\n`#dumb-identifier-2` int,\n`--dumb-identifier-3` int,"
+ "\n_CLIENT_ID int, -- ABC\n_LOGIN_ID int, # DEF\n_WHERE varchar(2000),\n_SORT varchar(2000),"
+ "\n out _SQL varchar(/* inline right here - oh my gosh! */ 8000),\n _SONG_ID int,\n _NOTES varchar(2000),\n out _RESULT varchar(10)"
+ "\n /*\n , -- Generic result parameter"
+ "\n out _PERIOD_ID int, -- Returns the period_id. Useful when using @PREDEFLINK to return which is the last period"
+ "\n _SONGS_LIST varchar(8000),\n _COMPOSERID int,\n _PUBLISHERID int,"
+ "\n _PREDEFLINK int -- If the user is accessing through a predefined link: 0=none 1=last period\n */) BEGIN SELECT 1; END");
createProcedure("testBug26959_1", "(`/*id*/` /* before type 1 */ varchar(20),"
+ "/* after type 1 */ OUT result2 DECIMAL(/*size1*/10,/*size2*/2) /* p2 */)BEGIN SELECT action, result; END");
this.conn.prepareCall("{call testBug26959(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)}").close();
this.rs = this.conn.getMetaData().getProcedureColumns(this.conn.getCatalog(), null, "testBug26959", "%");
String[] parameterNames = new String[] { "_ACTION", "/*dumb-identifier-1*/", "#dumb-identifier-2", "--dumb-identifier-3", "_CLIENT_ID", "_LOGIN_ID",
"_WHERE", "_SORT", "_SQL", "_SONG_ID", "_NOTES", "_RESULT" };
int[] parameterTypes = new int[] { Types.VARCHAR, Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.VARCHAR,
Types.VARCHAR, Types.VARCHAR, Types.INTEGER, Types.VARCHAR, Types.VARCHAR };
int[] direction = new int[] { java.sql.DatabaseMetaData.procedureColumnIn, java.sql.DatabaseMetaData.procedureColumnIn,
java.sql.DatabaseMetaData.procedureColumnIn, java.sql.DatabaseMetaData.procedureColumnIn, java.sql.DatabaseMetaData.procedureColumnIn,
java.sql.DatabaseMetaData.procedureColumnIn, java.sql.DatabaseMetaData.procedureColumnIn, java.sql.DatabaseMetaData.procedureColumnIn,
java.sql.DatabaseMetaData.procedureColumnOut, java.sql.DatabaseMetaData.procedureColumnIn, java.sql.DatabaseMetaData.procedureColumnIn,
java.sql.DatabaseMetaData.procedureColumnOut };
int[] precision = new int[] { 20, 10, 10, 10, 10, 10, 2000, 2000, 8000, 10, 2000, 10 };
int index = 0;
while (this.rs.next()) {
assertEquals(parameterNames[index], this.rs.getString("COLUMN_NAME"));
assertEquals(parameterTypes[index], this.rs.getInt("DATA_TYPE"));
switch (index) {
case 0:
case 6:
case 7:
case 8:
case 10:
case 11:
assertEquals(precision[index], this.rs.getInt("LENGTH"));
break;
default:
assertEquals(precision[index], this.rs.getInt("PRECISION"));
}
assertEquals(direction[index], this.rs.getInt("COLUMN_TYPE"));
index++;
}
this.rs.close();
index = 0;
parameterNames = new String[] { "/*id*/", "result2" };
parameterTypes = new int[] { Types.VARCHAR, Types.DECIMAL };
precision = new int[] { 20, 10 };
direction = new int[] { java.sql.DatabaseMetaData.procedureColumnIn, java.sql.DatabaseMetaData.procedureColumnOut };
int[] scale = new int[] { 0, 2 };
this.conn.prepareCall("{call testBug26959_1(?, ?)}").close();
this.rs = this.conn.getMetaData().getProcedureColumns(this.conn.getCatalog(), null, "testBug26959_1", "%");
while (this.rs.next()) {
assertEquals(parameterNames[index], this.rs.getString("COLUMN_NAME"));
assertEquals(parameterTypes[index], this.rs.getInt("DATA_TYPE"));
switch (index) {
case 0:
case 6:
case 7:
case 8:
case 10:
case 11:
assertEquals(precision[index], this.rs.getInt("LENGTH"));
break;
default:
assertEquals(precision[index], this.rs.getInt("PRECISION"));
}
assertEquals(scale[index], this.rs.getInt("SCALE"));
assertEquals(direction[index], this.rs.getInt("COLUMN_TYPE"));
index++;
}
}
/**
* Tests fix for BUG#27400 - CALL [comment] some_proc() doesn't work
*/
public void testBug27400() throws Exception {
if (!serverSupportsStoredProcedures()) {
return; // SPs not supported
}
createProcedure("testBug27400", "(a INT, b VARCHAR(32)) BEGIN SELECT 1; END");
CallableStatement cStmt = null;
try {
cStmt = this.conn.prepareCall("{CALL /* SOME COMMENT */ testBug27400( /* does this work too? */ ?, ?)} # and a commented ? here too");
assertTrue(cStmt.toString().indexOf("/*") != -1); // we don't want
// to strip the
// comments
cStmt.setInt(1, 1);
cStmt.setString(2, "bleh");
cStmt.execute();
} finally {
if (cStmt != null) {
cStmt.close();
}
}
}
/**
* Tests fix for BUG#28689 - CallableStatement.executeBatch() doesn't work
* when connection property "noAccessToProcedureBodies" has been set to
* "true".
*
* The fix involves changing the behavior of "noAccessToProcedureBodies", in
* that the driver will now report all paramters as "IN" paramters but allow
* callers to call registerOutParameter() on them.
*
* @throws Exception
*/
public void testBug28689() throws Exception {
if (!versionMeetsMinimum(5, 0)) {
return; // no stored procedures
}
createTable("testBug28689", "(" +
"`id` int(11) NOT NULL auto_increment,`usuario` varchar(255) default NULL,PRIMARY KEY (`id`))");
this.stmt.executeUpdate("INSERT INTO testBug28689 (usuario) VALUES ('AAAAAA')");
createProcedure("sp_testBug28689", "(tid INT)\nBEGIN\nUPDATE testBug28689 SET usuario = 'BBBBBB' WHERE id = tid;\nEND");
Connection noProcedureBodiesConn = getConnectionWithProps("noAccessToProcedureBodies=true");
CallableStatement cStmt = null;
try {
cStmt = noProcedureBodiesConn.prepareCall("{CALL sp_testBug28689(?)}");
cStmt.setInt(1, 1);
cStmt.addBatch();
cStmt.executeBatch();
assertEquals("BBBBBB", getSingleIndexedValueWithQuery(noProcedureBodiesConn, 1, "SELECT `usuario` FROM testBug28689 WHERE id=1"));
} finally {
if (cStmt != null) {
cStmt.close();
}
if (noProcedureBodiesConn != null) {
noProcedureBodiesConn.close();
}
}
}
/**
* Tests fix for Bug#31823 - CallableStatement.setNull() on a stored
* function would throw an ArrayIndexOutOfBounds when setting the last
* parameter to null when calling setNull().
*
* @throws Exception
*/
public void testBug31823() throws Exception {
if (!versionMeetsMinimum(5, 0)) {
return; // no stored functions
}
createTable("testBug31823", "(value_1 BIGINT PRIMARY KEY,value_2 VARCHAR(20))");
createFunction("f_testBug31823", "(value_1_v BIGINT,value_2_v VARCHAR(20)) RETURNS BIGINT "
+ "DETERMINISTIC MODIFIES SQL DATA BEGIN INSERT INTO testBug31823 VALUES (value_1_v,value_2_v); RETURN value_1_v; END;");
// Prepare the function call
CallableStatement callable = null;
try {
callable = this.conn.prepareCall("{? = call f_testBug31823(?,?)}");
callable.registerOutParameter(1, Types.BIGINT);
// Add row with non-null value
callable.setLong(2, 1);
callable.setString(3, "Non-null value");
callable.executeUpdate();
assertEquals(1, callable.getLong(1));
// Add row with null value
callable.setLong(2, 2);
callable.setNull(3, Types.VARCHAR);
callable.executeUpdate();
assertEquals(2, callable.getLong(1));
Method[] setters = CallableStatement.class.getMethods();
for (int i = 0; i < setters.length; i++) {
if (setters[i].getName().startsWith("set")) {
Class<?>[] args = setters[i].getParameterTypes();
if (args.length == 2 && args[0].equals(Integer.TYPE)) {
if (!args[1].isPrimitive()) {
try {
setters[i].invoke(callable, new Object[] { new Integer(2), null });
} catch (InvocationTargetException ive) {
if (!(ive.getCause() instanceof com.mysql.jdbc.NotImplemented
|| ive.getCause().getClass().getName().equals("java.sql.SQLFeatureNotSupportedException"))) {
throw ive;
}
}
} else {
if (args[1].getName().equals("boolean")) {
try {
setters[i].invoke(callable, new Object[] { new Integer(2), Boolean.FALSE });
} catch (InvocationTargetException ive) {
if (!(ive.getCause() instanceof com.mysql.jdbc.NotImplemented
|| ive.getCause().getClass().getName().equals("java.sql.SQLFeatureNotSupportedException"))) {
throw ive;
}
}
}
if (args[1].getName().equals("byte")) {
try {
setters[i].invoke(callable, new Object[] { new Integer(2), new Byte((byte) 0) });
} catch (InvocationTargetException ive) {
if (!(ive.getCause() instanceof com.mysql.jdbc.NotImplemented
|| ive.getCause().getClass().getName().equals("java.sql.SQLFeatureNotSupportedException"))) {
throw ive;
}
}
}
if (args[1].getName().equals("double")) {
try {
setters[i].invoke(callable, new Object[] { new Integer(2), new Double(0) });
} catch (InvocationTargetException ive) {
if (!(ive.getCause() instanceof com.mysql.jdbc.NotImplemented
|| ive.getCause().getClass().getName().equals("java.sql.SQLFeatureNotSupportedException"))) {
throw ive;
}
}
}
if (args[1].getName().equals("float")) {
try {
setters[i].invoke(callable, new Object[] { new Integer(2), new Float(0) });
} catch (InvocationTargetException ive) {
if (!(ive.getCause() instanceof com.mysql.jdbc.NotImplemented
|| ive.getCause().getClass().getName().equals("java.sql.SQLFeatureNotSupportedException"))) {
throw ive;
}
}
}
if (args[1].getName().equals("int")) {
try {
setters[i].invoke(callable, new Object[] { new Integer(2), new Integer(0) });
} catch (InvocationTargetException ive) {
if (!(ive.getCause() instanceof com.mysql.jdbc.NotImplemented
|| ive.getCause().getClass().getName().equals("java.sql.SQLFeatureNotSupportedException"))) {
throw ive;
}
}
}
if (args[1].getName().equals("long")) {
try {
setters[i].invoke(callable, new Object[] { new Integer(2), new Long(0) });
} catch (InvocationTargetException ive) {
if (!(ive.getCause() instanceof com.mysql.jdbc.NotImplemented
|| ive.getCause().getClass().getName().equals("java.sql.SQLFeatureNotSupportedException"))) {
throw ive;
}
}
}
if (args[1].getName().equals("short")) {
try {
setters[i].invoke(callable, new Object[] { new Integer(2), new Short((short) 0) });
} catch (InvocationTargetException ive) {
if (!(ive.getCause() instanceof com.mysql.jdbc.NotImplemented
|| ive.getCause().getClass().getName().equals("java.sql.SQLFeatureNotSupportedException"))) {
throw ive;
}
}
}
}
}
}
}
} finally {
if (callable != null) {
callable.close();
}
}
}
/**
* Tests fix for Bug#32246 - When unpacking rows directly, we don't hand off
* error message packets to the internal method which decodes them
* correctly, so no exception is rasied, and the driver than hangs trying to
* read rows that aren't there...
*
* @throws Exception
* if the test fails
*/
public void testBug32246() throws Exception {
if (!versionMeetsMinimum(5, 0)) {
return;
}
dropTable("test_table_2");
dropTable("test_table_1");
doBug32246(this.conn);
dropTable("test_table_2");
dropTable("test_table_1");
doBug32246(getConnectionWithProps("useDirectRowUnpack=false"));
}
private void doBug32246(Connection aConn) throws SQLException {
createTable("test_table_1", "(value_1 BIGINT PRIMARY KEY) ENGINE=InnoDB");
this.stmt.executeUpdate("INSERT INTO test_table_1 VALUES (1)");
createTable("test_table_2", "(value_2 BIGINT PRIMARY KEY) ENGINE=InnoDB");
this.stmt.executeUpdate("DROP FUNCTION IF EXISTS test_function");
createFunction("test_function", "() RETURNS BIGINT DETERMINISTIC MODIFIES SQL DATA BEGIN DECLARE max_value BIGINT; "
+ "SELECT MAX(value_1) INTO max_value FROM test_table_2; RETURN max_value; END;");
CallableStatement callable = null;
try {
callable = aConn.prepareCall("{? = call test_function()}");
callable.registerOutParameter(1, Types.BIGINT);
try {
callable.executeUpdate();
fail("impossible; we should never get here.");
} catch (SQLException sqlEx) {
assertEquals("42S22", sqlEx.getSQLState());
}
createTable("test_table_1", "(value_1 BIGINT PRIMARY KEY) ENGINE=InnoDB");
this.stmt.executeUpdate("INSERT INTO test_table_1 VALUES (1)");
createTable("test_table_2",
"(value_2 BIGINT PRIMARY KEY, " + " FOREIGN KEY (value_2) REFERENCES test_table_1 (value_1) ON DELETE CASCADE) ENGINE=InnoDB");
createFunction("test_function",
"(value BIGINT) RETURNS BIGINT DETERMINISTIC MODIFIES SQL DATA BEGIN " + "INSERT INTO test_table_2 VALUES (value); RETURN value; END;");
callable = aConn.prepareCall("{? = call test_function(?)}");
callable.registerOutParameter(1, Types.BIGINT);
callable.setLong(2, 1);
callable.executeUpdate();
callable.setLong(2, 2);
try {
callable.executeUpdate();
fail("impossible; we should never get here.");
} catch (SQLException sqlEx) {
assertEquals("23000", sqlEx.getSQLState());
}
} finally {
if (callable != null) {
callable.close();
}
}
}
public void testBitSp() throws Exception {
if (!versionMeetsMinimum(5, 0)) {
return;
}
createTable("`Bit_Tab`", "( `MAX_VAL` tinyint(1) default NULL, `MIN_VAL` tinyint(1) default NULL, `NULL_VAL` tinyint(1) default NULL)");
createProcedure("Bit_Proc", "(out MAX_PARAM TINYINT, out MIN_PARAM TINYINT, out NULL_PARAM TINYINT)"
+ "begin select MAX_VAL, MIN_VAL, NULL_VAL into MAX_PARAM, MIN_PARAM, NULL_PARAM from Bit_Tab; end");
Boolean minBooleanVal;
Boolean oRetVal;
String Min_Val_Query = "SELECT MIN_VAL from Bit_Tab";
//String sMaxBooleanVal = "1";
// sMaxBooleanVal = "true";
//Boolean bool = Boolean.valueOf("true");
String Min_Insert = "insert into Bit_Tab values(1,0,null)";
// System.out.println("Value to insert=" + extractVal(Min_Insert,1));
CallableStatement cstmt;
this.stmt.executeUpdate("delete from Bit_Tab");
this.stmt.executeUpdate(Min_Insert);
cstmt = this.conn.prepareCall("{call Bit_Proc(?,?,?)}");
System.out.println("register the output parameters");
cstmt.registerOutParameter(1, java.sql.Types.BIT);
cstmt.registerOutParameter(2, java.sql.Types.BIT);
cstmt.registerOutParameter(3, java.sql.Types.BIT);
System.out.println("execute the procedure");
cstmt.executeUpdate();
System.out.println("invoke getBoolean method");
boolean bRetVal = cstmt.getBoolean(2);
oRetVal = new Boolean(bRetVal);
minBooleanVal = new Boolean("false");
this.rs = this.stmt.executeQuery(Min_Val_Query);
if (oRetVal.equals(minBooleanVal)) {
System.out.println("getBoolean returns the Minimum value ");
} else {
System.out.println("getBoolean() did not return the Minimum value, getBoolean Failed!");
}
}
public void testNotReallyCallableStatement() throws Exception {
if (!versionMeetsMinimum(5, 0)) {
return;
}
CallableStatement cstmt = null;
try {
this.stmt.executeUpdate("DROP TABLE IF EXISTS testNotReallyCallableStatement");
cstmt = this.conn.prepareCall("CREATE TABLE testNotReallyCallableStatement(field1 INT)");
} finally {
this.stmt.executeUpdate("DROP TABLE IF EXISTS testNotReallyCallableStatement");
if (cstmt != null) {
cstmt.close();
}
}
}
public void testBug35199() throws Exception {
if (!versionMeetsMinimum(5, 0)) {
return;
}
createFunction("test_function", "(a varchar(40), b bigint(20), c varchar(80)) RETURNS bigint(20) LANGUAGE SQL DETERMINISTIC "
+ "MODIFIES SQL DATA COMMENT 'bbb' BEGIN RETURN 1; END; ");
CallableStatement callable = null;
try {
callable = this.conn.prepareCall("{? = call test_function(?,101,?)}");
callable.registerOutParameter(1, Types.BIGINT);
callable.setString(2, "FOO");
callable.setString(3, "BAR");
callable.executeUpdate();
} finally {
if (callable != null) {
callable.close();
}
}
}
public void testBug49831() throws Exception {
if (!serverSupportsStoredProcedures()) {
return;
}
createTable("testBug49831", "(val varchar(32))");
createProcedure("pTestBug49831", "(testval varchar(32)) BEGIN insert into testBug49831 (val) values (testval);END;");
execProcBug49831(this.conn);
this.stmt.execute("TRUNCATE TABLE testBug49831");
assertEquals(0, getRowCount("testBug49831"));
Connection noBodiesConn = getConnectionWithProps("noAccessToProcedureBodies=true,jdbcCompliantTruncation=false,characterEncoding=utf8,useUnicode=yes");
try {
execProcBug49831(noBodiesConn);
} finally {
noBodiesConn.close();
}
}
private void execProcBug49831(Connection c) throws Exception {
CallableStatement cstmt = c.prepareCall("{call pTestBug49831(?)}");
cstmt.setObject(1, "abc", Types.VARCHAR, 32);
cstmt.addBatch();
cstmt.setObject(1, "def", Types.VARCHAR, 32);
cstmt.addBatch();
cstmt.executeBatch();
assertEquals(2, getRowCount("testBug49831"));
this.rs = this.stmt.executeQuery("SELECT * from testBug49831 ORDER BY VAL ASC");
this.rs.next();
assertEquals("abc", this.rs.getString(1));
this.rs.next();
assertEquals("def", this.rs.getString(1));
}
public void testBug43576() throws Exception {
createTable("TMIX91P",
"(F01SMALLINT SMALLINT NOT NULL, F02INTEGER INTEGER,F03REAL REAL,"
+ "F04FLOAT FLOAT,F05NUMERIC31X4 NUMERIC(31,4), F06NUMERIC16X16 NUMERIC(16,16), F07CHAR_10 CHAR(10),"
+ " F08VARCHAR_10 VARCHAR(10), F09CHAR_20 CHAR(20), F10VARCHAR_20 VARCHAR(20), F11DATE DATE,"
+ " F12DATETIME DATETIME, PRIMARY KEY (F01SMALLINT))");
this.stmt.executeUpdate("INSERT INTO TMIX91P VALUES (1,1,1234567.12,1234567.12,111111111111111111111111111.1111,.111111111111111,'1234567890',"
+ "'1234567890','CHAR20CHAR20','VARCHAR20ABCD','2001-01-01','2001-01-01 01:01:01.111')");
this.stmt.executeUpdate("INSERT INTO TMIX91P VALUES (7,1,1234567.12,1234567.12,22222222222.0001,.99999999999,'1234567896','1234567896','CHAR20',"
+ "'VARCHAR20ABCD','2001-01-01','2001-01-01 01:01:01.111')");
this.stmt.executeUpdate("INSERT INTO TMIX91P VALUES (12,12,1234567.12,1234567.12,111222333.4444,.1234567890,'2234567891','2234567891','CHAR20',"
+ "'VARCHAR20VARCHAR20','2001-01-01','2001-01-01 01:01:01.111')");
createProcedure("MSQSPR100",
"\n( p1_in INTEGER , p2_in CHAR(20), OUT p3_out INTEGER, OUT p4_out CHAR(11))\nBEGIN "
+ "\n SELECT F01SMALLINT,F02INTEGER, F11DATE,F12DATETIME,F03REAL \n FROM TMIX91P WHERE F02INTEGER = p1_in; "
+ "\n SELECT F02INTEGER,F07CHAR_10,F08VARCHAR_10,F09CHAR_20 \n FROM TMIX91P WHERE F09CHAR_20 = p2_in ORDER BY F02INTEGER ; "
+ "\n SET p3_out = 144; \n SET p4_out = 'CHARACTER11'; \n SELECT p3_out, p4_out; END");
String sql = "{call MSQSPR100(1,'CHAR20',?,?)}";
CallableStatement cs = this.conn.prepareCall(sql);
cs.registerOutParameter(1, Types.INTEGER);
cs.registerOutParameter(2, Types.CHAR);
cs.execute();
cs.close();
createProcedure("bug43576_1", "(OUT nfact VARCHAR(100), IN ccuenta VARCHAR(100),\nOUT ffact VARCHAR(100),\nOUT fdoc VARCHAR(100))\nBEGIN"
+ "\nSET nfact = 'ncfact string';\nSET ffact = 'ffact string';\nSET fdoc = 'fdoc string';\nEND");
createProcedure("bug43576_2", "(IN ccuent1 VARCHAR(100), IN ccuent2 VARCHAR(100),\nOUT nfact VARCHAR(100),\nOUT ffact VARCHAR(100),"
+ "\nOUT fdoc VARCHAR(100))\nBEGIN\nSET nfact = 'ncfact string';\nSET ffact = 'ffact string';\nSET fdoc = 'fdoc string';\nEND");
Properties props = new Properties();
props.put("jdbcCompliantTruncation", "true");
props.put("useInformationSchema", "true");
Connection conn1 = null;
conn1 = getConnectionWithProps(props);
try {
CallableStatement callSt = conn1.prepareCall("{ call bug43576_1(?, ?, ?, ?) }");
callSt.setString(2, "xxx");
callSt.registerOutParameter(1, java.sql.Types.VARCHAR);
callSt.registerOutParameter(3, java.sql.Types.VARCHAR);
callSt.registerOutParameter(4, java.sql.Types.VARCHAR);
callSt.execute();
assertEquals("ncfact string", callSt.getString(1));
assertEquals("ffact string", callSt.getString(3));
assertEquals("fdoc string", callSt.getString(4));
CallableStatement callSt2 = conn1.prepareCall("{ call bug43576_2(?, ?, ?, ?, ?) }");
callSt2.setString(1, "xxx");
callSt2.setString(2, "yyy");
callSt2.registerOutParameter(3, java.sql.Types.VARCHAR);
callSt2.registerOutParameter(4, java.sql.Types.VARCHAR);
callSt2.registerOutParameter(5, java.sql.Types.VARCHAR);
callSt2.execute();
assertEquals("ncfact string", callSt2.getString(3));
assertEquals("ffact string", callSt2.getString(4));
assertEquals("fdoc string", callSt2.getString(5));
CallableStatement callSt3 = conn1.prepareCall("{ call bug43576_2(?, 'yyy', ?, ?, ?) }");
callSt3.setString(1, "xxx");
// callSt3.setString(2, "yyy");
callSt3.registerOutParameter(2, java.sql.Types.VARCHAR);
callSt3.registerOutParameter(3, java.sql.Types.VARCHAR);
callSt3.registerOutParameter(4, java.sql.Types.VARCHAR);
callSt3.execute();
assertEquals("ncfact string", callSt3.getString(2));
assertEquals("ffact string", callSt3.getString(3));
assertEquals("fdoc string", callSt3.getString(4));
} finally {
conn1.close();
}
}
/**
* Tests fix for Bug#57022 - cannot execute a store procedure with output
* parameters Problem was in CallableStatement.java, private void
* determineParameterTypes() throws SQLException if (procName.indexOf(".")
* == -1) { useCatalog = true; } The fix will be to "sanitize" db.sp call
* just like in noAccessToProcedureBodies.
*
* @throws Exception
* if the test fails
*/
public void testBug57022() throws Exception {
if (!serverSupportsStoredProcedures()) {
return;
}
String originalCatalog = this.conn.getCatalog();
createDatabase("bug57022");
createProcedure("bug57022.procbug57022", "(x int, out y int)\nbegin\ndeclare z int;\nset z = x+1, y = z;\nend\n");
CallableStatement cStmt = null;
try {
cStmt = this.conn.prepareCall("{call `bug57022`.`procbug57022`(?, ?)}");
cStmt.setInt(1, 5);
cStmt.registerOutParameter(2, Types.INTEGER);
cStmt.execute();
assertEquals(6, cStmt.getInt(2));
cStmt.clearParameters();
cStmt.close();
this.conn.setCatalog("bug57022");
cStmt = this.conn.prepareCall("{call bug57022.procbug57022(?, ?)}");
cStmt.setInt(1, 5);
cStmt.registerOutParameter(2, Types.INTEGER);
cStmt.execute();
assertEquals(6, cStmt.getInt(2));
cStmt.clearParameters();
cStmt.close();
this.conn.setCatalog("mysql");
cStmt = this.conn.prepareCall("{call `bug57022`.`procbug57022`(?, ?)}");
cStmt.setInt(1, 5);
cStmt.registerOutParameter(2, Types.INTEGER);
cStmt.execute();
assertEquals(6, cStmt.getInt(2));
} finally {
if (cStmt != null) {
cStmt.clearParameters();
cStmt.close();
}
this.conn.setCatalog(originalCatalog);
}
}
/**
* Tests fix for BUG#60816 - Cannot pass NULL to an INOUT procedure parameter
*
* @throws Exception
*/
public void testBug60816() throws Exception {
createProcedure("test60816_1", "(INOUT x INTEGER)\nBEGIN\nSET x = x + 1;\nEND");
createProcedure("test60816_2", "(x INTEGER, OUT y INTEGER)\nBEGIN\nSET y = x + 1;\nEND");
createProcedure("test60816_3", "(INOUT x INTEGER)\nBEGIN\nSET x = 10;\nEND");
CallableStatement call = this.conn.prepareCall("{ call test60816_1(?) }");
call.setInt(1, 1);
call.registerOutParameter(1, Types.INTEGER);
call.execute();
assertEquals(2, call.getInt(1));
call = this.conn.prepareCall("{ call test60816_2(?, ?) }");
call.setInt(1, 1);
call.registerOutParameter(2, Types.INTEGER);
call.execute();
assertEquals(2, call.getInt(2));
call = this.conn.prepareCall("{ call test60816_2(?, ?) }");
call.setNull(1, Types.INTEGER);
call.registerOutParameter(2, Types.INTEGER);
call.execute();
assertEquals(0, call.getInt(2));
assertTrue(call.wasNull());
call = this.conn.prepareCall("{ call test60816_1(?) }");
call.setNull(1, Types.INTEGER);
call.registerOutParameter(1, Types.INTEGER);
call.execute();
assertEquals(0, call.getInt(1));
assertTrue(call.wasNull());
call = this.conn.prepareCall("{ call test60816_3(?) }");
call.setNull(1, Types.INTEGER);
call.registerOutParameter(1, Types.INTEGER);
call.execute();
assertEquals(10, call.getInt(1));
}
}