Class Database

  • All Implemented Interfaces:
    Shutdownable

    public class Database
    extends java.lang.Object
    implements Shutdownable
    Class that represents a physical SQL database
    Author:
    Andrew Varley, Matthew Wakeling
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected javax.sql.DataSource datasource  
      protected java.lang.String driver  
      protected int parallel
      The number of worker threads to use for background SQL statements
      protected java.lang.String platform  
      protected java.lang.String schema  
      protected java.util.Properties settings  
      protected java.lang.String version  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected Database()
      No argument constructor for testing purposes
      protected Database​(java.util.Properties props)
      Constructs a Database object from a set of properties
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void configure​(java.util.Properties props)
      Configures a datasource from a Properties object
      void executeSqlInParallel​(java.lang.String sql)
      Executes an SQL statement on the database in a separate thread.
      void finalize()
      java.lang.String getColumnTypeString​(java.lang.Class<?> c)
      Return the SQL type used to store objects of the given Class.
      java.sql.Connection getConnection()
      Gets a Connection to this Database
      javax.sql.DataSource getDataSource()
      Gets the DataSource object for this Database
      java.lang.String getDriver()
      Gets the driver this Database
      java.lang.String getName()
      Gets the database name only, not the full URL.
      java.lang.String getPassword()
      Gets the password for this Database
      java.lang.String getPlatform()
      Gets the platform of this Database
      java.lang.String getURL()
      Gets the URL from this database
      java.lang.String getUser()
      Gets the username for this Database
      java.lang.String getVersion()
      Get the version number of the database as a string.
      boolean isVersionAtLeast​(java.lang.String testVersionStr)
      Return true if the database version is at least as high as the test number given, taking into account major and minor versions.
      void shutdown()
      Logs stuff
      java.lang.String toString()
      void waitForCurrentJobs()
      Blocks until all the current pending jobs are finished.
      • Methods inherited from class java.lang.Object

        clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • datasource

        protected javax.sql.DataSource datasource
      • platform

        protected java.lang.String platform
      • driver

        protected java.lang.String driver
      • parallel

        protected int parallel
        The number of worker threads to use for background SQL statements
      • settings

        protected java.util.Properties settings
      • version

        protected java.lang.String version
      • schema

        protected java.lang.String schema
    • Constructor Detail

      • Database

        protected Database()
        No argument constructor for testing purposes
      • Database

        protected Database​(java.util.Properties props)
                    throws java.lang.ClassNotFoundException
        Constructs a Database object from a set of properties
        Parameters:
        props - the properties by which this Database is configured
        Throws:
        java.lang.ClassNotFoundException - if there is a class in props that cannot be found
    • Method Detail

      • getDataSource

        public javax.sql.DataSource getDataSource()
        Gets the DataSource object for this Database
        Returns:
        the datasource for this Database
      • getConnection

        public java.sql.Connection getConnection()
                                          throws DatabaseConnectionException
        Gets a Connection to this Database
        Returns:
        a Connection to this Database
        Throws:
        DatabaseConnectionException - if it wasn't possible to get a connection to the underlying database.
      • finalize

        public void finalize()
                      throws java.lang.Throwable
        Overrides:
        finalize in class java.lang.Object
        Throws:
        java.lang.Throwable
      • getPlatform

        public java.lang.String getPlatform()
        Gets the platform of this Database
        Returns:
        the datasource for this Database
      • getDriver

        public java.lang.String getDriver()
        Gets the driver this Database
        Returns:
        the driver for this Database
      • getUser

        public java.lang.String getUser()
        Gets the username for this Database
        Returns:
        the username for this Database
      • getPassword

        public java.lang.String getPassword()
        Gets the password for this Database
        Returns:
        the password for this Database
      • getURL

        public java.lang.String getURL()
        Gets the URL from this database
        Returns:
        the URL for this database
      • getName

        public java.lang.String getName()
        Gets the database name only, not the full URL.
        Returns:
        the database name
      • getVersion

        public java.lang.String getVersion()
        Get the version number of the database as a string. Currently throws an error if database server is anything other than postgres.
        Returns:
        the database version number
      • isVersionAtLeast

        public boolean isVersionAtLeast​(java.lang.String testVersionStr)
        Return true if the database version is at least as high as the test number given, taking into account major and minor versions. e.g. test if database is at least 9.2
        Parameters:
        testVersionStr - a postgres version number of dot separated integers
        Returns:
        true if the database is the version specified or later
      • configure

        protected void configure​(java.util.Properties props)
                          throws java.lang.ClassNotFoundException
        Configures a datasource from a Properties object
        Parameters:
        props - the properties for configuring the Database
        Throws:
        java.lang.ClassNotFoundException - if the class given in the properties file cannot be found
        java.lang.IllegalArgumentException - if the configuration properties are empty
        java.lang.NullPointerException - if props is null
      • getColumnTypeString

        public java.lang.String getColumnTypeString​(java.lang.Class<?> c)
        Return the SQL type used to store objects of the given Class. eg. return "double precision" for Double.class
        Parameters:
        c - the Class representing the java type
        Returns:
        the SQL type
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • executeSqlInParallel

        public void executeSqlInParallel​(java.lang.String sql)
                                  throws java.sql.SQLException
        Executes an SQL statement on the database in a separate thread. A certain number of worker threads controlled by the "parallel" property will operate simultaneously.
        Parameters:
        sql - an SQL string.
        Throws:
        java.sql.SQLException - if an error has been reported by a previous operation - however, this does not cancel the current operation.
      • waitForCurrentJobs

        public void waitForCurrentJobs()
                                throws java.sql.SQLException
        Blocks until all the current pending jobs are finished. This will not block new jobs from arriving, and those new jobs do not need to be finished for this method to return.
        Throws:
        java.sql.SQLException - if an error has been reported by a previous operation