|
|
@ -295,7 +295,7 @@ func GetData(dbName, table string, title string, where map[string]string, limit |
|
|
|
* @param dbName 数据表名 |
|
|
|
* @param title 查询字段名 |
|
|
|
*/ |
|
|
|
func GetRow(dbName, table_name, alias string, titles string, join [][]string, where, where_or []string, valueList []interface{}, orderby, groupby, having string, debug bool) (int, map[string]string, error) { |
|
|
|
func GetRow(dbName, table_name, alias string, titles string, with, join [][]string, where, where_or []string, valueList []interface{}, orderby, groupby, having string, debug bool) (int, map[string]string, error) { |
|
|
|
var count int = 0 |
|
|
|
info := make(map[string]string) |
|
|
|
|
|
|
@ -304,7 +304,7 @@ func GetRow(dbName, table_name, alias string, titles string, join [][]string, wh |
|
|
|
} |
|
|
|
|
|
|
|
table := "" |
|
|
|
if strings.Contains(table_name, "select ") { |
|
|
|
if strings.Contains(table_name, "select ") || strings.HasPrefix(table, "(") { |
|
|
|
table = table_name |
|
|
|
} else { |
|
|
|
table = getTableName(dbName, table_name) |
|
|
@ -316,10 +316,33 @@ func GetRow(dbName, table_name, alias string, titles string, join [][]string, wh |
|
|
|
} else { |
|
|
|
title = "*" |
|
|
|
} |
|
|
|
withSql := "" |
|
|
|
if len(with) > 0 { |
|
|
|
var builder strings.Builder |
|
|
|
builder.WriteString("WITH ") |
|
|
|
boo := false |
|
|
|
for k, v := range with { |
|
|
|
if len(v) < 2 { |
|
|
|
continue |
|
|
|
} |
|
|
|
if k != 0 { |
|
|
|
builder.WriteString(", ") |
|
|
|
} |
|
|
|
builder.WriteString(v[1]) |
|
|
|
builder.WriteString(" as (") |
|
|
|
builder.WriteString(v[0]) |
|
|
|
builder.WriteString(")") |
|
|
|
boo = true |
|
|
|
} |
|
|
|
if boo { |
|
|
|
builder.WriteString(" ") |
|
|
|
withSql = builder.String() |
|
|
|
} |
|
|
|
} |
|
|
|
if DB_PROVIDER == "TencentDB" { |
|
|
|
sql_str = helper.StringJoin("/*slave*/ select ", title) |
|
|
|
sql_str = helper.StringJoin("/*slave*/ ", withSql, " select ", title) |
|
|
|
} else { |
|
|
|
sql_str = helper.StringJoin("select ", title) |
|
|
|
sql_str = helper.StringJoin(withSql, "select ", title) |
|
|
|
} |
|
|
|
if alias != "" { |
|
|
|
table = helper.StringJoin(table, " as ", alias) |
|
|
@ -328,17 +351,29 @@ func GetRow(dbName, table_name, alias string, titles string, join [][]string, wh |
|
|
|
sql_str = helper.StringJoin(sql_str, " from ", table) |
|
|
|
|
|
|
|
if len(join) > 0 { |
|
|
|
var builder strings.Builder |
|
|
|
for _, joinitem := range join { |
|
|
|
if len(joinitem) < 2 { |
|
|
|
continue |
|
|
|
} |
|
|
|
if len(joinitem) == 4 { |
|
|
|
sql_str = helper.StringJoin(sql_str, " ", joinitem[2], " join ", joinitem[0], " on ", joinitem[1]) |
|
|
|
} else if len(joinitem) == 3 { |
|
|
|
sql_str = helper.StringJoin(sql_str, " ", joinitem[2], " join ", getTableName(dbName, joinitem[0]), " on ", joinitem[1]) |
|
|
|
} else { //默认左连接
|
|
|
|
sql_str = helper.StringJoin(sql_str, " left join ", getTableName(dbName, joinitem[0]), " on ", joinitem[1]) |
|
|
|
builder.WriteString(sql_str) |
|
|
|
builder.WriteString(" ") |
|
|
|
if len(joinitem) >= 3 { |
|
|
|
builder.WriteString(joinitem[2]) |
|
|
|
} else { |
|
|
|
builder.WriteString("left") |
|
|
|
} |
|
|
|
builder.WriteString(" join ") |
|
|
|
if strings.Contains(joinitem[0], "select ") || strings.HasPrefix(joinitem[0], "(") || len(joinitem) > 4 { |
|
|
|
builder.WriteString(joinitem[0]) |
|
|
|
} else { |
|
|
|
builder.WriteString(getTableName(dbName, joinitem[0])) |
|
|
|
} |
|
|
|
builder.WriteString(" on ") |
|
|
|
builder.WriteString(joinitem[1]) |
|
|
|
} |
|
|
|
if builder.Len() > 0 { |
|
|
|
sql_str = builder.String() |
|
|
|
} |
|
|
|
} |
|
|
|
if len(where) > 0 || len(where_or) > 0 { |
|
|
@ -449,7 +484,7 @@ func GetRow(dbName, table_name, alias string, titles string, join [][]string, wh |
|
|
|
* @param dbName 数据表名 |
|
|
|
* @param title 查询字段名 |
|
|
|
*/ |
|
|
|
func FetchRows(dbName, table_name, alias string, titles string, join [][]string, where, where_or []string, valueList []interface{}, orderby, groupby, having string, page int, page_size int, debug bool) (int, []map[string]string, error) { |
|
|
|
func FetchRows(dbName, table_name, alias string, titles string, with, join [][]string, where, where_or []string, valueList []interface{}, orderby, groupby, having string, page int, page_size int, debug bool) (int, []map[string]string, error) { |
|
|
|
var count int = 0 |
|
|
|
list := make([]map[string]string, 0) |
|
|
|
|
|
|
@ -457,7 +492,7 @@ func FetchRows(dbName, table_name, alias string, titles string, join [][]string, |
|
|
|
return count, list, errors.New("没有数据表") |
|
|
|
} |
|
|
|
table := "" |
|
|
|
if strings.Contains(table_name, "select ") { |
|
|
|
if strings.Contains(table_name, "select ") || strings.HasPrefix(table, "(") { |
|
|
|
table = table_name |
|
|
|
} else { |
|
|
|
table = getTableName(dbName, table_name) |
|
|
@ -470,10 +505,33 @@ func FetchRows(dbName, table_name, alias string, titles string, join [][]string, |
|
|
|
} else { |
|
|
|
title = "*" |
|
|
|
} |
|
|
|
withSql := "" |
|
|
|
if len(with) > 0 { |
|
|
|
var builder strings.Builder |
|
|
|
builder.WriteString("WITH ") |
|
|
|
boo := false |
|
|
|
for k, v := range with { |
|
|
|
if len(v) < 2 { |
|
|
|
continue |
|
|
|
} |
|
|
|
if k != 0 { |
|
|
|
builder.WriteString(", ") |
|
|
|
} |
|
|
|
builder.WriteString(v[1]) |
|
|
|
builder.WriteString(" as (") |
|
|
|
builder.WriteString(v[0]) |
|
|
|
builder.WriteString(")") |
|
|
|
boo = true |
|
|
|
} |
|
|
|
if boo { |
|
|
|
builder.WriteString(" ") |
|
|
|
withSql = builder.String() |
|
|
|
} |
|
|
|
} |
|
|
|
if DB_PROVIDER == "TencentDB" { |
|
|
|
sql_str = helper.StringJoin("/*slave*/ select ", title) |
|
|
|
sql_str = helper.StringJoin("/*slave*/ ", withSql, " select ", title) |
|
|
|
} else { |
|
|
|
sql_str = helper.StringJoin("select ", title) |
|
|
|
sql_str = helper.StringJoin(withSql, "select ", title) |
|
|
|
} |
|
|
|
if alias != "" { |
|
|
|
table = helper.StringJoin(table, " as ", alias) |
|
|
@ -482,17 +540,29 @@ func FetchRows(dbName, table_name, alias string, titles string, join [][]string, |
|
|
|
sql_str = helper.StringJoin(sql_str, " from ", table) |
|
|
|
|
|
|
|
if len(join) > 0 { |
|
|
|
var builder strings.Builder |
|
|
|
for _, joinitem := range join { |
|
|
|
if len(joinitem) < 2 { |
|
|
|
continue |
|
|
|
} |
|
|
|
if len(joinitem) == 4 { |
|
|
|
sql_str = helper.StringJoin(sql_str, " ", joinitem[2], " join ", joinitem[0], " on ", joinitem[1]) |
|
|
|
} else if len(joinitem) == 3 { |
|
|
|
sql_str = helper.StringJoin(sql_str, " ", joinitem[2], " join ", getTableName(dbName, joinitem[0]), " on ", joinitem[1]) |
|
|
|
} else { //默认左连接
|
|
|
|
sql_str = helper.StringJoin(sql_str, " left join ", getTableName(dbName, joinitem[0]), " on ", joinitem[1]) |
|
|
|
builder.WriteString(sql_str) |
|
|
|
builder.WriteString(" ") |
|
|
|
if len(joinitem) >= 3 { |
|
|
|
builder.WriteString(joinitem[2]) |
|
|
|
} else { |
|
|
|
builder.WriteString("left") |
|
|
|
} |
|
|
|
builder.WriteString(" join ") |
|
|
|
if strings.Contains(joinitem[0], "select ") || strings.HasPrefix(joinitem[0], "(") || len(joinitem) > 4 { |
|
|
|
builder.WriteString(joinitem[0]) |
|
|
|
} else { |
|
|
|
builder.WriteString(getTableName(dbName, joinitem[0])) |
|
|
|
} |
|
|
|
builder.WriteString(" on ") |
|
|
|
builder.WriteString(joinitem[1]) |
|
|
|
} |
|
|
|
if builder.Len() > 0 { |
|
|
|
sql_str = builder.String() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|