# 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 IOException
    `CREATE TABLE IF NOT EXISTS ...`. Idempotent.
    Throws:
      `IOException`

  - ### dropTable

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

  - ### insert

    void insert(T entity) throws 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:
      `IOException`

  - ### update

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

  - ### delete

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

  - ### findById

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

  - ### findAll

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

  - ### find

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

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

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

    Throws:
      `IOException`