Skip to content

Commit 6771a22

Browse files
author
Keith Donald
committed
polish
1 parent c25760f commit 6771a22

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/ResourceDatabasePopulator.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,19 @@
3131
import org.springframework.util.StringUtils;
3232

3333
/**
34-
* Populates a database from schema and test-data SQL defined in external resources. By default, looks for a schema.sql
35-
* file and test-data.sql resource in the root of the classpath.
34+
* Populates a database from SQL scripts defined in external resources.
3635
* <p>
37-
* May be configured:<br>
38-
* Call {@link #setSchemaLocation(Resource)} to configure the location of the database schema file.<br>
39-
* Call {@link #setTestDataLocation(Resource)} to configure the location of the test data file.<br>
40-
* Call {@link #setSqlScriptEncoding(String)} to set the encoding for the schema and test data SQL.<br>
36+
* Call {@link #addScript(Resource)} to add a SQL script location.<br>
37+
* Call {@link #setSqlScriptEncoding(String)} to set the encoding for all added scripts.<br>
4138
*/
4239
public class ResourceDatabasePopulator implements DatabasePopulator {
4340

4441
private static final Log logger = LogFactory.getLog(ResourceDatabasePopulator.class);
4542

46-
private String sqlScriptEncoding;
47-
4843
private List<Resource> scripts = new ArrayList<Resource>();
4944

45+
private String sqlScriptEncoding;
46+
5047
/**
5148
* Add a script to execute to populate the database.
5249
* @param script the path to a SQL script
@@ -57,17 +54,27 @@ public void addScript(Resource script) {
5754

5855
/**
5956
* Specify the encoding for SQL scripts, if different from the platform encoding.
57+
* Note setting this property has no effect on added scripts that are already {@link EncodedResource encoded resources}.
58+
* @see #addScript(Resource)
6059
*/
6160
public void setSqlScriptEncoding(String sqlScriptEncoding) {
6261
this.sqlScriptEncoding = sqlScriptEncoding;
6362
}
6463

6564
public void populate(Connection connection) throws SQLException {
6665
for (Resource script : scripts) {
67-
executeSqlScript(connection, new EncodedResource(script, sqlScriptEncoding), false);
66+
executeSqlScript(connection, applyEncodingIfNecessary(script), false);
6867
}
6968
}
7069

70+
private EncodedResource applyEncodingIfNecessary(Resource script) {
71+
if (script instanceof EncodedResource) {
72+
return (EncodedResource) script;
73+
} else {
74+
return new EncodedResource(script, sqlScriptEncoding);
75+
}
76+
}
77+
7178
/**
7279
* Execute the given SQL script. <p>The script will normally be loaded by classpath. There should be one statement
7380
* per line. Any semicolons will be removed. <b>Do not use this method to execute DDL if you expect rollback.</b>

0 commit comments

Comments
 (0)