18 package org.turro.elephant.db;
20 import java.util.ArrayList;
21 import java.util.Date;
22 import java.util.HashMap;
23 import java.util.List;
25 import javax.persistence.Query;
26 import org.turro.string.Strings;
34 private String clause =
"";
35 private ArrayList values =
new ArrayList();
36 private Map<String, Object> namedValues =
new HashMap<>();
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;
57 this.clause +=
" " + clause;
61 if(this.clause ==
null || !this.clause.contains(clause)) {
62 this.clause +=
" " + clause;
66 public void addInRange(String
operator, String startField, String endField, String attribute, Date date) {
68 addClause(
"(" + startField +
" is null or " + startField +
" >= :" + attribute +
")");
69 addClause(
"and (" + endField +
" is null or " + endField +
" <= :" + attribute +
")");
79 namedValues.put(name, value);
83 if(value ==
null)
return;
84 if(value.startsWith(
"=") || value.startsWith(
"?")) {
86 }
else if(value.startsWith(
"\"") && value.endsWith(
"\"")) {
87 value = value.substring(1, value.length() - 1);
89 (value ==
null ?
"" : value.replaceAll(
"\\*",
"%").replaceAll(
"\\?",
"_")),
null);
92 (value ==
null ?
"" : value.replaceAll(
"\\*",
"%").replaceAll(
"\\?",
"_")),
" +");
101 ArrayList<Object> list =
new ArrayList<>();
102 for(String n : name) {
103 if(namedValues.containsKey(n)) list.add(namedValues.get(n));
109 return values.size();
113 return values.get(index);
117 if(!Strings.isBlank(clause)) {
118 if(!Strings.isBlank(groupByClause) && clause.toLowerCase().contains(
"group by")) {
119 clause +=
" " + groupByClause;
121 if(!Strings.isBlank(orderByClause) && clause.toLowerCase().contains(
"order by")) {
122 clause +=
" " + orderByClause;
125 if(clause.matches(
".*\\{[^\\}]+\\}.*")) {
126 clause = clause.replaceAll(
"\\{([^\\}]+)\\}", useNative ?
"*" :
"$1");
132 this.clause = clause;
136 this.orderByClause = orderByClause;
140 this.groupByClause = groupByClause;
148 this.useNative = useNative;
156 this.resultClass = resultClass;
160 return uniqueSuffix++;
164 return values.toArray(
new Object[0]);
170 q.setParameter(i + start,
getValue(i));
175 for (Map.Entry<String, Object> e : namedValues.entrySet()) {
176 q.setParameter(e.getKey(), e.getValue());
181 return fieldOcurrence;
185 if(fieldOcurrence ==
null) fieldOcurrence =
"and";
186 this.fieldOcurrence = fieldOcurrence;
194 if(prefix ==
null) prefix =
"and";
195 this.prefix = prefix;
198 private static final int IN_LIMIT = 500;
200 public void addNotIn(String
operator, String field, List values) {
201 addIn(
operator, field +
" not", values);
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"));
208 int listSize = values.size();
209 if(listSize <= IN_LIMIT) {
211 addClause((
operator !=
null ?
operator +
" " :
"") + field +
" in (:"+ var +
")");
214 addClause((
operator !=
null ?
operator +
" (" :
"("));
216 for (
int i = 0; i < listSize; i += IN_LIMIT) {
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 +
")");
static WhereClause getWhereClause(String[] fields, String value)
Map< String, Object > getNamedValues()
void addNotIn(String operator, String field, List values)
List< Object > getNamedValues(String ... name)
void addValue(Object value)
void setPrefix(String prefix)
void addClauseIfNotExists(String clause)
Object getValue(int index)
String getUniqueVariable(String prefix)
void addInRange(String operator, String startField, String endField, String attribute, Date date)
void setUseNative(boolean useNative)
WhereClause(String prefix)
void setGroupByClause(String groupByClause)
Map< String, Object > getAttributes()
void setNamedParameters(Query q)
void setClause(String clause)
void setOrderByClause(String orderByClause)
void addClause(String clause)
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)
String getFieldOcurrence()
static WhereClause getFromClause(String clause)
void setFieldOcurrence(String fieldOcurrence)