diff --git a/error.go b/error.go index 03520655..d582110f 100644 --- a/error.go +++ b/error.go @@ -10,4 +10,5 @@ var ( ErrUnSupportedType error = errors.New("unsupported type error") ErrNotExist error = errors.New("not exist error") ErrCacheFailed error = errors.New("cache failed") + ErrNeedDeletedCond error = errors.New("delete need at least one condition") ) diff --git a/session.go b/session.go index 411a7680..380b4c34 100644 --- a/session.go +++ b/session.go @@ -1718,20 +1718,19 @@ func (session *Session) Delete(bean interface{}) (int64, error) { var condition = "" if session.Statement.WhereStr != "" { - condition = fmt.Sprintf("WHERE %v", session.Statement.WhereStr) + condition = session.Statement.WhereStr if len(colNames) > 0 { - condition += " and " - condition += strings.Join(colNames, " and ") + condition += " and " + strings.Join(colNames, " and ") } } 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", - session.Engine.QuoteStr(), - session.Statement.TableName(), - session.Engine.QuoteStr(), - condition) + sql := fmt.Sprintf("DELETE FROM %v WHERE %v", + session.Engine.Quote(session.Statement.TableName()), condition) args = append(session.Statement.Params, args...)