Skip to content

Commit 5d049e0

Browse files
committed
Introduce execute(DataSource) in ResrcDbPopulator
To simplify common use cases, this commit introduces a new execute(DataSource) method in ResourceDatabasePopulator that complements the existing populate(Connection) method. Issue: SPR-11629
1 parent b766686 commit 5d049e0

File tree

4 files changed

+30
-28
lines changed

4 files changed

+30
-28
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulator.java

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,28 @@
1616

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

19+
1920
import java.sql.Connection;
2021
import java.util.ArrayList;
2122
import java.util.Arrays;
2223
import java.util.List;
2324

25+
import javax.sql.DataSource;
26+
2427
import org.springframework.core.io.Resource;
2528
import org.springframework.core.io.support.EncodedResource;
2629

2730
/**
28-
* Populates or initializes a database from SQL scripts defined in external
29-
* resources.
31+
* Populates, initializes, or cleans up a database using SQL scripts defined in
32+
* external resources.
3033
*
31-
* <p>Call {@link #addScript(Resource)} to add a single SQL script location.
32-
* Call {@link #addScripts(Resource...)} to add multiple SQL script locations.
33-
* Call {@link #setSqlScriptEncoding(String)} to set the encoding for all added
34-
* scripts.
34+
* <ul>
35+
* <li>Call {@link #addScript} to add a single SQL script location.
36+
* <li>Call {@link #addScripts} to add multiple SQL script locations.
37+
* <li>Consult the setter methods in this class for further configuration options.
38+
* <li>Call {@link #populate} or {@link #execute} to initialize or clean up the
39+
* database using the configured scripts.
40+
* </ul>
3541
*
3642
* @author Keith Donald
3743
* @author Dave Syer
@@ -199,6 +205,7 @@ public void setIgnoreFailedDrops(boolean ignoreFailedDrops) {
199205

200206
/**
201207
* {@inheritDoc}
208+
* @see #execute(DataSource)
202209
*/
203210
@Override
204211
public void populate(Connection connection) throws ScriptException {
@@ -210,8 +217,20 @@ public void populate(Connection connection) throws ScriptException {
210217
}
211218

212219
/**
213-
* {@link EncodedResource} is not a sub-type of {@link Resource}. Thus we
214-
* always need to wrap each script resource in an encoded resource.
220+
* Execute this {@code DatabasePopulator} against the given {@link DataSource}.
221+
* <p>Delegates to {@link DatabasePopulatorUtils#execute}.
222+
* @param dataSource the {@code DataSource} to execute against
223+
* @throws ScriptException if an error occurs
224+
* @since 4.1
225+
* @see #populate(Connection)
226+
*/
227+
public void execute(DataSource dataSource) throws ScriptException {
228+
DatabasePopulatorUtils.execute(this, dataSource);
229+
}
230+
231+
/**
232+
* {@link EncodedResource} is not a sub-type of {@link Resource}. Thus we always need
233+
* to wrap each script resource in an encoded resource.
215234
*/
216235
private EncodedResource encodeScript(Resource script) {
217236
return new EncodedResource(script, this.sqlScriptEncoding);

spring-test/src/main/java/org/springframework/test/context/junit4/AbstractTransactionalJUnit4SpringContextTests.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
import org.springframework.core.io.Resource;
2424
import org.springframework.dao.DataAccessException;
2525
import org.springframework.jdbc.core.JdbcTemplate;
26-
import org.springframework.jdbc.datasource.init.DatabasePopulator;
27-
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
2826
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
2927
import org.springframework.test.context.ContextConfiguration;
3028
import org.springframework.test.context.TestExecutionListeners;
@@ -180,14 +178,11 @@ protected void dropTables(String... names) {
180178
* exception in the event of an error
181179
* @throws DataAccessException if there is an error executing a statement
182180
* @see ResourceDatabasePopulator
183-
* @see DatabasePopulatorUtils
184181
* @see #setSqlScriptEncoding
185182
*/
186183
protected void executeSqlScript(String sqlResourcePath, boolean continueOnError) throws DataAccessException {
187184
Resource resource = this.applicationContext.getResource(sqlResourcePath);
188-
DatabasePopulator databasePopulator = new ResourceDatabasePopulator(continueOnError, false,
189-
this.sqlScriptEncoding, resource);
190-
DatabasePopulatorUtils.execute(databasePopulator, jdbcTemplate.getDataSource());
185+
new ResourceDatabasePopulator(continueOnError, false, this.sqlScriptEncoding, resource).execute(jdbcTemplate.getDataSource());
191186
}
192187

193188
}

spring-test/src/main/java/org/springframework/test/context/testng/AbstractTransactionalTestNGSpringContextTests.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
import org.springframework.core.io.Resource;
2424
import org.springframework.dao.DataAccessException;
2525
import org.springframework.jdbc.core.JdbcTemplate;
26-
import org.springframework.jdbc.datasource.init.DatabasePopulator;
27-
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
2826
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
2927
import org.springframework.test.context.TestExecutionListeners;
3028
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
@@ -171,14 +169,11 @@ protected void dropTables(String... names) {
171169
* exception in the event of an error
172170
* @throws DataAccessException if there is an error executing a statement
173171
* @see ResourceDatabasePopulator
174-
* @see DatabasePopulatorUtils
175172
* @see #setSqlScriptEncoding
176173
*/
177174
protected void executeSqlScript(String sqlResourcePath, boolean continueOnError) throws DataAccessException {
178175
Resource resource = this.applicationContext.getResource(sqlResourcePath);
179-
DatabasePopulator databasePopulator = new ResourceDatabasePopulator(continueOnError, false,
180-
this.sqlScriptEncoding, resource);
181-
DatabasePopulatorUtils.execute(databasePopulator, jdbcTemplate.getDataSource());
176+
new ResourceDatabasePopulator(continueOnError, false, this.sqlScriptEncoding, resource).execute(jdbcTemplate.getDataSource());
182177
}
183178

184179
}

spring-test/src/main/java/org/springframework/test/jdbc/JdbcTestUtils.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
import org.springframework.dao.DataAccessException;
3030
import org.springframework.jdbc.core.JdbcTemplate;
3131
import org.springframework.jdbc.core.SqlParameterValue;
32-
import org.springframework.jdbc.datasource.init.DatabasePopulator;
33-
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
3432
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
3533
import org.springframework.jdbc.datasource.init.ScriptUtils;
3634
import org.springframework.util.StringUtils;
@@ -164,7 +162,6 @@ public static void dropTables(JdbcTemplate jdbcTemplate, String... tableNames) {
164162
* @throws DataAccessException if there is an error executing a statement
165163
* and {@code continueOnError} is {@code false}
166164
* @see ResourceDatabasePopulator
167-
* @see DatabasePopulatorUtils
168165
* @see #executeSqlScript(JdbcTemplate, Resource, boolean)
169166
* @deprecated as of Spring 4.0.3, in favor of using
170167
* {@link org.springframework.jdbc.datasource.init.ScriptUtils#executeSqlScript}
@@ -192,7 +189,6 @@ public static void executeSqlScript(JdbcTemplate jdbcTemplate, ResourceLoader re
192189
* @throws DataAccessException if there is an error executing a statement
193190
* and {@code continueOnError} is {@code false}
194191
* @see ResourceDatabasePopulator
195-
* @see DatabasePopulatorUtils
196192
* @see #executeSqlScript(JdbcTemplate, EncodedResource, boolean)
197193
* @deprecated as of Spring 4.0.3, in favor of using
198194
* {@link org.springframework.jdbc.datasource.init.ScriptUtils#executeSqlScript}
@@ -217,17 +213,14 @@ public static void executeSqlScript(JdbcTemplate jdbcTemplate, Resource resource
217213
* @throws DataAccessException if there is an error executing a statement
218214
* and {@code continueOnError} is {@code false}
219215
* @see ResourceDatabasePopulator
220-
* @see DatabasePopulatorUtils
221216
* @deprecated as of Spring 4.0.3, in favor of using
222217
* {@link org.springframework.jdbc.datasource.init.ScriptUtils#executeSqlScript}
223218
* or {@link org.springframework.jdbc.datasource.init.ResourceDatabasePopulator}.
224219
*/
225220
@Deprecated
226221
public static void executeSqlScript(JdbcTemplate jdbcTemplate, EncodedResource resource, boolean continueOnError)
227222
throws DataAccessException {
228-
DatabasePopulator databasePopulator = new ResourceDatabasePopulator(continueOnError, false,
229-
resource.getEncoding(), resource.getResource());
230-
DatabasePopulatorUtils.execute(databasePopulator, jdbcTemplate.getDataSource());
223+
new ResourceDatabasePopulator(continueOnError, false, resource.getEncoding(), resource.getResource()).execute(jdbcTemplate.getDataSource());
231224
}
232225

233226
/**

0 commit comments

Comments
 (0)