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.

QueryBuilder.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * 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, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.v7.data.util.sqlcontainer.query.generator.filter;
  17. import java.io.Serializable;
  18. import java.util.ArrayList;
  19. import java.util.Collection;
  20. import java.util.List;
  21. import com.vaadin.v7.data.Container.Filter;
  22. import com.vaadin.v7.data.util.sqlcontainer.query.generator.StatementHelper;
  23. /**
  24. * @deprecated As of 8.0, no replacement available.
  25. */
  26. @Deprecated
  27. public class QueryBuilder implements Serializable {
  28. private static ArrayList<FilterTranslator> filterTranslators = new ArrayList<FilterTranslator>();
  29. private static StringDecorator stringDecorator = new StringDecorator("\"",
  30. "\"");
  31. static {
  32. /* Register all default filter translators */
  33. addFilterTranslator(new AndTranslator());
  34. addFilterTranslator(new OrTranslator());
  35. addFilterTranslator(new LikeTranslator());
  36. addFilterTranslator(new BetweenTranslator());
  37. addFilterTranslator(new CompareTranslator());
  38. addFilterTranslator(new NotTranslator());
  39. addFilterTranslator(new IsNullTranslator());
  40. addFilterTranslator(new SimpleStringTranslator());
  41. }
  42. public static synchronized void addFilterTranslator(
  43. FilterTranslator translator) {
  44. filterTranslators.add(translator);
  45. }
  46. /**
  47. * Allows specification of a custom ColumnQuoter instance that handles
  48. * quoting of column names for the current DB dialect.
  49. *
  50. * @param decorator
  51. * the ColumnQuoter instance to use.
  52. */
  53. public static void setStringDecorator(StringDecorator decorator) {
  54. stringDecorator = decorator;
  55. }
  56. public static String quote(Object str) {
  57. return stringDecorator.quote(str);
  58. }
  59. public static String group(String str) {
  60. return stringDecorator.group(str);
  61. }
  62. /**
  63. * Constructs and returns a string representing the filter that can be used
  64. * in a WHERE clause.
  65. *
  66. * @param filter
  67. * the filter to translate
  68. * @param sh
  69. * the statement helper to update with the value(s) of the filter
  70. * @return a string representing the filter.
  71. */
  72. public static synchronized String getWhereStringForFilter(Filter filter,
  73. StatementHelper sh) {
  74. for (FilterTranslator ft : filterTranslators) {
  75. if (ft.translatesFilter(filter)) {
  76. return ft.getWhereStringForFilter(filter, sh);
  77. }
  78. }
  79. return "";
  80. }
  81. public static String getJoinedFilterString(Collection<Filter> filters,
  82. String joinString, StatementHelper sh) {
  83. StringBuilder result = new StringBuilder();
  84. for (Filter f : filters) {
  85. result.append(getWhereStringForFilter(f, sh));
  86. result.append(" ").append(joinString).append(" ");
  87. }
  88. // Remove the last instance of joinString
  89. result.delete(result.length() - joinString.length() - 2,
  90. result.length());
  91. return result.toString();
  92. }
  93. public static String getWhereStringForFilters(List<Filter> filters,
  94. StatementHelper sh) {
  95. if (filters == null || filters.isEmpty()) {
  96. return "";
  97. }
  98. StringBuilder where = new StringBuilder(" WHERE ");
  99. where.append(getJoinedFilterString(filters, "AND", sh));
  100. return where.toString();
  101. }
  102. }