?? databasemetadata.java
字號:
* @return true if changes are detected by the resultset type * @exception SQLException * if a database-access error occurs. */ public boolean deletesAreDetected(int type) throws SQLException { return false; } // ---------------------------------------------------------------------- /** * Did getMaxRowSize() include LONGVARCHAR and LONGVARBINARY blobs? * * @return true if so * @throws SQLException * DOCUMENT ME! */ public boolean doesMaxRowSizeIncludeBlobs() throws SQLException { return true; } /** * Finds the end of the parameter declaration from the output of "SHOW * CREATE PROCEDURE". * * @param beginIndex * should be the index of the procedure body that contains the * first "(". * @param procedureDef * the procedure body * @param quoteChar * the identifier quote character in use * @return the ending index of the parameter declaration, not including the * closing ")" * @throws SQLException * if a parse error occurs. */ private int endPositionOfParameterDeclaration(int beginIndex, String procedureDef, String quoteChar) throws SQLException { int currentPos = beginIndex + 1; int parenDepth = 1; // counting the first openParen while (parenDepth > 0 && currentPos < procedureDef.length()) { int closedParenIndex = StringUtils.indexOfIgnoreCaseRespectQuotes( currentPos, procedureDef, ")", quoteChar.charAt(0), !this.conn.isNoBackslashEscapesSet()); if (closedParenIndex != -1) { int nextOpenParenIndex = StringUtils .indexOfIgnoreCaseRespectQuotes(currentPos, procedureDef, "(", quoteChar.charAt(0), !this.conn.isNoBackslashEscapesSet()); if (nextOpenParenIndex != -1 && nextOpenParenIndex < closedParenIndex) { parenDepth++; currentPos = closedParenIndex + 1; // set after closed // paren that increases // depth } else { parenDepth--; currentPos = closedParenIndex; // start search from same // position } } else { // we should always get closed paren of some sort throw SQLError .createSQLException( "Internal error when parsing callable statement metadata", SQLError.SQL_STATE_GENERAL_ERROR); } } return currentPos; } /** * Extracts foreign key info for one table. * * @param rows * the list of rows to add to * @param rs * the result set from 'SHOW CREATE TABLE' * @param catalog * the database name * @return the list of rows with new rows added * @throws SQLException * if a database access error occurs */ public List extractForeignKeyForTable(ArrayList rows, java.sql.ResultSet rs, String catalog) throws SQLException { byte[][] row = new byte[3][]; row[0] = rs.getBytes(1); row[1] = s2b(SUPPORTS_FK); String createTableString = rs.getString(2); StringTokenizer lineTokenizer = new StringTokenizer(createTableString, "\n"); StringBuffer commentBuf = new StringBuffer("comment; "); boolean firstTime = true; String quoteChar = getIdentifierQuoteString(); if (quoteChar == null) { quoteChar = "`"; } while (lineTokenizer.hasMoreTokens()) { String line = lineTokenizer.nextToken().trim(); String constraintName = null; if (StringUtils.startsWithIgnoreCase(line, "CONSTRAINT")) { boolean usingBackTicks = true; int beginPos = line.indexOf(quoteChar); if (beginPos == -1) { beginPos = line.indexOf("\""); usingBackTicks = false; } if (beginPos != -1) { int endPos = -1; if (usingBackTicks) { endPos = line.indexOf(quoteChar, beginPos + 1); } else { endPos = line.indexOf("\"", beginPos + 1); } if (endPos != -1) { constraintName = line.substring(beginPos + 1, endPos); line = line.substring(endPos + 1, line.length()).trim(); } } } if (line.startsWith("FOREIGN KEY")) { if (line.endsWith(",")) { line = line.substring(0, line.length() - 1); } char quote = this.quotedId.charAt(0); int indexOfFK = line.indexOf("FOREIGN KEY"); String localColumnName = null; String referencedCatalogName = this.quotedId + catalog + this.quotedId; String referencedTableName = null; String referencedColumnName = null; if (indexOfFK != -1) { int afterFk = indexOfFK + "FOREIGN KEY".length(); int indexOfRef = StringUtils .indexOfIgnoreCaseRespectQuotes(afterFk, line, "REFERENCES", quote, true); if (indexOfRef != -1) { int indexOfParenOpen = line.indexOf('(', afterFk); int indexOfParenClose = StringUtils .indexOfIgnoreCaseRespectQuotes( indexOfParenOpen, line, ")", quote, true); if (indexOfParenOpen == -1 || indexOfParenClose == -1) { // throw SQLError.createSQLException(); } localColumnName = line.substring(indexOfParenOpen + 1, indexOfParenClose); int afterRef = indexOfRef + "REFERENCES".length(); int referencedColumnBegin = StringUtils .indexOfIgnoreCaseRespectQuotes(afterRef, line, "(", quote, true); if (referencedColumnBegin != -1) { referencedTableName = line.substring(afterRef, referencedColumnBegin); int referencedColumnEnd = StringUtils .indexOfIgnoreCaseRespectQuotes( referencedColumnBegin + 1, line, ")", quote, true); if (referencedColumnEnd != -1) { referencedColumnName = line.substring( referencedColumnBegin + 1, referencedColumnEnd); } int indexOfCatalogSep = StringUtils .indexOfIgnoreCaseRespectQuotes(0, referencedTableName, ".", quote, true); if (indexOfCatalogSep != -1) { referencedCatalogName = referencedTableName .substring(0, indexOfCatalogSep); referencedTableName = referencedTableName .substring(indexOfCatalogSep + 1); } } } } if (!firstTime) { commentBuf.append("; "); } else { firstTime = false; } if (constraintName != null) { commentBuf.append(constraintName); } else { commentBuf.append("not_available"); } commentBuf.append("("); commentBuf.append(localColumnName); commentBuf.append(") REFER "); commentBuf.append(referencedCatalogName); commentBuf.append("/"); commentBuf.append(referencedTableName); commentBuf.append("("); commentBuf.append(referencedColumnName); commentBuf.append(")"); int lastParenIndex = line.lastIndexOf(")"); if (lastParenIndex != (line.length() - 1)) { String cascadeOptions = cascadeOptions = line .substring(lastParenIndex + 1); commentBuf.append(" "); commentBuf.append(cascadeOptions); } } } row[2] = s2b(commentBuf.toString()); rows.add(row); return rows; } /** * Creates a result set similar enough to 'SHOW TABLE STATUS' to allow the * same code to work on extracting the foreign key data * * @param connToUse * the database connection to use * @param metadata * the DatabaseMetaData instance calling this method * @param catalog * the database name to extract foreign key info for * @param tableName * the table to extract foreign key info for * @return A result set that has the structure of 'show table status' * @throws SQLException * if a database access error occurs. */ public ResultSet extractForeignKeyFromCreateTable(String catalog, String tableName) throws SQLException { ArrayList tableList = new ArrayList(); java.sql.ResultSet rs = null; java.sql.Statement stmt = null; if (tableName != null) { tableList.add(tableName); } else { try { rs = getTables(catalog, "", "%", new String[] { "TABLE" }); while (rs.next()) { tableList.add(rs.getString("TABLE_NAME")); } } finally { if (rs != null) { rs.close(); } rs = null; } } ArrayList rows = new ArrayList(); Field[] fields = new Field[3]; fields[0] = new Field("", "Name", Types.CHAR, Integer.MAX_VALUE); fields[1] = new Field("", "Type", Types.CHAR, 255); fields[2] = new Field("", "Comment", Types.CHAR, Integer.MAX_VALUE); int numTables = tableList.size(); stmt = this.conn.getMetadataSafeStatement(); String quoteChar = getIdentifierQuoteString(); if (quoteChar == null) { quoteChar = "`"; } try { for (int i = 0; i < numTables; i++) { String tableToExtract = (String) tableList.get(i); String query = new StringBuffer("SHOW CREATE TABLE ").append( quoteChar).append(catalog).append(quoteChar) .append(".").append(quoteChar).append(tableToExtract) .append(quoteChar).toString(); try { rs = stmt.executeQuery(query); } catch (SQLException sqlEx) { // Table might've disappeared on us, not really an error String sqlState = sqlEx.getSQLState(); if (!"42S02".equals(sqlState) && sqlEx.getErrorCode() != MysqlErrorNumbers.ER_NO_SUCH_TABLE) { throw sqlEx; } continue; } while (rs.next()) { extractForeignKeyForTable(rows, rs, catalog); } } } finally { if (rs != null) { rs.close(); } rs = null; if (stmt != null) { stmt.close(); } stmt = null; } return buildResultSet(fields, rows); } /** * Finds the end of the RETURNS clause for SQL Functions by using any of the * keywords allowed after the RETURNS clause, or a label. * * @param procedureDefn * the function body containing the definition of the function * @param quoteChar * the identifier quote string in use * @param positionOfReturnKeyword * the position of "RETRUNS" in the definition * @return the end of the returns clause * @throws SQLException * if a parse error occurs */ private int findEndOfReturnsClause(String procedureDefn, String quoteChar, int positionOfReturnKeyword) throws SQLException { /* * characteristic: LANGUAGE SQL | [NOT] DETERMINISTIC | { CONTAINS SQL | * NO SQL | READS SQL DATA | MODIFIES SQL DATA } | SQL SECURITY { * DEFINER | INVOKER } | COMMENT 'string' */ String[] tokens = new String[] { "LANGUAGE", "NOT", "DETERMINISTIC", "CONTAINS", "NO", "READ", "MODIFIES", "SQL", "COMMENT", "BEGIN", "RETURN" }; int startLookingAt = positionOfReturnKeyword + "RETURNS".length() + 1; for (int i = 0; i < tokens.length; i++) { int endOfReturn = StringUtils.indexOfIgnoreCaseRespectQuotes( startLookingAt, procedureDefn, tokens[i], quoteChar .charAt(0), !this.conn.isNoBackslashEscapesSet()); if (endOfReturn != -1) { return endOfReturn; } } // Label? int endOfReturn = StringUtils.indexOfIgnoreCaseRespectQuotes( startLookingAt, procedureDefn, ":", quoteChar.charAt(0), !this.conn.isNoBackslashEscapesSet()); if (endOfReturn != -1) { // seek back until whitespace for (int i = endOfReturn; i > 0; i--) { if (Character.isWhitespace(procedureDefn.charAt(i))) { return i; } } } // We can't parse it. throw SQLError.createSQLException( "Internal error when parsing callable statement metadata", SQLError.SQL_STATE_GENERAL_ERROR); } /** * @see DatabaseMetaData#getAttributes(String, String, String, String) */ public java.sql.ResultSet getAttributes(String arg0, String arg1, String arg2, String arg3) throws SQLException { Field[] fields = new Field[21]; fields[0] = new Field("", "TYPE_CAT", Types.CHAR, 32); fields[1] = new Field("", "TYPE_SCHEM", Types.CHAR, 32); fields[2] = new Field("", "TYPE_NAME", Types.CHAR, 32);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -