Class AbstractJdbcDao<T>

java.lang.Object
org.springframework.dao.support.DaoSupport
org.springframework.jdbc.core.support.JdbcDaoSupport
net.solarnetwork.node.dao.jdbc.AbstractJdbcDao<T>
Type Parameters:
T - the domain object type managed by this DAO
All Implemented Interfaces:
JdbcDao, org.springframework.beans.factory.InitializingBean
Direct Known Subclasses:
BaseJdbcGenericDao, JdbcGeneralNodeDatumDao

public abstract class AbstractJdbcDao<T> extends org.springframework.jdbc.core.support.JdbcDaoSupport implements JdbcDao
Base class for JDBC based DAO implementations.

This class extends JdbcDaoSupport with methods for handling upgrade maintenance of the table(s) managed by this DAO over time, e.g. creating tables if they don't exist, running DDL update scripts to upgrade to a new version, etc.

Version:
2.2
Author:
matt
  • Field Details

    • log

      protected final org.slf4j.Logger log
      A class-level Logger.
  • Constructor Details

    • AbstractJdbcDao

      public AbstractJdbcDao()
  • Method Details

    • init

      public void init()
      Initialize this class after properties are set.
    • insertDomainObject

      protected void insertDomainObject(T obj, String sqlInsert)
      Insert a new domain object.
      Parameters:
      obj - the domain object to insert
      sqlInsert - the SQL to persist the object with
    • updateDomainObject

      protected int updateDomainObject(T obj, String sqlUpdate)
      Update a domain object.
      Parameters:
      obj - the domain object to update
      sqlUpdate - the SQL to persist the object with
      Returns:
      the updated row count
      Since:
      1.2
    • setUpdateStatementValues

      protected void setUpdateStatementValues(T obj, PreparedStatement ps) throws SQLException
      Set PreparedStatement values for updating a domain object.

      Called from updateDomainObject(Object, String) to persist changed values of a domain object.

      This implementation does not do anything. Extending classes should override this and set values on the PreparedStatement object as needed to persist the domain object.

      Parameters:
      obj - the domain object to persist
      ps - the PreparedStatement to persist with
      Throws:
      SQLException - if any SQL error occurs
      Since:
      1.2
    • storeDomainObject

      protected Long storeDomainObject(T obj, String sqlInsert)
      Store (insert) a new domain object.

      If isUseAutogeneratedKeys() is true then this method will use JDBC's Statement.RETURN_GENERATED_KEYS to obtain the auto-generated primary key for the newly inserted object. Otherwise, this method will call the storeDomainObjectWithoutAutogeneratedKeys(Object, String) method.

      Parameters:
      obj - the domain object to persist
      sqlInsert - the SQL to persist the object with
      Returns:
      the primary key created for the domain object
    • setStoreStatementValues

      protected void setStoreStatementValues(T obj, PreparedStatement ps) throws SQLException
      Set PreparedStatement values for storing a domain object.

      Called from storeDomainObject(Object, String) and storeDomainObjectWithoutAutogeneratedKeys(Object, String) to persist values of a domain object.

      This implementation does not do anything. Extending classes should override this and set values on the PreparedStatement object as needed to persist the domain object.

      Parameters:
      obj - the domain object to persist
      ps - the PreparedStatement to persist with
      Throws:
      SQLException - if any SQL error occurs
    • storeDomainObjectWithoutAutogeneratedKeys

      protected Long storeDomainObjectWithoutAutogeneratedKeys(T obj, String sqlInsert)
      Persist a domain object, without using auto-generated keys.
      Parameters:
      obj - the domain object to persist
      sqlInsert - the SQL insert statement to use
      Returns:
      the primary key created for the domain object
    • verifyDatabaseExists

      protected void verifyDatabaseExists(String schema, String table, org.springframework.core.io.Resource initSql)
      Verify a database table exists, and if not initialize the database with the SQL in the provided initSqlResource.
      Parameters:
      schema - the schema to check
      table - the table to check
      initSql - the init SQL resource
    • schemaExists

      protected boolean schemaExists(Connection conn, String aSchemaName) throws SQLException
      Test if a schema exists in the database.
      Parameters:
      conn - the connection
      aSchemaName - the schema name to look for
      Returns:
      true if the schema is found
      Throws:
      SQLException - if any SQL error occurs
      Since:
      1.3
    • tableExists

      protected boolean tableExists(Connection conn, String aSchemaName, String aTableName) throws SQLException
      Test if a table exists in the database.
      Parameters:
      conn - the connection
      aSchemaName - the schema name to look for (or null for any schema)
      aTableName - the table name to look for
      Returns:
      boolean if table is found
      Throws:
      SQLException - if any SQL error occurs
    • upgradeTablesVersion

      protected void upgradeTablesVersion() throws IOException
      Upgrade the database tables to the configured version, if the database version is less than the configured version.

      This method uses the getSqlGetTablesVersion() SQL statement to query for the current database table version. If the version found there is less than the getTablesVersion() value then a sequence of SQL resources, relative to the getInitSqlResource() resource, are loaded and executed. The resources are assumed to have a file name patter like [tablesUpdatePrefix]-update-[#].sql where [tablesUpdatePrefix] is the getSqlResourcePrefix() value and [#] is the version number, starting at the currently found table version + 1 up through and including getTablesVersion(). If no version value is found when querying, the current version is assumed to be 0.

      For example, if by querying the current version is reported as 1 and the getTablesVersion() value is 3, and the getSqlResourcePrefix() is derby-mytable, the following SQL resources will be exectued:

      1. derby-mytable-update-2.sql
      2. derby-mytable-update-3.sql

      Each update SQL is expected, therefor, to also update the "current" table version so that after running the update subsequent calls to this method where getTablesVersion() has not changed will not do anything.

      Throws:
      IOException - if update resources cannot be found
    • getSqlResource

      protected String getSqlResource(String classPathResource)
      Load a classpath SQL resource into a String.

      The classpath resource is taken as the getSqlResourcePrefix() value and - and the classPathResource combined with a .sql suffix. If that resoruce is not found, then the prefix is split into components separated by a - character, and the last component is dropped and then combined with - and classPathResource again to try to find a match, until there is no prefix left and just the classPathResource itself is tried.

      This method will cache the SQL resource in-memory for quick future access.

      Parameters:
      classPathResource - the classpath resource to load as a SQL string
      Returns:
      the String
    • getSqlResource

      protected String getSqlResource(org.springframework.core.io.Resource resource)
      Load a SQL resource into a String.
      Parameters:
      resource - the SQL resource to load
      Returns:
      the String
    • getBatchSqlResource

      protected String[] getBatchSqlResource(org.springframework.core.io.Resource sqlResource)
      Get batch SQL statements, split into multiple statements on the ; character.
      Parameters:
      sqlResource - the SQL resource to load
      Returns:
      split SQL
    • postEvent

      protected final void postEvent(org.osgi.service.event.Event event)
      Post an Event.

      This method only works if a EventAdmin has been configured via setEventAdmin(OptionalService). Otherwise the event is silently ignored.

      Parameters:
      event - the event to post
      Since:
      1.5
    • getTableNames

      public String[] getTableNames()
      This implementation simply returns a new array with a single value: getTableName().
      Specified by:
      getTableNames in interface JdbcDao
      Returns:
      database table name
      See Also:
    • getSqlGetTablesVersion

      public String getSqlGetTablesVersion()
      Get the SQL to get the tables version.
      Returns:
      the SQL
    • setSqlGetTablesVersion

      public void setSqlGetTablesVersion(String sqlGetTablesVersion)
      Set the SQL to get the tables version.
      Parameters:
      sqlGetTablesVersion - the SQL to set
    • getSqlResourcePrefix

      public String getSqlResourcePrefix()
      Get the SQL resource prefix.
      Returns:
      the prefix
    • setSqlResourcePrefix

      public void setSqlResourcePrefix(String sqlResourcePrefix)
      Set the SQL resource prefix.
      Parameters:
      sqlResourcePrefix - the prefix
    • getTablesVersion

      public int getTablesVersion()
      Get the tables version.
      Returns:
      the version
    • setTablesVersion

      public void setTablesVersion(int tablesVersion)
      Set the tables version.
      Parameters:
      tablesVersion - the version to set
    • isUseAutogeneratedKeys

      public boolean isUseAutogeneratedKeys()
      Get the auto-generated keys toggle value.
      Returns:
      true to use auto-generated keys
    • setUseAutogeneratedKeys

      public void setUseAutogeneratedKeys(boolean useAutogeneratedKeys)
      Toggle the use of auto-generated keys.
      Parameters:
      useAutogeneratedKeys - true to use auto-generated keys
    • getInitSqlResource

      public org.springframework.core.io.Resource getInitSqlResource()
      Get the initial SQL resource.
      Returns:
      the resource
    • setInitSqlResource

      public void setInitSqlResource(org.springframework.core.io.Resource initSqlResource)
      Set the initial SQL resource.
      Parameters:
      initSqlResource - the resource to set
    • getSchemaName

      public String getSchemaName()
      Description copied from interface: JdbcDao
      Get the database schema name this DAO is working with.
      Specified by:
      getSchemaName in interface JdbcDao
      Returns:
      database schema name
    • setSchemaName

      public void setSchemaName(String schemaName)
      Set the schema name.
      Parameters:
      schemaName - the name to set
    • getTableName

      public String getTableName()
      Description copied from interface: JdbcDao
      Get the primary database table name this DAO is working with.
      Specified by:
      getTableName in interface JdbcDao
      Returns:
      primary database table name
    • setTableName

      public void setTableName(String tableName)
      Set the table name.
      Parameters:
      tableName - the name to set
    • getMessageSource

      public org.springframework.context.MessageSource getMessageSource()
      Description copied from interface: JdbcDao
      Get a MessageSource to resolve messages with related to this DAO.
      Specified by:
      getMessageSource in interface JdbcDao
      Returns:
      message source
    • setMessageSource

      public void setMessageSource(org.springframework.context.MessageSource messageSource)
      Set the message source.
      Parameters:
      messageSource - the message source to set
    • getSqlForUpdateSuffix

      public String getSqlForUpdateSuffix()
      Set a SQL fragment to append to SQL statements where an updatable result set is desired.
      Returns:
      the SQL suffix, or null if not desired
      Since:
      1.4
    • setSqlForUpdateSuffix

      public void setSqlForUpdateSuffix(String sqlForUpdateSuffix)
      Set a SQL fragment to append to SQL statements where an updatable result set is desired.

      This defaults to FOR UPDATE. Note a space must be included at the beginning. Set to null to disable.

      Parameters:
      sqlForUpdateSuffix - the suffix to set
      Since:
      1.4
    • getEventAdmin

      public net.solarnetwork.service.OptionalService<org.osgi.service.event.EventAdmin> getEventAdmin()
      Get the EventAdmin service.
      Returns:
      the EventAdmin service
      Since:
      1.5
    • setEventAdmin

      public void setEventAdmin(net.solarnetwork.service.OptionalService<org.osgi.service.event.EventAdmin> eventAdmin)
      Set an EventAdmin service to use.
      Parameters:
      eventAdmin - the EventAdmin to use
      Since:
      1.5