001: /*
002: * (c) Copyright 2007 by Volker Bergmann. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, is permitted under the terms of the
006: * GNU General Public License.
007: *
008: * For redistributing this software or a derivative work under a license other
009: * than the GPL-compatible Free Software License as defined by the Free
010: * Software Foundation or approved by OSI, you must first obtain a commercial
011: * license to this software product from Volker Bergmann.
012: *
013: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
014: * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
015: * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
016: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
017: * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
018: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
019: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
020: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
021: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
022: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
023: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
024: * POSSIBILITY OF SUCH DAMAGE.
025: */
026:
027: package org.databene.platform.db;
028:
029: import org.databene.commons.HeavyweightIterator;
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032:
033: import java.sql.ResultSet;
034: import java.sql.SQLException;
035: import java.sql.Statement;
036:
037: /**
038: * Wraps a ResultSet into the semantic of a heavyweight iterator.
039: * @see HeavyweightIterator<br/>
040: * <br/>
041: * Created: 15.08.2007 18:19:25
042: */
043: public class ResultSetIterator implements
044: HeavyweightIterator<ResultSet> {
045:
046: private static final Log logger = LogFactory
047: .getLog(ResultSetIterator.class);
048:
049: private String query;
050:
051: private ResultSet resultSet;
052: private Boolean hasNext;
053:
054: public ResultSetIterator(ResultSet resultSet, String query) {
055: this .resultSet = resultSet;
056: this .hasNext = null;
057: this .query = query;
058: }
059:
060: // interface -------------------------------------------------------------------------------------------------------
061:
062: public boolean hasNext() {
063: if (logger.isDebugEnabled())
064: logger.debug("hasNext() called on: " + this );
065: if (hasNext != null)
066: return hasNext;
067: if (resultSet == null)
068: return false;
069: try {
070: hasNext = resultSet.next();
071: if (!hasNext)
072: close();
073: return hasNext;
074: } catch (SQLException e) {
075: throw new RuntimeException("Error in query: " + query, e);
076: }
077: }
078:
079: public ResultSet next() {
080: if (logger.isDebugEnabled())
081: logger.debug("next() called on: " + this );
082: try {
083: if (!hasNext())
084: throw new IllegalStateException("No more row available");
085: return resultSet;
086: } finally {
087: hasNext = null;
088: }
089: }
090:
091: public void remove() {
092: throw new UnsupportedOperationException("Not supported");
093: }
094:
095: public synchronized void close() {
096: if (logger.isDebugEnabled())
097: logger.debug("closing " + this );
098: hasNext = false;
099: if (resultSet == null)
100: return;
101: try {
102: Statement statement = resultSet.getStatement();
103: DBUtil.close(resultSet);
104: resultSet = null;
105: DBUtil.close(statement);
106: statement = null;
107: } catch (SQLException e) {
108: logger.error(e, e);
109: }
110: }
111:
112: // java.lang.Object overrides --------------------------------------------------------------------------------------
113:
114: public String toString() {
115: return getClass().getSimpleName() + '[' + query + ']';
116: }
117: }
|