Class 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
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 Summary
FieldsFields inherited from class org.springframework.dao.support.DaoSupport
logger -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected String[]getBatchSqlResource(org.springframework.core.io.Resource sqlResource) Get batch SQL statements, split into multiple statements on the ; character.net.solarnetwork.service.OptionalService<org.osgi.service.event.EventAdmin>Get theEventAdminservice.org.springframework.core.io.ResourceGet the initial SQL resource.org.springframework.context.MessageSourceGet a MessageSource to resolve messages with related to this DAO.Get the database schema name this DAO is working with.Set a SQL fragment to append to SQL statements where an updatable result set is desired.Get the SQL to get the tables version.protected StringgetSqlResource(String classPathResource) Load a classpath SQL resource into a String.protected StringgetSqlResource(org.springframework.core.io.Resource resource) Load a SQL resource into a String.Get the SQL resource prefix.Get the primary database table name this DAO is working with.String[]This implementation simply returns a new array with a single value:getTableName().intGet the tables version.voidinit()Initialize this class after properties are set.protected voidinsertDomainObject(T obj, String sqlInsert) Insert a new domain object.booleanGet the auto-generated keys toggle value.protected final voidpostEvent(org.osgi.service.event.Event event) Post anEvent.protected booleanschemaExists(Connection conn, String aSchemaName) Test if a schema exists in the database.voidsetEventAdmin(net.solarnetwork.service.OptionalService<org.osgi.service.event.EventAdmin> eventAdmin) Set anEventAdminservice to use.voidsetInitSqlResource(org.springframework.core.io.Resource initSqlResource) Set the initial SQL resource.voidsetMessageSource(org.springframework.context.MessageSource messageSource) Set the message source.voidsetSchemaName(String schemaName) Set the schema name.voidsetSqlForUpdateSuffix(String sqlForUpdateSuffix) Set a SQL fragment to append to SQL statements where an updatable result set is desired.voidsetSqlGetTablesVersion(String sqlGetTablesVersion) Set the SQL to get the tables version.voidsetSqlResourcePrefix(String sqlResourcePrefix) Set the SQL resource prefix.protected voidsetStoreStatementValues(T obj, PreparedStatement ps) SetPreparedStatementvalues for storing a domain object.voidsetTableName(String tableName) Set the table name.voidsetTablesVersion(int tablesVersion) Set the tables version.protected voidsetUpdateStatementValues(T obj, PreparedStatement ps) SetPreparedStatementvalues for updating a domain object.voidsetUseAutogeneratedKeys(boolean useAutogeneratedKeys) Toggle the use of auto-generated keys.protected LongstoreDomainObject(T obj, String sqlInsert) Store (insert) a new domain object.protected LongstoreDomainObjectWithoutAutogeneratedKeys(T obj, String sqlInsert) Persist a domain object, without using auto-generated keys.protected booleantableExists(Connection conn, String aSchemaName, String aTableName) Test if a table exists in the database.protected intupdateDomainObject(T obj, String sqlUpdate) Update a domain object.protected voidUpgrade the database tables to the configured version, if the database version is less than the configured version.protected voidverifyDatabaseExists(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 providedinitSqlResource.Methods inherited from class org.springframework.jdbc.core.support.JdbcDaoSupport
checkDaoConfig, createJdbcTemplate, getConnection, getDataSource, getExceptionTranslator, getJdbcTemplate, initTemplateConfig, releaseConnection, setDataSource, setJdbcTemplateMethods inherited from class org.springframework.dao.support.DaoSupport
afterPropertiesSet, initDao
-
Field Details
-
log
protected final org.slf4j.Logger logA class-level Logger.
-
-
Constructor Details
-
AbstractJdbcDao
public AbstractJdbcDao()
-
-
Method Details
-
init
public void init()Initialize this class after properties are set. -
insertDomainObject
Insert a new domain object.- Parameters:
obj- the domain object to insertsqlInsert- the SQL to persist the object with
-
updateDomainObject
Update a domain object.- Parameters:
obj- the domain object to updatesqlUpdate- the SQL to persist the object with- Returns:
- the updated row count
- Since:
- 1.2
-
setUpdateStatementValues
SetPreparedStatementvalues 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
PreparedStatementobject as needed to persist the domain object.- Parameters:
obj- the domain object to persistps- the PreparedStatement to persist with- Throws:
SQLException- if any SQL error occurs- Since:
- 1.2
-
storeDomainObject
Store (insert) a new domain object.If
isUseAutogeneratedKeys()is true then this method will use JDBC'sStatement.RETURN_GENERATED_KEYSto obtain the auto-generated primary key for the newly inserted object. Otherwise, this method will call thestoreDomainObjectWithoutAutogeneratedKeys(Object, String)method.- Parameters:
obj- the domain object to persistsqlInsert- the SQL to persist the object with- Returns:
- the primary key created for the domain object
-
setStoreStatementValues
SetPreparedStatementvalues for storing a domain object.Called from
storeDomainObject(Object, String)andstoreDomainObjectWithoutAutogeneratedKeys(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
PreparedStatementobject as needed to persist the domain object.- Parameters:
obj- the domain object to persistps- the PreparedStatement to persist with- Throws:
SQLException- if any SQL error occurs
-
storeDomainObjectWithoutAutogeneratedKeys
Persist a domain object, without using auto-generated keys.- Parameters:
obj- the domain object to persistsqlInsert- 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 providedinitSqlResource.- Parameters:
schema- the schema to checktable- the table to checkinitSql- the init SQL resource
-
schemaExists
Test if a schema exists in the database.- Parameters:
conn- the connectionaSchemaName- 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 connectionaSchemaName- 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
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 thegetTablesVersion()value then a sequence of SQL resources, relative to thegetInitSqlResource()resource, are loaded and executed. The resources are assumed to have a file name patter like[tablesUpdatePrefix]-update-[#].sqlwhere [tablesUpdatePrefix] is thegetSqlResourcePrefix()value and [#] is the version number, starting at the currently found table version + 1 up through and includinggetTablesVersion(). If no version value is found when querying, the current version is assumed to be0.For example, if by querying the current version is reported as
1and thegetTablesVersion()value is3, and thegetSqlResourcePrefix()isderby-mytable, the following SQL resources will be exectued:- derby-mytable-update-2.sql
- 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
Load a classpath SQL resource into a String.The classpath resource is taken as the
getSqlResourcePrefix()value and-and theclassPathResourcecombined with a.sqlsuffix. 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-andclassPathResourceagain to try to find a match, until there is no prefix left and just theclassPathResourceitself 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
Load a SQL resource into a String.- Parameters:
resource- the SQL resource to load- Returns:
- the String
-
getBatchSqlResource
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 anEvent.This method only works if a
EventAdminhas been configured viasetEventAdmin(OptionalService). Otherwise the event is silently ignored.- Parameters:
event- the event to post- Since:
- 1.5
-
getTableNames
This implementation simply returns a new array with a single value:getTableName().- Specified by:
getTableNamesin interfaceJdbcDao- Returns:
- database table name
- See Also:
-
getSqlGetTablesVersion
Get the SQL to get the tables version.- Returns:
- the SQL
-
setSqlGetTablesVersion
Set the SQL to get the tables version.- Parameters:
sqlGetTablesVersion- the SQL to set
-
getSqlResourcePrefix
Get the SQL resource prefix.- Returns:
- the prefix
-
setSqlResourcePrefix
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
Description copied from interface:JdbcDaoGet the database schema name this DAO is working with.- Specified by:
getSchemaNamein interfaceJdbcDao- Returns:
- database schema name
-
setSchemaName
Set the schema name.- Parameters:
schemaName- the name to set
-
getTableName
Description copied from interface:JdbcDaoGet the primary database table name this DAO is working with.- Specified by:
getTableNamein interfaceJdbcDao- Returns:
- primary database table name
-
setTableName
Set the table name.- Parameters:
tableName- the name to set
-
getMessageSource
public org.springframework.context.MessageSource getMessageSource()Description copied from interface:JdbcDaoGet a MessageSource to resolve messages with related to this DAO.- Specified by:
getMessageSourcein interfaceJdbcDao- Returns:
- message source
-
setMessageSource
public void setMessageSource(org.springframework.context.MessageSource messageSource) Set the message source.- Parameters:
messageSource- the message source to set
-
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
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 theEventAdminservice.- Returns:
- the EventAdmin service
- Since:
- 1.5
-
setEventAdmin
public void setEventAdmin(net.solarnetwork.service.OptionalService<org.osgi.service.event.EventAdmin> eventAdmin) Set anEventAdminservice to use.- Parameters:
eventAdmin- the EventAdmin to use- Since:
- 1.5
-