01: /*
02: * Copyright 2002 (C) TJDO.
03: * All rights reserved.
04: *
05: * This software is distributed under the terms of the TJDO License version 1.0.
06: * See the terms of the TJDO License in the documentation provided with this software.
07: *
08: * $Id: BigIntegerMapping.java,v 1.4 2003/08/04 16:40:35 pierreg0 Exp $
09: */
10:
11: package com.triactive.jdo.store;
12:
13: import com.triactive.jdo.PersistenceManager;
14: import java.math.BigDecimal;
15: import java.math.BigInteger;
16: import java.sql.PreparedStatement;
17: import java.sql.ResultSet;
18: import java.sql.SQLException;
19: import java.sql.Types;
20: import javax.jdo.JDODataStoreException;
21:
22: public class BigIntegerMapping extends ColumnMapping {
23: public BigIntegerMapping(DatabaseAdapter dba, Class type) {
24: super (dba, type);
25:
26: initTypeInfo();
27: }
28:
29: public BigIntegerMapping(Column col) {
30: super (col);
31:
32: col.checkInteger();
33:
34: initTypeInfo();
35: }
36:
37: public BigIntegerMapping(ClassBaseTable table,
38: int relativeFieldNumber) {
39: this (table.newColumn(relativeFieldNumber));
40: }
41:
42: protected TypeInfo getTypeInfo() {
43: if (col != null && col.isExactPrecision())
44: return dba.getTypeInfo(new int[] { Types.NUMERIC,
45: Types.DECIMAL });
46: else
47: return dba.getTypeInfo(new int[] { Types.DECIMAL,
48: Types.NUMERIC });
49: }
50:
51: public void setObject(PersistenceManager pm, PreparedStatement ps,
52: int param, Object value) {
53: try {
54: if (value == null)
55: ps.setNull(param, typeInfo.dataType);
56: else
57: ps.setBigDecimal(param, new BigDecimal(
58: (BigInteger) value));
59: } catch (SQLException e) {
60: throw new JDODataStoreException(
61: "Can't set BigInteger parameter: value = " + value,
62: e);
63: }
64: }
65:
66: public Object getObject(PersistenceManager pm, ResultSet rs,
67: int param) {
68: try {
69: BigDecimal value = rs.getBigDecimal(param);
70:
71: if (value == null)
72: return null;
73: else
74: return value.toBigInteger();
75: } catch (SQLException e) {
76: throw new JDODataStoreException(
77: "Can't get BigInteger result: param = " + param, e);
78: }
79: }
80:
81: public SQLExpression newSQLLiteral(QueryStatement qs, Object value) {
82: return new IntegerLiteral(qs, (BigInteger) value);
83: }
84:
85: public SQLExpression newSQLExpression(QueryStatement qs,
86: QueryStatement.QueryColumn qsc, String fieldName) {
87: return new NumericExpression(qs, qsc);
88: }
89: }
|