SPR-7364: added separator property to database populator to deal with things like PL/SQL

This commit is contained in:
David Syer 2011-06-02 16:22:26 +00:00
parent ac735d73ac
commit 14edc9fc03
21 changed files with 288 additions and 72 deletions

View File

@ -1,22 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="src" path="src/main/resources"/>
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/>
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
<classpathentry kind="src" output="target/test-classes" path="src/test/resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="var" path="IVY_CACHE/org.apache.commons/com.springsource.org.apache.commons.logging/1.1.1/com.springsource.org.apache.commons.logging-1.1.1.jar" sourcepath="/IVY_CACHE/org.apache.commons/com.springsource.org.apache.commons.logging/1.1.1/com.springsource.org.apache.commons.logging-sources-1.1.1.jar"/>
<classpathentry combineaccessrules="false" kind="src" path="/org.springframework.aop"/>
<classpathentry combineaccessrules="false" kind="src" path="/org.springframework.beans"/>
<classpathentry combineaccessrules="false" kind="src" path="/org.springframework.context"/>
<classpathentry combineaccessrules="false" kind="src" path="/org.springframework.core"/>
<classpathentry combineaccessrules="false" kind="src" path="/org.springframework.transaction"/>
<classpathentry kind="var" path="IVY_CACHE/javax.transaction/com.springsource.javax.transaction/1.1.0/com.springsource.javax.transaction-1.1.0.jar" sourcepath="/IVY_CACHE/javax.transaction/com.springsource.javax.transaction/1.1.0/com.springsource.javax.transaction-sources-1.1.0.jar"/>
<classpathentry kind="var" path="IVY_CACHE/com.mchange.c3p0/com.springsource.com.mchange.v2.c3p0/0.9.1.2/com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar" sourcepath="/IVY_CACHE/com.mchange.c3p0/com.springsource.com.mchange.v2.c3p0/0.9.1.2/com.springsource.com.mchange.v2.c3p0-sources-0.9.1.2.jar"/>
<classpathentry kind="var" path="IVY_CACHE/org.junit/com.springsource.org.junit/4.8.1/com.springsource.org.junit-4.8.1.jar" sourcepath="/IVY_CACHE/org.junit/com.springsource.org.junit/4.8.1/com.springsource.org.junit-sources-4.8.1.jar"/>
<classpathentry kind="var" path="IVY_CACHE/org.easymock/com.springsource.org.easymock/2.5.1/com.springsource.org.easymock-2.5.1.jar" sourcepath="/IVY_CACHE/org.easymock/com.springsource.org.easymock/2.5.1/com.springsource.org.easymock-sources-2.5.1.jar"/>
<classpathentry kind="var" path="IVY_CACHE/org.hsqldb/com.springsource.org.hsqldb/1.8.0.9/com.springsource.org.hsqldb-1.8.0.9.jar" sourcepath="/IVY_CACHE/org.hsqldb/com.springsource.org.hsqldb/1.8.0.9/com.springsource.org.hsqldb-sources-1.8.0.9.jar"/>
<classpathentry kind="var" path="IVY_CACHE/com.h2database/com.springsource.org.h2/1.0.71/com.springsource.org.h2-1.0.71.jar" sourcepath="/IVY_CACHE/com.h2database/com.springsource.org.h2/1.0.71/com.springsource.org.h2-sources-1.0.71.jar"/>
<classpathentry kind="var" path="IVY_CACHE/org.apache.derby/com.springsource.org.apache.derby/10.5.1000001.764942/com.springsource.org.apache.derby-10.5.1000001.764942.jar"/>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@ -10,8 +10,14 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.maven.ide.eclipse.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.maven.ide.eclipse.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@ -1,7 +1,7 @@
#Wed Jul 15 00:01:31 PDT 2009
#Thu Jun 02 17:16:32 BST 2011
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.5
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
@ -9,6 +9,7 @@ org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.5
org.eclipse.jdt.core.formatter.align_type_members_on_columns=false
org.eclipse.jdt.core.formatter.alignment_for_arguments_in_allocation_expression=16

View File

