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.

NestedConditionsTest.java 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright (c) 2009-2014, Architector Inc., Japan
  3. * All rights reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.iciql.test;
  18. import com.iciql.Db;
  19. import com.iciql.IciqlException;
  20. import com.iciql.NestedConditions.And;
  21. import com.iciql.NestedConditions.Or;
  22. import com.iciql.QueryWhere;
  23. import com.iciql.test.models.Customer;
  24. import org.junit.After;
  25. import org.junit.Before;
  26. import org.junit.Test;
  27. import java.util.List;
  28. import java.util.Set;
  29. import java.util.TreeSet;
  30. import static org.junit.Assert.assertEquals;
  31. import static org.junit.Assert.assertTrue;
  32. public class NestedConditionsTest {
  33. enum Region {
  34. JP, FR
  35. }
  36. private Db db;
  37. @Before
  38. public void setUp() {
  39. db = IciqlSuite.openNewDb();
  40. db.insertAll(Customer.getList());
  41. }
  42. @After
  43. public void tearDown() {
  44. db.close();
  45. }
  46. private String search(Region region, String... customerIds) {
  47. Customer model;
  48. QueryWhere<Customer> query;
  49. model = new Customer();
  50. query = db.from(model).whereTrue();
  51. if (customerIds != null && customerIds.length > 0) {
  52. query.andOpen();
  53. for (String value : customerIds) {
  54. query.or(model.customerId).is(value);
  55. }
  56. query.close();
  57. }
  58. if (region != null) {
  59. query.and(model.region).is(region.name());
  60. }
  61. return query.toSQL();
  62. }
  63. @Test
  64. public void andOrSyntaxTest() {
  65. String Customer = db.getDialect().prepareTableName(null, "Customer");
  66. String customerId = db.getDialect().prepareColumnName("customerId");
  67. String region = db.getDialect().prepareColumnName("region");
  68. String trueValue = db.getDialect().prepareStringParameter(true);
  69. assertEquals(
  70. String.format("SELECT * FROM %s WHERE (%s)", Customer, trueValue),
  71. search(null, (String[]) null));
  72. assertEquals(
  73. String.format("SELECT * FROM %s WHERE (%s)", Customer, trueValue),
  74. search(null, new String[0]));
  75. assertEquals(
  76. String.format("SELECT * FROM %s WHERE (%s) AND ( %s = '0001' )",
  77. Customer, trueValue, customerId),
  78. search(null, "0001"));
  79. assertEquals(
  80. String.format("SELECT * FROM %s WHERE (%s) AND ( %s = '0001' OR %s = '0002' )",
  81. Customer, trueValue, customerId, customerId),
  82. search(null, "0001", "0002"));
  83. assertEquals(
  84. String.format("SELECT * FROM %s WHERE (%s) AND %s = 'JP'",
  85. Customer, trueValue, region),
  86. search(Region.JP, (String[]) null));
  87. assertEquals(
  88. String.format("SELECT * FROM %s WHERE (%s) AND %s = 'JP'",
  89. Customer, trueValue, region),
  90. search(Region.JP, new String[0]));
  91. assertEquals(
  92. String.format("SELECT * FROM %s WHERE (%s) AND ( %s = '0001' ) AND %s = 'JP'",
  93. Customer, trueValue, customerId, region),
  94. search(Region.JP, "0001"));
  95. assertEquals(
  96. String.format("SELECT * FROM %s WHERE (%s) AND ( %s = '0001' OR %s = '0002' ) AND %s = 'JP'",
  97. Customer, trueValue, customerId, customerId, region),
  98. search(Region.JP, "0001", "0002"));
  99. }
  100. @Test
  101. public void errorTest() {
  102. Customer model;
  103. model = new Customer();
  104. try {
  105. db.from(model)
  106. .where(model.customerId).is("0001")
  107. .andOpen()
  108. .or(model.region).is("FR")
  109. .or(model.region).is("JP")
  110. .close()
  111. .toSQL();
  112. assertTrue(true);
  113. } catch (IciqlException error) {
  114. assertTrue(false);
  115. }
  116. try {
  117. db.from(model)
  118. .where(model.customerId).is("0001")
  119. .andOpen()
  120. .or(model.region).is("FR")
  121. .or(model.region).is("JP")
  122. .toSQL();
  123. assertTrue(false);
  124. } catch (IciqlException error) {
  125. assertTrue(true);
  126. }
  127. try {
  128. db.from(model)
  129. .where(model.customerId).is("0001")
  130. .andOpen()
  131. .or(model.region).is("FR")
  132. .or(model.region).is("JP")
  133. .close()
  134. .close();
  135. assertTrue(false);
  136. } catch (IciqlException error) {
  137. assertTrue(true);
  138. }
  139. }
  140. @Test
  141. public void fluentSyntaxTest() {
  142. String Customer = db.getDialect().prepareTableName(null, "Customer");
  143. String customerId = db.getDialect().prepareColumnName("customerId");
  144. String region = db.getDialect().prepareColumnName("region");
  145. String trueValue = db.getDialect().prepareStringParameter(true);
  146. String falseValue = db.getDialect().prepareStringParameter(false);
  147. final Customer model = new Customer();
  148. assertEquals(
  149. String.format("SELECT * FROM %s WHERE (%s) AND %s = '0001' AND ( %s = 'CA' OR %s = 'LA' )",
  150. Customer, trueValue, customerId, region, region),
  151. db.from(model).where(new And<Customer>(db, model) {{
  152. and(model.customerId).is("0001");
  153. and(new Or<Customer>(db, model) {{
  154. or(model.region).is("CA");
  155. or(model.region).is("LA");
  156. }});
  157. }})
  158. .toSQL());
  159. assertEquals(
  160. String.format("SELECT * FROM %s WHERE (%s) OR %s = '0001' OR ( %s = '0002' AND %s = 'LA' )",
  161. Customer, falseValue, customerId, customerId, region),
  162. db.from(model).where(new Or<Customer>(db, model) {{
  163. or(model.customerId).is("0001");
  164. or(new And<Customer>(db, model) {{
  165. and(model.customerId).is("0002");
  166. and(model.region).is("LA");
  167. }});
  168. }})
  169. .toSQL());
  170. assertEquals(
  171. String.format("SELECT * FROM %s WHERE (%s) OR ( %s = '0001' AND %s = 'WA' ) OR ( %s = '0002' AND %s = 'LA' )",
  172. Customer, falseValue, customerId, region, customerId, region),
  173. db.from(model).where(new Or<Customer>(db, model) {{
  174. or(new And<Customer>(db, model) {{
  175. and(model.customerId).is("0001");
  176. and(model.region).is("WA");
  177. }});
  178. or(new And<Customer>(db, model) {{
  179. and(model.customerId).is("0002");
  180. and(model.region).is("LA");
  181. }});
  182. }})
  183. .toSQL());
  184. assertEquals(
  185. String.format("SELECT * FROM %s WHERE %s = '0001' OR ( %s = '0002' AND %s = 'LA' )",
  186. Customer, customerId, customerId, region),
  187. db.from(model).where(model.customerId).is("0001")
  188. .or(new And<Customer>(db, model) {{
  189. and(model.customerId).is("0002");
  190. and(model.region).is("LA");
  191. }})
  192. .toSQL());
  193. assertEquals(
  194. String.format("SELECT * FROM %s WHERE %s IS NOT NULL AND ( %s = 'LA' OR %s = 'CA' OR %s = 'WA' )",
  195. Customer, customerId, region, region, region),
  196. db.from(model)
  197. .where(model.customerId).isNotNull()
  198. .and(new Or<Customer>(db, model) {{
  199. or(model.region).is("LA");
  200. or(model.region).is("CA");
  201. or(model.region).is("WA");
  202. }})
  203. .toSQL());
  204. }
  205. @Test
  206. public void compoundConditionsTest() {
  207. final Customer c = new Customer();
  208. List<Customer> matches = db.from(c)
  209. .where(c.customerId).like("A%")
  210. .and(c.region).isNotNull()
  211. .and(new Or<Customer>(db, c) {{
  212. or(c.region).is("LA");
  213. or(c.region).is("CA");
  214. }}).select();
  215. assertEquals(2, matches.size());
  216. Set<String> ids = new TreeSet<String>();
  217. for (Customer customer : matches) {
  218. ids.add(customer.customerId);
  219. }
  220. assertEquals("[ANTON, ASLAN]", ids.toString());
  221. }
  222. }