oracle,达梦已知sql异常修复

冗余代码优化
This commit is contained in:
diant
2023-05-30 18:18:48 +08:00
parent 289a42b7d4
commit 76c8b83284
5 changed files with 48 additions and 37 deletions

View File

@@ -36,7 +36,8 @@
<!-- 获取表列表 -->
<select id="getTableList" resultType="com.zyplayer.doc.db.framework.db.dto.TableInfoDto">
select t.OWNER as dbName,t.TABLE_NAME as tableName,c.COMMENTS as tableComment from all_tables t left join user_tab_comments c on t.TABLE_NAME = c.TABLE_NAME
select t.OWNER as dbName,t.TABLE_NAME as tableName,c.COMMENTS as tableComment from all_tables t
left join all_tab_comments c on t.TABLE_NAME = c.TABLE_NAME and t.OWNER = c.OWNER
<where>
<if test="dbName != null">t.owner = #{dbName}</if>
</where>
@@ -45,10 +46,17 @@
<!-- 获取表字段集合 -->
<select id="getTableColumnList" resultMap="TableColumnDescDtoMap">
select t.TABLE_NAME,t.COLUMN_NAME,t.DATA_TYPE,case t.NULLABLE when 'Y' then '1' when 'N' then '0' end NULLABLE, c.COMMENTS,
s.POSITION as primaryKey from all_tab_columns t
left join user_col_comments c on t.COLUMN_NAME = c.COLUMN_NAME and t.TABLE_NAME = c.TABLE_NAME
left join user_cons_columns s on t.COLUMN_NAME = s.COLUMN_NAME and t.TABLE_NAME = s.TABLE_NAME and s.POSITION = 1
select t.TABLE_NAME,t.COLUMN_NAME,t.DATA_TYPE,case t.NULLABLE when 'Y' then '1' when 'N' then '0' end NULLABLE,
cons.POSITION as primaryKey,col_comments.comments from all_tab_columns t
left join (
select s.owner, s.table_name, s.column_name,s.POSITION, cons.constraint_type from all_cons_columns s
join all_constraints cons on s.owner = cons.owner and
s.constraint_name = cons.constraint_name where cons.constraint_type in ('P', 'U', 'R')
)cons on t.owner = cons.owner and t.table_name = cons.table_name and t.column_name = cons.column_name
left join (
select comments.owner, comments.table_name, comments.column_name, comments.comments
from all_col_comments comments) col_comments on t.owner = col_comments.owner and t.table_name = col_comments.table_name
and t.column_name = col_comments.column_name
<where>
t.owner = #{dbName}
<if test="tableName != null"> and t.TABLE_NAME = #{tableName}</if>
@@ -59,7 +67,7 @@
<select id="getTableAndColumnBySearch" resultMap="QueryTableColumnDescDtoMap">
select t.TABLE_NAME,t.COLUMN_NAME,t.DATA_TYPE,case t.NULLABLE when 'Y' then 1 when 'N' then 0 end NULLABLE, c.COMMENTS
from all_tab_columns t
left join all_col_comments c on t.COLUMN_NAME = c.COLUMN_NAME and t.TABLE_NAME = c.TABLE_NAME
left join all_col_comments c on t.COLUMN_NAME = c.COLUMN_NAME and t.TABLE_NAME = c.TABLE_NAME and t.OWNER = c.OWNER
where t.OWNER = #{dbName} AND (upper(t.COLUMN_NAME) like upper(#{searchText}) or upper(c.COMMENTS) like upper(#{searchText}))
</select>
@@ -67,7 +75,7 @@
<select id="getTableDescList" resultType="com.zyplayer.doc.db.framework.db.dto.TableDescDto">
select t.OWNER,t.TABLE_NAME as tableName,c.COMMENTS as description
from all_tables t
left join user_tab_comments c on t.TABLE_NAME = c.TABLE_NAME
left join all_tab_comments c on t.TABLE_NAME = c.TABLE_NAME and t.owner = c.owner
<where>
<if test="dbName != null">and t.owner = #{dbName}</if>
<if test="tableName != null">and t.TABLE_NAME = #{tableName}</if>

