{-# LANGUAGE LiberalTypeSynonyms #-}
{-# LANGUAGE ImpredicativeTypes #-}
module PgSchema.DML.Select.Types
  -- ( QueryParam(..), qpEmpty
  -- , CondWithPath(..), OrdWithPath(..), DistWithPath(..), LimOffWithPath(..)
  -- , LO(..)
  -- , CondMonad, qRoot, qPath, qWhere, qOrderBy, qDistinct, qDistinctOn, qLimit, qOffset
  -- , Cond(..), pnull, pchild, pparent, pnot, pin, pinArr, pUnsafeCond
  -- , (|||), (&&&), (<?),(>?),(<=?),(>=?),(=?),(~=?),(~~?), showCmp, BoolOp(..)
  -- , TabParam(..), OrdFld(..), Dist(..), defTabParam, defLO, lo1
  -- , OrdDirection(..), ascf, descf, ordf
  -- , SomeToField(..)
  -- )
  where

import Control.Monad.RWS
import Data.Kind
import Data.Proxy
import Data.Singletons (demote)
import Data.List as L
import Data.List.NonEmpty as NE
import Data.String
import Data.Text(Text)
import Database.PostgreSQL.Simple.ToField
import PgSchema.Schema
import PgSchema.Types
import GHC.Generics
import GHC.Natural
import GHC.TypeLits
import PgSchema.Utils.Internal
import PgSchema.Utils.TF


-- | Parameters that are used to describe @SELECT@.
--
-- You don't need to make it directly. Use 'MonadQP' to define 'QueryParam' instead.
--
data QueryParam sch t = QueryParam
  { forall sch (t :: NameNSK). QueryParam sch t -> [CondWithPath sch t]
qpConds     :: ![CondWithPath sch t]    -- ^ `where` conditions for branches of Data-Tree
  , forall sch (t :: NameNSK). QueryParam sch t -> [OrdWithPath sch t]
qpOrds      :: ![OrdWithPath sch t]     -- ^ `order by` clauses
  , forall sch (t :: NameNSK).
QueryParam sch t -> [LimOffWithPath sch t]
qpLOs       :: ![LimOffWithPath sch t]  -- ^ `limit/offset` clauses
  , forall sch (t :: NameNSK). QueryParam sch t -> [DistWithPath sch t]
qpDistinct  :: ![DistWithPath sch t]    -- ^ `distinct` and `distinct on` clauses
  }

-- | Empty 'QueryParam'.
--
-- It means that @SELECT@ is defined only by structure of output type
qpEmpty :: forall sch t. QueryParam sch t
qpEmpty :: forall sch (t :: NameNSK). QueryParam sch t
qpEmpty = [CondWithPath sch t]
-> [OrdWithPath sch t]
-> [LimOffWithPath sch t]
-> [DistWithPath sch t]
-> QueryParam sch t
forall sch (t :: NameNSK).
[CondWithPath sch t]
-> [OrdWithPath sch t]
-> [LimOffWithPath sch t]
-> [DistWithPath sch t]
-> QueryParam sch t
QueryParam [] [] [] []

type MonadQP sch t path = (TabPath sch t path, ToStar path) => RWS (Proxy path) () (QueryParam sch t) ()

-- | Execute 'MonadQP' and get 'QueryParam'.
--
-- The table `t` defines a context and becomes the "current" table
qRoot :: RWS (Proxy '[]) () (QueryParam sch t) () -> QueryParam sch t
qRoot :: forall {a} sch (t :: NameNSK).
RWS (Proxy '[]) () (QueryParam sch t) () -> QueryParam sch t
qRoot RWS (Proxy '[]) () (QueryParam sch t) ()
m = (QueryParam sch t, ()) -> QueryParam sch t
forall a b. (a, b) -> a
fst ((QueryParam sch t, ()) -> QueryParam sch t)
-> (QueryParam sch t, ()) -> QueryParam sch t
forall a b. (a -> b) -> a -> b
$ RWS (Proxy '[]) () (QueryParam sch t) ()
-> Proxy '[] -> QueryParam sch t -> (QueryParam sch t, ())
forall r w s a. RWS r w s a -> r -> s -> (s, w)
execRWS RWS (Proxy '[]) () (QueryParam sch t) ()
m Proxy '[]
forall {k} (t :: k). Proxy t
Proxy QueryParam sch t
forall sch (t :: NameNSK). QueryParam sch t
qpEmpty

-- | Change context (current table) to parent or child table.
--
-- The 'Symbol' must name the foreign-key constraint (edge to the parent or from the child)
-- for the step away from the current table.
--
qPath :: forall sch t path path'.
  forall (p :: Symbol) ->
  (TabPath sch t path', ToStar path', path' ~ path ++ '[p]) =>
  MonadQP sch t path' -> MonadQP sch t path
qPath :: forall sch (t :: NameNSK) (path :: [Symbol]) (path' :: [Symbol]).
forall (p :: Symbol) ->
(TabPath sch t path', ToStar path', path' ~ (path ++ '[p])) =>
MonadQP sch t path' -> MonadQP sch t path
qPath _p MonadQP sch t path'
m = do
  s <- RWST (Proxy path) () (QueryParam sch t) Identity (QueryParam sch t)
forall s (m :: * -> *). MonadState s m => m s
get
  put $ fst $ execRWS m Proxy s

-- | Add @WHERE@ condition for the current table.
--
-- If several 'qWhere' exist they are composed according to the 'Monoid' instance for 'Cond', i.e. with '(&&&)'
qWhere :: forall sch t path. Cond sch (TabOnPath sch t path) -> MonadQP sch t path
qWhere :: forall sch (t :: NameNSK) (path :: [Symbol]).
Cond sch (TabOnPath sch t path) -> MonadQP sch t path
qWhere Cond sch (TabOnPath sch t path)
c = (QueryParam sch t -> QueryParam sch t)
-> RWST (Proxy path) () (QueryParam sch t) Identity ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify \QueryParam sch t
qp -> QueryParam sch t
qp { qpConds = CondWithPath @path c : qp.qpConds }

-- | Add @ORDER BY@ condition for the current table
--
-- Several 'qOrderBy' are possible and they are composed together.
--
-- Ordering can span the current table and joined parents. For example:
--
-- @
-- qOrderBy [ascf "f1"]
-- qPath "ref-to-parent" do
--   qOrderBy [descf "f2"]
-- qOrderBy [ascf "f3"]
-- @
--
-- we get @ORDER BY t1.f1, t2.f2 DESC, t1.f3@
--
qOrderBy :: forall sch t path. [OrdFld sch (TabOnPath sch t path)] -> MonadQP sch t path
qOrderBy :: forall sch (t :: NameNSK) (path :: [Symbol]).
[OrdFld sch (TabOnPath sch t path)] -> MonadQP sch t path
qOrderBy [OrdFld sch (TabOnPath sch t path)]
ofs = (QueryParam sch t -> QueryParam sch t)
-> RWST (Proxy path) () (QueryParam sch t) Identity ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify \QueryParam sch t
qp -> QueryParam sch t
qp { qpOrds = OrdWithPath @path ofs : qp.qpOrds }

-- | Add `DISTINCT` condition for the current table.
-- It is applied only to "root" or "children" tables.
qDistinct :: forall sch t path t'. TabOnPath2 sch t path ~ '(t', 'RelMany) =>
  MonadQP sch t path
qDistinct :: forall sch (t :: NameNSK) (path :: [Symbol]) (t' :: NameNSK).
(TabOnPath2 sch t path ~ '(t', 'RelMany)) =>
MonadQP sch t path
qDistinct = (QueryParam sch t -> QueryParam sch t)
-> RWST (Proxy path) () (QueryParam sch t) Identity ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify \QueryParam sch t
qp -> QueryParam sch t
qp { qpDistinct = DistWithPath @path Distinct : qp.qpDistinct }

-- | Add @DISTINCT ON@ condition for the current table.
--
-- It can also be applied on a parent table because that parent is joined to the current table.
--
-- 'qDistinctOn' automatically adds fields to the @ORDER BY@ clause (as PostgreSQL requires).
-- (That's why 'OrdDirection' is needed)
-- So having
--
-- @
-- qOrderBy [descf "f0"]
-- qDistinctOn [ascf "f1"]
-- qPath "ref-to-parent" do
--   qDistinctOn [descf "f2"]
-- qDistinctOn [ascf "f3"]
-- @
--
-- we get @DISTINCT ON (t1.f1, t2.f2, t1.f3) ... ORDER BY t1.f1, t2.f2 DESC, t1.f3, t1.f0 DESC@
--
qDistinctOn :: forall sch t path. [OrdFld sch (TabOnPath sch t path)] -> MonadQP sch t path
qDistinctOn :: forall sch (t :: NameNSK) (path :: [Symbol]).
[OrdFld sch (TabOnPath sch t path)] -> MonadQP sch t path
qDistinctOn [OrdFld sch (TabOnPath sch t path)]
ofs = (QueryParam sch t -> QueryParam sch t)
-> RWST (Proxy path) () (QueryParam sch t) Identity ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify \QueryParam sch t
qp -> QueryParam sch t
qp { qpDistinct = DistWithPath @path (DistinctOn ofs) : qp.qpDistinct }

-- | Add `LIMIT` condition for the current table.
-- It is applied only to "root" or "children" tables.
--
-- If 'qLimit' is applied several times on the same path, only the last one is used
qLimit :: forall sch t path. Snd (TabOnPath2 sch t path) ~ RelMany
  => Natural -> MonadQP sch t path
qLimit :: forall sch (t :: NameNSK) (path :: [Symbol]).
(Snd (TabOnPath2 sch t path) ~ 'RelMany) =>
Natural -> MonadQP sch t path
qLimit Natural
n = (QueryParam sch t -> QueryParam sch t)
-> RWST (Proxy path) () (QueryParam sch t) Identity ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify \QueryParam sch t
qp -> QueryParam sch t
qp { qpLOs = mk qp.qpLOs }
  where
    mk :: [LimOffWithPath sch t] -> [LimOffWithPath sch t]
mk [LimOffWithPath sch t]
xs = case (LimOffWithPath sch t -> Bool)
-> [LimOffWithPath sch t]
-> ([LimOffWithPath sch t], [LimOffWithPath sch t])
forall a. (a -> Bool) -> [a] -> ([a], [a])
L.break LimOffWithPath sch t -> Bool
eq [LimOffWithPath sch t]
xs of
      ([LimOffWithPath sch t]
xs1, []) -> LimOffWithPath sch t
new LimOffWithPath sch t
-> [LimOffWithPath sch t] -> [LimOffWithPath sch t]
forall a. a -> [a] -> [a]
: [LimOffWithPath sch t]
xs1
      ([LimOffWithPath sch t]
xs1, LimOffWithPath sch t
x:[LimOffWithPath sch t]
xs2) -> [LimOffWithPath sch t]
xs1 [LimOffWithPath sch t]
-> [LimOffWithPath sch t] -> [LimOffWithPath sch t]
forall a. Semigroup a => a -> a -> a
<> [LimOffWithPath sch t -> LimOffWithPath sch t
upd LimOffWithPath sch t
x] [LimOffWithPath sch t]
-> [LimOffWithPath sch t] -> [LimOffWithPath sch t]
forall a. Semigroup a => a -> a -> a
<> [LimOffWithPath sch t]
xs2
    eq :: LimOffWithPath sch t -> Bool
eq (LimOffWithPath @p LO
_) = forall (a :: [Symbol]).
(SingKind [Symbol], SingI a) =>
Demote [Symbol]
forall {k} (a :: k). (SingKind k, SingI a) => Demote k
demote @p Demote [Symbol] -> Demote [Symbol] -> Bool
forall a. Eq a => a -> a -> Bool
== forall (a :: [Symbol]).
(SingKind [Symbol], SingI a) =>
Demote [Symbol]
forall {k} (a :: k). (SingKind k, SingI a) => Demote k
demote @path
    upd :: LimOffWithPath sch t -> LimOffWithPath sch t
upd (LimOffWithPath @p LO
lo) = forall (fld :: [Symbol]) sch (t :: NameNSK).
(TabPath sch t fld, ToStar fld,
 Snd (TabOnPath2 sch t fld) ~ 'RelMany) =>
LO -> LimOffWithPath sch t
forall {k} (fld :: [Symbol]) (sch :: k) (t :: NameNSK).
(TabPath sch t fld, ToStar fld,
 Snd (TabOnPath2 sch t fld) ~ 'RelMany) =>
LO -> LimOffWithPath sch t
LimOffWithPath @p LO
lo{ limit = Just n }
    new :: LimOffWithPath sch t
new = forall (fld :: [Symbol]) sch (t :: NameNSK).
(TabPath sch t fld, ToStar fld,
 Snd (TabOnPath2 sch t fld) ~ 'RelMany) =>
LO -> LimOffWithPath sch t
forall {k} (fld :: [Symbol]) (sch :: k) (t :: NameNSK).
(TabPath sch t fld, ToStar fld,
 Snd (TabOnPath2 sch t fld) ~ 'RelMany) =>
LO -> LimOffWithPath sch t
LimOffWithPath @path LO { limit :: Maybe Natural
limit = Natural -> Maybe Natural
forall a. a -> Maybe a
Just Natural
n, offset :: Maybe Natural
offset = Maybe Natural
forall a. Maybe a
Nothing }

-- | Add `OFFSET` condition for the current table.
-- It is applied only to "root" or "children" tables.
--
-- If 'qOffset' is applied several times on the same path, only the last one is used
qOffset :: forall sch t path. Snd (TabOnPath2 sch t path) ~ RelMany
  => Natural -> MonadQP sch t path
qOffset :: forall sch (t :: NameNSK) (path :: [Symbol]).
(Snd (TabOnPath2 sch t path) ~ 'RelMany) =>
Natural -> MonadQP sch t path
qOffset Natural
n = (QueryParam sch t -> QueryParam sch t)
-> RWST (Proxy path) () (QueryParam sch t) Identity ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify \QueryParam sch t
qp -> QueryParam sch t
qp { qpLOs = mk qp.qpLOs }
  where
    mk :: [LimOffWithPath sch t] -> [LimOffWithPath sch t]
mk [LimOffWithPath sch t]
xs = case (LimOffWithPath sch t -> Bool)
-> [LimOffWithPath sch t]
-> ([LimOffWithPath sch t], [LimOffWithPath sch t])
forall a. (a -> Bool) -> [a] -> ([a], [a])
L.break LimOffWithPath sch t -> Bool
eq [LimOffWithPath sch t]
xs of
      ([LimOffWithPath sch t]
xs1, []) -> LimOffWithPath sch t
new LimOffWithPath sch t
-> [LimOffWithPath sch t] -> [LimOffWithPath sch t]
forall a. a -> [a] -> [a]
: [LimOffWithPath sch t]
xs1
      ([LimOffWithPath sch t]
xs1, LimOffWithPath sch t
x:[LimOffWithPath sch t]
xs2) -> [LimOffWithPath sch t]
xs1 [LimOffWithPath sch t]
-> [LimOffWithPath sch t] -> [LimOffWithPath sch t]
forall a. Semigroup a => a -> a -> a
<> [LimOffWithPath sch t -> LimOffWithPath sch t
upd LimOffWithPath sch t
x] [LimOffWithPath sch t]
-> [LimOffWithPath sch t] -> [LimOffWithPath sch t]
forall a. Semigroup a => a -> a -> a
<> [LimOffWithPath sch t]
xs2
    eq :: LimOffWithPath sch t -> Bool
eq (LimOffWithPath @p LO
_) = forall (a :: [Symbol]).
(SingKind [Symbol], SingI a) =>
Demote [Symbol]
forall {k} (a :: k). (SingKind k, SingI a) => Demote k
demote @p Demote [Symbol] -> Demote [Symbol] -> Bool
forall a. Eq a => a -> a -> Bool
== forall (a :: [Symbol]).
(SingKind [Symbol], SingI a) =>
Demote [Symbol]
forall {k} (a :: k). (SingKind k, SingI a) => Demote k
demote @path
    upd :: LimOffWithPath sch t -> LimOffWithPath sch t
upd (LimOffWithPath @p LO
lo) = forall (fld :: [Symbol]) sch (t :: NameNSK).
(TabPath sch t fld, ToStar fld,
 Snd (TabOnPath2 sch t fld) ~ 'RelMany) =>
LO -> LimOffWithPath sch t
forall {k} (fld :: [Symbol]) (sch :: k) (t :: NameNSK).
(TabPath sch t fld, ToStar fld,
 Snd (TabOnPath2 sch t fld) ~ 'RelMany) =>
LO -> LimOffWithPath sch t
LimOffWithPath @p LO
lo{offset = Just n}
    new :: LimOffWithPath sch t
new = forall (fld :: [Symbol]) sch (t :: NameNSK).
(TabPath sch t fld, ToStar fld,
 Snd (TabOnPath2 sch t fld) ~ 'RelMany) =>
LO -> LimOffWithPath sch t
forall {k} (fld :: [Symbol]) (sch :: k) (t :: NameNSK).
(TabPath sch t fld, ToStar fld,
 Snd (TabOnPath2 sch t fld) ~ 'RelMany) =>
LO -> LimOffWithPath sch t
LimOffWithPath @path LO { offset :: Maybe Natural
offset = Natural -> Maybe Natural
forall a. a -> Maybe a
Just Natural
n, limit :: Maybe Natural
limit = Maybe Natural
forall a. Maybe a
Nothing }

-- | GADT to safely set `where` condition
data CondWithPath sch t where
  CondWithPath ::  forall (path :: [Symbol]) sch t. ToStar path
    => Cond sch (TabOnPath sch t path) -> CondWithPath sch t

-- | GADT to safely set `order by` clauses
data OrdWithPath sch t where
  OrdWithPath :: forall (path :: [Symbol]) sch t. ToStar path
    => [OrdFld sch (TabOnPath sch t path)] -> OrdWithPath sch t

-- | GADT to safely set `distinct/distinct on` clauses
data DistWithPath sch t where
  DistWithPath :: forall (path :: [Symbol]) sch t. ToStar path
    => Dist sch (TabOnPath sch t path) -> DistWithPath sch t

-- | GADT to safely set `limit/offset` clauses
data LimOffWithPath sch t where
  LimOffWithPath :: forall (path :: [Symbol]) sch t.
    (TabPath sch t path, ToStar path, Snd (TabOnPath2 sch t path) ~ 'RelMany)
    => LO -> LimOffWithPath sch t

-- | Comparison constructors; each is paired with its corresponding operator
-- (e.g. '(:=)' with '(=?)').
data Cmp = (:=) | (:<=) | (:>=) | (:>) | (:<) | Like | ILike
  deriving (Int -> Cmp -> ShowS
[Cmp] -> ShowS
Cmp -> String
(Int -> Cmp -> ShowS)
-> (Cmp -> String) -> ([Cmp] -> ShowS) -> Show Cmp
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Cmp -> ShowS
showsPrec :: Int -> Cmp -> ShowS
$cshow :: Cmp -> String
show :: Cmp -> String
$cshowList :: [Cmp] -> ShowS
showList :: [Cmp] -> ShowS
Show, Cmp -> Cmp -> Bool
(Cmp -> Cmp -> Bool) -> (Cmp -> Cmp -> Bool) -> Eq Cmp
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Cmp -> Cmp -> Bool
== :: Cmp -> Cmp -> Bool
$c/= :: Cmp -> Cmp -> Bool
/= :: Cmp -> Cmp -> Bool
Eq, (forall x. Cmp -> Rep Cmp x)
-> (forall x. Rep Cmp x -> Cmp) -> Generic Cmp
forall x. Rep Cmp x -> Cmp
forall x. Cmp -> Rep Cmp x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Cmp -> Rep Cmp x
from :: forall x. Cmp -> Rep Cmp x
$cto :: forall x. Rep Cmp x -> Cmp
to :: forall x. Rep Cmp x -> Cmp
Generic)

-- | Just boolean operations
data BoolOp = And | Or deriving (Int -> BoolOp -> ShowS
[BoolOp] -> ShowS
BoolOp -> String
(Int -> BoolOp -> ShowS)
-> (BoolOp -> String) -> ([BoolOp] -> ShowS) -> Show BoolOp
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BoolOp -> ShowS
showsPrec :: Int -> BoolOp -> ShowS
$cshow :: BoolOp -> String
show :: BoolOp -> String
$cshowList :: [BoolOp] -> ShowS
showList :: [BoolOp] -> ShowS
Show, BoolOp -> BoolOp -> Bool
(BoolOp -> BoolOp -> Bool)
-> (BoolOp -> BoolOp -> Bool) -> Eq BoolOp
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BoolOp -> BoolOp -> Bool
== :: BoolOp -> BoolOp -> Bool
$c/= :: BoolOp -> BoolOp -> Bool
/= :: BoolOp -> BoolOp -> Bool
Eq, (forall x. BoolOp -> Rep BoolOp x)
-> (forall x. Rep BoolOp x -> BoolOp) -> Generic BoolOp
forall x. Rep BoolOp x -> BoolOp
forall x. BoolOp -> Rep BoolOp x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. BoolOp -> Rep BoolOp x
from :: forall x. BoolOp -> Rep BoolOp x
$cto :: forall x. Rep BoolOp x -> BoolOp
to :: forall x. Rep BoolOp x -> BoolOp
Generic)

showCmp :: IsString s => Cmp -> s
showCmp :: forall s. IsString s => Cmp -> s
showCmp = \case
  Cmp
(:=)  -> s
"="
  Cmp
(:<=) -> s
"<="
  Cmp
(:>=) -> s
">="
  Cmp
(:<)  -> s
"<"
  Cmp
(:>)  -> s
">"
  Cmp
Like  -> s
"like"
  Cmp
ILike -> s
"ilike"

{- | RWS-Monad to generate condition.
* Read: Stack of numbers of parent tables. The top is "current table"
* Write: List of placeholder-values.

    Note: SQL is generated top-down so placeholder values appear in the correct order.

* State: Last number of table "in use"
-}
type CondMonad = RWS (Text, NonEmpty Int) [SomeToField] Int

data SomeToField where
  SomeToField :: (ToField a, Show a) => a -> SomeToField

deriving instance Show SomeToField

instance ToField SomeToField where
  toField :: SomeToField -> Action
toField (SomeToField a
v) = a -> Action
forall a. ToField a => a -> Action
toField a
v

type CDBField sch tab fld fd = (CDBFieldInfo sch tab fld
  , TDBFieldInfo sch tab fld ~ 'RFPlain fd )

type CDBValue sch tab fld fd v =
  (CDBField sch tab fld fd, ToField v, Show v, CanConvert sch tab fld fd v)

type CDBFieldNullable sch tab fld fd =
  ( CDBField sch tab fld fd, FdNullable fd ~ 'True)

-- | GADT to safely set `where` condition for table `tab` based on definition of schema `sch`
--
-- 'Cond' is 'Monoid' with conjunction ('(&&&)') as 'mappend'
--
data Cond (sch::Type) (tab::NameNSK) where
  EmptyCond :: Cond sch tab
  -- ^ Empty Condition. Neutral for conjunction '(&&&)' and disjunction '(|||)'.
  Cmp :: forall fld v sch tab fd. CDBValue sch tab fld fd v => Cmp -> v -> Cond sch tab
  -- ^ Comparing field value with parameter
  In :: forall fld v sch tab fd. CDBValue sch tab fld fd v => NonEmpty v -> Cond sch tab
  -- ^ Check that field value belongs to non-empty list of values
  InArr :: forall fld v sch tab fd. CDBValue sch tab fld fd v => [v] -> Cond sch tab
  -- ^ Check that field value belongs to the list of values.
  -- If the list is empty, the condition evaluates to @false@.
  Null :: forall fld sch tab fd. CDBFieldNullable sch tab fld fd => Cond sch tab
  -- ^ Check that field value is @NULL@
  Not :: Cond sch tab -> Cond sch tab
  -- ^ Boolean @NOT@
  BoolOp :: BoolOp -> Cond sch tab -> Cond sch tab -> Cond sch tab
  -- ^ Conjunction and disjunction
  Child :: forall sch ref. CRelDef sch ref =>
    TabParam sch (RdFrom (TRelDef sch ref)) -> Cond sch (RdFrom (TRelDef sch ref))
    -> Cond sch (RdTo (TRelDef sch ref))
  -- ^ condition @EXISTS@ in child table. 'TabParam' is used to limit
  -- child dataset (usually with @ORDER BY@ and @LIMIT@) before applying
  -- condition on child table
  Parent :: forall sch ref . CRelDef sch ref =>
    Cond sch (RdTo (TRelDef sch ref)) -> Cond sch (RdFrom (TRelDef sch ref))
  -- ^ @JOIN@ to parent rows that satisfy the nested condition
  UnsafeCond :: CondMonad Text -> Cond sch tab
  -- ^ Unsafe condition built manually inside 'CondMonad'

-- Conjunction '(&&&)' is much more often operation for query conditions so
-- we use it for 'Semigroup'.
-- But note that 'EmptyCond' is also neutral for disjunction '(|||)'.
instance Semigroup (Cond sch tab) where
  Cond sch tab
c1 <> :: Cond sch tab -> Cond sch tab -> Cond sch tab
<> Cond sch tab
c2 = Cond sch tab
c1 Cond sch tab -> Cond sch tab -> Cond sch tab
forall sch (tab :: NameNSK).
Cond sch tab -> Cond sch tab -> Cond sch tab
&&& Cond sch tab
c2
  -- ^ Using conjunction ('(&&&)') for 'Semigroup' instance

instance Monoid (Cond sch tab) where
  mempty :: Cond sch tab
mempty = Cond sch tab
forall sch (tab :: NameNSK). Cond sch tab
EmptyCond

-- | Parameters for child table.
--
-- It is used to limit child dataset (usually with @ORDER BY@ and @LIMIT@) before applying
-- condition on child table
--
data TabParam sch tab = TabParam
  { forall sch (tab :: NameNSK). TabParam sch tab -> Cond sch tab
cond :: Cond sch tab
  , forall sch (tab :: NameNSK). TabParam sch tab -> [OrdFld sch tab]
order :: [OrdFld sch tab]
  , forall sch (tab :: NameNSK). TabParam sch tab -> LO
lo :: LO }

-- | Default empty 'TabParam'.
defTabParam :: TabParam sch tab
defTabParam :: forall sch (tab :: NameNSK). TabParam sch tab
defTabParam = Cond sch tab -> [OrdFld sch tab] -> LO -> TabParam sch tab
forall sch (tab :: NameNSK).
Cond sch tab -> [OrdFld sch tab] -> LO -> TabParam sch tab
TabParam Cond sch tab
forall a. Monoid a => a
mempty [OrdFld sch tab]
forall a. Monoid a => a
mempty LO
defLO

-- | Check that field value is @NULL@
{-# INLINE pnull #-}
pnull :: forall sch tab fd. forall name -> CDBFieldNullable sch tab name fd => Cond sch tab
pnull :: forall sch (tab :: NameNSK) (fd :: FldDef' Symbol).
forall (name :: Symbol) ->
CDBFieldNullable sch tab name fd => Cond sch tab
pnull name = forall (fld :: Symbol) sch (tab :: NameNSK) (fd :: FldDef' Symbol).
CDBFieldNullable sch tab fld fd =>
Cond sch tab
Null @name

-- | True when related rows exist in the child table and satisfy the nested condition there
--
-- 'TabParam' is used to limit child dataset (usually with @ORDER BY@ and @LIMIT@) before applying
-- condition on child table
--
{-# INLINE pchild #-}
pchild :: forall sch . forall ref -> CRelDef sch ref =>
  TabParam sch (RdFrom (TRelDef sch ref)) -> Cond sch (RdFrom (TRelDef sch ref))
  -> Cond sch (RdTo (TRelDef sch ref))
pchild :: forall sch.
forall (ref :: NameNSK) ->
CRelDef sch ref =>
TabParam sch (RdFrom (TRelDef sch ref))
-> Cond sch (RdFrom (TRelDef sch ref))
-> Cond sch (RdTo (TRelDef sch ref))
pchild name = forall sch.
forall (ref :: NameNSK) ->
CRelDef sch ref =>
TabParam sch (RdFrom (TRelDef sch ref))
-> Cond sch (RdFrom (TRelDef sch ref))
-> Cond sch (RdTo (TRelDef sch ref))
Child @sch @name

-- | Check that condition is satisfied in parent table
{-# INLINE pparent #-}
pparent :: forall sch. forall ref -> CRelDef sch ref =>
  Cond sch (RdTo (TRelDef sch ref)) -> Cond sch (RdFrom (TRelDef sch ref))
pparent :: forall sch.
forall (ref :: NameNSK) ->
CRelDef sch ref =>
Cond sch (RdTo (TRelDef sch ref))
-> Cond sch (RdFrom (TRelDef sch ref))
pparent name = forall sch.
forall (ref :: NameNSK) ->
CRelDef sch ref =>
Cond sch (RdTo (TRelDef sch ref))
-> Cond sch (RdFrom (TRelDef sch ref))
Parent @sch @name

-- | Boolean @NOT@
{-# INLINE pnot #-}
pnot :: Cond sch tab -> Cond sch tab
pnot :: forall sch (tab :: NameNSK). Cond sch tab -> Cond sch tab
pnot = Cond sch tab -> Cond sch tab
forall sch (tab :: NameNSK). Cond sch tab -> Cond sch tab
Not

{-# INLINE pUnsafeCond #-}
pUnsafeCond :: CondMonad Text -> Cond sch tab
pUnsafeCond :: forall sch (tab :: NameNSK). CondMonad Text -> Cond sch tab
pUnsafeCond = CondMonad Text -> Cond sch tab
forall sch (tab :: NameNSK). CondMonad Text -> Cond sch tab
UnsafeCond

-- | Check that field value belongs to non-empty list of values
{-# INLINE pin #-}
pin :: forall name -> forall sch tab fd v. CDBValue sch tab name fd v
  => NonEmpty v -> Cond sch tab
pin :: forall (name :: Symbol) ->
forall sch (tab :: NameNSK) (fd :: FldDef' Symbol) v.
CDBValue sch tab name fd v =>
NonEmpty v -> Cond sch tab
pin name = forall (fld :: Symbol) fd sch (tab :: NameNSK)
       (fd :: FldDef' Symbol).
CDBValue sch tab fld fd fd =>
NonEmpty fd -> Cond sch tab
In @name

-- | Check that field value belongs to the list of values.
-- If the list is empty, the condition evaluates to @false@.
{-# INLINE pinArr #-}
pinArr :: forall name -> forall sch tab fd v. CDBValue sch tab name fd v
  => [v] -> Cond sch tab
pinArr :: forall (name :: Symbol) ->
forall sch (tab :: NameNSK) (fd :: FldDef' Symbol) v.
CDBValue sch tab name fd v =>
[v] -> Cond sch tab
pinArr name = forall (fld :: Symbol) fd sch (tab :: NameNSK)
       (fd :: FldDef' Symbol).
CDBValue sch tab fld fd fd =>
[fd] -> Cond sch tab
InArr @name

-- | Conjunction
(&&&) :: Cond sch tab -> Cond sch tab -> Cond sch tab
-- | Disjunction
(|||) :: Cond sch tab -> Cond sch tab -> Cond sch tab
Cond sch tab
EmptyCond &&& :: forall sch (tab :: NameNSK).
Cond sch tab -> Cond sch tab -> Cond sch tab
&&& Cond sch tab
cond = Cond sch tab
cond
Cond sch tab
cond &&& Cond sch tab
EmptyCond = Cond sch tab
cond
Cond sch tab
c1 &&& Cond sch tab
c2 = BoolOp -> Cond sch tab -> Cond sch tab -> Cond sch tab
forall sch (tab :: NameNSK).
BoolOp -> Cond sch tab -> Cond sch tab -> Cond sch tab
BoolOp BoolOp
And Cond sch tab
c1 Cond sch tab
c2
Cond sch tab
EmptyCond ||| :: forall sch (tab :: NameNSK).
Cond sch tab -> Cond sch tab -> Cond sch tab
||| Cond sch tab
cond = Cond sch tab
cond
Cond sch tab
cond ||| Cond sch tab
EmptyCond = Cond sch tab
cond
Cond sch tab
c1 ||| Cond sch tab
c2 = BoolOp -> Cond sch tab -> Cond sch tab -> Cond sch tab
forall sch (tab :: NameNSK).
BoolOp -> Cond sch tab -> Cond sch tab -> Cond sch tab
BoolOp BoolOp
Or Cond sch tab
c1 Cond sch tab
c2
infixl 2 |||
infixl 3 &&&
--
{-# INLINE (<?) #-}
{-# INLINE (>?) #-}
{-# INLINE (<=?) #-}
{-# INLINE (>=?) #-}
{-# INLINE (=?) #-}
{-# INLINE (~=?) #-}
{-# INLINE (~~?) #-}
(<?),(>?),(<=?),(>=?),(=?)
   :: forall fld -> forall sch tab fd v. CDBValue sch tab fld fd v => v -> Cond sch tab
x <? :: forall (fld :: Symbol) ->
forall sch (tab :: NameNSK) (fd :: FldDef' Symbol) v.
CDBValue sch tab fld fd v =>
v -> Cond sch tab
<? v
b  = forall (fld :: Symbol) fd sch (tab :: NameNSK)
       (fd :: FldDef' Symbol).
CDBValue sch tab fld fd fd =>
Cmp -> fd -> Cond sch tab
Cmp @x Cmp
(:<)  v
b
x >? :: forall (fld :: Symbol) ->
forall sch (tab :: NameNSK) (fd :: FldDef' Symbol) v.
CDBValue sch tab fld fd v =>
v -> Cond sch tab
>? v
b  = forall (fld :: Symbol) fd sch (tab :: NameNSK)
       (fd :: FldDef' Symbol).
CDBValue sch tab fld fd fd =>
Cmp -> fd -> Cond sch tab
Cmp @x Cmp
(:>)  v
b
x <=? :: forall (fld :: Symbol) ->
forall sch (tab :: NameNSK) (fd :: FldDef' Symbol) v.
CDBValue sch tab fld fd v =>
v -> Cond sch tab
<=? v
b = forall (fld :: Symbol) fd sch (tab :: NameNSK)
       (fd :: FldDef' Symbol).
CDBValue sch tab fld fd fd =>
Cmp -> fd -> Cond sch tab
Cmp @x Cmp
(:<=) v
b
x >=? :: forall (fld :: Symbol) ->
forall sch (tab :: NameNSK) (fd :: FldDef' Symbol) v.
CDBValue sch tab fld fd v =>
v -> Cond sch tab
>=? v
b = forall (fld :: Symbol) fd sch (tab :: NameNSK)
       (fd :: FldDef' Symbol).
CDBValue sch tab fld fd fd =>
Cmp -> fd -> Cond sch tab
Cmp @x Cmp
(:>=) v
b
x =? :: forall (fld :: Symbol) ->
forall sch (tab :: NameNSK) (fd :: FldDef' Symbol) v.
CDBValue sch tab fld fd v =>
v -> Cond sch tab
=? v
b = forall (fld :: Symbol) fd sch (tab :: NameNSK)
       (fd :: FldDef' Symbol).
CDBValue sch tab fld fd fd =>
Cmp -> fd -> Cond sch tab
Cmp @x Cmp
(:=) v
b
(~=?),(~~?)
  :: forall fld -> forall sch tab fd v. CDBValue sch tab fld fd v => v -> Cond sch tab
x ~=? :: forall (fld :: Symbol) ->
forall sch (tab :: NameNSK) (fd :: FldDef' Symbol) v.
CDBValue sch tab fld fd v =>
v -> Cond sch tab
~=? v
b  = forall (fld :: Symbol) fd sch (tab :: NameNSK)
       (fd :: FldDef' Symbol).
CDBValue sch tab fld fd fd =>
Cmp -> fd -> Cond sch tab
Cmp @x Cmp
Like v
b
x ~~? :: forall (fld :: Symbol) ->
forall sch (tab :: NameNSK) (fd :: FldDef' Symbol) v.
CDBValue sch tab fld fd v =>
v -> Cond sch tab
~~? v
b  = forall (fld :: Symbol) fd sch (tab :: NameNSK)
       (fd :: FldDef' Symbol).
CDBValue sch tab fld fd fd =>
Cmp -> fd -> Cond sch tab
Cmp @x Cmp
ILike v
b
infix 4 <?, >?, <=?, >=?, =?, ~=?, ~~?

data OrdDirection = Asc | Desc deriving Int -> OrdDirection -> ShowS
[OrdDirection] -> ShowS
OrdDirection -> String
(Int -> OrdDirection -> ShowS)
-> (OrdDirection -> String)
-> ([OrdDirection] -> ShowS)
-> Show OrdDirection
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> OrdDirection -> ShowS
showsPrec :: Int -> OrdDirection -> ShowS
$cshow :: OrdDirection -> String
show :: OrdDirection -> String
$cshowList :: [OrdDirection] -> ShowS
showList :: [OrdDirection] -> ShowS
Show

data OrdFld sch tab where
  OrdFld :: forall fld sch tab fd. CDBField sch tab fld fd =>
    OrdDirection -> OrdFld sch tab
  UnsafeOrd :: CondMonad (Text, OrdDirection) -> OrdFld sch tab

data Dist sch tab where
  Distinct :: Dist sch tab
  -- | Having 'DistinctOn' we automatically add fields from 'DistinctOn'
  -- into the begining of @ORDER BY@.
  -- (It is "good enough" and more simple than check it on type level).
  --
  -- That's why we use 'OrdFld' who include 'OrdDirection'.
  -- Naturally 'OrdDirection' is not used in DISTINCT ON part itself.
  --
  -- Beside that @DISTINCT ON@ part can include expressions like @ORDER BY@.
  -- We can also use 'UnsafeOrd' here
  DistinctOn :: [OrdFld sch tab] -> Dist sch tab

{-# INLINE ordf #-}
ordf
  :: forall fld -> forall sch tab fd. CDBField sch tab fld fd
  => OrdDirection -> OrdFld sch tab
ordf :: forall {k}.
forall (fld :: Symbol) ->
forall (sch :: k) (tab :: NameNSK) (fd :: FldDef' Symbol).
CDBField sch tab fld fd =>
OrdDirection -> OrdFld sch tab
ordf fld = forall {k}.
forall (fld :: Symbol) ->
forall (sch :: k) (tab :: NameNSK) (fd :: FldDef' Symbol).
CDBField sch tab fld fd =>
OrdDirection -> OrdFld sch tab
forall (fld :: Symbol) (sch :: k) (tab :: NameNSK)
       (fd :: FldDef' Symbol).
CDBField sch tab fld fd =>
OrdDirection -> OrdFld sch tab
OrdFld @fld

{-# INLINE ascf #-}
ascf :: forall fld -> forall sch tab fd. CDBField sch tab fld fd => OrdFld sch tab
ascf :: forall {k}.
forall (fld :: Symbol) ->
forall (sch :: k) (tab :: NameNSK) (fd :: FldDef' Symbol).
CDBField sch tab fld fd =>
OrdFld sch tab
ascf fld = OrdDirection -> OrdFld sch tab
forall (fld :: Symbol) ->
forall (sch :: k) (tab :: NameNSK) (fd :: FldDef' Symbol).
CDBField sch tab fld fd =>
OrdDirection -> OrdFld sch tab
forall (sch :: k) (tab :: NameNSK) (fd :: FldDef' Symbol).
CDBField sch tab fld fd =>
OrdDirection -> OrdFld sch tab
forall {k}.
forall (fld :: Symbol) ->
forall (sch :: k) (tab :: NameNSK) (fd :: FldDef' Symbol).
CDBField sch tab fld fd =>
OrdDirection -> OrdFld sch tab
ordf fld OrdDirection
Asc

{-# INLINE descf #-}
descf :: forall fld -> forall sch tab fd. CDBField sch tab fld fd => OrdFld sch tab
descf :: forall {k}.
forall (fld :: Symbol) ->
forall (sch :: k) (tab :: NameNSK) (fd :: FldDef' Symbol).
CDBField sch tab fld fd =>
OrdFld sch tab
descf fld = OrdDirection -> OrdFld sch tab
forall (fld :: Symbol) ->
forall (sch :: k) (tab :: NameNSK) (fd :: FldDef' Symbol).
CDBField sch tab fld fd =>
OrdDirection -> OrdFld sch tab
forall (sch :: k) (tab :: NameNSK) (fd :: FldDef' Symbol).
CDBField sch tab fld fd =>
OrdDirection -> OrdFld sch tab
forall {k}.
forall (fld :: Symbol) ->
forall (sch :: k) (tab :: NameNSK) (fd :: FldDef' Symbol).
CDBField sch tab fld fd =>
OrdDirection -> OrdFld sch tab
ordf fld OrdDirection
Desc

data LO = LO
  { LO -> Maybe Natural
limit  :: Maybe Natural
  , LO -> Maybe Natural
offset :: Maybe Natural }
  deriving Int -> LO -> ShowS
[LO] -> ShowS
LO -> String
(Int -> LO -> ShowS)
-> (LO -> String) -> ([LO] -> ShowS) -> Show LO
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> LO -> ShowS
showsPrec :: Int -> LO -> ShowS
$cshow :: LO -> String
show :: LO -> String
$cshowList :: [LO] -> ShowS
showList :: [LO] -> ShowS
Show

defLO :: LO
defLO :: LO
defLO = Maybe Natural -> Maybe Natural -> LO
LO Maybe Natural
forall a. Maybe a
Nothing Maybe Natural
forall a. Maybe a
Nothing

lo1 :: LO
lo1 :: LO
lo1 = Maybe Natural -> Maybe Natural -> LO
LO (Natural -> Maybe Natural
forall a. a -> Maybe a
Just Natural
1) Maybe Natural
forall a. Maybe a
Nothing