1 21 22 package org.apache.derbyTesting.functionTests.tests.jdbcapi; 23 24 import java.sql.CallableStatement ; 25 import java.sql.Connection ; 26 import java.sql.Date ; 27 import java.sql.DriverManager ; 28 import java.sql.ResultSetMetaData ; 29 import java.sql.ResultSet ; 30 import java.sql.PreparedStatement ; 31 import java.sql.Statement ; 32 import java.sql.SQLException ; 33 import java.sql.Time ; 34 import java.sql.Timestamp ; 35 import java.sql.Types ; 36 37 38 import java.lang.reflect.*; 39 40 import org.apache.derby.tools.ij; 41 import org.apache.derbyTesting.functionTests.util.SecurityCheck; 42 import org.apache.derbyTesting.functionTests.util.TestUtil; 43 import org.apache.derbyTesting.functionTests.util.JDBCTestDisplayUtil; 44 import org.apache.derby.iapi.reference.JDBC30Translation; 45 import org.apache.derby.iapi.reference.SQLState; 46 import org.apache.derbyTesting.functionTests.util.BigDecimalHandler; 47 48 63 64 public class resultset { 65 66 private static Class [] CONN_PARAM = { Integer.TYPE }; 67 private static Object [] CONN_ARG = { new Integer (JDBC30Translation.CLOSE_CURSORS_AT_COMMIT)}; 68 69 static private boolean isDerbyNet = false; 70 71 private static String VALID_DATE_STRING = "'2000-01-01'"; 72 private static String VALID_TIME_STRING = "'15:30:20'"; 73 private static String VALID_TIMESTAMP_STRING = "'2000-01-01 15:30:20'"; 74 private static String NULL_VALUE="NULL"; 75 76 private static String [] SQLTypes = 77 { 78 "SMALLINT", 79 "INTEGER", 80 "BIGINT", 81 "DECIMAL(10,5)", 82 "REAL", 83 "DOUBLE", 84 "CHAR(60)", 85 "VARCHAR(60)", 86 "LONG VARCHAR", 87 "CHAR(60) FOR BIT DATA", 88 "VARCHAR(60) FOR BIT DATA", 89 "LONG VARCHAR FOR BIT DATA", 90 "CLOB(1k)", 91 "DATE", 92 "TIME", 93 "TIMESTAMP", 94 "BLOB(1k)", 95 96 }; 97 98 private static String [] ColumnNames = 99 { 100 "SMALLINTCOL", 101 "INTEGERCOL", 102 "BIGINTCOL", 103 "DECIMALCOL", 104 "REALCOL", 105 "DOUBLECOL", 106 "CHARCOL", 107 "VARCHARCOL", 108 "LONGVARCHARCOL", 109 "CHARFORBITCOL", 110 "VARCHARFORBITCOL", 111 "LVARCHARFORBITCOL", 112 "CLOBCOL", 113 "DATECOL", 114 "TIMECOL", 115 "TIMESTAMPCOL", 116 "BLOBCOL", 117 118 }; 119 120 private static String [][]SQLData = 121 { 122 {NULL_VALUE, "0","1","2"}, {NULL_VALUE,"0","1","21"}, {NULL_VALUE,"0","1","22"}, {NULL_VALUE,"0.0","1.0","23.0"}, {NULL_VALUE,"0.0","1.0","24.0"}, {NULL_VALUE,"0.0","1.0","25.0"}, {NULL_VALUE,"'0'","'aa'","'2.0'"}, {NULL_VALUE,"'0'","'aa'",VALID_TIME_STRING}, {NULL_VALUE,"'0'","'aa'",VALID_TIMESTAMP_STRING}, {NULL_VALUE,"X'10aa'",NULL_VALUE,"X'10aaaa'"}, {NULL_VALUE,"X'10aa'",NULL_VALUE,"X'10aaba'"}, {NULL_VALUE,"X'10aa'",NULL_VALUE,"X'10aaca'"}, {NULL_VALUE,"'13'","'14'",NULL_VALUE}, {NULL_VALUE,VALID_DATE_STRING,VALID_DATE_STRING,NULL_VALUE}, {NULL_VALUE,VALID_TIME_STRING,VALID_TIME_STRING,VALID_TIME_STRING}, {NULL_VALUE,VALID_TIMESTAMP_STRING,VALID_TIMESTAMP_STRING,VALID_TIMESTAMP_STRING}, {NULL_VALUE,NULL_VALUE,NULL_VALUE,NULL_VALUE} }; 140 141 146 private final Object nogc = SecurityCheck.class; 147 148 public static void main(String [] args) throws Throwable { 149 150 isDerbyNet = TestUtil.isNetFramework(); 151 152 Connection con; 153 ResultSetMetaData met; 154 ResultSet rs; 155 Statement stmt; 156 String [] columnNames = {"i", "s", "r", "d", "dt", "t", "ts", "c", "v", 157 "dc", "bi", "cbd", "vbd", "lvbd", "cl", "bl"}; 158 159 160 System.out.println("Test resultset starting"); 161 162 try 163 { 164 ij.getPropertyArg(args); 167 con = ij.startJBMS(); 168 con.setCatalog("mycatalog"); 170 String cat = con.getCatalog(); 171 if (cat != null ) 172 System.out.println("ERROR: getCatalog did not return null"); 173 try { 175 Method sh = con.getClass().getMethod("setHoldability", CONN_PARAM); 176 sh.invoke(con, CONN_ARG); 177 } catch (Exception e) {System.out.println("shouldn't get that error " + e.getMessage());} 179 stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, 180 ResultSet.CONCUR_UPDATABLE); 181 182 try { 183 stmt.execute("drop table t"); 184 } catch (SQLException se) { 185 } 187 stmt.execute("create table t (i int, s smallint, r real, "+ 188 "d double precision, dt date, t time, ts timestamp, "+ 189 "c char(10), v varchar(40) not null, dc dec(10,2),"+ 190 "bi bigint, cbd char(10) for bit data," + 191 "vbd varchar(10) for bit data,lvbd long varchar for bit data,"+ 192 "cl clob(2G), bl blob(1G) )"); 193 stmt.execute("insert into t values(1,2,3.3,4.4,date('1990-05-05'),"+ 194 "time('12:06:06'),timestamp('1990-07-07 07:07:07.000007'),"+ 195 "'eight','nine', 10.1, 11," + 196 TestUtil.stringToHexLiteral("twelv") + "," + 197 TestUtil.stringToHexLiteral("3teen") + "," + 198 TestUtil.stringToHexLiteral("4teen") + ", null, null)" ); 199 200 201 rs = stmt.executeQuery("select i, s, r, d, dt, t, ts, c, v, dc, bi, cbd, vbd, lvbd, cl, bl from t"); 202 met = rs.getMetaData(); 203 204 int colCount; 205 System.out.println("getColumnCount(): "+(colCount=met.getColumnCount())); 206 207 for (int i=1;i<=colCount;i++) { 209 System.out.println("isAutoIncrement("+i+"): "+met.isAutoIncrement(i)); 210 System.out.println("isCaseSensitive("+i+"): "+met.isCaseSensitive(i)); 211 System.out.println("isSearchable("+i+"): "+met.isSearchable(i)); 212 System.out.println("isCurrency("+i+"): "+met.isCurrency(i)); 213 System.out.println("isNullable("+i+"): "+met.isNullable(i)); 214 System.out.println("isSigned("+i+"): "+met.isSigned(i)); 215 System.out.println("getColumnDisplaySize("+i+"): "+met.getColumnDisplaySize(i)); 216 System.out.println("getColumnLabel("+i+"): "+met.getColumnLabel(i)); 217 System.out.println("getColumnName("+i+"): "+met.getColumnName(i)); 218 System.out.println("getTableName("+i+"): "+met.getTableName(i)); 220 System.out.println("getSchemaName("+i+"): "+met.getSchemaName(i)); 221 System.out.println("getCatalogName("+i+"): "+met.getCatalogName(i)); 222 System.out.println("getColumnType("+i+"): "+met.getColumnType(i)); 223 System.out.println("getPrecision("+i+"): "+met.getPrecision(i)); 224 System.out.println("getScale("+i+"): "+met.getScale(i)); 225 System.out.println("getColumnTypeName("+i+"): "+met.getColumnTypeName(i)); 226 System.out.println("isReadOnly("+i+"): "+met.isReadOnly(i)); 227 boolean writable = met.isWritable(i); 228 if (writable == true) 230 System.out.println("isWritable("+i+"): Expected isWritable value"); 231 System.out.println("isDefinitelyWritable("+i+"): "+met.isDefinitelyWritable(i)); 232 } 233 234 235 while (rs.next()) 236 { 237 for (int i=1;i<=colCount;i++) { 239 try { 240 System.out.println("getBigDecimal("+i+",1): "+ 241 BigDecimalHandler.getBigDecimalString(rs,i)); 242 } 243 catch (Throwable e) { 244 System.out.println( 245 "getBigDecimal("+i+",1) got exception "); 246 if (e instanceof SQLException ) 247 JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException )e); 248 } 249 250 try { 251 if (isDerbyNet) 252 System.out.println("beetle 5328 - JCC returns incorrect scale for getBigDecimal(String,int)"); 253 System.out.println("getBigDecimal("+ 254 columnNames[i-1]+ ",1): "+ 255 BigDecimalHandler.getBigDecimalString(rs,columnNames[i-1],i)); 256 } 257 catch (Throwable e) { 258 System.out.println( 259 "getBigDecimal("+ 260 columnNames[i-1]+",1) got exception "); 261 if (e instanceof SQLException ) 262 JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException )e); 263 } 264 265 try { 266 System.out.println("getBoolean("+i+"): "+ 267 rs.getBoolean(i)); 268 } 269 catch (Throwable e) { 270 System.out.println( 271 "getBoolean("+i+") got exception "); 272 if (e instanceof SQLException ) 273 JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException )e); 274 } 275 276 try { 277 System.out.println("getBoolean("+ 278 columnNames[i-1]+ "): "+ 279 rs.getBoolean(columnNames[i-1])); 280 } 281 catch (Throwable e) { 282 System.out.println( 283 "getBoolean("+ 284 columnNames[i-1]+") got exception " ); 285 if (e instanceof SQLException ) 286 JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException )e); 287 } 288 289 try { 290 System.out.println("getByte("+i+"): "+ 291 rs.getByte(i)); 292 } 293 catch (Throwable e) { 294 System.out.println( 295 "getByte("+i+") got exception " ); 296 if (e instanceof SQLException ) 297 JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException )e); 298 } 299 300 try { 301 System.out.println("getByte("+ 302 columnNames[i-1]+ "): "+ 303 rs.getByte(columnNames[i-1])); 304 } 305 catch (Throwable e) { 306 System.out.println( 307 "getByte("+ 308 columnNames[i-1]+") got exception " ); 309 if (e instanceof SQLException ) 310 JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException )e); 311 } 312 313 try { 314 System.out.println("getBytes("+i+"): "+ 315 showBytes(rs.getBytes(i))); 316 } 317 catch (SQLException e) { 318 System.out.println( 319 "getBytes("+i+") got exception " ); 320 JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException )e); 321 } 322 323 try { 324 System.out.println("getBytes("+ 325 columnNames[i-1]+ "): "+ 326 showBytes(rs.getBytes(columnNames[i-1]))); 327 } 328 catch (SQLException e) { 329 System.out.println( 330 "getBytes("+ 331 columnNames[i-1]+") got exception " ); 332 JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException )e); 333 } 334 335 try { 336 System.out.println("getDate("+i+"): "+ 337 rs.getDate(i)); 338 } 339 catch (SQLException e) { 340 System.out.println( 341 "getDate("+i+") got exception " ); 342 JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException )e); 343 } 344 345 try { 346 System.out.println("getDate("+ 347 columnNames[i-1]+ "): "+ 348 rs.getDate(columnNames[i-1])); 349 } 350 catch (SQLException e) { 351 System.out.println( 352 "getDate("+ 353 columnNames[i-1]+") got exception " ); 354 JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException )e); 355 } 356 357 try { 358 System.out.println("getDouble("+i+"): "+ 359 rs.getDouble(i)); 360 } 361 catch (Throwable e) { 362 System.out.println( 363 "getDouble("+i+") got exception " ); 364 if (e instanceof SQLException ) 365 JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException )e); 366 } 367 368 try { 369 System.out.println("getDouble("+ 370 columnNames[i-1]+ "): "+ 371 rs.getDouble(columnNames[i-1])); 372 } 373 catch (Throwable e) { 374 System.out.println( 375 "getDouble("+ 376 columnNames[i-1]+") got exception " ); 377 if (e instanceof SQLException ) 378 JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException )e); 379 } 380 381 try { 382 System.out.println("getFloat("+i+"): "+ 383 rs.getFloat(i)); 384 } 385 catch (Throwable e) { 386 System.out.println( 387 "getFloat("+i+") got exception " ); 388 if (e instanceof SQLException ) 389 JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException )e); 390 } 391 392 try { 393 System.out.println("getFloat("+ 394 columnNames[i-1]+ "): "+ 395 rs.getFloat(columnNames[i-1])); 396 } 397 catch (Throwable e) { 398 System.out.println( 399 "getFloat("+ 400 columnNames[i-1]+") got exception " ); 401 if (e instanceof SQLException ) 402 JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException )e); 403 } 404 405 try { 406 System.out.println("getInt("+i+"): "+ 407 rs.getInt(i)); 408 } 409 catch (Throwable e) { 410 System.out.println( 411 "getInt("+i+") got exception " ); 412 if (e instanceof SQLException ) 413 JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException )e); 414 } 415 416 try { 417 System.out.println("getInt("+ 418 columnNames[i-1]+ "): "+ 419 rs.getInt(columnNames[i-1])); 420 } 421 catch (Throwable e) { 422 System.out.println( 423 "getInt("+ 424 columnNames[i-1]+") got exception " ); 425 if (e instanceof SQLException ) 426 JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException )e); 427 } 428 429 try { 430 System.out.println("getLong("+i+"): "+ 431 rs.getLong(i)); 432 } 433 catch (Throwable e) { 434 System.out.println( 435 "getLong("+i+") got exception " ); 436 if (e instanceof SQLException ) 437 JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException )e); 438 } 439 440 try { 441 System.out.println("getLong("+ 442 columnNames[i-1]+ "): "+ 443 rs.getLong(columnNames[i-1])); 444 } 445 catch (Throwable e) { 446 System.out.println( 447 "getLong("+ 448 columnNames[i-1]+") got exception " ); 449 if (e instanceof SQLException ) 450 JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException )e); 451 } 452 453 try { 454 if (i>11) 458 { 459 BigDecimalHandler.getObjectString(rs,i); 460 System.out.println("getObject("+i+") is ok"); 461 } 462 else 463 System.out.println("getObject("+i+"): "+ 464 BigDecimalHandler.getObjectString(rs,i)); 465 } 466 catch (SQLException e) { 467 System.out.println( 468 "getObject("+i+") got exception " ); 469 JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException )e); 470 } 471 472 try { 473 if (i>11) 477 { 478 BigDecimalHandler.getObjectString(rs,columnNames[i-1],i); 479 System.out.println("getObject("+columnNames[i-1]+") is ok "); 480 } 481 else 482 System.out.println("getObject("+ 483 columnNames[i-1]+ "): "+ 484 BigDecimalHandler.getObjectString(rs,columnNames[i-1],i)); 485 } 486 catch (SQLException e) { 487 System.out.println( 488 "getObject("+ 489 columnNames[i-1]+") got exception " ); 490 JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException )e); 491 } 492 493 try { 494 System.out.println("getShort("+i+"): "+ 495 rs.getShort(i)); 496 } 497 catch (Throwable e) { 498 System.out.println( 499 "getShort("+i+") got exception " ); 500 if (e instanceof SQLException ) 501 JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException )e); 502 } 503 504 try { 505 System.out.println("getShort("+ 506 columnNames[i-1]+ "): "+ 507 rs.getShort(columnNames[i-1])); 508 } 509 catch (Throwable e) { 510 System.out.println( 511 "getShort("+ 512 columnNames[i-1]+") got exception " ); 513 if (e instanceof SQLException ) 514 JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException )e); 515 } 516 517 try { 518 System.out.println("getString("+i+"): "+ 519 rs.getString(i)); 520 } 521 catch (SQLException e) { 522 System.out.println( 523 "getString("+i+") got exception " ); 524 JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException )e); 525 } 526 527 try { 528 System.out.println("getString("+ 529 columnNames[i-1]+ "): "+ 530 rs.getString(columnNames[i-1])); 531 } 532 catch (SQLException e) { 533 System.out.println( 534 "getString("+ 535 columnNames[i-1]+") got exception " ); 536 JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException )e); 537 } 538 539 try { 540 System.out.println("getTime("+i+"): "+ 541 rs.getTime(i)); 542 } 543 catch (SQLException e) { 544 System.out.println( 545 "getTime("+i+") got exception " ); 546 JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException )e); 547 } 548 549 try { 550 System.out.println("getTime("+ 551 columnNames[i-1]+ "): "+ 552 rs.getTime(columnNames[i-1])); 553 } 554 catch (SQLException e) { 555 System.out.println( 556 "getTime("+ 557 columnNames[i-1]+") got exception " ); 558 JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException )e); 559 } 560 561 try { 562 System.out.println("getTimestamp("+i+"): "+ 563 rs.getTimestamp(i)); 564 } 565 catch (SQLException e) { 566 System.out.println( 567 "getTimestamp("+i+") got exception " ); 568 JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException )e); 569 } 570 571 try { 572 System.out.println("getTimestamp("+ 573 columnNames[i-1]+ "): "+ 574 rs.getTimestamp(columnNames[i-1])); 575 } 576 catch (SQLException e) { 577 System.out.println( 578 "getTimestamp("+ 579 columnNames[i-1]+") got exception " ); 580 JDBCTestDisplayUtil.ShowCommonSQLException(System.out, (SQLException )e); 581 } 582 } 583 } 584 585 rs.close(); 586 587 try { 589 rs.next(); 590 System.out.println( 591 "FAIL - rs.next() allowed on closed result set."); 592 } 593 catch (SQLException e) { 594 System.out.println( 595 "rs.next() on closed result set got exception " ); 596 JDBCTestDisplayUtil.ShowCommonSQLException(System.out, e); 597 } 598 catch (Throwable e) { 599 System.out.println("rs.next() didn't fail with SQLException as "+ 600 "expected on closed result set. Got Throwable instead: "+e); 601 } 602 603 stmt.executeUpdate("create table bug4810(i int, b int)"); 605 stmt.executeUpdate("insert into bug4810 values (1,1), (1,2), (1,3), (1,4)"); 606 stmt.executeUpdate("insert into bug4810 values (1,1), (1,2), (1,3), (1,4)"); 607 con.commit(); 608 con.setAutoCommit(true); 609 con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); 610 System.out.println("just auto commit"); 611 showLocksForAutoCommitSelect(con, stmt, 0); 612 System.out.println("commit with auto commit"); 613 showLocksForAutoCommitSelect(con, stmt, 1); 614 System.out.println("rollback with auto commit"); 615 showLocksForAutoCommitSelect(con, stmt, 2); 616 617 stmt.execute("drop table bug4810"); 618 con.commit(); 619 stmt.close(); 620 621 testMutableValues(con); 622 testCorrelationNamesAndMetaDataCalls(con); 623 testNullIfAndMetaDataCalls(con); 624 testFloatMAX_VALUE(con); 625 if (!TestUtil.isJCCFramework()) { 628 runAutoCommitTests(con); 629 } 630 cleanUp(con); 632 con.close(); 633 634 } 635 catch (SQLException e) { 636 System.out.println("FAIL -- unexpected exception: " + e.toString()); 637 dumpSQLExceptions(e); 638 e.printStackTrace(); 639 } 640 catch (Throwable e) { 641 System.out.println("FAIL -- unexpected exception: "+e); 642 e.printStackTrace(); 643 } 644 645 SecurityCheck.report(); 648 System.out.println("Test resultset finished"); 649 } 650 651 652 static private void testNullIfAndMetaDataCalls(Connection conn) throws Throwable 654 { 655 System.out.println("Tests to check metadata information of nullif column"); 656 tablesForTestingAllDatatypesCombinations(conn); 657 testAllDatatypesCombinations(conn); 658 testParameterForFirstOperandToNullIf(conn); 659 } 660 661 public static void testParameterForFirstOperandToNullIf( Connection conn) throws Throwable 662 { 663 System.out.println("Start testing first operand as parameter to nullif"); 664 PreparedStatement ps; 665 for (int secondColumnType = 0; secondColumnType < SQLTypes.length; secondColumnType++) { 666 System.out.println("Testing nullif(?,"+SQLTypes[secondColumnType]+")"); 667 String nullIfString = 668 new String ("SELECT NULLIF(?," + ColumnNames[secondColumnType] +") from AllDataTypesTable"); 669 try { 670 ps = conn.prepareStatement(nullIfString); 671 switch(secondColumnType) { 672 case 0: 673 case 1: 674 case 2: 675 case 3: 676 case 4: 677 case 5: 678 case 6: 679 case 7: 680 System.out.println("Testing nullif(?,"+SQLTypes[secondColumnType]+") with setBoolean"); 681 ps.setBoolean(1, true); 682 break; 683 case 8: case 11: case 12: case 16: case 9: 693 case 10: 694 System.out.println("Testing nullif(?,"+SQLTypes[secondColumnType]+") with setBinaryStream"); 695 ps.setBinaryStream(1, (java.io.InputStream )null, 1); 696 break; 697 case 13: System.out.println("Testing nullif(?,"+SQLTypes[secondColumnType]+") with setDate"); 699 ps.setDate(1, Date.valueOf("2000-01-01")); 700 break; 701 case 14: System.out.println("Testing nullif(?,"+SQLTypes[secondColumnType]+") with setTime"); 703 ps.setTime(1, Time.valueOf("15:30:20")); 704 break; 705 case 15: System.out.println("Testing nullif(?,"+SQLTypes[secondColumnType]+") with setTimestamp"); 707 ps.setTimestamp(1, Timestamp.valueOf("2000-01-01 15:30:20")); 708 break; 709 default: break; 710 } 711 dumpRS(ps.executeQuery()); 712 } catch (SQLException e) 713 { 714 dumpSQLExceptions(e); 715 } 716 } 717 } 718 719 public static void testAllDatatypesCombinations( Connection conn) throws Throwable 720 { 721 System.out.println("Start testing all datatypes combinations in NULLIF function"); 722 Statement s = conn.createStatement(); 723 for (int firstColumnType = 0; firstColumnType < SQLTypes.length; firstColumnType++) { 724 StringBuffer nullIfString = new StringBuffer ("SELECT NULLIF(" + ColumnNames[firstColumnType]); 725 for (int secondColumnType = 0; secondColumnType < SQLTypes.length; secondColumnType++) { 726 try { 727 StringBuffer completeNullIfString = new StringBuffer (nullIfString.toString() + "," + ColumnNames[secondColumnType]); 728 System.out.println(completeNullIfString + ") from AllDataTypesTable"); 729 dumpRS(s.executeQuery(completeNullIfString + ") from AllDataTypesTable")); 730 } catch (SQLException e) 731 { 732 dumpSQLExceptions(e); 733 } 734 } 735 } 736 } 737 738 public static void tablesForTestingAllDatatypesCombinations( Connection conn) throws Throwable 739 { 740 System.out.println("Set up by creating table for testing all datatypes combinations"); 741 742 Statement s = conn.createStatement(); 743 744 try { 745 s.executeUpdate("DROP TABLE AllDataTypesTable"); 746 } 747 catch(SQLException se) {} 748 749 StringBuffer createSQL = new StringBuffer ("create table AllDataTypesTable ("); 750 for (int type = 0; type < SQLTypes.length - 1; type++) 751 { 752 createSQL.append(ColumnNames[type] + " " + SQLTypes[type] + ","); 753 } 754 createSQL.append(ColumnNames[SQLTypes.length - 1] + " " + SQLTypes[SQLTypes.length - 1] + ")"); 755 System.out.println(createSQL); 756 s.executeUpdate(createSQL.toString()); 757 758 for (int row = 0; row < SQLData[0].length; row++) 759 { 760 createSQL = new StringBuffer ("insert into AllDataTypesTable values("); 761 for (int type = 0; type < SQLTypes.length - 1; type++) 762 { 763 createSQL.append(SQLData[type][row] + ","); 764 } 765 createSQL.append(SQLData[SQLTypes.length - 1][row]+")"); 766 System.out.println(createSQL); 767 s.executeUpdate(createSQL.toString()); 768 } 769 770 s.close(); 771 conn.commit(); 772 } 773 774 public static void dumpRS(ResultSet s) throws SQLException 775 { 776 SecurityCheck.inspect(s, "java.sql.ResultSet"); 777 778 if (s == null) 779 { 780 System.out.println("<NULL>"); 781 return; 782 } 783 784 boolean hasData = s.next(); 793 794 ResultSetMetaData rsmd = s.getMetaData(); 795 SecurityCheck.inspect(rsmd, "java.sql.ResultSetMetaData"); 796 797 int numCols = rsmd.getColumnCount(); 799 800 if (numCols <= 0) 801 { 802 System.out.println("(no columns!)"); 803 return; 804 } 805 806 StringBuffer heading = new StringBuffer ("\t "); 807 StringBuffer underline = new StringBuffer ("\t "); 808 809 int len; 810 for (int i=1; i<=numCols; i++) 812 { 813 if (i > 1) 814 { 815 heading.append(","); 816 underline.append(" "); 817 } 818 len = heading.length(); 819 heading.append("COL"+i); 820 heading.append("(datatype : " + rsmd.getColumnTypeName(i)); 821 heading.append(", precision : " + rsmd.getPrecision(i)); 822 heading.append(", scale : " + rsmd.getScale(i) + ")"); 823 len = heading.length() - len; 824 for (int j = len; j > 0; j--) 825 { 826 underline.append("-"); 827 } 828 } 829 System.out.println(heading.toString()); 830 System.out.println(underline.toString()); 831 832 833 StringBuffer row = new StringBuffer (); 834 while (hasData) 836 { 837 row.append("\t{"); 838 for (int i=1; i<=numCols; i++) 841 { 842 if (i > 1) row.append(","); 843 try{ 844 row.append(s.getString(i)); 845 } catch(SQLException ex){ 846 if (ex.getSQLState().equals("22005")) 847 row.append("Invalid Conversion Error\n"); 848 else throw ex; 849 } 850 } 851 row.append("}\n"); 852 hasData = s.next(); 853 } 854 System.out.println(row.toString()); 855 s.close(); 856 } 857 858 859 static private void testCorrelationNamesAndMetaDataCalls(Connection conn) throws Exception 860 { 861 Statement stmt = conn.createStatement(); 862 stmt.executeUpdate("create table s (a int, b int, c int, d int, e int, f int)"); 863 stmt.executeUpdate("insert into s values (0,1,2,3,4,5)"); 864 stmt.executeUpdate("insert into s values (10,11,12,13,14,15)"); 865 System.out.println("Run select * from s ss (f, e, d, c, b, a) where f = 0 and then try getTableName and getSchemaName on columns"); 866 ResultSet rs = stmt.executeQuery("select * from s ss (f, e, d, c, b, a) where f = 0"); 867 rs.next(); 868 ResultSetMetaData met = rs.getMetaData(); 869 System.out.println("getTableName(1): "+met.getTableName(1)); 870 System.out.println("getSchemaName(1): "+met.getSchemaName(1)); 871 872 System.out.println("Run select * from (select * from s) a and then try getTableName and getSchemaName on columns"); 873 rs = stmt.executeQuery("select * from (select * from s) a"); 874 rs.next(); 875 met = rs.getMetaData(); 876 System.out.println("getTableName(1): "+met.getTableName(1)); 877 System.out.println("getSchemaName(1): "+met.getSchemaName(1)); 878 879 stmt.executeUpdate("create schema s1"); 880 stmt.executeUpdate("create table s1.t1 (c11 int, c12 int)"); 881 stmt.executeUpdate("insert into s1.t1 values (11, 12), (21, 22)"); 882 System.out.println("Run select * from s1.t1 as abc and then try getTableName and getSchemaName on columns"); 883 rs = stmt.executeQuery("select * from s1.t1 as abc"); 884 met = rs.getMetaData(); 885 System.out.println("Table name of first column is " + met.getTableName(1)); 886 System.out.println("Schema name of first column is " + met.getSchemaName(1)); 887 System.out.println("Table name of second column is " + met.getTableName(2)); 888 System.out.println("Schema name of second column is " + met.getSchemaName(2)); 889 System.out.println("Run select abc.c11 from s1.t1 as abc and then try getTableName and getSchemaName on columns"); 890 rs = stmt.executeQuery("select abc.c11 from s1.t1 as abc"); 891 met = rs.getMetaData(); 892 System.out.println("Table name of first column is " + met.getTableName(1)); 893 System.out.println("Schema name of first column is " + met.getSchemaName(1)); 894 System.out.println("Run select bcd.a, abc.c11 from s1.t1 as abc, s as bcd and then try getTableName and getSchemaName on columns"); 895 rs = stmt.executeQuery("select bcd.a, abc.c11 from s1.t1 as abc, s as bcd"); 896 met = rs.getMetaData(); 897 System.out.println("Table name of first column is " + met.getTableName(1)); 898 System.out.println("Schema name of first column is " + met.getSchemaName(1)); 899 System.out.println("Table name of second column is " + met.getTableName(2)); 900 System.out.println("Schema name of second column is " + met.getSchemaName(2)); 901 902 stmt.executeUpdate("create schema app1"); 903 stmt.executeUpdate("create table app1.t1 (c11 int, c12 int)"); 904 stmt.executeUpdate("insert into app1.t1 values (11, 12), (21, 22)"); 905 stmt.executeUpdate("create schema app2"); 906 stmt.executeUpdate("create table app2.t1 (c11 int, c12 int)"); 907 stmt.executeUpdate("insert into app2.t1 values (11, 12), (21, 22)"); 908 System.out.println("Run select app1.t1.c11, app2.t1.c11 from app1.t1, app2.t1 and then try getTableName and getSchemaName on columns"); 909 rs = stmt.executeQuery("select app1.t1.c11, app2.t1.c11 from app1.t1, app2.t1"); 910 met = rs.getMetaData(); 911 System.out.println("Table name of first column is " + met.getTableName(1)); 912 System.out.println("Schema name of first column is " + met.getSchemaName(1)); 913 System.out.println("Table name of second column is " + met.getTableName(2)); 914 System.out.println("Schema name of second column is " + met.getSchemaName(2)); 915 stmt.execute("drop table s"); 916 stmt.execute("drop table s1.t1"); 917 stmt.execute("drop schema s1 restrict"); 918 stmt.execute("drop table app1.t1"); 919 stmt.execute("drop table app2.t1"); 920 stmt.execute("drop schema app2 restrict"); 921 stmt.execute("drop schema app1 restrict"); 922 } 923 924 static private void doTheTests() throws Exception 925 { 926 927 } 928 929 static private void showLocksForAutoCommitSelect(Connection conn, Statement stmt, int action) throws Exception { 930 931 ResultSet rs = stmt.executeQuery("select i,b from bug4810"); 932 rs.next(); 933 System.out.println(" bug4810 " + rs.getInt(1) + ", " + rs.getInt(2)); 934 rs.next(); 935 System.out.println(" bug4810 " + rs.getInt(1) + ", " + rs.getInt(2)); 936 937 if (action == 1) { 938 System.out.println("commit"); 939 conn.commit(); 940 } else if (action == 2) { 941 System.out.println("rollback"); 942 conn.rollback(); 943 } 944 945 showLocks(); 946 947 try { 948 949 rs.next(); 950 System.out.println(" bug4810 " + rs.getInt(1) + ", " + rs.getInt(2)); 951 } catch (SQLException sqle) { 952 JDBCTestDisplayUtil.ShowCommonSQLException(System.out, sqle); } 953 showLocks(); 954 rs.close(); 955 956 showLocks(); 957 958 } 959 960 private static void showLocks() throws Exception { 961 System.out.println(" LOCK TABLE"); 962 Connection con2 = ij.startJBMS(); 963 PreparedStatement ps2 = con2.prepareStatement("select XID, count(*) from SYSCS_DIAG.LOCK_TABLE as L group by XID"); 964 ResultSet rs2 = ps2.executeQuery(); 965 966 while (rs2.next()) { 967 if (rs2.getInt(2) > 0) { 968 System.out.println("Locks are held"); 969 } else if (rs2.getInt(2) == 0) { 970 System.out.println("No locks to hold"); 971 } 972 } 973 974 rs2.close(); 975 ps2.close(); 976 con2.close(); 977 } 978 979 static private void dumpSQLExceptions (SQLException se) { 980 while (se != null) { 981 JDBCTestDisplayUtil.ShowCommonSQLException(System.out, se); 982 se = se.getNextException(); 983 } 984 } 985 986 static private String showBytes(byte[] bytes) { 987 if (bytes == null) 988 return "null"; 989 990 StringBuffer s = new StringBuffer ("0x"); 991 s.ensureCapacity(2+2*bytes.length); 992 for (int i=0;i<bytes.length;i++) { 993 int hi = (bytes[i] & 0xf0) >>> 4; 994 int lo = (bytes[i] & 0x0f); 995 s.append(representation[hi]); 996 s.append(representation[lo]); 997 } 998 return s.toString(); 999 } 1000 1001 static final char[] representation = 1002 { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 1003 'A', 'B', 'C', 'D', 'E', 'F' } ; 1004 1005 1006 1019 private static void testMutableValues(Connection conn) throws SQLException 1020 { 1021 System.out.println("START testMutableValues"); 1022 1023 Statement s = conn.createStatement(); 1024 1025 s.execute("CREATE TABLE MUTABLE.T1(C CHAR(10) FOR BIT DATA, V VARCHAR(10) FOR BIT DATA, L LONG VARCHAR FOR BIT DATA, D DATE, T TIME, TS TIMESTAMP)"); 1026 s.execute("INSERT INTO MUTABLE.T1 VALUES (X'34', X'4de5', X'5e3a67', '1992-01-01', '17.05.00', '2003-3-1-17.05.43.123456')"); 1027 s.execute("INSERT INTO MUTABLE.T1 VALUES (X'93', X'4825', X'6e3a64', '1992-01-03', '17.06.00', '2007-3-1-17.05.43.123456')"); 1028 s.execute("INSERT INTO MUTABLE.T1 VALUES (X'34', X'4de5', X'5e3a67', '1992-01-01', '17.05.00', '2003-3-1-17.05.43.123456')"); 1029 1030 { 1031 ResultSet rs = s.executeQuery("SELECT C,V,L,D,T,TS FROM MUTABLE.T1"); 1032 java.util.ArrayList [] values = new java.util.ArrayList [6]; 1033 for (int i = 0; i < values.length; i++) { 1034 values[i] = new java.util.ArrayList (); 1035 } 1036 System.out.println("CHECKING on getXXX()"); 1037 int rc = 0; 1038 while (rs.next()) { 1039 rc++; 1040 System.out.println("ROW " + rc); 1041 1042 checkMutableValue(values[0], 1, rs.getBytes(1)); 1043 checkMutableValue(values[1], 2, rs.getBytes(2)); 1044 checkMutableValue(values[2], 3, rs.getBytes(3)); 1045 1046 checkMutableValue(values[3], 4, rs.getDate(4)); 1047 checkMutableValue(values[4], 5, rs.getTime(5)); 1048 checkMutableValue(values[5], 6, rs.getTimestamp(6)); 1049 1050 } 1051 rs.close(); 1052 } 1053 { 1054 ResultSet rs = s.executeQuery("SELECT C,V,L,D,T,TS FROM MUTABLE.T1"); 1055 java.util.ArrayList [] values = new java.util.ArrayList [6]; 1056 for (int i = 0; i < values.length; i++) { 1057 values[i] = new java.util.ArrayList (); 1058 } 1059 System.out.println("CHECKING on getObject()"); 1060 int rc = 0; 1061 while (rs.next()) { 1062 rc++; 1063 System.out.println("ROW " + rc); 1064 1065 for (int i = 0; i < 6; i++) 1066 checkMutableValue(values[i], i+1, rs.getObject(i+1)); 1067 } 1068 rs.close(); 1069 } 1070 1071 s.execute("DROP TABLE MUTABLE.T1"); 1072 1073 System.out.println("COMPLETE testMutableValues"); 1074 } 1075 1076 private static void checkMutableValue(java.util.ArrayList list, int col, Object value) { 1077 1078 int same = -1; 1079 int equals = -1; 1080 for (int i = 0; i < list.size(); i++) { 1081 Object previous = list.get(i); 1082 if (previous == value) 1083 same = i+1; 1084 if (previous.equals(value)) 1085 equals = i+1; 1086 } 1087 1088 if (same != -1) 1089 System.out.println("FAIL SAME OBJECT RETURNED column " + col + " existing " + same); 1090 if (equals != -1) 1091 System.out.println("OK EQUALITY OBJECT RETURNED column " + col + " existing " + equals); 1092 1093 list.add(value); 1094 } 1095 1096 private static void testFloatMAX_VALUE(Connection conn) 1102 throws SQLException 1103 { 1104 Statement stmt = conn.createStatement(); 1105 try { stmt.execute("drop table jira1136"); } catch (Throwable t) { } 1106 stmt.execute("create table jira1136 (f float)"); 1107 stmt.execute("insert into jira1136 values (3.4028235E38)"); 1108 PreparedStatement ps = conn.prepareStatement("select * from jira1136"); 1109 ResultSet rs = ps.executeQuery(); 1110 while (rs.next()) 1111 { 1112 if (!TestUtil.isJCCFramework()) 1113 System.out.println("DERBY-1136 fetched: " + rs.getFloat(1)); 1114 else System.out.println("DERBY-1136 fetched: 3.4028235E38"); 1116 } 1117 } 1118 1119 1125 private static void runAutoCommitTests(Connection conn) throws SQLException { 1126 Statement s = conn.createStatement(); 1127 ResultSet rs = s.executeQuery("select tablename from sys.systables " + 1128 "where tablename = 'AUTOCOMMITTABLE'"); 1129 if (rs.next()) { 1130 rs.close(); 1131 s.executeUpdate("delete from AutoCommitTable"); 1132 } else { 1133 rs.close(); 1134 s.executeUpdate("create table AutoCommitTable (num int)"); 1135 } 1136 s.executeUpdate("insert into AutoCommitTable values (1)"); 1137 s.executeUpdate("insert into AutoCommitTable values (2)"); 1138 int isolation = conn.getTransactionIsolation(); 1139 conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); 1140 testSingleRSAutoCommit(conn); 1141 testSingleRSCloseCursorsAtCommit(conn); 1142 conn.setTransactionIsolation(isolation); 1143 s.executeUpdate("drop table AutoCommitTable"); 1144 s.close(); 1145 } 1146 1147 1159 private static void testSingleRSAutoCommit(Connection conn) throws SQLException { 1160 setHoldability(conn, JDBC30Translation.HOLD_CURSORS_OVER_COMMIT); 1161 System.out.print("Single RS auto-commit test: "); 1162 Statement s = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 1163 ResultSet rs = s.executeQuery("select * from AutoCommitTable"); 1164 while (rs.next()); 1165 if (!checkLocks()) { 1166 System.out.println("FAIL. Auto-commit unsuccessful."); 1167 rs.close(); 1168 return; 1169 } 1170 try { 1171 if (!rs.next()) { 1172 System.out.println("PASS."); 1173 } else { 1174 System.out.println("FAIL. Final call of the ResultSet should return false"); 1175 } 1176 rs.close(); 1177 } catch (SQLException e) { 1178 System.out.println("FAIL. Final call to ResultSet.next() threw an Exception: "); 1179 e.printStackTrace(); 1180 } 1181 } 1182 1183 1190 private static void testSingleRSCloseCursorsAtCommit(Connection conn) throws SQLException { 1191 setHoldability(conn, JDBC30Translation.CLOSE_CURSORS_AT_COMMIT); 1192 conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); 1193 System.out.print("SingleRSCloseCursorsAtCommit: "); 1194 Statement s = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 1195 ResultSet rs = s.executeQuery("select * from AutoCommitTable"); 1196 while (rs.next()); 1197 if (!checkLocks()) { 1198 System.out.println("FAIL. Auto-commit unsuccessful."); 1199 rs.close(); 1200 return; 1201 } 1202 try { 1203 rs.next(); 1204 System.out.println("FAIL. ResultSet not closed implicitly"); 1205 rs.close(); 1206 } catch (SQLException e) { 1207 System.out.println("PASS."); 1208 } 1209 } 1210 1211 1212 1221 private static boolean checkLocks() throws SQLException { 1222 Connection conn = null; 1223 try { 1224 conn = ij.startJBMS(); 1225 } catch (Exception e) { 1226 System.out.println("FAIL. Unable to establish connection in checkLocks"); 1227 return false; 1228 } 1229 Statement stmt = conn.createStatement(); 1230 try { 1231 stmt.executeUpdate("update AutoCommitTable " 1232 + "set num = 3 where num = 2"); 1233 stmt.executeUpdate("update AutoCommitTable " 1234 + "set num = 2 where num = 3"); 1235 } catch (SQLException e) { 1236 if (e.getSQLState().equals(SQLState.LOCK_TIMEOUT)) { 1237 return false; 1238 } else { 1239 throw e; 1240 } 1241 } 1242 stmt.close(); 1243 conn.close(); 1244 return true; 1245 } 1246 1247 1255 public static void setHoldability(Connection conn, int hold) throws SQLException { 1256 try { 1257 Object [] holdArray = {new Integer (hold)}; 1258 Method sh = conn.getClass().getMethod("setHoldability", CONN_PARAM); 1259 sh.invoke(conn, holdArray); 1260 } catch (Exception e) {System.out.println("shouldn't get that error " + e.getMessage());} } 1262 1263 private static void cleanUp(Connection conn) throws SQLException { 1264 Statement s = conn.createStatement(); 1265 String [] testObjects = {"TABLE APP.T"}; 1266 try { 1267 TestUtil.cleanUpTest(s, testObjects); 1268 } catch (SQLException se){ 1269 } 1270 } 1271 1272} 1273 | Popular Tags |