public class ResourceDatabasePopulator extends Object implements DatabasePopulator
Call addScript(Resource)
to add a single SQL script location.
Call addScripts(Resource...)
to add multiple SQL script locations.
Call setSqlScriptEncoding(String)
to set the encoding for all added
scripts.
DatabasePopulatorUtils
Constructor and Description |
---|
ResourceDatabasePopulator()
Construct a new
ResourceDatabasePopulator with default settings. |
ResourceDatabasePopulator(boolean continueOnError,
boolean ignoreFailedDrops,
String sqlScriptEncoding,
Resource... scripts)
Construct a new
ResourceDatabasePopulator with the supplied values. |
ResourceDatabasePopulator(Resource... scripts)
Construct a new
ResourceDatabasePopulator with default settings
for the supplied scripts. |
Modifier and Type | Method and Description |
---|---|
void |
addScript(Resource script)
Add a script to execute to initialize or populate the database.
|
void |
addScripts(Resource... scripts)
Add multiple scripts to execute to initialize or populate the database.
|
void |
populate(Connection connection)
Populate, initialize, or clean up the database using the provided JDBC
connection.
|
void |
setBlockCommentEndDelimiter(String blockCommentEndDelimiter)
Set the end delimiter that identifies block comments within the SQL
scripts.
|
void |
setBlockCommentStartDelimiter(String blockCommentStartDelimiter)
Set the start delimiter that identifies block comments within the SQL
scripts.
|
void |
setCommentPrefix(String commentPrefix)
Set the prefix that identifies single-line comments within the SQL scripts.
|
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 initialize or populate the database,
replacing any previously added scripts.
|
void |
setSeparator(String separator)
Specify the statement separator, if a custom one.
|
void |
setSqlScriptEncoding(String sqlScriptEncoding)
Specify the encoding for SQL scripts, if different from the platform encoding.
|
public ResourceDatabasePopulator()
ResourceDatabasePopulator
with default settings.public ResourceDatabasePopulator(Resource... scripts)
ResourceDatabasePopulator
with default settings
for the supplied scripts.scripts
- the scripts to execute to initialize or populate the databasepublic ResourceDatabasePopulator(boolean continueOnError, boolean ignoreFailedDrops, String sqlScriptEncoding, Resource... scripts)
ResourceDatabasePopulator
with the supplied values.continueOnError
- flag to indicate that all failures in SQL should be
logged but not cause a failureignoreFailedDrops
- flag to indicate that a failed SQL DROP
statement can be ignoredsqlScriptEncoding
- the encoding for the supplied SQL scripts, if
different from the platform encoding; may be null
scripts
- the scripts to execute to initialize or populate the databasepublic void addScript(Resource script)
script
- the path to an SQL scriptpublic void addScripts(Resource... scripts)
scripts
- the scripts to executepublic void setScripts(Resource... scripts)
scripts
- the scripts to executepublic void setSqlScriptEncoding(String sqlScriptEncoding)
sqlScriptEncoding
- the encoding used in scriptsaddScript(Resource)
public void setSeparator(String separator)
Defaults to ";"
if not specified and falls back to "\n"
as a last resort; may be set to ScriptUtils.EOF_STATEMENT_SEPARATOR
to signal that each script contains a single statement without a separator.
separator
- the script statement separatorpublic void setCommentPrefix(String commentPrefix)
Defaults to "--"
.
commentPrefix
- the prefix for single-line commentspublic void setBlockCommentStartDelimiter(String blockCommentStartDelimiter)
Defaults to "/*"
.
blockCommentStartDelimiter
- the start delimiter for block commentssetBlockCommentEndDelimiter(java.lang.String)
public void setBlockCommentEndDelimiter(String blockCommentEndDelimiter)
Defaults to "*/"
.
blockCommentEndDelimiter
- the end delimiter for block commentssetBlockCommentStartDelimiter(java.lang.String)
public void setContinueOnError(boolean continueOnError)
Defaults to false
.
continueOnError
- true
if script execution should continue on errorpublic 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
statement.
The default is false
so that if the populator runs accidentally, it will
fail fast if the script starts with a DROP
statement.
ignoreFailedDrops
- true
if failed drop statements should be ignoredpublic void populate(Connection connection) throws ScriptException
Concrete implementations may throw an SQLException
if
an error is encountered but are strongly encouraged to throw a
specific ScriptException
instead. For example, Spring's
ResourceDatabasePopulator
and DatabasePopulatorUtils
wrap
all SQLExceptions
in ScriptExceptions
.
populate
in interface DatabasePopulator
connection
- the JDBC connection to use to populate the db; already
configured and ready to useScriptException
- in all other error casesDatabasePopulatorUtils.execute(org.springframework.jdbc.datasource.init.DatabasePopulator, javax.sql.DataSource)