📄 databasemetadata.java
字号:
} 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
rowVal[16] = Integer.toString(ordPos++).getBytes();
// ORDINAL_POSITION
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 (java.sql.Connection) 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 java.sql.SQLException if a database access error occurs
*/
public java.sql.ResultSet getCrossReference(String primaryCatalog,
String primarySchema, String primaryTable, String foreignCatalog,
String foreignSchema, String foreignTable) throws java.sql.SQLException {
if (Driver.TRACE) {
Object[] args = {
primaryCatalog, primarySchema, primaryTable, foreignCatalog,
foreignSchema, foreignTable
};
Debug.methodCall(this, "getCrossReference", args);
}
if (primaryTable == null) {
throw new java.sql.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, 255);
fields[12] = new Field("", "PK_NAME", Types.CHAR, 0);
fields[13] = new Field("", "DEFERRABILITY", Types.INTEGER, 2);
if (this.conn.getIO().versionMeetsMinimum(3, 23, 0)) {
Statement stmt = null;
ResultSet fkresults = null;
try {
/*
* Get foreign key information for table
*/
if (this.conn.getIO().versionMeetsMinimum(3, 23, 50)) {
// we can use 'SHOW CREATE TABLE'
String database = this.database;
if (foreignCatalog != null) {
if (!foreignCatalog.equals("")) {
database = foreignCatalog;
}
}
fkresults = extractForeignKeyFromCreateTable(this.conn,
this, database, null);
} else {
String databasePart = "";
if (foreignCatalog != null) {
if (!foreignCatalog.equals("")) {
databasePart = " FROM " + foreignCatalog;
}
} else {
databasePart = " FROM " + this.database;
}
stmt = this.conn.createStatement();
if (stmt.getMaxRows() != 0) {
stmt.setMaxRows(0);
}
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 constraintName = keys.substring(0,
firstLeftParenIndex);
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;
} else {
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] = s2b(constraintName); // FK_NAME
tuple[12] = null; // PK_NAME
tuple[13] = Integer.toString(java.sql.DatabaseMetaData.importedKeyNotDeferrable)
.getBytes();
tuples.add(tuple);
keySeq++;
}
}
}
}
}
if (Driver.TRACE) {
StringBuffer rows = new StringBuffer();
rows.append("\n");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -