Return error if Delete has no any condition

This commit is contained in:
Lunny Xiao 2013-09-30 22:22:54 +08:00
parent 5113f5d35b
commit 8294b99ea6
2 changed files with 9 additions and 9 deletions

View File

@ -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")
) )

View File

@ -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...)