Skip to content

Commit

Permalink
make a dummy execution if update without assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
lqs committed Jan 26, 2022
1 parent 17a252c commit bb226cc
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
7 changes: 3 additions & 4 deletions update.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package sqlingo

import (
"database/sql"
"errors"
"strconv"
"strings"
)
Expand Down Expand Up @@ -79,15 +78,15 @@ func (s updateStatus) Limit(limit int) updateWithLimit {
}

func (s updateStatus) GetSQL() (string, error) {
if len(s.assignments) == 0 {
return "/* UPDATE without SET clause */ DO 0", nil
}
var sb strings.Builder
sb.Grow(128)

sb.WriteString("UPDATE ")
sb.WriteString(s.scope.Tables[0].GetSQL(s.scope))

if len(s.assignments) == 0 {
return "", errors.New("no set in update")
}
assignmentsSql, err := commaAssignments(s.scope, s.assignments)
if err != nil {
return "", err
Expand Down
12 changes: 5 additions & 7 deletions update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@ func TestUpdate(t *testing.T) {
Execute()
assertLastSql(t, "UPDATE `table1` SET `field1` = 10 WHERE 1")

if _, err := db.Update(Table1).
_, _ = db.Update(Table1).
SetIf(false, field1, 10).
Where(trueExpression()).
Execute(); err == nil {
t.Error("should get error here")
}
Execute()
assertLastSql(t, "/* UPDATE without SET clause */ DO 0")

if _, err := db.Update(Table1).Limit(3).Execute(); err == nil {
t.Error("should get error here")
}
_, _ = db.Update(Table1).Limit(3).Execute()
assertLastSql(t, "/* UPDATE without SET clause */ DO 0")

errExp := &expression{
builder: func(scope scope) (string, error) {
Expand Down
1 change: 1 addition & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func assertLastSql(t *testing.T, expectedSql string) {
if sharedMockConn.lastSql != expectedSql {
t.Errorf("last sql [%s] expected [%s]", sharedMockConn.lastSql, expectedSql)
}
sharedMockConn.lastSql = ""
}

func assertError(t *testing.T, value interface{}) {
Expand Down

0 comments on commit bb226cc

Please sign in to comment.