Class BeetRootDatabaseManager

java.lang.Object
ch.autumo.beetroot.BeetRootDatabaseManager

public class BeetRootDatabaseManager extends Object
Database manager. Supported databases: H2, MySQL, MariaDB, Oracle, PostgreSQL and unsupported databases.
  • Field Details

    • LOG

      protected static final org.slf4j.Logger LOG
      Logger for this class.
    • POOL_NAME_POSTFIX

      public static final String POOL_NAME_POSTFIX
      Postfix appended to the connection pool name.
      See Also:
    • CFG_KEY_DS_EXT_JNDI

      public static final String CFG_KEY_DS_EXT_JNDI
      Configuration key of an external JNDI data source.
      See Also:
    • CFG_KEY_DS_INT_DSCN

      public static final String CFG_KEY_DS_INT_DSCN
      Configuration key of the internal data source class name.
      See Also:
  • Method Details

    • getInstance

      public static BeetRootDatabaseManager getInstance()
      Access DB manager.
      Returns:
      DB manager
    • isInitialized

      public boolean isInitialized()
      Has this database manager been initialized?
      Returns:
      true if so, otherwise false
    • initialize

      public void initialize(String webAppRootPath) throws Exception
      Initialize DB manager.
      Parameters:
      webAppRootPath - Web app root path
      Throws:
      Exception - exception
    • initialize

      public void initialize() throws Exception
      Initialize DB manager.
      Throws:
      Exception - exception
    • release

      public void release()
      Resource database pool resources. Should be called when a container life-cycle or a server ends!
    • getDataSource

      public DataSource getDataSource()
      Get the data source.
      Returns:
      data source
    • getConnection

      public Connection getConnection() throws SQLException
      Get an new DB connection.
      Returns:
      DB connection
      Throws:
      SQLException - SQL exception
    • getGlobalConnection

      public Connection getGlobalConnection() throws SQLException
      Get an new global DB connection. You have to roll back or commit the transaction, before you retire it with retireGlobalConnection(Connection). If you use DB roll-backs are done automatically and you'll receive an SQLException. Don't close it by yourself!
      Returns:
      global DB connection
      Throws:
      SQLException - SQL exception
    • retireGlobalConnection

      public void retireGlobalConnection(Connection conn) throws SQLException
      Retire a global DB connection.
      Parameters:
      conn - the connection to retire
      Throws:
      SQLException - SQL exception
      See Also:
    • getH2Url

      public H2Url getH2Url()
      Returns the parsed H2 URL.
      Returns:
      the H2 URL
    • isH2Db

      public boolean isH2Db()
      Checks whether the database is H2.
      Returns:
      true if the database is H2
    • isMysqlDb

      public boolean isMysqlDb()
      Checks whether the database is MySQL.
      Returns:
      true if the database is MySQL
    • isMariaDb

      public boolean isMariaDb()
      Checks whether the database is MariaDB.
      Returns:
      true if the database is MariaDB
    • isOracleDb

      public boolean isOracleDb()
      Checks whether the database is Oracle.
      Returns:
      true if the database is Oracle
    • isPostgreDb

      public boolean isPostgreDb()
      Checks whether the database is PostgreSQL.
      Returns:
      true if the database is PostgreSQL
    • isPostgreDbWithNGDriver

      public boolean isPostgreDbWithNGDriver()
      Checks whether PostgreSQL is used with the NG driver.
      Returns:
      true if the NG driver is used
    • isUnsupported

      public boolean isUnsupported()
      Checks whether the database is unsupported.
      Returns:
      true if the database is unsupported
    • resetToken

      public void resetToken(int dbId) throws Exception
      Reset users token.
      Parameters:
      dbId - user id
      Throws:
      Exception - exception
    • countRecords

      public int countRecords(String entity) throws SQLException
      Count amount of records of an entity / table in database.
      Parameters:
      entity - entity
      Returns:
      amount of records
      Throws:
      SQLException - SQL exception
    • onOrOff

      public boolean onOrOff(String name) throws SQLException
      Get property On/Off value from database (table 'properties'). If the value isn't found, false is returned.
      Parameters:
      name - On/Off switch name
      Returns:
      true (On) or false (Off, null or any other value not "on" or "On")
      Throws:
      SQLException - SQL exception
    • getProperty

      public String getProperty(String name, String defValue)
      Get property value from database (table 'properties'). If the value isn't found or an exception occurs, the default value is returned.
      Parameters:
      name - name/key
      defValue - default value
      Returns:
      value for name/key
    • getPropertyInt

      public int getPropertyInt(String name) throws SQLException
      Get property value from database (table 'properties'). If the value isn't found, null is returned. It throws a NullPointerException if value is not found in database.
      Parameters:
      name - name/key
      Returns:
      value for name/key as integer
      Throws:
      SQLException - SQL exception
    • getProperty

      public String getProperty(String name) throws SQLException
      Get property value from database (table 'properties'). If the value isn't found, null is returned.
      Parameters:
      name - name/key
      Returns:
      value for name/key
      Throws:
      SQLException - SQL exception
    • getLanguage

      public String getLanguage(int userId) throws Exception
      Get language for user.
      Parameters:
      userId - user id
      Returns:
      language
      Throws:
      Exception - exception
    • updateLanguage

      public void updateLanguage(String lang, int dbId) throws Exception
      Update language.
      Parameters:
      lang - language code
      dbId - user id
      Throws:
      Exception - exception
    • describeTable

      public List<DBField> describeTable(String table) throws SQLException
      Describe table columns for given table.
      Parameters:
      table - database table
      Returns:
      list of DB fields with table column descriptions
      Throws:
      SQLException - SQL exception
    • getUrl

      public String getUrl()
      Returns the JDBC URL.
      Returns:
      the JDBC URL
    • getUser

      public String getUser()
      Returns the database user.
      Returns:
      the database user
    • getPass

      public String getPass()
      Returns the database password.
      Returns:
      the database password
    • getDriver

      public String getDriver()
      Returns the JDBC driver class name.
      Returns:
      the driver class name
    • getDataSourceClassName

      public String getDataSourceClassName()
      Returns the data source class name.
      Returns:
      the data source class name