KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > sql > visualeditor > querymodel > WhereNode


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.db.sql.visualeditor.querymodel;
20
21 /**
22  * Represents a SQL WHERE clause
23  * Example Form: WHERE ((a.x = b.y) AND (c.w = d.v))
24  */

25 // ToDo: Decide whether a null WHERE clause is better represented as a null
26
// ptr, or a Where with null condition.
27

28 import java.util.ArrayList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Collection JavaDoc;
31
32 public class WhereNode implements Where {
33
34     // Fields
35

36     // This could be an AND or (in future) OR
37
private Expression _cond;
38
39     // Constructors
40

41     public WhereNode() {
42         _cond = null;
43     }
44
45     public WhereNode(Expression cond) {
46         _cond = cond;
47 // if (cond instanceof Predicate)
48
// _cond = new And((Predicat)_cond);
49
// else
50
// _cond = cond;
51
}
52
53
54     // Accessors/mutators
55

56     public void resetExpression() {
57         _cond = null;
58     }
59
60     public void replaceExpression(Expression expression) {
61         _cond = expression;
62     }
63
64     public Expression getExpression () {
65         return _cond;
66     }
67
68     // Methods
69

70     public Expression findExpression(String JavaDoc table1, String JavaDoc column1, String JavaDoc table2, String JavaDoc column2) {
71         return _cond.findExpression(table1, column1, table2, column2);
72     }
73
74     // Remove any WHERE clauses that mention the table in question,
75
// since the table itself is being removed from the model
76
void removeTable(String JavaDoc tableSpec) {
77         if (_cond instanceof ExpressionList) {
78             ExpressionList list = (ExpressionList)_cond;
79             list.removeTable(tableSpec);
80             if (list.size() == 0)
81                 _cond = null;
82         }
83         else {
84             ArrayList JavaDoc column = new ArrayList JavaDoc();
85             _cond.getReferencedColumns(column);
86             for (int i = 0; i < column.size(); i++) {
87                 Column col = (Column)column.get(i);
88                 if (col.matches(tableSpec)) {
89                     _cond = null;
90                 }
91             }
92         }
93     }
94
95     // adds any column in the condition to the ArrayList of columns
96
public void getReferencedColumns (Collection JavaDoc columns) {
97         if (_cond != null)
98             _cond.getReferencedColumns (columns);
99     }
100
101     public void getQueryItems(Collection JavaDoc items) {
102         if (_cond != null)
103             items.add(_cond);
104     }
105
106     // Return the Where clause as a SQL string
107
public String JavaDoc genText() {
108         if (_cond!=null)
109             return "\nWHERE " + _cond.genText() ; // NOI18N
110
else
111             return ""; // NOI18N
112
}
113
114     // See if we have a parameter marker (string literal "?")
115
public boolean isParameterized() {
116         if (_cond!=null)
117             return _cond.isParameterized();
118         else
119             return false;
120     }
121
122
123     void renameTableSpec(String JavaDoc oldTableSpec, String JavaDoc corrName) {
124         _cond.renameTableSpec(oldTableSpec, corrName);
125     }
126 }
127
Popular Tags