From d2fbdb68e6acb32d7aecd8cc17d9952015f1a19f Mon Sep 17 00:00:00 2001 From: go-jet Date: Sat, 14 Dec 2019 19:11:35 +0100 Subject: [PATCH] Add support for conditional constructed projection list. --- mysql/types.go | 3 +++ postgres/types.go | 3 +++ tests/mysql/select_test.go | 33 +++++++++++++++++++++++++++++++++ tests/postgres/select_test.go | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+) diff --git a/mysql/types.go b/mysql/types.go index 2902e72..4ef84b4 100644 --- a/mysql/types.go +++ b/mysql/types.go @@ -7,3 +7,6 @@ type Statement = jet.Statement // Projection is interface for all projection types. Types that can be part of, for instance SELECT clause. type Projection = jet.Projection + +// ProjectionList can be used to create conditional constructed projection list. +type ProjectionList = jet.ProjectionList diff --git a/postgres/types.go b/postgres/types.go index 215cceb..58a8ae9 100644 --- a/postgres/types.go +++ b/postgres/types.go @@ -7,3 +7,6 @@ type Statement = jet.Statement // Projection is interface for all projection types. Types that can be part of, for instance SELECT clause. type Projection = jet.Projection + +// ProjectionList can be used to create conditional constructed projection list. +type ProjectionList = jet.ProjectionList diff --git a/tests/mysql/select_test.go b/tests/mysql/select_test.go index 952eb63..ed75bad 100644 --- a/tests/mysql/select_test.go +++ b/tests/mysql/select_test.go @@ -708,3 +708,36 @@ func TestJoinViewWithTable(t *testing.T) { assert.Equal(t, len(dest[0].Rentals), 32) assert.Equal(t, len(dest[1].Rentals), 27) } + +func TestConditionalProjectionList(t *testing.T) { + projectionList := ProjectionList{} + + columnsToSelect := []string{"customer_id", "create_date"} + + for _, columnName := range columnsToSelect { + switch columnName { + case Customer.CustomerID.Name(): + projectionList = append(projectionList, Customer.CustomerID) + case Customer.Email.Name(): + projectionList = append(projectionList, Customer.Email) + case Customer.CreateDate.Name(): + projectionList = append(projectionList, Customer.CreateDate) + } + } + + stmt := SELECT(projectionList). + FROM(Customer). + LIMIT(3) + + testutils.AssertDebugStatementSql(t, stmt, ` +SELECT customer.customer_id AS "customer.customer_id", + customer.create_date AS "customer.create_date" +FROM dvds.customer +LIMIT 3; +`) + var dest []model.Customer + err := stmt.Query(db, &dest) + assert.NilError(t, err) + + assert.Equal(t, len(dest), 3) +} diff --git a/tests/postgres/select_test.go b/tests/postgres/select_test.go index 2ac6c36..35ba373 100644 --- a/tests/postgres/select_test.go +++ b/tests/postgres/select_test.go @@ -1791,3 +1791,36 @@ func TestJoinViewWithTable(t *testing.T) { assert.Equal(t, len(dest[0].Rentals), 32) assert.Equal(t, len(dest[1].Rentals), 27) } + +func TestConditionalProjectionList(t *testing.T) { + projectionList := ProjectionList{} + + columnsToSelect := []string{"customer_id", "create_date"} + + for _, columnName := range columnsToSelect { + switch columnName { + case Customer.CustomerID.Name(): + projectionList = append(projectionList, Customer.CustomerID) + case Customer.Email.Name(): + projectionList = append(projectionList, Customer.Email) + case Customer.CreateDate.Name(): + projectionList = append(projectionList, Customer.CreateDate) + } + } + + stmt := SELECT(projectionList). + FROM(Customer). + LIMIT(3) + + testutils.AssertDebugStatementSql(t, stmt, ` +SELECT customer.customer_id AS "customer.customer_id", + customer.create_date AS "customer.create_date" +FROM dvds.customer +LIMIT 3; +`) + var dest []model.Customer + err := stmt.Query(db, &dest) + assert.NilError(t, err) + + assert.Equal(t, len(dest), 3) +}