|
1 | 1 | package com.clickhouse.jdbc; |
2 | 2 |
|
| 3 | +import com.clickhouse.data.ClickHouseVersion; |
3 | 4 | import com.clickhouse.jdbc.internal.DriverProperties; |
4 | 5 | import org.apache.commons.lang3.RandomStringUtils; |
5 | 6 | import org.testng.Assert; |
|
9 | 10 |
|
10 | 11 | import java.sql.Array; |
11 | 12 | import java.sql.Connection; |
| 13 | +import java.sql.Date; |
12 | 14 | import java.sql.PreparedStatement; |
13 | 15 | import java.sql.ResultSet; |
14 | 16 | import java.sql.ResultSetMetaData; |
15 | 17 | import java.sql.SQLException; |
16 | 18 | import java.sql.Statement; |
17 | 19 | import java.sql.Timestamp; |
18 | 20 | import java.sql.Types; |
| 21 | +import java.time.LocalDate; |
19 | 22 | import java.time.LocalDateTime; |
20 | 23 | import java.util.ArrayList; |
21 | 24 | import java.util.Arrays; |
|
30 | 33 | import static org.testng.Assert.assertFalse; |
31 | 34 | import static org.testng.Assert.assertNotNull; |
32 | 35 | import static org.testng.Assert.assertNull; |
| 36 | +import static org.testng.Assert.assertThrows; |
33 | 37 | import static org.testng.Assert.assertTrue; |
34 | 38 |
|
35 | 39 | public class PreparedStatementTest extends JdbcIntegrationTest { |
@@ -825,4 +829,48 @@ public static Object[][] testReplaceQuestionMark_dataProvider() { |
825 | 829 | {"INSERT INTO \"t2?\" VALUES (?, ?, 'some_?', ?)", "INSERT INTO \"t2?\" VALUES (NULL, NULL, 'some_?', NULL)"} |
826 | 830 | }; |
827 | 831 | } |
| 832 | + |
| 833 | + @Test(groups = { "integration" }) |
| 834 | + public void testJdbcEscapeSyntax() throws Exception { |
| 835 | + if (ClickHouseVersion.of(getServerVersion()).check("(,23.8]")) { |
| 836 | + return; // there is no `timestamp` function TODO: fix in JDBC |
| 837 | + } |
| 838 | + try (Connection conn = getJdbcConnection()) { |
| 839 | + try (PreparedStatement stmt = conn.prepareStatement("SELECT {d '2021-11-01'} AS D, {ts '2021-08-01 12:34:56'} AS TS, " + |
| 840 | + "toInt32({fn ABS(-1)}) AS FNABS, {fn CONCAT('Hello', 'World')} AS FNCONCAT, {fn UCASE('hello')} AS FNUPPER, " + |
| 841 | + "{fn LCASE('HELLO')} AS FNLOWER, {fn LTRIM(' Hello ')} AS FNLTRIM, {fn RTRIM(' Hello ')} AS FNRTRIM, " + |
| 842 | + "toInt32({fn LENGTH('Hello')}) AS FNLENGTH, toInt32({fn POSITION('Hello', 'l')}) AS FNPOSITION, toInt32({fn MOD(10, 3)}) AS FNMOD, " + |
| 843 | + "{fn SQRT(9)} AS FNSQRT, {fn SUBSTRING('Hello', 3, 2)} AS FNSUBSTRING")) { |
| 844 | + try (ResultSet rs = stmt.executeQuery()) { |
| 845 | + assertTrue(rs.next()); |
| 846 | + assertEquals(rs.getDate(1), Date.valueOf(LocalDate.of(2021, 11, 1))); |
| 847 | + //assertEquals(rs.getTimestamp(2), java.sql.Timestamp.valueOf(LocalDateTime.of(2021, 11, 1, 12, 34, 56))); |
| 848 | + assertEquals(rs.getInt(3), 1); |
| 849 | + assertEquals(rs.getInt("FNABS"), 1); |
| 850 | + assertEquals(rs.getString(4), "HelloWorld"); |
| 851 | + assertEquals(rs.getString("FNCONCAT"), "HelloWorld"); |
| 852 | + assertEquals(rs.getString(5), "HELLO"); |
| 853 | + assertEquals(rs.getString("FNUPPER"), "HELLO"); |
| 854 | + assertEquals(rs.getString(6), "hello"); |
| 855 | + assertEquals(rs.getString("FNLOWER"), "hello"); |
| 856 | + assertEquals(rs.getString(7), "Hello "); |
| 857 | + assertEquals(rs.getString("FNLTRIM"), "Hello "); |
| 858 | + assertEquals(rs.getString(8), " Hello"); |
| 859 | + assertEquals(rs.getString("FNRTRIM"), " Hello"); |
| 860 | + assertEquals(rs.getInt(9), 5); |
| 861 | + assertEquals(rs.getInt("FNLENGTH"), 5); |
| 862 | + assertEquals(rs.getInt(10), 3); |
| 863 | + assertEquals(rs.getInt("FNPOSITION"), 3); |
| 864 | + assertEquals(rs.getInt(11), 1); |
| 865 | + assertEquals(rs.getInt("FNMOD"), 1); |
| 866 | + assertEquals(rs.getDouble(12), 3); |
| 867 | + assertEquals(rs.getDouble("FNSQRT"), 3); |
| 868 | + assertEquals(rs.getString(13), "ll"); |
| 869 | + assertEquals(rs.getString("FNSUBSTRING"), "ll"); |
| 870 | + assertThrows(SQLException.class, () -> rs.getString(14)); |
| 871 | + assertFalse(rs.next()); |
| 872 | + } |
| 873 | + } |
| 874 | + } |
| 875 | + } |
828 | 876 | } |
0 commit comments