1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
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.kernel;
20
21 import java.sql.SQLException;
22
23 import org.apache.openjpa.jdbc.meta.ClassMapping;
24 import org.apache.openjpa.jdbc.sql.Result;
25 import org.apache.openjpa.jdbc.sql.SQLExceptions;
26 import org.apache.openjpa.kernel.StoreContext;
27 import org.apache.openjpa.lib.rop.ResultObjectProvider;
28 import org.apache.openjpa.util.StoreException;
29 import org.apache.openjpa.util.UnsupportedException;
30
31 /**
32 * Object provider implementation wrapped around a generic {@link Result}.
33 *
34 * @author Abe White
35 */
36 public class GenericResultObjectProvider
37 implements ResultObjectProvider {
38
39 private final ClassMapping _mapping;
40 private final JDBCStore _store;
41 private final JDBCFetchConfiguration _fetch;
42 private final Result _res;
43
44 /**
45 * Constructor.
46 *
47 * @param pcClass the base class of the result objects
48 * @param store the store manager to delegate loading to
49 * @param fetch the fetch configuration, or null for default
50 * @param res the result containing the data
51 */
52 public GenericResultObjectProvider(Class pcClass,
53 JDBCStore store, JDBCFetchConfiguration fetch, Result res) {
54 this(store.getConfiguration().getMappingRepositoryInstance().getMapping
55 (pcClass, store.getContext().getClassLoader(), true),
56 store, fetch, res);
57 }
58
59 /**
60 * Constructor.
61 *
62 * @param mapping the mapping for the base class of the result objects
63 * @param store the store manager to delegate loading to
64 * @param fetch the fetch configuration, or null for default
65 * @param res the result containing the data
66 */
67 public GenericResultObjectProvider(ClassMapping mapping,
68 JDBCStore store, JDBCFetchConfiguration fetch, Result res) {
69 _mapping = mapping;
70 _store = store;
71 if (fetch == null)
72 _fetch = store.getFetchConfiguration();
73 else
74 _fetch = fetch;
75 _res = res;
76 }
77
78 public boolean supportsRandomAccess() {
79 try {
80 return _res.supportsRandomAccess();
81 } catch (Throwable t) {
82 return false;
83 }
84 }
85
86 public void open() {
87 }
88
89 public Object getResultObject()
90 throws SQLException {
91 // rather than use the standard result.load(), we go direct to
92 // the store manager so we can tell it not to load anything additional
93 return ((JDBCStoreManager) _store).load(_mapping, _fetch,
94 StoreContext.EXCLUDE_ALL, _res);
95 }
96
97 public boolean next()
98 throws SQLException {
99 return _res.next();
100 }
101
102 public boolean absolute(int pos)
103 throws SQLException {
104 return _res.absolute(pos);
105 }
106
107 public int size()
108 throws SQLException {
109 if (_fetch.getLRSSize() == LRSSizes.SIZE_UNKNOWN
110 || !supportsRandomAccess())
111 return Integer.MAX_VALUE;
112 return _res.size();
113 }
114
115 public void reset() {
116 throw new UnsupportedException();
117 }
118
119 public void close() {
120 _res.close();
121 }
122
123 public void handleCheckedException(Exception e) {
124 if (e instanceof SQLException)
125 throw SQLExceptions.getStore((SQLException) e,
126 _store.getDBDictionary());
127 throw new StoreException(e);
128 }
129 }