001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.dbcp;
019:
020: import java.sql.Connection;
021: import java.sql.ResultSet;
022: import java.sql.SQLException;
023: import java.sql.SQLWarning;
024: import java.sql.Statement;
025:
026: /**
027: * A dummy {@link Statement}, for testing purposes.
028: *
029: * @author Rodney Waldhoff
030: * @author Dirk Verbeeck
031: * @version $Revision: 479137 $ $Date: 2006-11-25 08:51:48 -0700 (Sat, 25 Nov 2006) $
032: */
033: public class TesterStatement implements Statement {
034: public TesterStatement(Connection conn) {
035: _connection = conn;
036: }
037:
038: public TesterStatement(Connection conn, int resultSetType,
039: int resultSetConcurrency) {
040: _connection = conn;
041: _resultSetType = resultSetType;
042: _resultSetConcurrency = resultSetConcurrency;
043: }
044:
045: protected Connection _connection = null;
046: protected boolean _open = true;
047: protected int _rowsUpdated = 1;
048: protected boolean _executeResponse = true;
049: protected int _maxFieldSize = 1024;
050: protected int _maxRows = 1024;
051: protected boolean _escapeProcessing = false;
052: protected int _queryTimeout = 1000;
053: protected String _cursorName = null;
054: protected int _fetchDirection = 1;
055: protected int _fetchSize = 1;
056: protected int _resultSetConcurrency = 1;
057: protected int _resultSetType = 1;
058: protected ResultSet _resultSet = null;
059:
060: public ResultSet executeQuery(String sql) throws SQLException {
061: checkOpen();
062: if ("null".equals(sql)) {
063: return null;
064: }
065: if ("invalid".equals(sql)) {
066: throw new SQLException("invalid query");
067: }
068: if ("broken".equals(sql)) {
069: throw new SQLException("broken connection");
070: }
071: if ("select username".equals(sql)) {
072: String username = ((TesterConnection) _connection)
073: .getUsername();
074: Object[][] data = { { username } };
075: return new TesterResultSet(this , data);
076: } else {
077: return new TesterResultSet(this );
078: }
079: }
080:
081: public int executeUpdate(String sql) throws SQLException {
082: checkOpen();
083: return _rowsUpdated;
084: }
085:
086: public void close() throws SQLException {
087: checkOpen();
088: _open = false;
089: if (_resultSet != null) {
090: _resultSet.close();
091: _resultSet = null;
092: }
093: }
094:
095: public int getMaxFieldSize() throws SQLException {
096: checkOpen();
097: return _maxFieldSize;
098: }
099:
100: public void setMaxFieldSize(int max) throws SQLException {
101: checkOpen();
102: _maxFieldSize = max;
103: }
104:
105: public int getMaxRows() throws SQLException {
106: checkOpen();
107: return _maxRows;
108: }
109:
110: public void setMaxRows(int max) throws SQLException {
111: checkOpen();
112: _maxRows = max;
113: }
114:
115: public void setEscapeProcessing(boolean enable) throws SQLException {
116: checkOpen();
117: _escapeProcessing = enable;
118: }
119:
120: public int getQueryTimeout() throws SQLException {
121: checkOpen();
122: return _queryTimeout;
123: }
124:
125: public void setQueryTimeout(int seconds) throws SQLException {
126: checkOpen();
127: _queryTimeout = seconds;
128: }
129:
130: public void cancel() throws SQLException {
131: checkOpen();
132: }
133:
134: public SQLWarning getWarnings() throws SQLException {
135: checkOpen();
136: return null;
137: }
138:
139: public void clearWarnings() throws SQLException {
140: checkOpen();
141: }
142:
143: public void setCursorName(String name) throws SQLException {
144: checkOpen();
145: _cursorName = name;
146: }
147:
148: public boolean execute(String sql) throws SQLException {
149: checkOpen();
150: return _executeResponse;
151: }
152:
153: public ResultSet getResultSet() throws SQLException {
154: checkOpen();
155: if (_resultSet == null) {
156: _resultSet = new TesterResultSet(this );
157: }
158: return _resultSet;
159: }
160:
161: public int getUpdateCount() throws SQLException {
162: checkOpen();
163: return _rowsUpdated;
164: }
165:
166: public boolean getMoreResults() throws SQLException {
167: checkOpen();
168: return false;
169: }
170:
171: public void setFetchDirection(int direction) throws SQLException {
172: checkOpen();
173: _fetchDirection = direction;
174: }
175:
176: public int getFetchDirection() throws SQLException {
177: checkOpen();
178: return _fetchDirection;
179: }
180:
181: public void setFetchSize(int rows) throws SQLException {
182: checkOpen();
183: _fetchSize = rows;
184: }
185:
186: public int getFetchSize() throws SQLException {
187: checkOpen();
188: return _fetchSize;
189: }
190:
191: public int getResultSetConcurrency() throws SQLException {
192: checkOpen();
193: return _resultSetConcurrency;
194: }
195:
196: public int getResultSetType() throws SQLException {
197: checkOpen();
198: return _resultSetType;
199: }
200:
201: public void addBatch(String sql) throws SQLException {
202: checkOpen();
203: }
204:
205: public void clearBatch() throws SQLException {
206: checkOpen();
207: }
208:
209: public int[] executeBatch() throws SQLException {
210: checkOpen();
211: return new int[0];
212: }
213:
214: public Connection getConnection() throws SQLException {
215: checkOpen();
216: return _connection;
217: }
218:
219: protected void checkOpen() throws SQLException {
220: if (!_open) {
221: throw new SQLException("Connection is closed.");
222: }
223: }
224:
225: // ------------------- JDBC 3.0 -----------------------------------------
226: // Will be commented by the build process on a JDBC 2.0 system
227:
228: /* JDBC_3_ANT_KEY_BEGIN */
229: public boolean getMoreResults(int current) throws SQLException {
230: throw new SQLException("Not implemented.");
231: }
232:
233: public ResultSet getGeneratedKeys() throws SQLException {
234: throw new SQLException("Not implemented.");
235: }
236:
237: public int executeUpdate(String sql, int autoGeneratedKeys)
238: throws SQLException {
239: throw new SQLException("Not implemented.");
240: }
241:
242: public int executeUpdate(String sql, int columnIndexes[])
243: throws SQLException {
244: throw new SQLException("Not implemented.");
245: }
246:
247: public int executeUpdate(String sql, String columnNames[])
248: throws SQLException {
249: throw new SQLException("Not implemented.");
250: }
251:
252: public boolean execute(String sql, int autoGeneratedKeys)
253: throws SQLException {
254: throw new SQLException("Not implemented.");
255: }
256:
257: public boolean execute(String sql, int columnIndexes[])
258: throws SQLException {
259: throw new SQLException("Not implemented.");
260: }
261:
262: public boolean execute(String sql, String columnNames[])
263: throws SQLException {
264: throw new SQLException("Not implemented.");
265: }
266:
267: public int getResultSetHoldability() throws SQLException {
268: checkOpen();
269: throw new SQLException("Not implemented.");
270: }
271: /* JDBC_3_ANT_KEY_END */
272:
273: }
|