001: /*
002: * $Id: BaseAxionStatement.java,v 1.10 2005/12/20 18:32:27 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2005 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.jdbc;
042:
043: import java.sql.Connection;
044: import java.sql.ResultSet;
045: import java.sql.SQLException;
046: import java.sql.SQLWarning;
047: import java.sql.Statement;
048: import java.util.ArrayList;
049: import java.util.Collections;
050: import java.util.Iterator;
051:
052: import org.axiondb.AxionCommand;
053: import org.axiondb.AxionException;
054: import org.axiondb.Database;
055: import org.axiondb.parser.AxionSqlParser;
056: import org.axiondb.util.ExceptionConverter;
057:
058: /**
059: * Abstract base {@link Statement}implementation.
060: *
061: * @version $Revision: 1.10 $ $Date: 2005/12/20 18:32:27 $
062: * @author Chuck Burdick
063: * @author Rodney Waldhoff
064: * @author Jonathan Giron
065: * @author Ahimanikya Satapathy
066: */
067: public abstract class BaseAxionStatement implements Statement {
068:
069: protected BaseAxionStatement(AxionConnection conn)
070: throws SQLException {
071: _conn = conn;
072: _parser = new AxionSqlParser();
073: }
074:
075: // ------------------------------------------------------------------------
076:
077: public void close() throws SQLException {
078: // Per JDBC API subsequent calls to close() on an already-closed Statement should
079: // be silently ignored.
080: if (!_closed) {
081: closeCurrentResultSet();
082: clearConnection();
083:
084: clearBatchContext();
085: _batchContext = null;
086: _closed = true;
087: }
088: }
089:
090: public Connection getConnection() throws SQLException {
091: return _conn;
092: }
093:
094: public int getMaxRows() throws SQLException {
095: return _maxRows;
096: }
097:
098: public void setMaxRows(int max) throws SQLException {
099: if (max < 0) {
100: throw new SQLException("MaxRows should be non-negative");
101: }
102: _maxRows = max;
103: }
104:
105: // ------------------------------------------------------------------------
106:
107: protected final void assertOpen() throws SQLException {
108: if (_closed) {
109: throw new SQLException("Already closed.");
110: }
111: }
112:
113: protected final void clearConnection() {
114: _conn = null;
115: }
116:
117: protected void clearCurrentResult() throws SQLException {
118: clearCurrentUpdateCount();
119: closeCurrentResultSet();
120: }
121:
122: protected int clearCurrentUpdateCount() {
123: int count = getCurrentUpdateCount();
124: setCurrentUpdateCount(-1);
125: return count;
126: }
127:
128: protected void closeCurrentResultSet() throws SQLException {
129: try {
130: if (null != _rset) {
131: _rset.close();
132: }
133: } finally {
134: _rset = null;
135: }
136: }
137:
138: protected final AxionConnection getAxionConnection()
139: throws SQLException {
140: return (AxionConnection) (getConnection());
141: }
142:
143: protected final AxionResultSet getCurrentResultSet() {
144: return _rset;
145: }
146:
147: protected final int getCurrentUpdateCount() {
148: return _updateCount;
149: }
150:
151: protected final Database getDatabase() throws AxionException {
152: return _conn.getCurrentTransaction();
153: }
154:
155: protected final boolean hasCurrentResultSet() {
156: return (null != _rset);
157: }
158:
159: public AxionCommand parseCommand(String sql) throws SQLException {
160: try {
161: return _parser.parse(sql);
162: } catch (AxionException e) {
163: throw ExceptionConverter.convert(e);
164: } catch (RuntimeException e) {
165: throw ExceptionConverter.convert(e);
166: }
167: }
168:
169: protected void setCurrentResult(boolean isrset, AxionCommand cmd) {
170: if (isrset) {
171: setCurrentResultSet((AxionResultSet) cmd.getResultSet());
172: } else {
173: setCurrentUpdateCount(cmd.getEffectedRowCount());
174: }
175: }
176:
177: /**
178: * @param rset the non- <code>null</code> instance to set current {@link ResultSet}
179: * to
180: * @see #clearCurrentResult
181: */
182: protected void setCurrentResultSet(AxionResultSet rset) {
183: rset.setMaxRows(_maxRows);
184: rset.setStatement(this );
185: _rset = rset;
186: }
187:
188: protected final void setCurrentUpdateCount(int count) {
189: _updateCount = count;
190: }
191:
192: protected final void addToBatchContext(Object obj)
193: throws SQLException {
194: if (_batchContext == null) {
195: _batchContext = new ArrayList();
196: }
197: _batchContext.add(obj);
198: }
199:
200: protected final void clearBatchContext() {
201: if (_batchContext != null) {
202: _batchContext.clear();
203: }
204: }
205:
206: protected final Iterator getBatchContext() {
207: if (_batchContext != null) {
208: return _batchContext.iterator();
209: } else {
210: return Collections.EMPTY_LIST.iterator();
211: }
212: }
213:
214: protected final int getBatchCount() {
215: if (_batchContext != null) {
216: return _batchContext.size();
217: } else {
218: return 0;
219: }
220: }
221:
222: // ------------------------------------------------------------------------
223:
224: protected ArrayList _batchContext;
225: private AxionSqlParser _parser;
226:
227: protected SQLWarning _warning;
228: private boolean _closed = false;
229: private AxionConnection _conn;
230:
231: private int _maxRows = 0;
232:
233: private AxionResultSet _rset;
234: private int _updateCount = -1;
235: }
|