Support for global separator in JDBC namespace

Previously, if a database needs to be initialized with several scripts
and many (or all) use a custom separator, said separator must be repeated
for each script.

This commit introduces a `separator` property at the parent element level
that can be used to customize the default separator. This is available
for both the `initialize-database` and `embedded-database` elements.

Issue: SPR-13792
This commit is contained in:
Stephane Nicoll
2016-01-18 16:09:07 +01:00
parent eb49f3c225
commit 714ae2684c
7 changed files with 97 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@@ -48,6 +48,7 @@ import static org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFacto
* @author Juergen Hoeller
* @author Chris Beams
* @author Sam Brannen
* @author Stephane Nicoll
*/
public class JdbcNamespaceIntegrationTests {
@@ -165,6 +166,16 @@ public class JdbcNamespaceIntegrationTests {
assertBeanPropertyValueOf("databaseName", "secondDataSource", factory);
}
@Test
public void initializeWithCustomSeparator() throws Exception {
assertCorrectSetupAndCloseContext("jdbc-initialize-custom-separator.xml", 2, "dataSource");
}
@Test
public void embeddedWithCustomSeparator() throws Exception {
assertCorrectSetupAndCloseContext("jdbc-config-custom-separator.xml", 2, "dataSource");
}
private ClassPathXmlApplicationContext context(String file) {
return new ClassPathXmlApplicationContext(file, getClass());
}

View File

@@ -0,0 +1,13 @@
<?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-4.3.xsd">
<jdbc:embedded-database id="dataSource" type="HSQL" separator="@@">
<jdbc:script location="classpath:org/springframework/jdbc/config/db-schema.sql" separator=";"/>
<jdbc:script location="classpath:org/springframework/jdbc/config/db-test-data-endings.sql"/>
</jdbc:embedded-database>
</beans>

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-4.3.xsd">
<jdbc:embedded-database id="dataSource" type="HSQL"/>
<jdbc:initialize-database data-source="dataSource" separator="@@">
<jdbc:script location="classpath:org/springframework/jdbc/config/db-schema.sql" separator=";" encoding="ISO-8859-1"/>
<jdbc:script location="classpath:org/springframework/jdbc/config/db-test-data-endings.sql"/>
</jdbc:initialize-database>
</beans>