public abstract class DBDatabase extends DBObject
It provides access to the various database objects such as tables, views and relations.
It also provides methods to execute DQL and DML SQL-commands.
Modifier and Type | Class and Description |
---|---|
static class |
DBDatabase.DBSystemDate
This class represents the database systems current date and time.
|
Modifier and Type | Field and Description |
---|---|
static String |
DATETIME_PATTERN |
protected DBDatabaseDriver |
driver |
static String |
EMPTY_STRING |
protected String |
instanceId |
protected String |
linkName |
protected long |
longRunndingStmtThreshold |
protected List<DBRelation> |
relations |
protected String |
schema
the database schema *
|
static DBDatabase.DBSystemDate |
SYSDATE |
protected List<DBTable> |
tables |
protected List<DBView> |
views |
Constructor and Description |
---|
DBDatabase()
Constructs a new DBDatabase object set the variable 'schema' = null.
|
DBDatabase(String schema)
Constructs a new DBDatabase object and sets the specified schema object.
|
DBDatabase(String schema,
String linkName)
Constructs a new DBDatabase object and sets the specified schema object.
|
Modifier and Type | Method and Description |
---|---|
DBRelation |
addRelation(DBRelation.DBReference reference)
Adds a foreign key relation to the database.
|
DBRelation |
addRelation(DBRelation.DBReference ref1,
DBRelation.DBReference ref2)
Add a foreign key relation to the database.
|
DBRelation |
addRelation(String name,
DBRelation.DBReference... references)
Adds a foreign key relation to the database.
|
protected void |
addTable(DBTable table)
Adds a DBTable object to list of database tables.
|
protected void |
addView(DBView view)
Adds a DBView object to list of database views.
|
void |
appendQualifiedName(StringBuilder buf,
String name,
boolean quoteName)
Adds a full qualified object name including schema prefix
and database link postfix (if any).
|
DBColumnExpr |
caseWhen(DBCompareExpr condition,
Object trueValue,
Object falseValue)
Creates a case column expression
"case when
|
DBColumnExpr |
caseWhenNull(DBColumnExpr expr,
Object trueValue,
Object falseValue)
Creates a case column expression that check whether a column or column expression is null
"case when
|
protected void |
checkOpen()
checks if the database has been opened or false otherwise
|
void |
close(Connection conn)
closes this database object by detaching it from the driver
this is a shortcut for calling
getDriver().closeDatabase(db, conn)
|
void |
closeResultSet(ResultSet rset)
Convenience function for closing a JDBC Resultset
Use it instead of rset.close() and stmt.close() |
void |
closeStatement(Statement stmt)
Convenience function for closing a JDBC Resultset
Use it instead of stmt.close() |
void |
commit(Connection conn)
Makes all changes made since the previous commit/rollback
permanent and releases any database locks currently held by the
Connection.
|
DBCommand |
createCommand()
Creates a new Command object for this database
|
void |
destroy()
Frees all resources and unregisters database in global map.
|
DataType |
detectDataType(Object value)
Detects the DataType of a given value.
|
int |
executeDelete(DBTable from,
DBCommand cmd,
Connection conn)
Executes a Delete statement from a command object
|
int |
executeInsert(DBCommand cmd,
Connection conn)
Executes an Insert statement from a command object
|
int |
executeInsertInto(DBTable table,
DBCommand cmd,
Connection conn)
Executes an InsertInfo statement from a command object
|
ResultSet |
executeQuery(String sqlCmd,
Object[] sqlParams,
boolean scrollable,
Connection conn)
Executes a select SQL-Statement and returns a ResultSet containing the query results.
|
int |
executeSQL(String sqlCmd,
Connection conn)
Deprecated.
This method has be deprecated in order to avoid missing command parameters for prepared statements
Instead of using this method signature please use any of the following: executeInsert(...) executeUpdate(...) executeDelete(...) or use executeSQL(String sqlCmd, Object[] sqlParams, Connection conn) |
int |
executeSQL(String sqlCmd,
Object[] sqlParams,
Connection conn) |
int |
executeSQL(String sqlCmd,
Object[] sqlParams,
Connection conn,
DBDatabaseDriver.DBSetGenKeys setGenKeys)
Executes an update, insert or delete SQL-Statement.
|
int |
executeUpdate(DBCommand cmd,
Connection conn)
Executes an Update statement from a command object
|
static DBDatabase |
findByClass(Class<? extends DBDatabase> cls)
find a database by id
|
static DBDatabase |
findById(String dbIdent)
find a database by id
|
protected void |
generateDDLScript(DBSQLScript script)
Override this to change or add DDL commands
|
void |
getCreateDDLScript(DBDatabaseDriver driver,
DBSQLScript script)
Creates a DDL Script for creating all database objects on the target database.
|
DBDatabase |
getDatabase()
Returns the database object to which this object belongs to.
|
protected String |
getDefaultId()
returns the default database id
Override this to customize
|
DBDatabaseDriver |
getDriver()
Returns the driver for this database.
|
String |
getId()
Returns the database instance id
|
String |
getLinkName()
Returns the database link name.
|
Object |
getNextSequenceValue(String seqName,
Connection conn) |
DBValueExpr |
getNullExpr()
Creates and returns a value expression for NULL
|
DBValueExpr |
getParamExpr(DBCmdParam param)
Creates and returns a value expression for a command parameter
|
String |
getQualifiedName(String name)
Deprecated.
|
DBRelation |
getRelation(String relationName)
Returns the relation of a given name
|
List<DBRelation> |
getRelations()
Returns the relations which have been defined in the database.
|
DBRowSet |
getRowSet(String name)
Finds a DBRowSet object (DBTable or DBView) by name.
|
String |
getSchema()
Returns the schema for SQL statements.
|
String |
getSchemaPrefix()
Returns the schema-prefix for SQL statements e.g.
|
DBValueExpr |
getSystemDateExpr()
Creates and returns a value object for the database systems
current date and time.
|
DBTable |
getTable(String name)
Finds a DBTable object by name.
|
List<DBTable> |
getTables()
Returns the tables which have been defined in the database.
|
Timestamp |
getUpdateTimestamp(Connection conn)
Returns a timestamp that is used for record updates.
|
DBValueExpr |
getValueExpr(boolean value)
Creates and returns a value object for the given boolean value.
|
DBValueExpr |
getValueExpr(int value)
Creates and returns a value object for the given integer value.
|
DBValueExpr |
getValueExpr(long value)
Creates and returns a value object for the given long value.
|
DBValueExpr |
getValueExpr(Object value,
DataType dataType)
Creates and returns a value object for the given scalar value.
|
DBValueExpr |
getValueExpr(String value)
Creates and returns a value object for the given string value.
|
DBView |
getView(String name)
Finds a DBView object by name.
|
List<DBView> |
getViews()
Returns the views which have been defined in the database.
|
boolean |
isOpen()
Indicates whether the database has been opened.
|
boolean |
isPreparedStatementsEnabled()
return whether prepared statements are preferred over normal statements (Default is false)
Note: This will only apply for update and insert commands as well as for read operations on a DBRecord.
|
void |
open(DBDatabaseDriver driver,
Connection conn)
Sets the database driver for this database.
|
List<Object[]> |
queryObjectList(DBCommand cmd,
Connection conn)
Returns the result of a query as a list Object-Arrays
This function should only be used for small lists.
|
int |
queryObjectList(DBCommand cmd,
Connection conn,
Collection<Object[]> result)
Adds the result of a query to a given collection.
|
int |
queryObjectList(String sqlCmd,
Object[] sqlParams,
Connection conn,
Collection<Object[]> result,
int maxRows)
Adds the result of a query to a given collection.
|
Options |
queryOptionList(DBCommand cmd,
Connection conn)
Returns a list of key value pairs from an sql query.
|
int |
queryOptionList(DBCommand cmd,
Connection conn,
Options result)
Fills an option list provided with the result from a query.
|
int |
queryOptionList(String sqlCmd,
Object[] sqlParams,
Connection conn,
Options result)
Fills an option list provided with the result from a query.
|
<T> List<T> |
querySimpleList(Class<T> c,
DBCommand cmd,
Connection conn)
Returns a one dimensional array from an sql query.
|
<T> int |
querySimpleList(Class<T> c,
DBCommand cmd,
Connection conn,
Collection<T> result)
Adds the first column of a query result to a collection.
|
<T> int |
querySimpleList(Class<T> c,
String sqlCmd,
Object[] sqlParams,
DataType dataType,
Connection conn,
Collection<T> result,
int maxRows)
Adds the first column of a query result to a collection.
|
List<Object> |
querySimpleList(DBCommand cmd,
Connection conn)
Returns a one dimensional array from an sql query.
|
int |
querySingleInt(DBCommand cmd,
Connection conn)
Returns the value of the first row/column of a sql-query as an int.
|
int |
querySingleInt(DBCommand cmd,
int defaultValue,
Connection conn)
Returns the value of the first row/column of a sql-query as an int.
|
int |
querySingleInt(String sqlCmd,
Object[] sqlParams,
int defaultValue,
Connection conn)
Returns the value of the first row/column of a sql-query as an int.
|
long |
querySingleLong(DBCommand cmd,
Connection conn)
Returns the value of the first row/column of a sql-query as a long.
|
long |
querySingleLong(DBCommand cmd,
long defaultValue,
Connection conn)
Returns the value of the first row/column of a sql-query as a long.
|
long |
querySingleLong(String sqlCmd,
Object[] sqlParams,
long defaultValue,
Connection conn)
Returns the value of the first row/column of a sql-query as a long.
|
Object[] |
querySingleRow(DBCommand cmd,
Connection conn)
Returns all values of the first row of a sql-query as an array.
|
Object[] |
querySingleRow(String sqlCmd,
Object[] sqlParams,
Connection conn)
Returns all values of the first row of a sql-query as an array.
|
String |
querySingleString(DBCommand cmd,
Connection conn)
Returns the value of the first row/column of a sql-query as a string.
|
String |
querySingleString(DBCommand cmd,
String defaultValue,
Connection conn)
Returns the value of the first row/column of a sql-query as a string.
|
String |
querySingleString(String sqlCmd,
Object[] sqlParams,
String defaultValue,
Connection conn)
Returns the value of the first row/column of a sql-query as a string.
|
Object |
querySingleValue(DBCommand cmd,
Connection conn)
Returns the value of the first row/column of a sql-query as an object.
|
Object |
querySingleValue(DBCommand cmd,
DataType dataType,
Connection conn)
Returns the value of the first row/column of a sql-query as an object.
|
Object |
querySingleValue(String sqlCmd,
Object[] sqlParams,
Connection conn)
Returns the value of the first row/column of a sql-query as an object.
|
Object |
querySingleValue(String sqlCmd,
Object[] sqlParams,
DataType dataType,
Connection conn)
Returns the value of the first row/column of a sql-query as an object.
|
protected void |
register(String dbid)
registers the database in the global list of databases
|
void |
rollback(Connection conn)
Discards all changes made since the previous commit/rollback
and releases any database locks currently held by this
Connection.
|
void |
setLinkName(String linkName)
Sets the name of the database link used to identify objects.
|
void |
setPreparedStatementsEnabled(boolean preparedStatementsEnabled)
enables or disables the use of prepared statements for update and insert commands as well as for read operations on a DBRecord.
|
void |
setSchema(String schema)
Sets the schema for SQL statements.
|
protected long longRunndingStmtThreshold
public static final DBDatabase.DBSystemDate SYSDATE
public static final String DATETIME_PATTERN
public static final String EMPTY_STRING
protected String schema
protected String linkName
protected List<DBRelation> relations
protected DBDatabaseDriver driver
protected String instanceId
public DBDatabase(String schema, String linkName)
schema
- the database schemalinkName
- the database link namepublic DBDatabase(String schema)
schema
- the database schemapublic DBDatabase()
public static DBDatabase findById(String dbIdent)
public static DBDatabase findByClass(Class<? extends DBDatabase> cls)
public void destroy()
protected void register(String dbid)
dbid
- protected String getDefaultId()
public String getId()
public DBDatabaseDriver getDriver()
public boolean isPreparedStatementsEnabled()
public void setPreparedStatementsEnabled(boolean preparedStatementsEnabled)
preparedStatementsEnabled
- public void open(DBDatabaseDriver driver, Connection conn)
driver
- the database driverconn
- the connectionpublic void close(Connection conn)
conn
- the connection to closepublic void getCreateDDLScript(DBDatabaseDriver driver, DBSQLScript script)
Once the database is open you can use getDriver().getDLLCommand()
to create, alter or delete other database objects
driver
- The driver for which to create a DDL Scriptscript
- the script object that will be completedprotected void generateDDLScript(DBSQLScript script)
script
- public DBDatabase getDatabase()
DBObject
getDatabase
in class DBObject
DBObject.getDatabase()
public String getSchema()
public void setSchema(String schema)
schema
- the schema to setpublic String getSchemaPrefix()
public String getLinkName()
public void setLinkName(String linkName)
linkName
- the database link name@Deprecated public String getQualifiedName(String name)
name
- the object's namepublic void appendQualifiedName(StringBuilder buf, String name, boolean quoteName)
buf
- the string buffer to which to append the qualified object namename
- the object's namequoteName
- use quotes or notpublic DBValueExpr getSystemDateExpr()
public DBValueExpr getValueExpr(String value)
value
- the String valuepublic DBValueExpr getValueExpr(boolean value)
value
- the Boolean valuepublic DBValueExpr getValueExpr(int value)
value
- the int valuepublic DBValueExpr getValueExpr(long value)
value
- the long valuepublic DBValueExpr getValueExpr(Object value, DataType dataType)
value
- the scalar valuedataType
- the database systems data type used for this valuepublic DBValueExpr getParamExpr(DBCmdParam param)
param
- the command parameterpublic DBValueExpr getNullExpr()
protected void addTable(DBTable table)
table
- the DBTable objectpublic List<DBTable> getTables()
public DBRowSet getRowSet(String name)
name
- the name of the tablepublic DBTable getTable(String name)
name
- the name of the tablepublic final DBRelation addRelation(DBRelation.DBReference reference)
reference
- a reference for a source and target column pairpublic final DBRelation addRelation(DBRelation.DBReference ref1, DBRelation.DBReference ref2)
ref1
- a reference for a source and target column pairref2
- a reference for a source and target column pairpublic DBRelation addRelation(String name, DBRelation.DBReference... references)
name
- the relation namereferences
- a list of source and target column pairspublic List<DBRelation> getRelations()
public DBRelation getRelation(String relationName)
protected void addView(DBView view)
view
- the DBView objectpublic List<DBView> getViews()
public DBView getView(String name)
name
- the name of the viewpublic boolean isOpen()
protected void checkOpen()
public DBCommand createCommand()
public Timestamp getUpdateTimestamp(Connection conn)
conn
- the connectionpublic Object getNextSequenceValue(String seqName, Connection conn)
public Object querySingleValue(String sqlCmd, Object[] sqlParams, DataType dataType, Connection conn)
sqlCmd
- the SQL-CommandsqlParams
- list of query parameter valuesdataType
- the expected data typeconn
- a valid connection to the database.public final Object querySingleValue(String sqlCmd, Object[] sqlParams, Connection conn)
sqlCmd
- the SQL-CommandsqlParams
- list of query parameter valuesconn
- a valid connection to the database.public final Object querySingleValue(DBCommand cmd, DataType dataType, Connection conn)
cmd
- the Command object that contains the select statementdataType
- the expected data typeconn
- a valid connection to the database.public final Object querySingleValue(DBCommand cmd, Connection conn)
cmd
- the Command object that contains the select statementconn
- a valid connection to the database.public final int querySingleInt(String sqlCmd, Object[] sqlParams, int defaultValue, Connection conn)
sqlCmd
- the SQL statementsqlParams
- list of query parameter valuesdefaultValue
- the default value if no value was returned by the databaseconn
- a valid connection to the database.public final int querySingleInt(DBCommand cmd, int defaultValue, Connection conn)
cmd
- the Command object that contains the select statementdefaultValue
- the default value if no value was returned by the databaseconn
- a valid connection to the database.public final int querySingleInt(DBCommand cmd, Connection conn)
cmd
- the Command object that contains the select statementconn
- a valid connection to the database.public final long querySingleLong(String sqlCmd, Object[] sqlParams, long defaultValue, Connection conn)
sqlCmd
- the SQL statementsqlParams
- list of query parameter valuesdefaultValue
- the default valueconn
- a valid connection to the database.public final long querySingleLong(DBCommand cmd, long defaultValue, Connection conn)
cmd
- the Command object that contains the select statementdefaultValue
- the default valueconn
- a valid connection to the database.public final long querySingleLong(DBCommand cmd, Connection conn)
cmd
- the Command object that contains the select statementconn
- a valid connection to the database.public final String querySingleString(String sqlCmd, Object[] sqlParams, String defaultValue, Connection conn)
sqlCmd
- the SQL statementsqlParams
- list of query parameter valuesdefaultValue
- the default value if no value was returned by the databaseconn
- a valid connection to the database.public final String querySingleString(DBCommand cmd, String defaultValue, Connection conn)
cmd
- the Command object that contains the select statementdefaultValue
- the default value if no value was returned by the databaseconn
- a valid connection to the database.public final String querySingleString(DBCommand cmd, Connection conn)
cmd
- the Command object that contains the select statementconn
- a valid connection to the database.public <T> int querySimpleList(Class<T> c, String sqlCmd, Object[] sqlParams, DataType dataType, Connection conn, Collection<T> result, int maxRows)
T
- the type for the listc
- the class type for the listsqlCmd
- the SQL statementdataType
- the expected data typeconn
- a valid connection to the database.maxRows
- maximum number of rows or -1 for all rowspublic final <T> int querySimpleList(Class<T> c, DBCommand cmd, Connection conn, Collection<T> result)
T
- the type for the listc
- the class type for the listcmd
- the Command object that contains the select statementconn
- a valid connection to the database.public final <T> List<T> querySimpleList(Class<T> c, DBCommand cmd, Connection conn)
T
- the type for the listc
- the class type for the listcmd
- the Command object that contains the select statementconn
- a valid connection to the database.public final List<Object> querySimpleList(DBCommand cmd, Connection conn)
cmd
- the Command object that contains the select statementconn
- a valid connection to the database.public int queryOptionList(String sqlCmd, Object[] sqlParams, Connection conn, Options result)
sqlCmd
- the SQL statementconn
- a valid connection to the database.public final int queryOptionList(DBCommand cmd, Connection conn, Options result)
cmd
- the Command object that contains the select statementconn
- a valid connection to the database.public final Options queryOptionList(DBCommand cmd, Connection conn)
cmd
- the Command object that contains the select statementconn
- a valid connection to the database.public int queryObjectList(String sqlCmd, Object[] sqlParams, Connection conn, Collection<Object[]> result, int maxRows)
This function should only be used for small lists. Otherwise a DBReader should be used!
sqlCmd
- the SQL statementconn
- a valid connection to the database.public final int queryObjectList(DBCommand cmd, Connection conn, Collection<Object[]> result)
This function should only be used for small lists. Otherwise a DBReader should be used!
cmd
- the Command object that contains the select statementconn
- a valid connection to the database.public final List<Object[]> queryObjectList(DBCommand cmd, Connection conn)
cmd
- the Command object that contains the select statementconn
- a valid connection to the database.public Object[] querySingleRow(String sqlCmd, Object[] sqlParams, Connection conn)
sqlCmd
- the SQL-CommandsqlParams
- list of query parameter valuesconn
- a valid connection to the database.public final Object[] querySingleRow(DBCommand cmd, Connection conn)
cmd
- the Command object that contains the select statementconn
- a valid connection to the database.public int executeSQL(String sqlCmd, Object[] sqlParams, Connection conn, DBDatabaseDriver.DBSetGenKeys setGenKeys)
sqlCmd
- the SQL-CommandsqlParams
- a list of objects to replace sql parametersconn
- a valid connection to the database.setGenKeys
- object to set the generated keys forpublic final int executeSQL(String sqlCmd, Object[] sqlParams, Connection conn)
@Deprecated public final int executeSQL(String sqlCmd, Connection conn)
Instead of using this method signature please use any of the following: executeInsert(...) executeUpdate(...) executeDelete(...) or use executeSQL(String sqlCmd, Object[] sqlParams, Connection conn)
public final int executeInsert(DBCommand cmd, Connection conn)
cmd
- the command object containing the insert commandconn
- a valid connection to the database.public final int executeInsertInto(DBTable table, DBCommand cmd, Connection conn)
table
- the table into which to insert the selected datacmd
- the command object containing the selection commandconn
- a valid connection to the database.public final int executeUpdate(DBCommand cmd, Connection conn)
cmd
- the command object containing the update commandconn
- a valid connection to the database.public final int executeDelete(DBTable from, DBCommand cmd, Connection conn)
from
- the database table from which to delete recordscmd
- the command object containing the delete constraintsconn
- a valid connection to the database.public ResultSet executeQuery(String sqlCmd, Object[] sqlParams, boolean scrollable, Connection conn)
sqlCmd
- the SQL-CommandsqlParams
- a list of parameters for parameter queries (may depend on driver)scrollable
- true if the reader should be scrollable or false if notconn
- a valid connection to the database.public void commit(Connection conn)
conn
- a valid database connectionpublic void rollback(Connection conn)
conn
- a valid database connectionpublic void closeStatement(Statement stmt)
stmt
- a Statement objectpublic void closeResultSet(ResultSet rset)
rset
- a ResultSet objectpublic DataType detectDataType(Object value)
value
- the value to detectpublic DBColumnExpr caseWhen(DBCompareExpr condition, Object trueValue, Object falseValue)
condition
- trueValue
- the value to select if the condition is truefalseValue
- the value to select if the condition is falsepublic DBColumnExpr caseWhenNull(DBColumnExpr expr, Object trueValue, Object falseValue)
expr
- a column or column expressiontrueValue
- the value to select if the condition is truefalseValue
- the value to select if the condition is falseCopyright © 2008–2018 Apache Software Foundation. All rights reserved.