数据库错误操作优化

This commit is contained in:
hoteas 2022-08-25 05:01:50 +08:00
parent 1533a57cb8
commit 96d8868694
2 changed files with 49 additions and 44 deletions

61
code.go
View File

@ -96,12 +96,12 @@ var TptProject = Proj{
for _, v := range that.MakeCodeRouter[hotimeName].TableColumns[fileConfig.GetString("table")] { for _, v := range that.MakeCodeRouter[hotimeName].TableColumns[fileConfig.GetString("table")] {
if v.GetString("link") != "" { if v.GetString("link") != "" {
//与用户信息有关联的parent_id自填充
if v.GetString("link") == tableName && that.MakeCodeRouter[hotimeName].TableColumns[v.GetString("link")]["parent_id"] != nil { if v.GetString("link") == tableName && that.MakeCodeRouter[hotimeName].TableColumns[v.GetString("link")]["parent_id"] != nil {
if inData["parent_id"] == nil && data[v.GetString("name")] != nil { if inData["parent_id"] == nil && data[v.GetString("name")] != nil {
inData["parent_id"] = data.GetCeilInt64(v.GetString("name")) inData["parent_id"] = data.GetCeilInt64(v.GetString("name"))
pre := that.Db.Get(v.GetString("link"), "parent_ids", Map{"id": data.GetCeilInt64(v.GetString("name"))}) //pre := that.Db.Get(v.GetString("link"), "parent_ids", Map{"id": data.GetCeilInt64(v.GetString("name"))})
inData["parent_ids"] = pre.GetString("parent_ids") //inData["parent_ids"] = pre.GetString("parent_ids")+ObjToStr(data.GetCeilInt64(v.GetString("name")))+","
} }
} }
@ -209,14 +209,16 @@ var TptProject = Proj{
return return
} }
//索引管理,便于检索以及权限 //索引管理,便于检索以及权限
if inData.Get("parent_id") != nil && inData.GetString("parent_ids") != "" { if inData.Get("parent_id") != nil {
index := that.Db.Get(tableName, "`parent_ids`", Map{"id": inData.Get("parent_id")}) index := that.Db.Get(tableName, "`parent_ids`", Map{"id": inData.Get("parent_id")})
inData["parent_ids"] = index.GetString("parent_ids") + ObjToStr(re) + "," parent_ids := index.GetString("parent_ids")
that.Db.Update(tableName, Map{"parent_ids": inData["parent_ids"]}, Map{"id": re}) if parent_ids == "" {
} else if inData.GetString("parent_ids") != "" { parent_ids = ","
inData["parent_ids"] = "," + ObjToStr(re) + "," }
inData["parent_ids"] = parent_ids + ObjToStr(re) + ","
that.Db.Update(tableName, Map{"parent_ids": inData["parent_ids"]}, Map{"id": re}) that.Db.Update(tableName, Map{"parent_ids": inData["parent_ids"]}, Map{"id": re})
} }
that.Log["table_id"] = re that.Log["table_id"] = re
that.Display(0, re) that.Display(0, re)
}, },
@ -354,21 +356,23 @@ var TptProject = Proj{
} }
//索引管理,便于检索以及权限 if inData.Get("parent_id") != nil {
if inData.GetString("parent_ids") != "" { //1
if inData.Get("parent_id") != nil { //12 62
Index := that.Db.Get(tableName, "`index`", Map{"id": that.RouterString[2]}) //123 623
parentIndex := that.Db.Get(tableName, "`index`", Map{"id": inData.Get("parent_id")}) Index := that.Db.Get(tableName, "parent_id,`parent_ids`", Map{"id": that.RouterString[2]})
inData["parent_ids"] = parentIndex.GetString("parent_ids") + that.RouterString[2] + "," if inData.GetCeilInt64("parent_id") != Index.GetCeilInt64("parent_id") {
parentIndex := that.Db.Get(tableName, "`parent_ids`", Map{"id": inData.Get("parent_id")})
childNodes := that.Db.Select(tableName, "id,`index``", Map{"index[~]": "," + that.RouterString[2] + ","}) if strings.Contains(parentIndex.GetString("parent_ids"), ","+that.RouterString[2]+",") {
that.Display(4, "不能将子级设置为父级")
return
}
parent_ids := parentIndex.GetString("parent_ids") + that.RouterString[2] + ","
childNodes := that.Db.Select(tableName, "id,`parent_ids``", Map{"parent_ids[~]": "," + that.RouterString[2] + ","})
for _, v := range childNodes { for _, v := range childNodes {
v["parent_ids"] = strings.Replace(v.GetString("parent_ids"), Index.GetString("parent_ids"), inData.GetString("parent_ids"), -1) v["parent_ids"] = strings.Replace(v.GetString("parent_ids"), Index.GetString("parent_ids"), parent_ids, -1)
that.Db.Update(tableName, Map{"parent_ids": v["parent_ids"]}, Map{"id": v.GetCeilInt("id")}) that.Db.Update(tableName, Map{"parent_ids": v["parent_ids"]}, Map{"id": v.GetCeilInt("id")})
} }
} else {
delete(inData, "parent_ids")
} }
} }
@ -380,25 +384,22 @@ var TptProject = Proj{
return return
} }
//索引管理,便于检索以及权限
that.Display(0, re) that.Display(0, re)
}, },
"remove": func(that *Context) { "remove": func(that *Context) {
tableName := that.RouterString[1] tableName := that.RouterString[1]
that.Log = Map{"table": tableName, "type": 3, "table_id": that.RouterString[2]} that.Log = Map{"table": tableName, "type": 3, "table_id": that.RouterString[2]}
hotimeName := that.RouterString[0] hotimeName := that.RouterString[0]
inData := that.MakeCodeRouter[hotimeName].Delete(tableName, that.Req)
if inData == nil {
that.Display(3, "请求参数不足")
return
}
re := int64(0) re := int64(0)
//索引管理,便于检索以及权限 //有索引则删除对应的参数
if inData.Get("parent_id") != nil && inData.GetSlice("parent_ids") != nil { if that.MakeCodeRouter[hotimeName].TableColumns[tableName]["parent_id"] != nil {
re = that.Db.Delete(tableName, Map{"index[~]": "," + that.RouterString[2] + ","}) that.Db.Delete(tableName, Map{"parent_ids[~]": "," + that.RouterString[2] + ","})
} else {
re = that.Db.Delete(tableName, Map{"id": that.RouterString[2]})
} }
re = that.Db.Delete(tableName, Map{"id": that.RouterString[2]})
if re == 0 { if re == 0 {
that.Display(4, "删除数据失败") that.Display(4, "删除数据失败")
return return

View File

@ -725,9 +725,9 @@ func (that *MakeCode) Edit(table string, req *http.Request) Map {
for _, v := range that.TableColumns[table] { for _, v := range that.TableColumns[table] {
//不可使用,未在前端展示,但在内存中保持有 //不可使用,未在前端展示,但在内存中保持有
if v.GetBool("notUse") { if v.GetBool("notUse") {
if v.GetString("type") == "index" && that.TableColumns[table]["parent_id"] != nil { //if v.GetString("type") == "index" && that.TableColumns[table]["parent_id"] != nil {
data[v.GetString("name")] = "," // data[v.GetString("name")] = ","
} //}
continue continue
} }
@ -764,16 +764,16 @@ func (that *MakeCode) Edit(table string, req *http.Request) Map {
} }
func (that *MakeCode) Delete(table string, req *http.Request) Map { func (that *MakeCode) Delete(table string, req *http.Request) Map {
data := Map{} data := Map{}
for _, v := range that.TableColumns[table] { //for _, v := range that.TableColumns[table] {
//不可使用,未在前端展示,但在内存中保持有 // //不可使用,未在前端展示,但在内存中保持有
if v.GetBool("notUse") { // if v.GetBool("notUse") {
if v.GetString("type") == "index" && that.TableColumns[table]["parent_id"] != nil { // //if v.GetString("type") == "index" && that.TableColumns[table]["parent_id"] != nil {
data[v.GetString("name")] = "," // // data[v.GetString("name")] = ","
} // //}
continue // continue
} // }
//
} //}
return data return data
} }
@ -1064,7 +1064,11 @@ func (that *MakeCode) Search(table string, userData Map, req *http.Request, db *
data["OR"] = Map{table + ".id": parentID, table + ".parent_id": nil} data["OR"] = Map{table + ".id": parentID, table + ".parent_id": nil}
} else { } else {
data["OR"] = Map{table + ".parent_id": reqValue, table + ".id": reqValue} if req.FormValue("showself") != "" {
data["OR"] = Map{table + ".parent_id": reqValue, table + ".id": reqValue}
} else {
data[table+".parent_id"] = reqValue
}
} }
continue continue