public class EmbeddedDatabaseBuilder extends Object
EmbeddedDatabase db = new EmbeddedDatabaseBuilder() .generateUniqueName(true) .setType(H2) .setScriptEncoding("UTF-8") .ignoreFailedDrops(true) .addScript("schema.sql") .addScripts("user_data.sql", "country_data.sql") .build(); // perform actions against the db (EmbeddedDatabase extends javax.sql.DataSource) db.shutdown();
ScriptUtils
,
ResourceDatabasePopulator
,
DatabasePopulatorUtils
Constructor and Description |
---|
EmbeddedDatabaseBuilder()
Create a new embedded database builder with a
DefaultResourceLoader . |
EmbeddedDatabaseBuilder(ResourceLoader resourceLoader)
Create a new embedded database builder with the given
ResourceLoader . |
Modifier and Type | Method and Description |
---|---|
EmbeddedDatabaseBuilder |
addDefaultScripts()
Add default SQL scripts to execute to populate the database.
|
EmbeddedDatabaseBuilder |
addScript(String script)
Add an SQL script to execute to initialize or populate the database.
|
EmbeddedDatabaseBuilder |
addScripts(String... scripts)
Add multiple SQL scripts to execute to initialize or populate the database.
|
EmbeddedDatabase |
build()
Build the embedded database.
|
EmbeddedDatabaseBuilder |
continueOnError(boolean flag)
Specify that all failures which occur while executing SQL scripts should
be logged but should not cause a failure.
|
EmbeddedDatabaseBuilder |
generateUniqueName(boolean flag)
Specify whether a unique ID should be generated and used as the database name.
|
EmbeddedDatabaseBuilder |
ignoreFailedDrops(boolean flag)
Specify that a failed SQL
DROP statement within an executed
script can be ignored. |
EmbeddedDatabaseBuilder |
setBlockCommentEndDelimiter(String blockCommentEndDelimiter)
Specify the end delimiter for block comments in all SQL scripts.
|
EmbeddedDatabaseBuilder |
setBlockCommentStartDelimiter(String blockCommentStartDelimiter)
Specify the start delimiter for block comments in all SQL scripts.
|
EmbeddedDatabaseBuilder |
setCommentPrefix(String commentPrefix)
Specify the single-line comment prefix used in all SQL scripts.
|
EmbeddedDatabaseBuilder |
setDataSourceFactory(DataSourceFactory dataSourceFactory)
Set the factory to use to create the
DataSource instance that
connects to the embedded database. |
EmbeddedDatabaseBuilder |
setName(String databaseName)
Set the name of the embedded database.
|
EmbeddedDatabaseBuilder |
setScriptEncoding(String scriptEncoding)
Specify the character encoding used in all SQL scripts, if different from
the platform encoding.
|
EmbeddedDatabaseBuilder |
setSeparator(String separator)
Specify the statement separator used in all SQL scripts, if a custom one.
|
EmbeddedDatabaseBuilder |
setType(EmbeddedDatabaseType databaseType)
Set the type of embedded database.
|
public EmbeddedDatabaseBuilder()
DefaultResourceLoader
.public EmbeddedDatabaseBuilder(ResourceLoader resourceLoader)
ResourceLoader
.resourceLoader
- the ResourceLoader
to delegate topublic EmbeddedDatabaseBuilder generateUniqueName(boolean flag)
If the configuration for this builder is reused across multiple
application contexts within a single JVM, this flag should be enabled
(i.e., set to true
) in order to ensure that each application context
gets its own embedded database.
Enabling this flag overrides any explicit name set via setName(java.lang.String)
.
flag
- true
if a unique database name should be generatedthis
, to facilitate method chainingsetName(java.lang.String)
public EmbeddedDatabaseBuilder setName(String databaseName)
Defaults to EmbeddedDatabaseFactory.DEFAULT_DATABASE_NAME
if
not called.
Will be overridden if the generateUniqueName
flag has been
set to true
.
databaseName
- the name of the embedded database to buildthis
, to facilitate method chaininggenerateUniqueName(boolean)
public EmbeddedDatabaseBuilder setType(EmbeddedDatabaseType databaseType)
Defaults to HSQL if not called.
databaseType
- the type of embedded database to buildthis
, to facilitate method chainingpublic EmbeddedDatabaseBuilder setDataSourceFactory(DataSourceFactory dataSourceFactory)
DataSource
instance that
connects to the embedded database.
Defaults to SimpleDriverDataSourceFactory
but can be overridden,
for example to introduce connection pooling.
this
, to facilitate method chainingpublic EmbeddedDatabaseBuilder addDefaultScripts()
The default scripts are "schema.sql"
to create the database
schema and "data.sql"
to populate the database with data.
this
, to facilitate method chainingpublic EmbeddedDatabaseBuilder addScript(String script)
script
- the script to executethis
, to facilitate method chainingpublic EmbeddedDatabaseBuilder addScripts(String... scripts)
scripts
- the scripts to executethis
, to facilitate method chainingpublic EmbeddedDatabaseBuilder setScriptEncoding(String scriptEncoding)
scriptEncoding
- the encoding used in scriptsthis
, to facilitate method chainingpublic EmbeddedDatabaseBuilder 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 statement separatorthis
, to facilitate method chainingpublic EmbeddedDatabaseBuilder setCommentPrefix(String commentPrefix)
Defaults to "--"
.
commentPrefix
- the prefix for single-line commentsthis
, to facilitate method chainingpublic EmbeddedDatabaseBuilder setBlockCommentStartDelimiter(String blockCommentStartDelimiter)
Defaults to "/*"
.
blockCommentStartDelimiter
- the start delimiter for block commentsthis
, to facilitate method chainingsetBlockCommentEndDelimiter(java.lang.String)
public EmbeddedDatabaseBuilder setBlockCommentEndDelimiter(String blockCommentEndDelimiter)
Defaults to "*/"
.
blockCommentEndDelimiter
- the end delimiter for block commentsthis
, to facilitate method chainingsetBlockCommentStartDelimiter(java.lang.String)
public EmbeddedDatabaseBuilder continueOnError(boolean flag)
Defaults to false
.
flag
- true
if script execution should continue on errorthis
, to facilitate method chainingpublic EmbeddedDatabaseBuilder ignoreFailedDrops(boolean flag)
DROP
statement within an executed
script can be ignored.
This is useful for a database whose SQL dialect does not support an
IF EXISTS
clause in a DROP
statement.
The default is false
so that building
will fail
fast if a script starts with a DROP
statement.
flag
- true
if failed drop statements should be ignoredthis
, to facilitate method chainingpublic EmbeddedDatabase build()