org.sormula.active
Class ActiveTable<R extends ActiveRecord<R>>

java.lang.Object
  extended by org.sormula.active.ActiveTable<R>
Type Parameters:
R - record type

public class ActiveTable<R extends ActiveRecord<R>>
extends java.lang.Object

Performs all database operations for ActiveRecord objects. This class is used by ActiveRecord to perform single record operations. This class may also be created explicity to invoke collection methods like, saveAll(Collection), insertAll(Collection), updateAll(Collection), deleteAll(Collection), and select methods.

Since:
1.7 and 2.1
Author:
Jeff Miller

Constructor Summary
ActiveTable(ActiveDatabase activeDatabase, java.lang.Class<R> recordClass)
          Constructs for a database and record type.
ActiveTable(java.lang.Class<R> recordClass)
          Constructs for default active database and a record type.
 
Method Summary
 int delete(R record)
          Deletes by primary key.
 int deleteAll()
          Deletes all records in table.
 int deleteAll(java.util.Collection<R> records)
          Deletes many records by primary key.
 int deleteAllBatch(java.util.Collection<R> records)
          Deletes many records by primary key in batch mode.
 ActiveDatabase getActiveDatabase()
          Gets the active database.
 java.lang.Class<R> getRecordClass()
          Gets the record class.
 int insert(R record)
          Inserts one record into table.
 int insertAll(java.util.Collection<R> records)
          Inserts collection of records.
 int insertAllBatch(java.util.Collection<R> records)
          Inserts collection of records in batch mode.
 R newActiveRecord()
          Creates a new active record of type R using zero-arg constructor.
 int save(R record)
          Updates an existing record or insert record if it is not already in database.
 int saveAll(java.util.Collection<R> records)
          Updates an existing records or insert records if they are not already in database.
 R select(java.lang.Object... primaryKeys)
          Selects one record in table using primary key.
 java.util.List<R> selectAll()
          Selects all records in table.
 java.util.List<R> selectAllCustom(java.lang.String customSql, java.lang.Object... parameters)
          Select list of records using custom sql.
 java.util.List<R> selectAllWhere(java.lang.String whereConditionName, java.lang.Object... parameters)
          Selects list of records for where condition and parameters.
 java.util.List<R> selectAllWhereOrdered(java.lang.String whereConditionName, java.lang.String orderByName, java.lang.Object... parameters)
          Selects list of records for where condition and parameters.
<T> T
selectAvg(java.lang.String expression)
          Selects average value.
<T> T
selectAvg(java.lang.String expression, java.lang.String whereConditionName, java.lang.Object... parameters)
          Selects average value.
<T> T
selectCount()
          Selects count of records using "select count(*) from ...".
<T> T
selectCount(java.lang.String expression)
          Selects count of records.
<T> T
selectCount(java.lang.String expression, java.lang.String whereConditionName, java.lang.Object... parameters)
          Selects count of records.
 R selectCustom(java.lang.String customSql, java.lang.Object... parameters)
          Select list of records using custom sql.
<T> T
selectMax(java.lang.String expression)
          Selects maximum value.
<T> T
selectMax(java.lang.String expression, java.lang.String whereConditionName, java.lang.Object... parameters)
          Selects maximum value.
<T> T
selectMin(java.lang.String expression)
          Selects minimum value.
<T> T
selectMin(java.lang.String expression, java.lang.String whereConditionName, java.lang.Object... parameters)
          Selects minimum value.
<T> T
selectSum(java.lang.String expression)
          Selects sum.
<T> T
selectSum(java.lang.String expression, java.lang.String whereConditionName, java.lang.Object... parameters)
          Selects sum.
 R selectWhere(java.lang.String whereConditionName, java.lang.Object... parameters)
          Selects one record for where condition and parameters.
 int update(R record)
          Updates one record in table by primary key.
 int updateAll(java.util.Collection<R> records)
          Updates collection of records using primary key.
 int updateAllBatch(java.util.Collection<R> records)
          Updates collection of records using primary key in batch mode.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ActiveTable

