KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.MaxMinAggregateDefinition
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.sql.conn.LanguageConnectionContext;
25 import org.apache.derby.iapi.services.sanity.SanityManager;
26
27 import org.apache.derby.impl.sql.execute.MaxMinAggregator;
28
29 import org.apache.derby.catalog.TypeDescriptor;
30 import org.apache.derby.iapi.types.TypeId;
31 import org.apache.derby.iapi.types.DataTypeDescriptor;
32 import org.apache.derby.iapi.types.NumberDataValue;
33
34 import org.apache.derby.iapi.error.StandardException;
35
36 import org.apache.derby.iapi.services.context.ContextService;
37 import org.apache.derby.iapi.reference.ClassName;
38
39 /**
40  * Defintion for the MAX()/MIN() aggregates.
41  *
42  * @author jamie
43  */

44 public class MaxMinAggregateDefinition
45         implements AggregateDefinition
46 {
47     private boolean isMax;
48   
49     /**
50      * Niladic constructor. Does nothing. For ease
51      * Of use, only.
52      */

53     public MaxMinAggregateDefinition() { super(); }
54
55     /**
56      * Determines the result datatype. Accept NumberDataValues
57      * only.
58      * <P>
59      * <I>Note</I>: In the future you should be able to do
60      * a sum user data types. One option would be to run
61      * sum on anything that implements divide().
62      *
63      * @param inputType the input type, either a user type or a java.lang object
64      *
65      * @return the output Class (null if cannot operate on
66      * value expression of this type.
67      */

68     public final TypeDescriptor getAggregator(TypeDescriptor inputType,
69                 StringBuffer JavaDoc aggregatorClass)
70     {
71         LanguageConnectionContext lcc = (LanguageConnectionContext)
72             ContextService.getContext(LanguageConnectionContext.CONTEXT_ID);
73
74             /*
75             ** MIN and MAX may return null
76             */

77         DataTypeDescriptor dts = new DataTypeDescriptor((DataTypeDescriptor) inputType, true);
78         TypeId compType = dts.getTypeId();
79
80         /*
81         ** If the class implements NumberDataValue, then we
82         ** are in business. Return type is same as input
83         ** type.
84         */

85         if (compType.orderable(
86                         lcc.getLanguageConnectionFactory().getClassFactory()))
87         {
88             aggregatorClass.append(ClassName.MaxMinAggregator);
89             
90             return dts;
91         }
92         return null;
93     }
94
95     /**
96      * This is set by the parser.
97      */

98     public final void setMaxOrMin(boolean isMax)
99     {
100         this.isMax = isMax;
101     }
102
103     /**
104      * Return if the aggregator class is for min/max.
105      *
106      * @return boolean true/false
107      */

108     public final boolean isMax()
109     {
110         return(isMax);
111     }
112 }
113
Popular Tags