adding test and cleaning up

This commit is contained in:
Marco Ronchese 2022-12-16 23:17:26 +01:00
parent 192826c456
commit 78ed3fdb13
2 changed files with 103 additions and 15 deletions

View file

@ -187,20 +187,7 @@ func (s *ScanContext) getGroupKeyInfo(
field := structType.Field(i)
fieldType := indirectType(field.Type)
if !isPrimaryKey(field, primaryKeyOverwrites) {
if !isSimpleModelType(fieldType) {
if fieldType.Kind() != reflect.Struct {
continue
}
subType := s.getGroupKeyInfo(fieldType, &field, typeVisited)
if len(subType.indexes) != 0 || len(subType.subTypes) != 0 {
ret.subTypes = append(ret.subTypes, subType)
}
}
} else {
if isPrimaryKey(field, primaryKeyOverwrites) {
newTypeName, fieldName := getTypeAndFieldName(typeName, field)
index := s.typeToColumnIndex(newTypeName, fieldName)
@ -211,6 +198,13 @@ func (s *ScanContext) getGroupKeyInfo(
ret.indexes = append(ret.indexes, index)
} else if fieldType.Kind() == reflect.Struct {
subType := s.getGroupKeyInfo(fieldType, &field, typeVisited)
if len(subType.indexes) != 0 || len(subType.subTypes) != 0 {
ret.subTypes = append(ret.subTypes, subType)
}
}
}

View file

@ -2,10 +2,12 @@ package postgres
import (
"context"
"github.com/go-jet/jet/v2/qrm"
"database/sql"
"testing"
"time"
"github.com/go-jet/jet/v2/qrm"
"github.com/stretchr/testify/require"
"github.com/go-jet/jet/v2/internal/testutils"
@ -658,6 +660,98 @@ ORDER BY city.city_id, address.address_id, customer.customer_id;
`)
}
// Test join with sql.NullInt64 primary keys
func TestExecution5(t *testing.T) {
var dest []struct {
CityID sql.NullInt64 `sql:"primary_key" alias:"city.city_id"`
CityName string `alias:"city.city"`
Customers []struct {
CustomerID sql.NullInt64 `sql:"primary_key" alias:"customer_id"`
LastName *string `alias:"last_name"`
Address struct {
AddressID sql.NullInt64 `sql:"primary_key" alias:"AddressId"`
AddressLine string `alias:"address.address"`
} `alias:"address.*"`
} `alias:"customer"`
}
stmt := City.
INNER_JOIN(Address, Address.CityID.EQ(City.CityID)).
INNER_JOIN(Customer, Customer.AddressID.EQ(Address.AddressID)).
SELECT(
City.CityID,
City.City,
Customer.CustomerID,
Customer.LastName,
Address.AddressID,
Address.Address,
).
WHERE(City.City.EQ(String("London")).OR(City.City.EQ(String("York")))).
ORDER_BY(City.CityID, Address.AddressID, Customer.CustomerID)
testutils.AssertDebugStatementSql(t, stmt, `
SELECT city.city_id AS "city.city_id",
city.city AS "city.city",
customer.customer_id AS "customer.customer_id",
customer.last_name AS "customer.last_name",
address.address_id AS "address.address_id",
address.address AS "address.address"
FROM dvds.city
INNER JOIN dvds.address ON (address.city_id = city.city_id)
INNER JOIN dvds.customer ON (customer.address_id = address.address_id)
WHERE (city.city = 'London'::text) OR (city.city = 'York'::text)
ORDER BY city.city_id, address.address_id, customer.customer_id;
`, "London", "York")
err := stmt.Query(db, &dest)
require.NoError(t, err)
require.Equal(t, len(dest), 2)
testutils.AssertJSON(t, dest, `
[
{
"CityID": 312,
"CityName": "London",
"Customers": [
{
"CustomerID": 252,
"LastName": "Hoffman",
"Address": {
"AddressID": 256,
"AddressLine": "1497 Yuzhou Drive"
}
},
{
"CustomerID": 512,
"LastName": "Vines",
"Address": {
"AddressID": 517,
"AddressLine": "548 Uruapan Street"
}
}
]
},
{
"CityID": 589,
"CityName": "York",
"Customers": [
{
"CustomerID": 497,
"LastName": "Sledge",
"Address": {
"AddressID": 502,
"AddressLine": "1515 Korla Way"
}
}
]
}
]
`)
}
func TestJoinQuerySliceWithPtrs(t *testing.T) {
type FilmsPerLanguage struct {
Language model.Language