Implement bindXXX functions on rust and java side

This commit is contained in:
김선우
2025-02-07 11:25:23 +09:00
parent d574e2c277
commit 21d6f33c6b
3 changed files with 306 additions and 16 deletions

View File

@@ -97,14 +97,106 @@ public class LimboStatement {
* @throws SQLException if a database access error occurs while retrieving column names
*/
public void initializeColumnMetadata() throws SQLException {
final String[] columnNames = this.columnNames(statementPointer);
final String[] columnNames = this.columns(statementPointer);
if (columnNames != null) {
this.resultSet.setColumnNames(columnNames);
}
}
@Nullable
private native String[] columnNames(long statementPointer) throws SQLException;
private native String[] columns(long statementPointer) throws SQLException;
/**
* Binds a NULL value to the prepared statement at the specified position.
*
* @param position The index of the SQL parameter to be set to NULL.
* @return <a href="https://www.sqlite.org/c3ref/c_abort.html">Result Codes</a>
* @throws SQLException If a database access error occurs.
*/
public int bindNull(int position) throws SQLException {
final int result = bindNull(statementPointer, position);
if (result != 0) {
throw new SQLException("Exception while binding NULL value at position " + position);
}
return result;
}
private native int bindNull(long statementPointer, int position) throws SQLException;
/**
* Binds a long value to the prepared statement at the specified position.
*
* @param position The index of the SQL parameter to be set.
* @param value The value to bind to the parameter.
* @return <a href="https://www.sqlite.org/c3ref/c_abort.html">Result Codes</a>
* @throws SQLException If a database access error occurs.
*/
public int bindLong(int position, long value) throws SQLException {
final int result = bindLong(statementPointer, position, value);
if (result != 0) {
throw new SQLException("Exception while binding long value at position " + position);
}
return result;
}
private native int bindLong(long statementPointer, int position, long value) throws SQLException;
/**
* Binds a double value to the prepared statement at the specified position.
*
* @param position The index of the SQL parameter to be set.
* @param value The value to bind to the parameter.
* @return <a href="https://www.sqlite.org/c3ref/c_abort.html">Result Codes</a>
* @throws SQLException If a database access error occurs.
*/
public int bindDouble(int position, double value) throws SQLException {
final int result = bindDouble(statementPointer, position, value);
if (result != 0) {
throw new SQLException("Exception while binding double value at position " + position);
}
return result;
}
private native int bindDouble(long statementPointer, int position, double value)
throws SQLException;
/**
* Binds a text value to the prepared statement at the specified position.
*
* @param position The index of the SQL parameter to be set.
* @param value The value to bind to the parameter.
* @return <a href="https://www.sqlite.org/c3ref/c_abort.html">Result Codes</a>
* @throws SQLException If a database access error occurs.
*/
public int bindText(int position, String value) throws SQLException {
final int result = bindText(statementPointer, position, value);
if (result != 0) {
throw new SQLException("Exception while binding text value at position " + position);
}
return result;
}
private native int bindText(long statementPointer, int position, String value)
throws SQLException;
/**
* Binds a blob value to the prepared statement at the specified position.
*
* @param position The index of the SQL parameter to be set.
* @param value The value to bind to the parameter.
* @return <a href="https://www.sqlite.org/c3ref/c_abort.html">Result Codes</a>
* @throws SQLException If a database access error occurs.
*/
public int bindBlob(int position, byte[] value) throws SQLException {
final int result = bindBlob(statementPointer, position, value);
if (result != 0) {
throw new SQLException("Exception while binding blob value at position " + position);
}
return result;
}
private native int bindBlob(long statementPointer, int position, byte[] value)
throws SQLException;
/**
* Checks if the statement is closed.

View File

@@ -1,7 +1,9 @@
package org.github.tursodatabase.core;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Properties;
@@ -41,9 +43,80 @@ class LimboStatementTest {
final LimboResultSet rs = stmt.getResultSet();
final String[] columnNames = rs.getColumnNames();
assertEquals(columnNames[0], "name");
assertEquals(columnNames[1], "age");
assertEquals(columnNames[2], "country");
assertEquals("name", columnNames[0]);
assertEquals("age", columnNames[1]);
assertEquals("country", columnNames[2]);
}
@Test
void test_bindNull() throws Exception {
runSql("CREATE TABLE test (col1 TEXT);");
LimboStatement stmt = connection.prepare("INSERT INTO test (col1) VALUES (?);");
stmt.bindNull(1);
stmt.execute();
stmt.close();
LimboStatement selectStmt = connection.prepare("SELECT col1 FROM test;");
selectStmt.execute();
assertNull(selectStmt.getResultSet().get(1));
selectStmt.close();
}
@Test
void test_bindLong() throws Exception {
runSql("CREATE TABLE test (col1 BIGINT);");
LimboStatement stmt = connection.prepare("INSERT INTO test (col1) VALUES (?);");
stmt.bindLong(1, 123456789L);
stmt.execute();
stmt.close();
LimboStatement selectStmt = connection.prepare("SELECT col1 FROM test;");
selectStmt.execute();
assertEquals(123456789L, selectStmt.getResultSet().get(1));
selectStmt.close();
}
@Test
void test_bindDouble() throws Exception {
runSql("CREATE TABLE test (col1 DOUBLE);");
LimboStatement stmt = connection.prepare("INSERT INTO test (col1) VALUES (?);");
stmt.bindDouble(1, 3.14);
stmt.execute();
stmt.close();
LimboStatement selectStmt = connection.prepare("SELECT col1 FROM test;");
selectStmt.execute();
assertEquals(3.14, selectStmt.getResultSet().get(1));
selectStmt.close();
}
@Test
void test_bindText() throws Exception {
runSql("CREATE TABLE test (col1 TEXT);");
LimboStatement stmt = connection.prepare("INSERT INTO test (col1) VALUES (?);");
stmt.bindText(1, "hello");
stmt.execute();
stmt.close();
LimboStatement selectStmt = connection.prepare("SELECT col1 FROM test;");
selectStmt.execute();
assertEquals("hello", selectStmt.getResultSet().get(1));
selectStmt.close();
}
@Test
void test_bindBlob() throws Exception {
runSql("CREATE TABLE test (col1 BLOB);");
LimboStatement stmt = connection.prepare("INSERT INTO test (col1) VALUES (?);");
byte[] blob = {1, 2, 3, 4, 5};
stmt.bindBlob(1, blob);
stmt.execute();
stmt.close();
LimboStatement selectStmt = connection.prepare("SELECT col1 FROM test;");
selectStmt.execute();
assertArrayEquals(blob, (byte[]) selectStmt.getResultSet().get(1));
selectStmt.close();
}
private void runSql(String sql) throws Exception {