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: /**
044: * Represents a SQL WHERE clause
045: * Example Form: WHERE ((a.x = b.y) AND (c.w = d.v))
046: */
047: // ToDo: Decide whether a null WHERE clause is better represented as a null
048: // ptr, or a Where with null condition.
049: import java.util.ArrayList;
050: import java.util.Collection;
051:
052: import org.netbeans.api.db.sql.support.SQLIdentifiers;
053:
054: public class WhereNode implements Where {
055:
056: // Fields
057:
058: // This could be an AND or (in future) OR
059: private Expression _cond;
060:
061: // Constructors
062:
063: public WhereNode() {
064: _cond = null;
065: }
066:
067: public WhereNode(Expression cond) {
068: _cond = cond;
069: // if (cond instanceof Predicate)
070: // _cond = new And((Predicat)_cond);
071: // else
072: // _cond = cond;
073: }
074:
075: // Accessors/mutators
076:
077: public void resetExpression() {
078: _cond = null;
079: }
080:
081: public void replaceExpression(Expression expression) {
082: _cond = expression;
083: }
084:
085: public Expression getExpression() {
086: return _cond;
087: }
088:
089: // Methods
090:
091: public Expression findExpression(String table1, String column1,
092: String table2, String column2) {
093: return _cond.findExpression(table1, column1, table2, column2);
094: }
095:
096: // Remove any WHERE clauses that mention the table in question,
097: // since the table itself is being removed from the model
098: void removeTable(String tableSpec) {
099: if (_cond instanceof ExpressionList) {
100: ExpressionList list = (ExpressionList) _cond;
101: list.removeTable(tableSpec);
102: if (list.size() == 0)
103: _cond = null;
104: } else {
105: ArrayList column = new ArrayList();
106: _cond.getReferencedColumns(column);
107: for (int i = 0; i < column.size(); i++) {
108: Column col = (Column) column.get(i);
109: if (col.matches(tableSpec)) {
110: _cond = null;
111: }
112: }
113: }
114: }
115:
116: // adds any column in the condition to the ArrayList of columns
117: public void getReferencedColumns(Collection columns) {
118: if (_cond != null)
119: _cond.getReferencedColumns(columns);
120: }
121:
122: // Return the Where clause as a SQL string
123: public String genText(SQLIdentifiers.Quoter quoter) {
124: if (_cond != null)
125: return " WHERE " + _cond.genText(quoter); // NOI18N
126: else
127: return ""; // NOI18N
128: }
129:
130: // See if we have a parameter marker (string literal "?")
131: public boolean isParameterized() {
132: if (_cond != null)
133: return _cond.isParameterized();
134: else
135: return false;
136: }
137:
138: void renameTableSpec(String oldTableSpec, String corrName) {
139: _cond.renameTableSpec(oldTableSpec, corrName);
140: }
141: }
|