Java Code Examples for org.springframework.jdbc.core.PreparedStatementCreatorFactory

The following examples show how to use org.springframework.jdbc.core.PreparedStatementCreatorFactory. These examples are extracted from open source projects. 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 Project: spring-analysis-note   Source File: NamedParameterJdbcTemplate.java    License: MIT License 6 votes vote down vote up
@Override
public int[] batchUpdate(String sql, SqlParameterSource[] batchArgs) {
	if (batchArgs.length == 0) {
		return new int[0];
	}

	ParsedSql parsedSql = getParsedSql(sql);
	PreparedStatementCreatorFactory pscf = getPreparedStatementCreatorFactory(parsedSql, batchArgs[0]);

	return getJdbcOperations().batchUpdate(
			pscf.getSql(),
			new BatchPreparedStatementSetter() {
				@Override
				public void setValues(PreparedStatement ps, int i) throws SQLException {
					Object[] values = NamedParameterUtils.buildValueArray(parsedSql, batchArgs[i], null);
					pscf.newPreparedStatementSetter(values).setValues(ps);
				}
				@Override
				public int getBatchSize() {
					return batchArgs.length;
				}
			});
}
 
Example 2
Source Project: java-technology-stack   Source File: NamedParameterJdbcTemplate.java    License: MIT License 6 votes vote down vote up
@Override
public int[] batchUpdate(String sql, SqlParameterSource[] batchArgs) {
	if (batchArgs.length == 0) {
		return new int[0];
	}

	ParsedSql parsedSql = getParsedSql(sql);
	PreparedStatementCreatorFactory pscf = getPreparedStatementCreatorFactory(parsedSql, batchArgs[0]);

	return getJdbcOperations().batchUpdate(
			pscf.getSql(),
			new BatchPreparedStatementSetter() {
				@Override
				public void setValues(PreparedStatement ps, int i) throws SQLException {
					Object[] values = NamedParameterUtils.buildValueArray(parsedSql, batchArgs[i], null);
					pscf.newPreparedStatementSetter(values).setValues(ps);
				}
				@Override
				public int getBatchSize() {
					return batchArgs.length;
				}
			});
}
 
Example 3
private long saveTacoInfo(Taco taco) {
  taco.setCreatedAt(new Date());
  PreparedStatementCreator psc =
      new PreparedStatementCreatorFactory(
          "insert into Taco (name, createdAt) values (?, ?)",
          Types.VARCHAR, Types.TIMESTAMP
      ).newPreparedStatementCreator(
          Arrays.asList(
              taco.getName(),
              new Timestamp(taco.getCreatedAt().getTime())));

  KeyHolder keyHolder = new GeneratedKeyHolder();
  jdbc.update(psc, keyHolder);

  return keyHolder.getKey().longValue();
}
 
Example 4
/**
 * Demonstrate how to insert and retrieve a generated key.
 * <p>
 * The {@code id} column of the {@code short_message} table can generated ids.
 * In order to retrieve the newly generated id, we need an update that returns
 * some data (a JDBC 3 feature). Spring supports this with a keyholder.
 * <p>
 * Unfortunately, {@link JdbcTemplate#update(PreparedStatementCreator, KeyHolder)}
 * needs a {@link PreparedStatementCreator} which is a bit overwhelming
 * here.
 */
public void insertMessage(Author author, String text) {
    String sql = "INSERT INTO short_message(author_id, posted_time, message_text)" +
            " VALUES(?, ?, ?)";
    Timestamp currentTimestamp = Timestamp.valueOf(LocalDateTime.now());

    PreparedStatementCreatorFactory statementCreatorFactory =
            new PreparedStatementCreatorFactory(sql,
                    Types.INTEGER, Types.TIMESTAMP, Types.VARCHAR);
    statementCreatorFactory.setReturnGeneratedKeys(true);
    statementCreatorFactory.setGeneratedKeysColumnNames("id");
    PreparedStatementCreator preparedStatementCreator =
            statementCreatorFactory.newPreparedStatementCreator(
                    new Object[]{author.getId(), currentTimestamp, text});

    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    KeyHolder keyHolder = new GeneratedKeyHolder();
    int update = jdbcTemplate.update(preparedStatementCreator, keyHolder);
    log.info("auto-insert created {} row with key {}", update, keyHolder.getKey());
}
 
