1 21 22 package org.apache.derbyTesting.functionTests.tests.largedata; 23 24 import java.sql.*; 25 import java.io.*; 26 27 import org.apache.derby.tools.ij; 28 import org.apache.derby.tools.JDBCDisplayUtil; 29 30 37 38 public class LobLimits { 39 40 static boolean trace = false; 41 static final int _2GB = 2 * 1024 * 1024* 1024 - 1; 42 static final int _100MB = 100 * 1024 * 1024; 43 static final int MORE_DATA_THAN_COL_WIDTH= (_100MB)+1; 44 static final int NUM_TRAILING_SPACES = 33*1024; 45 46 static PreparedStatement insertBlob = null; 47 static PreparedStatement selectBlob = null; 48 static PreparedStatement insertClob = null; 49 static PreparedStatement selectClob = null; 50 static PreparedStatement deleteBlob = null; 51 static PreparedStatement deleteClob = null; 52 static PreparedStatement insertBlob2 = null; 53 static PreparedStatement selectBlob2 = null; 54 static PreparedStatement insertClob2 = null; 55 static PreparedStatement selectClob2 = null; 56 static PreparedStatement deleteBlob2 = null; 57 static PreparedStatement deleteClob2 = null; 58 59 static final String DATAFILE = "byteLobLimits.dat"; 60 61 static final String CHARDATAFILE = "charLobLimits.txt"; 62 63 68 private void setup(Connection conn) throws SQLException { 69 System.out.println("-----------------------------------"); 70 System.out.println(" START setup"); 71 72 conn.setAutoCommit(true); 73 Statement s = conn.createStatement(); 75 try { 76 s.execute("DROP TABLE BLOBTBL"); 77 } catch (Exception e) { 78 } 79 try { 80 s.execute("DROP TABLE CLOBTBL"); 81 } catch (Exception e) { 82 } 83 try { 84 s.execute("DROP TABLE BLOBTBL2"); 85 } catch (Exception e) { 86 } 87 try { 88 s.execute("DROP TABLE CLOBTBL2"); 89 } catch (Exception e) { 90 } 91 92 s.execute("CREATE TABLE BLOBTBL (ID INT NOT NULL PRIMARY KEY, " 93 + "POS BIGINT, DLEN BIGINT, CONTENT BLOB(2G))"); 94 95 insertBlob = conn 96 .prepareStatement("INSERT INTO BLOBTBL values (?,?,?,?)"); 97 98 s.execute("CREATE TABLE CLOBTBL (ID INT NOT NULL PRIMARY KEY," 99 + "POS BIGINT, DLEN BIGINT, CONTENT CLOB(2G))"); 100 101 insertBlob = conn 102 .prepareStatement("INSERT INTO BLOBTBL values (?,?,?,?)"); 103 selectBlob = conn 104 .prepareStatement("SELECT CONTENT,DLEN FROM BLOBTBL WHERE ID = ?"); 105 106 insertClob = conn 107 .prepareStatement("INSERT INTO CLOBTBL values (?,?,?,?)"); 108 109 selectClob = conn 110 .prepareStatement("SELECT CONTENT,DLEN FROM CLOBTBL WHERE ID = ?"); 111 112 deleteBlob = conn.prepareStatement("DELETE FROM BLOBTBL"); 113 deleteClob = conn.prepareStatement("DELETE from CLOBTBL"); 114 115 s.execute("CREATE TABLE BLOBTBL2 (ID INT NOT NULL PRIMARY KEY, " 116 + "POS BIGINT, CONTENT BLOB("+_100MB+"),DLEN BIGINT)"); 117 118 insertBlob2 = conn 119 .prepareStatement("INSERT INTO BLOBTBL2 values (?,?,?,?)"); 120 121 s.execute("CREATE TABLE CLOBTBL2 (ID INT NOT NULL PRIMARY KEY," 124 + "POS BIGINT, CONTENT CLOB("+_100MB+"), DLEN BIGINT)"); 125 126 insertBlob2 = conn 127 .prepareStatement("INSERT INTO BLOBTBL2 values (?,?,?,?)"); 128 selectBlob2 = conn 129 .prepareStatement("SELECT CONTENT,DLEN FROM BLOBTBL2 WHERE ID = ?"); 130 131 insertClob2 = conn 132 .prepareStatement("INSERT INTO CLOBTBL2 values (?,?,?,?)"); 133 134 selectClob2 = conn 135 .prepareStatement("SELECT CONTENT,DLEN FROM CLOBTBL2 WHERE ID = ?"); 136 System.out.println("-----------------------------------"); 137 System.out.println(" END setup"); 138 139 deleteBlob2 = conn.prepareStatement("DELETE FROM BLOBTBL2"); 140 deleteClob2 = conn.prepareStatement("DELETE from CLOBTBL2"); 141 System.out.println("-----------------------------------"); 142 System.out.println(" END setup"); 143 144 } 145 146 149 public static void main(String [] args) { 150 new LobLimits().runTests(args); 152 153 } 154 155 162 public void runTests(String [] args) { 163 Connection conn = null; 164 try { 165 166 ij.getPropertyArg(args); 169 conn = ij.startJBMS(); 170 171 setup(conn); 174 175 conn.setAutoCommit(false); 176 177 clobTests(conn); 178 blobTests(conn); 179 cleanup(conn); 181 } catch (Exception e) { 182 System.out.println("FAIL -- Unexpected exception:"); 183 e.printStackTrace(System.out); 184 } 185 } 186 187 192 private void cleanup(Connection conn) throws Exception { 193 insertBlob.close(); 194 selectBlob.close(); 195 selectClob.close(); 196 insertClob.close(); 197 deleteClob.close(); 198 deleteBlob.close(); 199 insertBlob2.close(); 200 selectBlob2.close(); 201 selectClob2.close(); 202 insertClob2.close(); 203 deleteBlob2.close(); 204 deleteClob2.close(); 205 conn.close(); 206 new File(DATAFILE).delete(); 207 new File(CHARDATAFILE).delete(); 208 } 209 210 215 private static void blobTests(Connection conn) throws Exception { 216 217 try { 218 224 insertBlob_SetBinaryStream("BlobTest #1", conn, insertBlob, _2GB, 226 0, 2, _2GB); 227 selectBlob("BlobTest #2", conn, selectBlob, _2GB, 0, 1); 229 selectBlob("BlobTest #3", conn, selectBlob, _2GB, 1, 1); 230 231 selectUpdateBlob("BlobTest #4",conn,selectBlob,_2GB,0,1,1); 236 selectInsertBlob("BlobTest #4.1",conn,selectBlob,insertBlob,_2GB,0,3,1); 239 240 245 FileOutputStream fos = new FileOutputStream(DATAFILE); 246 RandomByteStream r = new RandomByteStream(new java.util.Random (), 247 _100MB); 248 byte[] buffer = new byte[32 * 1024]; 250 int count = 0; 251 252 while((count=r.read(buffer))>=0) 253 fos.write(buffer,0,count); 254 255 fos.flush(); 256 fos.close(); 257 258 insertBlob2("BlobTest #5.1 ", conn, insertBlob2, _100MB, 0, 1, 259 _100MB, DATAFILE); 260 selectBlob2("BlobTest #5.2 ", conn, selectBlob2, _100MB, 0, 1, 261 DATAFILE); 262 263 264 selectUpdateBlob2("BlobTest #6",conn,selectBlob2,selectBlob,_100MB,0,1,1,DATAFILE); 267 268 deleteTable(conn, deleteBlob2, 1); 269 270 } catch (Exception e) { 271 System.out.println("FAIL -- Unexpected exception:"); 272 e.printStackTrace(System.out); 273 } 274 275 conn.commit(); 276 277 deleteTable(conn, deleteBlob, 3); 278 279 long _4GB = 4*1024*1024*(1024L); 281 BlobImpl _4GbBlob = new BlobImpl(new RandomByteStream(new java.util.Random (),_4GB),_4GB); 282 283 try 284 { 285 insertBlob_SetBlob("BlobTest #7 (setBlob with 4Gb blob",conn,insertBlob,_4GbBlob, 286 _4GB,0,1,0); 287 } 288 catch(SQLException sqle) 289 { 290 System.out.println("DERBY DOES NOT SUPPORT INSERT OF 4GB BLOB "); 291 expectedException(sqle); 292 } 293 } 295 296 301 private static void clobTests(Connection conn) throws Exception { 302 try { 303 insertClob_SetCharacterStream("ClobTest #1", conn, insertClob, 308 _2GB, 0, 2, _2GB); 309 selectClob("ClobTest #2", conn, selectClob, _2GB, 0, 1); 311 selectClob("ClobTest #3", conn, selectClob, _2GB, 0, 1); 312 selectUpdateClob("ClobTest #4",conn,selectClob,_2GB,0,1,1); 314 315 316 writeToFile(CHARDATAFILE,new RandomCharReader(new java.util.Random (),_100MB)); 321 insertClob2("ClobTest #5.1 ", conn, insertClob2, _100MB, 0, 1, 322 _100MB, CHARDATAFILE); 323 selectClob2("ClobTest #5.2 ", conn, selectClob2, _100MB, 0, 1, 324 CHARDATAFILE); 325 326 330 selectUpdateClob2("ClobTest #8.2",conn,selectClob2,selectClob,_100MB,0,1,1,CHARDATAFILE); 333 334 339 writeToFile(CHARDATAFILE,new RandomCharReader(new java.util.Random (), 342 (NUM_TRAILING_SPACES +_100MB),NUM_TRAILING_SPACES)); 343 insertClob2("ClobTest #6.1 ", conn, insertClob2,_100MB, 3, 1, 344 (NUM_TRAILING_SPACES +_100MB), CHARDATAFILE); 345 selectClob2("ClobTest #6.2 ", conn, selectClob2, _100MB, 3, 1, 347 CHARDATAFILE); 348 349 negativeSpaceTruncationTest("ClobTest #7",conn); 350 351 writeToFile(CHARDATAFILE,new RandomCharReader(new java.util.Random (),MORE_DATA_THAN_COL_WIDTH)); 354 try 355 { 356 insertClob2("ClobTest #9.1 ", conn, insertClob2,MORE_DATA_THAN_COL_WIDTH, 4, 1, 357 MORE_DATA_THAN_COL_WIDTH, CHARDATAFILE); 358 }catch(SQLException sqle) 359 { 360 System.out.println("NEGATIVE TEST - Expected Exception: truncation of non-blanks not allowed"); 361 expectedException(sqle); 362 } 363 selectClob2("ClobTest #9.2 ", conn, selectClob2, _100MB, 4, 0, 365 CHARDATAFILE); 366 367 try 368 { 369 insertClob2("ClobTest #10 ", conn, insertClob2,MORE_DATA_THAN_COL_WIDTH, 4, 1, 370 MORE_DATA_THAN_COL_WIDTH +1 , CHARDATAFILE); 371 }catch(SQLException sqle) 372 { 373 System.out.println("NEGATIVE TEST - Expected Exception: truncation of non-blanks not allowed and"+ 374 " stream length is one greater than actual length of the stream "); 375 expectedException(sqle); 376 } 377 378 try 379 { 380 insertClob2("ClobTest #11 ", conn, insertClob2,MORE_DATA_THAN_COL_WIDTH, 4, 1, 381 MORE_DATA_THAN_COL_WIDTH -1 , CHARDATAFILE); 382 }catch(SQLException sqle) 383 { 384 System.out.println("NEGATIVE TEST - Expected Exception: truncation of non-blanks not allowed and"+ 385 " stream length is one less than actual length of the stream "); 386 expectedException(sqle); 387 } 388 deleteTable(conn, deleteClob2, 2); 389 } catch (Exception e) { 390 System.out.println("FAIL -- Unexpected exception:"); 391 e.printStackTrace(System.out); 392 } 393 394 try { 395 insertClob_SetCharacterStream("ClobTest #12.1", conn, insertClob, 397 _100MB, 4, 1, -1); 398 } catch (SQLException sqle) { 399 System.out.println("NEGATIVE TEST - Expected Exception:"); 400 expectedException(sqle); 401 } 402 403 selectClob("ClobTest #12.2", conn, selectClob,_100MB, 4, 0); 404 405 deleteTable(conn, deleteClob, 2); 406 407 409 long _4GB = 4*1024*1024*(1024L); 410 411 ClobImpl _4GBClob = new ClobImpl(new RandomCharReader(new java.util.Random (),_4GB),_4GB); 412 413 try 414 { 415 insertClob_SetClob("ClobTest #13 (setClob with 4Gb clob",conn,insertClob,_4GBClob, 416 _4GB,0,1,0); 417 } 418 catch(SQLException sqle) 419 { 420 System.out.println("DERBY DOES NOT SUPPORT INSERT OF 4GB CLOB "); 421 expectedException(sqle); 422 } 423 424 } 426 427 private static void negativeSpaceTruncationTest(String msg,Connection conn) 428 throws Exception 429 { 430 try 433 { 434 insertClob2(msg, conn, insertClob2,_100MB, 4, 1, 435 (NUM_TRAILING_SPACES +_100MB - 1), CHARDATAFILE); 436 }catch(SQLException sqle) 437 { 438 System.out.println("EXPECTED EXCEPTION - stream has trailing spaces,but stream "+ 439 " length is 1 less than actual length of stream"); 440 expectedException(sqle); 441 } 442 443 try 444 { 445 insertClob2(msg, conn, insertClob2,_100MB, 5, 1, 446 (NUM_TRAILING_SPACES +_100MB + 1), CHARDATAFILE); 447 }catch(SQLException sqle) 448 { 449 System.out.println("EXPECTED EXCEPTION - stream has trailing spaces,but stream "+ 450 " length is 1 greater than actual length of stream"); 451 expectedException(sqle); 452 } 453 } 454 455 456 463 private static void insertBlob_SetBinaryStream(String testId, 464 Connection conn, PreparedStatement ps, int bloblen, int start, 465 int rows, int streamLength) throws SQLException { 466 System.out.println("========================================"); 467 System.out.println("START " + testId + "insertBlob of size = " 468 + bloblen); 469 long ST = 0; 470 if (trace) 471 ST = System.currentTimeMillis(); 472 473 int count = 0; 474 java.util.Random random = new java.util.Random (); 475 for (int i = start; i < start + rows; i++) { 476 ps.setInt(1, i); 477 ps.setInt(2, 0); 478 ps.setLong(3, bloblen); 479 ps.setBinaryStream(4, new RandomByteStream(random, bloblen), 480 streamLength); 481 count += ps.executeUpdate(); 482 } 483 conn.commit(); 484 if (trace) { 485 System.out.println("Insert Blob (" + bloblen + ")" + " rows= " 486 + count + " = " + (long) (System.currentTimeMillis() - ST)); 487 488 } 489 verifyTest(count, rows, " Rows inserted with blob of size (" + bloblen 490 + ") ="); 491 System.out.println("========================================"); 492 493 } 494 495 508 private static void insertBlob_SetBlob(String testId, Connection conn, 509 PreparedStatement ps, java.sql.Blob blob, long bloblen, int start, 510 int rows, int expectedRows) throws SQLException { 511 System.out.println("========================================"); 512 System.out.println("START " + testId + "insertBlob of size = " 513 + bloblen); 514 long ST = 0; 515 if (trace) 516 ST = System.currentTimeMillis(); 517 int count = 0; 518 519 try { 520 521 for (int i = start; i < start + rows; i++) { 522 ps.setInt(1, i); 523 ps.setInt(2, 0); 524 ps.setLong(3, bloblen); 525 ps.setBlob(4, blob); 526 count += ps.executeUpdate(); 527 } 528 conn.commit(); 529 if (trace) { 530 System.out.println("Insert Blob (" + bloblen + ")" + " rows= " 531 + count + " = " 532 + (long) (System.currentTimeMillis() - ST)); 533 534 } 535 } catch (SQLException e) { 536 verifyTest(count, expectedRows, 537 " Rows inserted with blob of size (" + bloblen + ") ="); 538 System.out.println("========================================"); 539 throw e; 540 } 541 542 verifyTest(count, expectedRows, 543 " Rows inserted with blob of size (" + bloblen + ") ="); 544 System.out.println("========================================"); 545 546 } 547 548 549 555 private static void selectBlob(String testId, Connection conn, 556 PreparedStatement ps, int bloblen, int id, int expectedRows) 557 throws SQLException { 558 System.out.println("========================================"); 559 System.out.println("START " + testId + " - SELECT BLOB of size = " 560 + bloblen); 561 562 long ST = 0; 563 ResultSet rs = null; 564 565 if (trace) 566 ST = System.currentTimeMillis(); 567 568 int count = 0; 569 ps.setInt(1, id); 570 rs = ps.executeQuery(); 571 572 while (rs.next()) { 573 count++; 574 Blob value = rs.getBlob(1); 575 long l = value.length(); 576 long dlen = rs.getLong(2); 577 if (dlen != l) { 578 System.out.println("FAIL - MISMATCH LENGTHS GOT " + l 579 + " expected " + dlen + " for row in BLOBTBL with ID=" 580 + id); 581 } 582 } 583 conn.commit(); 584 585 verifyTest(count, expectedRows, 586 "Matched rows selected with blob of size(" + bloblen + ") ="); 587 588 if (trace) { 589 System.out.println("Select Blob (" + bloblen + ")" + " rows= " 590 + expectedRows + " = " 591 + (long) (System.currentTimeMillis() - ST)); 592 } 593 System.out.println("========================================"); 594 } 595 596 604 605 private static void insertBlob2(String testId, Connection conn, 606 PreparedStatement ps, int bloblen, int start, int rows, 607 int streamLength, String file) throws Exception { 608 System.out.println("========================================"); 609 System.out.println("START " + testId + "insert Blob of size = " 610 + bloblen); 611 int count = 0; 612 java.util.Random random = new java.util.Random (); 613 FileInputStream fis = null; 614 615 long ST = 0; 616 if (trace) 617 ST = System.currentTimeMillis(); 618 619 for (int i = start; i < start + rows; i++) { 620 fis = new FileInputStream(file); 621 ps.setInt(1, i); 622 ps.setInt(2, 0); 623 ps.setLong(4, bloblen); 624 ps.setBinaryStream(3, fis, streamLength); 625 count += ps.executeUpdate(); 626 fis.close(); 627 } 628 conn.commit(); 629 if (trace) { 630 System.out.println("Insert Blob (" + bloblen + ")" + " rows= " 631 + count + " = " + (long) (System.currentTimeMillis() - ST)); 632 633 } 634 verifyTest(count, rows, " Rows inserted with blob of size (" + bloblen 635 + ") ="); 636 System.out.println("========================================"); 637 638 } 639 640 648 private static void selectBlob2(String testId, Connection conn, 649 PreparedStatement ps, int bloblen, int id, int expectedRows, 650 String file) throws Exception { 651 System.out.println("========================================"); 652 System.out.println("START " + testId + " - SELECT BLOB of size = " 653 + bloblen); 654 655 long ST = 0; 656 ResultSet rs = null; 657 658 if (trace) 659 ST = System.currentTimeMillis(); 660 661 int count = 0; 662 ps.setInt(1, id); 663 rs = ps.executeQuery(); 664 665 while (rs.next()) { 666 count++; 667 Blob value = rs.getBlob(1); 668 long l = value.length(); 669 long dlen = rs.getLong(2); 670 if (dlen != l) { 671 System.out.println("FAIL - MISMATCH LENGTHS GOT " + l 672 + " expected " + dlen + " for row in BLOBTBL with ID=" 673 + id); 674 } else 675 compareBlobToFile(value.getBinaryStream(), file); 676 } 677 conn.commit(); 678 679 verifyTest(count, expectedRows, 680 "Matched rows selected with blob of size(" + bloblen + ") ="); 681 682 if (trace) { 683 System.out.println("Select Blob (" + bloblen + ")" + " rows= " 684 + expectedRows + " = " 685 + (long) (System.currentTimeMillis() - ST)); 686 } 687 System.out.println("========================================"); 688 } 689 690 700 private static void selectUpdateBlob(String testId, Connection conn, 701 PreparedStatement ps, int bloblen, int id, int updateId, 702 int expectedRows) throws Exception { 703 System.out.println("========================================"); 704 System.out.println("START " + testId + " - select and then update blob of size= " 705 + bloblen + " - Uses getBlob api"); 706 707 ResultSet rs = null; 708 709 ps.setInt(1, id); 710 rs = ps.executeQuery(); 711 rs.next(); 712 Blob value = rs.getBlob(1); 713 long l = value.length(); 714 long dlen = rs.getLong(2); 715 if (dlen != l) { 716 System.out 717 .println("FAIL - MISMATCH LENGTHS GOT " + l + " expected " 718 + dlen + " for row in BLOBTBL with ID=" + id); 719 } 720 721 PreparedStatement psUpd = conn 722 .prepareStatement("update BLOBTBL set content=?,dlen =? where id = ?"); 723 psUpd.setBlob(1,value); 724 psUpd.setLong(2, l); 725 psUpd.setInt(3, updateId); 726 727 System.out.println("Rows Updated = " + psUpd.executeUpdate()); 728 conn.commit(); 729 730 ps.setInt(1, updateId); 732 ResultSet rs2 = ps.executeQuery(); 733 rs2.next(); 734 Blob updatedValue = rs2.getBlob(1); 735 736 if(updatedValue.length() != l) 737 System.out.println("FAIL - Retrieving the updated blob length does not match "+ 738 "expected length = "+l +" found = "+ updatedValue.length()); 739 740 conn.commit(); 742 rs.close(); 743 rs2.close(); 744 psUpd.close(); 745 System.out.println("========================================"); 746 } 747 748 758 private static void selectInsertBlob(String testId, Connection conn, 759 PreparedStatement ps,PreparedStatement ins, int bloblen, int id, int insertId, 760 int expectedRows) throws Exception { 761 System.out.println("========================================"); 762 System.out.println("START " + testId + " - select and then insert blob of size= " 763 + bloblen + " - Uses getBlob api to do select and setBlob for insert"); 764 765 ResultSet rs = null; 766 767 ps.setInt(1, id); 768 rs = ps.executeQuery(); 769 rs.next(); 770 Blob value = rs.getBlob(1); 771 long l = value.length(); 772 long dlen = rs.getLong(2); 773 if (dlen != l) { 774 System.out 775 .println("FAIL - MISMATCH LENGTHS GOT " + l + " expected " 776 + dlen + " for row in BLOBTBL with ID=" + id); 777 } 778 779 ins.setInt(1,insertId); 780 ins.setInt(2,0); 781 ins.setLong(3,l); 782 ins.setBlob(4,value); 783 784 System.out.println("Rows Updated = " + ins.executeUpdate()); 785 conn.commit(); 786 787 ps.setInt(1, insertId); 789 ResultSet rs2 = ps.executeQuery(); 790 rs2.next(); 791 Blob insertedValue = rs2.getBlob(1); 792 793 if(insertedValue.length() != l) 794 System.out.println("FAIL - Retrieving the updated blob length does not match "+ 795 "expected length = "+l +" found = "+ insertedValue.length()); 796 797 conn.commit(); 799 rs.close(); 800 rs2.close(); 801 System.out.println("========================================"); 802 } 803 804 805 816 private static void selectUpdateBlob2(String testId, Connection conn, 817 PreparedStatement ps,PreparedStatement sel,int bloblen, int id, int updateId, 818 int expectedRows,String file) throws Exception { 819 System.out.println("========================================"); 820 System.out.println("START " + testId + " - select and then update blob of size= " 821 + bloblen + " - Uses getBlob and setBlob api"); 822 823 ResultSet rs = null; 824 825 ps.setInt(1, id); 827 rs = ps.executeQuery(); 828 rs.next(); 829 Blob value = rs.getBlob(1); 830 long l = value.length(); 831 long dlen = rs.getLong(2); 832 if (dlen != l) { 833 System.out 834 .println("FAIL - MISMATCH LENGTHS GOT " + l + " expected " 835 + dlen + " for row in BLOBTBL2 with ID=" + id); 836 } 837 838 PreparedStatement psUpd = conn 839 .prepareStatement("update BLOBTBL set content=?,dlen =? where id = ?"); 840 psUpd.setBlob(1,value); 841 psUpd.setLong(2, l); 842 psUpd.setInt(3, updateId); 843 844 System.out.println("Rows Updated = " + psUpd.executeUpdate()); 845 conn.commit(); 846 847 sel.setInt(1, updateId); 849 ResultSet rs2 = sel.executeQuery(); 850 rs2.next(); 851 Blob updatedValue = rs2.getBlob(1); 852 853 if (updatedValue.length() != l) 854 { 855 System.out.println("FAIL - MISMATCH length of updated blob value : expected="+ 856 l+" found ="+updatedValue.length()); 857 } 858 else 859 compareBlobToFile(updatedValue.getBinaryStream(),file); 860 861 conn.commit(); 863 rs.close(); 864 rs2.close(); 865 psUpd.close(); 866 System.out.println("========================================"); 867 868 } 869 870 private static void compareBlobToFile(InputStream lobstream, String filename) 871 throws Exception { 872 FileInputStream file = new FileInputStream(filename); 873 int l = 0; 874 int b = 0; 875 do { 876 l = lobstream.read(); 877 b = file.read(); 878 if (l != b) 879 { 880 System.out.println("FAIL -- MISMATCH in data stored versus"+ 881 "data retrieved"); 882 break; 883 } 884 } while (l != -1 && b != -1); 885 } 886 887 private static void deleteTable(Connection conn, PreparedStatement ps, 888 int expectedRows) throws SQLException { 889 int count = ps.executeUpdate(); 890 conn.commit(); 891 verifyTest(count, expectedRows, "Rows deleted ="); 892 } 893 894 895 902 private static void insertClob_SetCharacterStream(String testId, 903 Connection conn, PreparedStatement ps, int cloblen, int start, 904 int rows, int streamLength) throws SQLException { 905 System.out.println("========================================"); 906 System.out.println("START " + testId + " -insertClob of size = " 907 + cloblen); 908 909 long ST = 0; 910 java.util.Random random = new java.util.Random (); 911 int count = 0; 912 if (trace) 913 ST = System.currentTimeMillis(); 914 915 for (int i = start; i < start + rows; i++) { 916 ps.setInt(1, i); 917 ps.setInt(2, 0); 918 ps.setLong(3, cloblen); 919 ps.setCharacterStream(4, new RandomCharReader(random, cloblen), 920 streamLength); 921 count += ps.executeUpdate(); 922 } 923 conn.commit(); 924 if (trace) { 925 System.out.println("Insert Clob (" + cloblen + ")" + " rows= " 926 + count + " = " + (long) (System.currentTimeMillis() - ST)); 927 928 } 929 verifyTest(count, rows, "Rows inserted with clob of size (" + cloblen 930 + ") = "); 931 System.out.println("========================================"); 932 933 } 934 935 948 private static void insertClob_SetClob(String testId, Connection conn, 949 PreparedStatement ps, java.sql.Clob clob, long cloblen, int start, 950 int rows, int expectedRows) throws SQLException { 951 System.out.println("========================================"); 952 System.out.println("START " + testId + "insertClob of size = " 953 + cloblen); 954 long ST = 0; 955 if (trace) 956 ST = System.currentTimeMillis(); 957 int count = 0; 958 959 try { 960 961 for (int i = start; i < start + rows; i++) { 962 ps.setInt(1, i); 963 ps.setInt(2, 0); 964 ps.setLong(3, cloblen); 965 ps.setClob(4, clob); 966 count += ps.executeUpdate(); 967 } 968 conn.commit(); 969 if (trace) { 970 System.out.println("Insert Clob (" + cloblen + ")" + " rows= " 971 + count + " = " 972 + (long) (System.currentTimeMillis() - ST)); 973 974 } 975 } catch (SQLException e) { 976 verifyTest(count, expectedRows, 977 " Rows inserted with clob of size (" + cloblen + ") ="); 978 System.out.println("========================================"); 979 throw e; 980 } 981 982 verifyTest(count, expectedRows, 983 " Rows inserted with clob of size (" + cloblen + ") ="); 984 System.out.println("========================================"); 985 986 } 987 988 994 private static void selectClob(String testId, Connection conn, 995 PreparedStatement ps, int cloblen, int id, int expectedRows) 996 throws SQLException { 997 System.out.println("========================================"); 998 System.out.println("START " + testId + " - SELECT CLOB of size = " 999 + cloblen); 1000 1001 long ST = 0; 1002 int count = 0; 1003 ResultSet rs = null; 1004 if (trace) 1005 ST = System.currentTimeMillis(); 1006 1007 ps.setInt(1, id); 1008 rs = ps.executeQuery(); 1009 while (rs.next()) { 1010 count++; 1011 Clob value = rs.getClob(1); 1012 long l = value.length(); 1013 long dlen = rs.getLong(2); 1014 if (dlen != l) { 1015 System.out.println("FAIL - MISMATCH LENGTHS GOT " + l 1016 + " expected " + dlen + " for row in CLOBTBL with ID=" 1017 + id); 1018 } 1019 1020 } 1021 conn.commit(); 1022 if (trace) { 1023 System.out.println("Select Clob (" + cloblen + ")" + " rows= " 1024 + expectedRows + " = " 1025 + (long) (System.currentTimeMillis() - ST)); 1026 1027 } 1028 1029 verifyTest(count, expectedRows, 1030 "Matched rows selected with clob of size(" + cloblen + ") ="); 1031 System.out.println("========================================"); 1032 1033 } 1034 1035 1043 private static void insertClob2(String testId, Connection conn, 1044 PreparedStatement ps, int cloblen, int start, int rows, 1045 int streamLength, String file) throws Exception { 1046 System.out.println("========================================"); 1047 System.out.println("START " + testId + "insert Clob of size = " 1048 + cloblen); 1049 int count = 0; 1050 FileReader reader = null; 1051 long ST = 0; 1052 if (trace) 1053 ST = System.currentTimeMillis(); 1054 1055 for (int i = start; i < start + rows; i++) { 1056 reader = new FileReader(file); 1057 ps.setInt(1, i); 1058 ps.setInt(2, 0); 1059 ps.setLong(4, cloblen); 1060 ps.setCharacterStream(3, reader, streamLength); 1061 count += ps.executeUpdate(); 1062 reader.close(); 1063 } 1064 conn.commit(); 1065 if (trace) { 1066 System.out.println("Insert Clob (" + cloblen + ")" + " rows= " 1067 + count + " = " + (long) (System.currentTimeMillis() - ST)); 1068 1069 } 1070 verifyTest(count, rows, " Rows inserted with clob of size (" + cloblen 1071 + ") ="); 1072 System.out.println("========================================"); 1073 1074 } 1075 1076 1083 private static void selectClob2(String testId, Connection conn, 1084 PreparedStatement ps, int cloblen, int id, int expectedRows, 1085 String file) throws SQLException, Exception { 1086 System.out.println("========================================"); 1087 System.out.println("START " + testId + " - SELECT CLOB of size = " 1088 + cloblen); 1089 1090 long ST = 0; 1091 ResultSet rs = null; 1092 1093 if (trace) 1094 ST = System.currentTimeMillis(); 1095 1096 int count = 0; 1097 ps.setInt(1, id); 1098 rs = ps.executeQuery(); 1099 1100 while (rs.next()) { 1101 count++; 1102 Clob value = rs.getClob(1); 1103 long l = value.length(); 1104 long dlen = rs.getLong(2); 1105 if (cloblen != l) { 1106 System.out.println("FAIL - MISMATCH LENGTHS GOT " + l 1107 + " expected " + dlen + " for row in CLOBTBL2 with ID=" 1108 + id); 1109 } else 1110 compareClobToFile(value.getCharacterStream(), file, cloblen); 1111 } 1112 conn.commit(); 1113 1114 verifyTest(count, expectedRows, 1115 "Matched rows selected with clob of size(" + cloblen + ") ="); 1116 1117 if (trace) { 1118 System.out.println("Select Clob (" + cloblen + ")" + " rows= " 1119 + expectedRows + " = " 1120 + (long) (System.currentTimeMillis() - ST)); 1121 } 1122 System.out.println("========================================"); 1123 } 1124 1125 1130 private static void selectUpdateClob(String testId, Connection conn, 1131 PreparedStatement ps, int cloblen, int id, int updateId, 1132 int expectedRows) throws Exception { 1133 System.out.println("========================================"); 1134 System.out.println("START " + testId + " - select and then update clob of size= " 1135 + cloblen + " - Uses setClob api"); 1136 1137 ResultSet rs = null; 1138 1139 ps.setInt(1, id); 1140 rs = ps.executeQuery(); 1141 rs.next(); 1142 Clob value = rs.getClob(1); 1143 long l = value.length(); 1144 long dlen = rs.getLong(2); 1145 if (dlen != l) { 1146 System.out 1147 .println("FAIL - MISMATCH LENGTHS GOT " + l + " expected " 1148 + dlen + " for row in CLOBTBL with ID=" + id); 1149 } 1150 1151 PreparedStatement psUpd = conn 1152 .prepareStatement("update CLOBTBL set content=?,dlen =? where id = ?"); 1153 psUpd.setCharacterStream(1, value.getCharacterStream(), (int) l); 1154 psUpd.setLong(2, l); 1155 psUpd.setInt(3, updateId); 1156 1157 System.out.println("Rows Updated = " + psUpd.executeUpdate()); 1158 conn.commit(); 1159 1160 ps.setInt(1, updateId); 1162 ResultSet rs2 = ps.executeQuery(); 1163 rs2.next(); 1164 Clob updatedValue = rs2.getClob(1); 1165 1166 if(updatedValue.length() != l) 1167 System.out.println("FAIL - Retrieving the updated clob length does not match "+ 1168 "expected length = "+l +" found = "+ updatedValue.length()); 1169 1170 conn.commit(); 1172 rs.close(); 1173 rs2.close(); 1174 psUpd.close(); 1175 System.out.println("========================================"); 1176 } 1177 1178 1179 1184 private static void selectUpdateClob2(String testId, Connection conn, 1185 PreparedStatement ps, PreparedStatement sel,int cloblen, int id, int updateId, 1186 int expectedRows,String file) throws Exception { 1187 System.out.println("========================================"); 1188 System.out.println("START " + testId + " - select and then update clob of size= " 1189 + cloblen + " - Uses setClob api"); 1190 1191 ResultSet rs = null; 1192 1193 ps.setInt(1, id); 1195 rs = ps.executeQuery(); 1196 rs.next(); 1197 Clob value = rs.getClob(1); 1198 long l = value.length(); 1199 long dlen = rs.getLong(2); 1200 if (dlen != l) { 1201 System.out 1202 .println("FAIL - MISMATCH LENGTHS GOT " + l + " expected " 1203 + dlen + " for row in CLOBTBL2 with ID=" + id); 1204 } 1205 1206 PreparedStatement psUpd = conn 1207 .prepareStatement("update CLOBTBL set content=?,dlen =? where id = ?"); 1208 psUpd.setClob(1,value); 1209 psUpd.setLong(2, l); 1210 psUpd.setInt(3, updateId); 1211 1212 System.out.println("Rows Updated = " + psUpd.executeUpdate()); 1213 conn.commit(); 1214 1215 sel.setInt(1, updateId); 1217 ResultSet rs2 = sel.executeQuery(); 1218 rs2.next(); 1219 Clob updatedValue = rs2.getClob(1); 1220 1221 if (updatedValue.length() != l) 1222 { 1223 System.out.println("FAIL - MISMATCH length of updated clob value , found="+ 1224 updatedValue.length() +",expected = "+l); 1225 } 1226 else 1227 compareClobToFile(updatedValue.getCharacterStream(),file,(int)l); 1228 1229 conn.commit(); 1231 rs.close(); 1232 rs2.close(); 1233 psUpd.close(); 1234 System.out.println("========================================"); 1235 1236 } 1237 1238 1244 private static void updateClob2(String testId, Connection conn,PreparedStatement sel, 1245 int cloblen, int id, int updateRowId,int updateIdVal, 1246 int expectedRows,String file) throws Exception { 1247 System.out.println("========================================"); 1248 System.out.println("START " + testId + " - select and then update clob of size= " 1249 + cloblen + " - Uses updateClob api"); 1250 1251 1252 PreparedStatement ps1 = conn.prepareStatement("SELECT * FROM CLOBTBL FOR UPDATE", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE); 1253 PreparedStatement ps = conn.prepareStatement("SELECT CONTENT,DLEN FROM CLOBTBL2 where ID =?"); 1254 1255 ResultSet rs = null; 1256 ps.setInt(1,id); 1257 rs = ps.executeQuery(); 1259 rs.next(); 1260 Clob value = rs.getClob(1); 1261 long l = value.length(); 1262 long dlen = rs.getLong(2); 1263 if (dlen != l) { 1264 System.out 1265 .println("FAIL - MISMATCH LENGTHS GOT " + l + " expected " 1266 + dlen + " for row in CLOBTBL2 with ID=" + id); 1267 } 1268 1269 ResultSet rs1 = ps1.executeQuery(); 1270 while (rs1.next()) { 1271 if (rs1.getInt(1) == updateRowId) 1272 { 1273 rs1.updateClob(4, value); 1274 rs1.updateInt(1, updateIdVal); 1275 rs1.updateInt(2, 0); 1276 rs1.updateLong(3, dlen); 1277 rs1.updateRow(); 1278 break; 1279 } 1280 } 1281 conn.commit(); 1283 rs.close(); 1284 rs1.close(); 1285 ps1.close(); 1286 ps.close(); 1287 1288 sel.setInt(1, updateIdVal); 1291 ResultSet rs2 = sel.executeQuery(); 1292 rs2.next(); 1293 Clob updatedValue = rs2.getClob(1); 1294 1295 if (updatedValue.length() != l) 1296 { 1297 System.out.println("FAIL - MISMATCH length of updated clob value , found="+ 1298 updatedValue.length() +",expected = "+l); 1299 } 1300 else 1301 compareClobToFile(updatedValue.getCharacterStream(),file,(int)l); 1302 1303 1304 System.out.println("========================================"); 1305 1306 } 1307 1308 1309 private static void compareClobToFile(Reader lobstream, String filename,int length) 1310 throws Exception { 1311 FileReader file = new FileReader(filename); 1312 int c1 = 0; 1313 int c2 = 0; 1314 long count = 0; 1315 do { 1316 c1 = lobstream.read(); 1317 c2 = file.read(); 1318 if (c1 != c2) 1319 { 1320 System.out.println("FAIL -- MISMATCH in data stored versus data retrieved at " + count); 1321 break; 1322 } 1323 count++; 1324 length--; 1325 } while (c1 != -1 && c2 != -1 && length > 0); 1326 } 1327 1328 private static void expectedException(SQLException sqle) { 1329 1330 while (sqle != null) { 1331 String sqlState = sqle.getSQLState(); 1332 if (sqlState == null) { 1333 sqlState = "<NULL>"; 1334 } 1335 System.out.println("EXPECTED SQL Exception: (" + sqlState + ") " 1336 + sqle.getMessage()); 1337 1338 sqle = sqle.getNextException(); 1339 } 1340 } 1341 1342 private static void verifyTest(int affectedRows, int expectedRows, 1343 String test) { 1344 if (affectedRows != expectedRows) 1345 System.out.println("FAIL --" + test + affectedRows 1346 + " , but expected rows =" + expectedRows); 1347 else 1348 System.out.println(test + affectedRows); 1349 } 1350 1351 private static void writeToFile(String file,Reader r) 1352 throws IOException 1353 { 1354 File f =new File(file); 1356 if (f.exists()) 1357 f.delete(); 1358 FileWriter writer = new FileWriter(file); 1359 char[] buffer = new char[32 * 1024]; 1361 int count = 0; 1362 1363 while((count = r.read(buffer)) >=0) 1364 writer.write(buffer,0,count); 1365 writer.flush(); 1366 writer.close(); 1367 } 1368} 1369 1370 1373class RandomByteStream extends java.io.InputStream { 1374 private long length; 1375 1376 private java.util.Random dpr; 1377 1378 RandomByteStream(java.util.Random dpr, long length) { 1379 this.length = length; 1380 this.dpr = dpr; 1381 1382 } 1383 1384 public int read() { 1385 if (length <= 0) 1386 return -1; 1387 1388 length--; 1389 return (byte) (dpr.nextInt() >>> 25); 1390 } 1391 1392 public int read(byte[] data, int off, int len) { 1393 1394 if (length <= 0) 1395 return -1; 1396 1397 if (len > length) 1398 len = (int)length; 1399 1400 for (int i = 0; i < len; i++) { 1401 data[off + i] = (byte) (dpr.nextInt() >>> 25); 1403 } 1404 1405 length -= len; 1406 return len; 1407 } 1408} 1409 1410 1413class RandomCharReader extends java.io.Reader { 1414 private long length; 1415 private long numTrailingSpaces; 1416 1417 private java.util.Random dpr; 1418 1419 RandomCharReader(java.util.Random dpr, long length) { 1420 this.length = length; 1421 this.dpr = dpr; 1422 this.numTrailingSpaces = 0; 1423 } 1424 1425 RandomCharReader(java.util.Random dpr, long length,long numTrailingSpaces) { 1426 this.length = length; 1427 this.dpr = dpr; 1428 this.numTrailingSpaces = numTrailingSpaces; 1429 } 1430 1431 private int randomInt(int min, int max) { 1432 return dpr.nextInt(max - min) + min; 1433 } 1434 1435 private char getChar() { 1436 if (length <= numTrailingSpaces) 1438 { 1439 return ' '; 1440 } 1441 1442 double drand = dpr.nextDouble(); 1443 char c = 'a'; 1444 if (drand < 0.25) 1445 c = (char) randomInt((int) 'A', (int) 'Z'); 1446 else if (drand < 0.5) 1447 switch (randomInt(1, 10)) { 1448 case 1: 1449 c = '\u00c0'; 1450 break; 1451 case 2: 1452 c = '\u00c1'; 1453 break; 1454 case 3: 1455 c = '\u00c2'; 1456 break; 1457 case 4: 1458 c = '\u00ca'; 1459 break; 1460 case 5: 1461 c = '\u00cb'; 1462 break; 1463 case 6: 1464 c = '\u00d4'; 1465 break; 1466 case 7: 1467 c = '\u00d8'; 1468 break; 1469 case 8: 1470 c = '\u00d1'; 1471 break; 1472 case 9: 1473 c = '\u00cd'; 1474 break; 1475 default: 1476 c = '\u00dc'; 1477 break; 1478 } 1479 else if (drand < 0.75) 1480 c = (char) randomInt((int) 'a', (int) 'z'); 1481 else if (drand < 1.0) 1482 switch (randomInt(1, 10)) { 1483 case 1: 1484 c = '\u00e2'; 1485 break; 1486 case 2: 1487 c = '\u00e4'; 1488 break; 1489 case 3: 1490 c = '\u00e7'; 1491 break; 1492 case 4: 1493 c = '\u00e8'; 1494 break; 1495 case 5: 1496 c = '\u00ec'; 1497 break; 1498 case 6: 1499 c = '\u00ef'; 1500 break; 1501 case 7: 1502 c = '\u00f6'; 1503 break; 1504 case 8: 1505 c = '\u00f9'; 1506 break; 1507 case 9: 1508 c = '\u00fc'; 1509 break; 1510 default: 1511 c = '\u00e5'; 1512 break; 1513 } 1514 1515 return c; 1516 1517 } 1518 1519 public int read() { 1520 if (length <= 0) 1521 return -1; 1522 1523 length--; 1524 return getChar(); 1525 } 1526 1527 public int read(char[] data, int off, int len) { 1528 1529 if (length <= 0) 1530 return -1; 1531 1532 if (len > length) 1533 len = (int)length; 1534 1535 for (int i = 0; i < len; i++) { 1536 data[off + i] = getChar(); 1537 length -= 1; 1538 } 1539 1540 return len; 1541 } 1542 1543 public void close() { 1544 1545 } 1546} 1547 1548 1553 1554class ClobImpl implements java.sql.Clob { 1555 long length; 1556 Reader myReader; 1557 1558 public ClobImpl(Reader myReader,long length) { 1559 this.length = length; 1560 this.myReader = myReader; 1561 } 1562 1563 public long length() throws SQLException { 1564 return length; 1565 } 1566 1567 public String getSubString(long pos, int length) throws SQLException { 1568 throw new SQLException("Not implemented"); 1569 } 1570 1571 public java.io.Reader getCharacterStream() throws SQLException { 1572 return myReader; 1573 } 1574 1575 public java.io.InputStream getAsciiStream() throws SQLException { 1576 throw new SQLException("Not implemented"); 1577 } 1578 1579 public long position(String searchstr, long start) throws SQLException { 1580 throw new SQLException("Not implemented"); 1581 } 1582 1583 public long position(Clob searchstr, long start) throws SQLException { 1584 throw new SQLException("Not implemented"); 1585 } 1586 1587 public int setString(long pos, String str) throws SQLException { 1588 throw new SQLException("Not implemented"); 1589 } 1590 1591 public int setString(long pos, String str, int offset, int len) throws SQLException { 1592 throw new SQLException("Not implemented"); 1593 } 1594 1595 public java.io.OutputStream setAsciiStream(long pos) throws SQLException { 1596 throw new SQLException("Not implemented"); 1597 } 1598 1599 public java.io.Writer setCharacterStream(long pos) throws SQLException { 1600 throw new SQLException("Not implemented"); 1601 } 1602 1603 public void truncate(long len) throws SQLException { 1604 throw new SQLException("Not implemented"); 1605 } 1606 1607 public void free() throws SQLException { 1608 throw new SQLException("Not implemented"); 1609 } 1610 1611 public Reader getCharacterStream(long pos, long length) throws SQLException { 1612 throw new SQLException("Not implemented"); 1613 } 1614 1615} 1616 1617 1627class BlobImpl implements java.sql.Blob 1628{ 1629 long length; 1630 InputStream myStream; 1631 1632 public BlobImpl(InputStream is, long length) 1633 { 1634 this.myStream = is; 1635 this.length = length; 1636 } 1637 public InputStream getBinaryStream() 1638 throws SQLException 1639 { 1640 return myStream; 1641 } 1642 1643 public byte[] getBytes() 1644 throws SQLException 1645 { 1646 throw new SQLException("Not implemented"); 1647 } 1648 public long length() 1649 throws SQLException 1650 { 1651 return length; 1652 } 1653 1654 public long position(Blob pattern,long start) 1655 throws SQLException 1656 { 1657 throw new SQLException("Not implemented"); 1658 } 1659 1660 public long position(byte[] pattern,long start) 1661 throws SQLException 1662 { 1663 throw new SQLException("Not implemented"); 1664 } 1665 public OutputStream setBinaryStream(long pos) 1666 throws SQLException 1667 1668 { 1669 throw new SQLException("Not implemented"); 1670 } 1671 1672 public int setBytes(long pos,byte[] bytes) 1673 throws SQLException 1674 { 1675 throw new SQLException("Not implemented"); 1676 } 1677 public int setBytes(long pos,byte[] bytes,int offset,int len) 1678 throws SQLException 1679 { 1680 throw new SQLException("Not implemented"); 1681 } 1682 1683 public void truncate(long len) 1684 throws SQLException 1685 { 1686 throw new SQLException("Not implemented"); 1687 } 1688 1689 public byte[] getBytes(long pos, int length) 1690 throws SQLException 1691 { 1692 throw new SQLException("Not implemented"); 1693 } 1694 1695} 1696 | Popular Tags |