KickJava   Java API By Example, From Geeks To Geeks.

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


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: OIDMapping.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.sql.PreparedStatement JavaDoc;
15 import java.sql.ResultSet JavaDoc;
16 import java.sql.SQLException JavaDoc;
17 import java.sql.Types JavaDoc;
18 import javax.jdo.JDODataStoreException;
19
20
21 public class OIDMapping extends ColumnMapping
22 {
23     private static final int LONG_MAX_DECIMAL_DIGITS = 19;
24
25     public OIDMapping(DatabaseAdapter dba, Class JavaDoc type)
26     {
27         super(dba, type);
28
29         initTypeInfo();
30     }
31
32     public OIDMapping(Column col)
33     {
34         super(col);
35
36         col.checkPrimitive();
37
38         initTypeInfo();
39     }
40
41     public OIDMapping(ClassBaseTable table, int relativeFieldNumber)
42     {
43         this(table.newColumn(relativeFieldNumber));
44     }
45
46     protected TypeInfo getTypeInfo()
47     {
48         return dba.getTypeInfo(new int[] { Types.BIGINT, Types.DECIMAL });
49     }
50
51     protected void initTypeInfo()
52     {
53         super.initTypeInfo();
54
55         /*
56          * In case the default DECIMAL precision is less than the number of
57          * digits we need, set it manually.
58          */

59         if (col != null && typeInfo.dataType == Types.DECIMAL)
60         {
61             col.setMinimumPrecision(Math.min(typeInfo.precision, LONG_MAX_DECIMAL_DIGITS));
62             col.checkDecimal();
63         }
64     }
65
66     public void setObject(PersistenceManager pm, PreparedStatement JavaDoc ps, int param, Object JavaDoc value)
67     {
68         try
69         {
70             if (value == null)
71                 ps.setNull(param, typeInfo.dataType);
72             else
73                 ps.setLong(param, ((OID)value).longValue());
74         }
75         catch (SQLException JavaDoc e)
76         {
77             throw dba.newDataStoreException("Can't set OID parameter: value = " + value, e);
78         }
79     }
80
81     public Object JavaDoc getObject(PersistenceManager pm, ResultSet JavaDoc rs, int param)
82     {
83         Object JavaDoc value;
84
85         try
86         {
87             long l = rs.getLong(param);
88             value = rs.wasNull() ? null : new OID(l);
89         }
90         catch (SQLException JavaDoc e)
91         {
92             throw dba.newDataStoreException("Can't get OID result: param = " + param, e);
93         }
94
95         return value;
96     }
97
98     public SQLExpression newSQLLiteral(QueryStatement qs, Object JavaDoc value)
99     {
100         return new ObjectLiteral(qs, this, value);
101     }
102
103     public SQLExpression newSQLExpression(QueryStatement qs, QueryStatement.QueryColumn qsc, String JavaDoc fieldName)
104     {
105         return new ObjectExpression(qs, qsc);
106     }
107 }
108
Popular Tags