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.

QueryWhere.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 java.util.List;
  19. /**
  20. * This class represents a query with a condition.
  21. *
  22. * @param <T>
  23. * the return type
  24. */
  25. public class QueryWhere<T> {
  26. Query<T> query;
  27. QueryWhere(Query<T> query) {
  28. this.query = query;
  29. }
  30. public <A> QueryCondition<T, A> and(A x) {
  31. query.addConditionToken(ConditionAndOr.AND);
  32. return new QueryCondition<T, A>(query, x);
  33. }
  34. public <A> QueryCondition<T, A> or(A x) {
  35. query.addConditionToken(ConditionAndOr.OR);
  36. return new QueryCondition<T, A>(query, x);
  37. }
  38. public QueryWhere<T> limit(long limit) {
  39. query.limit(limit);
  40. return this;
  41. }
  42. public QueryWhere<T> offset(long offset) {
  43. query.offset(offset);
  44. return this;
  45. }
  46. public <X, Z> List<X> select(Z x) {
  47. return query.select(x);
  48. }
  49. public String getSQL() {
  50. SQLStatement stat = new SQLStatement(query.getDb());
  51. stat.appendSQL("SELECT *");
  52. query.appendFromWhere(stat);
  53. return stat.getSQL().trim();
  54. }
  55. public <X, Z> List<X> selectDistinct(Z x) {
  56. return query.selectDistinct(x);
  57. }
  58. public <X, Z> X selectFirst(Z x) {
  59. List<X> list = query.select(x);
  60. return list.isEmpty() ? null : list.get(0);
  61. }
  62. public List<T> select() {
  63. return query.select();
  64. }
  65. public T selectFirst() {
  66. List<T> list = select();
  67. return list.isEmpty() ? null : list.get(0);
  68. }
  69. public List<T> selectDistinct() {
  70. return query.selectDistinct();
  71. }
  72. /**
  73. * Order by a number of columns.
  74. *
  75. * @param expressions
  76. * the order by expressions
  77. * @return the query
  78. */
  79. public QueryWhere<T> orderBy(Object... expressions) {
  80. for (Object expr : expressions) {
  81. OrderExpression<T> e = new OrderExpression<T>(query, expr, false, false, false);
  82. query.addOrderBy(e);
  83. }
  84. return this;
  85. }
  86. public QueryWhere<T> orderByNullsFirst(Object expr) {
  87. OrderExpression<T> e = new OrderExpression<T>(query, expr, false, true, false);
  88. query.addOrderBy(e);
  89. return this;
  90. }
  91. public QueryWhere<T> orderByNullsLast(Object expr) {
  92. OrderExpression<T> e = new OrderExpression<T>(query, expr, false, false, true);
  93. query.addOrderBy(e);
  94. return this;
  95. }
  96. public QueryWhere<T> orderByDesc(Object expr) {
  97. OrderExpression<T> e = new OrderExpression<T>(query, expr, true, false, false);
  98. query.addOrderBy(e);
  99. return this;
  100. }
  101. public QueryWhere<T> orderByDescNullsFirst(Object expr) {
  102. OrderExpression<T> e = new OrderExpression<T>(query, expr, true, true, false);
  103. query.addOrderBy(e);
  104. return this;
  105. }
  106. public QueryWhere<T> orderByDescNullsLast(Object expr) {
  107. OrderExpression<T> e = new OrderExpression<T>(query, expr, true, false, true);
  108. query.addOrderBy(e);
  109. return this;
  110. }
  111. public int delete() {
  112. return query.delete();
  113. }
  114. public int update() {
  115. return query.update();
  116. }
  117. public long selectCount() {
  118. return query.selectCount();
  119. }
  120. }