New Query Improvements (DONE)

5 views
Skip to first unread message

Dan Kubb (dkubb)

unread,
Nov 3, 2009, 4:23:56 PM11/3/09
to DataMapper
Hi Everyone,

I just wanted to give everyone a follow-up on the new query
improvements I hinted at last week. For those of you who missed it,
here was the original post:

http://bit.ly/10gIJb

As of a few minutes ago, I just committed changes that would permit
Set operations to be performed on a Collection, eg:

Union
--------

Person.all(:name => 'Dan Kubb') | Person.all(:name => 'Alex Kubb')
Person.all(:name => 'Dan Kubb') + Person.all(:name => 'Alex Kubb')

# => SELECT "id", "name" FROM "people" WHERE ("name" = 'Dan Kubb' OR
"name" = 'Alex Kubb') ORDER BY "id"

Intersection
----------------

Person.all(:name => 'Dan Kubb') & Person.all(:name => 'Alex Kubb')

# => SELECT "id", "name" FROM "people" WHERE ("name" = 'Dan Kubb'
AND "name" = 'Alex Kubb') ORDER BY "id"

Difference
--------------

Person.all(:name => 'Dan Kubb') - Person.all(:name => 'Alex Kubb')

# => SELECT "id", "name" FROM "people" WHERE ("name" <> 'Alex Kubb'
AND "name" = 'Dan Kubb') ORDER BY "id"

This should work fine with any kind of Collection that you return from
"all", including 1:m and m:m associations. There's nothing special
about these Collections, so you can still chain them, lazy load and do
all the other things you've come to expect from DataMapper.

I have not yet updated "all" chaining to use intersection, as I hinted
in the original email. I wanted to get this out and tested before
swapping DM's collection chaining algo with Collection#intersection.
That probably won't happen until after the gem release this week.

In case you're curious to see how this was implemented, check out
these commits:

Operation: http://bit.ly/2jTYs
Query: http://bit.ly/IjyIR
Collection: http://bit.ly/2AJ7Jy

As you can see the implementation is fairly clean and simple. I'm
guessing it took about 40-50 lines of actual code, although once I
refactor collection chaining to use intersection there will probably
be a reduction of about 200-300 loc overall. Actually there's more
docs than code, and 5-10x more specs than that even! The new code is
fully covered by the specs with rcov, and pass with heckle.

If you haven't already done so, please checkout edge dm-core and play
around with this to construct queries. Let me know of any issues you
run into either on the mailing list, on IRC in #datamapper @
freenode.net, or in the ticket tracker: http://bit.ly/x8ZLU

--

Dan
(dkubb)

Thanatos

unread,
Nov 3, 2009, 4:29:45 PM11/3/09
to DataMapper
Looks great! I look forward to using this, or seeing how dm-sugar-
glider makes use of it!

Thanks Dan

- Tony

Brian Smith

unread,
Nov 3, 2009, 5:37:54 PM11/3/09
to datam...@googlegroups.com
Sweetness!

tjholowaychuk

unread,
Nov 3, 2009, 10:56:16 PM11/3/09
to DataMapper
very cool! I will add this to my 'ch' sheet asap
Reply all
Reply to author
Forward
0 new messages