KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > jdbc > StatementWrapper


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
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 package scriptella.jdbc;
17
18 import scriptella.spi.ParametersCallback;
19 import scriptella.spi.QueryCallback;
20 import scriptella.util.ExceptionUtils;
21 import scriptella.util.IOUtils;
22
23 import java.io.Closeable JavaDoc;
24 import java.sql.PreparedStatement JavaDoc;
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.sql.Statement JavaDoc;
28 import java.util.List JavaDoc;
29
30 /**
31  * Abstraction for {@link java.sql.Statement} and {@link java.sql.PreparedStatement}.
32  *
33  * @author Fyodor Kupolov
34  * @version 1.0
35  */

36 abstract class StatementWrapper<T extends Statement JavaDoc> implements Closeable JavaDoc {
37     protected final JdbcTypesConverter converter;
38     protected final T statement;
39
40     /**
41      * For testing only.
42      */

43     protected StatementWrapper() {
44         converter = null;
45         statement = null;
46     }
47
48     protected StatementWrapper(T statement, JdbcTypesConverter converter) {
49         if (statement == null) {
50             throw new IllegalArgumentException JavaDoc("statement cannot be null");
51         }
52         if (converter == null) {
53             throw new IllegalArgumentException JavaDoc("converter cannot be null");
54         }
55         this.statement = statement;
56         this.converter = converter;
57     }
58
59     /**
60      * Release any resources opened by this statement.
61      */

62     public void close() {
63         JdbcUtils.closeSilent(statement);
64     }
65
66
67     /**
68      * Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement
69      * or an SQL statement that returns nothing, such as an SQL DDL statement.
70      *
71      * @return either the row count for INSERT, UPDATE, or DELETE statements or 0 for SQL statements that return nothing.
72      * @throws SQLException if JDBC driver fails to execute the operation.
73      */

74     public abstract int update() throws SQLException JavaDoc;
75
76     /**
77      * Executes the query and returns the result set.
78      *
79      * @return result set with query result.
80      * @throws SQLException if JDBC driver fails to execute the operation.
81      */

82     protected abstract ResultSet JavaDoc query() throws SQLException JavaDoc;
83
84     public void query(final QueryCallback queryCallback, final ParametersCallback parametersCallback) throws SQLException JavaDoc {
85         ResultSetAdapter r = null;
86         try {
87             r = new ResultSetAdapter(query(), parametersCallback, converter);
88             while (r.next()) {
89                 queryCallback.processRow(r);
90             }
91         } finally {
92             IOUtils.closeSilently(r);
93         }
94     }
95
96     public void setParameters(final List JavaDoc<Object JavaDoc> params) throws SQLException JavaDoc {
97     }
98
99     /**
100      * Clears any transient state variables, e.g. statement parameters etc.
101      */

102     public void clear() {
103     }
104
105     /**
106      * @see java.sql.Statement#toString()
107      */

108     public String JavaDoc toString() {
109         return statement.toString();
110     }
111
112
113     /**
114      * {@link Statement} wrapper.
115      */

116     static class Simple extends StatementWrapper<Statement JavaDoc> {
117         private final String JavaDoc sql;
118
119         /**
120          * For testing only.
121          */

122         protected Simple(String JavaDoc sql) {
123             this.sql = sql;
124         }
125
126         public Simple(Statement JavaDoc s, String JavaDoc sql, JdbcTypesConverter converter) {
127             super(s, converter);
128             this.sql = sql;
129         }
130
131         @Override JavaDoc
132         public int update() throws SQLException JavaDoc {
133             return statement.executeUpdate(sql);
134         }
135
136         @Override JavaDoc
137         protected ResultSet JavaDoc query() throws SQLException JavaDoc {
138             return statement.executeQuery(sql);
139         }
140
141     }
142
143     /**
144      * {@link PreparedStatement} wrapper.
145      */

146     static class Prepared extends StatementWrapper<PreparedStatement JavaDoc> {
147         /**
148          * For testing only.
149          */

150         protected Prepared() {
151         }
152
153         public Prepared(PreparedStatement JavaDoc s, JdbcTypesConverter converter) {
154             super(s, converter);
155         }
156
157         /**
158          * Sets parameters for this statement.
159          * <p>Default implementation is noop.
160          *
161          * @param params parameters to set.
162          * @throws SQLException
163          */

164         @Override JavaDoc
165         public void setParameters(List JavaDoc<Object JavaDoc> params) throws SQLException JavaDoc {
166             for (int i = 0, n = params.size(); i < n; i++) {
167                 Object JavaDoc o = params.get(i);
168                 converter.setObject(statement, i + 1, o);
169             }
170         }
171
172         @Override JavaDoc
173         public int update() throws SQLException JavaDoc {
174             try {
175                 return statement.executeUpdate();
176             } finally {
177                 converter.close(); //Disposing converter
178
}
179         }
180
181         @Override JavaDoc
182         protected ResultSet JavaDoc query() throws SQLException JavaDoc {
183             return statement.executeQuery();
184         }
185
186         @Override JavaDoc
187         public void clear() {
188             try {
189                 statement.clearParameters();
190             } catch (SQLException JavaDoc e) {
191                 ExceptionUtils.ignoreThrowable(e);
192             }
193
194         }
195
196     }
197
198
199 }
200
Popular Tags