01: /*
02: * Copyright (C) 1999-2004 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2 of the License, or (at your option) any later version.
08: *
09: * This library is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: * Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public
15: * License along with this library; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: */
18:
19: package org.mandarax.jdbc.server.sql;
20:
21: import java.util.*;
22:
23: /**
24: * Represents the SQL WHERE clause.
25: * @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
26: * @version 3.3.2 <29 December 2004>
27: * @since 3.0
28: */
29:
30: public class WhereClause extends CompoundCondition implements
31: ConditionContext {
32: private SelectStatement owner = null;
33:
34: /**
35: * Constructor.
36: */
37: public WhereClause() {
38: super ();
39: }
40:
41: /**
42: * @return SelectStatement
43: */
44: public SelectStatement getOwner() {
45: return owner;
46: }
47:
48: /**
49: * Sets the owner.
50: * @param owner The owner to set
51: */
52: void setOwner(SelectStatement owner) {
53: this .owner = owner;
54: }
55:
56: /**
57: * Extract conditions of the form "column name = value" and "column name1 = column name2" that can be used
58: * in order to optimize a mandarax query. These conditions are removed from the list of conditions (if any,
59: * must be connected with AND).
60: * TODO: full normalization of AND/OR/NOT connectives !!
61: * @return a list of equals conditions
62: */
63: List extractEqualConditions() {
64: List equalConditions = new ArrayList();
65: if (conditions.size() == 0 || isNegated())
66: return equalConditions;
67: else if (connective == AND || conditions.size() == 1) {
68: for (Iterator iter = conditions.iterator(); iter.hasNext();) {
69: Condition c = (Condition) iter.next();
70: if (c instanceof SimpleCondition
71: && ((SimpleCondition) c).getConnective() == SimpleCondition.EQUALS
72: && !c.isNegated()) {
73: equalConditions.add(c);
74: iter.remove();
75: }
76: }
77: }
78: return equalConditions;
79: }
80:
81: }
|