-
Notifications
You must be signed in to change notification settings - Fork 592
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(cursor): add extended declare cursor test
- Loading branch information
1 parent
288e69e
commit afe95e3
Showing
1 changed file
with
69 additions
and
0 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
integration_tests/client-library/java/src/test/java/com/risingwave/TestCursor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.risingwave; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.sql.*; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
|
||
public class TestCursor { | ||
|
||
public static void createTable() throws SQLException { | ||
try (Connection connection = TestUtils.establishConnection()) { | ||
String createTableSQL = "CREATE TABLE test_table (" + | ||
"id INT PRIMARY KEY, " + | ||
"trading_date DATE, " + | ||
"volume INT)"; | ||
Statement statement = connection.createStatement(); | ||
statement.execute(createTableSQL); | ||
|
||
String insertSQL = "INSERT INTO test_table (id, trading_date, volume) VALUES (1, '2024-07-10', 23)"; | ||
statement.execute(insertSQL); | ||
System.out.println("Table test_table created successfully."); | ||
} | ||
} | ||
|
||
public static void dropTable() throws SQLException { | ||
String dropSourceQuery = "DROP TABLE test_table;"; | ||
try (Connection connection = TestUtils.establishConnection()) { | ||
Statement statement = connection.createStatement(); | ||
statement.executeUpdate(dropSourceQuery); | ||
System.out.println("Table test_table dropped successfully."); | ||
} | ||
} | ||
|
||
|
||
public static void readWithExtendedCursor() throws SQLException { | ||
try (Connection connection = TestUtils.establishConnection()) { | ||
connection.setAutoCommit(false); | ||
Statement statement = connection.createStatement(); | ||
statement.execute("START TRANSACTION ISOLATION LEVEL REPEATABLE READ"); | ||
|
||
String declareCursorSql = "DECLARE c1 CURSOR FOR SELECT id, trading_date, volume FROM public.test_table WHERE ((id = CAST(? AS INT)))"; | ||
PreparedStatement pstmt = connection.prepareStatement(declareCursorSql); | ||
pstmt.setInt(1, 1); | ||
pstmt.execute(); | ||
|
||
statement.execute("FETCH 100 FROM c1"); | ||
ResultSet resultSet = statement.getResultSet(); | ||
|
||
while (resultSet != null && resultSet.next()) { | ||
Assertions.assertEquals(resultSet.getInt("id"), 1); | ||
Assertions.assertEquals(resultSet.getString("trading_date"), "2024-07-10"); | ||
Assertions.assertEquals(resultSet.getInt("volume"), 23); | ||
} | ||
|
||
statement.execute("CLOSE c1"); | ||
statement.execute("COMMIT"); | ||
|
||
System.out.println("Data in table read with cursor successfully."); | ||
} | ||
} | ||
|
||
@Test | ||
public void testCursor() throws SQLException { | ||
createTable(); | ||
readWithExtendedCursor(); | ||
dropTable(); | ||
} | ||
} |