mirror of
https://github.com/aljazceru/turso.git
synced 2026-01-04 08:54:20 +01:00
Implement basic getXX methods for JDBC4ResultSet
This commit is contained in:
@@ -117,6 +117,25 @@ public class LimboResultSet {
|
||||
this.open = false;
|
||||
}
|
||||
|
||||
// Note that columnIndex starts from 1
|
||||
@Nullable
|
||||
public Object get(int columnIndex) throws SQLException {
|
||||
if (!this.isOpen()) {
|
||||
throw new SQLException("ResultSet is not open");
|
||||
}
|
||||
|
||||
if (this.lastStepResult == null || this.lastStepResult.getResult() == null) {
|
||||
throw new SQLException("ResultSet is null");
|
||||
}
|
||||
|
||||
final Object[] resultSet = this.lastStepResult.getResult();
|
||||
if (columnIndex > resultSet.length) {
|
||||
throw new SQLException("columnIndex out of bound");
|
||||
}
|
||||
|
||||
return resultSet[columnIndex - 1];
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "LimboResultSet{"
|
||||
|
||||
@@ -55,6 +55,11 @@ public class LimboStatement {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Because Limbo supports async I/O, it is possible to return a {@link LimboStepResult} with
|
||||
* {@link LimboStepResult#STEP_RESULT_ID_ROW}. However, this is handled by the native side, so you
|
||||
* can expect that this method will not return a {@link LimboStepResult#STEP_RESULT_ID_ROW}.
|
||||
*/
|
||||
@Nullable
|
||||
private native LimboStepResult step(long stmtPointer) throws SQLException;
|
||||
|
||||
|
||||
@@ -47,6 +47,11 @@ public class LimboStepResult {
|
||||
|| stepResultId == STEP_RESULT_ID_ERROR;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Object[] getResult() {
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "LimboStepResult{"
|
||||
|
||||
@@ -3,10 +3,26 @@ package org.github.tursodatabase.jdbc4;
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.net.URL;
|
||||
import java.sql.*;
|
||||
import java.sql.Array;
|
||||
import java.sql.Blob;
|
||||
import java.sql.Clob;
|
||||
import java.sql.Date;
|
||||
import java.sql.NClob;
|
||||
import java.sql.Ref;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.RowId;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.SQLWarning;
|
||||
import java.sql.SQLXML;
|
||||
import java.sql.Statement;
|
||||
import java.sql.Time;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Calendar;
|
||||
import java.util.Map;
|
||||
import org.github.tursodatabase.annotations.Nullable;
|
||||
import org.github.tursodatabase.annotations.SkipNullableCheck;
|
||||
import org.github.tursodatabase.core.LimboResultSet;
|
||||
|
||||
@@ -35,64 +51,99 @@ public class JDBC4ResultSet implements ResultSet {
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public String getString(int columnIndex) throws SQLException {
|
||||
// TODO
|
||||
return "";
|
||||
final Object result = resultSet.get(columnIndex);
|
||||
if (result == null) {
|
||||
return null;
|
||||
}
|
||||
return wrapTypeConversion(() -> (String) result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(int columnIndex) throws SQLException {
|
||||
// TODO
|
||||
return false;
|
||||
final Object result = resultSet.get(columnIndex);
|
||||
if (result == null) {
|
||||
return false;
|
||||
}
|
||||
return wrapTypeConversion(() -> (Long) result != 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte getByte(int columnIndex) throws SQLException {
|
||||
// TODO
|
||||
return 0;
|
||||
final Object result = resultSet.get(columnIndex);
|
||||
if (result == null) {
|
||||
return 0;
|
||||
}
|
||||
return wrapTypeConversion(() -> ((Long) result).byteValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public short getShort(int columnIndex) throws SQLException {
|
||||
// TODO
|
||||
return 0;
|
||||
final Object result = resultSet.get(columnIndex);
|
||||
if (result == null) {
|
||||
return 0;
|
||||
}
|
||||
return wrapTypeConversion(() -> ((Long) result).shortValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInt(int columnIndex) throws SQLException {
|
||||
// TODO
|
||||
return 0;
|
||||
final Object result = resultSet.get(columnIndex);
|
||||
if (result == null) {
|
||||
return 0;
|
||||
}
|
||||
return wrapTypeConversion(() -> ((Long) result).intValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getLong(int columnIndex) throws SQLException {
|
||||
// TODO
|
||||
return 0;
|
||||
final Object result = resultSet.get(columnIndex);
|
||||
if (result == null) {
|
||||
return 0;
|
||||
}
|
||||
return wrapTypeConversion(() -> (long) result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getFloat(int columnIndex) throws SQLException {
|
||||
// TODO
|
||||
return 0;
|
||||
final Object result = resultSet.get(columnIndex);
|
||||
if (result == null) {
|
||||
return 0;
|
||||
}
|
||||
return wrapTypeConversion(() -> ((Double) result).floatValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getDouble(int columnIndex) throws SQLException {
|
||||
// TODO
|
||||
return 0;
|
||||
final Object result = resultSet.get(columnIndex);
|
||||
if (result == null) {
|
||||
return 0;
|
||||
}
|
||||
return wrapTypeConversion(() -> (double) result);
|
||||
}
|
||||
|
||||
// TODO: customize rounding mode?
|
||||
@Override
|
||||
@SkipNullableCheck
|
||||
@Nullable
|
||||
public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException {
|
||||
// TODO
|
||||
return null;
|
||||
final Object result = resultSet.get(columnIndex);
|
||||
if (result == null) {
|
||||
return null;
|
||||
}
|
||||
final double doubleResult = wrapTypeConversion(() -> (double) result);
|
||||
final BigDecimal bigDecimalResult = BigDecimal.valueOf(doubleResult);
|
||||
return bigDecimalResult.setScale(scale, RoundingMode.HALF_UP);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public byte[] getBytes(int columnIndex) throws SQLException {
|
||||
// TODO
|
||||
return new byte[0];
|
||||
final Object result = resultSet.get(columnIndex);
|
||||
if (result == null) {
|
||||
return null;
|
||||
}
|
||||
return wrapTypeConversion(() -> (byte[]) result);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -300,10 +351,14 @@ public class JDBC4ResultSet implements ResultSet {
|
||||
}
|
||||
|
||||
@Override
|
||||
@SkipNullableCheck
|
||||
@Nullable
|
||||
public BigDecimal getBigDecimal(int columnIndex) throws SQLException {
|
||||
// TODO
|
||||
return null;
|
||||
final Object result = resultSet.get(columnIndex);
|
||||
if (result == null) {
|
||||
return null;
|
||||
}
|
||||
final double doubleResult = wrapTypeConversion(() -> (double) result);
|
||||
return BigDecimal.valueOf(doubleResult);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -1126,7 +1181,16 @@ public class JDBC4ResultSet implements ResultSet {
|
||||
return false;
|
||||
}
|
||||
|
||||
private SQLException throwNotSupportedException() {
|
||||
return new SQLFeatureNotSupportedException("Not implemented by the driver");
|
||||
@FunctionalInterface
|
||||
public interface ResultSetSupplier<T> {
|
||||
T get() throws Exception;
|
||||
}
|
||||
|
||||
private <T> T wrapTypeConversion(ResultSetSupplier<T> supplier) throws SQLException {
|
||||
try {
|
||||
return supplier.get();
|
||||
} catch (Exception e) {
|
||||
throw new SQLException("Type conversion failed: " + e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user