View Javadoc
1   /*
2    * Copyright 2004-2025 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 com.ibatis.common.jdbc.logging;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.HashSet;
21  import java.util.List;
22  import java.util.Map;
23  import java.util.Set;
24  
25  /**
26   * Base class for proxies to do logging.
27   */
28  public class BaseLogProxy {
29  
30    /** The next id. */
31    private static int nextId = 100000;
32  
33    /** The Constant SET_METHODS. */
34    protected static final Set SET_METHODS = new HashSet<>();
35  
36    /** The Constant GET_METHODS. */
37    protected static final Set GET_METHODS = new HashSet<>();
38  
39    /** The Constant EXECUTE_METHODS. */
40    protected static final Set EXECUTE_METHODS = new HashSet<>();
41  
42    /** The column map. */
43    private Map columnMap = new HashMap<>();
44  
45    /** The column names. */
46    private List columnNames = new ArrayList<>();
47  
48    /** The column values. */
49    private List columnValues = new ArrayList<>();
50  
51    /** The id. */
52    protected int id;
53  
54    /**
55     * Default constructor.
56     */
57    public BaseLogProxy() {
58      id = getNextId();
59    }
60  
61    static {
62      SET_METHODS.add("setString");
63      SET_METHODS.add("setInt");
64      SET_METHODS.add("setByte");
65      SET_METHODS.add("setShort");
66      SET_METHODS.add("setLong");
67      SET_METHODS.add("setDouble");
68      SET_METHODS.add("setFloat");
69      SET_METHODS.add("setTimestamp");
70      SET_METHODS.add("setDate");
71      SET_METHODS.add("setTime");
72      SET_METHODS.add("setArray");
73      SET_METHODS.add("setBigDecimal");
74      SET_METHODS.add("setAsciiStream");
75      SET_METHODS.add("setBinaryStream");
76      SET_METHODS.add("setBlob");
77      SET_METHODS.add("setBoolean");
78      SET_METHODS.add("setBytes");
79      SET_METHODS.add("setCharacterStream");
80      SET_METHODS.add("setClob");
81      SET_METHODS.add("setObject");
82      SET_METHODS.add("setNull");
83  
84      GET_METHODS.add("getString");
85      GET_METHODS.add("getInt");
86      GET_METHODS.add("getByte");
87      GET_METHODS.add("getShort");
88      GET_METHODS.add("getLong");
89      GET_METHODS.add("getDouble");
90      GET_METHODS.add("getFloat");
91      GET_METHODS.add("getTimestamp");
92      GET_METHODS.add("getDate");
93      GET_METHODS.add("getTime");
94      GET_METHODS.add("getArray");
95      GET_METHODS.add("getBigDecimal");
96      GET_METHODS.add("getAsciiStream");
97      GET_METHODS.add("getBinaryStream");
98      GET_METHODS.add("getBlob");
99      GET_METHODS.add("getBoolean");
100     GET_METHODS.add("getBytes");
101     GET_METHODS.add("getCharacterStream");
102     GET_METHODS.add("getClob");
103     GET_METHODS.add("getObject");
104     GET_METHODS.add("getNull");
105 
106     EXECUTE_METHODS.add("execute");
107     EXECUTE_METHODS.add("executeUpdate");
108     EXECUTE_METHODS.add("executeQuery");
109 
110   }
111 
112   /**
113    * Sets the column.
114    *
115    * @param key
116    *          the key
117    * @param value
118    *          the value
119    */
120   protected void setColumn(Object key, Object value) {
121     columnMap.put(key, value);
122     columnNames.add(key);
123     columnValues.add(value);
124   }
125 
126   /**
127    * Gets the column.
128    *
129    * @param key
130    *          the key
131    *
132    * @return the column
133    */
134   protected Object getColumn(Object key) {
135     return columnMap.get(key);
136   }
137 
138   /**
139    * Gets the value string.
140    *
141    * @return the value string
142    */
143   protected String getValueString() {
144     return columnValues.toString();
145   }
146 
147   /**
148    * Gets the type string.
149    *
150    * @return the type string
151    */
152   protected String getTypeString() {
153     List typeList = new ArrayList<>(columnValues.size());
154     for (int i = 0; i < columnValues.size(); i++) {
155       Object value = columnValues.get(i);
156       if (value == null) {
157         typeList.add("null");
158       } else {
159         typeList.add(value.getClass().getName());
160       }
161     }
162     return typeList.toString();
163   }
164 
165   /**
166    * Gets the column string.
167    *
168    * @return the column string
169    */
170   protected String getColumnString() {
171     return columnNames.toString();
172   }
173 
174   /**
175    * Clear column info.
176    */
177   protected void clearColumnInfo() {
178     columnMap.clear();
179     columnNames.clear();
180     columnValues.clear();
181   }
182 
183   /**
184    * Removes the breaking whitespace.
185    *
186    * @param original
187    *          the original
188    *
189    * @return the string
190    */
191   protected String removeBreakingWhitespace(String original) {
192     return original.replace('\n', ' ').replace('\r', ' ').replace('\t', ' ');
193   }
194 
195   /**
196    * Gets the next id.
197    *
198    * @return the next id
199    */
200   protected synchronized static int getNextId() {
201     return nextId++;
202   }
203 
204 }