Class ResourceDatabasePopulator
java.lang.Object
org.springframework.r2dbc.connection.init.ResourceDatabasePopulator
- All Implemented Interfaces:
DatabasePopulator
Populates, initializes, or cleans up a database using SQL
scripts defined in external resources.
- Call
addScript(Resource)to add a single SQL script location. - Call
addScripts(Resource...)to add multiple SQL script locations. - Consult the setter methods in this class for further configuration options.
- Call
populate(Connection)to initialize or clean up the database using the configured scripts.
- Since:
- 5.3
- Author:
- Keith Donald, Dave Syer, Juergen Hoeller, Chris Beams, Oliver Gierke, Sam Brannen, Chris Baldwin, Phillip Webb, Mark Paluch
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreate a newResourceDatabasePopulatorwith default settings.ResourceDatabasePopulator(boolean continueOnError, boolean ignoreFailedDrops, @Nullable String sqlScriptEncoding, Resource... scripts) Construct a newResourceDatabasePopulatorwith the supplied values.ResourceDatabasePopulator(Resource... scripts) Create a newResourceDatabasePopulatorwith default settings for the supplied scripts. -
Method Summary
Modifier 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.reactor.core.publisher.Mono<Void> populate(Connection connection) Populate, initialize, or clean up the database using the provided R2DBCConnection.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.voidsetDataBufferFactory(DataBufferFactory dataBufferFactory) Set theDataBufferFactoryto use forResourceloading.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(@Nullable String sqlScriptEncoding) Specify the encoding for the configured SQL scripts, if different from the platform encoding.voidsetSqlScriptEncoding(@Nullable Charset sqlScriptEncoding) Specify the encoding for the configured SQL scripts, if different from the platform encoding.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface DatabasePopulator
populate
-
Constructor Details
-
ResourceDatabasePopulator
public ResourceDatabasePopulator()Create a newResourceDatabasePopulatorwith default settings. -
ResourceDatabasePopulator
Create a newResourceDatabasePopulatorwith default settings for the supplied scripts.- Parameters:
scripts- the scripts to execute to initialize or clean up the database (nevernull)
-
ResourceDatabasePopulator
public 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 failureignoreFailedDrops- flag to indicate that a failed SQLDROPstatement can be ignoredsqlScriptEncoding- the encoding for the supplied SQL scripts (may benullor empty to indicate platform encoding)scripts- the scripts to execute to initialize or clean up the database (nevernull)
-
-
Method Details
-
addScript
Add a script to execute to initialize or clean up the database.- Parameters:
script- the path to an SQL script (nevernull)
-
addScripts
Add multiple scripts to execute to initialize or clean up the database.- Parameters:
scripts- the scripts to execute (nevernull)
-
setScripts
Set the scripts to execute to initialize or clean up the database, replacing any previously added scripts.- Parameters:
scripts- the scripts to execute (nevernull)
-
setSqlScriptEncoding
-
setSqlScriptEncoding
-
setSeparator
Specify 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
-
setCommentPrefix
Set the prefix that identifies single-line comments within the SQL scripts.Defaults to
"--".- Parameters:
commentPrefix- the prefix for single-line comments- See Also:
-
setCommentPrefixes
Set the prefixes that identify single-line comments within the SQL scripts.Defaults to
["--"].- Parameters:
commentPrefixes- the prefixes for single-line comments
-
setBlockCommentStartDelimiter
Set the start delimiter that identifies block comments within the SQL scripts.Defaults to
"/*".- Parameters:
blockCommentStartDelimiter- the start delimiter for block comments (nevernullor empty)- See Also:
-
setBlockCommentEndDelimiter
Set the end delimiter that identifies block comments within the SQL scripts.Defaults to
"*/".- Parameters:
blockCommentEndDelimiter- the end delimiter for block comments (nevernullor empty)- See Also:
-
setContinueOnError
public 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
-
setIgnoreFailedDrops
public 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
-
setDataBufferFactory
- Parameters:
dataBufferFactory- theDataBufferFactoryto use, must not benull
-
populate
Description copied from interface:DatabasePopulatorPopulate, initialize, or clean up the database using the provided R2DBCConnection.- Specified by:
populatein interfaceDatabasePopulator- Parameters:
connection- the R2DBC connection to use to populate the db; already configured and ready to use, must not benull- Returns:
Monothat initiates script execution and is notified upon completion
-