Skip to content

Commit

Permalink
Merge pull request #26 from twharmon/update-query-bug-fix
Browse files Browse the repository at this point in the history
Fix update query bug
  • Loading branch information
twharmon authored Sep 9, 2020
2 parents 7a48817 + 89a320d commit 2e8ac4d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Query builder with some handy utility functions.

## Documentation
For full documentation see the [godoc](https://godoc.org/github.com/twharmon/gosql) or [GitBook](https://twharmon.gitbook.io/gosql/).
For full documentation see the [pkg.go.dev](https://pkg.go.dev/github.com/twharmon/gosql?tab=doc) or [GitBook](https://twharmon.gitbook.io/gosql/).

## Examples
```go
Expand Down
32 changes: 32 additions & 0 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,38 @@ func TestUpdate(t *testing.T) {
check(t, mock.ExpectationsWereMet())
}

func TestUpdateThreeFields(t *testing.T) {
db, mock, err := getMockDB()
check(t, err)
type T struct {
ID int `gosql:"primary"`
Name string
Email string
}
check(t, db.Register(T{}))
updateModel := T{5, "foo", "foo@example.com"}
mock.ExpectExec(`^update t set name = \?, email = \? where id = \?$`).WithArgs(updateModel.Name, updateModel.Email, updateModel.ID).WillReturnResult(sqlmock.NewResult(0, 1))
_, err = db.Update(&updateModel)
check(t, err)
check(t, mock.ExpectationsWereMet())
}

func TestUpdateThreeFieldsTwoPrimaries(t *testing.T) {
db, mock, err := getMockDB()
check(t, err)
type T struct {
ID int `gosql:"primary"`
Name string
Email string `gosql:"primary"`
}
check(t, db.Register(T{}))
updateModel := T{5, "foo", "foo@example.com"}
mock.ExpectExec(`^update t set name = \? where id = \? and email = \?$`).WithArgs(updateModel.Name, updateModel.ID, updateModel.Email).WillReturnResult(sqlmock.NewResult(0, 1))
_, err = db.Update(&updateModel)
check(t, err)
check(t, mock.ExpectationsWereMet())
}

func TestBegin(t *testing.T) {
db, mock, err := getMockDB()
check(t, err)
Expand Down
2 changes: 1 addition & 1 deletion model.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (m *model) getUpdateQuery() string {
}
query.WriteString(m.fields[i])
query.WriteString(" = ?")
if i < len(m.fields)-1 {
if i < len(m.fields)-1 && !isIntIn(i+1, m.primaryFieldIndecies) {
query.WriteString(", ")
}
}
Expand Down

0 comments on commit 2e8ac4d

Please sign in to comment.