public class ResourceDatabasePopulator extends java.lang.Object implements DatabasePopulator
Call addScript(Resource)
to add a SQL script location.
Call setSqlScriptEncoding(String)
to set the encoding for all added scripts.
Modifier and Type | Field and Description |
---|---|
private java.lang.String |
commentPrefix |
private boolean |
continueOnError |
private static java.lang.String |
DEFAULT_COMMENT_PREFIX |
private static java.lang.String |
DEFAULT_STATEMENT_SEPARATOR |
private boolean |
ignoreFailedDrops |
private static Log |
logger |
private java.util.List<Resource> |
scripts |
private java.lang.String |
separator |
private java.lang.String |
sqlScriptEncoding |
Constructor and Description |
---|
ResourceDatabasePopulator() |
Modifier and Type | Method and Description |
---|---|
void |
addScript(Resource script)
Add a script to execute to populate the database.
|
private EncodedResource |
applyEncodingIfNecessary(Resource script) |
private boolean |
containsSqlScriptDelimiters(java.lang.String script,
java.lang.String delim)
Does the provided SQL script contain the specified delimiter?
|
private void |
executeSqlScript(java.sql.Connection connection,
EncodedResource resource,
boolean continueOnError,
boolean ignoreFailedDrops)
Execute the given SQL script.
|
private void |
maybeAddSeparatorToScript(java.lang.StringBuilder scriptBuilder) |
void |
populate(java.sql.Connection connection)
Populate the database using the JDBC connection provided.
|
private java.lang.String |
readScript(EncodedResource resource)
Read a script from the given resource and build a String containing the lines.
|
void |
setCommentPrefix(java.lang.String commentPrefix)
Set the line prefix that identifies comments in the SQL script.
|
void |
setContinueOnError(boolean continueOnError)
Flag to indicate that all failures in SQL should be logged but not cause a failure.
|
void |
setIgnoreFailedDrops(boolean ignoreFailedDrops)
Flag to indicate that a failed SQL
DROP statement can be ignored. |
void |
setScripts(Resource[] scripts)
Set the scripts to execute to populate the database.
|
void |
setSeparator(java.lang.String separator)
Specify the statement separator, if a custom one.
|
void |
setSqlScriptEncoding(java.lang.String sqlScriptEncoding)
Specify the encoding for SQL scripts, if different from the platform encoding.
|
private void |
splitSqlScript(java.lang.String script,
java.lang.String delim,
java.util.List<java.lang.String> statements)
Split an SQL script into separate statements delimited with the provided delimiter
character.
|
private boolean |
startsWithDelimiter(java.lang.String source,
int startIndex,
java.lang.String delim)
Return whether the substring of a given source
String starting at the
given index starts with the given delimiter. |
private static final java.lang.String DEFAULT_COMMENT_PREFIX
private static final java.lang.String DEFAULT_STATEMENT_SEPARATOR
private static final Log logger
private java.util.List<Resource> scripts
private java.lang.String sqlScriptEncoding
private java.lang.String separator
private java.lang.String commentPrefix
private boolean continueOnError
private boolean ignoreFailedDrops
public void addScript(Resource script)
script
- the path to a SQL scriptpublic void setScripts(Resource[] scripts)
scripts
- the scripts to executepublic void setSqlScriptEncoding(java.lang.String sqlScriptEncoding)
encoded resources
.addScript(Resource)
public void setSeparator(java.lang.String separator)
public void setCommentPrefix(java.lang.String commentPrefix)
public void setContinueOnError(boolean continueOnError)
public void setIgnoreFailedDrops(boolean ignoreFailedDrops)
DROP
statement can be ignored.
This is useful for non-embedded databases whose SQL dialect does not support an
IF EXISTS
clause in a DROP
. The default is false so that if the
populator runs accidentally, it will fail fast when the script starts with a DROP
.
public void populate(java.sql.Connection connection) throws java.sql.SQLException
DatabasePopulator
populate
in interface DatabasePopulator
connection
- the JDBC connection to use to populate the db; already configured and ready to usejava.sql.SQLException
- if an unrecoverable data access exception occurs during database populationprivate EncodedResource applyEncodingIfNecessary(Resource script)
private void executeSqlScript(java.sql.Connection connection, EncodedResource resource, boolean continueOnError, boolean ignoreFailedDrops) throws java.sql.SQLException
The script will normally be loaded by classpath. There should be one statement
per line. Any statement separators
will be removed.
Do not use this method to execute DDL if you expect rollback.
connection
- the JDBC Connection with which to perform JDBC operationsresource
- the resource (potentially associated with a specific encoding) to load the SQL script fromcontinueOnError
- whether or not to continue without throwing an exception in the event of an errorignoreFailedDrops
- whether of not to continue in the event of specifically an error on a DROP
java.sql.SQLException
private java.lang.String readScript(EncodedResource resource) throws java.io.IOException
resource
- the resource to be readString
containing the script linesjava.io.IOException
- in case of I/O errorsprivate void maybeAddSeparatorToScript(java.lang.StringBuilder scriptBuilder)
private boolean containsSqlScriptDelimiters(java.lang.String script, java.lang.String delim)
script
- the SQL scriptdelim
- character delimiting each statement - typically a ';' characterprivate boolean startsWithDelimiter(java.lang.String source, int startIndex, java.lang.String delim)
String
starting at the
given index starts with the given delimiter.source
- the source String
to inspectstartIndex
- the index to look for the delimiterdelim
- the delimiter to look forprivate void splitSqlScript(java.lang.String script, java.lang.String delim, java.util.List<java.lang.String> statements)
List
.script
- the SQL scriptdelim
- character delimiting each statement (typically a ';' character)statements
- the List that will contain the individual statements