Class DAO


  • public class DAO
    extends Object
    Persistence API entry point.
    • Method Detail

      • getDataSource

        public DataSource getDataSource()
        Return current data source object.
        Returns:
        data source object
      • setDataSource

        public void setDataSource​(DataSource ds,
                                  DAO.DatabaseType databaseType)
        Sets a new data source.
        Parameters:
        ds - data source
        databaseType - type of the database
      • getConnection

        public Connection getConnection()
                                 throws SQLException
        Returns connection for the current data source.
        Returns:
        connection
        Throws:
        SQLException - in case of SQL error
      • get

        public <K,​T extends Record<K>> T get​(K id,
                                                   Class<? extends T> clazz)
        Retrieves record from the database using record primary key.
        Type Parameters:
        K - primary key type
        T - type of the record
        Parameters:
        id - record id
        clazz - record class
        Returns:
        record
      • getAll

        public <T extends RecordList<T> getAll​(Class<T> clazz)
        Retrieves all records of the specified type.
        Type Parameters:
        T - type of the record
        Parameters:
        clazz - record class
        Returns:
        list of records
      • getAll

        public <K,​T extends Record<K>> void getAll​(Class<T> clazz,
                                                         Map<K,​T> result)
        Retrieves all records of the specified type and fills the map.
        Type Parameters:
        K - type of the primary key
        T - type of the record
        Parameters:
        clazz - record class
        result - map to fill
      • createTables

        public void createTables​(List<Class<? extends Record>> tables)
        This method creates table for the specified classes according to their annotations.
        Parameters:
        tables - list of tables
      • preload

        public void preload​(Collection<Class<? extends Record>> tables)
        Pre-loads necessary information from the just opened database. This method must be called prior to any other database operations. Otherwise primary keys may be generated incorrectly.
        Parameters:
        tables - list of Record types
      • generatePrimaryKey

        public <K extends Number> K generatePrimaryKey​(Class<? extends Record<K>> clazz)
        Returns next available primary key value. This method is thread safe. Only numeric types (int, long, Integer, Long) are currently supported.
        Type Parameters:
        K - primary key type
        Parameters:
        clazz - record class
        Returns:
        primary key value
      • insert

        public void insert​(Record record)
        This method inserts new record with predefined id into the database. No attempt to generate new id is made. Calling code must ensure that predefined id is unique.
        Parameters:
        record - record
        Throws:
        IllegalArgumentException - if id of the record is 0
      • insert

        public void insert​(Connection conn,
                           Record record)
        This method inserts new record with predefined id into the database. No attempt to generate new id is made. Calling code must ensure that predefined id is unique.
        Parameters:
        conn - SQL connection
        record - record
        Throws:
        IllegalArgumentException - if id of the record is 0
      • insert

        public <T extends Record> void insert​(int size,
                                              List<T> records)

        This method inserts multiple records with predefined id using batch insert. No attempt to generate new id is made. Calling code must ensure that predefined id is unique for all records.

        Supplied records are divided to batches of the specified size. To avoid memory issues size of the batch must be tuned appropriately.

        Type Parameters:
        T - type of records
        Parameters:
        size - size of the batch
        records - list of records
      • insert

        public <T extends Record> void insert​(Connection conn,
                                              int size,
                                              List<T> records)

        This method inserts multiple records with predefined id using batch insert. No attempt to generate new id is made. Calling code must ensure that predefined id is unique for all records.

        Supplied records are divided to batches of the specified size. To avoid memory issues size of the batch must be tuned appropriately.

        Type Parameters:
        T - type of records
        Parameters:
        conn - SQL connection
        size - size of the batch
        records - list of records
      • update

        public void update​(Record record)
        Updates record in the database. This method returns instance of the Record, i.e. supplied object is not changed.
        Parameters:
        record - record
      • update

        public void update​(Connection conn,
                           Record record)
        Updates record in the database. This method returns instance of the Record, i.e. supplied object is not changed.
        Parameters:
        conn - SQL connection
        record - record
      • delete

        public void delete​(Record record)
        Deleted record from the database.
        Parameters:
        record - record to delete
      • delete

        public void delete​(Integer id,
                           Class<? extends Record> clazz)
        Deletes record from the database.
        Parameters:
        id - id of the record
        clazz - record type
      • deleteAll

        public void deleteAll​(Class<? extends Record> table)
        Deletes all records from table.
        Parameters:
        table - table
      • deleteAll

        public void deleteAll​(Connection connection,
                              Class<? extends Record> table)
        Deletes all records from table using provided connection.
        Parameters:
        connection - SQL connection
        table - table class
      • truncate

        public void truncate​(List<Class<? extends Record>> tables)
        Truncates tables removing all records. Primary key generation starts from 1 again. For MySQL this operation uses TRUNCATE TABLE table_name command. As SQLite does not support this command DELETE FROM table_name is used instead.
        Parameters:
        tables - tables to truncate
      • resetPrimaryKey

        protected void resetPrimaryKey​(Class<? extends Record> table)
        Resets primary key generation for the given table. Next call to generatePrimaryKey(Class) will return 1. This method should only be used in case of manual table truncate.
        Parameters:
        table - table class
      • dropTables

        public void dropTables​(List<Class<? extends Record>> tables)
        Drops specified tables according to their annotations.
        Parameters:
        tables - table classes