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:
parent
eb49f3c225
commit
714ae2684c
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2011 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.
|
||||
|
@ -32,6 +32,7 @@ import org.springframework.util.xml.DomUtils;
|
|||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @author Stephane Nicoll
|
||||
* @since 3.1
|
||||
*/
|
||||
class DatabasePopulatorConfigUtils {
|
||||
|
@ -70,8 +71,9 @@ class DatabasePopulatorConfigUtils {
|
|||
if (StringUtils.hasLength(scriptElement.getAttribute("encoding"))) {
|
||||
delegate.addPropertyValue("sqlScriptEncoding", new TypedStringValue(scriptElement.getAttribute("encoding")));
|
||||
}
|
||||
if (StringUtils.hasLength(scriptElement.getAttribute("separator"))) {
|
||||
delegate.addPropertyValue("separator", new TypedStringValue(scriptElement.getAttribute("separator")));
|
||||
String separator = getSeparator(element, scriptElement);
|
||||
if (separator != null) {
|
||||
delegate.addPropertyValue("separator", new TypedStringValue(separator));
|
||||
}
|
||||
delegates.add(delegate.getBeanDefinition());
|
||||
}
|
||||
|
@ -80,4 +82,16 @@ class DatabasePopulatorConfigUtils {
|
|||
return builder.getBeanDefinition();
|
||||
}
|
||||
|
||||
private static String getSeparator(Element element, Element scriptElement) {
|
||||
String scriptSeparator = scriptElement.getAttribute("separator");
|
||||
if (StringUtils.hasLength(scriptSeparator)) {
|
||||
return scriptSeparator;
|
||||
}
|
||||
String elementSeparator = element.getAttribute("separator");
|
||||
if (StringUtils.hasLength(elementSeparator)) {
|
||||
return elementSeparator;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -55,6 +55,14 @@
|
|||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="separator" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
The default statement separator to use (the default is to use ';' if it is present
|
||||
in the script, or '\n' otherwise).
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="type" type="databaseType" default="HSQL">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
|
@ -135,6 +143,14 @@
|
|||
</xsd:restriction>
|
||||
</xsd:simpleType>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="separator" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
The default statement separator to use (the default is to use ';' if it is present
|
||||
in the script, or '\n' otherwise).
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -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>
|
|
@ -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>
|
|
@ -4877,6 +4877,25 @@ followed by a set of `CREATE` statements.
|
|||
The `ignore-failures` option can be set to `NONE` (the default), `DROPS` (ignore failed
|
||||
drops), or `ALL` (ignore all failures).
|
||||
|
||||
Each statement should be separated by `;` or a new line if the `;` character is not
|
||||
present at all in the script. You can control that globally or script by script, for
|
||||
example:
|
||||
|
||||
[source,xml,indent=0]
|
||||
[subs="verbatim,quotes"]
|
||||
----
|
||||
<jdbc:initialize-database data-source="dataSource" **separator="@@"**>
|
||||
<jdbc:script location="classpath:com/foo/sql/db-schema.sql" **separator=";"**/>
|
||||
<jdbc:script location="classpath:com/foo/sql/db-test-data-1.sql"/>
|
||||
<jdbc:script location="classpath:com/foo/sql/db-test-data-2.sql"/>
|
||||
</jdbc:initialize-database>
|
||||
----
|
||||
|
||||
In this example, the two `test-data` scripts use `@@` as statement separator and only
|
||||
the `db-schema.sql` uses `;`. This configuration specifies that the default separator
|
||||
is `@@` and override that default for the `db-schema` script.
|
||||
|
||||
|
||||
If you need more control than you get from the XML namespace, you can simply use the
|
||||
`DataSourceInitializer` directly and define it as a component in your application.
|
||||
|
||||
|
|
|
@ -635,6 +635,11 @@ public @interface MyTestConfig {
|
|||
* Any SpEL expression used to specify the `condition` of an `@EventListener` can
|
||||
now refer to beans (i.e. `@beanName.method()`).
|
||||
|
||||
=== Data Access Improvements
|
||||
|
||||
* `jdbc:initialize-database` and `jdbc:embedded-database` support a configurable
|
||||
separator to be applied to each script.
|
||||
|
||||
=== Caching Improvements
|
||||
|
||||
Spring 4.3 allows concurrent calls on a given key to be synchronized so that the
|
||||
|
|
Loading…
Reference in New Issue