Search in sources :

Example 51 with SqlParameter

use of org.springframework.jdbc.core.SqlParameter in project spring-framework by spring-projects.

the class RdbmsOperation method validateNamedParameters.

/**
	 * 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<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);
}
Also used : SqlParameter(org.springframework.jdbc.core.SqlParameter) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException)

Example 52 with SqlParameter

use of org.springframework.jdbc.core.SqlParameter in project spring-framework by spring-projects.

the class SqlCall method compileInternal.

/**
	 * Overridden method to configure the CallableStatementCreatorFactory
	 * based on our declared parameters.
	 * @see RdbmsOperation#compileInternal()
	 */
@Override
protected final void compileInternal() {
    if (isSqlReadyForUse()) {
        this.callString = getSql();
    } else {
        List<SqlParameter> parameters = getDeclaredParameters();
        int parameterCount = 0;
        if (isFunction()) {
            this.callString = "{? = call " + getSql() + "(";
            parameterCount = -1;
        } else {
            this.callString = "{call " + getSql() + "(";
        }
        for (SqlParameter parameter : parameters) {
            if (!(parameter.isResultsParameter())) {
                if (parameterCount > 0) {
                    this.callString += ", ";
                }
                if (parameterCount >= 0) {
                    this.callString += "?";
                }
                parameterCount++;
            }
        }
        this.callString += ")}";
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Compiled stored procedure. Call string is [" + getCallString() + "]");
    }
    this.callableStatementFactory = new CallableStatementCreatorFactory(getCallString(), getDeclaredParameters());
    this.callableStatementFactory.setResultSetType(getResultSetType());
    this.callableStatementFactory.setUpdatableResults(isUpdatableResults());
    onCompileInternal();
}
Also used : SqlParameter(org.springframework.jdbc.core.SqlParameter) CallableStatementCreatorFactory(org.springframework.jdbc.core.CallableStatementCreatorFactory)

Example 53 with SqlParameter

use of org.springframework.jdbc.core.SqlParameter in project spring-framework by spring-projects.

the class NamedParameterJdbcTemplate method update.

@Override
public int update(String sql, SqlParameterSource paramSource, KeyHolder generatedKeyHolder, String[] keyColumnNames) throws DataAccessException {
    ParsedSql parsedSql = getParsedSql(sql);
    String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, paramSource);
    Object[] params = NamedParameterUtils.buildValueArray(parsedSql, paramSource, null);
    List<SqlParameter> declaredParameters = NamedParameterUtils.buildSqlParameterList(parsedSql, paramSource);
    PreparedStatementCreatorFactory pscf = new PreparedStatementCreatorFactory(sqlToUse, declaredParameters);
    if (keyColumnNames != null) {
        pscf.setGeneratedKeysColumnNames(keyColumnNames);
    } else {
        pscf.setReturnGeneratedKeys(true);
    }
    return getJdbcOperations().update(pscf.newPreparedStatementCreator(params), generatedKeyHolder);
}
Also used : SqlParameter(org.springframework.jdbc.core.SqlParameter) PreparedStatementCreatorFactory(org.springframework.jdbc.core.PreparedStatementCreatorFactory)

Example 54 with SqlParameter

use of org.springframework.jdbc.core.SqlParameter in project spring-framework by spring-projects.

the class NamedParameterUtils method buildSqlParameterList.

/**
	 * 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<>();
    for (String paramName : paramNames) {
        params.add(new SqlParameter(paramName, paramSource.getSqlType(paramName), paramSource.getTypeName(paramName)));
    }
    return params;
}
Also used : SqlParameter(org.springframework.jdbc.core.SqlParameter) LinkedList(java.util.LinkedList)

Example 55 with SqlParameter

use of org.springframework.jdbc.core.SqlParameter in project spring-framework by spring-projects.

the class StoredProcedure method execute.

/**
	 * Execute the stored procedure with the provided parameter values. This is
	 * a convenience method where the order of the passed in parameter values
	 * must match the order that the parameters where declared in.
	 * @param inParams variable number of input parameters. Output parameters should
	 * not be included in this map.
	 * It is legal for values to be {@code null}, and this will produce the
	 * correct behavior using a NULL argument to the stored procedure.
	 * @return map of output params, keyed by name as in parameter declarations.
	 * Output parameters will appear here, with their values after the
	 * stored procedure has been called.
	 */
public Map<String, Object> execute(Object... inParams) {
    Map<String, Object> paramsToUse = new HashMap<>();
    validateParameters(inParams);
    int i = 0;
    for (SqlParameter sqlParameter : getDeclaredParameters()) {
        if (sqlParameter.isInputValueProvided()) {
            if (i < inParams.length) {
                paramsToUse.put(sqlParameter.getName(), inParams[i++]);
            }
        }
    }
    return getJdbcTemplate().call(newCallableStatementCreator(paramsToUse), getDeclaredParameters());
}
Also used : SqlParameter(org.springframework.jdbc.core.SqlParameter) HashMap(java.util.HashMap)

Aggregations

SqlParameter (org.springframework.jdbc.core.SqlParameter)66 BatchSqlUpdate (org.springframework.jdbc.object.BatchSqlUpdate)23 Test (org.junit.Test)22 ResultSet (java.sql.ResultSet)15 DataSource (javax.sql.DataSource)15 HashMap (java.util.HashMap)12 Customer (org.springframework.jdbc.Customer)12 SqlOutParameter (org.springframework.jdbc.core.SqlOutParameter)6 LinkedHashMap (java.util.LinkedHashMap)5 ArrayList (java.util.ArrayList)4 InvalidDataAccessApiUsageException (org.springframework.dao.InvalidDataAccessApiUsageException)4 File (com.github.hakko.musiccabinet.domain.model.library.File)3 PreparedStatementCreatorFactory (org.springframework.jdbc.core.PreparedStatementCreatorFactory)3 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)3 Track (com.github.hakko.musiccabinet.domain.model.music.Track)2 PreparedStatement (java.sql.PreparedStatement)2 Map (java.util.Map)2 CallableStatementCreatorFactory (org.springframework.jdbc.core.CallableStatementCreatorFactory)2 SqlParameterValue (org.springframework.jdbc.core.SqlParameterValue)2 DriverManagerDataSource (org.springframework.jdbc.datasource.DriverManagerDataSource)2