KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > compile > OrderedColumnList


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.OrderedColumnList
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (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 JavaDoc;
29
30 /**
31  * List of OrderedColumns
32  *
33  * @author Jamie
34  */

35 public abstract class OrderedColumnList extends QueryTreeNodeVector
36 {
37     /**
38      * Get an array of ColumnOrderings to pass to the store
39      */

40     public IndexColumnOrder[] getColumnOrdering()
41     {
42         IndexColumnOrder[] ordering;
43         int numCols = size();
44         int actualCols;
45
46         ordering = new IndexColumnOrder[numCols];
47
48         /*
49             order by is fun, in that we need to ensure
50             there are no duplicates in the list. later copies
51             of an earlier entry are considered purely redundant,
52             they won't affect the result, so we can drop them.
53             We don't know how many columns are in the source,
54             so we use a hashtable for lookup of the positions
55         */

56         Hashtable JavaDoc hashColumns = new Hashtable JavaDoc();
57
58         actualCols = 0;
59
60         for (int i = 0; i < numCols; i++)
61         {
62             OrderedColumn oc = (OrderedColumn) elementAt(i);
63
64             // order by (lang) positions are 1-based,
65
// order items (store) are 0-based.
66
int position = oc.getColumnPosition() - 1;
67
68             Integer JavaDoc posInt = new Integer JavaDoc(position);
69
70             if (! hashColumns.containsKey(posInt))
71             {
72                 ordering[i] = new IndexColumnOrder(position,
73                                                 oc.isAscending());
74                 actualCols++;
75                 hashColumns.put(posInt, posInt);
76             }
77         }
78
79         /*
80             If there were duplicates removed, we need
81             to shrink the array down to what we used.
82         */

83         if (actualCols < numCols)
84         {
85             IndexColumnOrder[] newOrdering = new IndexColumnOrder[actualCols];
86             System.arraycopy(ordering, 0, newOrdering, 0, actualCols);
87             ordering = newOrdering;
88         }
89
90         return ordering;
91     }
92 }
93
Popular Tags