@Test public void map2() { List<Map<Integer, String>> results = MAP2_RESULTS.transform( groupBy(postId).list(map(commentId, commentText))); assertEquals(4, results.size()); Map<Integer, String> comments = results.get(1); assertEquals(3, comments.size()); assertEquals("comment 2", comments.get(2)); }
@Test public void map1() { List<Group> results = MAP_RESULTS.transform( groupBy(postId).list(postName, map(commentId, commentText))); assertEquals(4, results.size()); Group group = results.get(1); Map<Integer, String> comments = group.getMap(commentId, commentText); assertEquals(3, comments.size()); assertEquals("comment 2", comments.get(2)); }
@Test public void group_order() { List<Group> results = BASIC_RESULTS .transform(groupBy(postId).list(postName, set(commentId))); assertEquals(4, results.size()); }
@Test(expected = ClassCastException.class) public void classCastException() { List<Group> results = BASIC_RESULTS.transform( groupBy(postId).list(postName, set(commentId), list(commentText))); assertEquals(4, results.size()); Group group = results.get(1); group.getList(commentId); }
@Test(expected = NoSuchElementException.class) public void noSuchElementException() { List<Group> results = BASIC_RESULTS.transform( groupBy(postId).list(postName, set(commentId), list(commentText))); assertEquals(4, results.size()); Group group = results.get(1); group.getSet(qComment); }
@Test public void map3() { List<Map<Integer, Map<Integer, String>>> actual = MAP3_RESULTS.transform( groupBy(postId).list(map(postId, map(commentId, commentText)))); Object postId = null; Map<Integer, Map<Integer, String>> posts = null; List<Map<Integer, Map<Integer, String>>> expected = new LinkedList<Map<Integer, Map<Integer, String>>>(); for (Iterator<Tuple> iterator = MAP3_RESULTS.iterate(); iterator.hasNext();) { Tuple tuple = iterator.next(); Object[] array = tuple.toArray(); if (posts == null || !(postId == array[0] || postId != null && postId.equals(array[0]))) { posts = new LinkedHashMap<Integer, Map<Integer,String>>(); expected.add(posts); } postId = array[0]; @SuppressWarnings("unchecked") Pair<Integer, Pair<Integer, String>> pair = (Pair<Integer, Pair<Integer, String>>) array[1]; Integer first = pair.getFirst(); Map<Integer, String> comments = posts.get(first); if (comments == null) { comments = new LinkedHashMap<Integer, String>(); posts.put(first, comments); } Pair<Integer, String> second = pair.getSecond(); comments.put(second.getFirst(), second.getSecond()); } assertEquals(expected.toString(), actual.toString()); }
@Test public void array_access() { List<Group> results = BASIC_RESULTS.transform( groupBy(postId).list(postName, set(commentId), list(commentText))); assertEquals(4, results.size()); Group group = results.get(1); Object[] array = group.toArray(); assertEquals(toInt(1), array[0]); assertEquals("post 1", array[1]); assertEquals(toSet(1, 2, 3), array[2]); assertEquals(Arrays.asList("comment 1", "comment 2", "comment 3"), array[3]); }
@Test public void first_set_and_list() { List<Group> results = BASIC_RESULTS.transform( groupBy(postId).list(postName, set(commentId), list(commentText))); assertEquals(4, results.size()); Group group = results.get(1); assertEquals(toInt(1), group.getOne(postId)); assertEquals("post 1", group.getOne(postName)); assertEquals(toSet(1, 2, 3), group.getSet(commentId)); assertEquals(Arrays.asList("comment 1", "comment 2", "comment 3"), group.getList(commentText)); }
@Test public void group_by_null() { List<Group> results = BASIC_RESULTS.transform( groupBy(postId).list(postName, set(commentId), list(commentText))); assertEquals(4, results.size()); Group group = results.get(0); assertNull(group.getOne(postId)); assertEquals("null post", group.getOne(postName)); assertEquals(toSet(7, 8), group.getSet(commentId)); assertEquals(Arrays.asList("comment 7", "comment 8"), group.getList(commentText)); }
@Test public void transform_results() { List<Post> results = POST_W_COMMENTS.transform( groupBy(postId).list(Projections.constructor(Post.class, postId, postName, set(qComment)))); assertEquals(4, results.size()); Post post = results.get(1); assertNotNull(post); assertEquals(toInt(1), post.getId()); assertEquals("post 1", post.getName()); assertEquals(toSet(comment(1), comment(2), comment(3)), post.getComments()); }
@Test public void oneToOneToMany_projection() { List<User> results = USERS_W_LATEST_POST_AND_COMMENTS.transform( groupBy(userName).list(Projections.constructor(User.class, userName, Projections.constructor(Post.class, postId, postName, set(qComment))))); assertEquals(2, results.size()); User user = results.get(0); Post post = user.getLatestPost(); assertEquals(toInt(2), post.getId()); assertEquals("post 2", post.getName()); assertEquals(toSet(comment(4), comment(5)), post.getComments()); }
@Test public void transform_as_bean() { List<Post> results = POST_W_COMMENTS.transform( groupBy(postId).list(Projections.bean(Post.class, postId, postName, set(qComment).as("comments")))); assertEquals(4, results.size()); Post post = results.get(1); assertNotNull(post); assertEquals(toInt(1), post.getId()); assertEquals("post 1", post.getName()); assertEquals(toSet(comment(1), comment(2), comment(3)), post.getComments()); }
@Test public void oneToOneToMany_projection_as_bean_and_constructor() { List<User> results = USERS_W_LATEST_POST_AND_COMMENTS.transform( groupBy(userName).list(Projections.bean(User.class, userName, Projections.constructor(Post.class, postId, postName, set(qComment)).as("latestPost")))); assertEquals(2, results.size()); User user = results.get(0); Post post = user.getLatestPost(); assertEquals(toInt(2), post.getId()); assertEquals("post 2", post.getName()); assertEquals(toSet(comment(4), comment(5)), post.getComments()); }
@Test public void oneToOneToMany_projection_as_bean() { List<User> results = USERS_W_LATEST_POST_AND_COMMENTS.transform( groupBy(userName).list(Projections.bean(User.class, userName, Projections.bean(Post.class, postId, postName, set(qComment).as("comments")).as("latestPost")))); assertEquals(2, results.size()); User user = results.get(0); Post post = user.getLatestPost(); assertEquals(toInt(2), post.getId()); assertEquals("post 2", post.getName()); assertEquals(toSet(comment(4), comment(5)), post.getComments()); }
public EntityVersionStoreJdbc(EntityStoreOptions<Id, M, V> options) { super(options); Expression<?>[] values = concat(versionAndParentColumns, options.sinceVersion.localOrdinal); versionAndParentsSince = groupBy(options.version.revision).list(values); }
public AbstractVersionStoreJdbc(Options options) { this.options = options; versionAndParentColumns = without(concat(options.version.all(), GroupBy.set(options.parent.parentRevision)), options.version.revision); versionAndParents = groupBy(options.version.revision).list(versionAndParentColumns); Expression<?>[] propertyColumns = without(options.property.all(), options.property.revision); properties = groupBy(options.property.revision).as(GroupBy.list(tuple(propertyColumns))); this.cache = options.cacheBuilder.apply(this); this.cacheLoader = this.cache != null ? this.cache::load : this::getOptimizedGraph; }
protected List<Group> versionsAndParentsSince(Id docId, Revision since) { SQLQuery<?> qry = options.queryFactory.from(options.sinceVersion); // Left join version version on version.ordinal > since.ordinal and version.doc_id = since.doc_id qry.leftJoin(options.version).on( options.version.ordinal.gt(options.sinceVersion.ordinal), predicate(EQ, options.version.docId, options.sinceVersion.docId)); // Left join parents qry.leftJoin(options.parent).on(options.parent.revision.eq(options.version.revision)); qry.where(options.sinceVersion.revision.eq(since), // Return "since" row even if there is no newer versions versionsOf(docId).or(predicate(IS_NULL, options.version.docId))); qry.orderBy(options.version.ordinal.asc()); return verifyVersionsAndParentsSince(qry.transform(groupBy(options.version.revision).list(versionAndParentsSince)), since); }