Example usage for org.springframework.jdbc.core.namedparam MapSqlParameterSource addValue

List of usage examples for org.springframework.jdbc.core.namedparam MapSqlParameterSource addValue

Introduction

In this page you can find the example usage for org.springframework.jdbc.core.namedparam MapSqlParameterSource addValue.

Prototype

public MapSqlParameterSource addValue(String paramName, @Nullable Object value) 

Source Link

Document

Add a parameter to this parameter source.

Usage

From source file:org.springframework.batch.integration.samples.payments.PaymentWriter.java

@Override
public void write(List<? extends Payment> payments) throws Exception {
    for (Payment payment : payments) {
        MapSqlParameterSource parameterSource = new MapSqlParameterSource();
        parameterSource.addValue("RECIPIENT", payment.getDestinationAccountNo())
                .addValue("PAYEE", payment.getSourceAccountNo()).addValue("AMOUNT", payment.getAmount())
                .addValue("DATE", payment.getDate());
        accountUpdate.update("UPDATE ACCOUNTS SET BALANCE = BALANCE + ? WHERE ID = ?", payment.getAmount(),
                payment.getDestinationAccountNo());
        accountUpdate.update("UPDATE ACCOUNTS SET BALANCE = BALANCE - ? WHERE ID = ?", payment.getAmount(),
                payment.getSourceAccountNo());
        paymentInsert.execute(parameterSource);
        logger.info("Executing step: " + payment);
    }/*from w w  w  .  j  a  v a 2 s .com*/
}

From source file:org.springframework.integration.jdbc.store.JdbcChannelMessageStore.java

/**
 * This method executes a call to the DB to get the oldest Message in the
 * MessageGroup which in the context of the {@link JdbcChannelMessageStore}
 * means the channel identifier./*from  w  w w .  j a v a  2s.  com*/
 *
 * @param groupIdKey String representation of message group (Channel) ID
 * @return a message; could be null if query produced no Messages
 */
protected Message<?> doPollForMessage(String groupIdKey) {

    final NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate);
    final MapSqlParameterSource parameters = new MapSqlParameterSource();

    parameters.addValue("region", region);
    parameters.addValue("group_key", groupIdKey);

    final String query;

    final List<Message<?>> messages;

    this.idCacheReadLock.lock();
    try {
        if (this.usingIdCache && !this.idCache.isEmpty()) {
            query = getQuery(this.channelMessageStoreQueryProvider.getPollFromGroupExcludeIdsQuery());
            parameters.addValue("message_ids", idCache);
        } else {
            query = getQuery(this.channelMessageStoreQueryProvider.getPollFromGroupQuery());
        }
        messages = namedParameterJdbcTemplate.query(query, parameters, messageRowMapper);
    } finally {
        this.idCacheReadLock.unlock();
    }

    Assert.isTrue(messages.size() == 0 || messages.size() == 1);
    if (messages.size() > 0) {

        final Message<?> message = messages.get(0);
        final String messageId = message.getHeaders().getId().toString();

        if (this.usingIdCache) {
            this.idCacheWriteLock.lock();
            try {
                boolean added = this.idCache.add(messageId);

                if (logger.isDebugEnabled()) {
                    logger.debug(String.format("Polled message with id '%s' added: '%s'.", messageId, added));
                }
            } finally {
                this.idCacheWriteLock.unlock();
            }
        }

        return message;
    }
    return null;
}

From source file:org.springframework.jdbc.core.simple.SimpleJdbcTemplateTests.java

public void testQueryForListWithSqlParameterSource() throws Exception {
    MapSqlParameterSource args = new MapSqlParameterSource();
    args.addValue("1", 1);
    args.addValue("2", 2);
    args.addValue("3", 3);
    testDelegation("queryForList", new Object[] { "sql" }, new Object[] { args }, new LinkedList());
}

From source file:org.springframework.jdbc.core.simple.SimpleJdbcTemplateTests.java

public void testQueryForMapWithSqlParameterSource() throws Exception {
    MapSqlParameterSource args = new MapSqlParameterSource();
    args.addValue("1", 1);
    args.addValue("2", 2);
    args.addValue("3", 3);
    testDelegation("queryForMap", new Object[] { "sql" }, new Object[] { args }, new HashMap());
}

From source file:org.springframework.jdbc.core.simple.SimpleJdbcTemplateTests.java

public void testUpdateWithSqlParameterSource() throws Exception {
    MapSqlParameterSource args = new MapSqlParameterSource();
    args.addValue("1", 1);
    args.addValue("2", 2);
    args.addValue("3", 3);
    testDelegation("update", new Object[] { "sql" }, new Object[] { args }, 666);
}

From source file:rapture.repo.jdbc.JDBCStructuredStore.java

@Override
public StoredProcedureResponse callProcedure(CallingContext context, String procName,
        StoredProcedureParams params) {//w  w w .  j  av  a  2 s .  co m

    // TODO RAP-3548 Need to check entitlements

    SimpleJdbcCall call = new SimpleJdbcCall(jdbc).withProcedureName(procName)
            .withoutProcedureColumnMetaDataAccess();
    MapSqlParameterSource paramSource = new MapSqlParameterSource();

    Map<String, Object> inParams = (params == null) ? null : params.getInParams();
    Map<String, Integer> outParams = (params == null) ? null : params.getOutParams();
    Map<String, Object> inOutParams = (params == null) ? null : params.getInOutParams();

    if (inParams != null) {
        // Declare Parameters
        Map<String, Integer> inParamTypes = getInputParamTypes(inParams);
        for (Map.Entry<String, Integer> entry : inParamTypes.entrySet()) {
            call.declareParameters(new SqlParameter(entry.getKey(), entry.getValue()));
        }

        // Give Input Parameters
        for (Map.Entry<String, Object> entry : inParams.entrySet()) {
            paramSource.addValue(entry.getKey(), entry.getValue());
        }
    }

    if (inOutParams != null) {
        Map<String, Integer> inOutParamTypes = getInputParamTypes(inOutParams);
        for (Map.Entry<String, Integer> entry : inOutParamTypes.entrySet()) {
            call.declareParameters(new SqlInOutParameter(entry.getKey(), entry.getValue()));
        }

        // Give Input Parameters
        for (Map.Entry<String, Object> entry : inOutParams.entrySet()) {
            paramSource.addValue(entry.getKey(), entry.getValue());
        }
    }

    if (outParams != null) {
        for (Map.Entry<String, Integer> entry : outParams.entrySet()) {
            call.declareParameters(new SqlOutParameter(entry.getKey(), entry.getValue()));
        }
    }

    try {
        return packageStoredProcedureReturn(call.execute(paramSource), true);
    } catch (BadSqlGrammarException e) {
        log.error(e.getSQLException());
        return packageStoredProcedureReturn(null, false);
    }

}