|
|
@ -3,6 +3,7 @@ package dbquery |
|
|
|
import ( |
|
|
|
"database/sql" |
|
|
|
"log" |
|
|
|
"strconv" |
|
|
|
|
|
|
|
"errors" |
|
|
|
"strings" |
|
|
@ -277,6 +278,280 @@ func GetData(dbName, table string, title string, where map[string]string, limit |
|
|
|
return count, info, nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 查找一条记录 |
|
|
|
* @param dbName 数据表名 |
|
|
|
* @param title 查询字段名 |
|
|
|
*/ |
|
|
|
func GetRow(dbName, table_name, alias string, titles string, join [][]string, where, where_or []string, valueList []interface{}, orderby string, debug bool) (int, map[string]string, error) { |
|
|
|
var count int = 0 |
|
|
|
info := make(map[string]string) |
|
|
|
|
|
|
|
if dbName == "" && table_name == "" { |
|
|
|
return count, info, errors.New("没有数据表") |
|
|
|
} |
|
|
|
|
|
|
|
table := getTableName(dbName, table_name) |
|
|
|
|
|
|
|
var sql_str, title string |
|
|
|
|
|
|
|
if titles != "" { |
|
|
|
title = titles |
|
|
|
} else { |
|
|
|
title = "*" |
|
|
|
} |
|
|
|
sql_str = helper.StringJoin("/*slave*/ select ", title) |
|
|
|
|
|
|
|
if alias != "" { |
|
|
|
table = helper.StringJoin(table, " as ", alias) |
|
|
|
} |
|
|
|
|
|
|
|
sql_str = helper.StringJoin(sql_str, " from ", table) |
|
|
|
|
|
|
|
if len(join) > 0 { |
|
|
|
for _, joinitem := range join { |
|
|
|
if len(joinitem) < 2 { |
|
|
|
continue |
|
|
|
} |
|
|
|
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]) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if len(where) > 0 || len(where_or) > 0 { |
|
|
|
sql_str = helper.StringJoin(sql_str, " where ") |
|
|
|
} |
|
|
|
if len(where) > 0 { |
|
|
|
sql_str = helper.StringJoin(sql_str, " (", strings.Join(where, " and "), " ) ") |
|
|
|
} |
|
|
|
if len(where_or) > 0 { |
|
|
|
if len(where) > 0 { |
|
|
|
sql_str = helper.StringJoin(sql_str, " or ", strings.Join(where_or, " or ")) |
|
|
|
} else { |
|
|
|
sql_str = helper.StringJoin(sql_str, strings.Join(where_or, " or ")) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if orderby != "" { |
|
|
|
sql_str = helper.StringJoin(sql_str, " order by ", orderby) |
|
|
|
} |
|
|
|
|
|
|
|
if debug { |
|
|
|
log.Println("query sql:", sql_str, valueList) |
|
|
|
} |
|
|
|
condition_len := 0 //所有条件数
|
|
|
|
for _, ch2 := range sql_str { |
|
|
|
if string(ch2) == "?" { |
|
|
|
condition_len++ |
|
|
|
} |
|
|
|
} |
|
|
|
if condition_len != len(valueList) { |
|
|
|
return 0, nil, errors.New("参数错误,条件值错误") |
|
|
|
} |
|
|
|
|
|
|
|
var rows *sql.Rows |
|
|
|
var err error |
|
|
|
var queryNum int = 0 |
|
|
|
for queryNum < 2 { //如发生错误,继续查询2次,防止数据库连接断开问题
|
|
|
|
|
|
|
|
rows, err = DB.Query(sql_str, valueList...) |
|
|
|
|
|
|
|
if err == nil { |
|
|
|
break |
|
|
|
} else { |
|
|
|
log.Println(err) |
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 500) |
|
|
|
} |
|
|
|
queryNum++ |
|
|
|
} |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
rows.Close() |
|
|
|
return count, info, err |
|
|
|
} |
|
|
|
|
|
|
|
columns, _ := rows.Columns() |
|
|
|
scanArgs := make([]interface{}, len(columns)) |
|
|
|
values := make([]interface{}, len(columns)) |
|
|
|
|
|
|
|
for i := range values { |
|
|
|
scanArgs[i] = &values[i] |
|
|
|
} |
|
|
|
|
|
|
|
var index string |
|
|
|
var rowerr error |
|
|
|
for rows.Next() { |
|
|
|
rowerr = rows.Scan(scanArgs...) |
|
|
|
if rowerr == nil { |
|
|
|
for i, col := range values { |
|
|
|
if col != nil { |
|
|
|
index = helper.StrFirstToUpper(columns[i]) |
|
|
|
info[index] = helper.ToString(col) |
|
|
|
} |
|
|
|
} |
|
|
|
count++ |
|
|
|
} else { |
|
|
|
log.Println("ERROR", rowerr) |
|
|
|
} |
|
|
|
} |
|
|
|
rows.Close() |
|
|
|
if rowerr != nil { |
|
|
|
return count, info, rowerr |
|
|
|
} |
|
|
|
return count, info, nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 查找多条记录 |
|
|
|
* @param dbName 数据表名 |
|
|
|
* @param title 查询字段名 |
|
|
|
*/ |
|
|
|
func FetchRows(dbName, table_name, alias string, titles string, join [][]string, where, where_or []string, valueList []interface{}, orderby string, page int, page_size int, debug bool) (int, []map[string]string, error) { |
|
|
|
var count int = 0 |
|
|
|
list := make([]map[string]string, 0) |
|
|
|
|
|
|
|
if dbName == "" && table_name == "" { |
|
|
|
return count, list, errors.New("没有数据表") |
|
|
|
} |
|
|
|
|
|
|
|
table := getTableName(dbName, table_name) |
|
|
|
|
|
|
|
var sql_str, title string |
|
|
|
|
|
|
|
if titles != "" { |
|
|
|
title = titles |
|
|
|
} else { |
|
|
|
title = "*" |
|
|
|
} |
|
|
|
sql_str = helper.StringJoin("/*slave*/ select ", title) |
|
|
|
|
|
|
|
if alias != "" { |
|
|
|
table = helper.StringJoin(table, " as ", alias) |
|
|
|
} |
|
|
|
|
|
|
|
sql_str = helper.StringJoin(sql_str, " from ", table) |
|
|
|
|
|
|
|
if len(join) > 0 { |
|
|
|
for _, joinitem := range join { |
|
|
|
if len(joinitem) < 2 { |
|
|
|
continue |
|
|
|
} |
|
|
|
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]) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if len(where) > 0 || len(where_or) > 0 { |
|
|
|
sql_str = helper.StringJoin(sql_str, " where ") |
|
|
|
} |
|
|
|
if len(where) > 0 { |
|
|
|
sql_str = helper.StringJoin(sql_str, " (", strings.Join(where, " and "), " ) ") |
|
|
|
} |
|
|
|
if len(where_or) > 0 { |
|
|
|
if len(where) > 0 { |
|
|
|
sql_str = helper.StringJoin(sql_str, " or ", strings.Join(where_or, " or ")) |
|
|
|
} else { |
|
|
|
sql_str = helper.StringJoin(sql_str, strings.Join(where_or, " or ")) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if orderby != "" { |
|
|
|
sql_str = helper.StringJoin(sql_str, " order by ", orderby) |
|
|
|
} |
|
|
|
|
|
|
|
if page > 0 || page_size > 0 { |
|
|
|
|
|
|
|
if page < 1 { |
|
|
|
page = 1 |
|
|
|
} |
|
|
|
if page_size < 1 { |
|
|
|
page_size = 10 |
|
|
|
} |
|
|
|
from := strconv.Itoa((page - 1) * page_size) |
|
|
|
offset := strconv.Itoa(page_size) |
|
|
|
if from != "" && offset != "" { |
|
|
|
sql_str = helper.StringJoin(sql_str, " limit ", from, " , ", offset) |
|
|
|
} |
|
|
|
} |
|
|
|
if debug { |
|
|
|
log.Println("query sql:", sql_str, valueList) |
|
|
|
} |
|
|
|
condition_len := 0 //所有条件数
|
|
|
|
for _, ch2 := range sql_str { |
|
|
|
if string(ch2) == "?" { |
|
|
|
condition_len++ |
|
|
|
} |
|
|
|
} |
|
|
|
if condition_len != len(valueList) { |
|
|
|
return 0, list, errors.New("参数错误,条件值错误") |
|
|
|
} |
|
|
|
|
|
|
|
var rows *sql.Rows |
|
|
|
var err error |
|
|
|
var queryNum int = 0 |
|
|
|
for queryNum < 2 { //如发生错误,继续查询2次,防止数据库连接断开问题
|
|
|
|
|
|
|
|
rows, err = DB.Query(sql_str, valueList...) |
|
|
|
|
|
|
|
if err == nil { |
|
|
|
break |
|
|
|
} else { |
|
|
|
log.Println(err) |
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 500) |
|
|
|
} |
|
|
|
queryNum++ |
|
|
|
} |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
rows.Close() |
|
|
|
return 0, list, err |
|
|
|
} |
|
|
|
|
|
|
|
columns, _ := rows.Columns() |
|
|
|
scanArgs := make([]interface{}, len(columns)) |
|
|
|
values := make([]interface{}, len(columns)) |
|
|
|
|
|
|
|
for i := range values { |
|
|
|
scanArgs[i] = &values[i] |
|
|
|
} |
|
|
|
|
|
|
|
var index string |
|
|
|
var rowerr error |
|
|
|
var info map[string]string |
|
|
|
|
|
|
|
for rows.Next() { |
|
|
|
rowerr = rows.Scan(scanArgs...) |
|
|
|
info = make(map[string]string) |
|
|
|
if rowerr == nil { |
|
|
|
for i, col := range values { |
|
|
|
if col != nil { |
|
|
|
index = helper.StrFirstToUpper(columns[i]) |
|
|
|
info[index] = helper.ToString(col) |
|
|
|
} |
|
|
|
} |
|
|
|
count++ |
|
|
|
} else { |
|
|
|
log.Println("ERROR", rowerr) |
|
|
|
} |
|
|
|
if len(info) > 0 { |
|
|
|
list = append(list, info) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
rows.Close() |
|
|
|
return count, list, nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
func GetInfo(dbName, table string, title string, where map[string]string) (map[string]string, error) { |
|
|
|
|
|
|
|
count, info, gzErr := GetData(dbName, table, title, where, nil) |
|
|
|