|
21 | 21 |
|
22 | 22 | import org.apache.iotdb.it.env.EnvFactory; |
23 | 23 | import org.apache.iotdb.it.framework.IoTDBTestRunner; |
| 24 | +import org.apache.iotdb.itbase.category.TableClusterIT; |
24 | 25 | import org.apache.iotdb.itbase.category.TableLocalStandaloneIT; |
| 26 | +import org.apache.iotdb.itbase.runtime.ClusterTestConnection; |
25 | 27 |
|
26 | 28 | import org.junit.AfterClass; |
27 | 29 | import org.junit.BeforeClass; |
|
30 | 32 | import org.junit.runner.RunWith; |
31 | 33 |
|
32 | 34 | import java.sql.Connection; |
| 35 | +import java.sql.ResultSet; |
| 36 | +import java.sql.ResultSetMetaData; |
33 | 37 | import java.sql.SQLException; |
34 | 38 | import java.sql.Statement; |
35 | 39 |
|
36 | 40 | import static org.apache.iotdb.db.it.utils.TestUtils.tableResultSetEqualTest; |
| 41 | +import static org.junit.Assert.assertEquals; |
37 | 42 | import static org.junit.Assert.assertThrows; |
38 | 43 | import static org.junit.Assert.assertTrue; |
39 | 44 | import static org.junit.Assert.fail; |
40 | 45 |
|
41 | 46 | @RunWith(IoTDBTestRunner.class) |
42 | | -@Category({TableLocalStandaloneIT.class}) |
| 47 | +@Category({TableLocalStandaloneIT.class, TableClusterIT.class}) |
43 | 48 | public class IoTDBPreparedStatementIT { |
44 | 49 | private static final String DATABASE_NAME = "test"; |
45 | 50 | private static final String[] sqls = |
@@ -76,6 +81,74 @@ public static void tearDown() { |
76 | 81 | EnvFactory.getEnv().cleanClusterEnvironment(); |
77 | 82 | } |
78 | 83 |
|
| 84 | + /** |
| 85 | + * Execute a prepared statement query and verify the result. For PreparedStatement EXECUTE |
| 86 | + * queries, use the write connection directly instead of tableResultSetEqualTest, because |
| 87 | + * PreparedStatements are session-scoped and tableResultSetEqualTest may route queries to |
| 88 | + * different nodes where the PreparedStatement doesn't exist. |
| 89 | + */ |
| 90 | + private static void executePreparedStatementAndVerify( |
| 91 | + Connection connection, |
| 92 | + Statement statement, |
| 93 | + String executeSql, |
| 94 | + String[] expectedHeader, |
| 95 | + String[] expectedRetArray) |
| 96 | + throws SQLException { |
| 97 | + // Execute with parameters using write connection directly |
| 98 | + // In cluster test, we need to use write connection to ensure same session |
| 99 | + if (connection instanceof ClusterTestConnection) { |
| 100 | + // Use write connection directly for PreparedStatement queries |
| 101 | + try (Statement writeStatement = |
| 102 | + ((ClusterTestConnection) connection) |
| 103 | + .writeConnection |
| 104 | + .getUnderlyingConnection() |
| 105 | + .createStatement(); |
| 106 | + ResultSet resultSet = writeStatement.executeQuery(executeSql)) { |
| 107 | + ResultSetMetaData metaData = resultSet.getMetaData(); |
| 108 | + |
| 109 | + // Verify header |
| 110 | + assertEquals(expectedHeader.length, metaData.getColumnCount()); |
| 111 | + for (int i = 1; i <= metaData.getColumnCount(); i++) { |
| 112 | + assertEquals(expectedHeader[i - 1], metaData.getColumnName(i)); |
| 113 | + } |
| 114 | + |
| 115 | + // Verify data |
| 116 | + int cnt = 0; |
| 117 | + while (resultSet.next()) { |
| 118 | + StringBuilder builder = new StringBuilder(); |
| 119 | + for (int i = 1; i <= expectedHeader.length; i++) { |
| 120 | + builder.append(resultSet.getString(i)).append(","); |
| 121 | + } |
| 122 | + assertEquals(expectedRetArray[cnt], builder.toString()); |
| 123 | + cnt++; |
| 124 | + } |
| 125 | + assertEquals(expectedRetArray.length, cnt); |
| 126 | + } |
| 127 | + } else { |
| 128 | + try (ResultSet resultSet = statement.executeQuery(executeSql)) { |
| 129 | + ResultSetMetaData metaData = resultSet.getMetaData(); |
| 130 | + |
| 131 | + // Verify header |
| 132 | + assertEquals(expectedHeader.length, metaData.getColumnCount()); |
| 133 | + for (int i = 1; i <= metaData.getColumnCount(); i++) { |
| 134 | + assertEquals(expectedHeader[i - 1], metaData.getColumnName(i)); |
| 135 | + } |
| 136 | + |
| 137 | + // Verify data |
| 138 | + int cnt = 0; |
| 139 | + while (resultSet.next()) { |
| 140 | + StringBuilder builder = new StringBuilder(); |
| 141 | + for (int i = 1; i <= expectedHeader.length; i++) { |
| 142 | + builder.append(resultSet.getString(i)).append(","); |
| 143 | + } |
| 144 | + assertEquals(expectedRetArray[cnt], builder.toString()); |
| 145 | + cnt++; |
| 146 | + } |
| 147 | + assertEquals(expectedRetArray.length, cnt); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
79 | 152 | @Test |
80 | 153 | public void testPrepareAndExecute() { |
81 | 154 | String[] expectedHeader = new String[] {"time", "id", "name", "value"}; |
|
0 commit comments