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: DoubleMapping.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 DoubleMapping extends ColumnMapping {
022: public DoubleMapping(DatabaseAdapter dba, Class type) {
023: super (dba, type);
024:
025: initTypeInfo();
026: }
027:
028: public DoubleMapping(Column col) {
029: super (col);
030:
031: col.checkPrimitive();
032:
033: initTypeInfo();
034: }
035:
036: public DoubleMapping(ClassBaseTable table, int relativeFieldNumber) {
037: this (table.newColumn(relativeFieldNumber));
038: }
039:
040: protected TypeInfo getTypeInfo() {
041: return dba.getTypeInfo(new int[] { Types.DOUBLE, Types.FLOAT });
042: }
043:
044: public void setDouble(PersistenceManager pm, PreparedStatement ps,
045: int param, double value) {
046: try {
047: ps.setDouble(param, value);
048: } catch (SQLException e) {
049: throw dba.newDataStoreException(
050: "Can't set double parameter: value = " + value, e);
051: }
052: }
053:
054: public double getDouble(PersistenceManager pm, ResultSet rs,
055: int param) {
056: double value;
057:
058: try {
059: value = rs.getDouble(param);
060:
061: if (rs.wasNull())
062: throw new NullValueException(
063: "Illegal null value in column " + col);
064: } catch (SQLException e) {
065: throw dba.newDataStoreException(
066: "Can't get double result: param = " + param, e);
067: }
068:
069: return value;
070: }
071:
072: public void setObject(PersistenceManager pm, PreparedStatement ps,
073: int param, Object value) {
074: try {
075: if (value == null)
076: ps.setNull(param, typeInfo.dataType);
077: else
078: ps.setDouble(param, ((Double) value).doubleValue());
079: } catch (SQLException e) {
080: throw dba.newDataStoreException(
081: "Can't set Double parameter: value = " + value, e);
082: }
083: }
084:
085: public Object getObject(PersistenceManager pm, ResultSet rs,
086: int param) {
087: Object value;
088:
089: try {
090: double d = rs.getDouble(param);
091: value = rs.wasNull() ? null : new Double(d);
092: } catch (SQLException e) {
093: throw dba.newDataStoreException(
094: "Can't get Double result: param = " + param, e);
095: }
096:
097: return value;
098: }
099:
100: public SQLExpression newSQLLiteral(QueryStatement qs, Object value) {
101: return new FloatingPointLiteral(qs, (Double) value);
102: }
103:
104: public SQLExpression newSQLExpression(QueryStatement qs,
105: QueryStatement.QueryColumn qsc, String fieldName) {
106: return new NumericExpression(qs, qsc);
107: }
108: }
|