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.

QueryCondition.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright 2004-2011 H2 Group.
  3. * Copyright 2011 James Moger.
  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;
  18. import com.iciql.util.Utils;
  19. /**
  20. * This class represents a query with an incomplete condition.
  21. *
  22. * @param <T> the return type of the query
  23. * @param <A> the incomplete condition data type
  24. */
  25. public class QueryCondition<T, A> {
  26. private Query<T> query;
  27. private A x;
  28. QueryCondition(Query<T> query, A x) {
  29. this.query = query;
  30. this.x = x;
  31. }
  32. public <Q, Z> QueryWhere<T> in(SubQuery<Q, Z> q) {
  33. query.addConditionToken(new SubQueryCondition<A, Q, Z>(x, q));
  34. return new QueryWhere<T>(query);
  35. }
  36. public QueryWhere<T> oneOf(A... a) {
  37. return oneOf(Utils.newArrayIterable(a));
  38. }
  39. public QueryWhere<T> oneOf(Iterable<A> i) {
  40. query.addConditionToken(new Condition<A>(x, i, CompareType.IN));
  41. return new QueryWhere<T>(query);
  42. }
  43. public QueryWhere<T> noneOf(A... a) {
  44. return noneOf(Utils.newArrayIterable(a));
  45. }
  46. public QueryWhere<T> noneOf(Iterable<A> i) {
  47. query.addConditionToken(new Condition<A>(x, i, CompareType.NOT_IN));
  48. return new QueryWhere<T>(query);
  49. }
  50. public QueryWhere<T> is(A y) {
  51. if (y == null) {
  52. query.addConditionToken(new Condition<A>(x, CompareType.IS_NULL));
  53. } else {
  54. query.addConditionToken(new Condition<A>(x, y, CompareType.EQUAL));
  55. }
  56. return new QueryWhere<T>(query);
  57. }
  58. public QueryWhere<T> isNot(A y) {
  59. query.addConditionToken(new Condition<A>(x, y, CompareType.NOT_EQUAL));
  60. return new QueryWhere<T>(query);
  61. }
  62. public QueryWhere<T> isNull() {
  63. query.addConditionToken(new Condition<A>(x, CompareType.IS_NULL));
  64. return new QueryWhere<T>(query);
  65. }
  66. public QueryWhere<T> isNotNull() {
  67. query.addConditionToken(new Condition<A>(x, CompareType.IS_NOT_NULL));
  68. return new QueryWhere<T>(query);
  69. }
  70. public QueryWhere<T> exceeds(A y) {
  71. query.addConditionToken(new Condition<A>(x, y, CompareType.EXCEEDS));
  72. return new QueryWhere<T>(query);
  73. }
  74. public QueryWhere<T> atLeast(A y) {
  75. query.addConditionToken(new Condition<A>(x, y, CompareType.AT_LEAST));
  76. return new QueryWhere<T>(query);
  77. }
  78. public QueryWhere<T> lessThan(A y) {
  79. query.addConditionToken(new Condition<A>(x, y, CompareType.LESS_THAN));
  80. return new QueryWhere<T>(query);
  81. }
  82. public QueryWhere<T> atMost(A y) {
  83. query.addConditionToken(new Condition<A>(x, y, CompareType.AT_MOST));
  84. return new QueryWhere<T>(query);
  85. }
  86. public QueryBetween<T, A> between(A y) {
  87. return new QueryBetween<T, A>(query, x, y);
  88. }
  89. public QueryWhere<T> like(A pattern) {
  90. query.addConditionToken(new Condition<A>(x, pattern, CompareType.LIKE));
  91. return new QueryWhere<T>(query);
  92. }
  93. /*
  94. * These method allows you to generate "x=?", "x!=?", etc where conditions.
  95. * Parameter substitution must be done manually later with db.executeQuery.
  96. * This allows for building re-usable SQL string statements from your model
  97. * classes.
  98. */
  99. public QueryWhere<T> isParameter() {
  100. query.addConditionToken(new RuntimeParameter<A>(x, CompareType.EQUAL));
  101. return new QueryWhere<T>(query);
  102. }
  103. public QueryWhere<T> isNotParameter() {
  104. query.addConditionToken(new RuntimeParameter<A>(x, CompareType.NOT_EQUAL));
  105. return new QueryWhere<T>(query);
  106. }
  107. public QueryWhere<T> exceedsParameter() {
  108. query.addConditionToken(new RuntimeParameter<A>(x, CompareType.EXCEEDS));
  109. return new QueryWhere<T>(query);
  110. }
  111. public QueryWhere<T> lessThanParameter() {
  112. query.addConditionToken(new RuntimeParameter<A>(x, CompareType.LESS_THAN));
  113. return new QueryWhere<T>(query);
  114. }
  115. public QueryWhere<T> atMostParameter() {
  116. query.addConditionToken(new RuntimeParameter<A>(x, CompareType.AT_MOST));
  117. return new QueryWhere<T>(query);
  118. }
  119. public QueryWhere<T> likeParameter() {
  120. query.addConditionToken(new RuntimeParameter<A>(x, CompareType.LIKE));
  121. return new QueryWhere<T>(query);
  122. }
  123. public Bitwise<A, T> bitAnd(A y) {
  124. BitCondition<A, T> bitCondition = new BitCondition<A, T>(x, y, BitCondition.Bitwise.AND, query);
  125. query.addConditionToken(bitCondition);
  126. return bitCondition;
  127. }
  128. public Bitwise<A, T> bitXor(A y) {
  129. BitCondition<A, T> bitCondition = new BitCondition<A, T>(x, y, BitCondition.Bitwise.XOR, query);
  130. query.addConditionToken(bitCondition);
  131. return bitCondition;
  132. }
  133. }