1 21 22 package org.apache.derbyTesting.functionTests.tests.tools; 23 24 import java.sql.DriverManager ; 25 import java.sql.ResultSet ; 26 import java.sql.Connection ; 27 import java.sql.Statement ; 28 import java.sql.PreparedStatement ; 29 import java.sql.ResultSetMetaData ; 30 import java.sql.SQLException ; 31 import java.sql.SQLWarning ; 32 import java.sql.Timestamp ; 33 34 import java.io.PrintWriter ; 35 import java.io.FileOutputStream ; 36 import java.io.FileNotFoundException ; 37 import java.io.BufferedReader ; 38 import java.io.FileReader ; 39 import java.io.File ; 40 41 import org.apache.derby.tools.dblook; 42 import org.apache.derby.tools.ij; 43 import org.apache.derby.catalog.DependableFinder; 44 import org.apache.derbyTesting.functionTests.util.TestUtil; 45 46 47 48 import java.util.HashMap ; 49 import java.util.TreeMap ; 50 import java.util.Set ; 51 import java.util.Iterator ; 52 import java.util.ArrayList ; 53 import java.util.StringTokenizer ; 54 55 public class dblook_test { 56 57 private static final int SERVER_PORT = 1527; 58 private static final int FRONT = -1; 59 private static final int REAR = 1; 60 61 protected static final String dbCreationScript_1 = "dblook_makeDB.sql"; 62 protected static final String dbCreationScript_2 = "dblook_makeDB_2.sql"; 63 private static final char TEST_DELIMITER='#'; 64 65 protected static String testDirectory = "dblook_test"; 66 protected static final String testDBName = "wombat"; 67 protected static String separator; 68 69 private static String dbPath; 70 private static int duplicateCounter = 0; 71 private static int sysNameCount = 0; 72 private static String jdbcProtocol; 73 74 77 78 public static void main (String [] args) { 79 80 separator = System.getProperty("file.separator"); 81 new dblook_test().doTest(); 82 System.out.println("\n[ Done. ]\n"); 83 84 } 85 86 90 91 protected void doTest() { 92 93 try { 94 95 System.out.println("\n-= Start dblook Functional Tests. =-"); 97 createTestDatabase(dbCreationScript_1); 98 runDBLook(testDBName); 99 100 System.out.println("\n-= Start dblook Message Tests =-"); 102 createTestDatabase(dbCreationScript_2); 103 runMessageCheckTest(testDBName); 104 105 } catch (SQLException se) { 106 107 System.out.println("FAILED: to complete the test:"); 108 se.printStackTrace(System.out); 109 for (se = se.getNextException(); se != null; 110 se = se.getNextException()) 111 { 112 se.printStackTrace(System.out); 113 } 114 115 } catch (Exception e) { 116 117 System.out.println("FAILED: to complete the test:"); 118 e.printStackTrace(System.out); 119 120 } 121 122 } 123 124 136 137 protected void createTestDatabase(String scriptName) 138 throws Exception 139 { 140 141 try { 143 deleteDB(testDBName); 144 } catch (Exception e) { 145 System.out.println("** Warning: failed to delete " + 146 "old test db before creating a new one..."); 147 } 148 149 Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance(); 150 jdbcProtocol = "jdbc:derby:"; 151 createDBFromDDL(testDBName, scriptName); 152 153 String systemhome = System.getProperty("derby.system.home"); 155 dbPath = systemhome + File.separatorChar; 156 return; 157 158 } 159 160 170 171 private void runDBLook(String dbName) 172 throws Exception 173 { 174 175 System.err.close(); 183 184 dumpSysCatalogs(dbName); 187 188 lookOne(dbName); 192 dumpFileToSysOut("dblook.log"); 193 194 String newDBName = dbName + "_new"; 197 createDBFromDDL(newDBName, dbName + ".sql"); 198 deleteFile(new File (dbName + ".sql")); 199 200 dumpSysCatalogs(newDBName); 204 205 deleteDB(newDBName); 207 deleteFile(new File (newDBName + ".sql")); 208 209 runAllTests(dbName, newDBName); 214 215 } 216 217 227 228 protected void runAllTests(String dbName, 229 String newDBName) throws Exception 230 { 231 232 runTest(2, dbName, newDBName); 233 234 237 runTest(4, dbName, newDBName); 238 runTest(5, dbName, newDBName); 239 runTest(7, dbName, newDBName); 240 runTest(6, dbName, newDBName); 241 return; 242 243 } 244 245 271 272 protected void runTest(int whichTest, String dbName, 273 String newDBName) 274 { 275 276 try { 277 278 switch(whichTest) { 279 case 2: lookTwo(dbName); break; 280 case 3: lookThree(dbName); break; 281 case 4: lookFour(dbName); break; 282 case 5: lookFive(dbName); break; 283 case 6: lookSix(dbName); break; 284 case 7: lookSeven(dbName); break; 285 default: break; 286 } 287 288 dumpFileToSysOut("dblook.log"); 289 createDBFromDDL(newDBName, dbName + ".sql"); 290 dumpSysCatalogs(newDBName); 291 deleteDB(newDBName); 292 deleteFile(new File (dbName + ".sql")); 293 294 } catch (SQLException e) { 295 296 System.out.println("FAILED: Test # : " + whichTest); 297 e.printStackTrace(System.out); 298 for (e = e.getNextException(); e != null; 299 e = e.getNextException()) 300 { 301 e.printStackTrace(System.out); 302 } 303 304 } catch (Exception e) { 305 306 System.out.println("FAILED: Test # : " + whichTest); 307 e.printStackTrace(System.out); 308 309 } 310 311 return; 312 313 } 314 315 325 326 private void lookOne(String dbName) 327 throws Exception 328 { 329 330 printAsHeader("\nDumping full schema for '" + 331 dbName + "'\nto file '" + dbName + ".sql':\n"); 332 333 String [] args = new String [] { 334 "-o", dbName + ".sql", 335 "-td", "" 336 }; 337 338 go(dbName, args); 339 return; 340 341 } 342 343 354 355 private void lookTwo(String dbName) 356 throws Exception 357 { 358 359 printAsHeader("\nDumping DDL for all objects " + 360 "with schema\n'BAR', excluding views:\n"); 361 362 String [] args = new String [] { 363 "-o", dbName + ".sql", 364 "-td", "", 365 "-z", "bar", 366 "-noview" 367 }; 368 369 go(dbName, args); 370 return; 371 372 } 373 374 384 385 private void lookThree(String dbName) 386 throws Exception 387 { 388 389 printAsHeader("\nDumping DDL for all objects, " + 390 "using\nNetwork Server:\n"); 391 String hostName = TestUtil.getHostName(); 392 jdbcProtocol = TestUtil.getJdbcUrlPrefix(hostName,SERVER_PORT); 393 394 String sourceDBUrl; 395 if (TestUtil.isJCCFramework()) 396 sourceDBUrl = jdbcProtocol + "\"" + dbPath + 397 separator + dbName + "\":user=someusr;password=somepwd;"; 398 else 399 sourceDBUrl = jdbcProtocol + dbPath + 400 separator + dbName + ";user=someusr;password=somepwd"; 401 402 try { 406 DriverManager.getConnection( 407 "jdbc:derby:" + dbName + ";shutdown=true"); 408 } catch (SQLException e) {} 409 410 try { 412 413 new dblook(new String [] { 414 "-d", sourceDBUrl, 415 "-o", dbName + ".sql", 416 "-td", "" } 417 ); 418 419 } catch (Exception e) { 420 System.out.println("FAILED: "); 421 e.printStackTrace(System.out); 422 } 423 424 return; 425 426 } 427 428 440 441 private void lookFour(String dbName) 442 throws Exception 443 { 444 445 printAsHeader("\nDumping DDL for all objects " + 446 "with schema 'BAR'\nthat are related to tables " + 447 "'T3', 'tWithKeys',\nand 'MULTI WORD NAME':\n"); 448 449 String [] args = new String [] { 450 "-o", dbName + ".sql", 451 "-td", "", 452 "-z", "BAR", 453 "-t", "t3", "\"tWithKeys\"", "Multi word name" 454 }; 455 456 go(dbName, args); 457 return; 458 459 } 460 461 473 474 private void lookFive(String dbName) 475 throws Exception 476 { 477 478 printAsHeader("\nDumping DDL for all objects " + 479 "related to 'T1'\nand 'TWITHKEYS':\n"); 480 481 String [] args = new String [] { 482 "-o", dbName + ".sql", 483 "-td", "", 484 "-t", "t1", "tWithKeys" 485 }; 486 487 go(dbName, args); 488 return; 489 490 } 491 492 502 503 private void lookSix(String dbName) 504 throws Exception 505 { 506 507 printAsHeader("\nDumping DDL w/ invalid url, and " + 508 "writing\nerror to the log:\n"); 509 510 new dblook(new String [] { 512 "-o", dbName + ".sql", 513 "-d", dbName } 514 ); 515 516 } 517 518 528 529 private void lookSeven(String dbName) 530 throws Exception 531 { 532 533 printAsHeader("\nDumping DDL for all objects " + 534 "with schema\n'\"Quoted\"Schema\"':\n"); 535 536 String [] args = new String [] { 537 "-o", dbName + ".sql", 538 "-td", "", 539 "-z", "\"\"Quoted\"Schema\"\"" 540 }; 541 542 go(dbName, args); 543 return; 544 545 } 546 547 556 557 private void go(String dbName, String [] args) { 558 559 jdbcProtocol = "jdbc:derby:"; 560 String sourceDBUrl = jdbcProtocol + dbPath + 561 separator + dbName; 562 563 String [] fullArgs = new String [args.length+2]; 564 fullArgs[0] = "-d"; 565 fullArgs[1] = sourceDBUrl; 566 for (int i = 2; i < fullArgs.length; i++) 567 fullArgs[i] = args[i-2]; 568 569 try { 570 new dblook(fullArgs); 571 } catch (Exception e) { 572 System.out.println("FAILED: to run dblook: "); 573 e.printStackTrace(System.out); 574 } 575 576 } 577 578 588 private void runMessageCheckTest(String dbName) 589 throws Exception 590 { 591 592 System.out.println("\n************\n" + 596 "Msg Test 1\n" + 597 "************\n"); 598 lookOne(dbName); 599 dumpFileToSysOut(dbName + ".sql"); 600 dumpFileToSysOut("dblook.log"); 601 602 605 System.out.println( 610 "\n************\n" + 611 "Msg Test 2\n" + 612 "************\n"); 613 go(dbName, new String [] { 614 "-t", "t1", 615 "-z", "bar", 616 "-o", dbName + ".sql" 617 }); 618 dumpFileToSysOut(dbName + ".sql"); 619 dumpFileToSysOut("dblook.log"); 620 621 System.out.println( 624 "\n************\n" + 625 "Msg Test 3\n" + 626 "************\n"); 627 try { 628 new dblook(new String [] { "-verbose" }); 629 } catch (Exception e) { 630 System.out.println("FAILED: to run dblook: "); 631 e.printStackTrace(System.out); 632 } 633 634 System.out.println( 639 "\n************\n" + 640 "Msg Test 4\n" + 641 "************\n"); 642 go(dbName, new String [] { 643 "-td", " " + TEST_DELIMITER 644 }); 645 646 System.out.println( 650 "\n************\n" + 651 "Msg Test 5\n" + 652 "************\n"); 653 654 String jarPath = (new 658 File (dbPath + separator + dbName)).getAbsolutePath(); 659 660 try { 662 Connection conn = 663 DriverManager.getConnection("jdbc:derby:" + 664 jarPath + ";shutdown=true"); 665 conn.close(); 666 } catch (SQLException se) { 667 } 669 670 jarPath = jarPath + separator + "jar"; 671 deleteFile(new File (jarPath)); 672 673 go(dbName, new String [] { 676 "-verbose", 677 "-o", dbName + ".sql" 678 }); 679 dumpFileToSysOut("dblook.log"); 680 681 try { 683 deleteFile(new File (dbName + ".sql")); 684 } catch (Exception e) { 685 } 687 688 } 689 690 708 709 private void dumpSysCatalogs(String dbName) 710 throws Exception 711 { 712 713 System.out.println("\nDumping system tables for '" + dbName + "'\n"); 714 715 writeOut("\n----------------=================---------------"); 716 writeOut("System Tables for: " + dbName); 717 writeOut("----------------=================---------------\n"); 718 719 Connection conn = DriverManager.getConnection( 721 "jdbc:derby:" + dbName); 722 conn.setAutoCommit(false); 723 Statement stmt = conn.createStatement(); 724 725 HashMap idToNameMap = loadIdMappings(stmt, conn); 728 729 733 writeOut("\n========== SYSALIASES ==========\n"); 734 ResultSet rs = 735 stmt.executeQuery("select schemaid, sys.sysaliases.* from sys.sysaliases"); 736 dumpResultSet(rs, idToNameMap, null); 737 738 writeOut("\n========== SYSCHECKS ==========\n"); 739 rs = stmt.executeQuery("select c.schemaid, ck.* from " + 740 "sys.syschecks ck, sys.sysconstraints c where " + 741 "ck.constraintid = c.constraintid"); 742 dumpResultSet(rs, idToNameMap, null); 743 744 writeOut("\n========== SYSCOLUMNS ==========\n"); 745 writeOut("--- Columns for Tables ---"); 746 rs = stmt.executeQuery("select t.schemaid, c.* from " + 747 "sys.syscolumns c, sys.systables t where c.referenceid " + 748 "= t.tableid" ); 749 dumpResultSet(rs, idToNameMap, null); 750 writeOut("\n--- Columns for Statements ---"); 751 rs = stmt.executeQuery("select s.schemaid, c.* from " + 752 "sys.syscolumns c, sys.sysstatements s where c.referenceid " + 753 "= s.stmtid" ); 754 dumpResultSet(rs, idToNameMap, null); 755 756 writeOut("\n========== SYSCONGLOMERATES ==========\n"); 757 rs = stmt.executeQuery("select schemaid, sys.sysconglomerates.* " + 758 "from sys.sysconglomerates"); 759 dumpResultSet(rs, idToNameMap, null); 760 761 writeOut("\n========== SYSCONSTRAINTS ==========\n"); 762 rs = stmt.executeQuery("select schemaid, sys.sysconstraints.* " + 763 "from sys.sysconstraints"); 764 dumpResultSet(rs, idToNameMap, null); 765 766 writeOut("\n========== SYSDEPENDS ==========\n"); 767 rs = stmt.executeQuery("select dependentid, sys.sysdepends.* from sys.sysdepends"); 768 dumpResultSet(rs, idToNameMap, conn); 769 770 writeOut("\n========== SYSFILES ==========\n"); 771 rs = stmt.executeQuery("select schemaid, sys.sysfiles.* from sys.sysfiles"); 772 dumpResultSet(rs, idToNameMap, null); 773 774 writeOut("\n========== SYSFOREIGNKEYS ==========\n"); 775 rs = stmt.executeQuery("select c.schemaid, fk.* from " + 776 "sys.sysforeignkeys fk, sys.sysconstraints c where " + 777 "fk.constraintid = c.constraintid"); 778 dumpResultSet(rs, idToNameMap, null); 779 780 writeOut("\n========== SYSKEYS ==========\n"); 781 rs = stmt.executeQuery("select c.schemaid, k.* from " + 782 "sys.syskeys k, sys.sysconstraints c where " + 783 "k.constraintid = c.constraintid"); 784 dumpResultSet(rs, idToNameMap, null); 785 786 writeOut("\n========== SYSSCHEMAS ==========\n"); 787 rs = stmt.executeQuery("select schemaid, sys.sysschemas.* from sys.sysschemas"); 788 dumpResultSet(rs, idToNameMap, null); 789 790 writeOut("\n========== SYSSTATEMENTS ==========\n"); 791 rs = stmt.executeQuery("select schemaid, sys.sysstatements.* from sys.sysstatements"); 792 dumpResultSet(rs, idToNameMap, null); 793 794 writeOut("\n========== SYSTABLES ==========\n"); 795 rs = stmt.executeQuery("select schemaid, sys.systables.* from sys.systables"); 796 dumpResultSet(rs, idToNameMap, null); 797 798 writeOut("\n========== SYSTRIGGERS ==========\n"); 799 rs = stmt.executeQuery("select schemaid, sys.systriggers.* from sys.systriggers"); 800 dumpResultSet(rs, idToNameMap, null); 801 802 writeOut("\n========== SYSVIEWS ==========\n"); 803 rs = stmt.executeQuery("select compilationschemaid, sys.sysviews.* from sys.sysviews"); 804 dumpResultSet(rs, idToNameMap, null); 805 806 stmt.close(); 807 rs.close(); 808 conn.commit(); 809 conn.close(); 810 return; 811 812 } 813 814 820 private boolean isIgnorableSchema(String schemaName) { 821 822 boolean ret = false; 823 824 for (int i = ignorableSchemaNames.length - 1; i >= 0;) 825 { 826 if ((ret = ignorableSchemaNames[i--].equalsIgnoreCase(schemaName))) 827 break; 828 } 829 830 return(ret); 831 } 832 833 private static final String [] ignorableSchemaNames = { 834 "SYSIBM", 835 "SYS", 836 "SYSVISUAL", 837 "SYSCAT", 838 "SYSFUN", 839 "SYSPROC", 840 "SYSSTAT", 841 "NULLID", 842 "SYSCS_ADMIN", 843 "SYSCS_DIAG", 844 "SYSCS_UTIL", 845 "SQLJ"}; 846 847 869 870 private void dumpResultSet (ResultSet rs, 871 HashMap idToNameMap, Connection conn) 872 throws Exception 873 { 874 875 StringBuffer uniqueName = new StringBuffer (); 889 890 TreeMap orderedRows = new TreeMap (); 891 ArrayList rowValues = new ArrayList (); 892 ArrayList duplicateRowIds = new ArrayList (); 893 894 ResultSetMetaData rsmd = rs.getMetaData(); 895 int cols = rsmd.getColumnCount(); 896 while (rs.next()) { 897 898 for (int i = 1; i <= cols; i++) { 899 900 String colName = rsmd.getColumnName(i); 901 String value = rs.getString(i); 902 String mappedName = (String )idToNameMap.get(value); 903 904 if ((colName.indexOf("SCHEMAID") != -1) && 905 (mappedName != null) && 906 ((mappedName.indexOf("SYS") != -1) || 907 (isIgnorableSchema(mappedName)))) 908 { 909 rowValues = null; 916 break; 917 } 918 else if (colName.equals("JAVACLASSNAME") && (value != null) && 919 (value.indexOf("org.apache.derby") != -1) && 920 (value.indexOf(".util.") == -1)) { 921 rowValues = null; 926 break; 927 } 928 929 if (i == 1) 930 continue; 934 935 String uniquePiece = dumpColumnData(colName, 936 value, mappedName, rowValues); 937 938 if (colName.equals("DEPENDENTID")) { 939 String hiddenInfo = getDependsData(rs, conn, 943 idToNameMap); 944 if (hiddenInfo.indexOf("SYS_OBJECT") != -1) { 945 rowValues = null; 948 break; 949 } 950 uniqueName.append(hiddenInfo); 951 rowValues.add(hiddenInfo); 954 } 955 956 if (uniquePiece != null) 957 uniqueName.append(uniquePiece); 958 959 if (colName.equals("STMTNAME") && 960 (value.indexOf("TRIGGERACTN") != -1)) 961 uniqueName.append(rs.getString(6)); 971 972 } 973 974 if (rowValues != null) { 975 976 if (duplicateRowIds.contains(uniqueName.toString())) 977 handleDuplicateRow(rowValues, null, orderedRows); 981 else { 982 ArrayList oldRow = (ArrayList )(orderedRows.put( 983 uniqueName.toString(), rowValues)); 984 if (oldRow != null) { 985 duplicateRowIds.add(uniqueName.toString()); 987 orderedRows.remove(uniqueName.toString()); 989 handleDuplicateRow(rowValues, oldRow, orderedRows); 990 } 991 } 992 } 993 994 uniqueName = new StringBuffer (); 995 rowValues = new ArrayList (); 996 997 } 998 999 Set objectNames = orderedRows.keySet(); 1002 for (Iterator itr = objectNames.iterator(); 1003 itr.hasNext(); ) { 1004 1005 String row = (String )itr.next(); 1006 ArrayList colData = (ArrayList )orderedRows.get(row); 1007 for (int i = 0; i < colData.size(); i++) 1008 writeOut((String )colData.get(i)); 1009 writeOut("----"); 1010 1011 } 1012 1013 orderedRows = null; 1014 rs.close(); 1015 1016 } 1017 1018 1040 1041 private String dumpColumnData(String colName, 1042 String value, String mappedName, ArrayList rowVals) 1043 { 1044 1045 if (mappedName == null) { 1046 if (colName.equals("CONGLOMERATENUMBER") || 1048 colName.equals("GENERATIONID")) 1049 rowVals.add("<systemnumber>"); 1054 else if (colName.equals("AUTOINCREMENTVALUE")) 1055 rowVals.add("<autoincval>"); 1059 else if (colName.equals("VALID")) 1060 rowVals.add("<validityflag>"); 1065 else if (value != null) { 1066 if (looksLikeSysGenName(value)) { 1067 if (columnHoldsObjectName(colName)) 1068 rowVals.add("<systemname>"); 1069 else { 1070 rowVals.add(value); 1072 return value; 1073 } 1074 } 1075 else if (looksLikeSysGenId(value)) 1076 rowVals.add("<systemid>"); 1077 else { 1078 rowVals.add(value); 1079 if (columnHoldsObjectName(colName)) 1080 return value; 1083 } 1084 } 1085 else 1086 rowVals.add(value); 1088 } 1089 else { 1090 if (!isSystemGenerated(mappedName)) { 1092 rowVals.add(mappedName); 1094 return mappedName; 1095 } 1096 else 1097 rowVals.add("<systemname>"); 1098 } 1099 1100 return null; 1103 1104 } 1105 1106 1132 1133 private void handleDuplicateRow( 1134 ArrayList newRow, ArrayList oldRow, 1135 TreeMap orderedRows) 1136 { 1137 1138 1141 StringBuffer newRowId = new StringBuffer (); 1142 for (int i = 0; i < newRow.size(); i++) 1143 newRowId.append((String )newRow.get(i)); 1144 1145 Object obj = (ArrayList )(orderedRows.put( 1146 newRowId.toString(), newRow)); 1147 if (obj != null) 1148 orderedRows.put(newRowId.toString() + 1150 duplicateCounter++, newRow); 1151 1152 if (oldRow != null) { 1153 1154 StringBuffer oldRowId = new StringBuffer (); 1155 for (int i = 0; i < oldRow.size(); i++) 1156 oldRowId.append((String )oldRow.get(i)); 1157 1158 obj = (ArrayList )(orderedRows.put( 1159 oldRowId.toString(), oldRow)); 1160 if (obj != null) 1161 orderedRows.put(oldRowId.toString() + 1163 duplicateCounter++, oldRow); 1164 } 1165 1166 return; 1167 1168 } 1169 1170 1181 1182 private void createDBFromDDL(String newDBName, 1183 String scriptName) throws Exception 1184 { 1185 1186 System.out.println("\n\nCreating database '" + newDBName + 1187 "' from ddl script '" + scriptName + "'"); 1188 1189 Connection conn = DriverManager.getConnection( 1190 "jdbc:derby:" + newDBName + ";create=true"); 1191 1192 Statement stmt = conn.createStatement(); 1193 BufferedReader ddlScript = 1194 new BufferedReader (new FileReader (scriptName)); 1195 1196 for (String sqlCmd = ddlScript.readLine(); sqlCmd != null; 1197 sqlCmd = ddlScript.readLine()) { 1198 1199 if (sqlCmd.indexOf("--") == 0) 1200 continue; 1202 else if (sqlCmd.trim().length() == 0) 1203 continue; 1205 1206 if ((sqlCmd.charAt(sqlCmd.length()-1) == TEST_DELIMITER) 1208 || (sqlCmd.charAt(sqlCmd.length()-1) == ';')) 1209 sqlCmd = sqlCmd.substring(0, sqlCmd.length()-1); 1211 1212 try { 1213 stmt.execute(sqlCmd); 1214 } catch (Exception e) { 1215 System.out.println("FAILED: to execute cmd " + 1216 "from DDL script:\n" + sqlCmd + "\n"); 1217 System.out.println(e.getMessage()); 1218 } 1219 1220 } 1221 1222 ddlScript.close(); 1224 stmt.close(); 1225 conn.close(); 1226 1227 return; 1228 1229 } 1230 1231 1236 1237 private static void writeOut(String str) { 1238 1239 System.out.println(str); 1240 return; 1241 1242 } 1243 1244 1257 1258 private HashMap loadIdMappings(Statement stmt, 1259 Connection conn) throws Exception { 1260 1261 HashMap idToNameMap = new HashMap (); 1262 1263 ResultSet rs = stmt.executeQuery( 1265 "select tableid, tablename from sys.systables"); 1266 while (rs.next()) 1267 idToNameMap.put(rs.getString(1), rs.getString(2)); 1268 1269 rs = stmt.executeQuery( 1271 "select schemaid, schemaname from sys.sysschemas"); 1272 while (rs.next()) 1273 idToNameMap.put(rs.getString(1), rs.getString(2)); 1274 1275 rs = stmt.executeQuery( 1277 "select constraintid, constraintname from " + 1278 "sys.sysconstraints"); 1279 while (rs.next()) 1280 idToNameMap.put(rs.getString(1), rs.getString(2)); 1281 1282 return idToNameMap; 1283 1284 } 1285 1286 1302 1303 private String getDependsData(ResultSet rs, 1304 Connection conn, HashMap idToNameMap) 1305 throws Exception 1306 { 1307 1308 DependableFinder dep = 1309 (DependableFinder)rs.getObject(3); 1310 1311 DependableFinder prov = 1312 (DependableFinder)rs.getObject(5); 1313 1314 String depType = dep.getSQLObjectType(); 1315 String provType = prov.getSQLObjectType(); 1316 1317 Statement dependsStmt = conn.createStatement(); 1318 StringBuffer dependsData = new StringBuffer (); 1319 dependsData.append(getHiddenDependsData(depType, 1320 rs.getString(2), dependsStmt, idToNameMap)); 1321 dependsData.append(" -> "); 1322 dependsData.append(getHiddenDependsData(provType, 1323 rs.getString(4), dependsStmt, idToNameMap)); 1324 1325 return dependsData.toString(); 1326 1327 } 1328 1329 1347 1348 private String getHiddenDependsData(String type, 1349 String id, Statement pStmt, HashMap idToNameMap) 1350 throws Exception 1351 { 1352 1353 ResultSet rs = null; 1354 if (type.equals("Constraint")) { 1355 rs = pStmt.executeQuery( 1356 "select schemaid, constraintname from " + 1357 "sys.sysconstraints where " + 1358 "constraintid = '" + id + "'"); 1359 } 1360 else if (type.equals("StoredPreparedStatement")) { 1361 rs = pStmt.executeQuery( 1362 "select schemaid, stmtname from " + 1363 "sys.sysstatements where stmtid = '" + 1364 id + "'"); 1365 } 1366 else if (type.equals("Trigger")) { 1367 rs = pStmt.executeQuery( 1368 "select schemaid, triggername from " + 1369 "sys.systriggers where triggerid = '" + 1370 id + "'"); 1371 } 1372 else if (type.equals("View") || type.equals("Table") 1373 || type.equals("ColumnsInTable")) { 1374 rs = pStmt.executeQuery( 1375 "select schemaid, tablename from " + 1376 "sys.systables where tableid = '" + 1377 id + "'"); 1378 } 1379 else if (type.equals("Conglomerate")) { 1380 rs = pStmt.executeQuery( 1381 "select schemaid, conglomeratename from " + 1382 "sys.sysconglomerates where conglomerateid = '" + 1383 id + "'"); 1384 } 1385 else { 1386 System.out.println("WARNING: Unexpected " + 1387 "dependent type: " + type); 1388 return ""; 1389 } 1390 1391 if (rs.next()) { 1392 String schema = (String )idToNameMap.get(rs.getString(1)); 1393 if (isIgnorableSchema(schema)) 1394 return "SYS_OBJECT"; 1397 StringBuffer result = new StringBuffer (); 1398 result.append("<"); 1399 result.append(type); 1400 result.append(">"); 1401 result.append(schema); 1402 result.append("."); 1403 if (isSystemGenerated(rs.getString(2))) 1404 result.append("<sysname>"); 1405 else 1406 result.append(rs.getString(2)); 1407 return result.toString(); 1408 } 1409 1410 return ""; 1411 1412 } 1413 1414 1423 1424 private void deleteDB(String dbName) 1425 throws Exception 1426 { 1427 1428 String deletePath = (new 1430 File (dbPath + separator + dbName)).getAbsolutePath(); 1431 1432 try { 1434 Connection conn = 1435 DriverManager.getConnection("jdbc:derby:" + 1436 deletePath + ";shutdown=true"); 1437 conn.close(); 1438 } catch (SQLException se) { 1439 } 1441 1442 File f = new File (deletePath); 1443 if (!f.exists()) 1444 return; 1446 1447 File [] files = f.listFiles(); 1448 for (int i = 0; i < files.length; i++) 1449 deleteFile(files[i]); 1450 1451 if (!f.delete()) { 1452 System.out.println("ERROR: deleting: " + 1454 f.getName()); 1455 } 1456 1457 deleteFile(new File (System.getProperty("user.dir") + 1460 separator + "CSJARS")); 1461 1462 System.out.println("Database '" + dbName + "' deleted."); 1463 return; 1464 1465 } 1466 1467 1476 1477 private void deleteFile(File aFile) 1478 throws Exception 1479 { 1480 1481 if (!aFile.exists()) 1482 return; 1484 1485 if (aFile.delete()) 1486 return; 1488 1489 File [] files = aFile.listFiles(); 1492 if (files != null) { 1493 for (int i = 0; i < files.length; i++) 1494 deleteFile(files[i]); 1495 } 1496 1497 if (!aFile.delete()) { 1499 System.out.println("ERROR: deleting: " + 1501 aFile.getName()); 1502 } 1503 1504 return; 1505 1506 } 1507 1508 1516 1517 private void dumpFileToSysOut(String fName) { 1518 1519 try { 1520 1521 BufferedReader dumpFile = 1522 new BufferedReader (new FileReader (fName)); 1523 1524 String line = dumpFile.readLine(); 1525 if (line != null) { 1526 System.out.println("File " + fName + " was NOT " + 1527 "empty. Contents are:\n" + 1528 "############## Begin File Contents ################\n"); 1529 do { 1530 System.out.println(line); 1531 line = dumpFile.readLine(); 1532 } while (line != null); 1533 System.out.println( 1534 "############## End File Contents ################"); 1535 } 1536 else 1537 System.out.println("File " + fName + " was empty."); 1538 1539 dumpFile.close(); 1541 1542 } catch (Exception e) { 1543 System.out.println("FAILED: to dump file '" + fName + "'"); 1544 e.printStackTrace(System.out); 1545 } 1546 1547 return; 1548 1549 } 1550 1551 1563 1564 private boolean isSystemGenerated(String str) { 1565 1566 return (looksLikeSysGenName(str) || 1567 looksLikeSysGenId(str)); 1568 1569 } 1570 1571 1588 1589 private boolean looksLikeSysGenName(String val) { 1590 1591 return ((val != null) && 1592 ((val.trim().indexOf("SQL") == 0) || ((val.trim().indexOf("TRIGGERACTN_") == 0) && (val.indexOf("-") != -1)))); 1595 1596 } 1597 1598 1610 1611 private boolean looksLikeSysGenId(String val) { 1612 1613 return ((val != null) && (val.indexOf("-") != -1)); 1614 1615 } 1616 1617 1630 1631 private boolean columnHoldsObjectName(String colName) { 1632 1633 return (colName.equals("ALIAS") || 1634 (colName.indexOf("NAME") != -1)); 1635 1636 } 1637 1638 1644 1645 private void printAsHeader(String str) { 1646 1647 writeOut("--\n*******************************************"); 1648 writeOut(str); 1649 writeOut("*******************************************\n"); 1650 return; 1651 1652 } 1653 1654} 1655 | Popular Tags |