Skip to content

Commit

Permalink
Merge pull request #115 from ironirc/alsoFetchNowResolvesCollectionProxy
Browse files Browse the repository at this point in the history
alsoFetch: (a ToManyRelation) now also resolves the collection proxy
  • Loading branch information
gcotelli authored Jul 24, 2023
2 parents 5d05e40 + d3e84d5 commit 851d7d3
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
Class {
#name : #GlorpReadingPersonWithoutEmailAddressesTest,
#superclass : #GlorpTestCase,
#instVars : [
'session',
'personRow'
],
#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 [

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 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 equals: 0
]
32 changes: 12 additions & 20 deletions src/Glorp/GlorpAttributeModel.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
38 changes: 19 additions & 19 deletions src/Glorp/GlorpCursoredStream.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Class {
'rawPeekBuffer',
'statement'
],
#category : 'Glorp-Queries'
#category : #'Glorp-Queries'
}

{ #category : #'instance creation' }
Expand Down Expand Up @@ -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' }
Expand Down

0 comments on commit 851d7d3

Please sign in to comment.