Class Db
SQLite persistence for server-side binaries, on the engine the translator already bundles. Not com.codename1.db.Database, which needs a CodenameOneImplementation for every call.
Parameters are always bound, never interpolated: string concatenation into SQL is how injection happens, and a server parses input it did not write.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceA unit of work run insidetransaction(Db.Work). -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Holds the connection gate (seeenter()) forcloseLocked().booleancreateFunction(String name, int argc, Function<Object[], Object> body) Binds one parameter, or fails.voidSwitches the database to write-ahead logging, which is what lets readers run while a writer is active.intHolds the connection gate (seeenter()) forexecuteLocked(String, Object[], long[]).intexecute(String, Object[]), also storinglastInsertId()intolastInsertId[0]when it is not null -- under the one hold of the gate, rather than taking it again.longHolds the connection gate (seeenter()) forlastInsertIdLocked().static DbOpens (or creates) the database at the given path.Holds the connection gate (seeenter()) forqueryLocked(String, Object[], boolean).queryPositional(String sql, Object[] params) Runs a query and returns every row as anObject[]of its values in the order the statement projects them; the column names are on the result (Database.columnNames(List)).String[]queryPositionalInto(String sql, Object[] params, List out) queryPositional(String, Object[])into a list the caller owns and reuses, answering the column names -- the statement's own array, which must not be changed.voidsetBusyTimeout(int millis) How long a blocked writer waits for a competing one before giving up.transaction(Db.Work body) Holds the connection gate (seeenter()) fortransactionLocked(Db.Work).
-
Method Details
-
open
Opens (or creates) the database at the given path. ":memory:" gives a process-lifetime database, which is what a stateless function usually wants for a cache.- Throws:
IOException
-
execute
Holds the connection gate (seeenter()) forexecuteLocked(String, Object[], long[]).- Throws:
IOException
-
execute
execute(String, Object[]), also storinglastInsertId()intolastInsertId[0]when it is not null -- under the one hold of the gate, rather than taking it again.- Throws:
IOException
-
query
Holds the connection gate (seeenter()) forqueryLocked(String, Object[], boolean).- Throws:
IOException
-
queryPositional
Runs a query and returns every row as anObject[]of its values in the order the statement projects them; the column names are on the result (Database.columnNames(List)). SeeDatabase.queryPositional(String, Object[]).- Throws:
IOException
-
queryPositionalInto
queryPositional(String, Object[])into a list the caller owns and reuses, answering the column names -- the statement's own array, which must not be changed. A new result list per statement cost an object and its first storage block, and the DB-API layer then copied every row into a list of its own.- Throws:
IOException
-
transaction
-
lastInsertId
public long lastInsertId()Holds the connection gate (seeenter()) forlastInsertIdLocked(). -
close
public void close()Holds the connection gate (see
enter()) forcloseLocked().NEVER WAITS FOR ANOTHER CALLER. When the connection is in use, the close is recorded and the user runs it as its outermost operation ends -- after a transaction, not in the middle of one. Waiting was a hang at shutdown: HttpServer.stop() lets go of a virtual thread whose query outlived the drain window while its sqlite3_step is still on the I/O pool, and that thread holds the gate and never runs again, so a pool.close() that waited for it waited for ever. Such a connection is left to the process exit.
-
enableWriteAheadLog
Switches the database to write-ahead logging, which is what lets readers run while a writer is active. Worth doing once after open for anything that serves concurrent requests; pointless for :memory:.- Throws:
IOException
-
setBusyTimeout
How long a blocked writer waits for a competing one before giving up. Without this, two connections writing at once produce SQLITE_BUSY immediately rather than queueing.- Throws:
IOException
-
createFunction
public boolean createFunction(String name, int argc, Function<Object[], Object> body) throws IOExceptionBinds one parameter, or fails.
The status was discarded, so binding more parameters than the statement has placeholders -- SQLITE_RANGE -- was ignored and the statement executed anyway, with the unbound parameter reading as NULL. A mutation committed with the wrong values and nothing said so, where the JavaSE JDBC path throws. Registers a scalar SQL function; see Database.createFunction.
- Throws:
IOException
-