01: /*
02: * Copyright 2002 (C) TJDO.
03: * All rights reserved.
04: *
05: * This software is distributed under the terms of the TJDO License version 1.0.
06: * See the terms of the TJDO License in the documentation provided with this software.
07: *
08: * $Id: OIDMapping.java,v 1.6 2003/10/10 22:22:15 pierreg0 Exp $
09: */
10:
11: package com.triactive.jdo.store;
12:
13: import com.triactive.jdo.PersistenceManager;
14: import java.sql.PreparedStatement;
15: import java.sql.ResultSet;
16: import java.sql.SQLException;
17: import java.sql.Types;
18: import javax.jdo.JDODataStoreException;
19:
20: public class OIDMapping extends ColumnMapping {
21: private static final int LONG_MAX_DECIMAL_DIGITS = 19;
22:
23: public OIDMapping(DatabaseAdapter dba, Class type) {
24: super (dba, type);
25:
26: initTypeInfo();
27: }
28:
29: public OIDMapping(Column col) {
30: super (col);
31:
32: col.checkPrimitive();
33:
34: initTypeInfo();
35: }
36:
37: public OIDMapping(ClassBaseTable table, int relativeFieldNumber) {
38: this (table.newColumn(relativeFieldNumber));
39: }
40:
41: protected TypeInfo getTypeInfo() {
42: return dba
43: .getTypeInfo(new int[] { Types.BIGINT, Types.DECIMAL });
44: }
45:
46: protected void initTypeInfo() {
47: super .initTypeInfo();
48:
49: /*
50: * In case the default DECIMAL precision is less than the number of
51: * digits we need, set it manually.
52: */
53: if (col != null && typeInfo.dataType == Types.DECIMAL) {
54: col.setMinimumPrecision(Math.min(typeInfo.precision,
55: LONG_MAX_DECIMAL_DIGITS));
56: col.checkDecimal();
57: }
58: }
59:
60: public void setObject(PersistenceManager pm, PreparedStatement ps,
61: int param, Object value) {
62: try {
63: if (value == null)
64: ps.setNull(param, typeInfo.dataType);
65: else
66: ps.setLong(param, ((OID) value).longValue());
67: } catch (SQLException e) {
68: throw dba.newDataStoreException(
69: "Can't set OID parameter: value = " + value, e);
70: }
71: }
72:
73: public Object getObject(PersistenceManager pm, ResultSet rs,
74: int param) {
75: Object value;
76:
77: try {
78: long l = rs.getLong(param);
79: value = rs.wasNull() ? null : new OID(l);
80: } catch (SQLException e) {
81: throw dba.newDataStoreException(
82: "Can't get OID result: param = " + param, e);
83: }
84:
85: return value;
86: }
87:
88: public SQLExpression newSQLLiteral(QueryStatement qs, Object value) {
89: return new ObjectLiteral(qs, this , value);
90: }
91:
92: public SQLExpression newSQLExpression(QueryStatement qs,
93: QueryStatement.QueryColumn qsc, String fieldName) {
94: return new ObjectExpression(qs, qsc);
95: }
96: }
|