# Interface Dao<T>

public interface Dao<T>

Runtime contract a build-time-generated `@Entity` data access object implements. Application code rarely references `Dao` by name -- the typed dao is reached through `EntityManager.dao(EntityClass.class)`.

All methods throw `IOException` for the same reasons `com.codename1.db` does: the underlying SQLite driver propagates IO failures, locking failures, and constraint violations through `IOException`.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `attach(Database db)`
  Connects this dao to a `Database` instance.
  `void`
  `createTable()`
  `CREATE TABLE IF NOT EXISTS ...`.
  `void`
  `delete(T entity)`
  `DELETE ... WHERE id = ?`.
  `void`
  `dropTable()`
  `DROP TABLE IF EXISTS ...`.
  `List<T>`
  `find(String where, Object... params)`
  Free-form WHERE clause; `where` is appended verbatim after `WHERE` and `params` fills the `?` placeholders.
  `List<T>`
  `findAll()`
  `SELECT * FROM ...`.
  `T`
  `findById(Object id)`
  `SELECT ... WHERE id = ?`.
  `void`
  `insert(T entity)`
  `INSERT INTO ...`.
  `String`
  `tableName()`
  The SQL table name.
  `Class<T>`
  `type()`
  The entity class this dao handles.
  `void`
  `update(T entity)`
  `UPDATE ... WHERE id = ?`.

- ## Method Details

  - ### type

    Class<T> type()
    The entity class this dao handles.

  - ### tableName

    String tableName()
    The SQL table name. Defaults to the simple class name; an explicit `@Entity(table="...")` wins.

  - ### attach

    void attach(Database db)
    Connects this dao to a `Database` instance. Called by `EntityManager.dao(Class)`; the same dao is reused for the lifetime of the entity manager.

  - ### createTable

    void createTable() throws java.io.IOException
    `CREATE TABLE IF NOT EXISTS ...`. Idempotent.
    Throws:
      `java.io.IOException`

  - ### dropTable

    void dropTable() throws java.io.IOException
    `DROP TABLE IF EXISTS ...`.
    Throws:
      `java.io.IOException`

  - ### insert

    void insert(T entity) throws java.io.IOException
    `INSERT INTO ...`. When the entity has an auto-increment `@Id`, the generated id is written back into the instance via `SELECT last_insert_rowid()`.
    Throws:
      `java.io.IOException`

  - ### update

    void update(T entity) throws java.io.IOException
    `UPDATE ... WHERE id = ?`.
    Throws:
      `java.io.IOException`

  - ### delete

    void delete(T entity) throws java.io.IOException
    `DELETE ... WHERE id = ?`.
    Throws:
      `java.io.IOException`

  - ### findById

    T findById(Object id) throws java.io.IOException
    `SELECT ... WHERE id = ?`. Returns `null` when no row matches.
    Throws:
      `java.io.IOException`

  - ### findAll

    List<T> findAll() throws java.io.IOException
    `SELECT * FROM ...`. Returns every row mapped to an instance of the entity class.
    Throws:
      `java.io.IOException`

  - ### find

    List<T> find(String where, Object... params) throws java.io.IOException

    Free-form WHERE clause; `where` is appended verbatim after `WHERE` and `params` fills the `?` placeholders.

        users.find("age > ? AND city = ?", 18, "Berlin");

    Throws:
      `java.io.IOException`