diff --git a/HISTORY.md b/HISTORY.md index fd9721a4..49b6f1cc 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -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 diff --git a/delete_dataset_test.go b/delete_dataset_test.go index bedf7d42..aa2e4ec3 100644 --- a/delete_dataset_test.go +++ b/delete_dataset_test.go @@ -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(). diff --git a/exp/col.go b/exp/col.go index cd6f686c..55def0e1 100644 --- a/exp/col.go +++ b/exp/col.go @@ -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: diff --git a/insert_dataset_test.go b/insert_dataset_test.go index e9ccdd42..7840863a 100644 --- a/insert_dataset_test.go +++ b/insert_dataset_test.go @@ -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(). diff --git a/issues_test.go b/issues_test.go index ce11fcac..03f48ff0 100644 --- a/issues_test.go +++ b/issues_test.go @@ -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)) } diff --git a/update_dataset_test.go b/update_dataset_test.go index 5dd93424..f779fa10 100644 --- a/update_dataset_test.go +++ b/update_dataset_test.go @@ -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().