- java.lang.Object
-
- org.panteleyev.persistence.DAO
-
public class DAO extends java.lang.ObjectPersistence API entry point.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcreateTables(java.util.List<java.lang.Class<? extends Record>> tables)This method creates table for the specified classes according to their annotations.voiddelete(java.lang.Integer id, java.lang.Class<? extends Record> clazz)Deletes record from the database.voiddelete(Record record)Deleted record from the database.voiddeleteAll(java.lang.Class<? extends Record> table)Deletes all records from table.voiddeleteAll(java.sql.Connection connection, java.lang.Class<? extends Record> table)Deletes all records from table using provided connection.voiddropTables(java.util.List<java.lang.Class<? extends Record>> tables)Drops specified tables according to their annotations.java.lang.IntegergeneratePrimaryKey(java.lang.Class<? extends Record> clazz)Returns next available primary key value.<T extends Record>
Tget(java.lang.Integer id, java.lang.Class<? extends T> clazz)Retrieves record from the database using record ID.<T extends Record>
java.util.List<T>getAll(java.lang.Class<T> clazz)Retrieves all records of the specified type.<T extends Record>
voidgetAll(java.lang.Class<T> clazz, java.util.Map<java.lang.Integer,T> result)Retrieves all records of the specified type and fills the map.java.sql.ConnectiongetConnection()Returns connection for the current data source.javax.sql.DataSourcegetDataSource()Return current data source object.<T extends Record>
voidinsert(int size, java.util.List<T> records)This method inserts multiple records with predefined id using batch insert.<T extends Record>
voidinsert(java.sql.Connection conn, int size, java.util.List<T> records)This method inserts multiple records with predefined id using batch insert.<T extends Record>
Tinsert(java.sql.Connection conn, T record)This method inserts new record with predefined id into the database.<T extends Record>
Tinsert(T record)This method inserts new record with predefined id into the database.voidpreload(java.util.Collection<java.lang.Class<? extends Record>> tables)Pre-loads necessary information from the just opened database.protected voidresetPrimaryKey(java.lang.Class<? extends Record> table)Resets primary key generation for the given table.voidsetDataSource(javax.sql.DataSource ds)Sets a new data source.voidtruncate(java.util.List<java.lang.Class<? extends Record>> tables)Truncates tables removing all records.<T extends Record>
Tupdate(java.sql.Connection conn, T record)Updates record in the database.<T extends Record>
Tupdate(T record)Updates record in the database.
-
-
-
Method Detail
-
getDataSource
public javax.sql.DataSource getDataSource()
Return current data source object.- Returns:
- data source object
-
setDataSource
public void setDataSource(javax.sql.DataSource ds)
Sets a new data source.- Parameters:
ds- data source
-
getConnection
public java.sql.Connection getConnection() throws java.sql.SQLExceptionReturns connection for the current data source.- Returns:
- connection
- Throws:
java.sql.SQLException- in case of SQL error
-
get
public <T extends Record> T get(java.lang.Integer id, java.lang.Class<? extends T> clazz)
Retrieves record from the database using record ID.- Type Parameters:
T- type of the record- Parameters:
id- record idclazz- record class- Returns:
- record
-
getAll
public <T extends Record> java.util.List<T> getAll(java.lang.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 <T extends Record> void getAll(java.lang.Class<T> clazz, java.util.Map<java.lang.Integer,T> result)
Retrieves all records of the specified type and fills the map.- Type Parameters:
T- type of the record- Parameters:
clazz- record classresult- map to fill
-
createTables
public void createTables(java.util.List<java.lang.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(java.util.Collection<java.lang.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 ofRecordtypes
-
generatePrimaryKey
public java.lang.Integer generatePrimaryKey(java.lang.Class<? extends Record> clazz)
Returns next available primary key value. This method is thread safe.- Parameters:
clazz- record class- Returns:
- primary key value
-
insert
public <T extends Record> T insert(T 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.- Type Parameters:
T- type of the record- Parameters:
record- record- Returns:
- inserted record
- Throws:
java.lang.IllegalArgumentException- if id of the record is 0
-
insert
public <T extends Record> T insert(java.sql.Connection conn, T 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.- Type Parameters:
T- type of the record- Parameters:
conn- SQL connectionrecord- record- Returns:
- inserted record
- Throws:
java.lang.IllegalArgumentException- if id of the record is 0
-
insert
public <T extends Record> void insert(int size, java.util.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(java.sql.Connection conn, int size, java.util.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 <T extends Record> T update(T record)
Updates record in the database. This method returns instance of theRecord, i.e. supplied object is not changed.- Type Parameters:
T- record type- Parameters:
record- record- Returns:
- updated record
- Throws:
java.lang.IllegalArgumentException- if id of the record is 0
-
update
public <T extends Record> T update(java.sql.Connection conn, T record)
Updates record in the database. This method returns instance of theRecord, i.e. supplied object is not changed.- Type Parameters:
T- record type- Parameters:
conn- SQL connectionrecord- record- Returns:
- updated record
- Throws:
java.lang.IllegalArgumentException- if id of the record is 0
-
delete
public void delete(Record record)
Deleted record from the database.- Parameters:
record- record to delete
-
delete
public void delete(java.lang.Integer id, java.lang.Class<? extends Record> clazz)Deletes record from the database.- Parameters:
id- id of the recordclazz- record type
-
deleteAll
public void deleteAll(java.lang.Class<? extends Record> table)
Deletes all records from table.- Parameters:
table- table
-
deleteAll
public void deleteAll(java.sql.Connection connection, java.lang.Class<? extends Record> table)Deletes all records from table using provided connection.- Parameters:
connection- SQL connectiontable- table class
-
truncate
public void truncate(java.util.List<java.lang.Class<? extends Record>> tables)
Truncates tables removing all records. Primary key generation starts from 1 again. For MySQL this operation usesTRUNCATE TABLE table_namecommand. As SQLite does not support this commandDELETE FROM table_nameis used instead.- Parameters:
tables- tables to truncate
-
resetPrimaryKey
protected void resetPrimaryKey(java.lang.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
-
dropTables
public void dropTables(java.util.List<java.lang.Class<? extends Record>> tables)
Drops specified tables according to their annotations.- Parameters:
tables- table classes
-
-