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;
020:
021: import java.sql.SQLException;
022:
023: import org.apache.openjpa.jdbc.sql.Result;
024: import org.apache.openjpa.jdbc.sql.SQLExceptions;
025: import org.apache.openjpa.jdbc.sql.Select;
026: import org.apache.openjpa.jdbc.sql.SelectExecutor;
027: import org.apache.openjpa.lib.rop.ResultObjectProvider;
028: import org.apache.openjpa.util.StoreException;
029:
030: /**
031: * Abstract provider implementation wrapped around a {@link Select}.
032: *
033: * @author Abe White
034: * @nojavadoc
035: */
036: public abstract class SelectResultObjectProvider implements
037: ResultObjectProvider {
038:
039: private final SelectExecutor _sel;
040: private final JDBCStore _store;
041: private final JDBCFetchConfiguration _fetch;
042: private Result _res = null;
043: private int _size = -1;
044: private Boolean _ra = null;
045:
046: /**
047: * Constructor.
048: *
049: * @param sel the select to execute
050: * @param store the store to delegate loading to
051: * @param fetch the fetch configuration, or null for the default
052: */
053: public SelectResultObjectProvider(SelectExecutor sel,
054: JDBCStore store, JDBCFetchConfiguration fetch) {
055: _sel = sel;
056: _store = store;
057: _fetch = fetch;
058: }
059:
060: public SelectExecutor getSelect() {
061: return _sel;
062: }
063:
064: public JDBCStore getStore() {
065: return _store;
066: }
067:
068: public JDBCFetchConfiguration getFetchConfiguration() {
069: return _fetch;
070: }
071:
072: public Result getResult() {
073: return _res;
074: }
075:
076: public boolean supportsRandomAccess() {
077: if (_ra == null) {
078: boolean ra;
079: if (_res != null) {
080: try {
081: ra = _res.supportsRandomAccess();
082: } catch (SQLException se) {
083: throw SQLExceptions.getStore(se, _store
084: .getDBDictionary());
085: }
086: } else
087: ra = _sel.supportsRandomAccess(_fetch
088: .getReadLockLevel() > 0);
089: _ra = (ra) ? Boolean.TRUE : Boolean.FALSE;
090: }
091: return _ra.booleanValue();
092: }
093:
094: public void open() throws SQLException {
095: _res = _sel.execute(_store, _fetch);
096: }
097:
098: public boolean next() throws SQLException {
099: return _res.next();
100: }
101:
102: public boolean absolute(int pos) throws SQLException {
103: return _res.absolute(pos);
104: }
105:
106: public int size() throws SQLException {
107: if (_size == -1) {
108: // if res is null, don't cache size
109: if (_res == null)
110: return Integer.MAX_VALUE;
111:
112: switch (_fetch.getLRSSize()) {
113: case LRSSizes.SIZE_UNKNOWN:
114: _size = Integer.MAX_VALUE;
115: break;
116: case LRSSizes.SIZE_LAST:
117: if (supportsRandomAccess())
118: _size = _res.size();
119: else
120: _size = Integer.MAX_VALUE;
121: break;
122: default: // query
123: _size = _sel.getCount(_store);
124: }
125: }
126: return _size;
127: }
128:
129: /**
130: * Allow subclasses that know the size to set it; otherwise we calculate
131: * it internally.
132: */
133: protected void setSize(int size) {
134: if (_size == -1)
135: _size = size;
136: }
137:
138: public void reset() throws SQLException {
139: close();
140: open();
141: }
142:
143: public void close() {
144: if (_res != null) {
145: _res.close();
146: _res = null;
147: }
148: }
149:
150: public void handleCheckedException(Exception e) {
151: if (e instanceof SQLException)
152: throw SQLExceptions.getStore((SQLException) e, _store
153: .getDBDictionary());
154: throw new StoreException(e);
155: }
156: }
|