module Database.Esqueleto.Internal.Language
(
Esqueleto(..)
, from
, Value(..)
, InnerJoin(..)
, CrossJoin(..)
, LeftOuterJoin(..)
, RightOuterJoin(..)
, FullOuterJoin(..)
, OnClauseWithoutMatchingJoinException(..)
, OrderBy
, Update
, JoinKind(..)
, IsJoinKind(..)
, PreprocessedFrom
, From
, FromPreprocess
) where
import Control.Applicative (Applicative(..), (<$>))
import Control.Exception (Exception)
import Data.Typeable (Typeable)
import Database.Persist.GenericSql
import Database.Persist.Store
class (Functor query, Applicative query, Monad query) =>
Esqueleto query expr backend | query -> expr backend, expr -> query backend where
fromStart
:: ( PersistEntity a
, PersistEntityBackend a ~ backend )
=> query (expr (PreprocessedFrom (expr (Entity a))))
fromStartMaybe
:: ( PersistEntity a
, PersistEntityBackend a ~ backend )
=> query (expr (PreprocessedFrom (expr (Maybe (Entity a)))))
fromJoin
:: IsJoinKind join
=> expr (PreprocessedFrom a)
-> expr (PreprocessedFrom b)
-> query (expr (PreprocessedFrom (join a b)))
fromFinish
:: expr (PreprocessedFrom a)
-> query a
where_ :: expr (Value Bool) -> query ()
on :: expr (Value Bool) -> query ()
orderBy :: [expr OrderBy] -> query ()
asc :: PersistField a => expr (Value a) -> expr OrderBy
desc :: PersistField a => expr (Value a) -> expr OrderBy
sub_select :: PersistField a => query (expr (Value a)) -> expr (Value a)
sub_selectDistinct :: PersistField a => query (expr (Value a)) -> expr (Value a)
(^.) :: (PersistEntity val, PersistField typ) =>
expr (Entity val) -> EntityField val typ -> expr (Value typ)
(?.) :: (PersistEntity val, PersistField typ) =>
expr (Maybe (Entity val)) -> EntityField val typ -> expr (Value (Maybe typ))
val :: PersistField typ => typ -> expr (Value typ)
isNothing :: PersistField typ => expr (Value (Maybe typ)) -> expr (Value Bool)
just :: expr (Value typ) -> expr (Value (Maybe typ))
nothing :: expr (Value (Maybe typ))
countRows :: Num a => expr (Value a)
not_ :: expr (Value Bool) -> expr (Value Bool)
(==.) :: PersistField typ => expr (Value typ) -> expr (Value typ) -> expr (Value Bool)
(>=.) :: PersistField typ => expr (Value typ) -> expr (Value typ) -> expr (Value Bool)
(>.) :: PersistField typ => expr (Value typ) -> expr (Value typ) -> expr (Value Bool)
(<=.) :: PersistField typ => expr (Value typ) -> expr (Value typ) -> expr (Value Bool)
(<.) :: PersistField typ => expr (Value typ) -> expr (Value typ) -> expr (Value Bool)
(!=.) :: PersistField typ => expr (Value typ) -> expr (Value typ) -> expr (Value Bool)
(&&.) :: expr (Value Bool) -> expr (Value Bool) -> expr (Value Bool)
(||.) :: expr (Value Bool) -> expr (Value Bool) -> expr (Value Bool)
(+.) :: PersistField a => expr (Value a) -> expr (Value a) -> expr (Value a)
(-.) :: PersistField a => expr (Value a) -> expr (Value a) -> expr (Value a)
(/.) :: PersistField a => expr (Value a) -> expr (Value a) -> expr (Value a)
(*.) :: PersistField a => expr (Value a) -> expr (Value a) -> expr (Value a)
set :: PersistEntity val => expr (Entity val) -> [expr (Update val)] -> query ()
(=.) :: (PersistEntity val, PersistField typ) => EntityField val typ -> expr (Value typ) -> expr (Update val)
(+=.) :: (PersistEntity val, PersistField a) => EntityField val a -> expr (Value a) -> expr (Update val)
(-=.) :: (PersistEntity val, PersistField a) => EntityField val a -> expr (Value a) -> expr (Update val)
(*=.) :: (PersistEntity val, PersistField a) => EntityField val a -> expr (Value a) -> expr (Update val)
(/=.) :: (PersistEntity val, PersistField a) => EntityField val a -> expr (Value a) -> expr (Update val)
infixl 9 ^.
infixl 7 *., /.
infixl 6 +., -.
infix 4 ==., >=., >., <=., <., !=.
infixr 3 &&., =., +=., -=., *=., /=.
infixr 2 ||., `InnerJoin`, `CrossJoin`, `LeftOuterJoin`, `RightOuterJoin`, `FullOuterJoin`
data Value a = Value a deriving (Eq, Ord, Show, Typeable)
data InnerJoin a b = a `InnerJoin` b
data CrossJoin a b = a `CrossJoin` b
data LeftOuterJoin a b = a `LeftOuterJoin` b
data RightOuterJoin a b = a `RightOuterJoin` b
data FullOuterJoin a b = a `FullOuterJoin` b
data JoinKind =
InnerJoinKind
| CrossJoinKind
| LeftOuterJoinKind
| RightOuterJoinKind
| FullOuterJoinKind
class IsJoinKind join where
smartJoin :: a -> b -> join a b
reifyJoinKind :: join a b -> JoinKind
instance IsJoinKind InnerJoin where
smartJoin a b = a `InnerJoin` b
reifyJoinKind _ = InnerJoinKind
instance IsJoinKind CrossJoin where
smartJoin a b = a `CrossJoin` b
reifyJoinKind _ = CrossJoinKind
instance IsJoinKind LeftOuterJoin where
smartJoin a b = a `LeftOuterJoin` b
reifyJoinKind _ = LeftOuterJoinKind
instance IsJoinKind RightOuterJoin where
smartJoin a b = a `RightOuterJoin` b
reifyJoinKind _ = RightOuterJoinKind
instance IsJoinKind FullOuterJoin where
smartJoin a b = a `FullOuterJoin` b
reifyJoinKind _ = FullOuterJoinKind
data OnClauseWithoutMatchingJoinException =
OnClauseWithoutMatchingJoinException String
deriving (Eq, Ord, Show, Typeable)
instance Exception OnClauseWithoutMatchingJoinException where
data PreprocessedFrom a
data OrderBy
data Update typ
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_ :: query a
instance ( Esqueleto query expr backend
, FromPreprocess query expr backend (expr (Entity val))
) => From query expr backend (expr (Entity val)) where
from_ = fromPreprocess >>= fromFinish
instance ( Esqueleto query expr backend
, FromPreprocess query expr backend (expr (Maybe (Entity val)))
) => From query expr backend (expr (Maybe (Entity val))) where
from_ = fromPreprocess >>= fromFinish
instance ( Esqueleto query expr backend
, FromPreprocess query expr backend (InnerJoin a b)
) => From query expr backend (InnerJoin a b) where
from_ = fromPreprocess >>= fromFinish
instance ( Esqueleto query expr backend
, FromPreprocess query expr backend (CrossJoin a b)
) => From query expr backend (CrossJoin a b) where
from_ = fromPreprocess >>= fromFinish
instance ( Esqueleto query expr backend
, FromPreprocess query expr backend (LeftOuterJoin a b)
) => From query expr backend (LeftOuterJoin a b) where
from_ = fromPreprocess >>= fromFinish
instance ( Esqueleto query expr backend
, FromPreprocess query expr backend (RightOuterJoin a b)
) => From query expr backend (RightOuterJoin a b) where
from_ = fromPreprocess >>= fromFinish
instance ( Esqueleto query expr backend
, FromPreprocess query expr backend (FullOuterJoin a b)
) => From query expr backend (FullOuterJoin a b) where
from_ = fromPreprocess >>= fromFinish
instance ( From query expr backend a
, From query expr backend b
) => From query expr backend (a, b) where
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_
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_
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 e
) => From query expr backend (a, b, c, d, e) 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 d
, 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_
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 e
, 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_
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 e
, From query expr backend f
, 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_
class Esqueleto query expr backend => FromPreprocess query expr backend a where
fromPreprocess :: query (expr (PreprocessedFrom a))
instance ( Esqueleto query expr backend
, PersistEntity val
, PersistEntityBackend val ~ backend
) => FromPreprocess query expr backend (expr (Entity val)) where
fromPreprocess = fromStart
instance ( Esqueleto query expr backend
, PersistEntity val
, PersistEntityBackend val ~ backend
) => FromPreprocess query expr backend (expr (Maybe (Entity val))) where
fromPreprocess = fromStartMaybe
instance ( Esqueleto query expr backend
, FromPreprocess query expr backend a
, FromPreprocess query expr backend b
, IsJoinKind join
) => FromPreprocess query expr backend (join a b) where
fromPreprocess = do
a <- fromPreprocess
b <- fromPreprocess
fromJoin a b