BrightSide Workbench Full Report + Source Code
SQLHelper.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.Collection;
21 import org.turro.string.Strings;
22 
27 public class SQLHelper {
28 
30  private SQLHelper() {
31  }
32 
33  public static String convertToLike(String value) {
34  if(value == null) return null;
35  return value.replaceAll("\\*", "%").replaceAll("\\?", "_");
36  }
37 
38  public static String convertToPartialLike(String value) {
39  if(value == null) return null;
40  return "%" + value.replaceAll("\\*", "%").replaceAll("\\?", "_") + "%";
41  }
42 
43  public static String convertToIn(Collection collection) {
44  String result = null;
45  for(Object o : collection) {
46  if(o instanceof String) {
47  result = (result == null) ? ("'" + o + "'") : (result + ",'" + o + "'");
48  } else {
49  result = (String) ((result == null) ? o + "" : (result + "," + o));
50  }
51  }
52  return result;
53  }
54 
55  public static String quoteTokensForIn(String inValue) {
56  String[] tokens = inValue.split(",");
57  String result = null;
58  for(String s : tokens) {
59  result = (result == null) ? ("'" + s + "'") : (result + ",'" + s + "'");
60  }
61  return result;
62  }
63 
64  public static WhereClause getWhereClause(String[] fields, String value) {
65  return getWhereClause(null, fields, value);
66  }
67 
68  public static WhereClause getWhereClause(WhereClause wc, String[] fields, String value) {
69  if(wc == null) wc = new WhereClause();
70  if(value != null) {
71  if(value.startsWith("=")) {
72  // value must be equal
73  value = value.substring(1);
74  return /*"%".equals(value) ? wc :*/ getWhereClause(wc, fields, value, null);
75  } else if(value.startsWith("?")) {
76  // where portion as is
77  wc.addClause(value.substring(1));
78  return wc;
79  }
80  }
81  return /*"%".equals(value) ? wc :*/ getWhereClause(wc, fields, value, " ");
82  }
83 
84  public static WhereClause getWhereClause(String[] fields, String value, String regexp) {
85  return getWhereClause(null, fields, value, regexp);
86  }
87 
88  public static WhereClause getWhereClause(WhereClause wc, String[] fields, String value, String regexp) {
89  if(value != null) {
90  if(regexp != null) {
91  return getWhereClause(wc, fields, value.split(regexp));
92  } else {
93  return getWhereClause(wc, fields, new String[] { value });
94  }
95  }
96  return new WhereClause();
97  }
98 
99  public static WhereClause getWhereClause(String[] fields, String[] values) {
100  return getWhereClause(fields, values, true);
101  }
102 
103  public static WhereClause getWhereClause(WhereClause wc, String[] fields, String[] values) {
104  return getWhereClause(wc, fields, values, true);
105  }
106 
107  public static WhereClause getWhereClause(String[] fields, String[] values, boolean namedValues) {
108  return getWhereClause(null, fields, values, namedValues);
109  }
110 
111  public static WhereClause getWhereClause(WhereClause wc, String[] fields, String[] values, boolean namedValues) {
112  if(wc == null) wc = new WhereClause();
113 
114  String result = "", partial, param;
115 
116  for(int f = 0; f < fields.length; f++) {
117  partial = "";
118  for(int v = 0; v < values.length; v++) {
119  if(!Strings.isBlank(values[v])) {
120  if(partial.length() > 0) {
121  partial += " " + wc.getFieldOcurrence() + " ";
122  }
123  if(namedValues) {
124  param = fields[f].replaceAll("\\.", "_") + wc.getUniqueSuffix();
125  partial += "UCASE(" + fields[f] + ") like :" + param;
126  if("%".equals(values[v])) {
127  wc.addNamedValue(param, "%");
128  } else {
129  wc.addNamedValue(param, "%" + values[v].toUpperCase() + "%");
130  }
131  } else {
132  partial += "UCASE(" + fields[f] + ") like ?";
133  if("%".equals(values[v])) {
134  wc.addValue("%");
135  } else {
136  wc.addValue("%" + values[v].toUpperCase() + "%");
137  }
138  }
139  }
140  }
141 
142  if(partial.length() > 0) {
143  if(result.length() > 0) {
144  result += " or ";
145  }
146  result += "(" + partial + ")";
147  }
148  }
149 
150  if(result.length() > 0) {
151  result = wc.getPrefix() + " (" + result + ") ";
152  }
153 
154  wc.addClause(result);
155 
156  return wc;
157  }
158 
159 }
static WhereClause getWhereClause(WhereClause wc, String[] fields, String value)
Definition: SQLHelper.java:68
static WhereClause getWhereClause(String[] fields, String[] values, boolean namedValues)
Definition: SQLHelper.java:107
static WhereClause getWhereClause(WhereClause wc, String[] fields, String[] values)
Definition: SQLHelper.java:103
static WhereClause getWhereClause(WhereClause wc, String[] fields, String value, String regexp)
Definition: SQLHelper.java:88
static WhereClause getWhereClause(String[] fields, String value, String regexp)
Definition: SQLHelper.java:84
static String quoteTokensForIn(String inValue)
Definition: SQLHelper.java:55
static WhereClause getWhereClause(String[] fields, String value)
Definition: SQLHelper.java:64
static WhereClause getWhereClause(String[] fields, String[] values)
Definition: SQLHelper.java:99
static WhereClause getWhereClause(WhereClause wc, String[] fields, String[] values, boolean namedValues)
Definition: SQLHelper.java:111
static String convertToIn(Collection collection)
Definition: SQLHelper.java:43
static String convertToPartialLike(String value)
Definition: SQLHelper.java:38
static String convertToLike(String value)
Definition: SQLHelper.java:33
void addNamedValue(String name, Object value)