Search in sources :

Example 16 with PreparedStatementSetter

use of org.springframework.jdbc.core.PreparedStatementSetter in project spring-integration by spring-projects.

the class JdbcMessageStoreTests method testExpireMessageGroupOnCreateOnly.

@Test
public void testExpireMessageGroupOnCreateOnly() throws Exception {
    final String groupId = "X";
    Message<String> message = MessageBuilder.withPayload("foo").setCorrelationId(groupId).build();
    messageStore.addMessagesToGroup(groupId, message);
    final CountDownLatch groupRemovalLatch = new CountDownLatch(1);
    messageStore.registerMessageGroupExpiryCallback((messageGroupStore, group) -> {
        messageGroupStore.removeMessageGroup(group.getGroupId());
        groupRemovalLatch.countDown();
    });
    messageStore.expireMessageGroups(2000);
    MessageGroup group = messageStore.getMessageGroup(groupId);
    assertEquals(1, group.size());
    messageStore.addMessagesToGroup(groupId, MessageBuilder.withPayload("bar").setCorrelationId(groupId).build());
    JdbcTemplate template = new JdbcTemplate(this.dataSource);
    template.afterPropertiesSet();
    template.update("UPDATE INT_MESSAGE_GROUP set CREATED_DATE=? where GROUP_KEY=? and REGION=?", (PreparedStatementSetter) ps -> {
        ps.setTimestamp(1, new Timestamp(System.currentTimeMillis() - 10000));
        ps.setString(2, UUIDConverter.getUUID(groupId).toString());
        ps.setString(3, "DEFAULT");
    });
    messageStore.expireMessageGroups(2000);
    group = messageStore.getMessageGroup(groupId);
    assertEquals(0, group.size());
    assertTrue(groupRemovalLatch.await(10, TimeUnit.SECONDS));
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) PreparedStatementSetter(org.springframework.jdbc.core.PreparedStatementSetter) Assert.assertNotSame(org.junit.Assert.assertNotSame) RunWith(org.junit.runner.RunWith) Autowired(org.springframework.beans.factory.annotation.Autowired) ArrayList(java.util.ArrayList) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Assert.assertThat(org.junit.Assert.assertThat) MessageBuilder(org.springframework.integration.support.MessageBuilder) SpringJUnit4ClassRunner(org.springframework.test.context.junit4.SpringJUnit4ClassRunner) MessageHistory(org.springframework.integration.history.MessageHistory) DataSource(javax.sql.DataSource) IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) Message(org.springframework.messaging.Message) Before(org.junit.Before) Properties(java.util.Properties) Assert.assertNotNull(org.junit.Assert.assertNotNull) Timestamp(java.sql.Timestamp) MessageGroup(org.springframework.integration.store.MessageGroup) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) UUIDConverter(org.springframework.integration.util.UUIDConverter) UUID(java.util.UUID) InputStreamReader(java.io.InputStreamReader) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) PayloadAndHeaderMatcher.sameExceptIgnorableHeaders(org.springframework.integration.test.matcher.PayloadAndHeaderMatcher.sameExceptIgnorableHeaders) ContextConfiguration(org.springframework.test.context.ContextConfiguration) BufferedReader(java.io.BufferedReader) GenericMessage(org.springframework.messaging.support.GenericMessage) DirectChannel(org.springframework.integration.channel.DirectChannel) Assert.assertEquals(org.junit.Assert.assertEquals) Transactional(org.springframework.transaction.annotation.Transactional) MessageGroup(org.springframework.integration.store.MessageGroup) CountDownLatch(java.util.concurrent.CountDownLatch) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Timestamp(java.sql.Timestamp) Test(org.junit.Test)

Example 17 with PreparedStatementSetter

use of org.springframework.jdbc.core.PreparedStatementSetter in project musiccabinet by hakko.

the class JdbcTagDao method setTopTags.

@Override
public void setTopTags(final List<String> topTags) {
    assert (topTags != null && topTags.size() > 0);
    jdbcTemplate.update("truncate library.toptag");
    String sql = "insert into library.toptag (tag_id)" + " select id from music.tag where tag_name in (" + getParameters(topTags.size()) + ")";
    jdbcTemplate.update(sql, new PreparedStatementSetter() {

        @Override
        public void setValues(PreparedStatement ps) throws SQLException {
            int index = 1;
            for (String topTag : topTags) {
                ps.setString(index++, topTag);
            }
        }
    });
}
Also used : SQLException(java.sql.SQLException) PreparedStatementSetter(org.springframework.jdbc.core.PreparedStatementSetter) PreparedStatement(java.sql.PreparedStatement)

