You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

258 lines
6.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package DatahyperService
import (
"dsDataex/MyHyper/DataHyper/DatahyperDAO"
"dsDataex/MyHyper/MySwagger"
"dsDataex/Utils/CommonUtil"
//"strconv"
"strings"
)
func GetResults(platform string, raw MySwagger.DatahyperQuery) (bool, string, int, []map[string]interface{}, error) {
queryID := raw.QueryID
var params []interface{}
res, msg, sql, _ := DatahyperDAO.GetQuerySQL(platform, queryID)
if res {
//reg := regexp.MustCompile(`(WHERE|where)`)
//if !reg.MatchString(sql) {
// sql += " WHERE "
//}
for no := 0; no < len(raw.QueryCondition); no++ {
params = append(params, raw.QueryCondition[no])
}
/*
if len(raw.QueryCondition) > 0 {
if find := strings.Contains(sql, "?"); find {
for _, v := range raw.QueryCondition {
var new string
switch v.(type) {
case int, int32, int64:
new = strconv.Itoa(int(v.(int64)))
break
case float64:
new = strconv.FormatFloat(v.(float64), 'f', -1, 64)
break
default:
new = v.(string)
}
sql = strings.Replace(sql, `?`, `'` + new + `'`, 1 )
}
}
}
*/
//reg1 := regexp.MustCompile(`(AS T|as T)$`)
if raw.QueryCombination != "" {
//if find := strings.Contains(strings.ToUpper(sql), "WHERE"); find {
// sql += " AND " + raw.QueryCombination
//} else {
// sql += " WHERE " + raw.QueryCombination
//}
sql += " WHERE " + raw.QueryCombination
}
if len(raw.QueryOrder) > 0 {
orderBy := strings.Join(raw.QueryOrder, ", ")
prefix := ""
//reg2 := regexp.MustCompile(`(ORDER|order)`)
//if ! reg1.MatchString(sql) {
// if reg2.MatchString(sql) {
// prefix = ", "
// } else {
// prefix = " ORDER BY "
// }
//} else {
// prefix = " ORDER BY "
//}
prefix = " ORDER BY "
sql += prefix + orderBy
}
//接收传入参数
var limit = raw.QueryPerPage
var offset = (raw.QueryPage - 1) * limit
switch raw.QueryFormat {
case "json":
res1, msg1, count, data, _ := DatahyperDAO.GetResults(platform, sql, params, limit, offset)
if res1 {
if len(raw.QueryCols) > 0 {
for k, v := range data {
for kk, _ := range v {
if CommonUtil.StringArrayContain((raw.QueryCols), kk) == false {
delete(data[k], kk)
}
}
}
}
return res1, msg1, count, data, nil
} else {
return res1, msg1, 0, nil, nil
}
break
default:
break
}
return false, "查询Format参数错误不支持此类型的数据格式转换", 0, nil, nil
} else {
return res, msg, 0, nil, nil
}
}
func GetRow(platform string, raw MySwagger.DatahyperQueryByID) (bool, string, map[string]interface{}, error) {
queryID := raw.QueryID
res, msg, sql, _ := DatahyperDAO.GetQuerySQL(platform, queryID)
if res {
switch raw.QueryFormat {
case "json":
res1, msg1, data, _ := DatahyperDAO.GetRow(platform, sql, raw.DataID)
if res1 {
if len(raw.QueryCols) > 0 {
for kk, _ := range data {
if CommonUtil.StringArrayContain((raw.QueryCols), kk) == false {
delete(data, kk)
}
}
}
return res1, msg1, data, nil
} else {
return res1, msg1, nil, nil
}
break
default:
break
}
return false, "查询Format参数错误不支持此类型的数据格式转换", nil, nil
} else {
return res, msg, nil, nil
}
}
func GetResultsFE(platform string, raw MySwagger.DatahyperQueryFE) (bool, string, int, []map[string]interface{}, error) {
queryID := raw.QueryID
var params []interface{}
res, msg, sql, _ := DatahyperDAO.GetQuerySQL(platform, queryID)
if res {
//reg := regexp.MustCompile(`(WHERE|where)`)
//if !reg.MatchString(sql) {
// sql += " WHERE " + sql
//}
for no := 0; no < len(raw.QueryCondition); no++ {
params = append(params, raw.QueryCondition[no])
}
/*
if len(raw.QueryCondition) > 0 {
if find := strings.Contains(sql, "?"); find {
for _, v := range raw.QueryCondition {
var new string
switch v.(type) {
case int, int32, int64:
new = strconv.Itoa(int(v.(int64)))
case float64:
new = strconv.FormatFloat(v.(float64), 'f', -1, 64)
default:
new = v.(string)
}
sql = strings.Replace(sql, `?`, `'` + new + `'`, 1 )
}
}
//condition := strings.Join(raw.QueryCondition, " AND ")
}
*/
if raw.QueryCombination != "" {
//if find := strings.Contains(strings.ToUpper(sql), "WHERE"); find {
// sql += " AND " + raw.QueryCombination
//} else {
// sql += " WHERE " + raw.QueryCombination
//}
sql += " WHERE " + raw.QueryCombination
}
if len(raw.QueryOrder) > 0 {
orderBy := strings.Join(raw.QueryOrder, ", ")
prefix := ""
//reg2 := regexp.MustCompile(`(ORDER|order)`)
//if ! reg1.MatchString(sql) {
// if reg2.MatchString(sql) {
// prefix = ", "
// } else {
// prefix = " ORDER BY "
// }
//} else {
// prefix = " ORDER BY "
//}
prefix = " ORDER BY "
sql += prefix + orderBy
}
//接收传入参数
var limit = raw.QueryPerPage
var offset = (raw.QueryPage - 1) * limit
switch raw.QueryFormat {
case "json":
res1, msg1, count, data, _ := DatahyperDAO.GetResults(platform, sql, params, limit, offset)
if res1 {
if len(raw.QueryCols) > 0 {
for k, v := range data {
for kk, _ := range v {
if CommonUtil.StringArrayContain((raw.QueryCols), kk) == false {
delete(data[k], kk)
}
}
}
}
return res1, msg1, count, data, nil
} else {
return res1, msg1, 0, nil, nil
}
break
default:
break
}
return false, "查询Format参数错误不支持此类型的数据格式转换", 0, nil, nil
} else {
return res, msg, 0, nil, nil
}
}
func GetRowFE(platform string, raw MySwagger.DatahyperQueryFEByID) (bool, string, map[string]interface{}, error) {
queryID := raw.QueryID
res, msg, sql, _ := DatahyperDAO.GetQuerySQL(platform, queryID)
if res {
switch raw.QueryFormat {
case "json":
res1, msg1, data, _ := DatahyperDAO.GetRow(platform, sql, raw.DataID)
if res1 {
if len(raw.QueryCols) > 0 {
for kk, _ := range data { //map[string]interface{}
if CommonUtil.StringArrayContain((raw.QueryCols), kk) == false {
delete(data, kk)
}
}
}
return res1, msg1, data, nil
} else {
return res1, msg1, nil, nil
}
break
default:
break
}
return false, "查询Format参数错误不支持此类型的数据格式转换", nil, nil
} else {
return res, msg, nil, nil
}
}