KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23
24 /**
25  * Represents a SQL Atomic Formula
26  * Example Form: a.x = b.y
27  */

28 public final class Predicate implements Expression {
29
30     // Fields
31

32     // ToDo: Generalize this, to allows arbitrary forms on both sides
33

34     Value _val1, _val2;
35 // ColumnNode _col1, _col2;
36
String JavaDoc _op;
37
38
39     // Constructors
40

41     public Predicate() {
42     }
43
44     public Predicate(Value val1, Value val2) {
45         this(val1, val2, "="); // NOI18N
46
}
47
48     public Predicate (Value val1, Value val2, String JavaDoc op) {
49         _val1 = val1;
50         _val2 = val2;
51         _op = op;
52         }
53
54     // Special ctor used when we have an FK
55
public Predicate (String JavaDoc[] rel) {
56         _val1 = new ColumnNode(rel[0], rel[1]);
57         _val2 = new ColumnNode(rel[2], rel[3]);
58         _op = "=";
59     }
60
61     // Methods
62

63     public Expression findExpression(String JavaDoc table1, String JavaDoc column1, String JavaDoc table2, String JavaDoc column2) {
64         if ((_val1 instanceof ColumnNode) && (_val2 instanceof ColumnNode)) {
65             ColumnNode col1 = (ColumnNode) _val1;
66             ColumnNode col2 = (ColumnNode) _val2;
67             if ((col1.matches(table1, column1) && col2.matches(table2, column2)) ||
68                 (col2.matches(table1, column1) && col1.matches(table2, column2))) {
69                 return this;
70             }
71         }
72         return null;
73     }
74
75     // get the columns specified in the condition if any
76
public void getReferencedColumns(Collection JavaDoc columns) {
77         if (_val1 instanceof ColumnItem) {
78             columns.add (((ColumnItem)_val1).getReferencedColumn());
79         }
80
81         if (_val2 instanceof ColumnItem) {
82             columns.add(((ColumnItem)_val2).getReferencedColumn());
83         }
84     }
85
86     public void getQueryItems(Collection JavaDoc items) {
87         items.add(_val1);
88         items.add(_val2);
89     }
90
91     // Return the Where clause as a SQL string
92
public String JavaDoc genText() {
93         return _val1.genText() + " " + _op + " " + _val2.genText(); // NOI18N
94
}
95
96     public Value getVal1() {
97         return _val1;
98     }
99
100     public void setVal1(Value val1 ) {
101         _val1 = val1;
102     }
103
104     public Value getVal2() {
105         return _val2;
106     }
107
108     public void setVal2(Value val2) {
109         _val2 = val2;
110     }
111
112     public String JavaDoc getOp() {
113         return _op;
114     }
115
116     public void setFields(String JavaDoc tableName1, String JavaDoc columnName1, String JavaDoc tableName2, String JavaDoc columnName2)
117     {
118         _val1 = new ColumnNode(tableName1, columnName1);
119         _val2 = new ColumnNode(tableName2, columnName2);
120     }
121
122     /**
123      * Return true if the Predicate is a criterion, rather than a relationship
124      */

125     public boolean isCriterion () {
126
127         // If both sides of the predicate are columns, we have a relationship
128
return ( ! ((getVal1() instanceof ColumnNode) && (getVal2() instanceof ColumnNode)));
129     }
130
131     /** Rename any column specs that use the old table spec
132      */

133     public void renameTableSpec(String JavaDoc oldTableSpec, String JavaDoc corrName) {
134
135         if (_val1 instanceof ColumnNode)
136             ((ColumnNode)_val1).renameTableSpec(oldTableSpec, corrName);
137         if (_val2 instanceof ColumnNode)
138             ((ColumnNode)_val2).renameTableSpec(oldTableSpec, corrName);
139     }
140
141     public boolean isParameterized() {
142         return (_val1.isParameterized() || _val2.isParameterized());
143     }
144
145 }
146
Popular Tags