|
@@ -10,10 +10,7 @@ import com.bowintek.practice.services.service.GenSqlStringService;
|
|
|
import com.bowintek.practice.services.service.TempService;
|
|
|
import com.bowintek.practice.util.multipdb.DBTypeEnum;
|
|
|
import com.bowintek.practice.util.multipdb.SwitchDataSource;
|
|
|
-import com.bowintek.practice.vo.temp.SqlColumnModel;
|
|
|
-import com.bowintek.practice.vo.temp.SqlFieldModel;
|
|
|
-import com.bowintek.practice.vo.temp.TempObjectModel;
|
|
|
-import com.bowintek.practice.vo.temp.TempSaveResult;
|
|
|
+import com.bowintek.practice.vo.temp.*;
|
|
|
import org.apache.ibatis.jdbc.SqlRunner;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
@@ -60,6 +57,27 @@ public class GenSqlStringServiceImpl implements GenSqlStringService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public Map<String,Object> getMapBySqlString(String sqlString){
|
|
|
+ try {
|
|
|
+ //加载数据库驱动
|
|
|
+ Class.forName(postgreDriver);
|
|
|
+ Connection connection = DriverManager.getConnection(postgreUrl, postgreUsername, postgrePassword);
|
|
|
+ SqlRunner sqlRunner = new SqlRunner(connection);
|
|
|
+ return sqlRunner.selectOne(sqlString);
|
|
|
+ }
|
|
|
+ catch (Exception ex){
|
|
|
+ ex.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int getIntBySqlString(String sqlString, String colName) {
|
|
|
+ Map<String, Object> map = getMapBySqlString(sqlString);
|
|
|
+ return Integer.parseInt(map.get(colName).toString());
|
|
|
+ }
|
|
|
+
|
|
|
//查询语句生成,要素点:查询主表、关联子表、查询字段、查询条件
|
|
|
//查询主表:FROM (sr_subject.execSql) AS T
|
|
|
//查询字段-维度:sr_tempdimension.fieldAlias
|
|
@@ -68,7 +86,7 @@ public class GenSqlStringServiceImpl implements GenSqlStringService {
|
|
|
//输出字段 AS:ReFieldName0,1,2...;依据维度度量排序来
|
|
|
//表名 AS:T 主表,T0,1,2...;子表,依据维度度量子表排序来
|
|
|
@Override
|
|
|
- public String Generation(TempObjectModel data, String userID){
|
|
|
+ public GenSqlStringResult Generation(TempObjectModel data, int page, int rows, String userID){
|
|
|
//取得字段原本定义
|
|
|
data.sub = srSubjectMapper.selectByPrimaryKey(data.temp.getSubId());
|
|
|
List<SrSubjectfield> fileds = getSubjectfieldsBySubId(data.temp.getSubId());
|
|
@@ -103,6 +121,7 @@ public class GenSqlStringServiceImpl implements GenSqlStringService {
|
|
|
colString += MessageFormat.format("{0} {1} AS {2}\n\t",
|
|
|
getCommaString(fieldIndex), cm.getDisplayColumn(), getFieldName(fieldIndex));
|
|
|
groupString += MessageFormat.format("{0} {1}\n\t",getCommaString(fieldIndex), cm.getDisplayColumn());
|
|
|
+ cm.setMeasureColumn(cm.getDisplayColumn());
|
|
|
fieldIndex++;
|
|
|
}
|
|
|
|
|
@@ -115,6 +134,7 @@ public class GenSqlStringServiceImpl implements GenSqlStringService {
|
|
|
getCommaString(fieldIndex),
|
|
|
getMeasurString(msr.getOperation(), cm.getDisplayColumn()),
|
|
|
getFieldName(fieldIndex));
|
|
|
+ cm.setMeasureColumn(getMeasurString(msr.getOperation(), cm.getDisplayColumn()));
|
|
|
fieldIndex++;
|
|
|
}
|
|
|
|
|
@@ -130,21 +150,31 @@ public class GenSqlStringServiceImpl implements GenSqlStringService {
|
|
|
|
|
|
//生成排序字段
|
|
|
String orderByString = "";
|
|
|
- if(IsNullEmpty(data.orderBy) || !aliasMap.containsKey(data.orderBy)) data.orderBy = aliasMap.keySet().stream().findFirst().get();
|
|
|
+ if(IsNullEmpty(data.orderBy) || !aliasMap.containsKey(data.orderBy))
|
|
|
+ data.orderBy = aliasMap.keySet().stream().findFirst().get();
|
|
|
System.out.println("data.orderBy:"+data.orderBy);
|
|
|
|
|
|
if(aliasMap.containsKey(data.orderBy)){
|
|
|
if(IsNullEmpty(data.orderByString)) data.orderByString = "asc";
|
|
|
SqlColumnModel cmOrder = aliasMap.get(data.orderBy);
|
|
|
- orderByString = MessageFormat.format("ORDER BY {0} {1}", cmOrder.getDisplayColumn(), data.orderByString);
|
|
|
+ orderByString = MessageFormat.format("ORDER BY {0} {1}", cmOrder.getMeasureColumn(), data.orderByString);
|
|
|
}
|
|
|
|
|
|
- String mainSqlStrimg = MessageFormat.format("SELECT {0} \nFROM {1} AS T {2} \nWHERE 1=1 {3} \nGROUP BY {4} \n\n{5}",
|
|
|
- colString, data.sub.getExecSql(), leftTableString, whereString, groupString, orderByString);
|
|
|
- System.out.println(mainSqlStrimg);
|
|
|
- return mainSqlStrimg;
|
|
|
- }
|
|
|
+ String limitString = MessageFormat.format("LIMIT {0} OFFSET {1}", rows, (page-1) * rows);
|
|
|
+
|
|
|
+ GenSqlStringResult result = new GenSqlStringResult();
|
|
|
+ String mainSqlString = MessageFormat.format("SELECT {0} \nFROM {1} AS T {2} \nWHERE 1=1 {3} \nGROUP BY {4} \n{5} \n{6}",
|
|
|
+ colString, data.sub.getExecSql(), leftTableString, whereString, groupString, orderByString, limitString);
|
|
|
+ result.setMainSqlString(mainSqlString);
|
|
|
|
|
|
+ String countSqlString = MessageFormat.format("SELECT COUNT(*) CNT FROM (SELECT {0} \nFROM {1} AS T {2} \nWHERE 1=1 {3} \nGROUP BY {4})",
|
|
|
+ colString, data.sub.getExecSql(), leftTableString, whereString, groupString);
|
|
|
+ result.setCountSqlStrimg(countSqlString);
|
|
|
+
|
|
|
+ System.out.println(mainSqlString);
|
|
|
+ System.out.println(countSqlString);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
|
|
|
|
|
|
public int getDataType(String typeString){
|
|
@@ -165,7 +195,7 @@ public class GenSqlStringServiceImpl implements GenSqlStringService {
|
|
|
return genRemark("genWhereString", stf.getTempFeildId());
|
|
|
|
|
|
if(stf.getOperation().equals("="))
|
|
|
- return MessageFormat.format("\n\tAND T.{0}='{1}'", stf.getFieldAlias(),
|
|
|
+ return MessageFormat.format("\n\tAND T.{0}={1}", stf.getFieldAlias(),
|
|
|
"'"+stf.getValue1()+"'");
|
|
|
else if(stf.getOperation().equals("like"))
|
|
|
return MessageFormat.format("\n\tAND T.{0} like {1}", stf.getFieldAlias(),
|
|
@@ -239,7 +269,7 @@ public class GenSqlStringServiceImpl implements GenSqlStringService {
|
|
|
}
|
|
|
|
|
|
public String getFieldName(int index){
|
|
|
- return "ReFieldName"+index;
|
|
|
+ return "RENAME"+index;
|
|
|
}
|
|
|
|
|
|
public String getCommaString(boolean is){
|