Example 5
/**
 * Demonstrate how to insert and retrieve a generated key.
 * <p>
 * The {@code id} column of the {@code short_message} table can generated ids.
 * In order to retrieve the newly generated id, we need an update that returns
 * some data (a JDBC 3 feature). Spring supports this with a keyholder.
 * <p>
 * Unfortunately, {@link JdbcTemplate#update(PreparedStatementCreator, KeyHolder)}
 * needs a {@link PreparedStatementCreator} which is a bit overwhelming
 * here.
 */
public void insertMessage(Author author, String text) {
    String sql = "INSERT INTO short_message(author_id, posted_time, message_text)" +
            " VALUES(?, ?, ?)";
    Timestamp currentTimestamp = Timestamp.valueOf(LocalDateTime.now());

    PreparedStatementCreatorFactory statementCreatorFactory =
            new PreparedStatementCreatorFactory(sql,
                    Types.INTEGER, Types.TIMESTAMP, Types.VARCHAR);
    statementCreatorFactory.setReturnGeneratedKeys(true);
    statementCreatorFactory.setGeneratedKeysColumnNames("id");
    PreparedStatementCreator preparedStatementCreator =
            statementCreatorFactory.newPreparedStatementCreator(
                    new Object[]{author.getId(), currentTimestamp, text});

    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    KeyHolder keyHolder = new GeneratedKeyHolder();
    int update = jdbcTemplate.update(preparedStatementCreator, keyHolder);
    log.info("auto-insert created {} row with key {}", update, keyHolder.getKey());
}
 
Example 6
Source Project: buffer-slayer   Source File: BatchJdbcTemplateTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void updateWithPreparedStatementCreator() {
  reporter = AsyncReporter.builder(new JdbcTemplateSender(underlying))
      .pendingMaxMessages(2)
      .bufferedMaxMessages(2)
      .messageTimeout(0, TimeUnit.MILLISECONDS)
      .build();
  batchJdbcTemplate = new BatchJdbcTemplate(underlying, reporter);

  PreparedStatementCreatorFactory creatorFactory = new PreparedStatementCreatorFactory(INSERTION);
  creatorFactory.addParameter(new SqlParameter(Types.VARCHAR));
  creatorFactory.addParameter(new SqlParameter(Types.TIMESTAMP));

  PreparedStatementCreator creator = creatorFactory
      .newPreparedStatementCreator(new Object[]{randomString(), new Date()});

  batchJdbcTemplate.update(creator);
  batchJdbcTemplate.update(creator);

  reporter.flush();
  int rowCount = batchJdbcTemplate.queryForObject("SELECT COUNT(1) FROM test;", Integer.class);
  assertEquals(2, rowCount);
}
 
Example 7
@Override
public int update(
		String sql, SqlParameterSource paramSource, KeyHolder generatedKeyHolder, String[] keyColumnNames)
		throws DataAccessException {

	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);
	if (keyColumnNames != null) {
		pscf.setGeneratedKeysColumnNames(keyColumnNames);
	}
	else {
		pscf.setReturnGeneratedKeys(true);
	}
	return getJdbcOperations().update(pscf.newPreparedStatementCreator(params), generatedKeyHolder);
}
 
Example 8
@Override
public int update(
		String sql, SqlParameterSource paramSource, KeyHolder generatedKeyHolder, String[] keyColumnNames)
		throws DataAccessException {

	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);
	if (keyColumnNames != null) {
		pscf.setGeneratedKeysColumnNames(keyColumnNames);
	}
	else {
		pscf.setReturnGeneratedKeys(true);
	}
	return getJdbcOperations().update(pscf.newPreparedStatementCreator(params), generatedKeyHolder);
}
 
Example 9
Source Project: effectivejava   Source File: NamedParameterJdbcTemplate.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public int update(
		String sql, SqlParameterSource paramSource, KeyHolder generatedKeyHolder, String[] keyColumnNames)
		throws DataAccessException {

	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);
	if (keyColumnNames != null) {
		pscf.setGeneratedKeysColumnNames(keyColumnNames);
	}
	else {
		pscf.setReturnGeneratedKeys(true);
	}
	return getJdbcOperations().update(pscf.newPreparedStatementCreator(params), generatedKeyHolder);
}
 
Example 10
Source Project: gemini   Source File: TransactionImpl.java    License: 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 11
Source Project: spring-analysis-note   Source File: SqlOperation.java    License: MIT License 5 votes vote down vote up
/**
 * Overridden method to configure the PreparedStatementCreatorFactory
 * based on our declared parameters.
 */