Example 18 with PreparedStatementSetter

use of org.springframework.jdbc.core.PreparedStatementSetter in project dhis2-core by dhis2.

the class HibernateDataStatisticsEventStore method getDataStatisticsEventCount.

@Override
public Map<DataStatisticsEventType, Double> getDataStatisticsEventCount(Date startDate, Date endDate) {
    Map<DataStatisticsEventType, Double> eventTypeCountMap = new HashMap<>();
    final String sql = "select eventtype as eventtype, count(eventtype) as numberofviews " + "from datastatisticsevent " + "where timestamp between ? and ? " + "group by eventtype;";
    PreparedStatementSetter pss = (ps) -> {
        int i = 1;
        ps.setDate(i++, asSqlDate(startDate));
        ps.setDate(i++, asSqlDate(endDate));
    };
    jdbcTemplate.query(sql, pss, (rs, i) -> {
        eventTypeCountMap.put(DataStatisticsEventType.valueOf(rs.getString("eventtype")), rs.getDouble("numberofviews"));
        return eventTypeCountMap;
    });
    final String totalSql = "select count(eventtype) as total " + "from datastatisticsevent " + "where timestamp between ? and ?;";
    jdbcTemplate.query(totalSql, pss, (resultSet, i) -> {
        return eventTypeCountMap.put(DataStatisticsEventType.TOTAL_VIEW, resultSet.getDouble("total"));
    });
    return eventTypeCountMap;
}
Also used : SortOrder(org.hisp.dhis.analytics.SortOrder) HibernateGenericStore(org.hisp.dhis.hibernate.HibernateGenericStore) DataStatisticsEventStore(org.hisp.dhis.datastatistics.DataStatisticsEventStore) Date(java.util.Date) PreparedStatementSetter(org.springframework.jdbc.core.PreparedStatementSetter) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) SessionFactory(org.hibernate.SessionFactory) HashMap(java.util.HashMap) DataStatisticsEventType(org.hisp.dhis.datastatistics.DataStatisticsEventType) COUNT_PASSIVE_DASHBOARD_VIEWS_IN_USAGE_ANALYTICS(org.hisp.dhis.setting.SettingKey.COUNT_PASSIVE_DASHBOARD_VIEWS_IN_USAGE_ANALYTICS) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) List(java.util.List) Lists(com.google.common.collect.Lists) DateUtils.asSqlDate(org.hisp.dhis.util.DateUtils.asSqlDate) Map(java.util.Map) SqlUtils.escapeSql(org.hisp.dhis.system.util.SqlUtils.escapeSql) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) Repository(org.springframework.stereotype.Repository) FavoriteStatistics(org.hisp.dhis.datastatistics.FavoriteStatistics) SystemSettingManager(org.hisp.dhis.setting.SystemSettingManager) DataStatisticsEvent(org.hisp.dhis.datastatistics.DataStatisticsEvent) Assert(org.springframework.util.Assert) DataStatisticsEventType(org.hisp.dhis.datastatistics.DataStatisticsEventType) HashMap(java.util.HashMap) PreparedStatementSetter(org.springframework.jdbc.core.PreparedStatementSetter)

Example 19 with PreparedStatementSetter

use of org.springframework.jdbc.core.PreparedStatementSetter in project dhis2-core by dhis2.

the class HibernateDataStatisticsEventStore method getFavoritesData.