View File

@@ -36,7 +36,8 @@
<!-- 获取表列表 -->
<select id="getTableList" resultType="com.zyplayer.doc.db.framework.db.dto.TableInfoDto">
select t.OWNER as dbName,t.TABLE_NAME as tableName,c.COMMENTS as tableComment from all_tables t left join user_tab_comments c on t.TABLE_NAME = c.TABLE_NAME
select t.OWNER as dbName,t.TABLE_NAME as tableName,c.COMMENTS as tableComment from all_tables t
left join all_tab_comments c on t.TABLE_NAME = c.TABLE_NAME and t.OWNER = c.OWNER
<where>
<if test="dbName != null">t.owner = #{dbName}</if>
</where>
@@ -45,10 +46,17 @@
<!-- 获取表字段集合 -->
<select id="getTableColumnList" resultMap="TableColumnDescDtoMap">
select t.TABLE_NAME,t.COLUMN_NAME,t.DATA_TYPE,case t.NULLABLE when 'Y' then '1' when 'N' then '0' end NULLABLE, c.COMMENTS,
s.POSITION as primaryKey from all_tab_columns t
left join user_col_comments c on t.COLUMN_NAME = c.COLUMN_NAME and t.TABLE_NAME = c.TABLE_NAME
left join user_cons_columns s on t.COLUMN_NAME = s.COLUMN_NAME and t.TABLE_NAME = s.TABLE_NAME and s.POSITION = 1
select t.TABLE_NAME,t.COLUMN_NAME,t.DATA_TYPE,case t.NULLABLE when 'Y' then '1' when 'N' then '0' end NULLABLE,
cons.POSITION as primaryKey,col_comments.comments from all_tab_columns t
left join (
select s.owner, s.table_name, s.column_name,s.POSITION, cons.constraint_type from all_cons_columns s
join all_constraints cons on s.owner = cons.owner and
s.constraint_name = cons.constraint_name where cons.constraint_type in ('P', 'U', 'R')
)cons on t.owner = cons.owner and t.table_name = cons.table_name and t.column_name = cons.column_name
left join (
select comments.owner, comments.table_name, comments.column_name, comments.comments
from all_col_comments comments) col_comments on t.owner = col_comments.owner and t.table_name = col_comments.table_name
and t.column_name = col_comments.column_name
<where>
t.owner = #{dbName}
<if test="tableName != null"> and t.TABLE_NAME = #{tableName}</if>
@@ -59,7 +67,7 @@
<select id="getTableAndColumnBySearch" resultMap="QueryTableColumnDescDtoMap">
select t.TABLE_NAME,t.COLUMN_NAME,t.DATA_TYPE,case t.NULLABLE when 'Y' then 1 when 'N' then 0 end NULLABLE, c.COMMENTS
from all_tab_columns t
left join all_col_comments c on t.COLUMN_NAME = c.COLUMN_NAME and t.TABLE_NAME = c.TABLE_NAME
left join all_col_comments c on t.COLUMN_NAME = c.COLUMN_NAME and t.TABLE_NAME = c.TABLE_NAME and t.OWNER = c.OWNER
where t.OWNER = #{dbName} AND (upper(t.COLUMN_NAME) like upper(#{searchText}) or upper(c.COMMENTS) like upper(#{searchText}))
</select>
@@ -67,7 +75,7 @@
<select id="getTableDescList" resultType="com.zyplayer.doc.db.framework.db.dto.TableDescDto">
select t.OWNER,t.TABLE_NAME as tableName,c.COMMENTS as description
from all_tables t
left join user_tab_comments c on t.TABLE_NAME = c.TABLE_NAME
left join all_tab_comments c on t.TABLE_NAME = c.TABLE_NAME and t.owner = c.owner
<where>
<if test="dbName != null">and t.owner = #{dbName}</if>
<if test="tableName != null">and t.TABLE_NAME = #{tableName}</if>

View File

