Remove AbstractDB and move those methods into LimboDB

This commit is contained in:
김선우
2025-02-09 18:40:42 +09:00
parent e406a030e6
commit d51c1dc5b1
4 changed files with 49 additions and 98 deletions

View File

@@ -1,75 +0,0 @@
package org.github.tursodatabase.core;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* Interface to Limbo. It provides some helper functions used by other parts of the driver. The goal
* of the helper functions here are not only to provide functionality, but to handle contractual
* differences between the JDBC specification and the Limbo API.
*/
public abstract class AbstractDB {
protected final String url;
protected final String filePath;
private final AtomicBoolean closed = new AtomicBoolean(true);
public AbstractDB(String url, String filePath) {
this.url = url;
this.filePath = filePath;
}
public boolean isClosed() {
return closed.get();
}
/** Aborts any pending operation and returns at its earliest opportunity. */
public abstract void interrupt() throws SQLException;
/**
* Creates an SQLite interface to a database for the given connection.
*
* @param openFlags Flags for opening the database.
* @throws SQLException if a database access error occurs.
*/
public final synchronized void open(int openFlags) throws SQLException {
open0(filePath, openFlags);
}
protected abstract void open0(String fileName, int openFlags) throws SQLException;
/**
* Closes a database connection and finalizes any remaining statements before the closing
* operation.
*
* @throws SQLException if a database access error occurs.
*/
public final synchronized void close() throws SQLException {
// TODO: add implementation
throw new SQLFeatureNotSupportedException();
}
/**
* Connects to a database.
*
* @return Pointer to the connection.
*/
public abstract long connect() throws SQLException;
/**
* Creates an SQLite interface to a database with the provided open flags.
*
* @param fileName The database to open.
* @param openFlags Flags for opening the database.
* @return pointer to database instance
* @throws SQLException if a database access error occurs.
*/
protected abstract long openUtf8(byte[] fileName, int openFlags) throws SQLException;
/**
* Closes the SQLite interface to a database.
*
* @throws SQLException if a database access error occurs.
*/
protected abstract void close0() throws SQLException;
}

View File

@@ -15,7 +15,7 @@ public abstract class LimboConnection implements Connection {
private static final Logger logger = LoggerFactory.getLogger(LimboConnection.class);
private final long connectionPtr;
private final AbstractDB database;
private final LimboDB database;
private boolean closed;
public LimboConnection(String url, String filePath) throws SQLException {
@@ -33,7 +33,7 @@ public abstract class LimboConnection implements Connection {
this.connectionPtr = this.database.connect();
}
private static AbstractDB open(String url, String filePath, Properties properties)
private static LimboDB open(String url, String filePath, Properties properties)
throws SQLException {
return LimboDBFactory.open(url, filePath, properties);
}
@@ -58,7 +58,7 @@ public abstract class LimboConnection implements Connection {
return closed;
}
public AbstractDB getDatabase() {
public LimboDB getDatabase() {
return database;
}

View File

@@ -7,7 +7,6 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.concurrent.locks.ReentrantLock;
import org.github.tursodatabase.LimboErrorCode;
import org.github.tursodatabase.annotations.NativeInvocation;
import org.github.tursodatabase.annotations.VisibleForTesting;
@@ -16,14 +15,15 @@ import org.github.tursodatabase.utils.Logger;
import org.github.tursodatabase.utils.LoggerFactory;
/** This class provides a thin JNI layer over the SQLite3 C API. */
public final class LimboDB extends AbstractDB {
public final class LimboDB implements AutoCloseable {
private static final Logger logger = LoggerFactory.getLogger(LimboDB.class);
// Pointer to database instance
private long dbPointer;
private boolean isOpen;
private final String url;
private final String filePath;
private static boolean isLoaded;
private ReentrantLock dbLock = new ReentrantLock();
static {
if ("The Android Project".equals(System.getProperty("java.vm.vendor"))) {
@@ -176,23 +176,26 @@ public final class LimboDB extends AbstractDB {
// TODO: receive config as argument
private LimboDB(String url, String filePath) {
super(url, filePath);
this.url = url;
this.filePath = filePath;
}
// TODO: add support for JNI
@Override
protected native long openUtf8(byte[] file, int openFlags) throws SQLException;
// TODO: add support for JNI
@Override
protected native void close0() throws SQLException;
// TODO: add support for JNI
@Override
public native void interrupt();
@Override
protected void open0(String filePath, int openFlags) throws SQLException {
public boolean isClosed() {
return !this.isOpen;
}
public boolean isOpen() {
return this.isOpen;
}
public void open(int openFlags) throws SQLException {
open0(filePath, openFlags);
}
private void open0(String filePath, int openFlags) throws SQLException {
if (isOpen) {
throw LimboExceptionUtils.buildLimboException(
LimboErrorCode.LIMBO_ETC.code, "Already opened");
@@ -209,13 +212,24 @@ public final class LimboDB extends AbstractDB {
isOpen = true;
}
@Override
private native long openUtf8(byte[] file, int openFlags) throws SQLException;
public long connect() throws SQLException {
return connect0(dbPointer);
}
private native long connect0(long databasePtr) throws SQLException;
@Override
public void close() throws Exception {
if (!isOpen) return;
close0(dbPointer);
isOpen = false;
}
private native void close0(long databasePtr) throws SQLException;
@VisibleForTesting
native void throwJavaException(int errorCode) throws SQLException;

View File

@@ -2,6 +2,7 @@ package org.github.tursodatabase.core;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertFalse;
import java.sql.SQLException;
import org.github.tursodatabase.LimboErrorCode;
@@ -13,16 +14,27 @@ public class LimboDBTest {
@Test
void db_should_open_normally() throws Exception {
String dbPath = TestUtils.createTempFile();
LimboDB.load();
String dbPath = TestUtils.createTempFile();
LimboDB db = LimboDB.create("jdbc:sqlite" + dbPath, dbPath);
db.open(0);
}
@Test
void should_throw_exception_when_opened_twice() throws Exception {
String dbPath = TestUtils.createTempFile();
void db_should_close_normally() throws Exception {
LimboDB.load();
String dbPath = TestUtils.createTempFile();
LimboDB db = LimboDB.create("jdbc:sqlite" + dbPath, dbPath);
db.open(0);
db.close();
assertFalse(db.isOpen());
}
@Test
void should_throw_exception_when_opened_twice() throws Exception {
LimboDB.load();
String dbPath = TestUtils.createTempFile();
LimboDB db = LimboDB.create("jdbc:sqlite:" + dbPath, dbPath);
db.open(0);
@@ -31,8 +43,8 @@ public class LimboDBTest {
@Test
void throwJavaException_should_throw_appropriate_java_exception() throws Exception {
String dbPath = TestUtils.createTempFile();
LimboDB.load();
String dbPath = TestUtils.createTempFile();
LimboDB db = LimboDB.create("jdbc:sqlite:" + dbPath, dbPath);
final int limboExceptionCode = LimboErrorCode.LIMBO_ETC.code;