public ActiveTable(java.lang.Class<R> recordClass)
            throws ActiveException
Constructs for default active database and a record type. Uses the default active database that is configured with ActiveDatabase.setDefault(ActiveDatabase). The default active database must be configured prior to first database access by this table.

Parameters:
recordClass - Java class of record
Throws:
ActiveException - if error

ActiveTable

public ActiveTable(ActiveDatabase activeDatabase,
                   java.lang.Class<R> recordClass)
            throws ActiveException
Constructs for a database and record type.

Parameters:
activeDatabase - active database that defines data source for records
recordClass - Java class of record
Throws:
ActiveException - if activeDatabase is null
Method Detail

getActiveDatabase

public ActiveDatabase getActiveDatabase()
Gets the active database.

Returns:
active database that was supplied in constructor

getRecordClass

public java.lang.Class<R> getRecordClass()
Gets the record class.

Returns:
record class supplied in constructor

newActiveRecord

public R newActiveRecord()
                                          throws ActiveException
Creates a new active record of type R using zero-arg constructor. New record is attached to this tables active database with ActiveRecord.attach(ActiveDatabase).

Returns:
new active record
Throws:
ActiveException - if error

select

public R select(java.lang.Object... primaryKeys)
                                 throws ActiveException
Selects one record in table using primary key. Primary key must be defined by one or more Column.primaryKey() annotations. ActiveRecord.attach(ActiveDatabase) is invoked on selected record to attach it to the active database of this table.

Example:

 ActiveDatabase activeDatabase = ...
 ActiveTable<Order> table = new ActiveTable<Order>(activeDatabase, Order.class);
 int orderNumber = 123456; // primary key
 Order order = table.select(orderNumber);
 

Parameters:
primaryKeys - primary key values to use for select (must be in same order as primary key columns appear with record class)
Returns:
record or null if none found
Throws:
ActiveException - if error

selectWhere

public R selectWhere(java.lang.String whereConditionName,
                     java.lang.Object... parameters)
                                      throws ActiveException
Selects one record for where condition and parameters. ActiveRecord.attach(ActiveDatabase) is invoked on selected record to attach it to the active database of this table.

Parameters:
whereConditionName - name of where condition to use; empty string to select arbitrary record in table
parameters - parameter values for where condition
Returns:
record for where condition and parameters; null if none found
Throws:
ActiveException - if error

selectCustom

public R selectCustom(java.lang.String customSql,
                      java.lang.Object... parameters)
                                       throws ActiveException
Select list of records using custom sql. ActiveRecord.attach(ActiveDatabase) is invoked on selected record to attach it to the active database of this table.

Example:

 ActiveDatabase activeDatabase = ...
 ActiveTable<Order> table = new ActiveTable<Order>(activeDatabase, Order.class);
 List<Order> orders = table.selectAllCustom("where orderdate >= '2011-01-01'");
 

Parameters:
customSql - custom sql to be appended to base sql (for example, "where somecolumn=?")
parameters - parameter value to be set in customSql
Returns:
record or null if none found
Throws:
ActiveException - if error

selectAll

public java.util.List<R> selectAll()
                                                    throws ActiveException
Selects all records in table. ActiveRecord.attach(ActiveDatabase) is invoked on selected records to attach them to the active database of this table.

Example:

 ActiveDatabase activeDatabase = ...
 ActiveTable<Order> table = new ActiveTable<Order>(activeDatabase, Order.class);
 List<Order> orders = table.selectAll();
 

Returns:
list of all records; empty list if none found
Throws:
ActiveException - if error

selectAllWhere

public java.util.List<R> selectAllWhere(java.lang.String whereConditionName,
                                        java.lang.Object... parameters)
                                                         throws ActiveException
Selects list of records for where condition and parameters. ActiveRecord.attach(ActiveDatabase) is invoked on selected records to attach them to the active database of this table.

Parameters:
whereConditionName - name of where condition to use; empty string to select all records in table
parameters - parameter values for where condition
Returns:
records for where condition and parameters; empty list if none found
Throws:
ActiveException - if error

selectAllWhereOrdered

public java.util.List<R> selectAllWhereOrdered(java.lang.String whereConditionName,
                                               java.lang.String orderByName,
                                               java.lang.Object... parameters)
                                                                throws ActiveException
Selects list of records for where condition and parameters. ActiveRecord.attach(ActiveDatabase) is invoked on selected records to attach them to the active database of this table.

Parameters:
whereConditionName - name of where condition to use; empty string to select all records in table
orderByName - name of order phrase to use as defined in OrderBy.name()
parameters - parameter values for where condition
Returns:
records for where condition and parameters; empty list if none found
Throws:
ActiveException - if error

selectAllCustom

public java.util.List<R> selectAllCustom(java.lang.String customSql,
                                         java.lang.Object... parameters)
                                                          throws ActiveException
Select list of records using custom sql. ActiveRecord.attach(ActiveDatabase) is invoked on selected records to attach them to the active database of this table.

Example:

 ActiveDatabase activeDatabase = ...
 ActiveTable<Order> table = new ActiveTable<Order>(activeDatabase, Order.class);
 List<Order> orders = table.selectAllCustom("where orderdate >= '2011-01-01'");
 

Parameters:
customSql - custom sql to be appended to base sql (for example, "where somecolumn=?")
parameters - parameter value to be set in customSql
Returns:
list of records selected; empty list if none found
Throws:
ActiveException - if error

selectCount

public <T> T selectCount()
              throws ActiveException
Selects count of records using "select count(*) from ...".

The data type returned from database is the same type as the expression. The return type is database dependent since expression is "*".

Type Parameters:
T - aggregate result type
Returns:
count of records for expression
Throws:
ActiveException - if error

selectCount

public <T> T selectCount(java.lang.String expression)
              throws ActiveException
Selects count of records.

The data type returned from database is the same type as the expression. For example, if expression is a column, then the returned type is the same type as column. If expression is "*", then return types are database dependent.

Type Parameters:
T - aggregate result type
Parameters:
expression - expression to use as parameter to function; typically it is the name of a column
Returns:
count of records for expression
Throws:
ActiveException - if error

selectCount

public <T> T selectCount(java.lang.String expression,
                         java.lang.String whereConditionName,
                         java.lang.Object... parameters)
              throws ActiveException
Selects count of records.

The data type returned from database is the same type as the expression. For example, if expression is a column, then the returned type is the same type as column. If expression is "*", then return types are database dependent.

Type Parameters:
T - aggregate result type
Parameters:
expression - expression to use as parameter to function; typically it is the name of a column
whereConditionName - name of where condition to use; empty string to count all records in table
parameters - parameter values for where condition
Returns:
count of records for expression and where condition
Throws:
ActiveException - if error

selectMin

public <T> T selectMin(java.lang.String expression)
            throws ActiveException
Selects minimum value.

Type Parameters:
T - aggregate result type
Parameters:
expression - expression to use as parameter to function; typically it is the name of a column
Returns:
minimum value for expression
Throws:
ActiveException - if error

selectMin

public <T> T selectMin(java.lang.String expression,
                       java.lang.String whereConditionName,
                       java.lang.Object... parameters)
            throws ActiveException
Selects minimum value.

Type Parameters:
T - aggregate result type
Parameters:
expression - expression to use as parameter to function; typically it is the name of a column
whereConditionName - name of where condition to use; empty string to count all records in table
parameters - parameter values for where condition
Returns:
minimum value for expression and where condition
Throws:
ActiveException - if error

selectMax

public <T> T selectMax(java.lang.String expression)
            throws ActiveException
Selects maximum value.

Type Parameters:
T - aggregate result type
Parameters:
expression - expression to use as parameter to function; typically it is the name of a column
Returns:
maximum value for expression
Throws:
ActiveException - if error

selectMax

public <T> T selectMax(java.lang.String expression,
                       java.lang.String whereConditionName,
                       java.lang.Object... parameters)
            throws ActiveException
Selects maximum value.

