Commit 80f52bb6 authored by yanzg's avatar yanzg

将源码打包进jar包

parent 9a92cec7
...@@ -19,9 +19,9 @@ public interface DbExecute { ...@@ -19,9 +19,9 @@ public interface DbExecute {
* @param sqlName SQL语句名称 * @param sqlName SQL语句名称
* @param sql SQL语句 * @param sql SQL语句
* @param paras 参数信息 * @param paras 参数信息
* @return * @return 影响行数
*/ */
int update(Class targetClass, String sqlName, String sql, Object... paras); int update(Class<?> targetClass, String sqlName, String sql, Object... paras);
/** /**
...@@ -36,7 +36,7 @@ public interface DbExecute { ...@@ -36,7 +36,7 @@ public interface DbExecute {
* @param <T> 返回的集合的类型 * @param <T> 返回的集合的类型
* @return 集合 * @return 集合
*/ */
<T extends Object> void query(Class targetClass, Class<T> cls, DbRow<T> rowHandle, String sqlName, String sql, Object... paras); <T extends Object> void query(Class<?> targetClass, Class<T> cls, DbRow<T> rowHandle, String sqlName, String sql, Object... paras);
/** /**
* 查询数据,并返回集合 * 查询数据,并返回集合
...@@ -49,7 +49,7 @@ public interface DbExecute { ...@@ -49,7 +49,7 @@ public interface DbExecute {
* @param <T> 返回的集合的类型 * @param <T> 返回的集合的类型
* @return 集合 * @return 集合
*/ */
<T> List<T> query(Class targetClass, Class<T> cls, String sqlName, String sql, Object... paras); <T> List<T> query(Class<?> targetClass, Class<T> cls, String sqlName, String sql, Object... paras);
/** /**
* 查询数据,并返回集合 * 查询数据,并返回集合
...@@ -60,7 +60,7 @@ public interface DbExecute { ...@@ -60,7 +60,7 @@ public interface DbExecute {
* @param paras 查询语句的参数 * @param paras 查询语句的参数
* @return 集合 * @return 集合
*/ */
List<MapRow> query(Class targetClass, String sqlName, String sql, Object... paras); List<MapRow> query(Class<?> targetClass, String sqlName, String sql, Object... paras);
/** /**
* 查询第一个单元格的信息 * 查询第一个单元格的信息
...@@ -71,5 +71,5 @@ public interface DbExecute { ...@@ -71,5 +71,5 @@ public interface DbExecute {
* @param paras 参数信息 * @param paras 参数信息
* @return 第一个单元格的数据 * @return 第一个单元格的数据
*/ */
Object queryCell(Class targetClass, String sqlName, String sql, Object... paras); Object queryCell(Class<?> targetClass, String sqlName, String sql, Object... paras);
} }
...@@ -42,20 +42,16 @@ public class DbExecuteImpl implements DbExecute { ...@@ -42,20 +42,16 @@ public class DbExecuteImpl implements DbExecute {
* @param sqlName SQL语句名称 * @param sqlName SQL语句名称
* @param sql SQL语句 * @param sql SQL语句
* @param paras 参数信息 * @param paras 参数信息
* @return * @return 影响行数
*/ */
@Override @Override
public int update(Class targetClass, String sqlName, String sql, Object... paras) { public int update(Class<?> targetClass, String sqlName, String sql, Object... paras) {
Ref<Integer> row = new Ref<>(0); SqlInfo sqlInfo = new SqlInfo(targetClass, sqlName, sql, paras);
long start = System.currentTimeMillis(); Ref<Integer> ret = new Ref<>(0);
try { executeSql(sqlInfo, (row, start) ->
sql = this.handleParas(sql, paras); ret.value = getJdbc().update(sqlInfo.getSql(), sqlInfo.getParas())
String finalSql = sql; );
YzgTimeout.timeOut(targetClass, sqlName, () -> row.value = getJdbc().update(finalSql, paras)); return ret.value;
return row.value;
} finally {
printSql.print(targetClass, sqlName, start, row.value, sql, paras);
}
} }
/** /**
...@@ -70,25 +66,17 @@ public class DbExecuteImpl implements DbExecute { ...@@ -70,25 +66,17 @@ public class DbExecuteImpl implements DbExecute {
* @param <T> 返回的集合的类型 * @param <T> 返回的集合的类型
*/ */
@Override @Override
public <T extends Object> void query(Class targetClass, Class<T> cls, DbRow<T> rowHandle, String sqlName, String sql, Object... paras) { public <T extends Object> void query(Class<?> targetClass, Class<T> cls, DbRow<T> rowHandle, String sqlName, String sql, Object... paras) {
Ref<Integer> row = new Ref<Integer>(0); SqlInfo sqlInfo = new SqlInfo(targetClass, sqlName, sql, paras);
long start = System.currentTimeMillis(); executeSql(sqlInfo, (row, start) -> {
try { RowCallbackHandler rowCallbackHandler = rs -> {
sql = this.handleParas(sql, paras); AllBeanRowMapper<T> rowMap = AllBeanRowMapper.getInstance(cls, configDb);
String finalSql = sql; T data = rowMap.mapRow(rs, row.value);
YzgTimeout.timeOut(targetClass, sqlName, () -> { rowHandle.handle(data);
RowCallbackHandler rowCallbackHandler = rs -> { row.value++;
AllBeanRowMapper<T> rowMap = AllBeanRowMapper.getInstance(cls, configDb); };
T data = rowMap.mapRow(rs, row.value); this.getJdbc().query(sqlInfo.getSql(), rowCallbackHandler, sqlInfo.getParas());
rowHandle.handle(data); });
row.value++;
};
this.getJdbc().query(finalSql, rowCallbackHandler, paras);
});
} finally {
printSql.print(targetClass, sqlName, start, row.value, sql, paras);
}
} }
/** /**
...@@ -103,25 +91,17 @@ public class DbExecuteImpl implements DbExecute { ...@@ -103,25 +91,17 @@ public class DbExecuteImpl implements DbExecute {
* @return 集合 * @return 集合
*/ */
@Override @Override
public <T extends Object> List<T> query(Class targetClass, Class<T> cls, String sqlName, String sql, Object... paras) { public <T extends Object> List<T> query(Class<?> targetClass, Class<T> cls, String sqlName, String sql, Object... paras) {
int row = 0; SqlInfo sqlInfo = new SqlInfo(targetClass, sqlName, sql, paras);
long start = System.currentTimeMillis(); Ref<List<T>> ret = new Ref<>(null);
try { executeSql(sqlInfo, (row, start) -> {
sql = this.handleParas(sql, paras); ret.value = this.getJdbc().query(sqlInfo.getSql(), sqlInfo.getParas(), AllBeanRowMapper.getInstance(cls, configDb));
Ref<List<T>> ret = new Ref<>(null);
String finalSql = sql;
YzgTimeout.timeOut(targetClass, sqlName, () -> {
ret.value = this.getJdbc().query(finalSql, paras, AllBeanRowMapper.getInstance(cls, configDb));
});
if (ret.value == null) { if (ret.value == null) {
ret.value = new ArrayList<T>(); ret.value = new ArrayList<T>();
} }
row = ret.value.size(); row.value = ret.value.size();
return ret.value; });
} finally { return ret.value;
printSql.print(targetClass, sqlName, start, row, sql, paras);
}
} }
/** /**
...@@ -134,7 +114,7 @@ public class DbExecuteImpl implements DbExecute { ...@@ -134,7 +114,7 @@ public class DbExecuteImpl implements DbExecute {
* @return 集合 * @return 集合
*/ */
@Override @Override
public List<MapRow> query(Class targetClass, String sqlName, String sql, Object... paras) { public List<MapRow> query(Class<?> targetClass, String sqlName, String sql, Object... paras) {
return query(targetClass, MapRow.class, sqlName, sql, paras); return query(targetClass, MapRow.class, sqlName, sql, paras);
} }
...@@ -148,36 +128,50 @@ public class DbExecuteImpl implements DbExecute { ...@@ -148,36 +128,50 @@ public class DbExecuteImpl implements DbExecute {
* @return 第一个单元格的数据 * @return 第一个单元格的数据
*/ */
@Override @Override
public Object queryCell(Class targetClass, String sqlName, String sql, Object... paras) { public Object queryCell(Class<?> targetClass, String sqlName, String sql, Object... paras) {
SqlInfo sqlInfo = new SqlInfo(targetClass, sqlName, sql, paras);
Ref<Object> ret = new Ref<>(null);
executeSql(sqlInfo, (row, start) -> {
SqlRowSet rowSet = getJdbc().queryForRowSet(sqlInfo.getSql(), sqlInfo.getParas());
if (rowSet.next()) {
row.value = 1;
ret.value = rowSet.getObject(1);
}
});
return ret.value;
}
/**
* 执行sql语句
*
* @param sqlInfo sql语句信息
* @param sqlFunction 执行函数,传入参数为影响的行数引用和开始执行时间
*/
private void executeSql(SqlInfo sqlInfo, DbSqlFunction<Ref<Integer>, Long> sqlFunction) {
Ref<Integer> row = new Ref<>(0); Ref<Integer> row = new Ref<>(0);
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
sqlInfo.setSql(this.handleParas(sqlInfo.getSql()));
try { try {
sql = this.handleParas(sql, paras); YzgTimeout.timeOut(sqlInfo.getTargetClass(), sqlInfo.getSqlName(), () -> {
Ref<Object> ret = new Ref<>(null); sqlFunction.accept(row, start);
String finalSql = sql;
YzgTimeout.timeOut(targetClass, sqlName, () -> {
SqlRowSet rowSet = this.getJdbc().queryForRowSet(finalSql, paras);
while (rowSet.next()) {
row.value = 1;
ret.value = rowSet.getObject(1);
break;
}
}); });
return null;
} finally { } finally {
printSql.print(targetClass, sqlName, start, row.value, sql, paras); printSql.print(sqlInfo, start, row.value);
} }
} }
/** /**
* 处理SQL语句和参数值 * 处理SQL语句和参数值
* *
* @param sql * @param sql 处理sql语句
* @param paras * @return 可执行的sql语句
* @return
*/ */
protected String handleParas(String sql, Object... paras) { protected String handleParas(String sql) {
return sql.replaceAll("1\\s*?=\\s*?1\\s*?(?i)AND", "").replaceAll("(?i)WHERE\\s*?1\\s*?=\\s*?1", "").replaceAll("((?i)ORDER\\s*?(?i)BY\\s*?)1\\s*?,", "$1").replaceAll("(?i)ORDER\\s*?(?i)BY\\s*?1\\s*?", "").replaceAll("((?i)GROUP\\s*?(?i)BY\\s*?)1\\s*?,", "$1").replaceAll("(?i)GROUP\\s*?(?i)BY\\s*?1\\s*?", ""); return sql.replaceAll("1\\s*?=\\s*?1\\s*?(?i)AND", "")
.replaceAll("(?i)WHERE\\s*?1\\s*?=\\s*?1", "")
.replaceAll("((?i)ORDER\\s*?(?i)BY\\s*?)1\\s*?,", "$1")
.replaceAll("(?i)ORDER\\s*?(?i)BY\\s*?1\\s*?", "")
.replaceAll("((?i)GROUP\\s*?(?i)BY\\s*?)1\\s*?,", "$1")
.replaceAll("(?i)GROUP\\s*?(?i)BY\\s*?1\\s*?", "");
} }
} }
...@@ -3,7 +3,6 @@ package com.yanzuoguang.db.impl; ...@@ -3,7 +3,6 @@ package com.yanzuoguang.db.impl;
import com.yanzuoguang.db.ConfigDb; import com.yanzuoguang.db.ConfigDb;
import com.yanzuoguang.util.helper.StringHelper; import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.log.Log; import com.yanzuoguang.util.log.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
/** /**
...@@ -14,8 +13,11 @@ import org.springframework.stereotype.Component; ...@@ -14,8 +13,11 @@ import org.springframework.stereotype.Component;
@Component @Component
public class DbPrintSql { public class DbPrintSql {
@Autowired private final ConfigDb configDb;
private ConfigDb configDb;
public DbPrintSql(ConfigDb configDb) {
this.configDb = configDb;
}
/** /**
* 获取参数处理之后的SQL语句 * 获取参数处理之后的SQL语句
...@@ -61,30 +63,27 @@ public class DbPrintSql { ...@@ -61,30 +63,27 @@ public class DbPrintSql {
/** /**
* 打印SQL语句 * 打印SQL语句
* *
* @param targetClass 触发类 * @param sqlInfo sql语句嘻嘻你
* @param sqlName SQL语句名称 * @param start 开始执行的语句
* @param start 开始执行的语句 * @param row SQL语句
* @param row SQL语句
* @param sql SQL语句
* @param paras 参数
*/ */
public void print(Class targetClass, String sqlName, long start, int row, String sql, Object... paras) { public void print(SqlInfo sqlInfo, long start, int row) {
try { try {
// 日志表忽略打印 // 日志表忽略打印
if (!configDb.isPrintSql()) { if (!configDb.isPrintSql()) {
return; return;
} }
if (!StringHelper.isEmpty(configDb.getPrintSqlFilter())) { if (!StringHelper.isEmpty(configDb.getPrintSqlFilter())) {
if (sql.matches(configDb.getPrintSqlFilter())) { if (sqlInfo.getSql().matches(configDb.getPrintSqlFilter())) {
return; return;
} }
} }
sql = getStringSql(sql, paras); String sql = getStringSql(sqlInfo.getSql(), sqlInfo.getParas());
long end = System.currentTimeMillis(); long end = System.currentTimeMillis();
long time = end - start; long time = end - start;
// 打印SQL语句 // 打印SQL语句
Log.infoTag(DbPrintSql.class, String.format("%d row %d ms %s.%s", row, time, targetClass.getSimpleName(), sqlName), sql); String tag = String.format("%d row %d ms %s.%s", row, time, sqlInfo.getTargetClass().getSimpleName(), sqlInfo.getSqlName());
Log.infoTag(DbPrintSql.class, tag, sql);
} catch (Exception ex) { } catch (Exception ex) {
Log.error(DbPrintSql.class, ex); Log.error(DbPrintSql.class, ex);
} }
......
package com.yanzuoguang.db.impl;
import java.util.Objects;
/**
* 处理2个参数的函数接口
*
* @param <H> 影响的函数
* @param <I> 开始执行的时间
* @author 颜佐光
*/
@FunctionalInterface
public interface DbSqlFunction<H, I> {
/**
* Performs this operation on the given argument.
*
* @param row the input argument
* @param start the input argument
*/
void accept(H row, I start);
/**
* Returns a composed {@code Consumer} that performs, in sequence, this
* operation followed by the {@code after} operation. If performing either
* operation throws an exception, it is relayed to the caller of the
* composed operation. If performing this operation throws an exception,
* the {@code after} operation will not be performed.
*
* @param after the operation to perform after this operation
* @return a composed {@code Consumer} that performs in sequence this
* operation followed by the {@code after} operation
* @throws NullPointerException if {@code after} is null
*/
default DbSqlFunction<H, I> andThen(DbSqlFunction<? super H, ? super I> after) {
Objects.requireNonNull(after);
return (H h, I i) -> {
accept(h, i);
after.accept(h, i);
};
}
}
package com.yanzuoguang.db.impl;
/**
* Sql语句信息
*
* @author 颜佐光
*/
public class SqlInfo {
/**
* 触发的类
*/
private Class<?> targetClass;
/**
* Sql语句的名称
*/
private String sqlName;
/**
* 执行的sql语句
*/
private String sql;
/**
* 执行的参数
*/
private Object[] paras;
public SqlInfo(Class<?> targetClass, String sqlName, String sql, Object[] paras) {
this.targetClass = targetClass;
this.sqlName = sqlName;
this.sql = sql;
this.paras = paras;
}
public Class<?> getTargetClass() {
return targetClass;
}
public void setTargetClass(Class<?> targetClass) {
this.targetClass = targetClass;
}
public String getSqlName() {
return sqlName;
}
public void setSqlName(String sqlName) {
this.sqlName = sqlName;
}
public String getSql() {
return sql;
}
public void setSql(String sql) {
this.sql = sql;
}
public Object[] getParas() {
return paras;
}
public void setParas(Object[] paras) {
this.paras = paras;
}
}
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