Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/derby/iapi/sql/compile/TypeCompiler.java


1   /*
2   
3      Derby - Class org.apache.derby.iapi.sql.compile.TypeCompiler
4   
5      Copyright 1999, 2004 The Apache Software Foundation or its licensors, as applicable.
6   
7      Licensed under the Apache License, Version 2.0 (the "License");
8      you may not use this file except in compliance with the License.
9      You may obtain a copy of the License at
10  
11        http://www.apache.org/licenses/LICENSE-2.0
12  
13     Unless required by applicable law or agreed to in writing, software
14     distributed under the License is distributed on an "AS IS" BASIS,
15     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16     See the License for the specific language governing permissions and
17     limitations under the License.
18  
19   */
20  
21  package org.apache.derby.iapi.sql.compile;
22  
23  import org.apache.derby.iapi.services.loader.ClassFactory;
24  
25  import org.apache.derby.iapi.services.compiler.MethodBuilder;
26  import org.apache.derby.iapi.services.compiler.LocalField;
27  
28  import org.apache.derby.iapi.types.DataTypeDescriptor;
29  import org.apache.derby.iapi.types.DataValueDescriptor;
30  import org.apache.derby.iapi.types.TypeId;
31  
32  import org.apache.derby.iapi.error.StandardException;
33  
34  import org.apache.derby.iapi.reference.Limits;
35  
36  /**
37   * This interface defines methods associated with a TypeId that are used
38   * by the compiler.
39   */
40  
41  public interface TypeCompiler
42  {
43    /**
44     * Various fixed numbers related to datatypes.
45     */
46    // Need to leave space for '-'
47    public static final int LONGINT_MAXWIDTH_AS_CHAR  = 20;
48  
49    // Need to leave space for '-'
50    public static final int INT_MAXWIDTH_AS_CHAR  = 11;
51  
52    // Need to leave space for '-'
53    public static final int SMALLINT_MAXWIDTH_AS_CHAR  = 6;
54  
55    // Need to leave space for '-'
56    public static final int TINYINT_MAXWIDTH_AS_CHAR  = 4;
57  
58    // Need to leave space for '-' and decimal point
59    public static final int DOUBLE_MAXWIDTH_AS_CHAR    = 54;
60  
61    // Need to leave space for '-' and decimal point
62    public static final int REAL_MAXWIDTH_AS_CHAR  = 25;
63  
64    public static final int DEFAULT_DECIMAL_PRECISION  = Limits.DB2_DEFAULT_DECIMAL_PRECISION;
65    public static final int DEFAULT_DECIMAL_SCALE     = Limits.DB2_DEFAULT_DECIMAL_SCALE;
66    public static final int MAX_DECIMAL_PRECISION_SCALE = Limits.DB2_MAX_DECIMAL_PRECISION_SCALE;
67  
68    public static final int BOOLEAN_MAXWIDTH_AS_CHAR  = 5;
69  
70    public static final String PLUS_OP     = "+";
71    public static final String DIVIDE_OP  = "/";
72    public static final String MINUS_OP   = "-";
73    public static final String TIMES_OP   = "*";
74    public static final String SUM_OP     = "sum";
75    public static final String AVG_OP     = "avg";
76    public static final String MOD_OP    = "mod";
77  
78    /**
79     * Type resolution methods on binary operators
80     *
81     * @param descrFactory  A factory to generate the return value
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 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 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  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 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 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 }