01: /*
02:
03: Derby - Class org.apache.derby.impl.sql.compile.OrderedColumnList
04:
05: Licensed to the Apache Software Foundation (ASF) under one or more
06: contributor license agreements. See the NOTICE file distributed with
07: this work for additional information regarding copyright ownership.
08: The ASF licenses this file to you under the Apache License, Version 2.0
09: (the "License"); you may not use this file except in compliance with
10: the License. You may obtain a copy of the License at
11:
12: http://www.apache.org/licenses/LICENSE-2.0
13:
14: Unless required by applicable law or agreed to in writing, software
15: distributed under the License is distributed on an "AS IS" BASIS,
16: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17: See the License for the specific language governing permissions and
18: limitations under the License.
19:
20: */
21:
22: package org.apache.derby.impl.sql.compile;
23:
24: import org.apache.derby.iapi.store.access.ColumnOrdering;
25:
26: import org.apache.derby.impl.sql.execute.IndexColumnOrder;
27:
28: import java.util.Hashtable;
29:
30: /**
31: * List of OrderedColumns
32: *
33: * @author Jamie
34: */
35: public abstract class OrderedColumnList extends QueryTreeNodeVector {
36: /**
37: * Get an array of ColumnOrderings to pass to the store
38: */
39: public IndexColumnOrder[] getColumnOrdering() {
40: IndexColumnOrder[] ordering;
41: int numCols = size();
42: int actualCols;
43:
44: ordering = new IndexColumnOrder[numCols];
45:
46: /*
47: order by is fun, in that we need to ensure
48: there are no duplicates in the list. later copies
49: of an earlier entry are considered purely redundant,
50: they won't affect the result, so we can drop them.
51: We don't know how many columns are in the source,
52: so we use a hashtable for lookup of the positions
53: */
54: Hashtable hashColumns = new Hashtable();
55:
56: actualCols = 0;
57:
58: for (int i = 0; i < numCols; i++) {
59: OrderedColumn oc = (OrderedColumn) elementAt(i);
60:
61: // order by (lang) positions are 1-based,
62: // order items (store) are 0-based.
63: int position = oc.getColumnPosition() - 1;
64:
65: Integer posInt = new Integer(position);
66:
67: if (!hashColumns.containsKey(posInt)) {
68: ordering[i] = new IndexColumnOrder(position, oc
69: .isAscending());
70: actualCols++;
71: hashColumns.put(posInt, posInt);
72: }
73: }
74:
75: /*
76: If there were duplicates removed, we need
77: to shrink the array down to what we used.
78: */
79: if (actualCols < numCols) {
80: IndexColumnOrder[] newOrdering = new IndexColumnOrder[actualCols];
81: System.arraycopy(ordering, 0, newOrdering, 0, actualCols);
82: ordering = newOrdering;
83: }
84:
85: return ordering;
86: }
87: }
|