|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.dellroad.stuff.schema.SQLCommandList
public class SQLCommandList
Holds and executes a configured SQL script, possibly containing multiple statements.
If the script contains multiple SQL statements, individual statements will be executed individually, in order, by
apply(); however, this requires proper configuration of the split pattern.
When using Spring, beans of this type can be created succintly using the <dellroad-stuff:sql> custom
XML element. The split pattern may be configured via the split-pattern attribute,
and the SQL script is specified either directly via inline text or using the resource attribute. In the latter case,
the character encoding can specified via the charset attribute (default is "UTF-8").
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">
<!-- SQL action that clears the audit log -->
<dellroad-stuff:sql>DELETE * FROM AUDIT_LOG</dellroad-stuff:sql>
<!-- A more complicated, externally referenced, SQL script -->
<dellroad-stuff:sql resource="classpath:reset.sql" split-pattern=";\n"/>
<!-- other beans... -->
</beans>
| Field Summary | |
|---|---|
static String |
DEFAULT_SPLIT_PATTERN
The default split pattern: ";[ \\t\\r]*\\n\\s*". |
| Constructor Summary | |
|---|---|
SQLCommandList()
|
|
SQLCommandList(String sqlScript)
|
|
| Method Summary | |
|---|---|
void |
apply(Connection c)
Applies each individual SQL command in the script. |
void |
setSplitPattern(String splitPattern)
Set the split pattern used to split apart a script containing multiple SQL statements into individual statements. |
void |
setSQLScript(String sqlScript)
Configure the SQL script. |
List<SQLCommand> |
split()
Split the SQL script into individual statements and return them as DatabaseActions. |
String[] |
splitSQL()
Split the configured SQL script into individual SQL statements using the configured split pattern. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final String DEFAULT_SPLIT_PATTERN
";[ \\t\\r]*\\n\\s*".
| Constructor Detail |
|---|
public SQLCommandList()
public SQLCommandList(String sqlScript)
| Method Detail |
|---|
public void setSQLScript(String sqlScript)
For scripts in external resources, consider ResourceReaderFactoryBean
or use the <dellroad-stuff:sql> element.
sqlScript - script containing one or more SQL statements; if more than one statement is present,
a split pattern must also be configuredsetSplitPattern()public void setSplitPattern(String splitPattern)
The default value for this property is ";[ \t\r]*\n\s*", which should handle cases where
SQL statements are terminated by semi-colons and each SQL statement starts on a new line.
If this is set to null, or the script does not contain any instances of the regular expression,
the script is assumed to contain a single SQL statement. SQL statements are whitespace-trimmed and any
"statements" that consist entirely of whitespace are ignored.
PatternSyntaxException - if the pattern is not a valid Java regular expression
public void apply(Connection c)
throws SQLException
split pattern.
apply in interface DatabaseAction<Connection>c - open transaction
SQLExceptionpublic List<SQLCommand> split()
DatabaseActions.
public String[] splitSQL()
IllegalArgumentException - if no SQL script is configured
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||