From 714ae2684c4f97b8fcae564afbc48d94bc76a544 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 18 Jan 2016 16:09:07 +0100 Subject: [PATCH] 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 --- .../config/DatabasePopulatorConfigUtils.java | 20 ++++++++++++++++--- .../jdbc/config/spring-jdbc-4.3.xsd | 16 +++++++++++++++ .../config/JdbcNamespaceIntegrationTests.java | 13 +++++++++++- .../config/jdbc-config-custom-separator.xml | 13 ++++++++++++ .../jdbc-initialize-custom-separator.xml | 15 ++++++++++++++ src/asciidoc/data-access.adoc | 19 ++++++++++++++++++ src/asciidoc/whats-new.adoc | 5 +++++ 7 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 spring-jdbc/src/test/resources/org/springframework/jdbc/config/jdbc-config-custom-separator.xml create mode 100644 spring-jdbc/src/test/resources/org/springframework/jdbc/config/jdbc-initialize-custom-separator.xml diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/config/DatabasePopulatorConfigUtils.java b/spring-jdbc/src/main/java/org/springframework/jdbc/config/DatabasePopulatorConfigUtils.java index eca9a2fdbd..384cb0c859 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/config/DatabasePopulatorConfigUtils.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/config/DatabasePopulatorConfigUtils.java @@ -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; + } + } diff --git a/spring-jdbc/src/main/resources/org/springframework/jdbc/config/spring-jdbc-4.3.xsd b/spring-jdbc/src/main/resources/org/springframework/jdbc/config/spring-jdbc-4.3.xsd index 967f3cf50c..ab76e90f3b 100644 --- a/spring-jdbc/src/main/resources/org/springframework/jdbc/config/spring-jdbc-4.3.xsd +++ b/spring-jdbc/src/main/resources/org/springframework/jdbc/config/spring-jdbc-4.3.xsd @@ -55,6 +55,14 @@ + + + + + + + + + + diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/config/JdbcNamespaceIntegrationTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/config/JdbcNamespaceIntegrationTests.java index 6615c8853d..d15f94b4ef 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/config/JdbcNamespaceIntegrationTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/config/JdbcNamespaceIntegrationTests.java @@ -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()); } diff --git a/spring-jdbc/src/test/resources/org/springframework/jdbc/config/jdbc-config-custom-separator.xml b/spring-jdbc/src/test/resources/org/springframework/jdbc/config/jdbc-config-custom-separator.xml new file mode 100644 index 0000000000..7f9479e3f4 --- /dev/null +++ b/spring-jdbc/src/test/resources/org/springframework/jdbc/config/jdbc-config-custom-separator.xml @@ -0,0 +1,13 @@ + + + + + + + + + diff --git a/spring-jdbc/src/test/resources/org/springframework/jdbc/config/jdbc-initialize-custom-separator.xml b/spring-jdbc/src/test/resources/org/springframework/jdbc/config/jdbc-initialize-custom-separator.xml new file mode 100644 index 0000000000..8a2ea73175 --- /dev/null +++ b/spring-jdbc/src/test/resources/org/springframework/jdbc/config/jdbc-initialize-custom-separator.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + diff --git a/src/asciidoc/data-access.adoc b/src/asciidoc/data-access.adoc index 1d6b86d68e..c1c9f6c725 100644 --- a/src/asciidoc/data-access.adoc +++ b/src/asciidoc/data-access.adoc @@ -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"] +---- + + + + + +---- + +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. diff --git a/src/asciidoc/whats-new.adoc b/src/asciidoc/whats-new.adoc index 54e050b7bd..09c03a3246 100644 --- a/src/asciidoc/whats-new.adoc +++ b/src/asciidoc/whats-new.adoc @@ -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