001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.jdbc.kernel.exps;
020:
021: import java.sql.SQLException;
022:
023: import org.apache.openjpa.jdbc.meta.ClassMapping;
024: import org.apache.openjpa.jdbc.meta.Joinable;
025: import org.apache.openjpa.jdbc.schema.Column;
026: import org.apache.openjpa.jdbc.sql.Result;
027: import org.apache.openjpa.jdbc.sql.SQLBuffer;
028: import org.apache.openjpa.jdbc.sql.Select;
029: import org.apache.openjpa.kernel.Filters;
030: import org.apache.openjpa.kernel.exps.ExpressionVisitor;
031: import org.apache.openjpa.lib.util.Localizer;
032: import org.apache.openjpa.meta.ClassMetaData;
033: import org.apache.openjpa.util.ApplicationIds;
034: import org.apache.openjpa.util.Id;
035: import org.apache.openjpa.util.OpenJPAId;
036: import org.apache.openjpa.util.UserException;
037: import serp.util.Numbers;
038:
039: /**
040: * Select the oid value of an object; typically used in projections.
041: *
042: * @author Abe White
043: */
044: class GetObjectId extends AbstractVal {
045:
046: private static final Localizer _loc = Localizer
047: .forPackage(GetObjectId.class);
048:
049: private final PCPath _path;
050: private ClassMetaData _meta = null;
051:
052: /**
053: * Constructor. Provide the value whose oid to extract.
054: */
055: public GetObjectId(PCPath path) {
056: _path = path;
057: }
058:
059: /**
060: * Return the oid columns.
061: */
062: public Column[] getColumns(ExpState state) {
063: return _path.getClassMapping(state).getPrimaryKeyColumns();
064: }
065:
066: public ClassMetaData getMetaData() {
067: return _meta;
068: }
069:
070: public void setMetaData(ClassMetaData meta) {
071: _meta = meta;
072: }
073:
074: public Class getType() {
075: return Object.class;
076: }
077:
078: public void setImplicitType(Class type) {
079: }
080:
081: public ExpState initialize(Select sel, ExpContext ctx, int flags) {
082: ExpState state = _path.initialize(sel, ctx, JOIN_REL);
083:
084: // it's difficult to get calls on non-pc fields to always return null
085: // without screwing up the SQL, to just don't let users call it on
086: // non-pc fields at all
087: ClassMapping cls = _path.getClassMapping(state);
088: if (cls == null || cls.getEmbeddingMapping() != null)
089: throw new UserException(_loc.get("bad-getobjectid", _path
090: .getFieldMapping(state)));
091: return state;
092: }
093:
094: public Object toDataStoreValue(Select sel, ExpContext ctx,
095: ExpState state, Object val) {
096: // if datastore identity, try to convert to a long value
097: ClassMapping mapping = _path.getClassMapping(state);
098: if (mapping.getIdentityType() == mapping.ID_DATASTORE) {
099: if (val instanceof Id)
100: return Numbers.valueOf(((Id) val).getId());
101: return Filters.convert(val, long.class);
102: }
103:
104: // if unknown identity, can't do much
105: if (mapping.getIdentityType() == mapping.ID_UNKNOWN)
106: return (val instanceof OpenJPAId) ? ((OpenJPAId) val)
107: .getIdObject() : val;
108:
109: // application identity; convert to pk values in the same order as
110: // the mapping's primary key columns will be returned
111: Object[] pks = ApplicationIds.toPKValues(val, mapping);
112: if (pks.length == 1)
113: return pks[0];
114: if (val == null)
115: return pks;
116: while (!mapping.isPrimaryKeyObjectId(false))
117: mapping = mapping.getJoinablePCSuperclassMapping();
118:
119: Column[] cols = mapping.getPrimaryKeyColumns();
120: Object[] vals = new Object[cols.length];
121: Joinable join;
122: for (int i = 0; i < cols.length; i++) {
123: join = mapping.assertJoinable(cols[i]);
124: vals[i] = pks[mapping.getField(join.getFieldIndex())
125: .getPrimaryKeyIndex()];
126: vals[i] = join.getJoinValue(vals[i], cols[i], ctx.store);
127: }
128: return vals;
129: }
130:
131: public void select(Select sel, ExpContext ctx, ExpState state,
132: boolean pks) {
133: selectColumns(sel, ctx, state, true);
134: }
135:
136: public void selectColumns(Select sel, ExpContext ctx,
137: ExpState state, boolean pks) {
138: _path.selectColumns(sel, ctx, state, true);
139: }
140:
141: public void groupBy(Select sel, ExpContext ctx, ExpState state) {
142: _path.groupBy(sel, ctx, state);
143: }
144:
145: public void orderBy(Select sel, ExpContext ctx, ExpState state,
146: boolean asc) {
147: _path.orderBy(sel, ctx, state, asc);
148: }
149:
150: public Object load(ExpContext ctx, ExpState state, Result res)
151: throws SQLException {
152: return _path.load(ctx, state, res, true);
153: }
154:
155: public void calculateValue(Select sel, ExpContext ctx,
156: ExpState state, Val other, ExpState otherState) {
157: _path.calculateValue(sel, ctx, state, null, null);
158: }
159:
160: public int length(Select sel, ExpContext ctx, ExpState state) {
161: return _path.length(sel, ctx, state);
162: }
163:
164: public void appendTo(Select sel, ExpContext ctx, ExpState state,
165: SQLBuffer sql, int index) {
166: _path.appendTo(sel, ctx, state, sql, index);
167: }
168:
169: public void acceptVisit(ExpressionVisitor visitor) {
170: visitor.enter(this);
171: _path.acceptVisit(visitor);
172: visitor.exit(this);
173: }
174: }
|