Class AbstractDatabaseAdapter

java.lang.Object
marauroa.server.db.adapter.AbstractDatabaseAdapter
All Implemented Interfaces:
DatabaseAdapter
Direct Known Subclasses:
H2DatabaseAdapter, MySQLDatabaseAdapter, PostgresDatabaseAdapter

public abstract class AbstractDatabaseAdapter extends Object implements DatabaseAdapter
abstract database adapter
Author:
hendrik
  • Field Details

  • Constructor Details

  • Method Details

    • createConnection

      protected Connection createConnection(Properties connInfo) throws SQLException, DatabaseConnectionException
      This method creates the real connection to database.
      Parameters:
      connInfo - connection information like url, username and password
      Returns:
      a connection to the database
      Throws:
      SQLException - if the connection cannot be established.
      DatabaseConnectionException - if the connection cannot be established.
    • commit

      public void commit() throws SQLException
      Description copied from interface: DatabaseAdapter
      commits the current transaction
      Specified by:
      commit in interface DatabaseAdapter
      Throws:
      SQLException - in case of an database error
    • rollback

      public void rollback() throws SQLException
      Description copied from interface: DatabaseAdapter
      rolls the current transaction back, undoing all the changes.
      Specified by:
      rollback in interface DatabaseAdapter
      Throws:
      SQLException - in case of an database error
    • execute

      public int execute(String sql) throws SQLException
      Description copied from interface: DatabaseAdapter
      executes an SQL statement
      Specified by:
      execute in interface DatabaseAdapter
      Parameters:
      sql - sql-statement to execute
      Returns:
      number of affected rows
      Throws:
      SQLException - in case of an database error
    • execute

      public int execute(String sql, InputStream... inputStreams) throws SQLException, IOException
      Description copied from interface: DatabaseAdapter
      executes an SQL statement with streamed parameters
      Specified by:
      execute in interface DatabaseAdapter
      Parameters:
      sql - sql-statement to execute
      inputStreams - parameters
      Returns:
      number of affected rows
      Throws:
      SQLException - in case of an database error
      IOException - in case the stream cannot be read to the end
    • executeBatch

      public void executeBatch(String sql, InputStream... inputStreams) throws SQLException, IOException
      Description copied from interface: DatabaseAdapter
      executes a batch of sql-statements
      Specified by:
      executeBatch in interface DatabaseAdapter
      Parameters:
      sql - sql-statement to execute
      inputStreams - a list of input stream. For each of them the statement is executed
      Throws:
      SQLException - in case of an database error
      IOException - in case one of the streams cannot be read to the end
    • query

      public ResultSet query(String sql) throws SQLException
      Description copied from interface: DatabaseAdapter
      queries the database for information
      Specified by:
      query in interface DatabaseAdapter
      Parameters:
      sql - sql-statement to execute
      Returns:
      ResultSet
      Throws:
      SQLException - in case of an database error
    • querySingleCellInt

      public int querySingleCellInt(String sql) throws SQLException
      Description copied from interface: DatabaseAdapter
      queries for a single row, single column integer response like a count-select
      Specified by:
      querySingleCellInt in interface DatabaseAdapter
      Parameters:
      sql - sql-statement to execute
      Returns:
      integer of the first column in the first row
      Throws:
      SQLException - in case of an database error
    • getLastInsertId

      public int getLastInsertId(String table, String idcolumn) throws SQLException
      Description copied from interface: DatabaseAdapter
      gets the id of the last insert. Note: The table and idcolumn parameters must match the last insert statement. This is because on some database systems a SELECT IDENTITY is performed and on other database systems a SELECT curval(table_idcolumn_seq).
      Specified by:
      getLastInsertId in interface DatabaseAdapter
      Parameters:
      table - name of table on which the last insert was done
      idcolumn - name autoincrement serial column of that table
      Returns:
      generated id
      Throws:
      SQLException - in case of an database error
    • close

      public void close() throws SQLException
      Description copied from interface: DatabaseAdapter
      closes the database connection
      Specified by:
      close in interface DatabaseAdapter
      Throws:
      SQLException - in case of an database error
    • prepareStatement

      public PreparedStatement prepareStatement(String sql) throws SQLException
      Description copied from interface: DatabaseAdapter
      Prepares a statement for a batch operation.
      Specified by:
      prepareStatement in interface DatabaseAdapter
      Parameters:
      sql - SQL statement
      Returns:
      PreparedStatement
      Throws:
      SQLException - in case of an database error
    • doesTableExist

      public boolean doesTableExist(String table) throws SQLException
      Description copied from interface: DatabaseAdapter
      checks whether the specified table exists
      Specified by:
      doesTableExist in interface DatabaseAdapter
      Parameters:
      table - name of table
      Returns:
      true, if the table exists, false otherwise
      Throws:
      SQLException - in case of an database error
    • doesColumnExist

      public boolean doesColumnExist(String table, String column) throws SQLException
      Description copied from interface: DatabaseAdapter
      checks whether the specified column exists
      Specified by:
      doesColumnExist in interface DatabaseAdapter
      Parameters:
      table - name of table
      column - name of column
      Returns:
      true, if the column exists, false otherwise
      Throws:
      SQLException - in case of an database error
    • getColumnLength

      public int getColumnLength(String table, String column) throws SQLException
      Description copied from interface: DatabaseAdapter
      Gets the length of the specified column
      Specified by:
      getColumnLength in interface DatabaseAdapter
      Parameters:
      table - name of table
      column - name of column
      Returns:
      the length of the column or -1 if no column with the given name in the given table exists
      Throws:
      SQLException - in case of an database error
    • rewriteSql

      protected String rewriteSql(String sql) throws SQLException
      rewrites an SQL statement so that it is accepted by the database server software
      Parameters:
      sql - original SQL statement
      Returns:
      modified SQL statement
      Throws:
      SQLException - in case of a database error
    • verifyConnection

      public boolean verifyConnection()
      verifies that the connection i still working
      Specified by:
      verifyConnection in interface DatabaseAdapter
      Returns:
      true, if the connection is valid; false otherwise
    • isConnectionError

      public boolean isConnectionError(Exception e)
      determine if the Exception indicates a connection error, so a retry of the transaction makes sense.
      Specified by:
      isConnectionError in interface DatabaseAdapter
      Parameters:
      e - Exception
      Returns:
      true, if the exception indicates a connection error; false otherwise