View Javadoc
1   /*
2    *    Copyright 2009-2023 the original author or authors.
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    *       https://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 org.apache.ibatis.logging.jdbc;
17  
18  import java.lang.reflect.Method;
19  import java.sql.Array;
20  import java.sql.PreparedStatement;
21  import java.sql.SQLException;
22  import java.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.HashMap;
25  import java.util.HashSet;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Set;
29  import java.util.stream.Collectors;
30  
31  import org.apache.ibatis.builder.SqlSourceBuilder;
32  import org.apache.ibatis.logging.Log;
33  import org.apache.ibatis.reflection.ArrayUtil;
34  
35  /**
36   * Base class for proxies to do logging.
37   *
38   * @author Clinton Begin
39   * @author Eduardo Macarron
40   */
41  public abstract class BaseJdbcLogger {
42  
43    protected static final Set<String> SET_METHODS;
44    protected static final Set<String> EXECUTE_METHODS = new HashSet<>();
45  
46    private final Map<Object, Object> columnMap = new HashMap<>();
47  
48    private final List<Object> columnNames = new ArrayList<>();
49    private final List<Object> columnValues = new ArrayList<>();
50  
51    protected final Log statementLog;
52    protected final int queryStack;
53  
54    /*
55     * Default constructor
56     */
57    public BaseJdbcLogger(Log log, int queryStack) {
58      this.statementLog = log;
59      if (queryStack == 0) {
60        this.queryStack = 1;
61      } else {
62        this.queryStack = queryStack;
63      }
64    }
65  
66    static {
67      SET_METHODS = Arrays.stream(PreparedStatement.class.getDeclaredMethods())
68          .filter(method -> method.getName().startsWith("set")).filter(method -> method.getParameterCount() > 1)
69          .map(Method::getName).collect(Collectors.toSet());
70  
71      EXECUTE_METHODS.add("execute");
72      EXECUTE_METHODS.add("executeUpdate");
73      EXECUTE_METHODS.add("executeQuery");
74      EXECUTE_METHODS.add("addBatch");
75    }
76  
77    protected void setColumn(Object key, Object value) {
78      columnMap.put(key, value);
79      columnNames.add(key);
80      columnValues.add(value);
81    }
82  
83    protected Object getColumn(Object key) {
84      return columnMap.get(key);
85    }
86  
87    protected String getParameterValueString() {
88      List<Object> typeList = new ArrayList<>(columnValues.size());
89      for (Object value : columnValues) {
90        if (value == null) {
91          typeList.add("null");
92        } else {
93          typeList.add(objectValueString(value) + "(" + value.getClass().getSimpleName() + ")");
94        }
95      }
96      final String parameters = typeList.toString();
97      return parameters.substring(1, parameters.length() - 1);
98    }
99  
100   protected String objectValueString(Object value) {
101     if (value instanceof Array) {
102       try {
103         return ArrayUtil.toString(((Array) value).getArray());
104       } catch (SQLException e) {
105         // Intentialy fall through to return value.toString()
106       }
107     }
108     return value.toString();
109   }
110 
111   protected String getColumnString() {
112     return columnNames.toString();
113   }
114 
115   protected void clearColumnInfo() {
116     columnMap.clear();
117     columnNames.clear();
118     columnValues.clear();
119   }
120 
121   protected String removeExtraWhitespace(String original) {
122     return SqlSourceBuilder.removeExtraWhitespaces(original);
123   }
124 
125   protected boolean isDebugEnabled() {
126     return statementLog.isDebugEnabled();
127   }
128 
129   protected boolean isTraceEnabled() {
130     return statementLog.isTraceEnabled();
131   }
132 
133   protected void debug(String text, boolean input) {
134     if (statementLog.isDebugEnabled()) {
135       statementLog.debug(prefix(input) + text);
136     }
137   }
138 
139   protected void trace(String text, boolean input) {
140     if (statementLog.isTraceEnabled()) {
141       statementLog.trace(prefix(input) + text);
142     }
143   }
144 
145   private String prefix(boolean isInput) {
146     char[] buffer = new char[queryStack * 2 + 2];
147     Arrays.fill(buffer, '=');
148     buffer[queryStack * 2 + 1] = ' ';
149     if (isInput) {
150       buffer[queryStack * 2] = '>';
151     } else {
152       buffer[0] = '<';
153     }
154     return new String(buffer);
155   }
156 
157 }