Class ResourceDatabasePopulator
- All Implemented Interfaces:
- DatabasePopulator
- Call addScript(org.springframework.core.io.Resource)to add a single SQL script location.
- Call addScripts(org.springframework.core.io.Resource...)to add multiple SQL script locations.
- Consult the setter methods in this class for further configuration options.
- Call populate(java.sql.Connection)orexecute(javax.sql.DataSource)to initialize or clean up the database using the configured scripts.
- Since:
- 3.0
- Author:
- Keith Donald, Dave Syer, Juergen Hoeller, Chris Beams, Oliver Gierke, Sam Brannen, Chris Baldwin, Phillip Webb
- See Also:
- 
Constructor SummaryConstructorsConstructorDescriptionConstruct a newResourceDatabasePopulatorwith default settings.ResourceDatabasePopulator(boolean continueOnError, boolean ignoreFailedDrops, String sqlScriptEncoding, Resource... scripts) Construct a newResourceDatabasePopulatorwith the supplied values.ResourceDatabasePopulator(Resource... scripts) Construct a newResourceDatabasePopulatorwith default settings for the supplied scripts.
- 
Method SummaryModifier and TypeMethodDescriptionvoidAdd a script to execute to initialize or clean up the database.voidaddScripts(Resource... scripts) Add multiple scripts to execute to initialize or clean up the database.voidexecute(DataSource dataSource) Execute thisResourceDatabasePopulatoragainst the givenDataSource.voidpopulate(Connection connection) Populate, initialize, or clean up the database using the provided JDBC connection.voidsetBlockCommentEndDelimiter(String blockCommentEndDelimiter) Set the end delimiter that identifies block comments within the SQL scripts.voidsetBlockCommentStartDelimiter(String blockCommentStartDelimiter) Set the start delimiter that identifies block comments within the SQL scripts.voidsetCommentPrefix(String commentPrefix) Set the prefix that identifies single-line comments within the SQL scripts.voidsetCommentPrefixes(String... commentPrefixes) Set the prefixes that identify single-line comments within the SQL scripts.voidsetContinueOnError(boolean continueOnError) Flag to indicate that all failures in SQL should be logged but not cause a failure.voidsetIgnoreFailedDrops(boolean ignoreFailedDrops) Flag to indicate that a failed SQLDROPstatement can be ignored.voidsetScripts(Resource... scripts) Set the scripts to execute to initialize or clean up the database, replacing any previously added scripts.voidsetSeparator(String separator) Specify the statement separator, if a custom one.voidsetSqlScriptEncoding(String sqlScriptEncoding) Specify the encoding for the configured SQL scripts, if different from the platform encoding.
- 
Constructor Details- 
ResourceDatabasePopulatorpublic ResourceDatabasePopulator()Construct a newResourceDatabasePopulatorwith default settings.- Since:
- 4.0.3
 
- 
ResourceDatabasePopulatorConstruct a newResourceDatabasePopulatorwith default settings for the supplied scripts.- Parameters:
- scripts- the scripts to execute to initialize or clean up the database (never- null)
- Since:
- 4.0.3
 
- 
ResourceDatabasePopulatorpublic ResourceDatabasePopulator(boolean continueOnError, boolean ignoreFailedDrops, @Nullable String sqlScriptEncoding, Resource... scripts) Construct a newResourceDatabasePopulatorwith the supplied values.- Parameters:
- continueOnError- flag to indicate that all failures in SQL should be logged but not cause a failure
- ignoreFailedDrops- flag to indicate that a failed SQL- DROPstatement can be ignored
- sqlScriptEncoding- the encoding for the supplied SQL scripts (may be- nullor empty to indicate platform encoding)
- scripts- the scripts to execute to initialize or clean up the database (never- null)
- Since:
- 4.0.3
 
 
- 
- 
Method Details- 
addScriptAdd a script to execute to initialize or clean up the database.- Parameters:
- script- the path to an SQL script (never- null)
 
- 
addScriptsAdd multiple scripts to execute to initialize or clean up the database.- Parameters:
- scripts- the scripts to execute (never- null)
 
- 
setScriptsSet the scripts to execute to initialize or clean up the database, replacing any previously added scripts.- Parameters:
- scripts- the scripts to execute (never- null)
 
- 
setSqlScriptEncodingSpecify the encoding for the configured SQL scripts, if different from the platform encoding.- Parameters:
- sqlScriptEncoding- the encoding used in scripts (may be- nullor empty to indicate platform encoding)
- See Also:
 
- 
setSeparatorSpecify the statement separator, if a custom one.Defaults to ";"if not specified and falls back to"\n"as a last resort; may be set toScriptUtils.EOF_STATEMENT_SEPARATORto signal that each script contains a single statement without a separator.- Parameters:
- separator- the script statement separator
 
- 
setCommentPrefixSet the prefix that identifies single-line comments within the SQL scripts.Defaults to "--".- Parameters:
- commentPrefix- the prefix for single-line comments
- See Also:
 
- 
setCommentPrefixesSet the prefixes that identify single-line comments within the SQL scripts.Defaults to ["--"].- Parameters:
- commentPrefixes- the prefixes for single-line comments
- Since:
- 5.2
 
- 
setBlockCommentStartDelimiterSet the start delimiter that identifies block comments within the SQL scripts.Defaults to "/*".- Parameters:
- blockCommentStartDelimiter- the start delimiter for block comments (never- nullor empty)
- Since:
- 4.0.3
- See Also:
 
- 
setBlockCommentEndDelimiterSet the end delimiter that identifies block comments within the SQL scripts.Defaults to "*/".- Parameters:
- blockCommentEndDelimiter- the end delimiter for block comments (never- nullor empty)
- Since:
- 4.0.3
- See Also:
 
- 
setContinueOnErrorpublic void setContinueOnError(boolean continueOnError) Flag to indicate that all failures in SQL should be logged but not cause a failure.Defaults to false.- Parameters:
- continueOnError-- trueif script execution should continue on error
 
- 
setIgnoreFailedDropspublic void setIgnoreFailedDrops(boolean ignoreFailedDrops) Flag to indicate that a failed SQLDROPstatement can be ignored.This is useful for a non-embedded database whose SQL dialect does not support an IF EXISTSclause in aDROPstatement.The default is falseso that if the populator runs accidentally, it will fail fast if a script starts with aDROPstatement.- Parameters:
- ignoreFailedDrops-- trueif failed drop statements should be ignored
 
- 
populatePopulate, initialize, or clean up the database using the provided JDBC connection.Warning: Concrete implementations should not close the provided Connection.Concrete implementations may throw an SQLExceptionif an error is encountered but are strongly encouraged to throw a specificScriptExceptioninstead. For example, Spring'sResourceDatabasePopulatorandDatabasePopulatorUtilswrap allSQLExceptionsinScriptExceptions.- Specified by:
- populatein interface- DatabasePopulator
- Parameters:
- connection- the JDBC connection to use; already configured and ready to use; never- null
- Throws:
- ScriptException- in all other error cases
- See Also:
 
- 
executeExecute thisResourceDatabasePopulatoragainst the givenDataSource.Delegates to DatabasePopulatorUtils.execute(org.springframework.jdbc.datasource.init.DatabasePopulator, javax.sql.DataSource).- Parameters:
- dataSource- the- DataSourceto execute against (never- null)
- Throws:
- ScriptException- if an error occurs
- Since:
- 4.1
- See Also:
 
 
-