Skip to content

Commit

Permalink
test(cursor): add extended declare cursor test
Browse files Browse the repository at this point in the history
  • Loading branch information
KeXiangWang committed Oct 23, 2024
1 parent 288e69e commit afe95e3
Showing 1 changed file with 69 additions and 0 deletions.
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();
}
}

0 comments on commit afe95e3

Please sign in to comment.