Skip to content

Commit

Permalink
Merge pull request #187 from doug-martin/issue183
Browse files Browse the repository at this point in the history
Fix for #183
  • Loading branch information
doug-martin authored Dec 7, 2019
2 parents 20ea999 + 3b783f9 commit 1b15b2a
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 1 deletion.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# v9.5.1

* [FIXED] Unable to execute union with order by expression [#185](https://github.com/doug-martin/goqu/issues/185)
* [FIXED] SelectDataset From with Error [#183](https://github.com/doug-martin/goqu/issues/183)

# v9.5.0

Expand Down
4 changes: 4 additions & 0 deletions delete_dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ func (dd *DeleteDataset) ToSQL() (sql string, params []interface{}, err error) {
// Appends this Dataset's DELETE statement to the SQLBuilder
// This is used internally when using deletes in CTEs
func (dd *DeleteDataset) AppendSQL(b sb.SQLBuilder) {
if dd.err != nil {
b.SetError(dd.err)
return
}
dd.dialect.ToDeleteSQL(b, dd.GetClauses())
}

Expand Down
4 changes: 4 additions & 0 deletions insert_dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ func (id *InsertDataset) ToSQL() (sql string, params []interface{}, err error) {
// Appends this Dataset's INSERT statement to the SQLBuilder
// This is used internally when using inserts in CTEs
func (id *InsertDataset) AppendSQL(b sb.SQLBuilder) {
if id.err != nil {
b.SetError(id.err)
return
}
id.dialect.ToInsertSQL(b, id.GetClauses())
}

Expand Down
42 changes: 41 additions & 1 deletion issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package goqu_test

import (
"context"
"fmt"
"strings"
"sync"
"testing"
"time"

"github.com/DATA-DOG/go-sqlmock"

"github.com/doug-martin/goqu/v9"
"github.com/doug-martin/goqu/v9/exp"
"github.com/stretchr/testify/suite"
)

Expand Down Expand Up @@ -327,6 +328,45 @@ func (gis *githubIssuesSuite) TestIssue164() {
)
}

// Test for https://github.com/doug-martin/goqu/issues/183
func (gis *githubIssuesSuite) TestIssue184() {
expectedErr := fmt.Errorf("an error")
testCases := []struct {
ds exp.AppendableExpression
}{
{ds: goqu.From("test").As("t").SetError(expectedErr)},
{ds: goqu.Insert("test").Rows(goqu.Record{"foo": "bar"}).Returning("foo").SetError(expectedErr)},
{ds: goqu.Update("test").Set(goqu.Record{"foo": "bar"}).Returning("foo").SetError(expectedErr)},
{ds: goqu.Update("test").Set(goqu.Record{"foo": "bar"}).Returning("foo").SetError(expectedErr)},
{ds: goqu.Delete("test").Returning("foo").SetError(expectedErr)},
}

for _, tc := range testCases {
ds := goqu.From(tc.ds)
sql, args, err := ds.ToSQL()
gis.Equal(expectedErr, err)
gis.Empty(sql)
gis.Empty(args)

sql, args, err = ds.Prepared(true).ToSQL()
gis.Equal(expectedErr, err)
gis.Empty(sql)
gis.Empty(args)

ds = goqu.From("test2").Where(goqu.Ex{"foo": tc.ds})

sql, args, err = ds.ToSQL()
gis.Equal(expectedErr, err)
gis.Empty(sql)
gis.Empty(args)

sql, args, err = ds.Prepared(true).ToSQL()
gis.Equal(expectedErr, err)
gis.Empty(sql)
gis.Empty(args)
}
}

// Test for https://github.com/doug-martin/goqu/issues/185
func (gis *githubIssuesSuite) TestIssue185() {
mDb, sqlMock, err := sqlmock.New()
Expand Down
4 changes: 4 additions & 0 deletions select_dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,10 @@ func (sd *SelectDataset) Executor() exec.QueryExecutor {
// Appends this Dataset's SELECT statement to the SQLBuilder
// This is used internally for sub-selects by the dialect
func (sd *SelectDataset) AppendSQL(b sb.SQLBuilder) {
if sd.err != nil {
b.SetError(sd.err)
return
}
sd.dialect.ToSelectSQL(b, sd.GetClauses())
}

Expand Down
4 changes: 4 additions & 0 deletions update_dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ func (ud *UpdateDataset) ToSQL() (sql string, params []interface{}, err error) {
// Appends this Dataset's UPDATE statement to the SQLBuilder
// This is used internally when using updates in CTEs
func (ud *UpdateDataset) AppendSQL(b sb.SQLBuilder) {
if ud.err != nil {
b.SetError(ud.err)
return
}
ud.dialect.ToUpdateSQL(b, ud.GetClauses())
}

Expand Down

0 comments on commit 1b15b2a

Please sign in to comment.