From ba5f2bf3812acc86b42b2f6b30f1a6837c13c973 Mon Sep 17 00:00:00 2001 From: Ivo Roefs Date: Wed, 28 Jun 2023 09:31:23 +0200 Subject: [PATCH 1/3] alsoFetch: (a ToManyRelation) now also resolves the collection proxy --- src/Glorp/GlorpAttributeModel.class.st | 32 ++++++++-------------- src/Glorp/GlorpCursoredStream.class.st | 38 +++++++++++++------------- 2 files changed, 31 insertions(+), 39 deletions(-) diff --git a/src/Glorp/GlorpAttributeModel.class.st b/src/Glorp/GlorpAttributeModel.class.st index 6586700a..c4a97fea 100644 --- a/src/Glorp/GlorpAttributeModel.class.st +++ b/src/Glorp/GlorpAttributeModel.class.st @@ -280,29 +280,21 @@ GlorpAttributeModel >> keyType: aClass [ ] { #category : #knitting } -GlorpAttributeModel >> knit: ourObject to: anotherObject [ +GlorpAttributeModel >> knit: ourObject to: anotherObject [ "Set up the relationship from our object to another one, indicated by our mapping." - self isCollectionAttribute - ifTrue: - [| collection | + self isCollectionAttribute + ifTrue: [ + | collection | collection := self getValueFrom: ourObject. - (collection isGlorpProxy and: [collection isInstantiated not]) - ifTrue: - [collection := self newCollection. - self setValueIn: ourObject to: collection. - self - add: anotherObject - to: collection - in: ourObject. - ^self]. - (collection includes: anotherObject) - ifFalse: - [self - add: anotherObject - to: collection - in: ourObject]] - ifFalse: [self setValueIn: ourObject to: anotherObject] + (collection isGlorpProxy and: [ collection isInstantiated not ]) + ifTrue: [ + collection := self newCollection. + self setValueIn: ourObject to: collection ]. + anotherObject ifNotNil: [ + (collection includes: anotherObject) ifFalse: [ + self add: anotherObject to: collection in: ourObject ] ] ] + ifFalse: [ self setValueIn: ourObject to: anotherObject ] ] { #category : #mapping } diff --git a/src/Glorp/GlorpCursoredStream.class.st b/src/Glorp/GlorpCursoredStream.class.st index 27e16f62..68662ec2 100644 --- a/src/Glorp/GlorpCursoredStream.class.st +++ b/src/Glorp/GlorpCursoredStream.class.st @@ -28,7 +28,7 @@ Class { 'rawPeekBuffer', 'statement' ], - #category : 'Glorp-Queries' + #category : #'Glorp-Queries' } { #category : #'instance creation' } @@ -79,24 +79,24 @@ GlorpCursoredStream >> atEnd [ { #category : #'building objects' } GlorpCursoredStream >> buildObjectsForRow: aRow [ - self query isNil ifTrue: [^aRow]. - builders - do: - [:each | each findInstanceForRow: aRow proxyType: self query proxyType]. - builders do: [:each | each buildObjectFrom: aRow]. - builders - do: - [:each | - each - sendPostFetchValidateIn: self session - OnFailureDo: [each instance: nil]]. - builders - do: [:each | each instance == nil ifFalse: [each knitResultIn: self]]. - builders do: [:each | self session sendPostFetchEventTo: each instance]. - ^self buildersThatReturnResults size = 1 - ifTrue: [self buildersThatReturnResults first returnValueIn: self] - ifFalse: - [self buildersThatReturnResults collect: [:each | each returnValueIn: self]] + + self query isNil ifTrue: [ ^ aRow ]. + builders do: [ :each | + each findInstanceForRow: aRow proxyType: self query proxyType ]. + builders do: [ :each | each buildObjectFrom: aRow ]. + builders do: [ :each | + each + sendPostFetchValidateIn: self session + OnFailureDo: [ each instance: nil ] ]. + builders do: [ :each | each knitResultIn: self ]. + builders do: [ :each | + self session sendPostFetchEventTo: each instance ]. + ^ self buildersThatReturnResults size = 1 + ifTrue: [ + self buildersThatReturnResults first returnValueIn: self ] + ifFalse: [ + self buildersThatReturnResults collect: [ :each | + each returnValueIn: self ] ] ] { #category : #'building objects' } From 90f29adaaa870d2021ec9ab564103befb456d7e3 Mon Sep 17 00:00:00 2001 From: Ivo Roefs Date: Sun, 23 Jul 2023 12:56:27 +0200 Subject: [PATCH 2/3] Test case for PR https://github.com/pharo-rdbms/glorp/pull/115 --- ...adingPersonWithEmailAddressesTest.class.st | 17 +++--- ...ngPersonWithoutEmailAddressesTest.class.st | 56 +++++++++++++++++++ 2 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 src/Glorp-Integration-Tests/GlorpReadingPersonWithoutEmailAddressesTest.class.st diff --git a/src/Glorp-Integration-Tests/GlorpReadingPersonWithEmailAddressesTest.class.st b/src/Glorp-Integration-Tests/GlorpReadingPersonWithEmailAddressesTest.class.st index 4a58c40c..081b8a1c 100644 --- a/src/Glorp-Integration-Tests/GlorpReadingPersonWithEmailAddressesTest.class.st +++ b/src/Glorp-Integration-Tests/GlorpReadingPersonWithEmailAddressesTest.class.st @@ -126,18 +126,17 @@ GlorpReadingPersonWithEmailAddressesTest >> testReadMultipleObjectsOneToMany [ { #category : #tests } GlorpReadingPersonWithEmailAddressesTest >> testReadPersonWithEmailAddresses [ + | query result emailAddresses | query := Query - readOneOf:GlorpPerson - where: [:person | person id = 3]. + readOneOf: GlorpPerson + where: [ :person | person id = 3 ]. result := query executeIn: session. emailAddresses := result emailAddresses getValue. self assert: emailAddresses size = 2. - self - assert: - (emailAddresses first id = id1 or: [emailAddresses last id = id1]). - self - assert: - (emailAddresses first id = id2 or: [emailAddresses last id = id2]). - self assert: emailAddresses first id ~= emailAddresses last id. + self assert: + (emailAddresses first id = id1 or: [ emailAddresses last id = id1 ]). + self assert: + (emailAddresses first id = id2 or: [ emailAddresses last id = id2 ]). + self assert: emailAddresses first id ~= emailAddresses last id ] diff --git a/src/Glorp-Integration-Tests/GlorpReadingPersonWithoutEmailAddressesTest.class.st b/src/Glorp-Integration-Tests/GlorpReadingPersonWithoutEmailAddressesTest.class.st new file mode 100644 index 00000000..a2604ba2 --- /dev/null +++ b/src/Glorp-Integration-Tests/GlorpReadingPersonWithoutEmailAddressesTest.class.st @@ -0,0 +1,56 @@ +Class { + #name : #GlorpReadingPersonWithoutEmailAddressesTest, + #superclass : #GlorpTestCase, + #instVars : [ + 'session', + 'personRow', + 'addressRow', + 'emailAddress1Row', + 'emailAddress2Row', + 'id1', + 'id2', + 'oldReuseState' + ], + #category : #'Glorp-Integration-Tests-Database' +} + +{ #category : #resources } +GlorpReadingPersonWithoutEmailAddressesTest class >> resources [ + ^Array with: GlorpSessionResource. +] + +{ #category : #running } +GlorpReadingPersonWithoutEmailAddressesTest >> setUp [ + + super setUp. + session := GlorpSessionResource current newSession. + session beginTransaction. + personRow := session system examplePersonRow2. + session writeRow: personRow. + +] + +{ #category : #running } +GlorpReadingPersonWithoutEmailAddressesTest >> tearDown [ + + oldReuseState isNil ifFalse: + [session reusePreparedStatements: oldReuseState]. + session rollbackTransaction. + session reset. + session := nil. + super tearDown +] + +{ #category : #tests } +GlorpReadingPersonWithoutEmailAddressesTest >> testReadMultipleObjectsOneToMany [ + | query result person addresses | + + query := Query read: GlorpPerson where: [:eachPerson | eachPerson id = 4]. + query alsoFetch: [:each | each emailAddresses asOuterJoin]. + result := query executeIn: session. + self assert: result size = 1. + person := result first. + addresses := person emailAddresses. + self deny: addresses isGlorpProxy. "testing PR https://github.com/pharo-rdbms/glorp/pull/115" + self assert: addresses size = 0. +] From d3e84d5e2fa3c39847e36144c9f2d27f566bd260 Mon Sep 17 00:00:00 2001 From: Gabriel Omar Cotelli Date: Mon, 24 Jul 2023 15:11:30 -0300 Subject: [PATCH 3/3] Remove unused variables and fix some lint issues --- ...ngPersonWithoutEmailAddressesTest.class.st | 21 +++++++------------ 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/src/Glorp-Integration-Tests/GlorpReadingPersonWithoutEmailAddressesTest.class.st b/src/Glorp-Integration-Tests/GlorpReadingPersonWithoutEmailAddressesTest.class.st index a2604ba2..a74f42e5 100644 --- a/src/Glorp-Integration-Tests/GlorpReadingPersonWithoutEmailAddressesTest.class.st +++ b/src/Glorp-Integration-Tests/GlorpReadingPersonWithoutEmailAddressesTest.class.st @@ -3,13 +3,7 @@ Class { #superclass : #GlorpTestCase, #instVars : [ 'session', - 'personRow', - 'addressRow', - 'emailAddress1Row', - 'emailAddress2Row', - 'id1', - 'id2', - 'oldReuseState' + 'personRow' ], #category : #'Glorp-Integration-Tests-Database' } @@ -33,8 +27,6 @@ GlorpReadingPersonWithoutEmailAddressesTest >> setUp [ { #category : #running } GlorpReadingPersonWithoutEmailAddressesTest >> tearDown [ - oldReuseState isNil ifFalse: - [session reusePreparedStatements: oldReuseState]. session rollbackTransaction. session reset. session := nil. @@ -43,14 +35,15 @@ GlorpReadingPersonWithoutEmailAddressesTest >> tearDown [ { #category : #tests } GlorpReadingPersonWithoutEmailAddressesTest >> testReadMultipleObjectsOneToMany [ + | query result person addresses | - query := Query read: GlorpPerson where: [:eachPerson | eachPerson id = 4]. - query alsoFetch: [:each | each emailAddresses asOuterJoin]. + query := Query read: GlorpPerson where: [ :eachPerson | eachPerson id = 4 ]. + query alsoFetch: [ :each | each emailAddresses asOuterJoin ]. result := query executeIn: session. - self assert: result size = 1. + self assert: result size equals: 1. person := result first. addresses := person emailAddresses. - self deny: addresses isGlorpProxy. "testing PR https://github.com/pharo-rdbms/glorp/pull/115" - self assert: addresses size = 0. + self deny: addresses isGlorpProxy "testing PR https://github.com/pharo-rdbms/glorp/pull/115". + self assert: addresses size equals: 0 ]