KickJava   Java API By Example, From Geeks To Geeks.

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


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.manager.AmberPersistenceUnit;
33 import com.caucho.java.JavaWriter;
34 import com.caucho.util.L10N;
35
36 import java.io.IOException JavaDoc;
37 import java.sql.PreparedStatement JavaDoc;
38 import java.sql.ResultSet JavaDoc;
39 import java.sql.SQLException JavaDoc;
40 import java.sql.Types JavaDoc;
41
42 /**
43  * Represents a java.util.Float type
44  */

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

57   public static FloatType create()
58   {
59     return FLOAT_TYPE;
60   }
61
62   /**
63    * Returns the type name.
64    */

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

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

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

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

103   public void generateSet(JavaWriter out, String JavaDoc pstmt,
104                           String JavaDoc index, String JavaDoc value)
105     throws IOException JavaDoc
106   {
107     out.println("if (" + value + " == null)");
108     out.println(" " + pstmt + ".setNull(" + index + "++, java.sql.Types.REAL);");
109     out.println("else");
110     // out.println(" " + pstmt + ".setFloat(" + index + "++, " +
111
// value + ".floatValue());");
112
// jpa/0u21
113
out.println(" " + pstmt + ".setString(" + index + "++, " +
114                 value + ".toString());");
115   }
116
117   /**
118    * Generates a string to set the property.
119    */

120   public void generateSetNull(JavaWriter out, String JavaDoc pstmt,
121                               String JavaDoc index)
122     throws IOException JavaDoc
123   {
124     out.println(pstmt + ".setNull(" + index + "++, java.sql.Types.REAL);");
125   }
126
127   /**
128    * Converts a value to a int.
129    */

130   public static Float JavaDoc toFloat(float value, boolean wasNull)
131   {
132     if (wasNull)
133       return null;
134     else
135       return new Float JavaDoc(value);
136   }
137
138   /**
139    * Gets the value.
140    */

141   public Object JavaDoc getObject(ResultSet JavaDoc rs, int index)
142     throws SQLException JavaDoc
143   {
144     float value = rs.getFloat(index);
145
146     return rs.wasNull() ? null : new Float JavaDoc(value);
147   }
148
149   /**
150    * Sets the value.
151    */

152   public void setParameter(PreparedStatement JavaDoc pstmt, int index, Object JavaDoc value)
153     throws SQLException JavaDoc
154   {
155     if (value == null)
156       pstmt.setNull(index, Types.FLOAT);
157     else
158       pstmt.setFloat(index, ((Float JavaDoc) value).floatValue());
159   }
160 }
161
Popular Tags