KickJava   Java API By Example, From Geeks To Geeks.

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


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.math.BigInteger JavaDoc;
38 import java.sql.PreparedStatement JavaDoc;
39 import java.sql.ResultSet JavaDoc;
40 import java.sql.SQLException JavaDoc;
41 import java.sql.Types JavaDoc;
42
43 /**
44  * The type of a property.
45  */

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

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

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

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

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

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

107   public void setParameter(PreparedStatement JavaDoc pstmt, int index, Object JavaDoc value)
108     throws SQLException JavaDoc
109   {
110     if (value == null)
111       pstmt.setNull(index, java.sql.Types.DECIMAL);
112     else
113       pstmt.setBigDecimal(index, new BigDecimal JavaDoc((BigInteger JavaDoc) value));
114   }
115
116   /**
117    * Gets the value.
118    */

119   public Object JavaDoc getObject(ResultSet JavaDoc rs, int index)
120     throws SQLException JavaDoc
121   {
122     BigDecimal JavaDoc v = rs.getBigDecimal(index);
123
124     if (rs.wasNull())
125       return null;
126
127     return v.toBigInteger();
128   }
129
130   public String JavaDoc generateCreateColumnSQL(AmberPersistenceUnit manager,
131                                         int length,
132                                         int precision,
133                                         int scale)
134   {
135     return manager.getCreateColumnSQL(Types.NUMERIC, length, precision, scale);
136   }
137 }
138
Popular Tags