class Database : AutoCloseable
Represents a database that we connect to using JDBC. This is the main entry point in this library for operations on a database.
Usage example:
Database.withConnection("jdbc:h2:mem:test", "root", "") { db ->
various operations on db
}
connection |
val connection: Connection |
close |
Close the underlying JDBC connection, and free other resources, like cached statements. fun close(): Unit |
createTable |
Execute a SQL CREATE TABLE statement for table. Include SQL "IF NOT EXISTS" if ifNotExists is set true. The CREATE TABLE statement is not cached. fun createTable(table: Table, ifNotExists: Boolean = false): Unit |
deleteFrom |
Run a DELETE FROM statement for the given table. See DeleteStatement.Builder for setting additional arguments and parameters. The statement is run after the code body sent to DeleteStatement.Builder.run completes. fun deleteFrom(table: Table): Builder |
dropTable |
Execute a SQL DROP TABLE statement for table. Include SQL "IF EXISTS" if ifExists is set true. The DROP TABLE statement is not cached. fun dropTable(table: Table, ifExists: Boolean = false): Unit |
insertInto |
Run an insert statement, and collect results, i.e. the values of primary key column(s). For example: fun insertInto(table: Table, setBody: (InsertStatement) -> Unit): Builder
Run an insert statement, but ignore any results, e.g. the values of primary keys. For example: fun insertInto(table: Table): BuilderNoResults |
select |
Run a SELECT statement over the given columns. See SelectQuery.Builder for parameters and arguments that can be set. The query is run within the code block given to SelectQuery.Builder.run when that code executes SelectQuery.next. fun select(columns: List<Column<*>>): Builder fun select(vararg columns: Column<*>): Builder |
statement |
Run a generic SQL statement. See GenericStatement.Builder for setting arguments and parameters, including the statement text. fun statement(): Builder |
transaction |
Execute the code body within a JDBC transaction. The transaction is committed when the code body completes. Set maxRetries to attempt the code body multiple times, if an exception is thrown in body or while committing. fun transaction(maxRetries: Int = 0, body: () -> Unit): Unit |
update |
Run an UPDATE statement on the given table. Rows to be updated are identified with calls to ParameterSetter.set within the code body passed to UpdateStatement.Builder.run. fun update(table: Table): Builder |
open |
Open a JDBC connection, and give a Database object representing it. See close, or, to automatically close the Database, withConnection. fun open(url: String, user: String, password: String): Database |
withConnection |
Execute a body of code with a new JDBC connection that is held within a fun <T> withConnection(url: String, user: String, password: String, body: (db: Database) -> T): T |