@ -16,16 +16,20 @@
package org.springframework.jdbc.config;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.BeanMetadataElement;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.jdbc.datasource.init.CompositeDatabasePopulator;
import org.springframework.jdbc.datasource.init.DataSourceInitializer;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.util.StringUtils;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;
@ -60,21 +64,36 @@ public class InitializeDatabaseBeanDefinitionParser extends AbstractBeanDefiniti
}
private BeanDefinition createDatabasePopulator(Element element, List<Element> scripts, ParserContext context) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(ResourceDatabasePopulator.class);
builder.addPropertyValue("ignoreFailedDrops", element.getAttribute("ignore-failures").equals("DROPS"));
builder.addPropertyValue("continueOnError", element.getAttribute("ignore-failures").equals("ALL"));
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(CompositeDatabasePopulator.class);
boolean ignoreFailedDrops = element.getAttribute("ignore-failures").equals("DROPS");
boolean continueOnError = element.getAttribute("ignore-failures").equals("ALL");
ManagedList<BeanMetadataElement> delegates = new ManagedList<BeanMetadataElement>();
List<String> locations = new ArrayList<String>();
for (Element scriptElement : scripts) {
String location = scriptElement.getAttribute("location");
locations.add(location);
}
// Use a factory bean for the resources so they can be given an order if a pattern is used
BeanDefinitionBuilder resourcesFactory = BeanDefinitionBuilder
.genericBeanDefinition(SortedResourcesFactoryBean.class);
resourcesFactory.addConstructorArgValue(locations);
builder.addPropertyValue("scripts", resourcesFactory.getBeanDefinition());
BeanDefinitionBuilder delegate = BeanDefinitionBuilder.genericBeanDefinition(ResourceDatabasePopulator.class);
delegate.addPropertyValue("ignoreFailedDrops", ignoreFailedDrops);
delegate.addPropertyValue("continueOnError", continueOnError);
List<String> locations = Arrays.asList(scriptElement.getAttribute("location"));
// Use a factory bean for the resources so they can be given an order if a pattern is used
BeanDefinitionBuilder resourcesFactory = BeanDefinitionBuilder
.genericBeanDefinition(SortedResourcesFactoryBean.class);
resourcesFactory.addConstructorArgValue(locations);
delegate.addPropertyValue("scripts", resourcesFactory.getBeanDefinition());
if (StringUtils.hasLength(scriptElement.getAttribute("separator"))) {
delegate.addPropertyValue("separator", scriptElement.getAttribute("separator"));
}
delegates.add(delegate.getBeanDefinition());
}
builder.addPropertyValue("populators", delegates);
return builder.getBeanDefinition();
}

View File

@ -23,7 +23,6 @@ import java.sql.Types;
import org.springframework.jdbc.core.ColumnMapRowMapper;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.SqlInOutParameter;
/**
* Oracle specific implementation for the {@link CallMetaDataProvider} interface.

View File

@ -18,8 +18,6 @@ package org.springframework.jdbc.core.namedparam;
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import org.springframework.jdbc.core.SqlParameterValue;
/**

View File

@ -0,0 +1,57 @@
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.jdbc.datasource.init;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* {@link DatabasePopulator} implementation that delegates to a list of other implementations, executing alll scripts.
*
* @author Dave Syer
*
*/
public class CompositeDatabasePopulator implements DatabasePopulator {
private List<DatabasePopulator> populators = new ArrayList<DatabasePopulator>();
/**
* @param populators the populators to set
*/
public void setPopulators(List<DatabasePopulator> populators) {
this.populators.clear();
this.populators.addAll(populators);
}
/**
* @param populator the populator to add
*/
public void addPopulator(DatabasePopulator populator) {
this.populators.add(populator);
}
/* (non-Javadoc)
* @see org.springframework.jdbc.datasource.init.DatabasePopulator#populate(java.sql.Connection)
*/
public void populate(Connection connection) throws SQLException {
for (DatabasePopulator populator : populators) {
populator.populate(connection);
}
}
}

View File

