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.

Function.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 provides static methods that represents common SQL functions.
  21. */
  22. public class Function implements Token {
  23. // must be a new instance
  24. private static final Long COUNT_STAR = Long.valueOf(0);
  25. protected Object[] x;
  26. private String name;
  27. protected Function(String name, Object... x) {
  28. this.name = name;
  29. this.x = x;
  30. }
  31. public <T> void appendSQL(SQLStatement stat, Query<T> query) {
  32. stat.appendSQL(name).appendSQL("(");
  33. int i = 0;
  34. for (Object o : x) {
  35. if (i++ > 0) {
  36. stat.appendSQL(",");
  37. }
  38. query.appendSQL(stat, o);
  39. }
  40. stat.appendSQL(")");
  41. }
  42. public static Long count() {
  43. return COUNT_STAR;
  44. }
  45. public static Integer length(Object x) {
  46. return Db.registerToken(Utils.newObject(Integer.class), new Function("LENGTH", x));
  47. }
  48. @SuppressWarnings("unchecked")
  49. public static <T extends Number> T sum(T x) {
  50. return (T) Db.registerToken(Utils.newObject(x.getClass()), new Function("SUM", x));
  51. }
  52. public static Long count(Object x) {
  53. return Db.registerToken(Utils.newObject(Long.class), new Function("COUNT", x));
  54. }
  55. public static Boolean isNull(Object x) {
  56. return Db.registerToken(Utils.newObject(Boolean.class), new Function("", x) {
  57. public <T> void appendSQL(SQLStatement stat, Query<T> query) {
  58. query.appendSQL(stat, x[0]);
  59. stat.appendSQL(" IS NULL");
  60. }
  61. });
  62. }
  63. public static Boolean isNotNull(Object x) {
  64. return Db.registerToken(Utils.newObject(Boolean.class), new Function("", x) {
  65. public <T> void appendSQL(SQLStatement stat, Query<T> query) {
  66. query.appendSQL(stat, x[0]);
  67. stat.appendSQL(" IS NOT NULL");
  68. }
  69. });
  70. }
  71. public static Boolean not(Boolean x) {
  72. return Db.registerToken(Utils.newObject(Boolean.class), new Function("", x) {
  73. public <T> void appendSQL(SQLStatement stat, Query<T> query) {
  74. stat.appendSQL("NOT ");
  75. query.appendSQL(stat, x[0]);
  76. }
  77. });
  78. }
  79. public static Boolean or(Boolean... x) {
  80. return Db.registerToken(Utils.newObject(Boolean.class), new Function("", (Object[]) x) {
  81. public <T> void appendSQL(SQLStatement stat, Query<T> query) {
  82. int i = 0;
  83. for (Object o : x) {
  84. if (i++ > 0) {
  85. stat.appendSQL(" OR ");
  86. }
  87. query.appendSQL(stat, o);
  88. }
  89. }
  90. });
  91. }
  92. public static Boolean and(Boolean... x) {
  93. return Db.registerToken(Utils.newObject(Boolean.class), new Function("", (Object[]) x) {
  94. public <T> void appendSQL(SQLStatement stat, Query<T> query) {
  95. int i = 0;
  96. for (Object o : x) {
  97. if (i++ > 0) {
  98. stat.appendSQL(" AND ");
  99. }
  100. query.appendSQL(stat, o);
  101. }
  102. }
  103. });
  104. }
  105. @SuppressWarnings("unchecked")
  106. public static <X> X min(X x) {
  107. Class<X> clazz = (Class<X>) x.getClass();
  108. X o = Utils.newObject(clazz);
  109. return Db.registerToken(o, new Function("MIN", x));
  110. }
  111. @SuppressWarnings("unchecked")
  112. public static <X> X max(X x) {
  113. Class<X> clazz = (Class<X>) x.getClass();
  114. X o = Utils.newObject(clazz);
  115. return Db.registerToken(o, new Function("MAX", x));
  116. }
  117. public static Boolean like(String x, String pattern) {
  118. Boolean o = Utils.newObject(Boolean.class);
  119. return Db.registerToken(o, new Function("LIKE", x, pattern) {
  120. public <T> void appendSQL(SQLStatement stat, Query<T> query) {
  121. stat.appendSQL("(");
  122. query.appendSQL(stat, x[0]);
  123. stat.appendSQL(" LIKE ");
  124. query.appendSQL(stat, x[1]);
  125. stat.appendSQL(")");
  126. }
  127. });
  128. }
  129. }