|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.sormula.Database
public class Database
Source of Table
objects for reading/writing from/to database. For single threaded use
only. Construct new instances for each transaction and/or thread.
Example - Construct database from JDBC connection:
Connection connection = ... // jdbc connection Database database = new Database(connection); Table<MyRow> table = database.getTable(MyRow.class); table.selectAll();
Example - Construct database from JNDI:
Database database = new Database("java:comp/env/mydatasource"); Table<MyRow> table = database.getTable(MyRow.class); table.selectAll();
Constructor Summary | |
---|---|
Database(java.sql.Connection connection)
Constructs for no schema. |
|
Database(java.sql.Connection connection,
java.lang.String schema)
Constructs for schema. |
|
Database(javax.sql.DataSource dataSource)
Constructs for no schema. |
|
Database(javax.sql.DataSource dataSource,
java.lang.String schema)
Constructs for schema. |
|
Database(java.lang.String dataSourceName)
Constructs for JNDI name and no schema. |
|
Database(java.lang.String dataSourceName,
java.lang.String schema)
Constructs for JNDI name and schema. |
Method Summary | ||
---|---|---|
void |
addNameTranslatorClass(java.lang.Class<? extends NameTranslator> nameTranslatorClass)
Adds a default name translator class. |
|
void |
addTable(Table<?> table)
Adds a table object to cache to be used for row objects of type Object.getClass() . |
|
void |
close()
Dereferences all objects used. |
|
OperationTime |
createOperationTime(java.lang.String timingId,
java.lang.String description)
Creates an OperationTime instance with this database total time,
getTotalOperationTime() as the parent time. |
|
java.sql.Connection |
getConnection()
Gets connection to use for sql operations. |
|
javax.sql.DataSource |
getDataSource()
Gets data source supplied in constructors, Database(DataSource) and
Database(DataSource, String) . |
|
java.lang.String |
getDataSourceName()
Gets the JNDI name to use to look up DataSource . |
|
java.lang.Class<? extends NameTranslator> |
getNameTranslatorClass()
Deprecated. |
|
java.util.List<java.lang.Class<? extends NameTranslator>> |
getNameTranslatorClasses()
Gets the default name translator classes for tables when none is specified for the table. |
|
OperationTime |
getOperationTime(java.lang.String timingId)
Gets the operation time for a specific timing id. |
|
java.util.Map<java.lang.String,OperationTime> |
getOperationTimeMap()
Gets operation times for all operations have been executed. |
|
java.lang.String |
getSchema()
Gets the schema name supplied in constructor. |
|
|
getTable(java.lang.Class<R> rowClass)
Gets table object for reading/writing row objects of type R from/to table. |
|
|
getTable(java.lang.Class<R> rowClass,
boolean create)
Gets table object for reading/writing row objects of type R from/to table. |
|
OperationTime |
getTotalOperationTime()
|
|
Transaction |
getTransaction()
Gets transaction for connection. |
|
TypeTranslator<?> |
getTypeTranslator(java.lang.Class<?> typeClass)
Gets the translator to use to convert a value to a prepared statement and to convert a value from a result set. |
|
TypeTranslator<?> |
getTypeTranslator(java.lang.String typeClassName)
Same as getTypeTranslator(Class) but uses class name. |
|
protected void |
init(java.sql.Connection connection,
java.lang.String schema)
Invoked by constructor to initialize. |
|
protected Transaction |
initTransaction(java.sql.Connection connection)
Contructs a transaction to use. |
|
protected void |
initTypeTranslatorMap()
Invoked by constructor to initialize default type translators and type translators annotated on Database class. |
|
boolean |
isReadOnly()
Gets read-only indicator. |
|
boolean |
isTimings()
Gets status of timings. |
|
void |
logTimings()
Logs all times from operation time map, getOperationTimeMap() to log. |
|
void |
putTypeTranslator(java.lang.Class<?> typeClass,
TypeTranslator<?> typeTranslator)
Defines the translator to use to convert a value to a prepared statement or to convert a value from a result set. |
|
void |
putTypeTranslator(java.lang.String typeClassName,
TypeTranslator<?> typeTranslator)
Same as putTypeTranslator(Class, TypeTranslator) but uses class name. |
|
void |
removeNameTranslatorClass(java.lang.Class<? extends NameTranslator> nameTranslatorClass)
Removes a name translator class. |
|
void |
setNameTranslatorClass(java.lang.Class<? extends NameTranslator> nameTranslatorClass)
Deprecated. |
|
void |
setReadOnly(boolean readOnly)
Sets read-only indicator. |
|
void |
setTimings(boolean timings)
Sets timings enabled for all database operations. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public Database(java.lang.String dataSourceName) throws SormulaException
DataSource
will be obtained from JNDI look up of dataSourceName. Typically web containers
require data source to be configured with a name like "someds" but the full path to the
data source contains an implied context of "java:comp/env". So the constructor parameter,
dataSourceName, would be "java:comp/env/someds".
Connection will be obtained with DataSource.getConnection()
and connection will be
closed when close()
is invoked.
Use this constructor when a DataSource
is required, for example when
DurableLazySelector
is used.
dataSourceName
- JNDI full path to data source definition; example "java:comp/env/someds"
SormulaException
- if errorpublic Database(java.lang.String dataSourceName, java.lang.String schema) throws SormulaException
DataSource
will be obtained from JNDI look up of dataSourceName. Typically web containers
require data source to be configured with a name like "someds" but the full path to the
data source contains an implied context of "java:comp/env". So the constructor parameter,
dataSourceName, would be "java:comp/env/someds".
Connection will be obtained with DataSource.getConnection()
and connection will be
closed when close()
is invoked.
Use this constructor when a DataSource
is required, for example when
DurableLazySelector
is used.
dataSourceName
- JNDI full path to data source definition; example "java:comp/env/someds"schema
- name of schema to be prefixed to table name in all table names in sql statements;
Connection.getCatalog()
is typically the schema name but catalog methods are inconsistently
supported by jdbc drivers
SormulaException
- if errorpublic Database(javax.sql.DataSource dataSource) throws SormulaException
Connection will be obtained with DataSource.getConnection()
and connection will be
closed when close()
is invoked.
Use this constructor when a DataSource
is required, for example when
DurableLazySelector
is used.
dataSource
- obtain JDBC connection from this data source
SormulaException
- if errorpublic Database(javax.sql.DataSource dataSource, java.lang.String schema) throws SormulaException
Connection will be obtained with DataSource.getConnection()
and connection will be
closed when close()
is invoked.
Use this constructor when a DataSource
is required, for example when
DurableLazySelector
is used.
dataSource
- obtain JDBC connection from this data sourceschema
- name of schema to be prefixed to table name in all table names in sql statements;
Connection.getCatalog()
is typically the schema name but catalog methods are inconsistently
supported by jdbc drivers
SormulaException
- if errorpublic Database(java.sql.Connection connection)
connection
- JDBC connectionpublic Database(java.sql.Connection connection, java.lang.String schema)
connection
- JDBC connectionschema
- name of schema to be prefixed to table name in all table names in sql statements;
Connection.getCatalog()
is typically the schema name but catalog methods are inconsistently
supported by jdbc driversMethod Detail |
---|
public java.lang.String getDataSourceName()
DataSource
. This name was supplied in constructors:
Database(String)
and Database(String, String)
.
public javax.sql.DataSource getDataSource()
Database(DataSource)
and
Database(DataSource, String)
.
protected void init(java.sql.Connection connection, java.lang.String schema) throws SormulaException
connection
- JDBC connectionschema
- name of schema to be prefixed to table name in all table names in sql statements;
Connection.getCatalog()
is typically the schema name but catalog methods are inconsistently
SormulaException
- if errorprotected void initTypeTranslatorMap() throws SormulaException
Database
class.
Connection.getCatalog()
is typically the schema name but catalog methods are inconsistently
SormulaException
- if errorprotected Transaction initTransaction(java.sql.Connection connection) throws SormulaException
Transaction
. Subclasses can
override to use transaction other than the default.
connection
- JDBC connection to database
SormulaException
- if errorpublic Transaction getTransaction()
Transaction
is optional for most situations. Transaction
is provided for basic JDBC transaction support if desired. Since Database
is
single threaded, only one Transaction
object exists per instance of Database
.
AbstractLazySelector
and subclasses start a new transaction if Transaction.isActive()
is
false. Use a custom subclass of AbstractLazySelector
and override begin, commit, and rollback
methods to avoid this default behavior.
public void close()
Database(Connection)
or Database(Connection, String)
.
Closes connection if this class created the connection. Connection is created by this class
when any of the data source constructors are used. This method is required when any of these
data source constructors are used: Database(DataSource)
, Database(DataSource, String)
,
Database(String)
, Database(String, String)
.
public java.sql.Connection getConnection()
SqlOperation
subclasses
to obtain the connection to use for the operation methods.
public java.lang.String getSchema()
public boolean isReadOnly()
SqlOperation.isReadOnly()
public void setReadOnly(boolean readOnly)
ModifyOperation
will fail with an exception. By default
read-only is false. Set to true as a safe-guard to prevent accidental
modification of database.
readOnly
- true to prevent modify operationsSqlOperation.setReadOnly(boolean)
public <R> Table<R> getTable(java.lang.Class<R> rowClass, boolean create) throws SormulaException
This method is optional. A table object may also be created with Table
constructor.
R
- row class typerowClass
- annotations on this class determine mapping from row objects to/from databasecreate
- true to create new instance of Table
if Database(Connection)
does not have one yet;
false to return null if no Table
instance is found for rowClass
SormulaException
- if errorpublic <R> Table<R> getTable(java.lang.Class<R> rowClass) throws SormulaException
This method is optional. A table object may also be created with Table
constructor.
R
- row class typerowClass
- annotations on this class determine mapping from row objects to/from database
SormulaException
- if errorpublic void addTable(Table<?> table)
Object.getClass()
. Use this method to save table in cache. This method provides
a way to ensure that a custom subclass of Table
is returned from getTable(Class)
for the table row class.
table
- table object to add to cache (table row class cannonical name is key)@Deprecated public java.lang.Class<? extends NameTranslator> getNameTranslatorClass()
getNameTranslatorClasses()
instead this method.
@Deprecated public void setNameTranslatorClass(java.lang.Class<? extends NameTranslator> nameTranslatorClass)
Column.translator()
. A new instance is created for each table.
Use addNameTranslatorClass(Class)
instead of this method.
nameTranslatorClass
- default name translator class; null for nonepublic java.util.List<java.lang.Class<? extends NameTranslator>> getNameTranslatorClasses()
Table.translateName(String)
public void addNameTranslatorClass(java.lang.Class<? extends NameTranslator> nameTranslatorClass)
nameTranslatorClass
- class to use to translate table/column namesTable.translateName(String)
public void removeNameTranslatorClass(java.lang.Class<? extends NameTranslator> nameTranslatorClass)
nameTranslatorClass
- class to removeTable.translateName(String)
public boolean isTimings()
public void setTimings(boolean timings)
logTimings()
to write the timings to the log. Use
getOperationTime(String)
or getOperationTimeMap()
to get timings
that have been recorded.
timings
- true to enable all operations to record execution timings; false for no
operation timings by default (can be overriden by each SqlOperation
)public OperationTime getOperationTime(java.lang.String timingId)
timingId
- timing id to get
OperationTime
for timing id or null if timing id is not in mappublic OperationTime createOperationTime(java.lang.String timingId, java.lang.String description)
OperationTime
instance with this database total time,
getTotalOperationTime()
as the parent time. The new instance is
add to operation time map, getOperationTimeMap()
.
timingId
- timing id for created instancedescription
- description for created instance
OperationTime
with id and descriptionpublic OperationTime getTotalOperationTime()
public java.util.Map<java.lang.String,OperationTime> getOperationTimeMap()
public void logTimings()
getOperationTimeMap()
to log. Total
operation time, getTotalOperationTime()
is logged also.
public void putTypeTranslator(java.lang.Class<?> typeClass, TypeTranslator<?> typeTranslator)
ImplicitType
annotation.
By default, all primative types and most subclasses of TypeTranslator
in
org.sormula.translator.standard package are added during initialization of this class.
Use this method to override default translators or to add a new translator.
These tranlators may be overridden for a table by Table.putTypeTranslator(Class, TypeTranslator)
.
putTypeTranslator
in interface TypeTranslatorMap
typeClass
- class that translator operates upontypeTranslator
- translator 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)
getTypeTranslator
in interface TypeTranslatorMap
typeClass
- class that translator operates upon
public TypeTranslator<?> getTypeTranslator(java.lang.String typeClassName)
getTypeTranslator(Class)
but uses class name.
typeClassName
- class name that translator operates upon
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |