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: BigDecimalMapping.java,v 1.5 2003/10/10 22:22:14 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.sql.PreparedStatement;
16: import java.sql.ResultSet;
17: import java.sql.SQLException;
18: import java.sql.Types;
19: import javax.jdo.JDODataStoreException;
20:
21: public class BigDecimalMapping extends ColumnMapping {
22: public BigDecimalMapping(DatabaseAdapter dba, Class type) {
23: super (dba, type);
24:
25: initTypeInfo();
26: }
27:
28: public BigDecimalMapping(Column col) {
29: super (col);
30:
31: col.checkDecimal();
32:
33: initTypeInfo();
34: }
35:
36: public BigDecimalMapping(ClassBaseTable table,
37: int relativeFieldNumber) {
38: this (table.newColumn(relativeFieldNumber));
39: }
40:
41: protected TypeInfo getTypeInfo() {
42: if (col != null && col.isExactPrecision())
43: return dba.getTypeInfo(new int[] { Types.NUMERIC,
44: Types.DECIMAL });
45: else
46: return dba.getTypeInfo(new int[] { Types.DECIMAL,
47: Types.NUMERIC });
48: }
49:
50: public void setObject(PersistenceManager pm, PreparedStatement ps,
51: int param, Object value) {
52: try {
53: if (value == null)
54: ps.setNull(param, typeInfo.dataType);
55: else
56: ps.setBigDecimal(param, (BigDecimal) value);
57: } catch (SQLException e) {
58: throw dba.newDataStoreException(
59: "Can't set BigDecimal parameter: value = " + value,
60: e);
61: }
62: }
63:
64: public Object getObject(PersistenceManager pm, ResultSet rs,
65: int param) {
66: try {
67: return rs.getBigDecimal(param);
68: } catch (SQLException e) {
69: throw dba.newDataStoreException(
70: "Can't get BigDecimal result: param = " + param, e);
71: }
72: }
73:
74: public SQLExpression newSQLLiteral(QueryStatement qs, Object value) {
75: return new FloatingPointLiteral(qs, (BigDecimal) value);
76: }
77:
78: public SQLExpression newSQLExpression(QueryStatement qs,
79: QueryStatement.QueryColumn qsc, String fieldName) {
80: return new NumericExpression(qs, qsc);
81: }
82: }
|