001: /*
002: * Copyright 2002 (C) TJDO.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the TJDO License version 1.0.
006: * See the terms of the TJDO License in the documentation provided with this software.
007: *
008: * $Id: FloatMapping.java,v 1.6 2003/10/10 22:22:14 pierreg0 Exp $
009: */
010:
011: package com.triactive.jdo.store;
012:
013: import com.triactive.jdo.PersistenceManager;
014: import java.math.BigDecimal;
015: import java.sql.PreparedStatement;
016: import java.sql.ResultSet;
017: import java.sql.SQLException;
018: import java.sql.Types;
019: import javax.jdo.JDODataStoreException;
020:
021: public class FloatMapping extends ColumnMapping {
022: public FloatMapping(DatabaseAdapter dba, Class type) {
023: super (dba, type);
024:
025: initTypeInfo();
026: }
027:
028: public FloatMapping(Column col) {
029: super (col);
030:
031: col.checkPrimitive();
032:
033: initTypeInfo();
034: }
035:
036: public FloatMapping(ClassBaseTable table, int relativeFieldNumber) {
037: this (table.newColumn(relativeFieldNumber));
038: }
039:
040: protected TypeInfo getTypeInfo() {
041: return dba.getTypeInfo(new int[] { Types.REAL, Types.FLOAT });
042: }
043:
044: public void setFloat(PersistenceManager pm, PreparedStatement ps,
045: int param, float value) {
046: try {
047: ps.setFloat(param, value);
048: } catch (SQLException e) {
049: throw dba.newDataStoreException(
050: "Can't set float parameter: value = " + value, e);
051: }
052: }
053:
054: public float getFloat(PersistenceManager pm, ResultSet rs, int param) {
055: float value;
056:
057: try {
058: value = rs.getFloat(param);
059:
060: if (rs.wasNull())
061: throw new NullValueException(
062: "Illegal null value in column " + col);
063: } catch (SQLException e) {
064: throw dba.newDataStoreException(
065: "Can't get float result: param = " + param, e);
066: }
067:
068: return value;
069: }
070:
071: public void setObject(PersistenceManager pm, PreparedStatement ps,
072: int param, Object value) {
073: try {
074: if (value == null)
075: ps.setNull(param, typeInfo.dataType);
076: else
077: ps.setFloat(param, ((Float) value).floatValue());
078: } catch (SQLException e) {
079: throw dba.newDataStoreException(
080: "Can't set Float parameter: value = " + value, e);
081: }
082: }
083:
084: public Object getObject(PersistenceManager pm, ResultSet rs,
085: int param) {
086: Object value;
087:
088: try {
089: float f = rs.getFloat(param);
090: value = rs.wasNull() ? null : new Float(f);
091: } catch (SQLException e) {
092: throw dba.newDataStoreException(
093: "Can't get Float result: param = " + param, e);
094: }
095:
096: return value;
097: }
098:
099: public SQLExpression newSQLLiteral(QueryStatement qs, Object value) {
100: return new FloatingPointLiteral(qs, (Float) value);
101: }
102:
103: public SQLExpression newSQLExpression(QueryStatement qs,
104: QueryStatement.QueryColumn qsc, String fieldName) {
105: return new NumericExpression(qs, qsc);
106: }
107: }
|