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.ResultSet;
022: import java.sql.ResultSetMetaData;
023: import java.sql.SQLException;
024:
025: import org.apache.openjpa.jdbc.meta.JavaSQLTypes;
026: import org.apache.openjpa.jdbc.sql.ResultSetResult;
027: import org.apache.openjpa.jdbc.sql.SQLExceptions;
028: import org.apache.openjpa.kernel.ResultPacker;
029: import org.apache.openjpa.lib.rop.ResultObjectProvider;
030: import org.apache.openjpa.util.StoreException;
031: import org.apache.openjpa.util.UnsupportedException;
032: import serp.util.Numbers;
033:
034: /**
035: * Provides all column data in a {@link ResultSet}.
036: *
037: * @author Abe White
038: */
039: class SQLProjectionResultObjectProvider implements ResultObjectProvider {
040:
041: private final JDBCStore _store;
042: private final JDBCFetchConfiguration _fetch;
043: private final ResultSetResult _res;
044: private final ResultPacker _packer;
045: private final int _cols;
046:
047: /**
048: * Constructor.
049: *
050: * @param res the result data
051: * @param cls the result class; may be null for the default
052: */
053: public SQLProjectionResultObjectProvider(JDBCStore store,
054: JDBCFetchConfiguration fetch, ResultSetResult res, Class cls)
055: throws SQLException {
056: _store = store;
057: _fetch = fetch;
058:
059: ResultSetMetaData meta = res.getResultSet().getMetaData();
060: _res = res;
061: _cols = meta.getColumnCount();
062:
063: if (cls != null) {
064: String[] aliases = new String[_cols];
065: for (int i = 0; i < _cols; i++)
066: aliases[i] = meta.getColumnLabel(i + 1);
067: _packer = new ResultPacker(null, aliases, cls);
068: } else
069: _packer = null;
070: }
071:
072: public boolean supportsRandomAccess() {
073: try {
074: return _res.supportsRandomAccess();
075: } catch (Throwable t) {
076: return false;
077: }
078: }
079:
080: public void open() {
081: }
082:
083: public Object getResultObject() throws SQLException {
084: if (_cols == 1) {
085: Object val = _res.getObject(Numbers.valueOf(1),
086: JavaSQLTypes.JDBC_DEFAULT, null);
087: return (_packer == null) ? val : _packer.pack(val);
088: }
089:
090: Object[] vals = new Object[_cols];
091: for (int i = 0; i < vals.length; i++)
092: vals[i] = _res.getObject(Numbers.valueOf(i + 1),
093: JavaSQLTypes.JDBC_DEFAULT, null);
094: return (_packer == null) ? vals : _packer.pack(vals);
095: }
096:
097: public boolean next() throws SQLException {
098: return _res.next();
099: }
100:
101: public boolean absolute(int pos) throws SQLException {
102: return _res.absolute(pos);
103: }
104:
105: public int size() throws SQLException {
106: if (_fetch.getLRSSize() == LRSSizes.SIZE_UNKNOWN
107: || !supportsRandomAccess())
108: return Integer.MAX_VALUE;
109: return _res.size();
110: }
111:
112: public void reset() {
113: throw new UnsupportedException();
114: }
115:
116: public void close() {
117: _res.close();
118: }
119:
120: public void handleCheckedException(Exception e) {
121: if (e instanceof SQLException)
122: throw SQLExceptions.getStore((SQLException) e, _store
123: .getDBDictionary());
124: throw new StoreException(e);
125: }
126: }
|