📄 dblook_test.java
字号:
/* ********************************************** * loadIdMappings: * Load mappings of object ids to object names * for purposes of having meaningful output * and for creating unique ids on the rows of * the system catalogs. * @param stmt Statement on a connection to the * database being examined. * @param conn Connection to the database being * examined. * @return A HashMap with all relevant id-to- * name mappings has been returned. ****/ private HashMap loadIdMappings(Statement stmt, Connection conn) throws Exception { HashMap idToNameMap = new HashMap(); // Table ids. ResultSet rs = stmt.executeQuery( "select tableid, tablename from sys.systables"); while (rs.next()) idToNameMap.put(rs.getString(1), rs.getString(2)); // Schema ids. rs = stmt.executeQuery( "select schemaid, schemaname from sys.sysschemas"); while (rs.next()) idToNameMap.put(rs.getString(1), rs.getString(2)); // Constraint ids. rs = stmt.executeQuery( "select constraintid, constraintname from " + "sys.sysconstraints"); while (rs.next()) idToNameMap.put(rs.getString(1), rs.getString(2)); return idToNameMap; } /* ********************************************** * getDependsData: * Forms a string containing detailed information * about a row in the SYSDEPENDS table, and returns * that string. * @param rs Result set with SYSDEPENDS rows; current * row is the one for which we're getting the * data. * @param conn Connection to the database being * examined. * @param idToNameMap mapping of object ids to names * for the database in question. * @return Schema, type and name of both the Provider * and the Dependent for the current row of * SYSDEPENDS have been returned as a string. ****/ private String getDependsData(ResultSet rs, Connection conn, HashMap idToNameMap) throws Exception { DependableFinder dep = (DependableFinder)rs.getObject(3); DependableFinder prov = (DependableFinder)rs.getObject(5); String depType = dep.getSQLObjectType(); String provType = prov.getSQLObjectType(); Statement dependsStmt = conn.createStatement(); StringBuffer dependsData = new StringBuffer(); dependsData.append(getHiddenDependsData(depType, rs.getString(2), dependsStmt, idToNameMap)); dependsData.append(" -> "); dependsData.append(getHiddenDependsData(provType, rs.getString(4), dependsStmt, idToNameMap)); return dependsData.toString(); } /* ********************************************** * getHiddenDependsData: * Returns a string containing the schema and * name of the object having the received id. * All object ids received by this message come * from rows of the SYSDEPENDS table. * @param type Type of the object that has the received * object id. * @param id Id of the object in question. * @param stmt Statement from the database in question. * @param idToNameMap mapping of ids to names for * the database in question. * @isProvider True if we're getting data for a * Provider object; false if we're getting data for * a Dependent object. * @return Schema, type, and name for the object with * the received id have been returned as a string. ****/ private String getHiddenDependsData(String type, String id, Statement pStmt, HashMap idToNameMap) throws Exception { ResultSet rs = null; if (type.equals("Constraint")) { rs = pStmt.executeQuery( "select schemaid, constraintname from " + "sys.sysconstraints where " + "constraintid = '" + id + "'"); } else if (type.equals("StoredPreparedStatement")) { rs = pStmt.executeQuery( "select schemaid, stmtname from " + "sys.sysstatements where stmtid = '" + id + "'"); } else if (type.equals("Trigger")) { rs = pStmt.executeQuery( "select schemaid, triggername from " + "sys.systriggers where triggerid = '" + id + "'"); } else if (type.equals("View") || type.equals("Table") || type.equals("ColumnsInTable")) { rs = pStmt.executeQuery( "select schemaid, tablename from " + "sys.systables where tableid = '" + id + "'"); } else if (type.equals("Conglomerate")) { rs = pStmt.executeQuery( "select schemaid, conglomeratename from " + "sys.sysconglomerates where conglomerateid = '" + id + "'"); } else { System.out.println("WARNING: Unexpected " + "dependent type: " + type); return ""; } if (rs.next()) { String schema = (String)idToNameMap.get(rs.getString(1)); if (isIgnorableSchema(schema)) // system object (so we want to ignore it); indicate // this by returning the string "SYS_OBJECT". return "SYS_OBJECT"; StringBuffer result = new StringBuffer(); result.append("<"); result.append(type); result.append(">"); result.append(schema); result.append("."); if (isSystemGenerated(rs.getString(2))) result.append("<sysname>"); else result.append(rs.getString(2)); return result.toString(); } return ""; } /* ********************************************** * deleteDB: * Deletes the database with the received name * from the test directory. * @param dbName Name of the database to be deleted. * @return Database has been completely deleted; * if deletion failed for any reason, a message * saying so has been printed to output. ****/ private void deleteDB(String dbName) throws Exception { // Get the full path. String deletePath = (new File(dbPath + separator + dbName)).getAbsolutePath(); // Have to shut it down before we can delete it. try { Connection conn = DriverManager.getConnection("jdbc:derby:" + deletePath + ";shutdown=true"); conn.close(); } catch (SQLException se) { // shutdown exception. } File f = new File(deletePath); if (!f.exists()) // nothing to do. return; File [] files = f.listFiles(); for (int i = 0; i < files.length; i++) deleteFile(files[i]); if (!f.delete()) { // still failed. System.out.println("ERROR: deleting: " + f.getName()); } // And finally, delete the CSJARS directory, // if there is one. deleteFile(new File(System.getProperty("user.dir") + separator + "CSJARS")); System.out.println("Database '" + dbName + "' deleted."); return; } /* ********************************************** * deleteFile: * Delete everything in a given directory, then * delete the directory itself (recursive). * @param aFile File object representing the directory * to be deleted. * @return the directory corresponding to aFile * has been deleted, as have all of its contents. ****/ private void deleteFile(File aFile) throws Exception { if (!aFile.exists()) // don't bother. return; if (aFile.delete()) // just a file; we're done. return; // Otherwise, have to descend and delete all // files in this directory. File [] files = aFile.listFiles(); if (files != null) { for (int i = 0; i < files.length; i++) deleteFile(files[i]); } // Now try to delete. if (!aFile.delete()) { // still failed. System.out.println("ERROR: deleting: " + aFile.getName()); } return; } /* ********************************************** * dumpFileToSysOut: * Checks to see if the received file is empty, * and prints a message saying so. * @param fName Name of the file to be written to output. * @return The contents of the specified file have * been written to System.out. ****/ private void dumpFileToSysOut(String fName) { try { BufferedReader dumpFile = new BufferedReader(new FileReader(fName)); String line = dumpFile.readLine(); if (line != null) { System.out.println("File " + fName + " was NOT " + "empty. Contents are:\n" + "############## Begin File Contents ################\n"); do { System.out.println(line); line = dumpFile.readLine(); } while (line != null); System.out.println( "############## End File Contents ################"); } else System.out.println("File " + fName + " was empty."); // Close the file. dumpFile.close(); } catch (Exception e) { System.out.println("FAILED: to dump file '" + fName + "'"); e.printStackTrace(System.out); } return; } /* ********************************************** * isSystemGenerated: * Returns true if the received string looks like * it is a system-generated string. We assume * it's system-generated if either 1) it starts * with the letters "SQL", in which case it's a * system-name, or 2) it has a dash in it, in which * case it's a system id. * @param str The string to check. * @return True if we assume the string is system- * generated, false otherwise. ****/ private boolean isSystemGenerated(String str) { return (looksLikeSysGenName(str) || looksLikeSysGenId(str)); } /* ********************************************** * looksLikeSysGenName: * See if the received string looks like it is * a system-generated name. There are two types * of system-generated names: 1) visible names, * which start with "SQL", and 2) hidden names, * which exist for Stored Statements that are * used to back triggers; these names start with * "TRIGGERACTN_" and then have a UUID. * NOTE: This test assumes that none of object names * provided in "dblook_makeDB.sql" satisfy * either of these conditions. If they do, they * will be filtered out of the test output. * @param val The string value in question. * @return True if the value looks like it is a system- * generated name; false otherwise. ****/ private boolean looksLikeSysGenName(String val) { return ((val != null) && ((val.trim().indexOf("SQL") == 0) || // case 1. ((val.trim().indexOf("TRIGGERACTN_") == 0) && // case 2. (val.indexOf("-") != -1)))); } /* ********************************************** * looksLikeSysGenId: * See if the received string looks like it is * a system-generated id (i.e. contains a dash (-)). * NOTE: This test assumes that none of object names * provided in "dblook_makeDB.sql" will contain * dashes. If they do, then they will be filtered out * in the test output. * @param val The string value in question. * @return True if the value looks like it is a system- * generated id; false otherwise. ****/ private boolean looksLikeSysGenId(String val) { return ((val != null) && (val.indexOf("-") != -1)); } /* ********************************************** * columnHoldsObjectName: * Return true if the received column, which is from * some system table, holds the _name_ of a database * object (table, constraint, etc.). Typically, we * can just look for the keyword "NAME"; the exception * is aliases, where the name is held in a column called * ALIAS. * @param colName Name of the column in question. * @return True if the column name indicates that it * holds the _name_ of a database object; false if the * column name indicates that it holds something else. ****/ private boolean columnHoldsObjectName(String colName) { return (colName.equals("ALIAS") || (colName.indexOf("NAME") != -1)); } /* ********************************************** * printAsHeader: * Print the received string to output as a * header. * @param str String to print. ****/ private void printAsHeader(String str) { writeOut("--\n*******************************************"); writeOut(str); writeOut("*******************************************\n"); return; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -