|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.sormula.Table<R>
R
- type of row objectspublic class Table<R>
A table within a sql database. Contains a RowTranslator
for reading/writing
data form/to database and contains methods for common input/output operations. The RowTranslator
is created based upon fields within the row class of type R and from annotations for the
class. All common input/output methods use the primary key defined by Column.primaryKey()
annotation on class R.
Example 1 - Get table from database:
Connection connection = ... // jdbc connection Database database = new Database(connection); Table<MyRow> table = database.getTable(MyRow.class); table.selectAll();
Example 2 - Instantiate table:
Connection connection = ... // jdbc connection Database database = new Database(connection); Table<MyRow> table = new Table<MyRow>(database, MyRow.class); table.selectAll();
Example 3 - Instantiate a table subclass:
public class MyCustomTable extends Table<MyRow> { public MyCustomTable(Connection connection) { super(connection, MyRow.class); } ... } Connection connection = ... // jdbc connection Database database = new Database(connection); MyCustomTable table = new MyCustomTable(database); table.selectAll();
Constructor Summary | |
---|---|
Table(Database database,
java.lang.Class<R> rowClass)
Constructs for a database and the class that used for row objects. |
Method Summary | ||
---|---|---|
int |
delete(java.lang.Object... parameters)
Deletes by primary key. |
|
int |
delete(R row)
Deletes by primary key. |
|
int |
deleteAll()
Deletes all rows in table. |
|
int |
deleteAll(java.util.Collection<R> rows)
Deletes many rows by primary key. |
|
int |
deleteAllBatch(java.util.Collection<R> rows)
Deletes many rows by primary key in batch mode. |
|
Database |
getDatabase()
Gets the database supplied in constructor. |
|
java.util.List<java.lang.reflect.Field> |
getLazySelectCascadeFields()
Gets fields for table record class that are annotated with SelectCascade.lazy() true. |
|
NameTranslator |
getNameTranslator()
Deprecated. |
|
java.util.List<? extends NameTranslator> |
getNameTranslators()
Gets the name translators used by translateName(String) . |
|
java.lang.String |
getQualifiedTableName()
Gets table name used in sql statements with optional schema prefix if necessary. |
|
java.lang.Class<R> |
getRowClass()
|
|
RowTranslator<R> |
getRowTranslator()
Gets the row translator for converting row values to/from sql parameters. |
|
java.lang.String |
getTableName()
Gets the table name used in sql statements. |
|
TypeTranslator<?> |
getTypeTranslator(java.lang.Class<?> typeClass)
Gets the translator to use to convert a value to a prepared statement or to convert from a result set. |
|
protected java.util.List<? extends NameTranslator> |
initNameTranslators(Row rowAnnotation)
Initialize all name translators annotated on table. |
|
protected RowTranslator<R> |
initRowTranslator()
Creates a RowTranslator for use by this table. |
|
protected java.lang.String |
initTableName(Row rowAnnotation)
Initializes table name from row annotation. |
|
protected void |
initTypeTranslatorMap()
Initializes type translators for table. |
|
int |
insert(R row)
Inserts one row into table. |
|
int |
insertAll(java.util.Collection<R> rows)
Inserts collection of rows. |
|
int |
insertAllBatch(java.util.Collection<R> rows)
Inserts a collection of rows in batch mode. |
|
R |
newRow()
Creates new instance of row. |
|
void |
putTypeTranslator(java.lang.Class<?> typeClass,
TypeTranslator<?> typeTranslator)
Overrides translator defined in Database for all operations on this table. |
|
void |
putTypeTranslator(java.lang.String typeClassName,
TypeTranslator<?> typeTranslator)
Same as putTypeTranslator(Class, TypeTranslator) but uses class name. |
|
int |
save(R row)
Uses SaveOperation to update an existing row or insert row if it
is not already in database. |
|
int |
saveAll(java.util.Collection<R> rows)
Uses SaveOperation to update an existing rows or insert rows if they
are not already in database. |
|
R |
select(java.lang.Object... primaryKeys)
Selects one row in table using primary key. |
|
java.util.List<R> |
selectAll()
Selects all rows in table. |
|
java.util.List<R> |
selectAllCustom(java.lang.String customSql,
java.lang.Object... parameters)
Select list of rows using custom sql. |
|
java.util.List<R> |
selectAllWhere(java.lang.String whereConditionName,
java.lang.Object... parameters)
Selects list of rows 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 rows for where condition and parameters. |
|
|
selectAvg(java.lang.String expression)
Selects average value. |
|
|
selectAvg(java.lang.String expression,
java.lang.String whereConditionName,
java.lang.Object... parameters)
Selects average value. |
|
int |
selectCount()
Gets count of all rows in table. |
|
|
selectCount(java.lang.String expression)
Selects count of rows. |
|
int |
selectCount(java.lang.String whereConditionName,
java.lang.Object... parameters)
Selects count for a subset of rows. |
|
|
selectCount(java.lang.String expression,
java.lang.String whereConditionName,
java.lang.Object... parameters)
Selects count of rows. |
|
R |
selectCustom(java.lang.String customSql,
java.lang.Object... parameters)
Selects one row using custom sql. |
|
|
selectMax(java.lang.String expression)
Selects maximum value. |
|
|
selectMax(java.lang.String expression,
java.lang.String whereConditionName,
java.lang.Object... parameters)
Selects maximum value. |
|
|
selectMin(java.lang.String expression)
Selects minimum value. |
|
|
selectMin(java.lang.String expression,
java.lang.String whereConditionName,
java.lang.Object... parameters)
Selects minimum value. |
|
|
selectSum(java.lang.String expression)
Selects sum. |
|
|
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 row for where condition and parameters. |
|
void |
setTableName(java.lang.String tableName)
Sets the table name to use in sql statements. |
|
java.lang.String |
translateName(java.lang.String javaName)
Converts a Java class or field name to corresponding SQL name. |
|
int |
update(R row)
Updates one row in table by primary key. |
|
int |
updateAll(java.util.Collection<R> rows)
Updates collection of rows using primary key. |
|
int |
updateAllBatch(java.util.Collection<R> rows)
Updates collection of rows 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 |
---|
public Table(Database database, java.lang.Class<R> rowClass) throws SormulaException
RowTranslator
for mapping row objects to/from database.
database
- database for this tablerowClass
- row objects are of this type
SormulaException
- if errorMethod Detail |
---|
protected void initTypeTranslatorMap() throws SormulaException
putTypeTranslator(Class, TypeTranslator)
.
SormulaException
- if errorprotected java.util.List<? extends NameTranslator> initNameTranslators(Row rowAnnotation)
Table
may
contain NameTranslator
annotations instead of annotating row class.
rowAnnotation
- the annotation that defines name translator(s)
protected java.lang.String initTableName(Row rowAnnotation)
Row.tableName()
. If no name is supplied, then table name will
be row class simple name, Class.getSimpleName()
.
rowAnnotation
- row annotation on table or null if none
protected RowTranslator<R> initRowTranslator() throws TranslatorException
RowTranslator
for use by this table. Invoked by constructor.
TranslatorException
- if errorpublic Database getDatabase()
public java.lang.Class<R> getRowClass()
public java.lang.String getTableName()
public void setTableName(java.lang.String tableName)
NameTranslator
if specified. Use this to override the default.
tableName
- table name (without schema prefix)public java.lang.String getQualifiedTableName()
@Deprecated public NameTranslator getNameTranslator()
Row.nameTranslator()
.
Use getNameTranslators()
instead of this method.
public java.util.List<? extends NameTranslator> getNameTranslators()
translateName(String)
.
public java.lang.String translateName(java.lang.String javaName)
NameTranslator.translate(String, Class)
for all name translators in
the order that they are defined.
Row.nameTranslators()
,
Database.addNameTranslatorClass(Class)
public RowTranslator<R> getRowTranslator()
public java.util.List<java.lang.reflect.Field> getLazySelectCascadeFields()
SelectCascade.lazy()
true.
public R newRow() throws SormulaException
SormulaException
- if errorpublic void putTypeTranslator(java.lang.Class<?> typeClass, TypeTranslator<?> typeTranslator)
Database
for all operations on this table. See
Database.putTypeTranslator(Class, TypeTranslator)
for an explanation
of translators.
putTypeTranslator
in interface TypeTranslatorMap
typeClass
- class that translator operates upontypeTranslator
- to use for typeClasspublic void putTypeTranslator(java.lang.String typeClassName, TypeTranslator<?> typeTranslator)
putTypeTranslator(Class, TypeTranslator)
but uses class name. Usefull for adding
primative types like "int", "boolean", "float", etc.
putTypeTranslator
in interface TypeTranslatorMap
typeClassName
- class name that translator operates upontypeTranslator
- translator to use for typeClasspublic TypeTranslator<?> getTypeTranslator(java.lang.Class<?> typeClass)
Database.getTypeTranslator(Class)
. See
Database.getTypeTranslator(Class)
for more details.
getTypeTranslator
in interface TypeTranslatorMap
typeClass
- class that translator operates upon
public java.util.List<R> selectAll() throws SormulaException
Example:
Database database = ... Table<Order> table = database.getTable(Order.class); List<Order> orders = table.selectAll();
SormulaException
- if errorpublic R select(java.lang.Object... primaryKeys) throws SormulaException
Column.primaryKey()
annotations.
Example:
Database database = ... Table<Order> table = database.getTable(Order.class); int orderNumber = 123456; // primary key Order order = table.select(orderNumber);
primaryKeys
- primary key values to use for select (must be in same order as
primary key columns appear with row class)
SormulaException
- if errorpublic R selectWhere(java.lang.String whereConditionName, java.lang.Object... parameters) throws SormulaException
whereConditionName
- name of where condition to use; empty string to select arbitrary row in tableparameters
- parameter values for where condition
SormulaException
- if errorpublic java.util.List<R> selectAllWhere(java.lang.String whereConditionName, java.lang.Object... parameters) throws SormulaException
whereConditionName
- name of where condition to use; empty string to select all rows in tableparameters
- parameter values for where condition
SormulaException
- if errorpublic java.util.List<R> selectAllWhereOrdered(java.lang.String whereConditionName, java.lang.String orderByName, java.lang.Object... parameters) throws SormulaException
whereConditionName
- name of where condition to use; empty string to select all rows in tableorderByName
- name of order phrase to use as defined in OrderBy.name()
parameters
- parameter values for where condition
SormulaException
- if errorpublic java.util.List<R> selectAllCustom(java.lang.String customSql, java.lang.Object... parameters) throws SormulaException
Example:
Database database = ... Table<Order> table = database.getTable(Order.class); List<Order> orders = table.selectAllCustom("where orderdate >= '2011-01-01'");
customSql
- custom sql to be appended to base sql (for example, "where somecolumn=?")parameters
- parameter value to be set in customSql
SormulaException
- if errorpublic R selectCustom(java.lang.String customSql, java.lang.Object... parameters) throws SormulaException
Example:
Database database = ... Table<Order> table = database.getTable(Order.class); Order order = table.selectCustom("where orderdate >= '2011-01-01'");
customSql
- custom sql to be appended to base sql (for example, "where somecolumn=?")parameters
- parameter value to be set in customSql
SormulaException
- if errorpublic int selectCount() throws SormulaException
SormulaException
- if errorpublic int selectCount(java.lang.String whereConditionName, java.lang.Object... parameters) throws SormulaException
Example:
Database database = ... Table<Order> table = database.getTable(Order.class); // quantityExceeds is the name of a Where annotation on Order that filters quantity >= ? int bigOrderCount = table.selectCount("quanityExceeds", 100);
whereConditionName
- name of where condition to use; empty string to count all rows in tableparameters
- parameter values for where condition
SormulaException
- if errorpublic <T> T selectCount(java.lang.String expression) throws SormulaException
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.
T
- aggregate result typeexpression
- expression to use as parameter to function; typically it is the name of a column
SormulaException
- if errorpublic <T> T selectCount(java.lang.String expression, java.lang.String whereConditionName, java.lang.Object... parameters) throws SormulaException
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.
T
- aggregate result typeexpression
- expression to use as parameter to function; typically it is the name of a columnwhereConditionName
- name of where condition to use; empty string to count all rows in tableparameters
- parameter values for where condition
SormulaException
- if errorpublic <T> T selectMin(java.lang.String expression) throws SormulaException
T
- aggregate result typeexpression
- expression to use as parameter to function; typically it is the name of a column
SormulaException
- if errorpublic <T> T selectMin(java.lang.String expression, java.lang.String whereConditionName, java.lang.Object... parameters) throws SormulaException
T
- aggregate result typeexpression
- expression to use as parameter to function; typically it is the name of a columnwhereConditionName
- name of where condition to use; empty string to count all rows in tableparameters
- parameter values for where condition
SormulaException
- if errorpublic <T> T selectMax(java.lang.String expression) throws SormulaException
T
- aggregate result typeexpression
- expression to use as parameter to function; typically it is the name of a column
SormulaException
- if errorpublic <T> T selectMax(java.lang.String expression, java.lang.String whereConditionName, java.lang.Object... parameters) throws SormulaException
T
- aggregate result typeexpression
- expression to use as parameter to function; typically it is the name of a columnwhereConditionName
- name of where condition to use; empty string to count all rows in tableparameters
- parameter values for where condition
SormulaException
- if errorpublic <T> T selectAvg(java.lang.String expression) throws SormulaException
T
- aggregate result typeexpression
- expression to use as parameter to function; typically it is the name of a column
SormulaException
- if errorpublic <T> T selectAvg(java.lang.String expression, java.lang.String whereConditionName, java.lang.Object... parameters) throws SormulaException
T
- aggregate result typeexpression
- expression to use as parameter to function; typically it is the name of a columnwhereConditionName
- name of where condition to use; empty string to count all rows in tableparameters
- parameter values for where condition
SormulaException
- if errorpublic <T> T selectSum(java.lang.String expression) throws SormulaException
T
- aggregate result typeexpression
- expression to use as parameter to function; typically it is the name of a column
SormulaException
- if errorpublic <T> T selectSum(java.lang.String expression, java.lang.String whereConditionName, java.lang.Object... parameters) throws SormulaException
T
- aggregate result typeexpression
- expression to use as parameter to function; typically it is the name of a columnwhereConditionName
- name of where condition to use; empty string to count all rows in tableparameters
- parameter values for where condition
SormulaException
- if errorpublic int insert(R row) throws SormulaException
Example:
Database database = ... Table<Student> table = database.getTable(Student.class); Student s = new Student(); s.setId(1234); s.setFirstName("Jeff"); s.setLastName("Miller"); s.setGraduationDate(new Date(System.currentTimeMillis())); table.insert(s);
row
- row to insert
SormulaException
- if errorpublic int insertAll(java.util.Collection<R> rows) throws SormulaException
Example:
Database database = ... Table<Student> table = database.getTable(Student.class); ArrayList<Student> list = new ArrayList<Student>(); list.add(s1); list.add(s2); list.add(s3); table.insertAll(list);
rows
- rows to insert
SormulaException
- if errorpublic int insertAllBatch(java.util.Collection<R> rows) throws SormulaException
ModifyOperation.setBatch(boolean)
.
rows
- rows to insert
SormulaException
- if errorpublic int update(R row) throws SormulaException
Column.primaryKey()
annotations.
Example:
Database database = ... Table<Student> table = database.getTable(Student.class); Student s = table.select(id); s.setGraduationDate(...); table.update(s);
row
- row to update
SormulaException
- if errorpublic int updateAll(java.util.Collection<R> rows) throws SormulaException
Column.primaryKey()
annotations.
Example:
Database database = ... Table<Student> table = database.getTable(Student.class); List<Student> list = table.selectAll(); for (Student s: list) s.setGraduationDate(...); table.updateAll(list);
rows
- rows to update
SormulaException
- if errorpublic int updateAllBatch(java.util.Collection<R> rows) throws SormulaException
Column.primaryKey()
annotations. See limitations about batch updates
in ModifyOperation.setBatch(boolean)
.
rows
- rows to update
SormulaException
- if errorpublic int delete(java.lang.Object... parameters) throws SormulaException
Column.primaryKey()
annotations.
Example:
Database database = ... Table<Student> table = database.getTable(Student.class); table.delete(1234); // deletes student with id of 1234
parameters
- where condition values to use for delete (must be in same order as
primary key fields appear with row class)
SormulaException
- if errorpublic int delete(R row) throws SormulaException
Column.primaryKey()
annotations.
Example:
Database database = ... Table<Student> table = database.getTable(Student.class); // delete student with id of 1234 Student s = table.select(1234); table.delete(s);
row
- get primary key values from this row
SormulaException
- if errorpublic int deleteAll(java.util.Collection<R> rows) throws SormulaException
Column.primaryKey()
annotations.
Example:
Database database = ... Table<Student> table = database.getTable(Student.class); List<Student> list = getSomeStudents(); table.deleteAll(list);
rows
- get primary key values from each row in this collection
SormulaException
- if errorpublic int deleteAllBatch(java.util.Collection<R> rows) throws SormulaException
Column.primaryKey()
annotations. See limitations about batch deletes
in ModifyOperation.setBatch(boolean)
.
rows
- get primary key values from each row in this collection
SormulaException
- if errorpublic int deleteAll() throws SormulaException
Example:
Database database = ... Table<Student> table = database.getTable(Student.class); table.deleteAll();
SormulaException
- if errorpublic int save(R row) throws SormulaException
SaveOperation
to update an existing row or insert row if it
is not already in database.
row
- row to save
SormulaException
- if errorpublic int saveAll(java.util.Collection<R> rows) throws SormulaException
SaveOperation
to update an existing rows or insert rows if they
are not already in database.
rows
- collection of new and/or existing rows to save (may be mixture of new and existing)
SormulaException
- if error
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |