Browse Source

修改子查询带条件查询

master v0.9.3
zhenghaorong 1 year ago
parent
commit
bb5a253e43
2 changed files with 77 additions and 20 deletions
  1. +42
    -12
      db.go
  2. +35
    -8
      transaction.go

+ 42
- 12
db.go View File

@ -23,7 +23,11 @@ func Insert(dbName, table string, data map[string]string) (int64, error) {
if dbName == "" && table == "" {
return insertId, errors.New("没有数据表")
}
dbName = getTableName(dbName, table)
if strings.Contains(table, "select ") {
dbName = table
} else {
dbName = getTableName(dbName, table)
}
if len(data) < 1 {
return insertId, errors.New("没有要写入的数据")
@ -64,7 +68,11 @@ func Update(dbName, table string, data map[string]string, where map[string]strin
return rowsAffected, errors.New("没有数据表")
}
dbName = getTableName(dbName, table)
if strings.Contains(table, "select ") {
dbName = table
} else {
dbName = getTableName(dbName, table)
}
if len(data) < 1 {
return rowsAffected, errors.New("同有更新的数据")
}
@ -124,8 +132,11 @@ func Delete(dbName, table string, data map[string]string, del_count ...string) (
return count, errors.New("没有数据表")
}
dbName = getTableName(dbName, table)
if strings.Contains(table, "select ") {
dbName = table
} else {
dbName = getTableName(dbName, table)
}
if len(data) < 1 {
return count, errors.New("没有要删除的数据")
}
@ -292,8 +303,12 @@ func GetRow(dbName, table_name, alias string, titles string, join [][]string, wh
return count, info, errors.New("没有数据表")
}
table := getTableName(dbName, table_name)
table := ""
if strings.Contains(table_name, "select ") {
table = table_name
} else {
table = getTableName(dbName, table_name)
}
var sql_str, title string
if titles != "" {
@ -431,8 +446,12 @@ func FetchRows(dbName, table_name, alias string, titles string, join [][]string,
if dbName == "" && table_name == "" {
return count, list, errors.New("没有数据表")
}
table := getTableName(dbName, table_name)
table := ""
if strings.Contains(table_name, "select ") {
table = table_name
} else {
table = getTableName(dbName, table_name)
}
var sql_str, title string
@ -598,8 +617,11 @@ func GetList(dbName, table string, title string, where map[string]string, limit
return list, errors.New("没有数据表")
}
dbName = getTableName(dbName, table)
if strings.Contains(table, "select ") {
dbName = table
} else {
dbName = getTableName(dbName, table)
}
var rows *sql.Rows
var err error
var queryNum int = 0
@ -722,7 +744,11 @@ func GetTotal(dbName, table string, args ...string) (total int) {
return
}
dbName = getTableName(dbName, table)
if strings.Contains(table, "select ") {
dbName = table
} else {
dbName = getTableName(dbName, table)
}
var title string = "*"
@ -776,7 +802,11 @@ func GetCount(dbName, table string, where map[string]string, args ...string) (to
if dbName == "" && table == "" {
return
}
dbName = getTableName(dbName, table)
if strings.Contains(table, "select ") {
dbName = table
} else {
dbName = getTableName(dbName, table)
}
var title string = "*"


+ 35
- 8
transaction.go View File

@ -21,7 +21,12 @@ func TxInsert(tx *sql.Tx, dbname, table string, data map[string]string) (int64,
if dbname == "" && table == "" {
return 0, errors.New("参数错误,没有数据表")
}
dbName := getTableName(dbname, table)
dbName := ""
if strings.Contains(table, "select ") {
dbName = table
} else {
dbName = getTableName(dbName, table)
}
if len(data) < 1 {
return 0, errors.New("参数错误,没有要写入的数据")
}
@ -62,7 +67,12 @@ func TxPreInsert(tx *sql.Tx, dbname, table string, data map[string]interface{})
return 0, errors.New("params error,no db or table")
}
dbName := getTableName(dbname, table)
dbName := ""
if strings.Contains(table, "select ") {
dbName = table
} else {
dbName = getTableName(dbName, table)
}
if len(data) < 1 {
return 0, errors.New("params error,no data to insert")
@ -108,7 +118,12 @@ func TxUpdate(tx *sql.Tx, dbname, table string, data map[string]string, where ma
if dbname == "" && table == "" {
return rowsAffected, errors.New("参数错误,没有数据表")
}
dbName := getTableName(dbname, table)
dbName := ""
if strings.Contains(table, "select ") {
dbName = table
} else {
dbName = getTableName(dbName, table)
}
if len(data) < 1 {
return rowsAffected, errors.New("参数错误,没有要写入的数据")
}
@ -167,7 +182,12 @@ func TxPreUpdate(tx *sql.Tx, dbname, table string, data []string, where []string
return 0, errors.New("params error,no db or table")
}
dbName := getTableName(dbname, table)
dbName := ""
if strings.Contains(table, "select ") {
dbName = table
} else {
dbName = getTableName(dbName, table)
}
if len(where) < 1 {
return 0, errors.New("params error, no data for update")
@ -204,8 +224,12 @@ func TxDelete(tx *sql.Tx, dbname, table string, where map[string]string, del_cou
if dbname == "" && table == "" {
return count, errors.New("参数错误,没有数据表")
}
dbName := getTableName(dbname, table)
dbName := ""
if strings.Contains(table, "select ") {
dbName = table
} else {
dbName = getTableName(dbName, table)
}
if len(where) < 1 {
return count, errors.New("参数错误,没有删除条件")
}
@ -257,8 +281,11 @@ func TxForRead(tx *sql.Tx, dbName, table string, title string, where []string) (
if dbName == "" && table == "" {
return nil, errors.New("参数错误,没有数据表")
}
dbName = getTableName(dbName, table)
if strings.Contains(table, "select ") {
dbName = table
} else {
dbName = getTableName(dbName, table)
}
if len(title) < 1 {
return nil, errors.New("没有要查询内容")


Loading…
Cancel
Save