📄 dblook_test.java
字号:
); } catch (Exception e) { System.out.println("FAILED: "); e.printStackTrace(System.out); } return; } /* ********************************************** * lookFour: * Use dblook to generate DDL for all objects * in the source database with schema 'BAR' * that are related to tables 'T3', 'tWithKeys', * and 'MULTI WORD NAME'. * -z bar -t t3 "\"tWithKeys\"" "Multi word name" * @param dbName The name of the source database (i.e. * the database for which the DDL is generated). * @return The appropriate DDL has been generated * and written to a file called <dbName + ".sql">. ****/ private void lookFour(String dbName) throws Exception { printAsHeader("\nDumping DDL for all objects " + "with schema 'BAR'\nthat are related to tables " + "'T3', 'tWithKeys',\nand 'MULTI WORD NAME':\n"); String [] args = new String [] { "-o", dbName + ".sql", "-td", "", "-z", "BAR", "-t", "t3", "\"tWithKeys\"", "Multi word name" }; go(dbName, args); return; } /* ********************************************** * lookFive: * Use dblook to generate DDL for all objects * in the source database (with any schema) * that are related to table 'T1' and 'TWITHKEYS' * (with no matches existing for the latter). * -t t1 "tWithKeys" * @param dbName The name of the source database (i.e. * the database for which the DDL is generated). * @return The appropriate DDL has been generated * and written to a file called <dbName + ".sql">. ****/ private void lookFive(String dbName) throws Exception { printAsHeader("\nDumping DDL for all objects " + "related to 'T1'\nand 'TWITHKEYS':\n"); String [] args = new String [] { "-o", dbName + ".sql", "-td", "", "-t", "t1", "tWithKeys" }; go(dbName, args); return; } /* ********************************************** * lookSix: * Call dblook with an invalid url, to make * sure that errors are printed to log. * -d <dbName> // missing protocol. * @param dbName The name of the source database (i.e. * the database for which the DDL is generated). * @return The appropriate DDL has been generated * and written to a file called <dbName + ".sql">. ****/ private void lookSix(String dbName) throws Exception { printAsHeader("\nDumping DDL w/ invalid url, and " + "writing\nerror to the log:\n"); // Url is intentionally incorrect; it will cause an error. new dblook(new String[] { "-o", dbName + ".sql", "-d", dbName } ); } /* ********************************************** * lookSeven: * Use dblook to generate DDL for all objects * in the source database with schema '"Quoted"Schema"'. * -z \"\"Quoted\"Schema\"\" * @param dbName The name of the source database (i.e. * the database for which the DDL is generated). * @return The appropriate DDL has been generated * and written to a file called <dbName + ".sql">. ****/ private void lookSeven(String dbName) throws Exception { printAsHeader("\nDumping DDL for all objects " + "with schema\n'\"Quoted\"Schema\"':\n"); String [] args = new String[] { "-o", dbName + ".sql", "-td", "", "-z", "\"\"Quoted\"Schema\"\"" }; go(dbName, args); return; } /* ********************************************** * go: * Makes the call to execute the dblook command * using the received arguments. * @param dbName The name of the source database (i.e. * the database for which the DDL is generated). * @args The list of arguments with which to execute * the dblook command. ****/ private void go(String dbName, String [] args) { jdbcProtocol = "jdbc:derby:"; String sourceDBUrl = jdbcProtocol + dbPath + separator + dbName; String [] fullArgs = new String[args.length+2]; fullArgs[0] = "-d"; fullArgs[1] = sourceDBUrl; for (int i = 2; i < fullArgs.length; i++) fullArgs[i] = args[i-2]; try { new dblook(fullArgs); } catch (Exception e) { System.out.println("FAILED: to run dblook: "); e.printStackTrace(System.out); } } /* ********************************************** * runMessageCheckTest * Run dblook and verify that all of the dblook * messages are correctly displayed. * @param dbName The name of the source database (i.e. * the database for which the DDL is generated). * @return The DDL for a simple database, plus all * dblook messages, have been generated and written * to System.out. ****/ private void runMessageCheckTest(String dbName) throws Exception { // #1: First, run DB look standard to check for // all of the "header" messages that are printed // out along with DDL. System.out.println("\n************\n" + "Msg Test 1\n" + "************\n"); lookOne(dbName); dumpFileToSysOut(dbName + ".sql"); dumpFileToSysOut("dblook.log"); // Now, we have to run some additional dblook commands // to get the "non-standard" messages. // #2: Specify a target table and target schema, to // make sure they are echoed correctly. Also, specify // an output file to make sure the file creation header // is printed in the file. System.out.println( "\n************\n" + "Msg Test 2\n" + "************\n"); go(dbName, new String [] { "-t", "t1", "-z", "bar", "-o", dbName + ".sql" }); dumpFileToSysOut(dbName + ".sql"); dumpFileToSysOut("dblook.log"); // #3: Run without specifying a database, to make // sure the usage message is printed to System.out System.out.println( "\n************\n" + "Msg Test 3\n" + "************\n"); try { new dblook(new String[] { "-verbose" }); } catch (Exception e) { System.out.println("FAILED: to run dblook: "); e.printStackTrace(System.out); } // #4: Just to confirm, try once with a statement // delimiter, to make sure it's actually working // correctly (this isn't a "message" per se, but // still, it's worth verifying). System.out.println( "\n************\n" + "Msg Test 4\n" + "************\n"); go(dbName, new String [] { "-td", " " + TEST_DELIMITER }); // #5: Intentionally create an error while loading // a jar file, to make sure the resultant message is // printed correctly. System.out.println( "\n************\n" + "Msg Test 5\n" + "************\n"); // We'll cause the error by going in and deleting // the jar file from the test database. First, // get the jar path. String jarPath = (new File(dbPath + separator + dbName)).getAbsolutePath(); // Have to shut db down before we can mess with it. try { Connection conn = DriverManager.getConnection("jdbc:derby:" + jarPath + ";shutdown=true"); conn.close(); } catch (SQLException se) { // shutdown exception. } jarPath = jarPath + separator + "jar"; deleteFile(new File(jarPath)); // Now that we've deleted the jar file, run dblook // and check the error. go(dbName, new String [] { "-verbose", "-o", dbName + ".sql" }); dumpFileToSysOut("dblook.log"); // Clean up. try { deleteFile(new File(dbName + ".sql")); } catch (Exception e) { // not too big of a deal if we fail; just ignore... } } /* ********************************************** * dumpSysCatalogs: * Takes a database name and dumps ALL of the * system catalogs for that database, with the * exception of SYSSTATISTICS. This allows us * to look at the full contents of a database's * schema (without using dblook, of course) * so that we can see if the databases created * from the DDL generated by dblook have been * built correctly--if they have all of the * correct system catalog information, then * the databases themselves must be correct. * @param dbName The name of the database for which * we are dumping the system catalogs. * @return All of the system catalogs for * the received database have been dumped * to output. ****/ private void dumpSysCatalogs(String dbName) throws Exception { System.out.println("\nDumping system tables for '" + dbName + "'\n"); writeOut("\n----------------=================---------------"); writeOut("System Tables for: " + dbName); writeOut("----------------=================---------------\n"); // Connect to the database. Connection conn = DriverManager.getConnection( "jdbc:derby:" + dbName); conn.setAutoCommit(false); Statement stmt = conn.createStatement(); // Load any id-to-name mappings that will be useful // when dumping the catalogs. HashMap idToNameMap = loadIdMappings(stmt, conn); // Go through and dump all system catalog information, // filtering out database-dependent id's so that they // won't cause diffs. writeOut("\n========== SYSALIASES ==========\n"); ResultSet rs = stmt.executeQuery("select schemaid, sys.sysaliases.* from sys.sysaliases"); dumpResultSet(rs, idToNameMap, null); writeOut("\n========== SYSCHECKS ==========\n"); rs = stmt.executeQuery("select c.schemaid, ck.* from " + "sys.syschecks ck, sys.sysconstraints c where " + "ck.constraintid = c.constraintid"); dumpResultSet(rs, idToNameMap, null); writeOut("\n========== SYSCOLUMNS ==========\n"); writeOut("--- Columns for Tables ---"); rs = stmt.executeQuery("select t.schemaid, c.* from " + "sys.syscolumns c, sys.systables t where c.referenceid " + "= t.tableid" ); dumpResultSet(rs, idToNameMap, null); writeOut("\n--- Columns for Statements ---"); rs = stmt.executeQuery("select s.schemaid, c.* from " + "sys.syscolumns c, sys.sysstatements s where c.referenceid " + "= s.stmtid" ); dumpResultSet(rs, idToNameMap, null); writeOut("\n========== SYSCONGLOMERATES ==========\n"); rs = stmt.executeQuery("select schemaid, sys.sysconglomerates.* " + "from sys.sysconglomerates"); dumpResultSet(rs, idToNameMap, null); writeOut("\n========== SYSCONSTRAINTS ==========\n"); rs = stmt.executeQuery("select schemaid, sys.sysconstraints.* " + "from sys.sysconstraints"); dumpResultSet(rs, idToNameMap, null); writeOut("\n========== SYSDEPENDS ==========\n"); rs = stmt.executeQuery("select dependentid, sys.sysdepends.* from sys.sysdepends"); dumpResultSet(rs, idToNameMap, conn); writeOut("\n========== SYSFILES ==========\n"); rs = stmt.executeQuery("select schemaid, sys.sysfiles.* from sys.sysfiles"); dumpResultSet(rs, idToNameMap, null); writeOut("\n========== SYSFOREIGNKEYS ==========\n"); rs = stmt.executeQuery("select c.schemaid, fk.* from " + "sys.sysforeignkeys fk, sys.sysconstraints c where " + "fk.constraintid = c.constraintid"); dumpResultSet(rs, idToNameMap, null); writeOut("\n========== SYSKEYS ==========\n"); rs = stmt.executeQuery("select c.schemaid, k.* from " + "sys.syskeys k, sys.sysconstraints c where " + "k.constraintid = c.constraintid"); dumpResultSet(rs, idToNameMap, null); writeOut("\n========== SYSSCHEMAS ==========\n"); rs = stmt.executeQuery("select schemaid, sys.sysschemas.* from sys.sysschemas"); dumpResultSet(rs, idToNameMap, null); writeOut("\n========== SYSSTATEMENTS ==========\n"); rs = stmt.executeQuery("select schemaid, sys.sysstatements.* from sys.sysstatements"); dumpResultSet(rs, idToNameMap, null); writeOut("\n========== SYSTABLES ==========\n"); rs = stmt.executeQuery("select schemaid, sys.systables.* from sys.systables"); dumpResultSet(rs, idToNameMap, null); writeOut("\n========== SYSTRIGGERS ==========\n"); rs = stmt.executeQuery("select schemaid, sys.systriggers.* from sys.systriggers"); dumpResultSet(rs, idToNameMap, null); writeOut("\n========== SYSVIEWS ==========\n"); rs = stmt.executeQuery("select compilationschemaid, sys.sysviews.* from sys.sysviews"); dumpResultSet(rs, idToNameMap, null); stmt.close(); rs.close(); conn.commit(); conn.close(); return; } /* ********************************************** * isIgnorableSchema: * Returns true if the the schema is a "system" schema, vs. a user * schema. * @param schemaName name of schema to check. ****/ private boolean isIgnorableSchema(String schemaName) { boolean ret = false; for (int i = ignorableSchemaNames.length - 1; i >= 0;) { if ((ret = ignorableSchemaNames[i--].equalsIgnoreCase(schemaName))) break; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -