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 Details

    • nextId

      static long nextId()
      New identifier for an Sql instance, 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

      Query query(String sql)
      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:
      true if the transaction was opened via Database.read(Transaction)
    • select

      default <T> EntityQuery<T> select(EntitySelect<T> select)
      Query of an entity declared on a constant of an EntitySelect enum (Select).
      Type Parameters:
      T - entity type
      Parameters:
      select - query, for example CellSelect.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

      default <T> EntityWrite<T> insert(T entity)
      Inserts an entity (Insert).
      Type Parameters:
      T - entity type
      Parameters:
      entity - entity
      Returns:
      write; execute with count(), exactlyOne() or, with returning, one()
      Throws:
      IllegalArgumentException - if the entity has no metadata or no insert statement
    • update

      default <T> EntityWrite<T> update(T entity)
      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

      default <T> EntityWrite<T> upsert(T entity)
      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

      default <T> EntityWrite<T> delete(T entity)
      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

      default <T> Batch insertAll(Iterable<? extends T> entities)
      Inserts entities of one type as a batch (Insert, without returning).
      Type Parameters:
      T - entity type
      Parameters:
      entities - entities, read lazily
      Returns:
      batch; execute with count() or exactlyEach()
      Throws:
      IllegalArgumentException - if the entity has no metadata or no insert statement
    • updateAll

      default <T> Batch updateAll(Iterable<? extends T> entities)
      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

      default <T> Batch upsertAll(Iterable<? extends T> entities)
      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

      default <T> Batch deleteAll(Iterable<? extends T> entities)
      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