1 21 22 package org.apache.derbyTesting.functionTests.tests.store; 23 24 import org.apache.derby.iapi.services.sanity.SanityManager; 25 26 import org.apache.derby.tools.ij; 27 28 import java.sql.Connection ; 29 import java.sql.Statement ; 30 import java.sql.PreparedStatement ; 31 import java.sql.ResultSet ; 32 import java.sql.SQLException ; 33 34 35 36 45 public abstract class BaseTest 46 { 47 private static boolean debug_system_procedures_created = false; 48 protected static boolean verbose = false; 49 50 abstract public void testList(Connection conn) throws SQLException ; 51 52 void runTests(String [] argv) 53 throws Throwable 54 { 55 ij.getPropertyArg(argv); 56 Connection conn = ij.startJBMS(); 57 System.out.println("conn from ij.startJBMS() = " + conn); 58 conn.setAutoCommit(false); 59 60 try 61 { 62 testList(conn); 63 } 64 catch (SQLException sqle) 65 { 66 org.apache.derby.tools.JDBCDisplayUtil.ShowSQLException( 67 System.out, sqle); 68 sqle.printStackTrace(System.out); 69 } 70 } 71 72 public BaseTest() 73 { 74 } 75 76 protected void beginTest( 77 Connection conn, 78 String str) 79 throws SQLException 80 { 81 log("Beginning test: " + str); 82 conn.commit(); 83 } 84 85 protected void testProgress( 86 String str) 87 throws SQLException 88 { 89 log("Executing test: " + str); 90 } 91 92 protected void endTest( 93 Connection conn, 94 String str) 95 throws SQLException 96 { 97 conn.commit(); 98 log("Ending test: " + str); 99 } 100 101 protected void log(String str) 102 { 103 System.out.println(str); 104 } 105 106 protected void logError(String str) 107 { 108 System.out.println("ERROR: " + str); 109 } 110 111 114 public void executeQuery( 115 Connection conn, 116 String stmt_str, 117 boolean commit_query) 118 throws SQLException 119 { 120 Statement stmt = conn.createStatement(); 121 stmt.executeUpdate(stmt_str); 122 stmt.close(); 123 if (commit_query) 124 conn.commit(); 125 } 126 127 131 protected boolean checkConsistency( 132 Connection conn, 133 String schemaName, 134 String tableName) 135 throws SQLException 136 { 137 Statement s = conn.createStatement(); 138 139 ResultSet rs = 140 s.executeQuery( 141 "values SYSCS_UTIL.SYSCS_CHECK_TABLE('" + 142 schemaName + "', '" + 143 tableName + "')"); 144 145 if (!rs.next()) 146 { 147 if (SanityManager.DEBUG) 148 { 149 SanityManager.THROWASSERT("no value from values clause."); 150 } 151 } 152 153 boolean consistent = rs.getBoolean(1); 154 155 rs.close(); 156 157 conn.commit(); 158 159 return(consistent); 160 } 161 162 166 protected boolean checkAllConsistency( 167 Connection conn) 168 throws SQLException 169 { 170 Statement s = conn.createStatement(); 171 172 ResultSet rs = 173 s.executeQuery( 174 "select schemaname, tablename, SYSCS_UTIL.SYSCS_CHECK_TABLE(schemaname, tablename) " + 175 "from sys.systables a, sys.sysschemas b where a.schemaid = b.schemaid"); 176 177 int table_count = 0; 178 179 while (rs.next()) 180 { 181 table_count++; 182 if (rs.getInt(3) != 1) 183 { 184 System.out.println( 185 "Bad return from consistency check of " + 186 rs.getString(1) + "." + rs.getString(2)); 187 } 188 } 189 190 if (table_count < 5) 191 { 192 System.out.println( 194 "Something wrong with consistency check query, found only " + 195 table_count + " tables."); 196 } 197 198 rs.close(); 199 s.close(); 200 201 conn.commit(); 202 203 return(true); 204 } 205 206 210 protected void createDebugSystemProcedures( 211 Connection conn) 212 throws SQLException 213 { 214 Statement s = conn.createStatement(); 215 s.executeUpdate( 216 "CREATE FUNCTION D_CONGLOMID_PRINT(DBNAME VARCHAR(128), CONGLOMID INT) RETURNS VARCHAR(32000) RETURNS NULL ON NULL INPUT EXTERNAL NAME 'org.apache.derby.impl.store.raw.data.D_DiagnosticUtil.diag_conglomid' LANGUAGE JAVA PARAMETER STYLE JAVA"); 217 s.executeUpdate( 218 "CREATE FUNCTION DIAG_CONGLOMID(DBNAME VARCHAR(128), CONGLOMID INT) RETURNS VARCHAR(32000) RETURNS NULL ON NULL INPUT EXTERNAL NAME 'org.apache.derby.impl.store.raw.data.D_DiagnosticUtil.diag_conglomid' LANGUAGE JAVA PARAMETER STYLE JAVA"); 219 s.close(); 220 conn.commit(); 221 222 debug_system_procedures_created = true; 223 } 224 225 231 String dump_table( 232 Connection conn, 233 String schemaName, 234 String tableName, 235 boolean commit_transaction) 236 throws SQLException 237 { 238 if (!debug_system_procedures_created) 239 createDebugSystemProcedures(conn); 240 241 255 PreparedStatement ps = 256 conn.prepareStatement( 257 "select sys.systables.tablename, sys.sysconglomerates.conglomeratenumber, DIAG_CONGLOMID('wombat', conglomeratenumber) from sys.systables, sys.sysconglomerates where sys.systables.tableid = sys.sysconglomerates.tableid and sys.systables.schemaid = sys.sysconglomerates.schemaid and sys.systables.tablename = ?"); 258 ps.setString(1, tableName); 259 ResultSet rs = ps.executeQuery(); 260 261 if (!rs.next()) 262 { 263 if (SanityManager.DEBUG) 264 { 265 SanityManager.THROWASSERT("no value from values clause."); 266 } 267 } 268 269 String dump_table_info = rs.getString(3); 270 271 rs.close(); 272 273 if (commit_transaction) 274 conn.commit(); 275 276 return(dump_table_info); 277 278 } 279 280 293 protected String get_lock_info( 294 Connection conn, 295 boolean include_system_locks) 296 throws SQLException 297 { 298 String lock_query = 321 "select cast(l.xid as char(8)) as xid, cast(username as char(8)) as username, cast(t.type as char(8)) as trantype, cast(l.type as char(8)) as type, cast(lockcount as char(3)) as cnt, cast(mode as char(4)) as mode, cast(tablename as char(12)) as tabname, cast(lockname as char(10)) as lockname, state, status from SYSCS_DIAG.LOCK_TABLE l right outer join SYSCS_DIAG.LOCK_TABLE t on l.xid = t.xid where l.tableType <> 'S' "; 322 if (!include_system_locks) 323 lock_query += "and t.type='UserTransaction' "; 324 325 lock_query += "order by tabname, type desc, mode, cnt, lockname"; 326 327 PreparedStatement ps = conn.prepareStatement(lock_query); 328 329 ResultSet rs = ps.executeQuery(); 330 331 String lock_output = 332 "xid |username|trantype|type |cnt|mode|tabname |lockname |state|status\n" + 333 "---------------------------------------------------------------------------------\n"; 334 while (rs.next()) 335 { 336 String username = rs.getString(1); 337 String trantype = rs.getString(2); 338 String type = rs.getString(3); 339 String lockcount = rs.getString(4); 340 String mode = rs.getString(5); 341 String tabname = rs.getString(6); 342 String lockname = rs.getString(7); 343 String state = rs.getString(8); 344 String status = rs.getString(9); 345 346 lock_output += 347 username + "|" + 348 trantype + "|" + 349 type + "|" + 350 lockcount+ "|" + 351 mode + "|" + 352 tabname + "|" + 353 lockname + "|" + 354 state + "|" + 355 status + "\n"; 356 } 357 358 rs.close(); 359 360 return(lock_output); 361 } 362 363 371 public void createTable( 372 Connection conn, 373 String tbl_name, 374 String create_str) 375 throws SQLException 376 { 377 Statement stmt = conn.createStatement(); 378 379 381 try 382 { 383 stmt.executeUpdate("drop table " + tbl_name); 384 } 385 catch (Exception e) 386 { 387 } 389 390 stmt.executeUpdate(create_str); 391 } 392 393 410 protected static final int SPACE_INFO_IS_INDEX = 0; 411 protected static final int SPACE_INFO_NUM_ALLOC = 1; 412 protected static final int SPACE_INFO_NUM_FREE = 2; 413 protected static final int SPACE_INFO_NUM_UNFILLED = 3; 414 protected static final int SPACE_INFO_PAGE_SIZE = 4; 415 protected static final int SPACE_INFO_ESTIMSPACESAVING = 5; 416 417 protected static final int SPACE_INFO_NUMCOLS = 6; 418 419 protected int[] getSpaceInfo( 420 Connection conn, 421 String schemaName, 422 String tableName, 423 boolean commit_xact) 424 throws SQLException 425 { 426 String stmt_str = 427 "select conglomeratename, isindex, numallocatedpages, numfreepages, numunfilledpages, pagesize, estimspacesaving from new org.apache.derby.diag.SpaceTable('" + 428 tableName + "') t where isindex = 0"; 429 PreparedStatement space_stmt = conn.prepareStatement(stmt_str); 430 ResultSet rs = space_stmt.executeQuery(); 431 432 if (!rs.next()) 433 { 434 if (SanityManager.DEBUG) 435 { 436 SanityManager.THROWASSERT( 437 "No rows returned from space table query on table: " + 438 schemaName + "." + tableName); 439 } 440 } 441 442 int[] ret_info = new int[SPACE_INFO_NUMCOLS]; 443 String conglomerate_name = rs.getString(1); 444 for (int i = 0; i < SPACE_INFO_NUMCOLS; i++) 445 { 446 ret_info[i] = rs.getInt(i + 2); 447 } 448 449 if (rs.next()) 450 { 451 if (SanityManager.DEBUG) 452 { 453 SanityManager.THROWASSERT( 454 "More than one row returned from space query on table: " + 455 schemaName + "." + tableName); 456 } 457 } 458 459 if (verbose) 460 { 461 System.out.println( 462 "Space information for " + schemaName + "." + tableName + ":"); 463 System.out.println( 464 "isindex = " + ret_info[SPACE_INFO_IS_INDEX]); 465 System.out.println( 466 "num_alloc = " + ret_info[SPACE_INFO_NUM_ALLOC]); 467 System.out.println( 468 "num_free = " + ret_info[SPACE_INFO_NUM_FREE]); 469 System.out.println( 470 "num_unfilled = " + ret_info[SPACE_INFO_NUM_UNFILLED]); 471 System.out.println( 472 "page_size = " + ret_info[SPACE_INFO_PAGE_SIZE]); 473 System.out.println( 474 "estimspacesaving = " + ret_info[SPACE_INFO_ESTIMSPACESAVING]); 475 } 476 477 rs.close(); 478 479 if (commit_xact) 480 conn.commit(); 481 482 return(ret_info); 483 } 484 485 491 protected int total_pages(int[] space_info) 492 { 493 return(space_info[SPACE_INFO_NUM_FREE] + 494 space_info[SPACE_INFO_NUM_ALLOC]); 495 } 496 } 497 | Popular Tags |