mirror of
https://github.com/aljazceru/turso.git
synced 2026-01-29 04:44:23 +01:00
Pass io to statement from db
This commit is contained in:
@@ -9,16 +9,19 @@ use jni::sys::jlong;
|
||||
use jni::JNIEnv;
|
||||
use limbo_core::Connection;
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Clone)]
|
||||
#[allow(dead_code)]
|
||||
pub struct LimboConnection {
|
||||
// Because java's LimboConnection is 1:1 mapped to limbo connection, we can use Rc
|
||||
pub(crate) conn: Rc<Connection>,
|
||||
pub(crate) io: Rc<dyn limbo_core::IO>,
|
||||
// Because io is shared across multiple `LimboConnection`s, wrap it with Arc
|
||||
pub(crate) io: Arc<dyn limbo_core::IO>,
|
||||
}
|
||||
|
||||
impl LimboConnection {
|
||||
pub fn new(conn: Rc<Connection>, io: Rc<dyn limbo_core::IO>) -> Self {
|
||||
pub fn new(conn: Rc<Connection>, io: Arc<dyn limbo_core::IO>) -> Self {
|
||||
LimboConnection { conn, io }
|
||||
}
|
||||
|
||||
|
||||
@@ -5,16 +5,16 @@ use jni::objects::{JByteArray, JObject};
|
||||
use jni::sys::{jint, jlong};
|
||||
use jni::JNIEnv;
|
||||
use limbo_core::Database;
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
|
||||
struct LimboDB {
|
||||
db: Arc<Database>,
|
||||
io: Arc<dyn limbo_core::IO>,
|
||||
}
|
||||
|
||||
impl LimboDB {
|
||||
pub fn new(db: Arc<Database>) -> Self {
|
||||
LimboDB { db }
|
||||
pub fn new(db: Arc<Database>, io: Arc<dyn limbo_core::IO>) -> Self {
|
||||
LimboDB { db, io }
|
||||
}
|
||||
|
||||
pub fn to_ptr(self) -> jlong {
|
||||
@@ -76,14 +76,13 @@ pub extern "system" fn Java_org_github_tursodatabase_core_LimboDB_openUtf8<'loca
|
||||
}
|
||||
};
|
||||
|
||||
LimboDB::new(db).to_ptr()
|
||||
LimboDB::new(db, io).to_ptr()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "system" fn Java_org_github_tursodatabase_core_LimboDB_connect0<'local>(
|
||||
mut env: JNIEnv<'local>,
|
||||
obj: JObject<'local>,
|
||||
file_path_byte_arr: JByteArray<'local>,
|
||||
db_pointer: jlong,
|
||||
) -> jlong {
|
||||
let db = match to_limbo_db(db_pointer) {
|
||||
@@ -94,41 +93,7 @@ pub extern "system" fn Java_org_github_tursodatabase_core_LimboDB_connect0<'loca
|
||||
}
|
||||
};
|
||||
|
||||
let path = match env
|
||||
.convert_byte_array(file_path_byte_arr)
|
||||
.map_err(|e| e.to_string())
|
||||
{
|
||||
Ok(bytes) => match String::from_utf8(bytes) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
set_err_msg_and_throw_exception(&mut env, obj, LIMBO_ETC, e.to_string());
|
||||
return 0;
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
set_err_msg_and_throw_exception(&mut env, obj, LIMBO_ETC, e.to_string());
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
let io: Rc<dyn limbo_core::IO> = match path.as_str() {
|
||||
":memory:" => match limbo_core::MemoryIO::new() {
|
||||
Ok(io) => Rc::new(io),
|
||||
Err(e) => {
|
||||
set_err_msg_and_throw_exception(&mut env, obj, LIMBO_ETC, e.to_string());
|
||||
return 0;
|
||||
}
|
||||
},
|
||||
_ => match limbo_core::PlatformIO::new() {
|
||||
Ok(io) => Rc::new(io),
|
||||
Err(e) => {
|
||||
set_err_msg_and_throw_exception(&mut env, obj, LIMBO_ETC, e.to_string());
|
||||
return 0;
|
||||
}
|
||||
},
|
||||
};
|
||||
let conn = LimboConnection::new(db.db.connect(), io);
|
||||
|
||||
let conn = LimboConnection::new(db.db.connect(), db.io.clone());
|
||||
conn.to_ptr()
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
package org.github.tursodatabase.core;
|
||||
|
||||
import static org.github.tursodatabase.utils.ByteArrayUtils.stringToUtf8ByteArray;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
import org.github.tursodatabase.LimboErrorCode;
|
||||
import org.github.tursodatabase.annotations.NativeInvocation;
|
||||
@@ -8,12 +12,6 @@ import org.github.tursodatabase.utils.LimboExceptionUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.sql.SQLFeatureNotSupportedException;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
import static org.github.tursodatabase.utils.ByteArrayUtils.stringToUtf8ByteArray;
|
||||
|
||||
/**
|
||||
* This class provides a thin JNI layer over the SQLite3 C API.
|
||||
*/
|
||||
@@ -39,7 +37,7 @@ public final class LimboDB extends AbstractDB {
|
||||
* Loads the SQLite interface backend.
|
||||
*/
|
||||
public static void load() {
|
||||
if (isLoaded) return;
|
||||
if (isLoaded) {return;}
|
||||
|
||||
try {
|
||||
System.loadLibrary("_limbo_java");
|
||||
@@ -49,7 +47,7 @@ public final class LimboDB extends AbstractDB {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param url e.g. "jdbc:sqlite:fileName
|
||||
* @param url e.g. "jdbc:sqlite:fileName
|
||||
* @param filePath e.g. path to file
|
||||
*/
|
||||
public static LimboDB create(String url, String filePath) throws SQLException {
|
||||
@@ -86,7 +84,9 @@ public final class LimboDB extends AbstractDB {
|
||||
|
||||
byte[] filePathBytes = stringToUtf8ByteArray(filePath);
|
||||
if (filePathBytes == null) {
|
||||
throw LimboExceptionUtils.buildLimboException(LimboErrorCode.LIMBO_ETC.code, "File path cannot be converted to byteArray. File name: " + filePath);
|
||||
throw LimboExceptionUtils.buildLimboException(
|
||||
LimboErrorCode.LIMBO_ETC.code,
|
||||
"File path cannot be converted to byteArray. File name: " + filePath);
|
||||
}
|
||||
|
||||
dbPointer = openUtf8(filePathBytes, openFlags);
|
||||
@@ -95,14 +95,10 @@ public final class LimboDB extends AbstractDB {
|
||||
|
||||
@Override
|
||||
public long connect() throws SQLException {
|
||||
byte[] filePathBytes = stringToUtf8ByteArray(filePath);
|
||||
if (filePathBytes == null) {
|
||||
throw LimboExceptionUtils.buildLimboException(LimboErrorCode.LIMBO_ETC.code, "File path cannot be converted to byteArray. File name: " + filePath);
|
||||
}
|
||||
return connect0(filePathBytes, dbPointer);
|
||||
return connect0(dbPointer);
|
||||
}
|
||||
|
||||
private native long connect0(byte[] path, long databasePtr) throws SQLException;
|
||||
private native long connect0(long databasePtr) throws SQLException;
|
||||
|
||||
@VisibleForTesting
|
||||
native void throwJavaException(int errorCode) throws SQLException;
|
||||
@@ -110,7 +106,7 @@ public final class LimboDB extends AbstractDB {
|
||||
/**
|
||||
* Throws formatted SQLException with error code and message.
|
||||
*
|
||||
* @param errorCode Error code.
|
||||
* @param errorCode Error code.
|
||||
* @param errorMessageBytes Error message.
|
||||
*/
|
||||
@NativeInvocation(invokedFrom = "limbo_db.rs")
|
||||
|
||||
Reference in New Issue
Block a user