DBManager (consumer-service 1.9.2-3.5.0 API)
Date: 2014-11-12

org.gcube.messaging.common.consumer.db

## Class DBManager

- java.lang.Object
- - org.gcube.messaging.common.consumer.db.DBManager

- Direct Known Subclasses:
    AccountingDBManager, AccountingSystemDBManager, MonitoringDBManager

      public abstract class DBManager
      extends Object

  Author:
    Luca Frosini (CNR), Andrea Manzi(CERN)

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static interface` | `DBManager.BaseConsumer`

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected static File` | `backupFolder`
    `protected static int` | `backupIntervalMS`
    `protected Connection` | `connection` Connection to the db - persist for life of program
    `protected String` | `dbFileBaseFolder` dbFileName is used to open or create files that hold the state of the db.
    `protected String` | `dbFileName`
    `protected String` | `dbName`
    `protected String` | `password` Password for db connection
    `protected PoolManager` | `poolManager`
    `protected File` | `queriesFile` Queries file
    `protected String` | `username` Username for db connection

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `DBManager()` Constructor for SQLDBManager

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `backup()` checkpoint the db and create a backup
    `void` | `close()` Shutdown the db and close the connection to the db.
    `protected void` | `connectToEmbeddedDB()` Load the HSQL Database Engine JDBC driver hsqldb.jar should be in the class path or made part of the current jar
    `protected abstract void` | `connectToMySql()`
    `protected void` | `createDB()` Create db tables.
    `abstract void` | `open()` Load the HSQL Database Engine JDBC driver and open the connection to the db.
    `ResultSet` | `query(String expression)` Executes SQL command SELECT and invokes the given consumer.
    `void` | `queryAndConsume(String expression, DBManager.BaseConsumer consumer)` Executes SQL command SELECT and invokes the given consumer.
    `String` | `queryJSON(String expression)` Executes SQL command SELECT and invokes the given consumer, giging back JSON formatetted output.
    `protected static String` | `quote(String string)` Produce a string in double quotes with backslash sequences in all the right places.
    `protected void` | `reconnectToDB()`
    `static String` | `toJSon(ResultSet resultSet)`
    `void` | `update(String expression)` Executes SQL commands CREATE, DROP, INSERT and UPDATE.
    `protected void` | `zipFolder(File[] files)`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### connection

          protected Connection connection

      Connection to the db - persist for life of program

    - #### backupFolder

          protected static File backupFolder

    - #### dbFileBaseFolder

          protected String dbFileBaseFolder

      dbFileName is used to open or create files that hold the state of the db. It can contain directory names relative to the current working directory

    - #### dbFileName

          protected String dbFileName

    - #### dbName

          protected String dbName

    - #### username

          protected String username

      Username for db connection

    - #### password

          protected String password

      Password for db connection

    - #### queriesFile

          protected File queriesFile

      Queries file

    - #### poolManager

          protected PoolManager poolManager

    - #### backupIntervalMS

          protected static int backupIntervalMS

  - ### Constructor Detail

    - #### DBManager

          public DBManager()

      Constructor for SQLDBManager

  - ### Method Detail

    - #### open

          public abstract void open()
                             throws ClassNotFoundException,
                                    SQLException,
                                    Exception

      Load the HSQL Database Engine JDBC driver and open the connection to the db.
      Throws:
        `ClassNotFoundException` - if the HSQL Database Engine JDBC driver is not loaded
        `SQLException` - if the connection to the db fails
        `Exception` - if the tables creation fails

    - #### close

          public void close()
                     throws Exception

      Shutdown the db and close the connection to the db.
      Throws:
        `Exception`

    - #### backup

          public void backup()
                      throws SQLException,
                             Exception

      checkpoint the db and create a backup
      Throws:
        `SQLException` - if the CHECKPOINT query fails
        `Exception`

    - #### queryAndConsume

          public void queryAndConsume(String expression,
                             DBManager.BaseConsumer consumer)
                               throws SQLException,
                                      Exception

      Executes SQL command SELECT and invokes the given consumer.
      Parameters:
        `expression` - the SQL expression to evaluate
        `consumer` - the resultset consumer to invoke
      Throws:
        `SQLException` - if the query fails
        `Exception` - if the given resultset consumer fails

    - #### query

          public ResultSet query(String expression)
                          throws SQLException,
                                 Exception

      Executes SQL command SELECT and invokes the given consumer.
      Parameters:
        `expression` - the SQL expression to evaluate
      Returns:
        Result Set
      Throws:
        `SQLException` - if the query fails
        `Exception` - if the given resultset consumer fails

    - #### queryJSON

          public String queryJSON(String expression)
                           throws SQLException,
                                  Exception

      Executes SQL command SELECT and invokes the given consumer, giging back JSON formatetted output.
      Parameters:
        `expression` - the SQL expression to evaluate
      Returns:
        Result Set
      Throws:
        `SQLException` - if the query fails
        `Exception` - if the given resultset consumer fails

    - #### update

          public void update(String expression)
                      throws Exception

      Executes SQL commands CREATE, DROP, INSERT and UPDATE.
      Parameters:
        `expression` - the SQL expression to modify the db
      Throws:
        `Exception`

    - #### createDB

          protected void createDB()
                           throws SQLException,
                                  Exception

      Create db tables.
      Throws:
        `SQLException` - if the tables creation fails
        `Exception` - if the parsing of query file fails

    - #### toJSon

          public static String toJSon(ResultSet resultSet)
                               throws SQLException

      Parameters:
        `resultSet` -
      Returns:
        Json String
      Throws:
        `SQLException`

    - #### quote

          protected static String quote(String string)

      Produce a string in double quotes with backslash sequences in all the right places. A backslash will be inserted within
      Parameters:
        `string` - A String
      Returns:
        A String correctly formatted for insertion in a JSON text.

    - #### zipFolder

          protected void zipFolder(File[] files)
                            throws Exception

      Throws:
        `Exception`

    - #### connectToMySql

          protected abstract void connectToMySql()
                                          throws Exception

      Throws:
        `Exception`

    - #### connectToEmbeddedDB

          protected void connectToEmbeddedDB()
                                      throws Exception

      Load the HSQL Database Engine JDBC driver hsqldb.jar should be in the class path or made part of the current jar
      Throws:
        `Exception`

    - #### reconnectToDB

          protected void reconnectToDB()
                                throws Exception

      Throws:
        `Exception`

Copyright © 2014. All Rights Reserved.