1 2 3 package org.enhydra.shark.expressionbuilders; 4 5 import java.io.Serializable ; 6 import java.util.*; 7 8 import org.enhydra.shark.api.common.ExpressionBuilder; 9 10 17 public abstract class BasicExpressionBuilder implements Serializable { 18 protected List bshExpression; 19 20 protected List sqlExpression; 21 22 protected List propertiesUsed; 23 24 private String orderBy; 25 26 protected boolean sqlComplete; 27 28 protected int operator; 29 30 protected char delimiter = '\''; 31 32 protected boolean usingStandardVariableModel; 33 34 protected String objectid_column_name = " oid "; 35 36 public static final String PARAM_NAME_STRING_DELIMITER = "ExpressionBuilder.StringDelimiter"; 37 38 public static final String PARAM_NAME_OBJECTID_COLUMN_NAME = "DatabaseManager.ObjectIdColumnName"; 39 40 public static final String PARAM_NAME_VARIABLE_MODEL = "DODSEventAuditManager.useStandardVariableDataModel"; 41 42 53 protected BasicExpressionBuilder(Properties p) { 54 this.bshExpression = new ArrayList(); 55 this.sqlExpression = new ArrayList(); 56 this.propertiesUsed = new ArrayList(); 57 this.sqlComplete = true; 58 if (null != p) { 59 this.objectid_column_name = " " 60 + p.getProperty(PARAM_NAME_OBJECTID_COLUMN_NAME, 61 "oid") + " "; 62 this.usingStandardVariableModel = Boolean.valueOf(p.getProperty(PARAM_NAME_VARIABLE_MODEL, 63 "true")) 64 .booleanValue(); 65 this.delimiter = p.getProperty(PARAM_NAME_STRING_DELIMITER, "'") 66 .charAt(0); 67 } 68 } 69 70 76 protected char getDelimiter() { 77 return delimiter; 78 } 79 80 88 protected char appendOperator(boolean sqlToo) { 89 char ret = ' '; 90 if (ExpressionBuilder.AND_OPERATOR == (ExpressionBuilder.AND_OPERATOR & this.operator)) { 91 this.bshExpression.add(" && "); 92 if (sqlToo) this.sqlExpression.add(" AND "); 93 } else if (ExpressionBuilder.OR_OPERATOR == (ExpressionBuilder.OR_OPERATOR & this.operator)) { 94 this.bshExpression.add("||"); 95 if (sqlToo) this.sqlExpression.add(" OR "); 96 } 97 if (ExpressionBuilder.NOT_OPERATOR == (ExpressionBuilder.NOT_OPERATOR & this.operator)) { 98 ret = '!'; 99 } 100 this.operator = 0; 101 return ret; 102 } 103 104 protected void addEquals(String java_name, String sql_name, String value) { 105 char _notPrecedes = appendOperator(true); 106 this.bshExpression.add(_notPrecedes 107 + java_name + ".equals(\"" + value + "\")"); 108 this.sqlExpression.add(sql_name 109 + _notPrecedes + "= " + getDelimiter() + value 110 + getDelimiter() + " "); 111 if (!this.propertiesUsed.contains(java_name)) { 112 this.propertiesUsed.add(java_name); 113 } 114 } 115 116 protected void addEquals(String java_name, String sql_name, long value) { 117 char _notPrecedes = appendOperator(true); 118 this.bshExpression.add(java_name 119 + ".longValue()" + _notPrecedes + "== " + value); 120 this.sqlExpression.add(sql_name + _notPrecedes + "= " + value + " "); 121 if (!this.propertiesUsed.contains(java_name)) { 122 this.propertiesUsed.add(java_name); 123 } 124 } 125 126 protected void addGreaterThan(String java_name, String sql_name, long value) { 127 char _notPrecedes = appendOperator(true); 128 String operator = ' ' == _notPrecedes ? " > " : " <= "; 129 this.bshExpression.add(java_name + ".longValue()" + operator + value); 130 this.sqlExpression.add(sql_name + operator + value + " "); 131 if (!this.propertiesUsed.contains(java_name)) { 132 this.propertiesUsed.add(java_name); 133 } 134 } 135 136 protected void addLessThan(String java_name, String sql_name, long value) { 137 char _notPrecedes = appendOperator(true); 138 String operator = ' ' == _notPrecedes ? " < " : " >= "; 139 this.bshExpression.add(java_name + ".longValue()" + operator + value); 140 this.sqlExpression.add(sql_name + operator + value + " "); 141 if (!this.propertiesUsed.contains(java_name)) { 142 this.propertiesUsed.add(java_name); 143 } 144 } 145 146 protected void addGreaterThanWithSubQuery(String javaName, 147 String sqlName, 148 String sqlInPart, 149 long value, 150 String sqlEndPart) { 151 char _notPrecedes = appendOperator(true); 152 String operator = ' ' == _notPrecedes ? " > " : " <= "; 153 this.bshExpression.add(javaName + ".longValue()" + operator + value); 154 this.sqlExpression.add(sqlName 155 + (' ' == _notPrecedes ? "" : "NOT ") 156 + sqlInPart + operator + value + sqlEndPart); 157 if (!this.propertiesUsed.contains(javaName)) { 158 this.propertiesUsed.add(javaName); 159 } 160 } 161 162 protected void addLessThanWithSubQuery(String javaName, 163 String sqlName, 164 String sqlInPart, 165 long value, 166 String sqlEndPart) { 167 char _notPrecedes = appendOperator(true); 168 String operator = ' ' == _notPrecedes ? " < " : " >= "; 169 this.bshExpression.add(javaName + ".longValue()" + operator + value); 170 this.sqlExpression.add(sqlName 171 + (' ' == _notPrecedes ? "" : "NOT ") 172 + sqlInPart + operator + value + sqlEndPart); 173 if (!this.propertiesUsed.contains(javaName)) { 174 this.propertiesUsed.add(javaName); 175 } 176 } 177 178 protected void addEqualsWithSubQuery(String javaName, 179 String sqlName, 180 String sqlInPart, 181 long value, 182 String sqlEndPart) { 183 char _notPrecedes = appendOperator(true); 184 this.bshExpression.add(_notPrecedes 185 + javaName + ".longValue() == " + value); 186 this.sqlExpression.add(sqlName 187 + (' ' == _notPrecedes ? "" : "NOT ") 188 + sqlInPart + value + sqlEndPart); 189 if (!this.propertiesUsed.contains(javaName)) { 190 this.propertiesUsed.add(javaName); 191 } 192 } 193 194 protected void addGreaterThanWithSubQuery(String javaName, 195 String sqlName, 196 String sqlInPart, 197 double value, 198 String sqlEndPart) { 199 char _notPrecedes = appendOperator(true); 200 String operator = ' ' == _notPrecedes ? " > " : " <= "; 201 this.bshExpression.add(javaName + ".doubleValue()" + operator + value); 202 this.sqlExpression.add(sqlName 203 + (' ' == _notPrecedes ? "" : "NOT ") 204 + sqlInPart + operator + value + sqlEndPart); 205 if (!this.propertiesUsed.contains(javaName)) { 206 this.propertiesUsed.add(javaName); 207 } 208 } 209 210 protected void addLessThanWithSubQuery(String javaName, 211 String sqlName, 212 String sqlInPart, 213 double value, 214 String sqlEndPart) { 215 char _notPrecedes = appendOperator(true); 216 String operator = ' ' == _notPrecedes ? " < " : " >= "; 217 this.bshExpression.add(javaName + ".doubleValue()" + operator + value); 218 this.sqlExpression.add(sqlName 219 + (' ' == _notPrecedes ? "" : "NOT ") 220 + sqlInPart + operator + value + sqlEndPart); 221 if (!this.propertiesUsed.contains(javaName)) { 222 this.propertiesUsed.add(javaName); 223 } 224 } 225 226 protected void addEqualsWithSubQuery(String javaName, 227 String sqlName, 228 String sqlInPart, 229 double value, 230 String sqlEndPart) { 231 char _notPrecedes = appendOperator(true); 232 this.bshExpression.add(_notPrecedes 233 + javaName + ".doubleValue() == " + value); 234 this.sqlExpression.add(sqlName 235 + (' ' == _notPrecedes ? "" : "NOT ") 236 + sqlInPart + value + sqlEndPart); 237 if (!this.propertiesUsed.contains(javaName)) { 238 this.propertiesUsed.add(javaName); 239 } 240 } 241 242 protected void addEqualsWithSubQuery(String javaName, 243 String sqlName, 244 String sqlInPart, 245 String value, 246 String sqlEndPart) { 247 char _notPrecedes = appendOperator(true); 248 this.bshExpression.add(_notPrecedes 249 + javaName + ".equals(\"" + value + "\")"); 250 this.sqlExpression.add(sqlName 251 + (' ' == _notPrecedes ? "" : "NOT ") 252 + sqlInPart + getDelimiter() + value 253 + getDelimiter() + sqlEndPart); 254 if (!this.propertiesUsed.contains(javaName)) { 255 this.propertiesUsed.add(javaName); 256 } 257 } 258 259 protected void addEqualsWithSubQueryTwice(String javaName, 260 String sqlName, 261 String sqlInPart, 262 String value, 263 String sqlMiddlePart, 264 String sqlEndPart) { 265 char _notPrecedes = appendOperator(true); 266 this.bshExpression.add(_notPrecedes 267 + javaName + ".equals(\"" + value + "\")"); 268 this.sqlExpression.add(sqlName 269 + (' ' == _notPrecedes ? "" : "NOT ") 270 + sqlInPart + getDelimiter() + value 271 + getDelimiter() + sqlMiddlePart 272 + getDelimiter() + value + getDelimiter() 273 + sqlEndPart); 274 if (!this.propertiesUsed.contains(javaName)) { 275 this.propertiesUsed.add(javaName); 276 } 277 } 278 279 protected void addStartsWithSubQuery(String javaName, 280 String sqlName, 281 String sqlInPart, 282 String value, 283 String sqlEndPart) { 284 char _notPrecedes = appendOperator(true); 285 this.bshExpression.add(_notPrecedes 286 + javaName + ".startsWith(\"" + value + "\")"); 287 this.sqlExpression.add(sqlName 288 + (' ' == _notPrecedes ? "" : "NOT ") 289 + sqlInPart + getDelimiter() + value + "%" 290 + getDelimiter() + sqlEndPart); 291 if (!this.propertiesUsed.contains(javaName)) { 292 this.propertiesUsed.add(javaName); 293 } 294 } 295 296 protected void addContainsWithSubQuery(String javaName, 297 String sqlName, 298 String sqlInPart, 299 String value, 300 String sqlEndPart) { 301 char _notPrecedes = appendOperator(true); 302 this.bshExpression.add(_notPrecedes 303 + javaName + ".indexOf(\"" + value + "\") != -1"); 304 this.sqlExpression.add(sqlName 305 + (' ' == _notPrecedes ? "" : "NOT ") 306 + sqlInPart + getDelimiter() + "%" + value + "%" 307 + getDelimiter() + sqlEndPart); 308 if (!this.propertiesUsed.contains(javaName)) { 309 this.propertiesUsed.add(javaName); 310 } 311 } 312 313 protected void addContains(String javaName, String sqlName, String value) { 314 char _notPrecedes = appendOperator(true); 315 this.bshExpression.add(_notPrecedes 316 + javaName + ".indexOf(\"" + value + "\") != -1"); 317 this.sqlExpression.add(sqlName 318 + (' ' == _notPrecedes ? "" : "NOT ") + " LIKE " 319 + getDelimiter() + "%" + value + "%" 320 + getDelimiter()); 321 if (!this.propertiesUsed.contains(javaName)) { 322 this.propertiesUsed.add(javaName); 323 } 324 } 325 326 protected String parseSQLArguments() { 327 StringBuffer sql = new StringBuffer (); 328 for (Iterator i = this.sqlExpression.iterator(); i.hasNext();) { 329 Object o = i.next(); 330 if (o instanceof ExpressionBuilder) { 331 ExpressionBuilder eb = (ExpressionBuilder) o; 332 sql.append("( " + eb.toSQL() + " )"); 333 } else if (o instanceof String ) { 334 sql.append(o); 335 } 336 } 337 if (null != orderBy) sql.append(orderBy); 338 return sql.toString(); 339 } 340 341 protected String parseBshParameters() { 342 StringBuffer bsh = new StringBuffer (); 343 for (Iterator i = this.bshExpression.iterator(); i.hasNext();) { 344 Object o = i.next(); 345 if (o instanceof ExpressionBuilder) { 346 ExpressionBuilder eb = (ExpressionBuilder) o; 347 bsh.append("( " + eb.toScript() + " )"); 348 } else if (o instanceof String ) { 349 bsh.append(o); 350 } 351 } 352 return bsh.toString(); 353 } 354 355 public boolean isComplete() { 356 return sqlComplete; 357 } 358 359 protected String whatsUsed() { 360 StringBuffer ret = new StringBuffer (); 361 for (Iterator iter = this.propertiesUsed.iterator(); iter.hasNext();) { 362 ret.append(iter.next()).append(" "); 363 } 364 return ret.toString(); 365 } 366 367 public String toSQL() { 368 return parseSQLArguments(); 369 } 370 371 public String toScript() { 372 return parseBshParameters(); 373 } 374 375 public String toExpression() { 376 StringBuffer ret = new StringBuffer (); 377 if (!this.isComplete()) { 378 ret.append("/*FORCE*/\n"); 379 } 380 if (0 < this.propertiesUsed.size()) { 381 ret.append("/*used ").append(whatsUsed()).append(" used*/\n"); 382 } 383 ret.append(toScript()); 384 if (0 < this.sqlExpression.size()) { 385 ret.append("\n/*sql ").append(toSQL()).append(" sql*/"); 386 } 387 return ret.toString(); 388 } 389 390 public void setOrderBy(String column, boolean isAscending) { 391 orderBy = " ORDER BY " + column + (isAscending ? " ASC" : " DESC"); 392 } 393 } | Popular Tags |