You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JoinTest.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright 2011 James Moger.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.iciql.test;
  17. import com.iciql.Db;
  18. import com.iciql.Iciql.IQColumn;
  19. import com.iciql.Iciql.IQTable;
  20. import com.iciql.QueryWhere;
  21. import org.junit.After;
  22. import org.junit.Before;
  23. import org.junit.Test;
  24. import java.util.Arrays;
  25. import java.util.List;
  26. import static org.junit.Assert.assertEquals;
  27. /**
  28. * Tests of Joins.
  29. */
  30. public class JoinTest {
  31. Db db;
  32. @Before
  33. public void setup() {
  34. db = IciqlSuite.openNewDb();
  35. db.insertAll(UserId.getList());
  36. db.insertAll(UserNote.getList());
  37. }
  38. @After
  39. public void tearDown() {
  40. db.close();
  41. }
  42. @Test
  43. public void testPrimitiveJoin() throws Exception {
  44. final UserId u = new UserId();
  45. final UserNote n = new UserNote();
  46. List<UserNote> notes = db.from(u).innerJoin(n).on(u.id).is(n.userId).where(u.id).is(2)
  47. .select(new UserNote() {
  48. {
  49. userId = n.userId;
  50. id = n.id;
  51. text = n.text;
  52. }
  53. });
  54. assertEquals(3, notes.size());
  55. }
  56. @Test
  57. public void testPrimitiveJoinAndSimpleSelect() throws Exception {
  58. final UserId u = new UserId();
  59. final UserNote n = new UserNote();
  60. List<UserId> userIds = db.from(u).innerJoin(n).on(u.id).is(n.userId).where(n.text).is("D")
  61. .select();
  62. assertEquals(1, userIds.size());
  63. assertEquals(1, userIds.get(0).id);
  64. }
  65. @Test
  66. public void testJoin() throws Exception {
  67. final UserId u = new UserId();
  68. final UserNote n = new UserNote();
  69. // this query returns 1 UserId if the user has a note
  70. // it's purpose is to confirm fluency/type-safety on a very simple
  71. // join case where the main table is filtered/reduced by hits in a
  72. // related table
  73. List<UserId> users = db.from(u).innerJoin(n).on(u.id).is(n.userId).where(u.id).is(2).selectDistinct();
  74. assertEquals(1, users.size());
  75. assertEquals(2, users.get(0).id);
  76. }
  77. @Test
  78. public void testLeftJoin() throws Exception {
  79. final UserId u = new UserId();
  80. final UserNote n = new UserNote();
  81. List<UserId> notes = db.from(u).leftJoin(n).on(u.id).is(n.userId).where(u.id).is(4).select();
  82. assertEquals(1, notes.size());
  83. assertEquals(4, notes.get(0).id);
  84. }
  85. @Test
  86. public void testSubQuery() throws Exception {
  87. final UserId u = new UserId();
  88. final UserNote n = new UserNote();
  89. QueryWhere<UserId> q = db.from(u).where(u.id).in(db.from(n).where(n.userId).exceeds(0).subQuery(n.userId));
  90. List<UserId> notes = q.select();
  91. assertEquals(3, notes.size());
  92. // do not test MySQL on this statement because the databases
  93. if (IciqlSuite.isMySQL(db)) {
  94. assertEquals("SELECT * FROM UserId WHERE `id` in (SELECT `userId` FROM UserNote WHERE `userId` > 0 )", q.toSQL());
  95. } else {
  96. assertEquals("SELECT * FROM UserId WHERE id in (SELECT userId FROM UserNote WHERE userId > 0 )", q.toSQL());
  97. }
  98. }
  99. @IQTable
  100. public static class UserId {
  101. @IQColumn(primaryKey = true)
  102. public int id;
  103. @IQColumn(length = 10)
  104. public String name;
  105. public UserId() {
  106. // public constructor
  107. }
  108. public UserId(int id, String name) {
  109. this.id = id;
  110. this.name = name;
  111. }
  112. public String toString() {
  113. return name + " (" + id + ")";
  114. }
  115. public static List<UserId> getList() {
  116. UserId[] list = {new UserId(1, "Tom"), new UserId(2, "Dick"), new UserId(3, "Harry"), new UserId(4, "Jack")};
  117. return Arrays.asList(list);
  118. }
  119. }
  120. @IQTable
  121. public static class UserNote {
  122. @IQColumn(autoIncrement = true, primaryKey = true)
  123. public int id;
  124. @IQColumn
  125. public int userId;
  126. @IQColumn(length = 10)
  127. public String text;
  128. public UserNote() {
  129. // public constructor
  130. }
  131. public UserNote(int userId, String text) {
  132. this.userId = userId;
  133. this.text = text;
  134. }
  135. public String toString() {
  136. return text;
  137. }
  138. public static List<UserNote> getList() {
  139. UserNote[] list = {new UserNote(1, "A"), new UserNote(2, "B"), new UserNote(3, "C"),
  140. new UserNote(1, "D"), new UserNote(2, "E"), new UserNote(3, "F"), new UserNote(1, "G"),
  141. new UserNote(2, "H"), new UserNote(3, "I"),};
  142. return Arrays.asList(list);
  143. }
  144. }
  145. }