List of usage examples for org.springframework.jdbc.core.namedparam SqlParameterSource SqlParameterSource
SqlParameterSource
From source file:com.p5solutions.core.jpa.orm.AbstractSQL.java
/** * New sql parameter source./*from ww w. j ava 2s .c om*/ * * @param query * the query * @param conversionUtility * the conversion utility * @return the sql parameter source */ public static SqlParameterSource newSQLParameterSource(final AbstractSQL query, final ConversionUtility conversionUtility) { SqlParameterSource ps = new SqlParameterSource() { protected final void throwQueryNullException() { if (query == null) { throw new NullPointerException( "Query cannot be null when generating new SQL Parameter Source" + this); } if (conversionUtility == null) { throw new NullPointerException( "Conversion Utility cannot be null when generating new SQL Parameter Source" + this); } } @Override public boolean hasValue(String paramName) { throwQueryNullException(); return query.hasValue(paramName); } @Override public Object getValue(String paramName) throws IllegalArgumentException { throwQueryNullException(); SQLParameterCriteria qc = query.getValue(paramName); Object value = qc.getValue(); // this will handle deep entity objects as well. try { value = conversionUtility.convert(qc.getParameterBinder(), value, paramName, qc.getBindingType()); } catch (TypeConversionException e) { throw new RuntimeException("Unable convert value " + value + " to target type " + qc.getBindingType() + ", please check target class " + (query.getEntityClass() != null ? query.getEntityClass() : "<NULL>") + " for the problem when binding path " + paramName, e); } return value; } @Override public String getTypeName(String paramName) { SQLParameterCriteria qc = query.getValue(paramName); if (qc != null && qc.getValue() != null) { Object value = qc.getValue(); return value.getClass().getName(); } return null; } @Override public int getSqlType(String paramName) { // TODO use some sort of utility to determine the sql type, // since blob and other types may not work correctly. return TYPE_UNKNOWN; } }; return ps; }
From source file:org.springframework.integration.jdbc.JdbcPollingChannelAdapterIntegrationTests.java
@Test public void testParameterizedPollForListOfMapsNoUpdate() { JdbcPollingChannelAdapter adapter = new JdbcPollingChannelAdapter(this.embeddedDatabase, "select * from item where status=:status"); adapter.setSelectSqlParameterSource(new SqlParameterSource() { public boolean hasValue(String name) { return "status".equals(name); }// w w w. java 2 s . co m public Object getValue(String name) throws IllegalArgumentException { return 2; } public String getTypeName(String name) { return null; } public int getSqlType(String name) { return Types.INTEGER; } }); this.jdbcTemplate.update("insert into item values(1,2)"); Message<Object> message = adapter.receive(); Object payload = message.getPayload(); assertTrue("Wrong payload type", payload instanceof List<?>); List<?> rows = (List<?>) payload; assertEquals("Wrong number of elements", 1, rows.size()); assertTrue("Returned row not a map", rows.get(0) instanceof Map<?, ?>); Map<?, ?> row = (Map<?, ?>) rows.get(0); assertEquals("Wrong id", 1, row.get("id")); assertEquals("Wrong status", 2, row.get("status")); }