1 23 package org.apache.slide.store.impl.rdbms.expression; 24 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 28 import org.apache.slide.content.NodeProperty.NamespaceCache; 29 import org.apache.slide.search.SearchException; 30 import org.apache.slide.search.basic.IBasicResultSet; 31 import org.apache.slide.search.basic.Literals; 32 import org.jdom.Element; 33 34 public class RDBMSCompareExpression extends RDBMSExpression { 35 36 static final HashMap COMPARE_OPERATORS = new HashMap (11); 37 static { 38 COMPARE_OPERATORS.put(Literals.EQ, "="); 39 COMPARE_OPERATORS.put(Literals.GT, ">"); 40 COMPARE_OPERATORS.put(Literals.GTE, ">="); 41 COMPARE_OPERATORS.put(Literals.LT, "<"); 42 COMPARE_OPERATORS.put(Literals.LTE, "<="); 43 COMPARE_OPERATORS.put(Literals.NOT_EQ, "<>"); 44 COMPARE_OPERATORS.put(Literals.NOT_GT, COMPARE_OPERATORS.get("LTE")); 45 COMPARE_OPERATORS.put(Literals.NOT_GTE, COMPARE_OPERATORS.get("LT")); 46 COMPARE_OPERATORS.put(Literals.NOT_LT, COMPARE_OPERATORS.get("GTE")); 47 COMPARE_OPERATORS.put(Literals.NOT_LTE, COMPARE_OPERATORS.get("GT")); 48 } 49 50 protected final Element _element; 51 protected int _tableIndex = -1; 52 private Element _property; 53 54 public RDBMSCompareExpression(Element element, RDBMSQueryContext context) { 55 super(context); 56 _element = element; 57 } 58 59 public IBasicResultSet execute() throws SearchException { 60 return compile(null); 61 } 62 63 protected IBasicResultSet compile(RDBMSMergeExpression expression) { 64 if (expression != null && expression.getName().equals(Literals.OR)) { 65 Iterator iter = expression.getRDBMSExpressions().iterator(); 66 while (iter.hasNext()) { 67 final RDBMSExpression e = (RDBMSExpression) iter.next(); 68 if (e instanceof RDBMSCompareExpression) { 69 _tableIndex = ((RDBMSCompareExpression) e).getTableIndex(); 70 if (_tableIndex != -1) { 71 break; 72 } 73 } 74 } 75 } 76 if (_tableIndex == -1) { 77 _tableIndex = _context.joins().size(); 78 } 79 _context.joins().add(join()); 80 _context.criteria().add(compile()); 81 final String selectKey = getPropertyNamespace() + getPropertyName(); 82 _context.selects().put(selectKey, select()); 83 return _context.results(); 84 } 85 86 protected String compile() { 87 Element literal = _element.getChild(Literals.LITERAL, NamespaceCache.DEFAULT_NAMESPACE); 88 return "(p" + _tableIndex + ".PROPERTY_NAME = '" + getPropertyName() + "' AND " + 89 "p" + _tableIndex + ".PROPERTY_NAMESPACE = '" + getPropertyNamespace() + "' AND " + 90 "p" + _tableIndex + ".PROPERTY_VALUE " + COMPARE_OPERATORS.get(_element.getName()) 91 + " '" + literal.getTextNormalize() + "')"; 92 } 93 94 protected String join() { 95 return "inner join PROPERTIES p" + _tableIndex + " on p" + _tableIndex + ".VERSION_ID = vh.VERSION_ID"; 96 } 97 98 protected String select() { 99 return "p" + _tableIndex + ".PROPERTY_VALUE AS " 101 + RDBMSExpressionFactory.propertyToAlias(getPropertyName()); 102 } 103 104 protected int getTableIndex() { 105 return _tableIndex; 106 } 107 108 protected Element getProperty() { 109 if (_property == null) { 110 _property = (Element) _element.getChild(Literals.PROP, 111 NamespaceCache.DEFAULT_NAMESPACE).getChildren().get(0); 112 } 113 return _property; 114 } 115 116 protected String getPropertyName() { 117 return getProperty().getName(); 118 } 119 120 protected String getPropertyNamespace() { 121 return getProperty().getNamespaceURI(); 122 } 123 124 } | Popular Tags |