Source code: com/ibatis/common/jdbc/logging/PreparedStatementLogProxy.java
1 /*
2 * Copyright 2004 Clinton Begin
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 com.ibatis.common.jdbc.logging;
17
18 import com.ibatis.common.beans.ClassInfo;
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22 import java.lang.reflect.InvocationHandler;
23 import java.lang.reflect.Method;
24 import java.lang.reflect.Proxy;
25 import java.sql.CallableStatement;
26 import java.sql.PreparedStatement;
27 import java.sql.ResultSet;
28
29 /**
30 * PreparedStatement proxy to add logging
31 */
32 public class PreparedStatementLogProxy extends BaseLogProxy implements InvocationHandler {
33
34 private static final Log log = LogFactory.getLog(PreparedStatement.class);
35
36 private PreparedStatement statement;
37 private String sql;
38
39 private PreparedStatementLogProxy(PreparedStatement stmt, String sql) {
40 this.statement = stmt;
41 this.sql = sql;
42 }
43
44 public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
45 try {
46 if (EXECUTE_METHODS.contains(method.getName())) {
47 if (log.isDebugEnabled()) {
48 log.debug("{pstm-" + id + "} PreparedStatement: " + removeBreakingWhitespace(sql));
49 log.debug("{pstm-" + id + "} Parameters: " + getValueString());
50 log.debug("{pstm-" + id + "} Types: " + getTypeString());
51 }
52 clearColumnInfo();
53 if ("executeQuery".equals(method.getName())) {
54 ResultSet rs = (ResultSet) method.invoke(statement, params);
55 return ResultSetLogProxy.newInstance(rs);
56 } else {
57 return method.invoke(statement, params);
58 }
59 } else if (SET_METHODS.contains(method.getName())) {
60 if ("setNull".equals(method.getName())) {
61 setColumn(params[0], null);
62 } else {
63 setColumn(params[0], params[1]);
64 }
65 return method.invoke(statement, params);
66 } else if ("getResultSet".equals(method.getName())) {
67 ResultSet rs = (ResultSet) method.invoke(statement, params);
68 return ResultSetLogProxy.newInstance(rs);
69 } else {
70 return method.invoke(statement, params);
71 }
72 } catch (Throwable t) {
73 throw ClassInfo.unwrapThrowable(t);
74 }
75 }
76
77 /**
78 * Creates a logging version of a PreparedStatement
79 * @param stmt - the statement
80 * @param sql - the sql statement
81 * @return - the proxy
82 */
83 public static PreparedStatement newInstance(PreparedStatement stmt, String sql) {
84 InvocationHandler handler = new PreparedStatementLogProxy(stmt, sql);
85 ClassLoader cl = PreparedStatement.class.getClassLoader();
86 return (PreparedStatement) Proxy.newProxyInstance(cl, new Class[]{PreparedStatement.class, CallableStatement.class}, handler);
87 }
88
89 }