From 8cb6f6274eb7e88feecd96e82c658f715f05ba5c Mon Sep 17 00:00:00 2001 From: ptiurin Date: Wed, 11 Dec 2024 17:05:22 +0000 Subject: [PATCH] Add complex struct test case --- .../tests/PreparedStatementTest.java | 39 +++++++++++++++++++ .../prepared-statement-struct/cleanup.sql | 2 + .../prepared-statement-struct/ddl.sql | 10 +++++ 3 files changed, 51 insertions(+) create mode 100644 src/integrationTest/resources/statements/prepared-statement-struct/cleanup.sql create mode 100644 src/integrationTest/resources/statements/prepared-statement-struct/ddl.sql diff --git a/src/integrationTest/java/integration/tests/PreparedStatementTest.java b/src/integrationTest/java/integration/tests/PreparedStatementTest.java index b9d400e9..cf3fe02f 100644 --- a/src/integrationTest/java/integration/tests/PreparedStatementTest.java +++ b/src/integrationTest/java/integration/tests/PreparedStatementTest.java @@ -457,6 +457,44 @@ void shouldInsertAndSelectStruct() throws SQLException { } } + @Test + @Tag("v2") + void shouldInsertAndSelectComplexStruct() throws SQLException { + Car car1 = Car.builder().ts(new Timestamp(2)).d(new Date(3)).tags(new String[] { "fast", "sleek" }).build(); + + executeStatementFromFile("/statements/prepared-statement-struct/ddl.sql"); + try (Connection connection = createConnection()) { + + try (PreparedStatement statement = connection + .prepareStatement("INSERT INTO test_struct_helper(a, b) VALUES (?,?)")) { + statement.setArray(1, connection.createArrayOf("VARCHAR", car1.getTags())); + statement.setTimestamp(2, car1.getTs()); + statement.executeUpdate(); + } + + setParam(connection, "advanced_mode", "true"); + setParam(connection, "enable_row_selection", "true"); + try (Statement statement = connection.createStatement()) { + statement.execute( + "INSERT INTO test_struct(id, s) SELECT 1, test_struct_helper FROM test_struct_helper"); + } + try (Statement statement = connection.createStatement(); + ResultSet rs = statement + .executeQuery("SELECT test_struct FROM test_struct")) { + rs.next(); + assertEquals(FireboltDataType.STRUCT.name().toLowerCase() + + "(id int, s struct(a array(text null), b timestamp null))", + rs.getMetaData().getColumnTypeName(1).toLowerCase()); + String expectedJson = String.format( + "{\"id\":%d,\"s\":{\"a\":[\"%s\",\"%s\"],\"b\":\"%s\"}}", 1, car1.getTags()[0], + car1.getTags()[1], car1.getTs().toString()); + assertEquals(expectedJson, rs.getString(1)); + } + } finally { + executeStatementFromFile("/statements/prepared-statement-struct/cleanup.sql"); + } + } + private QueryResult createExpectedResult(List> expectedRows) { return QueryResult.builder().databaseName(ConnectionInfo.getInstance().getDatabase()) .tableName("prepared_statement_test") @@ -527,6 +565,7 @@ private static class Car { Timestamp ts; Date d; URL url; + String[] tags; } } diff --git a/src/integrationTest/resources/statements/prepared-statement-struct/cleanup.sql b/src/integrationTest/resources/statements/prepared-statement-struct/cleanup.sql new file mode 100644 index 00000000..5df6e649 --- /dev/null +++ b/src/integrationTest/resources/statements/prepared-statement-struct/cleanup.sql @@ -0,0 +1,2 @@ +DROP TABLE IF EXISTS test_struct; +DROP TABLE IF EXISTS test_struct_helper; \ No newline at end of file diff --git a/src/integrationTest/resources/statements/prepared-statement-struct/ddl.sql b/src/integrationTest/resources/statements/prepared-statement-struct/ddl.sql new file mode 100644 index 00000000..333f372a --- /dev/null +++ b/src/integrationTest/resources/statements/prepared-statement-struct/ddl.sql @@ -0,0 +1,10 @@ +SET advanced_mode=1; +SET enable_struct=1; +SET enable_create_table_v2=true; +SET enable_row_selection=true; +SET prevent_create_on_information_schema=true; +SET enable_create_table_with_struct_type=true; +DROP TABLE IF EXISTS test_struct; +DROP TABLE IF EXISTS test_struct_helper; +CREATE TABLE IF NOT EXISTS test_struct(id int not null, s struct(a array(text) not null, b datetime null) not null); +CREATE TABLE IF NOT EXISTS test_struct_helper(a array(text) not null, b datetime null);