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);
}
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;
}
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;
}
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();
}
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();
}
Aggregations