01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19: package org.apache.openjpa.jdbc.meta.strats;
20:
21: import java.sql.SQLException;
22:
23: import org.apache.openjpa.jdbc.kernel.JDBCStore;
24: import org.apache.openjpa.jdbc.meta.ClassMapping;
25: import org.apache.openjpa.jdbc.meta.Joinable;
26: import org.apache.openjpa.jdbc.schema.Column;
27: import org.apache.openjpa.jdbc.schema.ForeignKey;
28: import org.apache.openjpa.jdbc.sql.Joins;
29: import org.apache.openjpa.jdbc.sql.Result;
30: import org.apache.openjpa.kernel.OpenJPAStateManager;
31: import org.apache.openjpa.util.Id;
32: import serp.util.Numbers;
33:
34: /**
35: * {@link Joinable} for the datastore identity column.
36: *
37: * @author Abe White
38: */
39: class IdentityJoinable implements Joinable {
40:
41: private final ClassMapping mapping;
42:
43: /**
44: * Constructor; supply datastore identity mapping.
45: */
46: public IdentityJoinable(ClassMapping mapping) {
47: this .mapping = mapping;
48: }
49:
50: public int getFieldIndex() {
51: return -1;
52: }
53:
54: public Object getPrimaryKeyValue(Result res, Column[] cols,
55: ForeignKey fk, JDBCStore store, Joins joins)
56: throws SQLException {
57: Column col = cols[0];
58: if (fk != null)
59: col = fk.getColumn(col);
60: long id = res.getLong(col);
61: if (id == 0 && res.wasNull())
62: return null;
63: return Numbers.valueOf(id);
64: }
65:
66: public Column[] getColumns() {
67: return mapping.getPrimaryKeyColumns();
68: }
69:
70: public Object getJoinValue(Object val, Column col, JDBCStore store) {
71: return val;
72: }
73:
74: public Object getJoinValue(OpenJPAStateManager sm, Column col,
75: JDBCStore store) {
76: Id id = (Id) sm.getObjectId();
77: return (id == null) ? null : id.getIdObject();
78: }
79:
80: public void setAutoAssignedValue(OpenJPAStateManager sm,
81: JDBCStore store, Column col, Object autogen) {
82: long id = ((Number) autogen).longValue();
83: sm.setObjectId(store.newDataStoreId(id, (ClassMapping) sm
84: .getMetaData(), true));
85: }
86: }
|