001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.compile.ColumnOrdering
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.sql.compile;
023:
024: import org.apache.derby.iapi.sql.compile.RowOrdering;
025: import org.apache.derby.iapi.sql.compile.Optimizable;
026:
027: import org.apache.derby.iapi.services.sanity.SanityManager;
028:
029: import java.util.Vector;
030:
031: class ColumnOrdering {
032:
033: /* See RowOrdering for possible values */
034: int myDirection;
035:
036: /* A vector of column numbers (Integers) */
037: Vector columns = new Vector();
038:
039: /*
040: ** A vector of table numbers (Integers), corresponding to the column
041: ** vector by position.
042: */
043: Vector tables = new Vector();
044:
045: /**
046: * @param direction See RowOrdering for possible values
047: */
048: ColumnOrdering(int direction) {
049: myDirection = direction;
050: }
051:
052: /**
053: * Does this ColumnOrdering contain the given column in the given table
054: * in the right direction?
055: *
056: * @param direction See RowOrdering for possible values
057: * @param tableNumber The number of the table in question
058: * @param columnNumber The column number in the table (one-based)
059: *
060: * @return true if the column is found here in the right direction
061: */
062: boolean ordered(int direction, int tableNumber, int columnNumber) {
063: /*
064: ** Check the direction only if the direction isn't DONTCARE
065: */
066: if (direction != RowOrdering.DONTCARE) {
067: if (direction != myDirection)
068: return false;
069: }
070:
071: /* The direction matches - see if the column is in this ordering */
072: return contains(tableNumber, columnNumber);
073: }
074:
075: /**
076: * Does this ColumnOrdering contain the given column?
077: *
078: * @param tableNumber The number of table in question
079: * @param columnNumber The column number in the table (one-based)
080: *
081: * @return true if the column is found here in the right direction
082: */
083: boolean contains(int tableNumber, int columnNumber) {
084: for (int i = 0; i < columns.size(); i++) {
085: Integer col = (Integer) columns.elementAt(i);
086: Integer tab = (Integer) tables.elementAt(i);
087:
088: if (tab.intValue() == tableNumber
089: && col.intValue() == columnNumber) {
090:
091: return true;
092: }
093: }
094:
095: return false;
096: }
097:
098: /**
099: * Get the direction of this ColumnOrdering
100: */
101: int direction() {
102: return myDirection;
103: }
104:
105: /**
106: * Add a column in a table to this ColumnOrdering
107: *
108: * @param tableNumber The number of table in question
109: * @param columnNumber The column number in the table (one-based)
110: */
111: void addColumn(int tableNumber, int columnNumber) {
112: tables.addElement(new Integer(tableNumber));
113: columns.addElement(new Integer(columnNumber));
114: }
115:
116: /**
117: * Remove all columns with the given table number
118: */
119: void removeColumns(int tableNumber) {
120: /*
121: ** Walk the list backwards, so we can remove elements
122: ** by position.
123: */
124: for (int i = tables.size() - 1; i >= 0; i--) {
125: Integer tab = (Integer) tables.elementAt(i);
126: if (tab.intValue() == tableNumber) {
127: tables.removeElementAt(i);
128: columns.removeElementAt(i);
129: }
130: }
131: }
132:
133: /**
134: * Tell whether this ColumnOrdering has no elements.
135: */
136: boolean empty() {
137: return (tables.size() == 0);
138: }
139:
140: /** Return a clone of this ColumnOrdering */
141: ColumnOrdering cloneMe() {
142: ColumnOrdering retval = new ColumnOrdering(myDirection);
143:
144: for (int i = 0; i < columns.size(); i++) {
145: /* Integers are immutable, so just copy the pointers */
146: retval.columns.addElement(columns.elementAt(i));
147: retval.tables.addElement(tables.elementAt(i));
148: }
149:
150: return retval;
151: }
152:
153: /** Is the given table number in this ColumnOrdering? */
154: boolean hasTable(int tableNumber) {
155: if (tables.size() == 0)
156: return false;
157:
158: for (int i = 0; i < tables.size(); i++) {
159: Integer tab = (Integer) tables.elementAt(i);
160:
161: if (tab.intValue() == tableNumber)
162: return true;
163: }
164:
165: return false;
166: }
167:
168: /** Is there any table other than the given one in this ColumnOrdering? */
169: boolean hasAnyOtherTable(int tableNumber) {
170: if (tables.size() == 0)
171: return false;
172:
173: for (int i = 0; i < tables.size(); i++) {
174: Integer tab = (Integer) tables.elementAt(i);
175:
176: if (tab.intValue() != tableNumber)
177: return true;
178: }
179:
180: return false;
181: }
182:
183: public String toString() {
184: String retval = "";
185:
186: if (SanityManager.DEBUG) {
187: retval += "Direction: " + myDirection;
188:
189: for (int i = 0; i < columns.size(); i++) {
190: retval += " Table " + tables.elementAt(i) + ", Column "
191: + columns.elementAt(i);
192: }
193: }
194:
195: return retval;
196: }
197: }
|