Skip to content

Commit 1cf1f80

Browse files
author
Paultagoras
committed
Tweaking escape syntax
1 parent 67fb6d1 commit 1cf1f80

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,6 @@ private static String encodeObject(Object x) throws SQLException {
556556
}
557557

558558
private static String escapeString(String x) {
559-
return x.replace("'", "''");//Escape single quotes
559+
return x.replace("\\", "\\\\").replace("'", "\\'");//Escape single quotes
560560
}
561561
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,19 @@ public void testPrimitiveArrays() throws Exception {
239239
}
240240
}
241241
}
242+
243+
244+
@Test(groups = { "integration" })
245+
public void testEscapeStrings() throws Exception {
246+
try (Connection conn = getJdbcConnection()) {
247+
try (PreparedStatement stmt = conn.prepareStatement("SELECT FALSE OR ? = 'test'")) {
248+
stmt.setString(1, "test\\\\' OR 1 = 1 --");
249+
try (ResultSet rs = stmt.executeQuery()) {
250+
assertTrue(rs.next());
251+
assertEquals(rs.getString(1), "false");
252+
assertFalse(rs.next());
253+
}
254+
}
255+
}
256+
}
242257
}

0 commit comments

Comments
 (0)