KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > hajdbc > sql > Statement


1 /*
2  * HA-JDBC: High-Availability JDBC
3  * Copyright (c) 2004-2006 Paul Ferraro
4  *
5  * This library is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by the
7  * Free Software Foundation; either version 2.1 of the License, or (at your
8  * option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13  * for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: ferraro@users.sourceforge.net
20  */

21 package net.sf.hajdbc.sql;
22
23 import java.sql.SQLException JavaDoc;
24 import java.sql.SQLWarning JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import net.sf.hajdbc.Database;
28 import net.sf.hajdbc.Operation;
29
30 /**
31  * @author Paul Ferraro
32  * @version $Revision: 1437 $
33  * @param <T>
34  * @since 1.0
35  */

36 public class Statement<T extends java.sql.Statement JavaDoc> extends SQLObject<T, java.sql.Connection JavaDoc> implements java.sql.Statement JavaDoc
37 {
38     /**
39      * Constructs a new StatementProxy.
40      * @param connection a Connection proxy
41      * @param operation an operation that creates Statements
42      * @throws SQLException if operation execution fails
43      */

44     public Statement(Connection<?> connection, Operation<java.sql.Connection JavaDoc, T> operation) throws SQLException JavaDoc
45     {
46         super(connection, operation, connection.getDatabaseCluster().getNonTransactionalExecutor(), null);
47     }
48
49     /**
50      * @see net.sf.hajdbc.sql.SQLObject#handleExceptions(java.util.Map)
51      */

52     @Override JavaDoc
53     public void handleExceptions(Map JavaDoc<Database, SQLException JavaDoc> exceptionMap) throws SQLException JavaDoc
54     {
55         if (this.getAutoCommit())
56         {
57             super.handleExceptions(exceptionMap);
58         }
59         else
60         {
61             // If auto-commit is off, give client the opportunity to rollback the transaction
62
SQLException JavaDoc exception = null;
63             
64             for (Map.Entry JavaDoc<Database, SQLException JavaDoc> exceptionMapEntry: exceptionMap.entrySet())
65             {
66                 Database database = exceptionMapEntry.getKey();
67                 SQLException JavaDoc cause = exceptionMapEntry.getValue();
68                 
69                 try
70                 {
71                     this.getDatabaseCluster().handleFailure(database, cause);
72                 }
73                 catch (SQLException JavaDoc e)
74                 {
75                     if (exception == null)
76                     {
77                         exception = e;
78                     }
79                     else
80                     {
81                         exception.setNextException(e);
82                     }
83                 }
84             }
85             
86             if (exception != null)
87             {
88                 throw exception;
89             }
90         }
91     }
92     
93     /**
94      * @see java.sql.Statement#addBatch(java.lang.String)
95      */

96     public void addBatch(final String JavaDoc sql) throws SQLException JavaDoc
97     {
98         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
99         {
100             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
101             {
102                 statement.addBatch(sql);
103                 
104                 return null;
105             }
106         };
107         
108         this.executeWriteToDriver(operation);
109     }
110
111     /**
112      * @see java.sql.Statement#cancel()
113      */

114     public void cancel() throws SQLException JavaDoc
115     {
116         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
117         {
118             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
119             {
120                 statement.cancel();
121                 
122                 return null;
123             }
124         };
125         
126         this.executeNonTransactionalWriteToDatabase(operation);
127     }
128
129     /**
130      * @see java.sql.Statement#clearBatch()
131      */

132     public void clearBatch() throws SQLException JavaDoc
133     {
134         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
135         {
136             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
137             {
138                 statement.clearBatch();
139                 
140                 return null;
141             }
142         };
143         
144         this.executeWriteToDriver(operation);
145     }
146
147     /**
148      * @see java.sql.Statement#clearWarnings()
149      */

150     public void clearWarnings() throws SQLException JavaDoc
151     {
152         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
153         {
154             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
155             {
156                 statement.clearWarnings();
157                 
158                 return null;
159             }
160         };
161         
162         this.executeWriteToDriver(operation);
163     }
164
165     /**
166      * @see java.sql.Statement#close()
167      */

168     public void close() throws SQLException JavaDoc
169     {
170         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
171         {
172             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
173             {
174                 statement.close();
175                 
176                 return null;
177             }
178         };
179         
180         this.executeNonTransactionalWriteToDatabase(operation);
181     }
182
183     /**
184      * @see java.sql.Statement#execute(java.lang.String)
185      */

186     public boolean execute(final String JavaDoc sql) throws SQLException JavaDoc
187     {
188         Operation<T, Boolean JavaDoc> operation = new Operation<T, Boolean JavaDoc>()
189         {
190             public Boolean JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
191             {
192                 return statement.execute(sql);
193             }
194         };
195         
196         return this.firstValue(this.executeTransactionalWriteToDatabase(operation));
197     }
198
199     /**
200      * @see java.sql.Statement#execute(java.lang.String, int)
201      */

202     public boolean execute(final String JavaDoc sql, final int autoGeneratedKeys) throws SQLException JavaDoc
203     {
204         Operation<T, Boolean JavaDoc> operation = new Operation<T, Boolean JavaDoc>()
205         {
206             public Boolean JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
207             {
208                 return statement.execute(sql, autoGeneratedKeys);
209             }
210         };
211         
212         return this.firstValue(this.executeTransactionalWriteToDatabase(operation));
213     }
214
215     /**
216      * @see java.sql.Statement#execute(java.lang.String, int[])
217      */

218     public boolean execute(final String JavaDoc sql, final int[] columnIndexes) throws SQLException JavaDoc
219     {
220         Operation<T, Boolean JavaDoc> operation = new Operation<T, Boolean JavaDoc>()
221         {
222             public Boolean JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
223             {
224                 return statement.execute(sql, columnIndexes);
225             }
226         };
227         
228         return this.firstValue(this.executeTransactionalWriteToDatabase(operation));
229     }
230
231     /**
232      * @see java.sql.Statement#execute(java.lang.String, java.lang.String[])
233      */

234     public boolean execute(final String JavaDoc sql, final String JavaDoc[] columnNames) throws SQLException JavaDoc
235     {
236         Operation<T, Boolean JavaDoc> operation = new Operation<T, Boolean JavaDoc>()
237         {
238             public Boolean JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
239             {
240                 return statement.execute(sql, columnNames);
241             }
242         };
243         
244         return this.firstValue(this.executeTransactionalWriteToDatabase(operation));
245     }
246
247     /**
248      * @see java.sql.Statement#executeBatch()
249      */

250     public int[] executeBatch() throws SQLException JavaDoc
251     {
252         Operation<T, int[]> operation = new Operation<T, int[]>()
253         {
254             public int[] execute(Database database, T statement) throws SQLException JavaDoc
255             {
256                 return statement.executeBatch();
257             }
258         };
259         
260         return this.firstValue(this.executeTransactionalWriteToDatabase(operation));
261     }
262
263     /**
264      * @see java.sql.Statement#executeQuery(java.lang.String)
265      */

266     public java.sql.ResultSet JavaDoc executeQuery(final String JavaDoc sql) throws SQLException JavaDoc
267     {
268         Operation<T, java.sql.ResultSet JavaDoc> operation = new Operation<T, java.sql.ResultSet JavaDoc>()
269         {
270             public java.sql.ResultSet JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
271             {
272                 return statement.executeQuery(sql);
273             }
274         };
275         
276         return ((this.getResultSetConcurrency() == java.sql.ResultSet.CONCUR_READ_ONLY) && !this.isSelectForUpdate(sql)) ? this.executeReadFromDatabase(operation) : new ResultSet<T>(this, operation);
277     }
278
279     /**
280      * @see java.sql.Statement#executeUpdate(java.lang.String)
281      */

282     public int executeUpdate(final String JavaDoc sql) throws SQLException JavaDoc
283     {
284         Operation<T, Integer JavaDoc> operation = new Operation<T, Integer JavaDoc>()
285         {
286             public Integer JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
287             {
288                 return statement.executeUpdate(sql);
289             }
290         };
291         
292         return this.firstValue(this.executeTransactionalWriteToDatabase(operation));
293     }
294
295     /**
296      * @see java.sql.Statement#executeUpdate(java.lang.String, int)
297      */

298     public int executeUpdate(final String JavaDoc sql, final int autoGeneratedKeys) throws SQLException JavaDoc
299     {
300         Operation<T, Integer JavaDoc> operation = new Operation<T, Integer JavaDoc>()
301         {
302             public Integer JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
303             {
304                 return statement.executeUpdate(sql, autoGeneratedKeys);
305             }
306         };
307         
308         return this.firstValue(this.executeTransactionalWriteToDatabase(operation));
309     }
310
311     /**
312      * @see java.sql.Statement#executeUpdate(java.lang.String, int[])
313      */

314     public int executeUpdate(final String JavaDoc sql, final int[] columnIndexes) throws SQLException JavaDoc
315     {
316         Operation<T, Integer JavaDoc> operation = new Operation<T, Integer JavaDoc>()
317         {
318             public Integer JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
319             {
320                 return statement.executeUpdate(sql, columnIndexes);
321             }
322         };
323         
324         return this.firstValue(this.executeTransactionalWriteToDatabase(operation));
325     }
326
327     /**
328      * @see java.sql.Statement#executeUpdate(java.lang.String, java.lang.String[])
329      */

330     public int executeUpdate(final String JavaDoc sql, final String JavaDoc[] columnNames) throws SQLException JavaDoc
331     {
332         Operation<T, Integer JavaDoc> operation = new Operation<T, Integer JavaDoc>()
333         {
334             public Integer JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
335             {
336                 return statement.executeUpdate(sql, columnNames);
337             }
338         };
339         
340         return this.firstValue(this.executeTransactionalWriteToDatabase(operation));
341     }
342
343     /**
344      * @see java.sql.Statement#getConnection()
345      */

346     public java.sql.Connection JavaDoc getConnection()
347     {
348         return Connection.class.cast(this.parent);
349     }
350
351     /**
352      * @see java.sql.Statement#getFetchDirection()
353      */

354     public int getFetchDirection() throws SQLException JavaDoc
355     {
356         Operation<T, Integer JavaDoc> operation = new Operation<T, Integer JavaDoc>()
357         {
358             public Integer JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
359             {
360                 return statement.getFetchDirection();
361             }
362         };
363         
364         return this.executeReadFromDriver(operation);
365     }
366
367     /**
368      * @see java.sql.Statement#getFetchSize()
369      */

370     public int getFetchSize() throws SQLException JavaDoc
371     {
372         Operation<T, Integer JavaDoc> operation = new Operation<T, Integer JavaDoc>()
373         {
374             public Integer JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
375             {
376                 return statement.getFetchSize();
377             }
378         };
379         
380         return this.executeReadFromDriver(operation);
381     }
382
383     /**
384      * @see java.sql.Statement#getGeneratedKeys()
385      */

386     public java.sql.ResultSet JavaDoc getGeneratedKeys() throws SQLException JavaDoc
387     {
388         Operation<T, java.sql.ResultSet JavaDoc> operation = new Operation<T, java.sql.ResultSet JavaDoc>()
389         {
390             public java.sql.ResultSet JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
391             {
392                 return statement.getGeneratedKeys();
393             }
394         };
395
396         return this.executeReadFromDriver(operation);
397     }
398
399     /**
400      * @see java.sql.Statement#getMaxFieldSize()
401      */

402     public int getMaxFieldSize() throws SQLException JavaDoc
403     {
404         Operation<T, Integer JavaDoc> operation = new Operation<T, Integer JavaDoc>()
405         {
406             public Integer JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
407             {
408                 return statement.getMaxFieldSize();
409             }
410         };
411         
412         return this.executeReadFromDriver(operation);
413     }
414
415     /**
416      * @see java.sql.Statement#getMaxRows()
417      */

418     public int getMaxRows() throws SQLException JavaDoc
419     {
420         Operation<T, Integer JavaDoc> operation = new Operation<T, Integer JavaDoc>()
421         {
422             public Integer JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
423             {
424                 return statement.getMaxRows();
425             }
426         };
427         
428         return this.executeReadFromDriver(operation);
429     }
430
431     /**
432      * @see java.sql.Statement#getMoreResults()
433      */

434     public boolean getMoreResults() throws SQLException JavaDoc
435     {
436         Operation<T, Boolean JavaDoc> operation = new Operation<T, Boolean JavaDoc>()
437         {
438             public Boolean JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
439             {
440                 return statement.getMoreResults();
441             }
442         };
443         
444         return this.firstValue(this.executeNonTransactionalWriteToDatabase(operation));
445     }
446
447     /**
448      * @see java.sql.Statement#getMoreResults(int)
449      */

450     public boolean getMoreResults(final int current) throws SQLException JavaDoc
451     {
452         Operation<T, Boolean JavaDoc> operation = new Operation<T, Boolean JavaDoc>()
453         {
454             public Boolean JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
455             {
456                 return statement.getMoreResults(current);
457             }
458         };
459         
460         return this.firstValue((current == KEEP_CURRENT_RESULT) ? this.executeWriteToDriver(operation) : this.executeNonTransactionalWriteToDatabase(operation));
461     }
462
463     /**
464      * @see java.sql.Statement#getQueryTimeout()
465      */

466     public int getQueryTimeout() throws SQLException JavaDoc
467     {
468         Operation<T, Integer JavaDoc> operation = new Operation<T, Integer JavaDoc>()
469         {
470             public Integer JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
471             {
472                 return statement.getQueryTimeout();
473             }
474         };
475         
476         return this.executeReadFromDriver(operation);
477     }
478
479     /**
480      * @see java.sql.Statement#getResultSet()
481      */

482     public java.sql.ResultSet JavaDoc getResultSet() throws SQLException JavaDoc
483     {
484         Operation<T, java.sql.ResultSet JavaDoc> operation = new Operation<T, java.sql.ResultSet JavaDoc>()
485         {
486             public java.sql.ResultSet JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
487             {
488                 return statement.getResultSet();
489             }
490         };
491
492         return (this.getResultSetConcurrency() == java.sql.ResultSet.CONCUR_READ_ONLY) ? this.executeReadFromDriver(operation) : new ResultSet<T>(this, operation);
493     }
494
495     /**
496      * @see java.sql.Statement#getResultSetConcurrency()
497      */

498     public int getResultSetConcurrency() throws SQLException JavaDoc
499     {
500         Operation<T, Integer JavaDoc> operation = new Operation<T, Integer JavaDoc>()
501         {
502             public Integer JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
503             {
504                 return statement.getResultSetConcurrency();
505             }
506         };
507         
508         return this.executeReadFromDriver(operation);
509     }
510
511     /**
512      * @see java.sql.Statement#getResultSetHoldability()
513      */

514     public int getResultSetHoldability() throws SQLException JavaDoc
515     {
516         Operation<T, Integer JavaDoc> operation = new Operation<T, Integer JavaDoc>()
517         {
518             public Integer JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
519             {
520                 return statement.getResultSetHoldability();
521             }
522         };
523         
524         return this.executeReadFromDriver(operation);
525     }
526
527     /**
528      * @see java.sql.Statement#getResultSetType()
529      */

530     public int getResultSetType() throws SQLException JavaDoc
531     {
532         Operation<T, Integer JavaDoc> operation = new Operation<T, Integer JavaDoc>()
533         {
534             public Integer JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
535             {
536                 return statement.getResultSetType();
537             }
538         };
539         
540         return this.executeReadFromDriver(operation);
541     }
542
543     /**
544      * @see java.sql.Statement#getUpdateCount()
545      */

546     public int getUpdateCount() throws SQLException JavaDoc
547     {
548         Operation<T, Integer JavaDoc> operation = new Operation<T, Integer JavaDoc>()
549         {
550             public Integer JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
551             {
552                 return statement.getUpdateCount();
553             }
554         };
555         
556         return this.executeReadFromDriver(operation);
557     }
558
559     /**
560      * @see java.sql.Statement#getWarnings()
561      */

562     public SQLWarning JavaDoc getWarnings() throws SQLException JavaDoc
563     {
564         Operation<T, SQLWarning JavaDoc> operation = new Operation<T, SQLWarning JavaDoc>()
565         {
566             public SQLWarning JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
567             {
568                 return statement.getWarnings();
569             }
570         };
571
572         return this.executeReadFromDriver(operation);
573     }
574
575     /**
576      * @see java.sql.Statement#setCursorName(java.lang.String)
577      */

578     public void setCursorName(final String JavaDoc name) throws SQLException JavaDoc
579     {
580         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
581         {
582             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
583             {
584                 statement.setCursorName(name);
585                 
586                 return null;
587             }
588         };
589         
590         this.executeNonTransactionalWriteToDatabase(operation);
591         
592         this.record(operation);
593     }
594
595     /**
596      * @see java.sql.Statement#setEscapeProcessing(boolean)
597      */

598     public void setEscapeProcessing(final boolean enable) throws SQLException JavaDoc
599     {
600         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
601         {
602             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
603             {
604                 statement.setEscapeProcessing(enable);
605                 
606                 return null;
607             }
608         };
609         
610         this.executeWriteToDriver(operation);
611     }
612
613     /**
614      * @see java.sql.Statement#setFetchDirection(int)
615      */

616     public void setFetchDirection(final int direction) throws SQLException JavaDoc
617     {
618         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
619         {
620             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
621             {
622                 statement.setFetchDirection(direction);
623                 
624                 return null;
625             }
626         };
627         
628         this.executeWriteToDriver(operation);
629     }
630
631     /**
632      * @see java.sql.Statement#setFetchSize(int)
633      */

634     public void setFetchSize(final int size) throws SQLException JavaDoc
635     {
636         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
637         {
638             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
639             {
640                 statement.setFetchSize(size);
641                 
642                 return null;
643             }
644         };
645         
646         this.executeWriteToDriver(operation);
647     }
648
649     /**
650      * @see java.sql.Statement#setMaxFieldSize(int)
651      */

652     public void setMaxFieldSize(final int size) throws SQLException JavaDoc
653     {
654         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
655         {
656             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
657             {
658                 statement.setMaxFieldSize(size);
659                 
660                 return null;
661             }
662         };
663         
664         this.executeWriteToDriver(operation);
665     }
666
667     /**
668      * @see java.sql.Statement#setMaxRows(int)
669      */

670     public void setMaxRows(final int rows) throws SQLException JavaDoc
671     {
672         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
673         {
674             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
675             {
676                 statement.setMaxRows(rows);
677                 
678                 return null;
679             }
680         };
681         
682         this.executeNonTransactionalWriteToDatabase(operation);
683         
684         this.record(operation);
685     }
686     
687     /**
688      * @see java.sql.Statement#setQueryTimeout(int)
689      */

690     public void setQueryTimeout(final int seconds) throws SQLException JavaDoc
691     {
692         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
693         {
694             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
695             {
696                 statement.setQueryTimeout(seconds);
697                 
698                 return null;
699             }
700         };
701         
702         this.executeWriteToDriver(operation);
703     }
704
705     private boolean getAutoCommit()
706     {
707         try
708         {
709             return this.getConnection().getAutoCommit();
710         }
711         catch (SQLException JavaDoc e)
712         {
713             return true;
714         }
715     }
716     
717     protected boolean isSelectForUpdate(String JavaDoc sql) throws SQLException JavaDoc
718     {
719         return this.getDatabaseCluster().getDialect().isSelectForUpdate(this.getConnection().getMetaData(), sql);
720     }
721
722     /**
723      * @see net.sf.hajdbc.sql.SQLObject#close(java.lang.Object)
724      */

725     @Override JavaDoc
726     protected void close(T statement) throws SQLException JavaDoc
727     {
728         statement.close();
729     }
730 }
731
Popular Tags