diff --git a/HISTORY.md b/HISTORY.md index 64947751..fb37329b 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,7 @@ +# v9.5.1 + +* [FIXED] Unable to execute union with order by expression [#185](https://github.com/doug-martin/goqu/issues/185) + # v9.5.0 * [ADDED] Ability to use regexp like, ilike, notlike, and notilike without a regexp [#172](https://github.com/doug-martin/goqu/issues/172) diff --git a/Makefile b/Makefile index bbc4f602..0249d4d9 100644 --- a/Makefile +++ b/Makefile @@ -3,4 +3,4 @@ phony: lint: - golangci-lint run + docker run --rm -v ${CURDIR}:/app -w /app golangci/golangci-lint:v1.21.0 golangci-lint run -v diff --git a/issues_test.go b/issues_test.go index 25e05abe..067ad77a 100644 --- a/issues_test.go +++ b/issues_test.go @@ -1,11 +1,14 @@ package goqu_test import ( + "context" "strings" "sync" "testing" "time" + "github.com/DATA-DOG/go-sqlmock" + "github.com/doug-martin/goqu/v9" "github.com/stretchr/testify/suite" ) @@ -324,6 +327,29 @@ func (gis *githubIssuesSuite) TestIssue164() { ) } +// Test for https://github.com/doug-martin/goqu/issues/185 +func (gis *githubIssuesSuite) TestIssue185() { + mDb, sqlMock, err := sqlmock.New() + gis.NoError(err) + sqlMock.ExpectQuery( + `SELECT \* FROM \(SELECT "id" FROM "table" ORDER BY "id" ASC\) AS "t1" UNION +\(SELECT \* FROM \(SELECT "id" FROM "table" ORDER BY "id" ASC\) AS "t1"\)`, + ). + WillReturnRows(sqlmock.NewRows([]string{"id"}).FromCSVString("1\n2\n3\n4\n")) + db := goqu.New("mock", mDb) + + ds := db.Select("id").From("table").Order(goqu.C("id").Asc()). + Union( + db.Select("id").From("table").Order(goqu.C("id").Asc()), + ) + + ctx := context.Background() + var i []int + gis.NoError(ds.ScanValsContext(ctx, &i)) + gis.Equal([]int{1, 2, 3, 4}, i) + +} + func TestGithubIssuesSuite(t *testing.T) { suite.Run(t, new(githubIssuesSuite)) } diff --git a/select_dataset.go b/select_dataset.go index ede6b452..4d9fa078 100644 --- a/select_dataset.go +++ b/select_dataset.go @@ -277,12 +277,7 @@ func (sd *SelectDataset) From(from ...interface{}) *SelectDataset { // Returns a new Dataset with the current one as an source. If the current Dataset is not aliased (See Dataset#As) then // it will automatically be aliased. See examples. func (sd *SelectDataset) FromSelf() *SelectDataset { - builder := SelectDataset{ - dialect: sd.dialect, - clauses: exp.NewSelectClauses(), - } - return builder.From(sd) - + return sd.copy(exp.NewSelectClauses()).From(sd) } // Alias to InnerJoin. See examples.