BaseJdbcLogger.java

  1. /*
  2.  *    Copyright 2009-2024 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. import java.lang.reflect.Method;
  18. import java.sql.Array;
  19. import java.sql.PreparedStatement;
  20. import java.sql.SQLException;
  21. import java.util.ArrayList;
  22. import java.util.Arrays;
  23. import java.util.HashMap;
  24. import java.util.HashSet;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.Set;
  28. import java.util.stream.Collectors;

  29. import org.apache.ibatis.builder.SqlSourceBuilder;
  30. import org.apache.ibatis.logging.Log;
  31. import org.apache.ibatis.reflection.ArrayUtil;

  32. /**
  33.  * Base class for proxies to do logging.
  34.  *
  35.  * @author Clinton Begin
  36.  * @author Eduardo Macarron
  37.  */
  38. public abstract class BaseJdbcLogger {

  39.   protected static final Set<String> SET_METHODS;
  40.   protected static final Set<String> EXECUTE_METHODS = new HashSet<>();

  41.   private final Map<Object, Object> columnMap = new HashMap<>();

  42.   private final List<Object> columnNames = new ArrayList<>();
  43.   private final List<Object> columnValues = new ArrayList<>();

  44.   protected final Log statementLog;
  45.   protected final int queryStack;

  46.   /*
  47.    * Default constructor
  48.    */
  49.   public BaseJdbcLogger(Log log, int queryStack) {
  50.     this.statementLog = log;
  51.     if (queryStack == 0) {
  52.       this.queryStack = 1;
  53.     } else {
  54.       this.queryStack = queryStack;
  55.     }
  56.   }

  57.   static {
  58.     SET_METHODS = Arrays.stream(PreparedStatement.class.getDeclaredMethods())
  59.         .filter(method -> method.getName().startsWith("set")).filter(method -> method.getParameterCount() > 1)
  60.         .map(Method::getName).collect(Collectors.toSet());

  61.     EXECUTE_METHODS.add("execute");
  62.     EXECUTE_METHODS.add("executeUpdate");
  63.     EXECUTE_METHODS.add("executeQuery");
  64.     EXECUTE_METHODS.add("addBatch");
  65.   }

  66.   protected void setColumn(Object key, Object value) {
  67.     columnMap.put(key, value);
  68.     columnNames.add(key);
  69.     columnValues.add(value);
  70.   }

  71.   protected Object getColumn(Object key) {
  72.     return columnMap.get(key);
  73.   }

  74.   protected String getParameterValueString() {
  75.     List<Object> typeList = new ArrayList<>(columnValues.size());
  76.     for (Object value : columnValues) {
  77.       if (value == null) {
  78.         typeList.add("null");
  79.       } else {
  80.         typeList.add(objectValueString(value) + "(" + value.getClass().getSimpleName() + ")");
  81.       }
  82.     }
  83.     final String parameters = typeList.toString();
  84.     return parameters.substring(1, parameters.length() - 1);
  85.   }

  86.   protected String objectValueString(Object value) {
  87.     if (value instanceof Array) {
  88.       try {
  89.         return ArrayUtil.toString(((Array) value).getArray());
  90.       } catch (SQLException e) {
  91.         // Intentionally fall through to return value.toString()
  92.       }
  93.     }
  94.     return value.toString();
  95.   }

  96.   protected String getColumnString() {
  97.     return columnNames.toString();
  98.   }

  99.   protected void clearColumnInfo() {
  100.     columnMap.clear();
  101.     columnNames.clear();
  102.     columnValues.clear();
  103.   }

  104.   protected String removeExtraWhitespace(String original) {
  105.     return SqlSourceBuilder.removeExtraWhitespaces(original);
  106.   }

  107.   protected boolean isDebugEnabled() {
  108.     return statementLog.isDebugEnabled();
  109.   }

  110.   protected boolean isTraceEnabled() {
  111.     return statementLog.isTraceEnabled();
  112.   }

  113.   protected void debug(String text, boolean input) {
  114.     if (statementLog.isDebugEnabled()) {
  115.       statementLog.debug(prefix(input) + text);
  116.     }
  117.   }

  118.   protected void trace(String text, boolean input) {
  119.     if (statementLog.isTraceEnabled()) {
  120.       statementLog.trace(prefix(input) + text);
  121.     }
  122.   }

  123.   private String prefix(boolean isInput) {
  124.     char[] buffer = new char[queryStack * 2 + 2];
  125.     Arrays.fill(buffer, '=');
  126.     buffer[queryStack * 2 + 1] = ' ';
  127.     if (isInput) {
  128.       buffer[queryStack * 2] = '>';
  129.     } else {
  130.       buffer[0] = '<';
  131.     }
  132.     return new String(buffer);
  133.   }

  134. }