Skip to content

Commit

Permalink
Merge pull request #89 from lhofhansl/reflect_escape
Browse files Browse the repository at this point in the history
Allow escaping in schema reflection - with test
  • Loading branch information
Mause authored Oct 1, 2024
2 parents 2876767 + 13b887d commit f6d37d3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/main/java/org/duckdb/DuckDBDatabaseMetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public String getTimeDateFunctions() throws SQLException {

@Override
public String getSearchStringEscape() throws SQLException {
return null;
return "\\";
}

@Override
Expand Down Expand Up @@ -719,7 +719,7 @@ public ResultSet getSchemas(String catalog, String schemaPattern) throws SQLExce
if (schemaPattern.isEmpty()) {
sb.append("schema_name IS NULL");
} else {
sb.append("schema_name LIKE ?");
sb.append("schema_name LIKE ? ESCAPE '\\'");
}
sb.append(lineSeparator());
}
Expand Down Expand Up @@ -784,7 +784,7 @@ public ResultSet getTables(String catalog, String schemaPattern, String tableNam
// non-standard behavior.
tableNamePattern = "%";
}
str.append("WHERE table_name LIKE ?").append(lineSeparator());
str.append("WHERE table_name LIKE ? ESCAPE '\\'").append(lineSeparator());

// catalog - a catalog name; must match the catalog name as it is stored in
// the database;
Expand All @@ -811,7 +811,7 @@ public ResultSet getTables(String catalog, String schemaPattern, String tableNam
if (schemaPattern.isEmpty()) {
str.append("IS NULL").append(lineSeparator());
} else {
str.append("LIKE ?").append(lineSeparator());
str.append("LIKE ? ESCAPE '\\'").append(lineSeparator());
hasSchemaParam = true;
}
}
Expand Down Expand Up @@ -896,10 +896,10 @@ public ResultSet getColumns(String catalogPattern, String schemaPattern, String
+ "'' AS 'IS_AUTOINCREMENT', "
+ "'' AS 'IS_GENERATEDCOLUMN' "
+ "FROM information_schema.columns c "
+ "WHERE table_catalog LIKE ? AND "
+ "table_schema LIKE ? AND "
+ "table_name LIKE ? AND "
+ "column_name LIKE ? "
+ "WHERE table_catalog LIKE ? ESCAPE '\\' AND "
+ "table_schema LIKE ? ESCAPE '\\' AND "
+ "table_name LIKE ? ESCAPE '\\' AND "
+ "column_name LIKE ? ESCAPE '\\' "
+ "ORDER BY \"TABLE_CAT\",\"TABLE_SCHEM\", \"TABLE_NAME\", \"ORDINAL_POSITION\"");
ps.setString(1, catalogPattern);
ps.setString(2, schemaPattern);
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/org/duckdb/TestDuckDBJDBC.java
Original file line number Diff line number Diff line change
Expand Up @@ -1746,6 +1746,29 @@ public static void test_decimal() throws Exception {
stmt.close();
conn.close();
}
public static void test_wildcard_reflection() throws Exception {
Connection conn = DriverManager.getConnection(JDBC_URL);
Statement stmt = conn.createStatement();
stmt.execute("CREATE TABLE _a (_i INTEGER, xi INTEGER)");
stmt.execute("CREATE TABLE xa (i INTEGER)");

DatabaseMetaData md = conn.getMetaData();
ResultSet rs;
rs = md.getTables(null, null, "\\_a", null);
assertTrue(rs.next());
assertEquals(rs.getString("TABLE_NAME"), "_a");
assertFalse(rs.next());
rs.close();

rs = md.getColumns(null, DuckDBConnection.DEFAULT_SCHEMA, "\\_a", "\\_i");
assertTrue(rs.next());
assertEquals(rs.getString("TABLE_NAME"), "_a");
assertEquals(rs.getString("COLUMN_NAME"), "_i");
assertFalse(rs.next());

rs.close();
conn.close();
}

public static void test_schema_reflection() throws Exception {
Connection conn = DriverManager.getConnection(JDBC_URL);
Expand Down

0 comments on commit f6d37d3

Please sign in to comment.