@ -50,7 +50,6 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
private static final Log logger = LogFactory.getLog(ResourceDatabasePopulator.class);
private List<Resource> scripts = new ArrayList<Resource>();
private String sqlScriptEncoding;
@ -61,6 +60,14 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
private boolean ignoreFailedDrops = false;
private String separator = null;
/**
* @param separator the statement separator
*/
public void setSeparator(String separator) {
this.separator = separator;
}
/**
* Add a script to execute to populate the database.
@ -114,7 +121,6 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
this.ignoreFailedDrops = ignoreFailedDrops;
}
public void populate(Connection connection) throws SQLException {
for (Resource script : this.scripts) {
executeSqlScript(connection, applyEncodingIfNecessary(script), this.continueOnError, this.ignoreFailedDrops);
@ -124,8 +130,7 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
private EncodedResource applyEncodingIfNecessary(Resource script) {
if (script instanceof EncodedResource) {
return (EncodedResource) script;
}
else {
} else {
return new EncodedResource(script, this.sqlScriptEncoding);
}
}
@ -140,8 +145,8 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
* @param continueOnError whether or not to continue without throwing an exception in the event of an error
* @param ignoreFailedDrops whether of not to continue in the event of specifically an error on a <code>DROP</code>
*/
private void executeSqlScript(Connection connection, EncodedResource resource,
boolean continueOnError, boolean ignoreFailedDrops) throws SQLException {
private void executeSqlScript(Connection connection, EncodedResource resource, boolean continueOnError,
boolean ignoreFailedDrops) throws SQLException {
if (logger.isInfoEnabled()) {
logger.info("Executing SQL script from " + resource);
@ -151,13 +156,15 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
String script;
try {
script = readScript(resource);
}
catch (IOException ex) {
} catch (IOException ex) {
throw new CannotReadScriptException(resource, ex);
}
char delimiter = ';';
if (!containsSqlScriptDelimiters(script, delimiter)) {
delimiter = '\n';
String delimiter = separator;
if (delimiter == null) {
delimiter = ";";
if (!containsSqlScriptDelimiters(script, delimiter)) {
delimiter = "\n";
}
}
splitSqlScript(script, delimiter, statements);
int lineNumber = 0;
@ -170,26 +177,22 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
if (logger.isDebugEnabled()) {
logger.debug(rowsAffected + " rows affected by SQL: " + statement);
}
}
catch (SQLException ex) {
} catch (SQLException ex) {
boolean dropStatement = StringUtils.startsWithIgnoreCase(statement.trim(), "drop");
if (continueOnError || (dropStatement && ignoreFailedDrops)) {
if (logger.isDebugEnabled()) {
logger.debug("Failed to execute SQL script statement at line " + lineNumber +
" of resource " + resource + ": " + statement, ex);
logger.debug("Failed to execute SQL script statement at line " + lineNumber
+ " of resource " + resource + ": " + statement, ex);
}
}
else {
} else {
throw new ScriptStatementFailedException(statement, lineNumber, resource, ex);
}
}
}
}
finally {
} finally {
try {
stmt.close();
}
catch (Throwable ex) {
} catch (Throwable ex) {
logger.debug("Could not close JDBC Statement", ex);
}
}
@ -210,8 +213,8 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
String currentStatement = lnr.readLine();
StringBuilder scriptBuilder = new StringBuilder();
while (currentStatement != null) {
if (StringUtils.hasText(currentStatement) &&
(this.commentPrefix != null && !currentStatement.startsWith(this.commentPrefix))) {
if (StringUtils.hasText(currentStatement)
&& (this.commentPrefix != null && !currentStatement.startsWith(this.commentPrefix))) {
if (scriptBuilder.length() > 0) {
scriptBuilder.append('\n');
}
@ -219,22 +222,34 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
}
currentStatement = lnr.readLine();
}
maybeAddSeparatorToScript(scriptBuilder);
return scriptBuilder.toString();
}
private void maybeAddSeparatorToScript(StringBuilder scriptBuilder) {
if (separator==null || separator.trim().length()==separator.length()) {
return;
}
String trimmed = separator.trim();
// separator ends in whitespace, so we might want to see if the script is trying to end the same way
if (scriptBuilder.lastIndexOf(trimmed)==scriptBuilder.length()-trimmed.length()) {
scriptBuilder.append(separator.substring(trimmed.length()));
}
}
/**
* Does the provided SQL script contain the specified delimiter?
* @param script the SQL script
* @param delim character delimiting each statement - typically a ';' character
*/
private boolean containsSqlScriptDelimiters(String script, char delim) {
private boolean containsSqlScriptDelimiters(String script, String delim) {
boolean inLiteral = false;
char[] content = script.toCharArray();
for (int i = 0; i < script.length(); i++) {
if (content[i] == '\'') {
inLiteral = !inLiteral;
}
if (content[i] == delim && !inLiteral) {
if (!inLiteral && script.substring(i).startsWith(delim)) {
return true;
}
}
@ -248,7 +263,7 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
* @param delim character delimiting each statement (typically a ';' character)
* @param statements the List that will contain the individual statements
*/
private void splitSqlScript(String script, char delim, List<String> statements) {
private void splitSqlScript(String script, String delim, List<String> statements) {
StringBuilder sb = new StringBuilder();
boolean inLiteral = false;
boolean inEscape = false;
@ -258,7 +273,7 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
if (inEscape) {
inEscape = false;
sb.append(c);
continue;
continue;
}
// MySQL style escapes
if (c == '\\') {
@ -270,14 +285,14 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
inLiteral = !inLiteral;
}
if (!inLiteral) {
if (c == delim) {
if (script.substring(i).startsWith(delim)) {
if (sb.length() > 0) {
statements.add(sb.toString());
sb = new StringBuilder();
}
i += delim.length() - 1;
continue;
}
else if (c == '\n' || c == '\t') {
} else if (c == '\n' || c == '\t') {
c = ' ';
}
}

View File

@ -129,6 +129,13 @@
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="separator" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[
The statement separator in the script (the default is to use ';' if it is present in the script, or '\n' otherwise).
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
<xsd:simpleType name="databaseType">

View File

@ -1,7 +1,9 @@
package org.springframework.jdbc.config;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import javax.sql.DataSource;
@ -22,6 +24,7 @@ public class JdbcNamespaceIntegrationTest {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"org/springframework/jdbc/config/jdbc-config.xml");
assertCorrectSetup(context, "dataSource", "h2DataSource", "derbyDataSource");
context.close();
}
@Test
@ -30,6 +33,7 @@ public class JdbcNamespaceIntegrationTest {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"org/springframework/jdbc/config/jdbc-config.xml");
assertCorrectSetup(context, "derbyDataSource");
context.close();
}
@Test
@ -37,6 +41,15 @@ public class JdbcNamespaceIntegrationTest {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"org/springframework/jdbc/config/jdbc-config-pattern.xml");
assertCorrectSetup(context, "dataSource");
context.close();
}
@Test
public void testCreateWithEndings() throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"org/springframework/jdbc/config/jdbc-initialize-endings-config.xml");
assertCorrectSetup(context, 2, "dataSource");
context.close();
}
@Test
@ -58,15 +71,21 @@ public class JdbcNamespaceIntegrationTest {
}
private void assertCorrectSetup(ConfigurableApplicationContext context, String... dataSources) {
assertCorrectSetup(context, 1, dataSources);
}
private void assertCorrectSetup(ConfigurableApplicationContext context, int count, String... dataSources) {
try {
for (String dataSourceName : dataSources) {
DataSource dataSource = context.getBean(dataSourceName, DataSource.class);
JdbcTemplate t = new JdbcTemplate(dataSource);
assertEquals(1, t.queryForInt("select count(*) from T_TEST"));
assertEquals(count, t.queryForInt("select count(*) from T_TEST"));
}
} finally {
context.close();
}
}
}

View File

@ -23,8 +23,6 @@ import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.sql.Types;
import junit.framework.TestCase;
import org.easymock.MockControl;
import org.apache.commons.logging.LogFactory;

View File

@ -42,7 +42,6 @@ import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.jdbc.CannotGetJdbcConnectionException;
import org.springframework.jdbc.SQLWarningException;
import org.springframework.jdbc.UncategorizedSQLException;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.jdbc.core.support.AbstractInterruptibleBatchPreparedStatementSetter;
import org.springframework.jdbc.datasource.SingleConnectionDataSource;
import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator;

View File

@ -102,6 +102,84 @@ public class DatabasePopulatorTests {
assertTestDatabaseCreated("\\$Keith\\$");
}
@Test
public void testBuildWithMultipleStatements() throws Exception {
databasePopulator.addScript(resourceLoader.getResource("db-schema.sql"));
databasePopulator.addScript(resourceLoader.getResource("db-test-data-multiple.sql"));
Connection connection = db.getConnection();
try {
databasePopulator.populate(connection);
} finally {
connection.close();
}
assertEquals(1, jdbcTemplate.queryForInt("select COUNT(NAME) from T_TEST where NAME='Keith'"));
assertEquals(1, jdbcTemplate.queryForInt("select COUNT(NAME) from T_TEST where NAME='Dave'"));
}
@Test
public void testBuildWithMultipleStatementsLongSeparator() throws Exception {
databasePopulator.addScript(resourceLoader.getResource("db-schema.sql"));
databasePopulator.addScript(resourceLoader.getResource("db-test-data-endings.sql"));
databasePopulator.setSeparator("@@");
Connection connection = db.getConnection();
try {
databasePopulator.populate(connection);
} finally {
connection.close();
}
assertEquals(1, jdbcTemplate.queryForInt("select COUNT(NAME) from T_TEST where NAME='Keith'"));
assertEquals(1, jdbcTemplate.queryForInt("select COUNT(NAME) from T_TEST where NAME='Dave'"));
}
@Test
public void testBuildWithMultipleStatementsWhitespaceSeparator() throws Exception {
databasePopulator.addScript(resourceLoader.getResource("db-schema.sql"));
databasePopulator.addScript(resourceLoader.getResource("db-test-data-whitespace.sql"));
databasePopulator.setSeparator("/\n");
Connection connection = db.getConnection();
try {
databasePopulator.populate(connection);
} finally {
connection.close();
}
assertEquals(1, jdbcTemplate.queryForInt("select COUNT(NAME) from T_TEST where NAME='Keith'"));
assertEquals(1, jdbcTemplate.queryForInt("select COUNT(NAME) from T_TEST where NAME='Dave'"));
}
@Test
public void testBuildWithMultipleStatementsNewlineSeparator() throws Exception {
databasePopulator.addScript(resourceLoader.getResource("db-schema.sql"));
databasePopulator.addScript(resourceLoader.getResource("db-test-data-newline.sql"));
Connection connection = db.getConnection();
try {
databasePopulator.populate(connection);
} finally {
connection.close();
}
assertEquals(1, jdbcTemplate.queryForInt("select COUNT(NAME) from T_TEST where NAME='Keith'"));
assertEquals(1, jdbcTemplate.queryForInt("select COUNT(NAME) from T_TEST where NAME='Dave'"));
}
@Test
public void testBuildWithMultipleStatementsMultipleNewlineSeparator() throws Exception {
databasePopulator.addScript(resourceLoader.getResource("db-schema.sql"));
databasePopulator.addScript(resourceLoader.getResource("db-test-data-multi-newline.sql"));
databasePopulator.setSeparator("\n\n");
Connection connection = db.getConnection();
try {
databasePopulator.populate(connection);
} finally {
connection.close();
}
assertEquals(1, jdbcTemplate.queryForInt("select COUNT(NAME) from T_TEST where NAME='Keith'"));
assertEquals(1, jdbcTemplate.queryForInt("select COUNT(NAME) from T_TEST where NAME='Dave'"));
}
@Test
public void scriptWithEolBetweenTokens() throws Exception {
databasePopulator.addScript(resourceLoader.getResource("users-schema.sql"));

View File

@ -27,7 +27,6 @@ import java.util.Map;
import org.easymock.MockControl;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.JdkVersion;
import org.springframework.jdbc.AbstractJdbcTests;
import org.springframework.jdbc.JdbcUpdateAffectedIncorrectNumberOfRowsException;
import org.springframework.jdbc.core.SqlParameter;

View File

@ -0,0 +1,2 @@
insert into T_TEST (NAME) values ('Keith')@@
insert into T_TEST (NAME) values ('Dave')@@

View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd">
<jdbc:embedded-database id="dataSource" type="HSQL"/>
<jdbc:initialize-database data-source="dataSource">
<jdbc:script location="classpath:org/springframework/jdbc/config/db-schema.sql"/>
<jdbc:script location="classpath:org/springframework/jdbc/config/db-test-data-endings.sql" separator="@@"/>
</jdbc:initialize-database>
</beans>

View File

@ -0,0 +1,2 @@
insert into T_TEST (NAME) values ('Keith')@@
insert into T_TEST (NAME) values ('Dave')@@

View File

@ -0,0 +1,5 @@
insert into T_TEST (NAME)
values ('Keith')
insert into T_TEST (NAME)
values ('Dave')

View File

@ -0,0 +1,2 @@
insert into T_TEST (NAME) values ('Keith');
insert into T_TEST (NAME) values ('Dave');

View File

@ -0,0 +1,2 @@
insert into T_TEST (NAME) values ('Keith')
insert into T_TEST (NAME) values ('Dave')

View File

@ -0,0 +1,5 @@
insert into T_TEST (NAME) values ('Keith')
/
insert into T_TEST (NAME) values ('Dave')
/