View Javadoc

1   /*
2    * $Source: /usr/cvsroot/melati/throwing-jdbc/src/main/java/org/melati/poem/dbms/test/sql/ThrowingConnectionJdbc3.java,v $
3    * $Revision: 1.4 $
4    *
5    * Copyright (C) 2008 Tim Pizey
6    *
7    * Part of Melati (http://melati.org), a framework for the rapid
8    * development of clean, maintainable web applications.
9    *
10   * Melati is free software; Permission is granted to copy, distribute
11   * and/or modify this software under the terms either:
12   *
13   * a) the GNU General Public License as published by the Free Software
14   *    Foundation; either version 2 of the License, or (at your option)
15   *    any later version,
16   *
17   *    or
18   *
19   * b) any version of the Melati Software License, as published
20   *    at http://melati.org
21   *
22   * You should have received a copy of the GNU General Public License and
23   * the Melati Software License along with this program;
24   * if not, write to the Free Software Foundation, Inc.,
25   * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
26   * GNU General Public License and visit http://melati.org to obtain the
27   * Melati Software License.
28   *
29   * Feel free to contact the Developers of Melati (http://melati.org),
30   * if you would like to work out a different arrangement than the options
31   * outlined here.  It is our intention to allow Melati to be used by as
32   * wide an audience as possible.
33   *
34   * This program is distributed in the hope that it will be useful,
35   * but WITHOUT ANY WARRANTY; without even the implied warranty of
36   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37   * GNU General Public License for more details.
38   *
39   * Contact details for copyright holder:
40   *
41   *     Tim Pizey <timp At paneris.org>
42   *     http://paneris.org/~timp
43   */
44  
45  package org.melati.poem.dbms.test.sql;
46  
47  import java.sql.CallableStatement;
48  import java.sql.Connection;
49  import java.sql.DatabaseMetaData;
50  import java.sql.PreparedStatement;
51  import java.sql.SQLException;
52  import java.sql.SQLWarning;
53  import java.sql.Savepoint;
54  import java.sql.Statement;
55  import java.util.Map;
56  
57  /**
58   * A JDBC3 {@link Connection}, decorated to throw an SQLException on command.
59   * 
60   * @author timp
61   * @since  5 Feb 2008
62   *
63   */
64  public abstract class ThrowingConnectionJdbc3 
65      extends Thrower 
66      implements Connection {
67  
68    Connection it = null;
69    
70    
71    /**
72     * {@inheritDoc}
73     * @see java.sql.Connection#clearWarnings()
74     */
75    public void clearWarnings() throws SQLException {
76      if (shouldThrow(this.getClass().getInterfaces()[0], "clearWarnings"))
77        throw new SQLException("Connection bombed");
78      it.clearWarnings();
79    }
80  
81    /**
82     * {@inheritDoc}
83     * @see java.sql.Connection#close()
84     */
85    public void close() throws SQLException {
86      if (shouldThrow(this.getClass().getInterfaces()[0], "close"))
87        throw new SQLException("Connection bombed");
88      it.close();
89    }
90  
91    /**
92     * {@inheritDoc}
93     * @see java.sql.Connection#commit()
94     */
95    public void commit() throws SQLException {
96      if (shouldThrow(this.getClass().getInterfaces()[0], "commit"))
97        throw new SQLException("Connection bombed");
98      it.commit();
99    }
100 
101   /**
102    * {@inheritDoc}
103    * @see java.sql.Connection#createStatement()
104    */
105   public Statement createStatement() throws SQLException {
106     if (shouldThrow(this.getClass().getInterfaces()[0], "createStatement"))
107       throw new SQLException("Connection bombed");
108     return new ThrowingStatement(it.createStatement());
109   }
110 
111   /**
112    * {@inheritDoc}
113    * @see java.sql.Connection#createStatement(int, int)
114    */
115   public Statement createStatement(int resultSetType, int resultSetConcurrency)
116       throws SQLException {
117     if (shouldThrow(this.getClass().getInterfaces()[0], "createStatement"))
118       throw new SQLException("Connection bombed");
119     return new ThrowingStatement(it.createStatement(resultSetType,resultSetConcurrency));
120   }
121 
122   /**
123    * {@inheritDoc}
124    * @see java.sql.Connection#createStatement(int, int, int)
125    */
126   public Statement createStatement(int resultSetType, int resultSetConcurrency,
127       int resultSetHoldability) throws SQLException {
128     if (shouldThrow(this.getClass().getInterfaces()[0], "createStatement"))
129       throw new SQLException("Connection bombed");
130     return new ThrowingStatement(it.createStatement(resultSetType, resultSetConcurrency));
131   }
132 
133   /**
134    * {@inheritDoc}
135    * @see java.sql.Connection#getAutoCommit()
136    */
137   public boolean getAutoCommit() throws SQLException {
138     if (shouldThrow(this.getClass().getInterfaces()[0], "getAutoCommit"))
139       throw new SQLException("Connection bombed");
140     return it.getAutoCommit();
141   }
142 
143   /**
144    * {@inheritDoc}
145    * @see java.sql.Connection#getCatalog()
146    */
147   public String getCatalog() throws SQLException {
148     if (shouldThrow(this.getClass().getInterfaces()[0], "getCatalog"))
149       throw new SQLException("Connection bombed");
150     return it.getCatalog();
151   }
152 
153   /**
154    * {@inheritDoc}
155    * @see java.sql.Connection#getHoldability()
156    */
157   public int getHoldability() throws SQLException {
158     if (shouldThrow(this.getClass().getInterfaces()[0], "getHoldability"))
159       throw new SQLException("Connection bombed");
160     return it.getHoldability();
161   }
162 
163   /**
164    * {@inheritDoc}
165    * @see java.sql.Connection#getMetaData()
166    */
167   public DatabaseMetaData getMetaData() throws SQLException {
168     if (shouldThrow(this.getClass().getInterfaces()[0], "getMetaData"))
169       throw new SQLException("Connection bombed");
170     return new ThrowingDatabaseMetaData(it.getMetaData());
171   }
172 
173   /**
174    * {@inheritDoc}
175    * @see java.sql.Connection#getTransactionIsolation()
176    */
177   public int getTransactionIsolation() throws SQLException {
178     if (shouldThrow(this.getClass().getInterfaces()[0], "getTransactionIsolation"))
179       throw new SQLException("Connection bombed");
180     return it.getTransactionIsolation();
181   }
182 
183   /**
184    * {@inheritDoc}
185    * @see java.sql.Connection#getTypeMap()
186    */
187   public Map<String,Class<?>> getTypeMap() throws SQLException {
188     if (shouldThrow(this.getClass().getInterfaces()[0], "getTypeMap()"))
189       throw new SQLException("Connection bombed");
190     return it.getTypeMap();
191   }
192 
193   /**
194    * {@inheritDoc}
195    * @see java.sql.Connection#getWarnings()
196    */
197   public SQLWarning getWarnings() throws SQLException {
198     if (shouldThrow(this.getClass().getInterfaces()[0], "getWarnings"))
199       throw new SQLException("Connection bombed");
200     return it.getWarnings();
201   }
202 
203   /**
204    * {@inheritDoc}
205    * @see java.sql.Connection#isClosed()
206    */
207   public boolean isClosed() throws SQLException {
208     if (shouldThrow(this.getClass().getInterfaces()[0], "isClosed"))
209       throw new SQLException("Connection bombed");
210     return it.isClosed();
211   }
212 
213   /**
214    * {@inheritDoc}
215    * @see java.sql.Connection#isReadOnly()
216    */
217   public boolean isReadOnly() throws SQLException {
218     if (shouldThrow(this.getClass().getInterfaces()[0], "isReadOnly"))
219       throw new SQLException("Connection bombed");
220     return it.isReadOnly();
221   }
222 
223   /**
224    * {@inheritDoc}
225    * @see java.sql.Connection#nativeSQL(java.lang.String)
226    */
227   public String nativeSQL(String sql) throws SQLException {
228     if (shouldThrow(this.getClass().getInterfaces()[0], "nativeSQL"))
229       throw new SQLException("Connection bombed");
230     return it.nativeSQL(sql);
231   }
232 
233   /**
234    * {@inheritDoc}
235    * @see java.sql.Connection#prepareCall(java.lang.String)
236    */
237   public CallableStatement prepareCall(String sql) throws SQLException {
238     if (shouldThrow(this.getClass().getInterfaces()[0], "prepareCall"))
239       throw new SQLException("Connection bombed");
240     return new ThrowingCallableStatement(it.prepareCall(sql));
241   }
242 
243   /**
244    * {@inheritDoc}
245    * @see java.sql.Connection#prepareCall(java.lang.String, int, int)
246    */
247   public CallableStatement prepareCall(String sql, int resultSetType,
248       int resultSetConcurrency) throws SQLException {
249     if (shouldThrow(this.getClass().getInterfaces()[0], "prepareCall"))
250       throw new SQLException("Connection bombed");
251     return new ThrowingCallableStatement(it.prepareCall(sql, resultSetType, resultSetConcurrency));
252   }
253 
254   /**
255    * {@inheritDoc}
256    * @see java.sql.Connection#prepareCall(java.lang.String, int, int, int)
257    */
258   public CallableStatement prepareCall(String sql, int resultSetType,
259       int resultSetConcurrency, int resultSetHoldability) throws SQLException {
260     if (shouldThrow(this.getClass().getInterfaces()[0], "prepareCall"))
261       throw new SQLException("Connection bombed");
262     return new ThrowingCallableStatement(it.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability));
263   }
264 
265   /**
266    * {@inheritDoc}
267    * @see java.sql.Connection#prepareStatement(java.lang.String)
268    */
269   public PreparedStatement prepareStatement(String sql) throws SQLException {
270     if (shouldThrow(this.getClass().getInterfaces()[0], "prepareStatement"))
271       throw new SQLException("Connection bombed");
272     return new ThrowingPreparedStatement(it.prepareStatement(sql));
273   }
274 
275   /**
276    * {@inheritDoc}
277    * @see java.sql.Connection#prepareStatement(java.lang.String, int)
278    */
279   public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
280       throws SQLException {
281     if (shouldThrow(this.getClass().getInterfaces()[0], "prepareStatement"))
282       throw new SQLException("Connection bombed");
283     return new ThrowingPreparedStatement(it.prepareStatement(sql, autoGeneratedKeys));
284   }
285 
286   /**
287    * {@inheritDoc}
288    * @see java.sql.Connection#prepareStatement(java.lang.String, int[])
289    */
290   public PreparedStatement prepareStatement(String sql, int[] columnIndexes)
291       throws SQLException {
292     if (shouldThrow(this.getClass().getInterfaces()[0], "prepareStatement"))
293       throw new SQLException("Connection bombed");
294     return new ThrowingPreparedStatement(it.prepareStatement(sql, columnIndexes));
295   }
296 
297   /**
298    * {@inheritDoc}
299    * @see java.sql.Connection#prepareStatement(java.lang.String, java.lang.String[])
300    */
301   public PreparedStatement prepareStatement(String sql, String[] columnNames)
302       throws SQLException {
303     if (shouldThrow(this.getClass().getInterfaces()[0], "prepareStatement"))
304       throw new SQLException("Connection bombed");
305     return new ThrowingPreparedStatement(it.prepareStatement(sql, columnNames));
306   }
307 
308   /**
309    * {@inheritDoc}
310    * @see java.sql.Connection#prepareStatement(java.lang.String, int, int)
311    */
312   public PreparedStatement prepareStatement(String sql, int resultSetType,
313       int resultSetConcurrency) throws SQLException {
314     if (shouldThrow(this.getClass().getInterfaces()[0], "prepareStatement"))
315       throw new SQLException("Connection bombed");
316     return new ThrowingPreparedStatement(it.prepareStatement(sql, resultSetType, resultSetConcurrency));
317   }
318 
319   /**
320    * {@inheritDoc}
321    * @see java.sql.Connection#prepareStatement(java.lang.String, int, int, int)
322    */
323   public PreparedStatement prepareStatement(String sql, int resultSetType,
324       int resultSetConcurrency, int resultSetHoldability) throws SQLException {
325     if (shouldThrow(this.getClass().getInterfaces()[0], "prepareStatement"))
326       throw new SQLException("Connection bombed");
327     return new ThrowingPreparedStatement(it.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability));
328   }
329 
330   /**
331    * {@inheritDoc}
332    * @see java.sql.Connection#releaseSavepoint(java.sql.Savepoint)
333    */
334   public void releaseSavepoint(Savepoint savepoint) throws SQLException {
335     if (shouldThrow(this.getClass().getInterfaces()[0], "releaseSavepoint"))
336       throw new SQLException("Connection bombed");
337     it.releaseSavepoint(savepoint);
338   }
339 
340   /**
341    * {@inheritDoc}
342    * @see java.sql.Connection#rollback()
343    */
344   public void rollback() throws SQLException {
345     if (shouldThrow(this.getClass().getInterfaces()[0], "rollback"))
346       throw new SQLException("Connection bombed");
347     it.rollback();
348   }
349 
350   /**
351    * {@inheritDoc}
352    * @see java.sql.Connection#rollback(java.sql.Savepoint)
353    */
354   public void rollback(Savepoint savepoint) throws SQLException {
355     if (shouldThrow(this.getClass().getInterfaces()[0], "rollback"))
356       throw new SQLException("Connection bombed");
357     it.rollback(savepoint);
358   }
359 
360   /**
361    * {@inheritDoc}
362    * @see java.sql.Connection#setAutoCommit(boolean)
363    */
364   public void setAutoCommit(boolean autoCommit) throws SQLException {
365     if (shouldThrow(this.getClass().getInterfaces()[0], "setAutoCommit"))
366       throw new SQLException("Connection bombed");
367     it.setAutoCommit(autoCommit);
368   }
369 
370   /**
371    * {@inheritDoc}
372    * @see java.sql.Connection#setCatalog(java.lang.String)
373    */
374   public void setCatalog(String catalog) throws SQLException {
375     if (shouldThrow(this.getClass().getInterfaces()[0], "setCatalog"))
376       throw new SQLException("Connection bombed");
377     it.setCatalog(catalog);
378   }
379 
380   /**
381    * {@inheritDoc}
382    * @see java.sql.Connection#setHoldability(int)
383    */
384   public void setHoldability(int holdability) throws SQLException {
385     if (shouldThrow(this.getClass().getInterfaces()[0], "setHoldability"))
386       throw new SQLException("Connection bombed");
387     it.setHoldability(holdability);
388   }
389 
390   /**
391    * {@inheritDoc}
392    * @see java.sql.Connection#setReadOnly(boolean)
393    */
394   public void setReadOnly(boolean readOnly) throws SQLException {
395     if (shouldThrow(this.getClass().getInterfaces()[0], "setReadOnly"))
396       throw new SQLException("Connection bombed");
397     it.setReadOnly(readOnly);
398   }
399 
400   /**
401    * {@inheritDoc}
402    * @see java.sql.Connection#setSavepoint()
403    */
404   public Savepoint setSavepoint() throws SQLException {
405     if (shouldThrow(this.getClass().getInterfaces()[0], "setSavepoint"))
406       throw new SQLException("Connection bombed");
407     return new ThrowingSavepoint(it.setSavepoint());
408   }
409 
410   /**
411    * {@inheritDoc}
412    * @see java.sql.Connection#setSavepoint(java.lang.String)
413    */
414   public Savepoint setSavepoint(String name) throws SQLException {
415     if (shouldThrow(this.getClass().getInterfaces()[0], "setSavepoint"))
416       throw new SQLException("Connection bombed");
417     return new ThrowingSavepoint(it.setSavepoint(name));
418   }
419 
420   /**
421    * {@inheritDoc}
422    * @see java.sql.Connection#setTransactionIsolation(int)
423    */
424   public void setTransactionIsolation(int level) throws SQLException {
425     if (shouldThrow(this.getClass().getInterfaces()[0], "setTransactionIsolation"))
426       throw new SQLException("Connection bombed");
427     it.setTransactionIsolation(level);
428   }
429 
430   /**
431    * {@inheritDoc}
432    * @see java.sql.Connection#setTypeMap(java.util.Map)
433    */
434   public void setTypeMap(Map<String,Class<?>> map) throws SQLException {
435     if (shouldThrow(this.getClass().getInterfaces()[0], "setTypeMap"))
436       throw new SQLException("Connection bombed");
437     it.setTypeMap(map);
438   }
439   
440   
441 }