Example usage for org.hibernate SQLQuery setLong

List of usage examples for org.hibernate SQLQuery setLong

Introduction

In this page you can find the example usage for org.hibernate SQLQuery setLong.

Prototype

@Deprecated
@SuppressWarnings("unchecked")
default Query<R> setLong(int position, long val) 

Source Link

Document

Bind a positional long-valued parameter.

Usage

From source file:com.thoughtworks.go.server.persistence.MaterialRepository.java

License:Apache License

public boolean hasPipelineEverRunWith(final String pipelineName, final MaterialRevisions revisions) {
    return (Boolean) getHibernateTemplate().execute((HibernateCallback) session -> {
        int numberOfMaterials = revisions.getRevisions().size();
        int match = 0;
        for (MaterialRevision revision : revisions) {
            long materialId = findOrCreateFrom(revision.getMaterial()).getId();
            long modificationId = revision.getLatestModification().getId();
            String key = cacheKeyForHasPipelineEverRunWithModification(pipelineName, materialId,
                    modificationId);//  w w w.j a v  a2  s.  c o m
            if (goCache.get(key) != null) {
                match++;
                continue;
            }
            String sql = "SELECT materials.id" + " FROM pipelineMaterialRevisions"
                    + " INNER JOIN pipelines ON pipelineMaterialRevisions.pipelineId = pipelines.id"
                    + " INNER JOIN modifications on modifications.id  = pipelineMaterialRevisions.torevisionId"
                    + " INNER JOIN materials on modifications.materialId = materials.id"
                    + " WHERE materials.id = ? AND pipelineMaterialRevisions.toRevisionId >= ? AND pipelineMaterialRevisions.fromRevisionId <= ? AND pipelines.name = ?"
                    + " GROUP BY materials.id;";
            SQLQuery query = session.createSQLQuery(sql);
            query.setLong(0, materialId);
            query.setLong(1, modificationId);
            query.setLong(2, modificationId);
            query.setString(3, pipelineName);
            if (!query.list().isEmpty()) {
                match++;
                goCache.put(key, Boolean.TRUE);
            }
        }
        return match == numberOfMaterials;
    });
}

From source file:com.thoughtworks.go.server.persistence.PipelineRepository.java

License:Apache License

public static int updateNaturalOrderForPipeline(Session session, Long pipelineId, double naturalOrder) {
    String sql = "UPDATE pipelines SET naturalOrder = :naturalOrder WHERE id = :pipelineId";
    SQLQuery query = session.createSQLQuery(sql);
    query.setLong("pipelineId", pipelineId);
    query.setDouble("naturalOrder", naturalOrder);
    return query.executeUpdate();
}

From source file:com.thoughtworks.go.server.persistence.PipelineRepository.java

License:Apache License

