KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Free Software Foundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.amber.type;
30
31 import com.caucho.amber.manager.AmberPersistenceUnit;
32 import com.caucho.java.JavaWriter;
33 import com.caucho.util.L10N;
34
35 import java.io.IOException JavaDoc;
36 import java.sql.PreparedStatement JavaDoc;
37 import java.sql.ResultSet JavaDoc;
38 import java.sql.SQLException JavaDoc;
39 import java.sql.Types JavaDoc;
40
41 /**
42  * The primitive int type.
43  */

44 public class PrimitiveIntType extends PrimitiveType {
45   private static final L10N L = new L10N(PrimitiveIntType.class);
46
47   private static final PrimitiveIntType INT_TYPE = new PrimitiveIntType();
48
49   private PrimitiveIntType()
50   {
51   }
52
53   /**
54    * Returns the boolean type.
55    */

56   public static PrimitiveIntType create()
57   {
58     return INT_TYPE;
59   }
60
61   /**
62    * Returns the type name.
63    */

64   public String JavaDoc getName()
65   {
66     return "int";
67   }
68
69   /**
70    * Returns true for a numeric type.
71    */

72   public boolean isNumeric()
73   {
74     return true;
75   }
76
77   /**
78    * Returns the type as a foreign key.
79    */

80   public Type getForeignType()
81   {
82     return IntegerType.create();
83   }
84
85   /**
86    * Generates the type for the table.
87    */

88   public String JavaDoc generateCreateColumnSQL(AmberPersistenceUnit manager, int length, int precision, int scale)
89   {
90     return manager.getCreateColumnSQL(Types.INTEGER, length, precision, scale);
91   }
92
93   /**
94    * Generates a string to load the property.
95    */

96   public int generateLoad(JavaWriter out, String JavaDoc rs,
97                           String JavaDoc indexVar, int index)
98     throws IOException JavaDoc
99   {
100     out.print(rs + ".getInt(" + indexVar + " + " + index + ")");
101
102     return index + 1;
103   }
104
105   /**
106    * Generates a string to load the property.
107    */

108   public int generateLoadForeign(JavaWriter out, String JavaDoc rs,
109                                  String JavaDoc indexVar, int index)
110     throws IOException JavaDoc
111   {
112     out.print("com.caucho.amber.type.PrimitiveIntType.toForeignInt(" +
113               rs + ".getInt(" + indexVar + " + " + index + "), " +
114               rs + ".wasNull())");
115
116     return index + 1;
117   }
118
119   /**
120    * Generates a string to set the property.
121    */

122   public void generateSet(JavaWriter out, String JavaDoc pstmt,
123                           String JavaDoc index, String JavaDoc value)
124     throws IOException JavaDoc
125   {
126     out.println(pstmt + ".setInt(" + index + "++, " + value + ");");
127   }
128
129   /**
130    * Generates a string to set the property.
131    */

132   public void generateSetNull(JavaWriter out, String JavaDoc pstmt, String JavaDoc index)
133     throws IOException JavaDoc
134   {
135     out.println(pstmt + ".setNull(" + index + "++, java.sql.Types.INTEGER);");
136   }
137
138   /**
139    * Generates a string to set the property.
140    */

141   public void generateSetVersion(JavaWriter out,
142                                  String JavaDoc pstmt,
143                                  String JavaDoc index,
144                                  String JavaDoc value)
145     throws IOException JavaDoc
146   {
147     out.println(pstmt + ".setInt(" + index + "++, " + value + " + 1);");
148   }
149
150   /**
151    * Converts to an object.
152    */

153   public String JavaDoc toObject(String JavaDoc value)
154   {
155     return "new Integer(" + value + ")";
156   }
157
158   /**
159    * Converts the value.
160    */

161   public String JavaDoc generateCastFromObject(String JavaDoc value)
162   {
163     return "((Number) " + value + ").intValue()";
164   }
165
166   /**
167    * Converts a value to a int.
168    */

169   public static Integer JavaDoc toForeignInt(int value, boolean wasNull)
170   {
171     // XXX: backwards compat
172
if (wasNull || value == 0)
173       return null;
174     else
175       return new Integer JavaDoc(value);
176   }
177
178   /**
179    * Gets the value.
180    */

181   public Object JavaDoc getObject(ResultSet JavaDoc rs, int index)
182     throws SQLException JavaDoc
183   {
184     int v = rs.getInt(index);
185
186     return rs.wasNull() ? null : new Integer JavaDoc(v);
187   }
188
189   /**
190    * Converts to an object.
191    */

192   public Object JavaDoc toObject(long value)
193   {
194     return new Integer JavaDoc((int) value);
195   }
196
197   /**
198    * Sets the value.
199    */

200   public void setParameter(PreparedStatement JavaDoc pstmt, int index, Object JavaDoc value)
201     throws SQLException JavaDoc
202   {
203     if (value instanceof Number JavaDoc)
204       pstmt.setString(index, value.toString());
205     else
206       throw new IllegalArgumentException JavaDoc("Invalid argument for setParameter.");
207   }
208 }
209
Popular Tags