Type Parameters:
T - aggregate result type
Parameters:
expression - expression to use as parameter to function; typically it is the name of a column
whereConditionName - name of where condition to use; empty string to count all records in table
parameters - parameter values for where condition
Returns:
maximum value for expression and where condition
Throws:
ActiveException - if error

selectSum

public <T> T selectSum(java.lang.String expression)
            throws ActiveException
Selects sum.

Type Parameters:
T - aggregate result type
Parameters:
expression - expression to use as parameter to function; typically it is the name of a column
Returns:
sum for expression
Throws:
ActiveException - if error

selectSum

public <T> T selectSum(java.lang.String expression,
                       java.lang.String whereConditionName,
                       java.lang.Object... parameters)
Selects sum.

Type Parameters:
T - aggregate result type
Parameters:
expression - expression to use as parameter to function; typically it is the name of a column
whereConditionName - name of where condition to use; empty string to count all records in table
parameters - parameter values for where condition
Returns:
sum for expression and where condition
Throws:
ActiveException - if error

selectAvg

public <T> T selectAvg(java.lang.String expression)
            throws ActiveException
Selects average value.

Type Parameters:
T - aggregate result type
Parameters:
expression - expression to use as parameter to function; typically it is the name of a column
Returns:
average value for expression
Throws:
ActiveException - if error

selectAvg

public <T> T selectAvg(java.lang.String expression,
                       java.lang.String whereConditionName,
                       java.lang.Object... parameters)
            throws ActiveException
Selects average value.

Type Parameters:
T - aggregate result type
Parameters:
expression - expression to use as parameter to function; typically it is the name of a column
whereConditionName - name of where condition to use; empty string to count all records in table
parameters - parameter values for where condition
Returns:
average value for expression and where condition
Throws:
ActiveException - if error

save

public int save(R record)
         throws ActiveException
Updates an existing record or insert record if it is not already in database. ActiveRecord.attach(ActiveDatabase) is invoked on record to attach it to the active database of this table prior to save. Typically ActiveRecord.save() is used instead of this method.

Parameters:
record - record to save
Returns:
count of records affected
Throws:
ActiveException - if error

saveAll

public int saveAll(java.util.Collection<R> records)
            throws ActiveException
Updates an existing records or insert records if they are not already in database. ActiveRecord.attach(ActiveDatabase) is invoked on records to attach them to the active database of this table prior to saving them.

Parameters:
records - collection of new and/or existing records to save (may be mixture of new and existing)
Returns:
count of records affected
Throws:
ActiveException - if error

insert

public int insert(R record)
           throws ActiveException
Inserts one record into table. ActiveRecord.attach(ActiveDatabase) is invoked on record to attach it to the active database of this table prior to insert. Typically ActiveRecord.insert() is used instead of this method.

Example:

 ActiveDatabase activeDatabase = ...
 ActiveTable<Student> table = new ActiveTable<Student>(activeDatabase, Student.class);
 Student s = new Student();
 s.setId(1234);
 s.setFirstName("Jeff");
 s.setLastName("Miller");
 s.setGraduationDate(new Date(System.currentTimeMillis()));
 table.insert(s);
 

Parameters:
record - record to insert
Returns:
count of records affected
Throws:
ActiveException - if error

insertAll

public int insertAll(java.util.Collection<R> records)
              throws ActiveException
Inserts collection of records. ActiveRecord.attach(ActiveDatabase) is invoked on records to attach them to the active database of this table prior to insert.

Example:

 ActiveDatabase activeDatabase = ...
 ActiveTable<Student> table = new ActiveTable<Student>(activeDatabase, Student.class);
 ArrayList<Student> list = new ArrayList<Student>();
 list.add(s1);
 list.add(s2);
 list.add(s3);
 table.insertAll(list);
 

Parameters:
records - records to insert
Returns:
count of records affected
Throws:
ActiveException - if error

insertAllBatch

public int insertAllBatch(java.util.Collection<R> records)
                   throws ActiveException
Inserts collection of records in batch mode. ActiveRecord.attach(ActiveDatabase) is invoked on records to attach them to the active database of this table prior to insert. See limitations about batch inserts in ModifyOperation.setBatch(boolean).

