Skip to content

Commit 8c3c2d7

Browse files
committed
fixed PreparedStatement tests
1 parent d7341a4 commit 8c3c2d7

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

jdbc-v2/src/main/java/com/clickhouse/jdbc/PreparedStatementImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,10 @@ public void setObject(int parameterIndex, Object x) throws SQLException {
262262
public boolean execute() throws SQLException {
263263
ensureOpen();
264264
if (parsedPreparedStatement.isHasResultSet()) {
265-
super.executeQueryImpl(buildSQL(), localSettings);
265+
currentResultSet = super.executeQueryImpl(buildSQL(), localSettings);
266266
return true;
267267
} else {
268-
super.executeUpdateImpl(buildSQL(), localSettings);
268+
currentUpdateCount = super.executeUpdateImpl(buildSQL(), localSettings);
269269
return false;
270270
}
271271
}

jdbc-v2/src/main/java/com/clickhouse/jdbc/StatementImpl.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public class StatementImpl implements Statement, JdbcV2Wrapper {
4343
private boolean closeOnCompletion;
4444
private boolean resultSetAutoClose;
4545
private int maxFieldSize;
46+
private boolean escapeProcessingEnabled;
4647

4748
// settings local to a statement
4849
protected QuerySettings localSettings;
@@ -56,6 +57,7 @@ public StatementImpl(ConnectionImpl connection) throws SQLException {
5657
this.localSettings = QuerySettings.merge(connection.getDefaultQuerySettings(), new QuerySettings());
5758
this.resultSets= new ConcurrentLinkedQueue<>();
5859
this.resultSetAutoClose = connection.getJdbcConfig().isSet(DriverProperties.RESULTSET_AUTO_CLOSE);
60+
this.escapeProcessingEnabled = true;
5961
}
6062

6163
protected void ensureOpen() throws SQLException {
@@ -65,24 +67,26 @@ protected void ensureOpen() throws SQLException {
6567
}
6668

6769

68-
protected static String parseJdbcEscapeSyntax(String sql) {
70+
private String parseJdbcEscapeSyntax(String sql) {
6971
LOG.trace("Original SQL: {}", sql);
70-
// Replace {d 'YYYY-MM-DD'} with corresponding SQL date format
71-
sql = sql.replaceAll("\\{d '([^']*)'\\}", "toDate('$1')");
72+
if (escapeProcessingEnabled) {
73+
// Replace {d 'YYYY-MM-DD'} with corresponding SQL date format
74+
sql = sql.replaceAll("\\{d '([^']*)'\\}", "toDate('$1')");
7275

73-
// Replace {ts 'YYYY-MM-DD HH:mm:ss'} with corresponding SQL timestamp format
74-
sql = sql.replaceAll("\\{ts '([^']*)'\\}", "timestamp('$1')");
76+
// Replace {ts 'YYYY-MM-DD HH:mm:ss'} with corresponding SQL timestamp format
77+
sql = sql.replaceAll("\\{ts '([^']*)'\\}", "timestamp('$1')");
7578

76-
// Replace function escape syntax {fn <function>} (e.g., {fn UCASE(name)})
77-
sql = sql.replaceAll("\\{fn ([^\\}]*)\\}", "$1");
79+
// Replace function escape syntax {fn <function>} (e.g., {fn UCASE(name)})
80+
sql = sql.replaceAll("\\{fn ([^\\}]*)\\}", "$1");
7881

79-
// Handle outer escape syntax
80-
//sql = sql.replaceAll("\\{escape '([^']*)'\\}", "'$1'");
82+
// Handle outer escape syntax
83+
//sql = sql.replaceAll("\\{escape '([^']*)'\\}", "'$1'");
8184

82-
// Clean new empty lines in sql
83-
sql = sql.replaceAll("(?m)^\\s*$\\n?", "");
84-
// Add more replacements as needed for other JDBC escape sequences
85-
LOG.trace("Parsed SQL: {}", sql);
85+
// Clean new empty lines in sql
86+
sql = sql.replaceAll("(?m)^\\s*$\\n?", "");
87+
// Add more replacements as needed for other JDBC escape sequences
88+
}
89+
LOG.trace("Escaped SQL: {}", sql);
8690
return sql;
8791
}
8892

@@ -253,7 +257,7 @@ public void setMaxRows(int max) throws SQLException {
253257
@Override
254258
public void setEscapeProcessing(boolean enable) throws SQLException {
255259
ensureOpen();
256-
//TODO: Should we support this?
260+
this.escapeProcessingEnabled = enable;
257261
}
258262

259263
@Override

jdbc-v2/src/test/java/com/clickhouse/jdbc/StatementTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import static org.testng.Assert.assertSame;
3434
import static org.testng.Assert.assertThrows;
3535
import static org.testng.Assert.assertTrue;
36+
import static org.testng.Assert.fail;
3637

3738

3839
@Test(groups = { "integration" })
@@ -331,6 +332,15 @@ public void testJdbcEscapeSyntax() throws Exception {
331332
assertFalse(rs.next());
332333
}
333334
}
335+
336+
try (Statement stmt = conn.createStatement()) {
337+
stmt.setEscapeProcessing(false);
338+
try (ResultSet rs = stmt.executeQuery("SELECT {d '2021-11-01'} AS D")) {
339+
fail("Expected to fail");
340+
} catch (SQLException e) {
341+
// ignore
342+
}
343+
}
334344
}
335345
}
336346

0 commit comments

Comments
 (0)