Skip to content

Commit dc6d675

Browse files
committed
Improve configurability of EmbeddedDatabaseBuilder
This commit improves the configurability of EmbeddedDatabaseBuilder by exposing the following new configuration methods. - setDataSourceFactory(DataSourceFactory) - addScripts(String...) - setScriptEncoding(String) - setSeparator(String) - setCommentPrefix(String) - setBlockCommentStartDelimiter(String) - setBlockCommentEndDelimiter(String) - continueOnError(boolean) - ignoreFailedDrops(boolean) If more fine grained control over the configuration of the embedded database is required, users are recommended to use EmbeddedDatabaseFactory with a ResourceDatabasePopulator and forego use of the builder. Issue: SPR-11410
1 parent 34fe252 commit dc6d675

File tree

6 files changed

+285
-55
lines changed

6 files changed

+285
-55
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseBuilder.java

Lines changed: 146 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,28 @@
1616

1717
package org.springframework.jdbc.datasource.embedded;
1818

19+
import javax.sql.DataSource;
20+
1921
import org.springframework.core.io.DefaultResourceLoader;
2022
import org.springframework.core.io.ResourceLoader;
2123
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
24+
import org.springframework.util.Assert;
2225

2326
/**
2427
* A builder that provides a convenient API for constructing an embedded database.
2528
*
2629
* <h3>Usage Example</h3>
2730
* <pre class="code">
28-
* EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
29-
* EmbeddedDatabase db = builder.setType(H2).addScript("schema.sql").addScript("data.sql").build();
31+
* EmbeddedDatabase db = new EmbeddedDatabaseBuilder()
32+
* .setType(H2)
33+
* .setScriptEncoding("UTF-8")
34+
* .ignoreFailedDrops(true)
35+
* .addScript("schema.sql")
36+
* .addScripts("user_data.sql", "country_data.sql")
37+
* .build();
38+
*
39+
* // ...
40+
*
3041
* db.shutdown();
3142
* </pre>
3243
*
@@ -35,6 +46,9 @@
3546
* @author Dave Syer
3647
* @author Sam Brannen
3748
* @since 3.0
49+
* @see org.springframework.jdbc.datasource.init.ScriptUtils
50+
* @see org.springframework.jdbc.datasource.init.ResourceDatabasePopulator
51+
* @see org.springframework.jdbc.datasource.init.DatabasePopulatorUtils
3852
*/
3953
public class EmbeddedDatabaseBuilder {
4054

@@ -46,15 +60,15 @@ public class EmbeddedDatabaseBuilder {
4660

4761

4862
/**
49-
* Create a new embedded database builder.
63+
* Create a new embedded database builder with a {@link DefaultResourceLoader}.
5064
*/
5165
public EmbeddedDatabaseBuilder() {
5266
this(new DefaultResourceLoader());
5367
}
5468

5569
/**
56-
* Create a new embedded database builder with the given ResourceLoader.
57-
* @param resourceLoader the ResourceLoader to delegate to
70+
* Create a new embedded database builder with the given {@link ResourceLoader}.
71+
* @param resourceLoader the {@code ResourceLoader} to delegate to
5872
*/
5973
public EmbeddedDatabaseBuilder(ResourceLoader resourceLoader) {
6074
this.databaseFactory = new EmbeddedDatabaseFactory();
@@ -63,12 +77,11 @@ public EmbeddedDatabaseBuilder(ResourceLoader resourceLoader) {
6377
this.resourceLoader = resourceLoader;
6478
}
6579

66-
6780
/**
6881
* Set the name of the embedded database.
6982
* <p>Defaults to {@link EmbeddedDatabaseFactory#DEFAULT_DATABASE_NAME} if
7083
* not called.
71-
* @param databaseName the database name
84+
* @param databaseName the name of the embedded database to build
7285
* @return {@code this}, to facilitate method chaining
7386
*/
7487
public EmbeddedDatabaseBuilder setName(String databaseName) {
@@ -79,7 +92,7 @@ public EmbeddedDatabaseBuilder setName(String databaseName) {
7992
/**
8093
* Set the type of embedded database.
8194
* <p>Defaults to HSQL if not called.
82-
* @param databaseType the database type
95+
* @param databaseType the type of embedded database to build
8396
* @return {@code this}, to facilitate method chaining
8497
*/
8598
public EmbeddedDatabaseBuilder setType(EmbeddedDatabaseType databaseType) {
@@ -88,24 +101,142 @@ public EmbeddedDatabaseBuilder setType(EmbeddedDatabaseType databaseType) {
88101
}
89102

90103
/**
91-
* Add an SQL script to execute to initialize or populate the database.
92-
* @param sqlResource the sql resource location
104+
* Set the factory to use to create the {@link DataSource} instance that
105+
* connects to the embedded database.
106+
* <p>Defaults to {@link SimpleDriverDataSourceFactory} but can be overridden,
107+
* for example to introduce connection pooling.
93108
* @return {@code this}, to facilitate method chaining
109+
* @since 4.0.3
94110
*/
95-
public EmbeddedDatabaseBuilder addScript(String sqlResource) {
96-
this.databasePopulator.addScript(this.resourceLoader.getResource(sqlResource));
111+
public EmbeddedDatabaseBuilder setDataSourceFactory(DataSourceFactory dataSourceFactory) {
112+
Assert.notNull(dataSourceFactory, "DataSourceFactory is required");
113+
this.databaseFactory.setDataSourceFactory(dataSourceFactory);
97114
return this;
98115
}
99116

100117
/**
101-
* Add default scripts to execute to populate the database.
118+
* Add default SQL scripts to execute to populate the database.
102119
* <p>The default scripts are {@code "schema.sql"} to create the database
103120
* schema and {@code "data.sql"} to populate the database with data.
104121
* @return {@code this}, to facilitate method chaining
105122
*/
106123
public EmbeddedDatabaseBuilder addDefaultScripts() {
107-
addScript("schema.sql");
108-
addScript("data.sql");
124+
return addScripts("schema.sql", "data.sql");
125+
}
126+
127+
/**
128+
* Add an SQL script to execute to initialize or populate the database.
129+
* @param script the script to execute
130+
* @return {@code this}, to facilitate method chaining
131+
*/
132+
public EmbeddedDatabaseBuilder addScript(String script) {
133+
this.databasePopulator.addScript(this.resourceLoader.getResource(script));
134+
return this;
135+
}
136+
137+
/**
138+
* Add multiple SQL scripts to execute to initialize or populate the database.
139+
* @param scripts the scripts to execute
140+
* @return {@code this}, to facilitate method chaining
141+
* @since 4.0.3
142+
*/
143+
public EmbeddedDatabaseBuilder addScripts(String... scripts) {
144+
for (String script : scripts) {
145+
addScript(script);
146+
}
147+
return this;
148+
}
149+
150+
/**
151+
* Specify the character encoding used in all SQL scripts, if different from
152+
* the platform encoding.
153+
* @param scriptEncoding the encoding used in scripts
154+
* @return {@code this}, to facilitate method chaining
155+
* @since 4.0.3
156+
*/
157+
public EmbeddedDatabaseBuilder setScriptEncoding(String scriptEncoding) {
158+
this.databasePopulator.setSqlScriptEncoding(scriptEncoding);
159+
return this;
160+
}
161+
162+
/**
163+
* Specify the statement separator used in all SQL scripts, if a custom one.
164+
* <p>Default is ";".
165+
* @param separator the statement separator
166+
* @return {@code this}, to facilitate method chaining
167+
* @since 4.0.3
168+
*/
169+
public EmbeddedDatabaseBuilder setSeparator(String separator) {
170+
this.databasePopulator.setSeparator(separator);
171+
return this;
172+
}
173+
174+
/**
175+
* Specify the single-line comment prefix used in all SQL scripts.
176+
* <p>Default is "--".
177+
* @param commentPrefix the prefix for single-line comments
178+
* @return {@code this}, to facilitate method chaining
179+
* @since 4.0.3
180+
*/
181+
public EmbeddedDatabaseBuilder setCommentPrefix(String commentPrefix) {
182+
this.databasePopulator.setCommentPrefix(commentPrefix);
183+
return this;
184+
}
185+
186+
/**
187+
* Specify the start delimiter for block comments in all SQL scripts.
188+
* <p>Default is "/*".
189+
* @param blockCommentStartDelimiter the start delimiter for block comments
190+
* @return {@code this}, to facilitate method chaining
191+
* @since 4.0.3
192+
* @see #setBlockCommentEndDelimiter
193+
* @since 4.0.3
194+
*/
195+
public EmbeddedDatabaseBuilder setBlockCommentStartDelimiter(String blockCommentStartDelimiter) {
196+
this.databasePopulator.setBlockCommentStartDelimiter(blockCommentStartDelimiter);
197+
return this;
198+
}
199+
200+
/**
201+
* Specify the end delimiter for block comments in all SQL scripts.
202+
* <p>Default is "*&#47;".
203+
* @param blockCommentEndDelimiter the end delimiter for block comments
204+
* @return {@code this}, to facilitate method chaining
205+
* @since 4.0.3
206+
* @see #setBlockCommentStartDelimiter
207+
* @since 4.0.3
208+
*/
209+
public EmbeddedDatabaseBuilder setBlockCommentEndDelimiter(String blockCommentEndDelimiter) {
210+
this.databasePopulator.setBlockCommentEndDelimiter(blockCommentEndDelimiter);
211+
return this;
212+
}
213+
214+
/**
215+
* Specify that all failures which occur while executing SQL scripts should
216+
* be logged but should not cause a failure.
217+
* <p>Defaults to {@code false}.
218+
* @param flag {@code true} if script execution should continue on error
219+
* @return {@code this}, to facilitate method chaining
220+
* @since 4.0.3
221+
*/
222+
public EmbeddedDatabaseBuilder continueOnError(boolean flag) {
223+
this.databasePopulator.setContinueOnError(flag);
224+
return this;
225+
}
226+
227+
/**
228+
* Specify that a failed SQL {@code DROP} statement within an executed
229+
* script can be ignored.
230+
* <p>This is useful for a database whose SQL dialect does not support an
231+
* {@code IF EXISTS} clause in a {@code DROP} statement.
232+
* <p>The default is {@code false} so that {@link #build building} will fail
233+
* fast if a script starts with a {@code DROP} statement.
234+
* @param flag {@code true} if failed drop statements should be ignored
235+
* @return {@code this}, to facilitate method chaining
236+
* @since 4.0.3
237+
*/
238+
public EmbeddedDatabaseBuilder ignoreFailedDrops(boolean flag) {
239+
this.databasePopulator.setIgnoreFailedDrops(flag);
109240
return this;
110241
}
111242

0 commit comments

Comments
 (0)