org.dellroad.stuff.schema
Class SpringSQLSchemaUpdate

java.lang.Object
  extended by org.dellroad.stuff.schema.AbstractSchemaUpdate<T>
      extended by org.dellroad.stuff.schema.AbstractSpringSchemaUpdate<Connection>
          extended by org.dellroad.stuff.schema.SpringSQLSchemaUpdate
All Implemented Interfaces:
SchemaUpdate<Connection>, BeanFactoryAware, BeanNameAware, InitializingBean

public class SpringSQLSchemaUpdate
extends AbstractSpringSchemaUpdate<Connection>

Spring-enabled SQL SchemaUpdate.

The sqlCommandList property is required.

Instances can be created succintly in Spring using the <dellroad-stuff:sql-update> custom XML element, which works just like <dellroad-stuff:sql> except that it wraps the resulting SQLCommandList as a delegate inside an instance of this class.

For example:

  <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:dellroad-stuff="http://dellroad-stuff.googlecode.com/schema/dellroad-stuff"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
      http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://dellroad-stuff.googlecode.com/schema/dellroad-stuff
        http://dellroad-stuff.googlecode.com/svn/wiki/schemas/dellroad-stuff-1.0.xsd">

      <!-- Schema update to add the 'phone' column to the 'User' table -->
      <dellroad-stuff:sql-update id="addPhone">ALTER TABLE User ADD phone VARCHAR(64)</dellroad-stuff:sql-update>

      <!-- Schema update to run some complicated external SQL script -->
      <dellroad-stuff:sql-update id="majorChanges" depends-on="addPhone" resource="classpath:majorChanges.sql"/>

      <!-- more beans... -->

  </beans>
 

A multi-statement SQL script is normally treated as a set of individual updates. For example:

      <dellroad-stuff:sql-update id="renameColumn">
          ALTER TABLE User ADD newName VARCHAR(64);
          ALTER TABLE User SET newName = oldName;
          ALTER TABLE User DROP oldName;
      </dellroad-stuff:sql-update>
 
This will create three separate update beans named renameColumn-00001, renameColumn-00002, and renameColumn-00003. You can disable this behavior by adding the attribute single-action="true", in which case all three of the statements will be executed together in the same transaction and recorded under the name renameColumn; this means that they must all complete successfully or you could end up with a partially completed update.

Note that if the nested SQL script only contains one SQL statement, any single-action attribute is ignored and the bean's given name (e.g., renameColumn) is always used as the name of the single update.

See Also:
SQLCommandList

Field Summary
 
Fields inherited from class org.dellroad.stuff.schema.AbstractSpringSchemaUpdate
beanFactory, beanName
 
Constructor Summary
SpringSQLSchemaUpdate()
           
 
Method Summary
 void afterPropertiesSet()
          Configures the update name and required predecessors based on the Spring bean's name and BeanFactory dependencies.
 List<DatabaseAction<Connection>> getDatabaseActions()
          Get the action(s) that comprise this update.
 SQLCommandList getSQLCommandList()
           
 void setSQLCommandList(SQLCommandList sqlCommandList)
          Configure the SQLCommandList.
 
Methods inherited from class org.dellroad.stuff.schema.AbstractSpringSchemaUpdate
setBeanFactory, setBeanName, setRequiredPredecessorsFromDependencies
 
Methods inherited from class org.dellroad.stuff.schema.AbstractSchemaUpdate
getName, getRequiredPredecessors, isSingleAction, setName, setRequiredPredecessors, setSingleAction, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SpringSQLSchemaUpdate

public SpringSQLSchemaUpdate()
Method Detail

afterPropertiesSet

public void afterPropertiesSet()
                        throws Exception
Description copied from class: AbstractSpringSchemaUpdate
Configures the update name and required predecessors based on the Spring bean's name and BeanFactory dependencies.

Specified by:
afterPropertiesSet in interface InitializingBean
Overrides:
afterPropertiesSet in class AbstractSpringSchemaUpdate<Connection>
Throws:
Exception

setSQLCommandList

public void setSQLCommandList(SQLCommandList sqlCommandList)
Configure the SQLCommandList. This is a required property.

See Also:
DatabaseAction

getSQLCommandList

public SQLCommandList getSQLCommandList()

getDatabaseActions

public List<DatabaseAction<Connection>> getDatabaseActions()
Description copied from interface: SchemaUpdate
Get the action(s) that comprise this update. Ideally, individual actions should be atomic database operations, i.e., each one should either finish completely, or else leave the database in a state where it can be tried again. In any case, each action will be applied within its own transaction when transactions are supported by the database unless SchemaUpdate.isSingleAction() returns true.

Returns:
a list of zero or more actions to apply
See Also:
SchemaUpdate.isSingleAction()