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.util.ArrayList; 028import java.util.HashMap; 029import java.util.List; 030import java.util.Map; 031 032public class AuditMessage implements Serializable { 033 034 private String platform; 035 private String module; 036 private String url; 037 private String bizId; //自定义业务ID 038 039 private String user; 040 private String userIp; 041 private String hostIp; 042 043 private String query; 044 private List<Object> queryParams; 045 private int queryCount; 046 047 private long queryTime; // Sql 执行的当前时间,单位毫秒 048 private long elapsedTime; // Sql 执行消耗的时间,单位毫秒 049 050 private Map<String, Object> metas; //其他信息,元信息 051 052 053 public String getPlatform() { 054 return platform; 055 } 056 057 public void setPlatform(String platform) { 058 this.platform = platform; 059 } 060 061 public String getModule() { 062 return module; 063 } 064 065 public void setModule(String module) { 066 this.module = module; 067 } 068 069 public String getUrl() { 070 return url; 071 } 072 073 public void setUrl(String url) { 074 this.url = url; 075 } 076 077 public String getBizId() { 078 return bizId; 079 } 080 081 public void setBizId(String bizId) { 082 this.bizId = bizId; 083 } 084 085 public String getUser() { 086 return user; 087 } 088 089 public void setUser(String user) { 090 this.user = user; 091 } 092 093 public String getUserIp() { 094 return userIp; 095 } 096 097 public void setUserIp(String userIp) { 098 this.userIp = userIp; 099 } 100 101 public String getHostIp() { 102 return hostIp; 103 } 104 105 public void setHostIp(String hostIp) { 106 this.hostIp = hostIp; 107 } 108 109 public String getQuery() { 110 return query; 111 } 112 113 public void setQuery(String query) { 114 this.query = query; 115 } 116 117 public List<Object> getQueryParams() { 118 return queryParams; 119 } 120 121 public void setQueryParams(List<Object> queryParams) { 122 this.queryParams = queryParams; 123 } 124 125 public void addParams(Object... objects) { 126 if (queryParams == null) { 127 queryParams = new ArrayList<>(); 128 } 129 for (Object object : objects) { 130 if (object != null && ClassUtil.isArray(object.getClass())) { 131 for (int i = 0; i < Array.getLength(object); i++) { 132 addParams(Array.get(object, i)); 133 } 134 } else if (object instanceof TypeHandlerObject) { 135 try { 136 ((TypeHandlerObject) object).setParameter(createPreparedStatement(), 0); 137 } catch (SQLException e) { 138 //ignore 139 } 140 } else { 141 queryParams.add(object); 142 } 143 } 144 } 145 146 public String getFullSql() { 147 List<Object> queryParams = getQueryParams(); 148 return SqlUtil.replaceSqlParams(getQuery(), queryParams == null ? null : queryParams.toArray()); 149 } 150 151 152 private PreparedStatement createPreparedStatement() { 153 return (PreparedStatement) Proxy.newProxyInstance( 154 AuditMessage.class.getClassLoader(), 155 new Class[]{PreparedStatement.class}, (proxy, method, args) -> { 156 if (args != null && args.length == 2) { 157 addParams(args[1]); 158 } 159 return null; 160 }); 161 } 162 163 public int getQueryCount() { 164 return queryCount; 165 } 166 167 public void setQueryCount(int queryCount) { 168 this.queryCount = queryCount; 169 } 170 171 public long getQueryTime() { 172 return queryTime; 173 } 174 175 public void setQueryTime(long queryTime) { 176 this.queryTime = queryTime; 177 } 178 179 public long getElapsedTime() { 180 return elapsedTime; 181 } 182 183 public void setElapsedTime(long elapsedTime) { 184 this.elapsedTime = elapsedTime; 185 } 186 187 public Map<String, Object> getMetas() { 188 return metas; 189 } 190 191 public void setMetas(Map<String, Object> metas) { 192 this.metas = metas; 193 } 194 195 public void addMeta(String key, Object value) { 196 if (metas == null) { 197 metas = new HashMap<>(); 198 } 199 metas.put(key, value); 200 } 201 202 @Override 203 public String toString() { 204 return "AuditMessage{" + 205 "platform='" + platform + '\'' + 206 ", module='" + module + '\'' + 207 ", url='" + url + '\'' + 208 ", bizId='" + bizId + '\'' + 209 ", user='" + user + '\'' + 210 ", userIp='" + userIp + '\'' + 211 ", hostIp='" + hostIp + '\'' + 212 ", query='" + query + '\'' + 213 ", queryParams=" + queryParams + 214 ", queryCount=" + queryCount + 215 ", queryTime=" + queryTime + 216 ", elapsedTime=" + elapsedTime + 217 ", metas=" + metas + 218 '}'; 219 } 220}