KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.BitTypeCompiler
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.services.sanity.SanityManager;
27
28 import org.apache.derby.iapi.services.io.StoredFormatIds;
29
30 import org.apache.derby.iapi.error.StandardException;
31
32 import org.apache.derby.iapi.types.BitDataValue;
33 import org.apache.derby.iapi.types.DataValueFactory;
34 import org.apache.derby.iapi.types.TypeId;
35
36 import org.apache.derby.iapi.types.DataTypeDescriptor;
37
38 import org.apache.derby.iapi.sql.compile.TypeCompiler;
39
40 import org.apache.derby.iapi.reference.ClassName;
41
42 import java.sql.Types JavaDoc;
43 import org.apache.derby.iapi.reference.JDBC20Translation;
44
45 /**
46  * This class implements TypeCompiler for the SQL BIT datatype.
47  *
48  * @author Jamie
49  */

50
51 public class BitTypeCompiler extends BaseTypeCompiler
52 {
53         /**
54          * Tell whether this type (bit) can be compared to the given type. //
55          *
56          * Bit Types can only be compared to Bit Types.
57          * Long Bit Types can not be compared
58          * @param otherType The TypeId of the other type.
59          */

60
61         public boolean comparable(TypeId otherType,
62                                   boolean forEquals,
63                                   ClassFactory cf)
64         {
65
66             if (getTypeId().isLongConcatableTypeId() ||
67                 otherType.isLongConcatableTypeId())
68                 return false;
69
70             TypeCompiler otherTC = getTypeCompiler(otherType);
71             return (otherType.isBitTypeId() ||
72                     (otherType.userType() &&
73                      otherTC.comparable(getTypeId(), forEquals, cf)));
74         }
75     
76         /**
77          * Tell whether this type (bit) can be converted to the given type.
78          *
79          * @see TypeCompiler#convertible
80          */

81         public boolean convertible(TypeId otherType,
82                                    boolean forDataTypeFunction)
83         {
84
85
86             return (otherType.isBitTypeId() ||
87                     otherType.isBlobTypeId() ||
88                     otherType.isBooleanTypeId() ||
89                     otherType.userType());
90         }
91
92     
93         /**
94          * Tell whether this type (bit) is compatible with the given type.
95          *
96          * @param otherType The TypeId of the other type.
97          */

98         public boolean compatible(TypeId otherType)
99         {
100         if (otherType.isBlobTypeId())
101           return false;
102         return (otherType.isBitTypeId());
103         }
104
105         /**
106          * Tell whether this type (bit) can be stored into from the given type.
107          *
108          * @param otherType The TypeId of the other type.
109          * @param cf A ClassFactory
110          */

111
112         public boolean storable(TypeId otherType, ClassFactory cf)
113         {
114         if (otherType.isBlobTypeId())
115           return false;
116                 if (otherType.isBitTypeId())
117                 {
118                         return true;
119                 }
120
121                 /*
122                 ** If the other type is user-defined, use the java types to determine
123                 ** assignability.
124                 */

125                 return userTypeStorable(this.getTypeId(), otherType, cf);
126         }
127
128         /** @see TypeCompiler#interfaceName */
129         public String JavaDoc interfaceName()
130         {
131                 // may need to return different for Blob
132
// however, since it the nullMethodName()
133
// does not operate on a BitTypeCompiler object?
134
// it should?
135
return ClassName.BitDataValue;
136         }
137
138         /**
139          * @see TypeCompiler#getCorrespondingPrimitiveTypeName
140          */

141
142         public String JavaDoc getCorrespondingPrimitiveTypeName()
143         {
144             return "byte[]";
145         }
146
147         /**
148          * @see TypeCompiler#getCastToCharWidth
149          */

150         public int getCastToCharWidth(DataTypeDescriptor dts)
151         {
152                 return dts.getMaximumWidth();
153         }
154
155         protected String JavaDoc nullMethodName()
156         {
157                 int formatId = getStoredFormatIdFromTypeId();
158                 switch (formatId)
159                 {
160                         case StoredFormatIds.BIT_TYPE_ID:
161                                 return "getNullBit";
162
163                         case StoredFormatIds.LONGVARBIT_TYPE_ID:
164                                 return "getNullLongVarbit";
165
166                         case StoredFormatIds.VARBIT_TYPE_ID:
167                                 return "getNullVarbit";
168
169                         default:
170                                 if (SanityManager.DEBUG)
171                                 {
172                                         SanityManager.THROWASSERT(
173                                                 "unexpected formatId in nullMethodName() - " + formatId);
174                                 }
175                                 return null;
176                 }
177         }
178
179         protected String JavaDoc dataValueMethodName()
180         {
181                 int formatId = getStoredFormatIdFromTypeId();
182                 switch (formatId)
183                 {
184                         case StoredFormatIds.BIT_TYPE_ID:
185                                 return "getBitDataValue";
186
187                         case StoredFormatIds.LONGVARBIT_TYPE_ID:
188                                 return "getLongVarbitDataValue";
189
190                         case StoredFormatIds.VARBIT_TYPE_ID:
191                                 return "getVarbitDataValue";
192
193                         default:
194                                 if (SanityManager.DEBUG)
195                                 {
196                                         SanityManager.THROWASSERT(
197                                                 "unexpected formatId in dataValueMethodName() - " + formatId);
198                                 }
199                                 return null;
200                 }
201         }
202 }
203
Popular Tags