KickJava   Java API By Example, From Geeks To Geeks.

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


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: LongMapping.java,v 1.6 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 LongMapping extends ColumnMapping
23 {
24     private static final int LONG_MAX_DECIMAL_DIGITS = 19;
25
26     public LongMapping(DatabaseAdapter dba, Class JavaDoc type)
27     {
28         super(dba, type);
29
30         initTypeInfo();
31     }
32
33     public LongMapping(Column col)
34     {
35         super(col);
36
37         col.checkPrimitive();
38
39         initTypeInfo();
40     }
41
42     public LongMapping(ClassBaseTable table, int relativeFieldNumber)
43     {
44         this(table.newColumn(relativeFieldNumber));
45     }
46
47     protected TypeInfo getTypeInfo()
48     {
49         return dba.getTypeInfo(new int[] { Types.BIGINT, 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(Math.min(typeInfo.precision, LONG_MAX_DECIMAL_DIGITS));
63             col.checkDecimal();
64         }
65     }
66
67     public void setLong(PersistenceManager pm, PreparedStatement JavaDoc ps, int param, long value)
68     {
69         try
70         {
71             ps.setLong(param, value);
72         }
73         catch (SQLException JavaDoc e)
74         {
75             throw dba.newDataStoreException("Can't set long parameter: value = " + value, e);
76         }
77     }
78
79     public long getLong(PersistenceManager pm, ResultSet JavaDoc rs, int param)
80     {
81         long value;
82
83         try
84         {
85             value = rs.getLong(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 long 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.setLong(param, ((Long JavaDoc)value).longValue());
106         }
107         catch (SQLException JavaDoc e)
108         {
109             throw dba.newDataStoreException("Can't set Long 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             long l = rs.getLong(param);
120             value = rs.wasNull() ? null : new Long JavaDoc(l);
121         }
122         catch (SQLException JavaDoc e)
123         {
124             throw dba.newDataStoreException("Can't get Long 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(((Long 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