From 2986d0996ec99699980f47a59401f73fc3e3ae0a Mon Sep 17 00:00:00 2001 From: Felipe Lessa Date: Mon, 3 Sep 2012 17:03:07 -0300 Subject: [PATCH] Nicer 'from' interface. Instead of select $ do (x,y,z) <- from where_ (z^.f ==. y^.f) return (x, y^.f, z) now you may write select $ from $ \(x,y,z) -> do where_ (z^.f ==. y^.f) return (x, y^.f, z) Now the only difference in reading order wrt. SQL is the return on the bottom of the expression. =) Note that this does not change at all the expressivity of the language since oldFrom = from return --- src/Database/Esqueleto/Internal/Language.hs | 48 +++++++++++---------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/src/Database/Esqueleto/Internal/Language.hs b/src/Database/Esqueleto/Internal/Language.hs index 10a14e1..02f9a8f 100644 --- a/src/Database/Esqueleto/Internal/Language.hs +++ b/src/Database/Esqueleto/Internal/Language.hs @@ -56,45 +56,49 @@ infixr 3 &&. infixr 2 ||. +-- | @FROM@ clause: bring an entity into scope. +-- +-- The following types implement 'from': +-- +-- * @Expr (Entity val)@, which brings a single entity into scope. +-- +-- * Tuples of any other types supported by 'from'. Calling +-- 'from' multiple times is the same as calling 'from' a +-- single time and using a tuple. +-- +-- Note that using 'from' for the same entity twice does work +-- and corresponds to a self-join. You don't even need to use +-- two different calls to 'from', you may use a tuple. +from :: From query expr backend a => (a -> query b) -> query b +from = (from_ >>=) + + class Esqueleto query expr backend => From query expr backend a where - -- | @FROM@ clause: bring an entity into scope. - -- - -- The following types implement 'from': - -- - -- * @Expr (Entity val)@, which brings a single entity into scope. - -- - -- * Tuples of any other types supported by 'from'. Calling - -- 'from' multiple times is the same as calling 'from' a - -- single time and using a tuple. - -- - -- Note that using 'from' for the same entity twice does work - -- and corresponds to a self-join. You don't even need to use - -- two different calls to 'from', you may use a tuple. - from :: query a + from_ :: query a instance ( Esqueleto query expr backend , PersistEntity val , PersistEntityBackend val ~ backend ) => From query expr backend (expr (Entity val)) where - from = fromSingle + from_ = fromSingle instance ( From query expr backend a , From query expr backend b ) => From query expr backend (a, b) where - from = (,) <$> from <*> from + from_ = (,) <$> from_ <*> from_ instance ( From query expr backend a , From query expr backend b , From query expr backend c ) => From query expr backend (a, b, c) where - from = (,,) <$> from <*> from <*> from + from_ = (,,) <$> from_ <*> from_ <*> from_ instance ( From query expr backend a , From query expr backend b , From query expr backend c , From query expr backend d ) => From query expr backend (a, b, c, d) where - from = (,,,) <$> from <*> from <*> from <*> from + from_ = (,,,) <$> from_ <*> from_ <*> from_ <*> from_ instance ( From query expr backend a , From query expr backend b @@ -102,7 +106,7 @@ instance ( From query expr backend a , From query expr backend d , From query expr backend e ) => From query expr backend (a, b, c, d, e) where - from = (,,,,) <$> from <*> from <*> from <*> from <*> from + from_ = (,,,,) <$> from_ <*> from_ <*> from_ <*> from_ <*> from_ instance ( From query expr backend a , From query expr backend b @@ -111,7 +115,7 @@ instance ( From query expr backend a , From query expr backend e , From query expr backend f ) => From query expr backend (a, b, c, d, e, f) where - from = (,,,,,) <$> from <*> from <*> from <*> from <*> from <*> from + from_ = (,,,,,) <$> from_ <*> from_ <*> from_ <*> from_ <*> from_ <*> from_ instance ( From query expr backend a , From query expr backend b @@ -121,7 +125,7 @@ instance ( From query expr backend a , From query expr backend f , From query expr backend g ) => From query expr backend (a, b, c, d, e, f, g) where - from = (,,,,,,) <$> from <*> from <*> from <*> from <*> from <*> from <*> from + from_ = (,,,,,,) <$> from_ <*> from_ <*> from_ <*> from_ <*> from_ <*> from_ <*> from_ instance ( From query expr backend a , From query expr backend b @@ -132,4 +136,4 @@ instance ( From query expr backend a , From query expr backend g , From query expr backend h ) => From query expr backend (a, b, c, d, e, f, g, h) where - from = (,,,,,,,) <$> from <*> from <*> from <*> from <*> from <*> from <*> from <*> from + from_ = (,,,,,,,) <$> from_ <*> from_ <*> from_ <*> from_ <*> from_ <*> from_ <*> from_ <*> from_