📄 databasemetadata.java
字号:
*/ public java.sql.ResultSet getCrossReference(String primaryCatalog, String primarySchema, String primaryTable, String foreignCatalog, String foreignSchema, String foreignTable) throws SQLException { if (primaryTable == null) { throw new SQLException("Table not specified.", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } Field[] fields = new Field[14]; fields[0] = new Field("", "PKTABLE_CAT", Types.CHAR, 255); fields[1] = new Field("", "PKTABLE_SCHEM", Types.CHAR, 0); fields[2] = new Field("", "PKTABLE_NAME", Types.CHAR, 255); fields[3] = new Field("", "PKCOLUMN_NAME", Types.CHAR, 32); fields[4] = new Field("", "FKTABLE_CAT", Types.CHAR, 255); fields[5] = new Field("", "FKTABLE_SCHEM", Types.CHAR, 0); fields[6] = new Field("", "FKTABLE_NAME", Types.CHAR, 255); fields[7] = new Field("", "FKCOLUMN_NAME", Types.CHAR, 32); fields[8] = new Field("", "KEY_SEQ", Types.SMALLINT, 2); fields[9] = new Field("", "UPDATE_RULE", Types.SMALLINT, 2); fields[10] = new Field("", "DELETE_RULE", Types.SMALLINT, 2); fields[11] = new Field("", "FK_NAME", Types.CHAR, 0); fields[12] = new Field("", "PK_NAME", Types.CHAR, 0); fields[13] = new Field("", "DEFERRABILITY", Types.INTEGER, 2); if (this.conn.versionMeetsMinimum(3, 23, 0)) { Statement stmt = null; ResultSet fkresults = null; try { stmt = this.conn.createStatement(); stmt.setEscapeProcessing(false); /* * Get foreign key information for table */ if (this.conn.versionMeetsMinimum(3, 23, 50)) { // we can use 'SHOW CREATE TABLE' String db = this.database; if (foreignCatalog != null) { if (!foreignCatalog.equals("")) { db = foreignCatalog; } } fkresults = extractForeignKeyFromCreateTable(this.conn, this, db, null); } else { String databasePart = ""; if (foreignCatalog != null) { if (!foreignCatalog.equals("")) { databasePart = " FROM " + foreignCatalog; } } else { databasePart = " FROM " + this.database; } fkresults = stmt.executeQuery("show table status " + databasePart); } String foreignTableWithCase = getTableNameWithCase(foreignTable); String primaryTableWithCase = getTableNameWithCase(primaryTable); /* * Parse imported foreign key information */ ArrayList tuples = new ArrayList(); String dummy; while (fkresults.next()) { String tableType = fkresults.getString("Type"); if ((tableType != null) && (tableType.equalsIgnoreCase("innodb") || tableType.equalsIgnoreCase(SUPPORTS_FK))) { String comment = fkresults.getString("Comment").trim(); if (comment != null) { StringTokenizer commentTokens = new StringTokenizer(comment, ";", false); if (commentTokens.hasMoreTokens()) { dummy = commentTokens.nextToken(); // Skip InnoDB comment } while (commentTokens.hasMoreTokens()) { String keys = commentTokens.nextToken(); // simple-columned keys: (m) REFER airline/tt(a) // multi-columned keys : (m n) REFER airline/vv(a b) int firstLeftParenIndex = keys.indexOf('('); int firstRightParenIndex = keys.indexOf(')'); String referencingColumns = keys.substring(firstLeftParenIndex + 1, firstRightParenIndex); StringTokenizer referencingColumnsTokenizer = new StringTokenizer(referencingColumns, ", "); int secondLeftParenIndex = keys.indexOf('(', firstRightParenIndex + 1); int secondRightParenIndex = keys.indexOf(')', firstRightParenIndex + 1); String referencedColumns = keys.substring(secondLeftParenIndex + 1, secondRightParenIndex); StringTokenizer referencedColumnsTokenizer = new StringTokenizer(referencedColumns, ", "); int slashIndex = keys.indexOf('/'); String referencedTable = keys.substring(slashIndex + 1, secondLeftParenIndex); int keySeq = 0; while (referencingColumnsTokenizer.hasMoreTokens()) { String referencingColumn = referencingColumnsTokenizer.nextToken(); // one tuple for each table between parenthesis byte[][] tuple = new byte[14][]; tuple[4] = ((foreignCatalog == null) ? null : s2b(foreignCatalog)); tuple[5] = ((foreignSchema == null) ? null : s2b(foreignSchema)); dummy = fkresults.getString("Name"); // FKTABLE_NAME if (dummy.compareTo(foreignTableWithCase) != 0) { continue; } tuple[6] = s2b(dummy); tuple[7] = s2b(referencingColumn); // FKCOLUMN_NAME tuple[0] = ((primaryCatalog == null) ? null : s2b(primaryCatalog)); tuple[1] = ((primarySchema == null) ? null : s2b(primarySchema)); // Skip foreign key if it doesn't refer to the right table if (referencedTable.compareTo( primaryTableWithCase) != 0) { continue; } tuple[2] = s2b(referencedTable); // PKTABLE_NAME tuple[3] = s2b(referencedColumnsTokenizer.nextToken()); // PKCOLUMN_NAME tuple[8] = Integer.toString(keySeq) .getBytes(); // KEY_SEQ int[] actions = getForeignKeyActions(keys); tuple[9] = Integer.toString(actions[1]) .getBytes(); tuple[10] = Integer.toString(actions[0]) .getBytes(); tuple[11] = null; // FK_NAME tuple[12] = null; // PK_NAME tuple[13] = Integer.toString(java.sql.DatabaseMetaData.importedKeyNotDeferrable) .getBytes(); tuples.add(tuple); keySeq++; } } } } } return buildResultSet(fields, tuples); } finally { if (fkresults != null) { try { fkresults.close(); } catch (Exception sqlEx) { AssertionFailedException.shouldNotHappen(sqlEx); } fkresults = null; } if (stmt != null) { try { stmt.close(); } catch (Exception ex) { ; } stmt = null; } } } return buildResultSet(fields, new ArrayList()); } /** * @see DatabaseMetaData#getDatabaseMajorVersion() */ public int getDatabaseMajorVersion() throws SQLException { return this.conn.getServerMajorVersion(); } /** * @see DatabaseMetaData#getDatabaseMinorVersion() */ public int getDatabaseMinorVersion() throws SQLException { return this.conn.getServerMinorVersion(); } /** * What's the name of this database product? * * @return database product name * * @throws SQLException DOCUMENT ME! */ public String getDatabaseProductName() throws SQLException { return "MySQL"; } /** * What's the version of this database product? * * @return database version * * @throws SQLException DOCUMENT ME! */ public String getDatabaseProductVersion() throws SQLException { return this.conn.getServerVersion(); } //---------------------------------------------------------------------- /** * What's the database's default transaction isolation level? The values * are defined in java.sql.Connection. * * @return the default isolation level * * @throws SQLException if a database access error occurs * * @see Connection */ public int getDefaultTransactionIsolation() throws SQLException { if (this.conn.supportsIsolationLevel()) { return java.sql.Connection.TRANSACTION_READ_COMMITTED; } return java.sql.Connection.TRANSACTION_NONE; } /** * What's this JDBC driver's major version number? * * @return JDBC driver major version */ public int getDriverMajorVersion() { return NonRegisteringDriver.getMajorVersionInternal(); } /** * What's this JDBC driver's minor version number? * * @return JDBC driver minor version number */ public int getDriverMinorVersion() { return NonRegisteringDriver.getMinorVersionInternal(); } /** * What's the name of this JDBC driver? * * @return JDBC driver name * * @throws SQLException DOCUMENT ME! */ public String getDriverName() throws SQLException { return "MySQL-AB JDBC Driver"; } /** * What's the version of this JDBC driver? * * @return JDBC driver version * * @throws java.sql.SQLException DOCUMENT ME! */ public String getDriverVersion() throws java.sql.SQLException { return "mysql-connector-java-3.1.7 ( $Date: 2005/01/25 19:11:41 $, $Revision: 1.27.4.54 $ )"; } /** * Get a description of a foreign key columns that reference a table's * primary key columns (the foreign keys exported by a table). 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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -