Skip to content

Commit

Permalink
[CONJ-814] DatabaseMetadata result-set PK_NAME now contains value
Browse files Browse the repository at this point in the history
* getCrossReference PK_NAME was always null.
* getExportedKeys PK_NAME was always 'PRIMARY'.
* getImportedKeys PK_NAME was always null.

now return parent key name when available

getCrossReference now permit patterns for parentTable and foreignTable parameters like many meta methods allows.
  • Loading branch information
rusher committed Aug 24, 2020
1 parent ed706d5 commit 7363e25
Show file tree
Hide file tree
Showing 3 changed files with 254 additions and 126 deletions.
85 changes: 36 additions & 49 deletions src/main/java/org/mariadb/jdbc/MariaDbDatabaseMetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ private static ResultSet getImportedKeys(
ColumnType.VARCHAR, ColumnType.VARCHAR, ColumnType.NULL,
ColumnType.VARCHAR, ColumnType.VARCHAR, ColumnType.SMALLINT,
ColumnType.SMALLINT, ColumnType.SMALLINT, ColumnType.VARCHAR,
ColumnType.NULL, ColumnType.SMALLINT
ColumnType.VARCHAR, ColumnType.SMALLINT
};

String[] parts = tableDef.split("\n");
Expand Down Expand Up @@ -280,23 +280,23 @@ private static ResultSet getImportedKeys(
for (int i = 0; i < primaryKeyCols.size(); i++) {

String[] row = new String[columnNames.length];
row[0] = pkTable.schema;
row[0] = pkTable.schema; // PKTABLE_CAT
if (row[0] == null) {
row[0] = catalog;
}
row[1] = null;
row[2] = pkTable.name;
row[3] = primaryKeyCols.get(i).name;
row[4] = catalog;
row[5] = null;
row[6] = tableName;
row[7] = foreignKeyCols.get(i).name;
row[8] = Integer.toString(i + 1);
row[9] = Integer.toString(onUpdateReferenceAction);
row[10] = Integer.toString(onDeleteReferenceAction);
row[11] = constraintName.name;
row[12] = null;
row[13] = Integer.toString(DatabaseMetaData.importedKeyNotDeferrable);
row[1] = null; // PKTABLE_SCHEM
row[2] = pkTable.name; // PKTABLE_NAME
row[3] = primaryKeyCols.get(i).name; // PKCOLUMN_NAME
row[4] = catalog; // FKTABLE_CAT
row[5] = null; // FKTABLE_SCHEM
row[6] = tableName; // FKTABLE_NAME
row[7] = foreignKeyCols.get(i).name; // FKCOLUMN_NAME
row[8] = Integer.toString(i + 1); // KEY_SEQ
row[9] = Integer.toString(onUpdateReferenceAction); // UPDATE_RULE
row[10] = Integer.toString(onDeleteReferenceAction); // DELETE_RULE
row[11] = constraintName.name; // FK_NAME
row[12] = null; // PK_NAME - unlike using information_schema, cannot know constraint name
row[13] = Integer.toString(DatabaseMetaData.importedKeyNotDeferrable); // DEFERRABILITY
data.add(row);
}
}
Expand Down Expand Up @@ -390,34 +390,26 @@ private static ResultSet getImportedKeys(
public ResultSet getImportedKeys(String catalog, String schema, String table)
throws SQLException {

String database = catalog;
// We avoid using information schema queries by default, because this appears to be an expensive
// query (CONJ-41).
if (table == null) {
throw new SQLException("'table' parameter in getImportedKeys cannot be null");
}

if (database == null && connection.nullCatalogMeansCurrent) {
/* Treat null catalog as current */
return getImportedKeysUsingInformationSchema("", table);
}

if (database == null) {
return getImportedKeysUsingInformationSchema(null, table);
}

if (database.isEmpty()) {
database = connection.getCatalog();
if (database == null || database.isEmpty()) {
return getImportedKeysUsingInformationSchema(database, table);
if (catalog == null || catalog.isEmpty()) {
if (connection.nullCatalogMeansCurrent) {
/* Treat null catalog as current */
return getImportedKeysUsingInformationSchema("", table);
} else {
return getImportedKeysUsingInformationSchema(catalog, table);
}
}

try {
return getImportedKeysUsingShowCreateTable(database, table);
return getImportedKeysUsingShowCreateTable(catalog, table);
} catch (Exception e) {
// Likely, parsing failed, try out I_S query.
return getImportedKeysUsingInformationSchema(database, table);
return getImportedKeysUsingInformationSchema(catalog, table);
}
}

Expand Down Expand Up @@ -922,7 +914,7 @@ public ResultSet getExportedKeys(String catalog, String schema, String table)
+ " WHEN 'SET DEFAULT' THEN 4"
+ " END DELETE_RULE,"
+ " RC.CONSTRAINT_NAME FK_NAME,"
+ " 'PRIMARY' PK_NAME,"
+ " RC.UNIQUE_CONSTRAINT_NAME PK_NAME,"
+ importedKeyNotDeferrable
+ " DEFERRABILITY"
+ " FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU"
Expand All @@ -945,7 +937,7 @@ public ResultSet getExportedKeys(String catalog, String schema, String table)
* @return resultset
* @throws SQLException exception
*/
public ResultSet getImportedKeysUsingInformationSchema(String catalog, String table)
public ResultSet getImportedKeysUsingInformationSchema(final String catalog, String table)
throws SQLException {
if (table == null) {
throw new SQLException("'table' parameter in getImportedKeys cannot be null");
Expand All @@ -969,7 +961,7 @@ public ResultSet getImportedKeysUsingInformationSchema(String catalog, String ta
+ " WHEN 'SET DEFAULT' THEN 4"
+ " END DELETE_RULE,"
+ " RC.CONSTRAINT_NAME FK_NAME,"
+ " NULL PK_NAME,"
+ " RC.UNIQUE_CONSTRAINT_NAME PK_NAME,"
+ importedKeyNotDeferrable
+ " DEFERRABILITY"
+ " FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU"
Expand All @@ -994,7 +986,7 @@ public ResultSet getImportedKeysUsingInformationSchema(String catalog, String ta
* @return resultset
* @throws Exception exception
*/
public ResultSet getImportedKeysUsingShowCreateTable(String catalog, String table)
public ResultSet getImportedKeysUsingShowCreateTable(final String catalog, String table)
throws Exception {

if (catalog == null || catalog.isEmpty()) {
Expand Down Expand Up @@ -2541,15 +2533,14 @@ public ResultSet getVersionColumns(String catalog, String schema, String table)
* @param parentSchema a schema name; must match the schema name as it is stored in the database;
* "" retrieves those without a schema; <code>null</code> means drop schema name from the
* selection criteria
* @param parentTable the name of the table that exports the key; must match the table name as it
* is stored in the database
* @param parentTable the name of the table that exports the key; pattern, or null (means any table) value
* @param foreignCatalog a catalog name; must match the catalog name as it is stored in the
* database; "" retrieves those without a catalog; <code>null</code> means drop catalog name
* from the selection criteria
* @param foreignSchema a schema name; must match the schema name as it is stored in the database;
* "" retrieves those without a schema; <code>null</code> means drop schema name from the
* selection criteria
* @param foreignTable the name of the table that imports the key; must match the table name as it
* @param foreignTable the name of the table that imports the key; pattern, or null (means any table) value
* is stored in the database
* @return <code>ResultSet</code> - each row is a foreign key column description
* @throws SQLException if a database access error occurs
Expand Down Expand Up @@ -2583,23 +2574,19 @@ public ResultSet getCrossReference(
+ " WHEN 'SET DEFAULT' THEN 4"
+ " END DELETE_RULE,"
+ " RC.CONSTRAINT_NAME FK_NAME,"
+ " NULL PK_NAME,"
+ " RC.UNIQUE_CONSTRAINT_NAME PK_NAME,"
+ importedKeyNotDeferrable
+ " DEFERRABILITY"
+ " FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU"
+ " DEFERRABILITY "
+ "FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU"
+ " INNER JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS RC"
+ " ON KCU.CONSTRAINT_SCHEMA = RC.CONSTRAINT_SCHEMA"
+ " AND KCU.CONSTRAINT_NAME = RC.CONSTRAINT_NAME"
+ " WHERE "
+ " AND KCU.CONSTRAINT_NAME = RC.CONSTRAINT_NAME "
+ "WHERE "
+ catalogCond("KCU.REFERENCED_TABLE_SCHEMA", parentCatalog)
+ " AND "
+ catalogCond("KCU.TABLE_SCHEMA", foreignCatalog)
+ " AND "
+ " KCU.REFERENCED_TABLE_NAME = "
+ escapeQuote(parentTable)
+ " AND "
+ " KCU.TABLE_NAME = "
+ escapeQuote(foreignTable)
+ patternCond("KCU.REFERENCED_TABLE_NAME", parentTable)
+ patternCond("KCU.TABLE_NAME", foreignTable)
+ " ORDER BY FKTABLE_CAT, FKTABLE_SCHEM, FKTABLE_NAME, KEY_SEQ";

return executeQuery(sql);
Expand Down
Loading

0 comments on commit 7363e25

Please sign in to comment.