Polish EmbeddedDatabaseBuilder

Minor improvements made during the triage of SPR-8817
This commit is contained in:
Chris Beams
2011-11-13 01:38:47 +00:00
parent 4ededaf11c
commit 7b491370a3
2 changed files with 14 additions and 10 deletions

View File

@@ -21,7 +21,7 @@ import org.springframework.core.io.ResourceLoader;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
/**
* A builder that provides a fluent API for constructing an embedded database.
* A builder that provides a convenient API for constructing an embedded database.
*
* <p>Usage example:
* <pre>
@@ -65,7 +65,7 @@ public class EmbeddedDatabaseBuilder {
* Sets the name of the embedded database
* Defaults to 'testdb' if not called.
* @param databaseName the database name
* @return this, for fluent call chaining
* @return this, to facilitate method chaining
*/
public EmbeddedDatabaseBuilder setName(String databaseName) {
this.databaseFactory.setDatabaseName(databaseName);
@@ -76,7 +76,7 @@ public class EmbeddedDatabaseBuilder {
* Sets the type of embedded database.
* Defaults to HSQL if not called.
* @param databaseType the database type
* @return this, for fluent call chaining
* @return this, to facilitate method chaining
*/
public EmbeddedDatabaseBuilder setType(EmbeddedDatabaseType databaseType) {
this.databaseFactory.setDatabaseType(databaseType);
@@ -86,7 +86,7 @@ public class EmbeddedDatabaseBuilder {
/**
* Adds a SQL script to execute to populate the database.
* @param sqlResource the sql resource location
* @return this, for fluent call chaining
* @return this, to facilitate method chaining
*/
public EmbeddedDatabaseBuilder addScript(String sqlResource) {
this.databasePopulator.addScript(this.resourceLoader.getResource(sqlResource));
@@ -95,8 +95,9 @@ public class EmbeddedDatabaseBuilder {
/**
* Add default scripts to execute to populate the database.
* The default scripts are <code>schema.sql</code> to create the db schema and <code>data.sql</code> to populate the db with data.
* @return this, for fluent call chaining
* The default scripts are <code>schema.sql</code> to create the db schema and
* <code>data.sql</code> to populate the db with data.
* @return this, to facilitate method chaining
*/
public EmbeddedDatabaseBuilder addDefaultScripts() {
addScript("schema.sql");

View File

@@ -42,12 +42,15 @@ import org.springframework.util.StringUtils;
* @author Keith Donald
* @author Dave Syer
* @author Juergen Hoeller
* @author Chris Beams
* @since 3.0
*/
public class ResourceDatabasePopulator implements DatabasePopulator {
private static String DEFAULT_COMMENT_PREFIX = "--";
private static String DEFAULT_STATEMENT_SEPARATOR = ";";
private static final Log logger = LogFactory.getLog(ResourceDatabasePopulator.class);
@@ -91,7 +94,7 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
}
/**
* Specify the statement separator, if a custom one.
* Specify the statement separator, if a custom one. Default is ";".
*/
public void setSeparator(String separator) {
this.separator = separator;
@@ -141,8 +144,8 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
/**
* Execute the given SQL script.
* <p>The script will normally be loaded by classpath. There should be one statement per line.
* Any semicolons will be removed.
* <p>The script will normally be loaded by classpath. There should be one statement
* per line. Any {@link #setSeparator(String) statement separators} will be removed.
* <p><b>Do not use this method to execute DDL if you expect rollback.</b>
* @param connection the JDBC Connection with which to perform JDBC operations
* @param resource the resource (potentially associated with a specific encoding) to load the SQL script from
@@ -166,7 +169,7 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
}
String delimiter = this.separator;
if (delimiter == null) {
delimiter = ";";
delimiter = DEFAULT_STATEMENT_SEPARATOR;
if (!containsSqlScriptDelimiters(script, delimiter)) {
delimiter = "\n";
}