@SuppressWarnings({ "unchecked" })
public void updatePipelineTimeline(final PipelineTimeline pipelineTimeline,
        final List<PipelineTimelineEntry> tempEntriesForRollback) {
    getHibernateTemplate().execute(new HibernateCallback() {
        private static final int PIPELINE_NAME = 0;
        private static final int ID = 1;
        private static final int COUNTER = 2;
        private static final int MODIFIED_TIME = 3;
        private static final int FINGERPRINT = 4;
        private static final int NATURAL_ORDER = 5;
        private static final int REVISION = 6;
        private static final int FOLDER = 7;
        private static final int MOD_ID = 8;
        private static final int PMR_ID = 9;

        public Object doInHibernate(Session session) throws HibernateException {
            LOGGER.info("Start updating pipeline timeline");
            List<Object[]> matches = retrieveTimeline(session, pipelineTimeline);
            List<PipelineTimelineEntry> newPipelines = populateFrom(matches);
            addEntriesToPipelineTimeline(newPipelines, pipelineTimeline, tempEntriesForRollback);

            updateNaturalOrdering(session, newPipelines);
            LOGGER.info("Pipeline timeline updated");
            return null;
        }//from   w  w w  .  j ava2  s . co m

        private void updateNaturalOrdering(Session session, List<PipelineTimelineEntry> pipelines) {
            for (PipelineTimelineEntry pipeline : pipelines) {
                if (pipeline.hasBeenUpdated()) {
                    updateNaturalOrderForPipeline(session, pipeline.getId(), pipeline.naturalOrder());
                }
            }
        }

        private List<Object[]> loadTimeline(SQLQuery query) {
            long startedAt = System.currentTimeMillis();
            List<Object[]> matches = (List<Object[]>) query.list();
            long duration = System.currentTimeMillis() - startedAt;
            if (duration > 1000) {
                LOGGER.warn("updating in memory pipeline-timeline took: {} ms", duration);
            }
            return matches;
        }

        private List<Object[]> retrieveTimeline(Session session, PipelineTimeline pipelineTimeline) {
            SQLQuery query = session.createSQLQuery(queryExtensions.retrievePipelineTimeline());
            query.setLong(0, pipelineTimeline.maximumId());

            List<Object[]> matches = loadTimeline(query);
            sortTimeLineByPidAndPmrId(matches);
            return matches;
        }

        private void sortTimeLineByPidAndPmrId(List<Object[]> matches) {
            matches.sort((m1, m2) -> {
                long id1 = id(m1);
                long id2 = id(m2);
                if (id1 == id2) {
                    return (int) (pmrId(m1) - pmrId(m2));
                }
                return (int) (id1 - id2);
            });
        }

        private List<PipelineTimelineEntry> populateFrom(List<Object[]> matches) {
            ArrayList<PipelineTimelineEntry> newPipelines = new ArrayList<>();
            if (matches.isEmpty()) {
                return newPipelines;
            }

            Map<String, List<PipelineTimelineEntry.Revision>> revisions = new HashMap<>();

            String name = null;
            long curId = -1;
            Integer counter = null;
            double naturalOrder = 0.0;

            PipelineTimelineEntry entry = null;

            for (int i = 0; i < matches.size(); i++) {
                Object[] row = matches.get(i);
                long id = id(row);
                if (curId != id) {
                    name = pipelineName(row);
                    curId = id;
                    counter = counter(row);
                    revisions = new HashMap<>();
                    naturalOrder = naturalOrder(row);
                }

                String fingerprint = fingerprint(row);

                if (!revisions.containsKey(fingerprint)) {
                    revisions.put(fingerprint, new ArrayList<>());
                }
                revisions.get(fingerprint).add(rev(row));

                int nextI = i + 1;
                if (((nextI < matches.size() && id(matches.get(nextI)) != curId) || //new pipeline instance starts in next record, so capture this one
                nextI == matches.size())) {//this is the last record, so capture it
                    entry = new PipelineTimelineEntry(name, curId, counter, revisions, naturalOrder);
                    newPipelines.add(entry);
                }
            }
            return newPipelines;
        }

        private String folder(Object[] row) {
            return (String) row[FOLDER];
        }

        private PipelineTimelineEntry.Revision rev(Object[] row) {
            return new PipelineTimelineEntry.Revision(modifiedTime(row), stringRevision(row), folder(row),
                    modId(row));
        }

        private long pmrId(Object[] row) {
            return ((BigInteger) row[PMR_ID]).longValue();
        }

        private long modId(Object[] row) {
            return ((BigInteger) row[MOD_ID]).longValue();
        }

        private double naturalOrder(Object[] row) {
            return (Double) row[NATURAL_ORDER];
        }

        private Date modifiedTime(Object[] row) {
            return (Date) row[MODIFIED_TIME];
        }

        private String stringRevision(Object[] row) {
            return (String) row[REVISION];
        }

        private String fingerprint(Object[] row) {
            return String.valueOf(row[FINGERPRINT]);
        }

        private String pipelineName(Object[] row) {
            return (String) row[PIPELINE_NAME];
        }

        private int counter(Object[] row) {
            return row[COUNTER] == null ? -1 : ((BigInteger) row[COUNTER]).intValue();
        }

        private long id(Object[] first) {
            return ((BigInteger) first[ID]).longValue();
        }
    });
}

From source file:com.tysanclan.site.projectewok.util.forum.MemberForumViewContext.java

License:Open Source License

@Override
public int countCategories(Session sess, User viewer) {
    StringBuilder q = new StringBuilder();
    q.append("SELECT COUNT(*) FROM FORUMCATEGORY FC ");
    q.append("WHERE EXISTS (");

    q.append("SELECT * FROM FORUM f ");
    q.append(//from  w ww. j  ava  2  s  . c  om
            "WHERE (f.DTYPE!='GroupForum' OR f.group_id IN (SELECT group_id FROM groupmembers WHERE user_id = :viewer)) AND f.category_id = FC.id");

    q.append(")");

    SQLQuery query = sess.createSQLQuery(q.toString());
    query.setLong("viewer", viewer.getId());

    return count(query);
}

From source file:com.tysanclan.site.projectewok.util.forum.MemberForumViewContext.java

License:Open Source License

@Override
public List<ForumCategory> getCategories(Session sess, User viewer, long offset, long count) {
    StringBuilder q = new StringBuilder();
    q.append("SELECT * FROM FORUMCATEGORY FC ");
    q.append("WHERE EXISTS (");

    q.append("SELECT * FROM FORUM f ");
    q.append(//from   w ww  .j  a v  a2s  .com
            "WHERE (f.DTYPE!='GroupForum' OR f.group_id IN (SELECT group_id FROM groupmembers WHERE user_id = :viewer)) AND f.category_id = FC.id");

    q.append(") ORDER BY id ASC LIMIT :count OFFSET :offset");

    SQLQuery query = sess.createSQLQuery(q.toString());
    query.setLong("count", count);
    query.setLong("offset", offset);
    query.setLong("viewer", viewer.getId());
    query.addEntity(ForumCategory.class);

    return listOf(query);
}

