Skip to content

Commit 714ae26

Browse files
committed
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
1 parent eb49f3c commit 714ae26

File tree

7 files changed

+97
-4
lines changed

7 files changed

+97
-4
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/config/DatabasePopulatorConfigUtils.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@
3232

3333
/**
3434
* @author Juergen Hoeller
35+
* @author Stephane Nicoll
3536
* @since 3.1
3637
*/
3738
class DatabasePopulatorConfigUtils {
@@ -70,8 +71,9 @@ static private BeanDefinition createDatabasePopulator(Element element, List<Elem
7071
if (StringUtils.hasLength(scriptElement.getAttribute("encoding"))) {
7172
delegate.addPropertyValue("sqlScriptEncoding", new TypedStringValue(scriptElement.getAttribute("encoding")));
7273
}
73-
if (StringUtils.hasLength(scriptElement.getAttribute("separator"))) {
74-
delegate.addPropertyValue("separator", new TypedStringValue(scriptElement.getAttribute("separator")));
74+
String separator = getSeparator(element, scriptElement);
75+
if (separator != null) {
76+
delegate.addPropertyValue("separator", new TypedStringValue(separator));
7577
}
7678
delegates.add(delegate.getBeanDefinition());
7779
}
@@ -80,4 +82,16 @@ static private BeanDefinition createDatabasePopulator(Element element, List<Elem
8082
return builder.getBeanDefinition();
8183
}
8284

85+
private static String getSeparator(Element element, Element scriptElement) {
86+
String scriptSeparator = scriptElement.getAttribute("separator");
87+
if (StringUtils.hasLength(scriptSeparator)) {
88+
return scriptSeparator;
89+
}
90+
String elementSeparator = element.getAttribute("separator");
91+
if (StringUtils.hasLength(elementSeparator)) {
92+
return elementSeparator;
93+
}
94+
return null;
95+
}
96+
8397
}

spring-jdbc/src/main/resources/org/springframework/jdbc/config/spring-jdbc-4.3.xsd

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@
5555
</xsd:documentation>
5656
</xsd:annotation>
5757
</xsd:attribute>
58+
<xsd:attribute name="separator" type="xsd:string">
59+
<xsd:annotation>
60+
<xsd:documentation><![CDATA[
61+
The default statement separator to use (the default is to use ';' if it is present
62+
in the script, or '\n' otherwise).
63+
]]></xsd:documentation>
64+
</xsd:annotation>
65+
</xsd:attribute>
5866
<xsd:attribute name="type" type="databaseType" default="HSQL">
5967
<xsd:annotation>
6068
<xsd:documentation><![CDATA[
@@ -135,6 +143,14 @@
135143
</xsd:restriction>
136144
</xsd:simpleType>
137145
</xsd:attribute>
146+
<xsd:attribute name="separator" type="xsd:string">
147+
<xsd:annotation>
148+
<xsd:documentation><![CDATA[
149+
The default statement separator to use (the default is to use ';' if it is present
150+
in the script, or '\n' otherwise).
151+
]]></xsd:documentation>
152+
</xsd:annotation>
153+
</xsd:attribute>
138154
</xsd:complexType>
139155
</xsd:element>
140156

spring-jdbc/src/test/java/org/springframework/jdbc/config/JdbcNamespaceIntegrationTests.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -48,6 +48,7 @@
4848
* @author Juergen Hoeller
4949
* @author Chris Beams
5050
* @author Sam Brannen
51+
* @author Stephane Nicoll
5152
*/
5253
public class JdbcNamespaceIntegrationTests {
5354

@@ -165,6 +166,16 @@ public void multipleDataSourcesHaveDifferentDatabaseNames() throws Exception {
165166
assertBeanPropertyValueOf("databaseName", "secondDataSource", factory);
166167
}
167168

169+
@Test
170+
public void initializeWithCustomSeparator() throws Exception {
171+
assertCorrectSetupAndCloseContext("jdbc-initialize-custom-separator.xml", 2, "dataSource");
172+
}
173+
174+
@Test
175+
public void embeddedWithCustomSeparator() throws Exception {
176+
assertCorrectSetupAndCloseContext("jdbc-config-custom-separator.xml", 2, "dataSource");
177+
}
178+
168179
private ClassPathXmlApplicationContext context(String file) {
169180
return new ClassPathXmlApplicationContext(file, getClass());
170181
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
6+
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd">
7+
8+
<jdbc:embedded-database id="dataSource" type="HSQL" separator="@@">
9+
<jdbc:script location="classpath:org/springframework/jdbc/config/db-schema.sql" separator=";"/>
10+
<jdbc:script location="classpath:org/springframework/jdbc/config/db-test-data-endings.sql"/>
11+
</jdbc:embedded-database>
12+
13+
</beans>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
6+
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd">
7+
8+
<jdbc:embedded-database id="dataSource" type="HSQL"/>
9+
10+
<jdbc:initialize-database data-source="dataSource" separator="@@">
11+
<jdbc:script location="classpath:org/springframework/jdbc/config/db-schema.sql" separator=";" encoding="ISO-8859-1"/>
12+
<jdbc:script location="classpath:org/springframework/jdbc/config/db-test-data-endings.sql"/>
13+
</jdbc:initialize-database>
14+
15+
</beans>

src/asciidoc/data-access.adoc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4877,6 +4877,25 @@ followed by a set of `CREATE` statements.
48774877
The `ignore-failures` option can be set to `NONE` (the default), `DROPS` (ignore failed
48784878
drops), or `ALL` (ignore all failures).
48794879

4880+
Each statement should be separated by `;` or a new line if the `;` character is not
4881+
present at all in the script. You can control that globally or script by script, for
4882+
example:
4883+
4884+
[source,xml,indent=0]
4885+
[subs="verbatim,quotes"]
4886+
----
4887+
<jdbc:initialize-database data-source="dataSource" **separator="@@"**>
4888+
<jdbc:script location="classpath:com/foo/sql/db-schema.sql" **separator=";"**/>
4889+
<jdbc:script location="classpath:com/foo/sql/db-test-data-1.sql"/>
4890+
<jdbc:script location="classpath:com/foo/sql/db-test-data-2.sql"/>
4891+
</jdbc:initialize-database>
4892+
----
4893+
4894+
In this example, the two `test-data` scripts use `@@` as statement separator and only
4895+
the `db-schema.sql` uses `;`. This configuration specifies that the default separator
4896+
is `@@` and override that default for the `db-schema` script.
4897+
4898+
48804899
If you need more control than you get from the XML namespace, you can simply use the
48814900
`DataSourceInitializer` directly and define it as a component in your application.
48824901

src/asciidoc/whats-new.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,11 @@ public @interface MyTestConfig {
635635
* Any SpEL expression used to specify the `condition` of an `@EventListener` can
636636
now refer to beans (i.e. `@beanName.method()`).
637637

638+
=== Data Access Improvements
639+
640+
* `jdbc:initialize-database` and `jdbc:embedded-database` support a configurable
641+
separator to be applied to each script.
642+
638643
=== Caching Improvements
639644

640645
Spring 4.3 allows concurrent calls on a given key to be synchronized so that the

0 commit comments

Comments
 (0)