KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > sql > compile > TypeCompiler


1 /*
2
3    Derby - Class org.apache.derby.iapi.sql.compile.TypeCompiler
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.iapi.sql.compile;
23
24 import org.apache.derby.iapi.services.loader.ClassFactory;
25
26 import org.apache.derby.iapi.services.compiler.MethodBuilder;
27 import org.apache.derby.iapi.services.compiler.LocalField;
28
29 import org.apache.derby.iapi.types.DataTypeDescriptor;
30 import org.apache.derby.iapi.types.DataValueDescriptor;
31 import org.apache.derby.iapi.types.TypeId;
32
33 import org.apache.derby.iapi.error.StandardException;
34
35 import org.apache.derby.iapi.reference.Limits;
36
37 /**
38  * This interface defines methods associated with a TypeId that are used
39  * by the compiler.
40  */

41
42 public interface TypeCompiler
43 {
44     /**
45      * Various fixed numbers related to datatypes.
46      */

47     // Need to leave space for '-'
48
public static final int LONGINT_MAXWIDTH_AS_CHAR = 20;
49
50     // Need to leave space for '-'
51
public static final int INT_MAXWIDTH_AS_CHAR = 11;
52
53     // Need to leave space for '-'
54
public static final int SMALLINT_MAXWIDTH_AS_CHAR = 6;
55
56     // Need to leave space for '-'
57
public static final int TINYINT_MAXWIDTH_AS_CHAR = 4;
58
59     // Need to leave space for '-' and decimal point
60
public static final int DOUBLE_MAXWIDTH_AS_CHAR = 54;
61
62     // Need to leave space for '-' and decimal point
63
public static final int REAL_MAXWIDTH_AS_CHAR = 25;
64
65     public static final int DEFAULT_DECIMAL_PRECISION = Limits.DB2_DEFAULT_DECIMAL_PRECISION;
66     public static final int DEFAULT_DECIMAL_SCALE = Limits.DB2_DEFAULT_DECIMAL_SCALE;
67     public static final int MAX_DECIMAL_PRECISION_SCALE = Limits.DB2_MAX_DECIMAL_PRECISION_SCALE;
68
69     public static final int BOOLEAN_MAXWIDTH_AS_CHAR = 5;
70
71     public static final String JavaDoc PLUS_OP = "+";
72     public static final String JavaDoc DIVIDE_OP = "/";
73     public static final String JavaDoc MINUS_OP = "-";
74     public static final String JavaDoc TIMES_OP = "*";
75     public static final String JavaDoc SUM_OP = "sum";
76     public static final String JavaDoc AVG_OP = "avg";
77     public static final String JavaDoc MOD_OP = "mod";
78
79     /**
80      * Type resolution methods on binary operators
81      *
82      * @param leftType The type of the left parameter
83      * @param rightType The type of the right parameter
84      * @param operator The name of the operator (e.g. "+").
85      *
86      * @return The type of the result
87      *
88      * @exception StandardException Thrown on error
89      */

90
91     DataTypeDescriptor resolveArithmeticOperation(
92                             DataTypeDescriptor leftType,
93                             DataTypeDescriptor rightType,
94                             String JavaDoc operator
95                                 )
96                             throws StandardException;
97
98     /**
99      * Determine if this type can be compared to some other type
100      *
101      * @param otherType The CompilationType of the other type to compare
102      * this type to
103      * @param forEquals True if this is an = or <> comparison, false otherwise.
104      * @param cf A ClassFactory
105      *
106      * @return true if the types can be compared, false if comparisons between
107      * the types are not allowed
108      */

109
110     boolean comparable(TypeId otherType,
111                                    boolean forEquals,
112                                    ClassFactory cf);
113
114
115
116     /**
117      * Determine if this type can be CONVERTed to some other type
118      *
119      * @param otherType The CompilationType of the other type to compare
120      * this type to
121      *
122      * @param forDataTypeFunction true if this is a type function that
123      * requires more liberal behavior (e.g DOUBLE can convert a char but
124      * you cannot cast a CHAR to double.
125      *
126      * @return true if the types can be converted, false if conversion
127      * is not allowed
128      */

129      boolean convertible(TypeId otherType,
130                                      boolean forDataTypeFunction);
131
132     /**
133      * Determine if this type is compatible to some other type
134      * (e.g. COALESCE(thistype, othertype)).
135      *
136      * @param otherType The CompilationType of the other type to compare
137      * this type to
138      *
139      * @return true if the types are compatible, false if not compatible
140      */

141     boolean compatible(TypeId otherType);
142
143     /**
144      * Determine if this type can have a value of another type stored into it.
145      * Note that direction is relevant here: the test is that the otherType
146      * is storable into this type.
147      *
148      * @param otherType The TypeId of the other type to compare this type to
149      * @param cf A ClassFactory
150      *
151      * @return true if the other type can be stored in a column of this type.
152      */

153
154     boolean storable(TypeId otherType, ClassFactory cf);
155
156     /**
157      * Get the name of the interface for this type. For example, the interface
158      * for a SQLInteger is NumberDataValue. The full path name of the type
159      * is returned.
160      *
161      * @return The name of the interface for this type.
162      */

163     String JavaDoc interfaceName();
164
165     /**
166      * Get the name of the corresponding Java type. For numerics and booleans
167      * we will get the corresponding Java primitive type.
168      e
169      * Each SQL type has a corresponding Java type. When a SQL value is
170      * passed to a Java method, it is translated to its corresponding Java
171      * type. For example, a SQL Integer will be mapped to a Java int, but
172      * a SQL date will be mapped to a java.sql.Date.
173      *
174      * @return The name of the corresponding Java primitive type.
175      */

176     String JavaDoc getCorrespondingPrimitiveTypeName();
177
178     /**
179      * Get the method name for getting out the corresponding primitive
180      * Java type from a DataValueDescriptor.
181      *
182      * @return String The method call name for getting the
183      * corresponding primitive Java type.
184      */

185     String JavaDoc getPrimitiveMethodName();
186
187
188     /**
189      * Get the name of the matching national char type.
190      *
191      * @return The name of the matching national char type.
192      */

193     String JavaDoc getMatchingNationalCharTypeName();
194
195     /**
196      * Generate the code necessary to produce a SQL null of the appropriate
197      * type. The stack must contain a DataValueFactory and a null or a value
198        of the correct type (interfaceName()).
199      *
200      * @param mb The method to put the expression in
201      *
202      */

203
204     void generateNull(MethodBuilder mb);
205
206
207     /**
208      * Generate the code necessary to produce a SQL value based on
209      * a value. The value's type is assumed to match
210      * the type of this TypeId. For example, a TypeId
211      * for the SQL int type should be given an value that evaluates
212      * to a Java int or Integer.
213      *
214      * If the type of the value is incorrect, the generated code will
215      * not work.
216
217        The stack must contain
218             data value factory
219             value
220      *
221      */

222     void generateDataValue(MethodBuilder eb, LocalField field);
223
224     /**
225      * Return the maximum width for this data type when cast to a char type.
226      *
227      * @param dts The associated DataTypeDescriptor for this TypeId.
228      *
229      * @return int The maximum width for this data type when cast to a char type.
230      */

231     int getCastToCharWidth(DataTypeDescriptor dts);
232
233 }
234
Popular Tags