From 78ed3fdb13ef506ad8e8be558b19c6b03c8288de Mon Sep 17 00:00:00 2001 From: Marco Ronchese <8733504+emarj@users.noreply.github.com> Date: Fri, 16 Dec 2022 23:17:26 +0100 Subject: [PATCH] adding test and cleaning up --- qrm/scan_context.go | 22 +++----- tests/postgres/select_test.go | 96 ++++++++++++++++++++++++++++++++++- 2 files changed, 103 insertions(+), 15 deletions(-) diff --git a/qrm/scan_context.go b/qrm/scan_context.go index 02997eb..9b1d3ee 100644 --- a/qrm/scan_context.go +++ b/qrm/scan_context.go @@ -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) + } } } diff --git a/tests/postgres/select_test.go b/tests/postgres/select_test.go index 5f34daa..5a42114 100644 --- a/tests/postgres/select_test.go +++ b/tests/postgres/select_test.go @@ -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