Add support Java bindings

This add support for Java bindings in the bindings/java directory.
This commit is contained in:
김선우
2025-01-01 21:53:50 +09:00
committed by Pekka Enberg
parent 1c2e074c93
commit 370e1ca5c2
22 changed files with 1253 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
package org.github.tursodatabase;
import org.github.tursodatabase.limbo.Connection;
import org.github.tursodatabase.limbo.Cursor;
import org.github.tursodatabase.limbo.Limbo;
public class Main {
public static void main(String[] args) throws Exception {
Limbo limbo = Limbo.create();
Connection connection = limbo.getConnection("database.db");
Cursor cursor = connection.cursor();
cursor.execute("SELECT * FROM example_table;");
System.out.println("result: " + cursor.fetchOne());
}
}

View File

@@ -0,0 +1,12 @@
package org.github.tursodatabase.exceptions;
/**
* This class defines error codes that correspond to specific error conditions
* that may occur while communicating with the JNI.
* <p />
* Refer to ErrorCode in rust package.
*/
public class ErrorCode {
public static int CONNECTION_FAILURE = -1;
}

View File

@@ -0,0 +1,66 @@
package org.github.tursodatabase.limbo;
import java.lang.Exception;
/**
* Represents a connection to the database.
*/
public class Connection {
// Pointer to the connection object
private final long connectionPtr;
public Connection(long connectionPtr) {
this.connectionPtr = connectionPtr;
}
/**
* Creates a new cursor object using this connection.
*
* @return A new Cursor object.
* @throws Exception If the cursor cannot be created.
*/
public Cursor cursor() throws Exception {
long cursorId = cursor(connectionPtr);
return new Cursor(cursorId);
}
private native long cursor(long connectionPtr);
/**
* Closes the connection to the database.
*
* @throws Exception If there is an error closing the connection.
*/
public void close() throws Exception {
close(connectionPtr);
}
private native void close(long connectionPtr);
/**
* Commits the current transaction.
*
* @throws Exception If there is an error during commit.
*/
public void commit() throws Exception {
try {
commit(connectionPtr);
} catch (Exception e) {
System.out.println("caught exception: " + e);
}
}
private native void commit(long connectionPtr) throws Exception;
/**
* Rolls back the current transaction.
*
* @throws Exception If there is an error during rollback.
*/
public void rollback() throws Exception {
rollback(connectionPtr);
}
private native void rollback(long connectionPtr) throws Exception;
}

View File

@@ -0,0 +1,85 @@
package org.github.tursodatabase.limbo;
/**
* Represents a database cursor.
*/
public class Cursor {
private long cursorPtr;
public Cursor(long cursorPtr) {
this.cursorPtr = cursorPtr;
}
// TODO: support parameters
public Cursor execute(String sql) {
var result = execute(cursorPtr, sql);
System.out.println("resut: " + result);
return this;
}
private static native int execute(long cursorPtr, String sql);
public Object fetchOne() throws Exception {
Object result = fetchOne(cursorPtr);
return processSingleResult(result);
}
private static native Object fetchOne(long cursorPtr);
public Object fetchAll() throws Exception {
Object result = fetchAll(cursorPtr);
return processArrayResult(result);
}
private static native Object fetchAll(long cursorPtr);
private Object processSingleResult(Object result) throws Exception {
if (result instanceof Object[]) {
System.out.println("The result is of type: Object[]");
for (Object element : (Object[]) result) {
printElementType(element);
}
return result;
} else {
printElementType(result);
return result;
}
}
private Object processArrayResult(Object result) throws Exception {
if (result instanceof Object[][]) {
System.out.println("The result is of type: Object[][]");
Object[][] array = (Object[][]) result;
for (Object[] row : array) {
for (Object element : row) {
printElementType(element);
}
}
return array;
} else {
throw new Exception("result should be of type Object[][]. Maybe internal logic has error.");
}
}
private void printElementType(Object element) {
if (element instanceof String) {
System.out.println("String: " + element);
} else if (element instanceof Integer) {
System.out.println("Integer: " + element);
} else if (element instanceof Double) {
System.out.println("Double: " + element);
} else if (element instanceof Boolean) {
System.out.println("Boolean: " + element);
} else if (element instanceof Long) {
System.out.println("Long: " + element);
} else if (element instanceof byte[]) {
System.out.print("byte[]: ");
for (byte b : (byte[]) element) {
System.out.print(b + " ");
}
System.out.println();
} else {
System.out.println("Unknown type: " + element);
}
}
}

View File

@@ -0,0 +1,31 @@
package org.github.tursodatabase.limbo;
import org.github.tursodatabase.exceptions.ErrorCode;
import java.lang.Exception;
public class Limbo {
private static volatile boolean initialized;
private Limbo() {
if (!initialized) {
System.loadLibrary("_limbo_java");
initialized = true;
}
}
public static Limbo create() {
return new Limbo();
}
public Connection getConnection(String path) throws Exception {
long connectionId = connect(path);
if (connectionId == ErrorCode.CONNECTION_FAILURE) {
throw new Exception("Failed to initialize connection");
}
return new Connection(connectionId);
}
private static native long connect(String path);
}