/** * Create a wrapped value if parameter has type information, plain object if not. * @param source the source of parameter values and type information * @param parameterName the name of the parameter * @return the value object */ @Nullable public static Object getTypedValue(SqlParameterSource source, String parameterName) { int sqlType = source.getSqlType(parameterName); if (sqlType != SqlParameterSource.TYPE_UNKNOWN) { if (source.getTypeName(parameterName) != null) { return new SqlParameterValue(sqlType, source.getTypeName(parameterName), source.getValue(parameterName)); } else { return new SqlParameterValue(sqlType, source.getValue(parameterName)); } } else { return source.getValue(parameterName); } }
actualSql.append(originalSql, lastIndex, startIndex); if (paramSource != null && paramSource.hasValue(paramName)) { Object value = paramSource.getValue(paramName); if (value instanceof SqlParameterValue) { value = ((SqlParameterValue) value).getValue();
String paramName = paramNames.get(i); try { Object value = paramSource.getValue(paramName); SqlParameter param = findParameter(declaredParams, paramName, i); paramArray[i] = (param != null ? new SqlParameterValue(param, value) : value);
@SuppressWarnings({ "rawtypes", "serial", "unchecked" }) @Test public void testWriteAndFlushMapWithItemSqlParameterSourceProvider() throws Exception { JdbcBatchItemWriter<Map<String, Object>> mapWriter = new JdbcBatchItemWriter<>(); mapWriter.setSql(sql); mapWriter.setJdbcTemplate(namedParameterJdbcOperations); mapWriter.setItemSqlParameterSourceProvider(new ItemSqlParameterSourceProvider<Map<String, Object>>() { @Override public SqlParameterSource createSqlParameterSource(Map<String, Object> item) { return new MapSqlParameterSource(item); } }); mapWriter.afterPropertiesSet(); ArgumentCaptor<SqlParameterSource []> captor = ArgumentCaptor.forClass(SqlParameterSource[].class); when(namedParameterJdbcOperations.batchUpdate(any(String.class), captor.capture())) .thenReturn(new int[] {1}); mapWriter.write(Collections.singletonList(new HashMap<String, Object>() {{put("foo", "bar");}})); assertEquals(1, captor.getValue().length); SqlParameterSource results = captor.getValue()[0]; assertEquals("bar", results.getValue("foo")); }
@Test public void testListOfMapsInputWithExpression() throws Exception { factory.setParameterExpressions(Collections.singletonMap("spam", "foo.toUpperCase()")); factory.setBeanFactory(mock(BeanFactory.class)); factory.afterPropertiesSet(); 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()); assertEquals(JdbcUtils.TYPE_UNKNOWN, source.getSqlType("foo")); }
@Test public void testListOfMapsInputWithExpressionAndTypes() throws Exception { factory.setParameterExpressions(Collections.singletonMap("spam", "foo.toUpperCase()")); factory.setBeanFactory(mock(BeanFactory.class)); factory.setSqlParameterTypes(Collections.singletonMap("spam", Types.SQLXML)); factory.afterPropertiesSet(); 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()); assertEquals(Types.SQLXML, source.getSqlType("spam")); }
@Test public void testListOfMapsInput() throws Exception { factory.setBeanFactory(mock(BeanFactory.class)); factory.afterPropertiesSet(); SqlParameterSource source = factory.createParameterSource(Arrays.asList(Collections.singletonMap("foo", "bar"), Collections.singletonMap("foo", "bucket"))); String expression = "foo"; assertTrue(source.hasValue(expression)); assertEquals("[bar, bucket]", source.getValue(expression).toString()); assertEquals(JdbcUtils.TYPE_UNKNOWN, source.getSqlType(expression)); }
@Test public void testListOfMapsInputWithExpressionAndEmptyTypes() throws Exception { factory.setParameterExpressions(Collections.singletonMap("spam", "foo.toUpperCase()")); factory.setBeanFactory(mock(BeanFactory.class)); factory.setSqlParameterTypes(Collections.emptyMap()); factory.afterPropertiesSet(); 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()); assertEquals(JdbcUtils.TYPE_UNKNOWN, source.getSqlType("spam")); }
@Test public void testMapInputWithMappedExpressionResolveStatic() throws Exception { factory.setParameterExpressions(Collections.singletonMap("spam", "#staticParameters['foo'].toUpperCase()")); factory.setStaticParameters(Collections.singletonMap("foo", "bar")); factory.setBeanFactory(mock(BeanFactory.class)); factory.afterPropertiesSet(); SqlParameterSource source = factory.createParameterSource(Collections.singletonMap("crap", "bucket")); assertTrue(source.hasValue("spam")); assertEquals("BAR", source.getValue("spam")); assertEquals(JdbcUtils.TYPE_UNKNOWN, source.getSqlType("spam")); }
@Test public void testMapInputWithMappedExpression() throws Exception { factory.setParameterExpressions(Collections.singletonMap("spam", "foo.toUpperCase()")); factory.setBeanFactory(mock(BeanFactory.class)); factory.afterPropertiesSet(); SqlParameterSource source = factory.createParameterSource(Collections.singletonMap("foo", "bar")); assertTrue(source.hasValue("spam")); assertEquals("BAR", source.getValue("spam")); assertEquals(JdbcUtils.TYPE_UNKNOWN, source.getSqlType("spam")); }
@Test public void testMapInput() throws Exception { factory.setBeanFactory(mock(BeanFactory.class)); factory.afterPropertiesSet(); SqlParameterSource source = factory.createParameterSource(Collections.singletonMap("foo", "bar")); assertTrue(source.hasValue("foo")); assertEquals("bar", source.getValue("foo")); assertEquals(JdbcUtils.TYPE_UNKNOWN, source.getSqlType("foo")); }
@Test public void testMapInputWithExpression() throws Exception { factory.setBeanFactory(mock(BeanFactory.class)); factory.afterPropertiesSet(); 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()")); assertEquals(JdbcUtils.TYPE_UNKNOWN, source.getSqlType("food")); }
@Test public void testSetStaticParameters() throws Exception { factory.setStaticParameters(Collections.singletonMap("foo", "bar")); factory.setBeanFactory(mock(BeanFactory.class)); factory.afterPropertiesSet(); SqlParameterSource source = factory.createParameterSource(null); assertTrue(source.hasValue("foo")); assertEquals("bar", source.getValue("foo")); assertEquals(JdbcUtils.TYPE_UNKNOWN, source.getSqlType("foo")); }
@Override public Object get(String variable) { return (_source.hasValue(variable) ? _source.getValue(variable) : null); }
/** * Create a wrapped value if parameter has type information, plain object if not. * @param source the source of paramer values and type information * @param parameterName the name of the parameter * @return the value object */ public static Object getTypedValue(SqlParameterSource source, String parameterName) { int sqlType = source.getSqlType(parameterName); if (sqlType != SqlParameterSource.TYPE_UNKNOWN) { if (source.getTypeName(parameterName) != null) { return new SqlParameterValue(sqlType, source.getTypeName(parameterName), source.getValue(parameterName)); } else { return new SqlParameterValue(sqlType, source.getValue(parameterName)); } } else { return source.getValue(parameterName); } }
actualSql.append(originalSql, lastIndex, startIndex); if (paramSource != null && paramSource.hasValue(paramName)) { Object value = paramSource.getValue(paramName); if (value instanceof SqlParameterValue) { value = ((SqlParameterValue) value).getValue();
actualSql.append(originalSql.substring(lastIndex, startIndex)); if (paramSource != null && paramSource.hasValue(paramName)) { Object value = paramSource.getValue(paramName); if (value instanceof SqlParameterValue) { value = ((SqlParameterValue) value).getValue();
String paramName = paramNames.get(i); try { Object value = paramSource.getValue(paramName); SqlParameter param = findParameter(declaredParams, paramName, i); paramArray[i] = (param != null ? new SqlParameterValue(param, value) : value);
String paramName = paramNames.get(i); try { Object value = paramSource.getValue(paramName); SqlParameter param = findParameter(declaredParams, paramName, i); paramArray[i] = (param != null ? new SqlParameterValue(param, value) : value);