KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > dbcp > TesterStatement


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.dbcp;
18
19 import java.sql.Connection JavaDoc;
20 import java.sql.ResultSet JavaDoc;
21 import java.sql.SQLException JavaDoc;
22 import java.sql.SQLWarning JavaDoc;
23 import java.sql.Statement JavaDoc;
24
25 /**
26  * A dummy {@link Statement}, for testing purposes.
27  *
28  * @author Rodney Waldhoff
29  * @author Dirk Verbeeck
30  * @version $Revision: 1.15 $ $Date: 2004/03/07 10:54:55 $
31  */

32 public class TesterStatement implements Statement JavaDoc {
33     public TesterStatement(Connection JavaDoc conn) {
34         _connection = conn;
35     }
36
37     public TesterStatement(Connection JavaDoc conn, int resultSetType, int resultSetConcurrency) {
38         _connection = conn;
39         _resultSetType = resultSetType;
40         _resultSetConcurrency = resultSetConcurrency;
41     }
42     
43     protected Connection JavaDoc _connection = null;
44     protected boolean _open = true;
45     protected int _rowsUpdated = 1;
46     protected boolean _executeResponse = true;
47     protected int _maxFieldSize = 1024;
48     protected int _maxRows = 1024;
49     protected boolean _escapeProcessing = false;
50     protected int _queryTimeout = 1000;
51     protected String JavaDoc _cursorName = null;
52     protected int _fetchDirection = 1;
53     protected int _fetchSize = 1;
54     protected int _resultSetConcurrency = 1;
55     protected int _resultSetType = 1;
56     protected ResultSet JavaDoc _resultSet = null;
57
58     public ResultSet JavaDoc executeQuery(String JavaDoc sql) throws SQLException JavaDoc {
59         checkOpen();
60         if("null".equals(sql)) {
61             return null;
62         } if("invalid".equals(sql)) {
63             throw new SQLException JavaDoc("invalid query");
64         } if ("broken".equals(sql)) {
65             throw new SQLException JavaDoc("broken connection");
66         } if("select username".equals(sql)) {
67             String JavaDoc username = ((TesterConnection) _connection).getUsername();
68             Object JavaDoc[][] data = {{username}};
69             return new TesterResultSet(this, data);
70         } else {
71             return new TesterResultSet(this);
72         }
73     }
74
75     public int executeUpdate(String JavaDoc sql) throws SQLException JavaDoc {
76         checkOpen();
77         return _rowsUpdated;
78     }
79
80     public void close() throws SQLException JavaDoc {
81         checkOpen();
82         _open = false;
83         if (_resultSet != null) {
84             _resultSet.close();
85             _resultSet = null;
86         }
87     }
88
89     public int getMaxFieldSize() throws SQLException JavaDoc {
90         checkOpen();
91         return _maxFieldSize;
92     }
93
94     public void setMaxFieldSize(int max) throws SQLException JavaDoc {
95         checkOpen();
96         _maxFieldSize = max;
97     }
98
99     public int getMaxRows() throws SQLException JavaDoc {
100         checkOpen();
101         return _maxRows;
102     }
103
104     public void setMaxRows(int max) throws SQLException JavaDoc {
105         checkOpen();
106         _maxRows = max;
107     }
108
109     public void setEscapeProcessing(boolean enable) throws SQLException JavaDoc {
110         checkOpen();
111         _escapeProcessing = enable;
112     }
113
114     public int getQueryTimeout() throws SQLException JavaDoc {
115         checkOpen();
116         return _queryTimeout;
117     }
118
119     public void setQueryTimeout(int seconds) throws SQLException JavaDoc {
120         checkOpen();
121         _queryTimeout = seconds;
122     }
123
124     public void cancel() throws SQLException JavaDoc {
125         checkOpen();
126     }
127
128     public SQLWarning JavaDoc getWarnings() throws SQLException JavaDoc {
129         checkOpen();
130         return null;
131     }
132
133     public void clearWarnings() throws SQLException JavaDoc {
134         checkOpen();
135     }
136
137     public void setCursorName(String JavaDoc name) throws SQLException JavaDoc {
138         checkOpen();
139         _cursorName = name;
140     }
141
142     public boolean execute(String JavaDoc sql) throws SQLException JavaDoc {
143         checkOpen();
144         return _executeResponse;
145     }
146
147     public ResultSet JavaDoc getResultSet() throws SQLException JavaDoc {
148         checkOpen();
149         if (_resultSet == null) {
150             _resultSet = new TesterResultSet(this);
151         }
152         return _resultSet;
153     }
154
155     public int getUpdateCount() throws SQLException JavaDoc {
156         checkOpen();
157         return _rowsUpdated;
158     }
159
160     public boolean getMoreResults() throws SQLException JavaDoc {
161         checkOpen();
162         return false;
163     }
164
165     public void setFetchDirection(int direction) throws SQLException JavaDoc {
166         checkOpen();
167         _fetchDirection = direction;
168     }
169
170     public int getFetchDirection() throws SQLException JavaDoc {
171         checkOpen();
172         return _fetchDirection;
173     }
174
175     public void setFetchSize(int rows) throws SQLException JavaDoc {
176         checkOpen();
177         _fetchSize = rows;
178     }
179
180     public int getFetchSize() throws SQLException JavaDoc {
181         checkOpen();
182         return _fetchSize;
183     }
184
185     public int getResultSetConcurrency() throws SQLException JavaDoc {
186         checkOpen();
187         return _resultSetConcurrency;
188     }
189
190     public int getResultSetType() throws SQLException JavaDoc {
191         checkOpen();
192         return _resultSetType;
193     }
194
195     public void addBatch(String JavaDoc sql) throws SQLException JavaDoc {
196         checkOpen();
197     }
198
199     public void clearBatch() throws SQLException JavaDoc {
200         checkOpen();
201     }
202
203     public int[] executeBatch() throws SQLException JavaDoc {
204         checkOpen();
205         return new int[0];
206     }
207
208     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
209         checkOpen();
210         return _connection;
211     }
212
213     protected void checkOpen() throws SQLException JavaDoc {
214         if(!_open) {
215             throw new SQLException JavaDoc("Connection is closed.");
216         }
217     }
218
219     // ------------------- JDBC 3.0 -----------------------------------------
220
// Will be commented by the build process on a JDBC 2.0 system
221

