Merge pull request #212 from mfish-qf/develop

feat: 完善数据查询逻辑
This commit is contained in:
mfish
2024-01-25 21:14:44 +08:00
committed by GitHub
10 changed files with 61 additions and 41 deletions
@@ -25,6 +25,8 @@ public class TableInfo implements Serializable {
private String tableComment;
@ApiModelProperty("")
private String tableSchema;
@ApiModelProperty("表类型 0表 1视图")
private Integer tableType;
@ApiModelProperty("列信息")
private List<FieldInfo> columns;
}
@@ -26,7 +26,7 @@ public class MysqlDBDialect extends AbstractDBDialect {
@Override
public BoundSql getTableInfo(String dbName, String tableName) {
String sql = "SELECT table_name, table_comment, table_schema\n" +
String sql = "SELECT table_name, table_comment, table_schema, if(table_type='VIEW',1,0) table_type\n" +
" FROM INFORMATION_SCHEMA.TABLES where 1=1";
return buildCondition(sql, dbName, tableName);
}
@@ -43,7 +43,7 @@ public class OracleDBDialect implements DBDialect {
@Override
public BoundSql getTableInfo(String dbName, String tableName) {
String sql = "select u.table_name, u.comments table_comment, a.owner table_schema\n" +
String sql = "select u.table_name, u.comments table_comment, a.owner table_schema,DECODE(u.table_type,'VIEW', 1, 0) table_type\n" +
" from user_tab_comments u\n" +
" inner join all_tab_comments a\n" +
" on u.table_name = a.table_name\n" +
@@ -54,8 +54,10 @@ public class OracleDBDialect implements DBDialect {
private BoundSql buildCondition(String sql, String tableName) {
BoundSql boundSql = new BoundSql();
if (!StringUtils.isEmpty(tableName)) {
sql += " and a.TABLE_NAME = ?";
//这里虽然匹配大小写,但是建议oracle表名视图都创建大写名称,避免数据查询时无法查到
sql += " and (a.TABLE_NAME = ? or a.TABLE_NAME = ?)";
boundSql.getParams().add(new QueryParam().setValue(tableName.toUpperCase(Locale.ROOT)));
boundSql.getParams().add(new QueryParam().setValue(tableName.toLowerCase(Locale.ROOT)));
}
return boundSql.setSql(sql);
}
@@ -35,18 +35,18 @@ public class PostgreDBDialect extends AbstractDBDialect {
@Override
public BoundSql getTableInfo(String dbName, String tableName) {
String strSql = "SELECT d.table_name,\n" +
String strSql = "SELECT\n" +
"d.table_name,\n" +
"CAST(obj_description(C.relfilenode, 'pg_class') AS VARCHAR) AS TABLE_COMMENT,\n" +
"d.table_schema \n" +
"d.table_schema,\n" +
"(CASE WHEN c.relkind = 'v' THEN 1 ELSE 0 END ) AS table_type\n" +
"FROM\n" +
"pg_class c,\n" +
"information_schema.tables d \n" +
"WHERE\n" +
"c.relkind = 'r' \n" +
"AND c.relname NOT LIKE'pg_%' \n" +
"AND c.relname NOT LIKE'sql_%' \n" +
"AND c.relkind = 'r' \n" +
"AND c.relname = d.TABLE_NAME\n";
"c.relnamespace IN ( SELECT relnamespace FROM pg_class WHERE relkind = 'r' AND relname NOT LIKE'pg_%' AND relname NOT LIKE'sql_%' ) \n" +
"AND ( c.relkind = 'r' OR c.relkind = 'v' )\n" +
"AND c.relname = d.TABLE_NAME";
//pg数据库table_schema是单独定义的schema不是数据库名称,此处暂时不传。参数预留
return buildCondition(strSql, "", tableName);
}
@@ -1,6 +1,8 @@
package cn.com.mfish.common.dblink.manger;
import cn.com.mfish.common.dblink.dbpool.PoolWrapper;
import lombok.Getter;
import lombok.Setter;
import javax.sql.DataSource;
import java.sql.Connection;
@@ -13,14 +15,18 @@ import java.sql.SQLException;
*/
public class PoolContext {
//连接池有效期(一天 单位:秒)
@Setter
@Getter
private long expire = 24 * 60 * 60;
//检查时间
// 当datasource被获取的1分钟内,不执行清理线程,降低线程额外开销
@Getter
@Setter
private long checkTime;
//连接池包装类
private PoolWrapper poolWrapper;
private final PoolWrapper<?> poolWrapper;
public PoolContext(PoolWrapper poolWrapper) {
public PoolContext(PoolWrapper<?> poolWrapper) {
this.poolWrapper = poolWrapper;
}
@@ -28,22 +34,6 @@ public class PoolContext {
poolWrapper.close();
}
public long getExpire() {
return expire;
}
public void setExpire(long expire) {
this.expire = expire;
}
public long getCheckTime() {
return checkTime;
}
public void setCheckTime(long checkTime) {
this.checkTime = checkTime;
}
public DataSource getDataSource() {
return poolWrapper.getDataSource();
}
@@ -86,7 +86,7 @@ public abstract class AbstractDialect implements Dialect {
return getPageSql(boundSql, page);
}
protected abstract BoundSql getPageSql(BoundSql boundSql, Page page);
protected abstract BoundSql getPageSql(BoundSql boundSql, Page<?> page);
@Override
public <T extends Collection> T afterPage(T dataTable) {
@@ -17,9 +17,9 @@ public class MysqlDialect extends AbstractDialect {
}
@Override
protected BoundSql getPageSql(BoundSql boundSql, Page page) {
StringBuilder sqlBuilder = new StringBuilder();
sqlBuilder.append("SELECT * FROM (").append(boundSql.getSql());
protected BoundSql getPageSql(BoundSql boundSql, Page<?> page) {
StringBuilder sqlBuilder = new StringBuilder("SELECT * FROM (");
sqlBuilder.append(boundSql.getSql());
if (page.getStartRow() == 0) {
sqlBuilder.append(") TEMP LIMIT ?");
boundSql.getParams().add(new QueryParam().setValue(page.getPageSize()).setType(DataType.INTEGER));
@@ -18,7 +18,7 @@ public class OracleDialect extends AbstractDialect {
}
@Override
protected BoundSql getPageSql(BoundSql boundSql, Page page) {
protected BoundSql getPageSql(BoundSql boundSql, Page<?> page) {
String sql = "SELECT * FROM ( " +
" SELECT TMP_PAGE.*, ROWNUM " + DataConstant.ORACLE_ROW + " FROM (" +
boundSql.getSql() +
@@ -18,9 +18,9 @@ public class PostgreDialect extends AbstractDialect {
}
@Override
protected BoundSql getPageSql(BoundSql boundSql, Page page) {
StringBuilder sqlBuilder = new StringBuilder();
sqlBuilder.append("SELECT * FROM (" + boundSql.getSql());
protected BoundSql getPageSql(BoundSql boundSql, Page<?> page) {
StringBuilder sqlBuilder = new StringBuilder("SELECT * FROM (")
.append(boundSql.getSql());
if (page.getStartRow() == 0) {
sqlBuilder.append(") TEMP LIMIT ?");
boundSql.getParams().add(new QueryParam().setValue(page.getPageSize()).setType(DataType.INTEGER));
@@ -103,12 +103,8 @@ public class BaseQuery {
continue;
}
String type = rs.getMetaData().getColumnTypeName(i);
Object columnValue = formatValue(type, rs.getObject(i));
Object columnValue = formatValue(type, getValue(field.getType().getTypeName(), rs, i));
field.setAccessible(true);
//数据库中是数字类型,字段类型为boolean时强制转换为boolean
if (field.getType().getTypeName().equals("java.lang.Boolean") && columnValue instanceof Number) {
columnValue = DataUtils.numCompare(columnValue, 0) > 0;
}
field.set(t, columnValue);
break;
}
@@ -118,6 +114,36 @@ public class BaseQuery {
return list;
}
//数据库中是数字类型,字段类型为boolean时强制转换为boolean
private Object getValue(String typeName, ResultSet rs, int i) throws SQLException {
//todo 类型不全后续根据实际情况补充
if ("java.lang.Boolean".equals(typeName) || "boolean".equals(typeName)) {
return rs.getBoolean(i);
}
if ("java.lang.Integer".equals(typeName) || "int".equals(typeName)) {
return rs.getInt(i);
}
if ("java.lang.Long".equals(typeName) || "long".equals(typeName)) {
return rs.getLong(i);
}
if ("java.lang.Float".equals(typeName) || "float".equals(typeName)) {
return rs.getFloat(i);
}
if ("java.lang.Double".equals(typeName) || "double".equals(typeName)) {
return rs.getDouble(i);
}
if ("java.lang.Short".equals(typeName) || "short".equals(typeName)) {
return rs.getShort(i);
}
if ("java.math.BigDecimal".equals(typeName)) {
return rs.getBigDecimal(i);
}
if ("java.util.Date".equals(typeName)) {
return rs.getTimestamp(i);
}
return rs.getObject(i);
}
/**
* 基础查询方法
*