Search in sources :

Example 1 with SqlParameter

use of org.springframework.jdbc.core.SqlParameter in project camel by apache.

the class ElsqlProducer method processStreamList.

protected void processStreamList(final Exchange exchange, final String sql, final SqlParameterSource param) throws Exception {
    // spring JDBC to parse the SQL and build the prepared statement creator
    // this is what NamedJdbcTemplate does internally
    final ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
    final String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, param);
    final Object[] params = NamedParameterUtils.buildValueArray(parsedSql, param, null);
    final List<SqlParameter> declaredParameters = NamedParameterUtils.buildSqlParameterList(parsedSql, param);
    final PreparedStatementCreatorFactory pscf = new PreparedStatementCreatorFactory(sqlToUse, declaredParameters);
    final PreparedStatementCreator statementCreator = pscf.newPreparedStatementCreator(params);
    processStreamList(exchange, statementCreator, sqlToUse);
}
Also used : SqlParameter(org.springframework.jdbc.core.SqlParameter) PreparedStatementCreator(org.springframework.jdbc.core.PreparedStatementCreator) ParsedSql(org.springframework.jdbc.core.namedparam.ParsedSql) PreparedStatementCreatorFactory(org.springframework.jdbc.core.PreparedStatementCreatorFactory)

Example 2 with SqlParameter

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

the class NamedParameterJdbcTemplate method getPreparedStatementCreator.

/**
	 * Build a PreparedStatementCreator based on the given SQL and named parameters.
	 * <p>Note: Not used for the {@code update} variant with generated key handling.
	 * @param sql SQL to execute
	 * @param paramSource container of arguments to bind
	 * @return the corresponding PreparedStatementCreator
	 */
protected PreparedStatementCreator getPreparedStatementCreator(String sql, SqlParameterSource paramSource) {
    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);
    return pscf.newPreparedStatementCreator(params);
}
Also used : SqlParameter(org.springframework.jdbc.core.SqlParameter) PreparedStatementCreatorFactory(org.springframework.jdbc.core.PreparedStatementCreatorFactory)

Example 3 with SqlParameter

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

the class NamedParameterUtils method buildValueArray.

/**
	 * Convert a Map of named parameter values to a corresponding array.
	 * @param parsedSql the parsed SQL statement
	 * @param paramSource the source for named parameters
	 * @param declaredParams the List of declared SqlParameter objects
	 * (may be {@code null}). If specified, the parameter metadata will
	 * be built into the value array in the form of SqlParameterValue objects.
	 * @return the array of values
	 */
public static Object[] buildValueArray(ParsedSql parsedSql, SqlParameterSource paramSource, List<SqlParameter> declaredParams) {
    Object[] paramArray = new Object[parsedSql.getTotalParameterCount()];
    if (parsedSql.getNamedParameterCount() > 0 && parsedSql.getUnnamedParameterCount() > 0) {
        throw new InvalidDataAccessApiUsageException("Not allowed to mix named and traditional ? placeholders. You have " + parsedSql.getNamedParameterCount() + " named parameter(s) and " + parsedSql.getUnnamedParameterCount() + " traditional placeholder(s) in statement: " + parsedSql.getOriginalSql());
    }
    List<String> paramNames = parsedSql.getParameterNames();
    for (int i = 0; i < paramNames.size(); i++) {
        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);
        } catch (IllegalArgumentException ex) {
            throw new InvalidDataAccessApiUsageException("No value supplied for the SQL parameter '" + paramName + "': " + ex.getMessage());
        }
    }
    return paramArray;
}
Also used : SqlParameter(org.springframework.jdbc.core.SqlParameter) SqlParameterValue(org.springframework.jdbc.core.SqlParameterValue) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException)

Example 4 with SqlParameter

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

the class AbstractJdbcCall method executeCallInternal.

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

Example 5 with SqlParameter

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

the class AbstractJdbcCall method compileInternal.

/**
	 * Delegate method to perform the actual compilation.
	 * <p>Subclasses can override this template method to perform their own compilation.
	 * Invoked after this base class's compilation is complete.
	 */
protected void compileInternal() {
    this.callMetaDataContext.initializeMetaData(getJdbcTemplate().getDataSource());
    // Iterate over the declared RowMappers and register the corresponding SqlParameter
    for (Map.Entry<String, RowMapper<?>> entry : this.declaredRowMappers.entrySet()) {
        SqlParameter resultSetParameter = this.callMetaDataContext.createReturnResultSetParameter(entry.getKey(), entry.getValue());
        this.declaredParameters.add(resultSetParameter);
    }
    this.callMetaDataContext.processParameters(this.declaredParameters);
    this.callString = this.callMetaDataContext.createCallString();
    if (logger.isDebugEnabled()) {
        logger.debug("Compiled stored procedure. Call string is [" + this.callString + "]");
    }
    this.callableStatementFactory = new CallableStatementCreatorFactory(getCallString(), this.callMetaDataContext.getCallParameters());
    onCompileInternal();
}
Also used : SqlParameter(org.springframework.jdbc.core.SqlParameter) CallableStatementCreatorFactory(org.springframework.jdbc.core.CallableStatementCreatorFactory) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) RowMapper(org.springframework.jdbc.core.RowMapper)

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