📄 dblook.java
字号:
// Remove starting and ending quotes. return quotedName.substring(1, quotedName.length() - 1); } /* ************************************************ * isExcludedTable: * Takes a table name and determines whether or not * the DDL for objects related to that table should be * generated. * @param tableName name of the table to check. * @return true if 1) the user specified a table list * and that list does NOT include the received name; or * 2) if the user specified a schema restriction and * the received name does NOT have that schema; false * otherwise. ****/ public static boolean isExcludedTable(String tableName) { if (tableName == null) return true; int dot = tableName.indexOf("."); if (dot != -1) { // strip off the schema part of the name, and see if we're // okay to use it. if (isIgnorableSchema(tableName.substring(0, dot))) // then we exclude this table. return true; tableName = tableName.substring(dot + 1, tableName.length()); } return ((tableList != null) && !tableList.contains(tableName)); } /* ************************************************ * Takes a schema name and determines whether or * not the DDL for objects with that schema should * be generated. * @param schemaName schema name to be checked. * @return true if 1) the user specified a target * schema and that target is NOT the same as the * received schema name, or 2) the schema is a * system schema (SYS, SYSVISUAL, or SYSIBM); * false otherwise; ****/ public static final String[] ignorableSchemaNames = { "SYSIBM", "SYS", "SYSVISUAL", "SYSCAT", "SYSFUN", "SYSPROC", "SYSSTAT", "NULLID", "SYSCS_ADMIN", "SYSCS_DIAG", "SYSCS_UTIL", "SQLJ"}; public static boolean isIgnorableSchema(String schemaName) { if ((targetSchema != null) && (!schemaName.equals(targetSchema))) return true; schemaName = stripQuotes(schemaName); boolean ret = false; for (int i = ignorableSchemaNames.length - 1; i >= 0;) { if ((ret = ignorableSchemaNames[i--].equalsIgnoreCase(schemaName))) break; } return(ret); } /* ************************************************ * Takes a string and determines whether or not that * string makes reference to any of the table names * in the user-specified table list. * @param str The string in which to search for table names. * @return true if 1) the user didn't specify a * target table list, or 2) the received string * contains at least one of the table names in the * user-specified target list; false otherwise. ****/ public static boolean stringContainsTargetTable(String str) { if (str == null) // if the string is null, it can't possibly contain // any table names. return false; if (tableList == null) // if we have no target tables, then default to true. return true; int strLen = str.length(); for (int i = 0; i < tableList.size(); i++) { String tableName = (String)tableList.get(i); tableName = expandDoubleQuotes(stripQuotes(tableName)); int nameLen = tableName.length(); String strCopy; if (tableName.equals(tableName.toUpperCase( java.util.Locale.ENGLISH))) // case doesn't matter. strCopy = str.toUpperCase(); else strCopy = str; int pos = strCopy.indexOf(tableName); while (pos != -1) { // If we found it, make sure it's really a match. // First, see if it's part of another word. if (!partOfWord(str, pos, nameLen, strLen)) { // See if the match is in quotes--if so, then // it should match the table name's case. if ((pos >= 1) && (strCopy.charAt(pos-1) == '"') && (pos + nameLen < strCopy.length()) && (strCopy.charAt(pos+nameLen) == '"')) { // match is quoted; check it's case. if (str.substring(pos, pos + nameLen).equals(tableName)) // everything checks out. return true; } else // match isn't quoted, so we're okay as is. return true; } pos = str.indexOf(tableName, pos + nameLen); } } // If we get here, we didn't find it. return false; } /* ************************************************ * partOfWord: * Returns true if the part of the string given by * str.substring(pos, pos + nameLen) is part of * another word. * @param str The string in which we're looking. * @param pos The position at which the substring in * question begins. * @param nameLen the length of the substring in * question. * @param strLen The length of the string in which * we're looking. * @return true if the substring from pos to * pos+nameLen is part of larger word (i.e. * if it has a letter/digit immediately before * or after); false otherwise. ****/ private static boolean partOfWord (String str, int pos, int nameLen, int strLen) { boolean somethingBefore = false; if (pos > 0) { char c = str.charAt(pos-1); somethingBefore = ((c == '_') || Character.isLetterOrDigit(c)); } boolean somethingAfter = false; if (pos + nameLen < strLen) { char c = str.charAt(pos + nameLen); somethingAfter = ((c == '_') || Character.isLetterOrDigit(c)); } return (somethingBefore || somethingAfter); } /* ************************************************ * expandDoubleQuotes: * If the received SQL id contains a quote, we have * to expand it into TWO quotes so that it can be * treated correctly at parse time. * @param name Id that we want to print. ****/ public static String expandDoubleQuotes(String name) { if ((name == null) || (name.indexOf("\"") < 0)) // nothing to do. return name; char [] cA = name.toCharArray(); // Worst (and extremely unlikely) case is every // character is a double quote, which means the // escaped string would need to be 2 times as long. char [] result = new char[2*cA.length]; int j = 0; for (int i = 0; i < cA.length; i++) { if (cA[i] == '"') { result[j++] = '"'; result[j++] = '"'; } else result[j++] = cA[i]; } return new String(result, 0, j); } /* ************************************************ * lookupSchemaId: * Return the schema name corresponding to the * received schema id. * @param schemaId The id to look up. * @return the schema name. ****/ public static String lookupSchemaId(String schemaId) { return (String)(schemaMap.get(schemaId)); } /* ************************************************ * lookupTableId: * Return the table name corresponding to the * received table id. * @param tableId The id to look up. * @return the table name. ****/ public static String lookupTableId(String tableId) { return (String)(tableIdToNameMap.get(tableId)); } /* ************************************************ * writeVerboseOutput: * Writes the received string as "verbose" output, * meaning that we write it to System.err. We * choose System.err so that the string doesn't * show up if the user pipes dblook output to * a file (unless s/he explicitly pipes System.err * output to that file, as well). * @param key Key for the message to be printed as * verbose output. * @param value Value to be substituted into the * message. * @return message for received key has been printed * to System.err. ****/ public static void writeVerboseOutput(String key, String value) { if (value == null) System.err.println(lookupMessage(key)); else System.err.println(lookupMessage(key, new String [] {value})); return; } /* ************************************************ * lookupMessage: * Retrieve a localized message. * @param key The key for the localized message. * @return the message corresponding to the received * key. ****/ public static String lookupMessage(String key) { return lookupMessage(key, null); } /* ************************************************ * lookupMessage: * Retreive a localized message. * @param key The key for the localized message. * @param vals Array of values to be used in the * message. * @return the message corresponding to the received * key, with the received values substituted where * appropriate. ****/ public static String lookupMessage(String key, String[] vals) { String msg = ""; if (vals == null) msg = langUtil.getTextMessage(key); else { switch (vals.length) { case 1: msg = langUtil.getTextMessage( key, vals[0]); break; case 2: msg = langUtil.getTextMessage( key, vals[0], vals[1]); break; default: /* shouldn't happen */ break; } } return msg; } /* ************************************************ * removeNewlines: * Remove any newline characters from the received * string (replace them with spaces). * @param str The string from which we are removing * all newline characters. * @return The string, with all newline characters * replaced with spaces. ****/ public static String removeNewlines(String str) { if (str == null) // don't do anything. return null; StringBuffer result = null; try { BufferedReader strVal = new BufferedReader (new StringReader(str)); for (String txt = strVal.readLine(); txt != null; txt = strVal.readLine()) { if (result == null) result = new StringBuffer(txt); else { result.append(" "); result.append(txt); } } return result.toString(); } catch (Exception e) { // if something went wrong, just return the string as is-- // worst case is that the generated DDL is correct, it just // can't be run in some SQL script apps (because of the newline // characters). return str; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -