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.

Operation.java 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.bytecode;
  18. import com.iciql.Query;
  19. import com.iciql.SQLStatement;
  20. import com.iciql.Token;
  21. /**
  22. * A mathematical or comparison operation.
  23. */
  24. class Operation implements Token {
  25. /**
  26. * The operation type.
  27. */
  28. enum Type {
  29. EQUALS("=") {
  30. Type reverse() {
  31. return NOT_EQUALS;
  32. }
  33. },
  34. NOT_EQUALS("<>") {
  35. Type reverse() {
  36. return EQUALS;
  37. }
  38. },
  39. BIGGER(">") {
  40. Type reverse() {
  41. return SMALLER_EQUALS;
  42. }
  43. },
  44. BIGGER_EQUALS(">=") {
  45. Type reverse() {
  46. return SMALLER;
  47. }
  48. },
  49. SMALLER_EQUALS("<=") {
  50. Type reverse() {
  51. return BIGGER;
  52. }
  53. },
  54. SMALLER("<") {
  55. Type reverse() {
  56. return BIGGER_EQUALS;
  57. }
  58. },
  59. ADD("+"), SUBTRACT("-"), MULTIPLY("*"), DIVIDE("/"), MOD("%");
  60. private String name;
  61. Type(String name) {
  62. this.name = name;
  63. }
  64. public String toString() {
  65. return name;
  66. }
  67. Type reverse() {
  68. return null;
  69. }
  70. }
  71. private final Token left, right;
  72. private final Type op;
  73. private Operation(Token left, Type op, Token right) {
  74. this.left = left;
  75. this.op = op;
  76. this.right = right;
  77. }
  78. static Token get(Token left, Type op, Token right) {
  79. if (op == Type.NOT_EQUALS && "0".equals(right.toString())) {
  80. return left;
  81. }
  82. return new Operation(left, op, right);
  83. }
  84. public String toString() {
  85. return left + " " + op + " " + right;
  86. }
  87. public Token reverse() {
  88. return get(left, op.reverse(), right);
  89. }
  90. public <T> void appendSQL(SQLStatement stat, Query<T> query) {
  91. left.appendSQL(stat, query);
  92. stat.appendSQL(op.toString());
  93. right.appendSQL(stat, query);
  94. }
  95. }