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: IntegerMapping.java,v 1.5 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 IntegerMapping extends ColumnMapping {
022: private static final int INT_MAX_DECIMAL_DIGITS = 10;
023:
024: public IntegerMapping(DatabaseAdapter dba, Class type) {
025: super (dba, type);
026:
027: initTypeInfo();
028: }
029:
030: public IntegerMapping(Column col) {
031: super (col);
032:
033: col.checkPrimitive();
034:
035: initTypeInfo();
036: }
037:
038: public IntegerMapping(ClassBaseTable table, int relativeFieldNumber) {
039: this (table.newColumn(relativeFieldNumber));
040: }
041:
042: protected TypeInfo getTypeInfo() {
043: return dba
044: .getTypeInfo(new int[] { Types.INTEGER, 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(INT_MAX_DECIMAL_DIGITS);
056: col.checkDecimal();
057: }
058: }
059:
060: public void setInt(PersistenceManager pm, PreparedStatement ps,
061: int param, int value) {
062: try {
063: ps.setInt(param, value);
064: } catch (SQLException e) {
065: throw dba.newDataStoreException(
066: "Can't set int parameter: value = " + value, e);
067: }
068: }
069:
070: public int getInt(PersistenceManager pm, ResultSet rs, int param) {
071: int value;
072:
073: try {
074: value = rs.getInt(param);
075:
076: if (rs.wasNull())
077: throw new NullValueException(
078: "Illegal null value in column " + col);
079: } catch (SQLException e) {
080: throw dba.newDataStoreException(
081: "Can't get int result: param = " + param, e);
082: }
083:
084: return value;
085: }
086:
087: public void setObject(PersistenceManager pm, PreparedStatement ps,
088: int param, Object value) {
089: try {
090: if (value == null)
091: ps.setNull(param, typeInfo.dataType);
092: else
093: ps.setInt(param, ((Integer) value).intValue());
094: } catch (SQLException e) {
095: throw dba.newDataStoreException(
096: "Can't set Integer parameter: value = " + value, e);
097: }
098: }
099:
100: public Object getObject(PersistenceManager pm, ResultSet rs,
101: int param) {
102: Object value;
103:
104: try {
105: int i = rs.getInt(param);
106: value = rs.wasNull() ? null : new Integer(i);
107: } catch (SQLException e) {
108: throw dba.newDataStoreException(
109: "Can't get Integer result: param = " + param, e);
110: }
111:
112: return value;
113: }
114:
115: public SQLExpression newSQLLiteral(QueryStatement qs, Object value) {
116: return new IntegerLiteral(qs, BigInteger
117: .valueOf(((Integer) value).longValue()));
118: }
119:
120: public SQLExpression newSQLExpression(QueryStatement qs,
121: QueryStatement.QueryColumn qsc, String fieldName) {
122: return new NumericExpression(qs, qsc);
123: }
124: }
|