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