001: /*
002:
003: Derby - Class org.apache.derby.iapi.sql.compile.TypeCompiler
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.iapi.sql.compile;
023:
024: import org.apache.derby.iapi.services.loader.ClassFactory;
025:
026: import org.apache.derby.iapi.services.compiler.MethodBuilder;
027: import org.apache.derby.iapi.services.compiler.LocalField;
028:
029: import org.apache.derby.iapi.types.DataTypeDescriptor;
030: import org.apache.derby.iapi.types.DataValueDescriptor;
031: import org.apache.derby.iapi.types.TypeId;
032:
033: import org.apache.derby.iapi.error.StandardException;
034:
035: import org.apache.derby.iapi.reference.Limits;
036:
037: /**
038: * This interface defines methods associated with a TypeId that are used
039: * by the compiler.
040: */
041:
042: public interface TypeCompiler {
043: /**
044: * Various fixed numbers related to datatypes.
045: */
046: // Need to leave space for '-'
047: public static final int LONGINT_MAXWIDTH_AS_CHAR = 20;
048:
049: // Need to leave space for '-'
050: public static final int INT_MAXWIDTH_AS_CHAR = 11;
051:
052: // Need to leave space for '-'
053: public static final int SMALLINT_MAXWIDTH_AS_CHAR = 6;
054:
055: // Need to leave space for '-'
056: public static final int TINYINT_MAXWIDTH_AS_CHAR = 4;
057:
058: // Need to leave space for '-' and decimal point
059: public static final int DOUBLE_MAXWIDTH_AS_CHAR = 54;
060:
061: // Need to leave space for '-' and decimal point
062: public static final int REAL_MAXWIDTH_AS_CHAR = 25;
063:
064: public static final int DEFAULT_DECIMAL_PRECISION = Limits.DB2_DEFAULT_DECIMAL_PRECISION;
065: public static final int DEFAULT_DECIMAL_SCALE = Limits.DB2_DEFAULT_DECIMAL_SCALE;
066: public static final int MAX_DECIMAL_PRECISION_SCALE = Limits.DB2_MAX_DECIMAL_PRECISION_SCALE;
067:
068: public static final int BOOLEAN_MAXWIDTH_AS_CHAR = 5;
069:
070: public static final String PLUS_OP = "+";
071: public static final String DIVIDE_OP = "/";
072: public static final String MINUS_OP = "-";
073: public static final String TIMES_OP = "*";
074: public static final String SUM_OP = "sum";
075: public static final String AVG_OP = "avg";
076: public static final String MOD_OP = "mod";
077:
078: /**
079: * Type resolution methods on binary operators
080: *
081: * @param leftType The type of the left parameter
082: * @param rightType The type of the right parameter
083: * @param operator The name of the operator (e.g. "+").
084: *
085: * @return The type of the result
086: *
087: * @exception StandardException Thrown on error
088: */
089:
090: DataTypeDescriptor resolveArithmeticOperation(
091: DataTypeDescriptor leftType, DataTypeDescriptor rightType,
092: String operator) throws StandardException;
093:
094: /**
095: * Determine if this type can be compared to some other type
096: *
097: * @param otherType The CompilationType of the other type to compare
098: * this type to
099: * @param forEquals True if this is an = or <> comparison, false otherwise.
100: * @param cf A ClassFactory
101: *
102: * @return true if the types can be compared, false if comparisons between
103: * the types are not allowed
104: */
105:
106: boolean comparable(TypeId otherType, boolean forEquals,
107: ClassFactory cf);
108:
109: /**
110: * Determine if this type can be CONVERTed to some other type
111: *
112: * @param otherType The CompilationType of the other type to compare
113: * this type to
114: *
115: * @param forDataTypeFunction true if this is a type function that
116: * requires more liberal behavior (e.g DOUBLE can convert a char but
117: * you cannot cast a CHAR to double.
118: *
119: * @return true if the types can be converted, false if conversion
120: * is not allowed
121: */
122: boolean convertible(TypeId otherType, boolean forDataTypeFunction);
123:
124: /**
125: * Determine if this type is compatible to some other type
126: * (e.g. COALESCE(thistype, othertype)).
127: *
128: * @param otherType The CompilationType of the other type to compare
129: * this type to
130: *
131: * @return true if the types are compatible, false if not compatible
132: */
133: boolean compatible(TypeId otherType);
134:
135: /**
136: * Determine if this type can have a value of another type stored into it.
137: * Note that direction is relevant here: the test is that the otherType
138: * is storable into this type.
139: *
140: * @param otherType The TypeId of the other type to compare this type to
141: * @param cf A ClassFactory
142: *
143: * @return true if the other type can be stored in a column of this type.
144: */
145:
146: boolean storable(TypeId otherType, ClassFactory cf);
147:
148: /**
149: * Get the name of the interface for this type. For example, the interface
150: * for a SQLInteger is NumberDataValue. The full path name of the type
151: * is returned.
152: *
153: * @return The name of the interface for this type.
154: */
155: String interfaceName();
156:
157: /**
158: * Get the name of the corresponding Java type. For numerics and booleans
159: * we will get the corresponding Java primitive type.
160: e
161: * Each SQL type has a corresponding Java type. When a SQL value is
162: * passed to a Java method, it is translated to its corresponding Java
163: * type. For example, a SQL Integer will be mapped to a Java int, but
164: * a SQL date will be mapped to a java.sql.Date.
165: *
166: * @return The name of the corresponding Java primitive type.
167: */
168: String getCorrespondingPrimitiveTypeName();
169:
170: /**
171: * Get the method name for getting out the corresponding primitive
172: * Java type from a DataValueDescriptor.
173: *
174: * @return String The method call name for getting the
175: * corresponding primitive Java type.
176: */
177: String getPrimitiveMethodName();
178:
179: /**
180: * Get the name of the matching national char type.
181: *
182: * @return The name of the matching national char type.
183: */
184: String getMatchingNationalCharTypeName();
185:
186: /**
187: * Generate the code necessary to produce a SQL null of the appropriate
188: * type. The stack must contain a DataValueFactory and a null or a value
189: of the correct type (interfaceName()).
190: *
191: * @param mb The method to put the expression in
192: *
193: */
194:
195: void generateNull(MethodBuilder mb);
196:
197: /**
198: * Generate the code necessary to produce a SQL value based on
199: * a value. The value's type is assumed to match
200: * the type of this TypeId. For example, a TypeId
201: * for the SQL int type should be given an value that evaluates
202: * to a Java int or Integer.
203: *
204: * If the type of the value is incorrect, the generated code will
205: * not work.
206:
207: The stack must contain
208: data value factory
209: value
210: *
211: */
212: void generateDataValue(MethodBuilder eb, LocalField field);
213:
214: /**
215: * Return the maximum width for this data type when cast to a char type.
216: *
217: * @param dts The associated DataTypeDescriptor for this TypeId.
218: *
219: * @return int The maximum width for this data type when cast to a char type.
220: */
221: int getCastToCharWidth(DataTypeDescriptor dts);
222:
223: }
|