org.springframework.jdbc.core.namedparam.ParsedSql Java Examples

The following examples show how to use org.springframework.jdbc.core.namedparam.ParsedSql. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SqlUpdate.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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;
}
 
Example #2
Source File: SqlUpdate.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Method to execute the update given arguments and
 * retrieve the generated keys using a KeyHolder.
 * @param paramMap Map of parameter name to parameter object,
 * matching named parameters specified in the SQL statement
 * @param generatedKeyHolder 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;
}
 
Example #3
Source File: SqlUpdate.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Generic method to execute the update given named parameters.
 * All other update methods invoke this method.
 * @param paramMap 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;
}
 
Example #4
Source File: MappingSqlQuery.java    From SimpleFlatMapper with MIT License 5 votes vote down vote up
public List<T> executeByNamedParam(Map<String, ?> paramMap, 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());
    return query(newPreparedStatementCreator(sqlToUse, params));
}
 
Example #5
Source File: SqlOperation.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Obtain a parsed representation of this operation's SQL statement.
 * <p>Typically used for named parameter parsing.
 */
protected ParsedSql getParsedSql() {
	synchronized (this.parsedSqlMonitor) {
		if (this.cachedSql == null) {
			this.cachedSql = NamedParameterUtils.parseSqlStatement(getSql());
		}
		return this.cachedSql;
	}
}
 
Example #6
Source File: DatasourceServletAction.java    From ureport with Apache License 2.0 5 votes vote down vote up
protected PreparedStatementCreator getPreparedStatementCreator(String sql, SqlParameterSource paramSource) {
	ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(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);
}
 
Example #7
Source File: SqlUpdate.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Method to execute the update given arguments and
 * retrieve the generated keys using a KeyHolder.
 * @param paramMap Map of parameter name to parameter object,
 * matching named parameters specified in the SQL statement
 * @param generatedKeyHolder 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;
}
 
Example #8
Source File: SqlUpdate.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generic method to execute the update given named parameters.
 * All other update methods invoke this method.
 * @param paramMap 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;
}
 
Example #9
Source File: SqlOperation.java    From effectivejava with Apache License 2.0 5 votes vote down vote up
/**
 * Obtain a parsed representation of this operation's SQL statement.
 * <p>Typically used for named parameter parsing.
 */
protected ParsedSql getParsedSql() {
	synchronized (this.parsedSqlMonitor) {
		if (this.cachedSql == null) {
			this.cachedSql = NamedParameterUtils.parseSqlStatement(getSql());
		}
		return this.cachedSql;
	}
}
 
Example #10
Source File: SqlOperation.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Obtain a parsed representation of this operation's SQL statement.
 * <p>Typically used for named parameter parsing.
 */
protected ParsedSql getParsedSql() {
	synchronized (this.parsedSqlMonitor) {
		if (this.cachedSql == null) {
			this.cachedSql = NamedParameterUtils.parseSqlStatement(getSql());
		}
		return this.cachedSql;
	}
}
 
Example #11
Source File: SqlUpdate.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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;
}
 
Example #12
Source File: SqlOperation.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Obtain a parsed representation of this operation's SQL statement.
 * <p>Typically used for named parameter parsing.
 */
protected ParsedSql getParsedSql() {
	synchronized (this.parsedSqlMonitor) {
		if (this.cachedSql == null) {
			this.cachedSql = NamedParameterUtils.parseSqlStatement(resolveSql());
		}
		return this.cachedSql;
	}
}
 
Example #13
Source File: SqlUpdate.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * 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;
}
 
Example #14
Source File: SqlUpdate.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * 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;
}
 
Example #15
Source File: SqlUpdate.java    From effectivejava with Apache License 2.0 5 votes vote down vote up
/**
 * Generic method to execute the update given named parameters.
 * All other update methods invoke this method.
 * @param paramMap 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;
}
 
Example #16
Source File: SqlOperation.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Obtain a parsed representation of this operation's SQL statement.
 * <p>Typically used for named parameter parsing.
 */