@Override
public List<FavoriteStatistics> getFavoritesData(DataStatisticsEventType eventType, int pageSize, SortOrder sortOrder, String username) {
    Assert.notNull(eventType, "Data statistics event type cannot be null");
    Assert.notNull(sortOrder, "Sort order cannot be null");
    String sql = "select c.uid, views, c.name, c.created from ( " + "select favoriteuid as uid, count(favoriteuid) as views " + "from datastatisticsevent where eventtype = '" + eventType.name() + "' ";
    if (username != null) {
        sql += "and username = ? ";
    }
    sql += "group by uid) as events " + "inner join " + escapeSql(eventType.getTable()) + " c on c.uid = events.uid " + "order by events.views " + escapeSql(sortOrder.getValue()) + " " + "limit ?;";
    PreparedStatementSetter pss = (ps) -> {
        int i = 1;
        if (username != null) {
            ps.setString(i++, username);
        }
        ps.setInt(i++, pageSize);
    };
    return jdbcTemplate.query(sql, pss, (rs, i) -> {
        FavoriteStatistics stats = new FavoriteStatistics();
        stats.setPosition(i + 1);
        stats.setId(rs.getString("uid"));
        stats.setName(rs.getString("name"));
        stats.setCreated(rs.getDate("created"));
        stats.setViews(rs.getInt("views"));
        return stats;
    });
}
Also used : SortOrder(org.hisp.dhis.analytics.SortOrder) HibernateGenericStore(org.hisp.dhis.hibernate.HibernateGenericStore) DataStatisticsEventStore(org.hisp.dhis.datastatistics.DataStatisticsEventStore) Date(java.util.Date) PreparedStatementSetter(org.springframework.jdbc.core.PreparedStatementSetter) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) SessionFactory(org.hibernate.SessionFactory) HashMap(java.util.HashMap) DataStatisticsEventType(org.hisp.dhis.datastatistics.DataStatisticsEventType) COUNT_PASSIVE_DASHBOARD_VIEWS_IN_USAGE_ANALYTICS(org.hisp.dhis.setting.SettingKey.COUNT_PASSIVE_DASHBOARD_VIEWS_IN_USAGE_ANALYTICS) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) List(java.util.List) Lists(com.google.common.collect.Lists) DateUtils.asSqlDate(org.hisp.dhis.util.DateUtils.asSqlDate) Map(java.util.Map) SqlUtils.escapeSql(org.hisp.dhis.system.util.SqlUtils.escapeSql) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) Repository(org.springframework.stereotype.Repository) FavoriteStatistics(org.hisp.dhis.datastatistics.FavoriteStatistics) SystemSettingManager(org.hisp.dhis.setting.SystemSettingManager) DataStatisticsEvent(org.hisp.dhis.datastatistics.DataStatisticsEvent) Assert(org.springframework.util.Assert) FavoriteStatistics(org.hisp.dhis.datastatistics.FavoriteStatistics) PreparedStatementSetter(org.springframework.jdbc.core.PreparedStatementSetter)

Example 20 with PreparedStatementSetter

use of org.springframework.jdbc.core.PreparedStatementSetter in project spring-security by spring-projects.

the class JdbcOAuth2AuthorizedClientService method insertAuthorizedClient.

private void insertAuthorizedClient(OAuth2AuthorizedClient authorizedClient, Authentication principal) {
    List<SqlParameterValue> parameters = this.authorizedClientParametersMapper.apply(new OAuth2AuthorizedClientHolder(authorizedClient, principal));
    try (LobCreator lobCreator = this.lobHandler.getLobCreator()) {
        PreparedStatementSetter pss = new LobCreatorArgumentPreparedStatementSetter(lobCreator, parameters.toArray());
        this.jdbcOperations.update(SAVE_AUTHORIZED_CLIENT_SQL, pss);
    }
}
Also used : SqlParameterValue(org.springframework.jdbc.core.SqlParameterValue) PreparedStatementSetter(org.springframework.jdbc.core.PreparedStatementSetter) ArgumentPreparedStatementSetter(org.springframework.jdbc.core.ArgumentPreparedStatementSetter) LobCreator(org.springframework.jdbc.support.lob.LobCreator)

Aggregations

PreparedStatementSetter (org.springframework.jdbc.core.PreparedStatementSetter)22 SQLException (java.sql.SQLException)13 PreparedStatement (java.sql.PreparedStatement)12 List (java.util.List)5 Timestamp (java.sql.Timestamp)4 Date (java.util.Date)4 ArgumentPreparedStatementSetter (org.springframework.jdbc.core.ArgumentPreparedStatementSetter)4 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)4 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)3 Map (java.util.Map)3 SqlParameterValue (org.springframework.jdbc.core.SqlParameterValue)3 Lists (com.google.common.collect.Lists)2 ResultSet (java.sql.ResultSet)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 DataSource (javax.sql.DataSource)2 SessionFactory (org.hibernate.SessionFactory)2 SortOrder (org.hisp.dhis.analytics.SortOrder)2 DataStatisticsEvent (org.hisp.dhis.datastatistics.DataStatisticsEvent)2