module Database.Beam.Query.Ord
( SqlEq(..), SqlEqQuantified(..)
, SqlOrd(..), SqlOrdQuantified(..)
, QQuantified(..)
, anyOf_, anyIn_
, allOf_, allIn_
, between_
, in_ ) where
import Database.Beam.Query.Internal
import Database.Beam.Query.Types
import Database.Beam.Query.Operator
import Database.Beam.Schema.Tables
import Database.Beam.Backend.SQL
import Control.Applicative
import Control.Monad.State
import Data.Maybe
data QQuantified expr s r
= QQuantified (Sql92ExpressionQuantifierSyntax expr) (WithExprContext expr)
allOf_
:: forall s r select expr db.
( ThreadRewritable (QNested s) r
, ProjectibleInSelectSyntax select r
, IsSql92SelectSyntax select
, IsSql92ExpressionSyntax expr
, HasQBuilder select
, Sql92ExpressionSelectSyntax expr ~ select )
=> Q select db (QNested s) r
-> QQuantified expr s (WithRewrittenThread (QNested s) s r)
allOf_ s = QQuantified quantifyOverAll (\tblPfx -> subqueryE (buildSqlQuery tblPfx s))
allIn_
:: forall s a expr
. ( IsSql92ExpressionSyntax expr )
=> [QExpr expr s a]
-> QQuantified expr s a
allIn_ es = QQuantified quantifyOverAll (rowE <$> mapM (\(QExpr e) -> e) es)
anyOf_
:: forall s r select expr db.
( ThreadRewritable (QNested s) r
, ProjectibleInSelectSyntax select r
, IsSql92SelectSyntax select
, IsSql92ExpressionSyntax expr
, HasQBuilder select
, Sql92ExpressionSelectSyntax expr ~ select )
=> Q select db (QNested s) r
-> QQuantified expr s (WithRewrittenThread (QNested s) s r)
anyOf_ s = QQuantified quantifyOverAny (\tblPfx -> subqueryE (buildSqlQuery tblPfx s))
anyIn_
:: forall s a expr
. ( IsSql92ExpressionSyntax expr )
=> [QExpr expr s a]
-> QQuantified expr s a
anyIn_ es = QQuantified quantifyOverAny (rowE <$> mapM (\(QExpr e) -> e) es)
between_ :: IsSql92ExpressionSyntax syntax
=> QGenExpr context syntax s a -> QGenExpr context syntax s a
-> QGenExpr context syntax s a -> QGenExpr context syntax s Bool
between_ (QExpr a) (QExpr min_) (QExpr max_) =
QExpr (liftA3 betweenE a min_ max_)
in_ :: ( IsSql92ExpressionSyntax syntax
, HasSqlValueSyntax (Sql92ExpressionValueSyntax syntax) Bool )
=> QGenExpr context syntax s a
-> [ QGenExpr context syntax s a ]
-> QGenExpr context syntax s Bool
in_ _ [] = QExpr (pure (valueE (sqlValueSyntax False)))
in_ (QExpr row) options = QExpr (inE <$> row <*> mapM (\(QExpr o) -> o) options)
class SqlEq expr a | a -> expr where
(==.) :: a -> a -> expr Bool
(/=.) :: a -> a -> expr Bool
class SqlEq expr a => SqlEqQuantified expr quantified a | a -> expr quantified where
(==*.), (/=*.) :: a -> quantified -> expr Bool
infix 4 ==., /=., ==*., /=*.
infix 4 <., >., <=., >=.
infix 4 <*., >*., <=*., >=*.
instance IsSql92ExpressionSyntax syntax =>
SqlEq (QGenExpr context syntax s) (QGenExpr context syntax s a) where
(==.) = qBinOpE (eqE Nothing)
(/=.) = qBinOpE (neqE Nothing)
instance IsSql92ExpressionSyntax syntax =>
SqlEqQuantified (QGenExpr context syntax s) (QQuantified syntax s a) (QGenExpr context syntax s a) where
a ==*. QQuantified q b = qBinOpE (eqE (Just q)) a (QExpr b)
a /=*. QQuantified q b = qBinOpE (neqE (Just q)) a (QExpr b)
instance ( IsSql92ExpressionSyntax syntax
, HasSqlValueSyntax (Sql92ExpressionValueSyntax syntax) Bool
, Beamable tbl ) =>
SqlEq (QGenExpr context syntax s) (tbl (QGenExpr context syntax s)) where
a ==. b = let (_, e) = runState (zipBeamFieldsM
(\x'@(Columnar' x) (Columnar' y) ->
do modify (\expr ->
case expr of
Nothing -> Just $ x ==. y
Just expr' -> Just $ expr' &&. x ==. y)
return x') a b) Nothing
in fromMaybe (QExpr (\_ -> valueE (sqlValueSyntax True))) e
a /=. b = not_ (a ==. b)
instance ( IsSql92ExpressionSyntax syntax
, HasSqlValueSyntax (Sql92ExpressionValueSyntax syntax) Bool
, Beamable tbl)
=> SqlEq (QGenExpr context syntax s) (tbl (Nullable (QGenExpr context syntax s))) where
a ==. b = let (_, e) = runState (zipBeamFieldsM
(\x'@(Columnar' x) (Columnar' y) -> do
modify (\expr ->
case expr of
Nothing -> Just $ x ==. y
Just expr' -> Just $ expr' &&. x ==. y)
return x') a b) Nothing
in fromMaybe (QExpr (\_ -> valueE (sqlValueSyntax True))) e
a /=. b = not_ (a ==. b)
class SqlEq expr e => SqlOrd expr e | e -> expr where
(<.), (>.), (<=.), (>=.) :: e -> e -> expr Bool
class (SqlOrd expr e, SqlEqQuantified expr quantified e) =>
SqlOrdQuantified expr quantified e | e -> expr quantified where
(<*.), (>*.), (<=*.), (>=*.) :: e -> quantified -> expr Bool
instance IsSql92ExpressionSyntax syntax =>
SqlOrd (QGenExpr context syntax s) (QGenExpr context syntax s a) where
(<.) = qBinOpE (ltE Nothing)
(>.) = qBinOpE (gtE Nothing)
(<=.) = qBinOpE (leE Nothing)
(>=.) = qBinOpE (geE Nothing)
instance IsSql92ExpressionSyntax syntax =>
SqlOrdQuantified (QGenExpr context syntax s) (QQuantified syntax s a) (QGenExpr context syntax s a) where
a <*. QQuantified q b = qBinOpE (ltE (Just q)) a (QExpr b)
a <=*. QQuantified q b = qBinOpE (leE (Just q)) a (QExpr b)
a >*. QQuantified q b = qBinOpE (gtE (Just q)) a (QExpr b)
a >=*. QQuantified q b = qBinOpE (geE (Just q)) a (QExpr b)