protected ParsedSql getParsedSql() {
	synchronized (this.parsedSqlMonitor) {
		if (this.cachedSql == null) {
			this.cachedSql = NamedParameterUtils.parseSqlStatement(resolveSql());
		}
		return this.cachedSql;
	}
}
 
Example #17
Source File: TransactionImpl.java    From gemini with Apache License 2.0 5 votes vote down vote up
private PreparedStatement getPreparedStatement(String sql, @Nullable Map<String, ?> parameters, boolean returnKeys) throws SQLException {
    SqlParameterSource paramSource = new MapSqlParameterSource(parameters);
    ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
    String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, paramSource);
    List<SqlParameter> declaredParameters = NamedParameterUtils.buildSqlParameterList(parsedSql, paramSource);
    Object[] params = NamedParameterUtils.buildValueArray(parsedSql, paramSource, null);
    PreparedStatementCreatorFactory psCreatorFactory = new PreparedStatementCreatorFactory(sqlToUse, declaredParameters);
    psCreatorFactory.setReturnGeneratedKeys(returnKeys);
    PreparedStatementCreator psCreator = psCreatorFactory.newPreparedStatementCreator(params);
    PreparedStatement preparedStatement = psCreator.createPreparedStatement(connection);
    logger.debug(preparedStatement.unwrap(PreparedStatement.class).toString());
    return preparedStatement;
}
 
Example #18
Source File: SqlUpdate.java    From effectivejava with Apache License 2.0 5 votes vote down vote up
/**
 * Method to execute the update given arguments and
 * retrieve the generated keys using a KeyHolder.
 * @param paramMap Map of parameter name to parameter object,
 * matching named parameters specified in the SQL statement
 * @param generatedKeyHolder 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;
}
 
Example #19
Source File: SqlQuery.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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 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, 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);
}
 
Example #20
Source File: SqlQuery.java    From effectivejava with Apache License 2.0 3 votes vote down vote up
/**
 * 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 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, 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);
}
 
Example #21
Source File: SqlQuery.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * 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 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, 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);
}
 
Example #22
Source File: NamedParameterJdbcTemplate.java    From sqlhelper with GNU Lesser General Public License v3.0 3 votes vote down vote up
/**
 * Build a {@link PreparedStatementCreator} based on the given SQL and named parameters.
 * <p>Note: Directly called from all {@code query} variants.
 * Not used for the {@code update} variant with generated key handling.
 *
 * @param sql         the SQL statement to execute
 * @param paramSource container of arguments to bind
 * @return the corresponding {@link PreparedStatementCreator}
 */
@Override
protected PreparedStatementCreator getPreparedStatementCreator(String sql, SqlParameterSource paramSource) {
    ParsedSql parsedSql = getParsedSql(sql);
    PreparedStatementCreatorFactory pscf = getPreparedStatementCreatorFactory(parsedSql, paramSource);
    Object[] params = NamedParameterUtils.buildValueArray(parsedSql, paramSource, null);
    return pscf.newPreparedStatementCreator(params);
}
 
Example #23
Source File: SqlQuery.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * 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 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);
}
 
Example #24
Source File: SqlQuery.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * 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 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);
}
 
Example #25
Source File: NamedParameterJdbcTemplate.java    From sqlhelper with GNU Lesser General Public License v3.0 3 votes vote down vote up
/**
 * Build a {@link PreparedStatementCreatorFactory} based on the given SQL and named parameters.
 *
 * @param parsedSql   parsed representation of the given SQL statement
 * @param paramSource container of arguments to bind
 * @return the corresponding {@link PreparedStatementCreatorFactory}
 * @see #getParsedSql(String)
 * @since Spring 5.1.3
 */
protected PreparedStatementCreatorFactory getPreparedStatementCreatorFactory(
        ParsedSql parsedSql, SqlParameterSource paramSource) {
    String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, paramSource);
    List<SqlParameter> declaredParameters = NamedParameterUtils.buildSqlParameterList(parsedSql, paramSource);
    return new NamedParameterPreparedStatementCreatorFactory(sqlToUse, declaredParameters);
}