KickJava   Java API By Example, From Geeks To Geeks.

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


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.Double type
44  */

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

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

65   public String JavaDoc getName()
66   {
67     return "java.lang.Double";
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.DOUBLE, 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.DoubleType.toDouble(" +
94               rs + ".getDouble(" + 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.DOUBLE);");
109     out.println("else");
110     out.println(" " + pstmt + ".setDouble(" + index + "++, " +
111                 value + ".doubleValue());");
112   }
113
114   /**
115    * Generates a string to set the property.
116    */

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

127   public static Double JavaDoc toDouble(double value, boolean wasNull)
128   {
129     if (wasNull)
130       return null;
131     else
132       return new Double JavaDoc(value);
133   }
134
135   /**
136    * Sets the value.
137    */

138   public void setParameter(PreparedStatement JavaDoc pstmt, int index, Object JavaDoc value)
139     throws SQLException JavaDoc
140   {
141     if (value == null)
142       pstmt.setNull(index, Types.DOUBLE);
143     else
144       pstmt.setDouble(index, ((Double JavaDoc) value).doubleValue());
145   }
146
147   /**
148    * Gets the value.
149    */

150   public Object JavaDoc getObject(ResultSet JavaDoc rs, int index)
151     throws SQLException JavaDoc
152   {
153     double value = rs.getDouble(index);
154
155     return rs.wasNull() ? null : new Double JavaDoc(value);
156   }
157 }
158
Popular Tags