KickJava   Java API By Example, From Geeks To Geeks.

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


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.SQLException JavaDoc;
38 import java.sql.Types JavaDoc;
39
40 /**
41  * The primitive double type.
42  */

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

55   public static PrimitiveDoubleType create()
56   {
57     return DOUBLE_TYPE;
58   }
59
60   /**
61    * Returns the type name.
62    */

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

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

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

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

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

107   public void generateSet(JavaWriter out, String JavaDoc pstmt,
108                           String JavaDoc index, String JavaDoc value)
109     throws IOException JavaDoc
110   {
111     out.println(pstmt + ".setDouble(" + index + "++, " + value + ");");
112   }
113
114   /**
115    * Generates a string to set the property.
116    */

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

126   public String JavaDoc toObject(String JavaDoc value)
127   {
128     return "new Double(" + value + ")";
129   }
130
131   /**
132    * Converts the value.
133    */

134   public String JavaDoc generateCastFromObject(String JavaDoc value)
135   {
136     return "((Number) " + value + ").doubleValue()";
137   }
138
139   /**
140    * Sets the value.
141    */

142   public void setParameter(PreparedStatement JavaDoc pstmt, int index, Object JavaDoc value)
143     throws SQLException JavaDoc
144   {
145     if (value instanceof Number JavaDoc)
146       pstmt.setString(index, value.toString());
147     else
148       throw new IllegalArgumentException JavaDoc("Invalid argument for setParameter.");
149   }
150 }
151
Popular Tags