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

public abstract class DBMSHandlerBase extends Object implements DBMSHandler
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.
  • Field Details

    • ILLEGAL_NAME_CHARS

      protected static final char[] ILLEGAL_NAME_CHARS
    • GENERAL_SQL_KEYWORDS

      protected static final String[] GENERAL_SQL_KEYWORDS
    • reservedSQLKeywords

      protected final Set<String> reservedSQLKeywords
    • SEQUENCE_NAME_SUFFIX

      protected String SEQUENCE_NAME_SUFFIX
  • Constructor Details

    • DBMSHandlerBase

      protected DBMSHandlerBase(String[] specificSqlKeywords)
      Constructor
    • DBMSHandlerBase

      protected DBMSHandlerBase()
      Constructor
  • Method Details

    • addSQLKeyword

      protected void addSQLKeyword(String keyWord)
      Adds an additional SQL Keyword to the keyword list
      Parameters:
      keyWord -
    • checkExists

      public boolean checkExists(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 table
      Specified by:
      checkExists in interface DBMSHandler
      Parameters:
      db - the database
      conn - the Jdbc connection
      Returns:
      true if the database exists or false otherwise
    • attachDatabase

      public void attachDatabase(DBDatabase db, Connection conn)
      Called when a database is opened
      Specified by:
      attachDatabase in interface DBMSHandler
      Parameters:
      db - the database
      conn - the Jdbc connection
    • detachDatabase

      public void detachDatabase(DBDatabase db, Connection conn)
      Called when a database is closed
      Specified by:
      detachDatabase in interface DBMSHandler
      Parameters:
      db - the database
      conn - the Jdbc connection
    • createSQLBuilder

      public DBSQLBuilder createSQLBuilder()
      This function creates a DBSQLBuilder for this DBMS
      Specified by:
      createSQLBuilder in interface DBMSHandler
      Returns:
      a DBMS specific DBSQLBuilder object
    • createCommand

      public DBCommand createCommand(boolean autoPrepareStmt)
      This function creates a DBCommand derived object this database
      Specified by:
      createCommand in interface DBMSHandler
      Parameters:
      autoPrepareStmt - flag whether to automatically provide literal values as prepared statement params
      Returns:
      a DBCommand object
    • createCombinedCommand

      public DBCommandExpr 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 INTERSECT
      Specified by:
      createCombinedCommand in interface DBMSHandler
      Parameters:
      left - the left command
      keyWord - the key word (either "UNION" or "INTERSECT")
      left - the right command
      right - the right command
      Returns:
      a DBCommandExpr object
    • isSupported

      public abstract boolean isSupported(DBMSFeature type)
      Returns whether or not a particular feature is supported by this dbms
      Specified by:
      isSupported in interface DBMSHandler
      Parameters:
      type - type of requested feature. @see DBMSFeature
      Returns:
      true if the features is supported or false otherwise
    • detectQuoteName

      public boolean detectQuoteName(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"
      Specified by:
      detectQuoteName in interface DBMSHandler
      Parameters:
      name - the name which to check
      Returns:
      true if the name needs to be quoted or false otherwise
    • appendObjectName

      public void appendObjectName(DBSQLBuilder sql, String name, Boolean useQuotes)
      Appends a table, view or column name to an SQL phrase.
      Specified by:
      appendObjectName in interface DBMSHandler
      Parameters:
      sql - the StringBuilder containing the SQL phrase.
      name - the name of the object (table, view or column)
      useQuotes - use quotes or not
    • getUpdateTimestamp

      public Timestamp getUpdateTimestamp(Connection conn)
      Returns a timestamp that is used for record updates.
      Specified by:
      getUpdateTimestamp in interface DBMSHandler
      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 database
      SeqName - the name of the sequence
      minValue - the minimum value of the sequence
      conn - a valid database connection
      Returns:
      a new unique sequence value or null if an error occurred
    • getNextSequenceValueExpr

      public abstract DBColumnExpr getNextSequenceValueExpr(DBTableColumn column)
      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

      public String getColumnSequenceName(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 name
      Parameters:
      column - the column for which to get a sequence
      Returns:
      the sequence name
    • getColumnAutoValue

      public Object getColumnAutoValue(DBDatabase db, DBTableColumn column, Connection conn)
      Returns an auto-generated value for a particular column
      Specified by:
      getColumnAutoValue in interface DBMSHandler
      Parameters:
      db - the database
      column - the column for which a value is required
      conn - a valid database connection
      Returns:
      the auto-generated value
    • getIgnoreCaseExpr

      public DBColumnExpr getIgnoreCaseExpr(DBColumnExpr expr)
      Returns an expression that ignores the case of a column expression Only for text columns. Default is upper(expr)
      Specified by:
      getIgnoreCaseExpr in interface DBMSHandler
      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:
      getResultValue in interface DBMSHandler
      Parameters:
      rset - the sql Resultset with the current data row
      columnIndex - one based column Index of the desired column
      dataType - 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:
      executeSQL in interface DBMSHandler
      Parameters:
      sqlCmd - the SQL-Command
      sqlParams - 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:
      executeBatch in interface DBMSHandler
      Parameters:
      sqlCmd - an array of sql statements
      sqlCmdParams - and array of statement parameters
      conn - 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:
      executeQuery in interface DBMSHandler
      Parameters:
      sqlCmd - the SQL-Command
      sqlParams - array of sql command parameters used for prepared statements (Optional).
      scrollable - true if scrollable or false otherwise
      conn - 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:
      querySingleValue in interface DBMSHandler
      Parameters:
      sqlCmd - the SQL-Command
      sqlParams - array of sql command parameters used for prepared statements (Optional).
      dataType - the requested return type
      conn - a valid connection to the database.
      Returns:
      the value of the first column in the first row of the query
      Throws:
      SQLException
    • appendEnableRelationStmt

      public void appendEnableRelationStmt(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.
      Specified by:
      appendEnableRelationStmt in interface DBMSHandler
      Parameters:
      r - the foreign key relation which should be enabled or disabled
      enable - true to enable the relation or false to disable
      script - the script to which to add the DDL command(s)
    • createModelParser

      public DBModelParser createModelParser(String catalog, String schema)
      Creates a DataModelParser instance of this DBMSHandler
      Specified by:
      createModelParser in interface DBMSHandler
      Parameters:
      catalog - the database catalog
      schema - the database schema
      Returns:
      the model parser
    • createModelChecker

      public DBModelChecker createModelChecker(DBDatabase db)
      Creates a DataModelChecker instance of this DBMSHandler
      Specified by:
      createModelChecker in interface DBMSHandler
      Parameters:
      db - the database
      Returns:
      the model checker
    • extractErrorMessage

      public String extractErrorMessage(SQLException e)
      Extracts native error message of an sqlExeption.
      Specified by:
      extractErrorMessage in interface DBMSHandler
      Parameters:
      e - the SQLException
      Returns:
      the error message of the database
    • closeResultSet

      public void closeResultSet(ResultSet rset)
      Convenience function for closing a JDBC Resultset
      Use it instead of rset.close() and stmt.close()

      Specified by:
      closeResultSet in interface DBMSHandler
      Parameters:
      rset - a ResultSet object
    • closeStatement

      protected void closeStatement(Statement stmt)
      Convenience function for closing a JDBC Resultset
      Use it instead of stmt.close()

      Parameters:
      stmt - a Statement object
    • prepareStatement

      protected void prepareStatement(PreparedStatement pstmt, Object[] sqlParams) throws SQLException
      Prepares an sql statement by setting the supplied objects as parameters.
      Parameters:
      pstmt - the prepared statement
      sqlParams - 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 statement
      paramIndex - the parameter index
      value - the parameter value
      Throws:
      SQLException - thrown if a database access error occurs