BrightSide Workbench Full Report + Source Code
WhereClause.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.elephant.db;
19 
20 import java.util.ArrayList;
21 import java.util.Date;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25 import javax.persistence.Query;
26 import org.turro.string.Strings;
27 
32 public class WhereClause {
33 
34  private String clause = "";
35  private ArrayList values = new ArrayList();
36  private Map<String, Object> namedValues = new HashMap<>();
37  // like options
38  private String prefix = "and", fieldOcurrence = "and";
39  private int uniqueSuffix = 0;
40  private Map<String, Object> attributes = new HashMap<>();
41  private String orderByClause, groupByClause;
42  private boolean useNative;
43  private Class resultClass;
44 
45  public WhereClause() {
46  }
47 
48  public WhereClause(String prefix) {
49  this.prefix = prefix;
50  }
51 
52  public Map<String, Object> getAttributes() {
53  return attributes;
54  }
55 
56  public void addClause(String clause) {
57  this.clause += " " + clause;
58  }
59 
60  public void addClauseIfNotExists(String clause) {
61  if(this.clause == null || !this.clause.contains(clause)) {
62  this.clause += " " + clause;
63  }
64  }
65 
66  public void addInRange(String operator, String startField, String endField, String attribute, Date date) {
67  addClause(operator + "(");
68  addClause("(" + startField + " is null or " + startField + " >= :" + attribute + ")");
69  addClause("and (" + endField + " is null or " + endField + " <= :" + attribute + ")");
70  addClause(")");
71  addNamedValue(attribute, date);
72  }
73 
74  public void addValue(Object value) {
75  values.add(value);
76  }
77 
78  public void addNamedValue(String name, Object value) {
79  namedValues.put(name, value);
80  }
81 
82  public void addLikeFields(String[] fields, String value) {
83  if(value == null) return;
84  if(value.startsWith("=") || value.startsWith("?")) {
85  SQLHelper.getWhereClause(this, fields, value);
86  } else if(value.startsWith("\"") && value.endsWith("\"")) {
87  value = value.substring(1, value.length() - 1);
88  SQLHelper.getWhereClause(this, fields,
89  (value == null ? "" : value.replaceAll("\\*", "%").replaceAll("\\?", "_")), null);
90  } else {
91  SQLHelper.getWhereClause(this, fields,
92  (value == null ? "" : value.replaceAll("\\*", "%").replaceAll("\\?", "_")), " +");
93  }
94  }
95 
96  public Map<String, Object> getNamedValues() {
97  return namedValues;
98  }
99 
100  public List<Object> getNamedValues(String ... name) {
101  ArrayList<Object> list = new ArrayList<>();
102  for(String n : name) {
103  if(namedValues.containsKey(n)) list.add(namedValues.get(n));
104  }
105  return list;
106  }
107 
108  public int getValueCount() {
109  return values.size();
110  }
111 
112  public Object getValue(int index) {
113  return values.get(index);
114  }
115 
116  public String getClause() {
117  if(!Strings.isBlank(clause)) {
118  if(!Strings.isBlank(groupByClause) && clause.toLowerCase().contains("group by")) {
119  clause += " " + groupByClause;
120  }
121  if(!Strings.isBlank(orderByClause) && clause.toLowerCase().contains("order by")) {
122  clause += " " + orderByClause;
123  }
124  }
125  if(clause.matches(".*\\{[^\\}]+\\}.*")) {
126  clause = clause.replaceAll("\\{([^\\}]+)\\}", useNative ? "*" : "$1");
127  }
128  return clause;
129  }
130 
131  public void setClause(String clause) {
132  this.clause = clause;
133  }
134 
135  public void setOrderByClause(String orderByClause) {
136  this.orderByClause = orderByClause;
137  }
138 
139  public void setGroupByClause(String groupByClause) {
140  this.groupByClause = groupByClause;
141  }
142 
143  public boolean isUseNative() {
144  return useNative;
145  }
146 
147  public void setUseNative(boolean useNative) {
148  this.useNative = useNative;
149  }
150 
151  public Class getResultClass() {
152  return resultClass;
153  }
154 
155  public void setResultClass(Class resultClass) {
156  this.resultClass = resultClass;
157  }
158 
159  public int getUniqueSuffix() {
160  return uniqueSuffix++;
161  }
162 
163  public Object[] getValues() {
164  return values.toArray(new Object[0]);
165  }
166 
167  @Deprecated
168  public void setParameters(Query q, int start) {
169  for (int i = 0; i < getValueCount(); i++) {
170  q.setParameter(i + start, getValue(i));
171  }
172  }
173 
174  public void setNamedParameters(Query q) {
175  for (Map.Entry<String, Object> e : namedValues.entrySet()) {
176  q.setParameter(e.getKey(), e.getValue());
177  }
178  }
179 
180  public String getFieldOcurrence() {
181  return fieldOcurrence;
182  }
183 
184  public void setFieldOcurrence(String fieldOcurrence) {
185  if(fieldOcurrence == null) fieldOcurrence = "and";
186  this.fieldOcurrence = fieldOcurrence;
187  }
188 
189  public String getPrefix() {
190  return prefix;
191  }
192 
193  public void setPrefix(String prefix) {
194  if(prefix == null) prefix = "and";
195  this.prefix = prefix;
196  }
197 
198  private static final int IN_LIMIT = 500;
199 
200  public void addNotIn(String operator, String field, List values) {
201  addIn(operator, field + " not", values);
202  }
203 
204  public void addIn(String operator, String field, List values) {
205  if(values == null || values.isEmpty()) {
206  addClause((operator != null ? operator + " 1=2" : "1=2"));
207  } else {
208  int listSize = values.size();
209  if(listSize <= IN_LIMIT) {
210  String var = getUniqueVariable("intids");
211  addClause((operator != null ? operator + " " : "") + field + " in (:"+ var + ")");
212  addNamedValue(var, values);
213  } else {
214  addClause((operator != null ? operator + " (" : "("));
215  String sep = "";
216  for (int i = 0; i < listSize; i += IN_LIMIT) {
217  String var = getUniqueVariable("intids");
218  List subList = (listSize > i + IN_LIMIT) ?
219  values.subList(i, (i + IN_LIMIT)) :
220  values.subList(i, listSize);
221  addClause(sep + field + " in (:" + var + ")");
222  addNamedValue(var, subList);
223  sep = "or ";
224  }
225  addClause(")");
226  }
227  }
228  }
229 
230  public String getUniqueVariable(String prefix) {
231  return prefix + "wcvar" + getUniqueSuffix();
232  }
233 
234  /* Factory */
235 
236  public static WhereClause getFromClause(String clause) {
237  WhereClause wc = new WhereClause();
238  wc.addClause(clause);
239  return wc;
240  }
241 
242 }
static WhereClause getWhereClause(String[] fields, String value)
Definition: SQLHelper.java:64
Map< String, Object > getNamedValues()
void addNotIn(String operator, String field, List values)
List< Object > getNamedValues(String ... name)
void addClauseIfNotExists(String clause)
String getUniqueVariable(String prefix)
void addInRange(String operator, String startField, String endField, String attribute, Date date)
void setUseNative(boolean useNative)
void setGroupByClause(String groupByClause)
Map< String, Object > getAttributes()
void setOrderByClause(String orderByClause)
void addLikeFields(String[] fields, String value)
void addIn(String operator, String field, List values)
void setParameters(Query q, int start)
void addNamedValue(String name, Object value)
void setResultClass(Class resultClass)
static WhereClause getFromClause(String clause)
void setFieldOcurrence(String fieldOcurrence)