ORM Model - Update/Delete - 图1warning

For security assurance and to prevent mistakes, the Update and Delete methods must have Where conditions to be executed; otherwise, an error will return with a message like: there should be WHERE condition statement for XXX operation. goframe is an enterprise-grade framework with rigorous module design and detailed handling of engineering practices.

Update Method

Update is used for data updates and often needs to be used in conjunction with Data and Where methods. The Data method specifies the data to be updated, while the Where method specifies the condition range for the update. The Update method also supports directly passing data and condition parameters.

Example usage:

  1. // UPDATE `user` SET `name`='john guo' WHERE name='john'
  2. g.Model("user").Data(g.Map{"name" : "john guo"}).Where("name", "john").Update()
  3. g.Model("user").Data("name='john guo'").Where("name", "john").Update()
  4. // UPDATE `user` SET `status`=1 WHERE `status`=0 ORDER BY `login_time` asc LIMIT 10
  5. g.Model("user").Data("status", 1).Order("login_time asc").Where("status", 0).Limit(10).Update()
  6. // UPDATE `user` SET `status`=1 WHERE 1
  7. g.Model("user").Data("status=1").Where(1).Update()
  8. g.Model("user").Data("status", 1).Where(1).Update()
  9. g.Model("user").Data(g.Map{"status" : 1}).Where(1).Update()

You can also directly pass data and where parameters to the Update method:

  1. // UPDATE `user` SET `name`='john guo' WHERE name='john'
  2. g.Model("user").Update(g.Map{"name" : "john guo"}, "name", "john")
  3. g.Model("user").Update("name='john guo'", "name", "john")
  4. // UPDATE `user` SET `status`=1 WHERE 1
  5. g.Model("user").Update("status=1", 1)
  6. g.Model("user").Update(g.Map{"status" : 1}, 1)

Counter Update Feature

You can use the Counter type parameter to numerically operate on specific fields, such as increasing and decreasing.

Counter data structure definition:

  1. // Counter is the type for update count.
  2. type Counter struct {
  3. Field string
  4. Value float64
  5. }

Example using Counter for field increment:

  1. updateData := g.Map{
  2. "views": &gdb.Counter{
  3. Field: "views",
  4. Value: 1,
  5. },
  6. }
  7. // UPDATE `article` SET `views`=`views`+1 WHERE `id`=1
  8. result, err := db.Update("article", updateData, "id", 1)

Counter can also be used for incrementing by a non-own field, for example:

  1. updateData := g.Map{
  2. "views": &gdb.Counter{
  3. Field: "clicks",
  4. Value: 1,
  5. },
  6. }
  7. // UPDATE `article` SET `views`=`clicks`+1 WHERE `id`=1
  8. result, err := db.Update("article", updateData, "id", 1)

Increment/Decrement

We can use the Increment and Decrement methods to perform commonly used operations for incrementing/decrementing specified fields. The definitions for the two methods are as follows:

  1. // Increment increments a column's value by a given amount.
  2. func (m *Model) Increment(column string, amount float64) (sql.Result, error)
  3. // Decrement decrements a column's value by a given amount.
  4. func (m *Model) Decrement(column string, amount float64) (sql.Result, error)

Example usage:

  1. // UPDATE `article` SET `views`=`views`+10000 WHERE `id`=1
  2. g.Model("article").Where("id", 1).Increment("views", 10000)
  3. // UPDATE `article` SET `views`=`views`-10000 WHERE `id`=1
  4. g.Model("article").Where("id", 1).Decrement("views", 10000)

RawSQL Statement Embedding

gdb.Raw is a string type, and the parameter of this type will be directly embedded as an SQL fragment into the SQL statement submitted to the underlying layer. It will not be automatically converted to a string parameter type, nor treated as a pre-processing parameter. For more detailed introduction, please refer to the chapter: ORM Senior - RawSQL. For example:

  1. // UPDATE `user` SET login_count='login_count+1',update_time='now()' WHERE id=1
  2. g.Model("user").Data(g.Map{
  3. "login_count": "login_count+1",
  4. "update_time": "now()",
  5. }).Where("id", 1).Update()
  6. // Execution error: Error Code: 1136. Column count doesn't match value count at row 1

Revised with gdb.Raw:

  1. // UPDATE `user` SET login_count=login_count+1,update_time=now() WHERE id=1
  2. g.Model("user").Data(g.Map{
  3. "login_count": gdb.Raw("login_count+1"),
  4. "update_time": gdb.Raw("now()"),
  5. }).Where("id", 1).Update()

Delete Method

The Delete method is used for data deletion.

Example usage:

  1. // DELETE FROM `user` WHERE uid=10
  2. g.Model("user").Where("uid", 10).Delete()
  3. // DELETE FROM `user` ORDER BY `login_time` asc LIMIT 10
  4. g.Model("user").Order("login_time asc").Limit(10).Delete()

You can also directly pass where parameters to the Delete method:

  1. // DELETE FROM `user` WHERE `uid`=10
  2. g.Model("user").Delete("uid", 10)
  3. // DELETE FROM `user` WHERE `score`<60
  4. g.Model("user").Delete("score < ", 60)

Soft Delete Feature

For soft delete feature details, please refer to the chapter: ORM Model - Time Fields