Skip to content

Commit b8eca2a

Browse files
dxbjavidgotson
authored andcommitted
fix(jdbc): escape resolved table name in getExportedKeys query
1 parent e21a3b4 commit b8eca2a

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1349,7 +1349,7 @@ public ResultSet getExportedKeys(String catalog, String schema, String table)
13491349
.append(" as PKTABLE_CAT, ")
13501350
.append(schema)
13511351
.append(" as PKTABLE_SCHEM, ")
1352-
.append(quote(target))
1352+
.append(quote(escape(target)))
13531353
.append(" as PKTABLE_NAME, ")
13541354
.append(hasImportedKey ? "pcn" : "''")
13551355
.append(" as PKCOLUMN_NAME, ")

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,21 @@ public void getExportedKeysCatalogSchemaWithQuote() throws SQLException {
512512
}
513513
}
514514

515+
@Test
516+
public void getExportedKeysTableNameWithQuote() throws SQLException {
517+
// the primary-key table name is read back from sqlite_schema and put into PKTABLE_NAME;
518+
// a name containing a single quote must stay a literal, not break out of the generated
519+
// query
520+
stat.executeUpdate("create table \"o'parent\" (id integer primary key)");
521+
stat.executeUpdate(
522+
"create table child1 (id integer primary key, pid integer, foreign key(pid) references \"o'parent\"(id))");
523+
try (ResultSet rs = meta.getExportedKeys(null, null, "o'parent")) {
524+
assertThat(rs.next()).isTrue();
525+
assertThat(rs.getString("PKTABLE_NAME")).isEqualTo("o'parent");
526+
assertThat(rs.getString("FKTABLE_NAME")).isEqualTo("child1");
527+
}
528+
}
529+
515530
@Test
516531
public void getCrossReferenceCatalogSchemaWithQuote() throws SQLException {
517532
stat.executeUpdate("create table parent (id integer primary key)");

0 commit comments

Comments
 (0)