Skip to content

Commit d1b05ac

Browse files
committed
Merge 'java/bindings: Add support for creating statement ' from Kim Seon Woo
## Purpose of this PR - Add support for `createStatement` in `JDBC4Connection` - Following works can use this statement to execute queries ## Changes - Implement `createStatement` for `JDBC4Connection` - Add `JDBC4Statement` ## References - #615 Closes #693
2 parents 256c0d4 + d151824 commit d1b05ac

File tree

6 files changed

+533
-19
lines changed

6 files changed

+533
-19
lines changed

bindings/java/src/main/java/org/github/tursodatabase/LimboConnection.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.github.tursodatabase.core.LimboDB;
55

66
import java.sql.Connection;
7+
import java.sql.ResultSet;
78
import java.sql.SQLException;
89
import java.util.Properties;
910

@@ -61,4 +62,43 @@ private static AbstractDB open(String url, String fileName, Properties propertie
6162
database.open(0);
6263
return database;
6364
}
65+
66+
protected void checkOpen() throws SQLException {
67+
if (isClosed()) throw new SQLException("database connection closed");
68+
}
69+
70+
@Override
71+
public void close() throws SQLException {
72+
if (isClosed()) return;
73+
database.close();
74+
}
75+
76+
@Override
77+
public boolean isClosed() throws SQLException {
78+
return database.isClosed();
79+
}
80+
81+
// TODO: check whether this is still valid for limbo
82+
/**
83+
* Checks whether the type, concurrency, and holdability settings for a {@link ResultSet} are
84+
* supported by the SQLite interface. Supported settings are:
85+
*
86+
* <ul>
87+
* <li>type: {@link ResultSet#TYPE_FORWARD_ONLY}
88+
* <li>concurrency: {@link ResultSet#CONCUR_READ_ONLY})
89+
* <li>holdability: {@link ResultSet#CLOSE_CURSORS_AT_COMMIT}
90+
* </ul>
91+
*
92+
* @param resultSetType the type setting.
93+
* @param resultSetConcurrency the concurrency setting.
94+
* @param resultSetHoldability the holdability setting.
95+
*/
96+
protected void checkCursor(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
97+
if (resultSetType != ResultSet.TYPE_FORWARD_ONLY)
98+
throw new SQLException("SQLite only supports TYPE_FORWARD_ONLY cursors");
99+
if (resultSetConcurrency != ResultSet.CONCUR_READ_ONLY)
100+
throw new SQLException("SQLite only supports CONCUR_READ_ONLY cursors");
101+
if (resultSetHoldability != ResultSet.CLOSE_CURSORS_AT_COMMIT)
102+
throw new SQLException("SQLite only supports closing cursors at commit");
103+
}
64104
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright (c) 2007 David Crawshaw <[email protected]>
3+
*
4+
* Permission to use, copy, modify, and/or distribute this software for any
5+
* purpose with or without fee is hereby granted, provided that the above
6+
* copyright notice and this permission notice appear in all copies.
7+
*
8+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15+
*/
16+
package org.github.tursodatabase.core;
17+
18+
public class Codes {
19+
/** Successful result */
20+
public static final int SQLITE_OK = 0;
21+
22+
/** SQL error or missing database */
23+
public static final int SQLITE_ERROR = 1;
24+
25+
/** An internal logic error in SQLite */
26+
public static final int SQLITE_INTERNAL = 2;
27+
28+
/** Access permission denied */
29+
public static final int SQLITE_PERM = 3;
30+
31+
/** Callback routine requested an abort */
32+
public static final int SQLITE_ABORT = 4;
33+
34+
/** The database file is locked */
35+
public static final int SQLITE_BUSY = 5;
36+
37+
/** A table in the database is locked */
38+
public static final int SQLITE_LOCKED = 6;
39+
40+
/** A malloc() failed */
41+
public static final int SQLITE_NOMEM = 7;
42+
43+
/** Attempt to write a readonly database */
44+
public static final int SQLITE_READONLY = 8;
45+
46+
/** Operation terminated by sqlite_interrupt() */
47+
public static final int SQLITE_INTERRUPT = 9;
48+
49+
/** Some kind of disk I/O error occurred */
50+
public static final int SQLITE_IOERR = 10;
51+
52+
/** The database disk image is malformed */
53+
public static final int SQLITE_CORRUPT = 11;
54+
55+
/** (Internal Only) Table or record not found */
56+
public static final int SQLITE_NOTFOUND = 12;
57+
58+
/** Insertion failed because database is full */
59+
public static final int SQLITE_FULL = 13;
60+
61+
/** Unable to open the database file */
62+
public static final int SQLITE_CANTOPEN = 14;
63+
64+
/** Database lock protocol error */
65+
public static final int SQLITE_PROTOCOL = 15;
66+
67+
/** (Internal Only) Database table is empty */
68+
public static final int SQLITE_EMPTY = 16;
69+
70+
/** The database schema changed */
71+
public static final int SQLITE_SCHEMA = 17;
72+
73+
/** Too much data for one row of a table */
74+
public static final int SQLITE_TOOBIG = 18;
75+
76+
/** Abort due to constraint violation */
77+
public static final int SQLITE_CONSTRAINT = 19;
78+
79+
/** Data type mismatch */
80+
public static final int SQLITE_MISMATCH = 20;
81+
82+
/** Library used incorrectly */
83+
public static final int SQLITE_MISUSE = 21;
84+
85+
/** Uses OS features not supported on host */
86+
public static final int SQLITE_NOLFS = 22;
87+
88+
/** Authorization denied */
89+
public static final int SQLITE_AUTH = 23;
90+
91+
/** sqlite_step() has another row ready */
92+
public static final int SQLITE_ROW = 100;
93+
94+
/** sqlite_step() has finished executing */
95+
public static final int SQLITE_DONE = 101;
96+
97+
// types returned by sqlite3_column_type()
98+
99+
public static final int SQLITE_INTEGER = 1;
100+
public static final int SQLITE_FLOAT = 2;
101+
public static final int SQLITE_TEXT = 3;
102+
public static final int SQLITE_BLOB = 4;
103+
public static final int SQLITE_NULL = 5;
104+
}
Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
package org.github.tursodatabase.core;
22

