UpdatableResultSet.java
91.7 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
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
/*
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 com.mysql.jdbc;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import com.mysql.jdbc.profiler.ProfilerEvent;
/**
* A result set that is updatable.
*/
public class UpdatableResultSet extends ResultSetImpl {
/** Marker for 'stream' data when doing INSERT rows */
final static byte[] STREAM_DATA_MARKER = StringUtils.getBytes("** STREAM DATA **");
protected SingleByteCharsetConverter charConverter;
private String charEncoding;
/** What is the default value for the column? */
private byte[][] defaultColumnValue;
/** PreparedStatement used to delete data */
private com.mysql.jdbc.PreparedStatement deleter = null;
private String deleteSQL = null;
private boolean initializedCharConverter = false;
/** PreparedStatement used to insert data */
protected com.mysql.jdbc.PreparedStatement inserter = null;
private String insertSQL = null;
/** Is this result set updateable? */
private boolean isUpdatable = false;
/** Reason the result set is not updatable */
private String notUpdatableReason = null;
/** List of primary keys */
private List<Integer> primaryKeyIndicies = null;
private String qualifiedAndQuotedTableName;
private String quotedIdChar = null;
/** PreparedStatement used to refresh data */
private com.mysql.jdbc.PreparedStatement refresher;
private String refreshSQL = null;
/** The binary data for the 'current' row */
private ResultSetRow savedCurrentRow;
/** PreparedStatement used to delete data */
protected com.mysql.jdbc.PreparedStatement updater = null;
/** SQL for in-place modifcation */
private String updateSQL = null;
private boolean populateInserterWithDefaultValues = false;
private Map<String, Map<String, Map<String, Integer>>> databasesUsedToTablesUsed = null;
/**
* Creates a new ResultSet object.
*
* @param catalog
* the database in use when we were created
* @param fields
* an array of Field objects (basically, the ResultSet MetaData)
* @param tuples
* actual row data
* @param conn
* the Connection that created us.
* @param creatorStmt
*
* @throws SQLException
*/
protected UpdatableResultSet(String catalog, Field[] fields, RowData tuples, MySQLConnection conn, StatementImpl creatorStmt) throws SQLException {
super(catalog, fields, tuples, conn, creatorStmt);
checkUpdatability();
this.populateInserterWithDefaultValues = this.connection.getPopulateInsertRowWithDefaultValues();
}
/**
* JDBC 2.0
*
* <p>
* Move to an absolute row number in the result set.
* </p>
*
* <p>
* If row is positive, moves to an absolute row with respect to the beginning of the result set. The first row is row 1, the second is row 2, etc.
* </p>
*
* <p>
* If row is negative, moves to an absolute row position with respect to the end of result set. For example, calling absolute(-1) positions the cursor on
* the last row, absolute(-2) indicates the next-to-last row, etc.
* </p>
*
* <p>
* An attempt to position the cursor beyond the first/last row in the result set, leaves the cursor before/after the first/last row, respectively.
* </p>
*
* <p>
* Note: Calling absolute(1) is the same as calling first(). Calling absolute(-1) is the same as calling last().
* </p>
*
* @param row
*
* @return true if on the result set, false if off.
*
* @exception SQLException
* if a database-access error occurs, or row is 0, or result
* set type is TYPE_FORWARD_ONLY.
*/
@Override
public synchronized boolean absolute(int row) throws SQLException {
return super.absolute(row);
}
/**
* JDBC 2.0
*
* <p>
* Moves to the end of the result set, just after the last row. Has no effect if the result set contains no rows.
* </p>
*
* @exception SQLException
* if a database-access error occurs, or result set type is
* TYPE_FORWARD_ONLY.
*/
@Override
public synchronized void afterLast() throws SQLException {
super.afterLast();
}
/**
* JDBC 2.0
*
* <p>
* Moves to the front of the result set, just before the first row. Has no effect if the result set contains no rows.
* </p>
*
* @exception SQLException
* if a database-access error occurs, or result set type is
* TYPE_FORWARD_ONLY
*/
@Override
public synchronized void beforeFirst() throws SQLException {
super.beforeFirst();
}
/**
* JDBC 2.0 The cancelRowUpdates() method may be called after calling an
* updateXXX() method(s) and before calling updateRow() to rollback the
* updates made to a row. If no updates have been made or updateRow() has
* already been called, then this method has no effect.
*
* @exception SQLException
* if a database-access error occurs, or if called when on
* the insert row.
*/
@Override
public synchronized void cancelRowUpdates() throws SQLException {
checkClosed();
if (this.doingUpdates) {
this.doingUpdates = false;
this.updater.clearParameters();
}
}
/*
* (non-Javadoc)
*
* @see com.mysql.jdbc.ResultSet#checkRowPos()
*/
@Override
protected synchronized void checkRowPos() throws SQLException {
checkClosed();
if (!this.onInsertRow) {
super.checkRowPos();
}
}
/**
* Is this ResultSet updateable?
*
* @throws SQLException
*/
protected void checkUpdatability() throws SQLException {
try {
if (this.fields == null) {
// we've been created to be populated with cached metadata, and we don't have the metadata yet, we'll be called again by
// Connection.initializeResultsMetadataFromCache() when the metadata has been made available
return;
}
String singleTableName = null;
String catalogName = null;
int primaryKeyCount = 0;
// We can only do this if we know that there is a currently selected database, or if we're talking to a > 4.1 version of MySQL server (as it returns
// database names in field info)
if ((this.catalog == null) || (this.catalog.length() == 0)) {
this.catalog = this.fields[0].getDatabaseName();
if ((this.catalog == null) || (this.catalog.length() == 0)) {
throw SQLError.createSQLException(Messages.getString("UpdatableResultSet.43"), SQLError.SQL_STATE_ILLEGAL_ARGUMENT,
getExceptionInterceptor());
}
}
if (this.fields.length > 0) {
singleTableName = this.fields[0].getOriginalTableName();
catalogName = this.fields[0].getDatabaseName();
if (singleTableName == null) {
singleTableName = this.fields[0].getTableName();
catalogName = this.catalog;
}
if (singleTableName != null && singleTableName.length() == 0) {
this.isUpdatable = false;
this.notUpdatableReason = Messages.getString("NotUpdatableReason.3");
return;
}
if (this.fields[0].isPrimaryKey()) {
primaryKeyCount++;
}
//
// References only one table?
//
for (int i = 1; i < this.fields.length; i++) {
String otherTableName = this.fields[i].getOriginalTableName();
String otherCatalogName = this.fields[i].getDatabaseName();
if (otherTableName == null) {
otherTableName = this.fields[i].getTableName();
otherCatalogName = this.catalog;
}
if (otherTableName != null && otherTableName.length() == 0) {
this.isUpdatable = false;
this.notUpdatableReason = Messages.getString("NotUpdatableReason.3");
return;
}
if ((singleTableName == null) || !otherTableName.equals(singleTableName)) {
this.isUpdatable = false;
this.notUpdatableReason = Messages.getString("NotUpdatableReason.0");
return;
}
// Can't reference more than one database
if ((catalogName == null) || !otherCatalogName.equals(catalogName)) {
this.isUpdatable = false;
this.notUpdatableReason = Messages.getString("NotUpdatableReason.1");
return;
}
if (this.fields[i].isPrimaryKey()) {
primaryKeyCount++;
}
}
if ((singleTableName == null) || (singleTableName.length() == 0)) {
this.isUpdatable = false;
this.notUpdatableReason = Messages.getString("NotUpdatableReason.2");
return;
}
} else {
this.isUpdatable = false;
this.notUpdatableReason = Messages.getString("NotUpdatableReason.3");
return;
}
if (this.connection.getStrictUpdates()) {
java.sql.DatabaseMetaData dbmd = this.connection.getMetaData();
java.sql.ResultSet rs = null;
HashMap<String, String> primaryKeyNames = new HashMap<String, String>();
try {
rs = dbmd.getPrimaryKeys(catalogName, null, singleTableName);
while (rs.next()) {
String keyName = rs.getString(4);
keyName = keyName.toUpperCase();
primaryKeyNames.put(keyName, keyName);
}
} finally {
if (rs != null) {
try {
rs.close();
} catch (Exception ex) {
AssertionFailedException.shouldNotHappen(ex);
}
rs = null;
}
}
int existingPrimaryKeysCount = primaryKeyNames.size();
if (existingPrimaryKeysCount == 0) {
this.isUpdatable = false;
this.notUpdatableReason = Messages.getString("NotUpdatableReason.5");
return; // we can't update tables w/o keys
}
//
// Contains all primary keys?
//
for (int i = 0; i < this.fields.length; i++) {
if (this.fields[i].isPrimaryKey()) {
String columnNameUC = this.fields[i].getName().toUpperCase();
if (primaryKeyNames.remove(columnNameUC) == null) {
// try original name
String originalName = this.fields[i].getOriginalName();
if (originalName != null) {
if (primaryKeyNames.remove(originalName.toUpperCase()) == null) {
// we don't know about this key, so give up :(
this.isUpdatable = false;
this.notUpdatableReason = Messages.getString("NotUpdatableReason.6", new Object[] { originalName });
return;
}
}
}
}
}
this.isUpdatable = primaryKeyNames.isEmpty();
if (!this.isUpdatable) {
if (existingPrimaryKeysCount > 1) {
this.notUpdatableReason = Messages.getString("NotUpdatableReason.7");
} else {
this.notUpdatableReason = Messages.getString("NotUpdatableReason.4");
}
return;
}
}
//
// Must have at least one primary key
//
if (primaryKeyCount == 0) {
this.isUpdatable = false;
this.notUpdatableReason = Messages.getString("NotUpdatableReason.4");
return;
}
this.isUpdatable = true;
this.notUpdatableReason = null;
return;
} catch (SQLException sqlEx) {
this.isUpdatable = false;
this.notUpdatableReason = sqlEx.getMessage();
}
}
/**
* JDBC 2.0 Delete the current row from the result set and the underlying
* database. Cannot be called when on the insert row.
*
* @exception SQLException
* if a database-access error occurs, or if called when on
* the insert row.
* @throws SQLException
* if the ResultSet is not updatable or some other error occurs
*/
@Override
public synchronized void deleteRow() throws SQLException {
checkClosed();
if (!this.isUpdatable) {
throw new NotUpdatable(this.notUpdatableReason);
}
if (this.onInsertRow) {
throw SQLError.createSQLException(Messages.getString("UpdatableResultSet.1"), getExceptionInterceptor());
} else if (this.rowData.size() == 0) {
throw SQLError.createSQLException(Messages.getString("UpdatableResultSet.2"), getExceptionInterceptor());
} else if (isBeforeFirst()) {
throw SQLError.createSQLException(Messages.getString("UpdatableResultSet.3"), getExceptionInterceptor());
} else if (isAfterLast()) {
throw SQLError.createSQLException(Messages.getString("UpdatableResultSet.4"), getExceptionInterceptor());
}
if (this.deleter == null) {
if (this.deleteSQL == null) {
generateStatements();
}
this.deleter = (PreparedStatement) this.connection.clientPrepareStatement(this.deleteSQL);
}
this.deleter.clearParameters();
int numKeys = this.primaryKeyIndicies.size();
if (numKeys == 1) {
int index = this.primaryKeyIndicies.get(0).intValue();
this.setParamValue(this.deleter, 1, this.thisRow, index, this.fields[index].getSQLType());
} else {
for (int i = 0; i < numKeys; i++) {
int index = this.primaryKeyIndicies.get(i).intValue();
this.setParamValue(this.deleter, i + 1, this.thisRow, index, this.fields[index].getSQLType());
}
}
this.deleter.executeUpdate();
this.rowData.removeRow(this.rowData.getCurrentRowNumber());
// position on previous row - Bug#27431
previous();
}
private synchronized void setParamValue(PreparedStatement ps, int psIdx, ResultSetRow row, int rsIdx, int sqlType) throws SQLException {
byte[] val = row.getColumnValue(rsIdx);
if (val == null) {
ps.setNull(psIdx, Types.NULL);
return;
}
switch (sqlType) {
case Types.NULL:
ps.setNull(psIdx, Types.NULL);
break;
case Types.TINYINT:
case Types.SMALLINT:
case Types.INTEGER:
ps.setInt(psIdx, row.getInt(rsIdx));
break;
case Types.BIGINT:
ps.setLong(psIdx, row.getLong(rsIdx));
break;
case Types.CHAR:
case Types.VARCHAR:
case Types.LONGVARCHAR:
case Types.DECIMAL:
case Types.NUMERIC:
ps.setString(psIdx, row.getString(rsIdx, this.charEncoding, this.connection));
break;
case Types.DATE:
ps.setDate(psIdx, row.getDateFast(rsIdx, this.connection, this, this.fastDefaultCal), this.fastDefaultCal);
break;
case Types.TIMESTAMP:
ps.setTimestamp(psIdx, row.getTimestampFast(rsIdx, this.fastDefaultCal, this.connection.getDefaultTimeZone(), false, this.connection, this));
break;
case Types.TIME:
ps.setTime(psIdx, row.getTimeFast(rsIdx, this.fastDefaultCal, this.connection.getDefaultTimeZone(), false, this.connection, this));
break;
case Types.FLOAT:
case Types.DOUBLE:
case Types.REAL:
case Types.BOOLEAN:
ps.setBytesNoEscapeNoQuotes(psIdx, val);
break;
/*
* default, but also explicitly for following types:
* case Types.BINARY:
* case Types.BLOB:
*/
default:
ps.setBytes(psIdx, val);
break;
}
}
private synchronized void extractDefaultValues() throws SQLException {
java.sql.DatabaseMetaData dbmd = this.connection.getMetaData();
this.defaultColumnValue = new byte[this.fields.length][];
java.sql.ResultSet columnsResultSet = null;
for (Map.Entry<String, Map<String, Map<String, Integer>>> dbEntry : this.databasesUsedToTablesUsed.entrySet()) {
//String databaseName = dbEntry.getKey().toString();
for (Map.Entry<String, Map<String, Integer>> tableEntry : dbEntry.getValue().entrySet()) {
String tableName = tableEntry.getKey();
Map<String, Integer> columnNamesToIndices = tableEntry.getValue();
try {
columnsResultSet = dbmd.getColumns(this.catalog, null, tableName, "%");
while (columnsResultSet.next()) {
String columnName = columnsResultSet.getString("COLUMN_NAME");
byte[] defaultValue = columnsResultSet.getBytes("COLUMN_DEF");
if (columnNamesToIndices.containsKey(columnName)) {
int localColumnIndex = columnNamesToIndices.get(columnName).intValue();
this.defaultColumnValue[localColumnIndex] = defaultValue;
} // else assert?
}
} finally {
if (columnsResultSet != null) {
columnsResultSet.close();
columnsResultSet = null;
}
}
}
}
}
/**
* JDBC 2.0
*
* <p>
* Moves to the first row in the result set.
* </p>
*
* @return true if on a valid row, false if no rows in the result set.
*
* @exception SQLException
* if a database-access error occurs, or result set type is
* TYPE_FORWARD_ONLY.
*/
@Override
public synchronized boolean first() throws SQLException {
return super.first();
}
/**
* Figure out whether or not this ResultSet is updateable, and if so,
* generate the PreparedStatements to support updates.
*
* @throws SQLException
* @throws NotUpdatable
*/
protected synchronized void generateStatements() throws SQLException {
if (!this.isUpdatable) {
this.doingUpdates = false;
this.onInsertRow = false;
throw new NotUpdatable(this.notUpdatableReason);
}
String quotedId = getQuotedIdChar();
Map<String, String> tableNamesSoFar = null;
if (this.connection.lowerCaseTableNames()) {
tableNamesSoFar = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
this.databasesUsedToTablesUsed = new TreeMap<String, Map<String, Map<String, Integer>>>(String.CASE_INSENSITIVE_ORDER);
} else {
tableNamesSoFar = new TreeMap<String, String>();
this.databasesUsedToTablesUsed = new TreeMap<String, Map<String, Map<String, Integer>>>();
}
this.primaryKeyIndicies = new ArrayList<Integer>();
StringBuilder fieldValues = new StringBuilder();
StringBuilder keyValues = new StringBuilder();
StringBuilder columnNames = new StringBuilder();
StringBuilder insertPlaceHolders = new StringBuilder();
StringBuilder allTablesBuf = new StringBuilder();
Map<Integer, String> columnIndicesToTable = new HashMap<Integer, String>();
boolean firstTime = true;
boolean keysFirstTime = true;
String equalsStr = this.connection.versionMeetsMinimum(3, 23, 0) ? "<=>" : "=";
for (int i = 0; i < this.fields.length; i++) {
StringBuilder tableNameBuffer = new StringBuilder();
Map<String, Integer> updColumnNameToIndex = null;
// FIXME: What about no table?
if (this.fields[i].getOriginalTableName() != null) {
String databaseName = this.fields[i].getDatabaseName();
if ((databaseName != null) && (databaseName.length() > 0)) {
tableNameBuffer.append(quotedId);
tableNameBuffer.append(databaseName);
tableNameBuffer.append(quotedId);
tableNameBuffer.append('.');
}
String tableOnlyName = this.fields[i].getOriginalTableName();
tableNameBuffer.append(quotedId);
tableNameBuffer.append(tableOnlyName);
tableNameBuffer.append(quotedId);
String fqTableName = tableNameBuffer.toString();
if (!tableNamesSoFar.containsKey(fqTableName)) {
if (!tableNamesSoFar.isEmpty()) {
allTablesBuf.append(',');
}
allTablesBuf.append(fqTableName);
tableNamesSoFar.put(fqTableName, fqTableName);
}
columnIndicesToTable.put(Integer.valueOf(i), fqTableName);
updColumnNameToIndex = getColumnsToIndexMapForTableAndDB(databaseName, tableOnlyName);
} else {
String tableOnlyName = this.fields[i].getTableName();
if (tableOnlyName != null) {
tableNameBuffer.append(quotedId);
tableNameBuffer.append(tableOnlyName);
tableNameBuffer.append(quotedId);
String fqTableName = tableNameBuffer.toString();
if (!tableNamesSoFar.containsKey(fqTableName)) {
if (!tableNamesSoFar.isEmpty()) {
allTablesBuf.append(',');
}
allTablesBuf.append(fqTableName);
tableNamesSoFar.put(fqTableName, fqTableName);
}
columnIndicesToTable.put(Integer.valueOf(i), fqTableName);
updColumnNameToIndex = getColumnsToIndexMapForTableAndDB(this.catalog, tableOnlyName);
}
}
String originalColumnName = this.fields[i].getOriginalName();
String columnName = null;
if (this.connection.getIO().hasLongColumnInfo() && (originalColumnName != null) && (originalColumnName.length() > 0)) {
columnName = originalColumnName;
} else {
columnName = this.fields[i].getName();
}
if (updColumnNameToIndex != null && columnName != null) {
updColumnNameToIndex.put(columnName, Integer.valueOf(i));
}
String originalTableName = this.fields[i].getOriginalTableName();
String tableName = null;
if (this.connection.getIO().hasLongColumnInfo() && (originalTableName != null) && (originalTableName.length() > 0)) {
tableName = originalTableName;
} else {
tableName = this.fields[i].getTableName();
}
StringBuilder fqcnBuf = new StringBuilder();
String databaseName = this.fields[i].getDatabaseName();
if (databaseName != null && databaseName.length() > 0) {
fqcnBuf.append(quotedId);
fqcnBuf.append(databaseName);
fqcnBuf.append(quotedId);
fqcnBuf.append('.');
}
fqcnBuf.append(quotedId);
fqcnBuf.append(tableName);
fqcnBuf.append(quotedId);
fqcnBuf.append('.');
fqcnBuf.append(quotedId);
fqcnBuf.append(columnName);
fqcnBuf.append(quotedId);
String qualifiedColumnName = fqcnBuf.toString();
if (this.fields[i].isPrimaryKey()) {
this.primaryKeyIndicies.add(Integer.valueOf(i));
if (!keysFirstTime) {
keyValues.append(" AND ");
} else {
keysFirstTime = false;
}
keyValues.append(qualifiedColumnName);
keyValues.append(equalsStr);
keyValues.append("?");
}
if (firstTime) {
firstTime = false;
fieldValues.append("SET ");
} else {
fieldValues.append(",");
columnNames.append(",");
insertPlaceHolders.append(",");
}
insertPlaceHolders.append("?");
columnNames.append(qualifiedColumnName);
fieldValues.append(qualifiedColumnName);
fieldValues.append("=?");
}
this.qualifiedAndQuotedTableName = allTablesBuf.toString();
this.updateSQL = "UPDATE " + this.qualifiedAndQuotedTableName + " " + fieldValues.toString() + " WHERE " + keyValues.toString();
this.insertSQL = "INSERT INTO " + this.qualifiedAndQuotedTableName + " (" + columnNames.toString() + ") VALUES (" + insertPlaceHolders.toString() + ")";
this.refreshSQL = "SELECT " + columnNames.toString() + " FROM " + this.qualifiedAndQuotedTableName + " WHERE " + keyValues.toString();
this.deleteSQL = "DELETE FROM " + this.qualifiedAndQuotedTableName + " WHERE " + keyValues.toString();
}
private Map<String, Integer> getColumnsToIndexMapForTableAndDB(String databaseName, String tableName) {
Map<String, Integer> nameToIndex;
Map<String, Map<String, Integer>> tablesUsedToColumnsMap = this.databasesUsedToTablesUsed.get(databaseName);
if (tablesUsedToColumnsMap == null) {
if (this.connection.lowerCaseTableNames()) {
tablesUsedToColumnsMap = new TreeMap<String, Map<String, Integer>>(String.CASE_INSENSITIVE_ORDER);
} else {
tablesUsedToColumnsMap = new TreeMap<String, Map<String, Integer>>();
}
this.databasesUsedToTablesUsed.put(databaseName, tablesUsedToColumnsMap);
}
nameToIndex = tablesUsedToColumnsMap.get(tableName);
if (nameToIndex == null) {
nameToIndex = new HashMap<String, Integer>();
tablesUsedToColumnsMap.put(tableName, nameToIndex);
}
return nameToIndex;
}
private synchronized SingleByteCharsetConverter getCharConverter() throws SQLException {
if (!this.initializedCharConverter) {
this.initializedCharConverter = true;
if (this.connection.getUseUnicode()) {
this.charEncoding = this.connection.getEncoding();
this.charConverter = this.connection.getCharsetConverter(this.charEncoding);
}
}
return this.charConverter;
}
/**
* JDBC 2.0 Return the concurrency of this result set. The concurrency used
* is determined by the statement that created the result set.
*
* @return the concurrency type, CONCUR_READ_ONLY, etc.
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public int getConcurrency() throws SQLException {
return (this.isUpdatable ? CONCUR_UPDATABLE : CONCUR_READ_ONLY);
}
private synchronized String getQuotedIdChar() throws SQLException {
if (this.quotedIdChar == null) {
boolean useQuotedIdentifiers = this.connection.supportsQuotedIdentifiers();
if (useQuotedIdentifiers) {
java.sql.DatabaseMetaData dbmd = this.connection.getMetaData();
this.quotedIdChar = dbmd.getIdentifierQuoteString();
} else {
this.quotedIdChar = "";
}
}
return this.quotedIdChar;
}
/**
* JDBC 2.0 Insert the contents of the insert row into the result set and
* the database. Must be on the insert row when this method is called.
*
* @exception SQLException
* if a database-access error occurs, if called when not on
* the insert row, or if all non-nullable columns in the
* insert row have not been given a value
*/
@Override
public synchronized void insertRow() throws SQLException {
checkClosed();
if (!this.onInsertRow) {
throw SQLError.createSQLException(Messages.getString("UpdatableResultSet.7"), getExceptionInterceptor());
}
this.inserter.executeUpdate();
long autoIncrementId = this.inserter.getLastInsertID();
int numFields = this.fields.length;
byte[][] newRow = new byte[numFields][];
for (int i = 0; i < numFields; i++) {
if (this.inserter.isNull(i)) {
newRow[i] = null;
} else {
newRow[i] = this.inserter.getBytesRepresentation(i);
}
//
// WARN: This non-variant only holds if MySQL never allows more than one auto-increment key (which is the way it is _today_)
//
if (this.fields[i].isAutoIncrement() && autoIncrementId > 0) {
newRow[i] = StringUtils.getBytes(String.valueOf(autoIncrementId));
this.inserter.setBytesNoEscapeNoQuotes(i + 1, newRow[i]);
}
}
ResultSetRow resultSetRow = new ByteArrayRow(newRow, getExceptionInterceptor());
refreshRow(this.inserter, resultSetRow);
this.rowData.addRow(resultSetRow);
resetInserter();
}
/**
* JDBC 2.0
*
* <p>
* Determine if the cursor is after the last row in the result set.
* </p>
*
* @return true if after the last row, false otherwise. Returns false when
* the result set contains no rows.
*
* @exception SQLException
* if a database-access error occurs.
*/
@Override
public synchronized boolean isAfterLast() throws SQLException {
return super.isAfterLast();
}
/**
* JDBC 2.0
*
* <p>
* Determine if the cursor is before the first row in the result set.
* </p>
*
* @return true if before the first row, false otherwise. Returns false when
* the result set contains no rows.
*
* @exception SQLException
* if a database-access error occurs.
*/
@Override
public synchronized boolean isBeforeFirst() throws SQLException {
return super.isBeforeFirst();
}
/**
* JDBC 2.0
*
* <p>
* Determine if the cursor is on the first row of the result set.
* </p>
*
* @return true if on the first row, false otherwise.
*
* @exception SQLException
* if a database-access error occurs.
*/
@Override
public synchronized boolean isFirst() throws SQLException {
return super.isFirst();
}
/**
* JDBC 2.0
*
* <p>
* Determine if the cursor is on the last row of the result set. Note: Calling isLast() may be expensive since the JDBC driver might need to fetch ahead one
* row in order to determine whether the current row is the last row in the result set.
* </p>
*
* @return true if on the last row, false otherwise.
*
* @exception SQLException
* if a database-access error occurs.
*/
@Override
public synchronized boolean isLast() throws SQLException {
return super.isLast();
}
boolean isUpdatable() {
return this.isUpdatable;
}
/**
* JDBC 2.0
*
* <p>
* Moves to the last row in the result set.
* </p>
*
* @return true if on a valid row, false if no rows in the result set.
*
* @exception SQLException
* if a database-access error occurs, or result set type is
* TYPE_FORWARD_ONLY.
*/
@Override
public synchronized boolean last() throws SQLException {
return super.last();
}
/**
* JDBC 2.0 Move the cursor to the remembered cursor position, usually the
* current row. Has no effect unless the cursor is on the insert row.
*
* @exception SQLException
* if a database-access error occurs, or the result set is
* not updatable
* @throws SQLException
* if the ResultSet is not updatable or some other error occurs
*/
@Override
public synchronized void moveToCurrentRow() throws SQLException {
checkClosed();
if (!this.isUpdatable) {
throw new NotUpdatable(this.notUpdatableReason);
}
if (this.onInsertRow) {
this.onInsertRow = false;
this.thisRow = this.savedCurrentRow;
}
}
/**
* JDBC 2.0 Move to the insert row. The current cursor position is
* remembered while the cursor is positioned on the insert row. The insert
* row is a special row associated with an updatable result set. It is
* essentially a buffer where a new row may be constructed by calling the
* updateXXX() methods prior to inserting the row into the result set. Only
* the updateXXX(), getXXX(), and insertRow() methods may be called when the
* cursor is on the insert row. All of the columns in a result set must be
* given a value each time this method is called before calling insertRow().
* UpdateXXX()must be called before getXXX() on a column.
*
* @exception SQLException
* if a database-access error occurs, or the result set is
* not updatable
* @throws NotUpdatable
*/
@Override
public synchronized void moveToInsertRow() throws SQLException {
checkClosed();
if (!this.isUpdatable) {
throw new NotUpdatable(this.notUpdatableReason);
}
if (this.inserter == null) {
if (this.insertSQL == null) {
generateStatements();
}
this.inserter = (PreparedStatement) this.connection.clientPrepareStatement(this.insertSQL);
if (this.populateInserterWithDefaultValues) {
extractDefaultValues();
}
resetInserter();
} else {
resetInserter();
}
int numFields = this.fields.length;
this.onInsertRow = true;
this.doingUpdates = false;
this.savedCurrentRow = this.thisRow;
byte[][] newRowData = new byte[numFields][];
this.thisRow = new ByteArrayRow(newRowData, getExceptionInterceptor());
this.thisRow.setMetadata(this.fields);
for (int i = 0; i < numFields; i++) {
if (!this.populateInserterWithDefaultValues) {
this.inserter.setBytesNoEscapeNoQuotes(i + 1, StringUtils.getBytes("DEFAULT"));
newRowData = null;
} else {
if (this.defaultColumnValue[i] != null) {
Field f = this.fields[i];
switch (f.getMysqlType()) {
case MysqlDefs.FIELD_TYPE_DATE:
case MysqlDefs.FIELD_TYPE_DATETIME:
case MysqlDefs.FIELD_TYPE_NEWDATE:
case MysqlDefs.FIELD_TYPE_TIME:
case MysqlDefs.FIELD_TYPE_TIMESTAMP:
if (this.defaultColumnValue[i].length > 7 && this.defaultColumnValue[i][0] == (byte) 'C'
&& this.defaultColumnValue[i][1] == (byte) 'U' && this.defaultColumnValue[i][2] == (byte) 'R'
&& this.defaultColumnValue[i][3] == (byte) 'R' && this.defaultColumnValue[i][4] == (byte) 'E'
&& this.defaultColumnValue[i][5] == (byte) 'N' && this.defaultColumnValue[i][6] == (byte) 'T'
&& this.defaultColumnValue[i][7] == (byte) '_') {
this.inserter.setBytesNoEscapeNoQuotes(i + 1, this.defaultColumnValue[i]);
break;
}
this.inserter.setBytes(i + 1, this.defaultColumnValue[i], false, false);
break;
default:
this.inserter.setBytes(i + 1, this.defaultColumnValue[i], false, false);
}
// This value _could_ be changed from a getBytes(), so we need a copy....
byte[] defaultValueCopy = new byte[this.defaultColumnValue[i].length];
System.arraycopy(this.defaultColumnValue[i], 0, defaultValueCopy, 0, defaultValueCopy.length);
newRowData[i] = defaultValueCopy;
} else {
this.inserter.setNull(i + 1, java.sql.Types.NULL);
newRowData[i] = null;
}
}
}
}
// ---------------------------------------------------------------------
// Updates
// ---------------------------------------------------------------------
/**
* A ResultSet is initially positioned before its first row, the first call
* to next makes the first row the current row; the second call makes the
* second row the current row, etc.
*
* <p>
* If an input stream from the previous row is open, it is implicitly closed. The ResultSet's warning chain is cleared when a new row is read
* </p>
*
* @return true if the new current is valid; false if there are no more rows
*
* @exception SQLException
* if a database access error occurs
*/
@Override
public synchronized boolean next() throws SQLException {
return super.next();
}
/**
* The prev method is not part of JDBC, but because of the architecture of
* this driver it is possible to move both forward and backward within the
* result set.
*
* <p>
* If an input stream from the previous row is open, it is implicitly closed. The ResultSet's warning chain is cleared when a new row is read
* </p>
*
* @return true if the new current is valid; false if there are no more rows
*
* @exception SQLException
* if a database access error occurs
*/
@Override
public synchronized boolean prev() throws SQLException {
return super.prev();
}
/**
* JDBC 2.0
*
* <p>
* Moves to the previous row in the result set.
* </p>
*
* <p>
* Note: previous() is not the same as relative(-1) since it makes sense to call previous() when there is no current row.
* </p>
*
* @return true if on a valid row, false if off the result set.
*
* @exception SQLException
* if a database-access error occurs, or result set type is
* TYPE_FORWAR_DONLY.
*/
@Override
public synchronized boolean previous() throws SQLException {
return super.previous();
}
/**
* Closes this ResultSet and releases resources.
*
* @param calledExplicitly
* was realClose called by the standard ResultSet.close() method, or was it closed internally by the
* driver?
*
* @throws SQLException
* if an error occurs.
*/
@Override
public synchronized void realClose(boolean calledExplicitly) throws SQLException {
if (this.isClosed) {
return;
}
SQLException sqlEx = null;
if (this.useUsageAdvisor) {
if ((this.deleter == null) && (this.inserter == null) && (this.refresher == null) && (this.updater == null)) {
this.eventSink = ProfilerEventHandlerFactory.getInstance(this.connection);
String message = Messages.getString("UpdatableResultSet.34");
this.eventSink.consumeEvent(
new ProfilerEvent(ProfilerEvent.TYPE_WARN, "", (this.owningStatement == null) ? "N/A" : this.owningStatement.currentCatalog,
this.connectionId, (this.owningStatement == null) ? (-1) : this.owningStatement.getId(), this.resultId,
System.currentTimeMillis(), 0, Constants.MILLIS_I18N, null, this.pointOfOrigin, message));
}
}
try {
if (this.deleter != null) {
this.deleter.close();
}
} catch (SQLException ex) {
sqlEx = ex;
}
try {
if (this.inserter != null) {
this.inserter.close();
}
} catch (SQLException ex) {
sqlEx = ex;
}
try {
if (this.refresher != null) {
this.refresher.close();
}
} catch (SQLException ex) {
sqlEx = ex;
}
try {
if (this.updater != null) {
this.updater.close();
}
} catch (SQLException ex) {
sqlEx = ex;
}
super.realClose(calledExplicitly);
if (sqlEx != null) {
throw sqlEx;
}
}
/**
* JDBC 2.0 Refresh the value of the current row with its current value in
* the database. Cannot be called when on the insert row. The refreshRow()
* method provides a way for an application to explicitly tell the JDBC
* driver to refetch a row(s) from the database. An application may want to
* call refreshRow() when caching or prefetching is being done by the JDBC
* driver to fetch the latest value of a row from the database. The JDBC
* driver may actually refresh multiple rows at once if the fetch size is
* greater than one. All values are refetched subject to the transaction
* isolation level and cursor sensitivity. If refreshRow() is called after
* calling updateXXX(), but before calling updateRow() then the updates made
* to the row are lost. Calling refreshRow() frequently will likely slow
* performance.
*
* @exception SQLException
* if a database-access error occurs, or if called when on
* the insert row.
* @throws NotUpdatable
*/
@Override
public synchronized void refreshRow() throws SQLException {
checkClosed();
if (!this.isUpdatable) {
throw new NotUpdatable();
}
if (this.onInsertRow) {
throw SQLError.createSQLException(Messages.getString("UpdatableResultSet.8"), getExceptionInterceptor());
} else if (this.rowData.size() == 0) {
throw SQLError.createSQLException(Messages.getString("UpdatableResultSet.9"), getExceptionInterceptor());
} else if (isBeforeFirst()) {
throw SQLError.createSQLException(Messages.getString("UpdatableResultSet.10"), getExceptionInterceptor());
} else if (isAfterLast()) {
throw SQLError.createSQLException(Messages.getString("UpdatableResultSet.11"), getExceptionInterceptor());
}
refreshRow(this.updater, this.thisRow);
}
private synchronized void refreshRow(PreparedStatement updateInsertStmt, ResultSetRow rowToRefresh) throws SQLException {
if (this.refresher == null) {
if (this.refreshSQL == null) {
generateStatements();
}
this.refresher = (PreparedStatement) this.connection.clientPrepareStatement(this.refreshSQL);
}
this.refresher.clearParameters();
int numKeys = this.primaryKeyIndicies.size();
if (numKeys == 1) {
byte[] dataFrom = null;
int index = this.primaryKeyIndicies.get(0).intValue();
if (!this.doingUpdates && !this.onInsertRow) {
dataFrom = rowToRefresh.getColumnValue(index);
} else {
dataFrom = updateInsertStmt.getBytesRepresentation(index);
// Primary keys not set?
if (updateInsertStmt.isNull(index) || (dataFrom.length == 0)) {
dataFrom = rowToRefresh.getColumnValue(index);
} else {
dataFrom = stripBinaryPrefix(dataFrom);
}
}
if (this.fields[index].getvalueNeedsQuoting()) {
this.refresher.setBytesNoEscape(1, dataFrom);
} else {
this.refresher.setBytesNoEscapeNoQuotes(1, dataFrom);
}
} else {
for (int i = 0; i < numKeys; i++) {
byte[] dataFrom = null;
int index = this.primaryKeyIndicies.get(i).intValue();
if (!this.doingUpdates && !this.onInsertRow) {
dataFrom = rowToRefresh.getColumnValue(index);
} else {
dataFrom = updateInsertStmt.getBytesRepresentation(index);
// Primary keys not set?
if (updateInsertStmt.isNull(index) || (dataFrom.length == 0)) {
dataFrom = rowToRefresh.getColumnValue(index);
} else {
dataFrom = stripBinaryPrefix(dataFrom);
}
}
this.refresher.setBytesNoEscape(i + 1, dataFrom);
}
}
java.sql.ResultSet rs = null;
try {
rs = this.refresher.executeQuery();
int numCols = rs.getMetaData().getColumnCount();
if (rs.next()) {
for (int i = 0; i < numCols; i++) {
byte[] val = rs.getBytes(i + 1);
if ((val == null) || rs.wasNull()) {
rowToRefresh.setColumnValue(i, null);
} else {
rowToRefresh.setColumnValue(i, rs.getBytes(i + 1));
}
}
} else {
throw SQLError.createSQLException(Messages.getString("UpdatableResultSet.12"), SQLError.SQL_STATE_GENERAL_ERROR, getExceptionInterceptor());
}
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException ex) {
// ignore
}
}
}
}
/**
* JDBC 2.0
*
* <p>
* Moves a relative number of rows, either positive or negative. Attempting to move beyond the first/last row in the result set positions the cursor
* before/after the the first/last row. Calling relative(0) is valid, but does not change the cursor position.
* </p>
*
* <p>
* Note: Calling relative(1) is different than calling next() since is makes sense to call next() when there is no current row, for example, when the cursor
* is positioned before the first row or after the last row of the result set.
* </p>
*
* @param rows
*
* @return true if on a row, false otherwise.
*
* @exception SQLException
* if a database-access error occurs, or there is no current
* row, or result set type is TYPE_FORWARD_ONLY.
*/
@Override
public synchronized boolean relative(int rows) throws SQLException {
return super.relative(rows);
}
private void resetInserter() throws SQLException {
this.inserter.clearParameters();
for (int i = 0; i < this.fields.length; i++) {
this.inserter.setNull(i + 1, 0);
}
}
/**
* JDBC 2.0 Determine if this row has been deleted. A deleted row may leave
* a visible "hole" in a result set. This method can be used to detect holes
* in a result set. The value returned depends on whether or not the result
* set can detect deletions.
*
* @return true if deleted and deletes are detected
*
* @exception SQLException
* if a database-access error occurs
* @throws NotImplemented
*
* @see DatabaseMetaData#deletesAreDetected
*/
@Override
public synchronized boolean rowDeleted() throws SQLException {
throw SQLError.createSQLFeatureNotSupportedException();
}
/**
* JDBC 2.0 Determine if the current row has been inserted. The value
* returned depends on whether or not the result set can detect visible
* inserts.
*
* @return true if inserted and inserts are detected
*
* @exception SQLException
* if a database-access error occurs
* @throws NotImplemented
*
* @see DatabaseMetaData#insertsAreDetected
*/
@Override
public synchronized boolean rowInserted() throws SQLException {
throw SQLError.createSQLFeatureNotSupportedException();
}
/**
* JDBC 2.0 Determine if the current row has been updated. The value
* returned depends on whether or not the result set can detect updates.
*
* @return true if the row has been visibly updated by the owner or another,
* and updates are detected
*
* @exception SQLException
* if a database-access error occurs
* @throws NotImplemented
*
* @see DatabaseMetaData#updatesAreDetected
*/
@Override
public synchronized boolean rowUpdated() throws SQLException {
throw SQLError.createSQLFeatureNotSupportedException();
}
/**
* Sets the concurrency type of this result set
*
* @param concurrencyFlag
* the type of concurrency that this ResultSet should support.
*/
@Override
protected void setResultSetConcurrency(int concurrencyFlag) {
super.setResultSetConcurrency(concurrencyFlag);
//
// FIXME: Issue warning when asked for updateable result set, but result
// set is not
// updatable
//
// if ((concurrencyFlag == CONCUR_UPDATABLE) && !isUpdatable()) {
// java.sql.SQLWarning warning = new java.sql.SQLWarning(
// NotUpdatable.NOT_UPDATEABLE_MESSAGE);
// }
}
private byte[] stripBinaryPrefix(byte[] dataFrom) {
return StringUtils.stripEnclosure(dataFrom, "_binary'", "'");
}
/**
* Reset UPDATE prepared statement to value in current row. This_Row MUST
* point to current, valid row.
*
* @throws SQLException
*/
protected synchronized void syncUpdate() throws SQLException {
if (this.updater == null) {
if (this.updateSQL == null) {
generateStatements();
}
this.updater = (PreparedStatement) this.connection.clientPrepareStatement(this.updateSQL);
}
int numFields = this.fields.length;
this.updater.clearParameters();
for (int i = 0; i < numFields; i++) {
if (this.thisRow.getColumnValue(i) != null) {
if (this.fields[i].getvalueNeedsQuoting()) {
this.updater.setBytes(i + 1, this.thisRow.getColumnValue(i), this.fields[i].isBinary(), false);
} else {
this.updater.setBytesNoEscapeNoQuotes(i + 1, this.thisRow.getColumnValue(i));
}
} else {
this.updater.setNull(i + 1, 0);
}
}
int numKeys = this.primaryKeyIndicies.size();
if (numKeys == 1) {
int index = this.primaryKeyIndicies.get(0).intValue();
this.setParamValue(this.updater, numFields + 1, this.thisRow, index, this.fields[index].getSQLType());
} else {
for (int i = 0; i < numKeys; i++) {
int idx = this.primaryKeyIndicies.get(i).intValue();
this.setParamValue(this.updater, numFields + i + 1, this.thisRow, idx, this.fields[idx].getSQLType());
}
}
}
/**
* JDBC 2.0 Update a column with an ascii stream value. The updateXXX()
* methods are used to update column values in the current row, or the
* insert row. The updateXXX() methods do not update the underlying
* database, instead the updateRow() or insertRow() methods are called to
* update the database.
*
* @param columnIndex
* the first column is 1, the second is 2, ...
* @param x
* the new column value
* @param length
* the length of the stream
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateAsciiStream(int columnIndex, java.io.InputStream x, int length) throws SQLException {
if (!this.onInsertRow) {
if (!this.doingUpdates) {
this.doingUpdates = true;
syncUpdate();
}
this.updater.setAsciiStream(columnIndex, x, length);
} else {
this.inserter.setAsciiStream(columnIndex, x, length);
this.thisRow.setColumnValue(columnIndex - 1, STREAM_DATA_MARKER);
}
}
/**
* JDBC 2.0 Update a column with an ascii stream value. The updateXXX()
* methods are used to update column values in the current row, or the
* insert row. The updateXXX() methods do not update the underlying
* database, instead the updateRow() or insertRow() methods are called to
* update the database.
*
* @param columnName
* the name of the column
* @param x
* the new column value
* @param length
* of the stream
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateAsciiStream(String columnName, java.io.InputStream x, int length) throws SQLException {
updateAsciiStream(findColumn(columnName), x, length);
}
/**
* JDBC 2.0 Update a column with a BigDecimal value. The updateXXX() methods
* are used to update column values in the current row, or the insert row.
* The updateXXX() methods do not update the underlying database, instead
* the updateRow() or insertRow() methods are called to update the database.
*
* @param columnIndex
* the first column is 1, the second is 2, ...
* @param x
* the new column value
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateBigDecimal(int columnIndex, BigDecimal x) throws SQLException {
if (!this.onInsertRow) {
if (!this.doingUpdates) {
this.doingUpdates = true;
syncUpdate();
}
this.updater.setBigDecimal(columnIndex, x);
} else {
this.inserter.setBigDecimal(columnIndex, x);
if (x == null) {
this.thisRow.setColumnValue(columnIndex - 1, null);
} else {
this.thisRow.setColumnValue(columnIndex - 1, StringUtils.getBytes(x.toString()));
}
}
}
/**
* JDBC 2.0 Update a column with a BigDecimal value. The updateXXX() methods
* are used to update column values in the current row, or the insert row.
* The updateXXX() methods do not update the underlying database, instead
* the updateRow() or insertRow() methods are called to update the database.
*
* @param columnName
* the name of the column
* @param x
* the new column value
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateBigDecimal(String columnName, BigDecimal x) throws SQLException {
updateBigDecimal(findColumn(columnName), x);
}
/**
* JDBC 2.0 Update a column with a binary stream value. The updateXXX()
* methods are used to update column values in the current row, or the
* insert row. The updateXXX() methods do not update the underlying
* database, instead the updateRow() or insertRow() methods are called to
* update the database.
*
* @param columnIndex
* the first column is 1, the second is 2, ...
* @param x
* the new column value
* @param length
* the length of the stream
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateBinaryStream(int columnIndex, java.io.InputStream x, int length) throws SQLException {
if (!this.onInsertRow) {
if (!this.doingUpdates) {
this.doingUpdates = true;
syncUpdate();
}
this.updater.setBinaryStream(columnIndex, x, length);
} else {
this.inserter.setBinaryStream(columnIndex, x, length);
if (x == null) {
this.thisRow.setColumnValue(columnIndex - 1, null);
} else {
this.thisRow.setColumnValue(columnIndex - 1, STREAM_DATA_MARKER);
}
}
}
/**
* JDBC 2.0 Update a column with a binary stream value. The updateXXX()
* methods are used to update column values in the current row, or the
* insert row. The updateXXX() methods do not update the underlying
* database, instead the updateRow() or insertRow() methods are called to
* update the database.
*
* @param columnName
* the name of the column
* @param x
* the new column value
* @param length
* of the stream
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateBinaryStream(String columnName, java.io.InputStream x, int length) throws SQLException {
updateBinaryStream(findColumn(columnName), x, length);
}
/**
* @see ResultSetInternalMethods#updateBlob(int, Blob)
*/
@Override
public synchronized void updateBlob(int columnIndex, java.sql.Blob blob) throws SQLException {
if (!this.onInsertRow) {
if (!this.doingUpdates) {
this.doingUpdates = true;
syncUpdate();
}
this.updater.setBlob(columnIndex, blob);
} else {
this.inserter.setBlob(columnIndex, blob);
if (blob == null) {
this.thisRow.setColumnValue(columnIndex - 1, null);
} else {
this.thisRow.setColumnValue(columnIndex - 1, STREAM_DATA_MARKER);
}
}
}
/**
* @see ResultSetInternalMethods#updateBlob(String, Blob)
*/
@Override
public synchronized void updateBlob(String columnName, java.sql.Blob blob) throws SQLException {
updateBlob(findColumn(columnName), blob);
}
/**
* JDBC 2.0 Update a column with a boolean value. The updateXXX() methods
* are used to update column values in the current row, or the insert row.
* The updateXXX() methods do not update the underlying database, instead
* the updateRow() or insertRow() methods are called to update the database.
*
* @param columnIndex
* the first column is 1, the second is 2, ...
* @param x
* the new column value
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateBoolean(int columnIndex, boolean x) throws SQLException {
if (!this.onInsertRow) {
if (!this.doingUpdates) {
this.doingUpdates = true;
syncUpdate();
}
this.updater.setBoolean(columnIndex, x);
} else {
this.inserter.setBoolean(columnIndex, x);
this.thisRow.setColumnValue(columnIndex - 1, this.inserter.getBytesRepresentation(columnIndex - 1));
}
}
/**
* JDBC 2.0 Update a column with a boolean value. The updateXXX() methods
* are used to update column values in the current row, or the insert row.
* The updateXXX() methods do not update the underlying database, instead
* the updateRow() or insertRow() methods are called to update the database.
*
* @param columnName
* the name of the column
* @param x
* the new column value
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateBoolean(String columnName, boolean x) throws SQLException {
updateBoolean(findColumn(columnName), x);
}
/**
* JDBC 2.0 Update a column with a byte value. The updateXXX() methods are
* used to update column values in the current row, or the insert row. The
* updateXXX() methods do not update the underlying database, instead the
* updateRow() or insertRow() methods are called to update the database.
*
* @param columnIndex
* the first column is 1, the second is 2, ...
* @param x
* the new column value
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateByte(int columnIndex, byte x) throws SQLException {
if (!this.onInsertRow) {
if (!this.doingUpdates) {
this.doingUpdates = true;
syncUpdate();
}
this.updater.setByte(columnIndex, x);
} else {
this.inserter.setByte(columnIndex, x);
this.thisRow.setColumnValue(columnIndex - 1, this.inserter.getBytesRepresentation(columnIndex - 1));
}
}
/**
* JDBC 2.0 Update a column with a byte value. The updateXXX() methods are
* used to update column values in the current row, or the insert row. The
* updateXXX() methods do not update the underlying database, instead the
* updateRow() or insertRow() methods are called to update the database.
*
* @param columnName
* the name of the column
* @param x
* the new column value
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateByte(String columnName, byte x) throws SQLException {
updateByte(findColumn(columnName), x);
}
/**
* JDBC 2.0 Update a column with a byte array value. The updateXXX() methods
* are used to update column values in the current row, or the insert row.
* The updateXXX() methods do not update the underlying database, instead
* the updateRow() or insertRow() methods are called to update the database.
*
* @param columnIndex
* the first column is 1, the second is 2, ...
* @param x
* the new column value
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateBytes(int columnIndex, byte[] x) throws SQLException {
if (!this.onInsertRow) {
if (!this.doingUpdates) {
this.doingUpdates = true;
syncUpdate();
}
this.updater.setBytes(columnIndex, x);
} else {
this.inserter.setBytes(columnIndex, x);
this.thisRow.setColumnValue(columnIndex - 1, x);
}
}
/**
* JDBC 2.0 Update a column with a byte array value. The updateXXX() methods
* are used to update column values in the current row, or the insert row.
* The updateXXX() methods do not update the underlying database, instead
* the updateRow() or insertRow() methods are called to update the database.
*
* @param columnName
* the name of the column
* @param x
* the new column value
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateBytes(String columnName, byte[] x) throws SQLException {
updateBytes(findColumn(columnName), x);
}
/**
* JDBC 2.0 Update a column with a character stream value. The updateXXX()
* methods are used to update column values in the current row, or the
* insert row. The updateXXX() methods do not update the underlying
* database, instead the updateRow() or insertRow() methods are called to
* update the database.
*
* @param columnIndex
* the first column is 1, the second is 2, ...
* @param x
* the new column value
* @param length
* the length of the stream
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateCharacterStream(int columnIndex, java.io.Reader x, int length) throws SQLException {
if (!this.onInsertRow) {
if (!this.doingUpdates) {
this.doingUpdates = true;
syncUpdate();
}
this.updater.setCharacterStream(columnIndex, x, length);
} else {
this.inserter.setCharacterStream(columnIndex, x, length);
if (x == null) {
this.thisRow.setColumnValue(columnIndex - 1, null);
} else {
this.thisRow.setColumnValue(columnIndex - 1, STREAM_DATA_MARKER);
}
}
}
/**
* JDBC 2.0 Update a column with a character stream value. The updateXXX()
* methods are used to update column values in the current row, or the
* insert row. The updateXXX() methods do not update the underlying
* database, instead the updateRow() or insertRow() methods are called to
* update the database.
*
* @param columnName
* the name of the column
* @param reader
* the new column value
* @param length
* of the stream
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateCharacterStream(String columnName, java.io.Reader reader, int length) throws SQLException {
updateCharacterStream(findColumn(columnName), reader, length);
}
/**
* @see ResultSetInternalMethods#updateClob(int, Clob)
*/
@Override
public void updateClob(int columnIndex, java.sql.Clob clob) throws SQLException {
if (clob == null) {
updateNull(columnIndex);
} else {
updateCharacterStream(columnIndex, clob.getCharacterStream(), (int) clob.length());
}
}
/**
* JDBC 2.0 Update a column with a Date value. The updateXXX() methods are
* used to update column values in the current row, or the insert row. The
* updateXXX() methods do not update the underlying database, instead the
* updateRow() or insertRow() methods are called to update the database.
*
* @param columnIndex
* the first column is 1, the second is 2, ...
* @param x
* the new column value
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateDate(int columnIndex, java.sql.Date x) throws SQLException {
if (!this.onInsertRow) {
if (!this.doingUpdates) {
this.doingUpdates = true;
syncUpdate();
}
this.updater.setDate(columnIndex, x);
} else {
this.inserter.setDate(columnIndex, x);
this.thisRow.setColumnValue(columnIndex - 1, this.inserter.getBytesRepresentation(columnIndex - 1));
}
}
/**
* JDBC 2.0 Update a column with a Date value. The updateXXX() methods are
* used to update column values in the current row, or the insert row. The
* updateXXX() methods do not update the underlying database, instead the
* updateRow() or insertRow() methods are called to update the database.
*
* @param columnName
* the name of the column
* @param x
* the new column value
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateDate(String columnName, java.sql.Date x) throws SQLException {
updateDate(findColumn(columnName), x);
}
/**
* JDBC 2.0 Update a column with a Double value. The updateXXX() methods are
* used to update column values in the current row, or the insert row. The
* updateXXX() methods do not update the underlying database, instead the
* updateRow() or insertRow() methods are called to update the database.
*
* @param columnIndex
* the first column is 1, the second is 2, ...
* @param x
* the new column value
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateDouble(int columnIndex, double x) throws SQLException {
if (!this.onInsertRow) {
if (!this.doingUpdates) {
this.doingUpdates = true;
syncUpdate();
}
this.updater.setDouble(columnIndex, x);
} else {
this.inserter.setDouble(columnIndex, x);
this.thisRow.setColumnValue(columnIndex - 1, this.inserter.getBytesRepresentation(columnIndex - 1));
}
}
/**
* JDBC 2.0 Update a column with a double value. The updateXXX() methods are
* used to update column values in the current row, or the insert row. The
* updateXXX() methods do not update the underlying database, instead the
* updateRow() or insertRow() methods are called to update the database.
*
* @param columnName
* the name of the column
* @param x
* the new column value
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateDouble(String columnName, double x) throws SQLException {
updateDouble(findColumn(columnName), x);
}
/**
* JDBC 2.0 Update a column with a float value. The updateXXX() methods are
* used to update column values in the current row, or the insert row. The
* updateXXX() methods do not update the underlying database, instead the
* updateRow() or insertRow() methods are called to update the database.
*
* @param columnIndex
* the first column is 1, the second is 2, ...
* @param x
* the new column value
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateFloat(int columnIndex, float x) throws SQLException {
if (!this.onInsertRow) {
if (!this.doingUpdates) {
this.doingUpdates = true;
syncUpdate();
}
this.updater.setFloat(columnIndex, x);
} else {
this.inserter.setFloat(columnIndex, x);
this.thisRow.setColumnValue(columnIndex - 1, this.inserter.getBytesRepresentation(columnIndex - 1));
}
}
/**
* JDBC 2.0 Update a column with a float value. The updateXXX() methods are
* used to update column values in the current row, or the insert row. The
* updateXXX() methods do not update the underlying database, instead the
* updateRow() or insertRow() methods are called to update the database.
*
* @param columnName
* the name of the column
* @param x
* the new column value
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateFloat(String columnName, float x) throws SQLException {
updateFloat(findColumn(columnName), x);
}
/**
* JDBC 2.0 Update a column with an integer value. The updateXXX() methods
* are used to update column values in the current row, or the insert row.
* The updateXXX() methods do not update the underlying database, instead
* the updateRow() or insertRow() methods are called to update the database.
*
* @param columnIndex
* the first column is 1, the second is 2, ...
* @param x
* the new column value
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateInt(int columnIndex, int x) throws SQLException {
if (!this.onInsertRow) {
if (!this.doingUpdates) {
this.doingUpdates = true;
syncUpdate();
}
this.updater.setInt(columnIndex, x);
} else {
this.inserter.setInt(columnIndex, x);
this.thisRow.setColumnValue(columnIndex - 1, this.inserter.getBytesRepresentation(columnIndex - 1));
}
}
/**
* JDBC 2.0 Update a column with an integer value. The updateXXX() methods
* are used to update column values in the current row, or the insert row.
* The updateXXX() methods do not update the underlying database, instead
* the updateRow() or insertRow() methods are called to update the database.
*
* @param columnName
* the name of the column
* @param x
* the new column value
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateInt(String columnName, int x) throws SQLException {
updateInt(findColumn(columnName), x);
}
/**
* JDBC 2.0 Update a column with a long value. The updateXXX() methods are
* used to update column values in the current row, or the insert row. The
* updateXXX() methods do not update the underlying database, instead the
* updateRow() or insertRow() methods are called to update the database.
*
* @param columnIndex
* the first column is 1, the second is 2, ...
* @param x
* the new column value
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateLong(int columnIndex, long x) throws SQLException {
if (!this.onInsertRow) {
if (!this.doingUpdates) {
this.doingUpdates = true;
syncUpdate();
}
this.updater.setLong(columnIndex, x);
} else {
this.inserter.setLong(columnIndex, x);
this.thisRow.setColumnValue(columnIndex - 1, this.inserter.getBytesRepresentation(columnIndex - 1));
}
}
/**
* JDBC 2.0 Update a column with a long value. The updateXXX() methods are
* used to update column values in the current row, or the insert row. The
* updateXXX() methods do not update the underlying database, instead the
* updateRow() or insertRow() methods are called to update the database.
*
* @param columnName
* the name of the column
* @param x
* the new column value
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateLong(String columnName, long x) throws SQLException {
updateLong(findColumn(columnName), x);
}
/**
* JDBC 2.0 Give a nullable column a null value. The updateXXX() methods are
* used to update column values in the current row, or the insert row. The
* updateXXX() methods do not update the underlying database, instead the
* updateRow() or insertRow() methods are called to update the database.
*
* @param columnIndex
* the first column is 1, the second is 2, ...
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateNull(int columnIndex) throws SQLException {
if (!this.onInsertRow) {
if (!this.doingUpdates) {
this.doingUpdates = true;
syncUpdate();
}
this.updater.setNull(columnIndex, 0);
} else {
this.inserter.setNull(columnIndex, 0);
this.thisRow.setColumnValue(columnIndex - 1, null);
}
}
/**
* JDBC 2.0 Update a column with a null value. The updateXXX() methods are
* used to update column values in the current row, or the insert row. The
* updateXXX() methods do not update the underlying database, instead the
* updateRow() or insertRow() methods are called to update the database.
*
* @param columnName
* the name of the column
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateNull(String columnName) throws SQLException {
updateNull(findColumn(columnName));
}
/**
* JDBC 2.0 Update a column with an Object value. The updateXXX() methods
* are used to update column values in the current row, or the insert row.
* The updateXXX() methods do not update the underlying database, instead
* the updateRow() or insertRow() methods are called to update the database.
*
* @param columnIndex
* the first column is 1, the second is 2, ...
* @param x
* the new column value
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateObject(int columnIndex, Object x) throws SQLException {
updateObjectInternal(columnIndex, x, null, 0);
}
/**
* JDBC 2.0 Update a column with an Object value. The updateXXX() methods
* are used to update column values in the current row, or the insert row.
* The updateXXX() methods do not update the underlying database, instead
* the updateRow() or insertRow() methods are called to update the database.
*
* @param columnIndex
* the first column is 1, the second is 2, ...
* @param x
* the new column value
* @param scale
* For java.sql.Types.DECIMAL or java.sql.Types.NUMERIC types
* this is the number of digits after the decimal. For all other
* types this value will be ignored.
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateObject(int columnIndex, Object x, int scale) throws SQLException {
updateObjectInternal(columnIndex, x, null, scale);
}
/**
* Internal setObject implementation. Although targetType is not part of default ResultSet methods signatures, it is used for type conversions from
* JDBC42UpdatableResultSet new JDBC 4.2 updateObject() methods.
*
* @param columnIndex
* @param x
* @param targetType
* @param scaleOrLength
* @throws SQLException
*/
protected synchronized void updateObjectInternal(int columnIndex, Object x, Integer targetType, int scaleOrLength) throws SQLException {
if (!this.onInsertRow) {
if (!this.doingUpdates) {
this.doingUpdates = true;
syncUpdate();
}
if (targetType == null) {
this.updater.setObject(columnIndex, x);
} else {
this.updater.setObject(columnIndex, x, targetType);
}
} else {
if (targetType == null) {
this.inserter.setObject(columnIndex, x);
} else {
this.inserter.setObject(columnIndex, x, targetType);
}
this.thisRow.setColumnValue(columnIndex - 1, this.inserter.getBytesRepresentation(columnIndex - 1));
}
}
/**
* JDBC 2.0 Update a column with an Object value. The updateXXX() methods
* are used to update column values in the current row, or the insert row.
* The updateXXX() methods do not update the underlying database, instead
* the updateRow() or insertRow() methods are called to update the database.
*
* @param columnName
* the name of the column
* @param x
* the new column value
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateObject(String columnName, Object x) throws SQLException {
updateObject(findColumn(columnName), x);
}
/**
* JDBC 2.0 Update a column with an Object value. The updateXXX() methods
* are used to update column values in the current row, or the insert row.
* The updateXXX() methods do not update the underlying database, instead
* the updateRow() or insertRow() methods are called to update the database.
*
* @param columnName
* the name of the column
* @param x
* the new column value
* @param scale
* For java.sql.Types.DECIMAL or java.sql.Types.NUMERIC types
* this is the number of digits after the decimal. For all other
* types this value will be ignored.
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateObject(String columnName, Object x, int scale) throws SQLException {
updateObject(findColumn(columnName), x);
}
/**
* JDBC 2.0 Update the underlying database with the new contents of the
* current row. Cannot be called when on the insert row.
*
* @exception SQLException
* if a database-access error occurs, or if called when on
* the insert row
* @throws NotUpdatable
*/
@Override
public synchronized void updateRow() throws SQLException {
if (!this.isUpdatable) {
throw new NotUpdatable(this.notUpdatableReason);
}
if (this.doingUpdates) {
this.updater.executeUpdate();
refreshRow();
this.doingUpdates = false;
} else if (this.onInsertRow) {
throw SQLError.createSQLException(Messages.getString("UpdatableResultSet.44"), getExceptionInterceptor());
}
//
// fixes calling updateRow() and then doing more
// updates on same row...
syncUpdate();
}
/**
* JDBC 2.0 Update a column with a short value. The updateXXX() methods are
* used to update column values in the current row, or the insert row. The
* updateXXX() methods do not update the underlying database, instead the
* updateRow() or insertRow() methods are called to update the database.
*
* @param columnIndex
* the first column is 1, the second is 2, ...
* @param x
* the new column value
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateShort(int columnIndex, short x) throws SQLException {
if (!this.onInsertRow) {
if (!this.doingUpdates) {
this.doingUpdates = true;
syncUpdate();
}
this.updater.setShort(columnIndex, x);
} else {
this.inserter.setShort(columnIndex, x);
this.thisRow.setColumnValue(columnIndex - 1, this.inserter.getBytesRepresentation(columnIndex - 1));
}
}
/**
* JDBC 2.0 Update a column with a short value. The updateXXX() methods are
* used to update column values in the current row, or the insert row. The
* updateXXX() methods do not update the underlying database, instead the
* updateRow() or insertRow() methods are called to update the database.
*
* @param columnName
* the name of the column
* @param x
* the new column value
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateShort(String columnName, short x) throws SQLException {
updateShort(findColumn(columnName), x);
}
/**
* JDBC 2.0 Update a column with a String value. The updateXXX() methods are
* used to update column values in the current row, or the insert row. The
* updateXXX() methods do not update the underlying database, instead the
* updateRow() or insertRow() methods are called to update the database.
*
* @param columnIndex
* the first column is 1, the second is 2, ...
* @param x
* the new column value
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateString(int columnIndex, String x) throws SQLException {
checkClosed();
if (!this.onInsertRow) {
if (!this.doingUpdates) {
this.doingUpdates = true;
syncUpdate();
}
this.updater.setString(columnIndex, x);
} else {
this.inserter.setString(columnIndex, x);
if (x == null) {
this.thisRow.setColumnValue(columnIndex - 1, null);
} else {
if (getCharConverter() != null) {
this.thisRow.setColumnValue(columnIndex - 1, StringUtils.getBytes(x, this.charConverter, this.charEncoding,
this.connection.getServerCharset(), this.connection.parserKnowsUnicode(), getExceptionInterceptor()));
} else {
this.thisRow.setColumnValue(columnIndex - 1, StringUtils.getBytes(x));
}
}
}
}
/**
* JDBC 2.0 Update a column with a String value. The updateXXX() methods are
* used to update column values in the current row, or the insert row. The
* updateXXX() methods do not update the underlying database, instead the
* updateRow() or insertRow() methods are called to update the database.
*
* @param columnName
* the name of the column
* @param x
* the new column value
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateString(String columnName, String x) throws SQLException {
updateString(findColumn(columnName), x);
}
/**
* JDBC 2.0 Update a column with a Time value. The updateXXX() methods are
* used to update column values in the current row, or the insert row. The
* updateXXX() methods do not update the underlying database, instead the
* updateRow() or insertRow() methods are called to update the database.
*
* @param columnIndex
* the first column is 1, the second is 2, ...
* @param x
* the new column value
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateTime(int columnIndex, java.sql.Time x) throws SQLException {
if (!this.onInsertRow) {
if (!this.doingUpdates) {
this.doingUpdates = true;
syncUpdate();
}
this.updater.setTime(columnIndex, x);
} else {
this.inserter.setTime(columnIndex, x);
this.thisRow.setColumnValue(columnIndex - 1, this.inserter.getBytesRepresentation(columnIndex - 1));
}
}
/**
* JDBC 2.0 Update a column with a Time value. The updateXXX() methods are
* used to update column values in the current row, or the insert row. The
* updateXXX() methods do not update the underlying database, instead the
* updateRow() or insertRow() methods are called to update the database.
*
* @param columnName
* the name of the column
* @param x
* the new column value
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateTime(String columnName, java.sql.Time x) throws SQLException {
updateTime(findColumn(columnName), x);
}
/**
* JDBC 2.0 Update a column with a Timestamp value. The updateXXX() methods
* are used to update column values in the current row, or the insert row.
* The updateXXX() methods do not update the underlying database, instead
* the updateRow() or insertRow() methods are called to update the database.
*
* @param columnIndex
* the first column is 1, the second is 2, ...
* @param x
* the new column value
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateTimestamp(int columnIndex, java.sql.Timestamp x) throws SQLException {
if (!this.onInsertRow) {
if (!this.doingUpdates) {
this.doingUpdates = true;
syncUpdate();
}
this.updater.setTimestamp(columnIndex, x);
} else {
this.inserter.setTimestamp(columnIndex, x);
this.thisRow.setColumnValue(columnIndex - 1, this.inserter.getBytesRepresentation(columnIndex - 1));
}
}
/**
* JDBC 2.0 Update a column with a Timestamp value. The updateXXX() methods
* are used to update column values in the current row, or the insert row.
* The updateXXX() methods do not update the underlying database, instead
* the updateRow() or insertRow() methods are called to update the database.
*
* @param columnName
* the name of the column
* @param x
* the new column value
*
* @exception SQLException
* if a database-access error occurs
*/
@Override
public synchronized void updateTimestamp(String columnName, java.sql.Timestamp x) throws SQLException {
updateTimestamp(findColumn(columnName), x);
}
}