gorm框架,db.Table("users").Where("cno = ?", "123").Updates(map[string]interface{}{"cno": "1234"})
时间: 2023-11-02 16:06:02 浏览: 125
在使用gorm框架的情况下,db.Table("users").Where("cno = ?", "123").Updates(map[string]interface{}{"cno": "1234"})是用于更新符合条件的记录的代码。其中,cno是要更新的字段,"123"是要匹配的条件,"1234"是要更新的新值。
相关问题
common.DB.Table("ps_rivers").Select("COALESCE(sum(segment_length), 0)").Where("watershed_id = ?", item.ID).Scan(&totalL)
这段代码使用了 GORM 进行数据库操作。`common.DB` 是一个 GORM 的数据库连接对象,`.Table("ps_rivers")` 指定要操作的表为 `ps_rivers`,`.Select("COALESCE(sum(segment_length), 0)")` 指定查询的字段为 `COALESCE(sum(segment_length), 0)`,即对 `segment_length` 字段进行求和,并将结果设为 0,以避免为空时返回 nil。`.Where("watershed_id = ?", item.ID)` 指定了查询条件为 `watershed_id = item.ID`,其中 `item.ID` 为传入的参数。`.Scan(&totalL)` 将查询结果赋值给 `totalL` 变量,`&totalL` 表示将查询结果的地址传入 `Scan` 方法中,以便将查询结果赋值给 `totalL`。
改进代码func TracingList(ctx *gin.Context) { resp := models.Response{ Code: 0, Msg: "success", } t, _ := strconv.Atoi(ctx.Query("t")) label, _ := strconv.Atoi(ctx.Query("label")) page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1")) pageSize, _ := strconv.Atoi(ctx.DefaultQuery("pageSize", "10")) code := ctx.Query("code") //设施类型 if len(code) == 0 { resp.Code = 400 resp.Msg = "请输入code值" ctx.JSON(400, resp) return } type Total struct { gorm.Model Type int16 Source int16 Explanation string Label int16 FacilityID string } db := common.DB.Session(&gorm.Session{}) if t < 0 || t > 5 { var req interface{} switch t { case 0: req = []*Total{} case 1: req = []*models.Flaw{} case 2: req = []*models.Issue{} case 3: req = []*models.Record{} case 4: req = []*models.Quality{} case 5: req = []*models.Inspection{} } //查询并分页 if t < 1 || t > 5 { res := db.Scopes(models.Paginate(page, pageSize)).Where("facility_id=?", code).Find(&req) if res.RowsAffected == 0 { resp.Code = 400 resp.Msg = "记录不存在" ctx.JSON(400, resp) return } } else { res := db.Scopes(models.Paginate(page, pageSize)).Raw("SELECT * FROM flaws WHERE facility_id = ? "+ "UNION SELECT * FROM issues WHERE facility_id = ? "+ "UNION SELECT * FROM records WHERE facility_id = ? "+ "UNION SELECT * FROM qualities WHERE facility_id = ? "+ "UNION SELECT * FROM inspections WHERE facility_id = ?", "code", "code", "code", "code", "code").Find(&req) if res.RowsAffected == 0 { resp.Code = 400 resp.Msg = "记录不存在" ctx.JSON(400, resp) return } } resp.Data = req ctx.JSON(200, resp) } else if label < 1 || label > 4 { var req []*Total res := db.Scopes(models.Paginate(page, pageSize)).Raw("SELECT * FROM flaws WHERE label = ? "+ "UNION SELECT * FROM issues WHERE label = ? "+ "UNION SELECT * FROM records WHERE label = ? "+ "UNION SELECT * FROM qualities WHERE label = ? "+ "UNION SELECT * FROM inspections WHERE label = ?", "label", "label", "label", "label", "label").Find(&req) if res.RowsAffected == 0 { resp.Code = 400 resp.Msg = "记录不存在" ctx.JSON(400, resp) return } resp.Data = req ctx.JSON(200, resp) } else { ctx.JSON(400, models.Response{ Code: 400, Msg: "参数有误", }) return } }
以下是改进的代码:
```go
func TracingList(ctx *gin.Context) {
resp := models.Response{
Code: 0,
Msg: "success",
}
code := ctx.Query("code")
if len(code) == 0 {
resp.Code = 400
resp.Msg = "请输入code值"
ctx.JSON(400, resp)
return
}
t, err := strconv.Atoi(ctx.Query("t"))
if err != nil || t < 0 || t > 5 {
resp.Code = 400
resp.Msg = "无效的参数t"
ctx.JSON(400, resp)
return
}
label, err := strconv.Atoi(ctx.Query("label"))
if err != nil || label < 1 || label > 4 {
resp.Code = 400
resp.Msg = "无效的参数label"
ctx.JSON(400, resp)
return
}
page, err := strconv.Atoi(ctx.DefaultQuery("page", "1"))
if err != nil || page < 1 {
resp.Code = 400
resp.Msg = "无效的参数page"
ctx.JSON(400, resp)
return
}
pageSize, err := strconv.Atoi(ctx.DefaultQuery("pageSize", "10"))
if err != nil || pageSize < 1 {
resp.Code = 400
resp.Msg = "无效的参数pageSize"
ctx.JSON(400, resp)
return
}
var req interface{}
switch t {
case 0:
req = []*Total{}
case 1:
req = []*models.Flaw{}
case 2:
req = []*models.Issue{}
case 3:
req = []*models.Record{}
case 4:
req = []*models.Quality{}
case 5:
req = []*models.Inspection{}
}
db := common.DB.Session(&gorm.Session{})
res := db.Scopes(models.Paginate(page, pageSize)).Where("facility_id=?", code)
if t >= 1 && t <= 5 {
res = res.Table("flaws").Where("facility_id=?", code).
Or("facility_id=?", code).
Or("facility_id=?", code).
Or("facility_id=?", code).
Or("facility_id=?", code)
}
res.Find(req)
if res.RowsAffected == 0 {
resp.Code = 400
resp.Msg = "记录不存在"
ctx.JSON(400, resp)
return
}
resp.Data = req
ctx.JSON(200, resp)
}
```
主要改进包括:
1. 对输入的参数进行了有效性检查,如 t、label、page 和 pageSize 都需要为正整数,code 不能为空。
2. 使用了 Go 语言内置的错误处理机制,当参数无效时,返回错误信息。
3. 将 SQL 查询语句拆分为两段,根据 t 的值判断使用哪一段,避免了使用 Raw() 方法的安全隐患。
4. 代码结构更加清晰,易于阅读和维护。
阅读全文