/** * Deletes all ACEs defined in the acl_entry table belonging to the presented * ObjectIdentity primary key. * * @param oidPrimaryKey the rows in acl_entry to delete */ protected void deleteEntries(Long oidPrimaryKey) { jdbcOperations.update(deleteEntryByObjectIdentityForeignKey, oidPrimaryKey); }
/** * Retrieves the primary key from the acl_object_identity table for the passed * ObjectIdentity. Unlike some other methods in this implementation, this method will * NOT create a row (use {@link #createObjectIdentity(ObjectIdentity, Sid)} instead). * * @param oid to find * * @return the object identity or null if not found */ protected Long retrieveObjectIdentityPrimaryKey(ObjectIdentity oid) { try { return jdbcOperations.queryForObject(selectObjectIdentityPrimaryKey, Long.class, oid.getType(), oid.getIdentifier().toString()); } catch (DataAccessException notFound) { return null; } }
@Override @Nullable public <T> T query(String sql, SqlParameterSource paramSource, ResultSetExtractor<T> rse) throws DataAccessException { return getJdbcOperations().query(getPreparedStatementCreator(sql, paramSource), rse); }
/** * Retrieves the primary key from acl_sid, creating a new row if needed and the * allowCreate property is true. * @param sidName name of Sid to find or to create * @param sidIsPrincipal whether it's a user or granted authority like role * @param allowCreate true if creation is permitted if not found * @return the primary key or null if not found */ protected Long createOrRetrieveSidPrimaryKey(String sidName, boolean sidIsPrincipal, boolean allowCreate) { List<Long> sidIds = jdbcOperations.queryForList(selectSidPrimaryKey, new Object[] { Boolean.valueOf(sidIsPrincipal), sidName }, Long.class); if (!sidIds.isEmpty()) { return sidIds.get(0); } if (allowCreate) { jdbcOperations.update(insertSid, Boolean.valueOf(sidIsPrincipal), sidName); Assert.isTrue(TransactionSynchronizationManager.isSynchronizationActive(), "Transaction must be running"); return jdbcOperations.queryForObject(sidIdentityQuery, Long.class); } return null; }
private void deleteSessionAttributes(JdbcSession session, List<String> attributeNames) { Assert.notEmpty(attributeNames, "attributeNames must not be null or empty"); if (attributeNames.size() > 1) { this.jdbcOperations.batchUpdate(this.deleteSessionAttributeQuery, new BatchPreparedStatementSetter() { @Override public void setValues(PreparedStatement ps, int i) throws SQLException { String attributeName = attributeNames.get(i); ps.setString(1, session.primaryKey); ps.setString(2, attributeName); } @Override public int getBatchSize() { return attributeNames.size(); } }); } else { this.jdbcOperations.update(this.deleteSessionAttributeQuery, (ps) -> { String attributeName = attributeNames.get(0); ps.setString(1, session.primaryKey); ps.setString(2, attributeName); }); } }
@Before public void onSetUp() throws Exception { jdbcTemplate.update("delete from TRADE"); jdbcTemplate.update("delete from CUSTOMER where ID > 4"); jdbcTemplate.update("update CUSTOMER set credit=100000"); List<Map<String, Object>> list = jdbcTemplate.queryForList("select name, CREDIT from CUSTOMER"); for (Map<String, Object> map : list) { credits.put((String) map.get("NAME"), ((Number) map.get("CREDIT")).doubleValue()); } }
@Test public void testLaunchJob() throws Exception { jdbcTemplate.update("DELETE FROM PLAYERS"); jdbcTemplate.update("DELETE FROM GAMES"); jdbcTemplate.update("DELETE FROM PLAYER_SUMMARY"); jobLauncherTestUtils.launchJob(); int count = jdbcTemplate.queryForObject("SELECT COUNT(*) from PLAYER_SUMMARY", Integer.class); assertTrue(count > 0); } }
@Override @Nullable public <T> T execute(String sql, SqlParameterSource paramSource, PreparedStatementCallback<T> action) throws DataAccessException { return getJdbcOperations().execute(getPreparedStatementCreator(sql, paramSource), action); }
@Test @Transactional public void testWrite() { jdbcTemplate.execute("INSERT INTO CUSTOMER VALUES (99, 0, 'testName', 100)"); CustomerDebit customerDebit = new CustomerDebit(); customerDebit.setName("testName"); customerDebit.setDebit(BigDecimal.valueOf(5)); writer.write(customerDebit); jdbcTemplate.query("SELECT name, credit FROM CUSTOMER WHERE name = 'testName'", new RowCallbackHandler() { @Override public void processRow(ResultSet rs) throws SQLException { assertEquals(95, rs.getLong("credit")); } }); } }
if (this.preparedStatementSetter != null) { return this.jdbcOperations.getJdbcOperations() .execute(this.generatedKeysStatementCreator, ps -> { this.preparedStatementSetter.setValues(ps, message); .batchUpdate(this.updateSql, new BatchPreparedStatementSetter() { .update(this.updateSql, ps -> this.preparedStatementSetter.setValues(ps, message));
@Override public void addMessagesToGroup(Object groupId, Message<?>... messages) { final String groupKey = getKey(groupId); boolean groupNotExist = this.jdbcTemplate.queryForObject(this.getQuery(Query.GROUP_EXISTS), // NOSONAR query never returns null Integer.class, groupKey, this.region) < 1; this.jdbcTemplate.queryForObject(getQuery(Query.GET_GROUP_CREATED_DATE), Timestamp.class, groupKey, this.region); addMessage(message); this.jdbcTemplate.batchUpdate(getQuery(Query.CREATE_GROUP_TO_MESSAGE), Arrays.asList(messages), 100,
public static int[] executeBatchUpdate( String sql, final List<Object[]> batchArgs, final int[] columnTypes, JdbcOperations jdbcOperations) { if (batchArgs.isEmpty()) { return new int[0]; } return jdbcOperations.batchUpdate( sql, new BatchPreparedStatementSetter() { @Override public void setValues(PreparedStatement ps, int i) throws SQLException { Object[] values = batchArgs.get(i); setStatementParameters(values, ps, columnTypes); } @Override public int getBatchSize() { return batchArgs.size(); } }); }
@Override public Integer getPortalUserId(String userName) { final List<Integer> results = this.jdbcOperations.queryForList( "SELECT USER_ID FROM UP_USER WHERE USER_NAME=?", Integer.class, userName); return DataAccessUtils.singleResult(results); }
@Test public void testJobLaunch() throws Exception { jdbcTemplate.update("DELETE from TRADE"); int before = jdbcTemplate.queryForObject("SELECT COUNT(*) from TRADE", Integer.class); jobLauncherTestUtils.launchJob(); checkOutputFile("build/test-outputs/CustomerReport1.txt"); checkOutputFile("build/test-outputs/CustomerReport2.txt"); checkOutputTable(before); }
/** * Retrieves the primary key from {@code acl_class}, creating a new row if needed and * the {@code allowCreate} property is {@code true}. * * @param type to find or create an entry for (often the fully-qualified class name) * @param allowCreate true if creation is permitted if not found * * @return the primary key or null if not found */ protected Long createOrRetrieveClassPrimaryKey(String type, boolean allowCreate, Class idType) { List<Long> classIds = jdbcOperations.queryForList(selectClassPrimaryKey, new Object[] { type }, Long.class); if (!classIds.isEmpty()) { return classIds.get(0); } if (allowCreate) { if (!isAclClassIdSupported()) { jdbcOperations.update(insertClass, type); } else { jdbcOperations.update(insertClass, type, idType.getCanonicalName()); } Assert.isTrue(TransactionSynchronizationManager.isSynchronizationActive(), "Transaction must be running"); return jdbcOperations.queryForObject(classIdentityQuery, Long.class); } return null; }
@Before public void onSetUpInTransaction() throws Exception { jdbcTemplate.execute("delete from PLAYERS"); }
private void updateSessionAttributes(JdbcSession session, List<String> attributeNames) { Assert.notEmpty(attributeNames, "attributeNames must not be null or empty"); if (attributeNames.size() > 1) { this.jdbcOperations.batchUpdate(this.updateSessionAttributeQuery, new BatchPreparedStatementSetter() { @Override public void setValues(PreparedStatement ps, int i) throws SQLException { String attributeName = attributeNames.get(i); setObjectAsBlob(ps, 1, session.getAttribute(attributeName)); ps.setString(2, session.primaryKey); ps.setString(3, attributeName); } @Override public int getBatchSize() { return attributeNames.size(); } }); } else { this.jdbcOperations.update(this.updateSessionAttributeQuery, (ps) -> { String attributeName = attributeNames.get(0); setObjectAsBlob(ps, 1, session.getAttribute(attributeName)); ps.setString(2, session.primaryKey); ps.setString(3, attributeName); }); } }
@Before public void onSetUp() throws Exception { jdbcTemplate.update("delete from TRADE"); List<Map<String, Object>> list = jdbcTemplate.queryForList("select NAME, CREDIT from CUSTOMER"); for (Map<String, Object> map : list) { credits.put((String) map.get("NAME"), ((Number) map.get("CREDIT")).doubleValue()); } }
public static int[] executeBatchUpdateWithNamedParameters( final ParsedSql parsedSql, final SqlParameterSource[] batchArgs, JdbcOperations jdbcOperations) { if (batchArgs.length == 0) { return new int[0]; } String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, batchArgs[0]); return jdbcOperations.batchUpdate( sqlToUse, new BatchPreparedStatementSetter() { @Override public void setValues(PreparedStatement ps, int i) throws SQLException { Object[] values = NamedParameterUtils.buildValueArray(parsedSql, batchArgs[i], null); int[] columnTypes = NamedParameterUtils.buildSqlTypeArray(parsedSql, batchArgs[i]); setStatementParameters(values, ps, columnTypes); } @Override public int getBatchSize() { return batchArgs.length; } }); }
public List<Map<String, Object>> queryForList(String sql, Object... args) throws DataAccessException { return (ObjectUtils.isEmpty(args) ? getJdbcOperations().queryForList(sql) : getJdbcOperations().queryForList(sql, getArguments(args))); }