/** * 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; }
/** * 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; }
/** * 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 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 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 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 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")); }
/** * 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); } }
/** * 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; }
/** * 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; }
/** * 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; }
/** * 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; }
/** * 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; }
/** * 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) { SqlParameter param = new SqlParameter( paramName, paramSource.getSqlType(paramName), paramSource.getTypeName(paramName)); params.add(param); } return params; }
/** * 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); } }