diff --git a/dsDataexV1.1/MyReport/GPSql/GPSqlDAO/GPSqlDAO.go b/dsDataexV1.1/MyReport/GPSql/GPSqlDAO/GPSqlDAO.go index 3b7e09a8..c846bf94 100644 --- a/dsDataexV1.1/MyReport/GPSql/GPSqlDAO/GPSqlDAO.go +++ b/dsDataexV1.1/MyReport/GPSql/GPSqlDAO/GPSqlDAO.go @@ -1 +1,20 @@ package GPSqlDAO + +import ( + "dsDataex/Utils/CacheUtil" + "html" +) + +func GetQuerybyCode(queryCode string) (bool, string, map[string]interface{}, error) { + + sql := "SELECT id from t_report_sqlquery where delete_flag = -1 and enable_flag = 1 and query_code = '" + html.EscapeString(queryCode) + "'" + + //通过SQL获取带缓存的数据 + list, count, _ := CacheUtil.Page(sql, 10, 0) + + if count == 1 { + return true, "数据获取成功", list[0], nil + } else { + return false, "数据获取失败,queryID不存在", nil, nil + } +} diff --git a/dsDataexV1.1/MyReport/GPSql/GPSqlOpenapi/GPSqlOpenapi.go b/dsDataexV1.1/MyReport/GPSql/GPSqlOpenapi/GPSqlOpenapi.go index 88795b6d..01e5dc61 100644 --- a/dsDataexV1.1/MyReport/GPSql/GPSqlOpenapi/GPSqlOpenapi.go +++ b/dsDataexV1.1/MyReport/GPSql/GPSqlOpenapi/GPSqlOpenapi.go @@ -24,12 +24,12 @@ func QuerySimpleGP(c *gin.Context) { var input MySwagger.QuerySimpleGP if err := c.ShouldBindJSON(&input); err != nil { - c.JSON(http.StatusOK, MySwagger.DataResult{Success: false, Message: "简单查询数据JSON格式错误"}) + c.JSON(http.StatusOK, MySwagger.DataResult{Success: false, Message: "查询数据JSON格式错误"}) return } if input.Query.QueryID == "" { - c.JSON(http.StatusOK, MySwagger.DataResult{Success: false, Message: "简单查询数据QueryID不能为空"}) + c.JSON(http.StatusOK, MySwagger.DataResult{Success: false, Message: "查询数据QueryID不能为空"}) return } @@ -51,10 +51,20 @@ func QuerySimpleGP(c *gin.Context) { flag2, _ := DataexService.CheckDatasourceSql(systemID, input.Query.QueryID) if flag2 == false { - //c.JSON(http.StatusOK, MySwagger.DataResult{Success: false,Message: "接入系统ES-SQL查询权限验证失败"}) + //c.JSON(http.StatusOK, MySwagger.DataResult{Success: false,Message: "接入系统GP-SQL查询权限验证失败"}) //return } + //TODO:输入参数 SQL注入检测 + if len(input.Query.QueryParam) > 0 { + + } + + //TODO: 缓存数据 + if input.QueryCache == 1 || input.QueryCache == 2 { + + } + res, msg, data := ESSqlService.QuerySimple(input.Query.QueryID, input.Query.QueryParam, input.QueryGroup, input.QueryCount, input.QueryFormat) if res { diff --git a/dsDataexV1.1/MyReport/GPSql/GPSqlService/GPSqlService.go b/dsDataexV1.1/MyReport/GPSql/GPSqlService/GPSqlService.go index bfe56e31..1e1870be 100644 --- a/dsDataexV1.1/MyReport/GPSql/GPSqlService/GPSqlService.go +++ b/dsDataexV1.1/MyReport/GPSql/GPSqlService/GPSqlService.go @@ -1 +1,74 @@ package GPSqlService + +import ( + "dsDataex/MyReport/GPSql/GPSqlDAO" + "dsDataex/Utils/ES7SqlUtil" + "dsDataex/Utils/GPUtil" + "encoding/json" +) + +func QuerySimple(queryCode string, queryParam []string, queryGroup []string, queryCount []string, queryFormat string) (bool, string, string) { + + res, msg, data, _ := GPSqlDAO.GetQuerybyCode(queryCode) + + if res { + + var gpSql = data["query_sql"].(string) + + switch queryFormat { + case "table": + flag, result := GPUtil.SqlQueryXml(gpSql, queryParam) + + return flag, "查询成功", result + + break + case "json": + flag, result := GPUtil.SqlQueryJson(gpSql, queryParam) + + return flag, "查询成功", result + + break + case "echarts": + result := ES7SqlUtil.SqlQueryObj(gpSql, queryParam) + + echarts := ConvEcharts(result, queryGroup, queryCount) + + if len(queryGroup) <= 2 { + + bytes, _ := json.Marshal(echarts) + return true, "查询成功,格式转换成功", string(bytes) + } else { + + return false, "查询成功,格式转换失败,Echarts格式不支持三级GroupBy", "" + } + + break + case "antd": + result := ES7SqlUtil.SqlQueryObj(gpSql, queryParam) + + antd := ConvAntd(result) + + bytes, _ := json.Marshal(antd) + + return true, "查询成功,格式转换成功", string(bytes) + + break + default: + break + } + + return false, "查询Format参数错误,不支持此类型的数据格式转换", "" + } else { + return false, msg, "" + } +} + +func ConvEcharts(result ES7SqlUtil.ESSqlResult, group []string, count []string) interface{} { + + return nil +} + +func ConvAntd(result ES7SqlUtil.ESSqlResult) interface{} { + + return nil +} diff --git a/dsDataexV1.1/Utils/GPUtil/GPUtil.go b/dsDataexV1.1/Utils/GPUtil/GPUtil.go index 20ba7334..b71a0895 100644 --- a/dsDataexV1.1/Utils/GPUtil/GPUtil.go +++ b/dsDataexV1.1/Utils/GPUtil/GPUtil.go @@ -15,6 +15,13 @@ import ( var Engine *xorm.Engine var ServerVersion string +/** + * @Author zhangjun + * @Description + * @Date 2021-02-06 10:00 + * @Param + * @return + **/ func init() { host := ConfigUtil.GreenPlumIp port := ConfigUtil.GreenPlumPort @@ -60,3 +67,57 @@ func init() { Engine.ShowSQL(true) // 则会在控制台打印出生成的SQL语句 Engine.Logger().SetLevel(log.LOG_DEBUG) //则会在控制台打印info及以上的信息 } + +/** + * @Author zhangjun + * @Description + * @Date 2021-02-06 10:00 + * @Param + * @return + **/ +func SqlQuery(sql string, param []string) (bool, []map[string]interface{}) { + + result, err := Engine.SQL(sql, param).QueryInterface() + + if err != nil { + return false, result + } else { + return true, result + } +} + +/** + * @Author zhangjun + * @Description + * @Date 2021-02-06 11:11 + * @Param + * @return + **/ +func SqlQueryJson(sql string, param []string) (bool, string) { + + result, err := Engine.SQL(sql, param).Query().Json() + + if err != nil { + return false, err.Error() + } else { + return true, result + } +} + +/** + * @Author zhangjun + * @Description + * @Date 2021-02-06 11:16 + * @Param + * @return + **/ +func SqlQueryXml(sql string, param []string) (bool, string) { + + result, err := Engine.SQL(sql, param).Query().Xml() + + if err != nil { + return false, err.Error() + } else { + return true, result + } +}