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: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.amber.type;
031:
032: import com.caucho.amber.manager.AmberPersistenceUnit;
033: import com.caucho.java.JavaWriter;
034: import com.caucho.util.L10N;
035:
036: import java.io.IOException;
037: import java.sql.PreparedStatement;
038: import java.sql.ResultSet;
039: import java.sql.SQLException;
040: import java.sql.Types;
041:
042: /**
043: * Represents a java.util.Float type
044: */
045: public class FloatType extends Type {
046: private static final L10N L = new L10N(FloatType.class);
047:
048: private static final FloatType FLOAT_TYPE = new FloatType();
049:
050: private FloatType() {
051: }
052:
053: /**
054: * Returns the singleton Float type.
055: */
056: public static FloatType create() {
057: return FLOAT_TYPE;
058: }
059:
060: /**
061: * Returns the type name.
062: */
063: public String getName() {
064: return "java.lang.Float";
065: }
066:
067: /**
068: * Returns true for a numeric type.
069: */
070: public boolean isNumeric() {
071: return true;
072: }
073:
074: /**
075: * Generates the type for the table.
076: */
077: public String generateCreateColumnSQL(AmberPersistenceUnit manager,
078: int length, int precision, int scale) {
079: return manager.getCreateColumnSQL(Types.REAL, length,
080: precision, scale);
081: }
082:
083: /**
084: * Generates a string to load the property.
085: */
086: public int generateLoad(JavaWriter out, String rs, String indexVar,
087: int index) throws IOException {
088: out.print("com.caucho.amber.type.FloatType.toFloat(" + rs
089: + ".getFloat(" + indexVar + " + " + index + "), " + rs
090: + ".wasNull())");
091:
092: return index + 1;
093: }
094:
095: /**
096: * Generates a string to set the property.
097: */
098: public void generateSet(JavaWriter out, String pstmt, String index,
099: String value) throws IOException {
100: out.println("if (" + value + " == null)");
101: out.println(" " + pstmt + ".setNull(" + index
102: + "++, java.sql.Types.REAL);");
103: out.println("else");
104: // jpa/0gh0
105: out.println(" " + pstmt + ".setFloat(" + index + "++, "
106: + value + ".floatValue());");
107: // XXX: jpa/0u21
108: // out.println(" " + pstmt + ".setString(" + index + "++, " +
109: // value + ".toString());");
110: }
111:
112: /**
113: * Generates a string to set the property.
114: */
115: public void generateSetNull(JavaWriter out, String pstmt,
116: String index) throws IOException {
117: out.println(pstmt + ".setNull(" + index
118: + "++, java.sql.Types.REAL);");
119: }
120:
121: /**
122: * Converts a value to a int.
123: */
124: public static Float toFloat(float value, boolean wasNull) {
125: if (wasNull)
126: return null;
127: else
128: return new Float(value);
129: }
130:
131: /**
132: * Gets the value.
133: */
134: public Object getObject(ResultSet rs, int index)
135: throws SQLException {
136: float value = rs.getFloat(index);
137:
138: return rs.wasNull() ? null : new Float(value);
139: }
140:
141: /**
142: * Sets the value.
143: */
144: public void setParameter(PreparedStatement pstmt, int index,
145: Object value) throws SQLException {
146: if (value == null)
147: pstmt.setNull(index, Types.FLOAT);
148: else
149: pstmt.setFloat(index, ((Float) value).floatValue());
150: }
151: }
|