222 /* JDBC_3_ANT_KEY_BEGIN */
223     public boolean getMoreResults(int current) throws SQLException JavaDoc {
224         throw new SQLException JavaDoc("Not implemented.");
225     }
226
227     public ResultSet JavaDoc getGeneratedKeys() throws SQLException JavaDoc {
228         throw new SQLException JavaDoc("Not implemented.");
229     }
230
231     public int executeUpdate(String JavaDoc sql, int autoGeneratedKeys)
232         throws SQLException JavaDoc {
233         throw new SQLException JavaDoc("Not implemented.");
234     }
235
236     public int executeUpdate(String JavaDoc sql, int columnIndexes[])
237         throws SQLException JavaDoc {
238         throw new SQLException JavaDoc("Not implemented.");
239     }
240
241     public int executeUpdate(String JavaDoc sql, String JavaDoc columnNames[])
242         throws SQLException JavaDoc {
243         throw new SQLException JavaDoc("Not implemented.");
244     }
245
246     public boolean execute(String JavaDoc sql, int autoGeneratedKeys)
247         throws SQLException JavaDoc {
248         throw new SQLException JavaDoc("Not implemented.");
249     }
250
251     public boolean execute(String JavaDoc sql, int columnIndexes[])
252         throws SQLException JavaDoc {
253         throw new SQLException JavaDoc("Not implemented.");
254     }
255
256     public boolean execute(String JavaDoc sql, String JavaDoc columnNames[])
257         throws SQLException JavaDoc {
258         throw new SQLException JavaDoc("Not implemented.");
259     }
260
261     public int getResultSetHoldability() throws SQLException JavaDoc {
262         checkOpen();
263         throw new SQLException JavaDoc("Not implemented.");
264     }
265 /* JDBC_3_ANT_KEY_END */
266
267 }
268
Popular Tags