From 098e8bc58f803989a2bf14576b3137b606310ead Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Mon, 6 Nov 2017 11:33:05 -0500 Subject: [PATCH] INT-4291: Post-merge polishing JIRA: https://jira.spring.io/browse/INT-4291 * Fix code style in the `ExpressionEvaluatingSqlParameterSourceFactory` * Remove redundant `ExpressionEvaluatingSqlParameterSourceFactory.registerSqlTypes` in favor of the direct access to the `sqlParametersTypes` from the `ExpressionEvaluatingSqlParameterSource` ctor * Reformat code style in the `pollingWithSelectParameterSourceJdbcInboundChannelAdapterTest.xml` * Fix code style in the `ExpressionEvaluatingSqlParameterSourceFactoryTests` * Polishing `whats-new.adoc` for proper formatting and single link from the section to the particular chapter * Fix formatting and wording in the `jdbc.adoc` --- ...onEvaluatingSqlParameterSourceFactory.java | 41 ++++++++----------- ...luatingSqlParameterSourceFactoryTests.java | 4 +- ...terSourceJdbcInboundChannelAdapterTest.xml | 38 ++++++++--------- src/reference/asciidoc/jdbc.adoc | 24 +++++------ src/reference/asciidoc/whats-new.adoc | 7 +--- 5 files changed, 52 insertions(+), 62 deletions(-) diff --git a/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/ExpressionEvaluatingSqlParameterSourceFactory.java b/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/ExpressionEvaluatingSqlParameterSourceFactory.java index f56cd5d072..1760eb6082 100644 --- a/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/ExpressionEvaluatingSqlParameterSourceFactory.java +++ b/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/ExpressionEvaluatingSqlParameterSourceFactory.java @@ -33,18 +33,20 @@ import org.springframework.jdbc.core.namedparam.AbstractSqlParameterSource; import org.springframework.jdbc.core.namedparam.SqlParameterSource; /** - * An implementation of {@link SqlParameterSourceFactory} which creates an {@link SqlParameterSource} that evaluates - * Spring EL expressions. In addition the user can supply static parameters that always take precedence. + * An implementation of {@link SqlParameterSourceFactory} which creates + * an {@link SqlParameterSource} that evaluates Spring EL expressions. + * In addition the user can supply static parameters that always take precedence. * * @author Dave Syer * @author Oleg Zhurakousky * @author Gary Russell * @author Artem Bilan * @author Meherzad Lahewala + * * @since 2.0 */ -public class ExpressionEvaluatingSqlParameterSourceFactory extends AbstractExpressionEvaluator implements - SqlParameterSourceFactory { +public class ExpressionEvaluatingSqlParameterSourceFactory extends AbstractExpressionEvaluator + implements SqlParameterSourceFactory { private final static Log logger = LogFactory.getLog(ExpressionEvaluatingSqlParameterSourceFactory.class); @@ -66,14 +68,13 @@ public class ExpressionEvaluatingSqlParameterSourceFactory extends AbstractExpre public ExpressionEvaluatingSqlParameterSourceFactory() { this.staticParameters = Collections.unmodifiableMap(new HashMap()); - this.parameterExpressions = new HashMap(); + this.parameterExpressions = new HashMap<>(); } /** * Define some static parameter values. These take precedence over those defined as expressions in the * {@link #setParameterExpressions(Map) parameterExpressions}, so a parameter in the query will be filled from here * first, and then from the expressions. - * * @param staticParameters the static parameters to set */ public void setStaticParameters(Map staticParameters) { @@ -111,12 +112,10 @@ public class ExpressionEvaluatingSqlParameterSourceFactory extends AbstractExpre * {@code select * from items where name=:key} * * - *

