From 6771a229886825911e8b63bb6a1275cdc409f3a0 Mon Sep 17 00:00:00 2001 From: Keith Donald Date: Wed, 6 May 2009 08:38:47 +0000 Subject: [PATCH] polish --- .../embedded/ResourceDatabasePopulator.java | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/ResourceDatabasePopulator.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/ResourceDatabasePopulator.java index fe9659d09d6..3b29cdd56b5 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/ResourceDatabasePopulator.java +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/ResourceDatabasePopulator.java @@ -31,22 +31,19 @@ import org.springframework.core.io.support.EncodedResource; import org.springframework.util.StringUtils; /** - * Populates a database from schema and test-data SQL defined in external resources. By default, looks for a schema.sql - * file and test-data.sql resource in the root of the classpath. + * Populates a database from SQL scripts defined in external resources. *

- * May be configured:
- * Call {@link #setSchemaLocation(Resource)} to configure the location of the database schema file.
- * Call {@link #setTestDataLocation(Resource)} to configure the location of the test data file.
- * Call {@link #setSqlScriptEncoding(String)} to set the encoding for the schema and test data SQL.
+ * Call {@link #addScript(Resource)} to add a SQL script location.
+ * Call {@link #setSqlScriptEncoding(String)} to set the encoding for all added scripts.
*/ public class ResourceDatabasePopulator implements DatabasePopulator { private static final Log logger = LogFactory.getLog(ResourceDatabasePopulator.class); - private String sqlScriptEncoding; - private List scripts = new ArrayList(); + private String sqlScriptEncoding; + /** * Add a script to execute to populate the database. * @param script the path to a SQL script @@ -57,6 +54,8 @@ public class ResourceDatabasePopulator implements DatabasePopulator { /** * Specify the encoding for SQL scripts, if different from the platform encoding. + * Note setting this property has no effect on added scripts that are already {@link EncodedResource encoded resources}. + * @see #addScript(Resource) */ public void setSqlScriptEncoding(String sqlScriptEncoding) { this.sqlScriptEncoding = sqlScriptEncoding; @@ -64,10 +63,18 @@ public class ResourceDatabasePopulator implements DatabasePopulator { public void populate(Connection connection) throws SQLException { for (Resource script : scripts) { - executeSqlScript(connection, new EncodedResource(script, sqlScriptEncoding), false); + executeSqlScript(connection, applyEncodingIfNecessary(script), false); } } + private EncodedResource applyEncodingIfNecessary(Resource script) { + if (script instanceof EncodedResource) { + return (EncodedResource) script; + } else { + return new EncodedResource(script, sqlScriptEncoding); + } + } + /** * Execute the given SQL script.

The script will normally be loaded by classpath. There should be one statement * per line. Any semicolons will be removed. Do not use this method to execute DDL if you expect rollback.