KickJava   Java API By Example, From Geeks To Geeks.

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


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: IntegerMapping.java,v 1.5 2003/10/10 22:22:15 pierreg0 Exp $
9  */

10
11 package com.triactive.jdo.store;
12
13 import com.triactive.jdo.PersistenceManager;
14 import java.math.BigInteger 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 IntegerMapping extends ColumnMapping
23 {
24     private static final int INT_MAX_DECIMAL_DIGITS = 10;
25
26     public IntegerMapping(DatabaseAdapter dba, Class JavaDoc type)
27     {
28         super(dba, type);
29
30         initTypeInfo();
31     }
32
33     public IntegerMapping(Column col)
34     {
35         super(col);
36
37         col.checkPrimitive();
38
39         initTypeInfo();
40     }
41
42     public IntegerMapping(ClassBaseTable table, int relativeFieldNumber)
43     {
44         this(table.newColumn(relativeFieldNumber));
45     }
46
47     protected TypeInfo getTypeInfo()
48     {
49         return dba.getTypeInfo(new int[] { Types.INTEGER, Types.DECIMAL });
50     }
51
52     protected void initTypeInfo()
53     {
54         super.initTypeInfo();
55
56         /*
57          * In case the default DECIMAL precision is less than the number of
58          * digits we need, set it manually.
59          */

60         if (col != null && typeInfo.dataType == Types.DECIMAL)
61         {
62             col.setMinimumPrecision(INT_MAX_DECIMAL_DIGITS);
63             col.checkDecimal();
64         }
65     }
66
67     public void setInt(PersistenceManager pm, PreparedStatement JavaDoc ps, int param, int value)
68     {
69         try
70         {
71             ps.setInt(param, value);
72         }
73         catch (SQLException JavaDoc e)
74         {
75             throw dba.newDataStoreException("Can't set int parameter: value = " + value, e);
76         }
77     }
78
79     public int getInt(PersistenceManager pm, ResultSet JavaDoc rs, int param)
80     {
81         int value;
82
83         try
84         {
85             value = rs.getInt(param);
86
87             if (rs.wasNull())
88                 throw new NullValueException("Illegal null value in column " + col);
89         }
90         catch (SQLException JavaDoc e)
91         {
92             throw dba.newDataStoreException("Can't get int result: param = " + param, e);
93         }
94
95         return value;
96     }
97
98     public void setObject(PersistenceManager pm, PreparedStatement JavaDoc ps, int param, Object JavaDoc value)
99     {
100         try
101         {
102             if (value == null)
103                 ps.setNull(param, typeInfo.dataType);
104             else
105                 ps.setInt(param, ((Integer JavaDoc)value).intValue());
106         }
107         catch (SQLException JavaDoc e)
108         {
109             throw dba.newDataStoreException("Can't set Integer parameter: value = " + value, e);
110         }
111     }
112
113     public Object JavaDoc getObject(PersistenceManager pm, ResultSet JavaDoc rs, int param)
114     {
115         Object JavaDoc value;
116
117         try
118         {
119             int i = rs.getInt(param);
120             value = rs.wasNull() ? null : new Integer JavaDoc(i);
121         }
122         catch (SQLException JavaDoc e)
123         {
124             throw dba.newDataStoreException("Can't get Integer result: param = " + param, e);
125         }
126
127         return value;
128     }
129
130     public SQLExpression newSQLLiteral(QueryStatement qs, Object JavaDoc value)
131     {
132         return new IntegerLiteral(qs, BigInteger.valueOf(((Integer JavaDoc)value).longValue()));
133     }
134
135     public SQLExpression newSQLExpression(QueryStatement qs, QueryStatement.QueryColumn qsc, String JavaDoc fieldName)
136     {
137         return new NumericExpression(qs, qsc);
138     }
139 }
140
Popular Tags