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`
This commit is contained in:
@@ -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<String, Object>());
|
||||
this.parameterExpressions = new HashMap<String, Expression[]>();
|
||||
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<String, ?> staticParameters) {
|
||||
@@ -111,12 +112,10 @@ public class ExpressionEvaluatingSqlParameterSourceFactory extends AbstractExpre
|
||||
* <td>{@code select * from items where name=:key}</td>
|
||||
* </tr>
|
||||
* </table>
|
||||
* <p>
|
||||
*
|
||||
* @param parameterExpressions the parameter expressions to set
|
||||
*/
|
||||
public void setParameterExpressions(Map<String, String> parameterExpressions) {
|
||||
Map<String, Expression[]> paramExpressions = new HashMap<String, Expression[]>(parameterExpressions.size());
|
||||
Map<String, Expression[]> paramExpressions = new HashMap<>(parameterExpressions.size());
|
||||
for (Map.Entry<String, String> 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,38 +1,38 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans:beans xmlns="http://www.springframework.org/schema/integration/jdbc"
|
||||
xmlns:beans="http://www.springframework.org/schema/beans"
|
||||
xmlns:si="http://www.springframework.org/schema/integration"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
xmlns:beans="http://www.springframework.org/schema/beans"
|
||||
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/integration
|
||||
http://www.springframework.org/schema/integration/spring-integration.xsd
|
||||
http://www.springframework.org/schema/integration/jdbc
|
||||
http://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc.xsd">
|
||||
|
||||
<inbound-channel-adapter query="select * from item where status=:status" channel="target"
|
||||
data-source="dataSource" select-sql-parameter-source="parameterSource"
|
||||
update="delete from item" />
|
||||
|
||||
<beans:import resource="jdbcInboundChannelAdapterCommonConfig.xml" />
|
||||
|
||||
<beans:bean id="parameterSource" factory-bean="parameterSourceFactory" factory-method="createParameterSourceNoCache">
|
||||
<beans:constructor-arg value="" />
|
||||
<inbound-channel-adapter query="select * from item where status=:status" channel="target"
|
||||
data-source="dataSource" select-sql-parameter-source="parameterSource"
|
||||
update="delete from item"/>
|
||||
|
||||
<beans:import resource="jdbcInboundChannelAdapterCommonConfig.xml"/>
|
||||
|
||||
<beans:bean id="parameterSource" factory-bean="parameterSourceFactory"
|
||||
factory-method="createParameterSourceNoCache">
|
||||
<beans:constructor-arg value=""/>
|
||||
</beans:bean>
|
||||
|
||||
<beans:bean id="parameterSourceFactory" class="org.springframework.integration.jdbc.ExpressionEvaluatingSqlParameterSourceFactory">
|
||||
<beans:bean id="parameterSourceFactory"
|
||||
class="org.springframework.integration.jdbc.ExpressionEvaluatingSqlParameterSourceFactory">
|
||||
<beans:property name="parameterExpressions">
|
||||
<beans:map>
|
||||
<beans:entry key="status" value="@statusBean.which()" />
|
||||
<beans:entry key="status" value="@statusBean.which()"/>
|
||||
</beans:map>
|
||||
</beans:property>
|
||||
<beans:property name="sqlParameterTypes">
|
||||
<beans:map>
|
||||
<beans:entry key="status" value="#{ T(java.sql.Types).INTEGER}" />
|
||||
</beans:map>
|
||||
<beans:entry key="status" value="#{ T(java.sql.Types).INTEGER}"/>
|
||||
</beans:map>
|
||||
</beans:property>
|
||||
</beans:bean>
|
||||
|
||||
<beans:bean id="statusBean" class="org.springframework.integration.jdbc.config.JdbcPollingChannelAdapterParserTests$Status" />
|
||||
<beans:bean id="statusBean"
|
||||
class="org.springframework.integration.jdbc.config.JdbcPollingChannelAdapterParserTests$Status"/>
|
||||
|
||||
</beans:beans>
|
||||
|
||||
@@ -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]
|
||||
----
|
||||
<int-jdbc:inbound-channel-adapter query="select * from item where status=:status"
|
||||
channel="target" data-source="dataSource"
|
||||
select-sql-parameter-source="parameterSource" />
|
||||
channel="target" data-source="dataSource"
|
||||
select-sql-parameter-source="parameterSource" />
|
||||
|
||||
<bean id="parameterSource" factory-bean="parameterSourceFactory"
|
||||
factory-method="createParameterSourceNoCache">
|
||||
<constructor-arg value="" />
|
||||
factory-method="createParameterSourceNoCache">
|
||||
<constructor-arg value="" />
|
||||
</bean>
|
||||
|
||||
<bean id="parameterSourceFactory"
|
||||
class="o.s.integration.jdbc.ExpressionEvaluatingSqlParameterSourceFactory">
|
||||
<property name="sqlParameterTypes">
|
||||
<map>
|
||||
<entry key="status" value=""#{ T(java.sql.Types).BINARY}" />
|
||||
</map>
|
||||
</property>
|
||||
class="o.s.integration.jdbc.ExpressionEvaluatingSqlParameterSourceFactory">
|
||||
<property name="sqlParameterTypes">
|
||||
<map>
|
||||
<entry key="status" value=""#{ T(java.sql.Types).BINARY}" />
|
||||
</map>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
----
|
||||
|
||||
IMPORTANT: Use the `createParameterSourceNoCache` factory method; otherwise the parameter source will cache the result of the evaluation.
|
||||
|
||||
@@ -296,9 +296,6 @@ See <<gemfire>> for more information.
|
||||
|
||||
The `JdbcMessageChannelStore` now provides setter for the `ChannelMessageStorePreparedStatementSetter` allowing users to customize a message insertion in the store.
|
||||
|
||||
See <<jdbc-message-store-channels>> 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 <<jdbc-inbound-channel-adapter>> for more information.
|
||||
See <<jdbc>> for more information.
|
||||
|
||||
Reference in New Issue
Block a user