1 16 17 package org.apache.commons.dbcp; 18 19 import java.sql.CallableStatement ; 20 import java.sql.Connection ; 21 import java.sql.DatabaseMetaData ; 22 import java.sql.PreparedStatement ; 23 import java.sql.SQLException ; 24 import java.sql.SQLWarning ; 25 import java.sql.Statement ; 26 import java.util.Map ; 27 28 35 public class TesterConnection implements Connection { 36 protected boolean _open = true; 37 protected boolean _autoCommit = true; 38 protected int _transactionIsolation = 1; 39 protected DatabaseMetaData _metaData = null; 40 protected String _catalog = null; 41 protected Map _typeMap = null; 42 protected boolean _readOnly = false; 43 protected SQLWarning warnings = null; 44 protected String username = null; 45 protected String password = null; 46 protected Exception failure; 47 48 public TesterConnection(String username, String password) { 49 this.username = username; 50 this.password = password; 51 } 52 53 public String getUsername() { 54 return this.username; 55 } 56 57 public void setWarnings(SQLWarning warning) { 58 this.warnings = warning; 59 } 60 61 public void clearWarnings() throws SQLException { 62 checkOpen(); 63 warnings = null; 64 } 65 66 public void close() throws SQLException { 67 checkOpen(); 68 _open = false; 69 } 70 71 public void commit() throws SQLException { 72 checkOpen(); 73 if (isReadOnly()) { 74 throw new SQLException ("Cannot commit a readonly connection"); 75 } 76 } 77 78 public Statement createStatement() throws SQLException { 79 checkOpen(); 80 return new TesterStatement(this); 81 } 82 83 public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { 84 checkOpen(); 85 return new TesterStatement(this); 86 } 87 88 public boolean getAutoCommit() throws SQLException { 89 checkOpen(); 90 return _autoCommit; 91 } 92 93 public String getCatalog() throws SQLException { 94 checkOpen(); 95 return _catalog; 96 } 97 98 public DatabaseMetaData getMetaData() throws SQLException { 99 checkOpen(); 100 return _metaData; 101 } 102 103 public int getTransactionIsolation() throws SQLException { 104 checkOpen(); 105 return _transactionIsolation; 106 } 107 108 public Map getTypeMap() throws SQLException { 109 checkOpen(); 110 return _typeMap; 111 } 112 113 public SQLWarning getWarnings() throws SQLException { 114 checkOpen(); 115 return warnings; 116 } 117 118 public boolean isClosed() throws SQLException { 119 checkFailure(); 120 return !_open; 121 } 122 123 public boolean isReadOnly() throws SQLException { 124 checkOpen(); 125 return _readOnly; 126 } 127 128 public String nativeSQL(String sql) throws SQLException { 129 checkOpen(); 130 return sql; 131 } 132 133 public CallableStatement prepareCall(String sql) throws SQLException { 134 checkOpen(); 135 if ("warning".equals(sql)) { 136 setWarnings(new SQLWarning ("warning in prepareCall")); 137 } 138 return null; 139 } 140 141 public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { 142 checkOpen(); 143 return null; 144 } 145 146 public PreparedStatement prepareStatement(String sql) throws SQLException { 147 checkOpen(); 148 if("null".equals(sql)) { 149 return null; 150 } if("invalid".equals(sql)) { 151 throw new SQLException ("invalid query"); 152 } if ("broken".equals(sql)) { 153 throw new SQLException ("broken connection"); 154 } 155 return new TesterPreparedStatement(this, sql); 156 } 157 158 public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { 159 checkOpen(); 160 return new TesterPreparedStatement(this, sql, resultSetType, resultSetConcurrency); 161 } 162 163 public void rollback() throws SQLException { 164 checkOpen(); 165 if (isReadOnly()) { 166 throw new SQLException ("Cannot rollback a readonly connection"); 167 } 168 } 169 170 public void setAutoCommit(boolean autoCommit) throws SQLException { 171 checkOpen(); 172 _autoCommit = autoCommit; 173 } 174 175 public void setCatalog(String catalog) throws SQLException { 176 checkOpen(); 177 _catalog = catalog; 178 } 179 180 public void setReadOnly(boolean readOnly) throws SQLException { 181 checkOpen(); 182 _readOnly = readOnly; 183 } 184 185 public void setTransactionIsolation(int level) throws SQLException { 186 checkOpen(); 187 _transactionIsolation = level; 188 } 189 190 public void setTypeMap(Map map) throws SQLException { 191 checkOpen(); 192 _typeMap = map; 193 } 194 195 protected void checkOpen() throws SQLException { 196 if(!_open) { 197 throw new SQLException ("Connection is closed."); 198 } 199 checkFailure(); 200 } 201 202 protected void checkFailure() throws SQLException { 203 if (failure != null) { 204 throw new SQLNestedException("TesterConnection failure", failure); 205 } 206 } 207 208 public void setFailure(Exception failure) { 209 this.failure = failure; 210 } 211 212 215 216 217 public int getHoldability() throws SQLException { 218 throw new SQLException ("Not implemented."); 219 } 220 221 public void setHoldability(int holdability) throws SQLException { 222 throw new SQLException ("Not implemented."); 223 } 224 225 public java.sql.Savepoint setSavepoint() throws SQLException { 226 throw new SQLException ("Not implemented."); 227 } 228 229 public java.sql.Savepoint setSavepoint(String name) throws SQLException { 230 throw new SQLException ("Not implemented."); 231 } 232 233 public void rollback(java.sql.Savepoint savepoint) throws SQLException { 234 throw new SQLException ("Not implemented."); 235 } 236 237 public void releaseSavepoint(java.sql.Savepoint savepoint) throws SQLException { 238 throw new SQLException ("Not implemented."); 239 } 240 241 public Statement createStatement(int resultSetType, 242 int resultSetConcurrency, 243 int resultSetHoldability) 244 throws SQLException { 245 throw new SQLException ("Not implemented."); 246 } 247 248 public PreparedStatement prepareStatement(String sql, int resultSetType, 249 int resultSetConcurrency, 250 int resultSetHoldability) 251 throws SQLException { 252 throw new SQLException ("Not implemented."); 253 } 254 255 public CallableStatement prepareCall(String sql, int resultSetType, 256 int resultSetConcurrency, 257 int resultSetHoldability) 258 throws SQLException { 259 throw new SQLException ("Not implemented."); 260 } 261 262 public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) 263 throws SQLException { 264 throw new SQLException ("Not implemented."); 265 } 266 267 public PreparedStatement prepareStatement(String sql, int columnIndexes[]) 268 throws SQLException { 269 throw new SQLException ("Not implemented."); 270 } 271 272 public PreparedStatement prepareStatement(String sql, String columnNames[]) 273 throws SQLException { 274 throw new SQLException ("Not implemented."); 275 } 276 277 278 } 279 | Popular Tags |