mirror of
https://github.com/aljazceru/turso.git
synced 2025-12-24 03:34:18 +01:00
Initial pass on step function
This commit is contained in:
@@ -2,21 +2,25 @@ use crate::errors::{
|
|||||||
LimboError, Result, LIMBO_ETC, LIMBO_FAILED_TO_PARSE_BYTE_ARRAY,
|
LimboError, Result, LIMBO_ETC, LIMBO_FAILED_TO_PARSE_BYTE_ARRAY,
|
||||||
LIMBO_FAILED_TO_PREPARE_STATEMENT,
|
LIMBO_FAILED_TO_PREPARE_STATEMENT,
|
||||||
};
|
};
|
||||||
use crate::limbo_statement::CoreStatement;
|
use crate::limbo_statement::LimboStatement;
|
||||||
use crate::utils::{set_err_msg_and_throw_exception, utf8_byte_arr_to_str};
|
use crate::utils::{set_err_msg_and_throw_exception, utf8_byte_arr_to_str};
|
||||||
use jni::objects::{JByteArray, JClass, JObject};
|
use jni::objects::{JByteArray, JObject};
|
||||||
use jni::sys::jlong;
|
use jni::sys::jlong;
|
||||||
use jni::JNIEnv;
|
use jni::JNIEnv;
|
||||||
|
use limbo_core::Connection;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct LimboConnection {
|
pub struct LimboConnection {
|
||||||
pub(crate) conn: Rc<limbo_core::Connection>,
|
pub(crate) conn: Rc<Connection>,
|
||||||
pub(crate) io: Rc<dyn limbo_core::IO>,
|
pub(crate) io: Rc<dyn limbo_core::IO>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LimboConnection {
|
impl LimboConnection {
|
||||||
|
pub fn new(conn: Rc<Connection>, io: Rc<dyn limbo_core::IO>) -> Self {
|
||||||
|
LimboConnection { conn, io }
|
||||||
|
}
|
||||||
|
|
||||||
pub fn to_ptr(self) -> jlong {
|
pub fn to_ptr(self) -> jlong {
|
||||||
Box::into_raw(Box::new(self)) as jlong
|
Box::into_raw(Box::new(self)) as jlong
|
||||||
}
|
}
|
||||||
@@ -78,7 +82,7 @@ pub extern "system" fn Java_org_github_tursodatabase_core_LimboConnection_prepar
|
|||||||
};
|
};
|
||||||
|
|
||||||
match connection.conn.prepare(sql) {
|
match connection.conn.prepare(sql) {
|
||||||
Ok(stmt) => CoreStatement::new(stmt).to_ptr(),
|
Ok(stmt) => LimboStatement::new(stmt).to_ptr(),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
set_err_msg_and_throw_exception(
|
set_err_msg_and_throw_exception(
|
||||||
&mut env,
|
&mut env,
|
||||||
|
|||||||
@@ -127,10 +127,10 @@ pub extern "system" fn Java_org_github_tursodatabase_core_LimboDB_connect0<'loca
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
let conn = LimboConnection {
|
let conn = LimboConnection::new(
|
||||||
conn: db.db.connect(),
|
db.db.connect(),
|
||||||
io,
|
io,
|
||||||
};
|
);
|
||||||
|
|
||||||
conn.to_ptr()
|
conn.to_ptr()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,34 +1,73 @@
|
|||||||
use crate::errors::LimboError;
|
|
||||||
use crate::errors::Result;
|
use crate::errors::Result;
|
||||||
|
use crate::errors::{LimboError, LIMBO_ETC};
|
||||||
|
use crate::utils::set_err_msg_and_throw_exception;
|
||||||
use jni::objects::{JObject, JValue};
|
use jni::objects::{JObject, JValue};
|
||||||
use jni::sys::jlong;
|
use jni::sys::jlong;
|
||||||
use jni::JNIEnv;
|
use jni::JNIEnv;
|
||||||
use limbo_core::Statement;
|
use limbo_core::{Statement, StepResult};
|
||||||
|
|
||||||
pub struct CoreStatement {
|
pub struct LimboStatement {
|
||||||
pub(crate) stmt: Statement,
|
pub(crate) stmt: Statement,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CoreStatement {
|
impl LimboStatement {
|
||||||
|
pub fn new(stmt: Statement) -> Self {
|
||||||
|
LimboStatement { stmt }
|
||||||
|
}
|
||||||
|
|
||||||
pub fn to_ptr(self) -> jlong {
|
pub fn to_ptr(self) -> jlong {
|
||||||
Box::into_raw(Box::new(self)) as jlong
|
Box::into_raw(Box::new(self)) as jlong
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(stmt: Statement) -> Self {
|
|
||||||
CoreStatement { stmt }
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn drop(ptr: jlong) {
|
pub fn drop(ptr: jlong) {
|
||||||
let _boxed = unsafe { Box::from_raw(ptr as *mut CoreStatement) };
|
let _boxed = unsafe { Box::from_raw(ptr as *mut LimboStatement) };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_statement(ptr: jlong) -> Result<&'static mut CoreStatement> {
|
pub fn to_limbo_statement(ptr: jlong) -> Result<&'static mut LimboStatement> {
|
||||||
if ptr == 0 {
|
if ptr == 0 {
|
||||||
Err(LimboError::InvalidConnectionPointer)
|
Err(LimboError::InvalidConnectionPointer)
|
||||||
} else {
|
} else {
|
||||||
unsafe { Ok(&mut *(ptr as *mut CoreStatement)) }
|
unsafe { Ok(&mut *(ptr as *mut LimboStatement)) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "system" fn Java_org_github_tursodatabase_core_LimboStatement_step<'local>(
|
||||||
|
mut env: JNIEnv<'local>,
|
||||||
|
obj: JObject<'local>,
|
||||||
|
stmt_ptr: jlong,
|
||||||
|
) -> JObject<'local> {
|
||||||
|
println!("statement pointer: {:?}", stmt_ptr);
|
||||||
|
let stmt = match to_limbo_statement(stmt_ptr) {
|
||||||
|
Ok(stmt) => stmt,
|
||||||
|
Err(e) => {
|
||||||
|
println!("error occurred");
|
||||||
|
set_err_msg_and_throw_exception(&mut env, obj, LIMBO_ETC, e.to_string());
|
||||||
|
|
||||||
|
return JObject::null();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
match stmt.stmt.step() {
|
||||||
|
Ok(StepResult::Row(row)) => match row_to_obj_array(&mut env, &row) {
|
||||||
|
Ok(row) => row,
|
||||||
|
Err(e) => {
|
||||||
|
set_err_msg_and_throw_exception(&mut env, obj, LIMBO_ETC, e.to_string());
|
||||||
|
|
||||||
|
JObject::null()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Ok(StepResult::IO) => match env.new_object_array(0, "java/lang/Object", JObject::null()) {
|
||||||
|
Ok(row) => row.into(),
|
||||||
|
Err(e) => {
|
||||||
|
set_err_msg_and_throw_exception(&mut env, obj, LIMBO_ETC, e.to_string());
|
||||||
|
|
||||||
|
JObject::null()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => JObject::null(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ public abstract class LimboConnection implements Connection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a connection to limbo database.
|
* Creates a connection to limbo database
|
||||||
*
|
*
|
||||||
* @param url e.g. "jdbc:sqlite:fileName"
|
* @param url e.g. "jdbc:sqlite:fileName"
|
||||||
* @param filePath path to file
|
* @param filePath path to file
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ package org.github.tursodatabase.core;
|
|||||||
import org.github.tursodatabase.LimboErrorCode;
|
import org.github.tursodatabase.LimboErrorCode;
|
||||||
import org.github.tursodatabase.annotations.NativeInvocation;
|
import org.github.tursodatabase.annotations.NativeInvocation;
|
||||||
import org.github.tursodatabase.annotations.VisibleForTesting;
|
import org.github.tursodatabase.annotations.VisibleForTesting;
|
||||||
import org.github.tursodatabase.utils.ByteArrayUtils;
|
|
||||||
import org.github.tursodatabase.utils.LimboExceptionUtils;
|
import org.github.tursodatabase.utils.LimboExceptionUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|||||||
@@ -1,9 +1,13 @@
|
|||||||
package org.github.tursodatabase.core;
|
package org.github.tursodatabase.core;
|
||||||
|
|
||||||
|
import org.github.tursodatabase.annotations.NativeInvocation;
|
||||||
import org.github.tursodatabase.annotations.Nullable;
|
import org.github.tursodatabase.annotations.Nullable;
|
||||||
import org.github.tursodatabase.jdbc4.JDBC4ResultSet;
|
import org.github.tursodatabase.jdbc4.JDBC4ResultSet;
|
||||||
|
import org.github.tursodatabase.utils.LimboExceptionUtils;
|
||||||
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public abstract class LimboStatement {
|
public abstract class LimboStatement {
|
||||||
|
|
||||||
@@ -30,20 +34,33 @@ public abstract class LimboStatement {
|
|||||||
// TODO
|
// TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// TODO: enhance
|
||||||
* Calls sqlite3_step() and sets up results.
|
protected List<Object[]> execute(long stmtPointer) throws SQLException {
|
||||||
*
|
List<Object[]> result = new ArrayList<>();
|
||||||
* @return true if the ResultSet has at least one row; false otherwise;
|
while (true) {
|
||||||
* @throws SQLException If the given SQL statement is nul or no database is open;
|
Object[] stepResult = step(stmtPointer);
|
||||||
*/
|
if (stepResult != null) {
|
||||||
protected boolean exec(long stmtPointer) throws SQLException {
|
for (int i = 0; i < stepResult.length; i++) {
|
||||||
if (sql == null) throw new SQLException("SQL must not be null");
|
System.out.println("stepResult" + i + ": " + stepResult[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (stepResult == null) break;
|
||||||
|
result.add(stepResult);
|
||||||
|
}
|
||||||
|
|
||||||
// TODO
|
return result;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void step(long stmtPointer) throws SQLException {
|
private native Object[] step(long stmtPointer) throws SQLException;
|
||||||
// TODO
|
|
||||||
|
/**
|
||||||
|
* Throws formatted SQLException with error code and message.
|
||||||
|
*
|
||||||
|
* @param errorCode Error code.
|
||||||
|
* @param errorMessageBytes Error message.
|
||||||
|
*/
|
||||||
|
@NativeInvocation
|
||||||
|
private void throwLimboException(int errorCode, byte[] errorMessageBytes) throws SQLException {
|
||||||
|
LimboExceptionUtils.throwLimboException(errorCode, errorMessageBytes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
package org.github.tursodatabase.jdbc4;
|
package org.github.tursodatabase.jdbc4;
|
||||||
|
|
||||||
import org.github.tursodatabase.annotations.SkipNullableCheck;
|
import org.github.tursodatabase.annotations.SkipNullableCheck;
|
||||||
import org.github.tursodatabase.core.LimboStatement;
|
|
||||||
import org.github.tursodatabase.core.LimboConnection;
|
import org.github.tursodatabase.core.LimboConnection;
|
||||||
|
import org.github.tursodatabase.core.LimboStatement;
|
||||||
|
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -128,11 +130,10 @@ public class JDBC4Statement extends LimboStatement implements Statement {
|
|||||||
try {
|
try {
|
||||||
connectionLock.lock();
|
connectionLock.lock();
|
||||||
final long stmtPointer = connection.prepare(sql);
|
final long stmtPointer = connection.prepare(sql);
|
||||||
boolean result = exec(stmtPointer);
|
List<Object[]> result = execute(stmtPointer);
|
||||||
updateGeneratedKeys();
|
updateGeneratedKeys();
|
||||||
// TODO: updateCount = connection.changes();
|
|
||||||
exhaustedResults = false;
|
exhaustedResults = false;
|
||||||
return result;
|
return !result.isEmpty();
|
||||||
} finally {
|
} finally {
|
||||||
connectionLock.unlock();
|
connectionLock.unlock();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,7 +64,9 @@ class JDBC4ConnectionTest {
|
|||||||
@Test
|
@Test
|
||||||
void exec_simple_create_table() throws Exception {
|
void exec_simple_create_table() throws Exception {
|
||||||
Statement stmt = createDefaultStatement();
|
Statement stmt = createDefaultStatement();
|
||||||
stmt.execute("CREATE TABLE users (id PRIMARY KEY INT, username TEXT)");
|
stmt.execute("CREATE TABLE users (id INT PRIMARY KEY, username TEXT);");
|
||||||
|
stmt.execute("INSERT INTO users VALUES (1, 'seonwoo');");
|
||||||
|
stmt.execute("SELECT * FROM users;");
|
||||||
}
|
}
|
||||||
|
|
||||||
private Statement createDefaultStatement() throws SQLException {
|
private Statement createDefaultStatement() throws SQLException {
|
||||||
|
|||||||
Reference in New Issue
Block a user