private JdbcTemplate getJdbcTemplate() { return (JdbcTemplate) namedParameterJdbcTemplate.getJdbcOperations(); } }
@Override @Nullable public <T> T execute(String sql, SqlParameterSource paramSource, PreparedStatementCallback<T> action) throws DataAccessException { return getJdbcOperations().execute(getPreparedStatementCreator(sql, paramSource), action); }
@Override @Nullable public <T> T query(String sql, SqlParameterSource paramSource, ResultSetExtractor<T> rse) throws DataAccessException { return getJdbcOperations().query(getPreparedStatementCreator(sql, paramSource), rse); }
@Override public void query(String sql, SqlParameterSource paramSource, RowCallbackHandler rch) throws DataAccessException { getJdbcOperations().query(getPreparedStatementCreator(sql, paramSource), rch); }
@Override public <T> List<T> query(String sql, SqlParameterSource paramSource, RowMapper<T> rowMapper) throws DataAccessException { return getJdbcOperations().query(getPreparedStatementCreator(sql, paramSource), rowMapper); }
@Override public int update(String sql, SqlParameterSource paramSource) throws DataAccessException { return getJdbcOperations().update(getPreparedStatementCreator(sql, paramSource)); }
@Override @Nullable public <T> T queryForObject(String sql, SqlParameterSource paramSource, RowMapper<T> rowMapper) throws DataAccessException { List<T> results = getJdbcOperations().query(getPreparedStatementCreator(sql, paramSource), rowMapper); return DataAccessUtils.nullableSingleResult(results); }
@Override public SqlRowSet queryForRowSet(String sql, SqlParameterSource paramSource) throws DataAccessException { SqlRowSet result = getJdbcOperations().query( getPreparedStatementCreator(sql, paramSource), new SqlRowSetResultSetExtractor()); Assert.state(result != null, "No result"); return result; }
@Override public int update( String sql, SqlParameterSource paramSource, KeyHolder generatedKeyHolder, @Nullable String[] keyColumnNames) throws DataAccessException { PreparedStatementCreator psc = getPreparedStatementCreator(sql, paramSource, pscf -> { if (keyColumnNames != null) { pscf.setGeneratedKeysColumnNames(keyColumnNames); } else { pscf.setReturnGeneratedKeys(true); } }); return getJdbcOperations().update(psc, generatedKeyHolder); }
@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; } }); }
@Test public void testConfig() { assertNotNull(jdbcPagingItemReader); NamedParameterJdbcTemplate namedParameterJdbcTemplate = (NamedParameterJdbcTemplate) ReflectionTestUtils.getField(jdbcPagingItemReader, "namedParameterJdbcTemplate"); JdbcTemplate jdbcTemplate = (JdbcTemplate) namedParameterJdbcTemplate.getJdbcOperations(); assertEquals(1000, jdbcTemplate.getMaxRows()); assertEquals(100, jdbcTemplate.getFetchSize()); }
/** * Expose the classic Spring JdbcTemplate to allow invocation of less * commonly used methods. */ public JdbcOperations getJdbcOperations() { return this.namedParameterJdbcOperations.getJdbcOperations(); }
private Map<String, BigDecimal> getVisitorsBy(String dimension, VisitorStat stat, VisitorInterval interval, boolean robots) { Map<String, BigDecimal> visitorsMap = new LinkedHashMap<>(); jdbcTemplate.getJdbcOperations().query( format(STATS_BY_QUERY, dimension, stat.getExpression(), interval.getExpression(), robotsCondition(robots)), rs -> { String country = rs.getString(dimension); BigDecimal value = rs.getBigDecimal("value"); visitorsMap.put(country, value); } ); return visitorsMap; }
@Override public synchronized void clear() { String sql = "truncate table " + getTableName(); jdbcTemplate.getJdbcOperations().execute(sql); }
public BatchUpdateResult batchUpdateWithSql(List<String> sqls) { checkNull(); String[] sqlArray = new String[sqls.size()]; int i = 0; for (String sql : sqls) { sqlArray[i] = sql; i++; } return new BatchUpdateResult(this.namedParameterJdbcTemplate.getJdbcOperations().batchUpdate(sqlArray)); }
public BigDecimal getVisitors(VisitorStat stat, VisitorInterval interval, boolean robots) { return jdbcTemplate.getJdbcOperations().queryForObject( format(STATS_QUERY, stat.getExpression(), interval.getExpression(), robotsCondition(robots)), (rs, rowNum) -> rs.getBigDecimal("value") ); }
public <T> T queryForObject(String sql, SqlParameterSource paramSource, RowMapper<T> rowMapper) throws DataAccessException { List<T> results = getJdbcOperations().query(getPreparedStatementCreator(sql, paramSource), rowMapper); return DataAccessUtils.requiredSingleResult(results); }
@Override public void initialize() throws Exception { NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(ds); try (InputStream in = JDBCResourceStoreProperties.class.getResourceAsStream("init.postgres.sql")) { Util.runScript(in, template.getJdbcOperations(), null); } }
@Override public void initialize() throws Exception { NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(ds); try (InputStream in = JDBCResourceStoreProperties.class.getResourceAsStream("init.h2.sql")) { Util.runScript(in, template.getJdbcOperations(), null); } }
@Override @Transactional(propagation = Propagation.REQUIRES_NEW) public void log(final MessageSentLog message) { message.setId(generator.generate()); message.setDate(new Date()); final String sql = "INSERT INTO " + TABLE_NAME + " (" + ALL_FIELDS + ") VALUES (?, ?, ?, ?, ?, ?)"; int update = jdbcTemplate.getJdbcOperations().update(sql, message.getId(), message.getRecipient(), message.getSubject(), message.getContent(), message.getContentHtml(), message.getDate()); if (update == 0) { logger.warn("Template [{}] was not inserted", new Object[] { message.getSubject() }); } }