KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.OrderByNode
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.services.context.ContextManager;
25
26 import org.apache.derby.iapi.sql.compile.Optimizable;
27 import org.apache.derby.iapi.sql.compile.OptimizableList;
28 import org.apache.derby.iapi.sql.compile.OptimizablePredicate;
29 import org.apache.derby.iapi.sql.compile.OptimizablePredicateList;
30 import org.apache.derby.iapi.sql.compile.Optimizer;
31 import org.apache.derby.iapi.sql.compile.CostEstimate;
32 import org.apache.derby.iapi.sql.compile.Visitable;
33 import org.apache.derby.iapi.sql.compile.Visitor;
34
35 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
36
37 import org.apache.derby.iapi.sql.execute.ExecutionContext;
38
39 import org.apache.derby.iapi.sql.Activation;
40 import org.apache.derby.iapi.sql.ResultColumnDescriptor;
41 import org.apache.derby.iapi.sql.ResultSet;
42
43 import org.apache.derby.iapi.error.StandardException;
44
45 import org.apache.derby.impl.sql.compile.ActivationClassBuilder;
46
47 import org.apache.derby.iapi.services.compiler.MethodBuilder;
48
49 import org.apache.derby.iapi.services.sanity.SanityManager;
50
51 import org.apache.derby.iapi.util.JBitSet;
52
53 import java.util.Properties JavaDoc;
54
55 /**
56  * An OrderByNode represents a result set for a sort operation
57  * for an order by list. It is expected to only be generated at
58  * the end of optimization, once we have determined that a sort
59  * is required.
60  *
61  * @author jerry
62  */

63 public class OrderByNode extends SingleChildResultSetNode
64 {
65
66     OrderByList orderByList;
67
68     /**
69      * Initializer for a OrderByNode.
70      *
71      * @param childResult The child ResultSetNode
72      * @param orderByList The order by list.
73      * @param tableProperties Properties list associated with the table
74     *
75      * @exception StandardException Thrown on error
76      */

77     public void init(
78                         Object JavaDoc childResult,
79                         Object JavaDoc orderByList,
80                         Object JavaDoc tableProperties)
81         throws StandardException
82     {
83         ResultSetNode child = (ResultSetNode) childResult;
84
85         super.init(childResult, tableProperties);
86
87         this.orderByList = (OrderByList) orderByList;
88
89         ResultColumnList prRCList;
90
91         /*
92             We want our own resultColumns, which are virtual columns
93             pointing to the child result's columns.
94
95             We have to have the original object in the distinct node,
96             and give the underlying project the copy.
97          */

98
99         /* We get a shallow copy of the ResultColumnList and its
100          * ResultColumns. (Copy maintains ResultColumn.expression for now.)
101          */

102         prRCList = child.getResultColumns().copyListAndObjects();
103         resultColumns = child.getResultColumns();
104         child.setResultColumns(prRCList);
105
106         /* Replace ResultColumn.expression with new VirtualColumnNodes
107          * in the DistinctNode's RCL. (VirtualColumnNodes include
108          * pointers to source ResultSetNode, this, and source ResultColumn.)
109          */

110         resultColumns.genVirtualColumnNodes(this, prRCList);
111     }
112
113     /**
114      * Convert this object to a String. See comments in QueryTreeNode.java
115      * for how this should be done for tree printing.
116      *
117      * @return This object as a String
118      */

119
120     public String JavaDoc toString()
121     {
122         if (SanityManager.DEBUG)
123         {
124             return childResult.toString() + "\n" +
125                 "orderByList: " +
126                 (orderByList != null ? orderByList.toString() : "null") + "\n" +
127                 super.toString();
128         }
129         else
130         {
131             return "";
132         }
133     }
134
135     ResultColumnDescriptor[] makeResultDescriptors(ExecutionContext ec)
136     {
137         return childResult.makeResultDescriptors(ec);
138     }
139
140     /**
141      * generate the distinct result set operating over the source
142      * resultset.
143      *
144      * @exception StandardException Thrown on error
145      */

146     public void generate(ActivationClassBuilder acb,
147                                 MethodBuilder mb)
148                             throws StandardException
149     {
150         // Get the cost estimate for the child
151
if (costEstimate == null)
152         {
153             costEstimate = childResult.getFinalCostEstimate();
154         }
155
156         orderByList.generate(acb, mb, childResult);
157     }
158 }
159
Popular Tags