Interface Sql
- All Known Implementing Classes:
WireSql
public interface Sql
Database access within a single transaction — a dedicated connection (analogue of JDBC
Connection).
Queries are allowed only inside the transaction: in SqlInitializer,
Transaction.run(Sql) and SqlFinalizer. The outer hooks
TransactionInitializer and TransactionFinalizer may use metadata only
(id(), readOnly()). After the transaction ends only id() remains
available. Passed explicitly to the calls that need database work.
Example:
Row row = sql.query("SELECT a, b FROM t WHERE a = ?").args("x").one();
List<Cell> list = sql.query("SELECT * FROM cell WHERE zone = ?").args(zone).list(Cell::new);
long changed = sql.query("UPDATE cell SET blocked = ? WHERE zone = ?").args(true, zone).count();
Not thread-safe: a transaction is used by one thread at a time.-
Method Summary
Modifier and TypeMethodDescriptiondefault <T> EntityWrite<T> delete(T entity) Deletes an entity (Delete).default <T> BatchDeletes entities of one type as a batch (Delete).longid()Identifier of this instance, unique within the JVM process (for example, to correlate logs and metrics of one transaction).default <T> EntityWrite<T> insert(T entity) Inserts an entity (Insert).default <T> BatchInserts entities of one type as a batch (Insert, withoutreturning).static longnextId()New identifier for anSqlinstance, unique within the JVM process.Statement to execute within this transaction.booleanreadOnly()Read-only transaction flag.default <T> EntityQuery<T> select(EntitySelect<T> select) Query of an entity declared on a constant of anEntitySelectenum (Select).default <T> EntityWrite<T> update(T entity) Updates an entity (Update).default <T> BatchUpdates entities of one type as a batch (Update).default <T> EntityWrite<T> upsert(T entity) Inserts or updates an entity (Upsert).default <T> BatchInserts or updates entities of one type as a batch (Upsert).
-
Method Details
-
nextId
static long nextId()New identifier for anSqlinstance, unique within the JVM process. Every implementation calls it once in its constructor.- Returns:
- the next identifier, starting from 1
-
id
long id()Identifier of this instance, unique within the JVM process (for example, to correlate logs and metrics of one transaction). Available in any state, including after the transaction ends.- Returns:
- the instance identifier
-
query
Statement to execute within this transaction.- Parameters:
sql- SQL text with positional?placeholders- Returns:
- the statement; executed by a terminal operation of
Result - Throws:
IllegalStateException- if the database transaction is not active
-
readOnly
boolean readOnly()Read-only transaction flag.- Returns:
trueif the transaction was opened viaDatabase.read(Transaction)
-
select
Query of an entity declared on a constant of anEntitySelectenum (Select).- Type Parameters:
T- entity type- Parameters:
select- query, for exampleCellSelect.BY_CODE- Returns:
- query; bind arguments with
args(...)or execute as is - Throws:
IllegalArgumentException- if the query or its entity has no generated metadata
-
insert
Inserts an entity (Insert).- Type Parameters:
T- entity type- Parameters:
entity- entity- Returns:
- write; execute with
count(),exactlyOne()or, withreturning,one() - Throws:
IllegalArgumentException- if the entity has no metadata or no insert statement
-
update
Updates an entity (Update).- Type Parameters:
T- entity type- Parameters:
entity- entity- Returns:
- write
- Throws:
IllegalArgumentException- if the entity has no metadata or no update statement
-
upsert
Inserts or updates an entity (Upsert).- Type Parameters:
T- entity type- Parameters:
entity- entity- Returns:
- write
- Throws:
IllegalArgumentException- if the entity has no metadata or no upsert statement
-
delete
Deletes an entity (Delete).- Type Parameters:
T- entity type- Parameters:
entity- entity- Returns:
- write
- Throws:
IllegalArgumentException- if the entity has no metadata or no delete statement
-
insertAll
Inserts entities of one type as a batch (Insert, withoutreturning).- Type Parameters:
T- entity type- Parameters:
entities- entities, read lazily- Returns:
- batch; execute with
count()orexactlyEach() - Throws:
IllegalArgumentException- if the entity has no metadata or no insert statement
-
updateAll
Updates entities of one type as a batch (Update).- Type Parameters:
T- entity type- Parameters:
entities- entities, read lazily- Returns:
- batch
- Throws:
IllegalArgumentException- if the entity has no metadata or no update statement
-
upsertAll
Inserts or updates entities of one type as a batch (Upsert).- Type Parameters:
T- entity type- Parameters:
entities- entities, read lazily- Returns:
- batch
- Throws:
IllegalArgumentException- if the entity has no metadata or no upsert statement
-
deleteAll
Deletes entities of one type as a batch (Delete).- Type Parameters:
T- entity type- Parameters:
entities- entities, read lazily- Returns:
- batch
- Throws:
IllegalArgumentException- if the entity has no metadata or no delete statement
-