@Override
protected final void compileInternal() {
	this.preparedStatementFactory = new PreparedStatementCreatorFactory(resolveSql(), getDeclaredParameters());
	this.preparedStatementFactory.setResultSetType(getResultSetType());
	this.preparedStatementFactory.setUpdatableResults(isUpdatableResults());
	this.preparedStatementFactory.setReturnGeneratedKeys(isReturnGeneratedKeys());
	if (getGeneratedKeysColumnNames() != null) {
		this.preparedStatementFactory.setGeneratedKeysColumnNames(getGeneratedKeysColumnNames());
	}

	onCompileInternal();
}
 
Example 12
Source Project: Project   Source File: JdbcSpittleRepository.java    License: Apache License 2.0 5 votes vote down vote up
public Spittle save(Spittle spittle) {
  
  System.out.println("--> " + spittle.getMessage());
  
  
  String sql = "insert into Spittle (message, created_at, latitude, longitude)" +
      " values (?, ?, ?, ?)";
  PreparedStatementCreatorFactory statementCreatorFactory = new PreparedStatementCreatorFactory(sql, Types.VARCHAR, Types.TIMESTAMP, Types.DOUBLE, Types.DOUBLE);
  statementCreatorFactory.setReturnGeneratedKeys(true);
  PreparedStatementCreator creator = statementCreatorFactory.newPreparedStatementCreator(new Object[] {
      spittle.getMessage(),
      spittle.getTime(),
      spittle.getLatitude(),
      spittle.getLongitude()
  });

  
  GeneratedKeyHolder keyHolder = new GeneratedKeyHolder();
  jdbc.update(creator, keyHolder);
  return new Spittle(
      keyHolder.getKey().longValue(), 
      spittle.getMessage(), 
      spittle.getTime(), 
      spittle.getLongitude(), 
      spittle.getLatitude());
  
  
}
 
Example 13
Source Project: Project   Source File: JdbcSpittleRepository.java    License: Apache License 2.0 5 votes vote down vote up
public void update(Spittle spittle) {
 String sql = "update Spittle set message=?, created_at=?, latitude=?, longitude=? "
 		+ "where id=?";
 PreparedStatementCreatorFactory creatorFactory = 
	  new PreparedStatementCreatorFactory(sql, Types.VARCHAR, Types.TIMESTAMP, Types.DOUBLE, Types.DOUBLE, Types.INTEGER);
 PreparedStatementCreator creator = creatorFactory.newPreparedStatementCreator(new Object[] {
	  spittle.getMessage(),
	  spittle.getTime(),
	  spittle.getLatitude(),
	  spittle.getLongitude(),
	  spittle.getId()
 });
 jdbc.update(creator);
}
 
Example 14
Source Project: java-technology-stack   Source File: SqlOperation.java    License: MIT License 5 votes vote down vote up
/**
 * Overridden method to configure the PreparedStatementCreatorFactory
 * based on our declared parameters.
 */
@Override
protected final void compileInternal() {
	this.preparedStatementFactory = new PreparedStatementCreatorFactory(resolveSql(), getDeclaredParameters());
	this.preparedStatementFactory.setResultSetType(getResultSetType());
	this.preparedStatementFactory.setUpdatableResults(isUpdatableResults());
	this.preparedStatementFactory.setReturnGeneratedKeys(isReturnGeneratedKeys());
	if (getGeneratedKeysColumnNames() != null) {
		this.preparedStatementFactory.setGeneratedKeysColumnNames(getGeneratedKeysColumnNames());
	}

	onCompileInternal();
}
 
Example 15
Source Project: qmq   Source File: DefaultMessageStore.java    License: Apache License 2.0 5 votes vote down vote up
private PreparedStatementCreatorFactory createFactory() {
    List<SqlParameter> parameters = new ArrayList<>();
    parameters.add(new SqlParameter("content", Types.LONGVARCHAR));
    parameters.add(new SqlParameter("create_time", Types.TIMESTAMP));
    PreparedStatementCreatorFactory factory = new PreparedStatementCreatorFactory(sqlStatementProvider.getInsertSql(), parameters);
    factory.setReturnGeneratedKeys(true);
    return factory;
}
 
