Skip to content

Commit

Permalink
add OrderBy() and Limit() in delete
Browse files Browse the repository at this point in the history
  • Loading branch information
lqs committed Apr 20, 2022
1 parent fe320f5 commit 9f29f0e
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 7 deletions.
63 changes: 56 additions & 7 deletions delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,39 @@ package sqlingo

import (
"database/sql"
"strconv"
"strings"
)

type deleteStatus struct {
scope scope
where BooleanExpression
orderBys []OrderBy
limit *int
}

type deleteWithTable interface {
Where(conditions ...BooleanExpression) deleteWithWhere
}

type deleteWithWhere interface {
GetSQL() (string, error)
Execute() (result sql.Result, err error)
toDeleteFinal
OrderBy(orderBys ...OrderBy) deleteWithOrder
Limit(limit int) deleteWithLimit
}

type deleteStatus struct {
scope scope
where BooleanExpression
type deleteWithOrder interface {
toDeleteFinal
Limit(limit int) deleteWithLimit
}

type deleteWithLimit interface {
toDeleteFinal
}

type toDeleteFinal interface {
GetSQL() (string, error)
Execute() (result sql.Result, err error)
}

func (d *database) DeleteFrom(table Table) deleteWithTable {
Expand All @@ -27,14 +46,44 @@ func (s deleteStatus) Where(conditions ...BooleanExpression) deleteWithWhere {
return s
}

func (s deleteStatus) OrderBy(orderBys ...OrderBy) deleteWithOrder {
s.orderBys = orderBys
return s
}

func (s deleteStatus) Limit(limit int) deleteWithLimit {
s.limit = &limit
return s
}

func (s deleteStatus) GetSQL() (string, error) {
var sb strings.Builder
sb.Grow(128)

sb.WriteString("DELETE FROM ")
sb.WriteString(s.scope.Tables[0].GetSQL(s.scope))
sb.WriteString(" WHERE ")
whereSql, err := s.where.GetSQL(s.scope)
if err != nil {
return "", err
}
sqlString := "DELETE FROM " + s.scope.Tables[0].GetSQL(s.scope) + " WHERE " + whereSql
sb.WriteString(whereSql)

if len(s.orderBys) > 0 {
orderBySql, err := commaOrderBys(s.scope, s.orderBys)
if err != nil {
return "", err
}
sb.WriteString(" ORDER BY ")
sb.WriteString(orderBySql)
}

if s.limit != nil {
sb.WriteString(" LIMIT ")
sb.WriteString(strconv.Itoa(*s.limit))
}

return sqlString, nil
return sb.String(), nil
}

func (s deleteStatus) Execute() (sql.Result, error) {
Expand Down
10 changes: 10 additions & 0 deletions delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,14 @@ func TestDelete(t *testing.T) {
if _, err := db.DeleteFrom(Table1).Where(errorExpression).Execute(); err == nil {
t.Error("should get error here")
}

if _, err := db.DeleteFrom(Table1).Where(Raw("#1#")).Limit(3).Execute(); err != nil {
t.Error(err)
}
assertLastSql(t, "DELETE FROM `table1` WHERE #1# LIMIT 3")

if _, err := db.DeleteFrom(Table1).Where(Raw("#1#")).OrderBy(Raw("#2#")).Limit(3).Execute(); err != nil {
t.Error(err)
}
assertLastSql(t, "DELETE FROM `table1` WHERE #1# ORDER BY #2# LIMIT 3")
}

0 comments on commit 9f29f0e

Please sign in to comment.