3-
// TODO: add fields and methods
4-
public class CoreStatement {
3+
import org.github.tursodatabase.LimboConnection;
4+
5+
import java.sql.SQLException;
6+
7+
public abstract class CoreStatement {
8+
9+
private final LimboConnection connection;
10+
11+
protected CoreStatement(LimboConnection connection) {
12+
this.connection = connection;
13+
}
14+
15+
protected void internalClose() throws SQLException {
16+
// TODO
17+
}
18+
19+
protected void clearGeneratedKeys() throws SQLException {
20+
// TODO
21+
}
22+
23+
protected void updateGeneratedKeys() throws SQLException {
24+
// TODO
25+
}
526
}

bindings/java/src/main/java/org/github/tursodatabase/jdbc4/JDBC4Connection.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,25 @@ public JDBC4Connection(String url, String fileName, Properties properties) throw
1616
}
1717

1818
@Override
19-
@SkipNullableCheck
2019
public Statement createStatement() throws SQLException {
21-
// TODO
22-
return null;
20+
return createStatement(
21+
ResultSet.TYPE_FORWARD_ONLY,
22+
ResultSet.CONCUR_READ_ONLY,
23+
ResultSet.CLOSE_CURSORS_AT_COMMIT
24+
);
25+
}
26+
27+
@Override
28+
public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
29+
return createStatement(resultSetType, resultSetConcurrency, ResultSet.CLOSE_CURSORS_AT_COMMIT);
30+
}
31+
32+
@Override
33+
public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
34+
checkOpen();
35+
checkCursor(resultSetType, resultSetConcurrency, resultSetHoldability);
36+
37+
return new JDBC4Statement(this);
2338
}
2439

2540
@Override
@@ -127,13 +142,6 @@ public void clearWarnings() throws SQLException {
127142
// TODO
128143
}
129144

130-
@Override
131-
@SkipNullableCheck
132-
public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
133-
// TODO
134-
return null;
135-
}
136-
137145
@Override
138146
@SkipNullableCheck
139147
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
@@ -193,13 +201,6 @@ public void releaseSavepoint(Savepoint savepoint) throws SQLException {
193201
// TODO
194202
}
195203

196-
@Override
197-
@SkipNullableCheck
198-
public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
199-
// TODO
200-
return null;
201-
}
202-
203204
@Override
204205
@SkipNullableCheck
205206
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {

0 commit comments

Comments
 (0)