Skip to content

Commit

Permalink
Merge pull request #142 from doug-martin/issue140
Browse files Browse the repository at this point in the history
Fix for #140
  • Loading branch information
doug-martin authored Aug 22, 2019
2 parents dd6b1d6 + e718b63 commit 8d914fd
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 0 deletions.
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 8.4.1

* [FIXED] Returning func be able to handle nil [#140](https://github.com/doug-martin/goqu/issues/140)

## 8.4.0

* Created new `sqlgen` module to encapsulate sql generation
Expand Down
12 changes: 12 additions & 0 deletions delete_dataset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,18 @@ func (dds *deleteDatasetSuite) TestReturning() {
SetFrom(C("items")).
SetReturning(exp.NewColumnListExpression("a")),
},
deleteTestCase{
ds: bd.Returning(),
clauses: exp.NewDeleteClauses().
SetFrom(C("items")).
SetReturning(exp.NewColumnListExpression()),
},
deleteTestCase{
ds: bd.Returning(nil),
clauses: exp.NewDeleteClauses().
SetFrom(C("items")).
SetReturning(exp.NewColumnListExpression()),
},
deleteTestCase{
ds: bd.Returning("a").Returning("b"),
clauses: exp.NewDeleteClauses().
Expand Down
1 change: 1 addition & 0 deletions exp/col.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func NewColumnListExpression(vals ...interface{}) ColumnListExpression {
var cols []Expression
for _, val := range vals {
switch t := val.(type) {
case nil: // do nothing
case string:
cols = append(cols, ParseIdentifier(t))
case ColumnListExpression:
Expand Down
18 changes: 18 additions & 0 deletions insert_dataset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,24 @@ func (ids *insertDatasetSuite) TestReturning() {
SetInto(C("items")).
SetReturning(exp.NewColumnListExpression("a")),
},
insertTestCase{
ds: bd.Returning(),
clauses: exp.NewInsertClauses().
SetInto(C("items")).
SetReturning(exp.NewColumnListExpression()),
},
insertTestCase{
ds: bd.Returning(nil),
clauses: exp.NewInsertClauses().
SetInto(C("items")).
SetReturning(exp.NewColumnListExpression()),
},
insertTestCase{
ds: bd.Returning(),
clauses: exp.NewInsertClauses().
SetInto(C("items")).
SetReturning(exp.NewColumnListExpression()),
},
insertTestCase{
ds: bd.Returning("a").Returning("b"),
clauses: exp.NewInsertClauses().
Expand Down
47 changes: 47 additions & 0 deletions issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,53 @@ func (gis *githubIssuesSuite) TestIssue118_withNilEmbeddedStructWithExportedFiel

}

// Test for https://github.com/doug-martin/goqu/issues/118
func (gis *githubIssuesSuite) TestIssue140() {

sql, arg, err := goqu.Insert(`test`).Returning().ToSQL()
gis.NoError(err)
gis.Empty(arg)
gis.Equal(`INSERT INTO "test" DEFAULT VALUES`, sql)

sql, arg, err = goqu.Update(`test`).Set(goqu.Record{"a": "b"}).Returning().ToSQL()
gis.NoError(err)
gis.Empty(arg)
gis.Equal(
`UPDATE "test" SET "a"='b'`,
sql,
)

sql, arg, err = goqu.Delete(`test`).Returning().ToSQL()
gis.NoError(err)
gis.Empty(arg)
gis.Equal(
`DELETE FROM "test"`,
sql,
)

sql, arg, err = goqu.Insert(`test`).Returning(nil).ToSQL()
gis.NoError(err)
gis.Empty(arg)
gis.Equal(`INSERT INTO "test" DEFAULT VALUES`, sql)

sql, arg, err = goqu.Update(`test`).Set(goqu.Record{"a": "b"}).Returning(nil).ToSQL()
gis.NoError(err)
gis.Empty(arg)
gis.Equal(
`UPDATE "test" SET "a"='b'`,
sql,
)

sql, arg, err = goqu.Delete(`test`).Returning(nil).ToSQL()
gis.NoError(err)
gis.Empty(arg)
gis.Equal(
`DELETE FROM "test"`,
sql,
)

}

func TestGithubIssuesSuite(t *testing.T) {
suite.Run(t, new(githubIssuesSuite))
}
12 changes: 12 additions & 0 deletions update_dataset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,18 @@ func (uds *updateDatasetSuite) TestReturning() {
SetTable(C("items")).
SetReturning(exp.NewColumnListExpression("a", "b")),
},
updateTestCase{
ds: bd.Returning(),
clauses: exp.NewUpdateClauses().
SetTable(C("items")).
SetReturning(exp.NewColumnListExpression()),
},
updateTestCase{
ds: bd.Returning(nil),
clauses: exp.NewUpdateClauses().
SetTable(C("items")).
SetReturning(exp.NewColumnListExpression()),
},
updateTestCase{
ds: bd.Returning("a", "b").Returning("c"),
clauses: exp.NewUpdateClauses().
Expand Down

0 comments on commit 8d914fd

Please sign in to comment.