public class Database extends java.lang.Object implements TypeTranslatorMap, java.lang.AutoCloseable
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 and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
void |
addNameTranslatorClass(java.lang.Class<? extends NameTranslator> nameTranslatorClass)
Adds a default name translator class.
|
void |
addTable(Table<?> table)
Adds a table object to map to be used for row objects of type
Object.getClass() . |
void |
close()
Closes connection if this class created the connection from a data source.
|
OperationTime |
createOperationTime(java.lang.String timingId,
java.lang.String description)
Creates an
OperationTime instance with this database total time,
getTotalOperationTime() as the parent time. |
void |
flush()
Invokes
Table.flush() on all known table objects in this database. |
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.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.
|
<R> Table<R> |
getTable(java.lang.Class<R> rowClass)
Gets table object for reading/writing row objects of type R from/to
table.
|
<R> Table<R> |
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.lang.String schema)
Invoked by constructor to initialize.
|
protected Transaction |
initTransaction(java.sql.Connection connection)
Constructs a transaction to use.
|
protected void |
initTypeTranslatorMap()
Invoked by constructor to initialize default type translators and type translators
annotated on
Database class. |
boolean |
isAutoGeneratedKeys()
Gets the most recent value of
setAutoGeneratedKeys(boolean) . |
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 |
setAutoGeneratedKeys(boolean autoGeneratedKeys)
Sets the default way identity columns are processed for newly created
Table objects. |
void |
setConnection(java.sql.Connection connection)
Sets new connection to use by this class and all related classes.
|
void |
setReadOnly(boolean readOnly)
Sets read-only indicator.
|
void |
setTimings(boolean timings)
Sets timings enabled for all database operations.
|
void |
setTransaction(Transaction transaction)
Sets a custom implementation of
Transaction subclass. |
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 driversSormulaException
- 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 sourceSormulaException
- 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 driversSormulaException
- 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 driverspublic 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)
.public boolean isAutoGeneratedKeys()
setAutoGeneratedKeys(boolean)
. The default is true.public void setAutoGeneratedKeys(boolean autoGeneratedKeys)
Table
objects. Table
objects created will have Table.setAutoGeneratedKeys(boolean)
set to
isAutoGeneratedKeys()
. Setting to false is useful when a column
is defined as an identity column but the key will be manually created by the application.
Changing this value will have no affect on Table
objects that have already been
created.
autoGeneratedKeys
- true if Table
objects created for this database should
assume that database will automatically generate key values for
fields defined as identity (Column.identity()
is true)Column.identity()
,
Table.isAutoGeneratedKeys()
,
Statement.getGeneratedKeys()
protected void init(java.lang.String schema) throws SormulaException
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 jdbc drivers are not consistentSormulaException
- if errorprotected void initTypeTranslatorMap() throws SormulaException
Database
class.
Field Type | Translator |
---|---|
boolean / java.lang.Boolean | BooleanTranslator |
byte / java.lang.Byte | ByteTranslator |
double / java.lang.Double | DoubleTranslator |
float / java.lang.Float | FloatTranslator |
int / java.lang.Integer | IntegerTranslator |
long / java.lang.Long | LongTranslator |
short / java.lang.Short | ShortTranslator |
java.math.BigDecimal | BigDecimalTranslator |
java.lang.String | StringTranslator |
java.util.Date | DateTranslator |
java.sql.Date | SqlDateTranslator |
java.sql.Time | SqlTimeTranslator |
java.sql.Timestamp | SqlTimestampTranslator |
java.util.GregorianCalendar | GregorianCalendarTranslator |
java.time.LocalDate | LocalDateTranslator |
java.time.LocalTime | LocalTimeTranslator |
java.time.Instant | InstantTranslator |
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 databaseSormulaException
- if errorpublic Transaction getTransaction()
Transaction
is optional for some 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 setTransaction(Transaction transaction)
Transaction
subclass. This method should be
invoked prior to creating Table
objects that use this Database
and
prior to using getTable(Class)
or getTable(Class, boolean)
.transaction
- transaction for this databasepublic void flush() throws SormulaException
Table.flush()
on all known table objects in this database.SormulaException
- if errorpublic void close()
Database(DataSource)
,
Database(DataSource, String)
, Database(String)
, Database(String, String)
.
If the Database
instance was created using a data source, then it may be reused after closing.
A new connection will be obtained from the data source if needed.
Does not close connection if these constructors were used:
Database(Connection)
or Database(Connection, String)
.
close
in interface java.lang.AutoCloseable
public java.sql.Connection getConnection()
SqlOperation
to obtain the connection to use for the operation methods.public void setConnection(java.sql.Connection connection) throws SormulaException
Table
, SqlOperation
, etc.
objects are created and you want to reuse them for a different connection. This method
is primarily intended to be used on Database
objects that were created
with the Connection constructors: Database(Connection)
and
Database(Connection, String)
.
Warning: Do not use this method while the transaction for this Database
is active.
Warning: If any of the datasource constructors were used to create Database then you must explicitly close the old connection to avoid a connection leak.
Note: If any datasource constructor was used, then setting to null will force new connection
to be obtained from datasource but the old connection will not be closed. Old connection
must be explicitly closed. Using close()
is a simpler way to achieve the same outcome.
connection
- new connection to use; null to force new connection from
datasourceSormulaException
- if the transaction (getTransaction()
) is activepublic java.lang.String getSchema()
public boolean isReadOnly()
SqlOperation.isReadOnly()
public void setReadOnly(boolean readOnly)
ModifyOperation
will fail with a ReadOnlyException
. 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 rowClassSormulaException
- 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 databaseSormulaException
- if errorpublic void addTable(Table<?> table)
Object.getClass()
. Use this method to save table in map. 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 map (table row class cannonical name is key)public 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 overridden by each SqlOperation
)public OperationTime getOperationTime(java.lang.String timingId)
timingId
- timing id to getOperationTime
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 instanceOperationTime
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 primitive 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 translators 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. Useful for adding
primitive 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 uponpublic TypeTranslator<?> getTypeTranslator(java.lang.String typeClassName)
getTypeTranslator(Class)
but uses class name.typeClassName
- class name that translator operates upon