add executeUpdate updated count

This commit is contained in:
zongkx
2025-09-11 12:17:05 +00:00
parent 0287512a47
commit 5d6e97b46b
4 changed files with 66 additions and 9 deletions

View File

@@ -231,6 +231,22 @@ public final class TursoStatement {
private native long totalChanges(long statementPointer) throws SQLException;
/**
* Returns number of changes.
*
* @throws SQLException If a database access error occurs
*/
public long changes() throws SQLException {
final long result = changes(statementPointer);
if (result == -1) {
throw new SQLException("Exception while retrieving number of changes");
}
return result;
}
private native long changes(long statementPointer) throws SQLException;
/**
* Checks if the statement is closed.
*

View File

@@ -49,9 +49,7 @@ public final class JDBC4PreparedStatement extends JDBC4Statement implements Prep
requireNonNull(this.statement);
final TursoResultSet resultSet = statement.getResultSet();
resultSet.consumeAll();
// TODO: return updated count
return 0;
return Math.toIntExact(statement.changes());
}
@Override

View File

@@ -6,12 +6,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.*;
import java.util.Properties;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -342,4 +337,35 @@ class JDBC4PreparedStatementTest {
assertEquals("row2", rs.getString(3));
assertArrayEquals(new byte[] {4, 5, 6}, rs.getBytes(4));
}
@Test
void execute_insert_should_return_number_of_inserted_elements() throws Exception {
connection.prepareStatement("CREATE TABLE test (col INTEGER)").execute();
PreparedStatement prepareStatement = connection.prepareStatement("INSERT INTO test (col) VALUES (?), (?), (?)");
prepareStatement.setInt(1, 1);
prepareStatement.setInt(2, 2);
prepareStatement.setInt(3, 3);
assertEquals(prepareStatement.executeUpdate(), 3);
}
@Test
void execute_update_should_return_number_of_updated_elements() throws Exception {
connection.prepareStatement("CREATE TABLE test (col INTEGER)").execute();
connection.prepareStatement("INSERT INTO test (col) VALUES (1), (2), (3)").execute();
PreparedStatement preparedStatement =
connection.prepareStatement("UPDATE test SET col = ? where col = 1 ");
preparedStatement.setInt(1, 4);
assertEquals(preparedStatement.executeUpdate(), 1);
}
@Test
void execute_delete_should_return_number_of_deleted_elements() throws Exception {
connection.prepareStatement("CREATE TABLE test (col INTEGER)").execute();
connection.prepareStatement("INSERT INTO test (col) VALUES (1), (2), (3)").execute();
PreparedStatement preparedStatement =
connection.prepareStatement("DELETE FROM test where col = ? ");
preparedStatement.setInt(1, 1);
assertEquals(preparedStatement.executeUpdate(), 1);
}
}