KickJava   Java API By Example, From Geeks To Geeks.

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


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 long type.
43  */

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

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

64   public String JavaDoc getName()
65   {
66     return "long";
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 LongType.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.BIGINT, 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 + ".getLong(" + 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.PrimitiveLongType.toForeignLong(" +
113               rs + ".getLong(" + 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 + ".setLong(" + 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.BIGINT);");
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 + ".setLong(" + index + "++, " + value + " + 1);");
148   }
149
150   /**
151    * Converts to an object.
152    */

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

161   public String JavaDoc generateCastFromObject(String JavaDoc value)
162   {
163     return "((Number) " + value + ").longValue()";
164   }
165
166   /**
167    * Gets the value.
168    */

169   public Object JavaDoc getObject(ResultSet JavaDoc rs, int index)
170     throws SQLException JavaDoc
171   {
172     long v = rs.getLong(index);
173
174     return rs.wasNull() ? null : new Long JavaDoc(v);
175   }
176
177   /**
178    * Converts a value to a int.
179    */

180   public static Long JavaDoc toForeignLong(long value, boolean wasNull)
181   {
182     // XXX: backwards compat
183
if (wasNull || value == 0)
184       return null;
185     else
186       return new Long JavaDoc(value);
187   }
188
189   /**
190    * Sets the value.
191    */

192   public void setParameter(PreparedStatement JavaDoc pstmt, int index, Object JavaDoc value)
193     throws SQLException JavaDoc
194   {
195     if (value == null) {
196       // jpa/141e
197
pstmt.setString(index, null);
198     }
199     else if (value instanceof Number JavaDoc)
200       pstmt.setString(index, value.toString());
201     else
202       throw new IllegalArgumentException JavaDoc("Invalid argument for setParameter.");
203   }
204 }
205
Popular Tags