1 16 17 package org.apache.commons.dbcp; 18 19 import java.sql.Connection ; 20 import java.sql.ResultSet ; 21 import java.sql.SQLException ; 22 import java.sql.SQLWarning ; 23 import java.sql.Statement ; 24 25 32 public class TesterStatement implements Statement { 33 public TesterStatement(Connection conn) { 34 _connection = conn; 35 } 36 37 public TesterStatement(Connection conn, int resultSetType, int resultSetConcurrency) { 38 _connection = conn; 39 _resultSetType = resultSetType; 40 _resultSetConcurrency = resultSetConcurrency; 41 } 42 43 protected Connection _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 _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 _resultSet = null; 57 58 public ResultSet executeQuery(String sql) throws SQLException { 59 checkOpen(); 60 if("null".equals(sql)) { 61 return null; 62 } if("invalid".equals(sql)) { 63 throw new SQLException ("invalid query"); 64 } if ("broken".equals(sql)) { 65 throw new SQLException ("broken connection"); 66 } if("select username".equals(sql)) { 67 String username = ((TesterConnection) _connection).getUsername(); 68 Object [][] data = {{username}}; 69 return new TesterResultSet(this, data); 70 } else { 71 return new TesterResultSet(this); 72 } 73 } 74 75 public int executeUpdate(String sql) throws SQLException { 76 checkOpen(); 77 return _rowsUpdated; 78 } 79 80 public void close() throws SQLException { 81 checkOpen(); 82 _open = false; 83 if (_resultSet != null) { 84 _resultSet.close(); 85 _resultSet = null; 86 } 87 } 88 89 public int getMaxFieldSize() throws SQLException { 90 checkOpen(); 91 return _maxFieldSize; 92 } 93 94 public void setMaxFieldSize(int max) throws SQLException { 95 checkOpen(); 96 _maxFieldSize = max; 97 } 98 99 public int getMaxRows() throws SQLException { 100 checkOpen(); 101 return _maxRows; 102 } 103 104 public void setMaxRows(int max) throws SQLException { 105 checkOpen(); 106 _maxRows = max; 107 } 108 109 public void setEscapeProcessing(boolean enable) throws SQLException { 110 checkOpen(); 111 _escapeProcessing = enable; 112 } 113 114 public int getQueryTimeout() throws SQLException { 115 checkOpen(); 116 return _queryTimeout; 117 } 118 119 public void setQueryTimeout(int seconds) throws SQLException { 120 checkOpen(); 121 _queryTimeout = seconds; 122 } 123 124 public void cancel() throws SQLException { 125 checkOpen(); 126 } 127 128 public SQLWarning getWarnings() throws SQLException { 129 checkOpen(); 130 return null; 131 } 132 133 public void clearWarnings() throws SQLException { 134 checkOpen(); 135 } 136 137 public void setCursorName(String name) throws SQLException { 138 checkOpen(); 139 _cursorName = name; 140 } 141 142 public boolean execute(String sql) throws SQLException { 143 checkOpen(); 144 return _executeResponse; 145 } 146 147 public ResultSet getResultSet() throws SQLException { 148 checkOpen(); 149 if (_resultSet == null) { 150 _resultSet = new TesterResultSet(this); 151 } 152 return _resultSet; 153 } 154 155 public int getUpdateCount() throws SQLException { 156 checkOpen(); 157 return _rowsUpdated; 158 } 159 160 public boolean getMoreResults() throws SQLException { 161 checkOpen(); 162 return false; 163 } 164 165 public void setFetchDirection(int direction) throws SQLException { 166 checkOpen(); 167 _fetchDirection = direction; 168 } 169 170 public int getFetchDirection() throws SQLException { 171 checkOpen(); 172 return _fetchDirection; 173 } 174 175 public void setFetchSize(int rows) throws SQLException { 176 checkOpen(); 177 _fetchSize = rows; 178 } 179 180 public int getFetchSize() throws SQLException { 181 checkOpen(); 182 return _fetchSize; 183 } 184 185 public int getResultSetConcurrency() throws SQLException { 186 checkOpen(); 187 return _resultSetConcurrency; 188 } 189 190 public int getResultSetType() throws SQLException { 191 checkOpen(); 192 return _resultSetType; 193 } 194 195 public void addBatch(String sql) throws SQLException { 196 checkOpen(); 197 } 198 199 public void clearBatch() throws SQLException { 200 checkOpen(); 201 } 202 203 public int[] executeBatch() throws SQLException { 204 checkOpen(); 205 return new int[0]; 206 } 207 208 public Connection getConnection() throws SQLException { 209 checkOpen(); 210 return _connection; 211 } 212 213 protected void checkOpen() throws SQLException { 214 if(!_open) { 215 throw new SQLException ("Connection is closed."); 216 } 217 } 218 219 222 223 public boolean getMoreResults(int current) throws SQLException { 224 throw new SQLException ("Not implemented."); 225 } 226 227 public ResultSet getGeneratedKeys() throws SQLException { 228 throw new SQLException ("Not implemented."); 229 } 230 231 public int executeUpdate(String sql, int autoGeneratedKeys) 232 throws SQLException { 233 throw new SQLException ("Not implemented."); 234 } 235 236 public int executeUpdate(String sql, int columnIndexes[]) 237 throws SQLException { 238 throw new SQLException ("Not implemented."); 239 } 240 241 public int executeUpdate(String sql, String columnNames[]) 242 throws SQLException { 243 throw new SQLException ("Not implemented."); 244 } 245 246 public boolean execute(String sql, int autoGeneratedKeys) 247 throws SQLException { 248 throw new SQLException ("Not implemented."); 249 } 250 251 public boolean execute(String sql, int columnIndexes[]) 252 throws SQLException { 253 throw new SQLException ("Not implemented."); 254 } 255 256 public boolean execute(String sql, String columnNames[]) 257 throws SQLException { 258 throw new SQLException ("Not implemented."); 259 } 260 261 public int getResultSetHoldability() throws SQLException { 262 checkOpen(); 263 throw new SQLException ("Not implemented."); 264 } 265 266 267 } 268 | Popular Tags |