KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > store > BigDecimalMapping


1 /*
2  * Copyright 2002 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: BigDecimalMapping.java,v 1.5 2003/10/10 22:22:14 pierreg0 Exp $
9  */

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