Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow escaping in schema reflection - with test #89

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading