diff --git a/bindings/java/rs_src/turso_statement.rs b/bindings/java/rs_src/turso_statement.rs index cdf58eddf..94eddf8dc 100644 --- a/bindings/java/rs_src/turso_statement.rs +++ b/bindings/java/rs_src/turso_statement.rs @@ -284,6 +284,23 @@ pub extern "system" fn Java_tech_turso_core_TursoStatement_totalChanges<'local>( stmt.connection.conn.total_changes() } +#[no_mangle] +pub extern "system" fn Java_tech_turso_core_TursoStatement_changes<'local>( + mut env: JNIEnv<'local>, + obj: JObject<'local>, + stmt_ptr: jlong, +) -> jlong { + let stmt = match to_turso_statement(stmt_ptr) { + Ok(stmt) => stmt, + Err(e) => { + set_err_msg_and_throw_exception(&mut env, obj, SQLITE_ERROR, e.to_string()); + return -1; + } + }; + + stmt.connection.conn.changes() +} + /// Converts an optional `JObject` into Java's `TursoStepResult`. /// /// This function takes an optional `JObject` and converts it into a Java object diff --git a/bindings/java/src/main/java/tech/turso/core/TursoStatement.java b/bindings/java/src/main/java/tech/turso/core/TursoStatement.java index c435fab9f..de4d86e7a 100644 --- a/bindings/java/src/main/java/tech/turso/core/TursoStatement.java +++ b/bindings/java/src/main/java/tech/turso/core/TursoStatement.java @@ -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. * diff --git a/bindings/java/src/main/java/tech/turso/jdbc4/JDBC4PreparedStatement.java b/bindings/java/src/main/java/tech/turso/jdbc4/JDBC4PreparedStatement.java index a260d1ef2..d9508e0dd 100644 --- a/bindings/java/src/main/java/tech/turso/jdbc4/JDBC4PreparedStatement.java +++ b/bindings/java/src/main/java/tech/turso/jdbc4/JDBC4PreparedStatement.java @@ -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 diff --git a/bindings/java/src/test/java/tech/turso/jdbc4/JDBC4PreparedStatementTest.java b/bindings/java/src/test/java/tech/turso/jdbc4/JDBC4PreparedStatementTest.java index 7d2f0274b..4eb51a9cb 100644 --- a/bindings/java/src/test/java/tech/turso/jdbc4/JDBC4PreparedStatementTest.java +++ b/bindings/java/src/test/java/tech/turso/jdbc4/JDBC4PreparedStatementTest.java @@ -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); + } + }