KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > type > Type


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.amber.type;
31
32 import com.caucho.amber.entity.EntityItem;
33 import com.caucho.amber.manager.AmberConnection;
34 import com.caucho.amber.manager.AmberPersistenceUnit;
35 import com.caucho.bytecode.JClass;
36 import com.caucho.config.ConfigException;
37 import com.caucho.java.JavaWriter;
38 import com.caucho.util.L10N;
39
40 import java.io.IOException JavaDoc;
41 import java.sql.PreparedStatement JavaDoc;
42 import java.sql.ResultSet JavaDoc;
43 import java.sql.SQLException JavaDoc;
44 import java.sql.Types JavaDoc;
45
46 /**
47  * The type of a property.
48  */

49 abstract public class Type {
50   private static final L10N L = new L10N(Type.class);
51
52   /**
53    * Returns the type name.
54    */

55   abstract public String JavaDoc getName();
56
57   /**
58    * Returns true for a boolean type.
59    */

60   public boolean isBoolean()
61   {
62     return false;
63   }
64
65   /**
66    * Returns true for a numeric type.
67    */

68   public boolean isNumeric()
69   {
70     return false;
71   }
72
73   /**
74    * Returns the java type.
75    */

76   public String JavaDoc getJavaTypeName()
77   {
78     return getName();
79   }
80
81   /**
82    * Returns the number of columns the type takes up.
83    */

84   public int getColumnCount()
85   {
86     return 1;
87   }
88
89   /**
90    * Initialize the type.
91    */

92   public void init()
93     throws ConfigException
94   {
95   }
96
97   /**
98    * Returns the type as a foreign key.
99    */

100   public Type getForeignType()
101   {
102     return this;
103   }
104
105   /**
106    * Returns the java class of the type as a foreign key.
107    */

108   public String JavaDoc getForeignTypeName()
109   {
110     return getForeignType().getJavaTypeName();
111   }
112
113   /**
114    * Returns true if the value is assignable to the Java type.
115    */

116   public boolean isAssignableTo(JClass javaType)
117   {
118     return true;
119   }
120
121   /**
122    * Generates the type for the table.
123    */

124   public String JavaDoc generateCreateColumnSQL(AmberPersistenceUnit manager, int length, int precision, int scale)
125   {
126     if (length == 0)
127       length = 255;
128
129     return manager.getCreateColumnSQL(Types.VARCHAR, length, precision, scale);
130   }
131
132   /**
133    * Generates a string to load the type as a property.
134    */

135   public int generateLoad(JavaWriter out, String JavaDoc rs,
136                           String JavaDoc indexVar, int index)
137     throws IOException JavaDoc
138   {
139     throw new UnsupportedOperationException JavaDoc(getClass().getName());
140   }
141
142   /**
143    * Generates a string to load the type as a property.
144    */

145   public int generateLoad(JavaWriter out, String JavaDoc rs,
146                           String JavaDoc indexVar, int index,
147                           JClass targetType)
148     throws IOException JavaDoc
149   {
150     String JavaDoc i = indexVar + " + " + index;
151
152     if ("java.lang.Byte".equals(targetType.getName()))
153       out.print("new Byte((byte) " + rs + ".getInt(" + i + "))");
154     else if ("java.lang.Short".equals(targetType.getName()))
155       out.print("new Short((short) " + rs + ".getInt(" + i + "))");
156     else if ("java.lang.Integer".equals(targetType.getName())) {
157       // ejb/0629
158
out.print("new Integer(" + rs + ".getInt(" + i + "))");
159     }
160     else if ("java.lang.Long".equals(targetType.getName()))
161       out.print("new Long(" + rs + ".getLong(" + i + "))");
162     else if ("java.lang.Float".equals(targetType.getName()))
163       out.print("new Float((float) " + rs + ".getDouble(" + i + "))");
164     else if ("java.lang.Double".equals(targetType.getName()))
165       out.print("new Double(" + rs + ".getDouble(" + i + "))");
166     else if ("java.lang.String".equals(targetType.getName()))
167       out.print(rs + ".getString(" + i + ")");
168     else {
169       out.print("(" + targetType.getName() + ") ");
170       out.print(rs + ".getObject(" + i + ")");
171     }
172
173     return index + 1;
174   }
175
176   /**
177    * Generates a string to load the type as a property.
178    */

179   public int generateLoadForeign(JavaWriter out, String JavaDoc rs,
180                                  String JavaDoc indexVar, int index)
181     throws IOException JavaDoc
182   {
183     return getForeignType().generateLoad(out, rs, indexVar, index);
184   }
185
186   /**
187    * Generates a string to set the type as a property.
188    */

189   public void generateSet(JavaWriter out, String JavaDoc pstmt,
190                           String JavaDoc index, String JavaDoc value)
191     throws IOException JavaDoc
192   {
193     throw new UnsupportedOperationException JavaDoc(getClass().getName());
194   }
195
196   /**
197    * Generates a string to set the type as a property.
198    */

199   public void generateSetVersion(JavaWriter out,
200                                  String JavaDoc pstmt,
201                                  String JavaDoc index,
202                                  String JavaDoc value)
203     throws IOException JavaDoc
204   {
205     throw new UnsupportedOperationException JavaDoc(getClass().getName());
206   }
207
208   /**
209    * Generates the increment version.
210    */

211   public String JavaDoc generateIncrementVersion(String JavaDoc value)
212     throws IOException JavaDoc
213   {
214     return value + " + 1";
215   }
216
217   /**
218    * Generates a string to set the property.
219    */

220   public void generateSetNull(JavaWriter out, String JavaDoc pstmt, String JavaDoc index)
221     throws IOException JavaDoc
222   {
223     generateSet(out, pstmt, index, null);
224   }
225
226   /**
227    * Sets the value.
228    */

229   public void setParameter(PreparedStatement JavaDoc pstmt, int index, Object JavaDoc value)
230     throws SQLException JavaDoc
231   {
232     pstmt.setObject(index, value);
233   }
234
235   /**
236    * Gets the value.
237    */

238   public Object JavaDoc getObject(ResultSet JavaDoc rs, int index)
239     throws SQLException JavaDoc
240   {
241     return rs.getObject(index);
242   }
243
244   /**
245    * Finds the object
246    */

247   public EntityItem findItem(AmberConnection aConn, ResultSet JavaDoc rs, int index)
248     throws SQLException JavaDoc
249   {
250     throw new UnsupportedOperationException JavaDoc(getClass().getName());
251   }
252
253   /**
254    * Gets the value.
255    */

256   public Object JavaDoc getObject(AmberConnection aConn, ResultSet JavaDoc rs, int index)
257     throws SQLException JavaDoc
258   {
259     return getObject(rs, index);
260   }
261
262   /**
263    * Converts to an object.
264    */

265   public String JavaDoc toObject(String JavaDoc value)
266   {
267     return value;
268   }
269
270   /**
271    * Converts from an object.
272    */

273   public String JavaDoc fromObject(String JavaDoc value)
274   {
275     return generateCastFromObject(value);
276   }
277
278   /**
279    * Converts to an object.
280    */

281   public Object JavaDoc toObject(long value)
282   {
283     return new Long JavaDoc(value);
284   }
285
286   /**
287    * Converts the value.
288    */

289   public String JavaDoc generateCastFromObject(String JavaDoc value)
290   {
291     return "((" + getName() + ") " + value + ")";
292   }
293
294   /**
295    * Returns a boolean equality.
296    */

297   public String JavaDoc generateEquals(String JavaDoc a, String JavaDoc b)
298   {
299     return a + ".equals(" + b + ")";
300   }
301
302   /**
303    * Returns a test for null.
304    */

305   public String JavaDoc generateIsNull(String JavaDoc value)
306   {
307     return "(" + value + " == " + generateNull() + ")";
308   }
309
310   /**
311    * Returns a test for null.
312    */

313   public String JavaDoc generateNull()
314   {
315     return "null";
316   }
317
318   /**
319    * Returns true for an auto-increment type.
320    */

321   public boolean isAutoIncrement()
322   {
323     return false;
324   }
325 }
326
Popular Tags