[Pharo-users] Glorp: #includesKey:

Herby Vojčík herby at mailbox.sk
Wed Oct 25 06:32:34 EDT 2017


jtuchel at objektfabrik.de wrote:
> Herby,
>
> I must admit I've never used Dictionary Mappings with Glorp, so I don't
> have an answer.
> But I am a bit confused by your code examples. See below
>
>
> Am 24.10.17 um 20:27 schrieb Herby Vojčík:
>> Hello!
>>
>> I am using a DictionaryMapping in my code, and I wanted to use
>> #includesKey: in #where: clause (something akin
>>
>>   each tools includesKey: aToolId
>
> What SQL expression would you expect here?

SELECT * FROM AGENT a WHERE a.tool_id = :aToolId

AFAICT, DISTINCT is not needed as <id, tool_id> are fks to other table's 
compound primary key <agent_id, id>, so they are known to be unique.

> I would guess that you want to build a subquery like exists, because the
> way I understand the query, you want to find all instances of (whatever
> each is) that hold an Association in their tools dictionary where the
> key is aToolId.

Yes.

Maybe it needs EXISTS, I don't know. Semantics is clear, though.

>>
>> ) to select only rows for which DictionaryMapping uses certain key. It
>> failed with the error in the lines of "#tools does not resolve to
>> field". I had to come up with
>>
>
>> each tools anySatisfy: [ :tool | tool id = aToolId ]
> Hmm. This makes me wonder. Is #tools really a Dictionary? Inside the
> Block, I'd expect the :tool parameter to be an Association, and that
> doesn't understand #id,does it? I guess @each is the parameter within an
> Block like in
>
> self session read: MyClass where: [:each| each tools ...]
>
> If so, I have a hard time believing that anySatisfy: would work (never
> tried)...

Yes, it works. Dictionary enumerates values, as I have written in reply 
to Tom's post.

>> Is it the bug / feature / problem in my approach? If bug, is it
>> planned to add #includesKey: translation to DictionaryMapping?
>>
> I don't know, but would guess it is not currently on the Todo-list.
>
> My first tip would be to try and find some slides (most likely made by
> Niall and presented at an ESUG) including the words "subquery", "glorp"
> and "exists". You won't find much, but that may be a starting point.

I actually managed to get there, but
   a) using ugly workaround IMO, #includesKey: is part of dictionary's 
protocol, should be known;
   b) as I wrote in Tom's reply, the workaround only worked because 
mapping was to object. If the mapping was to primitive value (number, 
string), I would not have any 'tool id' ready to use and I would be left 
.... without option. There is no way to construct such query atm in 
Glorp, afaict, if I cannot use #keys not #includesKey: in where clause. 
Is that not a bug?

> Not sure this helps, ;-)
>
>
> Joachim

Thanks, Herby




More information about the Pharo-users mailing list