Add support for SELECT_JSON statements.
This commit is contained in:
parent
7047de44a9
commit
7b16e432ff
46 changed files with 2732 additions and 307 deletions
|
|
@ -9,7 +9,50 @@ import (
|
|||
"testing"
|
||||
)
|
||||
|
||||
func TestNorthwindJoinEverything(t *testing.T) {
|
||||
type Dest []struct {
|
||||
model.Customers
|
||||
|
||||
Demographics model.CustomerDemographics
|
||||
|
||||
Orders []struct {
|
||||
model.Orders
|
||||
|
||||
Shipper model.Shippers
|
||||
|
||||
Employee struct {
|
||||
model.Employees
|
||||
|
||||
Territories []struct {
|
||||
model.Territories
|
||||
|
||||
Region model.Region
|
||||
}
|
||||
}
|
||||
|
||||
Details []struct {
|
||||
model.OrderDetails
|
||||
|
||||
Products struct {
|
||||
model.Products
|
||||
|
||||
Category model.Categories
|
||||
Supplier model.Suppliers
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkTestNorthwindJoinEverything(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
testNorthwindJoinEverything(b)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTestNorthwindJoinEverything(t *testing.T) {
|
||||
testNorthwindJoinEverything(t)
|
||||
}
|
||||
|
||||
func testNorthwindJoinEverything(t require.TestingT) {
|
||||
|
||||
stmt :=
|
||||
SELECT(
|
||||
|
|
@ -21,6 +64,9 @@ func TestNorthwindJoinEverything(t *testing.T) {
|
|||
Products.AllColumns,
|
||||
Categories.AllColumns,
|
||||
Suppliers.AllColumns,
|
||||
Employees.AllColumns,
|
||||
Territories.AllColumns,
|
||||
Region.AllColumns,
|
||||
).FROM(
|
||||
Customers.
|
||||
LEFT_JOIN(CustomerCustomerDemo, Customers.CustomerID.EQ(CustomerCustomerDemo.CustomerID)).
|
||||
|
|
@ -35,35 +81,110 @@ func TestNorthwindJoinEverything(t *testing.T) {
|
|||
LEFT_JOIN(EmployeeTerritories, EmployeeTerritories.EmployeeID.EQ(Employees.EmployeeID)).
|
||||
LEFT_JOIN(Territories, EmployeeTerritories.TerritoryID.EQ(Territories.TerritoryID)).
|
||||
LEFT_JOIN(Region, Territories.RegionID.EQ(Region.RegionID)),
|
||||
).ORDER_BY(Customers.CustomerID, Orders.OrderID, Products.ProductID)
|
||||
).ORDER_BY(
|
||||
Customers.CustomerID,
|
||||
Orders.OrderID,
|
||||
Products.ProductID,
|
||||
Territories.TerritoryID,
|
||||
)
|
||||
|
||||
var dest []struct {
|
||||
model.Customers
|
||||
//fmt.Println(stmt.DebugSql())
|
||||
|
||||
Demographics model.CustomerDemographics
|
||||
|
||||
Orders []struct {
|
||||
model.Orders
|
||||
|
||||
Shipper model.Shippers
|
||||
|
||||
Details struct {
|
||||
model.OrderDetails
|
||||
|
||||
Products []struct {
|
||||
model.Products
|
||||
|
||||
Category model.Categories
|
||||
Supplier model.Suppliers
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
var dest Dest
|
||||
|
||||
err := stmt.Query(db, &dest)
|
||||
require.NoError(t, err)
|
||||
|
||||
//jsonSave("./testdata/northwind-all.json", dest)
|
||||
//testutils.SaveJSONFile(dest, "./testdata/results/postgres/northwind-all.json")
|
||||
testutils.AssertJSONFile(t, dest, "./testdata/results/postgres/northwind-all.json")
|
||||
requireLogged(t, stmt)
|
||||
}
|
||||
|
||||
func BenchmarkTestNorthwindJoinEverythingJson(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
testNorthwindJoinEverythingJson(b)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNorthwindJoinEverythingJson(t *testing.T) {
|
||||
testNorthwindJoinEverythingJson(t)
|
||||
}
|
||||
|
||||
func testNorthwindJoinEverythingJson(t require.TestingT) {
|
||||
|
||||
stmt := SELECT_JSON_ARR(
|
||||
Customers.AllColumns,
|
||||
|
||||
SELECT_JSON_OBJ(CustomerDemographics.AllColumns).
|
||||
FROM(CustomerDemographics.INNER_JOIN(CustomerCustomerDemo, CustomerCustomerDemo.CustomerTypeID.EQ(CustomerDemographics.CustomerTypeID))).
|
||||
WHERE(CustomerCustomerDemo.CustomerID.EQ(Customers.CustomerID)).AS("Demographics"),
|
||||
|
||||
SELECT_JSON_ARR(
|
||||
Orders.AllColumns,
|
||||
|
||||
SELECT_JSON_OBJ(Shippers.AllColumns).
|
||||
FROM(Shippers).
|
||||
WHERE(Shippers.ShipperID.EQ(Orders.ShipVia)).AS("Shipper"),
|
||||
|
||||
SELECT_JSON_OBJ(
|
||||
Employees.AllColumns,
|
||||
SELECT_JSON_ARR(
|
||||
Territories.AllColumns,
|
||||
|
||||
SELECT_JSON_OBJ(Region.AllColumns).
|
||||
FROM(Region).
|
||||
WHERE(Region.RegionID.EQ(Territories.RegionID)).AS("Region"),
|
||||
).FROM(
|
||||
EmployeeTerritories.LEFT_JOIN(
|
||||
Territories,
|
||||
EmployeeTerritories.TerritoryID.EQ(Territories.TerritoryID)),
|
||||
).WHERE(
|
||||
EmployeeTerritories.EmployeeID.EQ(Employees.EmployeeID), // TODO: move to join
|
||||
).AS("Territories"),
|
||||
).FROM(Employees).
|
||||
WHERE(Orders.EmployeeID.EQ(Employees.EmployeeID)).AS("Employee"),
|
||||
|
||||
SELECT_JSON_ARR(
|
||||
OrderDetails.AllColumns,
|
||||
|
||||
SELECT_JSON_OBJ(
|
||||
Products.AllColumns,
|
||||
|
||||
SELECT_JSON_OBJ(
|
||||
Categories.AllColumns,
|
||||
).FROM(Categories).
|
||||
WHERE(Categories.CategoryID.EQ(Products.CategoryID)).AS("Category"),
|
||||
|
||||
SELECT_JSON_OBJ(Suppliers.AllColumns).
|
||||
FROM(Suppliers).
|
||||
WHERE(Suppliers.SupplierID.EQ(Products.SupplierID)).AS("Supplier"),
|
||||
).FROM(Products).
|
||||
WHERE(Products.ProductID.EQ(OrderDetails.ProductID)).AS("Products"),
|
||||
).FROM(
|
||||
OrderDetails,
|
||||
).WHERE(
|
||||
OrderDetails.OrderID.EQ(Orders.OrderID),
|
||||
).AS("Details"),
|
||||
).FROM(
|
||||
Orders,
|
||||
).WHERE(
|
||||
Orders.CustomerID.EQ(Customers.CustomerID),
|
||||
).ORDER_BY(
|
||||
Orders.OrderID,
|
||||
).AS("Orders"),
|
||||
).FROM(
|
||||
Customers,
|
||||
).ORDER_BY(
|
||||
Customers.CustomerID,
|
||||
)
|
||||
|
||||
//fmt.Println(stmt.DebugSql())
|
||||
|
||||
var dest Dest
|
||||
|
||||
err := stmt.QueryJSON(ctx, db, &dest)
|
||||
require.NoError(t, err)
|
||||
|
||||
//testutils.SaveJSONFile(dest, "./testdata/results/postgres/northwind-all2.json")
|
||||
testutils.AssertJSONFile(t, dest, "./testdata/results/postgres/northwind-all.json")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue