KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > store > impl > rdbms > expression > RDBMSCompareExpression


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/stores/org/apache/slide/store/impl/rdbms/expression/RDBMSCompareExpression.java,v 1.4.2.4 2004/11/23 16:04:18 unico Exp $
3  * $Revision: 1.4.2.4 $
4  * $Date: 2004/11/23 16:04:18 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23 package org.apache.slide.store.impl.rdbms.expression;
24
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
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 JavaDoc COMPARE_OPERATORS = new HashMap JavaDoc(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 JavaDoc 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 JavaDoc selectKey = getPropertyNamespace() + getPropertyName();
82         _context.selects().put(selectKey, select());
83         return _context.results();
84     }
85
86     protected String JavaDoc 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 JavaDoc join() {
95         return "inner join PROPERTIES p" + _tableIndex + " on p" + _tableIndex + ".VERSION_ID = vh.VERSION_ID";
96     }
97
98     protected String JavaDoc select() {
99         // TODO: qualify alias
100
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 JavaDoc getPropertyName() {
117         return getProperty().getName();
118     }
119     
120     protected String JavaDoc getPropertyNamespace() {
121         return getProperty().getNamespaceURI();
122     }
123     
124 }
Popular Tags