2121
2222import org .apache .iotdb .it .env .EnvFactory ;
2323import org .apache .iotdb .it .framework .IoTDBTestRunner ;
24- import org .apache .iotdb .itbase .category .TableClusterIT ;
2524import org .apache .iotdb .itbase .category .TableLocalStandaloneIT ;
26- import org .apache .iotdb .itbase .runtime .ClusterTestConnection ;
2725
2826import org .junit .AfterClass ;
2927import org .junit .BeforeClass ;
3230import org .junit .runner .RunWith ;
3331
3432import java .sql .Connection ;
35- import java .sql .ResultSet ;
36- import java .sql .ResultSetMetaData ;
3733import java .sql .SQLException ;
3834import java .sql .Statement ;
3935
4036import static org .apache .iotdb .db .it .utils .TestUtils .tableResultSetEqualTest ;
41- import static org .junit .Assert .assertEquals ;
4237import static org .junit .Assert .assertThrows ;
4338import static org .junit .Assert .assertTrue ;
4439import static org .junit .Assert .fail ;
4540
4641@ RunWith (IoTDBTestRunner .class )
47- @ Category ({TableLocalStandaloneIT .class , TableClusterIT . class })
42+ @ Category ({TableLocalStandaloneIT .class })
4843public class IoTDBPreparedStatementIT {
4944 private static final String DATABASE_NAME = "test" ;
5045 private static final String [] sqls =
@@ -81,74 +76,6 @@ public static void tearDown() {
8176 EnvFactory .getEnv ().cleanClusterEnvironment ();
8277 }
8378
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-
15279 @ Test
15380 public void testPrepareAndExecute () {
15481 String [] expectedHeader = new String [] {"time" , "id" , "name" , "value" };
@@ -158,9 +85,8 @@ public void testPrepareAndExecute() {
15885 statement .execute ("USE " + DATABASE_NAME );
15986 // Prepare a statement
16087 statement .execute ("PREPARE stmt1 FROM SELECT * FROM test_table WHERE id = ?" );
161- // Execute with parameter using write connection directly
162- executePreparedStatementAndVerify (
163- connection , statement , "EXECUTE stmt1 USING 2" , expectedHeader , retArray );
88+ // Execute with parameter
89+ tableResultSetEqualTest ("EXECUTE stmt1 USING 2" , expectedHeader , retArray , DATABASE_NAME );
16490 // Deallocate
16591 statement .execute ("DEALLOCATE PREPARE stmt1" );
16692 } catch (SQLException e ) {
@@ -178,11 +104,9 @@ public void testPrepareAndExecuteMultipleTimes() {
178104 statement .execute ("USE " + DATABASE_NAME );
179105 // Prepare a statement
180106 statement .execute ("PREPARE stmt2 FROM SELECT * FROM test_table WHERE id = ?" );
181- // Execute multiple times with different parameters using write connection directly
182- executePreparedStatementAndVerify (
183- connection , statement , "EXECUTE stmt2 USING 1" , expectedHeader , retArray1 );
184- executePreparedStatementAndVerify (
185- connection , statement , "EXECUTE stmt2 USING 3" , expectedHeader , retArray2 );
107+ // Execute multiple times with different parameters
108+ tableResultSetEqualTest ("EXECUTE stmt2 USING 1" , expectedHeader , retArray1 , DATABASE_NAME );
109+ tableResultSetEqualTest ("EXECUTE stmt2 USING 3" , expectedHeader , retArray2 , DATABASE_NAME );
186110 // Deallocate
187111 statement .execute ("DEALLOCATE PREPARE stmt2" );
188112 } catch (SQLException e ) {
@@ -199,9 +123,9 @@ public void testPrepareWithMultipleParameters() {
199123 statement .execute ("USE " + DATABASE_NAME );
200124 // Prepare a statement with multiple parameters
201125 statement .execute ("PREPARE stmt3 FROM SELECT * FROM test_table WHERE id = ? AND value > ?" );
202- // Execute with multiple parameters using write connection directly
203- executePreparedStatementAndVerify (
204- connection , statement , "EXECUTE stmt3 USING 2, 150.0" , expectedHeader , retArray );
126+ // Execute with multiple parameters
127+ tableResultSetEqualTest (
128+ "EXECUTE stmt3 USING 2, 150.0" , expectedHeader , retArray , DATABASE_NAME );
205129 // Deallocate
206130 statement .execute ("DEALLOCATE PREPARE stmt3" );
207131 } catch (SQLException e ) {
@@ -309,11 +233,10 @@ public void testMultiplePreparedStatements() {
309233 // Prepare multiple statements
310234 statement .execute ("PREPARE stmt4 FROM SELECT * FROM test_table WHERE id = ?" );
311235 statement .execute ("PREPARE stmt5 FROM SELECT COUNT(*) FROM test_table WHERE value > ?" );
312- // Execute both statements using write connection directly
313- executePreparedStatementAndVerify (
314- connection , statement , "EXECUTE stmt4 USING 1" , expectedHeader1 , retArray1 );
315- executePreparedStatementAndVerify (
316- connection , statement , "EXECUTE stmt5 USING 200.0" , expectedHeader2 , retArray2 );
236+ // Execute both statements
237+ tableResultSetEqualTest ("EXECUTE stmt4 USING 1" , expectedHeader1 , retArray1 , DATABASE_NAME );
238+ tableResultSetEqualTest (
239+ "EXECUTE stmt5 USING 200.0" , expectedHeader2 , retArray2 , DATABASE_NAME );
317240 // Deallocate both
318241 statement .execute ("DEALLOCATE PREPARE stmt4" );
319242 statement .execute ("DEALLOCATE PREPARE stmt5" );
@@ -354,9 +277,8 @@ public void testPrepareAndExecuteWithAggregation() {
354277 // Prepare a statement with aggregation
355278 statement .execute (
356279 "PREPARE stmt7 FROM SELECT AVG(value) FROM test_table WHERE id >= ? AND id <= ?" );
357- // Execute with parameters using write connection directly
358- executePreparedStatementAndVerify (
359- connection , statement , "EXECUTE stmt7 USING 2, 4" , expectedHeader , retArray );
280+ // Execute with parameters
281+ tableResultSetEqualTest ("EXECUTE stmt7 USING 2, 4" , expectedHeader , retArray , DATABASE_NAME );
360282 // Deallocate
361283 statement .execute ("DEALLOCATE PREPARE stmt7" );
362284 } catch (SQLException e ) {
@@ -373,9 +295,9 @@ public void testPrepareAndExecuteWithStringParameter() {
373295 statement .execute ("USE " + DATABASE_NAME );
374296 // Prepare a statement with string parameter
375297 statement .execute ("PREPARE stmt8 FROM SELECT * FROM test_table WHERE name = ?" );
376- // Execute with string parameter using write connection directly
377- executePreparedStatementAndVerify (
378- connection , statement , "EXECUTE stmt8 USING 'Charlie'" , expectedHeader , retArray );
298+ // Execute with string parameter
299+ tableResultSetEqualTest (
300+ "EXECUTE stmt8 USING 'Charlie'" , expectedHeader , retArray , DATABASE_NAME );
379301 // Deallocate
380302 statement .execute ("DEALLOCATE PREPARE stmt8" );
381303 } catch (SQLException e ) {
0 commit comments