Example 16
Source Project: fish-admin   Source File: BaseRepository.java    License: MIT License 5 votes vote down vote up
public long insert(List<String> columns, List<Object> args, int[] types) {
    String insertColumns = primaryKeyName();
    String insertPlaceholders = "nextval('"+tableName()+"_id_seq')";
    for (String column : columns) {
        insertColumns += "," + column;
        insertPlaceholders += ",?";
    }

    insertColumns += ", " + createdAtName();
    insertPlaceholders += ", now()";
    insertColumns += ", " + updatedAtName();
    insertPlaceholders += ", now()";

    String sql = "insert into " + tableName() +
            " ("+insertColumns+") values ("+insertPlaceholders+")";

    KeyHolder keyHolder = new GeneratedKeyHolder();

    PreparedStatementCreatorFactory pscf = new PreparedStatementCreatorFactory(sql, types);
    pscf.setReturnGeneratedKeys(true);
    try {
        logger.info("insert.sql: {}, args: {}", sql, jsonMapper.writeValueAsString(args));
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
    writeJdbcTemplate.update(pscf.newPreparedStatementCreator(args), keyHolder);
    if (keyHolder.getKeyList() != null && !keyHolder.getKeyList().isEmpty()) {
        return (long) keyHolder.getKeyList().get(0).get(primaryKeyName());
    }
    return -1;
}
 
Example 17
Source Project: lams   Source File: SqlOperation.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Overridden method to configure the PreparedStatementCreatorFactory
 * based on our declared parameters.
 */
@Override
protected final void compileInternal() {
	this.preparedStatementFactory = new PreparedStatementCreatorFactory(getSql(), getDeclaredParameters());
	this.preparedStatementFactory.setResultSetType(getResultSetType());
	this.preparedStatementFactory.setUpdatableResults(isUpdatableResults());
	this.preparedStatementFactory.setReturnGeneratedKeys(isReturnGeneratedKeys());
	if (getGeneratedKeysColumnNames() != null) {
		this.preparedStatementFactory.setGeneratedKeysColumnNames(getGeneratedKeysColumnNames());
	}
	this.preparedStatementFactory.setNativeJdbcExtractor(getJdbcTemplate().getNativeJdbcExtractor());

	onCompileInternal();
}
 
Example 18
/**
 * 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);
}
 
Example 19
public void queryUnsafe(String input) {
    String sql = "select * from Users where name = '" + input + "' id=?";

    new PreparedStatementCreatorFactory(sql);
    new PreparedStatementCreatorFactory(sql, new int[] {Types.INTEGER});
    new PreparedStatementCreatorFactory(sql, new ArrayList<SqlParameter>());
}
 
Example 20
Source Project: ureport   Source File: DatasourceServletAction.java    License: 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 21
Source Project: Spring-MVC-Blueprints   Source File: ApplicationDaoImpl.java    License: MIT License 5 votes vote down vote up
@Override
public void setStudentAccount(Application application) {
	
	String sql = "INSERT INTO tblStudentUser (username, password) VALUES (?, ?)";
	PreparedStatementCreatorFactory psCreateFactory = new PreparedStatementCreatorFactory(sql, new int[]{Types.VARCHAR, Types.VARCHAR});
	jdbcTemplate.update(psCreateFactory.newPreparedStatementCreator(new Object[]{application.getUsername(),application.getPassword()}));
	
}
 
Example 22
Source Project: spring4-understanding   Source File: SqlOperation.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Overridden method to configure the PreparedStatementCreatorFactory
 * based on our declared parameters.
 */
@Override
protected final void compileInternal() {
	this.preparedStatementFactory = new PreparedStatementCreatorFactory(getSql(), getDeclaredParameters());
	this.preparedStatementFactory.setResultSetType(getResultSetType());
	this.preparedStatementFactory.setUpdatableResults(isUpdatableResults());
	this.preparedStatementFactory.setReturnGeneratedKeys(isReturnGeneratedKeys());
	if (getGeneratedKeysColumnNames() != null) {
		this.preparedStatementFactory.setGeneratedKeysColumnNames(getGeneratedKeysColumnNames());
	}
	this.preparedStatementFactory.setNativeJdbcExtractor(getJdbcTemplate().getNativeJdbcExtractor());

	onCompileInternal();
}
 
Example 23
/**
 * 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);
}
 
Example 24
Source Project: effectivejava   Source File: SqlOperation.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Overridden method to configure the PreparedStatementCreatorFactory
 * based on our declared parameters.
 */
@Override
protected final void compileInternal() {
	this.preparedStatementFactory = new PreparedStatementCreatorFactory(getSql(), getDeclaredParameters());
	this.preparedStatementFactory.setResultSetType(getResultSetType());
	this.preparedStatementFactory.setUpdatableResults(isUpdatableResults());
	this.preparedStatementFactory.setReturnGeneratedKeys(isReturnGeneratedKeys());
	if (getGeneratedKeysColumnNames() != null) {
		this.preparedStatementFactory.setGeneratedKeysColumnNames(getGeneratedKeysColumnNames());
	}
	this.preparedStatementFactory.setNativeJdbcExtractor(getJdbcTemplate().getNativeJdbcExtractor());

	onCompileInternal();
}
 
Example 25
Source Project: effectivejava   Source File: NamedParameterJdbcTemplate.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 26
/**
 * 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 27
/**
 * 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);
}
 
Example 28
Source Project: spring-analysis-note   Source File: NamedParameterJdbcTemplate.java    License: MIT License 3 votes vote down vote up
/**
 * Build a {@link PreparedStatementCreator} based on the given SQL and named parameters.
 * <p>Note: Used for the {@code update} variant with generated key handling, and also
 * delegated from {@link #getPreparedStatementCreator(String, SqlParameterSource)}.
 * @param sql the SQL statement to execute
 * @param paramSource container of arguments to bind
 * @param customizer callback for setting further properties on the
 * {@link PreparedStatementCreatorFactory} in use), applied before the
 * actual {@code newPreparedStatementCreator} call
 * @return the corresponding {@link PreparedStatementCreator}
 * @since 5.0.5
 * @see #getParsedSql(String)
 * @see PreparedStatementCreatorFactory#PreparedStatementCreatorFactory(String, List)
 * @see PreparedStatementCreatorFactory#newPreparedStatementCreator(Object[])
 */
protected PreparedStatementCreator getPreparedStatementCreator(String sql, SqlParameterSource paramSource,
		@Nullable Consumer<PreparedStatementCreatorFactory> customizer) {

	ParsedSql parsedSql = getParsedSql(sql);
	PreparedStatementCreatorFactory pscf = getPreparedStatementCreatorFactory(parsedSql, paramSource);
	if (customizer != null) {
		customizer.accept(pscf);
	}
	Object[] params = NamedParameterUtils.buildValueArray(parsedSql, paramSource, null);
	return pscf.newPreparedStatementCreator(params);
}
 
Example 29
Source Project: spring-analysis-note   Source File: NamedParameterJdbcTemplate.java    License: MIT License 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}
 * @since 5.1.3
 * @see #getPreparedStatementCreator(String, SqlParameterSource, Consumer)
 * @see #getParsedSql(String)
 */
