Commit c1703d87 authored by yanzg's avatar yanzg

处理SQL语句

parent f995b489
...@@ -50,7 +50,7 @@ public class DbExecuteImpl implements DbExecute { ...@@ -50,7 +50,7 @@ public class DbExecuteImpl implements DbExecute {
row = jdbc.update(sql, paras); row = jdbc.update(sql, paras);
return row; return row;
} finally { } finally {
printSql.print(targetClass, sqlName, row, sql, paras, start); printSql.print(targetClass, sqlName, start, row, sql, paras);
} }
} }
...@@ -81,7 +81,7 @@ public class DbExecuteImpl implements DbExecute { ...@@ -81,7 +81,7 @@ public class DbExecuteImpl implements DbExecute {
} }
}); });
} finally { } finally {
printSql.print(targetClass, sqlName, row.value, sql, paras, start); printSql.print(targetClass, sqlName, start, row.value, sql, paras);
} }
} }
...@@ -109,7 +109,7 @@ public class DbExecuteImpl implements DbExecute { ...@@ -109,7 +109,7 @@ public class DbExecuteImpl implements DbExecute {
row = ret.size(); row = ret.size();
return ret; return ret;
} finally { } finally {
printSql.print(targetClass, sqlName, row, sql, paras, start); printSql.print(targetClass, sqlName, start, row, sql, paras);
} }
} }
...@@ -147,7 +147,7 @@ public class DbExecuteImpl implements DbExecute { ...@@ -147,7 +147,7 @@ public class DbExecuteImpl implements DbExecute {
} }
return null; return null;
} finally { } finally {
printSql.print(targetClass, sqlName, row, sql, paras, start); printSql.print(targetClass, sqlName, start, row, sql, paras);
} }
} }
......
...@@ -14,23 +14,13 @@ public class DbPrintSql { ...@@ -14,23 +14,13 @@ public class DbPrintSql {
private ConfigDb configDb; private ConfigDb configDb;
/** /**
* 打印SQL语句 * 获取参数处理之后的SQL语句
* *
* @param targetClass 触发类 * @param sql 带参数的SQL语句
* @param sqlName SQL语句名称
* @param sql SQL语句
* @param paras 参数 * @param paras 参数
* @return 不带参数的SQL语句
*/ */
public void print(Class targetClass, String sqlName, int row, String sql, Object... paras) { public String getStringSql(String sql, Object... paras) {
// 日志表忽略打印
if (!configDb.isPrintSql()) {
return;
}
if (!StringHelper.isEmpty(configDb.getPrintSqlFilter())) {
if (sql.matches(configDb.getPrintSqlFilter())) {
return;
}
}
// 进行SQL语句参数替换,后面增加一个空格,方便后续用正则表达式进行替换处理 // 进行SQL语句参数替换,后面增加一个空格,方便后续用正则表达式进行替换处理
for (Object item : paras) { for (Object item : paras) {
String str = StringHelper.toString(item); String str = StringHelper.toString(item);
...@@ -40,12 +30,37 @@ public class DbPrintSql { ...@@ -40,12 +30,37 @@ public class DbPrintSql {
} else { } else {
sql = sql.replaceFirst("\\?", str); sql = sql.replaceFirst("\\?", str);
} }
} else { } else {
sql = sql.replaceFirst("\\?", "'" + str.replace("'", "''") + "'"); sql = sql.replaceFirst("\\?", "'" + str.replace("'", "''") + "'");
} }
} }
return sql;
}
/**
* 打印SQL语句
*
* @param targetClass 触发类
* @param sqlName SQL语句名称
* @param start 开始执行的语句
* @param row SQL语句
* @param sql SQL语句
* @param paras 参数
*/
public void print(Class targetClass, String sqlName, long start, int row, String sql, Object... paras) {
// 日志表忽略打印
if (!configDb.isPrintSql()) {
return;
}
if (!StringHelper.isEmpty(configDb.getPrintSqlFilter())) {
if (sql.matches(configDb.getPrintSqlFilter())) {
return;
}
}
sql = getStringSql(sql, paras);
// 打印SQL语句
Log.infoTag(DbPrintSql.class, String.format("%d row %s.%s", row, targetClass.getSimpleName(), sqlName), sql); Log.infoTag(DbPrintSql.class, String.format("%d row %s.%s", row, targetClass.getSimpleName(), sqlName), sql);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment