Implement basic functionality of JDBC4PreparedStatement

This commit is contained in:
김선우 2025-02-07 12:33:13 +09:00
parent f5c4f4e8a1
commit eeb457f7a1
4 changed files with 597 additions and 16 deletions

View file

@ -0,0 +1,336 @@
package org.github.tursodatabase.jdbc4;
import static java.util.Objects.requireNonNull;
import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Date;
import java.sql.NClob;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.Ref;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.RowId;
import java.sql.SQLException;
import java.sql.SQLXML;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;
import org.github.tursodatabase.annotations.SkipNullableCheck;
import org.github.tursodatabase.core.LimboConnection;
public class JDBC4PreparedStatement extends JDBC4Statement implements PreparedStatement {
private final String sql;
public JDBC4PreparedStatement(LimboConnection connection, String sql) throws SQLException {
super(connection);
this.sql = sql;
this.statement = connection.prepare(sql);
this.statement.initializeColumnMetadata();
}
@Override
public ResultSet executeQuery() throws SQLException {
// TODO: check bindings etc
requireNonNull(this.statement);
return new JDBC4ResultSet(this.statement.getResultSet());
}
@Override
public int executeUpdate() throws SQLException {
// TODO
return 0;
}
@Override
public void setNull(int parameterIndex, int sqlType) throws SQLException {
requireNonNull(this.statement);
this.statement.bindNull(parameterIndex);
}
@Override
public void setBoolean(int parameterIndex, boolean x) throws SQLException {
requireNonNull(this.statement);
this.statement.bindInt(parameterIndex, x ? 1 : 0);
}
@Override
public void setByte(int parameterIndex, byte x) throws SQLException {
requireNonNull(this.statement);
this.statement.bindInt(parameterIndex, x);
}
@Override
public void setShort(int parameterIndex, short x) throws SQLException {
requireNonNull(this.statement);
this.statement.bindInt(parameterIndex, x);
}
@Override
public void setInt(int parameterIndex, int x) throws SQLException {
requireNonNull(this.statement);
this.statement.bindInt(parameterIndex, x);
}
@Override
public void setLong(int parameterIndex, long x) throws SQLException {
requireNonNull(this.statement);
this.statement.bindLong(parameterIndex, x);
}
@Override
public void setFloat(int parameterIndex, float x) throws SQLException {
requireNonNull(this.statement);
this.statement.bindDouble(parameterIndex, x);
}
@Override
public void setDouble(int parameterIndex, double x) throws SQLException {
requireNonNull(this.statement);
this.statement.bindDouble(parameterIndex, x);
}
@Override
public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException {
requireNonNull(this.statement);
this.statement.bindText(parameterIndex, x.toString());
}
@Override
public void setString(int parameterIndex, String x) throws SQLException {
requireNonNull(this.statement);
this.statement.bindText(parameterIndex, x);
}
@Override
public void setBytes(int parameterIndex, byte[] x) throws SQLException {
requireNonNull(this.statement);
this.statement.bindBlob(parameterIndex, x);
}
@Override
public void setDate(int parameterIndex, Date x) throws SQLException {
// TODO
}
@Override
public void setTime(int parameterIndex, Time x) throws SQLException {
// TODO
}
@Override
public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException {
// TODO
}
@Override
public void setAsciiStream(int parameterIndex, InputStream x, int length) throws SQLException {
// TODO
}
@Override
public void setUnicodeStream(int parameterIndex, InputStream x, int length) throws SQLException {
// TODO
}
@Override
public void setBinaryStream(int parameterIndex, InputStream x, int length) throws SQLException {
// TODO
}
@Override
public void clearParameters() throws SQLException {
// TODO
}
@Override
public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException {
// TODO
}
@Override
public void setObject(int parameterIndex, Object x) throws SQLException {
// TODO
}
@Override
public boolean execute() throws SQLException {
// TODO: check whether this is sufficient
requireNonNull(this.statement);
return statement.execute();
}
@Override
public void addBatch() throws SQLException {
// TODO
}
@Override
public void setCharacterStream(int parameterIndex, Reader reader, int length)
throws SQLException {}
@Override
public void setRef(int parameterIndex, Ref x) throws SQLException {
// TODO
}
@Override
public void setBlob(int parameterIndex, Blob x) throws SQLException {
// TODO
}
@Override
public void setClob(int parameterIndex, Clob x) throws SQLException {
// TODO
}
@Override
public void setArray(int parameterIndex, Array x) throws SQLException {
// TODO
}
@Override
@SkipNullableCheck
public ResultSetMetaData getMetaData() throws SQLException {
return null;
}
@Override
public void setDate(int parameterIndex, Date x, Calendar cal) throws SQLException {
// TODO
}
@Override
public void setTime(int parameterIndex, Time x, Calendar cal) throws SQLException {
// TODO
}
@Override
public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal) throws SQLException {
// TODO
}
@Override
public void setNull(int parameterIndex, int sqlType, String typeName) throws SQLException {
// TODO
}
@Override
public void setURL(int parameterIndex, URL x) throws SQLException {
// TODO
}
@Override
@SkipNullableCheck
public ParameterMetaData getParameterMetaData() throws SQLException {
// TODO
return null;
}
@Override
public void setRowId(int parameterIndex, RowId x) throws SQLException {
// TODO
}
@Override
public void setNString(int parameterIndex, String value) throws SQLException {
// TODO
}
@Override
public void setNCharacterStream(int parameterIndex, Reader value, long length)
throws SQLException {
// TODO
}
@Override
public void setNClob(int parameterIndex, NClob value) throws SQLException {
// TODO
}
@Override
public void setClob(int parameterIndex, Reader reader, long length) throws SQLException {
// TODO
}
@Override
public void setBlob(int parameterIndex, InputStream inputStream, long length)
throws SQLException {
// TODO
}
@Override
public void setNClob(int parameterIndex, Reader reader, long length) throws SQLException {
// TODO
}
@Override
public void setSQLXML(int parameterIndex, SQLXML xmlObject) throws SQLException {
// TODO
}
@Override
public void setObject(int parameterIndex, Object x, int targetSqlType, int scaleOrLength)
throws SQLException {
// TODO
}
@Override
public void setAsciiStream(int parameterIndex, InputStream x, long length) throws SQLException {
// TODO
}
@Override
public void setBinaryStream(int parameterIndex, InputStream x, long length) throws SQLException {
// TODO
}
@Override
public void setCharacterStream(int parameterIndex, Reader reader, long length)
throws SQLException {
// TODO
}
@Override
public void setAsciiStream(int parameterIndex, InputStream x) throws SQLException {
// TODO
}
@Override
public void setBinaryStream(int parameterIndex, InputStream x) throws SQLException {
// TODO
}
@Override
public void setCharacterStream(int parameterIndex, Reader reader) throws SQLException {
// TODO
}
@Override
public void setNCharacterStream(int parameterIndex, Reader value) throws SQLException {
// TODO
}
@Override
public void setClob(int parameterIndex, Reader reader) throws SQLException {
// TODO
}
@Override
public void setBlob(int parameterIndex, InputStream inputStream) throws SQLException {
// TODO
}
@Override
public void setNClob(int parameterIndex, Reader reader) throws SQLException {
// TODO
}
}

