001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.db.sql.visualeditor.querymodel;
042:
043: import java.util.Collection;
044:
045: import org.netbeans.api.db.sql.support.SQLIdentifiers;
046:
047: /**
048: * Represents a SQL Atomic Formula
049: * Example Form: a.x = b.y
050: */
051: public final class Predicate implements Expression {
052:
053: // Fields
054:
055: // ToDo: Generalize this, to allows arbitrary forms on both sides
056:
057: Value _val1, _val2;
058: // ColumnNode _col1, _col2;
059: String _op;
060:
061: // Constructors
062:
063: public Predicate() {
064: }
065:
066: public Predicate(Value val1, Value val2) {
067: this (val1, val2, "="); // NOI18N
068: }
069:
070: public Predicate(Value val1, Value val2, String op) {
071: _val1 = val1;
072: _val2 = val2;
073: _op = op;
074: }
075:
076: // Special ctor used when we have an FK
077: public Predicate(String[] rel) {
078: _val1 = new ColumnNode(rel[0], rel[1]);
079: _val2 = new ColumnNode(rel[2], rel[3]);
080: _op = "=";
081: }
082:
083: // Methods
084:
085: public Expression findExpression(String table1, String column1,
086: String table2, String column2) {
087: if ((_val1 instanceof ColumnNode)
088: && (_val2 instanceof ColumnNode)) {
089: ColumnNode col1 = (ColumnNode) _val1;
090: ColumnNode col2 = (ColumnNode) _val2;
091: if ((col1.matches(table1, column1) && col2.matches(table2,
092: column2))
093: || (col2.matches(table1, column1) && col1.matches(
094: table2, column2))) {
095: return this ;
096: }
097: }
098: return null;
099: }
100:
101: // get the columns specified in the condition if any
102: public void getReferencedColumns(Collection columns) {
103: if (_val1 instanceof ColumnItem) {
104: columns.add(((ColumnItem) _val1).getReferencedColumn());
105: }
106:
107: if (_val2 instanceof ColumnItem) {
108: columns.add(((ColumnItem) _val2).getReferencedColumn());
109: }
110: }
111:
112: // Return the Where clause as a SQL string
113: public String genText(SQLIdentifiers.Quoter quoter) {
114: return _val1.genText(quoter) + " " + _op + " "
115: + _val2.genText(quoter); // NOI18N
116: }
117:
118: public Value getVal1() {
119: return _val1;
120: }
121:
122: public void setVal1(Value val1) {
123: _val1 = val1;
124: }
125:
126: public Value getVal2() {
127: return _val2;
128: }
129:
130: public void setVal2(Value val2) {
131: _val2 = val2;
132: }
133:
134: public String getOp() {
135: return _op;
136: }
137:
138: public void setFields(String tableName1, String columnName1,
139: String tableName2, String columnName2) {
140: _val1 = new ColumnNode(tableName1, columnName1);
141: _val2 = new ColumnNode(tableName2, columnName2);
142: }
143:
144: /**
145: * Return true if the Predicate is a criterion, rather than a relationship
146: */
147: public boolean isCriterion() {
148:
149: // If both sides of the predicate are columns, we have a relationship
150: return (!((getVal1() instanceof ColumnNode) && (getVal2() instanceof ColumnNode)));
151: }
152:
153: /** Rename any column specs that use the old table spec
154: */
155: public void renameTableSpec(String oldTableSpec, String corrName) {
156:
157: if (_val1 instanceof ColumnNode)
158: ((ColumnNode) _val1)
159: .renameTableSpec(oldTableSpec, corrName);
160: if (_val2 instanceof ColumnNode)
161: ((ColumnNode) _val2)
162: .renameTableSpec(oldTableSpec, corrName);
163: }
164:
165: public boolean isParameterized() {
166: return (_val1.isParameterized() || _val2.isParameterized());
167: }
168:
169: }
|