- java.lang.Object
-
- org.panteleyev.persistence.DAO
-
public class DAO extends Object
Persistence API entry point.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
DAO.DatabaseType
Supported database types.
-
Constructor Summary
Constructors Constructor Description DAO()
Creates DAO object.DAO(DataSource ds, DAO.DatabaseType databaseType)
Creates DAO object with predefined data source.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
createTables(List<Class<? extends Record>> tables)
This method creates table for the specified classes according to their annotations.void
delete(Integer id, Class<? extends Record> clazz)
Deletes record from the database.void
delete(Record record)
Deleted record from the database.void
deleteAll(Class<? extends Record> table)
Deletes all records from table.void
deleteAll(Connection connection, Class<? extends Record> table)
Deletes all records from table using provided connection.void
dropTables(List<Class<? extends Record>> tables)
Drops specified tables according to their annotations.<K extends Number>
KgeneratePrimaryKey(Class<? extends Record<K>> clazz)
Returns next available primary key value.<K,T extends Record<K>>
Tget(K id, Class<? extends T> clazz)
Retrieves record from the database using record primary key.<T extends Record>
List<T>getAll(Class<T> clazz)
Retrieves all records of the specified type.<K,T extends Record<K>>
voidgetAll(Class<T> clazz, Map<K,T> result)
Retrieves all records of the specified type and fills the map.Connection
getConnection()
Returns connection for the current data source.DataSource
getDataSource()
Return current data source object.<T extends Record>
voidinsert(int size, List<T> records)
This method inserts multiple records with predefined id using batch insert.<T extends Record>
voidinsert(Connection conn, int size, List<T> records)
This method inserts multiple records with predefined id using batch insert.void
insert(Connection conn, Record record)
This method inserts new record with predefined id into the database.void
insert(Record record)
This method inserts new record with predefined id into the database.void
preload(Collection<Class<? extends Record>> tables)
Pre-loads necessary information from the just opened database.protected void
resetPrimaryKey(Class<? extends Record> table)
Resets primary key generation for the given table.void
setDataSource(DataSource ds, DAO.DatabaseType databaseType)
Sets a new data source.void
truncate(List<Class<? extends Record>> tables)
Truncates tables removing all records.void
update(Connection conn, Record record)
Updates record in the database.void
update(Record record)
Updates record in the database.
-
-
-
Constructor Detail
-
DAO
public DAO()
Creates DAO object. Data source should be set later usingsetDataSource(DataSource, DatabaseType)
method.
-
DAO
public DAO(DataSource ds, DAO.DatabaseType databaseType)
Creates DAO object with predefined data source.- Parameters:
ds
- data sourcedatabaseType
- type of the database
-
-
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 sourcedatabaseType
- 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 typeT
- type of the record- Parameters:
id
- record idclazz
- record class- Returns:
- record
-
getAll
public <T extends Record> List<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 keyT
- type of the record- Parameters:
clazz
- record classresult
- 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 ofRecord
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 connectionrecord
- 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 batchrecords
- 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 connectionsize
- size of the batchrecords
- list of records
-
update
public void update(Record record)
Updates record in the database. This method returns instance of theRecord
, 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 theRecord
, i.e. supplied object is not changed.- Parameters:
conn
- SQL connectionrecord
- 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 recordclazz
- 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 connectiontable
- 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 usesTRUNCATE TABLE table_name
command. As SQLite does not support this commandDELETE 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 togeneratePrimaryKey(Class)
will return 1. This method should only be used in case of manual table truncate.- Parameters:
table
- table class
-
-