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
Thanks for your kind feedback!
Marco