From source file:com.tysanclan.site.projectewok.util.forum.MemberForumViewContext.java

License:Open Source License

@Override
public int countForums(Session sess, ForumCategory context, User viewer) {

    StringBuilder q = new StringBuilder();
    q.append("SELECT COUNT(*) FROM FORUM f ");
    q.append(//from   ww w  . j  av a  2  s. c  o  m
            "WHERE (f.DTYPE!='GroupForum' OR f.group_id IN (SELECT group_id FROM groupmembers WHERE user_id = :viewer)) AND f.category_id = :cat");

    SQLQuery query = sess.createSQLQuery(q.toString());
    query.setLong("cat", context.getId());
    query.setLong("viewer", viewer.getId());

    return count(query);
}

From source file:com.tysanclan.site.projectewok.util.forum.MemberForumViewContext.java

License:Open Source License

@Override
public List<Forum> getForums(Session sess, ForumCategory context, User viewer, long offset, long count) {
    StringBuilder q = new StringBuilder();
    q.append("SELECT * FROM FORUM f ");
    q.append(// w w  w. ja  va 2  s.co m
            "WHERE (f.DTYPE!='GroupForum' OR f.group_id IN (SELECT group_id FROM groupmembers WHERE user_id = :viewer)) AND f.category_id = :cat ");
    q.append("ORDER BY position ASC ");
    q.append("LIMIT :count OFFSET :offset");

    SQLQuery query = sess.createSQLQuery(q.toString());
    query.setLong("cat", context.getId());
    query.setLong("count", count);
    query.setLong("offset", offset);
    query.setLong("viewer", viewer.getId());
    query.addEntity(Forum.class);

    return listOf(query);
}

From source file:com.tysanclan.site.projectewok.util.forum.MemberForumViewContext.java

License:Open Source License

@Override
public int countThreads(Session sess, Forum context, User viewer) {

    StringBuilder q = new StringBuilder();

    q.append("SELECT COUNT(*) FROM FORUMTHREAD FT WHERE ft.forum_id = :forum AND ft.shadow = false AND ");
    q.append("NOT EXISTS ");
    q.append("(SELECT * FROM trial WHERE trialthread_id = ft.id AND accused_id != :user1 ");
    q.append(//from   www .j  a  v a2 s.  com
            "AND NOT EXISTS (SELECT * FROM tuser WHERE Rank IN ('CHANCELLOR', 'SENATOR', 'TRUTHSAYER') AND id=:user2)) AND ");
    q.append("EXISTS (SELECT * FROM FORUMPOST FP WHERE fp.shadow = false AND fp.thread_id = ft.id)");

    SQLQuery query = sess.createSQLQuery(q.toString());
    query.setLong("forum", context.getId());
    query.setLong("user1", viewer.getId());
    query.setLong("user2", viewer.getId());

    return count(query);

}

From source file:com.tysanclan.site.projectewok.util.forum.MemberForumViewContext.java

License:Open Source License

@Override
public List<ForumThread> getThreads(Session sess, Forum context, User viewer, long offset, long count) {
    StringBuilder q = new StringBuilder();

    q.append("SELECT * FROM FORUMTHREAD FT WHERE ft.forum_id = :forum AND ft.shadow = false AND ");
    q.append("EXISTS (SELECT * FROM FORUMPOST FP WHERE fp.shadow = false AND fp.thread_id = ft.id) ");
    q.append("AND NOT EXISTS ");
    q.append("(SELECT * FROM trial WHERE trialthread_id = ft.id AND accused_id != :user1 ");
    q.append(/*from   ww w. j ava  2 s.c om*/
            "AND NOT EXISTS (SELECT * FROM tuser WHERE Rank IN ('CHANCELLOR', 'SENATOR', 'TRUTHSAYER') AND id=:user2)) ");
    q.append("ORDER BY STICKY DESC, lastPost DESC ");
    q.append("LIMIT :count OFFSET :offset");

    SQLQuery query = sess.createSQLQuery(q.toString());
    query.setLong("forum", context.getId());
    query.setLong("count", count);
    query.setLong("offset", offset);
    query.setLong("user1", viewer.getId());
    query.setLong("user2", viewer.getId());
    query.addEntity(ForumThread.class);

    return listOf(query);
}

From source file:com.tysanclan.site.projectewok.util.forum.MemberForumViewContext.java

License:Open Source License

@Override
public int countPosts(Session sess, ForumThread context, User viewer) {
    StringBuilder q = new StringBuilder();

    q.append("SELECT COUNT(*) FROM FORUMPOST FP WHERE fp.shadow = false AND fp.thread_id = :thread");

    SQLQuery query = sess.createSQLQuery(q.toString());
    query.setLong("thread", context.getId());

    return count(query);
}