Package org.apache.empire.db
Class DBDatabase
java.lang.Object
org.apache.empire.db.DBObject
org.apache.empire.db.DBDatabase
- Direct Known Subclasses:
OracleSYSDatabase,TDatabase
This abstract class is the applicaton's interface for a particular database schema.
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classThis class represents the database systems current date and time. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected DBMSHandlerstatic final Stringprotected booleanFlag indicating whether Bean getters / setters use java.util.Date or Java types (LocalDate, LocalDateTime) True (default) when java.util.Date is usedprotected final List<DBRelation>static final DBDatabase.DBSystemDate -
Constructor Summary
ConstructorsConstructorDescriptionConstructs 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. -
Method Summary
Modifier and TypeMethodDescriptionaddRelation(String name, DBRelation.DBReference... references) Adds a foreign key relation to the database.final DBRelationaddRelation(DBRelation.DBReference reference) Adds a foreign key relation to the database.final DBRelationaddRelation(DBRelation.DBReference ref1, DBRelation.DBReference ref2) Adds a foreign key relation to the database.protected voidaddRowsetToAliasMap(DBRowSet rowset) adds a DBRowSet to the alias mapprotected voidAdds a DBTable object to list of database tables.
This function is called internally from the DBTable's constructor.protected voidAdds a DBView object to list of database views.
This function is called internally from the DBView's constructor.voidappendQualifiedName(DBSQLBuilder sql, String name, Boolean quoteName) Adds a full qualified object name including schema prefix and database link postfix (if any).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"Creates a SQL case expression in the form "case [Expr] when [mapKey] then [mapValue] else [elseValue] end"caseMap(DBColumnExpr column, Options options, Object elseValue) Creates a SQL case expression in the form "case [Expr] when [optionValue] then [optionText] else [elseValue] end"caseWhen(Map<DBCompareExpr, ? extends Object> whenMap, Object elseValue) Creates a SQL case expression in the form "case when [mapKey] then [mapValue] else [elseValue] end"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 usagecaseWhenNull(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 usagebooleancheckExists(DBContext context) Checks if the database exists The implementation depends on the DBMSHandlerprotected voidchecks if the database has been opened or false otherwisevoidCloses this database object by detaching it from the dbmsCreates a new Command object for this database Deprecated instead use context.createCommand();detectDataType(Object value) Detects the DataType of a given value.voiddiscard()Frees all resources and unregisters database in global map.static DBDatabasefindByClass(Class<? extends DBDatabase> clazz) find a database by its Java typestatic DBDatabasefindByIdentifier(String dbIdent) find a database by idfindRelationsOn(DBTable table) Returns a list of foreign key relations on a given tableprotected voidgenerateDDLScript(DBSQLScript script) Override this to change or add DDL commandsReturns all DBRowSet instances currently created for this databaseClass<?>Returns the java class type for a given dataTypevoidgetCreateDDLScript(DBSQLScript script) Creates a DDL Script for creating all database objects on the target database.
This function may be called even if the database has not been previously opened.Returns the database object to which this object belongs to.<T extends DBMSHandler>
TgetDbms()Returns the DBMS Handler for this database.protected Stringreturns the default database identifier Override this to customizeReturns the database instance identifiergetIgnoreCaseExpr(DBColumnExpr columnExpr) Returns an expression that ignores the case of a column expression This is only called for case sensitive column expressions (expr.isCaseSensitive()) Default is upper(expr)Returns the database link name.final DBValueExprCreates and returns a value expression for NULLfinal DBValueExprgetParamExpr(DBCmdParam param) Creates and returns a value expression for a command parametergetRelation(String relationName) Returns the relation of a given nameReturns the relations which have been defined in the database.Finds a DBRowSet object (DBTable or DBView) by name.getRowSetByAlias(String alias) Finds a RowSet object by the alias name.Returns the schema for SQL statements.Returns the schema-prefix for SQL statements e.g.final DBValueExprCreates and returns a value object for the database systems current date and time.Finds a DBTable object by name.Returns the tables which have been defined in the database.final DBValueExprgetValueExpr(boolean value) Creates and returns a value object for the given boolean value.final DBValueExprgetValueExpr(int value) Creates and returns a value object for the given integer value.final DBValueExprgetValueExpr(long value) Creates and returns a value object for the given long value.getValueExpr(Object value, DataType dataType) Creates and returns a value object for the given scalar value.final DBValueExprgetValueExpr(String value) Creates and returns a value object for the given string value.final DBValueExprgetValueExpr(BigDecimal value) Creates and returns a value object for the given string value.Finds a DBView object by name.getViews()Returns the views which have been defined in the database.booleanisOpen()Indicates whether the database has been opened.booleanReturns whether Prepared Statements are enabled for the read and update operations in DBRecord.protected booleanisSameRowSet(DBRowSet first, DBRowSet second) Checks if two Rowsets with the same name are equal and just another instancevoidAttaches the Database to the DBMS Handler provided with the context and allows the Database and the Handler to perform initialization tasksprotected voidregisters the database in the global list of databasesvoidremoveRelation(DBRelation relation) removes a relation from the list of relationsprotected voidremoveRowsetFromAliasMap(DBRowSet rowset) removes a rowset from the alias mapvoidremoveTable(DBTable table) Removes a table from the list of database tablesvoidremoveView(DBView view) Removes a view from the list of database viewsvoidsetLinkName(String linkName) Sets the name of the database link used to identify objects.voidsetPreparedStatementsEnabled(boolean autoPrepareStmt) Enables or Disables the use of Prepared Statements only for the read and update operations in DBRecord.voidSets the schema for SQL statements.protected NumbervalidateNumber(DBTableColumn column, DataType type, Number n) protected StringvalidateString(DBTableColumn column, String value) protected ObjectvalidateValue(DBTableColumn column, Object value) Checks whether the supplied value is valid for the given column.Methods inherited from class org.apache.empire.db.DBObject
checkParamNull
-
Field Details
-
SYSDATE
-
EMPTY_STRING
- See Also:
-
tables
-
relations
-
views
-
rowsetAliasMap
-
dbms
-
legacyDate
protected boolean legacyDateFlag indicating whether Bean getters / setters use java.util.Date or Java types (LocalDate, LocalDateTime) True (default) when java.util.Date is used
-
-
Constructor Details
-
DBDatabase
Constructs a new DBDatabase object and sets the specified schema object.- Parameters:
schema- the database schemalinkName- the database link name
-
DBDatabase
Constructs a new DBDatabase object and sets the specified schema object.- Parameters:
schema- the database schema
-
DBDatabase
public DBDatabase()Constructs a new DBDatabase object set the variable 'schema' = null.
-
-
Method Details
-
findByIdentifier
find a database by id- Parameters:
dbIdent- the database id to look for- Returns:
- the database or null if not found
-
findByClass
find a database by its Java type- Parameters:
clazz- the database class to look for- Returns:
- the database or null if not found
-
discard
public void discard()Frees all resources and unregisters database in global map. Do not reuse this object afterwards! Hint: Database must be closed! -
register
registers the database in the global list of databases- Parameters:
dbid- the database id
-
getDefaultIdentifier
returns the default database identifier Override this to customize- Returns:
- the defaultId
-
getIdentifier
Returns the database instance identifier- Returns:
- the identifier of the database
-
getDbms
Returns the DBMS Handler for this database.- Type Parameters:
T- the DBMSHandler type- Returns:
- returns the DBMS Handler for this database
-
isPreparedStatementsEnabled
public boolean isPreparedStatementsEnabled()Returns whether Prepared Statements are enabled for the read and update operations in DBRecord. Note: This will not affect statements generated via DBContext.createCommand() However statement parameters can always be manually declared using cmd.addCmdParam();- Returns:
- true if prepared Statements are enabled or false if not
-
setPreparedStatementsEnabled
public void setPreparedStatementsEnabled(boolean autoPrepareStmt) Enables or Disables the use of Prepared Statements only for the read and update operations in DBRecord. For general use of Prepared Statements please use DBContext.createCommand() However statement parameters can always be manually declared using cmd.addCmdParam();- Parameters:
autoPrepareStmt- flag whether to automatically convert literal values to prepared statement params
-
checkExists
Checks if the database exists The implementation depends on the DBMSHandler- Parameters:
context- the database context- Returns:
- true if the database exists or false otherwise
-
open
Attaches the Database to the DBMS Handler provided with the context and allows the Database and the Handler to perform initialization tasks- Parameters:
context- the DBContext
-
close
Closes this database object by detaching it from the dbms- Parameters:
context- the DBContext
-
getCreateDDLScript
Creates a DDL Script for creating all database objects on the target database.
This function may be called even if the database has not been previously opened.
Once the database is open you can use getDbms().getDLLCommand() to create, alter or delete other database objects
- Parameters:
script- the script object that will be completed
-
generateDDLScript
Override this to change or add DDL commands- Parameters:
script- the script on which to add the DDL commands
-
getDatabase
Description copied from class:DBObjectReturns the database object to which this object belongs to. For the database object itself this function will return the this pointer.- Specified by:
getDatabasein classDBObject- Returns:
- the database object
- See Also:
-
getSchema
Returns the schema for SQL statements.- Returns:
- the schema
-
setSchema
Sets the schema for SQL statements.- Parameters:
schema- the schema to set
-
getSchemaPrefix
Returns the schema-prefix for SQL statements e.g. "SCHEMA." or empty string if no schema is defined.- Returns:
- the schema-prefix
-
getLinkName
Returns the database link name.- Returns:
- the name of the database link
-
setLinkName
Sets the name of the database link used to identify objects.- Parameters:
linkName- the database link name
-
appendQualifiedName
Adds a full qualified object name including schema prefix and database link postfix (if any). to the string buffer supplied- Parameters:
sql- the SQL Builder to which to append the qualified object namename- the object's namequoteName- use quotes or not. If null detectQuoteName() is called
-
getColumnJavaType
Returns the java class type for a given dataType- Parameters:
expr- the column expression for which to return the java type- Returns:
- return the java class used for storing values of this dataType
-
getIgnoreCaseExpr
Returns an expression that ignores the case of a column expression This is only called for case sensitive column expressions (expr.isCaseSensitive()) Default is upper(expr)- Parameters:
columnExpr- the expression for which to ignore the case- Returns:
- the ignore case expression
-
getValueExpr
Creates and returns a value object for the given scalar value.- Parameters:
value- the scalar valuedataType- the database systems data type used for this value- Returns:
- the new DBValueExpr object
-
getSystemDateExpr
Creates and returns a value object for the database systems current date and time.- Returns:
- a DBValueExpr object
-
getValueExpr
Creates and returns a value object for the given string value.- Parameters:
value- the String value- Returns:
- the new DBValueExpr object
-
getValueExpr
Creates and returns a value object for the given boolean value.- Parameters:
value- the Boolean value- Returns:
- the new DBValueExpr object
-
getValueExpr
Creates and returns a value object for the given integer value.- Parameters:
value- the int value- Returns:
- the new DBValueExpr object
-
getValueExpr
Creates and returns a value object for the given long value.- Parameters:
value- the long value- Returns:
- the new DBValueExpr object
-
getValueExpr
Creates and returns a value object for the given string value.- Parameters:
value- the String value- Returns:
- the new DBValueExpr object
-
getParamExpr
Creates and returns a value expression for a command parameter- Parameters:
param- the command parameter- Returns:
- the corresponding DBValueExpr object
-
getNullExpr
Creates and returns a value expression for NULL- Returns:
- the corresponding DBValueExpr object
-
addTable
Adds a DBTable object to list of database tables.
This function is called internally from the DBTable's constructor.- Parameters:
table- the DBTable object
-
removeTable
Removes a table from the list of database tables- Parameters:
table- to remove
-
getTables
Returns the tables which have been defined in the database.- Returns:
- db tables.
-
getTable
Finds a DBTable object by name.- Parameters:
name- the name of the table- Returns:
- the located DBTable object
-
getRowSet
Finds a DBRowSet object (DBTable or DBView) by name.- Parameters:
name- the name of the table- Returns:
- the located DBTable object
-
getRowSetByAlias
Finds a RowSet object by the alias name.- Parameters:
alias- the alias of the desired table- Returns:
- the located DBTable object
-
getAllRowSets
Returns all DBRowSet instances currently created for this database- Returns:
- all DBRowSet instances
-
addRelation
Adds a foreign key relation to the database.- Parameters:
reference- a reference for a source and target column pair- Returns:
- the relation object
-
addRelation
Adds a foreign key relation to the database.- Parameters:
ref1- a reference for a source and target column pairref2- a reference for a source and target column pair- Returns:
- the relation object
-
addRelation
Adds a foreign key relation to the database.- Parameters:
name- the relation namereferences- a list of source and target column pairs- Returns:
- the relation object
-
removeRelation
removes a relation from the list of relations- Parameters:
relation- the relation to remove
-
getRelations
Returns the relations which have been defined in the database.- Returns:
- the list of relations.
-
getRelation
Returns the relation of a given name- Parameters:
relationName- the name of the relation- Returns:
- db the relation of the given name
-
findRelationsOn
Returns a list of foreign key relations on a given table- Parameters:
table- the table for which to find all foreign keys- Returns:
- the list of all foreign key relations
-
addView
Adds a DBView object to list of database views.
This function is called internally from the DBView's constructor.- Parameters:
view- the DBView object
-
removeView
Removes a view from the list of database views- Parameters:
view- to remove
-
getViews
Returns the views which have been defined in the database.- Returns:
- db views.
-
getView
Finds a DBView object by name.- Parameters:
name- the name of the view- Returns:
- the located DBTable object
-
isOpen
public boolean isOpen()Indicates whether the database has been opened.- Returns:
- The name of the encoding or null if a single byte encoding is used.
-
checkOpen
protected void checkOpen()checks if the database has been opened or false otherwise -
createCommand
Creates a new Command object for this database Deprecated instead use context.createCommand();- Returns:
- the command object.
-
detectDataType
Detects the DataType of a given value.- Parameters:
value- the value to detect- Returns:
- the DataType enum for the value
-
validateValue
Checks whether the supplied value is valid for the given column. If the type of the value supplied does not match the columns data type the value will be checked for compatibility. If the value is not valid a FieldIllegalValueException is thrown- Parameters:
column- the column to checkvalue- the checked to check for validity- Returns:
- the (possibly converted) value
- Throws:
FieldValueException- exception thrown if value is not valid
-
validateNumber
-
validateString
-
caseWhen
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- Parameters:
condition- the compare expressiontrueValue- the value to select if the condition is truefalseValue- the value to select if the condition is false- Returns:
- the case expression
-
caseWhen
Creates a SQL case expression in the form "case when [mapKey] then [mapValue] else [elseValue] end"- Parameters:
whenMap- the map with constraintselseValue- the else expression- Returns:
- the expression
-
caseWhenNull
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- Parameters:
column- a column or column expressiontrueValue- the value to select if the condition is truefalseValue- the value to select if the condition is false- Returns:
- an sql case expression
-
caseMap
public 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"- Parameters:
column- the column expression which to mapvalueMap- map of key value pairselseValue- the else expression- Returns:
- the expression
-
caseMap
Creates a SQL case expression in the form "case [Expr] when [optionValue] then [optionText] else [elseValue] end"- Parameters:
column- a column or column expressionoptions- the options to mapelseValue- the else expression- Returns:
- the expression
-
caseMap
public 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"- Parameters:
column- a column or column expressioncmpValue- the value to compare the column value withtrueValue- the true expressionfalseValue- the false expression- Returns:
- the expression
-
isSameRowSet
Checks if two Rowsets with the same name are equal and just another instance- Parameters:
first- the first rowsetsecond- the second rowset- Returns:
- true if both are the same and are just another instance
-
addRowsetToAliasMap
adds a DBRowSet to the alias map- Parameters:
rowset- the rowset to add to the map
-
removeRowsetFromAliasMap
removes a rowset from the alias map- Parameters:
rowset- the rowset to remove
-