📄 databasemetadata.java
字号:
} else { databasePart = " FROM " + this.quotedId + this.database + this.quotedId; } ArrayList tableNameList = new ArrayList(); int tablenameLength = 0; if (tableName == null) { // Select from all tables java.sql.ResultSet tables = null; try { tables = getTables(catalog, schemaPattern, "%", new String[0]); while (tables.next()) { String tableNameFromList = tables.getString("TABLE_NAME"); tableNameList.add(tableNameFromList); if (tableNameFromList.length() > tablenameLength) { tablenameLength = tableNameFromList.length(); } } } finally { if (tables != null) { try { tables.close(); } catch (Exception sqlEx) { AssertionFailedException.shouldNotHappen(sqlEx); } tables = null; } } } else { java.sql.ResultSet tables = null; try { tables = getTables(catalog, schemaPattern, tableName, new String[0]); while (tables.next()) { String tableNameFromList = tables.getString("TABLE_NAME"); tableNameList.add(tableNameFromList); if (tableNameFromList.length() > tablenameLength) { tablenameLength = tableNameFromList.length(); } } } finally { if (tables != null) { try { tables.close(); } catch (SQLException sqlEx) { AssertionFailedException.shouldNotHappen(sqlEx); } tables = null; } } } int catalogLength = 0; if (catalog != null) { catalogLength = catalog.length(); } else { catalog = ""; catalogLength = 0; } java.util.Iterator tableNames = tableNameList.iterator(); Field[] fields = new Field[18]; fields[0] = new Field("", "TABLE_CAT", Types.CHAR, catalogLength); fields[1] = new Field("", "TABLE_SCHEM", Types.CHAR, 0); fields[2] = new Field("", "TABLE_NAME", Types.CHAR, tablenameLength); fields[3] = new Field("", "COLUMN_NAME", Types.CHAR, 32); fields[4] = new Field("", "DATA_TYPE", Types.SMALLINT, 5); fields[5] = new Field("", "TYPE_NAME", Types.CHAR, 16); fields[6] = new Field("", "COLUMN_SIZE", Types.INTEGER, Integer.toString(Integer.MAX_VALUE).length()); fields[7] = new Field("", "BUFFER_LENGTH", Types.INTEGER, 10); fields[8] = new Field("", "DECIMAL_DIGITS", Types.INTEGER, 10); fields[9] = new Field("", "NUM_PREC_RADIX", Types.INTEGER, 10); fields[10] = new Field("", "NULLABLE", Types.INTEGER, 10); fields[11] = new Field("", "REMARKS", Types.CHAR, 0); fields[12] = new Field("", "COLUMN_DEF", Types.CHAR, 0); fields[13] = new Field("", "SQL_DATA_TYPE", Types.INTEGER, 10); fields[14] = new Field("", "SQL_DATETIME_SUB", Types.INTEGER, 10); fields[15] = new Field("", "CHAR_OCTET_LENGTH", Types.INTEGER, Integer.toString(Integer.MAX_VALUE).length()); fields[16] = new Field("", "ORDINAL_POSITION", Types.INTEGER, 10); fields[17] = new Field("", "IS_NULLABLE", Types.CHAR, 3); ArrayList tuples = new ArrayList(); while (tableNames.hasNext()) { String tableNamePattern = (String) tableNames.next(); Statement stmt = null; ResultSet results = null; try { stmt = this.conn.createStatement(); stmt.setEscapeProcessing(false); StringBuffer queryBuf = new StringBuffer("SHOW "); if (this.conn.versionMeetsMinimum(4, 1, 0)) { queryBuf.append("FULL "); } queryBuf.append("COLUMNS FROM "); queryBuf.append(this.quotedId); queryBuf.append(tableNamePattern); queryBuf.append(this.quotedId); queryBuf.append(databasePart); queryBuf.append(" LIKE '"); queryBuf.append(columnNamePattern); queryBuf.append("'"); // Return correct ordinals if column name pattern is // not '%' // Currently, MySQL doesn't show enough data to do // this, so we do it the 'hard' way...Once _SYSTEM // tables are in, this should be much easier boolean fixUpOrdinalsRequired = false; Map ordinalFixUpMap = null; if (!columnNamePattern.equals("%")) { fixUpOrdinalsRequired = true; StringBuffer fullColumnQueryBuf = new StringBuffer("SHOW "); if (this.conn.versionMeetsMinimum(4, 1, 0)) { fullColumnQueryBuf.append("FULL "); } fullColumnQueryBuf.append("COLUMNS FROM "); fullColumnQueryBuf.append(this.quotedId); fullColumnQueryBuf.append(tableNamePattern); fullColumnQueryBuf.append(this.quotedId); fullColumnQueryBuf.append(databasePart); results = stmt.executeQuery(fullColumnQueryBuf.toString()); ordinalFixUpMap = new HashMap(); int fullOrdinalPos = 1; while (results.next()) { String fullOrdColName = results.getString("Field"); ordinalFixUpMap.put(fullOrdColName, new Integer(fullOrdinalPos++)); } } results = stmt.executeQuery(queryBuf.toString()); int ordPos = 1; while (results.next()) { byte[][] rowVal = new byte[18][]; rowVal[0] = s2b(catalog); // TABLE_CAT rowVal[1] = null; // TABLE_SCHEM (No schemas in MySQL) rowVal[2] = s2b(tableNamePattern); // TABLE_NAME rowVal[3] = results.getBytes("Field"); TypeDescriptor typeDesc = new TypeDescriptor(results.getString( "Type"), results.getString("Null")); rowVal[4] = Short.toString(typeDesc.dataType).getBytes(); // DATA_TYPE (jdbc) rowVal[5] = s2b(typeDesc.typeName); // TYPE_NAME (native) rowVal[6] = s2b(Integer.toString(typeDesc.columnSize)); rowVal[7] = s2b(Integer.toString(typeDesc.bufferLength)); rowVal[8] = s2b(Integer.toString(typeDesc.decimalDigits)); rowVal[9] = s2b(Integer.toString(typeDesc.numPrecRadix)); rowVal[10] = s2b(Integer.toString(typeDesc.nullability)); // // Doesn't always have this field, depending on version // // // REMARK column // try { if (this.conn.versionMeetsMinimum(4, 1, 0)) { rowVal[11] = results.getBytes("Comment"); } else { rowVal[11] = results.getBytes("Extra"); } } catch (Exception E) { rowVal[11] = new byte[0]; } // COLUMN_DEF rowVal[12] = results.getBytes("Default"); rowVal[13] = new byte[] { (byte) '0' }; // SQL_DATA_TYPE rowVal[14] = new byte[] { (byte) '0' }; // SQL_DATE_TIME_SUB rowVal[15] = rowVal[6]; // CHAR_OCTET_LENGTH // ORDINAL_POSITION if (!fixUpOrdinalsRequired) { rowVal[16] = Integer.toString(ordPos++).getBytes(); } else { String origColName = results.getString("Field"); Integer realOrdinal = (Integer) ordinalFixUpMap.get(origColName); if (realOrdinal != null) { rowVal[16] = realOrdinal.toString().getBytes(); } else { throw new SQLException("Can not find column in full column list to determine true ordinal position.", SQLError.SQL_STATE_GENERAL_ERROR); } } rowVal[17] = s2b(typeDesc.isNullable); tuples.add(rowVal); } } finally { if (results != null) { try { results.close(); } catch (Exception ex) { ; } results = null; } if (stmt != null) { try { stmt.close(); } catch (Exception ex) { ; } stmt = null; } } } java.sql.ResultSet results = buildResultSet(fields, tuples); return results; } /** * JDBC 2.0 Return the connection that produced this metadata object. * * @return the connection that produced this metadata object. * * @throws SQLException if a database error occurs */ public java.sql.Connection getConnection() throws SQLException { return this.conn; } /** * Get a description of the foreign key columns in the foreign key table * that reference the primary key columns of the primary key table * (describe how one table imports another's key.) This should normally * return a single foreign key/primary key pair (most tables only import a * foreign key from a table once.) They are ordered by FKTABLE_CAT, * FKTABLE_SCHEM, FKTABLE_NAME, and KEY_SEQ. * * <P> * Each foreign key column description has the following columns: * * <OL> * <li> * <B>PKTABLE_CAT</B> String => primary key table catalog (may be null) * </li> * <li> * <B>PKTABLE_SCHEM</B> String => primary key table schema (may be null) * </li> * <li> * <B>PKTABLE_NAME</B> String => primary key table name * </li> * <li> * <B>PKCOLUMN_NAME</B> String => primary key column name * </li> * <li> * <B>FKTABLE_CAT</B> String => foreign key table catalog (may be null) * being exported (may be null) * </li> * <li> * <B>FKTABLE_SCHEM</B> String => foreign key table schema (may be null) * being exported (may be null) * </li> * <li> * <B>FKTABLE_NAME</B> String => foreign key table name being exported * </li> * <li> * <B>FKCOLUMN_NAME</B> String => foreign key column name being exported * </li> * <li> * <B>KEY_SEQ</B> short => sequence number within foreign key * </li> * <li> * <B>UPDATE_RULE</B> short => What happens to foreign key when primary is * updated: * * <UL> * <li> * importedKeyCascade - change imported key to agree with primary key * update * </li> * <li> * importedKeyRestrict - do not allow update of primary key if it has been * imported * </li> * <li> * importedKeySetNull - change imported key to NULL if its primary key has * been updated * </li> * </ul> * * </li> * <li> * <B>DELETE_RULE</B> short => What happens to the foreign key when primary * is deleted. * * <UL> * <li> * importedKeyCascade - delete rows that import a deleted key * </li> * <li> * importedKeyRestrict - do not allow delete of primary key if it has been * imported * </li> * <li> * importedKeySetNull - change imported key to NULL if its primary key has * been deleted * </li> * </ul> * * </li> * <li> * <B>FK_NAME</B> String => foreign key identifier (may be null) * </li> * <li> * <B>PK_NAME</B> String => primary key identifier (may be null) * </li> * </ol> * </p> * * @param primaryCatalog a catalog name; "" retrieves those without a * catalog * @param primarySchema a schema name pattern; "" retrieves those without a * schema * @param primaryTable a table name * @param foreignCatalog a catalog name; "" retrieves those without a * catalog * @param foreignSchema a schema name pattern; "" retrieves those without a * schema * @param foreignTable a table name * * @return ResultSet each row is a foreign key column description * * @throws SQLException if a database access error occurs
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -