1 18 19 package sync4j.framework.server.store; 20 21 import org.apache.commons.lang.builder.ToStringBuilder; 22 import java.io.Serializable ; 23 24 31 public class WhereClause 32 extends Clause 33 implements Serializable { 34 35 public static final String OPT_START_WITH = "START_WITH"; 37 public static final String OPT_END_WITH = "END_WITH" ; 38 public static final String OPT_CONTAINS = "CONTAINS" ; 39 public static final String OPT_EQ = "EQ" ; 40 public static final String OPT_GT = "GT" ; 41 public static final String OPT_LT = "LT" ; 42 public static final String OPT_BETWEEN = "BETWEEN" ; 43 public static final String OPT_GE = "GE" ; 44 public static final String OPT_LE = "LE" ; 45 46 private static final String OPT_UPPER = "upper(" ; 48 49 private String property; 50 private String [] value; 51 private String operator; 52 private boolean caseSensitive; 53 54 57 public WhereClause() { 58 this(null, null, null, false); 59 } 60 61 public WhereClause(String property, String [] value, String operator, boolean caseSensitive) { 62 this.property = property; 63 this.value = value; 64 this.operator = operator; 65 this.caseSensitive = caseSensitive; 66 } 67 68 72 public String getProperty() { 73 return property; 74 } 75 76 80 public void setProperty(String property) { 81 this.property = property; 82 } 83 84 88 public String [] getvalue() { 89 return value; 90 } 91 92 96 public void setValue(String [] value) { 97 this.value = value; 98 } 99 100 104 public void setParameter(String [] value) { 105 this.value = value; 106 } 107 108 112 public String getOperator() { 113 return operator; 114 } 115 116 120 public void setOperator(String operator) { 121 this.operator = operator; 122 } 123 124 128 public boolean isCaseSensitive() { 129 return caseSensitive; 130 } 131 132 136 public void setCaseSensitive(boolean caseSensitive) { 137 this.caseSensitive = caseSensitive; 138 } 139 140 142 public PreparedWhere getPreparedWhere() { 143 String property = getProperty(); 144 String operator = getOperator(); 145 String [] values = getvalue(); 146 boolean caseSensitive = isCaseSensitive(); 147 148 StringBuffer query = new StringBuffer (); 149 150 assert (values != null); 151 152 PreparedWhere where = new PreparedWhere(); 153 where.parameters = new Object [values.length]; 154 155 String uprOpen = "" ; 156 String uprClose = "" ; 157 String uprProperty = property; 158 159 if (!caseSensitive) { 160 uprOpen = OPT_UPPER; 161 uprClose = ")"; 162 uprProperty = " UPPER("+property+")"; 163 } 164 165 if (OPT_START_WITH.equalsIgnoreCase(operator)) { 166 query.append(uprProperty).append(" LIKE ").append(uprOpen).append('?').append(uprClose); 167 where.sql = query.toString(); 168 where.parameters[0] = values[0] + '%'; 169 } else if (OPT_END_WITH.equalsIgnoreCase(operator)) { 170 query.append(uprProperty).append(" LIKE ").append(uprOpen).append('?').append(uprClose); 171 where.sql = query.toString(); 172 where.parameters[0] = '%' + values[0]; 173 } else if (OPT_CONTAINS.equalsIgnoreCase(operator)) { 174 query.append(uprProperty).append(" LIKE ").append(uprOpen).append('?').append(uprClose); 175 where.sql = query.toString(); 176 where.parameters[0] = '%' + values[0] + '%'; 177 } else if (OPT_EQ.equalsIgnoreCase(operator)) { 178 query.append(uprProperty).append(" = ").append(uprOpen).append('?').append(uprClose); 179 where.sql = query.toString(); 180 where.parameters[0] = values[0]; 181 } else if (OPT_GT.equalsIgnoreCase(operator)) { 182 query.append(uprProperty).append(" > ").append(uprOpen).append('?').append(uprClose); 183 where.sql = query.toString(); 184 where.parameters[0] = values[0]; 185 } else if (OPT_LT.equalsIgnoreCase(operator)) { 186 query.append(uprProperty).append(" < ").append(uprOpen).append('?').append(uprClose); 187 where.sql = query.toString(); 188 where.parameters[0] = values[0]; 189 } else if (OPT_BETWEEN.equalsIgnoreCase(operator)) { 190 query.append(uprProperty).append(' ').append(OPT_BETWEEN).append(' ').append(uprOpen).append('?').append(uprClose) 191 .append(" AND ").append(uprOpen).append('?').append(uprClose); 192 where.sql = query.toString(); 193 where.parameters[0] = values[0]; 194 where.parameters[1] = values[1]; 195 } else if (OPT_GE.equalsIgnoreCase(operator)) { 196 query.append(uprProperty).append(" >= ").append(uprOpen).append('?').append(uprClose); 197 where.sql = query.toString(); 198 where.parameters[0] = values[0]; 199 } else if (OPT_LE.equalsIgnoreCase(operator)) { 200 query.append(uprProperty).append(" <= ").append(uprOpen).append('?').append(uprClose); 201 where.sql = query.toString(); 202 where.parameters[0] = values[0]; 203 } 204 205 where.sql = '(' + where.sql + ')'; 206 return where; 207 } 208 209 211 public String toString() { 212 ToStringBuilder sb = new ToStringBuilder(this); 213 sb.append("operator:", operator); 214 sb.append("property:", property); 215 for (int i=0; i<value.length; i++) { 216 sb.append("value:", value[i]); 217 } 218 sb.append("caseSensitive:", caseSensitive); 219 220 return sb.toString(); 221 } 222 } 223 | Popular Tags |