1 23 package org.objectweb.medor.expression.converter.rdb; 24 25 import org.objectweb.jorm.mapper.rdb.adapter.api.RdbAdapter; 26 import org.objectweb.jorm.mapper.rdb.adapter.api.RdbAdapterException; 27 import org.objectweb.jorm.type.api.PType; 28 import org.objectweb.jorm.type.api.PTypeSpace; 29 import org.objectweb.medor.expression.api.BinaryOperator; 30 import org.objectweb.medor.expression.api.Expression; 31 import org.objectweb.medor.expression.api.ExpressionException; 32 import org.objectweb.medor.expression.api.Operator; 33 import org.objectweb.medor.expression.api.ParameterOperand; 34 import org.objectweb.medor.expression.api.UnaryOperator; 35 import org.objectweb.medor.expression.lib.Abs; 36 import org.objectweb.medor.expression.lib.BasicOperand; 37 import org.objectweb.medor.expression.lib.Concat; 38 import org.objectweb.medor.expression.lib.FirstLocate; 39 import org.objectweb.medor.expression.lib.IndexedLocate; 40 import org.objectweb.medor.expression.lib.Length; 41 import org.objectweb.medor.expression.lib.Like; 42 import org.objectweb.medor.expression.lib.Mod; 43 import org.objectweb.medor.expression.lib.Round; 44 import org.objectweb.medor.expression.lib.Sqrt; 45 import org.objectweb.medor.expression.lib.Substring; 46 import org.objectweb.medor.expression.lib.StringUpper; 47 import org.objectweb.medor.expression.lib.StringLower; 48 import org.objectweb.medor.expression.converter.api.Expression2WhereClause; 49 50 55 public class Expression2WhereClauseImpl implements Expression2WhereClause { 56 57 private final static String LAND = "&"; 58 private final static String LCOND_AND = "&&"; 59 private final static String AND = "AND"; 60 private final static String EQUAL = "="; 61 private final static String LEQUAL = "=="; 62 private final static String LOR = "|"; 63 private final static String LCOND_OR = "||"; 64 private final static String OR = "OR"; 65 private final static String NOT = "NOT"; 66 67 public final String convertExp2WhereClause(Expression exp, 68 RdbAdapter rdbAdapter) 69 throws ExpressionException { 70 StringBuffer sb = new StringBuffer (); 71 convertExp2WhereClause(exp, rdbAdapter, sb); 72 return sb.toString(); 73 } 74 75 83 public void convertExp2WhereClause(Expression exp, 84 RdbAdapter rdbAdapter, 85 StringBuffer sb) 86 throws ExpressionException { 87 if (exp instanceof ParameterOperand) { 88 sb.append("?"); 89 } else if (exp instanceof BasicOperand) { 90 BasicOperand o = (BasicOperand) exp; 91 if (rdbAdapter != null) { 92 switch (o.getType().getTypeCode()) { 93 case PType.TYPECODE_BOOLEAN: 94 sb.append(rdbAdapter.getValueAsSQLString(o.getBoolean())); 95 break; 96 case PType.TYPECODE_CHAR: 97 sb.append(rdbAdapter.getValueAsSQLString(o.getChar())); 98 break; 99 case PType.TYPECODE_BYTE: 100 sb.append(rdbAdapter.getValueAsSQLString(o.getByte())); 101 break; 102 case PType.TYPECODE_SHORT: 103 sb.append(rdbAdapter.getValueAsSQLString(o.getShort())); 104 break; 105 case PType.TYPECODE_INT: 106 sb.append(rdbAdapter.getValueAsSQLString(o.getInt())); 107 break; 108 case PType.TYPECODE_LONG: 109 sb.append(rdbAdapter.getValueAsSQLString(o.getLong())); 110 break; 111 case PType.TYPECODE_FLOAT: 112 sb.append(rdbAdapter.getValueAsSQLString(o.getFloat())); 113 break; 114 case PType.TYPECODE_DOUBLE: 115 sb.append(rdbAdapter.getValueAsSQLString(o.getDouble())); 116 break; 117 default: 118 sb.append(rdbAdapter.getValueAsSQLString( 119 o.getObject(), 120 o.getType().getTypeCode())); 121 } 122 } else { 123 switch (o.getType().getTypeCode()) { 124 case PType.TYPECODE_OBJBOOLEAN: 125 case PType.TYPECODE_BOOLEAN: 126 sb.append((o.getBoolean() ? "1" : "0")); 127 break; 128 case PType.TYPECODE_BYTE: 129 case PType.TYPECODE_OBJBYTE: 130 case PType.TYPECODE_SHORT: 131 case PType.TYPECODE_OBJSHORT: 132 case PType.TYPECODE_INT: 133 case PType.TYPECODE_OBJINT: 134 case PType.TYPECODE_LONG: 135 case PType.TYPECODE_OBJLONG: 136 case PType.TYPECODE_FLOAT: 137 case PType.TYPECODE_OBJFLOAT: 138 case PType.TYPECODE_DOUBLE: 139 case PType.TYPECODE_OBJDOUBLE: 140 sb.append(o.getValueAsString()); 141 break; 142 default: 143 sb.append("'" + o.getValueAsString() + "'"); 144 break; 145 } 146 } 147 } else if (exp instanceof Round) { 148 Round round = (Round) exp; 149 sb.append("ROUND("); 150 convertExp2WhereClause(round.getExpression(0), rdbAdapter, sb); 151 sb.append(", "); 152 sb.append(round.getRound()); 153 sb.append(")"); 154 } else if (exp instanceof Like) { 155 Like l = (Like) exp; 156 sb.append("("); 157 convertExp2WhereClause(l.getExpression(0), rdbAdapter, sb); 158 sb.append(" "); 159 sb.append(l.getOperatorString()); 160 sb.append(" "); 161 convertExp2WhereClause(l.getExpression(1), rdbAdapter, sb); 162 if (l.getOperandNumber() == 3) { 163 sb.append(" ESCAPE "); 164 convertExp2WhereClause(l.getExpression(2), rdbAdapter, sb); 165 } 166 sb.append(")"); 167 } else if (exp instanceof FirstLocate) { Operator op = (Operator) exp; 169 if (rdbAdapter == null) { 170 sb.append(toSqlOperator(op.getOperatorString())); 171 sb.append("("); 172 convertExp2WhereClause(op.getExpression(0), rdbAdapter, sb); 173 sb.append(", "); 174 convertExp2WhereClause(op.getExpression(1), rdbAdapter, sb); 175 sb.append(")"); 176 } else { 177 try { 178 sb.append( 179 rdbAdapter.getFirstLocateExpression( 180 convertExp2WhereClause(op.getExpression(0), rdbAdapter), 181 convertExp2WhereClause(op.getExpression(1), rdbAdapter) 182 )); 183 } catch (RdbAdapterException pe) { 184 throw new ExpressionException(pe.getMessage()); 185 } 186 } 187 } else if (exp instanceof Concat) { 188 Operator op = (Operator) exp; 189 if (rdbAdapter == null) { 190 sb.append(toSqlOperator(op.getOperatorString())); 191 sb.append("("); 192 convertExp2WhereClause(op.getExpression(0), rdbAdapter, sb); 193 sb.append(", "); 194 convertExp2WhereClause(op.getExpression(1), rdbAdapter, sb); 195 sb.append(")"); 196 } else { 197 rdbAdapter.escapeFunctionOpen(sb); 198 sb.append(rdbAdapter.getConcatExpression( 199 convertExp2WhereClause(op.getExpression(0), rdbAdapter), 200 convertExp2WhereClause(op.getExpression(1), rdbAdapter) 201 )); 202 rdbAdapter.escapeFunctionClose(sb); 203 } 204 } else if ((exp instanceof Length) || (exp instanceof Abs) || 205 (exp instanceof Sqrt)) { 206 Operator op = (Operator) exp; 207 if (rdbAdapter != null) { 208 rdbAdapter.escapeFunctionOpen(sb); 209 } 210 sb.append(toSqlOperator(op.getOperatorString())); 211 sb.append("("); 212 convertExp2WhereClause(op.getExpression(0), rdbAdapter, sb); 213 sb.append(")"); 214 if (rdbAdapter != null) { 215 rdbAdapter.escapeFunctionClose(sb); 216 } 217 } else if (exp instanceof Mod) { 218 Operator op = (Operator) exp; 219 if (rdbAdapter != null) { 220 rdbAdapter.escapeFunctionOpen(sb); 221 } 222 sb.append(toSqlOperator(op.getOperatorString())); 223 sb.append("("); 224 convertExp2WhereClause(op.getExpression(0), rdbAdapter, sb); 225 sb.append(", "); 226 convertExp2WhereClause(op.getExpression(1), rdbAdapter, sb); 227 sb.append(")"); 228 if (rdbAdapter != null) { 229 rdbAdapter.escapeFunctionClose(sb); 230 } 231 } else if (exp instanceof BinaryOperator) { 232 Operator op = (Operator) exp; 233 sb.append("("); 234 convertExp2WhereClause(op.getExpression(0), rdbAdapter, sb); 235 sb.append(" "); 236 sb.append(toSqlOperator(op.getOperatorString())); 237 sb.append(" "); 238 convertExp2WhereClause(op.getExpression(1), rdbAdapter, sb); 239 sb.append(")"); 240 } else if (exp instanceof UnaryOperator) { 241 Operator op = (Operator) exp; 242 sb.append(toSqlOperator(op.getOperatorString())); 243 sb.append("("); 244 convertExp2WhereClause(op.getExpression(0), rdbAdapter, sb); 245 sb.append(")"); 246 } else if (exp instanceof Substring) { 247 Operator op = (Operator) exp; 248 if (rdbAdapter != null) { 249 rdbAdapter.escapeFunctionOpen(sb); 250 } 251 sb.append(toSqlOperator(op.getOperatorString())); 252 sb.append("("); 253 sb.append(convertExp2WhereClause(op.getExpression(0), rdbAdapter)); 254 sb.append(", "); 255 sb.append(convertExp2WhereClause(op.getExpression(1), rdbAdapter)); 256 sb.append(", "); 257 sb.append(convertExp2WhereClause(op.getExpression(2), rdbAdapter)); 258 sb.append(")"); 259 if (rdbAdapter != null) { 260 rdbAdapter.escapeFunctionClose(sb); 261 } 262 } else if (exp instanceof StringUpper || exp instanceof StringLower) { 263 Operator op = (Operator) exp; 264 sb.append(toSqlOperator(op.getOperatorString())); 265 sb.append("("); 266 sb.append(convertExp2WhereClause(op.getExpression(0), rdbAdapter)); 267 sb.append(")"); 268 } else if (exp instanceof StringUpper 269 || exp instanceof StringLower || exp instanceof Length) { 270 Operator op = (Operator) exp; 271 sb.append(toSqlOperator(op.getOperatorString())); 272 sb.append("("); 273 sb.append(convertExp2WhereClause(op.getExpression(0), rdbAdapter)); 274 sb.append(")"); 275 } else if (exp instanceof IndexedLocate) { 276 Operator op = (Operator) exp; 277 if (rdbAdapter == null) { 278 sb.append(toSqlOperator(op.getOperatorString())); 279 sb.append("("); 280 convertExp2WhereClause(op.getExpression(0), rdbAdapter, sb); 281 sb.append(", "); 282 convertExp2WhereClause(op.getExpression(1), rdbAdapter, sb); 283 sb.append(", "); 284 convertExp2WhereClause(op.getExpression(2), rdbAdapter, sb); 285 sb.append(")"); 286 } else { 287 try { 288 sb.append( 289 rdbAdapter.getIndexedLocateExpression( 290 convertExp2WhereClause(op.getExpression(0), rdbAdapter), 291 convertExp2WhereClause(op.getExpression(1), rdbAdapter), 292 convertExp2WhereClause(op.getExpression(2), rdbAdapter) 293 )); 294 } catch (RdbAdapterException pe) { 295 throw new ExpressionException(pe.getMessage()); 296 } 297 } 298 } else 299 throw new ExpressionException("Umanaged expression element: " + exp); 300 } 301 302 305 public String toSqlOperator(String in) { 306 if (LAND.equals(in) || LCOND_AND.equals(in)) { 307 return AND; 308 } else if (LOR.equals(in) || LCOND_OR.equals(in)) { 309 return OR; 310 } else if (in.equals(LEQUAL)) { 311 return EQUAL; 312 } else if (in.equals(Operator.NOT)) { 313 return NOT; 314 } else 315 return in; 316 } 317 318 326 public void convertExp2WhereClauseBuilder(Expression exp, 327 String rdbAdapterVarName, 328 StringBuffer sb) 329 throws ExpressionException { 330 if (exp instanceof ParameterOperand) { 331 sb.append("?"); 332 } else if (exp instanceof BasicOperand) { 333 sb.append("\" + "); 334 sb.append(rdbAdapterVarName); 335 sb.append(".getValueAsSQLString("); 336 BasicOperand o = (BasicOperand) exp; 337 switch (o.getType().getTypeCode()) { 338 case PType.TYPECODE_BOOLEAN: 339 case PType.TYPECODE_OBJBOOLEAN: 340 sb.append(o.getBoolean()); 341 break; 342 case PType.TYPECODE_CHAR: 343 case PType.TYPECODE_OBJCHAR: 344 sb.append("'"); 345 sb.append(o.getChar()); 346 sb.append("'"); 347 break; 348 case PType.TYPECODE_BYTE: 349 case PType.TYPECODE_OBJBYTE: 350 sb.append("(byte) "); 351 sb.append(o.getByte()); 352 break; 353 case PType.TYPECODE_OBJSHORT: 354 sb.append("(short) "); 355 sb.append(o.getShort()); 356 break; 357 case PType.TYPECODE_INT: 358 case PType.TYPECODE_OBJINT: 359 sb.append(o.getInt()); 360 break; 361 case PType.TYPECODE_LONG: 362 case PType.TYPECODE_OBJLONG: 363 sb.append(o.getLong()); 364 sb.append("l"); 365 break; 366 case PType.TYPECODE_FLOAT: 367 case PType.TYPECODE_OBJFLOAT: 368 sb.append("(float) "); 369 sb.append(o.getFloat()); 370 break; 371 case PType.TYPECODE_DOUBLE: 372 case PType.TYPECODE_OBJDOUBLE: 373 sb.append("(double) "); 374 sb.append(o.getDouble()); 375 break; 376 case PType.TYPECODE_STRING: 377 sb.append("\""); 378 sb.append(o.getString()); 379 sb.append("\", "); 380 sb.append(PTypeSpace.STRING.getProgName()); 381 break; 382 case PType.TYPECODE_DATE: 383 sb.append("new java.util.Date("); 384 sb.append(o.getDate().getTime()); 385 sb.append("), "); 386 sb.append(PTypeSpace.DATE.getProgName()); 387 break; 388 case PType.TYPECODE_BIGINTEGER: 389 sb.append("new java.math.BigInteger("); 390 sb.append(o.getBigInteger().toString()); 391 sb.append("), "); 392 sb.append(PTypeSpace.BIGINTEGER.getProgName()); 393 break; 394 case PType.TYPECODE_BIGDECIMAL: 395 sb.append("new java.math.BigDecimal("); 396 sb.append(o.getBigDecimal().toString()); 397 sb.append("), "); 398 sb.append(PTypeSpace.BIGDECIMAL.getProgName()); 399 break; 400 default: 401 sb.append("null"); 402 } 403 sb.append(") + \""); 404 } else if (exp instanceof Round) { 405 Round round = (Round) exp; 406 sb.append("ROUND("); 407 convertExp2WhereClauseBuilder(round.getExpression(0), rdbAdapterVarName, sb); 408 sb.append(", "); 409 sb.append(round.getRound()); 410 sb.append(")"); 411 412 } else if (exp instanceof Like) { 413 Like l = (Like) exp; 414 sb.append("("); 415 convertExp2WhereClauseBuilder(l.getExpression(0), rdbAdapterVarName, sb); 416 sb.append(l.getOperatorString()); 417 convertExp2WhereClauseBuilder(l.getExpression(1), rdbAdapterVarName, sb); 418 if (l.getOperandNumber() == 3) { 419 sb.append(" ESCAPE "); 420 convertExp2WhereClauseBuilder(l.getExpression(2), rdbAdapterVarName, sb); 421 sb.append(")"); 422 } else { 423 sb.append(")"); 424 } 425 } else if (exp instanceof FirstLocate) { Operator op = (Operator) exp; 427 sb.append("\" + "); 428 sb.append(rdbAdapterVarName); 429 sb.append(".getFirstLocateExpression("); 430 convertExp2WhereClauseBuilder(op.getExpression(0), rdbAdapterVarName, sb); 431 sb.append(", "); 432 convertExp2WhereClauseBuilder(op.getExpression(1), rdbAdapterVarName, sb); 433 sb.append(") + \""); 434 } else if (exp instanceof Concat) { 435 Operator op = (Operator) exp; 436 sb.append("\" + "); 437 sb.append(rdbAdapterVarName); 438 sb.append(".getConcatExpression("); 439 convertExp2WhereClauseBuilder(op.getExpression(0), rdbAdapterVarName, sb); 440 sb.append(", "); 441 convertExp2WhereClauseBuilder(op.getExpression(1), rdbAdapterVarName, sb); 442 sb.append(") + \""); 443 } else if (exp instanceof Mod) { 444 Operator op = (Operator) exp; 445 sb.append(toSqlOperator(op.getOperatorString())); 446 sb.append("("); 447 convertExp2WhereClauseBuilder(op.getExpression(0), rdbAdapterVarName, sb); 448 sb.append(", "); 449 convertExp2WhereClauseBuilder(op.getExpression(1), rdbAdapterVarName, sb); 450 sb.append(")"); 451 } else if (exp instanceof BinaryOperator) { 452 Operator op = (Operator) exp; 453 sb.append("("); 454 convertExp2WhereClauseBuilder(op.getExpression(0), rdbAdapterVarName, sb); 455 sb.append(" "); 456 sb.append(toSqlOperator(op.getOperatorString())); 457 sb.append(" "); 458 convertExp2WhereClauseBuilder(op.getExpression(1), rdbAdapterVarName, sb); 459 sb.append(")"); 460 } else if (exp instanceof UnaryOperator) { 461 Operator op = (Operator) exp; 462 sb.append(toSqlOperator(op.getOperatorString())); 463 sb.append("("); 464 convertExp2WhereClauseBuilder(op.getExpression(0), rdbAdapterVarName, sb); 465 sb.append(")"); 466 } else if (exp instanceof Substring) { 467 Operator op = (Operator) exp; 468 sb.append("substring"); 469 sb.append("("); 470 convertExp2WhereClauseBuilder(op.getExpression(0), rdbAdapterVarName, sb); 471 sb.append(", "); 472 convertExp2WhereClauseBuilder(op.getExpression(1), rdbAdapterVarName, sb); 473 sb.append(", "); 474 convertExp2WhereClauseBuilder(op.getExpression(2), rdbAdapterVarName, sb); 475 sb.append(")"); 476 } else if (exp instanceof Length) { 477 Operator op = (Operator) exp; 478 sb.append("\" + "); 479 sb.append(rdbAdapterVarName); 480 sb.append(".getLengthOperator() + \""); 481 sb.append("("); 482 convertExp2WhereClauseBuilder(op.getExpression(0), rdbAdapterVarName, sb); 483 sb.append(")"); 484 } else if (exp instanceof StringUpper 485 || exp instanceof StringLower) { 486 Operator op = (Operator) exp; 487 sb.append(toSqlOperator(op.getOperatorString())); 488 sb.append("("); 489 convertExp2WhereClauseBuilder(op.getExpression(0), rdbAdapterVarName, sb); 490 sb.append(")"); 491 } else if (exp instanceof IndexedLocate) { 492 Operator op = (Operator) exp; 493 sb.append("\" + "); 494 sb.append(rdbAdapterVarName); 495 sb.append(".getIndexedLocateExpression("); 496 convertExp2WhereClauseBuilder(op.getExpression(0), rdbAdapterVarName, sb); 497 sb.append(", "); 498 convertExp2WhereClauseBuilder(op.getExpression(1), rdbAdapterVarName, sb); 499 sb.append(", "); 500 convertExp2WhereClauseBuilder(op.getExpression(2), rdbAdapterVarName, sb); 501 sb.append(") + \""); 502 } else 503 throw new ExpressionException("Umanaged expression element: " + exp); 504 } 505 } 506 | Popular Tags |