Skip to content

Commit be935e1

Browse files
committed
feat(jdbc): add support for DatabaseMetaData#getSearchStringEscape
Closes: #595
1 parent 8c5cd18 commit be935e1

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ public String getProcedureTerm() {
248248

249249
/** @see java.sql.DatabaseMetaData#getSearchStringEscape() */
250250
public String getSearchStringEscape() {
251-
return null;
251+
return "\\";
252252
}
253253

254254
/** @see java.sql.DatabaseMetaData#getIdentifierQuoteString() */
@@ -850,7 +850,7 @@ public ResultSet getColumns(String c, String s, String tblNamePattern, String co
850850
// create a Matrix Cursor for each of the tables
851851
// create a merge cursor from all the Matrix Cursors
852852
// and return the columname and type from:
853-
// "PRAGMA table_info(tablename)"
853+
// "PRAGMA table_xinfo(tablename)"
854854
// which returns data like this:
855855
// sqlite> PRAGMA lastyear.table_info(gross_sales);
856856
// cid|name|type|notnull|dflt_value|pk
@@ -1102,7 +1102,9 @@ public ResultSet getColumns(String c, String s, String tblNamePattern, String co
11021102
if (colNamePattern != null) {
11031103
sql.append(" where upper(cn) like upper('")
11041104
.append(escape(colNamePattern))
1105-
.append("')");
1105+
.append("') ESCAPE '")
1106+
.append(getSearchStringEscape())
1107+
.append("'");
11061108
}
11071109
}
11081110
}
@@ -1714,7 +1716,10 @@ public synchronized ResultSet getTables(
17141716
sql.append(" )").append("\n");
17151717
sql.append(" WHERE TABLE_NAME LIKE '")
17161718
.append(tblNamePattern)
1717-
.append("' AND TABLE_TYPE IN (");
1719+
.append("' ESCAPE '")
1720+
.append(getSearchStringEscape())
1721+
.append("'")
1722+
.append(" AND TABLE_TYPE IN (");
17181723

17191724
if (types == null || types.length == 0) {
17201725
sql.append("'TABLE','VIEW'");

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,27 @@ public void getTables() throws SQLException {
8484
rs.close();
8585
}
8686

87+
@Test
88+
public void getTablesWithEscape() throws SQLException {
89+
stat.executeUpdate("create table 'table%with%wildcards'(c1 integer)");
90+
stat.executeUpdate("create table 'table_with_wildcards'(c2 integer)");
91+
stat.executeUpdate("create table 'tableXwithXwildcards'(c3 integer)");
92+
93+
String esc = meta.getSearchStringEscape();
94+
try (ResultSet rs =
95+
meta.getTables(null, null, "table_with_wildcards".replace("_", esc + "_"), null)) {
96+
assertThat(rs.next()).isTrue();
97+
assertThat(rs.getString("TABLE_NAME")).isEqualTo("table_with_wildcards");
98+
assertThat(rs.next()).isFalse();
99+
}
100+
try (ResultSet rs =
101+
meta.getTables(null, null, "table%with%wildcards".replace("%", esc + "%"), null)) {
102+
assertThat(rs.next()).isTrue();
103+
assertThat(rs.getString("TABLE_NAME")).isEqualTo("table%with%wildcards");
104+
assertThat(rs.next()).isFalse();
105+
}
106+
}
107+
87108
@Test
88109
public void getTableTypes() throws SQLException {
89110
ResultSet rs = meta.getTableTypes();
@@ -271,6 +292,25 @@ public void getColumnsIncludingGenerated() throws SQLException {
271292
assertThat(rs.next()).isFalse();
272293
}
273294

295+
@Test
296+
public void getColumnsWithEscape() throws SQLException {
297+
stat.executeUpdate("create table wildcard(col1 integer, co_1 integer, 'co%1' integer)");
298+
299+
String esc = meta.getSearchStringEscape();
300+
try (ResultSet rs =
301+
meta.getColumns(null, null, "wildcard", "co_1".replace("_", esc + "_"))) {
302+
assertThat(rs.next()).isTrue();
303+
assertThat(rs.getString("COLUMN_NAME")).isEqualTo("co_1");
304+
assertThat(rs.next()).isFalse();
305+
}
306+
try (ResultSet rs =
307+
meta.getColumns(null, null, "wildcard", "co%1".replace("%", esc + "%"))) {
308+
assertThat(rs.next()).isTrue();
309+
assertThat(rs.getString("COLUMN_NAME")).isEqualTo("co%1");
310+
assertThat(rs.next()).isFalse();
311+
}
312+
}
313+
274314
@Test
275315
public void numberOfgetImportedKeysCols() throws SQLException {
276316

0 commit comments

Comments
 (0)