1 24 25 package org.continuent.sequoia.driver; 26 27 import java.io.ByteArrayInputStream ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.UnsupportedEncodingException ; 31 import java.math.BigDecimal ; 32 import java.math.BigInteger ; 33 import java.net.URL ; 34 import java.nio.CharBuffer ; 35 import java.nio.charset.CharacterCodingException ; 36 import java.nio.charset.Charset ; 37 import java.sql.Clob ; 38 import java.sql.PreparedStatement ; 39 import java.sql.Ref ; 40 import java.sql.ResultSet ; 41 import java.sql.SQLException ; 42 import java.sql.SQLWarning ; 43 import java.sql.Time ; 44 import java.sql.Timestamp ; 45 import java.util.ArrayList ; 46 import java.util.Calendar ; 47 import java.util.Hashtable ; 48 49 import org.continuent.sequoia.common.exceptions.NotImplementedException; 50 import org.continuent.sequoia.common.exceptions.ProtocolException; 51 import org.continuent.sequoia.common.exceptions.driver.DriverSQLException; 52 import org.continuent.sequoia.common.exceptions.driver.protocol.BackendDriverException; 53 import org.continuent.sequoia.common.protocol.ByteArrayBlob; 54 import org.continuent.sequoia.common.protocol.Field; 55 import org.continuent.sequoia.common.protocol.SQLDataSerialization; 56 import org.continuent.sequoia.common.protocol.StringClob; 57 import org.continuent.sequoia.common.protocol.TypeTag; 58 import org.continuent.sequoia.common.stream.DriverBufferedInputStream; 59 60 115 public class DriverResultSet 116 implements 117 java.sql.ResultSet , 118 java.io.Serializable , 119 java.lang.Cloneable 120 { 121 private static final long serialVersionUID = 7408879935608629886L; 122 123 124 protected int currentRow = -1; 125 126 protected int nbOfRows = -1; 127 128 protected int nbOfColumns = -1; 129 130 protected ArrayList data; 131 132 private boolean hasMoreData; 133 134 protected int fetchDirection = FETCH_FORWARD; 135 136 protected int fetchSize = 0; 137 138 private String cursorName; 139 140 141 protected Field[] fields; 142 143 144 private SQLDataSerialization.Serializer[] serializers; 145 146 147 protected boolean wasNullFlag = false; 148 149 protected transient Hashtable lowerNameToIndex = null; 150 151 protected transient Hashtable sensitiveNameToIndex = null; 152 153 154 protected int resultSetType = 0; 155 156 protected int resultSetConcurrency = 0; 157 158 protected BackendDriverException warnings = null; 159 160 protected SQLWarning statementWarnings = null; 161 162 protected transient Statement owningStatement; 163 164 private final Connection connection; 165 166 private boolean isClosed = true; 167 168 169 private transient PreparedStatement deleteStatement = null; 170 171 private transient PreparedStatement insertStatement = null; 172 173 private transient PreparedStatement refreshStatement = null; 174 175 private transient PreparedStatement updateStatement = null; 176 177 private transient boolean inserting = false; 178 179 private transient boolean updating = false; 180 181 private transient Object [] tempRow = null; 182 183 private transient String [] primaryKeyColumns = null; 184 185 private static final String UPDATEABLE_MESSAGE = "ResultSet not updateable. The " 186 + "query that generated this result set must select only one table, and must " 187 + "select all primary keys from that table. See the JDBC 2.1 API Specification, " 188 + "section 5.6 for more details."; 189 193 private InputStream currentStream = null; 194 195 199 211 public boolean next() throws java.sql.SQLException 212 { 213 checkIfClosed(); 214 215 if (inserting) 216 { 217 insertStatement.clearParameters(); 218 tempRow = null; 219 inserting = false; 220 } 221 222 if (updating) 223 cancelRowUpdates(); 224 225 if (nbOfRows == 0) 226 return false; 227 228 if (currentRow + 1 >= nbOfRows) 229 { 230 if (hasMoreData) 231 { 232 this.connection.tryFetchNext(cursorName, fetchSize); 234 try 236 { 237 receiveRows(); 238 } 239 catch (IOException ioe) 240 { 241 throw new DriverSQLException("I/O Error while fetching new rows:\n" 242 + ioe.getLocalizedMessage(), ioe); 243 } 244 currentRow = 0; 245 if (data == null) 246 { 247 nbOfRows = 0; 248 return false; 249 } 250 else 251 { 252 nbOfRows = data.size(); 253 return true; 254 } 255 } 256 257 currentRow = nbOfRows; 259 return false; 260 } 261 262 clearWarnings(); 263 currentRow++; 264 return true; 265 } 266 267 279 public boolean prev() throws SQLException 280 { 281 checkIfClosed(); 282 283 if (inserting) 284 { 285 insertStatement.clearParameters(); 286 tempRow = null; 287 inserting = false; 288 } 289 290 if (updating) 291 cancelRowUpdates(); 292 293 if (currentRow - 1 >= 0) 294 { 295 currentRow--; 296 return true; 297 } 298 299 return false; 300 } 301 302 312 public boolean isBeforeFirst() throws SQLException 313 { 314 checkIfClosed(); 315 if (nbOfRows == 0) 316 return false; 317 else 318 return (currentRow == -1); 319 } 320 321 331 public boolean isAfterLast() throws SQLException 332 { 333 checkIfClosed(); 334 if (nbOfRows == 0) 335 return false; 336 else 337 return (currentRow >= nbOfRows); 338 } 339 340 349 public boolean isFirst() throws SQLException 350 { 351 checkIfClosed(); 352 if (nbOfRows == 0) 353 return false; 354 else 355 return (currentRow == 0); 356 } 357 358 370 public boolean isLast() throws SQLException 371 { 372 checkIfClosed(); 373 if (nbOfRows == 0) 374 return false; 375 else 376 return (currentRow == nbOfRows - 1); 377 } 378 379 388 public void beforeFirst() throws SQLException 389 { 390 checkIfScrollable(); 391 392 if (inserting) 393 { 394 insertStatement.clearParameters(); 395 tempRow = null; 396 inserting = false; 397 } 398 399 if (updating) 400 cancelRowUpdates(); 401 402 currentRow = -1; 403 } 404 405 414 public void afterLast() throws SQLException 415 { 416 checkIfScrollable(); 417 418 if (inserting) 419 { 420 insertStatement.clearParameters(); 421 tempRow = null; 422 inserting = false; 423 } 424 425 if (updating) 426 cancelRowUpdates(); 427 428 if (nbOfRows != 0) 429 currentRow = nbOfRows; 430 } 431 432 442 public boolean first() throws SQLException 443 { 444 checkIfScrollable(); 445 446 if (inserting) 447 { 448 insertStatement.clearParameters(); 449 tempRow = null; 450 inserting = false; 451 } 452 453 if (updating) 454 cancelRowUpdates(); 455 456 if (nbOfRows == 0) 457 return false; 458 459 currentRow = 0; 460 return true; 461 } 462 463 473 public boolean last() throws SQLException 474 { 475 checkIfScrollable(); 476 477 if (inserting) 478 { 479 insertStatement.clearParameters(); 480 tempRow = null; 481 inserting = false; 482 } 483 484 if (updating) 485 cancelRowUpdates(); 486 487 if (nbOfRows == 0) 488 return false; 489 490 currentRow = nbOfRows - 1; 491 return true; 492 } 493 494 503 public int getRow() throws SQLException 504 { 505 checkIfClosed(); 506 if (currentRow < 0 || currentRow >= nbOfRows || nbOfRows == 0) 507 return 0; 508 else 509 return currentRow + 1; 510 } 511 512 535 public boolean absolute(int row) throws SQLException 536 { 537 checkIfScrollable(); 538 539 if (inserting) 540 { 541 insertStatement.clearParameters(); 542 tempRow = null; 543 inserting = false; 544 } 545 546 if (updating) 547 cancelRowUpdates(); 548 549 if (nbOfRows == 0) 550 return false; 551 552 if (row == 0) 553 { 554 beforeFirst(); 555 return false; 556 } 557 558 if (row == 1) 559 return first(); 560 561 if (row == -1) 562 return last(); 563 564 if (row > nbOfRows) 565 { 566 afterLast(); 567 return false; 568 } 569 570 if (row < 0) 571 { int newRowPosition = nbOfRows + row + 1; 573 574 if (newRowPosition <= 0) 575 { 576 beforeFirst(); 577 return false; 578 } 579 580 return absolute(newRowPosition); 581 } 582 else 583 { 584 row--; currentRow = row; 586 return true; 587 } 588 } 589 590 608 public boolean relative(int rows) throws SQLException 609 { 610 checkIfScrollable(); 611 612 if (inserting) 613 { 614 insertStatement.clearParameters(); 615 tempRow = null; 616 inserting = false; 617 } 618 619 if (updating) 620 cancelRowUpdates(); 621 622 if (nbOfRows == 0) 623 return false; 624 625 return absolute(currentRow + rows + 1); 626 } 627 628 640 public boolean previous() throws SQLException 641 { 642 checkIfScrollable(); 643 return prev(); 644 } 645 646 658 public void setFetchDirection(int direction) throws SQLException 659 { 660 if (direction != FETCH_FORWARD && direction != FETCH_REVERSE) 661 throw new SQLException ("Illegal value for fetch direction"); 662 663 if (resultSetType == ResultSet.TYPE_FORWARD_ONLY 664 && direction != FETCH_FORWARD) 665 throw new SQLException ( 666 "Operation requires a scrollable ResultSet, but this ResultSet is FORWARD_ONLY"); 667 668 fetchDirection = direction; 669 } 670 671 677 public int getFetchDirection() throws SQLException 678 { 679 return fetchDirection; 680 } 681 682 692 public void setFetchSize(int rows) throws SQLException 693 { 694 if (rows < 0) 697 throw new SQLException ("Value must be between 0 and getMaxRows()"); 698 699 fetchSize = rows; 700 } 701 702 708 public int getFetchSize() throws SQLException 709 { 710 return fetchSize; 711 } 712 713 719 726 public String getString(int columnIndex) throws SQLException 727 { 728 checkRowAndColPosAndSetNullFlag(columnIndex); 729 730 if (wasNullFlag) 731 return null; 732 733 if (inserting || updating) 734 return tempRow[columnIndex - 1].toString(); 735 else 736 { 737 Object tmpData = (((Object []) data.get(currentRow))[columnIndex - 1]); 738 if (tmpData instanceof byte[]) 739 { 740 String encoding = this.fields[columnIndex - 1].getEncoding(); 741 if (encoding != null) 742 try 743 { 744 return new String ((byte[]) tmpData, encoding); 745 } 746 catch (UnsupportedEncodingException e) 747 { 748 throw (SQLException ) new SQLException ( 749 "UnsupportedEncodingException (to " + encoding + ") : " 750 + e.getMessage()).initCause(e); 751 } 752 else 753 return new String ((byte[]) tmpData); 754 } 755 return tmpData.toString(); 756 } 757 } 758 759 766 public boolean getBoolean(int columnIndex) throws SQLException 767 { 768 checkRowAndColPosAndSetNullFlag(columnIndex); 769 770 if (wasNullFlag) 771 return false; 772 773 Object object; 774 if (inserting || updating) 775 object = tempRow[columnIndex - 1]; 776 else 777 object = (((Object []) data.get(currentRow))[columnIndex - 1]); 778 779 String stringVal = object.toString(); 780 if ((stringVal != null) && (stringVal.length() > 0)) 781 { 782 stringVal = stringVal.toLowerCase(); 783 784 if ("t".equals(stringVal)) 785 { 786 return true; 787 } 788 else if ("f".equals(stringVal)) 789 { 790 return false; 791 } 792 else if ("true".equals(stringVal)) 793 { 794 return true; 795 } 796 else if ("false".equals(stringVal)) 797 { 798 return false; 799 } 800 else if ("1".equals(stringVal)) 801 { 802 return true; 803 } 804 else if ("0".equals(stringVal)) 805 { 806 return false; 807 } 808 else if ("y".equals(stringVal)) 809 { 810 return true; 811 } 812 else if ("n".equals(stringVal)) 813 { 814 return false; 815 } 816 else if ("yes".equals(stringVal)) 817 { 818 return true; 819 } 820 else if ("no".equals(stringVal)) 821 { 822 return false; 823 } 824 else if (object instanceof Number ) 825 { 826 int value = ((Number ) object).intValue(); 827 if (value == 0) 828 return false; 829 else if (value == 1) 830 return true; 831 } 833 834 throw new SQLException ("column value " + stringVal + " of type: " 836 + object.getClass() + " could not be converted to boolean"); 837 } 838 else 839 { 840 return false; 841 } 842 } 843 844 851 public short getShort(int columnIndex) throws SQLException 852 { 853 checkRowAndColPosAndSetNullFlag(columnIndex); 854 855 if (wasNullFlag) 856 return 0; 857 858 Object obj; 859 if (inserting || updating) 860 obj = tempRow[columnIndex - 1]; 861 else 862 obj = ((Object []) data.get(currentRow))[columnIndex - 1]; 863 864 if (obj instanceof Number ) 865 { 866 return ((Number ) obj).shortValue(); 867 } 868 869 try 871 { 872 String string = obj.toString(); 873 string = string.trim(); 874 return Short.parseShort(string); 875 } 876 catch (NumberFormatException e) 877 { 878 String string = obj.toString().toLowerCase().trim(); 880 if ("true".equals(string)) 881 return 1; 882 else if ("false".equals(string)) 883 return 0; 884 throw new SQLException ("the value " + obj.toString() 885 + " is not a valid short number"); 886 } 887 } 888 889 896 public int getInt(int columnIndex) throws SQLException 897 { 898 checkRowAndColPosAndSetNullFlag(columnIndex); 899 900 if (wasNullFlag) 901 return 0; 902 903 Object obj; 904 if (inserting || updating) 905 obj = tempRow[columnIndex - 1]; 906 else 907 obj = ((Object []) data.get(currentRow))[columnIndex - 1]; 908 909 if (obj instanceof Number ) 910 { 911 return ((Number ) obj).intValue(); 912 } 913 914 try 916 { 917 String string = obj.toString(); 918 string = string.trim(); 919 return Integer.parseInt(string); 920 } 921 catch (NumberFormatException e) 922 { 923 String string = obj.toString().toLowerCase().trim(); 925 if ("true".equals(string)) 926 return 1; 927 else if ("false".equals(string)) 928 return 0; 929 throw new SQLException ("the value " + obj.toString() 930 + " is not a valid int number"); 931 } 932 } 933 934 941 public long getLong(int columnIndex) throws SQLException 942 { 943 checkRowAndColPosAndSetNullFlag(columnIndex); 944 945 if (wasNullFlag) 946 return 0; 947 948 Object obj; 949 if (inserting || updating) 950 obj = tempRow[columnIndex - 1]; 951 else 952 obj = ((Object []) data.get(currentRow))[columnIndex - 1]; 953 954 if (obj instanceof Number ) 955 { 956 return ((Number ) obj).longValue(); 957 } 958 959 try 961 { 962 String string = obj.toString(); 963 string = string.trim(); 964 return Long.parseLong(string); 965 } 966 catch (NumberFormatException e) 967 { 968 String string = obj.toString().toLowerCase().trim(); 970 if ("true".equals(string)) 971 return 1; 972 else if ("false".equals(string)) 973 return 0; 974 throw new SQLException ("the value " + obj.toString() 975 + " is not a valid long number"); 976 } 977 } 978 979 986 public float getFloat(int columnIndex) throws SQLException 987 { 988 checkRowAndColPosAndSetNullFlag(columnIndex); 989 990 if (wasNullFlag) 991 return 0; 992 993 Object obj; 994 if (inserting || updating) 995 obj = tempRow[columnIndex - 1]; 996 else 997 obj = ((Object []) data.get(currentRow))[columnIndex - 1]; 998 999 if (obj instanceof Number ) 1000 { 1001 return ((Number ) obj).floatValue(); 1002 } 1003 1004 try 1006 { 1007 String string = obj.toString(); 1008 string = string.trim(); 1009 return Float.parseFloat(string); 1010 } 1011 catch (NumberFormatException e) 1012 { 1013 String string = obj.toString().toLowerCase().trim(); 1015 if ("true".equals(string)) 1016 return 1; 1017 else if ("false".equals(string)) 1018 return 0; 1019 throw new SQLException ("the value " + obj.toString() 1020 + " is not a valid float number"); 1021 } 1022 } 1023 1024 1031 public double getDouble(int columnIndex) throws SQLException 1032 { 1033 checkRowAndColPosAndSetNullFlag(columnIndex); 1034 1035 if (wasNullFlag) 1036 return 0; 1037 1038 Object obj; 1039 if (inserting || updating) 1040 obj = tempRow[columnIndex - 1]; 1041 else 1042 obj = ((Object []) data.get(currentRow))[columnIndex - 1]; 1043 1044 if (obj instanceof Number ) 1045 { 1046 return ((Number ) obj).doubleValue(); 1047 } 1048 1049 try 1051 { 1052 String string = obj.toString(); 1053 string = string.trim(); 1054 return Double.parseDouble(string); 1055 } 1056 catch (NumberFormatException e) 1057 { 1058 String string = obj.toString().toLowerCase().trim(); 1060 if ("true".equals(string)) 1061 return 1; 1062 else if ("false".equals(string)) 1063 return 0; 1064 throw new SQLException ("the value " + obj.toString() 1065 + " is not a valid double number"); 1066 } 1067 } 1068 1069 1079 public BigDecimal getBigDecimal(int columnIndex, int scale) 1080 throws SQLException 1081 { 1082 BigDecimal bigDecimal = getBigDecimal(columnIndex); 1083 if (bigDecimal == null) 1084 return null; 1085 else 1086 return bigDecimal.setScale(scale); 1087 } 1088 1089 1098 public byte[] getBytes(int columnIndex) throws SQLException 1099 { 1100 checkRowAndColPosAndSetNullFlag(columnIndex); 1101 1102 if (wasNullFlag) 1103 return null; 1104 1105 Object o; 1106 if (inserting || updating) 1107 o = tempRow[columnIndex - 1]; 1108 else 1109 o = ((Object []) data.get(currentRow))[columnIndex - 1]; 1110 1111 byte[] maybeEncodedBytes; 1112 if (o instanceof byte[]) 1113 { 1114 maybeEncodedBytes = (byte[]) o; 1115 } 1116 else if (o instanceof ByteArrayBlob) 1117 { 1118 ByteArrayBlob b = (ByteArrayBlob) o; 1119 maybeEncodedBytes = b.getBytes(1, (int) b.length()); 1121 } 1122 else 1123 { 1124 throw new NotImplementedException("in getBytes(" + columnIndex 1125 + "), don't know how to convert type " + o.getClass().getName() 1126 + " into a byte array "); 1127 } 1128 1129 return maybeEncodedBytes; } 1131 1132 1139 public java.sql.Date getDate(int columnIndex) throws SQLException 1140 { 1141 checkRowAndColPosAndSetNullFlag(columnIndex); 1142 1143 if (wasNullFlag) 1144 return null; 1145 1146 String dateString; 1150 if (inserting || updating) 1151 dateString = tempRow[columnIndex - 1].toString(); 1152 else 1153 dateString = ((Object []) data.get(currentRow))[columnIndex - 1] 1154 .toString(); 1155 1156 if (dateString.length() == 10) 1157 return java.sql.Date.valueOf(dateString); 1158 else 1159 return java.sql.Date.valueOf(dateString.substring(0, 10)); 1160 } 1161 1162 1169 public Time getTime(int columnIndex) throws SQLException 1170 { 1171 checkRowAndColPosAndSetNullFlag(columnIndex); 1172 1173 if (wasNullFlag) 1174 return null; 1175 1176 Object obj; 1177 if (inserting || updating) 1178 obj = tempRow[columnIndex - 1]; 1179 else 1180 obj = ((Object []) data.get(currentRow))[columnIndex - 1]; 1181 1182 if (obj instanceof java.util.Date ) 1183 { 1184 java.util.Date time = (java.util.Date ) obj; 1185 String timeString = time.toString(); 1188 if (timeString.length() == 8) 1189 { 1190 return new Time (time.getTime()); 1193 } 1194 1195 Calendar cal = Calendar.getInstance(); 1198 cal.setTime(time); 1199 cal.clear(Calendar.YEAR); 1200 cal.clear(Calendar.MONTH); 1201 cal.clear(Calendar.DATE); 1202 cal.clear(Calendar.MILLISECOND); 1203 return new Time (cal.getTimeInMillis()); 1204 } 1205 1206 try 1208 { 1209 String string = obj.toString(); 1210 string = string.trim(); 1211 return Time.valueOf(string); 1212 } 1213 catch (IllegalArgumentException e) 1214 { 1215 throw new SQLException ("the value " + obj.toString() 1216 + " is not a valid time"); 1217 } 1218 } 1219 1220 1227 public Timestamp getTimestamp(int columnIndex) throws SQLException 1228 { 1229 checkRowAndColPosAndSetNullFlag(columnIndex); 1230 1231 if (wasNullFlag) 1232 return null; 1233 1234 Object obj; 1235 if (inserting || updating) 1236 obj = tempRow[columnIndex - 1]; 1237 else 1238 obj = ((Object []) data.get(currentRow))[columnIndex - 1]; 1239 1240 if (obj instanceof java.util.Date ) 1241 { 1242 return new Timestamp (((java.util.Date ) obj).getTime()); 1243 } 1244 1245 try 1247 { 1248 String string = obj.toString(); 1249 string = string.trim(); 1250 return Timestamp.valueOf(string); 1251 } 1252 catch (IllegalArgumentException e) 1253 { 1254 throw new SQLException ("the value " + obj.toString() 1255 + " is not a valid timestamp"); 1256 } 1257 } 1258 1259 1277 public InputStream getAsciiStream(int columnIndex) throws SQLException 1278 { 1279 checkRowAndColPosAndSetNullFlag(columnIndex); 1280 1281 try 1282 { currentStream = new ByteArrayInputStream (Charset.forName("US-ASCII") 1285 .newEncoder().encode(CharBuffer.wrap(getString(columnIndex))).array()); 1286 } 1287 catch (CharacterCodingException e) 1288 { 1289 throw (SQLException ) new SQLException ( 1290 "Caught CharacterCodingException (data is probably not ASCII): " 1291 + e.getMessage()).initCause(e); 1292 } 1293 return currentStream; 1294 } 1295 1296 1309 public InputStream getUnicodeStream(int columnIndex) throws SQLException 1310 { 1311 checkRowAndColPosAndSetNullFlag(columnIndex); 1312 1313 currentStream = new ByteArrayInputStream (getString(columnIndex).getBytes()); 1314 return currentStream; 1315 } 1316 1317 1328 1329 public InputStream getBinaryStream(int columnIndex) throws SQLException 1330 { 1331 checkRowAndColPosAndSetNullFlag(columnIndex); 1332 1333 byte[] b = getBytes(columnIndex); 1334 if (b != null) 1335 { 1336 currentStream = new ByteArrayInputStream (b); 1337 return currentStream; 1338 } 1339 else 1340 return null; } 1342 1343 1351 public String getString(String columnName) throws SQLException 1352 { 1353 return this.getString(findColumn(columnName)); 1354 } 1355 1356 1359 public boolean getBoolean(String columnName) throws SQLException 1360 { 1361 return getBoolean(findColumn(columnName)); 1362 } 1363 1364 1367 public byte getByte(String columnName) throws SQLException 1368 { 1369 return getByte(findColumn(columnName)); 1370 } 1371 1372 1375 public short getShort(String columnName) throws SQLException 1376 { 1377 return getShort(findColumn(columnName)); 1378 } 1379 1380 1383 public int getInt(String columnName) throws SQLException 1384 { 1385 return getInt(findColumn(columnName)); 1386 } 1387 1388 1391 public long getLong(String columnName) throws SQLException 1392 { 1393 return getLong(findColumn(columnName)); 1394 } 1395 1396 1399 public float getFloat(String columnName) throws SQLException 1400 { 1401 return getFloat(findColumn(columnName)); 1402 } 1403 1404 1407 public double getDouble(String columnName) throws SQLException 1408 { 1409 return getDouble(findColumn(columnName)); 1410 } 1411 1412 1416 public BigDecimal getBigDecimal(String columnName, int scale) 1417 throws SQLException 1418 { 1419 return getBigDecimal(findColumn(columnName), scale); 1420 } 1421 1422 1425 public byte[] getBytes(String columnName) throws SQLException 1426 { 1427 return getBytes(findColumn(columnName)); 1428 } 1429 1430 1433 public java.sql.Date getDate(String columnName) throws SQLException 1434 { 1435 return getDate(findColumn(columnName)); 1436 } 1437 1438 1441 public Time getTime(String columnName) throws SQLException 1442 { 1443 return getTime(findColumn(columnName)); 1444 } 1445 1446 1449 public Timestamp getTimestamp(String columnName) throws SQLException 1450 { 1451 return getTimestamp(findColumn(columnName)); 1452 } 1453 1454 1457 public InputStream getAsciiStream(String columnName) throws SQLException 1458 { 1459 return getAsciiStream(findColumn(columnName)); 1460 } 1461 1462 1466 public InputStream getUnicodeStream(String columnName) throws SQLException 1467 { 1468 return getUnicodeStream(findColumn(columnName)); 1469 } 1470 1471 1474 public InputStream getBinaryStream(String columnName) throws SQLException 1475 { 1476 return getBinaryStream(findColumn(columnName)); 1477 } 1478 1479 1493 public java.sql.SQLWarning getWarnings() throws SQLException 1494 { 1495 checkIfClosed(); 1496 return connection.convertToSQLWarnings(warnings); 1497 } 1498 1499 1505 1506 public void clearWarnings() throws SQLException 1507 { 1508 warnings = null; 1509 } 1510 1511 SQLWarning getStatementWarnings() 1512 { 1513 return statementWarnings; 1514 } 1515 1516 void setStatementWarnings(SQLWarning sqlw) 1517 { 1518 statementWarnings = sqlw; 1519 } 1520 1521 1524 public String getCursorName() throws SQLException 1525 { 1526 return cursorName; 1527 } 1528 1529 1543 public Object getObject(int columnIndex) throws SQLException 1544 { 1545 checkRowAndColPosAndSetNullFlag(columnIndex); 1546 1547 if (wasNullFlag) 1548 return null; 1549 1550 Object o; 1551 if (inserting || updating) 1552 o = tempRow[columnIndex]; 1553 else 1554 o = ((Object []) data.get(currentRow))[columnIndex - 1]; 1555 return o; 1556 } 1557 1558 1572 public Object getObject(String columnName) throws SQLException 1573 { 1574 return getObject(findColumn(columnName)); 1575 } 1576 1577 1579 1582 public java.io.Reader getCharacterStream(int columnIndex) throws SQLException 1583 { 1584 String s = getString(columnIndex); 1585 if (s == null) 1586 return null; 1587 char[] content = s.toCharArray(); 1588 return new java.io.CharArrayReader (content); 1589 } 1590 1591 1594 public java.io.Reader getCharacterStream(String columnName) 1595 throws SQLException 1596 { 1597 return getCharacterStream(findColumn(columnName)); 1598 } 1599 1600 1609 public BigDecimal getBigDecimal(int columnIndex) throws SQLException 1610 { 1611 checkRowAndColPosAndSetNullFlag(columnIndex); 1612 1613 if (wasNullFlag) 1614 return null; 1615 1616 Object obj; 1617 if (inserting || updating) 1618 obj = tempRow[columnIndex - 1]; 1619 else 1620 obj = ((Object []) data.get(currentRow))[columnIndex - 1]; 1621 1622 if (obj instanceof BigDecimal ) 1623 return (BigDecimal ) obj; 1624 else if (obj instanceof Long ) 1625 { 1626 return new BigDecimal (((Long ) obj).longValue()); 1627 } 1628 else if (obj instanceof BigInteger ) 1629 { 1630 return new BigDecimal (((BigInteger ) obj)); 1631 } 1632 else if (obj instanceof Short ) 1633 { 1634 return new BigDecimal (((Short ) obj).shortValue()); 1635 } 1636 else if (obj instanceof Integer ) 1637 { 1638 return new BigDecimal (((Integer ) obj).intValue()); 1639 } 1640 else if (obj instanceof String ) 1641 { 1642 return new BigDecimal ((String ) obj); 1643 } 1644 else if (obj instanceof Number ) 1645 { 1646 1658 return new BigDecimal (obj.toString()); 1663 } 1664 else if (obj instanceof Boolean ) 1665 { 1666 String string = obj.toString().toLowerCase().trim(); 1668 if ("true".equals(string)) 1669 return new BigDecimal (1); 1670 else if ("false".equals(string)) 1671 return new BigDecimal (0); 1672 throw new SQLException ("Type " + obj.getClass().getName() 1673 + " is not compatible with BigDecimal"); 1674 1675 } 1676 else 1677 { 1678 throw new SQLException ("Type " + obj.getClass().getName() 1679 + " is not compatible with BigDecimal"); 1680 } 1681 } 1682 1683 1686 public BigDecimal getBigDecimal(String columnName) throws SQLException 1687 { 1688 return getBigDecimal(findColumn(columnName)); 1689 } 1690 1691 1701 public Object getObject(int i, java.util.Map map) throws SQLException 1702 { 1703 throw new NotImplementedException("getObject(int, java.util.Map)"); 1704 } 1705 1706 1713 public java.sql.Ref getRef(int i) throws SQLException 1714 { 1715 checkRowAndColPosAndSetNullFlag(i); 1716 1717 if (wasNullFlag) 1718 return null; 1719 1720 if (inserting || updating) 1721 return (Ref ) tempRow[i - 1]; 1722 else 1723 return (Ref ) (((Object []) data.get(currentRow))[i - 1]); 1724 } 1725 1726 1733 public java.sql.Blob getBlob(int columnIndex) throws SQLException 1734 { 1735 checkRowAndColPosAndSetNullFlag(columnIndex); 1736 1737 if (wasNullFlag) 1738 return null; 1739 1740 Object o; 1741 if (inserting || updating) 1742 o = tempRow[columnIndex - 1]; 1743 else 1744 o = (((Object []) data.get(currentRow))[columnIndex - 1]); 1745 1746 if (o instanceof ByteArrayBlob || o instanceof byte[]) 1748 return new ByteArrayBlob(getBytes(columnIndex)); 1749 else 1750 throw new NotImplementedException("in getBlob(" + columnIndex 1751 + "), don't know how to convert type " + o.getClass().getName() 1752 + " into a Blob"); 1753 } 1754 1755 1762 public java.sql.Clob getClob(int columnIndex) throws SQLException 1763 { 1764 checkRowAndColPosAndSetNullFlag(columnIndex); 1765 1766 if (wasNullFlag) 1767 return null; 1768 1769 Object o; 1770 if (inserting || updating) 1771 o = tempRow[columnIndex - 1]; 1772 else 1773 o = (((Object []) data.get(currentRow))[columnIndex - 1]); 1774 1775 if (o instanceof String ) 1778 return new StringClob((String ) o); 1779 else if (o instanceof Clob ) 1780 return (java.sql.Clob ) o; 1781 else 1782 return new StringClob(new String (o.toString())); 1784 } 1785 1786 1793 public java.sql.Array getArray(int columnIndex) throws SQLException 1794 { 1795 checkRowAndColPosAndSetNullFlag(columnIndex); 1796 1797 if (wasNullFlag) 1798 return null; 1799 1800 if (inserting || updating) 1801 return (java.sql.Array ) tempRow[columnIndex - 1]; 1802 else 1803 return (java.sql.Array ) (((Object []) data.get(currentRow))[columnIndex - 1]); 1804 } 1805 1806 1816 public Object getObject(String colName, java.util.Map map) 1817 throws SQLException 1818 { 1819 return getObject(findColumn(colName), map); 1820 } 1821 1822 1829 public java.sql.Ref getRef(String colName) throws SQLException 1830 { 1831 return getRef(findColumn(colName)); 1832 } 1833 1834 1841 public java.sql.Blob getBlob(String colName) throws SQLException 1842 { 1843 return getBlob(findColumn(colName)); 1844 } 1845 1846 1853 public java.sql.Clob getClob(String colName) throws SQLException 1854 { 1855 return getClob(findColumn(colName)); 1856 } 1857 1858 1865 public java.sql.Array getArray(String colName) throws SQLException 1866 { 1867 return getArray(findColumn(colName)); 1868 } 1869 1870 1880 public java.sql.Date getDate(int columnIndex, Calendar cal) 1881 throws SQLException 1882 { 1883 return getDate(columnIndex); 1884 } 1885 1886 1896 public java.sql.Date getDate(String columnName, Calendar cal) 1897 throws SQLException 1898 { 1899 return getDate(findColumn(columnName), cal); 1900 } 1901 1902 1912 public java.sql.Time getTime(int columnIndex, Calendar cal) 1913 throws SQLException 1914 { 1915 return getTime(columnIndex); 1916 } 1917 1918 1928 public java.sql.Time getTime(String columnName, Calendar cal) 1929 throws SQLException 1930 { 1931 return getTime(findColumn(columnName), cal); 1932 } 1933 1934 1945 public java.sql.Timestamp getTimestamp(int columnIndex, Calendar cal) 1946 throws SQLException 1947 { 1948 return getTimestamp(columnIndex); 1949 } 1950 1951 1962 public java.sql.Timestamp getTimestamp(String columnName, Calendar cal) 1963 throws SQLException 1964 { 1965 return getTimestamp(findColumn(columnName), cal); 1966 } 1967 1968 1972 1981 public boolean rowUpdated() throws SQLException 1982 { 1983 throw new NotImplementedException("rowUpdated"); 1984 } 1985 1986 1994 public boolean rowInserted() throws SQLException 1995 { 1996 throw new NotImplementedException("rowInserted"); 1997 } 1998 1999 2009 public boolean rowDeleted() throws SQLException 2010 { 2011 throw new NotImplementedException("rowDeleted"); 2012 } 2013 2014 2023 public void updateNull(int columnIndex) throws SQLException 2024 { 2025 checkIfClosed(); 2026 2027 if (inserting) 2028 insertStatement 2029 .setNull(columnIndex, fields[columnIndex - 1].getSqlType()); 2030 else 2031 { 2032 checkUpdateFlagAndPrepareUpdateIfNeeded(); 2033 updateStatement 2034 .setNull(columnIndex, fields[columnIndex - 1].getSqlType()); 2035 } 2036 2037 tempRow[columnIndex - 1] = null; 2038 } 2039 2040 2050 2051 public void updateBoolean(int columnIndex, boolean x) throws SQLException 2052 { 2053 checkIfClosed(); 2054 2055 if (inserting) 2056 insertStatement.setBoolean(columnIndex, x); 2057 else 2058 { 2059 checkUpdateFlagAndPrepareUpdateIfNeeded(); 2060 updateStatement.setBoolean(columnIndex, x); 2061 } 2062 2063 tempRow[columnIndex - 1] = Boolean.valueOf(x); 2064 } 2065 2066 2076 public void updateByte(int columnIndex, byte x) throws SQLException 2077 { 2078 checkIfClosed(); 2079 2080 if (inserting) 2081 insertStatement.setByte(columnIndex, x); 2082 else 2083 { 2084 checkUpdateFlagAndPrepareUpdateIfNeeded(); 2085 updateStatement.setByte(columnIndex, x); 2086 } 2087 2088 tempRow[columnIndex - 1] = new Byte (x); 2089 } 2090 2091 2101 public void updateShort(int columnIndex, short x) throws SQLException 2102 { 2103 checkIfClosed(); 2104 2105 if (inserting) 2106 insertStatement.setShort(columnIndex, x); 2107 else 2108 { 2109 checkUpdateFlagAndPrepareUpdateIfNeeded(); 2110 updateStatement.setShort(columnIndex, x); 2111 } 2112 2113 tempRow[columnIndex - 1] = new Short (x); 2114 } 2115 2116 2126 public void updateInt(int columnIndex, int x) throws SQLException 2127 { 2128 checkIfClosed(); 2129 2130 if (inserting) 2131 insertStatement.setInt(columnIndex, x); 2132 else 2133 { 2134 checkUpdateFlagAndPrepareUpdateIfNeeded(); 2135 updateStatement.setInt(columnIndex, x); 2136 } 2137 2138 tempRow[columnIndex - 1] = new Integer (x); 2139 } 2140 2141 2151 public void updateLong(int columnIndex, long x) throws SQLException 2152 { 2153 checkIfClosed(); 2154 2155 if (inserting) 2156 insertStatement.setLong(columnIndex, x); 2157 else 2158 { 2159 checkUpdateFlagAndPrepareUpdateIfNeeded(); 2160 updateStatement.setLong(columnIndex, x); 2161 } 2162 2163 tempRow[columnIndex - 1] = new Long (x); 2164 } 2165 2166 2176 public void updateFloat(int columnIndex, float x) throws SQLException 2177 { 2178 checkIfClosed(); 2179 2180 if (inserting) 2181 insertStatement.setFloat(columnIndex, x); 2182 else 2183 { 2184 checkUpdateFlagAndPrepareUpdateIfNeeded(); 2185 updateStatement.setFloat(columnIndex, x); 2186 } 2187 2188 tempRow[columnIndex - 1] = new Float (x); 2189 } 2190 2191 2201 public void updateDouble(int columnIndex, double x) throws SQLException 2202 { 2203 checkIfClosed(); 2204 2205 if (inserting) 2206 insertStatement.setDouble(columnIndex, x); 2207 else 2208 { 2209 checkUpdateFlagAndPrepareUpdateIfNeeded(); 2210 updateStatement.setDouble(columnIndex, x); 2211 } 2212 2213 tempRow[columnIndex - 1] = new Double (x); 2214 } 2215 2216 2226 public void updateBigDecimal(int columnIndex, BigDecimal x) 2227 throws SQLException 2228 { 2229 checkIfClosed(); 2230 2231 if (inserting) 2232 insertStatement.setBigDecimal(columnIndex, x); 2233 else 2234 { 2235 checkUpdateFlagAndPrepareUpdateIfNeeded(); 2236 updateStatement.setBigDecimal(columnIndex, x); 2237 } 2238 2239 tempRow[columnIndex - 1] = x; 2240 } 2241 2242 2252 public void updateString(int columnIndex, String x) throws SQLException 2253 { 2254 checkIfClosed(); 2255 2256 if (inserting) 2257 insertStatement.setString(columnIndex, x); 2258 else 2259 { 2260 checkUpdateFlagAndPrepareUpdateIfNeeded(); 2261 updateStatement.setString(columnIndex, x); 2262 } 2263 2264 tempRow[columnIndex - 1] = x; 2265 } 2266 2267 2277 public void updateBytes(int columnIndex, byte[] x) throws SQLException 2278 { 2279 checkIfClosed(); 2280 2281 if (inserting) 2282 insertStatement.setBytes(columnIndex, x); 2283 else 2284 { 2285 checkUpdateFlagAndPrepareUpdateIfNeeded(); 2286 updateStatement.setBytes(columnIndex, x); 2287 } 2288 2289 tempRow[columnIndex - 1] = x; 2290 } 2291 2292 2302 public void updateDate(int columnIndex, java.sql.Date x) throws SQLException 2303 { 2304 checkIfClosed(); 2305 2306 if (inserting) 2307 insertStatement.setDate(columnIndex, x); 2308 else 2309 { 2310 checkUpdateFlagAndPrepareUpdateIfNeeded(); 2311 updateStatement.setDate(columnIndex, x); 2312 } 2313 2314 tempRow[columnIndex - 1] = x; 2315 } 2316 2317 2327 public void updateTime(int columnIndex, java.sql.Time x) throws SQLException 2328 { 2329 checkIfClosed(); 2330 2331 if (inserting) 2332 insertStatement.setTime(columnIndex, x); 2333 else 2334 { 2335 checkUpdateFlagAndPrepareUpdateIfNeeded(); 2336 updateStatement.setTime(columnIndex, x); 2337 } 2338 2339 tempRow[columnIndex - 1] = x; 2340 } 2341 2342 2352 public void updateTimestamp(int columnIndex, java.sql.Timestamp x) 2353 throws SQLException 2354 { 2355 checkIfClosed(); 2356 2357 if (inserting) 2358 insertStatement.setTimestamp(columnIndex, x); 2359 else 2360 { 2361 checkUpdateFlagAndPrepareUpdateIfNeeded(); 2362 updateStatement.setTimestamp(columnIndex, x); 2363 } 2364 2365 tempRow[columnIndex - 1] = x; 2366 } 2367 2368 2379 public void updateAsciiStream(int columnIndex, java.io.InputStream x, 2380 int length) throws SQLException 2381 { 2382 this.updateBinaryStream(columnIndex, x, length); 2383 } 2384 2385 2396 public void updateBinaryStream(int columnIndex, java.io.InputStream x, 2397 int length) throws SQLException 2398 { 2399 checkIfClosed(); 2400 2401 byte[] content = new byte[length]; 2402 try 2403 { 2404 x.read(content, 0, length); 2405 } 2406 catch (Exception ioe) 2407 { 2408 throw new SQLException ("Problem with streaming of data"); 2409 } 2410 2411 this.updateBytes(columnIndex, content); 2412 } 2413 2414 2425 public void updateCharacterStream(int columnIndex, java.io.Reader x, 2426 int length) throws SQLException 2427 { 2428 checkIfClosed(); 2429 2430 char[] content = new char[length]; 2431 try 2432 { 2433 x.read(content, 0, length); 2434 } 2435 catch (Exception ioe) 2436 { 2437 throw new SQLException ("Problem with streaming of data"); 2438 } 2439 2440 this.updateString(columnIndex, new String (content)); 2441 } 2442 2443 2456 public void updateObject(int columnIndex, Object x, int scale) 2457 throws SQLException 2458 { 2459 checkIfClosed(); 2460 2461 if (inserting) 2462 insertStatement.setObject(columnIndex, x, scale); 2463 else 2464 { 2465 checkUpdateFlagAndPrepareUpdateIfNeeded(); 2466 updateStatement.setObject(columnIndex, x, scale); 2467 } 2468 2469 tempRow[columnIndex - 1] = x; 2470 } 2471 2472 2482 public void updateObject(int columnIndex, Object x) throws SQLException 2483 { 2484 checkIfClosed(); 2485 2486 if (inserting) 2487 insertStatement.setObject(columnIndex, x); 2488 else 2489 { 2490 checkUpdateFlagAndPrepareUpdateIfNeeded(); 2491 updateStatement.setObject(columnIndex, x); 2492 } 2493 2494 tempRow[columnIndex - 1] = x; 2495 } 2496 2497 2506 2507 public void updateNull(String columnName) throws SQLException 2508 { 2509 this.updateNull(findColumn(columnName)); 2510 } 2511 2512 2522 public void updateBoolean(String columnName, boolean x) throws SQLException 2523 { 2524 this.updateBoolean(findColumn(columnName), x); 2525 } 2526 2527 2537 public void updateByte(String columnName, byte x) throws SQLException 2538 { 2539 this.updateByte(findColumn(columnName), x); 2540 } 2541 2542 2552 public void updateShort(String columnName, short x) throws SQLException 2553 { 2554 this.updateShort(findColumn(columnName), x); 2555 } 2556 2557 2567 public void updateInt(String columnName, int x) throws SQLException 2568 { 2569 this.updateInt(findColumn(columnName), x); 2570 } 2571 2572 2582 public void updateLong(String columnName, long x) throws SQLException 2583 { 2584 this.updateLong(findColumn(columnName), x); 2585 } 2586 2587 2597 public void updateFloat(String columnName, float x) throws SQLException 2598 { 2599 this.updateFloat(findColumn(columnName), x); 2600 } 2601 2602 2612 public void updateDouble(String columnName, double x) throws SQLException 2613 { 2614 this.updateDouble(findColumn(columnName), x); 2615 } 2616 2617 2627 public void updateBigDecimal(String columnName, BigDecimal x) 2628 throws SQLException 2629 { 2630 this.updateBigDecimal(findColumn(columnName), x); 2631 } 2632 2633 2643 public void updateString(String columnName, String x) throws SQLException 2644 { 2645 this.updateString(findColumn(columnName), x); 2646 } 2647 2648 2658 public void updateBytes(String columnName, byte[] x) throws SQLException 2659 { 2660 this.updateBytes(findColumn(columnName), x); 2661 } 2662 2663 2673 public void updateDate(String columnName, java.sql.Date x) 2674 throws SQLException 2675 { 2676 this.updateDate(findColumn(columnName), x); 2677 } 2678 2679 2689 public void updateTime(String columnName, java.sql.Time x) 2690 throws SQLException 2691 { 2692 this.updateTime(findColumn(columnName), x); 2693 } 2694 2695 2705 public void updateTimestamp(String columnName, java.sql.Timestamp x) 2706 throws SQLException 2707 { 2708 this.updateTimestamp(findColumn(columnName), x); 2709 } 2710 2711 2722 public void updateAsciiStream(String columnName, java.io.InputStream x, 2723 int length) throws SQLException 2724 { 2725 this.updateAsciiStream(findColumn(columnName), x, length); 2726 } 2727 2728 2739 public void updateBinaryStream(String columnName, java.io.InputStream x, 2740 int length) throws SQLException 2741 { 2742 this.updateBinaryStream(findColumn(columnName), x, length); 2743 } 2744 2745 2756 public void updateCharacterStream(String columnName, java.io.Reader reader, 2757 int length) throws SQLException 2758 { 2759 this.updateCharacterStream(findColumn(columnName), reader, length); 2760 } 2761 2762 2775 public void updateObject(String columnName, Object x, int scale) 2776 throws SQLException 2777 { 2778 this.updateObject(findColumn(columnName), x, scale); 2779 } 2780 2781 2791 public void updateObject(String columnName, Object x) throws SQLException 2792 { 2793 this.updateObject(findColumn(columnName), x); 2794 } 2795 2796 2804 public void insertRow() throws SQLException 2805 { 2806 checkIfClosed(); 2807 2808 if (!inserting) 2809 throw new SQLException ("insertRow cannot be called " 2810 + "when the cursor is not on the insert row"); 2811 2812 try 2814 { 2815 insertStatement.executeUpdate(); 2816 } 2817 finally 2818 { 2819 insertStatement.clearParameters(); 2821 } 2822 2823 if (data == null) 2825 data = new ArrayList (); 2826 data.add(tempRow); 2827 nbOfRows++; 2828 2829 tempRow = new Object [nbOfColumns]; 2831 } 2832 2833 2840 public void updateRow() throws SQLException 2841 { 2842 checkIfClosed(); 2843 2844 if (inserting) 2845 throw new SQLException ("updateRow cannot be called " 2846 + "when the cursor is on the insert row"); 2847 2848 if (!updating) 2849 return; 2850 2851 for (int i = 0; i < primaryKeyColumns.length; ++i) 2853 updateStatement.setObject(nbOfColumns + i + 1, ((Object []) data 2854 .get(currentRow))[findColumn(primaryKeyColumns[i]) - 1]); 2855 2856 try 2858 { 2859 updateStatement.executeUpdate(); 2860 } 2861 finally 2862 { 2863 updateStatement.clearParameters(); 2865 } 2866 2867 data.set(currentRow, tempRow); 2869 2870 tempRow = null; 2872 updating = false; 2873 } 2874 2875 2882 public void deleteRow() throws SQLException 2883 { 2884 checkIfClosed(); 2885 2886 if (inserting) 2887 throw new SQLException ("deleteRow cannot be called " 2888 + "when the cursor is on the insert row"); 2889 2890 this.checkRowPos(); 2891 2892 if (deleteStatement == null) 2893 this.buildDeleteStatement(); 2894 2895 for (int i = 0; i < primaryKeyColumns.length; ++i) 2897 deleteStatement 2898 .setObject( 2899 i + 1, 2900 ((Object []) data.get(currentRow))[findColumn(primaryKeyColumns[i]) - 1]); 2901 2902 try 2904 { 2905 deleteStatement.executeUpdate(); 2906 } 2907 finally 2908 { 2909 deleteStatement.clearParameters(); 2911 } 2912 2913 data.remove(currentRow); 2915 nbOfRows--; 2916 2917 if (updating) 2919 this.cancelRowUpdates(); 2920 } 2921 2922 2938 public void refreshRow() throws SQLException 2939 { 2940 checkIfClosed(); 2941 2942 if (inserting) 2943 throw new SQLException ("refreshRow cannot be called " 2944 + "when the cursor is on the insert row"); 2945 2946 this.checkRowPos(); 2947 2948 if (refreshStatement == null) 2949 this.buildRefreshStatement(); 2950 2951 for (int i = 0; i < primaryKeyColumns.length; ++i) 2953 refreshStatement 2954 .setObject( 2955 i + 1, 2956 ((Object []) data.get(currentRow))[findColumn(primaryKeyColumns[i]) - 1]); 2957 2958 DriverResultSet res; 2960 try 2961 { 2962 res = (DriverResultSet) refreshStatement.executeQuery(); 2963 } 2964 finally 2965 { 2966 refreshStatement.clearParameters(); 2968 } 2969 2970 try 2972 { 2973 data.set(currentRow, res.data.get(0)); 2974 } 2975 catch (NullPointerException e) 2976 { 2977 throw new SQLException ("The current row has been " 2978 + "removed from the database"); 2979 } 2980 finally 2981 { 2982 res.close(); 2984 } 2985 2986 if (updating) 2988 this.cancelRowUpdates(); 2989 } 2990 2991 3000 public void cancelRowUpdates() throws SQLException 3001 { 3002 checkIfClosed(); 3003 3004 if (inserting) 3005 throw new SQLException ("cancelRowUpdates cannot be " 3006 + "called when the cursor is on the insert row"); 3007 3008 if (!updating) 3009 return; 3010 3011 updateStatement.clearParameters(); 3012 tempRow = null; 3013 updating = false; 3014 } 3015 3016 3030 public void moveToInsertRow() throws SQLException 3031 { 3032 checkIfClosed(); 3033 3034 if (inserting) 3035 return; 3036 3037 if (insertStatement == null) 3038 this.buildInsertStatement(); 3039 3040 tempRow = new Object [nbOfColumns]; 3041 inserting = true; 3042 } 3043 3044 3051 public void moveToCurrentRow() throws SQLException 3052 { 3053 checkIfClosed(); 3054 3055 if (!inserting) 3056 return; 3057 3058 insertStatement.clearParameters(); 3059 tempRow = null; 3060 inserting = false; 3061 } 3062 3063 3069 3077 public int getType() throws SQLException 3078 { 3079 return resultSetType; 3080 } 3081 3082 3089 3090 public int getConcurrency() throws SQLException 3091 { 3092 return resultSetConcurrency; 3093 } 3094 3095 3101 public void close() throws SQLException 3102 { 3103 if (isClosed) 3104 return; 3105 3106 closeCurrentStreamIfExists(); 3107 3108 if (hasMoreData) 3109 this.connection.closeRemoteResultSet(cursorName); 3110 isClosed = true; 3111 this.data = null; 3113 this.fields = null; 3114 this.owningStatement = null; 3115 this.warnings = null; 3116 } 3117 3118 3127 3128 public boolean wasNull() throws SQLException 3129 { 3130 return wasNullFlag; 3131 } 3132 3133 3142 public java.sql.Statement getStatement() throws SQLException 3143 { 3144 return owningStatement; 3145 } 3146 3147 3153 public void setHasMoreData(boolean hasMore) 3154 { 3155 hasMoreData = hasMore; 3156 } 3157 3158 3165 public java.sql.ResultSetMetaData getMetaData() throws SQLException 3166 { 3167 return new ResultSetMetaData(this); 3168 } 3169 3170 3178 public int findColumn(String columnName) throws SQLException 3179 { 3180 if (columnName == null) 3181 throw new java.sql.SQLException ("Invalid null column name"); 3182 3183 checkIfClosed(); 3184 3185 Integer index; 3186 3187 if (sensitiveNameToIndex == null) 3189 { 3190 sensitiveNameToIndex = new Hashtable (); 3191 buildIndexMapping(sensitiveNameToIndex, true); 3192 } 3193 index = (Integer ) sensitiveNameToIndex.get(columnName); 3194 if (index != null) 3195 return index.intValue() + 1; 3196 3197 if (lowerNameToIndex == null) 3199 { 3200 lowerNameToIndex = new Hashtable (); 3201 buildIndexMapping(lowerNameToIndex, false); 3202 } 3203 index = (Integer ) lowerNameToIndex.get(columnName.toLowerCase()); 3204 if (index != null) 3205 return index.intValue() + 1; 3206 3207 throw new java.sql.SQLException ("Column '" + columnName + "' not found."); 3209 } 3210 3211 3217 3226 3227 public DriverResultSet(Connection conn) throws IOException , ProtocolException 3228 { 3229 this.connection = conn; 3230 DriverBufferedInputStream input = this.connection.socketInput; 3231 if (input.readBoolean()) 3233 { 3234 warnings = new BackendDriverException(input); 3235 } 3236 else 3237 warnings = null; 3238 this.nbOfColumns = input.readInt(); 3240 this.fields = new Field[nbOfColumns]; 3241 for (int f = 0; f < this.nbOfColumns; f++) 3242 this.fields[f] = new Field(input); 3243 3244 if (!TypeTag.COL_TYPES.equals(new TypeTag(input))) 3245 throw new ProtocolException("Column types were expected"); 3246 3247 this.nbOfRows = input.readInt(); 3250 3251 if (this.nbOfRows > 0) 3253 { 3254 this.serializers = new SQLDataSerialization.Serializer[nbOfColumns]; 3255 for (int col = 0; col < this.nbOfColumns; col++) 3256 { 3257 TypeTag tag = new TypeTag(input); 3258 try 3259 { 3260 serializers[col] = SQLDataSerialization.getSerializer(tag); 3261 } 3262 catch (IllegalArgumentException iae) 3263 { 3264 ProtocolException pe = new ProtocolException( 3265 "Protocol corruption: received unknown TypeTag " + tag 3266 + " for column " + col); 3267 pe.initCause(iae); 3268 throw pe; 3269 } 3270 } 3271 } 3272 3273 receiveRows(); 3274 3275 if (this.hasMoreData) 3276 this.cursorName = input.readLongUTF(); 3277 3278 this.isClosed = false; 3279 3280 } 3281 3282 3287 DriverResultSet(Connection conn, boolean empty) 3288 { 3289 if (!empty) 3290 throw new IllegalArgumentException ( 3291 "empty flag must be true in DriverResultSet constructor"); 3292 3293 connection = conn; 3294 inserting = false; 3295 updating = false; 3296 nbOfRows = 0; 3297 nbOfColumns = 0; 3298 hasMoreData = false; 3299 isClosed = false; 3300 } 3301 3302 3311 public DriverResultSet(Connection conn, ArrayList fromData, Field fromField) 3312 { 3313 this(conn, true); 3314 if (fromData != null && fromData.size() > 0) 3315 { 3316 nbOfRows = fromData.size(); 3317 nbOfColumns = 1; 3318 hasMoreData = false; 3319 this.data = new ArrayList (this.nbOfRows); 3320 for (int i = 0; i < nbOfRows; i++) 3321 { 3322 Object [] row = new Object [nbOfColumns]; 3323 row[0] = fromData.get(i); 3324 this.data.add(row); 3325 } 3326 this.fields = new Field[1]; 3327 this.fields[0] = fromField; 3328 } 3329 } 3330 3331 3339 private void receiveRows() throws IOException , ProtocolException 3340 { 3341 3342 DriverBufferedInputStream input = connection.socketInput; 3343 this.nbOfRows = input.readInt(); 3344 3345 boolean[] nulls = new boolean[this.nbOfColumns]; 3346 3347 this.data = new ArrayList (this.nbOfRows); 3349 3350 for (int r = 0; r < this.nbOfRows; r++) 3351 { 3352 if (!TypeTag.ROW.equals(new TypeTag(input))) 3353 throw new ProtocolException("A row was expected"); 3354 3355 for (int col = 0; col < nbOfColumns; col++) 3358 nulls[col] = input.readBoolean(); 3359 3360 Object [] row = new Object [this.nbOfColumns]; 3361 3362 3371 for (int col = 0; col < this.nbOfColumns; col++) 3372 if (nulls[col]) 3373 row[col] = null; 3374 else 3375 row[col] = serializers[col].receiveFromStream(input); 3376 3377 this.data.add(row); 3378 } 3379 3380 this.hasMoreData = input.readBoolean(); 3381 } 3382 3383 3389 protected void setStatement(Statement stmt) throws SQLException 3390 { 3391 owningStatement = stmt; 3392 fetchSize = stmt.getFetchSize(); 3393 resultSetConcurrency = stmt.getResultSetConcurrency(); 3394 resultSetType = stmt.getResultSetType(); 3395 } 3396 3397 3402 private void buildIndexMapping(Hashtable table, boolean sensitive) 3403 { 3404 int numFields = nbOfColumns; 3405 3406 for (int i = 0; i < numFields; i++) 3407 { 3408 putFieldNameInIndexMapping(fields[i].getFieldLabel(), i, sensitive, table); 3411 putFieldNameInIndexMapping(fields[i].getFieldName(), i, sensitive, table); 3412 3413 3418 putFieldNameInIndexMapping(fields[i].getFullName(), i, sensitive, table); 3419 } 3420 } 3421 3422 private void putFieldNameInIndexMapping(String name, int i, 3423 boolean sensitive, Hashtable table) 3424 { 3425 if ((name == null) || "".equals(name)) 3426 return; 3427 3428 Integer index = new Integer (i); 3429 3430 String key = sensitive ? name : name.toLowerCase(); 3431 3432 if (!table.containsKey(key)) 3433 table.put(key, index); 3434 } 3435 3436 3441 private void buildDeleteStatement() throws SQLException 3442 { 3443 this.checkUpdatability(); 3445 3446 StringBuffer sb = new StringBuffer (); 3448 sb.append("DELETE FROM "); 3449 sb.append(fields[0].getTableName()); 3450 sb.append(" WHERE "); 3451 for (int i = 0; i < primaryKeyColumns.length; ++i) 3452 { 3453 if (i > 0) 3454 sb.append(" AND "); 3455 sb.append(primaryKeyColumns[i]); 3456 sb.append(" = ?"); 3457 } 3458 3459 deleteStatement = this.connection.prepareStatement(sb.toString()); 3461 } 3462 3463 3468 private void buildInsertStatement() throws SQLException 3469 { 3470 this.checkUpdatability(); 3472 3473 StringBuffer sb = new StringBuffer (); 3475 sb.append("INSERT INTO "); 3476 sb.append(fields[0].getTableName()); 3477 sb.append(" ("); 3478 for (int i = 0; i < fields.length; ++i) 3479 { 3480 if (i > 0) 3481 sb.append(", "); 3482 sb.append(fields[i].getFieldName()); 3483 } 3484 sb.append(") VALUES ("); 3485 for (int i = 0; i < fields.length; ++i) 3486 { 3487 if (i > 0) 3488 sb.append(", "); 3489 sb.append("?"); 3490 } 3491 sb.append(")"); 3492 3493 insertStatement = this.connection.prepareStatement(sb.toString()); 3495 } 3496 3497 3502 private void buildRefreshStatement() throws SQLException 3503 { 3504 this.checkUpdatability(); 3506 3507 StringBuffer sb = new StringBuffer (); 3509 sb.append("SELECT "); 3510 for (int i = 0; i < fields.length; ++i) 3511 { 3512 if (i > 0) 3513 sb.append(", "); 3514 sb.append(fields[i].getFieldName()); 3515 } 3516 sb.append(" FROM "); 3517 sb.append(fields[0].getTableName()); 3518 sb.append(" WHERE "); 3519 for (int i = 0; i < primaryKeyColumns.length; ++i) 3520 { 3521 if (i > 0) 3522 sb.append(" AND "); 3523 sb.append(primaryKeyColumns[i]); 3524 sb.append(" = ?"); 3525 } 3526 3527 refreshStatement = this.connection.prepareStatement(sb.toString()); 3529 } 3530 3531 3536 private void buildUpdateStatement() throws SQLException 3537 { 3538 this.checkUpdatability(); 3540 3541 StringBuffer sb = new StringBuffer (); 3543 sb.append("UPDATE "); 3544 sb.append(fields[0].getTableName()); 3545 sb.append(" SET "); 3546 for (int i = 0; i < fields.length; ++i) 3547 { 3548 if (i > 0) 3549 sb.append(", "); 3550 sb.append(fields[i].getFieldName()); 3551 sb.append(" = ?"); 3552 } 3553 sb.append(" WHERE "); 3554 for (int i = 0; i < primaryKeyColumns.length; ++i) 3555 { 3556 if (i > 0) 3557 sb.append(" AND "); 3558 sb.append(primaryKeyColumns[i]); 3559 sb.append(" = ?"); 3560 } 3561 3562 updateStatement = this.connection.prepareStatement(sb.toString()); 3564 } 3565 3566 3571 private void extractPrimaryKey() throws SQLException 3572 { 3573 if (fields[0].getTableName() == null 3575 || fields[0].getTableName().length() == 0) 3576 throw new SQLException ("Could not extractPrimaryKeys: empty tablename"); 3577 3578 ResultSet res = this.connection.getPrimaryKeys(null, null, fields[0] 3579 .getTableName()); 3580 3581 try 3582 { 3583 primaryKeyColumns = new String [((DriverResultSet) res).nbOfRows]; 3584 while (res.next()) 3585 primaryKeyColumns[res.getRow() - 1] = res.getString(4); 3586 } 3587 finally 3588 { 3589 res.close(); 3591 } 3592 } 3593 3594 3599 private void checkUpdatability() throws SQLException 3600 { 3601 switch (resultSetConcurrency) 3603 { 3604 case ResultSet.CONCUR_READ_ONLY : 3605 throw new SQLException ("Cannot update ResultSet with " 3606 + "concurrency mode CONCUR_READ_ONLY"); 3607 case ResultSet.CONCUR_UPDATABLE : 3608 break; 3609 default : 3610 throw new SQLException ("Invalid ResultSet concurrency mode: " 3611 + resultSetConcurrency); 3612 } 3613 3614 String tableName = fields[0].getTableName(); 3616 for (int i = 1; i < nbOfColumns; ++i) 3617 if (!tableName.equals(fields[i].getTableName())) 3618 throw new SQLException (UPDATEABLE_MESSAGE); 3619 3620 if (primaryKeyColumns == null) 3622 this.extractPrimaryKey(); 3623 3624 for (int i = 0; i < primaryKeyColumns.length; ++i) 3626 try 3627 { 3628 findColumn(primaryKeyColumns[i]); 3629 } 3630 catch (SQLException e) 3631 { 3632 throw new SQLException (UPDATEABLE_MESSAGE); 3633 } 3634 } 3635 3636 3642 private void checkRowAndColPosAndSetNullFlag(int columnIndex) 3643 throws SQLException 3644 { 3645 checkIfClosed(); 3646 3647 closeCurrentStreamIfExists(); 3648 3649 if (!inserting) 3650 checkRowPos(); 3651 3652 if (fields == null) 3653 throw new java.sql.SQLException ("Query generated no fields for ResultSet"); 3654 3655 if (columnIndex < 1 || columnIndex > nbOfColumns) 3656 throw new java.sql.SQLException ("Column Index out of range ( " 3657 + columnIndex + " > " + nbOfColumns + ")."); 3658 3659 try 3660 { 3661 Object obj; 3662 if (inserting || updating) 3663 obj = tempRow[columnIndex - 1]; 3664 else 3665 obj = ((Object []) data.get(currentRow))[columnIndex - 1]; 3666 3667 if (obj == null) 3668 wasNullFlag = true; 3669 else 3670 wasNullFlag = false; 3671 } 3672 catch (NullPointerException e) 3673 { 3674 wasNullFlag = true; 3675 } 3676 } 3677 3678 3683 private void checkRowPos() throws SQLException 3684 { 3685 if (currentRow < 0) 3686 throw new SQLException ("Before start of result set"); 3687 3688 if (currentRow == nbOfRows) 3689 throw new SQLException ("After end of result set"); 3690 } 3691 3692 3698 private void checkUpdateFlagAndPrepareUpdateIfNeeded() throws SQLException 3699 { 3700 if (updating) 3701 return; 3702 3703 this.checkRowPos(); 3704 3705 if (updateStatement == null) 3706 this.buildUpdateStatement(); 3707 3708 tempRow = (Object []) ((Object []) data.get(currentRow)).clone(); 3709 3710 for (int i = 0; i < nbOfColumns; ++i) 3711 updateStatement.setObject(i + 1, tempRow[i]); 3712 3713 updating = true; 3714 } 3715 3716 3721 private void checkIfClosed() throws SQLException 3722 { 3723 if (isClosed) 3724 throw new SQLException ("Trying to access a closed ResultSet"); 3725 } 3726 3727 3733 private void checkIfScrollable() throws SQLException 3734 { 3735 checkIfClosed(); 3736 if (resultSetType == ResultSet.TYPE_FORWARD_ONLY) 3737 { 3738 throw new SQLException ( 3739 "Operation requires a scrollable ResultSet, but this ResultSet is FORWARD_ONLY"); 3740 } 3741 } 3742 3743 3746 private void closeCurrentStreamIfExists() 3747 { 3748 if (currentStream != null) 3749 { 3750 try 3751 { 3752 currentStream.close(); 3754 } 3755 catch (IOException ignore) 3756 { 3757 } 3758 finally 3759 { 3760 currentStream = null; 3761 } 3762 } 3763 } 3764 3765 3768 3782 public java.net.URL getURL(int columnIndex) throws SQLException 3783 { 3784 checkRowAndColPosAndSetNullFlag(columnIndex); 3785 3786 if (wasNullFlag) 3787 return null; 3788 3789 if (inserting || updating) 3790 return (URL ) tempRow[columnIndex - 1]; 3791 else 3792 return (URL ) (((Object []) data.get(currentRow))[columnIndex - 1]); 3793 } 3794 3795 3808 public java.net.URL getURL(String columnName) throws SQLException 3809 { 3810 return getURL(findColumn(columnName)); 3811 } 3812 3813 3825 public void updateRef(int columnIndex, java.sql.Ref x) throws SQLException 3826 { 3827 throw new NotImplementedException("updateRef"); 3828 } 3829 3830 3842 public void updateRef(String columnName, java.sql.Ref x) throws SQLException 3843 { 3844 updateRef(findColumn(columnName), x); 3845 } 3846 3847 3859 public void updateBlob(int columnIndex, java.sql.Blob x) throws SQLException 3860 { 3861 throw new NotImplementedException("updateBlob"); 3862 } 3863 3864 3876 public void updateBlob(String columnName, java.sql.Blob x) 3877 throws SQLException 3878 { 3879 updateBlob(findColumn(columnName), x); 3880 } 3881 3882 3894 public void updateClob(int columnIndex, java.sql.Clob x) throws SQLException 3895 { 3896 throw new NotImplementedException("updateClob"); 3897 } 3898 3899 3911 public void updateClob(String columnName, java.sql.Clob x) 3912 throws SQLException 3913 { 3914 updateClob(findColumn(columnName), x); 3915 } 3916 3917 3929 public void updateArray(int columnIndex, java.sql.Array x) 3930 throws SQLException 3931 { 3932 throw new NotImplementedException("updateArray"); 3933 } 3934 3935 3947 public void updateArray(String columnName, java.sql.Array x) 3948 throws SQLException 3949 { 3950 updateArray(findColumn(columnName), x); 3951 } 3952 3953 3960 public byte getByte(int columnIndex) throws SQLException 3961 { 3962 checkRowAndColPosAndSetNullFlag(columnIndex); 3963 3964 if (wasNullFlag) 3965 return 0; 3966 3967 Object obj; 3968 if (inserting || updating) 3969 obj = tempRow[columnIndex - 1]; 3970 else 3971 obj = ((Object []) data.get(currentRow))[columnIndex - 1]; 3972 3973 if (obj instanceof Number ) 3974 { 3975 return ((Number ) obj).byteValue(); 3976 } 3977 3978 try 3980 { 3981 String string = obj.toString(); 3982 string = string.trim(); 3983 return Byte.parseByte(string); 3984 } 3985 catch (NumberFormatException e) 3986 { 3987 throw new SQLException ("the value " + obj.toString() 3988 + " is not a valid byte"); 3989 } 3990 } 3991 3992 3995 public String toString() 3996 { 3997 return nbOfRows + " rows - " + nbOfColumns + " columns - current row:" 3998 + currentRow + " - hasMoreData:" + hasMoreData + " - isClosed:" 3999 + isClosed; 4000 } 4001} | Popular Tags |