|
4 | 4 | import com.clickhouse.data.ClickHouseVersion;
|
5 | 5 | import com.clickhouse.data.Tuple;
|
6 | 6 | import com.clickhouse.jdbc.internal.DriverProperties;
|
| 7 | +import com.clickhouse.jdbc.internal.JdbcUtils; |
7 | 8 | import org.apache.commons.lang3.RandomStringUtils;
|
8 |
| -import org.testcontainers.shaded.com.fasterxml.jackson.databind.deser.std.UUIDDeserializer; |
9 | 9 | import org.testng.Assert;
|
10 | 10 | import org.testng.annotations.DataProvider;
|
11 |
| -import org.testng.annotations.Ignore; |
12 | 11 | import org.testng.annotations.Test;
|
13 | 12 |
|
14 | 13 | import java.io.ByteArrayInputStream;
|
15 | 14 | import java.io.InputStreamReader;
|
16 | 15 | import java.sql.Array;
|
17 | 16 | import java.sql.Connection;
|
18 | 17 | import java.sql.Date;
|
| 18 | +import java.sql.JDBCType; |
19 | 19 | import java.sql.PreparedStatement;
|
20 | 20 | import java.sql.ResultSet;
|
21 | 21 | import java.sql.ResultSetMetaData;
|
22 | 22 | import java.sql.SQLException;
|
| 23 | +import java.sql.SQLType; |
23 | 24 | import java.sql.Statement;
|
24 | 25 | import java.sql.Timestamp;
|
25 | 26 | import java.sql.Types;
|
@@ -1260,22 +1261,84 @@ public void testWithInClause() throws Exception {
|
1260 | 1261 | }
|
1261 | 1262 | }
|
1262 | 1263 |
|
| 1264 | + @Test(groups = {"integration"}, dataProvider = "testTypeCastsDP") |
| 1265 | + public void testTypeCastsWithoutArgument(Object value, SQLType targetType, ClickHouseDataType expectedType) throws Exception { |
| 1266 | + try (Connection conn = getJdbcConnection()) { |
| 1267 | + try (PreparedStatement stmt = conn.prepareStatement("select ?, toTypeName(?)")) { |
| 1268 | + stmt.setObject(1, value, targetType); |
| 1269 | + stmt.setObject(2, value, targetType); |
| 1270 | + |
| 1271 | + try (ResultSet rs = stmt.executeQuery()) { |
| 1272 | + rs.next(); |
| 1273 | + assertEquals(rs.getString(2), expectedType.getName()); |
| 1274 | + switch (expectedType) { |
| 1275 | + case IPv4: |
| 1276 | + assertEquals(rs.getString(1), "/" + value); |
| 1277 | + break; |
| 1278 | + case IPv6: |
| 1279 | + // do not check |
| 1280 | + break; |
| 1281 | + default: |
| 1282 | + assertEquals(rs.getString(1), String.valueOf(value)); |
| 1283 | + } |
| 1284 | + } |
| 1285 | + } |
| 1286 | + } |
| 1287 | + } |
| 1288 | + |
| 1289 | + @DataProvider(name = "testTypeCastsDP") |
| 1290 | + public static Object[][] testTypeCastsDP() { |
| 1291 | + return new Object[][] { |
| 1292 | + {100, ClickHouseDataType.Int8, ClickHouseDataType.Int8}, |
| 1293 | + {100L, ClickHouseDataType.Int16, ClickHouseDataType.Int16}, |
| 1294 | + {100L, ClickHouseDataType.Int32, ClickHouseDataType.Int32}, |
| 1295 | + {100L, ClickHouseDataType.Int64, ClickHouseDataType.Int64}, |
| 1296 | + {100L, ClickHouseDataType.UInt8, ClickHouseDataType.UInt8}, |
| 1297 | + {100L, ClickHouseDataType.UInt16, ClickHouseDataType.UInt16}, |
| 1298 | + {100L, ClickHouseDataType.UInt32, ClickHouseDataType.UInt32}, |
| 1299 | + {100L, ClickHouseDataType.UInt64, ClickHouseDataType.UInt64}, |
| 1300 | + {"ed0c77a3-2e4b-4954-98ee-22a4fdad9565", ClickHouseDataType.UUID, ClickHouseDataType.UUID}, |
| 1301 | + {"::ffff:127.0.0.1", ClickHouseDataType.IPv6, ClickHouseDataType.IPv6}, |
| 1302 | + {"116.253.40.133", ClickHouseDataType.IPv4, ClickHouseDataType.IPv4}, |
| 1303 | + }; |
| 1304 | + } |
| 1305 | + |
1263 | 1306 | @Test(groups = {"integration"})
|
1264 |
| - public void testTypeCasts() throws Exception { |
| 1307 | + public void testTypesInvalidForCast() throws Exception { |
1265 | 1308 | try (Connection conn = getJdbcConnection()) {
|
1266 | 1309 | try (PreparedStatement stmt = conn.prepareStatement("select ?, toTypeName(?)")) {
|
1267 |
| - stmt.setObject(1, 100, ClickHouseDataType.Int8); |
1268 |
| - stmt.setObject(2, 100, ClickHouseDataType.Int8); |
| 1310 | + for (ClickHouseDataType type : JdbcUtils.INVALID_TARGET_TYPES) { |
| 1311 | + expectThrows(SQLException.class, ()->stmt.setObject(1, "", type)); |
| 1312 | + } |
| 1313 | + } |
| 1314 | + } |
| 1315 | + } |
1269 | 1316 |
|
1270 |
| - try (ResultSet rs = stmt.executeQuery()) { |
1271 |
| - rs.next(); |
1272 |
| - assertEquals(rs.getInt(1), 100); |
1273 |
| - assertEquals(rs.getString(2), ClickHouseDataType.Int8.getName()); |
1274 |
| - } |
| 1317 | + @Test(groups = {"integration"}, dataProvider = "testTypeCastWithScaleOrLengthDP") |
| 1318 | + public void testTypeCastWithScaleOrLength(Object value, SQLType targetType, Integer scaleOrLength, String expectedValue, |
| 1319 | + String expectedType) throws Exception { |
| 1320 | + try (Connection conn = getJdbcConnection()) { |
| 1321 | + try (PreparedStatement stmt = conn.prepareStatement("select ?, toTypeName(?)")) { |
| 1322 | + stmt.setObject(1, value, targetType, scaleOrLength); |
| 1323 | + stmt.setObject(2, value, targetType, scaleOrLength); |
| 1324 | + try (ResultSet rs = stmt.executeQuery()) { |
| 1325 | + rs.next(); |
| 1326 | + assertEquals(rs.getString(1), expectedValue); |
| 1327 | + assertEquals(rs.getString(2), expectedType); |
| 1328 | + } |
1275 | 1329 | }
|
1276 | 1330 | }
|
1277 | 1331 | }
|
1278 | 1332 |
|
| 1333 | + @DataProvider(name = "testTypeCastWithScaleOrLengthDP") |
| 1334 | + public static Object[][] testTypeCastWithScaleOrLengthDP() { |
| 1335 | + return new Object[][] { |
| 1336 | + {0.123456789, ClickHouseDataType.Decimal64, 3, "0.123", "Decimal(18, 3)"}, |
| 1337 | + {"hello", ClickHouseDataType.FixedString, 5, "hello", "FixedString(5)"}, |
| 1338 | + {"2017-10-02 10:20:30.333333", ClickHouseDataType.DateTime64, 3, "2017-10-02T10:20:30.333", "DateTime64(3)"} |
| 1339 | + }; |
| 1340 | + } |
| 1341 | + |
1279 | 1342 | @Test(groups = {"integration"})
|
1280 | 1343 | public void testCheckOfParametersAreSet() throws Exception {
|
1281 | 1344 | try (Connection conn = getJdbcConnection()) {
|
|
0 commit comments