Possible fix for issue #56: Prevent compilation of mass update without where clause

4 views
Skip to first unread message

Marco Rico-Gomez

unread,
Mar 2, 2011, 5:39:39 AM3/2/11
to squeryl-co...@googlegroups.com
Hi all,

I have written a possible fix for issue #56
(https://github.com/max-l/Squeryl/issues/56). This solution uses a
phantom type instead of splitting the WhereState trait into two
separate traits.

The main idea is to add a parameter type (Cond) to the WhereState
which indicates whether a where clause is present (Conditioned) or not
(Unconditioned).
---
abstract sealed class Conditioned
abstract sealed class Unconditioned

trait WhereState[Cond] { ...}
---

Now the set function has an implicit parameter which "tests" for the
right Cond parameter type. So the set function can only be called if a
where clause is present. Otherwise compilation fails.
---
def set(...)(implicit cond: Cond =:= Conditioned)
---

From a user perspective there are no visible changes. The API can be
used as before:
---
update(students)(s => where(s.id === 1)set(s.age := Some(30))
---

But the following causes a compilation error:
---
update(students)(s => set(s.age := Some(30))
---

I saw this approach on a 4sq blog post some time ago. They are using
it for their mongo dsl.

To support mass updates I introduced a new function called setAll
which can only be used without a where clause.

Changes can be found here:
https://github.com/mrico/Squeryl/commit/f4910a5f40890e1912f05c358a20023af755b334

What do you think?

--
\ Marco
--
Twitter: @mricog
Website: http://mrico.eu

Maxime Lévesque

unread,
Mar 2, 2011, 9:54:26 AM3/2/11
to squeryl-co...@googlegroups.com

l like it, I'm glad to be instructed about this technique, if it passes all tests, 
feel free to merge it in master, if it's not to much asking, please put a little 
mention of it in the gh-pages docs and close the issue(56).

Thanks !

ML

Marco Rico-Gomez

unread,
Mar 2, 2011, 5:21:18 PM3/2/11
to squeryl-co...@googlegroups.com
Ok, I merged the changes in master and updated the docs.
Feel free to adjust the text since I'm not a native speaker.

Thanks for your kind feedback!

Marco

Maxime Lévesque

unread,
Mar 2, 2011, 6:54:24 PM3/2/11
to squeryl-co...@googlegroups.com
 I looked at the docs, it looks perfect to the non native speaker that I am  ;-)
Reply all
Reply to author
Forward
0 new messages