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.ArrayList;
044: import java.util.Collection;
045:
046: import org.netbeans.api.db.sql.support.SQLIdentifiers;
047:
048: /**
049: * Represents a SQL ORDER BY clause
050: */
051: public class OrderByNode implements OrderBy {
052:
053: // Fields
054:
055: // A vector of generalized column objects (JoinTables)
056:
057: ArrayList _sortSpecificationList;
058:
059: // Constructors
060:
061: public OrderByNode() {
062: }
063:
064: public OrderByNode(ArrayList sortSpecificationList) {
065: _sortSpecificationList = sortSpecificationList;
066: }
067:
068: // Methods
069:
070: // Return the SQL string that corresponds to this From clause
071: public String genText(SQLIdentifiers.Quoter quoter) {
072: String res = ""; // NOI18N
073: if (_sortSpecificationList != null
074: && _sortSpecificationList.size() > 0) {
075:
076: res = " ORDER BY "
077: + ((SortSpecification) _sortSpecificationList
078: .get(0)).genText(quoter); // NOI18N
079:
080: for (int i = 1; i < _sortSpecificationList.size(); i++) {
081: res += ", "
082: + " "
083: + // NOI18N
084: ((SortSpecification) _sortSpecificationList
085: .get(i)).genText(quoter);
086: }
087: }
088:
089: return res;
090: }
091:
092: // Methods
093:
094: // Accessors/Mutators
095:
096: void renameTableSpec(String oldTableSpec, String corrName) {
097: if (_sortSpecificationList != null) {
098: for (int i = 0; i < _sortSpecificationList.size(); i++)
099: ((SortSpecification) _sortSpecificationList.get(i))
100: .renameTableSpec(oldTableSpec, corrName);
101: }
102: }
103:
104: public void removeSortSpecification(String tableSpec) {
105: if (_sortSpecificationList != null) {
106: for (int i = 0; i < _sortSpecificationList.size(); i++) {
107: ColumnNode col = (ColumnNode) ((SortSpecification) _sortSpecificationList
108: .get(i)).getColumn();
109: if (col.getTableSpec().equals(tableSpec)) {
110: _sortSpecificationList.remove(i);
111: // item from arraylist is removed, reset index value
112: // as remove shifts any subsequent elements to the left
113: // (subtracts one from their indices).
114: i = i - 1;
115: }
116: }
117: }
118: }
119:
120: public void removeSortSpecification(String tableSpec,
121: String columnName) {
122: if (_sortSpecificationList != null) {
123: for (int i = 0; i < _sortSpecificationList.size(); i++) {
124: ColumnNode col = (ColumnNode) ((SortSpecification) _sortSpecificationList
125: .get(i)).getColumn();
126: if (col.matches(tableSpec, columnName)) {
127: _sortSpecificationList.remove(i);
128: // item from arraylist is removed, reset index value
129: // as remove shifts any subsequent elements to the left
130: // (subtracts one from their indices).
131: i = i - 1;
132: }
133: }
134: }
135: }
136:
137: public void addSortSpecification(String tableSpec,
138: String columnName, String direction, int order) {
139: SortSpecification sortSpec = new SortSpecification(
140: new ColumnNode(tableSpec, columnName), direction);
141: // Insert the new one in an appropriate place
142: if (_sortSpecificationList == null)
143: _sortSpecificationList = new ArrayList();
144: _sortSpecificationList.add(order - 1, sortSpec);
145: }
146:
147: public int getSortSpecificationCount() {
148: return (_sortSpecificationList != null) ? _sortSpecificationList
149: .size()
150: : 0;
151: }
152:
153: public SortSpecification getSortSpecification(int i) {
154: return (_sortSpecificationList != null) ? ((SortSpecification) _sortSpecificationList
155: .get(i))
156: : null;
157: }
158:
159: public void getReferencedColumns(Collection columns) {
160: if (_sortSpecificationList != null) {
161: for (int i = 0; i < _sortSpecificationList.size(); i++)
162: ((SortSpecification) _sortSpecificationList.get(i))
163: .getReferencedColumns(columns);
164: }
165: }
166:
167: }
|