Merge ' bindings/java: PreparedStatement executeUpdate ' from zongkx

JDBC Driver  Add PreparedStatement executeUpdate return

Closes #3022
This commit is contained in:
Pekka Enberg
2025-09-11 18:43:58 +03:00
committed by GitHub
4 changed files with 66 additions and 9 deletions

View File

@@ -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

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);
}
}