📄 databasemetadata.java
字号:
*/ public boolean allTablesAreSelectable() throws SQLException { return false; } private java.sql.ResultSet buildResultSet(com.mysql.jdbc.Field[] fields, java.util.ArrayList rows) throws SQLException { int fieldsLength = fields.length; for (int i = 0; i < fieldsLength; i++) { fields[i].setConnection(this.conn); } return new com.mysql.jdbc.ResultSet(this.conn.getCatalog(), fields, new RowDataStatic(rows), this.conn, null); } private void convertToJdbcFunctionList(String catalog, ResultSet proceduresRs, boolean needsClientFiltering, String db, Map procedureRowsOrderedByName, int nameIndex) throws SQLException { while (proceduresRs.next()) { boolean shouldAdd = true; if (needsClientFiltering) { shouldAdd = false; String procDb = proceduresRs.getString(1); if (db == null && procDb == null) { shouldAdd = true; } else if (db != null & db.equals(procDb)) { shouldAdd = true; } } if (shouldAdd) { String functionName = proceduresRs.getString(nameIndex); byte[][] rowData = new byte[8][]; rowData[0] = catalog == null ? null : s2b(catalog); rowData[1] = null; rowData[2] = s2b(functionName); rowData[3] = null; rowData[4] = null; rowData[5] = null; rowData[6] = null; rowData[7] = s2b(Integer.toString(procedureReturnsResult)); procedureRowsOrderedByName.put(functionName, rowData); } } } private void convertToJdbcProcedureList(boolean fromSelect, String catalog, ResultSet proceduresRs, boolean needsClientFiltering, String db, Map procedureRowsOrderedByName, int nameIndex) throws SQLException { while (proceduresRs.next()) { boolean shouldAdd = true; if (needsClientFiltering) { shouldAdd = false; String procDb = proceduresRs.getString(1); if (db == null && procDb == null) { shouldAdd = true; } else if (db != null & db.equals(procDb)) { shouldAdd = true; } } if (shouldAdd) { String procedureName = proceduresRs.getString(nameIndex); byte[][] rowData = new byte[8][]; rowData[0] = catalog == null ? null : s2b(catalog); rowData[1] = null; rowData[2] = s2b(procedureName); rowData[3] = null; rowData[4] = null; rowData[5] = null; rowData[6] = null; boolean isFunction = fromSelect ? "FUNCTION" .equalsIgnoreCase(proceduresRs.getString("type")) : false; rowData[7] = s2b(isFunction ? Integer .toString(procedureReturnsResult) : Integer .toString(procedureResultUnknown)); procedureRowsOrderedByName.put(procedureName, rowData); } } } private byte[][] convertTypeDescriptorToProcedureRow( byte[] procNameAsBytes, String paramName, boolean isOutParam, boolean isInParam, boolean isReturnParam, TypeDescriptor typeDesc) throws SQLException { byte[][] row = new byte[14][]; row[0] = null; // PROCEDURE_CAT row[1] = null; // PROCEDURE_SCHEM row[2] = procNameAsBytes; // PROCEDURE/NAME row[3] = s2b(paramName); // COLUMN_NAME // COLUMN_TYPE if (isInParam && isOutParam) { row[4] = s2b(String.valueOf(procedureColumnInOut)); } else if (isInParam) { row[4] = s2b(String.valueOf(procedureColumnIn)); } else if (isOutParam) { row[4] = s2b(String.valueOf(procedureColumnOut)); } else if (isReturnParam) { row[4] = s2b(String.valueOf(procedureColumnReturn)); } else { row[4] = s2b(String.valueOf(procedureColumnUnknown)); } row[5] = s2b(Short.toString(typeDesc.dataType)); // DATA_TYPE row[6] = s2b(typeDesc.typeName); // TYPE_NAME row[7] = s2b(Integer.toString(typeDesc.columnSize)); // PRECISION row[8] = s2b(Integer.toString(typeDesc.bufferLength)); // LENGTH row[9] = s2b(Integer.toString(typeDesc.decimalDigits)); // SCALE row[10] = s2b(Integer.toString(typeDesc.numPrecRadix)); // RADIX // Map 'column****' to 'procedure****' switch (typeDesc.nullability) { case columnNoNulls: row[11] = s2b(Integer.toString(procedureNoNulls)); // NULLABLE break; case columnNullable: row[11] = s2b(Integer.toString(procedureNullable)); // NULLABLE break; case columnNullableUnknown: row[11] = s2b(Integer.toString(procedureNullableUnknown)); // nullable break; default: throw new SQLException( "Internal error while parsing callable statement metadata (unknown nullability value fount)", SQLError.SQL_STATE_GENERAL_ERROR); } row[12] = null; return row; } /** * Does a data definition statement within a transaction force the * transaction to commit? * * @return true if so * @throws SQLException * DOCUMENT ME! */ public boolean dataDefinitionCausesTransactionCommit() throws SQLException { return true; } /** * Is a data definition statement within a transaction ignored? * * @return true if so * @throws SQLException * DOCUMENT ME! */ public boolean dataDefinitionIgnoredInTransactions() throws SQLException { return false; } /** * JDBC 2.0 Determine whether or not a visible row delete can be detected by * calling ResultSet.rowDeleted(). If deletesAreDetected() returns false, * then deleted rows are removed from the result set. * * @param type * set type, i.e. ResultSet.TYPE_XXX * @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; } /** * 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 new SQLException(); } 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(); rs = stmt.executeQuery(query); while (rs.next()) { extractForeignKeyForTable(rows, rs, catalog); } } } finally { if (rs != null) { rs.close(); } rs = null; if (stmt != null) { stmt.close(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -