|
@ -45,16 +45,33 @@ func Insert(dbName, table string, data map[string]string) (int64, error) { |
|
|
valueList[i] = value |
|
|
valueList[i] = value |
|
|
i++ |
|
|
i++ |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
result, err := DB.Exec("insert into "+dbName+" ("+strings.Join(keyList, ",")+") value("+strings.Join(keyStr, ",")+")", valueList...) |
|
|
|
|
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
log.Println("ERROR|插入", dbName, "数据失败,", err) |
|
|
|
|
|
return insertId, err |
|
|
|
|
|
|
|
|
var Sql string |
|
|
|
|
|
Sql = "insert into " + dbName + " (" + strings.Join(keyList, ",") + ") values (" + strings.Join(keyStr, ",") + ")" |
|
|
|
|
|
if DB_PROVIDER == "PgsqlDb" { |
|
|
|
|
|
Sql = sqlx.Rebind(sqlx.DOLLAR, Sql) |
|
|
|
|
|
Sql = helper.StringJoin(Sql, " RETURNING id") |
|
|
|
|
|
stmt, err = DB.Prepare(Sql) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return 0, errors.New("创建失败:" + err.Error()) |
|
|
|
|
|
} |
|
|
|
|
|
row := stmt.QueryRow(valueList...) |
|
|
|
|
|
var id int64 |
|
|
|
|
|
err = row.Scan(&id) // 扫描 RETURNING 返回的 ID
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return 0, errors.New("创建失败:" + err.Error()) |
|
|
|
|
|
} |
|
|
|
|
|
return id, nil |
|
|
} else { |
|
|
} else { |
|
|
insertId, _ = result.LastInsertId() |
|
|
|
|
|
time.Sleep(time.Second * 2) |
|
|
|
|
|
return insertId, nil |
|
|
|
|
|
|
|
|
result, err := DB.Exec(Sql, valueList...) |
|
|
|
|
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
log.Println("ERROR|插入", dbName, "数据失败,", err) |
|
|
|
|
|
return insertId, err |
|
|
|
|
|
} else { |
|
|
|
|
|
insertId, _ = result.LastInsertId() |
|
|
|
|
|
time.Sleep(time.Second * 2) |
|
|
|
|
|
return insertId, nil |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
@ -68,7 +85,6 @@ func Update(dbName, table string, data map[string]string, where map[string]strin |
|
|
if dbName == "" && table == "" { |
|
|
if dbName == "" && table == "" { |
|
|
return rowsAffected, errors.New("没有数据表") |
|
|
return rowsAffected, errors.New("没有数据表") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if strings.Contains(table, "select ") { |
|
|
if strings.Contains(table, "select ") { |
|
|
dbName = table |
|
|
dbName = table |
|
|
} else { |
|
|
} else { |
|
@ -110,7 +126,12 @@ func Update(dbName, table string, data map[string]string, where map[string]strin |
|
|
log.Println("ERROR|修改数据表", dbName, "时条件中有空数据,条件:", where, "数据:", data) |
|
|
log.Println("ERROR|修改数据表", dbName, "时条件中有空数据,条件:", where, "数据:", data) |
|
|
return rowsAffected, errors.New("条件中有空数据") |
|
|
return rowsAffected, errors.New("条件中有空数据") |
|
|
} |
|
|
} |
|
|
result, err := DB.Exec("update "+dbName+" set "+strings.Join(keyList, " , ")+" where "+strings.Join(whereStr, " and "), valueList...) |
|
|
|
|
|
|
|
|
var Sql string |
|
|
|
|
|
Sql = "update " + dbName + " set " + strings.Join(keyList, " , ") + " where " + strings.Join(whereStr, " and ") |
|
|
|
|
|
if DB_PROVIDER == "PgsqlDb" { |
|
|
|
|
|
Sql = sqlx.Rebind(sqlx.DOLLAR, Sql) |
|
|
|
|
|
} |
|
|
|
|
|
result, err := DB.Exec(Sql, valueList...) |
|
|
|
|
|
|
|
|
if err != nil { |
|
|
if err != nil { |
|
|
log.Println("ERROR|修改", dbName, "数据失败,", err) |
|
|
log.Println("ERROR|修改", dbName, "数据失败,", err) |
|
@ -132,7 +153,6 @@ func Delete(dbName, table string, data map[string]string, del_count ...string) ( |
|
|
if dbName == "" && table == "" { |
|
|
if dbName == "" && table == "" { |
|
|
return count, errors.New("没有数据表") |
|
|
return count, errors.New("没有数据表") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if strings.Contains(table, "select ") { |
|
|
if strings.Contains(table, "select ") { |
|
|
dbName = table |
|
|
dbName = table |
|
|
} else { |
|
|
} else { |
|
@ -168,7 +188,12 @@ func Delete(dbName, table string, data map[string]string, del_count ...string) ( |
|
|
limitStr = " limit " + del_count[0] |
|
|
limitStr = " limit " + del_count[0] |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
result, err := DB.Exec("delete from "+dbName+" where "+strings.Join(keyList, " and ")+limitStr, valueList...) |
|
|
|
|
|
|
|
|
var Sql string |
|
|
|
|
|
Sql = "delete from " + dbName + " where " + strings.Join(keyList, " and ") + limitStr |
|
|
|
|
|
if DB_PROVIDER == "PgsqlDb" { |
|
|
|
|
|
Sql = sqlx.Rebind(sqlx.DOLLAR, Sql) |
|
|
|
|
|
} |
|
|
|
|
|
result, err := DB.Exec(Sql, valueList...) |
|
|
|
|
|
|
|
|
if err != nil { |
|
|
if err != nil { |
|
|
log.Println("ERROR|删除", dbName, "数据失败,", err) |
|
|
log.Println("ERROR|删除", dbName, "数据失败,", err) |
|
@ -192,7 +217,6 @@ func GetData(dbName, table string, title string, where map[string]string, limit |
|
|
if dbName == "" && table == "" { |
|
|
if dbName == "" && table == "" { |
|
|
return count, info, errors.New("没有数据表") |
|
|
return count, info, errors.New("没有数据表") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
dbName = getTableName(dbName, table) |
|
|
dbName = getTableName(dbName, table) |
|
|
|
|
|
|
|
|
if len(title) < 1 { |
|
|
if len(title) < 1 { |
|
@ -211,7 +235,11 @@ func GetData(dbName, table string, title string, where map[string]string, limit |
|
|
if _, ok := limit["from"]; ok { |
|
|
if _, ok := limit["from"]; ok { |
|
|
from = limit["from"] |
|
|
from = limit["from"] |
|
|
} |
|
|
} |
|
|
limitStr += " limit " + from + ",1" |
|
|
|
|
|
|
|
|
if DB_PROVIDER == "PgsqlDb" { |
|
|
|
|
|
limitStr += " limit 1 OFFSET " + from |
|
|
|
|
|
} else { |
|
|
|
|
|
limitStr += " limit " + from + ",1" |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} else { |
|
|
} else { |
|
|
limitStr = " limit 1" |
|
|
limitStr = " limit 1" |
|
@ -242,8 +270,12 @@ func GetData(dbName, table string, title string, where map[string]string, limit |
|
|
var err error |
|
|
var err error |
|
|
var queryNum int = 0 |
|
|
var queryNum int = 0 |
|
|
for queryNum < 3 { //如发生错误,继续查询3次,防止数据库连接断开问题
|
|
|
for queryNum < 3 { //如发生错误,继续查询3次,防止数据库连接断开问题
|
|
|
|
|
|
|
|
|
rows, err = DB.Query("SELECT "+title+" FROM "+dbName+" where "+strings.Join(keyList, " and ")+" "+limitStr, valueList...) |
|
|
|
|
|
|
|
|
var Sql string |
|
|
|
|
|
Sql = "SELECT " + title + " FROM " + dbName + " where " + strings.Join(keyList, " and ") + " " + limitStr |
|
|
|
|
|
if DB_PROVIDER == "PgsqlDb" { |
|
|
|
|
|
Sql = sqlx.Rebind(sqlx.DOLLAR, Sql) |
|
|
|
|
|
} |
|
|
|
|
|
rows, err = DB.Query(Sql, valueList...) |
|
|
|
|
|
|
|
|
if err == nil { |
|
|
if err == nil { |
|
|
break |
|
|
break |
|
@ -303,7 +335,6 @@ func GetRow(dbName, table_name, alias string, titles string, with, join [][]stri |
|
|
if dbName == "" && table_name == "" { |
|
|
if dbName == "" && table_name == "" { |
|
|
return count, info, errors.New("没有数据表") |
|
|
return count, info, errors.New("没有数据表") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
table := "" |
|
|
table := "" |
|
|
withSql := "" |
|
|
withSql := "" |
|
|
if len(with) > 0 { |
|
|
if len(with) > 0 { |
|
@ -432,9 +463,7 @@ func GetRow(dbName, table_name, alias string, titles string, with, join [][]stri |
|
|
|
|
|
|
|
|
for queryNum < 2 { //如发生错误,继续查询2次,防止数据库连接断开问题
|
|
|
for queryNum < 2 { //如发生错误,继续查询2次,防止数据库连接断开问题
|
|
|
if DB_PROVIDER == "PgsqlDb" { |
|
|
if DB_PROVIDER == "PgsqlDb" { |
|
|
log.Println("PgsqlDb sql_str", sql_str) |
|
|
|
|
|
sql_str = sqlx.Rebind(sqlx.DOLLAR, sql_str) |
|
|
sql_str = sqlx.Rebind(sqlx.DOLLAR, sql_str) |
|
|
log.Println("PgsqlDb sql_str", sql_str) |
|
|
|
|
|
} |
|
|
} |
|
|
rows, err = db.Query(sql_str, valueList...) |
|
|
rows, err = db.Query(sql_str, valueList...) |
|
|
|
|
|
|
|
@ -642,9 +671,7 @@ func FetchRows(dbName, table_name, alias string, titles string, with, join [][]s |
|
|
var queryNum int = 0 |
|
|
var queryNum int = 0 |
|
|
for queryNum < 2 { //如发生错误,继续查询2次,防止数据库连接断开问题
|
|
|
for queryNum < 2 { //如发生错误,继续查询2次,防止数据库连接断开问题
|
|
|
if DB_PROVIDER == "PgsqlDb" { |
|
|
if DB_PROVIDER == "PgsqlDb" { |
|
|
log.Println("PgsqlDb sql_str", sql_str) |
|
|
|
|
|
sql_str = sqlx.Rebind(sqlx.DOLLAR, sql_str) |
|
|
sql_str = sqlx.Rebind(sqlx.DOLLAR, sql_str) |
|
|
log.Println("PgsqlDb sql_str", sql_str) |
|
|
|
|
|
} |
|
|
} |
|
|
rows, err = db.Query(sql_str, valueList...) |
|
|
rows, err = db.Query(sql_str, valueList...) |
|
|
|
|
|
|
|
@ -723,7 +750,6 @@ func GetList(dbName, table string, title string, where map[string]string, limit |
|
|
if dbName == "" && table == "" { |
|
|
if dbName == "" && table == "" { |
|
|
return list, errors.New("没有数据表") |
|
|
return list, errors.New("没有数据表") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if strings.Contains(table, "select ") { |
|
|
if strings.Contains(table, "select ") { |
|
|
dbName = table |
|
|
dbName = table |
|
|
} else { |
|
|
} else { |
|
@ -751,7 +777,12 @@ func GetList(dbName, table string, title string, where map[string]string, limit |
|
|
from = limit["from"] |
|
|
from = limit["from"] |
|
|
} |
|
|
} |
|
|
if offset != "0" && from != "" { |
|
|
if offset != "0" && from != "" { |
|
|
limitStr += " limit " + from + "," + offset |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if DB_PROVIDER == "PgsqlDb" { |
|
|
|
|
|
limitStr += " limit " + offset + " OFFSET " + from |
|
|
|
|
|
} else { |
|
|
|
|
|
limitStr += " limit " + from + "," + offset |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -785,8 +816,12 @@ func GetList(dbName, table string, title string, where map[string]string, limit |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
for queryNum < 5 { //如发生错误,继续查询5次,防止数据库连接断开问题
|
|
|
for queryNum < 5 { //如发生错误,继续查询5次,防止数据库连接断开问题
|
|
|
|
|
|
|
|
|
rows, err = DB.Query("select "+title+" from "+dbName+" where "+strings.Join(whereStr, " and ")+" "+limitStr, valueList...) |
|
|
|
|
|
|
|
|
var Sql string |
|
|
|
|
|
Sql = "select " + title + " from " + dbName + " where " + strings.Join(whereStr, " and ") + " " + limitStr |
|
|
|
|
|
if DB_PROVIDER == "PgsqlDb" { |
|
|
|
|
|
Sql = sqlx.Rebind(sqlx.DOLLAR, Sql) |
|
|
|
|
|
} |
|
|
|
|
|
rows, err = DB.Query(Sql, valueList...) |
|
|
|
|
|
|
|
|
if err == nil { |
|
|
if err == nil { |
|
|
break |
|
|
break |
|
@ -850,7 +885,6 @@ func GetTotal(dbName, table string, args ...string) (total int) { |
|
|
if dbName == "" && table == "" { |
|
|
if dbName == "" && table == "" { |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if strings.Contains(table, "select ") { |
|
|
if strings.Contains(table, "select ") { |
|
|
dbName = table |
|
|
dbName = table |
|
|
} else { |
|
|
} else { |
|
@ -868,7 +902,6 @@ func GetTotal(dbName, table string, args ...string) (total int) { |
|
|
var queryNum int = 0 |
|
|
var queryNum int = 0 |
|
|
|
|
|
|
|
|
for queryNum < 5 { //如发生错误,继续查询5次,防止数据库连接断开问题
|
|
|
for queryNum < 5 { //如发生错误,继续查询5次,防止数据库连接断开问题
|
|
|
|
|
|
|
|
|
rows, err = DB.Query("select count(" + title + ") as count from " + dbName + " limit 1") |
|
|
rows, err = DB.Query("select count(" + title + ") as count from " + dbName + " limit 1") |
|
|
|
|
|
|
|
|
if err == nil { |
|
|
if err == nil { |
|
@ -952,7 +985,12 @@ func GetCount(dbName, table string, where map[string]string, args ...string) (to |
|
|
|
|
|
|
|
|
for queryNum < 5 { //如发生错误,继续查询5次,防止数据库连接断开问题
|
|
|
for queryNum < 5 { //如发生错误,继续查询5次,防止数据库连接断开问题
|
|
|
|
|
|
|
|
|
rows, err = DB.Query("select count("+title+") as count from "+dbName+" where "+strings.Join(whereStr, " and ")+" limit 1", valueList...) |
|
|
|
|
|
|
|
|
var Sql string |
|
|
|
|
|
Sql = "select count(" + title + ") as count from " + dbName + " where " + strings.Join(whereStr, " and ") + " limit 1" |
|
|
|
|
|
if DB_PROVIDER == "PgsqlDb" { |
|
|
|
|
|
Sql = sqlx.Rebind(sqlx.DOLLAR, Sql) |
|
|
|
|
|
} |
|
|
|
|
|
rows, err = DB.Query(Sql, valueList...) |
|
|
|
|
|
|
|
|
if err == nil { |
|
|
if err == nil { |
|
|
break |
|
|
break |
|
@ -1011,6 +1049,9 @@ func DoQuery(args ...interface{}) ([]map[string]string, error) { |
|
|
|
|
|
|
|
|
for queryNum < 3 { //如发生错误,继续查询5次,防止数据库连接断开问题
|
|
|
for queryNum < 3 { //如发生错误,继续查询5次,防止数据库连接断开问题
|
|
|
if len(args) > 1 { |
|
|
if len(args) > 1 { |
|
|
|
|
|
if DB_PROVIDER == "PgsqlDb" { |
|
|
|
|
|
queryStr = sqlx.Rebind(sqlx.DOLLAR, queryStr) |
|
|
|
|
|
} |
|
|
rows, err = DB.Query(queryStr, args[1:]...) //strings.Join(args[1:], ",")
|
|
|
rows, err = DB.Query(queryStr, args[1:]...) //strings.Join(args[1:], ",")
|
|
|
if err != nil { |
|
|
if err != nil { |
|
|
log.Println("ERROR|DoQuery error:", err) |
|
|
log.Println("ERROR|DoQuery error:", err) |
|
|