Package org.apache.empire.dbms
Class DBMSHandlerBase
java.lang.Object
org.apache.empire.dbms.DBMSHandlerBase
- All Implemented Interfaces:
DBMSHandler
- Direct Known Subclasses:
DBMSHandlerDerby,DBMSHandlerH2,DBMSHandlerHSql,DBMSHandlerMSSQL,DBMSHandlerMySQL,DBMSHandlerOracle,DBMSHandlerPostgreSQL,DBMSHandlerSQLite
The DBMSHandler class is an abstract base class for all database handler.
Its purpose is to handle everything that is - or might be - database vendor specific.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classDBMSBuilder A Default DBSQLBuilder implementation with no additional featuresstatic final classDBMSCommand A Default DBCommand implementation with no additional featuresstatic classThis class is used to emulate sequences by using a sequence table.Nested classes/interfaces inherited from interface org.apache.empire.dbms.DBMSHandler
DBMSHandler.DBSetGenKeys -
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedConstructorprotectedDBMSHandlerBase(String[] specificSqlKeywords) Constructor -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddSQLKeyword(String keyWord) Adds an additional SQL Keyword to the keyword listprotected voidaddStatementParam(PreparedStatement pstmt, int paramIndex, Object value) Adds a statement parameter to a prepared statementvoidappendEnableRelationStmt(DBRelation r, boolean enable, DBSQLScript script) Appends a statement to enable or disable a foreign key relation.
The default is to drop or create the relation Override this method to provide different behavior for your database.voidappendObjectName(DBSQLBuilder sql, String name, Boolean useQuotes) Appends a table, view or column name to an SQL phrase.voidattachDatabase(DBDatabase db, Connection conn) Called when a database is openedbooleancheckExists(DBDatabase db, Connection conn) checks if the database exists The default implementation performs a simple count query on the first table or view SELECT count(*) FROM tablevoidcloseResultSet(ResultSet rset) Convenience function for closing a JDBC Resultset
Use it instead of rset.close() and stmt.close()protected voidcloseStatement(Statement stmt) Convenience function for closing a JDBC Resultset
Use it instead of stmt.close()createCombinedCommand(DBCommandExpr left, String keyWord, DBCommandExpr right) This function gives the dbms a chance to provide a custom implementation for a combined command such as UNION or INTERSECTcreateCommand(boolean autoPrepareStmt) This function creates a DBCommand derived object this databaseCreates a DataModelChecker instance of this DBMSHandlercreateModelParser(String catalog, String schema) Creates a DataModelParser instance of this DBMSHandlerThis function creates a DBSQLBuilder for this DBMSvoiddetachDatabase(DBDatabase db, Connection conn) Called when a database is closedbooleandetectQuoteName(DBObject object, String name) Detects whether a table or column name needs to be quoted or not
By default all reserved SQL keywords as well as names containing a "-", "/", "+" or " " require quoting.
Overrides this function to add database specific keywords like "user" or "count"int[]executeBatch(String[] sqlCmd, Object[][] sqlCmdParams, Connection conn) Executes a list of sql statements as batchexecuteQuery(String sqlCmd, Object[] sqlParams, boolean scrollable, Connection conn) Executes an select SQL-command and returns the query resultsintexecuteSQL(String sqlCmd, Object[] sqlParams, Connection conn, DBMSHandler.DBSetGenKeys genKeys) Executes the select, update or delete SQL-Command with a Statement object.Extracts native error message of an sqlExeption.getColumnAutoValue(DBDatabase db, DBTableColumn column, Connection conn) Returns an auto-generated value for a particular columngetColumnSequenceName(DBTableColumn column) Returns the sequence name of for a column of type AUTOINC The sequence name is usually provided as the default value If no Default value is provided the sequence name is generated from the table and the column nameReturns an expression that ignores the case of a column expression Only for text columns.abstract ObjectgetNextSequenceValue(DBDatabase db, String SeqName, int minValue, Connection conn) Returns the next value of a named sequence The numbers are used for fields of type DBExpr.DT_AUTOINC.
If a dbms supports this function it must return true for isSupported(DBMSFeature.SEQUENCES).abstract DBColumnExprReturns an expression for creating a sequence value.getResultValue(ResultSet rset, int columnIndex, DataType dataType) Reads a single column value from the given JDBC ResultSet and returns a value object of desired data type.
This gives the dbms the opportunity to change the value i.e. to simulate missing data types with other types.getUpdateTimestamp(Connection conn) Returns a timestamp that is used for record updates.abstract booleanisSupported(DBMSFeature type) Returns whether or not a particular feature is supported by this dbmsprotected voidprepareStatement(PreparedStatement pstmt, Object[] sqlParams) Prepares an sql statement by setting the supplied objects as parameters.querySingleValue(String sqlCmd, Object[] sqlParams, DataType dataType, Connection conn) Query a single valueMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.empire.dbms.DBMSHandler
getConvertPhrase, getDDLScript, getSQLPhrase
-
Field Details
-
ILLEGAL_NAME_CHARS
protected static final char[] ILLEGAL_NAME_CHARS -
GENERAL_SQL_KEYWORDS
-
reservedSQLKeywords
-
SEQUENCE_NAME_SUFFIX
-
-
Constructor Details
-
DBMSHandlerBase
Constructor -
DBMSHandlerBase
protected DBMSHandlerBase()Constructor
-
-
Method Details
-
addSQLKeyword
Adds an additional SQL Keyword to the keyword list- Parameters:
keyWord-
-
checkExists
checks if the database exists The default implementation performs a simple count query on the first table or view SELECT count(*) FROM table- Specified by:
checkExistsin interfaceDBMSHandler- Parameters:
db- the databaseconn- the Jdbc connection- Returns:
- true if the database exists or false otherwise
-
attachDatabase
Called when a database is opened- Specified by:
attachDatabasein interfaceDBMSHandler- Parameters:
db- the databaseconn- the Jdbc connection
-
detachDatabase
Called when a database is closed- Specified by:
detachDatabasein interfaceDBMSHandler- Parameters:
db- the databaseconn- the Jdbc connection
-
createSQLBuilder
This function creates a DBSQLBuilder for this DBMS- Specified by:
createSQLBuilderin interfaceDBMSHandler- Returns:
- a DBMS specific DBSQLBuilder object
-
createCommand
This function creates a DBCommand derived object this database- Specified by:
createCommandin interfaceDBMSHandler- Parameters:
autoPrepareStmt- flag whether to automatically provide literal values as prepared statement params- Returns:
- a DBCommand object
-
createCombinedCommand
This function gives the dbms a chance to provide a custom implementation for a combined command such as UNION or INTERSECT- Specified by:
createCombinedCommandin interfaceDBMSHandler- Parameters:
left- the left commandkeyWord- the key word (either "UNION" or "INTERSECT")left- the right commandright- the right command- Returns:
- a DBCommandExpr object
-
isSupported
Returns whether or not a particular feature is supported by this dbms- Specified by:
isSupportedin interfaceDBMSHandler- Parameters:
type- type of requested feature. @see DBMSFeature- Returns:
- true if the features is supported or false otherwise
-
detectQuoteName
Detects whether a table or column name needs to be quoted or not
By default all reserved SQL keywords as well as names containing a "-", "/", "+" or " " require quoting.
Overrides this function to add database specific keywords like "user" or "count"- Specified by:
detectQuoteNamein interfaceDBMSHandler- Parameters:
name- the name which to check- Returns:
- true if the name needs to be quoted or false otherwise
-
appendObjectName
Appends a table, view or column name to an SQL phrase.- Specified by:
appendObjectNamein interfaceDBMSHandler- Parameters:
sql- the StringBuilder containing the SQL phrase.name- the name of the object (table, view or column)useQuotes- use quotes or not
-
getUpdateTimestamp
Returns a timestamp that is used for record updates.- Specified by:
getUpdateTimestampin interfaceDBMSHandler- Parameters:
conn- the connection that might be used- Returns:
- the current date and time.
-
getNextSequenceValue
public abstract Object getNextSequenceValue(DBDatabase db, String SeqName, int minValue, Connection conn) Returns the next value of a named sequence The numbers are used for fields of type DBExpr.DT_AUTOINC.
If a dbms supports this function it must return true for isSupported(DBMSFeature.SEQUENCES).- Parameters:
db- the databaseSeqName- the name of the sequenceminValue- the minimum value of the sequenceconn- a valid database connection- Returns:
- a new unique sequence value or null if an error occurred
-
getNextSequenceValueExpr
Returns an expression for creating a sequence value. This is intended for the use with INSERT INTO statements where many records are affected.- Parameters:
column- the column for which to obtain an expression providing the next sequence value- Returns:
- an expression for the next sequence value
-
getColumnSequenceName
Returns the sequence name of for a column of type AUTOINC The sequence name is usually provided as the default value If no Default value is provided the sequence name is generated from the table and the column name- Parameters:
column- the column for which to get a sequence- Returns:
- the sequence name
-
getColumnAutoValue
Returns an auto-generated value for a particular column- Specified by:
getColumnAutoValuein interfaceDBMSHandler- Parameters:
db- the databasecolumn- the column for which a value is requiredconn- a valid database connection- Returns:
- the auto-generated value
-
getIgnoreCaseExpr
Returns an expression that ignores the case of a column expression Only for text columns. Default is upper(expr)- Specified by:
getIgnoreCaseExprin interfaceDBMSHandler- Parameters:
expr- the expression for which to ignore the case- Returns:
- the ignore case expression or the expression itself
-
getResultValue
public Object getResultValue(ResultSet rset, int columnIndex, DataType dataType) throws SQLException Reads a single column value from the given JDBC ResultSet and returns a value object of desired data type.
This gives the dbms the opportunity to change the value i.e. to simulate missing data types with other types.- Specified by:
getResultValuein interfaceDBMSHandler- Parameters:
rset- the sql Resultset with the current data rowcolumnIndex- one based column Index of the desired columndataType- the required data type- Returns:
- the value of the Column
- Throws:
SQLException- if a database access error occurs
-
executeSQL
public int executeSQL(String sqlCmd, Object[] sqlParams, Connection conn, DBMSHandler.DBSetGenKeys genKeys) throws SQLException Executes the select, update or delete SQL-Command with a Statement object.- Specified by:
executeSQLin interfaceDBMSHandler- Parameters:
sqlCmd- the SQL-CommandsqlParams- array of sql command parameters used for prepared statements (Optional).conn- a valid connection to the database.genKeys- allows to set the auto generated key of a record (INSERT statements only)- Returns:
- the row count for insert, update or delete or 0 for SQL statements that return nothing
- Throws:
SQLException- if a database access error occurs
-
executeBatch
public int[] executeBatch(String[] sqlCmd, Object[][] sqlCmdParams, Connection conn) throws SQLException Executes a list of sql statements as batch- Specified by:
executeBatchin interfaceDBMSHandler- Parameters:
sqlCmd- an array of sql statementssqlCmdParams- and array of statement parametersconn- a JDBC connection- Returns:
- an array containing the number of records affected by each statement
- Throws:
SQLException- thrown if a database access error occurs
-
executeQuery
public ResultSet executeQuery(String sqlCmd, Object[] sqlParams, boolean scrollable, Connection conn) throws SQLException Executes an select SQL-command and returns the query results- Specified by:
executeQueryin interfaceDBMSHandler- Parameters:
sqlCmd- the SQL-CommandsqlParams- array of sql command parameters used for prepared statements (Optional).scrollable- true if scrollable or false otherwiseconn- a valid connection to the database.- Returns:
- the JDBC resultset
- Throws:
SQLException- thrown if a database access error occurs
-
querySingleValue
public Object querySingleValue(String sqlCmd, Object[] sqlParams, DataType dataType, Connection conn) throws SQLException Query a single value- Specified by:
querySingleValuein interfaceDBMSHandler- Parameters:
sqlCmd- the SQL-CommandsqlParams- array of sql command parameters used for prepared statements (Optional).dataType- the requested return typeconn- a valid connection to the database.- Returns:
- the value of the first column in the first row of the query
- Throws:
SQLException
-
appendEnableRelationStmt
Appends a statement to enable or disable a foreign key relation.
The default is to drop or create the relation Override this method to provide different behavior for your database.- Specified by:
appendEnableRelationStmtin interfaceDBMSHandler- Parameters:
r- the foreign key relation which should be enabled or disabledenable- true to enable the relation or false to disablescript- the script to which to add the DDL command(s)
-
createModelParser
Creates a DataModelParser instance of this DBMSHandler- Specified by:
createModelParserin interfaceDBMSHandler- Parameters:
catalog- the database catalogschema- the database schema- Returns:
- the model parser
-
createModelChecker
Creates a DataModelChecker instance of this DBMSHandler- Specified by:
createModelCheckerin interfaceDBMSHandler- Parameters:
db- the database- Returns:
- the model checker
-
extractErrorMessage
Extracts native error message of an sqlExeption.- Specified by:
extractErrorMessagein interfaceDBMSHandler- Parameters:
e- the SQLException- Returns:
- the error message of the database
-
closeResultSet
Convenience function for closing a JDBC Resultset
Use it instead of rset.close() and stmt.close()
- Specified by:
closeResultSetin interfaceDBMSHandler- Parameters:
rset- a ResultSet object
-
closeStatement
Convenience function for closing a JDBC Resultset
Use it instead of stmt.close()
- Parameters:
stmt- a Statement object
-
prepareStatement
Prepares an sql statement by setting the supplied objects as parameters.- Parameters:
pstmt- the prepared statementsqlParams- list of objects- Throws:
SQLException- thrown if a database access error occurs
-
addStatementParam
protected void addStatementParam(PreparedStatement pstmt, int paramIndex, Object value) throws SQLException Adds a statement parameter to a prepared statement- Parameters:
pstmt- the prepared statementparamIndex- the parameter indexvalue- the parameter value- Throws:
SQLException- thrown if a database access error occurs
-