KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.BooleanTypeCompiler
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.loader.ClassFactory;
25
26 import org.apache.derby.iapi.error.StandardException;
27
28 import org.apache.derby.iapi.reference.SQLState;
29
30 import org.apache.derby.iapi.types.DataTypeDescriptor;
31 import org.apache.derby.iapi.types.BooleanDataValue;
32 import org.apache.derby.iapi.types.TypeId;
33
34 import org.apache.derby.iapi.sql.compile.TypeCompiler;
35
36 import org.apache.derby.catalog.types.BaseTypeIdImpl;
37 import org.apache.derby.iapi.reference.ClassName;
38
39 import java.sql.Types JavaDoc;
40
41 /**
42  * This class implements TypeCompiler for the SQL BOOLEAN datatype.
43  *
44  * @author Jerry Brenner
45  */

46
47 public class BooleanTypeCompiler extends BaseTypeCompiler
48 {
49     /**
50      * Tell whether this type (boolean) can be compared to the given type.
51      *
52      * @param otherType The TypeId of the other type.
53      */

54
55     public boolean comparable(TypeId otherType,
56                               boolean forEquals,
57                               ClassFactory cf)
58     {
59         TypeId thisTypeId = getTypeId();
60         TypeCompiler otherTypeCompiler = getTypeCompiler(otherType);
61
62         /* Only allow comparison of Boolean with Boolean or string types */
63         return otherType.getSQLTypeName().equals(thisTypeId.getSQLTypeName()) ||
64                 otherType.isStringTypeId() ||
65                 otherType.isNumericTypeId() ||
66                 (otherType.userType() &&
67                     otherTypeCompiler.comparable(thisTypeId, forEquals, cf));
68     }
69
70     /**
71      * Tell whether this type (boolean) can be converted to the given type.
72      *
73      * @see TypeCompiler#convertible
74      */

75     public boolean convertible(TypeId otherType, boolean forDataTypeFunction)
76     {
77         int otherJDBCTypeId = otherType.getJDBCTypeId();
78
79         if ((otherJDBCTypeId == Types.DATE) ||
80             (otherJDBCTypeId == Types.TIME) ||
81             (otherJDBCTypeId == Types.TIMESTAMP))
82         {
83             return false;
84         }
85
86         return true;
87     }
88
89         /**
90          * Tell whether this type (boolean) is compatible with the given type.
91          *
92          * @param otherType The TypeId of the other type.
93          */

94     public boolean compatible(TypeId otherType)
95     {
96         return convertible(otherType,false);
97     }
98
99     /** @see TypeCompiler#storable */
100     public boolean storable(TypeId otherType, ClassFactory cf)
101     {
102         /* Are the types the same or is other type a string or number type? */
103         if (otherType.isBooleanTypeId() ||
104                 otherType.isStringTypeId() ||
105                 otherType.isNumericTypeId())
106         {
107             return true;
108         }
109
110         /*
111         ** If the other type is user-defined, use the java types to determine
112         ** assignability.
113         */

114         return userTypeStorable(getTypeId(), otherType, cf);
115     }
116
117     /** @see TypeCompiler#interfaceName */
118     public String JavaDoc interfaceName()
119     {
120         return ClassName.BooleanDataValue;
121     }
122
123     /**
124      * @see TypeCompiler#getCorrespondingPrimitiveTypeName
125      */

126
127     public String JavaDoc getCorrespondingPrimitiveTypeName()
128     {
129         /* Only numerics and booleans get mapped to Java primitives */
130         return "boolean";
131     }
132
133     /**
134      * Get the method name for getting out the corresponding primitive
135      * Java type.
136      *
137      * @return String The method call name for getting the
138      * corresponding primitive Java type.
139      */

140     public String JavaDoc getPrimitiveMethodName()
141     {
142         return "getBoolean";
143     }
144
145     /**
146      * @see TypeCompiler#getCastToCharWidth
147      */

148     public int getCastToCharWidth(DataTypeDescriptor dts)
149     {
150         return TypeCompiler.BOOLEAN_MAXWIDTH_AS_CHAR;
151     }
152
153     protected String JavaDoc nullMethodName()
154     {
155         return "getNullBoolean";
156     }
157 }
158
Popular Tags