-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge 'bindings/java: Implement JDBC
ResultSet
' from Kim Seon Woo
## Purpose of this PR Associate jdbc's `ResultSet` with the returned values from limbo's step function. ## Changes ### Rust - `Java_org_github_tursodatabase_core_LimboStatement_step` now returns an object of java's `LimboStepResult.java` ### Java - Added `LimboStepResult.java` in order to distinguish the type of `StepResult`(which limbo returns) and to encapsulate the interpretation of limbo's `StepResult` - Change `JDBC4ResultSet` inheriting `LimboResultSet` to composition. IMO when using inheritance, it's too burdensome to fit unmatching parts together. - Enhance `JDBC4Statement.java`'s `execute` method - By looking at the `ResultSet` created after executing the qury, it's now able to determine the (boolean) result. ## Reference - #615 Closes #743
- Loading branch information
Showing
14 changed files
with
446 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
.PHONY: test build_test | ||
|
||
test: build_test | ||
./gradlew test | ||
./gradlew test --info | ||
|
||
build_test: | ||
CARGO_TARGET_DIR=src/test/resources/limbo cargo build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 47 additions & 39 deletions
86
bindings/java/src/main/java/org/github/tursodatabase/core/LimboStatement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,76 @@ | ||
package org.github.tursodatabase.core; | ||
|
||
import java.sql.SQLException; | ||
|
||
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 org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.sql.SQLException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public abstract class LimboStatement { | ||
|
||
protected final LimboConnection connection; | ||
protected final LimboResultSet resultSet; | ||
|
||
@Nullable | ||
protected String sql = null; | ||
/** | ||
* By default, only one <code>resultSet</code> object per <code>LimboStatement</code> can be open at the same time. | ||
* Therefore, if the reading of one <code>resultSet</code> object is interleaved with the reading of another, each must | ||
* have been generated by different <code>LimboStatement</code> objects. All execution method in the <code>LimboStatement</code> | ||
* implicitly close the current <code>resultSet</code> object of the statement if an open one exists. | ||
*/ | ||
public class LimboStatement { | ||
private static final Logger log = LoggerFactory.getLogger(LimboStatement.class); | ||
|
||
protected LimboStatement(LimboConnection connection) { | ||
this.connection = connection; | ||
this.resultSet = new JDBC4ResultSet(this); | ||
} | ||
private final String sql; | ||
private final long statementPointer; | ||
private final LimboResultSet resultSet; | ||
|
||
protected void internalClose() throws SQLException { | ||
// TODO | ||
// TODO: what if the statement we ran was DDL, update queries and etc. Should we still create a resultSet? | ||
public LimboStatement(String sql, long statementPointer) { | ||
this.sql = sql; | ||
this.statementPointer = statementPointer; | ||
this.resultSet = LimboResultSet.of(this); | ||
log.debug("Creating statement with sql: {}", this.sql); | ||
} | ||
|
||
protected void clearGeneratedKeys() throws SQLException { | ||
// TODO | ||
public LimboResultSet getResultSet() { | ||
return resultSet; | ||
} | ||
|
||
protected void updateGeneratedKeys() throws SQLException { | ||
// TODO | ||
/** | ||
* Expects a clean statement created right after prepare method is called. | ||
* | ||
* @return true if the ResultSet has at least one row; false otherwise. | ||
*/ | ||
public boolean execute() throws SQLException { | ||
resultSet.next(); | ||
return resultSet.hasLastStepReturnedRow(); | ||
} | ||
|
||
// TODO: associate the result with CoreResultSet | ||
// TODO: we can make this async!! | ||
// TODO: distinguish queries that return result or doesn't return result | ||
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); | ||
LimboStepResult step() throws SQLException { | ||
final LimboStepResult result = step(this.statementPointer); | ||
if (result == null) { | ||
throw new SQLException("step() returned null, which is only returned when an error occurs"); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
private native Object[] step(long stmtPointer) throws SQLException; | ||
@Nullable | ||
private native LimboStepResult step(long stmtPointer) throws SQLException; | ||
|
||
/** | ||
* Throws formatted SQLException with error code and message. | ||
* | ||
* @param errorCode Error code. | ||
* @param errorCode Error code. | ||
* @param errorMessageBytes Error message. | ||
*/ | ||
@NativeInvocation | ||
@NativeInvocation(invokedFrom = "limbo_statement.rs") | ||
private void throwLimboException(int errorCode, byte[] errorMessageBytes) throws SQLException { | ||
LimboExceptionUtils.throwLimboException(errorCode, errorMessageBytes); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "LimboStatement{" + | ||
"statementPointer=" + statementPointer + | ||
", sql='" + sql + '\'' + | ||
'}'; | ||
} | ||
} |
Oops, something went wrong.