protected PreparedStatementCreatorFactory getPreparedStatementCreatorFactory(
		ParsedSql parsedSql, SqlParameterSource paramSource) {

	String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, paramSource);
	List<SqlParameter> declaredParameters = NamedParameterUtils.buildSqlParameterList(parsedSql, paramSource);
	return new PreparedStatementCreatorFactory(sqlToUse, declaredParameters);
}
 
Example 30
Source Project: java-technology-stack   Source File: NamedParameterJdbcTemplate.java    License: MIT License 3 votes vote down vote up
/**
 * Build a {@link PreparedStatementCreator} based on the given SQL and named parameters.
 * <p>Note: Used for the {@code update} variant with generated key handling, and also
 * delegated from {@link #getPreparedStatementCreator(String, SqlParameterSource)}.
 * @param sql the SQL statement to execute
 * @param paramSource container of arguments to bind
 * @param customizer callback for setting further properties on the
 * {@link PreparedStatementCreatorFactory} in use), applied before the
 * actual {@code newPreparedStatementCreator} call
 * @return the corresponding {@link PreparedStatementCreator}
 * @since 5.0.5
 * @see #getParsedSql(String)
 * @see PreparedStatementCreatorFactory#PreparedStatementCreatorFactory(String, List)
 * @see PreparedStatementCreatorFactory#newPreparedStatementCreator(Object[])
 */
protected PreparedStatementCreator getPreparedStatementCreator(String sql, SqlParameterSource paramSource,
		@Nullable Consumer<PreparedStatementCreatorFactory> customizer) {

	ParsedSql parsedSql = getParsedSql(sql);
	PreparedStatementCreatorFactory pscf = getPreparedStatementCreatorFactory(parsedSql, paramSource);
	if (customizer != null) {
		customizer.accept(pscf);
	}
	Object[] params = NamedParameterUtils.buildValueArray(parsedSql, paramSource, null);
	return pscf.newPreparedStatementCreator(params);
}