KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Represents a java.util.Long type
43  */

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

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

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

72   public boolean isNumeric()
73   {
74     return true;
75   }
76
77   /**
78    * Generates the type for the table.
79    */

80   public String JavaDoc generateCreateColumnSQL(AmberPersistenceUnit manager, int length, int precision, int scale)
81   {
82     return manager.getCreateColumnSQL(Types.BIGINT, length, precision, scale);
83   }
84
85   /**
86    * Generates a string to load the property.
87    */

88   public int generateLoad(JavaWriter out, String JavaDoc rs,
89                           String JavaDoc indexVar, int index)
90     throws IOException JavaDoc
91   {
92     out.print("com.caucho.amber.type.LongType.toLong(" +
93               rs + ".getLong(" + indexVar + " + " + index + "), " +
94               rs + ".wasNull())");
95
96     return index + 1;
97   }
98
99   /**
100    * Generates a string to set the property.
101    */

102   public void generateSet(JavaWriter out, String JavaDoc pstmt,
103                           String JavaDoc index, String JavaDoc value)
104     throws IOException JavaDoc
105   {
106     out.println("if (" + value + " == null)");
107     out.println(" " + pstmt + ".setNull(" + index + "++, java.sql.Types.BIGINT);");
108     out.println("else");
109     out.println(" " + pstmt + ".setLong(" + index + "++, " +
110                 value + ".longValue());");
111   }
112
113   /**
114    * Generates a string to set the property.
115    */

116   public void generateSetNull(JavaWriter out, String JavaDoc pstmt,
117                               String JavaDoc index)
118     throws IOException JavaDoc
119   {
120     out.println(pstmt + ".setNull(" + index + "++, java.sql.Types.BIGINT);");
121   }
122
123   /**
124    * Generates a string to set the property.
125    */

126   public void generateSetVersion(JavaWriter out,
127                                  String JavaDoc pstmt,
128                                  String JavaDoc index,
129                                  String JavaDoc value)
130     throws IOException JavaDoc
131   {
132     out.println("if (" + value + " == null)");
133     out.println(" " + pstmt + ".setLong(" + index + "++, 1);");
134     out.println("else");
135     out.println(" " + pstmt + ".setLong(" + index + "++, " +
136                 value + ".longValue() + 1);");
137   }
138
139   /**
140    * Generates the increment version.
141    */

142   public String JavaDoc generateIncrementVersion(String JavaDoc value)
143     throws IOException JavaDoc
144   {
145     return value + ".longValue() + 1";
146   }
147
148   /**
149    * Converts a value to a int.
150    */

151   public static Long JavaDoc toLong(long value, boolean wasNull)
152   {
153     if (wasNull)
154       return null;
155     else
156       return new Long JavaDoc(value);
157   }
158
159   /**
160    * Sets the value.
161    */

162   public void setParameter(PreparedStatement JavaDoc pstmt, int index, Object JavaDoc value)
163     throws SQLException JavaDoc
164   {
165     if (value == null)
166       pstmt.setNull(index, 0);
167     else
168       pstmt.setLong(index, ((Long JavaDoc) value).longValue());
169   }
170
171   /**
172    * Gets the value.
173    */

174   public Object JavaDoc getObject(ResultSet JavaDoc rs, int index)
175     throws SQLException JavaDoc
176   {
177     long value = rs.getLong(index);
178
179     return rs.wasNull() ? null : new Long JavaDoc(value);
180   }
181 }
182
Popular Tags