diff --git a/bindings/java/rs_src/errors.rs b/bindings/java/rs_src/errors.rs index 0fa2e0276..b323a4385 100644 --- a/bindings/java/rs_src/errors.rs +++ b/bindings/java/rs_src/errors.rs @@ -44,58 +44,56 @@ impl From for LimboError { pub type Result = std::result::Result; +pub const SQLITE_OK: i32 = 0; // Successful result +pub const SQLITE_ERROR: i32 = 1; // Generic error #[allow(dead_code)] -pub const SQLITE_OK: i32 = 0; +pub const SQLITE_INTERNAL: i32 = 2; // Internal logic error in SQLite #[allow(dead_code)] -pub const SQLITE_ERROR: i32 = 1; +pub const SQLITE_PERM: i32 = 3; // Access permission denied #[allow(dead_code)] -pub const SQLITE_INTERNAL: i32 = 2; +pub const SQLITE_ABORT: i32 = 4; // Callback routine requested an abort #[allow(dead_code)] -pub const SQLITE_PERM: i32 = 3; +pub const SQLITE_BUSY: i32 = 5; // The database file is locked #[allow(dead_code)] -pub const SQLITE_ABORT: i32 = 4; +pub const SQLITE_LOCKED: i32 = 6; // A table in the database is locked #[allow(dead_code)] -pub const SQLITE_BUSY: i32 = 5; +pub const SQLITE_NOMEM: i32 = 7; // A malloc() failed #[allow(dead_code)] -pub const SQLITE_LOCKED: i32 = 6; +pub const SQLITE_READONLY: i32 = 8; // Attempt to write a readonly database #[allow(dead_code)] -pub const SQLITE_NOMEM: i32 = 7; +pub const SQLITE_INTERRUPT: i32 = 9; // Operation terminated by sqlite3_interrupt() #[allow(dead_code)] -pub const SQLITE_READONLY: i32 = 8; +pub const SQLITE_IOERR: i32 = 10; // Some kind of disk I/O error occurred #[allow(dead_code)] -pub const SQLITE_INTERRUPT: i32 = 9; +pub const SQLITE_CORRUPT: i32 = 11; // The database disk image is malformed #[allow(dead_code)] -pub const SQLITE_IOERR: i32 = 10; +pub const SQLITE_NOTFOUND: i32 = 12; // Unknown opcode in sqlite3_file_control() #[allow(dead_code)] -pub const SQLITE_CORRUPT: i32 = 11; +pub const SQLITE_FULL: i32 = 13; // Insertion failed because database is full #[allow(dead_code)] -pub const SQLITE_NOTFOUND: i32 = 12; +pub const SQLITE_CANTOPEN: i32 = 14; // Unable to open the database file #[allow(dead_code)] -pub const SQLITE_FULL: i32 = 13; +pub const SQLITE_PROTOCOL: i32 = 15; // Database lock protocol error #[allow(dead_code)] -pub const SQLITE_CANTOPEN: i32 = 14; +pub const SQLITE_EMPTY: i32 = 16; // Internal use only #[allow(dead_code)] -pub const SQLITE_PROTOCOL: i32 = 15; +pub const SQLITE_SCHEMA: i32 = 17; // The database schema changed #[allow(dead_code)] -pub const SQLITE_EMPTY: i32 = 16; +pub const SQLITE_TOOBIG: i32 = 18; // String or BLOB exceeds size limit #[allow(dead_code)] -pub const SQLITE_SCHEMA: i32 = 17; +pub const SQLITE_CONSTRAINT: i32 = 19; // Abort due to constraint violation #[allow(dead_code)] -pub const SQLITE_TOOBIG: i32 = 18; +pub const SQLITE_MISMATCH: i32 = 20; // Data type mismatch #[allow(dead_code)] -pub const SQLITE_CONSTRAINT: i32 = 19; +pub const SQLITE_MISUSE: i32 = 21; // Library used incorrectly #[allow(dead_code)] -pub const SQLITE_MISMATCH: i32 = 20; +pub const SQLITE_NOLFS: i32 = 22; // Uses OS features not supported on host #[allow(dead_code)] -pub const SQLITE_MISUSE: i32 = 21; +pub const SQLITE_AUTH: i32 = 23; // Authorization denied #[allow(dead_code)] -pub const SQLITE_NOLFS: i32 = 22; +pub const SQLITE_ROW: i32 = 100; // sqlite3_step() has another row ready #[allow(dead_code)] -pub const SQLITE_AUTH: i32 = 23; -#[allow(dead_code)] -pub const SQLITE_ROW: i32 = 100; -#[allow(dead_code)] -pub const SQLITE_DONE: i32 = 101; +pub const SQLITE_DONE: i32 = 101; // sqlite3_step() has finished executing #[allow(dead_code)] pub const SQLITE_INTEGER: i32 = 1; diff --git a/bindings/java/rs_src/limbo_statement.rs b/bindings/java/rs_src/limbo_statement.rs index 97328a964..dd1feb55a 100644 --- a/bindings/java/rs_src/limbo_statement.rs +++ b/bindings/java/rs_src/limbo_statement.rs @@ -1,11 +1,12 @@ -use crate::errors::Result; use crate::errors::{LimboError, LIMBO_ETC}; +use crate::errors::{Result, SQLITE_ERROR, SQLITE_OK}; use crate::limbo_connection::LimboConnection; use crate::utils::set_err_msg_and_throw_exception; -use jni::objects::{JObject, JValue}; -use jni::sys::jlong; +use jni::objects::{JByteArray, JObject, JObjectArray, JString, JValue}; +use jni::sys::{jdouble, jint, jlong}; use jni::JNIEnv; -use limbo_core::{Statement, StepResult}; +use limbo_core::{Statement, StepResult, Value}; +use std::num::NonZero; pub const STEP_RESULT_ID_ROW: i32 = 10; #[allow(dead_code)] @@ -125,6 +126,151 @@ fn row_to_obj_array<'local>( Ok(obj_array.into()) } +#[no_mangle] +pub extern "system" fn Java_org_github_tursodatabase_core_LimboStatement_columns<'local>( + mut env: JNIEnv<'local>, + _obj: JObject<'local>, + stmt_ptr: jlong, +) -> JObject<'local> { + let stmt = to_limbo_statement(stmt_ptr).unwrap(); + let num_columns = stmt.stmt.num_columns(); + let obj_arr: JObjectArray = env + .new_object_array(num_columns as i32, "java/lang/String", JObject::null()) + .unwrap(); + + for i in 0..num_columns { + if let Some(column_name) = stmt.stmt.get_column_name(i) { + let str = env.new_string(column_name).unwrap(); + env.set_object_array_element(&obj_arr, i as i32, str) + .unwrap(); + } + } + + obj_arr.into() +} + +#[no_mangle] +pub extern "system" fn Java_org_github_tursodatabase_core_LimboStatement_bindNull<'local>( + mut env: JNIEnv<'local>, + obj: JObject<'local>, + stmt_ptr: jlong, + position: jint, +) -> jint { + let stmt = match to_limbo_statement(stmt_ptr) { + Ok(stmt) => stmt, + Err(e) => { + set_err_msg_and_throw_exception(&mut env, obj, SQLITE_ERROR, e.to_string()); + return SQLITE_ERROR; + } + }; + + stmt.stmt + .bind_at(NonZero::new(position as usize).unwrap(), Value::Null); + SQLITE_OK +} + +#[no_mangle] +pub extern "system" fn Java_org_github_tursodatabase_core_LimboStatement_bindLong<'local>( + mut env: JNIEnv<'local>, + obj: JObject<'local>, + stmt_ptr: jlong, + position: jint, + value: jlong, +) -> jint { + let stmt = match to_limbo_statement(stmt_ptr) { + Ok(stmt) => stmt, + Err(e) => { + set_err_msg_and_throw_exception(&mut env, obj, SQLITE_ERROR, e.to_string()); + return SQLITE_ERROR; + } + }; + + stmt.stmt.bind_at( + NonZero::new(position as usize).unwrap(), + Value::Integer(value), + ); + SQLITE_OK +} + +#[no_mangle] +pub extern "system" fn Java_org_github_tursodatabase_core_LimboStatement_bindDouble<'local>( + mut env: JNIEnv<'local>, + obj: JObject<'local>, + stmt_ptr: jlong, + position: jint, + value: jdouble, +) -> jint { + let stmt = match to_limbo_statement(stmt_ptr) { + Ok(stmt) => stmt, + Err(e) => { + set_err_msg_and_throw_exception(&mut env, obj, SQLITE_ERROR, e.to_string()); + return SQLITE_ERROR; + } + }; + + stmt.stmt.bind_at( + NonZero::new(position as usize).unwrap(), + Value::Float(value), + ); + SQLITE_OK +} + +#[no_mangle] +pub extern "system" fn Java_org_github_tursodatabase_core_LimboStatement_bindText<'local>( + mut env: JNIEnv<'local>, + obj: JObject<'local>, + stmt_ptr: jlong, + position: jint, + value: JString<'local>, +) -> jint { + let stmt = match to_limbo_statement(stmt_ptr) { + Ok(stmt) => stmt, + Err(e) => { + set_err_msg_and_throw_exception(&mut env, obj, SQLITE_ERROR, e.to_string()); + return SQLITE_ERROR; + } + }; + + let text: String = match env.get_string(&value) { + Ok(s) => s.into(), + Err(_) => return SQLITE_ERROR, + }; + + stmt.stmt.bind_at( + NonZero::new(position as usize).unwrap(), + Value::Text(text.as_str()), + ); + SQLITE_OK +} + +#[no_mangle] +pub extern "system" fn Java_org_github_tursodatabase_core_LimboStatement_bindBlob<'local>( + mut env: JNIEnv<'local>, + obj: JObject<'local>, + stmt_ptr: jlong, + position: jint, + value: JByteArray<'local>, +) -> jint { + let stmt = match to_limbo_statement(stmt_ptr) { + Ok(stmt) => stmt, + Err(e) => { + set_err_msg_and_throw_exception(&mut env, obj, SQLITE_ERROR, e.to_string()); + return SQLITE_ERROR; + } + }; + + let blob: Vec = match env.convert_byte_array(value) { + Ok(b) => b, + Err(_) => return SQLITE_ERROR, + }; + + stmt.stmt.bind_at( + NonZero::new(position as usize).unwrap(), + Value::Blob(blob.as_ref()), + ); + SQLITE_OK +} + /// Converts an optional `JObject` into Java's `LimboStepResult`. /// /// This function takes an optional `JObject` and converts it into a Java object diff --git a/bindings/java/src/main/java/org/github/tursodatabase/core/LimboResultSet.java b/bindings/java/src/main/java/org/github/tursodatabase/core/LimboResultSet.java index 1884d4786..67bc6b8ee 100644 --- a/bindings/java/src/main/java/org/github/tursodatabase/core/LimboResultSet.java +++ b/bindings/java/src/main/java/org/github/tursodatabase/core/LimboResultSet.java @@ -19,6 +19,8 @@ public class LimboResultSet { private final LimboStatement statement; + // Name of the columns + private String[] columnNames = new String[0]; // Whether the result set does not have any rows. private boolean isEmptyResultSet = false; // If the result set is open. Doesn't mean it has results. @@ -136,6 +138,14 @@ public class LimboResultSet { return resultSet[columnIndex - 1]; } + public String[] getColumnNames() { + return this.columnNames; + } + + public void setColumnNames(String[] columnNames) { + this.columnNames = columnNames; + } + @Override public String toString() { return "LimboResultSet{" diff --git a/bindings/java/src/main/java/org/github/tursodatabase/core/LimboStatement.java b/bindings/java/src/main/java/org/github/tursodatabase/core/LimboStatement.java index fa660b67c..0fdf243e0 100644 --- a/bindings/java/src/main/java/org/github/tursodatabase/core/LimboStatement.java +++ b/bindings/java/src/main/java/org/github/tursodatabase/core/LimboStatement.java @@ -89,6 +89,131 @@ public class LimboStatement { private native void _close(long statementPointer); + /** + * Initializes the column metadata, such as the names of the columns. Since {@link LimboStatement} + * can only have a single {@link LimboResultSet}, it is appropriate to place the initialization of + * column metadata here. + * + * @throws SQLException if a database access error occurs while retrieving column names + */ + public void initializeColumnMetadata() throws SQLException { + final String[] columnNames = this.columns(statementPointer); + if (columnNames != null) { + this.resultSet.setColumnNames(columnNames); + } + } + + @Nullable + 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 Result Codes + * @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 an integer value to the prepared statement at the specified position. This function calls + * bindLong because Limbo treats all integers as long (as well as SQLite). + * + *

According to SQLite documentation, the value is a signed integer, stored in 0, 1, 2, 3, 4, + * 6, or 8 bytes depending on the magnitude of the value. + * + * @param position The index of the SQL parameter to be set. + * @param value The integer value to bind to the parameter. + * @return A result code indicating the success or failure of the operation. + * @throws SQLException If a database access error occurs. + */ + public int bindInt(int position, int value) throws SQLException { + return bindLong(position, value); + } + + /** + * 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 Result Codes + * @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 Result Codes + * @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 Result Codes + * @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 Result Codes + * @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. * diff --git a/bindings/java/src/main/java/org/github/tursodatabase/jdbc4/JDBC4Connection.java b/bindings/java/src/main/java/org/github/tursodatabase/jdbc4/JDBC4Connection.java index 3c32ffaf2..d5b73e627 100644 --- a/bindings/java/src/main/java/org/github/tursodatabase/jdbc4/JDBC4Connection.java +++ b/bindings/java/src/main/java/org/github/tursodatabase/jdbc4/JDBC4Connection.java @@ -40,10 +40,8 @@ public class JDBC4Connection extends LimboConnection { } @Override - @SkipNullableCheck public PreparedStatement prepareStatement(String sql) throws SQLException { - // TODO - return null; + return new JDBC4PreparedStatement(this, sql); } @Override diff --git a/bindings/java/src/main/java/org/github/tursodatabase/jdbc4/JDBC4PreparedStatement.java b/bindings/java/src/main/java/org/github/tursodatabase/jdbc4/JDBC4PreparedStatement.java new file mode 100644 index 000000000..f109cb647 --- /dev/null +++ b/bindings/java/src/main/java/org/github/tursodatabase/jdbc4/JDBC4PreparedStatement.java @@ -0,0 +1,336 @@ +package org.github.tursodatabase.jdbc4; + +import static java.util.Objects.requireNonNull; + +import java.io.InputStream; +import java.io.Reader; +import java.math.BigDecimal; +import java.net.URL; +import java.sql.Array; +import java.sql.Blob; +import java.sql.Clob; +import java.sql.Date; +import java.sql.NClob; +import java.sql.ParameterMetaData; +import java.sql.PreparedStatement; +import java.sql.Ref; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.RowId; +import java.sql.SQLException; +import java.sql.SQLXML; +import java.sql.Time; +import java.sql.Timestamp; +import java.util.Calendar; +import org.github.tursodatabase.annotations.SkipNullableCheck; +import org.github.tursodatabase.core.LimboConnection; + +public class JDBC4PreparedStatement extends JDBC4Statement implements PreparedStatement { + + private final String sql; + + public JDBC4PreparedStatement(LimboConnection connection, String sql) throws SQLException { + super(connection); + + this.sql = sql; + this.statement = connection.prepare(sql); + this.statement.initializeColumnMetadata(); + } + + @Override + public ResultSet executeQuery() throws SQLException { + // TODO: check bindings etc + requireNonNull(this.statement); + return new JDBC4ResultSet(this.statement.getResultSet()); + } + + @Override + public int executeUpdate() throws SQLException { + // TODO + return 0; + } + + @Override + public void setNull(int parameterIndex, int sqlType) throws SQLException { + requireNonNull(this.statement); + this.statement.bindNull(parameterIndex); + } + + @Override + public void setBoolean(int parameterIndex, boolean x) throws SQLException { + requireNonNull(this.statement); + this.statement.bindInt(parameterIndex, x ? 1 : 0); + } + + @Override + public void setByte(int parameterIndex, byte x) throws SQLException { + requireNonNull(this.statement); + this.statement.bindInt(parameterIndex, x); + } + + @Override + public void setShort(int parameterIndex, short x) throws SQLException { + requireNonNull(this.statement); + this.statement.bindInt(parameterIndex, x); + } + + @Override + public void setInt(int parameterIndex, int x) throws SQLException { + requireNonNull(this.statement); + this.statement.bindInt(parameterIndex, x); + } + + @Override + public void setLong(int parameterIndex, long x) throws SQLException { + requireNonNull(this.statement); + this.statement.bindLong(parameterIndex, x); + } + + @Override + public void setFloat(int parameterIndex, float x) throws SQLException { + requireNonNull(this.statement); + this.statement.bindDouble(parameterIndex, x); + } + + @Override + public void setDouble(int parameterIndex, double x) throws SQLException { + requireNonNull(this.statement); + this.statement.bindDouble(parameterIndex, x); + } + + @Override + public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException { + requireNonNull(this.statement); + this.statement.bindText(parameterIndex, x.toString()); + } + + @Override + public void setString(int parameterIndex, String x) throws SQLException { + requireNonNull(this.statement); + this.statement.bindText(parameterIndex, x); + } + + @Override + public void setBytes(int parameterIndex, byte[] x) throws SQLException { + requireNonNull(this.statement); + this.statement.bindBlob(parameterIndex, x); + } + + @Override + public void setDate(int parameterIndex, Date x) throws SQLException { + // TODO + } + + @Override + public void setTime(int parameterIndex, Time x) throws SQLException { + // TODO + } + + @Override + public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException { + // TODO + } + + @Override + public void setAsciiStream(int parameterIndex, InputStream x, int length) throws SQLException { + // TODO + } + + @Override + public void setUnicodeStream(int parameterIndex, InputStream x, int length) throws SQLException { + // TODO + } + + @Override + public void setBinaryStream(int parameterIndex, InputStream x, int length) throws SQLException { + // TODO + } + + @Override + public void clearParameters() throws SQLException { + // TODO + } + + @Override + public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException { + // TODO + } + + @Override + public void setObject(int parameterIndex, Object x) throws SQLException { + // TODO + } + + @Override + public boolean execute() throws SQLException { + // TODO: check whether this is sufficient + requireNonNull(this.statement); + return statement.execute(); + } + + @Override + public void addBatch() throws SQLException { + // TODO + } + + @Override + public void setCharacterStream(int parameterIndex, Reader reader, int length) + throws SQLException {} + + @Override + public void setRef(int parameterIndex, Ref x) throws SQLException { + // TODO + } + + @Override + public void setBlob(int parameterIndex, Blob x) throws SQLException { + // TODO + } + + @Override + public void setClob(int parameterIndex, Clob x) throws SQLException { + // TODO + } + + @Override + public void setArray(int parameterIndex, Array x) throws SQLException { + // TODO + } + + @Override + @SkipNullableCheck + public ResultSetMetaData getMetaData() throws SQLException { + return null; + } + + @Override + public void setDate(int parameterIndex, Date x, Calendar cal) throws SQLException { + // TODO + } + + @Override + public void setTime(int parameterIndex, Time x, Calendar cal) throws SQLException { + // TODO + } + + @Override + public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal) throws SQLException { + // TODO + } + + @Override + public void setNull(int parameterIndex, int sqlType, String typeName) throws SQLException { + // TODO + } + + @Override + public void setURL(int parameterIndex, URL x) throws SQLException { + // TODO + } + + @Override + @SkipNullableCheck + public ParameterMetaData getParameterMetaData() throws SQLException { + // TODO + return null; + } + + @Override + public void setRowId(int parameterIndex, RowId x) throws SQLException { + // TODO + } + + @Override + public void setNString(int parameterIndex, String value) throws SQLException { + // TODO + } + + @Override + public void setNCharacterStream(int parameterIndex, Reader value, long length) + throws SQLException { + // TODO + } + + @Override + public void setNClob(int parameterIndex, NClob value) throws SQLException { + // TODO + } + + @Override + public void setClob(int parameterIndex, Reader reader, long length) throws SQLException { + // TODO + } + + @Override + public void setBlob(int parameterIndex, InputStream inputStream, long length) + throws SQLException { + // TODO + } + + @Override + public void setNClob(int parameterIndex, Reader reader, long length) throws SQLException { + // TODO + } + + @Override + public void setSQLXML(int parameterIndex, SQLXML xmlObject) throws SQLException { + // TODO + } + + @Override + public void setObject(int parameterIndex, Object x, int targetSqlType, int scaleOrLength) + throws SQLException { + // TODO + } + + @Override + public void setAsciiStream(int parameterIndex, InputStream x, long length) throws SQLException { + // TODO + } + + @Override + public void setBinaryStream(int parameterIndex, InputStream x, long length) throws SQLException { + // TODO + } + + @Override + public void setCharacterStream(int parameterIndex, Reader reader, long length) + throws SQLException { + // TODO + } + + @Override + public void setAsciiStream(int parameterIndex, InputStream x) throws SQLException { + // TODO + } + + @Override + public void setBinaryStream(int parameterIndex, InputStream x) throws SQLException { + // TODO + } + + @Override + public void setCharacterStream(int parameterIndex, Reader reader) throws SQLException { + // TODO + } + + @Override + public void setNCharacterStream(int parameterIndex, Reader value) throws SQLException { + // TODO + } + + @Override + public void setClob(int parameterIndex, Reader reader) throws SQLException { + // TODO + } + + @Override + public void setBlob(int parameterIndex, InputStream inputStream) throws SQLException { + // TODO + } + + @Override + public void setNClob(int parameterIndex, Reader reader) throws SQLException { + // TODO + } +} diff --git a/bindings/java/src/main/java/org/github/tursodatabase/jdbc4/JDBC4Statement.java b/bindings/java/src/main/java/org/github/tursodatabase/jdbc4/JDBC4Statement.java index 3965e3cae..729134a44 100644 --- a/bindings/java/src/main/java/org/github/tursodatabase/jdbc4/JDBC4Statement.java +++ b/bindings/java/src/main/java/org/github/tursodatabase/jdbc4/JDBC4Statement.java @@ -17,7 +17,7 @@ import org.github.tursodatabase.core.LimboStatement; public class JDBC4Statement implements Statement { private final LimboConnection connection; - @Nullable private LimboStatement statement = null; + @Nullable protected LimboStatement statement = null; // Because JDBC4Statement has different life cycle in compared to LimboStatement, let's use this // field to manage JDBC4Statement lifecycle diff --git a/bindings/java/src/test/java/org/github/tursodatabase/core/LimboStatementTest.java b/bindings/java/src/test/java/org/github/tursodatabase/core/LimboStatementTest.java index fe274b07e..cd601643c 100644 --- a/bindings/java/src/test/java/org/github/tursodatabase/core/LimboStatementTest.java +++ b/bindings/java/src/test/java/org/github/tursodatabase/core/LimboStatementTest.java @@ -1,6 +1,10 @@ package org.github.tursodatabase.core; -import static org.junit.jupiter.api.Assertions.*; +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; import org.github.tursodatabase.TestUtils; @@ -28,4 +32,97 @@ class LimboStatementTest { assertTrue(stmt.isClosed()); assertFalse(stmt.getResultSet().isOpen()); } + + @Test + void test_initializeColumnMetadata() throws Exception { + runSql("CREATE TABLE users (name TEXT, age INT, country TEXT);"); + runSql("INSERT INTO users VALUES ('seonwoo', 30, 'KR');"); + + final LimboStatement stmt = connection.prepare("SELECT * FROM users"); + stmt.initializeColumnMetadata(); + final LimboResultSet rs = stmt.getResultSet(); + final String[] columnNames = rs.getColumnNames(); + + 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 { + LimboStatement stmt = connection.prepare(sql); + while (stmt.execute()) { + stmt.execute(); + } + } } diff --git a/bindings/java/src/test/java/org/github/tursodatabase/jdbc4/JDBC4PreparedStatementTest.java b/bindings/java/src/test/java/org/github/tursodatabase/jdbc4/JDBC4PreparedStatementTest.java new file mode 100644 index 000000000..775192589 --- /dev/null +++ b/bindings/java/src/test/java/org/github/tursodatabase/jdbc4/JDBC4PreparedStatementTest.java @@ -0,0 +1,264 @@ +package org.github.tursodatabase.jdbc4; + +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.assertTrue; + +import java.math.BigDecimal; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Properties; +import org.github.tursodatabase.TestUtils; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class JDBC4PreparedStatementTest { + + private JDBC4Connection connection; + + @BeforeEach + void setUp() throws Exception { + String filePath = TestUtils.createTempFile(); + String url = "jdbc:sqlite:" + filePath; + connection = new JDBC4Connection(url, filePath, new Properties()); + } + + @Test + void testSetBoolean() throws SQLException { + connection.prepareStatement("CREATE TABLE test (col INTEGER)").execute(); + PreparedStatement stmt = + connection.prepareStatement("INSERT INTO test (col) VALUES (?), (?), (?)"); + stmt.setBoolean(1, true); + stmt.setBoolean(2, false); + stmt.setBoolean(3, true); + stmt.execute(); + + PreparedStatement stmt2 = connection.prepareStatement("SELECT * FROM test;"); + ResultSet rs = stmt2.executeQuery(); + assertTrue(rs.next()); + assertTrue(rs.getBoolean(1)); + assertTrue(rs.next()); + assertFalse(rs.getBoolean(1)); + assertTrue(rs.next()); + assertTrue(rs.getBoolean(1)); + } + + @Test + void testSetByte() throws SQLException { + connection.prepareStatement("CREATE TABLE test (col INTEGER)").execute(); + PreparedStatement stmt = + connection.prepareStatement("INSERT INTO test (col) VALUES (?), (?), (?)"); + stmt.setByte(1, (byte) 1); + stmt.setByte(2, (byte) 2); + stmt.setByte(3, (byte) 3); + stmt.execute(); + + PreparedStatement stmt2 = connection.prepareStatement("SELECT * FROM test;"); + ResultSet rs = stmt2.executeQuery(); + assertTrue(rs.next()); + assertEquals(1, rs.getByte(1)); + assertTrue(rs.next()); + assertEquals(2, rs.getByte(1)); + assertTrue(rs.next()); + assertEquals(3, rs.getByte(1)); + } + + @Test + void testSetShort() throws SQLException { + connection.prepareStatement("CREATE TABLE test (col INTEGER)").execute(); + PreparedStatement stmt = + connection.prepareStatement("INSERT INTO test (col) VALUES (?), (?), (?)"); + stmt.setShort(1, (short) 1); + stmt.setShort(2, (short) 2); + stmt.setShort(3, (short) 3); + stmt.execute(); + + PreparedStatement stmt2 = connection.prepareStatement("SELECT * FROM test;"); + ResultSet rs = stmt2.executeQuery(); + assertTrue(rs.next()); + assertEquals(1, rs.getShort(1)); + assertTrue(rs.next()); + assertEquals(2, rs.getShort(1)); + assertTrue(rs.next()); + assertEquals(3, rs.getShort(1)); + } + + @Test + void testSetInt() throws SQLException { + connection.prepareStatement("CREATE TABLE test (col INTEGER)").execute(); + PreparedStatement stmt = + connection.prepareStatement("INSERT INTO test (col) VALUES (?), (?), (?)"); + stmt.setInt(1, 1); + stmt.setInt(2, 2); + stmt.setInt(3, 3); + stmt.execute(); + + PreparedStatement stmt2 = connection.prepareStatement("SELECT * FROM test;"); + ResultSet rs = stmt2.executeQuery(); + assertTrue(rs.next()); + assertEquals(1, rs.getInt(1)); + assertTrue(rs.next()); + assertEquals(2, rs.getInt(1)); + assertTrue(rs.next()); + assertEquals(3, rs.getInt(1)); + } + + @Test + void testSetLong() throws SQLException { + connection.prepareStatement("CREATE TABLE test (col INTEGER)").execute(); + PreparedStatement stmt = + connection.prepareStatement("INSERT INTO test (col) VALUES (?), (?), (?)"); + stmt.setLong(1, 1L); + stmt.setLong(2, 2L); + stmt.setLong(3, 3L); + stmt.execute(); + + PreparedStatement stmt2 = connection.prepareStatement("SELECT * FROM test;"); + ResultSet rs = stmt2.executeQuery(); + assertTrue(rs.next()); + assertEquals(1L, rs.getLong(1)); + assertTrue(rs.next()); + assertEquals(2L, rs.getLong(1)); + assertTrue(rs.next()); + assertEquals(3L, rs.getLong(1)); + } + + @Test + void testSetFloat() throws SQLException { + connection.prepareStatement("CREATE TABLE test (col REAL)").execute(); + PreparedStatement stmt = + connection.prepareStatement("INSERT INTO test (col) VALUES (?), (?), (?)"); + stmt.setFloat(1, 1.0f); + stmt.setFloat(2, 2.0f); + stmt.setFloat(3, 3.0f); + stmt.execute(); + + PreparedStatement stmt2 = connection.prepareStatement("SELECT * FROM test;"); + ResultSet rs = stmt2.executeQuery(); + assertTrue(rs.next()); + assertEquals(1.0f, rs.getFloat(1)); + assertTrue(rs.next()); + assertEquals(2.0f, rs.getFloat(1)); + assertTrue(rs.next()); + assertEquals(3.0f, rs.getFloat(1)); + } + + @Test + void testSetDouble() throws SQLException { + connection.prepareStatement("CREATE TABLE test (col REAL)").execute(); + PreparedStatement stmt = + connection.prepareStatement("INSERT INTO test (col) VALUES (?), (?), (?)"); + stmt.setDouble(1, 1.0); + stmt.setDouble(2, 2.0); + stmt.setDouble(3, 3.0); + stmt.execute(); + + PreparedStatement stmt2 = connection.prepareStatement("SELECT * FROM test;"); + ResultSet rs = stmt2.executeQuery(); + assertTrue(rs.next()); + assertEquals(1.0, rs.getDouble(1)); + assertTrue(rs.next()); + assertEquals(2.0, rs.getDouble(1)); + assertTrue(rs.next()); + assertEquals(3.0, rs.getDouble(1)); + } + + @Test + void testSetBigDecimal() throws SQLException { + connection.prepareStatement("CREATE TABLE test (col TEXT)").execute(); + PreparedStatement stmt = + connection.prepareStatement("INSERT INTO test (col) VALUES (?), (?), (?)"); + stmt.setBigDecimal(1, new BigDecimal("1.0")); + stmt.setBigDecimal(2, new BigDecimal("2.0")); + stmt.setBigDecimal(3, new BigDecimal("3.0")); + stmt.execute(); + + PreparedStatement stmt2 = connection.prepareStatement("SELECT * FROM test;"); + ResultSet rs = stmt2.executeQuery(); + assertTrue(rs.next()); + assertEquals("1.0", rs.getString(1)); + assertTrue(rs.next()); + assertEquals("2.0", rs.getString(1)); + assertTrue(rs.next()); + assertEquals("3.0", rs.getString(1)); + } + + @Test + void testSetString() throws SQLException { + connection.prepareStatement("CREATE TABLE test (col TEXT)").execute(); + PreparedStatement stmt = + connection.prepareStatement("INSERT INTO test (col) VALUES (?), (?), (?)"); + stmt.setString(1, "test1"); + stmt.setString(2, "test2"); + stmt.setString(3, "test3"); + stmt.execute(); + + PreparedStatement stmt2 = connection.prepareStatement("SELECT * FROM test;"); + ResultSet rs = stmt2.executeQuery(); + assertTrue(rs.next()); + assertEquals("test1", rs.getString(1)); + assertTrue(rs.next()); + assertEquals("test2", rs.getString(1)); + assertTrue(rs.next()); + assertEquals("test3", rs.getString(1)); + } + + @Test + void testSetBytes() throws SQLException { + connection.prepareStatement("CREATE TABLE test (col BLOB)").execute(); + PreparedStatement stmt = + connection.prepareStatement("INSERT INTO test (col) VALUES (?), (?), (?)"); + stmt.setBytes(1, new byte[] {1, 2, 3}); + stmt.setBytes(2, new byte[] {4, 5, 6}); + stmt.setBytes(3, new byte[] {7, 8, 9}); + stmt.execute(); + + PreparedStatement stmt2 = connection.prepareStatement("SELECT * FROM test;"); + ResultSet rs = stmt2.executeQuery(); + assertTrue(rs.next()); + assertArrayEquals(new byte[] {1, 2, 3}, rs.getBytes(1)); + assertTrue(rs.next()); + assertArrayEquals(new byte[] {4, 5, 6}, rs.getBytes(1)); + assertTrue(rs.next()); + assertArrayEquals(new byte[] {7, 8, 9}, rs.getBytes(1)); + } + + @Test + void testInsertMultipleTypes() throws SQLException { + connection + .prepareStatement("CREATE TABLE test (col1 INTEGER, col2 REAL, col3 TEXT, col4 BLOB)") + .execute(); + PreparedStatement stmt = + connection.prepareStatement( + "INSERT INTO test (col1, col2, col3, col4) VALUES (?, ?, ?, ?), (?, ?, ?, ?)"); + + stmt.setInt(1, 1); + stmt.setFloat(2, 1.1f); + stmt.setString(3, "row1"); + stmt.setBytes(4, new byte[] {1, 2, 3}); + + stmt.setInt(5, 2); + stmt.setFloat(6, 2.2f); + stmt.setString(7, "row2"); + stmt.setBytes(8, new byte[] {4, 5, 6}); + + stmt.execute(); + + PreparedStatement stmt2 = connection.prepareStatement("SELECT * FROM test;"); + ResultSet rs = stmt2.executeQuery(); + + assertTrue(rs.next()); + assertEquals(1, rs.getInt(1)); + assertEquals(1.1f, rs.getFloat(2)); + assertEquals("row1", rs.getString(3)); + assertArrayEquals(new byte[] {1, 2, 3}, rs.getBytes(4)); + + assertTrue(rs.next()); + assertEquals(2, rs.getInt(1)); + assertEquals(2.2f, rs.getFloat(2)); + assertEquals("row2", rs.getString(3)); + assertArrayEquals(new byte[] {4, 5, 6}, rs.getBytes(4)); + } +}