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.

BitCondition.java 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright 2017 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;
  17. /**
  18. * A bitwise condition contains two operands, a bit operation, and a comparison test.
  19. *
  20. * @param <A> the operand type
  21. */
  22. class BitCondition<A, T> implements Token, Bitwise<A, T> {
  23. enum Bitwise {
  24. AND, XOR
  25. }
  26. Bitwise bitwiseType;
  27. CompareType compareType;
  28. A x, y, z;
  29. Query<T> query;
  30. BitCondition(A x, A y, Bitwise bitwiseType, Query<T> query) {
  31. this.bitwiseType = bitwiseType;
  32. this.x = x;
  33. this.y = y;
  34. this.query = query;
  35. }
  36. public QueryWhere<T> exceeds(A y) {
  37. z = y;
  38. compareType = CompareType.EXCEEDS;
  39. return new QueryWhere<T>(query);
  40. }
  41. public QueryWhere<T> atLeast(A y) {
  42. z = y;
  43. compareType = CompareType.AT_LEAST;
  44. return new QueryWhere<T>(query);
  45. }
  46. public QueryWhere<T> lessThan(A y) {
  47. z = y;
  48. compareType = CompareType.LESS_THAN;
  49. return new QueryWhere<T>(query);
  50. }
  51. public QueryWhere<T> atMost(A y) {
  52. z = y;
  53. compareType = CompareType.AT_MOST;
  54. return new QueryWhere<T>(query);
  55. }
  56. @SuppressWarnings("unchecked")
  57. public <T> void appendSQL(SQLStatement stat, Query<T> query) {
  58. stat.appendSQL("(");
  59. switch (bitwiseType) {
  60. case AND:
  61. query.getDb().getDialect().prepareBitwiseAnd(stat, query, x, y);
  62. break;
  63. case XOR:
  64. query.getDb().getDialect().prepareBitwiseXor(stat, query, x, y);
  65. break;
  66. }
  67. stat.appendSQL(")");
  68. stat.appendSQL(compareType.getString());
  69. stat.appendSQL("" + z);
  70. }
  71. }