001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free Software Foundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.amber.type;
030:
031: import com.caucho.amber.manager.AmberPersistenceUnit;
032: import com.caucho.java.JavaWriter;
033: import com.caucho.util.L10N;
034:
035: import java.io.IOException;
036: import java.math.BigDecimal;
037: import java.sql.PreparedStatement;
038: import java.sql.ResultSet;
039: import java.sql.SQLException;
040: import java.sql.Types;
041:
042: /**
043: * The type of a property.
044: */
045: public class BigDecimalType extends Type {
046: private static final L10N L = new L10N(BigDecimalType.class);
047:
048: private int _scale = -1;
049:
050: private BigDecimalType() {
051: }
052:
053: /**
054: * Returns the singleton BigDecimal type.
055: */
056: public static BigDecimalType create() {
057: return new BigDecimalType();
058: }
059:
060: /**
061: * Returns the type name.
062: */
063: public String getName() {
064: return "java.math.BigDecimal";
065: }
066:
067: /**
068: * Returns true for a numeric type.
069: */
070: public boolean isNumeric() {
071: return true;
072: }
073:
074: /**
075: * Generates a string to load the property.
076: */
077: public int generateLoad(JavaWriter out, String rs, String indexVar,
078: int index) throws IOException {
079: out.print(rs + ".getBigDecimal(" + indexVar + " + " + index
080: + ")");
081:
082: return index + 1;
083: }
084:
085: /**
086: * Generates a string to set the property.
087: */
088: public void generateSet(JavaWriter out, String pstmt, String index,
089: String value) throws IOException {
090: out.println("if (" + value + " == null)");
091: out.println(" " + pstmt + ".setNull(" + index
092: + "++, java.sql.Types.DECIMAL);");
093: out.println("else");
094: out.println(" " + pstmt + ".setBigDecimal(" + index + "++, "
095: + value + ");");
096: }
097:
098: /**
099: * Sets the value.
100: */
101: public void setParameter(PreparedStatement pstmt, int index,
102: Object value) throws SQLException {
103: pstmt.setBigDecimal(index, (BigDecimal) value);
104: }
105:
106: /**
107: * Gets the value.
108: */
109: public Object getObject(ResultSet rs, int index)
110: throws SQLException {
111: return rs.getBigDecimal(index);
112: }
113:
114: public String generateCreateColumnSQL(AmberPersistenceUnit manager,
115: int length, int precision, int scale) {
116: return manager.getCreateColumnSQL(Types.NUMERIC, length,
117: precision, scale);
118: }
119: }
|