1 25 26 package org.objectweb.cjdbc.driver; 27 28 import java.io.ByteArrayInputStream ; 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 import java.io.NotSerializableException ; 32 import java.io.ObjectInputStream ; 33 import java.math.BigDecimal ; 34 import java.math.BigInteger ; 35 import java.net.URL ; 36 import java.sql.PreparedStatement ; 37 import java.sql.Ref ; 38 import java.sql.ResultSet ; 39 import java.sql.SQLException ; 40 import java.sql.SQLWarning ; 41 import java.sql.Time ; 42 import java.sql.Timestamp ; 43 import java.util.ArrayList ; 44 import java.util.Calendar ; 45 import java.util.Hashtable ; 46 import java.util.Iterator ; 47 48 import org.objectweb.cjdbc.common.exceptions.NotImplementedException; 49 import org.objectweb.cjdbc.common.exceptions.ProtocolException; 50 import org.objectweb.cjdbc.common.stream.CJDBCInputStream; 51 import org.objectweb.cjdbc.common.stream.CJDBCOutputStream; 52 import org.objectweb.cjdbc.driver.protocol.SQLDataSerialization; 53 import org.objectweb.cjdbc.driver.protocol.TypeTag; 54 55 108 public class DriverResultSet 109 implements 110 java.sql.ResultSet , 111 java.io.Serializable , 112 java.lang.Cloneable 113 { 114 private static final long serialVersionUID = 7408879935608629886L; 115 116 117 protected int currentRow = -1; 118 119 protected int nbOfRows = -1; 120 121 protected int nbOfColumns = -1; 122 123 protected ArrayList data; 124 125 private boolean hasMoreData; 126 127 protected int fetchDirection = FETCH_FORWARD; 128 129 protected int fetchSize = 0; 130 131 private String cursorName; 132 133 134 protected Field[] fields; 135 136 137 private SQLDataSerialization.Serializer[] serializers; 138 139 140 protected boolean wasNullFlag = false; 141 142 protected transient Hashtable columnNameToIndex = null; 143 144 protected transient Hashtable fullColumnNameToIndex = null; 145 146 147 protected int resultSetType = 0; 148 149 protected int resultSetConcurrency = 0; 150 151 protected SQLWarning warnings = null; 152 153 protected transient Statement owningStatement; 154 155 private final Connection connection; 156 157 private boolean isClosed = true; 158 159 160 private transient PreparedStatement deleteStatement = null; 161 162 private transient PreparedStatement insertStatement = null; 163 164 private transient PreparedStatement refreshStatement = null; 165 166 private transient PreparedStatement updateStatement = null; 167 168 private transient boolean inserting = false; 169 170 private transient boolean updating = false; 171 172 private transient Object [] tempRow = null; 173 174 private transient String [] primaryKeyColumns = null; 175 176 private static final String UPDATEABLE_MESSAGE = "ResultSet not updateable. The " 177 + "query that generated this result set must select only one table, and must " 178 + "select all primary keys from that table. See the JDBC 2.1 API Specification, " 179 + "section 5.6 for more details."; 180 181 185 197 public boolean next() throws java.sql.SQLException 198 { 199 checkIfClosed(); 200 201 if (inserting) 202 { 203 insertStatement.clearParameters(); 204 tempRow = null; 205 inserting = false; 206 } 207 208 if (updating) 209 cancelRowUpdates(); 210 211 if (nbOfRows == 0) 212 return false; 213 214 if (currentRow + 1 >= nbOfRows) 215 { 216 if (hasMoreData) 217 { 218 this.connection.fetchNextData(cursorName, fetchSize, this); 219 currentRow = 0; 220 if (data == null) 221 { 222 nbOfRows = 0; 223 return false; 224 } 225 else 226 { 227 nbOfRows = data.size(); 228 return true; 229 } 230 } 231 232 currentRow = nbOfRows; 234 return false; 235 } 236 237 clearWarnings(); 238 currentRow++; 239 return true; 240 } 241 242 254 public boolean prev() throws SQLException 255 { 256 checkIfClosed(); 257 258 if (inserting) 259 { 260 insertStatement.clearParameters(); 261 tempRow = null; 262 inserting = false; 263 } 264 265 if (updating) 266 cancelRowUpdates(); 267 268 if (currentRow - 1 >= 0) 269 { 270 currentRow--; 271 return true; 272 } 273 274 return false; 275 } 276 277 287 public boolean isBeforeFirst() throws SQLException 288 { 289 checkIfClosed(); 290 if (nbOfRows == 0) 291 return false; 292 else 293 return (currentRow == -1); 294 } 295 296 306 public boolean isAfterLast() throws SQLException 307 { 308 checkIfClosed(); 309 if (nbOfRows == 0) 310 return false; 311 else 312 return (currentRow >= nbOfRows); 313 } 314 315 324 public boolean isFirst() throws SQLException 325 { 326 checkIfClosed(); 327 if (nbOfRows == 0) 328 return false; 329 else 330 return (currentRow == 0); 331 } 332 333 345 public boolean isLast() throws SQLException 346 { 347 checkIfClosed(); 348 if (nbOfRows == 0) 349 return false; 350 else 351 return (currentRow == nbOfRows - 1); 352 } 353 354 363 public void beforeFirst() throws SQLException 364 { 365 checkIfClosed(); 366 367 if (inserting) 368 { 369 insertStatement.clearParameters(); 370 tempRow = null; 371 inserting = false; 372 } 373 374 if (updating) 375 cancelRowUpdates(); 376 377 currentRow = -1; 378 } 379 380 389 public void afterLast() throws SQLException 390 { 391 checkIfClosed(); 392 393 if (inserting) 394 { 395 insertStatement.clearParameters(); 396 tempRow = null; 397 inserting = false; 398 } 399 400 if (updating) 401 cancelRowUpdates(); 402 403 if (nbOfRows != 0) 404 currentRow = nbOfRows; 405 } 406 407 417 public boolean first() throws SQLException 418 { 419 checkIfClosed(); 420 421 if (inserting) 422 { 423 insertStatement.clearParameters(); 424 tempRow = null; 425 inserting = false; 426 } 427 428 if (updating) 429 cancelRowUpdates(); 430 431 if (nbOfRows == 0) 432 return false; 433 434 currentRow = 0; 435 return true; 436 } 437 438 448 public boolean last() throws SQLException 449 { 450 checkIfClosed(); 451 452 if (inserting) 453 { 454 insertStatement.clearParameters(); 455 tempRow = null; 456 inserting = false; 457 } 458 459 if (updating) 460 cancelRowUpdates(); 461 462 if (nbOfRows == 0) 463 return false; 464 465 currentRow = nbOfRows - 1; 466 return true; 467 } 468 469 478 public int getRow() throws SQLException 479 { 480 checkIfClosed(); 481 if (currentRow < 0 || currentRow >= nbOfRows || nbOfRows == 0) 482 return 0; 483 else 484 return currentRow + 1; 485 } 486 487 510 public boolean absolute(int row) throws SQLException 511 { 512 checkIfClosed(); 513 514 if (inserting) 515 { 516 insertStatement.clearParameters(); 517 tempRow = null; 518 inserting = false; 519 } 520 521 if (updating) 522 cancelRowUpdates(); 523 524 if (nbOfRows == 0) 525 return false; 526 527 if (row == 0) 528 throw new SQLException ("Cannot absolute position to row 0"); 529 530 if (row == 1) 531 return first(); 532 533 if (row == -1) 534 return last(); 535 536 if (row > nbOfRows) 537 { 538 afterLast(); 539 return false; 540 } 541 542 if (row < 0) 543 { int newRowPosition = nbOfRows + row + 1; 545 546 if (newRowPosition <= 0) 547 { 548 beforeFirst(); 549 return false; 550 } 551 552 return absolute(newRowPosition); 553 } 554 else 555 { 556 row--; currentRow = row; 558 return true; 559 } 560 } 561 562 580 public boolean relative(int rows) throws SQLException 581 { 582 checkIfClosed(); 583 584 if (inserting) 585 { 586 insertStatement.clearParameters(); 587 tempRow = null; 588 inserting = false; 589 } 590 591 if (updating) 592 cancelRowUpdates(); 593 594 if (nbOfRows == 0) 595 return false; 596 597 return absolute(currentRow + rows + 1); 598 } 599 600 612 public boolean previous() throws SQLException 613 { 614 return prev(); 615 } 616 617 629 public void setFetchDirection(int direction) throws SQLException 630 { 631 if (direction != FETCH_FORWARD && direction != FETCH_REVERSE) 632 throw new SQLException ("Illegal value for fetch direction"); 633 else 634 fetchDirection = direction; 635 } 636 637 643 public int getFetchDirection() throws SQLException 644 { 645 return fetchDirection; 646 } 647 648 661 public void setFetchSize(int rows) throws SQLException 662 { 663 if (rows < 0) 666 throw new SQLException ("Value must be between 0 and getMaxRows()"); 667 668 fetchSize = rows; 669 } 670 671 677 public int getFetchSize() throws SQLException 678 { 679 return fetchSize; 680 } 681 682 688 695 public String getString(int columnIndex) throws SQLException 696 { 697 checkRowAndColPosAndSetNullFlag(columnIndex); 698 699 if (wasNullFlag) 700 return null; 701 702 if (inserting || updating) 703 return tempRow[columnIndex - 1].toString(); 704 else 705 return (((Object []) data.get(currentRow))[columnIndex - 1]).toString(); 706 } 707 708 715 public boolean getBoolean(int columnIndex) throws SQLException 716 { 717 checkRowAndColPosAndSetNullFlag(columnIndex); 718 719 if (wasNullFlag) 720 return false; 721 722 Object object; 723 if (inserting || updating) 724 object = tempRow[columnIndex - 1]; 725 else 726 object = (((Object []) data.get(currentRow))[columnIndex - 1]); 727 728 String stringVal = object.toString(); 729 if ((stringVal != null) && (stringVal.length() > 0)) 730 { 731 stringVal = stringVal.toLowerCase(); 732 733 if (this.connection.getPreparedStatementBooleanTrue().equals( 735 stringVal)) 736 { 737 return true; 738 } 739 else if (this.connection.getPreparedStatementBooleanFalse() 740 .equals(stringVal)) 741 { 742 return false; 743 } 744 745 else if ("t".equals(stringVal)) 747 { 748 return true; 749 } 750 else if ("f".equals(stringVal)) 751 { 752 return false; 753 } 754 else if ("true".equals(stringVal)) 755 { 756 return true; 757 } 758 else if ("false".equals(stringVal)) 759 { 760 return false; 761 } 762 else if ("1".equals(stringVal)) 763 { 764 return true; 765 } 766 else if ("0".equals(stringVal)) 767 { 768 return false; 769 } 770 else if ("y".equals(stringVal)) 771 { 772 return true; 773 } 774 else if ("n".equals(stringVal)) 775 { 776 return false; 777 } 778 else if ("yes".equals(stringVal)) 779 { 780 return true; 781 } 782 else if ("no".equals(stringVal)) 783 { 784 return false; 785 } 786 else if (object instanceof Number ) 787 { 788 int value = ((Number ) object).intValue(); 789 if (value == 0) 790 return false; 791 else if (value == 1) 792 return true; 793 } 795 796 throw new SQLException ("column value " + stringVal 798 + " could not be converted to boolean"); 799 } 800 else 801 { 802 return false; 803 } 804 } 805 806 813 public short getShort(int columnIndex) throws SQLException 814 { 815 checkRowAndColPosAndSetNullFlag(columnIndex); 816 817 if (wasNullFlag) 818 return 0; 819 820 Object obj; 821 if (inserting || updating) 822 obj = tempRow[columnIndex - 1]; 823 else 824 obj = ((Object []) data.get(currentRow))[columnIndex - 1]; 825 826 if (obj instanceof Number ) 827 { 828 return ((Number ) obj).shortValue(); 829 } 830 831 try 833 { 834 String string = obj.toString(); 835 string = string.trim(); 836 return Short.parseShort(string); 837 } 838 catch (NumberFormatException e) 839 { 840 throw new SQLException ("the value " + obj.toString() 841 + " is not a valid short number"); 842 } 843 } 844 845 852 public int getInt(int columnIndex) throws SQLException 853 { 854 checkRowAndColPosAndSetNullFlag(columnIndex); 855 856 if (wasNullFlag) 857 return 0; 858 859 Object obj; 860 if (inserting || updating) 861 obj = tempRow[columnIndex - 1]; 862 else 863 obj = ((Object []) data.get(currentRow))[columnIndex - 1]; 864 865 if (obj instanceof Number ) 866 { 867 return ((Number ) obj).intValue(); 868 } 869 870 try 872 { 873 String string = obj.toString(); 874 string = string.trim(); 875 return Integer.parseInt(string); 876 } 877 catch (NumberFormatException e) 878 { 879 throw new SQLException ("the value " + obj.toString() 880 + " is not a valid int number"); 881 } 882 } 883 884 891 public long getLong(int columnIndex) throws SQLException 892 { 893 checkRowAndColPosAndSetNullFlag(columnIndex); 894 895 if (wasNullFlag) 896 return 0; 897 898 Object obj; 899 if (inserting || updating) 900 obj = tempRow[columnIndex - 1]; 901 else 902 obj = ((Object []) data.get(currentRow))[columnIndex - 1]; 903 904 if (obj instanceof Number ) 905 { 906 return ((Number ) obj).longValue(); 907 } 908 909 try 911 { 912 String string = obj.toString(); 913 string = string.trim(); 914 return Long.parseLong(string); 915 } 916 catch (NumberFormatException e) 917 { 918 throw new SQLException ("the value " + obj.toString() 919 + " is not a valid long number"); 920 } 921 } 922 923 930 public float getFloat(int columnIndex) throws SQLException 931 { 932 checkRowAndColPosAndSetNullFlag(columnIndex); 933 934 if (wasNullFlag) 935 return 0; 936 937 Object obj; 938 if (inserting || updating) 939 obj = tempRow[columnIndex - 1]; 940 else 941 obj = ((Object []) data.get(currentRow))[columnIndex - 1]; 942 943 if (obj instanceof Number ) 944 { 945 return ((Number ) obj).floatValue(); 946 } 947 948 try 950 { 951 String string = obj.toString(); 952 string = string.trim(); 953 return Float.parseFloat(string); 954 } 955 catch (NumberFormatException e) 956 { 957 throw new SQLException ("the value " + obj.toString() 958 + " is not a valid float number"); 959 } 960 } 961 962 969 public double getDouble(int columnIndex) throws SQLException 970 { 971 checkRowAndColPosAndSetNullFlag(columnIndex); 972 973 if (wasNullFlag) 974 return 0; 975 976 Object obj; 977 if (inserting || updating) 978 obj = tempRow[columnIndex - 1]; 979 else 980 obj = ((Object []) data.get(currentRow))[columnIndex - 1]; 981 982 if (obj instanceof Number ) 983 { 984 return ((Number ) obj).doubleValue(); 985 } 986 987 try 989 { 990 String string = obj.toString(); 991 string = string.trim(); 992 return Double.parseDouble(string); 993 } 994 catch (NumberFormatException e) 995 { 996 throw new SQLException ("the value " + obj.toString() 997 + " is not a valid double number"); 998 } 999 } 1000 1001 1011 public BigDecimal getBigDecimal(int columnIndex, int scale) 1012 throws SQLException 1013 { 1014 BigDecimal bigDecimal = getBigDecimal(columnIndex); 1015 if (bigDecimal == null) 1016 return null; 1017 else 1018 return bigDecimal.setScale(scale); 1019 } 1020 1021 1030 public byte[] getBytes(int columnIndex) throws SQLException 1031 { 1032 checkRowAndColPosAndSetNullFlag(columnIndex); 1033 1034 if (wasNullFlag) 1035 return null; 1036 1037 Object o; 1038 if (inserting || updating) 1039 o = tempRow[columnIndex - 1]; 1040 else 1041 o = ((Object []) data.get(currentRow))[columnIndex - 1]; 1042 1043 byte[] tmp; 1044 if (o instanceof java.lang.String ) 1045 { 1046 tmp = ((String ) o).getBytes(); 1047 } 1048 else 1049 { 1050 tmp = (byte[]) o; 1051 } 1052 1053 if (this.connection.isDriverProcessed()) 1054 return this.connection.getBlobFilter().decode(tmp); 1055 else 1056 return tmp; 1057 } 1058 1059 1066 public java.sql.Date getDate(int columnIndex) throws SQLException 1067 { 1068 checkRowAndColPosAndSetNullFlag(columnIndex); 1069 1070 if (wasNullFlag) 1071 return null; 1072 1073 String dateString; 1077 if (inserting || updating) 1078 dateString = tempRow[columnIndex - 1].toString(); 1079 else 1080 dateString = ((Object []) data.get(currentRow))[columnIndex - 1] 1081 .toString(); 1082 1083 if (dateString.length() == 10) 1084 return java.sql.Date.valueOf(dateString); 1085 else 1086 return java.sql.Date.valueOf(dateString.substring(0, 10)); 1087 } 1088 1089 1096 public Time getTime(int columnIndex) throws SQLException 1097 { 1098 checkRowAndColPosAndSetNullFlag(columnIndex); 1099 1100 if (wasNullFlag) 1101 return null; 1102 1103 Object obj; 1104 if (inserting || updating) 1105 obj = tempRow[columnIndex - 1]; 1106 else 1107 obj = ((Object []) data.get(currentRow))[columnIndex - 1]; 1108 1109 if (obj instanceof java.util.Date ) 1110 { 1111 java.util.Date time = (java.util.Date ) obj; 1112 String timeString = time.toString(); 1115 if (timeString.length() == 8) 1116 { 1117 return new Time (time.getTime()); 1120 } 1121 1122 Calendar cal = Calendar.getInstance(); 1125 cal.setTime(time); 1126 cal.clear(Calendar.YEAR); 1127 cal.clear(Calendar.MONTH); 1128 cal.clear(Calendar.DATE); 1129 cal.clear(Calendar.MILLISECOND); 1130 return new Time (cal.getTimeInMillis()); 1131 } 1132 1133 try 1135 { 1136 String string = obj.toString(); 1137 string = string.trim(); 1138 return Time.valueOf(string); 1139 } 1140 catch (IllegalArgumentException e) 1141 { 1142 throw new SQLException ("the value " + obj.toString() 1143 + " is not a valid time"); 1144 } 1145 } 1146 1147 1154 public Timestamp getTimestamp(int columnIndex) throws SQLException 1155 { 1156 checkRowAndColPosAndSetNullFlag(columnIndex); 1157 1158 if (wasNullFlag) 1159 return null; 1160 1161 Object obj; 1162 if (inserting || updating) 1163 obj = tempRow[columnIndex - 1]; 1164 else 1165 obj = ((Object []) data.get(currentRow))[columnIndex - 1]; 1166 1167 if (obj instanceof java.util.Date ) 1168 { 1169 return new Timestamp (((java.util.Date ) obj).getTime()); 1170 } 1171 1172 try 1174 { 1175 String string = obj.toString(); 1176 string = string.trim(); 1177 return Timestamp.valueOf(string); 1178 } 1179 catch (IllegalArgumentException e) 1180 { 1181 throw new SQLException ("the value " + obj.toString() 1182 + " is not a valid timestamp"); 1183 } 1184 } 1185 1186 1204 public InputStream getAsciiStream(int columnIndex) throws SQLException 1205 { 1206 checkRowAndColPosAndSetNullFlag(columnIndex); 1207 1208 return getBinaryStream(columnIndex); 1209 } 1210 1211 1224 public InputStream getUnicodeStream(int columnIndex) throws SQLException 1225 { 1226 checkRowAndColPosAndSetNullFlag(columnIndex); 1227 1228 return getBinaryStream(columnIndex); 1229 } 1230 1231 1242 1243 public InputStream getBinaryStream(int columnIndex) throws SQLException 1244 { 1245 checkRowAndColPosAndSetNullFlag(columnIndex); 1246 1247 byte[] b = getBytes(columnIndex); 1248 if (b != null) 1249 return new ByteArrayInputStream (b); 1250 else 1251 return null; } 1253 1254 1262 public String getString(String columnName) throws SQLException 1263 { 1264 return this.getString(findColumn(columnName)); 1265 } 1266 1267 1270 public boolean getBoolean(String columnName) throws SQLException 1271 { 1272 return getBoolean(findColumn(columnName)); 1273 } 1274 1275 1278 public byte getByte(String columnName) throws SQLException 1279 { 1280 return getByte(findColumn(columnName)); 1281 } 1282 1283 1286 public short getShort(String columnName) throws SQLException 1287 { 1288 return getShort(findColumn(columnName)); 1289 } 1290 1291 1294 public int getInt(String columnName) throws SQLException 1295 { 1296 return getInt(findColumn(columnName)); 1297 } 1298 1299 1302 public long getLong(String columnName) throws SQLException 1303 { 1304 return getLong(findColumn(columnName)); 1305 } 1306 1307 1310 public float getFloat(String columnName) throws SQLException 1311 { 1312 return getFloat(findColumn(columnName)); 1313 } 1314 1315 1318 public double getDouble(String columnName) throws SQLException 1319 { 1320 return getDouble(findColumn(columnName)); 1321 } 1322 1323 1327 public BigDecimal getBigDecimal(String columnName, int scale) 1328 throws SQLException 1329 { 1330 return getBigDecimal(findColumn(columnName), scale); 1331 } 1332 1333 1336 public byte[] getBytes(String columnName) throws SQLException 1337 { 1338 return getBytes(findColumn(columnName)); 1339 } 1340 1341 1344 public java.sql.Date getDate(String columnName) throws SQLException 1345 { 1346 return getDate(findColumn(columnName)); 1347 } 1348 1349 1352 public Time getTime(String columnName) throws SQLException 1353 { 1354 return getTime(findColumn(columnName)); 1355 } 1356 1357 1360 public Timestamp getTimestamp(String columnName) throws SQLException 1361 { 1362 return getTimestamp(findColumn(columnName)); 1363 } 1364 1365 1368 public InputStream getAsciiStream(String columnName) throws SQLException 1369 { 1370 return getAsciiStream(findColumn(columnName)); 1371 } 1372 1373 1377 public InputStream getUnicodeStream(String columnName) throws SQLException 1378 { 1379 return getUnicodeStream(findColumn(columnName)); 1380 } 1381 1382 1385 public InputStream getBinaryStream(String columnName) throws SQLException 1386 { 1387 return getBinaryStream(findColumn(columnName)); 1388 } 1389 1390 1403 public java.sql.SQLWarning getWarnings() throws SQLException 1404 { 1405 return warnings; 1406 } 1407 1408 1414 1415 public void clearWarnings() throws SQLException 1416 { 1417 warnings = null; 1418 } 1419 1420 1423 public String getCursorName() throws SQLException 1424 { 1425 return cursorName; 1426 } 1427 1428 1442 public Object getObject(int columnIndex) throws SQLException 1443 { 1444 checkRowAndColPosAndSetNullFlag(columnIndex); 1445 1446 if (wasNullFlag) 1447 return null; 1448 1449 Object o; 1450 if (inserting || updating) 1451 o = tempRow[columnIndex]; 1452 else 1453 o = ((Object []) data.get(currentRow))[columnIndex - 1]; 1454 1455 if (owningStatement != null) 1456 { 1457 1462 if (this.connection.isDriverProcessed() && o instanceof byte[]) 1463 { 1464 try 1465 { 1466 byte[] b = this.connection.getBlobFilter().decode((byte[]) o); 1467 return new ObjectInputStream (new ByteArrayInputStream (b)) 1468 .readObject(); 1469 } 1470 catch (ClassNotFoundException e) 1471 { 1472 e.printStackTrace(); 1473 throw new SQLException ("Missing class while deserializing Blob" + e); 1474 } 1475 catch (IOException e) 1476 { 1477 e.printStackTrace(); 1478 throw new SQLException ("IOException while deserializing Blob" + e); 1479 } 1480 } 1481 } 1482 return o; 1483 } 1484 1485 1499 public Object getObject(String columnName) throws SQLException 1500 { 1501 return getObject(findColumn(columnName)); 1502 } 1503 1504 1506 1509 public java.io.Reader getCharacterStream(int columnIndex) throws SQLException 1510 { 1511 String s = getString(columnIndex); 1512 if (s == null) 1513 return null; 1514 char[] content = s.toCharArray(); 1515 return new java.io.CharArrayReader (content); 1516 } 1517 1518 1521 public java.io.Reader getCharacterStream(String columnName) 1522 throws SQLException 1523 { 1524 return getCharacterStream(findColumn(columnName)); 1525 } 1526 1527 1536 public BigDecimal getBigDecimal(int columnIndex) throws SQLException 1537 { 1538 checkRowAndColPosAndSetNullFlag(columnIndex); 1539 1540 if (wasNullFlag) 1541 return null; 1542 1543 Object obj; 1544 if (inserting || updating) 1545 obj = tempRow[columnIndex - 1]; 1546 else 1547 obj = ((Object []) data.get(currentRow))[columnIndex - 1]; 1548 1549 if (obj instanceof BigDecimal ) 1550 return (BigDecimal ) obj; 1551 else if (obj instanceof Long ) 1552 { 1553 return new BigDecimal (((Long ) obj).longValue()); 1554 } 1555 else if (obj instanceof BigInteger ) 1556 { 1557 return new BigDecimal (((BigInteger ) obj)); 1558 } 1559 else if (obj instanceof Short ) 1560 { 1561 return new BigDecimal (((Short ) obj).shortValue()); 1562 } 1563 else if (obj instanceof Integer ) 1564 { 1565 return new BigDecimal (((Integer ) obj).intValue()); 1566 } 1567 else if (obj instanceof String ) 1568 { 1569 return new BigDecimal ((String ) obj); 1570 } 1571 else if (obj instanceof Number ) 1572 { 1573 1585 return new BigDecimal (obj.toString()); 1590 } 1591 else 1592 { 1593 throw new SQLException ("Type " + obj.getClass().getName() 1594 + " is not compatible with BigDecimal"); 1595 } 1596 } 1597 1598 1601 public BigDecimal getBigDecimal(String columnName) throws SQLException 1602 { 1603 return getBigDecimal(findColumn(columnName)); 1604 } 1605 1606 1616 public Object getObject(int i, java.util.Map map) throws SQLException 1617 { 1618 throw new NotImplementedException("getObject(int, java.util.Map)"); 1619 } 1620 1621 1628 public java.sql.Ref getRef(int i) throws SQLException 1629 { 1630 checkRowAndColPosAndSetNullFlag(i); 1631 1632 if (wasNullFlag) 1633 return null; 1634 1635 if (inserting || updating) 1636 return (Ref ) tempRow[i - 1]; 1637 else 1638 return (Ref ) (((Object []) data.get(currentRow))[i - 1]); 1639 } 1640 1641 1648 public java.sql.Blob getBlob(int columnIndex) throws SQLException 1649 { 1650 checkRowAndColPosAndSetNullFlag(columnIndex); 1651 1652 if (wasNullFlag) 1653 return null; 1654 1655 Object o; 1656 if (inserting || updating) 1657 o = tempRow[columnIndex - 1]; 1658 else 1659 o = (((Object []) data.get(currentRow))[columnIndex - 1]); 1660 1661 if (o instanceof Blob) 1664 return (java.sql.Blob ) o; 1665 else 1666 return new Blob(getBytes(columnIndex)); 1667 } 1668 1669 1676 public java.sql.Clob getClob(int columnIndex) throws SQLException 1677 { 1678 checkRowAndColPosAndSetNullFlag(columnIndex); 1679 1680 if (wasNullFlag) 1681 return null; 1682 1683 Object o; 1684 if (inserting || updating) 1685 o = tempRow[columnIndex - 1]; 1686 else 1687 o = (((Object []) data.get(currentRow))[columnIndex - 1]); 1688 1689 if (o instanceof String ) 1692 return new Clob((String ) o); 1693 else if (o instanceof Clob) 1694 return (java.sql.Clob ) o; 1695 else 1696 return new Clob(new String (o.toString())); 1697 } 1698 1699 1706 public java.sql.Array getArray(int columnIndex) throws SQLException 1707 { 1708 checkRowAndColPosAndSetNullFlag(columnIndex); 1709 1710 if (wasNullFlag) 1711 return null; 1712 1713 if (inserting || updating) 1714 return (java.sql.Array ) tempRow[columnIndex - 1]; 1715 else 1716 return (java.sql.Array ) (((Object []) data.get(currentRow))[columnIndex - 1]); 1717 } 1718 1719 1729 public Object getObject(String colName, java.util.Map map) 1730 throws SQLException 1731 { 1732 return getObject(findColumn(colName), map); 1733 } 1734 1735 1742 public java.sql.Ref getRef(String colName) throws SQLException 1743 { 1744 return getRef(findColumn(colName)); 1745 } 1746 1747 1754 public java.sql.Blob getBlob(String colName) throws SQLException 1755 { 1756 return getBlob(findColumn(colName)); 1757 } 1758 1759 1766 public java.sql.Clob getClob(String colName) throws SQLException 1767 { 1768 return getClob(findColumn(colName)); 1769 } 1770 1771 1778 public java.sql.Array getArray(String colName) throws SQLException 1779 { 1780 return getArray(findColumn(colName)); 1781 } 1782 1783 1793 public java.sql.Date getDate(int columnIndex, Calendar cal) 1794 throws SQLException 1795 { 1796 return getDate(columnIndex); 1797 } 1798 1799 1809 public java.sql.Date getDate(String columnName, Calendar cal) 1810 throws SQLException 1811 { 1812 return getDate(findColumn(columnName), cal); 1813 } 1814 1815 1825 public java.sql.Time getTime(int columnIndex, Calendar cal) 1826 throws SQLException 1827 { 1828 return getTime(columnIndex); 1829 } 1830 1831 1841 public java.sql.Time getTime(String columnName, Calendar cal) 1842 throws SQLException 1843 { 1844 return getTime(findColumn(columnName), cal); 1845 } 1846 1847 1858 public java.sql.Timestamp getTimestamp(int columnIndex, Calendar cal) 1859 throws SQLException 1860 { 1861 return getTimestamp(columnIndex); 1862 } 1863 1864 1875 public java.sql.Timestamp getTimestamp(String columnName, Calendar cal) 1876 throws SQLException 1877 { 1878 return getTimestamp(findColumn(columnName), cal); 1879 } 1880 1881 1885 1894 public boolean rowUpdated() throws SQLException 1895 { 1896 throw new NotImplementedException("rowUpdated"); 1897 } 1898 1899 1907 public boolean rowInserted() throws SQLException 1908 { 1909 throw new NotImplementedException("rowInserted"); 1910 } 1911 1912 1922 public boolean rowDeleted() throws SQLException 1923 { 1924 throw new NotImplementedException("rowDeleted"); 1925 } 1926 1927 1936 public void updateNull(int columnIndex) throws SQLException 1937 { 1938 checkIfClosed(); 1939 1940 if (inserting) 1941 insertStatement 1942 .setNull(columnIndex, fields[columnIndex - 1].getSqlType()); 1943 else 1944 { 1945 checkUpdateFlagAndPrepareUpdateIfNeeded(); 1946 updateStatement 1947 .setNull(columnIndex, fields[columnIndex - 1].getSqlType()); 1948 } 1949 1950 tempRow[columnIndex - 1] = null; 1951 } 1952 1953 1963 1964 public void updateBoolean(int columnIndex, boolean x) throws SQLException 1965 { 1966 checkIfClosed(); 1967 1968 if (inserting) 1969 insertStatement.setBoolean(columnIndex, x); 1970 else 1971 { 1972 checkUpdateFlagAndPrepareUpdateIfNeeded(); 1973 updateStatement.setBoolean(columnIndex, x); 1974 } 1975 1976 tempRow[columnIndex - 1] = Boolean.valueOf(x); 1977 } 1978 1979 1989 public void updateByte(int columnIndex, byte x) throws SQLException 1990 { 1991 checkIfClosed(); 1992 1993 if (inserting) 1994 insertStatement.setByte(columnIndex, x); 1995 else 1996 { 1997 checkUpdateFlagAndPrepareUpdateIfNeeded(); 1998 updateStatement.setByte(columnIndex, x); 1999 } 2000 2001 tempRow[columnIndex - 1] = new Byte (x); 2002 } 2003 2004 2014 public void updateShort(int columnIndex, short x) throws SQLException 2015 { 2016 checkIfClosed(); 2017 2018 if (inserting) 2019 insertStatement.setShort(columnIndex, x); 2020 else 2021 { 2022 checkUpdateFlagAndPrepareUpdateIfNeeded(); 2023 updateStatement.setShort(columnIndex, x); 2024 } 2025 2026 tempRow[columnIndex - 1] = new Short (x); 2027 } 2028 2029 2039 public void updateInt(int columnIndex, int x) throws SQLException 2040 { 2041 checkIfClosed(); 2042 2043 if (inserting) 2044 insertStatement.setInt(columnIndex, x); 2045 else 2046 { 2047 checkUpdateFlagAndPrepareUpdateIfNeeded(); 2048 updateStatement.setInt(columnIndex, x); 2049 } 2050 2051 tempRow[columnIndex - 1] = new Integer (x); 2052 } 2053 2054 2064 public void updateLong(int columnIndex, long x) throws SQLException 2065 { 2066 checkIfClosed(); 2067 2068 if (inserting) 2069 insertStatement.setLong(columnIndex, x); 2070 else 2071 { 2072 checkUpdateFlagAndPrepareUpdateIfNeeded(); 2073 updateStatement.setLong(columnIndex, x); 2074 } 2075 2076 tempRow[columnIndex - 1] = new Long (x); 2077 } 2078 2079 2089 public void updateFloat(int columnIndex, float x) throws SQLException 2090 { 2091 checkIfClosed(); 2092 2093 if (inserting) 2094 insertStatement.setFloat(columnIndex, x); 2095 else 2096 { 2097 checkUpdateFlagAndPrepareUpdateIfNeeded(); 2098 updateStatement.setFloat(columnIndex, x); 2099 } 2100 2101 tempRow[columnIndex - 1] = new Float (x); 2102 } 2103 2104 2114 public void updateDouble(int columnIndex, double x) throws SQLException 2115 { 2116 checkIfClosed(); 2117 2118 if (inserting) 2119 insertStatement.setDouble(columnIndex, x); 2120 else 2121 { 2122 checkUpdateFlagAndPrepareUpdateIfNeeded(); 2123 updateStatement.setDouble(columnIndex, x); 2124 } 2125 2126 tempRow[columnIndex - 1] = new Double (x); 2127 } 2128 2129 2139 public void updateBigDecimal(int columnIndex, BigDecimal x) 2140 throws SQLException 2141 { 2142 checkIfClosed(); 2143 2144 if (inserting) 2145 insertStatement.setBigDecimal(columnIndex, x); 2146 else 2147 { 2148 checkUpdateFlagAndPrepareUpdateIfNeeded(); 2149 updateStatement.setBigDecimal(columnIndex, x); 2150 } 2151 2152 tempRow[columnIndex - 1] = x; 2153 } 2154 2155 2165 public void updateString(int columnIndex, String x) throws SQLException 2166 { 2167 checkIfClosed(); 2168 2169 if (inserting) 2170 insertStatement.setString(columnIndex, x); 2171 else 2172 { 2173 checkUpdateFlagAndPrepareUpdateIfNeeded(); 2174 updateStatement.setString(columnIndex, x); 2175 } 2176 2177 tempRow[columnIndex - 1] = x; 2178 } 2179 2180 2190 public void updateBytes(int columnIndex, byte[] x) throws SQLException 2191 { 2192 checkIfClosed(); 2193 2194 if (inserting) 2195 insertStatement.setBytes(columnIndex, x); 2196 else 2197 { 2198 checkUpdateFlagAndPrepareUpdateIfNeeded(); 2199 updateStatement.setBytes(columnIndex, x); 2200 } 2201 2202 tempRow[columnIndex - 1] = x; 2203 } 2204 2205 2215 public void updateDate(int columnIndex, java.sql.Date x) throws SQLException 2216 { 2217 checkIfClosed(); 2218 2219 if (inserting) 2220 insertStatement.setDate(columnIndex, x); 2221 else 2222 { 2223 checkUpdateFlagAndPrepareUpdateIfNeeded(); 2224 updateStatement.setDate(columnIndex, x); 2225 } 2226 2227 tempRow[columnIndex - 1] = x; 2228 } 2229 2230 2240 public void updateTime(int columnIndex, java.sql.Time x) throws SQLException 2241 { 2242 checkIfClosed(); 2243 2244 if (inserting) 2245 insertStatement.setTime(columnIndex, x); 2246 else 2247 { 2248 checkUpdateFlagAndPrepareUpdateIfNeeded(); 2249 updateStatement.setTime(columnIndex, x); 2250 } 2251 2252 tempRow[columnIndex - 1] = x; 2253 } 2254 2255 2265 public void updateTimestamp(int columnIndex, java.sql.Timestamp x) 2266 throws SQLException 2267 { 2268 checkIfClosed(); 2269 2270 if (inserting) 2271 insertStatement.setTimestamp(columnIndex, x); 2272 else 2273 { 2274 checkUpdateFlagAndPrepareUpdateIfNeeded(); 2275 updateStatement.setTimestamp(columnIndex, x); 2276 } 2277 2278 tempRow[columnIndex - 1] = x; 2279 } 2280 2281 2292 public void updateAsciiStream(int columnIndex, java.io.InputStream x, 2293 int length) throws SQLException 2294 { 2295 this.updateBinaryStream(columnIndex, x, length); 2296 } 2297 2298 2309 public void updateBinaryStream(int columnIndex, java.io.InputStream x, 2310 int length) throws SQLException 2311 { 2312 checkIfClosed(); 2313 2314 byte[] content = new byte[length]; 2315 try 2316 { 2317 x.read(content, 0, length); 2318 } 2319 catch (Exception ioe) 2320 { 2321 throw new SQLException ("Problem with streaming of data"); 2322 } 2323 2324 this.updateBytes(columnIndex, content); 2325 } 2326 2327 2338 public void updateCharacterStream(int columnIndex, java.io.Reader x, 2339 int length) throws SQLException 2340 { 2341 checkIfClosed(); 2342 2343 char[] content = new char[length]; 2344 try 2345 { 2346 x.read(content, 0, length); 2347 } 2348 catch (Exception ioe) 2349 { 2350 throw new SQLException ("Problem with streaming of data"); 2351 } 2352 2353 this.updateString(columnIndex, new String (content)); 2354 } 2355 2356 2369 public void updateObject(int columnIndex, Object x, int scale) 2370 throws SQLException 2371 { 2372 checkIfClosed(); 2373 2374 if (inserting) 2375 insertStatement.setObject(columnIndex, x, scale); 2376 else 2377 { 2378 checkUpdateFlagAndPrepareUpdateIfNeeded(); 2379 updateStatement.setObject(columnIndex, x, scale); 2380 } 2381 2382 tempRow[columnIndex - 1] = x; 2383 } 2384 2385 2395 public void updateObject(int columnIndex, Object x) throws SQLException 2396 { 2397 checkIfClosed(); 2398 2399 if (inserting) 2400 insertStatement.setObject(columnIndex, x); 2401 else 2402 { 2403 checkUpdateFlagAndPrepareUpdateIfNeeded(); 2404 updateStatement.setObject(columnIndex, x); 2405 } 2406 2407 tempRow[columnIndex - 1] = x; 2408 } 2409 2410 2419 2420 public void updateNull(String columnName) throws SQLException 2421 { 2422 this.updateNull(findColumn(columnName)); 2423 } 2424 2425 2435 public void updateBoolean(String columnName, boolean x) throws SQLException 2436 { 2437 this.updateBoolean(findColumn(columnName), x); 2438 } 2439 2440 2450 public void updateByte(String columnName, byte x) throws SQLException 2451 { 2452 this.updateByte(findColumn(columnName), x); 2453 } 2454 2455 2465 public void updateShort(String columnName, short x) throws SQLException 2466 { 2467 this.updateShort(findColumn(columnName), x); 2468 } 2469 2470 2480 public void updateInt(String columnName, int x) throws SQLException 2481 { 2482 this.updateInt(findColumn(columnName), x); 2483 } 2484 2485 2495 public void updateLong(String columnName, long x) throws SQLException 2496 { 2497 this.updateLong(findColumn(columnName), x); 2498 } 2499 2500 2510 public void updateFloat(String columnName, float x) throws SQLException 2511 { 2512 this.updateFloat(findColumn(columnName), x); 2513 } 2514 2515 2525 public void updateDouble(String columnName, double x) throws SQLException 2526 { 2527 this.updateDouble(findColumn(columnName), x); 2528 } 2529 2530 2540 public void updateBigDecimal(String columnName, BigDecimal x) 2541 throws SQLException 2542 { 2543 this.updateBigDecimal(findColumn(columnName), x); 2544 } 2545 2546 2556 public void updateString(String columnName, String x) throws SQLException 2557 { 2558 this.updateString(findColumn(columnName), x); 2559 } 2560 2561 2571 public void updateBytes(String columnName, byte[] x) throws SQLException 2572 { 2573 this.updateBytes(findColumn(columnName), x); 2574 } 2575 2576 2586 public void updateDate(String columnName, java.sql.Date x) 2587 throws SQLException 2588 { 2589 this.updateDate(findColumn(columnName), x); 2590 } 2591 2592 2602 public void updateTime(String columnName, java.sql.Time x) 2603 throws SQLException 2604 { 2605 this.updateTime(findColumn(columnName), x); 2606 } 2607 2608 2618 public void updateTimestamp(String columnName, java.sql.Timestamp x) 2619 throws SQLException 2620 { 2621 this.updateTimestamp(findColumn(columnName), x); 2622 } 2623 2624 2635 public void updateAsciiStream(String columnName, java.io.InputStream x, 2636 int length) throws SQLException 2637 { 2638 this.updateAsciiStream(findColumn(columnName), x, length); 2639 } 2640 2641 2652 public void updateBinaryStream(String columnName, java.io.InputStream x, 2653 int length) throws SQLException 2654 { 2655 this.updateBinaryStream(findColumn(columnName), x, length); 2656 } 2657 2658 2669 public void updateCharacterStream(String columnName, java.io.Reader reader, 2670 int length) throws SQLException 2671 { 2672 this.updateCharacterStream(findColumn(columnName), reader, length); 2673 } 2674 2675 2688 public void updateObject(String columnName, Object x, int scale) 2689 throws SQLException 2690 { 2691 this.updateObject(findColumn(columnName), x, scale); 2692 } 2693 2694 2704 public void updateObject(String columnName, Object x) throws SQLException 2705 { 2706 this.updateObject(findColumn(columnName), x); 2707 } 2708 2709 2717 public void insertRow() throws SQLException 2718 { 2719 checkIfClosed(); 2720 2721 if (!inserting) 2722 throw new SQLException ("insertRow cannot be called " 2723 + "when the cursor is not on the insert row"); 2724 2725 try 2727 { 2728 insertStatement.executeUpdate(); 2729 } 2730 finally 2731 { 2732 insertStatement.clearParameters(); 2734 } 2735 2736 if (data == null) 2738 data = new ArrayList (); 2739 data.add(tempRow); 2740 nbOfRows++; 2741 2742 tempRow = new Object [nbOfColumns]; 2744 } 2745 2746 2753 public void updateRow() throws SQLException 2754 { 2755 checkIfClosed(); 2756 2757 if (inserting) 2758 throw new SQLException ("updateRow cannot be called " 2759 + "when the cursor is on the insert row"); 2760 2761 if (!updating) 2762 return; 2763 2764 for (int i = 0; i < primaryKeyColumns.length; ++i) 2766 updateStatement.setObject(nbOfColumns + i + 1, ((Object []) data 2767 .get(currentRow))[findColumn(primaryKeyColumns[i]) - 1]); 2768 2769 try 2771 { 2772 updateStatement.executeUpdate(); 2773 } 2774 finally 2775 { 2776 updateStatement.clearParameters(); 2778 } 2779 2780 data.set(currentRow, tempRow); 2782 2783 tempRow = null; 2785 updating = false; 2786 } 2787 2788 2795 public void deleteRow() throws SQLException 2796 { 2797 checkIfClosed(); 2798 2799 if (inserting) 2800 throw new SQLException ("deleteRow cannot be called " 2801 + "when the cursor is on the insert row"); 2802 2803 this.checkRowPos(); 2804 2805 if (deleteStatement == null) 2806 this.buildDeleteStatement(); 2807 2808 for (int i = 0; i < primaryKeyColumns.length; ++i) 2810 deleteStatement 2811 .setObject( 2812 i + 1, 2813 ((Object []) data.get(currentRow))[findColumn(primaryKeyColumns[i]) - 1]); 2814 2815 try 2817 { 2818 deleteStatement.executeUpdate(); 2819 } 2820 finally 2821 { 2822 deleteStatement.clearParameters(); 2824 } 2825 2826 data.remove(currentRow); 2828 nbOfRows--; 2829 2830 if (updating) 2832 this.cancelRowUpdates(); 2833 } 2834 2835 2851 public void refreshRow() throws SQLException 2852 { 2853 checkIfClosed(); 2854 2855 if (inserting) 2856 throw new SQLException ("refreshRow cannot be called " 2857 + "when the cursor is on the insert row"); 2858 2859 this.checkRowPos(); 2860 2861 if (refreshStatement == null) 2862 this.buildRefreshStatement(); 2863 2864 for (int i = 0; i < primaryKeyColumns.length; ++i) 2866 refreshStatement 2867 .setObject( 2868 i + 1, 2869 ((Object []) data.get(currentRow))[findColumn(primaryKeyColumns[i]) - 1]); 2870 2871 DriverResultSet res; 2873 try 2874 { 2875 res = (DriverResultSet) refreshStatement.executeQuery(); 2876 } 2877 finally 2878 { 2879 refreshStatement.clearParameters(); 2881 } 2882 2883 try 2885 { 2886 data.set(currentRow, res.data.get(0)); 2887 } 2888 catch (NullPointerException e) 2889 { 2890 throw new SQLException ("The current row has been " 2891 + "removed from the database"); 2892 } 2893 finally 2894 { 2895 res.close(); 2897 } 2898 2899 if (updating) 2901 this.cancelRowUpdates(); 2902 } 2903 2904 2913 public void cancelRowUpdates() throws SQLException 2914 { 2915 checkIfClosed(); 2916 2917 if (inserting) 2918 throw new SQLException ("cancelRowUpdates cannot be " 2919 + "called when the cursor is on the insert row"); 2920 2921 if (!updating) 2922 return; 2923 2924 updateStatement.clearParameters(); 2925 tempRow = null; 2926 updating = false; 2927 } 2928 2929 2943 public void moveToInsertRow() throws SQLException 2944 { 2945 checkIfClosed(); 2946 2947 if (inserting) 2948 return; 2949 2950 if (insertStatement == null) 2951 this.buildInsertStatement(); 2952 2953 tempRow = new Object [nbOfColumns]; 2954 inserting = true; 2955 } 2956 2957 2964 public void moveToCurrentRow() throws SQLException 2965 { 2966 checkIfClosed(); 2967 2968 if (!inserting) 2969 return; 2970 2971 insertStatement.clearParameters(); 2972 tempRow = null; 2973 inserting = false; 2974 } 2975 2976 2982 2990 public int getType() throws SQLException 2991 { 2992 return resultSetType; 2993 } 2994 2995 3002 3003 public int getConcurrency() throws SQLException 3004 { 3005 return resultSetConcurrency; 3006 } 3007 3008 3014 public void close() throws SQLException 3015 { 3016 if (hasMoreData) 3017 this.connection.closeRemoteResultSet(cursorName); 3018 isClosed = true; 3019 } 3020 3021 3030 3031 public boolean wasNull() throws SQLException 3032 { 3033 return wasNullFlag; 3034 } 3035 3036 3045 public java.sql.Statement getStatement() throws SQLException 3046 { 3047 return owningStatement; 3048 } 3049 3050 3058 public void setData(ArrayList newData) 3059 { 3060 this.data = newData; 3061 if (newData == null) 3062 this.nbOfRows = 0; 3063 else 3064 this.nbOfRows = newData.size(); 3065 } 3066 3067 3073 public void setHasMoreData(boolean hasMore) 3074 { 3075 hasMoreData = hasMore; 3076 } 3077 3078 3085 public java.sql.ResultSetMetaData getMetaData() throws SQLException 3086 { 3087 return new ResultSetMetaData(this); 3088 } 3089 3090 3098 public int findColumn(String columnName) throws SQLException 3099 { 3100 if (columnNameToIndex == null) 3101 buildIndexMapping(); 3102 3103 Integer index = (Integer ) columnNameToIndex.get(columnName); 3104 3105 if (index == null) 3106 index = (Integer ) fullColumnNameToIndex.get(columnName); 3107 3108 if (index != null) 3109 return index.intValue() + 1; 3110 else 3111 { 3112 String columnNameUC = columnName.toUpperCase(); 3114 for (int i = 0; i < nbOfColumns; i++) 3115 { 3116 if (fields[i].getFieldName().toUpperCase().equals(columnNameUC)) 3117 return i + 1; 3118 else if (fields[i].getFullName().toUpperCase().equals(columnNameUC)) 3119 return i + 1; 3120 } 3121 throw new java.sql.SQLException ("Column '" + columnName + "' not found."); 3122 } 3123 } 3124 3125 3131 3136 private void initSerializers() throws NotSerializableException 3137 { 3138 3139 if (this.serializers != null) 3140 return; 3141 3142 this.serializers = new SQLDataSerialization.Serializer[nbOfColumns]; 3143 3144 for (int col = 0; col < nbOfColumns; col++) 3145 { 3146 int rowIdx = -1; 3147 while (serializers[col] == null) 3148 { 3149 rowIdx++; 3150 3151 if (rowIdx >= nbOfRows) break; 3154 3155 Object [] row = (Object []) data.get(rowIdx); 3156 Object sqlObj = row[col]; 3157 3158 3162 if (sqlObj == null) 3163 continue; 3164 3165 try 3166 { 3167 serializers[col] = SQLDataSerialization.getSerializer(sqlObj); 3168 } 3169 catch (NotImplementedException nie) 3170 { 3171 if (sqlObj instanceof Short ) 3172 { 3173 3183 3184 3189 try 3190 { 3191 serializers[col] = SQLDataSerialization 3192 .getSerializer(TypeTag.INTEGER); 3193 } 3194 catch (NotImplementedException e) 3195 { 3196 throw new IllegalArgumentException ("Internal bug"); 3197 } 3198 } 3199 else 3200 { 3201 NotSerializableException nse = new NotSerializableException ( 3202 "Backend driver gave an object of an unsupported java type:" 3203 + sqlObj.getClass().getName() + ", at colum number " + col 3204 + " of name " + fields[col].getFieldName()); 3205 nse.initCause(nie); 3206 throw nse; 3207 } 3208 } 3209 3210 } 3212 if (serializers[col] == null) { 3214 3217 3225 3230 3231 TypeTag javaObjectType = TypeTag.jdbcToJavaObjectType(fields[col] 3232 .getSqlType()); 3233 3234 if (javaObjectType != TypeTag.TYPE_ERROR) 3235 try 3236 { 3237 serializers[col] = SQLDataSerialization 3238 .getSerializer(javaObjectType); 3239 } 3240 catch (NotImplementedException nie) 3241 { 3242 IllegalArgumentException ipe = new IllegalArgumentException ( 3244 "Really incredible bug, TypeTag was: " + javaObjectType); 3245 ipe.initCause(nie); 3246 throw ipe; 3247 } 3248 else 3249 { throw new NotSerializableException ( 3251 "Could not guess type of column number " + col 3252 + "whole column is null and backend does not provide " 3253 + "a known JDBC type"); 3254 } 3255 } 3256 3257 } 3259 } 3260 3261 3271 public DriverResultSet(Field[] fields, ArrayList resultData) 3272 throws IllegalArgumentException 3273 { 3274 if (resultData == null) 3275 throw new IllegalArgumentException ( 3276 "Cannot build a DriverResultSet with null data ArrayList"); 3277 3278 this.data = resultData; 3279 currentRow = -1; 3280 this.fields = fields; 3281 3282 nbOfRows = resultData.size(); 3283 nbOfColumns = fields.length; 3284 isClosed = false; 3285 this.connection = null; 3287 } 3288 3289 3301 public DriverResultSet(Field[] fields, ArrayList resultData, 3302 boolean crsHasMoreData, String cursorName) 3303 throws IllegalArgumentException 3304 { 3305 this(fields, resultData); 3306 this.cursorName = cursorName; 3307 hasMoreData = crsHasMoreData; 3308 } 3309 3310 3318 3319 public DriverResultSet(Connection conn) throws IOException , ProtocolException 3320 { 3321 this.connection = conn; 3322 CJDBCInputStream input = this.connection.socketInput; 3323 this.nbOfColumns = input.readInt(); 3325 this.fields = new Field[nbOfColumns]; 3326 for (int f = 0; f < this.nbOfColumns; f++) 3327 this.fields[f] = new Field(input); 3328 3329 if (!TypeTag.COL_TYPES.equals(new TypeTag(input))) 3330 throw new ProtocolException("Column types were expected"); 3331 3332 this.nbOfRows = input.readInt(); 3333 3334 if (this.nbOfRows > 0) 3336 { 3337 this.serializers = new SQLDataSerialization.Serializer[nbOfColumns]; 3338 for (int col = 0; col < this.nbOfColumns; col++) 3339 { 3340 TypeTag tag = new TypeTag(input); 3341 try 3342 { 3343 serializers[col] = SQLDataSerialization.getSerializer(tag); 3344 } 3345 catch (NotImplementedException nie) 3346 { 3347 IllegalArgumentException ipe = new IllegalArgumentException ( 3349 "Really incredible bug, TypeTag was: " + tag); 3350 ipe.initCause(nie); 3351 throw ipe; 3352 } 3353 catch (IllegalArgumentException iae) 3354 { 3355 ProtocolException pe = new ProtocolException( 3356 "Protocol corruption: received unknown TypeTag " + tag 3357 + " for column " + col); 3358 pe.initCause(iae); 3359 throw pe; 3360 } 3361 } 3362 } 3363 3364 receiveRows(input); 3365 3366 if (this.hasMoreData) 3367 this.cursorName = input.readUTF(); 3368 3369 this.isClosed = false; 3370 3371 } 3372 3373 3381 3382 public void sendToStream( 3383 org.objectweb.cjdbc.common.stream.CJDBCOutputStream output) 3384 throws IOException 3385 { 3386 output.writeInt(this.nbOfColumns); 3388 for (int f = 0; f < this.nbOfColumns; f++) 3389 this.fields[f].sendToStream(output); 3390 3391 TypeTag.COL_TYPES.sendToStream(output); 3392 output.writeInt(this.nbOfRows); 3393 3394 if (this.nbOfRows > 0) 3396 { 3397 initSerializers(); 3398 3399 for (int col = 0; col < this.nbOfColumns; col++) 3400 serializers[col].getTypeTag().sendToStream(output); 3401 } 3402 3403 sendRowsToStream(output); 3405 3406 if (this.hasMoreData) 3407 { output.writeUTF(this.cursorName); 3409 } 3410 output.flush(); 3411 } 3412 3413 3420 3421 void receiveRows(CJDBCInputStream input) throws IOException , 3422 ProtocolException 3423 { 3424 3425 this.nbOfRows = input.readInt(); 3426 3427 boolean[] nulls = new boolean[this.nbOfColumns]; 3428 3429 this.data = new ArrayList (this.nbOfRows); 3431 3432 for (int r = 0; r < this.nbOfRows; r++) 3433 { 3434 if (!TypeTag.ROW.equals(new TypeTag(input))) 3435 throw new ProtocolException("A row was expected"); 3436 3437 for (int col = 0; col < nbOfColumns; col++) 3440 nulls[col] = input.readBoolean(); 3441 3442 Object [] row = new Object [this.nbOfColumns]; 3443 3444 3449 for (int col = 0; col < this.nbOfColumns; col++) 3450 if (nulls[col]) 3451 row[col] = null; 3452 else 3453 row[col] = serializers[col].receiveFromStream(input); 3454 3455 this.data.add(row); 3456 } 3457 3458 this.hasMoreData = input.readBoolean(); 3459 } 3460 3461 3468 public void sendRowsToStream(CJDBCOutputStream output) throws IOException 3469 { 3470 3471 output.writeInt(this.nbOfRows); 3472 3473 boolean[] nulls = new boolean[nbOfColumns]; 3474 3475 Iterator rowsIter = this.data.iterator(); 3476 while (rowsIter.hasNext()) 3477 { 3478 Object [] row = (Object []) rowsIter.next(); 3479 TypeTag.ROW.sendToStream(output); 3480 3481 for (int col = 0; col < row.length; col++) 3483 { 3484 if (null == row[col]) 3485 nulls[col] = true; 3486 else 3487 nulls[col] = false; 3488 output.writeBoolean(nulls[col]); 3490 } 3491 3492 for (int col = 0; col < row.length; col++) 3493 if (!nulls[col]) { 3495 try 3496 { 3497 3506 serializers[col].sendToStream(row[col], output); 3507 } 3508 catch (ClassCastException cce1) 3509 { 3510 ClassCastException cce2 = new ClassCastException ("Serializer " 3511 + serializers[col] + " failed on Java object: " + row[col] 3512 + " found in column: " + col + ", because of unexpected type " 3513 + row[col].getClass().getName()); 3514 cce2.initCause(cce1); 3515 throw cce2; 3516 } 3517 } 3519 } 3521 output.writeBoolean(this.hasMoreData); 3522 output.flush(); 3523 } 3524 3525 3531 protected void setStatement(Statement stmt) throws SQLException 3532 { 3533 owningStatement = stmt; 3534 fetchSize = stmt.getFetchSize(); 3535 resultSetConcurrency = stmt.getResultSetConcurrency(); 3536 resultSetType = stmt.getResultSetType(); 3537 } 3538 3539 3542 private void buildIndexMapping() 3543 { 3544 int numFields = nbOfColumns; 3545 3546 columnNameToIndex = new Hashtable (); 3547 fullColumnNameToIndex = new Hashtable (); 3548 3549 for (int i = 0; i < numFields; i++) 3550 { 3551 Integer index = new Integer (i); 3552 3553 String columnName = fields[i].getFieldName(); 3554 String fullColumnName = fields[i].getFullName(); 3555 if (columnName != null) 3556 { 3557 columnNameToIndex.put(fields[i].getFieldName(), index); 3558 columnNameToIndex.put(fields[i].getFieldName().toUpperCase(), index); 3559 columnNameToIndex.put(fields[i].getFieldName().toLowerCase(), index); 3560 } 3561 3562 if (fullColumnName != null) 3563 { 3564 fullColumnNameToIndex.put(fields[i].getFullName(), index); 3565 fullColumnNameToIndex.put(fields[i].getFullName().toUpperCase(), index); 3566 fullColumnNameToIndex.put(fields[i].getFullName().toLowerCase(), index); 3567 } 3568 } 3569 } 3570 3571 3576 private void buildDeleteStatement() throws SQLException 3577 { 3578 this.checkUpdatability(); 3580 3581 StringBuffer sb = new StringBuffer (); 3583 sb.append("DELETE FROM "); 3584 sb.append(fields[0].getTableName()); 3585 sb.append(" WHERE "); 3586 for (int i = 0; i < primaryKeyColumns.length; ++i) 3587 { 3588 if (i > 0) 3589 sb.append(" AND "); 3590 sb.append(primaryKeyColumns[i]); 3591 sb.append(" = ?"); 3592 } 3593 3594 deleteStatement = this.connection.prepareStatement(sb.toString()); 3596 } 3597 3598 3603 private void buildInsertStatement() throws SQLException 3604 { 3605 this.checkUpdatability(); 3607 3608 StringBuffer sb = new StringBuffer (); 3610 sb.append("INSERT INTO "); 3611 sb.append(fields[0].getTableName()); 3612 sb.append(" ("); 3613 for (int i = 0; i < fields.length; ++i) 3614 { 3615 if (i > 0) 3616 sb.append(", "); 3617 sb.append(fields[i].getFieldName()); 3618 } 3619 sb.append(") VALUES ("); 3620 for (int i = 0; i < fields.length; ++i) 3621 { 3622 if (i > 0) 3623 sb.append(", "); 3624 sb.append("?"); 3625 } 3626 sb.append(")"); 3627 3628 insertStatement = this.connection.prepareStatement(sb.toString()); 3630 } 3631 3632 3637 private void buildRefreshStatement() throws SQLException 3638 { 3639 this.checkUpdatability(); 3641 3642 StringBuffer sb = new StringBuffer (); 3644 sb.append("SELECT "); 3645 for (int i = 0; i < fields.length; ++i) 3646 { 3647 if (i > 0) 3648 sb.append(", "); 3649 sb.append(fields[i].getFieldName()); 3650 } 3651 sb.append(" FROM "); 3652 sb.append(fields[0].getTableName()); 3653 sb.append(" WHERE "); 3654 for (int i = 0; i < primaryKeyColumns.length; ++i) 3655 { 3656 if (i > 0) 3657 sb.append(" AND "); 3658 sb.append(primaryKeyColumns[i]); 3659 sb.append(" = ?"); 3660 } 3661 3662 refreshStatement = this.connection.prepareStatement(sb.toString()); 3664 } 3665 3666 3671 private void buildUpdateStatement() throws SQLException 3672 { 3673 this.checkUpdatability(); 3675 3676 StringBuffer sb = new StringBuffer (); 3678 sb.append("UPDATE "); 3679 sb.append(fields[0].getTableName()); 3680 sb.append(" SET "); 3681 for (int i = 0; i < fields.length; ++i) 3682 { 3683 if (i > 0) 3684 sb.append(", "); 3685 sb.append(fields[i].getFieldName()); 3686 sb.append(" = ?"); 3687 } 3688 sb.append(" WHERE "); 3689 for (int i = 0; i < primaryKeyColumns.length; ++i) 3690 { 3691 if (i > 0) 3692 sb.append(" AND "); 3693 sb.append(primaryKeyColumns[i]); 3694 sb.append(" = ?"); 3695 } 3696 3697 updateStatement = this.connection.prepareStatement(sb.toString()); 3699 } 3700 3701 3706 private void extractPrimaryKey() throws SQLException 3707 { 3708 ResultSet res = this.connection.getPrimaryKeys(null, null, fields[0].getTableName()); 3709 3710 try 3711 { 3712 primaryKeyColumns = new String [((DriverResultSet) res).nbOfRows]; 3713 while (res.next()) 3714 primaryKeyColumns[res.getRow() - 1] = res.getString(4); 3715 } 3716 finally 3717 { 3718 res.close(); 3720 } 3721 } 3722 3723 3728 private void checkUpdatability() throws SQLException 3729 { 3730 switch (resultSetConcurrency) 3732 { 3733 case ResultSet.CONCUR_READ_ONLY : 3734 throw new SQLException ("Cannot update ResultSet with " 3735 + "concurrency mode CONCUR_READ_ONLY"); 3736 case ResultSet.CONCUR_UPDATABLE : 3737 break; 3738 default : 3739 throw new SQLException ("Invalid ResultSet concurrency mode: " 3740 + resultSetConcurrency); 3741 } 3742 3743 String tableName = fields[0].getTableName(); 3745 for (int i = 1; i < nbOfColumns; ++i) 3746 if (!tableName.equals(fields[i].getTableName())) 3747 throw new SQLException (UPDATEABLE_MESSAGE); 3748 3749 if (primaryKeyColumns == null) 3751 this.extractPrimaryKey(); 3752 3753 for (int i = 0; i < primaryKeyColumns.length; ++i) 3755 try 3756 { 3757 findColumn(primaryKeyColumns[i]); 3758 } 3759 catch (SQLException e) 3760 { 3761 throw new SQLException (UPDATEABLE_MESSAGE); 3762 } 3763 } 3764 3765 3771 private void checkRowAndColPosAndSetNullFlag(int columnIndex) 3772 throws SQLException 3773 { 3774 checkIfClosed(); 3775 3776 if (!inserting) 3777 checkRowPos(); 3778 3779 if (fields == null) 3780 throw new java.sql.SQLException ("Query generated no fields for ResultSet"); 3781 3782 if (columnIndex < 1 || columnIndex > nbOfColumns) 3783 throw new java.sql.SQLException ("Column Index out of range ( " 3784 + columnIndex + " > " + nbOfColumns + ")."); 3785 3786 try 3787 { 3788 Object obj; 3789 if (inserting || updating) 3790 obj = tempRow[columnIndex - 1]; 3791 else 3792 obj = ((Object []) data.get(currentRow))[columnIndex - 1]; 3793 3794 if (obj == null) 3795 wasNullFlag = true; 3796 else 3797 wasNullFlag = false; 3798 } 3799 catch (NullPointerException e) 3800 { 3801 wasNullFlag = true; 3802 } 3803 } 3804 3805 3810 private void checkRowPos() throws SQLException 3811 { 3812 if (currentRow < 0) 3813 throw new SQLException ("Before start of result set"); 3814 3815 if (currentRow == nbOfRows) 3816 throw new SQLException ("After end of result set"); 3817 } 3818 3819 3825 private void checkUpdateFlagAndPrepareUpdateIfNeeded() throws SQLException 3826 { 3827 if (updating) 3828 return; 3829 3830 this.checkRowPos(); 3831 3832 if (updateStatement == null) 3833 this.buildUpdateStatement(); 3834 3835 tempRow = (Object []) ((Object []) data.get(currentRow)).clone(); 3836 3837 for (int i = 0; i < nbOfColumns; ++i) 3838 updateStatement.setObject(i + 1, tempRow[i]); 3839 3840 updating = true; 3841 } 3842 3843 3848 private void checkIfClosed() throws SQLException 3849 { 3850 if (isClosed) 3851 throw new SQLException ("Trying to access a closed ResultSet"); 3852 } 3853 3854 3857 3871 public java.net.URL getURL(int columnIndex) throws SQLException 3872 { 3873 checkRowAndColPosAndSetNullFlag(columnIndex); 3874 3875 if (wasNullFlag) 3876 return null; 3877 3878 if (inserting || updating) 3879 return (URL ) tempRow[columnIndex - 1]; 3880 else 3881 return (URL ) (((Object []) data.get(currentRow))[columnIndex - 1]); 3882 } 3883 3884 3897 public java.net.URL getURL(String columnName) throws SQLException 3898 { 3899 return getURL(findColumn(columnName)); 3900 } 3901 3902 3914 public void updateRef(int columnIndex, java.sql.Ref x) throws SQLException 3915 { 3916 throw new NotImplementedException("updateRef"); 3917 } 3918 3919 3931 public void updateRef(String columnName, java.sql.Ref x) throws SQLException 3932 { 3933 updateRef(findColumn(columnName), x); 3934 } 3935 3936 3948 public void updateBlob(int columnIndex, java.sql.Blob x) throws SQLException 3949 { 3950 throw new NotImplementedException("updateBlob"); 3951 } 3952 3953 3965 public void updateBlob(String columnName, java.sql.Blob x) 3966 throws SQLException 3967 { 3968 updateBlob(findColumn(columnName), x); 3969 } 3970 3971 3983 public void updateClob(int columnIndex, java.sql.Clob x) throws SQLException 3984 { 3985 throw new NotImplementedException("updateClob"); 3986 } 3987 3988 4000 public void updateClob(String columnName, java.sql.Clob x) 4001 throws SQLException 4002 { 4003 updateClob(findColumn(columnName), x); 4004 } 4005 4006 4018 public void updateArray(int columnIndex, java.sql.Array x) 4019 throws SQLException 4020 { 4021 throw new NotImplementedException("updateArray"); 4022 } 4023 4024 4036 public void updateArray(String columnName, java.sql.Array x) 4037 throws SQLException 4038 { 4039 updateArray(findColumn(columnName), x); 4040 } 4041 4042 4049 public byte getByte(int columnIndex) throws SQLException 4050 { 4051 checkRowAndColPosAndSetNullFlag(columnIndex); 4052 4053 if (wasNullFlag) 4054 return 0; 4055 4056 Object obj; 4057 if (inserting || updating) 4058 obj = tempRow[columnIndex - 1]; 4059 else 4060 obj = ((Object []) data.get(currentRow))[columnIndex - 1]; 4061 4062 if (obj instanceof Number ) 4063 { 4064 return ((Number ) obj).byteValue(); 4065 } 4066 4067 try 4069 { 4070 String string = obj.toString(); 4071 string = string.trim(); 4072 return Byte.parseByte(string); 4073 } 4074 catch (NumberFormatException e) 4075 { 4076 throw new SQLException ("the value " + obj.toString() 4077 + " is not a valid byte"); 4078 } 4079 } 4080 4081 4084 public String toString() 4085 { 4086 return nbOfRows + " rows - " + nbOfColumns + " columns - current row:" 4087 + currentRow + " - hasMoreData:" + hasMoreData + " - isClosed:" 4088 + isClosed; 4089 } 4090} | Popular Tags |