2 * Copyright 2000-2021 Vaadin Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
16 package com.vaadin.v7.data.util.sqlcontainer.query.generator.filter;
18 import java.io.Serializable;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.List;
23 import com.vaadin.v7.data.Container.Filter;
24 import com.vaadin.v7.data.util.sqlcontainer.query.generator.StatementHelper;
27 * @deprecated As of 8.0, no replacement available.
30 public class QueryBuilder implements Serializable {
32 private static ArrayList<FilterTranslator> filterTranslators = new ArrayList<FilterTranslator>();
33 private static StringDecorator stringDecorator = new StringDecorator("\"",
37 /* Register all default filter translators */
38 addFilterTranslator(new AndTranslator());
39 addFilterTranslator(new OrTranslator());
40 addFilterTranslator(new LikeTranslator());
41 addFilterTranslator(new BetweenTranslator());
42 addFilterTranslator(new CompareTranslator());
43 addFilterTranslator(new NotTranslator());
44 addFilterTranslator(new IsNullTranslator());
45 addFilterTranslator(new SimpleStringTranslator());
48 public static synchronized void addFilterTranslator(
49 FilterTranslator translator) {
50 filterTranslators.add(translator);
54 * Allows specification of a custom ColumnQuoter instance that handles
55 * quoting of column names for the current DB dialect.
58 * the ColumnQuoter instance to use.
60 public static void setStringDecorator(StringDecorator decorator) {
61 stringDecorator = decorator;
64 public static String quote(Object str) {
65 return stringDecorator.quote(str);
68 public static String group(String str) {
69 return stringDecorator.group(str);
73 * Constructs and returns a string representing the filter that can be used
77 * the filter to translate
79 * the statement helper to update with the value(s) of the filter
80 * @return a string representing the filter.
82 public static synchronized String getWhereStringForFilter(Filter filter,
84 for (FilterTranslator ft : filterTranslators) {
85 if (ft.translatesFilter(filter)) {
86 return ft.getWhereStringForFilter(filter, sh);
92 public static String getJoinedFilterString(Collection<Filter> filters,
93 String joinString, StatementHelper sh) {
94 StringBuilder result = new StringBuilder();
95 for (Filter f : filters) {
96 result.append(getWhereStringForFilter(f, sh));
97 result.append(' ').append(joinString).append(' ');
99 // Remove the last instance of joinString
100 result.delete(result.length() - joinString.length() - 2,
102 return result.toString();
105 public static String getWhereStringForFilters(List<Filter> filters,
106 StatementHelper sh) {
107 if (filters == null || filters.isEmpty()) {
110 StringBuilder where = new StringBuilder(" WHERE ");
111 where.append(getJoinedFilterString(filters, "AND", sh));
112 return where.toString();