KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdbc > conn > LoggingStatement


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdbc.conn;
13
14 import com.versant.core.logging.LogEventStore;
15 import com.versant.core.jdbc.logging.JdbcStatementEvent;
16 import com.versant.core.jdbc.logging.JdbcLogEvent;
17 import com.versant.core.logging.LogEventStore;
18 import com.versant.core.logging.LogEventStore;
19
20 import java.sql.*;
21
22 /**
23  * A JDBC Statement wrapped for event logging.
24  */

25 public class LoggingStatement implements Statement {
26
27     protected final LoggingConnection con;
28     private final Statement statement;
29     protected final LogEventStore pes;
30
31     public LoggingStatement(LoggingConnection con, Statement statement,
32             LogEventStore pes) {
33         this.con = con;
34         this.statement = statement;
35         this.pes = pes;
36     }
37
38     /**
39      * Get the real statement.
40      */

41     public Statement getStatement() {
42         return statement;
43     }
44
45     public boolean getMoreResults(int current) throws SQLException {
46         return statement.getMoreResults(current);
47     }
48
49     public ResultSet getGeneratedKeys() throws SQLException {
50         return statement.getGeneratedKeys();
51     }
52
53     public int executeUpdate(String JavaDoc sql, int autoGeneratedKeys) throws SQLException {
54         return statement.executeUpdate(sql, autoGeneratedKeys);
55     }
56
57     public int executeUpdate(String JavaDoc sql, int columnIndexes[]) throws SQLException {
58         return statement.executeUpdate(sql, columnIndexes);
59     }
60
61     public int executeUpdate(String JavaDoc sql, String JavaDoc columnNames[]) throws SQLException {
62         return statement.executeUpdate(sql, columnNames);
63     }
64
65     public boolean execute(String JavaDoc sql, int autoGeneratedKeys) throws SQLException {
66         return statement.execute(sql, autoGeneratedKeys);
67     }
68
69     public boolean execute(String JavaDoc sql, int columnIndexes[]) throws SQLException {
70         return statement.execute(sql, columnIndexes);
71     }
72
73     public boolean execute(String JavaDoc sql, String JavaDoc columnNames[]) throws SQLException {
74         return statement.execute(sql, columnNames);
75     }
76
77     public int getResultSetHoldability() throws SQLException {
78         return statement.getResultSetHoldability();
79     }
80
81     public ResultSet executeQuery(String JavaDoc sql) throws SQLException {
82         if (!pes.isFine()) return statement.executeQuery(sql);
83         JdbcStatementEvent ev = new JdbcStatementEvent(0, this,
84             sql, JdbcStatementEvent.STAT_EXEC_QUERY);
85         pes.log(ev);
86         try {
87             ResultSet rs = statement.executeQuery(sql);
88             ev.updateResultSetID(rs);
89             if (pes.isFiner()) rs = new LoggingResultSet(this, sql, rs, pes);
90             return rs;
91         } catch (SQLException e) {
92             con.setNeedsValidation(true);
93             ev.setErrorMsg(e);
94             throw e;
95         } catch (RuntimeException JavaDoc e) {
96             con.setNeedsValidation(true);
97             ev.setErrorMsg(e);
98             throw e;
99         } finally {
100             ev.updateTotalMs();
101         }
102     }
103
104     public int executeUpdate(String JavaDoc sql) throws SQLException {
105         if (!pes.isFine()) {
106             return statement.executeUpdate(sql);
107         }
108         JdbcStatementEvent ev = new JdbcStatementEvent(0, this,
109             sql, JdbcStatementEvent.STAT_EXEC_UPDATE);
110         pes.log(ev);
111         try {
112             int c = statement.executeUpdate(sql);
113             ev.setUpdateCount(c);
114             return c;
115         } catch (SQLException e) {
116             con.setNeedsValidation(true);
117             ev.setErrorMsg(e);
118             throw e;
119         } catch (RuntimeException JavaDoc e) {
120             con.setNeedsValidation(true);
121             ev.setErrorMsg(e);
122             throw e;
123         } finally {
124             ev.updateTotalMs();
125         }
126     }
127
128     public void close() throws SQLException {
129         if (!pes.isFiner()) {
130             statement.close();
131             return;
132         }
133         JdbcStatementEvent ev = new JdbcStatementEvent(0, this,
134             null, JdbcStatementEvent.STAT_CLOSE);
135         pes.log(ev);
136         try {
137             statement.close();
138         } catch (SQLException e) {
139             con.setNeedsValidation(true);
140             ev.setErrorMsg(e);
141             throw e;
142         } catch (RuntimeException JavaDoc e) {
143             con.setNeedsValidation(true);
144             ev.setErrorMsg(e);
145             throw e;
146         } finally {
147             ev.updateTotalMs();
148         }
149     }
150
151     public int getMaxFieldSize() throws SQLException {
152         return statement.getMaxFieldSize();
153     }
154
155     public void setMaxFieldSize(int max) throws SQLException {
156         statement.setMaxFieldSize(max);
157     }
158
159     public int getMaxRows() throws SQLException {
160         return statement.getMaxRows();
161     }
162
163     public void setMaxRows(int max) throws SQLException {
164         JdbcLogEvent ev = null;
165         if (pes.isFiner()) {
166             ev = new JdbcLogEvent(0,
167                 JdbcLogEvent.STAT_MAX_ROWS, Integer.toString(max));
168             pes.log(ev);
169         }
170         try {
171             statement.setMaxRows(max);
172         } catch (SQLException e) {
173             if (ev != null) ev.setErrorMsg(e);
174             throw e;
175         } catch (RuntimeException JavaDoc e) {
176             if (ev != null) ev.setErrorMsg(e);
177             throw e;
178         } finally {
179             if (ev != null) ev.updateTotalMs();
180         }
181     }
182
183     public void setEscapeProcessing(boolean enable) throws SQLException {
184         statement.setEscapeProcessing(enable);
185     }
186
187     public int getQueryTimeout() throws SQLException {
188         return statement.getQueryTimeout();
189     }
190
191     public void setQueryTimeout(int seconds) throws SQLException {
192         statement.setQueryTimeout(seconds);
193     }
194
195     public void cancel() throws SQLException {
196         statement.cancel();
197     }
198
199     public SQLWarning getWarnings() throws SQLException {
200         return statement.getWarnings();
201     }
202
203     public void clearWarnings() throws SQLException {
204         statement.clearWarnings();
205     }
206
207     public void setCursorName(String JavaDoc name) throws SQLException {
208         statement.setCursorName(name);
209     }
210
211     public boolean execute(String JavaDoc sql) throws SQLException {
212         if (!pes.isFine()) return statement.execute(sql);
213         JdbcStatementEvent ev = new JdbcStatementEvent(0, this,
214             sql, JdbcStatementEvent.STAT_EXEC);
215         pes.log(ev);
216         try {
217             boolean ans = statement.execute(sql);
218             ev.setHasResultSet(ans);
219             return ans;
220         } catch (SQLException e) {
221             con.setNeedsValidation(true);
222             ev.setErrorMsg(e);
223             throw e;
224         } catch (RuntimeException JavaDoc e) {
225             con.setNeedsValidation(true);
226             ev.setErrorMsg(e);
227             throw e;
228         } finally {
229             ev.updateTotalMs();
230         }
231     }
232
233     public java.sql.ResultSet JavaDoc getResultSet() throws SQLException {
234         return statement.getResultSet();
235     }
236
237     public int getUpdateCount() throws SQLException {
238         return statement.getUpdateCount();
239     }
240
241     public boolean getMoreResults() throws SQLException {
242         return statement.getMoreResults();
243     }
244
245     public void setFetchDirection(int direction) throws SQLException {
246         statement.setFetchDirection(direction);
247     }
248
249     public int getFetchDirection() throws SQLException {
250         return statement.getFetchDirection();
251     }
252
253     public void setFetchSize(int rows) throws SQLException {
254         statement.setFetchSize(rows);
255     }
256
257     public int getFetchSize() throws SQLException {
258         return statement.getFetchSize();
259     }
260
261     public int getResultSetConcurrency() throws SQLException {
262         return statement.getResultSetConcurrency();
263     }
264
265     public int getResultSetType() throws SQLException {
266         return statement.getResultSetType();
267     }
268
269     public void addBatch(String JavaDoc sql) throws SQLException {
270         statement.addBatch(sql);
271     }
272
273     public void clearBatch() throws SQLException {
274         statement.clearBatch();
275     }
276
277     public int[] executeBatch() throws SQLException {
278         if (!pes.isFine()) return statement.executeBatch();
279         JdbcStatementEvent ev = new JdbcStatementEvent(0, this,
280             getSql(), JdbcStatementEvent.STAT_EXEC_BATCH);
281         pes.log(ev);
282         try {
283             int[] a = statement.executeBatch();
284             ev.setUpdateCounts(a);
285             return a;
286         } catch (SQLException e) {
287             con.setNeedsValidation(true);
288             ev.setErrorMsg(e);
289             throw e;
290         } catch (RuntimeException JavaDoc e) {
291             con.setNeedsValidation(true);
292             ev.setErrorMsg(e);
293             throw e;
294         } finally {
295             ev.updateTotalMs();
296         }
297     }
298
299     /**
300      * PreparedStatement subclasses override this to return the SQL for
301      * event logging.
302      */

303     protected String JavaDoc getSql() {
304         return null;
305     }
306
307     public Connection getConnection() throws SQLException {
308         return con;
309     }
310
311 }
312
313
Popular Tags