Return error if Delete has no any condition
This commit is contained in:
parent
5113f5d35b
commit
8294b99ea6
1
error.go
1
error.go
|
@ -10,4 +10,5 @@ var (
|
||||||
ErrUnSupportedType error = errors.New("unsupported type error")
|
ErrUnSupportedType error = errors.New("unsupported type error")
|
||||||
ErrNotExist error = errors.New("not exist error")
|
ErrNotExist error = errors.New("not exist error")
|
||||||
ErrCacheFailed error = errors.New("cache failed")
|
ErrCacheFailed error = errors.New("cache failed")
|
||||||
|
ErrNeedDeletedCond error = errors.New("delete need at least one condition")
|
||||||
)
|
)
|
||||||
|
|
17
session.go
17
session.go
|
@ -1718,20 +1718,19 @@ func (session *Session) Delete(bean interface{}) (int64, error) {
|
||||||
|
|
||||||
var condition = ""
|
var condition = ""
|
||||||
if session.Statement.WhereStr != "" {
|
if session.Statement.WhereStr != "" {
|
||||||
condition = fmt.Sprintf("WHERE %v", session.Statement.WhereStr)
|
condition = session.Statement.WhereStr
|
||||||
if len(colNames) > 0 {
|
if len(colNames) > 0 {
|
||||||
condition += " and "
|
condition += " and " + strings.Join(colNames, " and ")
|
||||||
condition += strings.Join(colNames, " and ")
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
condition = "WHERE " + strings.Join(colNames, " and ")
|
condition = strings.Join(colNames, " and ")
|
||||||
|
}
|
||||||
|
if len(condition) == 0 {
|
||||||
|
return 0, ErrNeedDeletedCond
|
||||||
}
|
}
|
||||||
|
|
||||||
sql := fmt.Sprintf("DELETE FROM %v%v%v %v",
|
sql := fmt.Sprintf("DELETE FROM %v WHERE %v",
|
||||||
session.Engine.QuoteStr(),
|
session.Engine.Quote(session.Statement.TableName()), condition)
|
||||||
session.Statement.TableName(),
|
|
||||||
session.Engine.QuoteStr(),
|
|
||||||
condition)
|
|
||||||
|
|
||||||
args = append(session.Statement.Params, args...)
|
args = append(session.Statement.Params, args...)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue