KickJava   Java API By Example, From Geeks To Geeks.

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


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