/** * Set the value for a parameter. The method used is based on the SQL type * of the parameter and we can handle complex types like arrays and LOBs. * @param ps the prepared statement or callable statement * @param paramIndex index of the parameter we are setting * @param param the parameter as it is declared including type * @param inValue the value to set * @throws SQLException if thrown by PreparedStatement methods */ public static void setParameterValue(PreparedStatement ps, int paramIndex, SqlParameter param, @Nullable Object inValue) throws SQLException { setParameterValueInternal(ps, paramIndex, param.getSqlType(), param.getTypeName(), param.getScale(), inValue); }
/** * Validate the parameters passed to an execute method based on declared parameters. * Subclasses should invoke this method before every {@code executeQuery()} * or {@code update()} method. * @param parameters parameters supplied (may be {@code null}) * @throws InvalidDataAccessApiUsageException if the parameters are invalid */ protected void validateParameters(@Nullable Object[] parameters) throws InvalidDataAccessApiUsageException { checkCompiled(); int declaredInParameters = 0; for (SqlParameter param : this.declaredParameters) { if (param.isInputValueProvided()) { if (!supportsLobParameters() && (param.getSqlType() == Types.BLOB || param.getSqlType() == Types.CLOB)) { throw new InvalidDataAccessApiUsageException( "BLOB or CLOB parameters are not allowed for this kind of operation"); } declaredInParameters++; } } validateParameterCount((parameters != null ? parameters.length : 0), declaredInParameters); }
/** * Validate the named parameters passed to an execute method based on declared parameters. * Subclasses should invoke this method before every {@code executeQuery()} or * {@code update()} method. * @param parameters parameter Map supplied (may be {@code null}) * @throws InvalidDataAccessApiUsageException if the parameters are invalid */ protected void validateNamedParameters(@Nullable Map<String, ?> parameters) throws InvalidDataAccessApiUsageException { checkCompiled(); Map<String, ?> paramsToUse = (parameters != null ? parameters : Collections.<String, Object> emptyMap()); int declaredInParameters = 0; for (SqlParameter param : this.declaredParameters) { if (param.isInputValueProvided()) { if (!supportsLobParameters() && (param.getSqlType() == Types.BLOB || param.getSqlType() == Types.CLOB)) { throw new InvalidDataAccessApiUsageException( "BLOB or CLOB parameters are not allowed for this kind of operation"); } if (param.getName() != null && !paramsToUse.containsKey(param.getName())) { throw new InvalidDataAccessApiUsageException("The parameter named '" + param.getName() + "' was not among the parameters supplied: " + paramsToUse.keySet()); } declaredInParameters++; } } validateParameterCount(paramsToUse.size(), declaredInParameters); }
if (in instanceof Collection && declaredParameter.getSqlType() != Types.ARRAY) { Collection<?> entries = (Collection<?>) in; for (Object entry : entries) {
/** * Delegate method to perform the actual call processing. */ private Map<String, Object> executeCallInternal(Map<String, ?> args) { CallableStatementCreator csc = getCallableStatementFactory().newCallableStatementCreator(args); if (logger.isDebugEnabled()) { logger.debug("The following parameters are used for call " + getCallString() + " with " + args); int i = 1; for (SqlParameter param : getCallParameters()) { logger.debug(i + ": " + param.getName() + ", SQL type "+ param.getSqlType() + ", type name " + param.getTypeName() + ", parameter class [" + param.getClass().getName() + "]"); i++; } } return getJdbcTemplate().call(csc, getCallParameters()); }
cs.registerOutParameter(sqlColIndx, declaredParam.getSqlType(), declaredParam.getTypeName()); cs.registerOutParameter(sqlColIndx, declaredParam.getSqlType(), declaredParam.getScale()); cs.registerOutParameter(sqlColIndx, declaredParam.getSqlType());
if (paramName == null) { throw new IllegalArgumentException("Anonymous parameters not supported for calls - " + "please specify a name for the parameter of SQL type " + param.getSqlType());
if (function) { if (cursorParameter instanceof SqlOutParameter) { cursorSqlType = cursorParameter.getSqlType(); cursorSqlType = parameters[refCursorPosition - 1].getSqlType();
@Test public void convertTypeMapToSqlParameterList() { MapSqlParameterSource namedParams = new MapSqlParameterSource(); namedParams.addValue("a", "a", 1).addValue("b", "b", 2).addValue("c", "c", 3, "SQL_TYPE"); assertSame(3, NamedParameterUtils .buildSqlParameterList(NamedParameterUtils.parseSqlStatement("xxx :a :b :c"), namedParams).size()); assertSame(5, NamedParameterUtils .buildSqlParameterList(NamedParameterUtils.parseSqlStatement("xxx :a :b :c xx :a :b"), namedParams).size()); assertSame(5, NamedParameterUtils .buildSqlParameterList(NamedParameterUtils.parseSqlStatement("xxx :a :a :a xx :a :a"), namedParams).size()); assertEquals(2, NamedParameterUtils .buildSqlParameterList(NamedParameterUtils.parseSqlStatement("xxx :a :b :c xx :a :b"), namedParams).get(4).getSqlType()); assertEquals("SQL_TYPE", NamedParameterUtils .buildSqlParameterList(NamedParameterUtils.parseSqlStatement("xxx :a :b :c"), namedParams).get(2).getTypeName()); }
assertNull("Expect that the scale is null.", parameter4.getScale()); assertEquals("SqlType is ", Types.VARCHAR, parameter1.getSqlType()); assertEquals("SqlType is ", Types.VARCHAR, parameter2.getSqlType()); assertEquals("SqlType is ", Types.INTEGER, parameter3.getSqlType()); assertEquals("SqlType is ", Types.VARCHAR, parameter4.getSqlType());
assertNull("Expect that the scale is null.", parameter4.getScale()); assertEquals("SqlType is ", Types.VARCHAR, parameter1.getSqlType()); assertEquals("SqlType is ", Types.VARCHAR, parameter2.getSqlType()); assertEquals("SqlType is ", Types.INTEGER, parameter3.getSqlType()); assertEquals("SqlType is ", Types.VARCHAR, parameter4.getSqlType());
assertNull("Expect that the scale is null.", parameter4.getScale()); assertEquals("SqlType is ", Types.VARCHAR, parameter1.getSqlType()); assertEquals("SqlType is ", Types.VARCHAR, parameter2.getSqlType()); assertEquals("SqlType is ", Types.INTEGER, parameter3.getSqlType()); assertEquals("SqlType is ", Types.VARCHAR, parameter4.getSqlType());
/** * Execute the call using a query instead of a procedure call.<br> * <br> * The way to execute correctly is to use a {@link org.springframework.jdbc.core.PreparedStatementSetter} which * will set the parameters correctly, since using a PreparedStatementCreator doesn't seem to work well. The * setter simply sets the parameter using the correct {@link Types} constant indicating the actual call type. */ private Map<String, Object> executeCallInternal(final Map<String, Object> params) { Map<String, Object> result = new HashMap<>(1); result.put(returnedMapKey, getJdbcTemplate().query( generateSql(), ps -> { List<SqlParameter> callParameters = getCallParameters(); for (int i = 0; i < callParameters.size(); i++) { SqlParameter parameter = callParameters.get(i); ps.setObject(i + 1, params.get(parameter.getName()), parameter.getSqlType()); } }, rowMapper)); return result; }
/** * Set the value for a parameter. The method used is based on the SQL type * of the parameter and we can handle complex types like arrays and LOBs. * @param ps the prepared statement or callable statement * @param paramIndex index of the parameter we are setting * @param param the parameter as it is declared including type * @param inValue the value to set * @throws SQLException if thrown by PreparedStatement methods */ public static void setParameterValue( PreparedStatement ps, int paramIndex, SqlParameter param, Object inValue) throws SQLException { setParameterValueInternal(ps, paramIndex, param.getSqlType(), param.getTypeName(), param.getScale(), inValue); }
/** * Delegate method to perform the actual call processing. */ private Map<String, Object> executeCallInternal(Map<String, ?> args) { CallableStatementCreator csc = getCallableStatementFactory().newCallableStatementCreator(args); if (logger.isDebugEnabled()) { logger.debug("The following parameters are used for call " + getCallString() + " with " + args); int i = 1; for (SqlParameter param : getCallParameters()) { logger.debug(i + ": " + param.getName() + ", SQL type "+ param.getSqlType() + ", type name " + param.getTypeName() + ", parameter class [" + param.getClass().getName() + "]"); i++; } } return getJdbcTemplate().call(csc, getCallParameters()); }
/** * Validate the parameters passed to an execute method based on declared parameters. * Subclasses should invoke this method before every {@code executeQuery()} * or {@code update()} method. * @param parameters parameters supplied (may be {@code null}) * @throws InvalidDataAccessApiUsageException if the parameters are invalid */ protected void validateParameters(Object[] parameters) throws InvalidDataAccessApiUsageException { checkCompiled(); int declaredInParameters = 0; for (SqlParameter param : this.declaredParameters) { if (param.isInputValueProvided()) { if (!supportsLobParameters() && (param.getSqlType() == Types.BLOB || param.getSqlType() == Types.CLOB)) { throw new InvalidDataAccessApiUsageException( "BLOB or CLOB parameters are not allowed for this kind of operation"); } declaredInParameters++; } } validateParameterCount((parameters != null ? parameters.length : 0), declaredInParameters); }
/** * Method to perform the actual call processing */ private Map<String, Object> executeCallInternal(Map<String, ?> params) { CallableStatementCreator csc = getCallableStatementFactory().newCallableStatementCreator(params); if (logger.isDebugEnabled()) { logger.debug("The following parameters are used for call " + getCallString() + " with: " + params); int i = 1; for (SqlParameter p : getCallParameters()) { logger.debug(i++ + ": " + p.getName() + " SQL Type "+ p.getSqlType() + " Type Name " + p.getTypeName() + " " + p.getClass().getName()); } } return getJdbcTemplate().call(csc, getCallParameters()); }
if (in instanceof Collection && declaredParameter.getSqlType() != Types.ARRAY) { Collection<?> entries = (Collection<?>) in; for (Object entry : entries) {
/** * Validate the named parameters passed to an execute method based on declared parameters. * Subclasses should invoke this method before every {@code executeQuery()} or * {@code update()} method. * @param parameters parameter Map supplied. May be {@code null}. * @throws InvalidDataAccessApiUsageException if the parameters are invalid */ protected void validateNamedParameters(Map<String, ?> parameters) throws InvalidDataAccessApiUsageException { checkCompiled(); Map paramsToUse = (parameters != null ? parameters : Collections.emptyMap()); int declaredInParameters = 0; for (SqlParameter param : this.declaredParameters) { if (param.isInputValueProvided()) { if (!supportsLobParameters() && (param.getSqlType() == Types.BLOB || param.getSqlType() == Types.CLOB)) { throw new InvalidDataAccessApiUsageException( "BLOB or CLOB parameters are not allowed for this kind of operation"); } if (param.getName() != null && !paramsToUse.containsKey(param.getName())) { throw new InvalidDataAccessApiUsageException("The parameter named '" + param.getName() + "' was not among the parameters supplied: " + paramsToUse.keySet()); } declaredInParameters++; } } validateParameterCount(paramsToUse.size(), declaredInParameters); }
if (in instanceof Collection && declaredParameter.getSqlType() != Types.ARRAY) { Collection entries = (Collection) in; for (Object entry : entries) {