INT-1783: add parameterExpressions to expression evaluating sql parameter source

This commit is contained in:
Dave Syer
2011-02-04 15:06:32 +00:00
parent 031aa372c0
commit 1b44da28c2
4 changed files with 91 additions and 7 deletions

View File

@@ -60,8 +60,37 @@ public class ExpressionEvaluatingSqlParameterSourceFactoryTests {
@Test
public void testMapInputWithExpression() {
SqlParameterSource source = factory.createParameterSource(Collections.singletonMap("foo", "bar"));
// This is an illegal parameter name in Spring JDBC so we'd never get this as input
assertTrue(source.hasValue("foo.toUpperCase()"));
assertEquals("BAR", source.getValue("foo.toUpperCase()"));
}
@Test
public void testMapInputWithMappedExpression() {
factory.setParameterExpressions(Collections.singletonMap("spam", "foo.toUpperCase()"));
SqlParameterSource source = factory.createParameterSource(Collections.singletonMap("foo", "bar"));
assertTrue(source.hasValue("spam"));
assertEquals("BAR", source.getValue("spam"));
}
@Test
public void testMapInputWithMappedExpressionResolveStatic() {
factory.setParameterExpressions(Collections.singletonMap("spam", "#staticParameters['foo'].toUpperCase()"));
factory.setStaticParameters(Collections.singletonMap("foo", "bar"));
SqlParameterSource source = factory.createParameterSource(Collections.singletonMap("crap", "bucket"));
assertTrue(source.hasValue("spam"));
assertEquals("BAR", source.getValue("spam"));
}
@Test
public void testListOfMapsInputWithExpression() {
factory.setParameterExpressions(Collections.singletonMap("spam", "foo.toUpperCase()"));
@SuppressWarnings("unchecked")
SqlParameterSource source = factory.createParameterSource(Arrays.asList(Collections.singletonMap("foo", "bar"),
Collections.singletonMap("foo", "bucket")));
String expression = "spam";
assertTrue(source.hasValue(expression));
assertEquals("[BAR, BUCKET]", source.getValue(expression).toString());
}
}

View File

@@ -116,7 +116,7 @@ public class JdbcPollingChannelAdapterParserTests {
assertNotNull(message);
List<Map<String, Object>> list = jdbcTemplate.queryForList("SELECT * FROM item WHERE status=1");
assertEquals(1, list.size());
assertEquals("bar", list.get(0).get("NAME"));
assertEquals("BAR", list.get(0).get("NAME"));
}
@Test

View File

@@ -10,12 +10,17 @@
http://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc.xsd">
<inbound-channel-adapter query="select * from item where status=2" channel="target"
update="update item set status=1, name=:foo where id in (:id)" jdbc-operations="jdbcTemplate"
update="update item set status=1, name=:name where id in (:id)" jdbc-operations="jdbcTemplate"
update-sql-parameter-source-factory="sqlParameterSourceFactory" />
<beans:import resource="jdbcInboundChannelAdapterCommonConfig.xml" />
<beans:bean id="sqlParameterSourceFactory" class="org.springframework.integration.jdbc.ExpressionEvaluatingSqlParameterSourceFactory">
<beans:property name="parameterExpressions">
<beans:map>
<beans:entry key="name" value="'bar'.toUpperCase()" />
</beans:map>
</beans:property>
<beans:property name="staticParameters">
<beans:map>
<beans:entry key="foo" value="bar" />