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: LongMapping.java,v 1.6 2003/10/10 22:22:15 pierreg0 Exp $
009: */
010:
011: package com.triactive.jdo.store;
012:
013: import com.triactive.jdo.PersistenceManager;
014: import java.math.BigInteger;
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 LongMapping extends ColumnMapping {
022: private static final int LONG_MAX_DECIMAL_DIGITS = 19;
023:
024: public LongMapping(DatabaseAdapter dba, Class type) {
025: super (dba, type);
026:
027: initTypeInfo();
028: }
029:
030: public LongMapping(Column col) {
031: super (col);
032:
033: col.checkPrimitive();
034:
035: initTypeInfo();
036: }
037:
038: public LongMapping(ClassBaseTable table, int relativeFieldNumber) {
039: this (table.newColumn(relativeFieldNumber));
040: }
041:
042: protected TypeInfo getTypeInfo() {
043: return dba
044: .getTypeInfo(new int[] { Types.BIGINT, Types.DECIMAL });
045: }
046:
047: protected void initTypeInfo() {
048: super .initTypeInfo();
049:
050: /*
051: * In case the default DECIMAL precision is less than the number of
052: * digits we need, set it manually.
053: */
054: if (col != null && typeInfo.dataType == Types.DECIMAL) {
055: col.setMinimumPrecision(Math.min(typeInfo.precision,
056: LONG_MAX_DECIMAL_DIGITS));
057: col.checkDecimal();
058: }
059: }
060:
061: public void setLong(PersistenceManager pm, PreparedStatement ps,
062: int param, long value) {
063: try {
064: ps.setLong(param, value);
065: } catch (SQLException e) {
066: throw dba.newDataStoreException(
067: "Can't set long parameter: value = " + value, e);
068: }
069: }
070:
071: public long getLong(PersistenceManager pm, ResultSet rs, int param) {
072: long value;
073:
074: try {
075: value = rs.getLong(param);
076:
077: if (rs.wasNull())
078: throw new NullValueException(
079: "Illegal null value in column " + col);
080: } catch (SQLException e) {
081: throw dba.newDataStoreException(
082: "Can't get long result: param = " + param, e);
083: }
084:
085: return value;
086: }
087:
088: public void setObject(PersistenceManager pm, PreparedStatement ps,
089: int param, Object value) {
090: try {
091: if (value == null)
092: ps.setNull(param, typeInfo.dataType);
093: else
094: ps.setLong(param, ((Long) value).longValue());
095: } catch (SQLException e) {
096: throw dba.newDataStoreException(
097: "Can't set Long parameter: value = " + value, e);
098: }
099: }
100:
101: public Object getObject(PersistenceManager pm, ResultSet rs,
102: int param) {
103: Object value;
104:
105: try {
106: long l = rs.getLong(param);
107: value = rs.wasNull() ? null : new Long(l);
108: } catch (SQLException e) {
109: throw dba.newDataStoreException(
110: "Can't get Long result: param = " + param, e);
111: }
112:
113: return value;
114: }
115:
116: public SQLExpression newSQLLiteral(QueryStatement qs, Object value) {
117: return new IntegerLiteral(qs, BigInteger.valueOf(((Long) value)
118: .longValue()));
119: }
120:
121: public SQLExpression newSQLExpression(QueryStatement qs,
122: QueryStatement.QueryColumn qsc, String fieldName) {
123: return new NumericExpression(qs, qsc);
124: }
125: }
|