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