View Javadoc
1   /*
2    * $Source$
3    * $Revision$
4    *
5    * Copyright (C) 2003 Samuel Goldstein
6    *
7    * Part of Melati (http://melati.org), a framework for the rapid
8    * development of clean, maintainable web applications.
9    *
10   * Melati is free software; Permission is granted to copy, distribute
11   * and/or modify this software under the terms either:
12   *
13   * a) the GNU General Public License as published by the Free Software
14   *    Foundation; either version 2 of the License, or (at your option)
15   *    any later version,
16   *
17   *    or
18   *
19   * b) any version of the Melati Software License, as published
20   *    at http://melati.org
21   *
22   * You should have received a copy of the GNU General Public License and
23   * the Melati Software License along with this program;
24   * if not, write to the Free Software Foundation, Inc.,
25   * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
26   * GNU General Public License and visit http://melati.org to obtain the
27   * Melati Software License.
28   *
29   * Feel free to contact the Developers of Melati (http://melati.org),
30   * if you would like to work out a different arrangement than the options
31   * outlined here.  It is our intention to allow Melati to be used by as
32   * wide an audience as possible.
33   *
34   * This program is distributed in the hope that it will be useful,
35   * but WITHOUT ANY WARRANTY; without even the implied warranty of
36   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37   * GNU General Public License for more details.
38   *
39   * Contact details for copyright holder:
40   *
41   *     Samuel Goldstein <samuel At 1969.ws>
42   *     http://www.1969.ws
43   *     13101 W. Washington Blvd Suite 248, Los Angeles, CA 90066 USA
44   */
45  
46  package org.melati.poem;
47  
48  import java.sql.Types;
49  import java.sql.ResultSet;
50  import java.sql.PreparedStatement;
51  import java.sql.SQLException;
52  import java.math.BigDecimal;
53  import org.melati.poem.dbms.Dbms;
54  
55  /**
56   * Java "BigDecimal", dependant upon the database/SQL implementation.
57   *
58   * By default, this is a 22 digit number, with 2 digits after the
59   * decimal.
60   * */
61  public class BigDecimalPoemType extends FixedPointAtomPoemType<BigDecimal> {
62  
63    /**
64     * Constructor.
65     * 
66     * @param nullable whether nullable 
67     */
68    public BigDecimalPoemType(boolean nullable) {
69      super(Types.DECIMAL, "DECIMAL", nullable, 22, 2);
70    }
71  
72    /**
73     * Constructor.
74     * 
75     * @param nullable whether nullable 
76     * @param precision defaults to 22
77     * @param scale defaults to 2
78     */
79    public BigDecimalPoemType(boolean nullable, int precision, int scale) {
80      super(Types.DECIMAL, "DECIMAL", nullable, precision, scale);
81      // because a newly added column of this type won't have valid 
82      // numbers, we fix that here...
83      // This does not appear to be needed - timp 01/01/2006
84      /* 
85      if (precision <= 0) {
86        setPrecision(22);
87      }
88      if (scale < 0) {
89        setScale(2);
90      }
91      */
92    }
93  
94    protected void _assertValidRaw(Object raw) {
95      if (raw != null && !(raw instanceof BigDecimal))
96        throw new TypeMismatchPoemException(raw, this);
97    }
98  
99    protected BigDecimal _getRaw(ResultSet rs, int col) throws SQLException {
100     synchronized (rs) {
101       BigDecimal x = rs.getBigDecimal(col);
102       return rs.wasNull() ? null : x; }
103   }
104 
105   protected void _setRaw(PreparedStatement ps, int col, Object real)
106     throws SQLException {
107     ps.setBigDecimal(col, ((BigDecimal)real));
108   }
109 
110   protected BigDecimal _rawOfString(String rawString) throws ParsingPoemException {
111     try {
112       return new BigDecimal(rawString);
113     } catch (NumberFormatException e) {
114       throw new ParsingPoemException(this, rawString, e);
115     }
116   }
117 
118   protected String _sqlDefinition(Dbms dbms) {
119     try {
120       return dbms.getFixedPtSqlDefinition(getScale(), getPrecision());
121     } catch (SQLException e) {
122       throw new SQLSeriousPoemException(e);
123     }
124   }
125 
126   /**
127    * Whilst BigDecimal cannot represent all Doubles it can represent 
128    * legacy money doubles, so we allow it to enable upgrades from Doubles 
129    * to BigDecimals.
130    * @see org.melati.poem.BasePoemType#_canRepresent(org.melati.poem.SQLPoemType)
131    */
132   protected boolean _canRepresent(SQLPoemType<?> other) {
133     return // sqlTypeCode() == other.sqlTypeCode() && //commented out to allow doubles  
134            other instanceof BigDecimalPoemType || other instanceof DoublePoemType;
135   }
136 
137   /**
138    * @return the field type used in the Data Structure Definition language.
139    */
140   public String toDsdType() {
141     return "BigDecimal";
142   }
143 
144   protected void _saveColumnInfo(ColumnInfo columnInfo)
145     throws AccessPoemException {
146     columnInfo.setTypefactory(PoemTypeFactory.BIGDECIMAL);
147     columnInfo.setPrecision(getPrecision());
148     columnInfo.setScale(getScale());
149   }
150 
151 }