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 |
---|---|
protected DBMSHandler |
dbms |
static String |
EMPTY_STRING |
protected boolean |
legacyDate
Flag indicating whether Bean getters / setters use java.util.Date or Java types (LocalDate, LocalDateTime)
True (default) when java.util.Date is used
|
protected List<DBRelation> |
relations |
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(DBSQLBuilder sql,
String name,
Boolean quoteName)
Adds a full qualified object name including schema prefix
and database link postfix (if any).
|
DBCaseExpr |
caseMap(DBColumnExpr column,
Map<? extends Object,? extends Object> valueMap,
Object elseValue)
Creates a SQL case expression
in the form "case [Expr] when [mapKey] then [mapValue] else [elseValue] end"
|
DBCaseExpr |
caseMap(DBColumnExpr column,
Object cmpValue,
Object trueValue,
Object falseValue)
Creates a SQL case expression
in the form "case [Expr] when [compareValue] then [trueValue] else [elseValue] end"
|
DBCaseExpr |
caseMap(DBColumnExpr column,
Options options,
Object elseValue)
Creates a SQL case expression
in the form "case [Expr] when [optionValue] then [optionText] else [elseValue] end"
|
DBCaseExpr |
caseWhen(DBCompareExpr condition,
Object trueValue,
Object falseValue)
Creates a SQL case expression
in the form "case when [condition] then [trueValue] else [falseValue] end"
This is a helper function to simplify client usage
|
DBCaseExpr |
caseWhen(Map<DBCompareExpr,? extends Object> whenMap,
Object elseValue)
Creates a SQL case expression
in the form "case when [mapKey] then [mapValue] else [elseValue] end"
|
DBCaseExpr |
caseWhenNull(DBColumnExpr column,
Object trueValue,
Object falseValue)
Creates a SQL case expression that check whether a column or column expression is null
"case when [condition] is null then [trueValue] else [falseValue] end"
This is a helper function to simplify client usage
|
boolean |
checkExists(DBContext context)
Checks if the database exists
The implementation depends on the DBMSHandler
|
protected void |
checkOpen()
checks if the database has been opened or false otherwise
|
void |
close(DBContext context)
Closes this database object by detaching it from the dbms
|
DBCommand |
createCommand()
Creates a new Command object for this database
Deprecated instead use context.createCommand();
|
DataType |
detectDataType(Object value)
Detects the DataType of a given value.
|
void |
discard()
Frees all resources and unregisters database in global map.
|
static DBDatabase |
findByClass(Class<? extends DBDatabase> cls)
find a database by id
|
static DBDatabase |
findByIdentifier(String dbIdent)
find a database by id
|
protected void |
generateDDLScript(DBSQLScript script)
Override this to change or add DDL commands
|
Class<?> |
getColumnJavaType(DBColumnExpr expr)
Returns the java class type for a given dataType
|
void |
getCreateDDLScript(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.
|
<T extends DBMSHandler> |
getDbms()
Returns the DBMS Handler for this database.
|
protected String |
getDefaultIdentifier()
returns the default database identifier
Override this to customize
|
String |
getIdentifier()
Returns the database instance identifier
|
String |
getLinkName()
Returns the database link name.
|
DBValueExpr |
getNullExpr()
Creates and returns a value expression for NULL
|
DBValueExpr |
getParamExpr(DBCmdParam param)
Creates and returns a value expression for a command parameter
|
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.
|
DBValueExpr |
getValueExpr(BigDecimal value)
Creates and returns a value object for the given string value.
|
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()
Returns whether Prepared Statements are enabled for the read and update operations in DBRecord.
|
void |
open(DBContext context)
Attaches the Database to the DBMS Handler provided with the context
and allows the Database and the Handler to perform initialization tasks
|
protected void |
register(String dbid)
registers the database in the global list of databases
|
void |
removeRelation(DBRelation relation)
removes a relation from the list of relations
|
void |
removeTable(DBTable table)
Removes a table from the list of database tables
|
void |
removeView(DBView view)
Removes a view from the list of database views
|
void |
setLinkName(String linkName)
Sets the name of the database link used to identify objects.
|
void |
setPreparedStatementsEnabled(boolean autoPrepareStmt)
Enables or Disables the use of Prepared Statements only for the read and update operations in DBRecord.
|
void |
setSchema(String schema)
Sets the schema for SQL statements.
|
protected Number |
validateNumber(DBTableColumn column,
DataType type,
Number n) |
protected Object |
validateValue(DBTableColumn column,
Object value)
Checks whether the supplied value is valid for the given column.
|
checkParamNull
public static final DBDatabase.DBSystemDate SYSDATE
public static final String EMPTY_STRING
protected final List<DBRelation> relations
protected DBMSHandler dbms
protected boolean legacyDate
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 findByIdentifier(String dbIdent)
public static DBDatabase findByClass(Class<? extends DBDatabase> cls)
public void discard()
protected void register(String dbid)
dbid
- protected String getDefaultIdentifier()
public String getIdentifier()
public <T extends DBMSHandler> T getDbms()
public boolean isPreparedStatementsEnabled()
public void setPreparedStatementsEnabled(boolean autoPrepareStmt)
autoPrepareStmt
- flag whether to automatically convert literal values to prepared statement paramspublic boolean checkExists(DBContext context)
public void open(DBContext context)
context
- the DBContextpublic void close(DBContext context)
context
- the DBContextpublic void getCreateDDLScript(DBSQLScript script)
Once the database is open you can use getDbms().getDLLCommand()
to create, alter or delete other database objects
script
- 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 namepublic void appendQualifiedName(DBSQLBuilder sql, String name, Boolean quoteName)
sql
- the SQL Builder to which to append the qualified object namename
- the object's namequoteName
- use quotes or not. If null detectQuoteName() is calledpublic Class<?> getColumnJavaType(DBColumnExpr expr)
expr
- the column expression for witch to return the java typepublic 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(BigDecimal value)
value
- the String 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 void removeTable(DBTable table)
table
- to removepublic List<DBTable> getTables()
public DBTable getTable(String name)
name
- the name of the tablepublic DBRowSet getRowSet(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 void removeRelation(DBRelation relation)
relation
- the relation to removepublic List<DBRelation> getRelations()
public DBRelation getRelation(String relationName)
protected void addView(DBView view)
view
- the DBView objectpublic void removeView(DBView view)
view
- to removepublic List<DBView> getViews()
public DBView getView(String name)
name
- the name of the viewpublic boolean isOpen()
protected void checkOpen()
public DBCommand createCommand()
public DataType detectDataType(Object value)
value
- the value to detectprotected Object validateValue(DBTableColumn column, Object value)
column
- the column to checkvalue
- the checked to check for validityFieldValueException
protected Number validateNumber(DBTableColumn column, DataType type, Number n)
public DBCaseExpr caseWhen(DBCompareExpr condition, Object trueValue, Object falseValue)
condition
- the compare expressiontrueValue
- the value to select if the condition is truefalseValue
- the value to select if the condition is falsepublic DBCaseExpr caseWhen(Map<DBCompareExpr,? extends Object> whenMap, Object elseValue)
whenMap
- the map with constraintselseValue
- the else expressionpublic DBCaseExpr caseWhenNull(DBColumnExpr column, Object trueValue, Object falseValue)
column
- a column or column expressiontrueValue
- the value to select if the condition is truefalseValue
- the value to select if the condition is falsepublic DBCaseExpr caseMap(DBColumnExpr column, Map<? extends Object,? extends Object> valueMap, Object elseValue)
valueMap
- map of key value pairselseValue
- the else expressionpublic DBCaseExpr caseMap(DBColumnExpr column, Options options, Object elseValue)
column
- a column or column expressionoptions
- the options to mapelseValue
- the else expressionpublic DBCaseExpr caseMap(DBColumnExpr column, Object cmpValue, Object trueValue, Object falseValue)
column
- a column or column expressioncmpValue
- the value to compare the column value withtrueValue
- the true expressionfalseValue
- the false expressionCopyright © 2008–2023 Apache Software Foundation. All rights reserved.