Skip to content

Commit

Permalink
[CONJ-765] DatabaseMetaData.getExportedKeys now not restricted to one…
Browse files Browse the repository at this point in the history
… mandatory table
  • Loading branch information
rusher committed Mar 18, 2020
1 parent d2e0430 commit ca6989d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 22 deletions.
24 changes: 3 additions & 21 deletions src/main/java/org/mariadb/jdbc/MariaDbDatabaseMetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -554,11 +554,11 @@ private String catalogCond(String columnName, String catalog) {
// table name)
private String patternCond(String columnName, String tableName) {
if (tableName == null) {
return "(1 = 1)";
return "";
}
String predicate =
(tableName.indexOf('%') == -1 && tableName.indexOf('_') == -1) ? "=" : "LIKE";
return "(" + columnName + " " + predicate + " '" + Utils.escapeString(tableName, true) + "')";
return " AND " + columnName + " " + predicate + " '" + Utils.escapeString(tableName, true) + "' ";
}

/**
Expand Down Expand Up @@ -598,9 +598,7 @@ public ResultSet getPrimaryKeys(String catalog, String schema, String table) thr
+ catalogCond("A.TABLE_SCHEMA", catalog)
+ " AND "
+ catalogCond("B.TABLE_SCHEMA", catalog)
+ " AND "
+ patternCond("A.TABLE_NAME", table)
+ " AND "
+ patternCond("B.TABLE_NAME", table)
+ " AND A.TABLE_SCHEMA = B.TABLE_SCHEMA AND A.TABLE_NAME = B.TABLE_NAME AND A.COLUMN_NAME = B.COLUMN_NAME "
+ " ORDER BY A.COLUMN_NAME";
Expand Down Expand Up @@ -659,7 +657,6 @@ public ResultSet getTables(
+ " FROM INFORMATION_SCHEMA.TABLES "
+ " WHERE "
+ catalogCond("TABLE_SCHEMA", catalog)
+ " AND "
+ patternCond("TABLE_NAME", tableNamePattern));

if (types != null && types.length > 0) {
Expand Down Expand Up @@ -818,9 +815,7 @@ public ResultSet getColumns(
+ " IF(EXTRA in ('VIRTUAL', 'PERSISTENT', 'VIRTUAL GENERATED', 'STORED GENERATED') ,'YES','NO') IS_GENERATEDCOLUMN "
+ " FROM INFORMATION_SCHEMA.COLUMNS WHERE "
+ catalogCond("TABLE_SCHEMA", catalog)
+ " AND "
+ patternCond("TABLE_NAME", tableNamePattern)
+ " AND "
+ patternCond("COLUMN_NAME", columnNamePattern)
+ " ORDER BY TABLE_CAT, TABLE_SCHEM, TABLE_NAME, ORDINAL_POSITION";

Expand Down Expand Up @@ -902,9 +897,6 @@ public ResultSet getColumns(
*/
public ResultSet getExportedKeys(String catalog, String schema, String table)
throws SQLException {
if (table == null) {
throw new SQLException("'table' parameter in getExportedKeys cannot be null");
}
String sql =
"SELECT KCU.REFERENCED_TABLE_SCHEMA PKTABLE_CAT, NULL PKTABLE_SCHEM, KCU.REFERENCED_TABLE_NAME PKTABLE_NAME,"
+ " KCU.REFERENCED_COLUMN_NAME PKCOLUMN_NAME, KCU.TABLE_SCHEMA FKTABLE_CAT, NULL FKTABLE_SCHEM, "
Expand Down Expand Up @@ -933,9 +925,7 @@ public ResultSet getExportedKeys(String catalog, String schema, String table)
+ " AND KCU.CONSTRAINT_NAME = RC.CONSTRAINT_NAME"
+ " WHERE "
+ catalogCond("KCU.REFERENCED_TABLE_SCHEMA", catalog)
+ " AND "
+ " KCU.REFERENCED_TABLE_NAME = "
+ escapeQuote(table)
+ patternCond("KCU.REFERENCED_TABLE_NAME", table)
+ " ORDER BY FKTABLE_CAT, FKTABLE_SCHEM, FKTABLE_NAME, KEY_SEQ";

return executeQuery(sql);
Expand Down Expand Up @@ -1959,7 +1949,6 @@ public ResultSet getProcedures(String catalog, String schemaPattern, String proc
+ " FROM INFORMATION_SCHEMA.ROUTINES "
+ " WHERE "
+ catalogCond("ROUTINE_SCHEMA", catalog)
+ " AND "
+ patternCond("ROUTINE_NAME", procedureNamePattern)
+ "/* AND ROUTINE_TYPE='PROCEDURE' */";
return executeQuery(sql);
Expand Down Expand Up @@ -2144,9 +2133,7 @@ public ResultSet getProcedureColumns(
+ " FROM INFORMATION_SCHEMA.PARAMETERS "
+ " WHERE "
+ catalogCond("SPECIFIC_SCHEMA", catalog)
+ " AND "
+ patternCond("SPECIFIC_NAME", procedureNamePattern)
+ " AND "
+ patternCond("PARAMETER_NAME", columnNamePattern)
+ " /* AND ROUTINE_TYPE='PROCEDURE' */ "
+ " ORDER BY SPECIFIC_SCHEMA, SPECIFIC_NAME, ORDINAL_POSITION";
Expand Down Expand Up @@ -2290,9 +2277,7 @@ public ResultSet getFunctionColumns(
+ " FROM INFORMATION_SCHEMA.PARAMETERS "
+ " WHERE "
+ catalogCond("SPECIFIC_SCHEMA", catalog)
+ " AND "
+ patternCond("SPECIFIC_NAME", functionNamePattern)
+ " AND "
+ patternCond("PARAMETER_NAME", columnNamePattern)
+ " AND ROUTINE_TYPE='FUNCTION'"
+ " ORDER BY FUNCTION_CAT, SPECIFIC_NAME, ORDINAL_POSITION";
Expand Down Expand Up @@ -2378,7 +2363,6 @@ public ResultSet getColumnPrivileges(
+ " AND "
+ " TABLE_NAME = "
+ escapeQuote(table)
+ " AND "
+ patternCond("COLUMN_NAME", columnNamePattern)
+ " ORDER BY COLUMN_NAME, PRIVILEGE_TYPE";

Expand Down Expand Up @@ -2428,7 +2412,6 @@ public ResultSet getTablePrivileges(String catalog, String schemaPattern, String
+ "GRANTEE, PRIVILEGE_TYPE PRIVILEGE, IS_GRANTABLE FROM INFORMATION_SCHEMA.TABLE_PRIVILEGES "
+ " WHERE "
+ catalogCond("TABLE_SCHEMA", catalog)
+ " AND "
+ patternCond("TABLE_NAME", tableNamePattern)
+ "ORDER BY TABLE_SCHEMA, TABLE_NAME, PRIVILEGE_TYPE ";

Expand Down Expand Up @@ -3896,7 +3879,6 @@ public ResultSet getFunctions(String catalog, String schemaPattern, String funct
+ " FROM INFORMATION_SCHEMA.ROUTINES "
+ " WHERE "
+ catalogCond("ROUTINE_SCHEMA", catalog)
+ " AND "
+ patternCond("ROUTINE_NAME", functionNamePattern)
+ " AND ROUTINE_TYPE='FUNCTION'";

Expand Down
39 changes: 38 additions & 1 deletion src/test/java/org/mariadb/jdbc/DatabaseMetadataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,22 @@ public void exportedKeysTest() throws SQLException {
stmt.execute("drop table if exists fore_key0");
stmt.execute("drop table if exists fore_key1");
stmt.execute("drop table if exists prim_key");
stmt.execute("drop table if exists fore_key3");
stmt.execute("drop table if exists prim2_key");

stmt.execute(
"create table prim_key (id int not null primary key, " + "val varchar(20)) engine=innodb");
"create table prim_key (id int not null primary key, val varchar(20)) engine=innodb");
stmt.execute(
"create table prim2_key (id int not null primary key, val varchar(20)) engine=innodb");
stmt.execute(
"create table fore_key0 (id int not null primary key, "
+ "id_ref0 int, foreign key (id_ref0) references prim_key(id)) engine=innodb");
stmt.execute(
"create table fore_key1 (id int not null primary key, "
+ "id_ref1 int, foreign key (id_ref1) references prim_key(id) on update cascade) engine=innodb");
stmt.execute(
"create table fore_key3 (id int not null primary key, "
+ "id_ref0 int, foreign key (id_ref0) references prim2_key(id)) engine=innodb");

DatabaseMetaData dbmd = sharedConnection.getMetaData();
ResultSet rs = dbmd.getExportedKeys("testj", null, "prim_key");
Expand All @@ -319,6 +326,36 @@ public void exportedKeysTest() throws SQLException {
counter++;
}
assertEquals(2, counter);

rs = dbmd.getExportedKeys("testj", null, "prim_k%");
counter = 0;
while (rs.next()) {
assertEquals("id", rs.getString("pkcolumn_name"));
assertEquals("fore_key" + counter, rs.getString("fktable_name"));
assertEquals("id_ref" + counter, rs.getString("fkcolumn_name"));
assertEquals("PRIMARY", rs.getString("PK_NAME"));
counter++;
}
assertEquals(2, counter);


rs = dbmd.getExportedKeys("testj", null, null);
counter = 0;
int totalCounter = 0;
while (rs.next()) {
if ("prim_key".equals(rs.getString("pktable_name"))) {
assertEquals("id", rs.getString("pkcolumn_name"));
assertEquals("fore_key" + counter, rs.getString("fktable_name"));
assertEquals("id_ref" + counter, rs.getString("fkcolumn_name"));
assertEquals("PRIMARY", rs.getString("PK_NAME"));
counter++;
}
totalCounter++;
}
assertEquals(2, counter);
assertTrue(totalCounter > 2);


stmt.execute("drop table if exists fore_key0");
stmt.execute("drop table if exists fore_key1");
stmt.execute("drop table if exists prim_key");
Expand Down

0 comments on commit ca6989d

Please sign in to comment.