version字段的update生成语句有歧义 #1784

Open
opened 2020-09-07 15:41:25 +00:00 by RelicOfTesla · 2 comments
t := tbl_task_record.Row{}
t.Id = 2
utils.DbMustExist(db.Get(&t))
n, err := db.Table(t).Update(tbl_task_record.Row{
	AccountInfo: "asd",
	Version:     t.Version, 
}, tbl_task_record.Row{
	Id: t.Id,
})

//  update ... version=version+1 where id=? and version=?

// 需要把Version放在参数1上,不仔细看,好像没毛病,但具体看update参数名,不应该将条件,扔到bean参数1去才对。
    
   func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int64, error)
n, err := db.Table(t).Update(tbl_task_record.Row{
	AccountInfo: "asd",
}, tbl_task_record.Row{
	Id: t.Id,
	Version:     t.Version, // not support cond sql => 
})
// 这才是与update参数说明对应的正确写法,但却是无法用的!!

//  update ... version=version+1 where id=? and version=?(0) // version永远=0

//  对于要使用update的参数2,condiBean时,不支持version 字段,会被忽略,并使用设置为0

建议按照参数顺序判断,优先判断参数2的version,没有再使用参数1的version?

```golang t := tbl_task_record.Row{} t.Id = 2 utils.DbMustExist(db.Get(&t)) n, err := db.Table(t).Update(tbl_task_record.Row{ AccountInfo: "asd", Version: t.Version, }, tbl_task_record.Row{ Id: t.Id, }) // update ... version=version+1 where id=? and version=? // 需要把Version放在参数1上,不仔细看,好像没毛病,但具体看update参数名,不应该将条件,扔到bean参数1去才对。 ``` ```golang func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int64, error) ``` ```golang n, err := db.Table(t).Update(tbl_task_record.Row{ AccountInfo: "asd", }, tbl_task_record.Row{ Id: t.Id, Version: t.Version, // not support cond sql => }) // 这才是与update参数说明对应的正确写法,但却是无法用的!! // update ... version=version+1 where id=? and version=?(0) // version永远=0 // 对于要使用update的参数2,condiBean时,不支持version 字段,会被忽略,并使用设置为0 ``` 建议按照参数顺序判断,优先判断参数2的version,没有再使用参数1的version?
lunny added the
kind
enhancement
label 2020-09-08 01:26:41 +00:00
Author

另外,还有一个问题,应该配一个开关,设置NoAutoVersionCondition( 即修改CheckVersion变量), 自带的NoAutoCondition 无效的。

例如我是要批量更新多条记录时,version自动加到条件(where version=x),导致会出现不命中。
如 update record set status = 1 , version = version+1 where status=2 这条语句

 db.Table(t).Update(tbl_task_record.Row{
	Status : 1,
   // Version : ??????  // 这里填啥?????,不填会自动被加入where version=0了。填了也不对
}, tbl_task_record.Row{
	Status : 2,
	// Version:     t.Version, //
})


生成出来的却是 update record set status = 1 , version = version+1 where status=2 and version=0

建议应当是

 db.Table(t).NoAutoVersionCondition(true).Update(tbl_task_record.Row{
	Status : 1,
}, tbl_task_record.Row{
	Status : 2,
})

// update record set status = 1 , version = version+1 where status=2 // good

另外,还有一个问题,应该配一个开关,设置NoAutoVersionCondition( 即修改CheckVersion变量), 自带的NoAutoCondition 无效的。 例如我是要批量更新**多条**记录时,version自动加到条件(where version=x),导致会出现不命中。 如 update record set status = 1 , version = version+1 where status=2 这条语句 ```go db.Table(t).Update(tbl_task_record.Row{ Status : 1, // Version : ?????? // 这里填啥?????,不填会自动被加入where version=0了。填了也不对 }, tbl_task_record.Row{ Status : 2, // Version: t.Version, // }) ``` 生成出来的却是 update record set status = 1 , version = version+1 where status=2 and ~~**version=0**~~ 建议应当是 ```go db.Table(t).NoAutoVersionCondition(true).Update(tbl_task_record.Row{ Status : 1, }, tbl_task_record.Row{ Status : 2, }) ``` // update record set status = 1 , version = version+1 where status=2 // good
Author
future:
  .NoAutoVersionCondition(true/false...)  // where version=condBean.version
  .NoAutoVersionUpdate(true/false...)   // set version=version+1

  .Where/WhereStruct/WhereBean
        if .NoAutoVersionCondition == false {
        	<- auto fetch version //  add sql "where and version = whereBean.version"
        }
  .Update()        
        
        
  sample:

   x.Where(condBean).Update(newBean)
   	=> update xx set a1 = {{newBean.a1}}, update_at = xxx, version = version+1
       where a1 = {{condBean.a1}} and ... version = {{condBean.version}}  
       /* dont't add newBean.version to cond when has WhereBean */
       
  x.NoAutoVersionCondition().Where(condBean).Update(newBean)
  	=> update xx set a1 = {{newBean.a1}}, update_at = xxx, version = version+1
       where a1 = {{condBean.a1}} and ... 
 
  x.NoAutoVersionUpdate().NoAutoVersionCondition().Where(condBean).Update(newBean)
  	=> update xx set a1 = newBean.a1}}, update_at = xxx
       where a1 = {{condBean.a1}} and ... 
       
       
  x.Update(newBean)
   /*  don'ts not WhereBean? what is it? */
```sql future: .NoAutoVersionCondition(true/false...) // where version=condBean.version .NoAutoVersionUpdate(true/false...) // set version=version+1 .Where/WhereStruct/WhereBean if .NoAutoVersionCondition == false { <- auto fetch version // add sql "where and version = whereBean.version" } .Update() sample: x.Where(condBean).Update(newBean) => update xx set a1 = {{newBean.a1}}, update_at = xxx, version = version+1 where a1 = {{condBean.a1}} and ... version = {{condBean.version}} /* dont't add newBean.version to cond when has WhereBean */ x.NoAutoVersionCondition().Where(condBean).Update(newBean) => update xx set a1 = {{newBean.a1}}, update_at = xxx, version = version+1 where a1 = {{condBean.a1}} and ... x.NoAutoVersionUpdate().NoAutoVersionCondition().Where(condBean).Update(newBean) => update xx set a1 = newBean.a1}}, update_at = xxx where a1 = {{condBean.a1}} and ... x.Update(newBean) /* don'ts not WhereBean? what is it? */ ```
lunny added this to the 1.3.0 milestone 2021-06-12 08:12:44 +00:00
lunny modified the milestone from 1.3.0 to 1.3.1 2022-01-25 03:20:31 +00:00
lunny modified the milestone from 1.3.1 to 1.3.2 2022-05-31 02:57:56 +00:00
lunny modified the milestone from 1.3.2 to 1.3.3 2022-09-03 07:12:16 +00:00
lunny modified the milestone from 1.3.3 to 2.0.0 2023-07-20 10:38:04 +00:00
Sign in to join this conversation.
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: xorm/xorm#1784
No description provided.