001/*
002 *  Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
003 *  <p>
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *  <p>
008 *  http://www.apache.org/licenses/LICENSE-2.0
009 *  <p>
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package com.mybatisflex.core.audit;
017
018import com.mybatisflex.core.mybatis.TypeHandlerObject;
019import com.mybatisflex.core.util.ClassUtil;
020import com.mybatisflex.core.util.SqlUtil;
021
022import java.io.Serializable;
023import java.lang.reflect.Array;
024import java.lang.reflect.Proxy;
025import java.sql.PreparedStatement;
026import java.sql.SQLException;
027import java.sql.Statement;
028import java.util.ArrayList;
029import java.util.HashMap;
030import java.util.List;
031import java.util.Map;
032
033/**
034 * SQL 审计详细消息。
035 */
036public class AuditMessage implements Serializable {
037
038    /**
039     * 平台,或者是运行的应用。
040     */
041    private String platform;
042
043    /**
044     * 应用模块。
045     */
046    private String module;
047
048    /**
049     * 执行这个 SQL 涉及的 URL 地址。
050     */
051    private String url;
052
053    /**
054     * 自定义业务 ID。
055     */
056    private String bizId;
057
058    /**
059     * 执行这个 SQL 涉及的平台用户。
060     */
061    private String user;
062
063    /**
064     * 执行这个 SQL 的平台用户 IP 地址。
065     */
066    private String userIp;
067
068    /**
069     * 执行这个 SQL 的服务器 IP 地址。
070     */
071    private String hostIp;
072
073    /**
074     * SQL 内容。
075     */
076    private String query;
077
078    /**
079     * SQL 参数。
080     */
081    private List<Object> queryParams;
082
083    /**
084     * SQL 查询出来数据的数量。
085     */
086    private int queryCount;
087
088    /**
089     * SQL 执行的时间点(当前时间,毫秒)。
090     */
091    private long queryTime;
092
093    /**
094     * SQL 执行的消耗时间(毫秒)。
095     */
096    private long elapsedTime;
097
098    /**
099     * 数据库名称
100     */
101    private String dsName;
102
103    /**
104     * 其他扩展元信息。
105     */
106    private Map<String, Object> metas;
107
108    public String getPlatform() {
109        return platform;
110    }
111
112    public void setPlatform(String platform) {
113        this.platform = platform;
114    }
115
116    public String getModule() {
117        return module;
118    }
119
120    public void setModule(String module) {
121        this.module = module;
122    }
123
124    public String getUrl() {
125        return url;
126    }
127
128    public void setUrl(String url) {
129        this.url = url;
130    }
131
132    public String getBizId() {
133        return bizId;
134    }
135
136    public void setBizId(String bizId) {
137        this.bizId = bizId;
138    }
139
140    public String getUser() {
141        return user;
142    }
143
144    public void setUser(String user) {
145        this.user = user;
146    }
147
148    public String getUserIp() {
149        return userIp;
150    }
151
152    public void setUserIp(String userIp) {
153        this.userIp = userIp;
154    }
155
156    public String getHostIp() {
157        return hostIp;
158    }
159
160    public void setHostIp(String hostIp) {
161        this.hostIp = hostIp;
162    }
163
164    public String getQuery() {
165        return query;
166    }
167
168    public void setQuery(String query) {
169        this.query = query;
170    }
171
172    public List<Object> getQueryParams() {
173        return queryParams;
174    }
175
176    public void setQueryParams(List<Object> queryParams) {
177        this.queryParams = queryParams;
178    }
179
180    public void addParams(Statement statement, Object... objects) {
181        if (queryParams == null) {
182            queryParams = new ArrayList<>();
183        }
184
185        for (Object object : objects) {
186            if (object != null && ClassUtil.isArray(object.getClass())) {
187                for (int i = 0; i < Array.getLength(object); i++) {
188                    doAddParam(statement, Array.get(object, i));
189                }
190            } else {
191                doAddParam(statement, object);
192            }
193        }
194    }
195
196    private void doAddParam(Statement statement, Object object) {
197        try {
198            if (object instanceof TypeHandlerObject) {
199                ((TypeHandlerObject) object).setParameter(createPreparedStatement(statement), 0);
200            } else if (object instanceof java.sql.Array) {
201                Object array = ((java.sql.Array) object).getArray();
202                queryParams.add(array);
203            } else {
204                queryParams.add(object);
205            }
206        } catch (SQLException e) {
207            //ignore
208        }
209    }
210
211    public String getFullSql() {
212        List<Object> queryParams = getQueryParams();
213        return SqlUtil.replaceSqlParams(getQuery(), queryParams == null ? null : queryParams.toArray());
214    }
215
216    private PreparedStatement createPreparedStatement(Statement statement) {
217        return (PreparedStatement) Proxy.newProxyInstance(
218            AuditMessage.class.getClassLoader(),
219            new Class[]{PreparedStatement.class}, (proxy, method, args) -> {
220                if (args != null && (args.length == 2 || args.length == 3)) {
221                    doAddParam(statement, args[1]);
222                } else if ("getConnection".equals(method.getName())) {
223                    return statement.getConnection();
224                }
225                return null;
226            });
227    }
228
229    public int getQueryCount() {
230        return queryCount;
231    }
232
233    public void setQueryCount(int queryCount) {
234        this.queryCount = queryCount;
235    }
236
237    public long getQueryTime() {
238        return queryTime;
239    }
240
241    public void setQueryTime(long queryTime) {
242        this.queryTime = queryTime;
243    }
244
245    public long getElapsedTime() {
246        return elapsedTime;
247    }
248
249    public void setElapsedTime(long elapsedTime) {
250        this.elapsedTime = elapsedTime;
251    }
252
253    public Map<String, Object> getMetas() {
254        return metas;
255    }
256
257    public void setMetas(Map<String, Object> metas) {
258        this.metas = metas;
259    }
260
261    public void addMeta(String key, Object value) {
262        if (metas == null) {
263            metas = new HashMap<>();
264        }
265        metas.put(key, value);
266    }
267
268    public String getDsName() {
269        return dsName;
270    }
271
272    public void setDsName(String dsName) {
273        this.dsName = dsName;
274    }
275
276    @Override
277    public String toString() {
278        return "AuditMessage{" +
279            "platform='" + platform + '\'' +
280            ", module='" + module + '\'' +
281            ", url='" + url + '\'' +
282            ", bizId='" + bizId + '\'' +
283            ", user='" + user + '\'' +
284            ", userIp='" + userIp + '\'' +
285            ", hostIp='" + hostIp + '\'' +
286            ", query='" + query + '\'' +
287            ", queryParams=" + queryParams +
288            ", queryCount=" + queryCount +
289            ", queryTime=" + queryTime +
290            ", elapsedTime=" + elapsedTime +
291            ", dsName=" + dsName +
292            ", metas=" + metas +
293            '}';
294    }
295
296}