1 5 package net.mlw.vlh.adapter.jdbc.objectWrapper; 6 7 import java.io.InputStream ; 8 import java.io.Reader ; 9 import java.math.BigDecimal ; 10 import java.net.URL ; 11 import java.sql.Array ; 12 import java.sql.Blob ; 13 import java.sql.Clob ; 14 import java.sql.Date ; 15 import java.sql.Ref ; 16 import java.sql.ResultSet ; 17 import java.sql.ResultSetMetaData ; 18 import java.sql.SQLException ; 19 import java.sql.SQLWarning ; 20 import java.sql.Statement ; 21 import java.sql.Time ; 22 import java.sql.Timestamp ; 23 import java.util.Calendar ; 24 import java.util.Map ; 25 26 import net.mlw.vlh.adapter.util.ObjectValidator; 27 28 36 public class ResultSetDecorator implements ResultSet 37 { 38 39 private static final int INITIAL_CAPACITY = 100; 40 41 private ResultSet _resultSet; 42 43 private ObjectValidator _validator; 44 45 private int _currentRow; 46 47 private int[] _index; 48 49 private int _size; 50 51 private boolean _isComplete; 52 53 58 public ResultSetDecorator(ResultSet rs, ObjectValidator objectValidator) 59 { 60 super(); 61 setValidator(objectValidator); 62 setResultSet(rs); } 64 65 68 public void beforeFirst() throws SQLException 69 { 70 71 if (!_resultSet.isBeforeFirst()) 72 { 73 _resultSet.beforeFirst(); 74 } 75 _currentRow = 0; 76 } 77 78 81 public boolean isBeforeFirst() throws SQLException 82 { 83 return (_currentRow == 0); 84 } 85 86 89 public boolean first() throws SQLException 90 { 91 return move(1); 92 } 93 94 97 public boolean isFirst() throws SQLException 98 { 99 return (_currentRow == 1); 100 } 101 102 105 public boolean last() throws SQLException 106 { 107 if (_isComplete) 108 { 109 return move(_size); 111 } 112 else 113 { 114 afterLast(); 115 return previous(); 116 } 117 } 118 119 122 public boolean isLast() throws SQLException 123 { 124 if (_isComplete) 125 { 126 return (_currentRow == _size); 128 } 129 else 130 { 131 boolean result = next(); 132 previous(); 133 return result; 134 } 135 } 136 137 140 public void afterLast() throws SQLException 141 { 142 if (_isComplete) 143 { 144 _resultSet.afterLast(); 146 _currentRow = _size + 1; 147 } 148 else 149 { 150 lastKnown(); 151 while (nextValid()) 152 ; 153 } 154 } 155 156 159 public boolean isAfterLast() throws SQLException 160 { 161 return _isComplete && (_currentRow > _size); 162 } 163 164 167 public boolean next() throws SQLException 168 { 169 return move(_currentRow + 1); 170 } 171 172 175 public boolean previous() throws SQLException 176 { 177 return move(_currentRow - 1); 178 } 179 180 183 public boolean relative(int rows) throws SQLException 184 { 185 if (rows == 0) 186 { 187 return _resultSet.relative(0); 188 } 189 190 return move(_currentRow + rows); 191 } 192 193 196 public boolean absolute(int row) throws SQLException 197 { 198 if (row > 0) 199 { 200 return move(row); 201 } 202 else if (row < 0) 203 { 204 if (!_isComplete) 205 { 206 afterLast(); 208 } 209 return move(_size + row + 1); 210 } 211 else 212 { 213 beforeFirst(); 215 return false; 216 } 217 } 218 219 private boolean move(int row) throws SQLException 220 { 221 if (row > 0) 222 { 223 if (row <= _size) 224 { 225 _currentRow = row; 226 int absolute = _index[_currentRow - 1]; 227 if (absolute != _resultSet.getRow()) 228 { 229 return _resultSet.absolute(absolute); 230 } 231 else 232 { 233 return true; 234 } 235 } 236 else 237 { 238 if (_isComplete) 239 { 240 afterLast(); 241 return false; 242 } 243 else 244 { 245 lastKnown(); 246 boolean result = true; 247 while (result && (_currentRow < row)) 248 { 249 result = nextValid(); 250 } 251 return result; 252 } 253 } 254 } 255 else 256 { 257 beforeFirst(); 258 return false; 259 } 260 } 261 262 private void lastKnown() throws SQLException 263 { 264 if (_size > 0) 265 { 266 _currentRow = _size; 267 int absolute = _index[_currentRow - 1]; 268 if (absolute != _resultSet.getRow()) 269 { 270 _resultSet.absolute(absolute); 271 } 272 } 273 else 274 { 275 beforeFirst(); 276 } 277 } 278 279 private boolean nextValid() throws SQLException 280 { 281 boolean result; 282 283 do 284 { 285 result = _resultSet.next(); 286 } 287 while (result && !_validator.isAcceptable(_resultSet.getObject(1))); 288 289 _currentRow++; 290 291 if (result) 292 { 293 _size = _currentRow; 294 ensureCapacity(_size); 295 _index[_currentRow - 1] = _resultSet.getRow(); 296 } 297 else 298 { 299 _isComplete = true; 300 } 301 302 return result; 303 } 304 305 private void ensureCapacity(int minCapacity) 306 { 307 int oldCapacity = _index.length; 308 if (minCapacity > oldCapacity) 309 { 310 int[] oldData = _index; 311 int newCapacity = (oldCapacity * 3) / 2 + 1; 312 if (newCapacity < minCapacity) 313 { 314 newCapacity = minCapacity; 315 } 316 _index = new int[newCapacity]; 317 System.arraycopy(oldData, 0, _index, 0, oldCapacity); 318 } 319 } 320 321 322 323 326 public int getConcurrency() throws SQLException 327 { 328 return _resultSet.getConcurrency(); 329 } 330 331 334 public int getFetchDirection() throws SQLException 335 { 336 return _resultSet.getFetchDirection(); 337 } 338 339 342 public int getFetchSize() throws SQLException 343 { 344 return _resultSet.getFetchSize(); 345 } 346 347 350 public int getRow() throws SQLException 351 { 352 return _currentRow; 353 } 354 355 358 public int getType() throws SQLException 359 { 360 return _resultSet.getType(); 361 } 362 363 366 public void cancelRowUpdates() throws SQLException 367 { 368 _resultSet.cancelRowUpdates(); 369 } 370 371 374 public void clearWarnings() throws SQLException 375 { 376 _resultSet.clearWarnings(); 377 } 378 379 382 public void close() throws SQLException 383 { 384 _resultSet.close(); 385 } 386 387 390 public void deleteRow() throws SQLException 391 { 392 _resultSet.deleteRow(); 393 } 394 395 398 public void insertRow() throws SQLException 399 { 400 _resultSet.insertRow(); 401 } 402 403 406 public void moveToCurrentRow() throws SQLException 407 { 408 _resultSet.moveToCurrentRow(); 409 } 410 411 414 public void moveToInsertRow() throws SQLException 415 { 416 _resultSet.moveToInsertRow(); 417 } 418 419 422 public void refreshRow() throws SQLException 423 { 424 _resultSet.refreshRow(); 425 } 426 427 430 public void updateRow() throws SQLException 431 { 432 _resultSet.updateRow(); 433 } 434 435 438 public boolean rowDeleted() throws SQLException 439 { 440 return _resultSet.rowDeleted(); 441 } 442 443 446 public boolean rowInserted() throws SQLException 447 { 448 return _resultSet.rowInserted(); 449 } 450 451 454 public boolean rowUpdated() throws SQLException 455 { 456 return _resultSet.rowUpdated(); 457 } 458 459 462 public boolean wasNull() throws SQLException 463 { 464 return _resultSet.wasNull(); 465 } 466 467 470 public byte getByte(int columnIndex) throws SQLException 471 { 472 return _resultSet.getByte(columnIndex); 473 } 474 475 478 public double getDouble(int columnIndex) throws SQLException 479 { 480 return _resultSet.getDouble(columnIndex); 481 } 482 483 486 public float getFloat(int columnIndex) throws SQLException 487 { 488 return _resultSet.getFloat(columnIndex); 489 } 490 491 494 public int getInt(int columnIndex) throws SQLException 495 { 496 return _resultSet.getInt(columnIndex); 497 } 498 499 502 public long getLong(int columnIndex) throws SQLException 503 { 504 return _resultSet.getLong(columnIndex); 505 } 506 507 510 public short getShort(int columnIndex) throws SQLException 511 { 512 return _resultSet.getShort(columnIndex); 513 } 514 515 518 public void setFetchDirection(int direction) throws SQLException 519 { 520 _resultSet.setFetchDirection(direction); 521 } 522 523 526 public void setFetchSize(int rows) throws SQLException 527 { 528 _resultSet.setFetchSize(rows); 529 } 530 531 534 public void updateNull(int columnIndex) throws SQLException 535 { 536 _resultSet.updateNull(columnIndex); 537 } 538 539 542 public boolean getBoolean(int columnIndex) throws SQLException 543 { 544 return _resultSet.getBoolean(columnIndex); 545 } 546 547 550 public byte[] getBytes(int columnIndex) throws SQLException 551 { 552 return _resultSet.getBytes(columnIndex); 553 } 554 555 558 public void updateByte(int columnIndex, byte x) throws SQLException 559 { 560 _resultSet.updateByte(columnIndex, x); 561 } 562 563 566 public void updateDouble(int columnIndex, double x) throws SQLException 567 { 568 _resultSet.updateDouble(columnIndex, x); 569 } 570 571 574 public void updateFloat(int columnIndex, float x) throws SQLException 575 { 576 _resultSet.updateFloat(columnIndex, x); 577 } 578 579 582 public void updateInt(int columnIndex, int x) throws SQLException 583 { 584 _resultSet.updateInt(columnIndex, x); 585 } 586 587 590 public void updateLong(int columnIndex, long x) throws SQLException 591 { 592 _resultSet.updateLong(columnIndex, x); 593 } 594 595 598 public void updateShort(int columnIndex, short x) throws SQLException 599 { 600 _resultSet.updateShort(columnIndex, x); 601 } 602 603 606 public void updateBoolean(int columnIndex, boolean x) throws SQLException 607 { 608 _resultSet.updateBoolean(columnIndex, x); 609 } 610 611 614 public void updateBytes(int columnIndex, byte[] x) throws SQLException 615 { 616 _resultSet.updateBytes(columnIndex, x); 617 } 618 619 622 public InputStream getAsciiStream(int columnIndex) throws SQLException 623 { 624 return _resultSet.getAsciiStream(columnIndex); 625 } 626 627 630 public InputStream getBinaryStream(int columnIndex) throws SQLException 631 { 632 return _resultSet.getBinaryStream(columnIndex); 633 } 634 635 639 public InputStream getUnicodeStream(int columnIndex) throws SQLException 640 { 641 return _resultSet.getUnicodeStream(columnIndex); 642 } 643 644 647 public void updateAsciiStream(int columnIndex, InputStream x, int length) throws SQLException 648 { 649 _resultSet.updateAsciiStream(columnIndex, x, length); 650 } 651 652 655 public void updateBinaryStream(int columnIndex, InputStream x, int length) throws SQLException 656 { 657 _resultSet.updateBinaryStream(columnIndex, x, length); 658 } 659 660 663 public Reader getCharacterStream(int columnIndex) throws SQLException 664 { 665 return _resultSet.getCharacterStream(columnIndex); 666 } 667 668 671 public void updateCharacterStream(int columnIndex, Reader x, int length) throws SQLException 672 { 673 _resultSet.updateCharacterStream(columnIndex, x, length); 674 } 675 676 679 public Object getObject(int columnIndex) throws SQLException 680 { 681 return _resultSet.getObject(columnIndex); 682 } 683 684 687 public void updateObject(int columnIndex, Object x) throws SQLException 688 { 689 _resultSet.updateObject(columnIndex, x); 690 } 691 692 695 public void updateObject(int columnIndex, Object x, int scale) throws SQLException 696 { 697 _resultSet.updateObject(columnIndex, x, scale); 698 } 699 700 703 public String getCursorName() throws SQLException 704 { 705 return _resultSet.getCursorName(); 706 } 707 708 711 public String getString(int columnIndex) throws SQLException 712 { 713 return _resultSet.getString(columnIndex); 714 } 715 716 719 public void updateString(int columnIndex, String x) throws SQLException 720 { 721 _resultSet.updateString(columnIndex, x); 722 } 723 724 727 public byte getByte(String columnName) throws SQLException 728 { 729 return _resultSet.getByte(columnName); 730 } 731 732 735 public double getDouble(String columnName) throws SQLException 736 { 737 return _resultSet.getDouble(columnName); 738 } 739 740 743 public float getFloat(String columnName) throws SQLException 744 { 745 return _resultSet.getFloat(columnName); 746 } 747 748 751 public int findColumn(String columnName) throws SQLException 752 { 753 return _resultSet.findColumn(columnName); 754 } 755 756 759 public int getInt(String columnName) throws SQLException 760 { 761 return _resultSet.getInt(columnName); 762 } 763 764 767 public long getLong(String columnName) throws SQLException 768 { 769 return _resultSet.getLong(columnName); 770 } 771 772 775 public short getShort(String columnName) throws SQLException 776 { 777 return _resultSet.getShort(columnName); 778 } 779 780 783 public void updateNull(String columnName) throws SQLException 784 { 785 _resultSet.updateNull(columnName); 786 } 787 788 791 public boolean getBoolean(String columnName) throws SQLException 792 { 793 return _resultSet.getBoolean(columnName); 794 } 795 796 799 public byte[] getBytes(String columnName) throws SQLException 800 { 801 return _resultSet.getBytes(columnName); 802 } 803 804 807 public void updateByte(String columnName, byte x) throws SQLException 808 { 809 _resultSet.updateByte(columnName, x); 810 } 811 812 815 public void updateDouble(String columnName, double x) throws SQLException 816 { 817 _resultSet.updateDouble(columnName, x); 818 } 819 820 823 public void updateFloat(String columnName, float x) throws SQLException 824 { 825 _resultSet.updateFloat(columnName, x); 826 } 827 828 831 public void updateInt(String columnName, int x) throws SQLException 832 { 833 _resultSet.updateInt(columnName, x); 834 } 835 836 839 public void updateLong(String columnName, long x) throws SQLException 840 { 841 _resultSet.updateLong(columnName, x); 842 } 843 844 847 public void updateShort(String columnName, short x) throws SQLException 848 { 849 _resultSet.updateShort(columnName, x); 850 } 851 852 855 public void updateBoolean(String columnName, boolean x) throws SQLException 856 { 857 _resultSet.updateBoolean(columnName, x); 858 } 859 860 863 public void updateBytes(String columnName, byte[] x) throws SQLException 864 { 865 _resultSet.updateBytes(columnName, x); 866 } 867 868 871 public BigDecimal getBigDecimal(int columnIndex) throws SQLException 872 { 873 return _resultSet.getBigDecimal(columnIndex); 874 } 875 876 880 public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException 881 { 882 return _resultSet.getBigDecimal(columnIndex, scale); 883 } 884 885 888 public void updateBigDecimal(int columnIndex, BigDecimal x) throws SQLException 889 { 890 _resultSet.updateBigDecimal(columnIndex, x); 891 } 892 893 896 public URL getURL(int columnIndex) throws SQLException 897 { 898 return _resultSet.getURL(columnIndex); 899 } 900 901 904 public Array getArray(int i) throws SQLException 905 { 906 return _resultSet.getArray(i); 907 } 908 909 912 public void updateArray(int columnIndex, Array x) throws SQLException 913 { 914 _resultSet.updateArray(columnIndex, x); 915 } 916 917 920 public Blob getBlob(int i) throws SQLException 921 { 922 return _resultSet.getBlob(i); 923 } 924 925 928 public void updateBlob(int columnIndex, Blob x) throws SQLException 929 { 930 _resultSet.updateBlob(columnIndex, x); 931 } 932 933 936 public Clob getClob(int i) throws SQLException 937 { 938 return _resultSet.getClob(i); 939 } 940 941 944 public void updateClob(int columnIndex, Clob x) throws SQLException 945 { 946 _resultSet.updateClob(columnIndex, x); 947 } 948 949 952 public Date getDate(int columnIndex) throws SQLException 953 { 954 return _resultSet.getDate(columnIndex); 955 } 956 957 960 public void updateDate(int columnIndex, Date x) throws SQLException 961 { 962 _resultSet.updateDate(columnIndex, x); 963 } 964 965 968 public Ref getRef(int i) throws SQLException 969 { 970 return _resultSet.getRef(i); 971 } 972 973 976 public void updateRef(int columnIndex, Ref x) throws SQLException 977 { 978 _resultSet.updateRef(columnIndex, x); 979 } 980 981 984 public ResultSetMetaData getMetaData() throws SQLException 985 { 986 return _resultSet.getMetaData(); 987 } 988 989 992 public SQLWarning getWarnings() throws SQLException 993 { 994 return _resultSet.getWarnings(); 995 } 996 997 1000 public Statement getStatement() throws SQLException 1001 { 1002 return _resultSet.getStatement(); 1003 } 1004 1005 1008 public Time getTime(int columnIndex) throws SQLException 1009 { 1010 return _resultSet.getTime(columnIndex); 1011 } 1012 1013 1016 public void updateTime(int columnIndex, Time x) throws SQLException 1017 { 1018 _resultSet.updateTime(columnIndex, x); 1019 } 1020 1021 1024 public Timestamp getTimestamp(int columnIndex) throws SQLException 1025 { 1026 return _resultSet.getTimestamp(columnIndex); 1027 } 1028 1029 1032 public void updateTimestamp(int columnIndex, Timestamp x) throws SQLException 1033 { 1034 _resultSet.updateTimestamp(columnIndex, x); 1035 } 1036 1037 1040 public InputStream getAsciiStream(String columnName) throws SQLException 1041 { 1042 return _resultSet.getAsciiStream(columnName); 1043 } 1044 1045 1048 public InputStream getBinaryStream(String columnName) throws SQLException 1049 { 1050 return _resultSet.getBinaryStream(columnName); 1051 } 1052 1053 1057 public InputStream getUnicodeStream(String columnName) throws SQLException 1058 { 1059 return _resultSet.getUnicodeStream(columnName); 1060 } 1061 1062 1066 public void updateAsciiStream(String columnName, InputStream x, int length) throws SQLException 1067 { 1068 _resultSet.updateAsciiStream(columnName, x, length); 1069 } 1070 1071 1075 public void updateBinaryStream(String columnName, InputStream x, int length) throws SQLException 1076 { 1077 _resultSet.updateBinaryStream(columnName, x, length); 1078 } 1079 1080 1083 public Reader getCharacterStream(String columnName) throws SQLException 1084 { 1085 return _resultSet.getCharacterStream(columnName); 1086 } 1087 1088 1092 public void updateCharacterStream(String columnName, Reader reader, int length) throws SQLException 1093 { 1094 _resultSet.updateCharacterStream(columnName, reader, length); 1095 } 1096 1097 1100 public Object getObject(String columnName) throws SQLException 1101 { 1102 return _resultSet.getObject(columnName); 1103 } 1104 1105 1108 public void updateObject(String columnName, Object x) throws SQLException 1109 { 1110 _resultSet.updateObject(columnName, x); 1111 } 1112 1113 1117 public void updateObject(String columnName, Object x, int scale) throws SQLException 1118 { 1119 _resultSet.updateObject(columnName, x, scale); 1120 } 1121 1122 1125 public Object getObject(int i, Map map) throws SQLException 1126 { 1127 return _resultSet.getObject(i, map); 1128 } 1129 1130 1133 public String getString(String columnName) throws SQLException 1134 { 1135 return _resultSet.getString(columnName); 1136 } 1137 1138 1141 public void updateString(String columnName, String x) throws SQLException 1142 { 1143 _resultSet.updateString(columnName, x); 1144 } 1145 1146 1149 public BigDecimal getBigDecimal(String columnName) throws SQLException 1150 { 1151 return _resultSet.getBigDecimal(columnName); 1152 } 1153 1154 1158 public BigDecimal getBigDecimal(String columnName, int scale) throws SQLException 1159 { 1160 return _resultSet.getBigDecimal(columnName, scale); 1161 } 1162 1163 1167 public void updateBigDecimal(String columnName, BigDecimal x) throws SQLException 1168 { 1169 _resultSet.updateBigDecimal(columnName, x); 1170 } 1171 1172 1175 public URL getURL(String columnName) throws SQLException 1176 { 1177 return _resultSet.getURL(columnName); 1178 } 1179 1180 1183 public Array getArray(String colName) throws SQLException 1184 { 1185 return _resultSet.getArray(colName); 1186 } 1187 1188 1191 public void updateArray(String columnName, Array x) throws SQLException 1192 { 1193 _resultSet.updateArray(columnName, x); 1194 } 1195 1196 1199 public Blob getBlob(String colName) throws SQLException 1200 { 1201 return _resultSet.getBlob(colName); 1202 } 1203 1204 1207 public void updateBlob(String columnName, Blob x) throws SQLException 1208 { 1209 _resultSet.updateBlob(columnName, x); 1210 } 1211 1212 1215 public Clob getClob(String colName) throws SQLException 1216 { 1217 return _resultSet.getClob(colName); 1218 } 1219 1220 1223 public void updateClob(String columnName, Clob x) throws SQLException 1224 { 1225 _resultSet.updateClob(columnName, x); 1226 } 1227 1228 1231 public Date getDate(String columnName) throws SQLException 1232 { 1233 return _resultSet.getDate(columnName); 1234 } 1235 1236 1239 public void updateDate(String columnName, Date x) throws SQLException 1240 { 1241 _resultSet.updateDate(columnName, x); 1242 } 1243 1244 1247 public Date getDate(int columnIndex, Calendar cal) throws SQLException 1248 { 1249 return _resultSet.getDate(columnIndex, cal); 1250 } 1251 1252 1255 public Ref getRef(String colName) throws SQLException 1256 { 1257 return _resultSet.getRef(colName); 1258 } 1259 1260 1263 public void updateRef(String columnName, Ref x) throws SQLException 1264 { 1265 _resultSet.updateRef(columnName, x); 1266 } 1267 1268 1271 public Time getTime(String columnName) throws SQLException 1272 { 1273 return _resultSet.getTime(columnName); 1274 } 1275 1276 1279 public void updateTime(String columnName, Time x) throws SQLException 1280 { 1281 _resultSet.updateTime(columnName, x); 1282 } 1283 1284 1287 public Time getTime(int columnIndex, Calendar cal) throws SQLException 1288 { 1289 return _resultSet.getTime(columnIndex, cal); 1290 } 1291 1292 1295 public Timestamp getTimestamp(String columnName) throws SQLException 1296 { 1297 return _resultSet.getTimestamp(columnName); 1298 } 1299 1300 1304 public void updateTimestamp(String columnName, Timestamp x) throws SQLException 1305 { 1306 _resultSet.updateTimestamp(columnName, x); 1307 } 1308 1309 1312 public Timestamp getTimestamp(int columnIndex, Calendar cal) throws SQLException 1313 { 1314 return _resultSet.getTimestamp(columnIndex, cal); 1315 } 1316 1317 1320 public Object getObject(String colName, Map map) throws SQLException 1321 { 1322 return _resultSet.getObject(colName, map); 1323 } 1324 1325 1328 public Date getDate(String columnName, Calendar cal) throws SQLException 1329 { 1330 return _resultSet.getDate(columnName, cal); 1331 } 1332 1333 1336 public Time getTime(String columnName, Calendar cal) throws SQLException 1337 { 1338 return _resultSet.getTime(columnName, cal); 1339 } 1340 1341 1345 public Timestamp getTimestamp(String columnName, Calendar cal) throws SQLException 1346 { 1347 return _resultSet.getTimestamp(columnName, cal); 1348 } 1349 1350 1351 1352 1355 public ObjectValidator getValidator() 1356 { 1357 return _validator; 1358 } 1359 1360 1363 public void setValidator(ObjectValidator validator) 1364 { 1365 _validator = validator; 1366 } 1367 1368 1371 public ResultSet getResultSet() 1372 { 1373 return _resultSet; 1374 } 1375 1376 1379 public void setResultSet(ResultSet rs) 1380 { 1381 if (rs != _resultSet) 1382 { 1383 _resultSet = rs; 1384 reset(); 1385 } 1386 } 1387 1388 private void reset() 1389 { 1390 _index = new int[INITIAL_CAPACITY]; 1391 _currentRow = 0; 1392 _size = 0; 1393 _isComplete = false; 1394 } 1395} | Popular Tags |