View file

@ -17,7 +17,7 @@ import org.github.tursodatabase.core.LimboStatement;
public class JDBC4Statement implements Statement {
private final LimboConnection connection;
@Nullable private LimboStatement statement = null;
@Nullable protected LimboStatement statement = null;
// Because JDBC4Statement has different life cycle in compared to LimboStatement, let's use this
// field to manage JDBC4Statement lifecycle

View file

@ -1,15 +0,0 @@
package org.github.tursodatabase.utils;
import org.github.tursodatabase.annotations.Nullable;
public class CommonUtils {
public static <T> T requireNotNull(@Nullable T obj) {
return requireNotNull(obj, obj + " must not be null");
}
public static <T> T requireNotNull(@Nullable T obj, String message) {
if (obj != null) return obj;
throw new IllegalArgumentException(message);
}
}

View file

@ -0,0 +1,260 @@
package org.github.tursodatabase.jdbc4;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import org.github.tursodatabase.TestUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class JDBC4PreparedStatementTest {
private JDBC4Connection connection;
@BeforeEach
void setUp() throws Exception {
String filePath = TestUtils.createTempFile();
String url = "jdbc:sqlite:" + filePath;
connection = new JDBC4Connection(url, filePath, new Properties());
}
@Test
void testSetBoolean() throws SQLException {
connection.prepareStatement("CREATE TABLE test (col INTEGER)").execute();
PreparedStatement stmt =
connection.prepareStatement("INSERT INTO test (col) VALUES (?), (?), (?)");
stmt.setBoolean(1, true);
stmt.setBoolean(2, false);
stmt.setBoolean(3, true);
stmt.execute();
PreparedStatement stmt2 = connection.prepareStatement("SELECT * FROM test;");
ResultSet rs = stmt2.executeQuery();
assertTrue(rs.next());
assertTrue(rs.getBoolean(1));
assertTrue(rs.next());
assertFalse(rs.getBoolean(1));
assertTrue(rs.next());
assertTrue(rs.getBoolean(1));
}
@Test
void testSetByte() throws SQLException {
connection.prepareStatement("CREATE TABLE test (col INTEGER)").execute();
PreparedStatement stmt =
connection.prepareStatement("INSERT INTO test (col) VALUES (?), (?), (?)");
stmt.setByte(1, (byte) 1);
stmt.setByte(2, (byte) 2);
stmt.setByte(3, (byte) 3);
stmt.execute();
PreparedStatement stmt2 = connection.prepareStatement("SELECT * FROM test;");
ResultSet rs = stmt2.executeQuery();
assertTrue(rs.next());
assertEquals(1, rs.getByte(1));
assertTrue(rs.next());
assertEquals(2, rs.getByte(1));
assertTrue(rs.next());
assertEquals(3, rs.getByte(1));
}
@Test
void testSetShort() throws SQLException {
connection.prepareStatement("CREATE TABLE test (col INTEGER)").execute();
PreparedStatement stmt =
connection.prepareStatement("INSERT INTO test (col) VALUES (?), (?), (?)");
stmt.setShort(1, (short) 1);
stmt.setShort(2, (short) 2);
stmt.setShort(3, (short) 3);
stmt.execute();
PreparedStatement stmt2 = connection.prepareStatement("SELECT * FROM test;");
ResultSet rs = stmt2.executeQuery();
assertTrue(rs.next());
assertEquals(1, rs.getShort(1));
assertTrue(rs.next());
assertEquals(2, rs.getShort(1));
assertTrue(rs.next());
assertEquals(3, rs.getShort(1));
}
@Test
void testSetInt() throws SQLException {
connection.prepareStatement("CREATE TABLE test (col INTEGER)").execute();
PreparedStatement stmt =
connection.prepareStatement("INSERT INTO test (col) VALUES (?), (?), (?)");
stmt.setInt(1, 1);
stmt.setInt(2, 2);
stmt.setInt(3, 3);
stmt.execute();
PreparedStatement stmt2 = connection.prepareStatement("SELECT * FROM test;");
ResultSet rs = stmt2.executeQuery();
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
assertTrue(rs.next());
assertEquals(2, rs.getInt(1));
assertTrue(rs.next());
assertEquals(3, rs.getInt(1));
}
@Test
void testSetLong() throws SQLException {
connection.prepareStatement("CREATE TABLE test (col INTEGER)").execute();
PreparedStatement stmt =
connection.prepareStatement("INSERT INTO test (col) VALUES (?), (?), (?)");
stmt.setLong(1, 1L);
stmt.setLong(2, 2L);
stmt.setLong(3, 3L);
stmt.execute();
PreparedStatement stmt2 = connection.prepareStatement("SELECT * FROM test;");
ResultSet rs = stmt2.executeQuery();
assertTrue(rs.next());
assertEquals(1L, rs.getLong(1));
assertTrue(rs.next());
assertEquals(2L, rs.getLong(1));
assertTrue(rs.next());
assertEquals(3L, rs.getLong(1));
}
@Test
void testSetFloat() throws SQLException {
connection.prepareStatement("CREATE TABLE test (col REAL)").execute();
PreparedStatement stmt =
connection.prepareStatement("INSERT INTO test (col) VALUES (?), (?), (?)");
stmt.setFloat(1, 1.0f);
stmt.setFloat(2, 2.0f);
stmt.setFloat(3, 3.0f);
stmt.execute();
PreparedStatement stmt2 = connection.prepareStatement("SELECT * FROM test;");
ResultSet rs = stmt2.executeQuery();
assertTrue(rs.next());
assertEquals(1.0f, rs.getFloat(1));
assertTrue(rs.next());
assertEquals(2.0f, rs.getFloat(1));
assertTrue(rs.next());
assertEquals(3.0f, rs.getFloat(1));
}
@Test
void testSetDouble() throws SQLException {
connection.prepareStatement("CREATE TABLE test (col REAL)").execute();
PreparedStatement stmt =
connection.prepareStatement("INSERT INTO test (col) VALUES (?), (?), (?)");
stmt.setDouble(1, 1.0);
stmt.setDouble(2, 2.0);
stmt.setDouble(3, 3.0);
stmt.execute();
PreparedStatement stmt2 = connection.prepareStatement("SELECT * FROM test;");
ResultSet rs = stmt2.executeQuery();
assertTrue(rs.next());
assertEquals(1.0, rs.getDouble(1));
assertTrue(rs.next());
assertEquals(2.0, rs.getDouble(1));
assertTrue(rs.next());
assertEquals(3.0, rs.getDouble(1));
}
@Test
void testSetBigDecimal() throws SQLException {
connection.prepareStatement("CREATE TABLE test (col TEXT)").execute();
PreparedStatement stmt =
connection.prepareStatement("INSERT INTO test (col) VALUES (?), (?), (?)");
stmt.setBigDecimal(1, new BigDecimal("1.0"));
stmt.setBigDecimal(2, new BigDecimal("2.0"));
stmt.setBigDecimal(3, new BigDecimal("3.0"));
stmt.execute();
PreparedStatement stmt2 = connection.prepareStatement("SELECT * FROM test;");
ResultSet rs = stmt2.executeQuery();
assertTrue(rs.next());
assertEquals("1.0", rs.getString(1));
assertTrue(rs.next());
assertEquals("2.0", rs.getString(1));
assertTrue(rs.next());
assertEquals("3.0", rs.getString(1));
}
@Test
void testSetString() throws SQLException {
connection.prepareStatement("CREATE TABLE test (col TEXT)").execute();
PreparedStatement stmt =
connection.prepareStatement("INSERT INTO test (col) VALUES (?), (?), (?)");
stmt.setString(1, "test1");
stmt.setString(2, "test2");
stmt.setString(3, "test3");
stmt.execute();
PreparedStatement stmt2 = connection.prepareStatement("SELECT * FROM test;");
ResultSet rs = stmt2.executeQuery();
assertTrue(rs.next());
assertEquals("test1", rs.getString(1));
assertTrue(rs.next());
assertEquals("test2", rs.getString(1));
assertTrue(rs.next());
assertEquals("test3", rs.getString(1));
}
@Test
void testSetBytes() throws SQLException {
connection.prepareStatement("CREATE TABLE test (col BLOB)").execute();
PreparedStatement stmt =
connection.prepareStatement("INSERT INTO test (col) VALUES (?), (?), (?)");
stmt.setBytes(1, new byte[] {1, 2, 3});
stmt.setBytes(2, new byte[] {4, 5, 6});
stmt.setBytes(3, new byte[] {7, 8, 9});
stmt.execute();
PreparedStatement stmt2 = connection.prepareStatement("SELECT * FROM test;");
ResultSet rs = stmt2.executeQuery();
assertTrue(rs.next());
assertArrayEquals(new byte[] {1, 2, 3}, rs.getBytes(1));
assertTrue(rs.next());
assertArrayEquals(new byte[] {4, 5, 6}, rs.getBytes(1));
assertTrue(rs.next());
assertArrayEquals(new byte[] {7, 8, 9}, rs.getBytes(1));
}
@Test
void testInsertMultipleTypes() throws SQLException {
connection.prepareStatement("CREATE TABLE test (col1 INTEGER, col2 REAL, col3 TEXT, col4 BLOB)").execute();
PreparedStatement stmt = connection.prepareStatement("INSERT INTO test (col1, col2, col3, col4) VALUES (?, ?, ?, ?), (?, ?, ?, ?)");
stmt.setInt(1, 1);
stmt.setFloat(2, 1.1f);
stmt.setString(3, "row1");
stmt.setBytes(4, new byte[] {1, 2, 3});
stmt.setInt(5, 2);
stmt.setFloat(6, 2.2f);
stmt.setString(7, "row2");
stmt.setBytes(8, new byte[] {4, 5, 6});
stmt.execute();
PreparedStatement stmt2 = connection.prepareStatement("SELECT * FROM test;");
ResultSet rs = stmt2.executeQuery();
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
assertEquals(1.1f, rs.getFloat(2));
assertEquals("row1", rs.getString(3));
assertArrayEquals(new byte[] {1, 2, 3}, rs.getBytes(4));
assertTrue(rs.next());
assertEquals(2, rs.getInt(1));
assertEquals(2.2f, rs.getFloat(2));
assertEquals("row2", rs.getString(3));
assertArrayEquals(new byte[] {4, 5, 6}, rs.getBytes(4));
}
}