@@ -5,7 +5,9 @@ import com.zyplayer.doc.db.framework.db.enums.DatabaseProductEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* 达梦数据查询服务实现类
@@ -42,17 +44,9 @@ public class DmServiceImpl extends DbBaseService {
public String getQueryPageSql(DataViewParam dataViewParam) {
String queryColumns = StringUtils.defaultIfBlank(dataViewParam.getRetainColumn(), "*");
if(!Objects.equals(queryColumns, "*")){
String[] queryColumnsArray = queryColumns.split(",");
String resultString = "";
for(int i=0;i<queryColumnsArray.length;i++){
queryColumnsArray[i] = "\""+queryColumnsArray[i]+"\"";
if(i < queryColumnsArray.length-1){
resultString +=queryColumnsArray[i] + ",";
}else{
resultString +=queryColumnsArray[i];
}
}
queryColumns = resultString;
queryColumns = Arrays.stream(queryColumns.split(","))
.map(word -> "\"" + word + "\"")
.collect(Collectors.joining(","));
}
StringBuilder sqlSb = new StringBuilder();
sqlSb.append(String.format("select %s from %s.%s", queryColumns, dataViewParam.getDbName(), dataViewParam.getTableName()));

View File

@@ -13,9 +13,11 @@ import org.springframework.stereotype.Service;
import java.io.IOException;
import java.sql.Clob;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* Oracle数据查询服务实现类
@@ -51,17 +53,9 @@ public class OracleServiceImpl extends DbBaseService {
public String getQueryPageSql(DataViewParam dataViewParam) {
String queryColumns = StringUtils.defaultIfBlank(dataViewParam.getRetainColumn(), "*");
if(!Objects.equals(queryColumns, "*")){
String[] queryColumnsArray = queryColumns.split(",");
String resultString = "";
for(int i=0;i<queryColumnsArray.length;i++){
queryColumnsArray[i] = "\""+queryColumnsArray[i]+"\"";
if(i < queryColumnsArray.length-1){
resultString +=queryColumnsArray[i] + ",";
}else{
resultString +=queryColumnsArray[i];
}
}
queryColumns = resultString;
queryColumns = Arrays.stream(queryColumns.split(","))
.map(word -> "\"" + word + "\"")
.collect(Collectors.joining(","));
}
StringBuilder sqlSb = new StringBuilder();
sqlSb.append(String.format("select %s from %s.%s", queryColumns, dataViewParam.getDbName(), dataViewParam.getTableName()));

View File

@@ -17,8 +17,10 @@ import javax.annotation.Resource;
import java.io.IOException;
import java.sql.Clob;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
/**
@@ -87,11 +89,16 @@ public class SqlserverServiceImpl extends DbBaseService {
@Override
public String getQueryPageSql(DataViewParam dataViewParam) {
String queryColumns = StringUtils.defaultIfBlank(dataViewParam.getRetainColumn(), "*");
if(!Objects.equals(queryColumns, "*")){
queryColumns = Arrays.stream(queryColumns.split(","))
.map(word -> "\"" + word + "\"")
.collect(Collectors.joining(","));
}
Integer pageNum = dataViewParam.getPageNum();
Integer pageSize = dataViewParam.getPageSize();
Integer rownumber = (pageNum-1)*pageSize;
StringBuilder sqlSb = new StringBuilder();
sqlSb.append(String.format("select top %s * from (select row_number() OVER(order by %s %s) as rowid, %s from %s..%s )A where rowid> %s",pageSize,dataViewParam.getOrderColumn(), dataViewParam.getOrderType(), queryColumns, dataViewParam.getDbName(), dataViewParam.getTableName(),rownumber));
sqlSb.append(String.format("select top %s %s from (select row_number() OVER(order by %s %s) as rowid, %s from %s..%s )A where rowid> %s",pageSize,queryColumns,dataViewParam.getOrderColumn(), dataViewParam.getOrderType(), queryColumns, dataViewParam.getDbName(), dataViewParam.getTableName(),rownumber));
if (StringUtils.isNotBlank(dataViewParam.getCondition())) {
sqlSb.append(String.format(" and %s", dataViewParam.getCondition()));
}