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);
} }
} }
......
...@@ -13,15 +13,41 @@ public class DbPrintSql { ...@@ -13,15 +13,41 @@ public class DbPrintSql {
@Resource @Resource
private ConfigDb configDb; private ConfigDb configDb;
/**
* 获取参数处理之后的SQL语句
*
* @param sql 带参数的SQL语句
* @param paras 参数
* @return 不带参数的SQL语句
*/
public String getStringSql(String sql, Object... paras) {
// 进行SQL语句参数替换,后面增加一个空格,方便后续用正则表达式进行替换处理
for (Object item : paras) {
String str = StringHelper.toString(item);
if (str == null || item instanceof Number || item.getClass().isEnum() || item instanceof Boolean) {
if (str == null) {
sql = sql.replaceFirst("\\?", "null");
} else {
sql = sql.replaceFirst("\\?", str);
}
} else {
sql = sql.replaceFirst("\\?", "'" + str.replace("'", "''") + "'");
}
}
return sql;
}
/** /**
* 打印SQL语句 * 打印SQL语句
* *
* @param targetClass 触发类 * @param targetClass 触发类
* @param sqlName SQL语句名称 * @param sqlName SQL语句名称
* @param start 开始执行的语句
* @param row SQL语句
* @param sql SQL语句 * @param sql SQL语句
* @param paras 参数 * @param paras 参数
*/ */
public void print(Class targetClass, String sqlName, int row, String sql, Object... paras) { public void print(Class targetClass, String sqlName, long start, int row, String sql, Object... paras) {
// 日志表忽略打印 // 日志表忽略打印
if (!configDb.isPrintSql()) { if (!configDb.isPrintSql()) {
return; return;
...@@ -31,21 +57,10 @@ public class DbPrintSql { ...@@ -31,21 +57,10 @@ public class DbPrintSql {
return; return;
} }
} }
// 进行SQL语句参数替换,后面增加一个空格,方便后续用正则表达式进行替换处理 sql = getStringSql(sql, paras);
for (Object item : paras) { // 打印SQL语句
String str = StringHelper.toString(item);
if (str == null || item instanceof Number || item.getClass().isEnum() || item instanceof Boolean) {
if (str == null) {
sql = sql.replaceFirst("\\?", "null");
} else {
sql = sql.replaceFirst("\\?", str);
}
} else {
sql = sql.replaceFirst("\\?", "'" + str.replace("'", "''") + "'");
}
}
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