001: package net.sourceforge.orbroker;
002:
003: import java.sql.Connection;
004: import java.sql.ResultSet;
005: import java.sql.SQLException;
006: import java.util.logging.Level;
007:
008: /*
009: * Created on May 18, 2004
010: *
011: */
012:
013: /**
014: * A container for Statement and ResultSet.
015: *
016: * @author Nils Kilden-Pedersen
017: */
018: final class QueryResult {
019: private final Connection connection;
020: private final ConnectionContext context;
021: private final ResultSet outputParameters;
022: private final ResultSet resultSet;
023: private final java.sql.Statement sqlStatement;
024:
025: QueryResult(java.sql.Statement stm, ResultSet resultSet,
026: ConnectionContext context) throws SQLException {
027: this (stm, resultSet, context, null);
028: }
029:
030: QueryResult(java.sql.Statement stm, ResultSet resultSet,
031: ConnectionContext context, ResultSet outputParameters)
032: throws SQLException {
033: this .sqlStatement = stm;
034: this .resultSet = resultSet;
035: this .context = context;
036: this .outputParameters = outputParameters;
037: this .connection = stm.getConnection();
038: }
039:
040: /**
041: * @param context
042: * @param cs
043: */
044: private void setOutputParameters() {
045: if (this .outputParameters != null) {
046: ResultRow row = new ResultRow(this .outputParameters,
047: this .context, this .connection);
048: Statement.setValuesOnMap(this .context.getParametersAsMap(),
049: row);
050: }
051: }
052:
053: void close() {
054: try {
055: this .resultSet.close();
056: } catch (SQLException e) {
057: Broker.log(Level.WARNING, e.toString());
058: }
059:
060: setOutputParameters();
061:
062: try {
063: this .sqlStatement.close();
064: } catch (SQLException e) {
065: Broker.log(Level.WARNING, e.toString());
066: }
067: }
068:
069: ResultRow getResultRow() {
070: return new ResultRow(this .resultSet, this .context,
071: this .connection);
072: }
073:
074: /**
075: * Get result row positioned at the given start position.
076: * @param startRow
077: * row to position to.
078: * @param scrollable
079: * true, if result set is scrollable
080: * @return Positioned result row
081: * @throws QueryException
082: */
083: ResultRow getResultRow(int startRow, boolean scrollable)
084: throws QueryException {
085: if (startRow > 1) {
086: try {
087: if (scrollable) {
088: this .resultSet.absolute(startRow - 1);
089: } else {
090: setFetchSize(startRow);
091: int currentPosition = 1;
092: while (currentPosition < startRow) {
093: if (!this .resultSet.next()) {
094: break;
095: }
096: currentPosition++;
097: }
098: }
099: } catch (SQLException e) {
100: throw new QueryException(e);
101: }
102: }
103:
104: return getResultRow();
105: }
106:
107: boolean nextRow() throws QueryException {
108: try {
109: return this .resultSet.next();
110: } catch (SQLException e) {
111: throw new QueryException(e);
112: }
113: }
114:
115: void setFetchSize(int rows) {
116: try {
117: this .resultSet.setFetchSize(rows);
118: } catch (SQLException e) {
119: Broker.log(Level.WARNING, e.toString());
120: }
121: }
122: }
|