KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.TypeCompilerFactoryImpl
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.sql.compile.TypeCompilerFactory;
25 import org.apache.derby.iapi.sql.compile.TypeCompiler;
26 import org.apache.derby.iapi.types.TypeId;
27
28 import org.apache.derby.iapi.services.sanity.SanityManager;
29
30 import org.apache.derby.iapi.reference.JDBC20Translation;
31 import org.apache.derby.iapi.reference.JDBC30Translation;
32
33 import org.apache.derby.iapi.services.io.StoredFormatIds;
34 import java.util.Properties JavaDoc;
35
36 import java.sql.Types JavaDoc;
37
38 public class TypeCompilerFactoryImpl implements TypeCompilerFactory
39 {
40         private static final String JavaDoc PACKAGE_NAME =
41                         "org.apache.derby.impl.sql.compile.";
42
43         // These are all the TypeCompilers that are stateless, so we can
44
// use a single instance of each. Initialize all to null, and fault
45
// them in.
46
static TypeCompiler bitTypeCompiler;
47         static TypeCompiler booleanTypeCompiler;
48         static TypeCompiler charTypeCompiler;
49         static TypeCompiler decimalTypeCompiler ;
50         static TypeCompiler doubleTypeCompiler ;
51         static TypeCompiler intTypeCompiler ;
52         static TypeCompiler longintTypeCompiler ;
53         static TypeCompiler longvarbitTypeCompiler ;
54         static TypeCompiler longvarcharTypeCompiler ;
55         static TypeCompiler nationalCharTypeCompiler ;
56         static TypeCompiler nationalLongvarcharTypeCompiler ;
57         static TypeCompiler nationalVarcharTypeCompiler ;
58         static TypeCompiler realTypeCompiler ;
59         static TypeCompiler smallintTypeCompiler ;
60         static TypeCompiler tinyintTypeCompiler ;
61         static TypeCompiler dateTypeCompiler ;
62         static TypeCompiler timeTypeCompiler ;
63         static TypeCompiler timestampTypeCompiler ;
64         static TypeCompiler varbitTypeCompiler ;
65         static TypeCompiler varcharTypeCompiler ;
66         static TypeCompiler refTypeCompiler ;
67         static TypeCompiler blobTypeCompiler ;
68         static TypeCompiler clobTypeCompiler ;
69         static TypeCompiler nclobTypeCompiler ;
70         static TypeCompiler xmlTypeCompiler ;
71
72         /**
73          * Get a TypeCompiler corresponding to the given TypeId
74          *
75          * @param typeId The TypeId to get a TypeCompiler for
76          *
77          * @return The corresponding TypeCompiler
78          */

79
80         public TypeCompiler getTypeCompiler(TypeId typeId)
81         {
82                 return staticGetTypeCompiler(typeId);
83         }
84
85         static TypeCompiler staticGetTypeCompiler(TypeId typeId)
86         {
87                 String JavaDoc sqlTypeName;
88
89                 switch (typeId.getJDBCTypeId())
90                 {
91                   case Types.BINARY:
92                         return bitTypeCompiler =
93                                         getAnInstance(PACKAGE_NAME + "BitTypeCompiler",
94                                                                         bitTypeCompiler,
95                                                                         typeId);
96
97                   case Types.BIT:
98                   case JDBC30Translation.SQL_TYPES_BOOLEAN:
99                         return booleanTypeCompiler =
100                                         getAnInstance(PACKAGE_NAME + "BooleanTypeCompiler",
101                                                                 booleanTypeCompiler,
102                                                                 typeId);
103
104                   case Types.CHAR:
105                           sqlTypeName = typeId.getSQLTypeName();
106                           if (sqlTypeName.equals(TypeId.CHAR_NAME))
107                           {
108                                 return charTypeCompiler =
109                                         getAnInstance(PACKAGE_NAME + "CharTypeCompiler",
110                                                                 charTypeCompiler,
111                                                                 typeId);
112                           }
113                           else
114                           {
115                                 return nationalCharTypeCompiler =
116                                         getAnInstance(PACKAGE_NAME + "CharTypeCompiler",
117                                                                 nationalCharTypeCompiler,
118                                                                 typeId);
119                           }
120
121                   case Types.NUMERIC:
122                   case Types.DECIMAL:
123                         return decimalTypeCompiler =
124                                 getAnInstance(PACKAGE_NAME + "NumericTypeCompiler",
125                                                                 decimalTypeCompiler,
126                                                                 typeId);
127
128                   case Types.DOUBLE:
129                         return doubleTypeCompiler =
130                                 getAnInstance(PACKAGE_NAME + "NumericTypeCompiler",
131                                                                 doubleTypeCompiler,
132                                                                 typeId);
133
134                   case Types.INTEGER:
135                         return intTypeCompiler =
136                                 getAnInstance(PACKAGE_NAME + "NumericTypeCompiler",
137                                                                 intTypeCompiler,
138                                                                 typeId);
139
140                   case Types.BIGINT:
141                         return longintTypeCompiler =
142                                 getAnInstance(PACKAGE_NAME + "NumericTypeCompiler",
143                                                                 longintTypeCompiler,
144                                                                 typeId);
145
146                   case JDBC20Translation.SQL_TYPES_BLOB:
147                         return blobTypeCompiler =
148                                 getAnInstance(PACKAGE_NAME + "LOBTypeCompiler",
149                                                           blobTypeCompiler,
150                                                           typeId);
151
152                   case Types.LONGVARBINARY:
153                         return longvarbitTypeCompiler =
154                                 getAnInstance(PACKAGE_NAME + "BitTypeCompiler",
155                                                           longvarbitTypeCompiler,
156                                                           typeId);
157
158                   case JDBC20Translation.SQL_TYPES_CLOB:
159                       sqlTypeName = typeId.getSQLTypeName();
160                       if (sqlTypeName.equals(TypeId.CLOB_NAME)) {
161                           return clobTypeCompiler =
162                               getAnInstance(PACKAGE_NAME + "CLOBTypeCompiler",
163                                             clobTypeCompiler,
164                                             typeId);
165                       } else {
166                           return nclobTypeCompiler =
167                               getAnInstance(PACKAGE_NAME + "CLOBTypeCompiler",
168                                             nclobTypeCompiler,
169                                             typeId);
170                       }
171                   case Types.LONGVARCHAR:
172                           sqlTypeName = typeId.getSQLTypeName();
173                           if (sqlTypeName.equals(TypeId.LONGVARCHAR_NAME))
174                           {
175                                 return longvarcharTypeCompiler =
176                                         getAnInstance(PACKAGE_NAME + "CharTypeCompiler",
177                                                                 longvarcharTypeCompiler,
178                                                                 typeId);
179                           }
180                           else
181                           {
182                                 return nationalLongvarcharTypeCompiler =
183                                         getAnInstance(PACKAGE_NAME + "CharTypeCompiler",
184                                                                 nationalLongvarcharTypeCompiler,
185                                                                 typeId);
186                           }
187
188                   case Types.REAL:
189                         return realTypeCompiler =
190                                 getAnInstance(PACKAGE_NAME + "NumericTypeCompiler",
191                                                                 realTypeCompiler,
192                                                                 typeId);
193
194                   case Types.SMALLINT:
195                         return smallintTypeCompiler =
196                                 getAnInstance(PACKAGE_NAME + "NumericTypeCompiler",
197                                                                 smallintTypeCompiler,
198                                                                 typeId);
199
200                   case Types.TINYINT:
201                     return tinyintTypeCompiler =
202                                 getAnInstance(PACKAGE_NAME + "NumericTypeCompiler",
203                                                                 tinyintTypeCompiler,
204                                                                 typeId);
205
206                   case Types.DATE:
207                         return dateTypeCompiler =
208                                         getAnInstance(PACKAGE_NAME + "DateTypeCompiler",
209                                                                         dateTypeCompiler,
210                                                                         typeId);
211
212                   case Types.TIME:
213                         return timeTypeCompiler =
214                                         getAnInstance(PACKAGE_NAME + "TimeTypeCompiler",
215                                                                         timeTypeCompiler,
216                                                                         typeId);
217                   case Types.TIMESTAMP:
218                         return timestampTypeCompiler =
219                                         getAnInstance(PACKAGE_NAME + "TimestampTypeCompiler",
220                                                                         timestampTypeCompiler,
221                                                                         typeId);
222                   case Types.VARBINARY:
223                         return varbitTypeCompiler =
224                                 getAnInstance(PACKAGE_NAME + "BitTypeCompiler",
225                                                                 varbitTypeCompiler,
226                                                                 typeId);
227
228                   case Types.VARCHAR:
229                           sqlTypeName = typeId.getSQLTypeName();
230                           if (sqlTypeName.equals(TypeId.VARCHAR_NAME))
231                           {
232                                 return varcharTypeCompiler =
233                                         getAnInstance(PACKAGE_NAME + "CharTypeCompiler",
234                                                                 varcharTypeCompiler,
235                                                                 typeId);
236                           }
237                           else
238                           {
239                                 return nationalVarcharTypeCompiler =
240                                         getAnInstance(PACKAGE_NAME + "CharTypeCompiler",
241                                                                 nationalVarcharTypeCompiler,
242                                                                 typeId);
243                           }
244
245                   case org.apache.derby.iapi.reference.JDBC20Translation.SQL_TYPES_JAVA_OBJECT:
246                   case Types.OTHER:
247                         if (typeId.isRefTypeId())
248                         {
249                                 return refTypeCompiler = getAnInstance(
250                                                                                         PACKAGE_NAME + "RefTypeCompiler",
251                                                                                         refTypeCompiler,
252                                                                                         typeId);
253                         }
254                         else
255                         {
256                                 // Cannot re-use instances of user-defined type compilers,
257
// because they contain the class name
258
BaseTypeCompiler btc = new UserDefinedTypeCompiler();
259                                 btc.setTypeId(typeId);
260                                 return btc;
261                         }
262
263                   case StoredFormatIds.XML_TYPE_ID:
264                         return xmlTypeCompiler =
265                                 getAnInstance(PACKAGE_NAME + "XMLTypeCompiler",
266                                                                 xmlTypeCompiler,
267                                                                 typeId);
268
269                 }
270
271                 if (SanityManager.DEBUG)
272                 {
273                         SanityManager.THROWASSERT("Unexpected JDBC type id " +
274                                                                                 typeId.getJDBCTypeId() +
275                                                                                 " for typeId of class " +
276                                                                                 typeId.getClass().getName());
277                 }
278
279                 return null;
280         }
281
282         /**
283          * Check whether the given TypeCompiler has been allocated yet.
284          * If so, just return it, otherwise allocate a new instance
285          * given its class.
286          */

287         private static TypeCompiler getAnInstance(String JavaDoc className,
288                                                                 TypeCompiler anInstance,
289                                                                 TypeId typeId)
290         {
291                 if (anInstance == null)
292                 {
293                         Exception JavaDoc exc = null;
294                         Class JavaDoc typeCompilerClass = null;
295
296                         try
297                         {
298                                 typeCompilerClass = Class.forName(className);
299                                 anInstance = (TypeCompiler) typeCompilerClass.newInstance();
300                                 ((BaseTypeCompiler) anInstance).setTypeId(typeId);
301                         }
302                         catch (ClassNotFoundException JavaDoc cnfe)
303                         {
304                                 exc = cnfe;
305                         }
306                         catch (IllegalAccessException JavaDoc iae)
307                         {
308                                 exc = iae;
309                         }
310                         catch (InstantiationException JavaDoc ie)
311                         {
312                                 exc = ie;
313                         }
314
315                         if (SanityManager.DEBUG)
316                         {
317                                 if (exc != null)
318                                 {
319                                         SanityManager.THROWASSERT(
320                                                 "Exception " +
321                                                 exc +
322                                                 " while trying to get new instance of a " +
323                                                 typeCompilerClass.getName());
324                                 }
325                         }
326                 }
327
328                 return anInstance;
329         }
330 }
331
Popular Tags