Search in sources :

Example 1 with MapSqlParameterSource

use of cn.taketoday.jdbc.core.namedparam.MapSqlParameterSource in project today-infrastructure by TAKETODAY.

the class SqlQuery method executeByNamedParam.

/**
 * Central execution method. All named parameter execution goes through this method.
 *
 * @param paramMap parameters associated with the name specified while declaring
 * the SqlParameters. Primitive parameters must be represented by their Object wrapper
 * type. The ordering of parameters is not significant since they are supplied in a
 * SqlParameterMap which is an implementation of the Map interface.
 * @param context the contextual information passed to the {@code mapRow}
 * callback method. The JDBC operation itself doesn't rely on this parameter,
 * but it can be useful for creating the objects of the result list.
 * @return a List of objects, one per row of the ResultSet. Normally all these
 * will be of the same class, although it is possible to use different types.
 */
public List<T> executeByNamedParam(Map<String, ?> paramMap, @Nullable Map<?, ?> context) throws DataAccessException {
    validateNamedParameters(paramMap);
    ParsedSql parsedSql = getParsedSql();
    MapSqlParameterSource paramSource = new MapSqlParameterSource(paramMap);
    String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, paramSource);
    Object[] params = NamedParameterUtils.buildValueArray(parsedSql, paramSource, getDeclaredParameters());
    RowMapper<T> rowMapper = newRowMapper(params, context);
    return getJdbcTemplate().query(newPreparedStatementCreator(sqlToUse, params), rowMapper);
}
Also used : MapSqlParameterSource(cn.taketoday.jdbc.core.namedparam.MapSqlParameterSource) ParsedSql(cn.taketoday.jdbc.core.namedparam.ParsedSql)

Example 2 with MapSqlParameterSource

use of cn.taketoday.jdbc.core.namedparam.MapSqlParameterSource in project today-infrastructure by TAKETODAY.

the class SqlUpdate method updateByNamedParam.

/**
 * Generic method to execute the update given named parameters.
 * All other update methods invoke this method.
 *
 * @param paramMap a Map of parameter name to parameter object,
 * matching named parameters specified in the SQL statement
 * @return the number of rows affected by the update
 */
public int updateByNamedParam(Map<String, ?> paramMap) throws DataAccessException {
    validateNamedParameters(paramMap);
    ParsedSql parsedSql = getParsedSql();
    MapSqlParameterSource paramSource = new MapSqlParameterSource(paramMap);
    String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, paramSource);
    Object[] params = NamedParameterUtils.buildValueArray(parsedSql, paramSource, getDeclaredParameters());
    int rowsAffected = getJdbcTemplate().update(newPreparedStatementCreator(sqlToUse, params));
    checkRowsAffected(rowsAffected);
    return rowsAffected;
}
Also used : MapSqlParameterSource(cn.taketoday.jdbc.core.namedparam.MapSqlParameterSource) ParsedSql(cn.taketoday.jdbc.core.namedparam.ParsedSql)

Example 3 with MapSqlParameterSource

use of cn.taketoday.jdbc.core.namedparam.MapSqlParameterSource in project today-infrastructure by TAKETODAY.

the class SqlUpdate method updateByNamedParam.

/**
 * Method to execute the update given arguments and
 * retrieve the generated keys using a KeyHolder.
 *
 * @param paramMap a Map of parameter name to parameter object,
 * matching named parameters specified in the SQL statement
 * @param generatedKeyHolder the KeyHolder that will hold the generated keys
 * @return the number of rows affected by the update
 */
public int updateByNamedParam(Map<String, ?> paramMap, KeyHolder generatedKeyHolder) throws DataAccessException {
    validateNamedParameters(paramMap);
    ParsedSql parsedSql = getParsedSql();
    MapSqlParameterSource paramSource = new MapSqlParameterSource(paramMap);
    String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, paramSource);
    Object[] params = NamedParameterUtils.buildValueArray(parsedSql, paramSource, getDeclaredParameters());
    int rowsAffected = getJdbcTemplate().update(newPreparedStatementCreator(sqlToUse, params), generatedKeyHolder);
    checkRowsAffected(rowsAffected);
    return rowsAffected;
}
Also used : MapSqlParameterSource(cn.taketoday.jdbc.core.namedparam.MapSqlParameterSource) ParsedSql(cn.taketoday.jdbc.core.namedparam.ParsedSql)

Example 4 with MapSqlParameterSource

use of cn.taketoday.jdbc.core.namedparam.MapSqlParameterSource in project today-infrastructure by TAKETODAY.

the class SimpleJdbcCallTests method addInvoiceProcWithoutMetaDataUsingMapParamSource.

@Test
void addInvoiceProcWithoutMetaDataUsingMapParamSource() throws Exception {
    initializeAddInvoiceWithoutMetaData(false);
    SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withProcedureName("add_invoice");
    adder.declareParameters(new SqlParameter("amount", Types.INTEGER), new SqlParameter("custid", Types.INTEGER), new SqlOutParameter("newid", Types.INTEGER));
    Number newId = adder.executeObject(Number.class, new MapSqlParameterSource().addValue("amount", 1103).addValue("custid", 3));
    assertThat(newId.intValue()).isEqualTo(4);
    verifyAddInvoiceWithoutMetaData(false);
    verify(connection, atLeastOnce()).close();
}
Also used : SqlParameter(cn.taketoday.jdbc.core.SqlParameter) MapSqlParameterSource(cn.taketoday.jdbc.core.namedparam.MapSqlParameterSource) SqlOutParameter(cn.taketoday.jdbc.core.SqlOutParameter) Test(org.junit.jupiter.api.Test)

Example 5 with MapSqlParameterSource

use of cn.taketoday.jdbc.core.namedparam.MapSqlParameterSource in project today-infrastructure by TAKETODAY.

the class SimpleJdbcCallTests method addInvoiceFuncWithoutMetaDataUsingMapParamSource.

@Test
void addInvoiceFuncWithoutMetaDataUsingMapParamSource() throws Exception {
    initializeAddInvoiceWithoutMetaData(true);
    SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withFunctionName("add_invoice");
    adder.declareParameters(new SqlOutParameter("return", Types.INTEGER), new SqlParameter("amount", Types.INTEGER), new SqlParameter("custid", Types.INTEGER));
    Number newId = adder.executeFunction(Number.class, new MapSqlParameterSource().addValue("amount", 1103).addValue("custid", 3));
    assertThat(newId.intValue()).isEqualTo(4);
    verifyAddInvoiceWithoutMetaData(true);
    verify(connection, atLeastOnce()).close();
}
Also used : SqlParameter(cn.taketoday.jdbc.core.SqlParameter) MapSqlParameterSource(cn.taketoday.jdbc.core.namedparam.MapSqlParameterSource) SqlOutParameter(cn.taketoday.jdbc.core.SqlOutParameter) Test(org.junit.jupiter.api.Test)

Aggregations

MapSqlParameterSource (cn.taketoday.jdbc.core.namedparam.MapSqlParameterSource)20 Test (org.junit.jupiter.api.Test)14 SqlOutParameter (cn.taketoday.jdbc.core.SqlOutParameter)6 SqlParameter (cn.taketoday.jdbc.core.SqlParameter)6 ParsedSql (cn.taketoday.jdbc.core.namedparam.ParsedSql)6 ResultSet (java.sql.ResultSet)4 SqlInOutParameter (cn.taketoday.jdbc.core.SqlInOutParameter)2 SqlParameterValue (cn.taketoday.jdbc.core.SqlParameterValue)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2