Avoid unnecessary double wrapping of SELECT statement when used as single function parameter.

This commit is contained in:
go-jet 2021-10-21 13:35:37 +02:00
parent 22b2901336
commit d197956271
16 changed files with 97 additions and 77 deletions

View file

@ -241,18 +241,18 @@ func TestExpressionOperators(t *testing.T) {
SELECT all_types.integer IS NULL AS "result.is_null",
all_types.date_ptr IS NOT NULL AS "result.is_not_null",
(all_types.small_int_ptr IN ($1, $2)) AS "result.in",
(all_types.small_int_ptr IN ((
(all_types.small_int_ptr IN (
SELECT all_types.integer AS "all_types.integer"
FROM test_sample.all_types
))) AS "result.in_select",
)) AS "result.in_select",
(CURRENT_USER) AS "result.raw",
($3 + COALESCE(all_types.small_int_ptr, 0) + $4) AS "result.raw_arg",
($5 + all_types.integer + $6 + $5 + $7 + $8) AS "result.raw_arg2",
(all_types.small_int_ptr NOT IN ($9, $10, NULL)) AS "result.not_in",
(all_types.small_int_ptr NOT IN ((
(all_types.small_int_ptr NOT IN (
SELECT all_types.integer AS "all_types.integer"
FROM test_sample.all_types
))) AS "result.not_in_select"
)) AS "result.not_in_select"
FROM test_sample.all_types
LIMIT $11;
`, int64(11), int64(22), 78, 56, 11, 22, 33, 44, int64(11), int64(22), int64(2))

View file

@ -5,6 +5,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"reflect"
"testing"
@ -368,16 +369,16 @@ func newActorInfoTableImpl(schemaName, tableName, alias string) actorInfoTable {
`
func TestGeneratedAllTypesSQLBuilderFiles(t *testing.T) {
enumDir := testRoot + ".gentestdata/jetdb/test_sample/enum/"
modelDir := testRoot + ".gentestdata/jetdb/test_sample/model/"
tableDir := testRoot + ".gentestdata/jetdb/test_sample/table/"
enumDir := filepath.Join(testRoot, "/.gentestdata/jetdb/test_sample/enum/")
modelDir := filepath.Join(testRoot, "/.gentestdata/jetdb/test_sample/model/")
tableDir := filepath.Join(testRoot, "/.gentestdata/jetdb/test_sample/table/")
enumFiles, err := ioutil.ReadDir(enumDir)
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, enumFiles, "mood.go", "level.go")
testutils.AssertFileContent(t, enumDir+"mood.go", moodEnumContent)
testutils.AssertFileContent(t, enumDir+"level.go", levelEnumContent)
testutils.AssertFileContent(t, enumDir+"/mood.go", moodEnumContent)
testutils.AssertFileContent(t, enumDir+"/level.go", levelEnumContent)
modelFiles, err := ioutil.ReadDir(modelDir)
require.NoError(t, err)
@ -385,7 +386,7 @@ func TestGeneratedAllTypesSQLBuilderFiles(t *testing.T) {
testutils.AssertFileNamesEqual(t, modelFiles, "all_types.go", "all_types_view.go", "employee.go", "link.go",
"mood.go", "person.go", "person_phone.go", "weird_names_table.go", "level.go", "user.go", "floats.go")
testutils.AssertFileContent(t, modelDir+"all_types.go", allTypesModelContent)
testutils.AssertFileContent(t, modelDir+"/all_types.go", allTypesModelContent)
tableFiles, err := ioutil.ReadDir(tableDir)
require.NoError(t, err)
@ -393,7 +394,7 @@ func TestGeneratedAllTypesSQLBuilderFiles(t *testing.T) {
testutils.AssertFileNamesEqual(t, tableFiles, "all_types.go", "employee.go", "link.go",
"person.go", "person_phone.go", "weird_names_table.go", "user.go", "floats.go")
testutils.AssertFileContent(t, tableDir+"all_types.go", allTypesTableContent)
testutils.AssertFileContent(t, tableDir+"/all_types.go", allTypesTableContent)
}
var moodEnumContent = `

View file

@ -140,8 +140,7 @@ ON CONFLICT ON CONSTRAINT employee_pkey DO NOTHING;
Link.ID.SET(Link.EXCLUDED.ID),
Link.URL.SET(String("http://www.postgresqltutorial2.com")),
),
).
RETURNING(Link.AllColumns)
).RETURNING(Link.AllColumns)
testutils.AssertStatementSql(t, stmt, `
INSERT INTO test_sample.link (id, url, name, description)

View file

@ -4,10 +4,9 @@ import (
"context"
"database/sql"
"fmt"
"github.com/go-jet/jet/v2/tests/internal/utils/repo"
"math/rand"
"os"
"os/exec"
"strings"
"testing"
"time"
@ -53,13 +52,7 @@ func TestMain(m *testing.M) {
}
func setTestRoot() {
cmd := exec.Command("git", "rev-parse", "--show-toplevel")
byteArr, err := cmd.Output()
if err != nil {
panic(err)
}
testRoot = strings.TrimSpace(string(byteArr)) + "/tests/"
testRoot = repo.GetTestsDirPath()
}
var loggedSQL string

View file

@ -75,11 +75,12 @@ LIMIT 30;
query := SELECT(
Payment.AllColumns,
Customer.AllColumns,
).
FROM(Payment.
INNER_JOIN(Customer, Payment.CustomerID.EQ(Customer.CustomerID))).
ORDER_BY(Payment.PaymentID.ASC()).
LIMIT(30)
).FROM(
Payment.
INNER_JOIN(Customer, Payment.CustomerID.EQ(Customer.CustomerID)),
).ORDER_BY(
Payment.PaymentID.ASC(),
).LIMIT(30)
testutils.AssertDebugStatementSql(t, query, expectedSQL, int64(30))

View file

@ -259,14 +259,14 @@ func TestUpdateWithModelData(t *testing.T) {
stmt := Link.
UPDATE(Link.AllColumns).
MODEL(link).
WHERE(Link.ID.EQ(Int(int64(link.ID))))
WHERE(Link.ID.EQ(Int32(link.ID)))
expectedSQL := `
UPDATE test_sample.link
SET (id, url, name, description) = (201, 'http://www.duckduckgo.com', 'DuckDuckGo', NULL)
WHERE link.id = 201;
`
testutils.AssertDebugStatementSql(t, stmt, expectedSQL, int32(201), "http://www.duckduckgo.com", "DuckDuckGo", nil, int64(201))
testutils.AssertDebugStatementSql(t, stmt, expectedSQL, int32(201), "http://www.duckduckgo.com", "DuckDuckGo", nil, int32(201))
AssertExec(t, stmt, 1)
}
@ -286,14 +286,14 @@ func TestUpdateWithModelDataAndPredefinedColumnList(t *testing.T) {
stmt := Link.
UPDATE(updateColumnList).
MODEL(link).
WHERE(Link.ID.EQ(Int(int64(link.ID))))
WHERE(Link.ID.EQ(Int32(link.ID)))
var expectedSQL = `
UPDATE test_sample.link
SET (description, name, url) = (NULL, 'DuckDuckGo', 'http://www.duckduckgo.com')
WHERE link.id = 201;
`
testutils.AssertDebugStatementSql(t, stmt, expectedSQL, nil, "DuckDuckGo", "http://www.duckduckgo.com", int64(201))
testutils.AssertDebugStatementSql(t, stmt, expectedSQL, nil, "DuckDuckGo", "http://www.duckduckgo.com", int32(201))
AssertExec(t, stmt, 1)
}

View file

@ -22,20 +22,23 @@ func TestWithRegionalSales(t *testing.T) {
SELECT(
Orders.ShipRegion,
SUM(OrderDetails.Quantity).AS(regionalSalesTotalSales.Name()),
).
FROM(Orders.INNER_JOIN(OrderDetails, OrderDetails.OrderID.EQ(Orders.OrderID))).
GROUP_BY(Orders.ShipRegion),
).FROM(
Orders.INNER_JOIN(OrderDetails, OrderDetails.OrderID.EQ(Orders.OrderID)),
).GROUP_BY(Orders.ShipRegion),
),
topRegion.AS(
SELECT(regionalSalesShipRegion).
FROM(regionalSales).
WHERE(regionalSalesTotalSales.GT(
SELECT(
regionalSalesShipRegion,
).FROM(
regionalSales,
).WHERE(
regionalSalesTotalSales.GT(
IntExp(
SELECT(SUM(regionalSalesTotalSales)).
FROM(regionalSales),
).DIV(Int(50)),
),
),
),
),
)(
SELECT(
@ -43,13 +46,17 @@ func TestWithRegionalSales(t *testing.T) {
OrderDetails.ProductID,
COUNT(STAR).AS("product_units"),
SUM(OrderDetails.Quantity).AS("product_sales"),
).
FROM(Orders.INNER_JOIN(OrderDetails, Orders.OrderID.EQ(OrderDetails.OrderID))).
WHERE(Orders.ShipRegion.IN(
topRegion.SELECT(topRegionShipRegion)),
).
GROUP_BY(Orders.ShipRegion, OrderDetails.ProductID).
ORDER_BY(SUM(OrderDetails.Quantity).DESC()),
).FROM(
Orders.
INNER_JOIN(OrderDetails, Orders.OrderID.EQ(OrderDetails.OrderID)),
).WHERE(
Orders.ShipRegion.IN(topRegion.SELECT(topRegionShipRegion)),
).GROUP_BY(
Orders.ShipRegion,
OrderDetails.ProductID,
).ORDER_BY(
SUM(OrderDetails.Quantity).DESC(),
),
)
//fmt.Println(stmt.DebugSql())
@ -75,10 +82,10 @@ SELECT orders.ship_region AS "orders.ship_region",
SUM(order_details.quantity) AS "product_sales"
FROM northwind.orders
INNER JOIN northwind.order_details ON (orders.order_id = order_details.order_id)
WHERE orders.ship_region IN ((
WHERE orders.ship_region IN (
SELECT top_region."orders.ship_region" AS "orders.ship_region"
FROM top_region
))
)
GROUP BY orders.ship_region, order_details.product_id
ORDER BY SUM(order_details.quantity) DESC;
`)
@ -141,19 +148,19 @@ func TestWithStatementDeleteAndInsert(t *testing.T) {
testutils.AssertStatementSql(t, stmt, `
WITH remove_discontinued_orders AS (
DELETE FROM northwind.order_details
WHERE order_details.product_id IN ((
WHERE order_details.product_id IN (
SELECT products.product_id AS "products.product_id"
FROM northwind.products
WHERE products.discontinued = $1
))
)
RETURNING order_details.product_id AS "order_details.product_id"
),update_discontinued_price AS (
UPDATE northwind.products
SET unit_price = $2
WHERE products.product_id IN ((
WHERE products.product_id IN (
SELECT remove_discontinued_orders."order_details.product_id" AS "order_details.product_id"
FROM remove_discontinued_orders
))
)
RETURNING products.product_id AS "products.product_id",
products.product_name AS "products.product_name",
products.supplier_id AS "products.supplier_id",