mirror of
https://github.com/aljazceru/turso.git
synced 2025-12-25 20:14:21 +01:00
Initial pass on step function
This commit is contained in:
@@ -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 filePath path to file
|
||||
|
||||
@@ -4,7 +4,6 @@ package org.github.tursodatabase.core;
|
||||
import org.github.tursodatabase.LimboErrorCode;
|
||||
import org.github.tursodatabase.annotations.NativeInvocation;
|
||||
import org.github.tursodatabase.annotations.VisibleForTesting;
|
||||
import org.github.tursodatabase.utils.ByteArrayUtils;
|
||||
import org.github.tursodatabase.utils.LimboExceptionUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
package org.github.tursodatabase.core;
|
||||
|
||||
import org.github.tursodatabase.annotations.NativeInvocation;
|
||||
import org.github.tursodatabase.annotations.Nullable;
|
||||
import org.github.tursodatabase.jdbc4.JDBC4ResultSet;
|
||||
import org.github.tursodatabase.utils.LimboExceptionUtils;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class LimboStatement {
|
||||
|
||||
@@ -30,20 +34,33 @@ public abstract class LimboStatement {
|
||||
// TODO
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls sqlite3_step() and sets up results.
|
||||
*
|
||||
* @return true if the ResultSet has at least one row; false otherwise;
|
||||
* @throws SQLException If the given SQL statement is nul or no database is open;
|
||||
*/
|
||||
protected boolean exec(long stmtPointer) throws SQLException {
|
||||
if (sql == null) throw new SQLException("SQL must not be null");
|
||||
// TODO: enhance
|
||||
protected List<Object[]> execute(long stmtPointer) throws SQLException {
|
||||
List<Object[]> result = new ArrayList<>();
|
||||
while (true) {
|
||||
Object[] stepResult = step(stmtPointer);
|
||||
if (stepResult != null) {
|
||||
for (int i = 0; i < stepResult.length; i++) {
|
||||
System.out.println("stepResult" + i + ": " + stepResult[i]);
|
||||
}
|
||||
}
|
||||
if (stepResult == null) break;
|
||||
result.add(stepResult);
|
||||
}
|
||||
|
||||
// TODO
|
||||
return true;
|
||||
return result;
|
||||
}
|
||||
|
||||
protected void step(long stmtPointer) throws SQLException {
|
||||
// TODO
|
||||
private native Object[] step(long stmtPointer) throws SQLException;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
import org.github.tursodatabase.annotations.SkipNullableCheck;
|
||||
import org.github.tursodatabase.core.LimboStatement;
|
||||
import org.github.tursodatabase.core.LimboConnection;
|
||||
import org.github.tursodatabase.core.LimboStatement;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
/**
|
||||
@@ -128,11 +130,10 @@ public class JDBC4Statement extends LimboStatement implements Statement {
|
||||
try {
|
||||
connectionLock.lock();
|
||||
final long stmtPointer = connection.prepare(sql);
|
||||
boolean result = exec(stmtPointer);
|
||||
List<Object[]> result = execute(stmtPointer);
|
||||
updateGeneratedKeys();
|
||||
// TODO: updateCount = connection.changes();
|
||||
exhaustedResults = false;
|
||||
return result;
|
||||
return !result.isEmpty();
|
||||
} finally {
|
||||
connectionLock.unlock();
|
||||
}
|
||||
|
||||
@@ -64,7 +64,9 @@ class JDBC4ConnectionTest {
|
||||
@Test
|
||||
void exec_simple_create_table() throws Exception {
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user