Parameters:
records - records to insert
Returns:
count of records affected
Throws:
ActiveException - if error
Since:
1.9 and 2.3

update

public int update(R record)
           throws ActiveException
Updates one record in table by primary key. Primary key must be defined by one or more Column.primaryKey() annotations. ActiveRecord.attach(ActiveDatabase) is invoked on record to attach it to the active database of this table prior to update. Typically ActiveRecord.update() is used instead of this method.

Example:

 ActiveDatabase activeDatabase = ...
 ActiveTable<Student> table = new ActiveTable<Student>(activeDatabase, Student.class);
 Student s = table.select(id);
 s.setGraduationDate(...);
 table.update(s);
 

Parameters:
record - record to update
Returns:
count of records affected
Throws:
ActiveException - if error

updateAll

public int updateAll(java.util.Collection<R> records)
              throws ActiveException
Updates collection of records using primary key. Primary key must be defined by one or more Column.primaryKey() annotations. ActiveRecord.attach(ActiveDatabase) is invoked on records to attach them to the active database of this table prior to update.

Example:

 ActiveDatabase activeDatabase = ...
 ActiveTable<Student> table = new ActiveTable<Student>(activeDatabase, Student.class);
 List<Student> list = table.selectAll();
 for (Student s: list)
     s.setGraduationDate(...);
   
 table.updateAll(list);
 

Parameters:
records - records to update
Returns:
count of records affected
Throws:
ActiveException - if error

updateAllBatch

public int updateAllBatch(java.util.Collection<R> records)
                   throws ActiveException
Updates collection of records using primary key in batch mode. Primary key must be defined by one or more Column.primaryKey() annotations. ActiveRecord.attach(ActiveDatabase) is invoked on records to attach them to the active database of this table prior to update. See limitations about batch updates in ModifyOperation.setBatch(boolean).

Parameters:
records - records to update
Returns:
count of records affected
Throws:
ActiveException - if error
Since:
1.9 and 2.3

delete

public int delete(R record)
           throws ActiveException
Deletes by primary key. Primary key must be defined by one or more Column.primaryKey() annotations. ActiveRecord.attach(ActiveDatabase) is invoked on record to attach it to the active database of this table prior to delete. Typically ActiveRecord.delete() is used instead of this method.

Example:

 ActiveDatabase activeDatabase = ...
 ActiveTable<Student> table = new ActiveTable<Student>(activeDatabase, Student.class);
 // delete student with id of 1234
 Student s = table.select(1234);
 table.delete(s);
 

Parameters:
record - get primary key values from this record
Returns:
count of records affected
Throws:
ActiveException - if error

deleteAll

public int deleteAll()
              throws ActiveException
Deletes all records in table.

Example:

 ActiveDatabase activeDatabase = ...
 ActiveTable<Student> table = new ActiveTable<Student>(activeDatabase, Student.class);
 table.deleteAll();
 

Returns:
count of records affected
Throws:
ActiveException - if error

deleteAll

public int deleteAll(java.util.Collection<R> records)
              throws ActiveException
Deletes many records by primary key. Primary key must be defined by one or more Column.primaryKey() annotations. ActiveRecord.attach(ActiveDatabase) is invoked on records to attach them to the active database of this table prior to delete.

Example:

 ActiveDatabase activeDatabase = ...
 ActiveTable<Student> table = new ActiveTable<Student>(activeDatabase, Student.class);
 List<Student> list = getSomeStudents();
 table.deleteAll(list);
 

Parameters:
records - get primary key values from each record in this collection
Returns:
count of records affected
Throws:
ActiveException - if error

deleteAllBatch

public int deleteAllBatch(java.util.Collection<R> records)
                   throws ActiveException
Deletes many records by primary key in batch mode. Primary key must be defined by one or more Column.primaryKey() annotations. ActiveRecord.attach(ActiveDatabase) is invoked on records to attach them to the active database of this table prior to delete.

Parameters:
records - get primary key values from each record in this collection
Returns:
count of records affected
Throws:
ActiveException - if error
Since:
1.9 and 2.3