Skip to content

Commit ff4b847

Browse files
authored
fix(jdbc): escape catalog and schema in foreign key metadata queries
1 parent 33eaa19 commit ff4b847

3 files changed

Lines changed: 53 additions & 13 deletions

File tree

src/main/java/org/sqlite/core/CoreDatabaseMetaData.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ protected String escape(final String val) {
171171
// TODO: this function is ugly, pass this work off to SQLite, then we
172172
// don't have to worry about Unicode 4, other characters needing
173173
// escaping, etc.
174+
if (val == null) {
175+
return null;
176+
}
174177
int len = val.length();
175178
StringBuilder buf = new StringBuilder(len);
176179
for (int i = 0; i < len; i++) {

src/main/java/org/sqlite/jdbc3/JDBC3DatabaseMetaData.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,18 +1157,18 @@ public ResultSet getCrossReference(
11571157

11581158
String query =
11591159
"select "
1160-
+ quote(pc)
1160+
+ quote(escape(pc))
11611161
+ " as PKTABLE_CAT, "
1162-
+ quote(ps)
1162+
+ quote(escape(ps))
11631163
+ " as PKTABLE_SCHEM, "
1164-
+ quote(pt)
1164+
+ quote(escape(pt))
11651165
+ " as PKTABLE_NAME, "
11661166
+ "'' as PKCOLUMN_NAME, "
1167-
+ quote(fc)
1167+
+ quote(escape(fc))
11681168
+ " as FKTABLE_CAT, "
1169-
+ quote(fs)
1169+
+ quote(escape(fs))
11701170
+ " as FKTABLE_SCHEM, "
1171-
+ quote(ft)
1171+
+ quote(escape(ft))
11721172
+ " as FKTABLE_NAME, "
11731173
+ "'' as FKCOLUMN_NAME, -1 as KEY_SEQ, 3 as UPDATE_RULE, 3 as DELETE_RULE, '' as FK_NAME, '' as PK_NAME, "
11741174
+ DatabaseMetaData.importedKeyInitiallyDeferred
@@ -1256,8 +1256,8 @@ public ResultSet getExportedKeys(String catalog, String schema, String table)
12561256
String[] pkColumns = pkFinder.getColumns();
12571257
Statement stat = conn.createStatement();
12581258

1259-
catalog = (catalog != null) ? quote(catalog) : null;
1260-
schema = (schema != null) ? quote(schema) : null;
1259+
catalog = (catalog != null) ? quote(escape(catalog)) : null;
1260+
schema = (schema != null) ? quote(escape(schema)) : null;
12611261

12621262
StringBuilder exportedKeysQuery = new StringBuilder(512);
12631263

@@ -1409,16 +1409,16 @@ public ResultSet getImportedKeys(String catalog, String schema, String table)
14091409
StringBuilder sql = new StringBuilder(700);
14101410

14111411
sql.append("select ")
1412-
.append(quote(catalog))
1412+
.append(quote(escape(catalog)))
14131413
.append(" as PKTABLE_CAT, ")
1414-
.append(quote(schema))
1414+
.append(quote(escape(schema)))
14151415
.append(" as PKTABLE_SCHEM, ")
14161416
.append("ptn as PKTABLE_NAME, pcn as PKCOLUMN_NAME, ")
1417-
.append(quote(catalog))
1417+
.append(quote(escape(catalog)))
14181418
.append(" as FKTABLE_CAT, ")
1419-
.append(quote(schema))
1419+
.append(quote(escape(schema)))
14201420
.append(" as FKTABLE_SCHEM, ")
1421-
.append(quote(table))
1421+
.append(quote(escape(table)))
14221422
.append(" as FKTABLE_NAME, ")
14231423
.append(
14241424
"fcn as FKCOLUMN_NAME, ks as KEY_SEQ, ur as UPDATE_RULE, dr as DELETE_RULE, fkn as FK_NAME, pkn as PK_NAME, ")

src/test/java/org/sqlite/DBMetaDataTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,43 @@ public void getTablesTypeWithQuote() throws SQLException {
486486
}
487487
}
488488

489+
@Test
490+
public void getImportedKeysCatalogSchemaWithQuote() throws SQLException {
491+
stat.executeUpdate("create table parent (id integer primary key)");
492+
stat.executeUpdate(
493+
"create table child1 (id integer primary key, pid integer, foreign key(pid) references parent(id))");
494+
// a catalog/schema containing a single quote must be carried through as a literal,
495+
// not break out of the surrounding string in the generated metadata query
496+
try (ResultSet rs = meta.getImportedKeys("ca'talog", "sch'ema", "child1")) {
497+
assertThat(rs.next()).isTrue();
498+
assertThat(rs.getString("PKTABLE_CAT")).isEqualTo("ca'talog");
499+
assertThat(rs.getString("PKTABLE_SCHEM")).isEqualTo("sch'ema");
500+
}
501+
}
502+
503+
@Test
504+
public void getExportedKeysCatalogSchemaWithQuote() throws SQLException {
505+
stat.executeUpdate("create table parent (id integer primary key)");
506+
stat.executeUpdate(
507+
"create table child1 (id integer primary key, pid integer, foreign key(pid) references parent(id))");
508+
try (ResultSet rs = meta.getExportedKeys("ca'talog", "sch'ema", "parent")) {
509+
assertThat(rs.next()).isTrue();
510+
assertThat(rs.getString("PKTABLE_CAT")).isEqualTo("ca'talog");
511+
assertThat(rs.getString("PKTABLE_SCHEM")).isEqualTo("sch'ema");
512+
}
513+
}
514+
515+
@Test
516+
public void getCrossReferenceCatalogSchemaWithQuote() throws SQLException {
517+
stat.executeUpdate("create table parent (id integer primary key)");
518+
stat.executeUpdate(
519+
"create table child1 (id integer primary key, pid integer, foreign key(pid) references parent(id))");
520+
try (ResultSet rs =
521+
meta.getCrossReference("ca'talog", "sch'ema", "parent", null, null, "child1")) {
522+
assertThat(rs.next()).isFalse();
523+
}
524+
}
525+
489526
@Test
490527
public void getColumnsTableNameWithQuote() throws SQLException {
491528
stat.executeUpdate("create table \"o'brien\" (id integer, name text)");

0 commit comments

Comments
 (0)