/** * 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); } }
/** * Create a Map of case insensitive parameter names together with the original name. * @param parameterSource the source of parameter names * @return the Map that can be used for case insensitive matching of parameter names */ public static Map<String, String> extractCaseInsensitiveParameterNames(SqlParameterSource parameterSource) { Map<String, String> caseInsensitiveParameterNames = new HashMap<>(); String[] paramNames = parameterSource.getParameterNames(); if (paramNames != null) { for (String name : paramNames) { caseInsensitiveParameterNames.put(name.toLowerCase(), name); } } return caseInsensitiveParameterNames; }
/** * Convert parameter declarations from an SqlParameterSource to a corresponding List of SqlParameters. * This is necessary in order to reuse existing methods on JdbcTemplate. * The SqlParameter for a named parameter is placed in the correct position in the * resulting list based on the parsed SQL statement info. * @param parsedSql the parsed SQL statement * @param paramSource the source for named parameters */ public static List<SqlParameter> buildSqlParameterList(ParsedSql parsedSql, SqlParameterSource paramSource) { List<String> paramNames = parsedSql.getParameterNames(); List<SqlParameter> params = new ArrayList<>(paramNames.size()); for (String paramName : paramNames) { params.add(new SqlParameter( paramName, paramSource.getSqlType(paramName), paramSource.getTypeName(paramName))); } return params; }
@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")); }
SqlParameterSourceUtils.extractCaseInsensitiveParameterNames(parameterSource); for (String column : this.tableColumns) { if (parameterSource.hasValue(column)) { values.add(SqlParameterSourceUtils.getTypedValue(parameterSource, column)); if (parameterSource.hasValue(lowerCaseName)) { values.add(SqlParameterSourceUtils.getTypedValue(parameterSource, lowerCaseName)); if (parameterSource.hasValue(propertyName)) { values.add(SqlParameterSourceUtils.getTypedValue(parameterSource, propertyName));
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);
/** * Convert parameter types from an SqlParameterSource into a corresponding int array. * This is necessary in order to reuse existing methods on JdbcTemplate. * Any named parameter types are placed in the correct position in the * Object array based on the parsed SQL statement info. * @param parsedSql the parsed SQL statement * @param paramSource the source for named parameters */ public static int[] buildSqlTypeArray(ParsedSql parsedSql, SqlParameterSource paramSource) { int[] sqlTypes = new int[parsedSql.getTotalParameterCount()]; List<String> paramNames = parsedSql.getParameterNames(); for (int i = 0; i < paramNames.size(); i++) { String paramName = paramNames.get(i); sqlTypes[i] = paramSource.getSqlType(paramName); } return sqlTypes; }
@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)); }
/** * Convert parameter declarations from an SqlParameterSource to a corresponding List of SqlParameters. * This is necessary in order to reuse existing methods on JdbcTemplate. * The SqlParameter for a named parameter is placed in the correct position in the * resulting list based on the parsed SQL statement info. * @param parsedSql the parsed SQL statement * @param paramSource the source for named parameters */ public static List<SqlParameter> buildSqlParameterList(ParsedSql parsedSql, SqlParameterSource paramSource) { List<String> paramNames = parsedSql.getParameterNames(); List<SqlParameter> params = new LinkedList<SqlParameter>(); for (String paramName : paramNames) { params.add(new SqlParameter(paramName, paramSource.getSqlType(paramName), paramSource.getTypeName(paramName))); } return params; }
if (parameterSource.hasValue(parameterName)) { matchedParameters.put(parameterName, SqlParameterSourceUtils.getTypedValue(parameterSource, parameterName)); if (parameterSource.hasValue(lowerCaseName)) { matchedParameters.put(parameterName, SqlParameterSourceUtils.getTypedValue(parameterSource, lowerCaseName)); if (parameterSource.hasValue(englishLowerCaseName)) { matchedParameters.put(parameterName, SqlParameterSourceUtils.getTypedValue(parameterSource, englishLowerCaseName)); if (parameterSource.hasValue(propertyName)) { matchedParameters.put(parameterName, SqlParameterSourceUtils.getTypedValue(parameterSource, propertyName));
@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")); }
/** * 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); } }
@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")); }
/** * 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); } }
/** * Convert parameter declarations from an SqlParameterSource to a corresponding List of SqlParameters. * This is necessary in order to reuse existing methods on JdbcTemplate. * The SqlParameter for a named parameter is placed in the correct position in the * resulting list based on the parsed SQL statement info. * @param parsedSql the parsed SQL statement * @param paramSource the source for named parameters */ public static List<SqlParameter> buildSqlParameterList(ParsedSql parsedSql, SqlParameterSource paramSource) { List<String> paramNames = parsedSql.getParameterNames(); List<SqlParameter> params = new ArrayList<>(paramNames.size()); for (String paramName : paramNames) { params.add(new SqlParameter( paramName, paramSource.getSqlType(paramName), paramSource.getTypeName(paramName))); } return params; }
@Override public boolean contains(String variable) { return _source.hasValue(variable); }
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);
/** * Convert parameter types from an SqlParameterSource into a corresponding int array. * This is necessary in order to reuse existing methods on JdbcTemplate. * Any named parameter types are placed in the correct position in the * Object array based on the parsed SQL statement info. * @param parsedSql the parsed SQL statement * @param paramSource the source for named parameters */ public static int[] buildSqlTypeArray(ParsedSql parsedSql, SqlParameterSource paramSource) { int[] sqlTypes = new int[parsedSql.getTotalParameterCount()]; List<String> paramNames = parsedSql.getParameterNames(); for (int i = 0; i < paramNames.size(); i++) { String paramName = paramNames.get(i); sqlTypes[i] = paramSource.getSqlType(paramName); } return sqlTypes; }