KickJava   Java API By Example, From Geeks To Geeks.

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


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.math.BigDecimal 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  * The type of a property.
44  */

45 public class BigDecimalType extends Type {
46   private static final L10N L = new L10N(BigDecimalType.class);
47
48   private int _scale = -1;
49
50   private BigDecimalType()
51   {
52   }
53
54   /**
55    * Returns the singleton BigDecimal type.
56    */

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

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

73   public boolean isNumeric()
74   {
75     return true;
76   }
77
78   /**
79    * Generates a string to load the property.
80    */

81   public int generateLoad(JavaWriter out, String JavaDoc rs,
82                           String JavaDoc indexVar, int index)
83     throws IOException JavaDoc
84   {
85     out.print(rs + ".getBigDecimal(" + indexVar + " + " + index + ")");
86
87     return index + 1;
88   }
89
90   /**
91    * Generates a string to set the property.
92    */

93   public void generateSet(JavaWriter out, String JavaDoc pstmt,
94                           String JavaDoc index, String JavaDoc value)
95     throws IOException JavaDoc
96   {
97     out.println("if (" + value + " == null)");
98     out.println(" " + pstmt + ".setNull(" + index + "++, java.sql.Types.DECIMAL);");
99     out.println("else");
100     out.println(" " + pstmt + ".setBigDecimal(" + index + "++, " + value + ");");
101   }
102
103   /**
104    * Sets the value.
105    */

106   public void setParameter(PreparedStatement JavaDoc pstmt, int index, Object JavaDoc value)
107     throws SQLException JavaDoc
108   {
109     pstmt.setBigDecimal(index, (BigDecimal JavaDoc) value);
110   }
111
112   /**
113    * Gets the value.
114    */

115   public Object JavaDoc getObject(ResultSet JavaDoc rs, int index)
116     throws SQLException JavaDoc
117   {
118     return rs.getBigDecimal(index);
119   }
120
121   public String JavaDoc generateCreateColumnSQL(AmberPersistenceUnit manager, int length, int precision, int scale)
122   {
123     return manager.getCreateColumnSQL(Types.NUMERIC, length, precision, scale);
124   }
125 }
126
Popular Tags