- * * @param parameterExpressions the parameter expressions to set */ public void setParameterExpressions(Map parameterExpressions) { - Map paramExpressions = new HashMap(parameterExpressions.size()); + Map paramExpressions = new HashMap<>(parameterExpressions.size()); for (Map.Entry entry : parameterExpressions.entrySet()) { String key = entry.getKey(); String expression = entry.getValue(); @@ -130,9 +129,9 @@ public class ExpressionEvaluatingSqlParameterSourceFactory extends AbstractExpre } /** - * Optionally set parameter sql types for the used parameters. Use - * {@link java.sql.Types} to get the parameter type value. - * @param sqlParametersTypes the parameter type to use + * Specify sql types for the parameters. Optional. + * Use {@link java.sql.Types} to get the parameter type value. + * @param sqlParametersTypes the parameter types to use * @since 5.0 * @see java.sql.Types */ @@ -142,10 +141,8 @@ public class ExpressionEvaluatingSqlParameterSourceFactory extends AbstractExpre @Override public SqlParameterSource createParameterSource(final Object input) { - AbstractSqlParameterSource sqlParameterSource = new ExpressionEvaluatingSqlParameterSource(input, + return new ExpressionEvaluatingSqlParameterSource(input, this.staticParameters, this.parameterExpressions, true); - registerSqlTypes(sqlParameterSource); - return sqlParameterSource; } /** @@ -156,10 +153,8 @@ public class ExpressionEvaluatingSqlParameterSourceFactory extends AbstractExpre * @return The parameter source. */ public SqlParameterSource createParameterSourceNoCache(final Object input) { - AbstractSqlParameterSource sqlParameterSource = new ExpressionEvaluatingSqlParameterSource(input, + return new ExpressionEvaluatingSqlParameterSource(input, this.staticParameters, this.parameterExpressions, false); - registerSqlTypes(sqlParameterSource); - return sqlParameterSource; } @Override @@ -168,12 +163,6 @@ public class ExpressionEvaluatingSqlParameterSourceFactory extends AbstractExpre this.getEvaluationContext().setVariable("staticParameters", this.staticParameters); } - private void registerSqlTypes(AbstractSqlParameterSource sqlParameterSource) { - if (this.sqlParametersTypes != null) { - this.sqlParametersTypes.forEach(sqlParameterSource::registerSqlType); - } - } - private final class ExpressionEvaluatingSqlParameterSource extends AbstractSqlParameterSource { private final Object input; @@ -190,6 +179,9 @@ public class ExpressionEvaluatingSqlParameterSourceFactory extends AbstractExpre this.parameterExpressions = parameterExpressions; this.values.putAll(staticParameters); this.cache = cache; + if (ExpressionEvaluatingSqlParameterSourceFactory.this.sqlParametersTypes != null) { + ExpressionEvaluatingSqlParameterSourceFactory.this.sqlParametersTypes.forEach(this::registerSqlType); + } } @Override @@ -253,6 +245,7 @@ public class ExpressionEvaluatingSqlParameterSourceFactory extends AbstractExpre } return true; } + } } diff --git a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/ExpressionEvaluatingSqlParameterSourceFactoryTests.java b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/ExpressionEvaluatingSqlParameterSourceFactoryTests.java index 9833ebdb02..406d1cd98a 100644 --- a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/ExpressionEvaluatingSqlParameterSourceFactoryTests.java +++ b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/ExpressionEvaluatingSqlParameterSourceFactoryTests.java @@ -36,7 +36,8 @@ import org.springframework.jdbc.support.JdbcUtils; */ public class ExpressionEvaluatingSqlParameterSourceFactoryTests { - private final ExpressionEvaluatingSqlParameterSourceFactory factory = new ExpressionEvaluatingSqlParameterSourceFactory(); + private final ExpressionEvaluatingSqlParameterSourceFactory factory = + new ExpressionEvaluatingSqlParameterSourceFactory(); @Test public void testSetStaticParameters() throws Exception { @@ -145,4 +146,5 @@ public class ExpressionEvaluatingSqlParameterSourceFactoryTests { assertEquals("[BAR, BUCKET]", source.getValue(expression).toString()); assertEquals(JdbcUtils.TYPE_UNKNOWN, source.getSqlType("spam")); } + } diff --git a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/config/pollingWithSelectParameterSourceJdbcInboundChannelAdapterTest.xml b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/config/pollingWithSelectParameterSourceJdbcInboundChannelAdapterTest.xml index 57e9245c71..b9226ce284 100644 --- a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/config/pollingWithSelectParameterSourceJdbcInboundChannelAdapterTest.xml +++ b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/config/pollingWithSelectParameterSourceJdbcInboundChannelAdapterTest.xml @@ -1,38 +1,38 @@ - - - - - - + + + + + + - + - + - - + + - + diff --git a/src/reference/asciidoc/jdbc.adoc b/src/reference/asciidoc/jdbc.adoc index 2f07e5160e..e0bd706f32 100644 --- a/src/reference/asciidoc/jdbc.adoc +++ b/src/reference/asciidoc/jdbc.adoc @@ -81,31 +81,29 @@ The `value` in each parameter expression can be any valid SpEL expression. The `#root` object for the expression evaluation is the constructor argument defined on the `parameterSource` bean. It is static for all evaluations (in this case, an empty String). -Starting with _version 5.0_, you can use pass parameter types using `setSqlParameterTypes` method in -the `ExpressionEvaluatingSqlParameterSourceFactory` can be supplied with the sqlParameterTypes to specify the target SQL type for the particular parameter. +Starting with _version 5.0_, the `ExpressionEvaluatingSqlParameterSourceFactory` can be supplied with the `sqlParameterTypes` to specify the target SQL type for the particular parameter. Below example provides sql type for the parameters being used in the query. [source,xml] ---- + channel="target" data-source="dataSource" + select-sql-parameter-source="parameterSource" /> - + factory-method="createParameterSourceNoCache"> + - - - - - + class="o.s.integration.jdbc.ExpressionEvaluatingSqlParameterSourceFactory"> + + + + + - ---- IMPORTANT: Use the `createParameterSourceNoCache` factory method; otherwise the parameter source will cache the result of the evaluation. diff --git a/src/reference/asciidoc/whats-new.adoc b/src/reference/asciidoc/whats-new.adoc index 3aa7155ca9..f353c7f887 100644 --- a/src/reference/asciidoc/whats-new.adoc +++ b/src/reference/asciidoc/whats-new.adoc @@ -296,9 +296,6 @@ See <> for more information. The `JdbcMessageChannelStore` now provides setter for the `ChannelMessageStorePreparedStatementSetter` allowing users to customize a message insertion in the store. -See <> for more information. +The `ExpressionEvaluatingSqlParameterSourceFactory` now provides setter for the sqlParameterTypes allowing users to customize sql types of the parameters. -The `ExpressionEvaluatingSqlParameterSourceFactory` now provides setter for the sqlParameterTypes allowing users -to customize sql types of the parameters. - -See <> for more information. \ No newline at end of file +See <> for more information.