{-# LANGUAGE BangPatterns, TypeFamilies, DataKinds, PolyKinds, TypeApplications, ScopedTypeVariables, ConstraintKinds, TypeOperators, GADTs, UndecidableInstances, StandaloneDeriving, FunctionalDependencies, FlexibleContexts, InstanceSigs, AllowAmbiguousTypes, DeriveAnyClass #-}
{-|
Module: IHP.QueryBuilder
Description:  Tool to build simple sql queries
Copyright: (c) digitally induced GmbH, 2020
QueryBuilder is mainly used for doing simple `SELECT` sql queries. It allows dynamic
creation of sql queries in a type safe way.

For more complex sql queries, use 'IHP.ModelSupport.sqlQuery'.
-}
module IHP.QueryBuilder
( query
, QueryBuilder (..)
, In (In)
, orderBy
, orderByAsc
, orderByDesc
, limit
, offset
, queryUnion
, queryUnionList
, queryOr
, DefaultScope (..)
, filterWhere
, filterWhereCaseInsensitive
, filterWhereNot
, filterWhereIn
, filterWhereIdIn
, filterWhereNotIn
, filterWhereLike
, filterWhereILike
, filterWhereMatches
, filterWhereIMatches
, filterWhereJoinedTable
, filterWhereNotJoinedTable
, filterWhereInJoinedTable
, filterWhereNotInJoinedTable
, filterWhereLikeJoinedTable
, filterWhereILikeJoinedTable
, filterWhereMatchesJoinedTable
, filterWhereIMatchesJoinedTable
, filterWherePast
, filterWhereFuture
, labelResults
, EqOrIsOperator
, filterWhereSql
, FilterPrimaryKey (..)
, distinctOn
, distinct
, toSQL
, toSQL'
, buildQuery
, SQLQuery (..)
, OrderByClause (..)
, innerJoin
, innerJoinThirdTable
, HasQueryBuilder
, JoinQueryBuilderWrapper
, NoJoinQueryBuilderWrapper
, LabeledQueryBuilderWrapper
, getQueryBuilder
, NoJoins
, Condition (..)
, Join (..)
, OrderByDirection (..)
, injectQueryBuilder
, FilterOperator (..)
, toEqOrIsOperator
)
where
import IHP.Prelude
import Database.PostgreSQL.Simple.Types (In (In))
import Database.PostgreSQL.Simple.ToField
import IHP.ModelSupport
import qualified Data.ByteString.Builder as Builder
import IHP.HSX.ToHtml
import qualified Data.ByteString.Char8 as ByteString
import qualified Data.ByteString.Lazy as LByteString
import qualified Control.DeepSeq as DeepSeq
import qualified Data.Text.Encoding as Text
import qualified GHC.Generics

class DefaultScope table where
    defaultScope :: QueryBuilder table -> QueryBuilder table

instance {-# OVERLAPPABLE #-} DefaultScope table where
    {-# INLINE defaultScope #-}
    defaultScope :: QueryBuilder table -> QueryBuilder table
defaultScope QueryBuilder table
queryBuilder = QueryBuilder table
queryBuilder

instance Table (GetModelByTableName table) => Default (QueryBuilder table) where
    {-# INLINE def #-}
    def :: QueryBuilder table
def = NewQueryBuilder { selectFrom :: ByteString
selectFrom = forall record. Table record => ByteString
tableNameByteString @(GetModelByTableName table), columns :: [ByteString]
columns = forall record. Table record => [ByteString]
columnNames @(GetModelByTableName table) }

data MatchSensitivity = CaseSensitive | CaseInsensitive deriving (Int -> MatchSensitivity -> ShowS
[MatchSensitivity] -> ShowS
MatchSensitivity -> String
(Int -> MatchSensitivity -> ShowS)
-> (MatchSensitivity -> String)
-> ([MatchSensitivity] -> ShowS)
-> Show MatchSensitivity
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MatchSensitivity -> ShowS
showsPrec :: Int -> MatchSensitivity -> ShowS
$cshow :: MatchSensitivity -> String
show :: MatchSensitivity -> String
$cshowList :: [MatchSensitivity] -> ShowS
showList :: [MatchSensitivity] -> ShowS
Show, MatchSensitivity -> MatchSensitivity -> Bool
(MatchSensitivity -> MatchSensitivity -> Bool)
-> (MatchSensitivity -> MatchSensitivity -> Bool)
-> Eq MatchSensitivity
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: MatchSensitivity -> MatchSensitivity -> Bool
== :: MatchSensitivity -> MatchSensitivity -> Bool
$c/= :: MatchSensitivity -> MatchSensitivity -> Bool
/= :: MatchSensitivity -> MatchSensitivity -> Bool
Eq)

data FilterOperator
    = EqOp -- ^ @col = val@
    | NotEqOp -- ^ @col != val@
    | InOp -- ^ @col IN (set)@
    | NotInOp -- ^ @col NOT IN (set)@
    | IsOp -- ^ @col IS val@
    | IsNotOp -- ^ @col IS NOT val@
    | LikeOp !MatchSensitivity -- ^ @col LIKE val@
    | NotLikeOp !MatchSensitivity -- ^ @col NOT LIKE val@
    | MatchesOp !MatchSensitivity -- ^ @col ~ pattern@
    | SqlOp -- ^ Used by 'filterWhereSql'
    deriving (Int -> FilterOperator -> ShowS
[FilterOperator] -> ShowS
FilterOperator -> String
(Int -> FilterOperator -> ShowS)
-> (FilterOperator -> String)
-> ([FilterOperator] -> ShowS)
-> Show FilterOperator
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> FilterOperator -> ShowS
showsPrec :: Int -> FilterOperator -> ShowS
$cshow :: FilterOperator -> String
show :: FilterOperator -> String
$cshowList :: [FilterOperator] -> ShowS
showList :: [FilterOperator] -> ShowS
Show, FilterOperator -> FilterOperator -> Bool
(FilterOperator -> FilterOperator -> Bool)
-> (FilterOperator -> FilterOperator -> Bool) -> Eq FilterOperator
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: FilterOperator -> FilterOperator -> Bool
== :: FilterOperator -> FilterOperator -> Bool
$c/= :: FilterOperator -> FilterOperator -> Bool
/= :: FilterOperator -> FilterOperator -> Bool
Eq)


compileOperator :: FilterOperator -> ByteString
compileOperator :: FilterOperator -> ByteString
compileOperator FilterOperator
EqOp = ByteString
"="
compileOperator FilterOperator
NotEqOp = ByteString
"!="
compileOperator FilterOperator
InOp = ByteString
"IN"
compileOperator FilterOperator
NotInOp = ByteString
"NOT IN"
compileOperator FilterOperator
IsOp = ByteString
"IS"
compileOperator FilterOperator
IsNotOp = ByteString
"IS NOT"
compileOperator (LikeOp MatchSensitivity
CaseSensitive) = ByteString
"LIKE"
compileOperator (LikeOp MatchSensitivity
CaseInsensitive) = ByteString
"ILIKE"
compileOperator (NotLikeOp MatchSensitivity
CaseSensitive) = ByteString
"NOT LIKE"
compileOperator (NotLikeOp MatchSensitivity
CaseInsensitive) = ByteString
"NOT ILIKE"
compileOperator (MatchesOp MatchSensitivity
CaseSensitive) = ByteString
" ~ "
compileOperator (MatchesOp MatchSensitivity
CaseInsensitive) = ByteString
" ~* "
compileOperator FilterOperator
SqlOp = ByteString
""
{-# INLINE compileOperator #-}

-- | Returns the "NOT" version of an operator
--
-- >>> negateFilterOperator EqOp
-- NotEqOp
--
negateFilterOperator :: FilterOperator -> FilterOperator
negateFilterOperator :: FilterOperator -> FilterOperator
negateFilterOperator FilterOperator
EqOp = FilterOperator
NotEqOp
negateFilterOperator FilterOperator
InOp = FilterOperator
NotInOp
negateFilterOperator FilterOperator
IsOp = FilterOperator
IsNotOp
negateFilterOperator (LikeOp MatchSensitivity
matchSensitivity) = (MatchSensitivity -> FilterOperator
NotLikeOp MatchSensitivity
matchSensitivity)
negateFilterOperator (MatchesOp MatchSensitivity
matchSensitivity) = Text -> FilterOperator
forall a. Text -> a
error Text
"not supported"
negateFilterOperator FilterOperator
SqlOp = FilterOperator
SqlOp

data OrderByClause =
    OrderByClause
    { OrderByClause -> ByteString
orderByColumn :: !ByteString
    , OrderByClause -> OrderByDirection
orderByDirection :: !OrderByDirection }
    deriving (Int -> OrderByClause -> ShowS
[OrderByClause] -> ShowS
OrderByClause -> String
(Int -> OrderByClause -> ShowS)
-> (OrderByClause -> String)
-> ([OrderByClause] -> ShowS)
-> Show OrderByClause
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> OrderByClause -> ShowS
showsPrec :: Int -> OrderByClause -> ShowS
$cshow :: OrderByClause -> String
show :: OrderByClause -> String
$cshowList :: [OrderByClause] -> ShowS
showList :: [OrderByClause] -> ShowS
Show, OrderByClause -> OrderByClause -> Bool
(OrderByClause -> OrderByClause -> Bool)
-> (OrderByClause -> OrderByClause -> Bool) -> Eq OrderByClause
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: OrderByClause -> OrderByClause -> Bool
== :: OrderByClause -> OrderByClause -> Bool
$c/= :: OrderByClause -> OrderByClause -> Bool
/= :: OrderByClause -> OrderByClause -> Bool
Eq, (forall x. OrderByClause -> Rep OrderByClause x)
-> (forall x. Rep OrderByClause x -> OrderByClause)
-> Generic OrderByClause
forall x. Rep OrderByClause x -> OrderByClause
forall x. OrderByClause -> Rep OrderByClause x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. OrderByClause -> Rep OrderByClause x
from :: forall x. OrderByClause -> Rep OrderByClause x
$cto :: forall x. Rep OrderByClause x -> OrderByClause
to :: forall x. Rep OrderByClause x -> OrderByClause
GHC.Generics.Generic, OrderByClause -> ()
(OrderByClause -> ()) -> NFData OrderByClause
forall a. (a -> ()) -> NFData a
$crnf :: OrderByClause -> ()
rnf :: OrderByClause -> ()
DeepSeq.NFData)

-- Types implementing a type level list to record joined tables. EmptyModelList and ConsModelList correspond to the data constructors [] and :. NoJoins is like the empty List but cannot be extended.
data NoJoins
data EmptyModelList
data ConsModelList model models

-- Type class to represent the true list type EmptyModelList ConsModelList.
class ModelList a

instance ModelList EmptyModelList
instance ModelList (ConsModelList model models)

-- Typeclass to quer containment in the type-level list.
class IsJoined a b

instance (ModelList b) => IsJoined a (ConsModelList a b)
instance {-# OVERLAPPABLE #-} (ModelList b, IsJoined a b) => IsJoined a (ConsModelList c b)

-- Class to generalise over different QueryBuilder-providing types. The actual query builder can be extracted with 'getQueryBuilder' and injected with 'injectQueryBuilder'. Also assigns a join reqister to a queryBilderProvider. 
class HasQueryBuilder queryBuilderProvider joinRegister | queryBuilderProvider -> joinRegister where
    getQueryBuilder :: queryBuilderProvider table -> QueryBuilder table
    injectQueryBuilder :: QueryBuilder table -> queryBuilderProvider table
    getQueryIndex :: queryBuilderProvider table -> Maybe ByteString
    getQueryIndex queryBuilderProvider table
_ = Maybe ByteString
forall a. Maybe a
Nothing
    {-# INLINABLE getQueryIndex #-}

-- Wrapper for QueryBuilders resulting from joins. Associates a joinRegister type.
newtype JoinQueryBuilderWrapper joinRegister table = JoinQueryBuilderWrapper (QueryBuilder table)

-- Wrapper for QueryBuilder that must not joins, e.g. queryUnion.
newtype NoJoinQueryBuilderWrapper table = NoJoinQueryBuilderWrapper (QueryBuilder table)

-- Wrapper for QueryBuilders with indexed results.
newtype LabeledQueryBuilderWrapper foreignTable indexColumn indexValue table = LabeledQueryBuilderWrapper (QueryBuilder table)

-- QueryBuilders have query builders and the join register is empty.
instance HasQueryBuilder QueryBuilder EmptyModelList where
    getQueryBuilder :: forall (table :: Symbol). QueryBuilder table -> QueryBuilder table
getQueryBuilder = QueryBuilder table -> QueryBuilder table
forall a. a -> a
forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id
    {-# INLINE getQueryBuilder #-}
    injectQueryBuilder :: forall (table :: Symbol). QueryBuilder table -> QueryBuilder table
injectQueryBuilder = QueryBuilder table -> QueryBuilder table
forall a. a -> a
forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id
    {-# INLINE injectQueryBuilder #-}

-- JoinQueryBuilderWrappers have query builders
instance HasQueryBuilder (JoinQueryBuilderWrapper joinRegister) joinRegister where
    getQueryBuilder :: forall (table :: Symbol).
JoinQueryBuilderWrapper joinRegister table -> QueryBuilder table
getQueryBuilder (JoinQueryBuilderWrapper QueryBuilder table
queryBuilder) = QueryBuilder table
queryBuilder
    {-# INLINABLE getQueryBuilder #-}
    injectQueryBuilder :: forall (table :: Symbol).
QueryBuilder table -> JoinQueryBuilderWrapper joinRegister table
injectQueryBuilder = QueryBuilder table -> JoinQueryBuilderWrapper joinRegister table
forall k (joinRegister :: k) (table :: Symbol).
QueryBuilder table -> JoinQueryBuilderWrapper joinRegister table
JoinQueryBuilderWrapper 
    {-# INLINABLE injectQueryBuilder #-}

-- NoJoinQueryBuilderWrapper have query builders and the join register does not allow any joins
instance HasQueryBuilder NoJoinQueryBuilderWrapper NoJoins where
    getQueryBuilder :: forall (table :: Symbol).
NoJoinQueryBuilderWrapper table -> QueryBuilder table
getQueryBuilder (NoJoinQueryBuilderWrapper QueryBuilder table
queryBuilder) = QueryBuilder table
queryBuilder
    {-# INLINABLE getQueryBuilder #-}
    injectQueryBuilder :: forall (table :: Symbol).
QueryBuilder table -> NoJoinQueryBuilderWrapper table
injectQueryBuilder  = QueryBuilder table -> NoJoinQueryBuilderWrapper table
forall (table :: Symbol).
QueryBuilder table -> NoJoinQueryBuilderWrapper table
NoJoinQueryBuilderWrapper 
    {-# INLINABLE injectQueryBuilder #-}

instance (KnownSymbol foreignTable, foreignModel ~ GetModelByTableName foreignTable , KnownSymbol indexColumn, HasField indexColumn foreignModel indexValue) => HasQueryBuilder (LabeledQueryBuilderWrapper foreignTable indexColumn indexValue) NoJoins where
    getQueryBuilder :: forall (table :: Symbol).
LabeledQueryBuilderWrapper
  foreignTable indexColumn indexValue table
-> QueryBuilder table
getQueryBuilder (LabeledQueryBuilderWrapper QueryBuilder table
queryBuilder) = QueryBuilder table
queryBuilder
    {-# INLINABLE getQueryBuilder #-}
    injectQueryBuilder :: forall (table :: Symbol).
QueryBuilder table
-> LabeledQueryBuilderWrapper
     foreignTable indexColumn indexValue table
injectQueryBuilder = QueryBuilder table
-> LabeledQueryBuilderWrapper
     foreignTable indexColumn indexValue table
forall {k} {k} {k} (foreignTable :: k) (indexColumn :: k)
       (indexValue :: k) (table :: Symbol).
QueryBuilder table
-> LabeledQueryBuilderWrapper
     foreignTable indexColumn indexValue table
LabeledQueryBuilderWrapper
    {-# INLINABLE injectQueryBuilder #-}
    getQueryIndex :: forall (table :: Symbol).
LabeledQueryBuilderWrapper
  foreignTable indexColumn indexValue table
-> Maybe ByteString
getQueryIndex LabeledQueryBuilderWrapper
  foreignTable indexColumn indexValue table
_ = ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just (ByteString -> Maybe ByteString) -> ByteString -> Maybe ByteString
forall a b. (a -> b) -> a -> b
$ forall (symbol :: Symbol). KnownSymbol symbol => ByteString
symbolToByteString @foreignTable ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"." ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> (Text -> ByteString
Text.encodeUtf8 (Text -> ByteString) -> (Text -> Text) -> Text -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Text -> Text
fieldNameToColumnName) (forall (symbol :: Symbol). KnownSymbol symbol => Text
symbolToText @indexColumn)
    {-# INLINABLE getQueryIndex #-}


data QueryBuilder (table :: Symbol) =
    NewQueryBuilder { forall (table :: Symbol). QueryBuilder table -> ByteString
selectFrom :: !ByteString, forall (table :: Symbol). QueryBuilder table -> [ByteString]
columns :: ![ByteString] }
    | DistinctQueryBuilder   { forall (table :: Symbol). QueryBuilder table -> QueryBuilder table
queryBuilder :: !(QueryBuilder table) }
    | DistinctOnQueryBuilder { queryBuilder :: !(QueryBuilder table), forall (table :: Symbol). QueryBuilder table -> ByteString
distinctOnColumn :: !ByteString }
    | FilterByQueryBuilder   { queryBuilder :: !(QueryBuilder table), forall (table :: Symbol).
QueryBuilder table -> (ByteString, FilterOperator, Action)
queryFilter :: !(ByteString, FilterOperator, Action), forall (table :: Symbol). QueryBuilder table -> Maybe ByteString
applyLeft :: !(Maybe ByteString), forall (table :: Symbol). QueryBuilder table -> Maybe ByteString
applyRight :: !(Maybe ByteString) }
    | OrderByQueryBuilder    { queryBuilder :: !(QueryBuilder table), forall (table :: Symbol). QueryBuilder table -> OrderByClause
queryOrderByClause :: !OrderByClause }
    | LimitQueryBuilder      { queryBuilder :: !(QueryBuilder table), forall (table :: Symbol). QueryBuilder table -> Int
queryLimit :: !Int }
    | OffsetQueryBuilder     { queryBuilder :: !(QueryBuilder table), forall (table :: Symbol). QueryBuilder table -> Int
queryOffset :: !Int }
    | UnionQueryBuilder      { forall (table :: Symbol). QueryBuilder table -> QueryBuilder table
firstQueryBuilder :: !(QueryBuilder table), forall (table :: Symbol). QueryBuilder table -> QueryBuilder table
secondQueryBuilder :: !(QueryBuilder table) }
    | JoinQueryBuilder       { queryBuilder :: !(QueryBuilder table), forall (table :: Symbol). QueryBuilder table -> Join
joinData :: Join}
    deriving (Int -> QueryBuilder table -> ShowS
[QueryBuilder table] -> ShowS
QueryBuilder table -> String
(Int -> QueryBuilder table -> ShowS)
-> (QueryBuilder table -> String)
-> ([QueryBuilder table] -> ShowS)
-> Show (QueryBuilder table)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall (table :: Symbol). Int -> QueryBuilder table -> ShowS
forall (table :: Symbol). [QueryBuilder table] -> ShowS
forall (table :: Symbol). QueryBuilder table -> String
$cshowsPrec :: forall (table :: Symbol). Int -> QueryBuilder table -> ShowS
showsPrec :: Int -> QueryBuilder table -> ShowS
$cshow :: forall (table :: Symbol). QueryBuilder table -> String
show :: QueryBuilder table -> String
$cshowList :: forall (table :: Symbol). [QueryBuilder table] -> ShowS
showList :: [QueryBuilder table] -> ShowS
Show, QueryBuilder table -> QueryBuilder table -> Bool
(QueryBuilder table -> QueryBuilder table -> Bool)
-> (QueryBuilder table -> QueryBuilder table -> Bool)
-> Eq (QueryBuilder table)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall (table :: Symbol).
QueryBuilder table -> QueryBuilder table -> Bool
$c== :: forall (table :: Symbol).
QueryBuilder table -> QueryBuilder table -> Bool
== :: QueryBuilder table -> QueryBuilder table -> Bool
$c/= :: forall (table :: Symbol).
QueryBuilder table -> QueryBuilder table -> Bool
/= :: QueryBuilder table -> QueryBuilder table -> Bool
Eq)

data Condition = VarCondition !ByteString !Action | OrCondition !Condition !Condition | AndCondition !Condition !Condition deriving (Int -> Condition -> ShowS
[Condition] -> ShowS
Condition -> String
(Int -> Condition -> ShowS)
-> (Condition -> String)
-> ([Condition] -> ShowS)
-> Show Condition
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Condition -> ShowS
showsPrec :: Int -> Condition -> ShowS
$cshow :: Condition -> String
show :: Condition -> String
$cshowList :: [Condition] -> ShowS
showList :: [Condition] -> ShowS
Show, Condition -> Condition -> Bool
(Condition -> Condition -> Bool)
-> (Condition -> Condition -> Bool) -> Eq Condition
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Condition -> Condition -> Bool
== :: Condition -> Condition -> Bool
$c/= :: Condition -> Condition -> Bool
/= :: Condition -> Condition -> Bool
Eq)

-- | Display QueryBuilder's as their sql query inside HSX
instance KnownSymbol table => ToHtml (QueryBuilder table) where
    toHtml :: QueryBuilder table -> Html
toHtml QueryBuilder table
queryBuilder = (ByteString, [Action]) -> Html
forall a. ToHtml a => a -> Html
toHtml (QueryBuilder table -> (ByteString, [Action])
forall {k} (table :: Symbol) (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k).
(KnownSymbol table,
 HasQueryBuilder queryBuilderProvider joinRegister) =>
queryBuilderProvider table -> (ByteString, [Action])
toSQL QueryBuilder table
queryBuilder)

data Join = Join { Join -> ByteString
table :: ByteString, Join -> ByteString
tableJoinColumn :: ByteString, Join -> ByteString
otherJoinColumn :: ByteString }
    deriving (Int -> Join -> ShowS
[Join] -> ShowS
Join -> String
(Int -> Join -> ShowS)
-> (Join -> String) -> ([Join] -> ShowS) -> Show Join
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Join -> ShowS
showsPrec :: Int -> Join -> ShowS
$cshow :: Join -> String
show :: Join -> String
$cshowList :: [Join] -> ShowS
showList :: [Join] -> ShowS
Show, Join -> Join -> Bool
(Join -> Join -> Bool) -> (Join -> Join -> Bool) -> Eq Join
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Join -> Join -> Bool
== :: Join -> Join -> Bool
$c/= :: Join -> Join -> Bool
/= :: Join -> Join -> Bool
Eq)

data OrderByDirection = Asc | Desc deriving (OrderByDirection -> OrderByDirection -> Bool
(OrderByDirection -> OrderByDirection -> Bool)
-> (OrderByDirection -> OrderByDirection -> Bool)
-> Eq OrderByDirection
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: OrderByDirection -> OrderByDirection -> Bool
== :: OrderByDirection -> OrderByDirection -> Bool
$c/= :: OrderByDirection -> OrderByDirection -> Bool
/= :: OrderByDirection -> OrderByDirection -> Bool
Eq, Int -> OrderByDirection -> ShowS
[OrderByDirection] -> ShowS
OrderByDirection -> String
(Int -> OrderByDirection -> ShowS)
-> (OrderByDirection -> String)
-> ([OrderByDirection] -> ShowS)
-> Show OrderByDirection
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> OrderByDirection -> ShowS
showsPrec :: Int -> OrderByDirection -> ShowS
$cshow :: OrderByDirection -> String
show :: OrderByDirection -> String
$cshowList :: [OrderByDirection] -> ShowS
showList :: [OrderByDirection] -> ShowS
Show, (forall x. OrderByDirection -> Rep OrderByDirection x)
-> (forall x. Rep OrderByDirection x -> OrderByDirection)
-> Generic OrderByDirection
forall x. Rep OrderByDirection x -> OrderByDirection
forall x. OrderByDirection -> Rep OrderByDirection x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. OrderByDirection -> Rep OrderByDirection x
from :: forall x. OrderByDirection -> Rep OrderByDirection x
$cto :: forall x. Rep OrderByDirection x -> OrderByDirection
to :: forall x. Rep OrderByDirection x -> OrderByDirection
GHC.Generics.Generic, OrderByDirection -> ()
(OrderByDirection -> ()) -> NFData OrderByDirection
forall a. (a -> ()) -> NFData a
$crnf :: OrderByDirection -> ()
rnf :: OrderByDirection -> ()
DeepSeq.NFData)
data SQLQuery = SQLQuery
    { SQLQuery -> Maybe ByteString
queryIndex :: !(Maybe ByteString)
    , SQLQuery -> ByteString
selectFrom :: !ByteString
    , SQLQuery -> Maybe ByteString
distinctClause :: !(Maybe ByteString)
    , SQLQuery -> Maybe ByteString
distinctOnClause :: !(Maybe ByteString)
    , SQLQuery -> Maybe Condition
whereCondition :: !(Maybe Condition)
    , SQLQuery -> [Join]
joins :: ![Join]
    , SQLQuery -> [OrderByClause]
orderByClause :: ![OrderByClause]
    , SQLQuery -> Maybe ByteString
limitClause :: !(Maybe ByteString)
    , SQLQuery -> Maybe ByteString
offsetClause :: !(Maybe ByteString)
    , SQLQuery -> [ByteString]
columns :: ![ByteString]
    } deriving (Int -> SQLQuery -> ShowS
[SQLQuery] -> ShowS
SQLQuery -> String
(Int -> SQLQuery -> ShowS)
-> (SQLQuery -> String) -> ([SQLQuery] -> ShowS) -> Show SQLQuery
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SQLQuery -> ShowS
showsPrec :: Int -> SQLQuery -> ShowS
$cshow :: SQLQuery -> String
show :: SQLQuery -> String
$cshowList :: [SQLQuery] -> ShowS
showList :: [SQLQuery] -> ShowS
Show, SQLQuery -> SQLQuery -> Bool
(SQLQuery -> SQLQuery -> Bool)
-> (SQLQuery -> SQLQuery -> Bool) -> Eq SQLQuery
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SQLQuery -> SQLQuery -> Bool
== :: SQLQuery -> SQLQuery -> Bool
$c/= :: SQLQuery -> SQLQuery -> Bool
/= :: SQLQuery -> SQLQuery -> Bool
Eq)

-- | Needed for the 'Eq QueryBuilder' instance
deriving instance Eq Action

-- | Need for the 'Eq QueryBuilder' instance
--
-- You likely wonder: Why do we need the 'Eq SQLQuery' instance if this causes so much trouble?
-- This has to do with how has-many and belongs-to relations are models by the SchemaCompiler
--
-- E.g. given a table users and a table posts. Each Post belongs to a user. The schema compiler will
-- add a field 'posts :: QueryBuilder "posts"' with the default value @query |> filterWhere (#userId, self.id)@ to all users by default.
--
-- This is needed to support syntax like this:
--
-- > fetch user.posts
--
instance Eq Builder.Builder where
    Builder
a == :: Builder -> Builder -> Bool
== Builder
b = (Builder -> LazyByteString
Builder.toLazyByteString Builder
a) LazyByteString -> LazyByteString -> Bool
forall a. Eq a => a -> a -> Bool
== (Builder -> LazyByteString
Builder.toLazyByteString Builder
b)

instance SetField "queryIndex" SQLQuery (Maybe ByteString) where setField :: Maybe ByteString -> SQLQuery -> SQLQuery
setField Maybe ByteString
value SQLQuery
sqlQuery = SQLQuery
sqlQuery { queryIndex = value }
instance SetField "selectFrom" SQLQuery ByteString where setField :: ByteString -> SQLQuery -> SQLQuery
setField ByteString
value SQLQuery
sqlQuery = SQLQuery
sqlQuery { selectFrom = value }
instance SetField "distinctClause" SQLQuery (Maybe ByteString) where setField :: Maybe ByteString -> SQLQuery -> SQLQuery
setField Maybe ByteString
value SQLQuery
sqlQuery = SQLQuery
sqlQuery { distinctClause = value }
instance SetField "distinctOnClause" SQLQuery (Maybe ByteString) where setField :: Maybe ByteString -> SQLQuery -> SQLQuery
setField Maybe ByteString
value SQLQuery
sqlQuery = SQLQuery
sqlQuery { distinctOnClause = value }
instance SetField "whereCondition" SQLQuery (Maybe Condition) where setField :: Maybe Condition -> SQLQuery -> SQLQuery
setField Maybe Condition
value SQLQuery
sqlQuery = SQLQuery
sqlQuery { whereCondition = value }
instance SetField "orderByClause" SQLQuery [OrderByClause] where setField :: [OrderByClause] -> SQLQuery -> SQLQuery
setField [OrderByClause]
value SQLQuery
sqlQuery = SQLQuery
sqlQuery { orderByClause = value }
instance SetField "limitClause" SQLQuery (Maybe ByteString) where setField :: Maybe ByteString -> SQLQuery -> SQLQuery
setField Maybe ByteString
value SQLQuery
sqlQuery = SQLQuery
sqlQuery { limitClause = value }
instance SetField "offsetClause" SQLQuery (Maybe ByteString) where setField :: Maybe ByteString -> SQLQuery -> SQLQuery
setField Maybe ByteString
value SQLQuery
sqlQuery = SQLQuery
sqlQuery { offsetClause = value }



-- | Represent's a @SELECT * FROM ..@ query. It's the starting point to build a query.
-- Used together with the other functions to compose a sql query.
--
-- Example:
--
-- > toSQL (query @User)
-- > -- Returns: ("SELECT id, firstname, lastname FROM users", [])
--
-- Example: Fetching all users
--
-- > allUsers <- query @User |> fetch
-- > -- Runs a 'SELECT * FROM users' query
--
-- You can use it together with 'filterWhere':
--
-- > activeUsers :: [User] <-
-- >    query @User
-- >     |> filterWhere (#active, True)
-- >     |> fetch
query :: forall model table. (table ~ GetTableName model, Table model) => DefaultScope table => QueryBuilder table
query :: forall model (table :: Symbol).
(table ~ GetTableName model, Table model, DefaultScope table) =>
QueryBuilder table
query = (forall (table :: Symbol).
DefaultScope table =>
QueryBuilder table -> QueryBuilder table
defaultScope @table) NewQueryBuilder { selectFrom :: ByteString
selectFrom = forall record. Table record => ByteString
tableNameByteString @model, columns :: [ByteString]
columns = forall record. Table record => [ByteString]
columnNames @model }
{-# INLINE query #-}

{-# INLINE buildQuery #-}
buildQuery :: forall table queryBuilderProvider joinRegister. (KnownSymbol table, HasQueryBuilder queryBuilderProvider joinRegister) => queryBuilderProvider table -> SQLQuery
buildQuery :: forall {k} (table :: Symbol) (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k).
(KnownSymbol table,
 HasQueryBuilder queryBuilderProvider joinRegister) =>
queryBuilderProvider table -> SQLQuery
buildQuery queryBuilderProvider table
queryBuilderProvider = QueryBuilder table -> SQLQuery
buildQueryHelper (QueryBuilder table -> SQLQuery) -> QueryBuilder table -> SQLQuery
forall a b. (a -> b) -> a -> b
$ queryBuilderProvider table -> QueryBuilder table
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
queryBuilderProvider table -> QueryBuilder table
forall (table :: Symbol).
queryBuilderProvider table -> QueryBuilder table
getQueryBuilder queryBuilderProvider table
queryBuilderProvider
    where
    buildQueryHelper :: QueryBuilder table -> SQLQuery
buildQueryHelper NewQueryBuilder { ByteString
selectFrom :: forall (table :: Symbol). QueryBuilder table -> ByteString
selectFrom :: ByteString
selectFrom, [ByteString]
columns :: forall (table :: Symbol). QueryBuilder table -> [ByteString]
columns :: [ByteString]
columns } =
        SQLQuery
            {     queryIndex :: Maybe ByteString
queryIndex = queryBuilderProvider table -> Maybe ByteString
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
queryBuilderProvider table -> Maybe ByteString
forall (table :: Symbol).
queryBuilderProvider table -> Maybe ByteString
getQueryIndex queryBuilderProvider table
queryBuilderProvider 
                , selectFrom :: ByteString
selectFrom = ByteString
selectFrom
                , distinctClause :: Maybe ByteString
distinctClause = Maybe ByteString
forall a. Maybe a
Nothing
                , distinctOnClause :: Maybe ByteString
distinctOnClause = Maybe ByteString
forall a. Maybe a
Nothing
                , whereCondition :: Maybe Condition
whereCondition = Maybe Condition
forall a. Maybe a
Nothing
                , joins :: [Join]
joins = []
                , orderByClause :: [OrderByClause]
orderByClause = []
                , limitClause :: Maybe ByteString
limitClause = Maybe ByteString
forall a. Maybe a
Nothing
                , offsetClause :: Maybe ByteString
offsetClause = Maybe ByteString
forall a. Maybe a
Nothing
                , [ByteString]
columns :: [ByteString]
columns :: [ByteString]
columns
                }
    buildQueryHelper DistinctQueryBuilder { QueryBuilder table
queryBuilder :: forall (table :: Symbol). QueryBuilder table -> QueryBuilder table
queryBuilder :: QueryBuilder table
queryBuilder } = QueryBuilder table
queryBuilder
            QueryBuilder table -> (QueryBuilder table -> SQLQuery) -> SQLQuery
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> QueryBuilder table -> SQLQuery
buildQueryHelper
            SQLQuery -> (SQLQuery -> SQLQuery) -> SQLQuery
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> Proxy "distinctClause" -> ByteString -> SQLQuery -> SQLQuery
forall model (name :: Symbol) value.
(KnownSymbol name, SetField name model (Maybe value)) =>
Proxy name -> value -> model -> model
setJust Proxy "distinctClause"
#distinctClause ByteString
"DISTINCT"
    buildQueryHelper DistinctOnQueryBuilder { QueryBuilder table
queryBuilder :: forall (table :: Symbol). QueryBuilder table -> QueryBuilder table
queryBuilder :: QueryBuilder table
queryBuilder, ByteString
distinctOnColumn :: forall (table :: Symbol). QueryBuilder table -> ByteString
distinctOnColumn :: ByteString
distinctOnColumn } = QueryBuilder table
queryBuilder
            QueryBuilder table -> (QueryBuilder table -> SQLQuery) -> SQLQuery
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> QueryBuilder table -> SQLQuery
buildQueryHelper
            SQLQuery -> (SQLQuery -> SQLQuery) -> SQLQuery
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> Proxy "distinctOnClause" -> ByteString -> SQLQuery -> SQLQuery
forall model (name :: Symbol) value.
(KnownSymbol name, SetField name model (Maybe value)) =>
Proxy name -> value -> model -> model
setJust Proxy "distinctOnClause"
#distinctOnClause (ByteString
"DISTINCT ON (" ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
distinctOnColumn ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
")")
    buildQueryHelper FilterByQueryBuilder { QueryBuilder table
queryBuilder :: forall (table :: Symbol). QueryBuilder table -> QueryBuilder table
queryBuilder :: QueryBuilder table
queryBuilder, queryFilter :: forall (table :: Symbol).
QueryBuilder table -> (ByteString, FilterOperator, Action)
queryFilter = (ByteString
columnName, FilterOperator
operator, Action
value), Maybe ByteString
applyLeft :: forall (table :: Symbol). QueryBuilder table -> Maybe ByteString
applyLeft :: Maybe ByteString
applyLeft, Maybe ByteString
applyRight :: forall (table :: Symbol). QueryBuilder table -> Maybe ByteString
applyRight :: Maybe ByteString
applyRight } =
                let
                    applyFn :: Maybe a -> a -> a
applyFn Maybe a
fn a
value = case Maybe a
fn of
                            Just a
fn -> a
fn a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
"(" a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
value a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
")"
                            Maybe a
Nothing -> a
value

                    condition :: Condition
condition = ByteString -> Action -> Condition
VarCondition (Maybe ByteString -> ByteString -> ByteString
forall {a}. (Semigroup a, IsString a) => Maybe a -> a -> a
applyFn Maybe ByteString
applyLeft ByteString
columnName ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
" " ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> FilterOperator -> ByteString
compileOperator FilterOperator
operator ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
" " ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Maybe ByteString -> ByteString -> ByteString
forall {a}. (Semigroup a, IsString a) => Maybe a -> a -> a
applyFn Maybe ByteString
applyRight ByteString
"?") Action
value
                in
                    QueryBuilder table
queryBuilder
                        QueryBuilder table -> (QueryBuilder table -> SQLQuery) -> SQLQuery
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> QueryBuilder table -> SQLQuery
buildQueryHelper
                        SQLQuery -> (SQLQuery -> SQLQuery) -> SQLQuery
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> Proxy "whereCondition"
-> (Maybe Condition -> Maybe Condition) -> SQLQuery -> SQLQuery
forall model (name :: Symbol) value.
(KnownSymbol name, HasField name model value,
 SetField name model value) =>
Proxy name -> (value -> value) -> model -> model
modify Proxy "whereCondition"
#whereCondition \case
                                Just Condition
c -> Condition -> Maybe Condition
forall a. a -> Maybe a
Just (Condition -> Condition -> Condition
AndCondition Condition
c Condition
condition)
                                Maybe Condition
Nothing -> Condition -> Maybe Condition
forall a. a -> Maybe a
Just Condition
condition
    buildQueryHelper OrderByQueryBuilder { QueryBuilder table
queryBuilder :: forall (table :: Symbol). QueryBuilder table -> QueryBuilder table
queryBuilder :: QueryBuilder table
queryBuilder, OrderByClause
queryOrderByClause :: forall (table :: Symbol). QueryBuilder table -> OrderByClause
queryOrderByClause :: OrderByClause
queryOrderByClause } = QueryBuilder table
queryBuilder
            QueryBuilder table -> (QueryBuilder table -> SQLQuery) -> SQLQuery
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> QueryBuilder table -> SQLQuery
buildQueryHelper
            SQLQuery -> (SQLQuery -> SQLQuery) -> SQLQuery
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> Proxy "orderByClause"
-> ([OrderByClause] -> [OrderByClause]) -> SQLQuery -> SQLQuery
forall model (name :: Symbol) value.
(KnownSymbol name, HasField name model value,
 SetField name model value) =>
Proxy name -> (value -> value) -> model -> model
modify Proxy "orderByClause"
#orderByClause (\[OrderByClause]
value -> [OrderByClause]
value [OrderByClause] -> [OrderByClause] -> [OrderByClause]
forall a. Semigroup a => a -> a -> a
<> [OrderByClause
queryOrderByClause] ) -- although adding to the end of a list is bad form, these lists are very short
    buildQueryHelper LimitQueryBuilder { QueryBuilder table
queryBuilder :: forall (table :: Symbol). QueryBuilder table -> QueryBuilder table
queryBuilder :: QueryBuilder table
queryBuilder, Int
queryLimit :: forall (table :: Symbol). QueryBuilder table -> Int
queryLimit :: Int
queryLimit } =
                    QueryBuilder table
queryBuilder
                    QueryBuilder table -> (QueryBuilder table -> SQLQuery) -> SQLQuery
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> QueryBuilder table -> SQLQuery
buildQueryHelper
                    SQLQuery -> (SQLQuery -> SQLQuery) -> SQLQuery
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> Proxy "limitClause" -> ByteString -> SQLQuery -> SQLQuery
forall model (name :: Symbol) value.
(KnownSymbol name, SetField name model (Maybe value)) =>
Proxy name -> value -> model -> model
setJust Proxy "limitClause"
#limitClause (
                            (ByteString -> Builder
Builder.byteString ByteString
"LIMIT " Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Int -> Builder
Builder.intDec Int
queryLimit)
                            Builder -> (Builder -> LazyByteString) -> LazyByteString
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> Builder -> LazyByteString
Builder.toLazyByteString
                            LazyByteString -> (LazyByteString -> ByteString) -> ByteString
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> LazyByteString -> ByteString
LByteString.toStrict
                        )
    buildQueryHelper OffsetQueryBuilder { QueryBuilder table
queryBuilder :: forall (table :: Symbol). QueryBuilder table -> QueryBuilder table
queryBuilder :: QueryBuilder table
queryBuilder, Int
queryOffset :: forall (table :: Symbol). QueryBuilder table -> Int
queryOffset :: Int
queryOffset } = QueryBuilder table
queryBuilder
            QueryBuilder table -> (QueryBuilder table -> SQLQuery) -> SQLQuery
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> QueryBuilder table -> SQLQuery
buildQueryHelper
            SQLQuery -> (SQLQuery -> SQLQuery) -> SQLQuery
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> Proxy "offsetClause" -> ByteString -> SQLQuery -> SQLQuery
forall model (name :: Symbol) value.
(KnownSymbol name, SetField name model (Maybe value)) =>
Proxy name -> value -> model -> model
setJust Proxy "offsetClause"
#offsetClause (
                    (ByteString -> Builder
Builder.byteString ByteString
"OFFSET " Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Int -> Builder
Builder.intDec Int
queryOffset)
                    Builder -> (Builder -> LazyByteString) -> LazyByteString
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> Builder -> LazyByteString
Builder.toLazyByteString
                    LazyByteString -> (LazyByteString -> ByteString) -> ByteString
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> LazyByteString -> ByteString
LByteString.toStrict
                )
    buildQueryHelper UnionQueryBuilder { QueryBuilder table
firstQueryBuilder :: forall (table :: Symbol). QueryBuilder table -> QueryBuilder table
firstQueryBuilder :: QueryBuilder table
firstQueryBuilder, QueryBuilder table
secondQueryBuilder :: forall (table :: Symbol). QueryBuilder table -> QueryBuilder table
secondQueryBuilder :: QueryBuilder table
secondQueryBuilder } =
                let
                    firstQuery :: SQLQuery
firstQuery = QueryBuilder table -> SQLQuery
buildQueryHelper QueryBuilder table
firstQueryBuilder
                    secondQuery :: SQLQuery
secondQuery = QueryBuilder table -> SQLQuery
buildQueryHelper QueryBuilder table
secondQueryBuilder
                    isSimpleQuery :: SQLQuery -> Bool
isSimpleQuery SQLQuery
query = [OrderByClause] -> Bool
forall mono. MonoFoldable mono => mono -> Bool
null (SQLQuery -> [OrderByClause]
orderByClause SQLQuery
query) Bool -> Bool -> Bool
&& Maybe ByteString -> Bool
forall a. Maybe a -> Bool
isNothing (SQLQuery -> Maybe ByteString
limitClause SQLQuery
query) Bool -> Bool -> Bool
&& Maybe ByteString -> Bool
forall a. Maybe a -> Bool
isNothing (SQLQuery -> Maybe ByteString
offsetClause SQLQuery
query) Bool -> Bool -> Bool
&& [Join] -> Bool
forall mono. MonoFoldable mono => mono -> Bool
null (SQLQuery -> [Join]
joins SQLQuery
query)
                    isSimpleUnion :: Bool
isSimpleUnion = SQLQuery -> Bool
isSimpleQuery SQLQuery
firstQuery Bool -> Bool -> Bool
&& SQLQuery -> Bool
isSimpleQuery SQLQuery
secondQuery
                    unionWhere :: Maybe Condition
unionWhere =
                        case (SQLQuery -> Maybe Condition
whereCondition SQLQuery
firstQuery, SQLQuery -> Maybe Condition
whereCondition SQLQuery
secondQuery) of
                            (Maybe Condition
Nothing, Maybe Condition
whereCondition) -> Maybe Condition
whereCondition
                            (Maybe Condition
whereCondition, Maybe Condition
Nothing) -> Maybe Condition
whereCondition
                            (Just Condition
firstWhere, Just Condition
secondWhere) -> Condition -> Maybe Condition
forall a. a -> Maybe a
Just (Condition -> Maybe Condition) -> Condition -> Maybe Condition
forall a b. (a -> b) -> a -> b
$ Condition -> Condition -> Condition
OrCondition Condition
firstWhere Condition
secondWhere
                in
                    if Bool
isSimpleUnion then
                        SQLQuery
firstQuery { whereCondition = unionWhere }
                    else
                        Text -> SQLQuery
forall a. Text -> a
error Text
"buildQuery: Union of complex queries not supported yet"
    
    buildQueryHelper JoinQueryBuilder { QueryBuilder table
queryBuilder :: forall (table :: Symbol). QueryBuilder table -> QueryBuilder table
queryBuilder :: QueryBuilder table
queryBuilder, Join
joinData :: forall (table :: Symbol). QueryBuilder table -> Join
joinData :: Join
joinData } =
        let 
            firstQuery :: SQLQuery
firstQuery = QueryBuilder table -> SQLQuery
buildQueryHelper QueryBuilder table
queryBuilder
         in SQLQuery
firstQuery { joins = joinData:joins firstQuery }
    
-- | Transforms a @query @@User |> ..@ expression into a SQL Query. Returns a tuple with the sql query template and it's placeholder values.
--
-- __Example:__ Get the sql query that is represented by a QueryBuilder
--
-- >>> let postsQuery = query @Post |> filterWhere (#public, True)
-- >>> toSQL postsQuery
-- ("SELECT posts.* FROM posts WHERE public = ?", [Plain "true"])
toSQL :: (KnownSymbol table, HasQueryBuilder queryBuilderProvider joinRegister) => queryBuilderProvider table -> (ByteString, [Action])
toSQL :: forall {k} (table :: Symbol) (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k).
(KnownSymbol table,
 HasQueryBuilder queryBuilderProvider joinRegister) =>
queryBuilderProvider table -> (ByteString, [Action])
toSQL queryBuilderProvider table
queryBuilderProvider = SQLQuery -> (ByteString, [Action])
toSQL' (queryBuilderProvider table -> SQLQuery
forall {k} (table :: Symbol) (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k).
(KnownSymbol table,
 HasQueryBuilder queryBuilderProvider joinRegister) =>
queryBuilderProvider table -> SQLQuery
buildQuery queryBuilderProvider table
queryBuilderProvider)
{-# INLINE toSQL #-}

toSQL' :: SQLQuery -> (ByteString, [Action])
toSQL' :: SQLQuery -> (ByteString, [Action])
toSQL' sqlQuery :: SQLQuery
sqlQuery@SQLQuery { Maybe ByteString
queryIndex :: SQLQuery -> Maybe ByteString
queryIndex :: Maybe ByteString
queryIndex, ByteString
selectFrom :: SQLQuery -> ByteString
selectFrom :: ByteString
selectFrom, Maybe ByteString
distinctClause :: SQLQuery -> Maybe ByteString
distinctClause :: Maybe ByteString
distinctClause, Maybe ByteString
distinctOnClause :: SQLQuery -> Maybe ByteString
distinctOnClause :: Maybe ByteString
distinctOnClause, [OrderByClause]
orderByClause :: SQLQuery -> [OrderByClause]
orderByClause :: [OrderByClause]
orderByClause, Maybe ByteString
limitClause :: SQLQuery -> Maybe ByteString
limitClause :: Maybe ByteString
limitClause, Maybe ByteString
offsetClause :: SQLQuery -> Maybe ByteString
offsetClause :: Maybe ByteString
offsetClause, [ByteString]
columns :: SQLQuery -> [ByteString]
columns :: [ByteString]
columns } =
        (ByteString -> ByteString
forall a. NFData a => a -> a
DeepSeq.force ByteString
theQuery, [Action]
theParams)
    where
        !theQuery :: ByteString
theQuery =
            ByteString -> [ByteString] -> ByteString
ByteString.intercalate ByteString
" " ([ByteString] -> ByteString) -> [ByteString] -> ByteString
forall a b. (a -> b) -> a -> b
$
                [Maybe ByteString] -> [ByteString]
forall a. [Maybe a] -> [a]
catMaybes
                    [ ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just ByteString
"SELECT"
                    , Maybe ByteString
distinctClause
                    , Maybe ByteString
distinctOnClause
                    , ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just ByteString
selectors
                    , ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just ByteString
"FROM"
                    , ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just ByteString
fromClause
                    , Maybe ByteString
joinClause
                    , Maybe ByteString
whereConditions'
                    , Maybe ByteString
orderByClause'
                    , Maybe ByteString
limitClause
                    , Maybe ByteString
offsetClause
                    ]

        selectors :: ByteString
        selectors :: ByteString
selectors = ByteString -> [ByteString] -> ByteString
ByteString.intercalate ByteString
", " ([ByteString] -> ByteString) -> [ByteString] -> ByteString
forall a b. (a -> b) -> a -> b
$ ([Maybe ByteString] -> [ByteString]
forall a. [Maybe a] -> [a]
catMaybes [Maybe ByteString
queryIndex]) [ByteString] -> [ByteString] -> [ByteString]
forall a. Semigroup a => a -> a -> a
<> [ByteString]
selectFromWithColumns
            where
                -- Generates a string like: `posts.id, posts.title, posts.body`
                selectFromWithColumns :: [ByteString]
                selectFromWithColumns :: [ByteString]
selectFromWithColumns = 
                    [ByteString]
columns
                    [ByteString] -> ([ByteString] -> [ByteString]) -> [ByteString]
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> (ByteString -> ByteString) -> [ByteString] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
map (\ByteString
column -> ByteString
selectFrom ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"." ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
column)
        fromClause :: ByteString
        fromClause :: ByteString
fromClause = ByteString
selectFrom

        !theParams :: [Action]
theParams =
            case SQLQuery -> Maybe Condition
whereCondition SQLQuery
sqlQuery of
                Just Condition
condition -> Condition -> [Action]
compileConditionArgs Condition
condition
                Maybe Condition
Nothing -> [Action]
forall a. Monoid a => a
mempty

        toQualifiedName :: ByteString -> ByteString
toQualifiedName ByteString
unqualifiedName = ByteString
selectFrom ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"." ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
unqualifiedName

        whereConditions' :: Maybe ByteString
whereConditions' = case SQLQuery -> Maybe Condition
whereCondition SQLQuery
sqlQuery of
                Just Condition
condition -> ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just (ByteString -> Maybe ByteString) -> ByteString -> Maybe ByteString
forall a b. (a -> b) -> a -> b
$ ByteString
"WHERE " ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Condition -> ByteString
compileConditionQuery Condition
condition
                Maybe Condition
Nothing -> Maybe ByteString
forall a. Maybe a
Nothing

        orderByClause' :: Maybe ByteString
        orderByClause' :: Maybe ByteString
orderByClause' = case [OrderByClause]
orderByClause of
                [] -> Maybe ByteString
forall a. Maybe a
Nothing
                [OrderByClause]
xs -> ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just (ByteString
"ORDER BY " ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString -> [ByteString] -> ByteString
ByteString.intercalate ByteString
"," (((OrderByClause -> ByteString) -> [OrderByClause] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
map (\OrderByClause { ByteString
orderByColumn :: OrderByClause -> ByteString
orderByColumn :: ByteString
orderByColumn, OrderByDirection
orderByDirection :: OrderByClause -> OrderByDirection
orderByDirection :: OrderByDirection
orderByDirection } -> ByteString
orderByColumn ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> (if OrderByDirection
orderByDirection OrderByDirection -> OrderByDirection -> Bool
forall a. Eq a => a -> a -> Bool
== OrderByDirection
Desc then ByteString
" DESC" else ByteString
forall a. Monoid a => a
mempty)) [OrderByClause]
xs)))
        joinClause :: Maybe ByteString
        joinClause :: Maybe ByteString
joinClause = [Join] -> Maybe ByteString
buildJoinClause ([Join] -> Maybe ByteString) -> [Join] -> Maybe ByteString
forall a b. (a -> b) -> a -> b
$ [Join] -> [Join]
forall a. [a] -> [a]
reverse ([Join] -> [Join]) -> [Join] -> [Join]
forall a b. (a -> b) -> a -> b
$ SQLQuery -> [Join]
joins SQLQuery
sqlQuery
        buildJoinClause :: [Join] -> Maybe ByteString
        buildJoinClause :: [Join] -> Maybe ByteString
buildJoinClause [] = Maybe ByteString
forall a. Maybe a
Nothing
        buildJoinClause (Join
joinClause:[Join]
joinClauses) = ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just (ByteString -> Maybe ByteString) -> ByteString -> Maybe ByteString
forall a b. (a -> b) -> a -> b
$ ByteString
"INNER JOIN " ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Join -> ByteString
table Join
joinClause ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
" ON " ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Join -> ByteString
tableJoinColumn Join
joinClause ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
" = " ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<>Join -> ByteString
table Join
joinClause ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"." ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Join -> ByteString
otherJoinColumn Join
joinClause ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
-> (ByteString -> ByteString) -> Maybe ByteString -> ByteString
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ByteString
"" (ByteString
" " <>) ([Join] -> Maybe ByteString
buildJoinClause [Join]
joinClauses)


{-# INLINE toSQL' #-}

{-# INLINE compileConditionQuery #-}
compileConditionQuery :: Condition -> ByteString
compileConditionQuery :: Condition -> ByteString
compileConditionQuery (VarCondition ByteString
var Action
_) =  ByteString
var
compileConditionQuery (OrCondition Condition
a Condition
b) =  ByteString
"(" ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Condition -> ByteString
compileConditionQuery Condition
a ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
") OR (" ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Condition -> ByteString
compileConditionQuery Condition
b ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
")"
compileConditionQuery (AndCondition Condition
a Condition
b) =  ByteString
"(" ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Condition -> ByteString
compileConditionQuery Condition
a ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
") AND (" ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Condition -> ByteString
compileConditionQuery Condition
b ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
")"

{-# INLINE compileConditionArgs #-}
compileConditionArgs :: Condition -> [Action]
compileConditionArgs :: Condition -> [Action]
compileConditionArgs (VarCondition ByteString
_ Action
arg) = [Action
arg]
compileConditionArgs (OrCondition Condition
a Condition
b) = Condition -> [Action]
compileConditionArgs Condition
a [Action] -> [Action] -> [Action]
forall a. Semigroup a => a -> a -> a
<> Condition -> [Action]
compileConditionArgs Condition
b
compileConditionArgs (AndCondition Condition
a Condition
b) = Condition -> [Action]
compileConditionArgs Condition
a [Action] -> [Action] -> [Action]
forall a. Semigroup a => a -> a -> a
<> Condition -> [Action]
compileConditionArgs Condition
b

class FilterPrimaryKey table where
    filterWhereId :: Id' table -> QueryBuilder table -> QueryBuilder table

-- | Adds a simple @WHERE x = y@ condition to the query.
--
-- __Example:__ Only show projects where @active@ is @True@.
--
-- > activeProjects <- query @Project
-- >     |> filterWhere (#active, True)
-- >     |> fetch
-- > -- SELECT * FROM projects WHERE active = True
--
-- __Example:__ Find book with title @Learn you a Haskell@.
--
-- > book <- query @Book
-- >     |> filterWhere (#title, "Learn you a Haskell")
-- >     |> fetchOne
-- > -- SELECT * FROM books WHERE name = 'Learn you a Haskell' LIMIT 1
--
--
-- __Example:__ Find active projects owned by the current user.
--
-- > projects <- query @Project
-- >     |> filterWhere (#active, True)
-- >     |> filterWhere (#currentUserId, currentUserId)
-- >     |> fetch
-- > -- SELECT * FROM projects WHERE active = true AND current_user_id = '..'
--
--
-- For dynamic conditions (e.g. involving @NOW()@), see 'filterWhereSql'.
--
-- For @WHERE x IN (a, b, c)@ conditions, take a look at 'filterWhereIn' and 'filterWhereNotIn'.
--
-- For @WHERE x LIKE a@ or @WHERE x ~ a@  conditions, see 'filterWhereLike' and 'filterWhereMatches' respectively.
-- For case-insensitive versions of these operators, see 'filterWhereILike' and 'filterWhereIMatches'.
--
-- When your condition is too complex, use a raw sql query with 'IHP.ModelSupport.sqlQuery'.
filterWhere :: forall name table model value queryBuilderProvider joinRegister. (KnownSymbol table, KnownSymbol name, ToField value, HasField name model value, EqOrIsOperator value, model ~ GetModelByTableName table, HasQueryBuilder queryBuilderProvider joinRegister, Table model) => (Proxy name, value) -> queryBuilderProvider table -> queryBuilderProvider table
filterWhere :: forall {k} (name :: Symbol) (table :: Symbol) model value
       (queryBuilderProvider :: Symbol -> *) (joinRegister :: k).
(KnownSymbol table, KnownSymbol name, ToField value,
 HasField name model value, EqOrIsOperator value,
 model ~ GetModelByTableName table,
 HasQueryBuilder queryBuilderProvider joinRegister, Table model) =>
(Proxy name, value)
-> queryBuilderProvider table -> queryBuilderProvider table
filterWhere (Proxy name
name, value
value) queryBuilderProvider table
queryBuilderProvider = QueryBuilder table -> queryBuilderProvider table
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
QueryBuilder table -> queryBuilderProvider table
forall (table :: Symbol).
QueryBuilder table -> queryBuilderProvider table
injectQueryBuilder FilterByQueryBuilder { QueryBuilder table
queryBuilder :: QueryBuilder table
queryBuilder :: QueryBuilder table
queryBuilder, queryFilter :: (ByteString, FilterOperator, Action)
queryFilter = (ByteString
columnName, value -> FilterOperator
forall value. EqOrIsOperator value => value -> FilterOperator
toEqOrIsOperator value
value, value -> Action
forall a. ToField a => a -> Action
toField value
value), applyLeft :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, applyRight :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing }
    where
        columnName :: ByteString
columnName = forall record. Table record => ByteString
tableNameByteString @model ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"." ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Text -> ByteString
Text.encodeUtf8 (Text -> Text
fieldNameToColumnName (forall (symbol :: Symbol). KnownSymbol symbol => Text
symbolToText @name))
        queryBuilder :: QueryBuilder table
queryBuilder = queryBuilderProvider table -> QueryBuilder table
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
queryBuilderProvider table -> QueryBuilder table
forall (table :: Symbol).
queryBuilderProvider table -> QueryBuilder table
getQueryBuilder queryBuilderProvider table
queryBuilderProvider
{-# INLINE filterWhere #-}

-- | Like 'filterWhere', but takes a type argument specifying the table which holds the column that is to be compared. The column must have been joined before using 'innerJoin' or 'innerJoinThirdTable'. Example: 
--
-- __Example:__ get posts by user Tom.
--
-- > tomPosts <- query @Post
-- >                    |> innerJoin @User (#createdBy, #id)
-- >                    |> filterWhereJoinedTable @User (#name, "Tom" :: Text)
-- >                    |> fetch
-- > -- SELECT posts.* FROM posts INNER JOIN users ON posts.created_by = users.id WHERE users.name = 'Tom'
--
filterWhereJoinedTable :: forall model name table value queryBuilderProvider joinRegister table'. (KnownSymbol table, KnownSymbol name, ToField value, HasField name model value, EqOrIsOperator value, table ~ GetTableName model, HasQueryBuilder queryBuilderProvider joinRegister, IsJoined model joinRegister, Table model) => (Proxy name, value) -> queryBuilderProvider table' -> queryBuilderProvider table'
filterWhereJoinedTable :: forall {k} model (name :: Symbol) (table :: Symbol) value
       (queryBuilderProvider :: Symbol -> *) (joinRegister :: k)
       (table' :: Symbol).
(KnownSymbol table, KnownSymbol name, ToField value,
 HasField name model value, EqOrIsOperator value,
 table ~ GetTableName model,
 HasQueryBuilder queryBuilderProvider joinRegister,
 IsJoined model joinRegister, Table model) =>
(Proxy name, value)
-> queryBuilderProvider table' -> queryBuilderProvider table'
filterWhereJoinedTable (Proxy name
name, value
value) queryBuilderProvider table'
queryBuilderProvider = QueryBuilder table' -> queryBuilderProvider table'
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
QueryBuilder table -> queryBuilderProvider table
forall (table :: Symbol).
QueryBuilder table -> queryBuilderProvider table
injectQueryBuilder FilterByQueryBuilder { QueryBuilder table'
queryBuilder :: QueryBuilder table'
queryBuilder :: QueryBuilder table'
queryBuilder, queryFilter :: (ByteString, FilterOperator, Action)
queryFilter = (ByteString
columnName, value -> FilterOperator
forall value. EqOrIsOperator value => value -> FilterOperator
toEqOrIsOperator value
value, value -> Action
forall a. ToField a => a -> Action
toField value
value), applyLeft :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, applyRight :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing }
    where
        columnName :: ByteString
columnName = forall record. Table record => ByteString
tableNameByteString @model ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"." ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Text -> ByteString
Text.encodeUtf8 (Text -> Text
fieldNameToColumnName (forall (symbol :: Symbol). KnownSymbol symbol => Text
symbolToText @name))
        queryBuilder :: QueryBuilder table'
queryBuilder = queryBuilderProvider table' -> QueryBuilder table'
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
queryBuilderProvider table -> QueryBuilder table
forall (table :: Symbol).
queryBuilderProvider table -> QueryBuilder table
getQueryBuilder queryBuilderProvider table'
queryBuilderProvider
{-# INLINE filterWhereJoinedTable #-}

-- | Like 'filterWhere' but negates the condition.
--
-- __Example:__ Only show projects created by other users.
--
-- > activeProjects <- query @Project
-- >     |> filterWhereNot (#userId, currentUserId)
-- >     |> fetch
-- > -- SELECT * FROM projects WHERE user_id != '23d5ea33-b28e-4f0a-99b3-77a3564a2546'
--
filterWhereNot :: forall name table model value. (KnownSymbol table, KnownSymbol name, ToField value, HasField name model value, EqOrIsOperator value, model ~ GetModelByTableName table, Table model) => (Proxy name, value) -> QueryBuilder table -> QueryBuilder table
filterWhereNot :: forall (name :: Symbol) (table :: Symbol) model value.
(KnownSymbol table, KnownSymbol name, ToField value,
 HasField name model value, EqOrIsOperator value,
 model ~ GetModelByTableName table, Table model) =>
(Proxy name, value) -> QueryBuilder table -> QueryBuilder table
filterWhereNot (Proxy name
name, value
value) QueryBuilder table
queryBuilder = FilterByQueryBuilder { QueryBuilder table
queryBuilder :: QueryBuilder table
queryBuilder :: QueryBuilder table
queryBuilder, queryFilter :: (ByteString, FilterOperator, Action)
queryFilter = (ByteString
columnName, FilterOperator -> FilterOperator
negateFilterOperator (value -> FilterOperator
forall value. EqOrIsOperator value => value -> FilterOperator
toEqOrIsOperator value
value), value -> Action
forall a. ToField a => a -> Action
toField value
value), applyLeft :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, applyRight :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing }
    where
        columnName :: ByteString
columnName = forall record. Table record => ByteString
tableNameByteString @model ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"." ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Text -> ByteString
Text.encodeUtf8 (Text -> Text
fieldNameToColumnName (forall (symbol :: Symbol). KnownSymbol symbol => Text
symbolToText @name))
{-# INLINE filterWhereNot #-}

-- | Like 'filterWhereNotJoinedTable' but negates the condition.
--
-- __Example:__ Only show projects not created by user Tom.
--
-- > tomPosts <- query @Post
-- >                    |> innerJoin @User (#createdBy, #id)
-- >                    |> filterWhereNotJoinedTable @User (#name, "Tom" :: Text)
-- >                    |> fetch
-- > -- SELECT posts.* FROM posts INNER JOIN users ON posts.created_by = users.id WHERE users.name = 'Tom'
--
filterWhereNotJoinedTable :: forall model name table value queryBuilderProvider joinRegister table'. (KnownSymbol table, KnownSymbol name, ToField value, HasField name model value, EqOrIsOperator value, table ~ GetTableName model, HasQueryBuilder queryBuilderProvider joinRegister, IsJoined model joinRegister, Table model) => (Proxy name, value) -> queryBuilderProvider table' -> queryBuilderProvider table'
filterWhereNotJoinedTable :: forall {k} model (name :: Symbol) (table :: Symbol) value
       (queryBuilderProvider :: Symbol -> *) (joinRegister :: k)
       (table' :: Symbol).
(KnownSymbol table, KnownSymbol name, ToField value,
 HasField name model value, EqOrIsOperator value,
 table ~ GetTableName model,
 HasQueryBuilder queryBuilderProvider joinRegister,
 IsJoined model joinRegister, Table model) =>
(Proxy name, value)
-> queryBuilderProvider table' -> queryBuilderProvider table'
filterWhereNotJoinedTable (Proxy name
name, value
value) queryBuilderProvider table'
queryBuilderProvider = QueryBuilder table' -> queryBuilderProvider table'
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
QueryBuilder table -> queryBuilderProvider table
forall (table :: Symbol).
QueryBuilder table -> queryBuilderProvider table
injectQueryBuilder FilterByQueryBuilder { QueryBuilder table'
queryBuilder :: QueryBuilder table'
queryBuilder :: QueryBuilder table'
queryBuilder, queryFilter :: (ByteString, FilterOperator, Action)
queryFilter = (ByteString
columnName, FilterOperator -> FilterOperator
negateFilterOperator (value -> FilterOperator
forall value. EqOrIsOperator value => value -> FilterOperator
toEqOrIsOperator value
value), value -> Action
forall a. ToField a => a -> Action
toField value
value), applyLeft :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, applyRight :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing }
    where
        columnName :: ByteString
columnName = forall record. Table record => ByteString
tableNameByteString @model ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"." ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Text -> ByteString
Text.encodeUtf8 (Text -> Text
fieldNameToColumnName (forall (symbol :: Symbol). KnownSymbol symbol => Text
symbolToText @name))
        queryBuilder :: QueryBuilder table'
queryBuilder = queryBuilderProvider table' -> QueryBuilder table'
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
queryBuilderProvider table -> QueryBuilder table
forall (table :: Symbol).
queryBuilderProvider table -> QueryBuilder table
getQueryBuilder queryBuilderProvider table'
queryBuilderProvider
{-# INLINE filterWhereNotJoinedTable #-}
-- | Adds a @WHERE x IN (y)@ condition to the query.
--
-- __Example:__ Only show projects where @status@ is @Draft@ or @Active@.
--
-- > visibleProjects <- query @Project
-- >     |> filterWhereIn (#status, [Draft, Active])
-- >     |> fetch
-- > -- SELECT * FROM projects WHERE status IN ('draft', 'active')
--
-- For negation use 'filterWhereNotIn'
--
filterWhereIn :: forall name table model value queryBuilderProvider (joinRegister :: Type). (KnownSymbol table, KnownSymbol name, ToField value, HasField name model value, model ~ GetModelByTableName table, HasQueryBuilder queryBuilderProvider joinRegister, EqOrIsOperator value, Table model) => (Proxy name, [value]) -> queryBuilderProvider table -> queryBuilderProvider table
filterWhereIn :: forall (name :: Symbol) (table :: Symbol) model value
       (queryBuilderProvider :: Symbol -> *) joinRegister.
(KnownSymbol table, KnownSymbol name, ToField value,
 HasField name model value, model ~ GetModelByTableName table,
 HasQueryBuilder queryBuilderProvider joinRegister,
 EqOrIsOperator value, Table model) =>
(Proxy name, [value])
-> queryBuilderProvider table -> queryBuilderProvider table
filterWhereIn (Proxy name
name, [value]
value) queryBuilderProvider table
queryBuilderProvider =
        case [value] -> Maybe value
forall a. [a] -> Maybe a
head [value]
nullValues of
            Maybe value
Nothing -> QueryBuilder table -> queryBuilderProvider table
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
QueryBuilder table -> queryBuilderProvider table
forall (table :: Symbol).
QueryBuilder table -> queryBuilderProvider table
injectQueryBuilder QueryBuilder table
whereInQuery -- All values non null
            Just value
nullValue ->
                let
                    isNullValueExpr :: QueryBuilder table
isNullValueExpr = FilterByQueryBuilder { QueryBuilder table
queryBuilder :: QueryBuilder table
queryBuilder :: QueryBuilder table
queryBuilder, queryFilter :: (ByteString, FilterOperator, Action)
queryFilter = (ByteString
columnName, FilterOperator
IsOp, value -> Action
forall a. ToField a => a -> Action
toField value
nullValue), applyLeft :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, applyRight :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing }
                in
                    case [value] -> Maybe value
forall a. [a] -> Maybe a
head [value]
nonNullValues of
                        Just value
nonNullValue -> -- Some non null values, some null values
                            QueryBuilder table -> queryBuilderProvider table
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
QueryBuilder table -> queryBuilderProvider table
forall (table :: Symbol).
QueryBuilder table -> queryBuilderProvider table
injectQueryBuilder (QueryBuilder table -> queryBuilderProvider table)
-> QueryBuilder table -> queryBuilderProvider table
forall a b. (a -> b) -> a -> b
$ QueryBuilder table -> QueryBuilder table -> QueryBuilder table
forall (table :: Symbol).
QueryBuilder table -> QueryBuilder table -> QueryBuilder table
UnionQueryBuilder
                                (QueryBuilder table -> QueryBuilder table
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
QueryBuilder table -> queryBuilderProvider table
forall (table :: Symbol). QueryBuilder table -> QueryBuilder table
injectQueryBuilder QueryBuilder table
whereInQuery)
                                (QueryBuilder table -> QueryBuilder table
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
QueryBuilder table -> queryBuilderProvider table
forall (table :: Symbol). QueryBuilder table -> QueryBuilder table
injectQueryBuilder QueryBuilder table
isNullValueExpr)
                        Maybe value
Nothing -> QueryBuilder table -> queryBuilderProvider table
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
QueryBuilder table -> queryBuilderProvider table
forall (table :: Symbol).
QueryBuilder table -> queryBuilderProvider table
injectQueryBuilder QueryBuilder table
isNullValueExpr -- All values null
    where
        -- Only NOT NULL values can be compares inside the IN expression, NULL values have to be compares using a manual appended IS expression
        -- https://github.com/digitallyinduced/ihp/issues/906
        --
        ([value]
nonNullValues, [value]
nullValues) = [value]
value [value] -> ([value] -> ([value], [value])) -> ([value], [value])
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> (value -> Bool) -> [value] -> ([value], [value])
forall a. (a -> Bool) -> [a] -> ([a], [a])
partition (\value
v -> value -> FilterOperator
forall value. EqOrIsOperator value => value -> FilterOperator
toEqOrIsOperator value
v FilterOperator -> FilterOperator -> Bool
forall a. Eq a => a -> a -> Bool
== FilterOperator
EqOp)

        whereInQuery :: QueryBuilder table
whereInQuery = FilterByQueryBuilder { QueryBuilder table
queryBuilder :: QueryBuilder table
queryBuilder :: QueryBuilder table
queryBuilder, queryFilter :: (ByteString, FilterOperator, Action)
queryFilter = (ByteString
columnName, FilterOperator
InOp, In [value] -> Action
forall a. ToField a => a -> Action
toField ([value] -> In [value]
forall a. a -> In a
In [value]
nonNullValues)), applyLeft :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, applyRight :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing }

        columnName :: ByteString
columnName = forall record. Table record => ByteString
tableNameByteString @model ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"." ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Text -> ByteString
Text.encodeUtf8 (Text -> Text
fieldNameToColumnName (forall (symbol :: Symbol). KnownSymbol symbol => Text
symbolToText @name))
        queryBuilder :: QueryBuilder table
queryBuilder = queryBuilderProvider table -> QueryBuilder table
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
queryBuilderProvider table -> QueryBuilder table
forall (table :: Symbol).
queryBuilderProvider table -> QueryBuilder table
getQueryBuilder queryBuilderProvider table
queryBuilderProvider
{-# INLINE filterWhereIn #-}

-- | Like 'filterWhereIn', but takes a type argument specifying the table which holds the column that is compared. The table needs to have been joined before using 'innerJoin' or 'innerJoinThirdTable'.
-- 
-- __Example:__ get posts by Tom and Tim.
--
-- > tomOrTimPosts <- query @Post 
-- >    |> innerJoin @User (#createdBy, #id) 
-- >    |> filterWhereInJoinedTable @User (#name, ["Tom","Tim"]) 
-- >    |> fetch
-- > -- SELECT posts.* FROM posts INNER JOIN users ON posts.created_by = users.id WHERE users.name IN ('Tom', 'Tim') 
--
filterWhereInJoinedTable :: forall model name table value queryBuilderProvider joinRegister table'. (KnownSymbol table, KnownSymbol name, ToField value, HasField name model value, table ~ GetTableName model, HasQueryBuilder queryBuilderProvider joinRegister, IsJoined model joinRegister, Table model) => (Proxy name, [value]) -> queryBuilderProvider table' -> queryBuilderProvider table'
filterWhereInJoinedTable :: forall {k} model (name :: Symbol) (table :: Symbol) value
       (queryBuilderProvider :: Symbol -> *) (joinRegister :: k)
       (table' :: Symbol).
(KnownSymbol table, KnownSymbol name, ToField value,
 HasField name model value, table ~ GetTableName model,
 HasQueryBuilder queryBuilderProvider joinRegister,
 IsJoined model joinRegister, Table model) =>
(Proxy name, [value])
-> queryBuilderProvider table' -> queryBuilderProvider table'
filterWhereInJoinedTable (Proxy name
name, [value]
value) queryBuilderProvider table'
queryBuilderProvider = QueryBuilder table' -> queryBuilderProvider table'
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
QueryBuilder table -> queryBuilderProvider table
forall (table :: Symbol).
QueryBuilder table -> queryBuilderProvider table
injectQueryBuilder FilterByQueryBuilder { QueryBuilder table'
queryBuilder :: QueryBuilder table'
queryBuilder :: QueryBuilder table'
queryBuilder, queryFilter :: (ByteString, FilterOperator, Action)
queryFilter = (ByteString
columnName, FilterOperator
InOp, In [value] -> Action
forall a. ToField a => a -> Action
toField ([value] -> In [value]
forall a. a -> In a
In [value]
value)), applyLeft :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, applyRight :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing }
    where
        columnName :: ByteString
columnName = forall record. Table record => ByteString
tableNameByteString @model ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"." ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Text -> ByteString
Text.encodeUtf8 (Text -> Text
fieldNameToColumnName (forall (symbol :: Symbol). KnownSymbol symbol => Text
symbolToText @name))
        queryBuilder :: QueryBuilder table'
queryBuilder = queryBuilderProvider table' -> QueryBuilder table'
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
queryBuilderProvider table -> QueryBuilder table
forall (table :: Symbol).
queryBuilderProvider table -> QueryBuilder table
getQueryBuilder queryBuilderProvider table'
queryBuilderProvider
{-# INLINE filterWhereInJoinedTable #-}


-- | Adds a @WHERE x NOT IN (y)@ condition to the query.
--
-- __Example:__ Only show projects where @status@ is not @Archived@
--
-- > visibleProjects <- query @Project
-- >     |> filterWhereNotIn (#status, [Archived])
-- >     |> fetch
-- > -- SELECT * FROM projects WHERE status NOT IN ('archived')
--
-- The inclusive version of this function is called 'filterWhereIn'.
--
filterWhereNotIn :: forall name table model value queryBuilderProvider joinRegister. (KnownSymbol table, KnownSymbol name, ToField value, HasField name model value, model ~ GetModelByTableName table, HasQueryBuilder queryBuilderProvider joinRegister, EqOrIsOperator value) => (Proxy name, [value]) -> queryBuilderProvider table -> queryBuilderProvider table
filterWhereNotIn :: forall {k} (name :: Symbol) (table :: Symbol) model value
       (queryBuilderProvider :: Symbol -> *) (joinRegister :: k).
(KnownSymbol table, KnownSymbol name, ToField value,
 HasField name model value, model ~ GetModelByTableName table,
 HasQueryBuilder queryBuilderProvider joinRegister,
 EqOrIsOperator value) =>
(Proxy name, [value])
-> queryBuilderProvider table -> queryBuilderProvider table
filterWhereNotIn (Proxy name
_, []) queryBuilderProvider table
queryBuilder = queryBuilderProvider table
queryBuilder -- Handle empty case by ignoring query part: `WHERE x NOT IN ()`
filterWhereNotIn (Proxy name
name, [value]
value) queryBuilderProvider table
queryBuilderProvider =
        case [value] -> Maybe value
forall a. [a] -> Maybe a
head [value]
nullValues of
            Maybe value
Nothing -> QueryBuilder table -> queryBuilderProvider table
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
QueryBuilder table -> queryBuilderProvider table
forall (table :: Symbol).
QueryBuilder table -> queryBuilderProvider table
injectQueryBuilder QueryBuilder table
whereNotInQuery -- All values non null
            Just value
nullValue ->
                case [value] -> Maybe value
forall a. [a] -> Maybe a
head [value]
nonNullValues of
                    Just value
nonNullValue -> QueryBuilder table -> queryBuilderProvider table
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
QueryBuilder table -> queryBuilderProvider table
forall (table :: Symbol).
QueryBuilder table -> queryBuilderProvider table
injectQueryBuilder FilterByQueryBuilder { queryBuilder :: QueryBuilder table
queryBuilder = QueryBuilder table
whereNotInQuery, queryFilter :: (ByteString, FilterOperator, Action)
queryFilter = (ByteString
columnName, FilterOperator
IsNotOp, value -> Action
forall a. ToField a => a -> Action
toField value
nullValue), applyLeft :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, applyRight :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing } -- Some non null values, some null values
                    Maybe value
Nothing -> QueryBuilder table -> queryBuilderProvider table
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
QueryBuilder table -> queryBuilderProvider table
forall (table :: Symbol).
QueryBuilder table -> queryBuilderProvider table
injectQueryBuilder FilterByQueryBuilder { QueryBuilder table
queryBuilder :: QueryBuilder table
queryBuilder :: QueryBuilder table
queryBuilder, queryFilter :: (ByteString, FilterOperator, Action)
queryFilter = (ByteString
columnName, FilterOperator
IsNotOp, value -> Action
forall a. ToField a => a -> Action
toField value
nullValue), applyLeft :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, applyRight :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing } -- All values null
    where
        -- Only NOT NULL values can be compares inside the IN expression, NULL values have to be compares using a manual appended IS expression
        -- https://github.com/digitallyinduced/ihp/issues/906
        --
        ([value]
nonNullValues, [value]
nullValues) = [value]
value [value] -> ([value] -> ([value], [value])) -> ([value], [value])
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> (value -> Bool) -> [value] -> ([value], [value])
forall a. (a -> Bool) -> [a] -> ([a], [a])
partition (\value
v -> value -> FilterOperator
forall value. EqOrIsOperator value => value -> FilterOperator
toEqOrIsOperator value
v FilterOperator -> FilterOperator -> Bool
forall a. Eq a => a -> a -> Bool
== FilterOperator
EqOp)

        whereNotInQuery :: QueryBuilder table
whereNotInQuery = FilterByQueryBuilder { QueryBuilder table
queryBuilder :: QueryBuilder table
queryBuilder :: QueryBuilder table
queryBuilder, queryFilter :: (ByteString, FilterOperator, Action)
queryFilter = (ByteString
columnName, FilterOperator
NotInOp, In [value] -> Action
forall a. ToField a => a -> Action
toField ([value] -> In [value]
forall a. a -> In a
In [value]
nonNullValues)), applyLeft :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, applyRight :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing }

        columnName :: ByteString
columnName = Text -> ByteString
Text.encodeUtf8 (forall (symbol :: Symbol). KnownSymbol symbol => Text
symbolToText @table) ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"." ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Text -> ByteString
Text.encodeUtf8 (Text -> Text
fieldNameToColumnName (forall (symbol :: Symbol). KnownSymbol symbol => Text
symbolToText @name))
        queryBuilder :: QueryBuilder table
queryBuilder = queryBuilderProvider table -> QueryBuilder table
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
queryBuilderProvider table -> QueryBuilder table
forall (table :: Symbol).
queryBuilderProvider table -> QueryBuilder table
getQueryBuilder queryBuilderProvider table
queryBuilderProvider
{-# INLINE filterWhereNotIn #-}

-- | Like 'filterWhereNotIn', but takes a type argument specifying the table which holds the column that is compared. The table needs to have been joined before using 'innerJoin' or 'innerJoinThirdTable'.
--
-- __Example:__ get posts by users not named Tom or Tim.
--
-- > notTomOrTimPosts <- query @Post
-- >    |> innerJoin @User (#createdBy, #id)
-- >    |> filterWhereNotInJoinedTable @User (#name, ["Tom","Tim"])
-- >    |> fetch
-- > -- SELECT posts.* FROM posts INNER JOIN users ON posts.created_by = users.id WHERE users.name NOT IN ('Tom', 'Tim')
filterWhereNotInJoinedTable :: forall model name table  value queryBuilderProvider joinRegister table'. (KnownSymbol table, KnownSymbol name, ToField value, HasField name model value, table ~ GetTableName model, HasQueryBuilder queryBuilderProvider joinRegister, IsJoined model joinRegister, Table model) => (Proxy name, [value]) -> queryBuilderProvider table' -> queryBuilderProvider table'
filterWhereNotInJoinedTable :: forall {k} model (name :: Symbol) (table :: Symbol) value
       (queryBuilderProvider :: Symbol -> *) (joinRegister :: k)
       (table' :: Symbol).
(KnownSymbol table, KnownSymbol name, ToField value,
 HasField name model value, table ~ GetTableName model,
 HasQueryBuilder queryBuilderProvider joinRegister,
 IsJoined model joinRegister, Table model) =>
(Proxy name, [value])
-> queryBuilderProvider table' -> queryBuilderProvider table'
filterWhereNotInJoinedTable (Proxy name
_, []) queryBuilderProvider table'
queryBuilderProvider = queryBuilderProvider table'
queryBuilderProvider -- Handle empty case by ignoring query part: `WHERE x NOT IN ()`
filterWhereNotInJoinedTable (Proxy name
name, [value]
value) queryBuilderProvider table'
queryBuilderProvider = QueryBuilder table' -> queryBuilderProvider table'
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
QueryBuilder table -> queryBuilderProvider table
forall (table :: Symbol).
QueryBuilder table -> queryBuilderProvider table
injectQueryBuilder FilterByQueryBuilder { QueryBuilder table'
queryBuilder :: QueryBuilder table'
queryBuilder :: QueryBuilder table'
queryBuilder, queryFilter :: (ByteString, FilterOperator, Action)
queryFilter = (ByteString
columnName, FilterOperator
NotInOp, In [value] -> Action
forall a. ToField a => a -> Action
toField ([value] -> In [value]
forall a. a -> In a
In [value]
value)), applyLeft :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, applyRight :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing }
    where
        columnName :: ByteString
columnName = forall record. Table record => ByteString
tableNameByteString @model ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"." ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Text -> ByteString
Text.encodeUtf8 (Text -> Text
fieldNameToColumnName (forall (symbol :: Symbol). KnownSymbol symbol => Text
symbolToText @name))
        queryBuilder :: QueryBuilder table'
queryBuilder = queryBuilderProvider table' -> QueryBuilder table'
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
queryBuilderProvider table -> QueryBuilder table
forall (table :: Symbol).
queryBuilderProvider table -> QueryBuilder table
getQueryBuilder queryBuilderProvider table'
queryBuilderProvider
{-# INLINE filterWhereNotInJoinedTable #-}


-- | Adds a @WHERE x LIKE y@ condition to the query.
--
-- __Example:__ Find titles matching search term.
--
-- > articles <- query @Article
-- >     |> filterWhereLike (#title, "%" <> searchTerm <> "%")
-- >     |> fetch
-- > -- SELECT * FROM articles WHERE title LIKE '%..%'
filterWhereLike :: forall name table model value queryBuilderProvider joinRegister. (KnownSymbol table, KnownSymbol name, ToField value, HasField name model value, model ~ GetModelByTableName table, HasQueryBuilder queryBuilderProvider joinRegister, Table model) => (Proxy name, value) -> queryBuilderProvider table -> queryBuilderProvider table
filterWhereLike :: forall {k} (name :: Symbol) (table :: Symbol) model value
       (queryBuilderProvider :: Symbol -> *) (joinRegister :: k).
(KnownSymbol table, KnownSymbol name, ToField value,
 HasField name model value, model ~ GetModelByTableName table,
 HasQueryBuilder queryBuilderProvider joinRegister, Table model) =>
(Proxy name, value)
-> queryBuilderProvider table -> queryBuilderProvider table
filterWhereLike (Proxy name
name, value
value) queryBuilderProvider table
queryBuilderProvider = QueryBuilder table -> queryBuilderProvider table
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
QueryBuilder table -> queryBuilderProvider table
forall (table :: Symbol).
QueryBuilder table -> queryBuilderProvider table
injectQueryBuilder FilterByQueryBuilder { QueryBuilder table
queryBuilder :: QueryBuilder table
queryBuilder :: QueryBuilder table
queryBuilder, queryFilter :: (ByteString, FilterOperator, Action)
queryFilter = (ByteString
columnName, MatchSensitivity -> FilterOperator
LikeOp MatchSensitivity
CaseSensitive, value -> Action
forall a. ToField a => a -> Action
toField value
value), applyLeft :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, applyRight :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing }
    where
        columnName :: ByteString
columnName = forall record. Table record => ByteString
tableNameByteString @model ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"." ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Text -> ByteString
Text.encodeUtf8 (Text -> Text
fieldNameToColumnName (forall (symbol :: Symbol). KnownSymbol symbol => Text
symbolToText @name))
        queryBuilder :: QueryBuilder table
queryBuilder = queryBuilderProvider table -> QueryBuilder table
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
queryBuilderProvider table -> QueryBuilder table
forall (table :: Symbol).
queryBuilderProvider table -> QueryBuilder table
getQueryBuilder queryBuilderProvider table
queryBuilderProvider
{-# INLINE filterWhereLike #-}

-- | Like 'filterWhereLik'e, but takes a type argument specifying the table which holds the column that is compared. The table needs to have been joined before using 'innerJoin' or 'innerJoinThirdTable'.
--
-- __Example:__ Serach for Posts by users whose name contains "olaf" (case insensitive)
--
-- > olafPosts <- query @Post  
-- >                |> innerJoin @User (#createdBy, #id)  
-- >                |> filterWhereLikeJoinedTable @User (#name, "%Olaf%")
-- >                |> fetch
-- > -- SELECT posts.* FROM posts INNER JOIN users ON posts.created_by = users.id WHERE users.name LIKE '%Olaf%'
filterWhereLikeJoinedTable :: forall model name table value queryBuilderProvider joinRegister table'. (KnownSymbol name, KnownSymbol table, table ~ GetTableName model, ToField value, HasField name model value, model ~ GetModelByTableName table, HasQueryBuilder queryBuilderProvider joinRegister, IsJoined model joinRegister, Table model) => (Proxy name, value) -> queryBuilderProvider table' -> queryBuilderProvider table'
filterWhereLikeJoinedTable :: forall {k} model (name :: Symbol) (table :: Symbol) value
       (queryBuilderProvider :: Symbol -> *) (joinRegister :: k)
       (table' :: Symbol).
(KnownSymbol name, KnownSymbol table, table ~ GetTableName model,
 ToField value, HasField name model value,
 model ~ GetModelByTableName table,
 HasQueryBuilder queryBuilderProvider joinRegister,
 IsJoined model joinRegister, Table model) =>
(Proxy name, value)
-> queryBuilderProvider table' -> queryBuilderProvider table'
filterWhereLikeJoinedTable (Proxy name
name, value
value) queryBuilderProvider table'
queryBuilderProvider = QueryBuilder table' -> queryBuilderProvider table'
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
QueryBuilder table -> queryBuilderProvider table
forall (table :: Symbol).
QueryBuilder table -> queryBuilderProvider table
injectQueryBuilder FilterByQueryBuilder { QueryBuilder table'
queryBuilder :: QueryBuilder table'
queryBuilder :: QueryBuilder table'
queryBuilder, queryFilter :: (ByteString, FilterOperator, Action)
queryFilter = (ByteString
columnName, MatchSensitivity -> FilterOperator
LikeOp MatchSensitivity
CaseSensitive, value -> Action
forall a. ToField a => a -> Action
toField value
value), applyLeft :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, applyRight :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing }
    where
        columnName :: ByteString
columnName = forall record. Table record => ByteString
tableNameByteString @model ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"." ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Text -> ByteString
Text.encodeUtf8 (Text -> Text
fieldNameToColumnName (forall (symbol :: Symbol). KnownSymbol symbol => Text
symbolToText @name))
        queryBuilder :: QueryBuilder table'
queryBuilder = queryBuilderProvider table' -> QueryBuilder table'
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
queryBuilderProvider table -> QueryBuilder table
forall (table :: Symbol).
queryBuilderProvider table -> QueryBuilder table
getQueryBuilder queryBuilderProvider table'
queryBuilderProvider
{-# INLINE filterWhereLikeJoinedTable #-}


-- | Adds a @WHERE x ILIKE y@ condition to the query. Case-insensitive version of 'filterWhereLike'.
--
-- __Example:__ Find titles matching search term.
--
-- > articles <- query @Article
-- >     |> filterWhereILike (#title, "%" <> searchTerm <> "%")
-- >     |> fetch
-- > -- SELECT * FROM articles WHERE title ILIKE '%..%'
filterWhereILike :: forall name table model value queryBuilderProvider joinRegister. (KnownSymbol table, KnownSymbol name, ToField value, HasField name model value, model ~ GetModelByTableName table, HasQueryBuilder queryBuilderProvider joinRegister, Table model) => (Proxy name, value) -> queryBuilderProvider table -> queryBuilderProvider table
filterWhereILike :: forall {k} (name :: Symbol) (table :: Symbol) model value
       (queryBuilderProvider :: Symbol -> *) (joinRegister :: k).
(KnownSymbol table, KnownSymbol name, ToField value,
 HasField name model value, model ~ GetModelByTableName table,
 HasQueryBuilder queryBuilderProvider joinRegister, Table model) =>
(Proxy name, value)
-> queryBuilderProvider table -> queryBuilderProvider table
filterWhereILike (Proxy name
name, value
value) queryBuilderProvider table
queryBuilderProvider = QueryBuilder table -> queryBuilderProvider table
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
QueryBuilder table -> queryBuilderProvider table
forall (table :: Symbol).
QueryBuilder table -> queryBuilderProvider table
injectQueryBuilder FilterByQueryBuilder { QueryBuilder table
queryBuilder :: QueryBuilder table
queryBuilder :: QueryBuilder table
queryBuilder, queryFilter :: (ByteString, FilterOperator, Action)
queryFilter = (ByteString
columnName, MatchSensitivity -> FilterOperator
LikeOp MatchSensitivity
CaseInsensitive, value -> Action
forall a. ToField a => a -> Action
toField value
value), applyLeft :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, applyRight :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing }
    where
        columnName :: ByteString
columnName = forall record. Table record => ByteString
tableNameByteString @model ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"." ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Text -> ByteString
Text.encodeUtf8 (Text -> Text
fieldNameToColumnName (forall (symbol :: Symbol). KnownSymbol symbol => Text
symbolToText @name))
        queryBuilder :: QueryBuilder table
queryBuilder = queryBuilderProvider table -> QueryBuilder table
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
queryBuilderProvider table -> QueryBuilder table
forall (table :: Symbol).
queryBuilderProvider table -> QueryBuilder table
getQueryBuilder queryBuilderProvider table
queryBuilderProvider
{-# INLINE filterWhereILike #-}

-- | Like 'filterWhereILike'; case-insensitive version of filterWhereLikeJoinedTable, takes a type argument specifying the table which holds the column that is compared. The table needs to have been joined before using 'innerJoin' or 'innerJoinThirdTable'.
--
-- __Example:__ Serach for Posts by users whose name contains "olaf" (case insensitive)
--
-- > olafPosts <- 
-- >    query @Post  
--      |> innerJoin @User (#createdBy, #id) 
--      |> filterWhereILikeJoinedTable @User (#name, "%Olaf%")
-- > -- SELECT posts.* FROM posts INNER JOIN users ON posts.created_by = users.id WHERE users.name ILIKE '%Olaf%' 
filterWhereILikeJoinedTable :: forall model table name table' model' value queryBuilderProvider joinRegister. (KnownSymbol table, KnownSymbol name, ToField value, HasField name model value, table ~ GetTableName model, model' ~ GetModelByTableName table', HasQueryBuilder queryBuilderProvider joinRegister, IsJoined model joinRegister, Table model) => (Proxy name, value) -> queryBuilderProvider table' -> queryBuilderProvider table'
filterWhereILikeJoinedTable :: forall {k} model (table :: Symbol) (name :: Symbol)
       (table' :: Symbol) model' value
       (queryBuilderProvider :: Symbol -> *) (joinRegister :: k).
(KnownSymbol table, KnownSymbol name, ToField value,
 HasField name model value, table ~ GetTableName model,
 model' ~ GetModelByTableName table',
 HasQueryBuilder queryBuilderProvider joinRegister,
 IsJoined model joinRegister, Table model) =>
(Proxy name, value)
-> queryBuilderProvider table' -> queryBuilderProvider table'
filterWhereILikeJoinedTable (Proxy name
name, value
value) queryBuilderProvider table'
queryBuilderProvider = QueryBuilder table' -> queryBuilderProvider table'
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
QueryBuilder table -> queryBuilderProvider table
forall (table :: Symbol).
QueryBuilder table -> queryBuilderProvider table
injectQueryBuilder FilterByQueryBuilder { QueryBuilder table'
queryBuilder :: QueryBuilder table'
queryBuilder :: QueryBuilder table'
queryBuilder, queryFilter :: (ByteString, FilterOperator, Action)
queryFilter = (ByteString
columnName, MatchSensitivity -> FilterOperator
LikeOp MatchSensitivity
CaseInsensitive, value -> Action
forall a. ToField a => a -> Action
toField value
value), applyLeft :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, applyRight :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing }
    where
        columnName :: ByteString
columnName = forall record. Table record => ByteString
tableNameByteString @model ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"." ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Text -> ByteString
Text.encodeUtf8 (Text -> Text
fieldNameToColumnName (forall (symbol :: Symbol). KnownSymbol symbol => Text
symbolToText @name))
        queryBuilder :: QueryBuilder table'
queryBuilder = queryBuilderProvider table' -> QueryBuilder table'
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
queryBuilderProvider table -> QueryBuilder table
forall (table :: Symbol).
queryBuilderProvider table -> QueryBuilder table
getQueryBuilder queryBuilderProvider table'
queryBuilderProvider
{-# INLINE filterWhereILikeJoinedTable #-}


-- | Adds a @WHERE x ~ y@ condition to the query.
--
-- __Example:__ Find names with titles in front.
--
-- > articles <- query @User
-- >     |> filterWhereMatches (#name, "^(M(rs|r|iss)|Dr|Sir). ")
-- >     |> fetch
-- > -- SELECT * FROM articles WHERE title ~ '^(M(rs|r|iss)|Dr|Sir). '
filterWhereMatches :: forall name table model value queryBuilderProvider joinRegister. (KnownSymbol table, KnownSymbol name, ToField value, HasField name model value, table ~ GetTableName model, HasQueryBuilder queryBuilderProvider joinRegister, Table model) => (Proxy name, value) -> queryBuilderProvider table -> queryBuilderProvider table
filterWhereMatches :: forall {k} (name :: Symbol) (table :: Symbol) model value
       (queryBuilderProvider :: Symbol -> *) (joinRegister :: k).
(KnownSymbol table, KnownSymbol name, ToField value,
 HasField name model value, table ~ GetTableName model,
 HasQueryBuilder queryBuilderProvider joinRegister, Table model) =>
(Proxy name, value)
-> queryBuilderProvider table -> queryBuilderProvider table
filterWhereMatches (Proxy name
name, value
value) queryBuilderProvider table
queryBuilderProvider = QueryBuilder table -> queryBuilderProvider table
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
QueryBuilder table -> queryBuilderProvider table
forall (table :: Symbol).
QueryBuilder table -> queryBuilderProvider table
injectQueryBuilder FilterByQueryBuilder { QueryBuilder table
queryBuilder :: QueryBuilder table
queryBuilder :: QueryBuilder table
queryBuilder, queryFilter :: (ByteString, FilterOperator, Action)
queryFilter = (ByteString
columnName, MatchSensitivity -> FilterOperator
MatchesOp MatchSensitivity
CaseSensitive, value -> Action
forall a. ToField a => a -> Action
toField value
value), applyLeft :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, applyRight :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing }
    where
        columnName :: ByteString
columnName = forall record. Table record => ByteString
tableNameByteString @model ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"." ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Text -> ByteString
Text.encodeUtf8 (Text -> Text
fieldNameToColumnName (forall (symbol :: Symbol). KnownSymbol symbol => Text
symbolToText @name))
        queryBuilder :: QueryBuilder table
queryBuilder = queryBuilderProvider table -> QueryBuilder table
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
queryBuilderProvider table -> QueryBuilder table
forall (table :: Symbol).
queryBuilderProvider table -> QueryBuilder table
getQueryBuilder queryBuilderProvider table
queryBuilderProvider
{-# INLINE filterWhereMatches #-}

-- | Adds a @WHERE x ~ y@ condition to the query, where the column x is held by a joined table.
--
-- __Example:__ Find Posts by people with names with titles in front.
--
-- > articles <- query @Post
-- >     |> innerJoin @User (#createdBy, #id)
-- >     |> filterWhereMatchesJoinedTable (#title, "^(M(rs|r|iss|s)|Dr|Sir). ")
-- >     |> fetch
-- > -- SELECT posts.* FROM posts INNER JOIN users ON posts.created_by = users.id WHERE users.title ~ '^(M(rs|r|iss|s)|Dr|Sir). '

filterWhereMatchesJoinedTable :: forall model table name value queryBuilderProvider joinRegister table'. (KnownSymbol table, KnownSymbol name, ToField value, HasField name model value, table ~ GetTableName model, HasQueryBuilder queryBuilderProvider joinRegister, IsJoined model joinRegister, Table model) => (Proxy name, value) -> queryBuilderProvider table' -> queryBuilderProvider table'
filterWhereMatchesJoinedTable :: forall {k} model (table :: Symbol) (name :: Symbol) value
       (queryBuilderProvider :: Symbol -> *) (joinRegister :: k)
       (table' :: Symbol).
(KnownSymbol table, KnownSymbol name, ToField value,
 HasField name model value, table ~ GetTableName model,
 HasQueryBuilder queryBuilderProvider joinRegister,
 IsJoined model joinRegister, Table model) =>
(Proxy name, value)
-> queryBuilderProvider table' -> queryBuilderProvider table'
filterWhereMatchesJoinedTable (Proxy name
name, value
value) queryBuilderProvider table'
queryBuilderProvider = QueryBuilder table' -> queryBuilderProvider table'
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
QueryBuilder table -> queryBuilderProvider table
forall (table :: Symbol).
QueryBuilder table -> queryBuilderProvider table
injectQueryBuilder FilterByQueryBuilder { QueryBuilder table'
queryBuilder :: QueryBuilder table'
queryBuilder :: QueryBuilder table'
queryBuilder, queryFilter :: (ByteString, FilterOperator, Action)
queryFilter = (ByteString
columnName, MatchSensitivity -> FilterOperator
MatchesOp MatchSensitivity
CaseSensitive, value -> Action
forall a. ToField a => a -> Action
toField value
value), applyLeft :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, applyRight :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing }
    where
        columnName :: ByteString
columnName = forall record. Table record => ByteString
tableNameByteString @model ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"." ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Text -> ByteString
Text.encodeUtf8 (Text -> Text
fieldNameToColumnName (forall (symbol :: Symbol). KnownSymbol symbol => Text
symbolToText @name))
        queryBuilder :: QueryBuilder table'
queryBuilder = queryBuilderProvider table' -> QueryBuilder table'
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
queryBuilderProvider table -> QueryBuilder table
forall (table :: Symbol).
queryBuilderProvider table -> QueryBuilder table
getQueryBuilder queryBuilderProvider table'
queryBuilderProvider
{-# INLINE filterWhereMatchesJoinedTable #-}


-- | Adds a @WHERE x ~* y@ condition to the query. Case-insensitive version of 'filterWhereMatches'.
filterWhereIMatches :: forall name table model value queryBuilderProvider joinRegister. (KnownSymbol table, KnownSymbol name, ToField value, HasField name model value, model ~ GetModelByTableName table, HasQueryBuilder queryBuilderProvider joinRegister, Table model) => (Proxy name, value) -> queryBuilderProvider table -> queryBuilderProvider table
filterWhereIMatches :: forall {k} (name :: Symbol) (table :: Symbol) model value
       (queryBuilderProvider :: Symbol -> *) (joinRegister :: k).
(KnownSymbol table, KnownSymbol name, ToField value,
 HasField name model value, model ~ GetModelByTableName table,
 HasQueryBuilder queryBuilderProvider joinRegister, Table model) =>
(Proxy name, value)
-> queryBuilderProvider table -> queryBuilderProvider table
filterWhereIMatches (Proxy name
name, value
value) queryBuilderProvider table
queryBuilderProvider = QueryBuilder table -> queryBuilderProvider table
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
QueryBuilder table -> queryBuilderProvider table
forall (table :: Symbol).
QueryBuilder table -> queryBuilderProvider table
injectQueryBuilder FilterByQueryBuilder { QueryBuilder table
queryBuilder :: QueryBuilder table
queryBuilder :: QueryBuilder table
queryBuilder, queryFilter :: (ByteString, FilterOperator, Action)
queryFilter = (ByteString
columnName, MatchSensitivity -> FilterOperator
MatchesOp MatchSensitivity
CaseInsensitive, value -> Action
forall a. ToField a => a -> Action
toField value
value), applyLeft :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, applyRight :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing }
    where
        columnName :: ByteString
columnName = forall record. Table record => ByteString
tableNameByteString @model ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"." ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Text -> ByteString
Text.encodeUtf8 (Text -> Text
fieldNameToColumnName (forall (symbol :: Symbol). KnownSymbol symbol => Text
symbolToText @name))
        queryBuilder :: QueryBuilder table
queryBuilder = queryBuilderProvider table -> QueryBuilder table
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
queryBuilderProvider table -> QueryBuilder table
forall (table :: Symbol).
queryBuilderProvider table -> QueryBuilder table
getQueryBuilder queryBuilderProvider table
queryBuilderProvider
{-# INLINE filterWhereIMatches #-}

-- | Case-insensitive version of 'filterWhereMatchesJoinedTable'
filterWhereIMatchesJoinedTable :: forall model table name value queryBuilderProvider joinRegister table'. (KnownSymbol table, KnownSymbol name, ToField value, HasField name model value, table ~ GetTableName model, HasQueryBuilder queryBuilderProvider joinRegister, IsJoined model joinRegister, Table model) => (Proxy name, value) -> queryBuilderProvider table' -> queryBuilderProvider table'
filterWhereIMatchesJoinedTable :: forall {k} model (table :: Symbol) (name :: Symbol) value
       (queryBuilderProvider :: Symbol -> *) (joinRegister :: k)
       (table' :: Symbol).
(KnownSymbol table, KnownSymbol name, ToField value,
 HasField name model value, table ~ GetTableName model,
 HasQueryBuilder queryBuilderProvider joinRegister,
 IsJoined model joinRegister, Table model) =>
(Proxy name, value)
-> queryBuilderProvider table' -> queryBuilderProvider table'
filterWhereIMatchesJoinedTable (Proxy name
name, value
value) queryBuilderProvider table'
queryBuilderProvider = QueryBuilder table' -> queryBuilderProvider table'
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
QueryBuilder table -> queryBuilderProvider table
forall (table :: Symbol).
QueryBuilder table -> queryBuilderProvider table
injectQueryBuilder FilterByQueryBuilder { QueryBuilder table'
queryBuilder :: QueryBuilder table'
queryBuilder :: QueryBuilder table'
queryBuilder, queryFilter :: (ByteString, FilterOperator, Action)
queryFilter = (ByteString
columnName, MatchSensitivity -> FilterOperator
MatchesOp MatchSensitivity
CaseInsensitive, value -> Action
forall a. ToField a => a -> Action
toField value
value), applyLeft :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, applyRight :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing }
    where
        columnName :: ByteString
columnName = forall record. Table record => ByteString
tableNameByteString @model ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"." ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Text -> ByteString
Text.encodeUtf8 (Text -> Text
fieldNameToColumnName (forall (symbol :: Symbol). KnownSymbol symbol => Text
symbolToText @name))
        queryBuilder :: QueryBuilder table'
queryBuilder = queryBuilderProvider table' -> QueryBuilder table'
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
queryBuilderProvider table -> QueryBuilder table
forall (table :: Symbol).
queryBuilderProvider table -> QueryBuilder table
getQueryBuilder queryBuilderProvider table'
queryBuilderProvider
{-# INLINE filterWhereIMatchesJoinedTable #-}


-- | Filter all rows by whether a field is in the past, determined by comparing 'NOW()' to the field's value.
--
-- Opposite of 'filterWhereFuture'
--
-- __Example:__ Fetch all posts scheduled for the past.
--
-- > publicPosts <- query @Post
-- >     |> filterWherePast #scheduledAt
-- >     |> fetch
-- > -- SELECT * FROM posts WHERE scheduled_at <= NOW()
filterWherePast
    :: ( KnownSymbol table
       , KnownSymbol name
       , ToField value
       , HasField name (GetModelByTableName table) value
       , HasQueryBuilder queryBuilderProvider joinRegister
       , Table (GetModelByTableName table)
       )
    => Proxy name -> queryBuilderProvider table -> queryBuilderProvider table
filterWherePast :: forall {k} (table :: Symbol) (name :: Symbol) value
       (queryBuilderProvider :: Symbol -> *) (joinRegister :: k).
(KnownSymbol table, KnownSymbol name, ToField value,
 HasField name (GetModelByTableName table) value,
 HasQueryBuilder queryBuilderProvider joinRegister,
 Table (GetModelByTableName table)) =>
Proxy name
-> queryBuilderProvider table -> queryBuilderProvider table
filterWherePast Proxy name
name = (Proxy name, ByteString)
-> queryBuilderProvider table -> queryBuilderProvider table
forall {k} (name :: Symbol) (table :: Symbol) model value
       (queryBuilderProvider :: Symbol -> *) (joinRegister :: k).
(KnownSymbol table, KnownSymbol name, ToField value,
 HasField name model value, model ~ GetModelByTableName table,
 HasQueryBuilder queryBuilderProvider joinRegister, Table model) =>
(Proxy name, ByteString)
-> queryBuilderProvider table -> queryBuilderProvider table
filterWhereSql (Proxy name
name, ByteString
"<= NOW()")
{-# INLINE filterWherePast #-}

-- | Filter all rows by whether a field is in the future, determined by comparing 'NOW()' to the field's value.
--
-- Opposite of 'filterWherePast'
--
-- __Example:__ Fetch all posts scheduled for the future.
--
-- > hiddenPosts <- query @Post
-- >     |> filterWhereFuture #scheduledAt
-- >     |> fetch
-- > -- SELECT * FROM posts WHERE scheduled_at > NOW()
filterWhereFuture
    :: ( KnownSymbol table
       , KnownSymbol name
       , ToField value
       , HasField name (GetModelByTableName table) value
       , HasQueryBuilder queryBuilderProvider joinRegister
       , Table (GetModelByTableName table)
       )
    => Proxy name -> queryBuilderProvider table -> queryBuilderProvider table
filterWhereFuture :: forall {k} (table :: Symbol) (name :: Symbol) value
       (queryBuilderProvider :: Symbol -> *) (joinRegister :: k).
(KnownSymbol table, KnownSymbol name, ToField value,
 HasField name (GetModelByTableName table) value,
 HasQueryBuilder queryBuilderProvider joinRegister,
 Table (GetModelByTableName table)) =>
Proxy name
-> queryBuilderProvider table -> queryBuilderProvider table
filterWhereFuture Proxy name
name = (Proxy name, ByteString)
-> queryBuilderProvider table -> queryBuilderProvider table
forall {k} (name :: Symbol) (table :: Symbol) model value
       (queryBuilderProvider :: Symbol -> *) (joinRegister :: k).
(KnownSymbol table, KnownSymbol name, ToField value,
 HasField name model value, model ~ GetModelByTableName table,
 HasQueryBuilder queryBuilderProvider joinRegister, Table model) =>
(Proxy name, ByteString)
-> queryBuilderProvider table -> queryBuilderProvider table
filterWhereSql (Proxy name
name, ByteString
"> NOW()")
{-# INLINE filterWhereFuture #-}


-- | Allows to add a custom raw sql where condition
--
-- If your query cannot be represented with 'filterWhereSql', take a look at 'IHP.ModelSupport.sqlQuery'.
--
-- __Example:__ Fetching all projects created in the last 24 hours.
--
-- > latestProjects <- query @Project
-- >     |> filterWhereSql (#startedAt, "< current_timestamp - interval '1 day'")
-- >     |> fetch
-- > -- SELECT * FROM projects WHERE started_at < current_timestamp - interval '1 day'
--
filterWhereSql :: forall name table model value queryBuilderProvider joinRegister. (KnownSymbol table, KnownSymbol name, ToField value, HasField name model value, model ~ GetModelByTableName table, HasQueryBuilder queryBuilderProvider joinRegister, Table model) => (Proxy name, ByteString) -> queryBuilderProvider table -> queryBuilderProvider table
filterWhereSql :: forall {k} (name :: Symbol) (table :: Symbol) model value
       (queryBuilderProvider :: Symbol -> *) (joinRegister :: k).
(KnownSymbol table, KnownSymbol name, ToField value,
 HasField name model value, model ~ GetModelByTableName table,
 HasQueryBuilder queryBuilderProvider joinRegister, Table model) =>
(Proxy name, ByteString)
-> queryBuilderProvider table -> queryBuilderProvider table
filterWhereSql (Proxy name
name, ByteString
sqlCondition) queryBuilderProvider table
queryBuilderProvider = QueryBuilder table -> queryBuilderProvider table
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
QueryBuilder table -> queryBuilderProvider table
forall (table :: Symbol).
QueryBuilder table -> queryBuilderProvider table
injectQueryBuilder FilterByQueryBuilder { QueryBuilder table
queryBuilder :: QueryBuilder table
queryBuilder :: QueryBuilder table
queryBuilder, queryFilter :: (ByteString, FilterOperator, Action)
queryFilter = (ByteString
columnName, FilterOperator
SqlOp, Builder -> Action
Plain (ByteString -> Builder
Builder.byteString ByteString
sqlCondition)), applyLeft :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, applyRight :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing }
    where
        columnName :: ByteString
columnName = forall record. Table record => ByteString
tableNameByteString @model ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"." ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Text -> ByteString
Text.encodeUtf8 (Text -> Text
fieldNameToColumnName (forall (symbol :: Symbol). KnownSymbol symbol => Text
symbolToText @name))
        queryBuilder :: QueryBuilder table
queryBuilder = queryBuilderProvider table -> QueryBuilder table
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
queryBuilderProvider table -> QueryBuilder table
forall (table :: Symbol).
queryBuilderProvider table -> QueryBuilder table
getQueryBuilder queryBuilderProvider table
queryBuilderProvider
{-# INLINE filterWhereSql #-}

-- | Adds a @WHERE LOWER(x) = LOWER(y)@ condition to the query.
--
-- __Example:__ Get a user by an email address, ignoring case
--
-- > user <- query @User
-- >     |> filterWhereCaseInsensitive (#email, "marc@digitallyinduced.com")
-- >     |> fetchOne
-- > -- SELECT * FROM users WHERE LOWER(email) = 'marc@digitallyinduced.com'
--
-- For high performance it's best to have an index for @LOWER(field)@ in your Schema.sql
--
-- >>> CREATE UNIQUE INDEX users_email_index ON users ((LOWER(email)));
--
filterWhereCaseInsensitive :: forall name table model value queryBuilderProvider joinRegister. (KnownSymbol table, KnownSymbol name, ToField value, HasField name model value, EqOrIsOperator value, model ~ GetModelByTableName table, HasQueryBuilder queryBuilderProvider joinRegister, Table model) => (Proxy name, value) -> queryBuilderProvider table -> queryBuilderProvider table
filterWhereCaseInsensitive :: forall {k} (name :: Symbol) (table :: Symbol) model value
       (queryBuilderProvider :: Symbol -> *) (joinRegister :: k).
(KnownSymbol table, KnownSymbol name, ToField value,
 HasField name model value, EqOrIsOperator value,
 model ~ GetModelByTableName table,
 HasQueryBuilder queryBuilderProvider joinRegister, Table model) =>
(Proxy name, value)
-> queryBuilderProvider table -> queryBuilderProvider table
filterWhereCaseInsensitive (Proxy name
name, value
value) queryBuilderProvider table
queryBuilderProvider = QueryBuilder table -> queryBuilderProvider table
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
QueryBuilder table -> queryBuilderProvider table
forall (table :: Symbol).
QueryBuilder table -> queryBuilderProvider table
injectQueryBuilder FilterByQueryBuilder { QueryBuilder table
queryBuilder :: QueryBuilder table
queryBuilder :: QueryBuilder table
queryBuilder, queryFilter :: (ByteString, FilterOperator, Action)
queryFilter = (ByteString
columnName, value -> FilterOperator
forall value. EqOrIsOperator value => value -> FilterOperator
toEqOrIsOperator value
value, value -> Action
forall a. ToField a => a -> Action
toField value
value), applyLeft :: Maybe ByteString
applyLeft = ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just ByteString
"LOWER", applyRight :: Maybe ByteString
applyRight = ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just ByteString
"LOWER" }
    where
        columnName :: ByteString
columnName = forall record. Table record => ByteString
tableNameByteString @model ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"." ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Text -> ByteString
Text.encodeUtf8 (Text -> Text
fieldNameToColumnName (forall (symbol :: Symbol). KnownSymbol symbol => Text
symbolToText @name))
        queryBuilder :: QueryBuilder table
queryBuilder = queryBuilderProvider table -> QueryBuilder table
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
queryBuilderProvider table -> QueryBuilder table
forall (table :: Symbol).
queryBuilderProvider table -> QueryBuilder table
getQueryBuilder queryBuilderProvider table
queryBuilderProvider
{-# INLINE filterWhereCaseInsensitive #-}


filterWhereIdIn :: forall table model queryBuilderProvider (joinRegister :: Type). (KnownSymbol table, Table model, model ~ GetModelByTableName table, HasQueryBuilder queryBuilderProvider joinRegister) => [Id model] -> queryBuilderProvider table -> queryBuilderProvider table
filterWhereIdIn :: forall (table :: Symbol) model
       (queryBuilderProvider :: Symbol -> *) joinRegister.
(KnownSymbol table, Table model, model ~ GetModelByTableName table,
 HasQueryBuilder queryBuilderProvider joinRegister) =>
[Id model]
-> queryBuilderProvider table -> queryBuilderProvider table
filterWhereIdIn [Id model]
values queryBuilderProvider table
queryBuilderProvider =
    -- We don't need to treat null values differently here, because primary keys imply not-null
    let
        pkConditions :: [Action]
pkConditions = (Id model -> Action) -> [Id model] -> [Action]
forall a b. (a -> b) -> [a] -> [b]
map (forall record. Table record => Id record -> Action
primaryKeyConditionForId @model) [Id model]
values

        queryBuilder :: QueryBuilder table
queryBuilder = queryBuilderProvider table -> QueryBuilder table
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
queryBuilderProvider table -> QueryBuilder table
forall (table :: Symbol).
queryBuilderProvider table -> QueryBuilder table
getQueryBuilder queryBuilderProvider table
queryBuilderProvider

        whereInQuery :: QueryBuilder table
whereInQuery = FilterByQueryBuilder {QueryBuilder table
queryBuilder :: QueryBuilder table
queryBuilder :: QueryBuilder table
queryBuilder, queryFilter :: (ByteString, FilterOperator, Action)
queryFilter = (forall record. Table record => ByteString
primaryKeyConditionColumnSelector @model, FilterOperator
InOp, In [Action] -> Action
forall a. ToField a => a -> Action
toField ([Action] -> In [Action]
forall a. a -> In a
In [Action]
pkConditions)), applyLeft :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, applyRight :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing}
     in
        QueryBuilder table -> queryBuilderProvider table
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
QueryBuilder table -> queryBuilderProvider table
forall (table :: Symbol).
QueryBuilder table -> queryBuilderProvider table
injectQueryBuilder QueryBuilder table
whereInQuery
{-# INLINE filterWhereIdIn #-}


-- | Joins a table to an existing QueryBuilder (or something holding a QueryBuilder) on the specified columns. Example:
-- >    query @Posts 
-- > |> innerJoin @Users (#author, #id)
-- > -- SELECT users.* FROM users INNER JOIN posts ON users.id = posts.author ...
innerJoin :: forall model' table' name' value' model table name value queryBuilderProvider joinRegister.
                            (
                                KnownSymbol name, 
                                KnownSymbol table,
                                HasField name model value,
                                KnownSymbol name', 
                                KnownSymbol table',
                                HasQueryBuilder queryBuilderProvider joinRegister,
                                ModelList joinRegister,
                                HasField name' model' value', 
                                value ~ value',
                                model ~ GetModelByTableName table,
                                table' ~ GetTableName model'
                            ) => (Proxy name, Proxy name') -> queryBuilderProvider table -> JoinQueryBuilderWrapper (ConsModelList model' joinRegister) table 
innerJoin :: forall {k} model' (table' :: Symbol) (name' :: Symbol) value' model
       (table :: Symbol) (name :: Symbol) value
       (queryBuilderProvider :: Symbol -> *) (joinRegister :: k).
(KnownSymbol name, KnownSymbol table, HasField name model value,
 KnownSymbol name', KnownSymbol table',
 HasQueryBuilder queryBuilderProvider joinRegister,
 ModelList joinRegister, HasField name' model' value',
 value ~ value', model ~ GetModelByTableName table,
 table' ~ GetTableName model') =>
(Proxy name, Proxy name')
-> queryBuilderProvider table
-> JoinQueryBuilderWrapper
     (ConsModelList model' joinRegister) table
innerJoin (Proxy name
name, Proxy name'
name') queryBuilderProvider table
queryBuilderProvider = QueryBuilder table
-> JoinQueryBuilderWrapper
     (ConsModelList model' joinRegister) table
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
QueryBuilder table -> queryBuilderProvider table
forall (table :: Symbol).
QueryBuilder table
-> JoinQueryBuilderWrapper
     (ConsModelList model' joinRegister) table
injectQueryBuilder (QueryBuilder table
 -> JoinQueryBuilderWrapper
      (ConsModelList model' joinRegister) table)
-> QueryBuilder table
-> JoinQueryBuilderWrapper
     (ConsModelList model' joinRegister) table
forall a b. (a -> b) -> a -> b
$ QueryBuilder table -> Join -> QueryBuilder table
forall (table :: Symbol).
QueryBuilder table -> Join -> QueryBuilder table
JoinQueryBuilder (queryBuilderProvider table -> QueryBuilder table
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
queryBuilderProvider table -> QueryBuilder table
forall (table :: Symbol).
queryBuilderProvider table -> QueryBuilder table
getQueryBuilder queryBuilderProvider table
queryBuilderProvider) (Join -> QueryBuilder table) -> Join -> QueryBuilder table
forall a b. (a -> b) -> a -> b
$ ByteString -> ByteString -> ByteString -> Join
Join ByteString
joinTableName ByteString
leftJoinColumn ByteString
rightJoinColumn 
    where 
        baseTableName :: ByteString
baseTableName = forall (symbol :: Symbol). KnownSymbol symbol => ByteString
symbolToByteString @table
        joinTableName :: ByteString
joinTableName = forall (symbol :: Symbol). KnownSymbol symbol => ByteString
symbolToByteString @table'
        leftJoinColumn :: ByteString
leftJoinColumn = ByteString
baseTableName ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"." ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> (Text -> ByteString
Text.encodeUtf8 (Text -> ByteString) -> (Text -> Text) -> Text -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Text -> Text
fieldNameToColumnName) (forall (symbol :: Symbol). KnownSymbol symbol => Text
symbolToText @name)
        rightJoinColumn :: ByteString
rightJoinColumn = (Text -> ByteString
Text.encodeUtf8 (Text -> ByteString) -> (Text -> Text) -> Text -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Text -> Text
fieldNameToColumnName) (forall (symbol :: Symbol). KnownSymbol symbol => Text
symbolToText @name')
{-# INLINE innerJoin #-}

-- | Index the values from a table with values of a field from a table joined by 'innerJoin' or 'innerJoinThirdTable'. Useful to get, e.g., the tags to a set of posts in such a way that the assignment of tags to posts is preserved.
--
--
-- __Example:__ Fetch a list of all comments, each paired with the id of the post it belongs to.
--
-- > labeledTags <-
-- >  query @Tag
-- >     |> innerJoin @Tagging (#id, #tagId)
-- >     |> innerJoinThirdTable @Post @Tagging (#id, #postId)
-- >     |> labelResults @Post #id
-- >     |> fetch
-- > -- SELECT posts.id, tags.* FROM comments INNER JOIN taggings ON tags.id = taggings.tagId INNER JOIN posts ON posts.id = taggings.postId 
-- 
-- labeledTags is then a list of type ['LabeledData' (Id' "posts") Tag] such that "LabeledData postId tag" is contained in that list if "tag" is a tag of the post with id postId.
--
labelResults :: forall foreignModel baseModel foreignTable baseTable name value queryBuilderProvider joinRegister.
                (
                    KnownSymbol foreignTable,
                    KnownSymbol baseTable,
                    foreignTable ~ GetTableName foreignModel, 
                    baseModel ~ GetModelByTableName baseTable,
                    HasField name foreignModel value,
                    HasQueryBuilder queryBuilderProvider joinRegister,
                    KnownSymbol name,
                    IsJoined foreignModel joinRegister
                ) => Proxy name -> queryBuilderProvider baseTable -> LabeledQueryBuilderWrapper foreignTable name value baseTable
labelResults :: forall {k} foreignModel baseModel (foreignTable :: Symbol)
       (baseTable :: Symbol) (name :: Symbol) value
       (queryBuilderProvider :: Symbol -> *) (joinRegister :: k).
(KnownSymbol foreignTable, KnownSymbol baseTable,
 foreignTable ~ GetTableName foreignModel,
 baseModel ~ GetModelByTableName baseTable,
 HasField name foreignModel value,
 HasQueryBuilder queryBuilderProvider joinRegister,
 KnownSymbol name, IsJoined foreignModel joinRegister) =>
Proxy name
-> queryBuilderProvider baseTable
-> LabeledQueryBuilderWrapper foreignTable name value baseTable
labelResults Proxy name
name queryBuilderProvider baseTable
queryBuilderProvider = QueryBuilder baseTable
-> LabeledQueryBuilderWrapper foreignTable name value baseTable
forall {k} {k} {k} (foreignTable :: k) (indexColumn :: k)
       (indexValue :: k) (table :: Symbol).
QueryBuilder table
-> LabeledQueryBuilderWrapper
     foreignTable indexColumn indexValue table
LabeledQueryBuilderWrapper (QueryBuilder baseTable
 -> LabeledQueryBuilderWrapper foreignTable name value baseTable)
-> QueryBuilder baseTable
-> LabeledQueryBuilderWrapper foreignTable name value baseTable
forall a b. (a -> b) -> a -> b
$ queryBuilderProvider baseTable -> QueryBuilder baseTable
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
queryBuilderProvider table -> QueryBuilder table
forall (table :: Symbol).
queryBuilderProvider table -> QueryBuilder table
getQueryBuilder queryBuilderProvider baseTable
queryBuilderProvider
                    
-- | Joins a table on a column held by a previously joined table. Example:
-- > query @Posts 
-- > |> innerJoin @Users (#author, #id)
-- > |> innerJoinThirdTable @City @Users (#id, #homeTown)
-- > -- SELECT posts.* FROM posts INNER JOIN users ON posts.author = users.id INNER JOIN cities ON user.home_town = cities.id
--
innerJoinThirdTable :: forall model model' name name' value value' table table' baseTable baseModel queryBuilderProvider joinRegister.
                        ( 
                            KnownSymbol name,
                            KnownSymbol table,
                            HasField name model value,
                            KnownSymbol name',
                            KnownSymbol table',
                            HasQueryBuilder queryBuilderProvider joinRegister,
                            ModelList joinRegister,
                            HasField name' model' value',
                            value ~ value',
                            table ~ GetTableName model,
                            table' ~ GetTableName model',
                            baseModel ~ GetModelByTableName baseTable 
                        ) => (Proxy name, Proxy name') -> queryBuilderProvider baseTable -> JoinQueryBuilderWrapper (ConsModelList model joinRegister) baseTable
innerJoinThirdTable :: forall {k} model model' (name :: Symbol) (name' :: Symbol) value
       value' (table :: Symbol) (table' :: Symbol) (baseTable :: Symbol)
       baseModel (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k).
(KnownSymbol name, KnownSymbol table, HasField name model value,
 KnownSymbol name', KnownSymbol table',
 HasQueryBuilder queryBuilderProvider joinRegister,
 ModelList joinRegister, HasField name' model' value',
 value ~ value', table ~ GetTableName model,
 table' ~ GetTableName model',
 baseModel ~ GetModelByTableName baseTable) =>
(Proxy name, Proxy name')
-> queryBuilderProvider baseTable
-> JoinQueryBuilderWrapper
     (ConsModelList model joinRegister) baseTable
innerJoinThirdTable (Proxy name
name, Proxy name'
name') queryBuilderProvider baseTable
queryBuilderProvider = QueryBuilder baseTable
-> JoinQueryBuilderWrapper
     (ConsModelList model joinRegister) baseTable
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
QueryBuilder table -> queryBuilderProvider table
forall (table :: Symbol).
QueryBuilder table
-> JoinQueryBuilderWrapper (ConsModelList model joinRegister) table
injectQueryBuilder (QueryBuilder baseTable
 -> JoinQueryBuilderWrapper
      (ConsModelList model joinRegister) baseTable)
-> QueryBuilder baseTable
-> JoinQueryBuilderWrapper
     (ConsModelList model joinRegister) baseTable
forall a b. (a -> b) -> a -> b
$ QueryBuilder baseTable -> Join -> QueryBuilder baseTable
forall (table :: Symbol).
QueryBuilder table -> Join -> QueryBuilder table
JoinQueryBuilder (queryBuilderProvider baseTable -> QueryBuilder baseTable
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
queryBuilderProvider table -> QueryBuilder table
forall (table :: Symbol).
queryBuilderProvider table -> QueryBuilder table
getQueryBuilder queryBuilderProvider baseTable
queryBuilderProvider) (Join -> QueryBuilder baseTable) -> Join -> QueryBuilder baseTable
forall a b. (a -> b) -> a -> b
$ ByteString -> ByteString -> ByteString -> Join
Join ByteString
joinTableName ByteString
leftJoinColumn ByteString
rightJoinColumn
     where 
        baseTableName :: ByteString
baseTableName = forall (symbol :: Symbol). KnownSymbol symbol => ByteString
symbolToByteString @table'
        joinTableName :: ByteString
joinTableName = forall (symbol :: Symbol). KnownSymbol symbol => ByteString
symbolToByteString @table
        leftJoinColumn :: ByteString
leftJoinColumn = ByteString
baseTableName ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"." ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> (Text -> ByteString
Text.encodeUtf8 (Text -> ByteString) -> (Text -> Text) -> Text -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Text -> Text
fieldNameToColumnName) (forall (symbol :: Symbol). KnownSymbol symbol => Text
symbolToText @name')
        rightJoinColumn :: ByteString
rightJoinColumn = (Text -> ByteString
Text.encodeUtf8 (Text -> ByteString) -> (Text -> Text) -> Text -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Text -> Text
fieldNameToColumnName) (forall (symbol :: Symbol). KnownSymbol symbol => Text
symbolToText @name)
{-# INLINE innerJoinThirdTable #-}
                       


-- | Adds an @ORDER BY .. ASC@ to your query.
--
-- Use 'orderByDesc' for descending order.
--
-- __Example:__ Fetch the 10 oldest books.
--
-- > query @Book
-- >     |> orderBy #createdAt -- >     |> limit 10
-- >     |> fetch
-- > -- SELECT * FROM books LIMIT 10 ORDER BY created_at ASC
orderByAsc :: forall name model table value queryBuilderProvider joinRegister. (KnownSymbol table, KnownSymbol name, HasField name model value, model ~ GetModelByTableName table, HasQueryBuilder queryBuilderProvider joinRegister, Table model) => Proxy name -> queryBuilderProvider table -> queryBuilderProvider table
orderByAsc :: forall {k} (name :: Symbol) model (table :: Symbol) value
       (queryBuilderProvider :: Symbol -> *) (joinRegister :: k).
(KnownSymbol table, KnownSymbol name, HasField name model value,
 model ~ GetModelByTableName table,
 HasQueryBuilder queryBuilderProvider joinRegister, Table model) =>
Proxy name
-> queryBuilderProvider table -> queryBuilderProvider table
orderByAsc !Proxy name
name queryBuilderProvider table
queryBuilderProvider = QueryBuilder table -> queryBuilderProvider table
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
QueryBuilder table -> queryBuilderProvider table
forall (table :: Symbol).
QueryBuilder table -> queryBuilderProvider table
injectQueryBuilder OrderByQueryBuilder { QueryBuilder table
queryBuilder :: QueryBuilder table
queryBuilder :: QueryBuilder table
queryBuilder, queryOrderByClause :: OrderByClause
queryOrderByClause = OrderByClause { orderByColumn :: ByteString
orderByColumn = ByteString
columnName, orderByDirection :: OrderByDirection
orderByDirection = OrderByDirection
Asc } }
    where
        columnName :: ByteString
columnName = forall record. Table record => ByteString
tableNameByteString @model ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"." ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Text -> ByteString
Text.encodeUtf8 (Text -> Text
fieldNameToColumnName (forall (symbol :: Symbol). KnownSymbol symbol => Text
symbolToText @name))
        queryBuilder :: QueryBuilder table
queryBuilder = queryBuilderProvider table -> QueryBuilder table
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
queryBuilderProvider table -> QueryBuilder table
forall (table :: Symbol).
queryBuilderProvider table -> QueryBuilder table
getQueryBuilder queryBuilderProvider table
queryBuilderProvider
{-# INLINE orderByAsc #-}

-- | Adds an @ORDER BY .. DESC@ to your query.
--
-- Use 'orderBy' for ascending order.
--
-- __Example:__ Fetch the 10 newest projects (ordered by creation time).
--
-- > query @Project
-- >     |> orderByDesc #createdAt
-- >     |> limit 10
-- >     |> fetch
-- > -- SELECT * FROM projects LIMIT 10 ORDER BY created_at DESC
orderByDesc :: forall name model table value queryBuilderProvider joinRegister. (KnownSymbol table, KnownSymbol name, HasField name model value, model ~ GetModelByTableName table, HasQueryBuilder queryBuilderProvider joinRegister, Table model) => Proxy name -> queryBuilderProvider table -> queryBuilderProvider table
orderByDesc :: forall {k} (name :: Symbol) model (table :: Symbol) value
       (queryBuilderProvider :: Symbol -> *) (joinRegister :: k).
(KnownSymbol table, KnownSymbol name, HasField name model value,
 model ~ GetModelByTableName table,
 HasQueryBuilder queryBuilderProvider joinRegister, Table model) =>
Proxy name
-> queryBuilderProvider table -> queryBuilderProvider table
orderByDesc !Proxy name
name queryBuilderProvider table
queryBuilderProvider = QueryBuilder table -> queryBuilderProvider table
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
QueryBuilder table -> queryBuilderProvider table
forall (table :: Symbol).
QueryBuilder table -> queryBuilderProvider table
injectQueryBuilder OrderByQueryBuilder { QueryBuilder table
queryBuilder :: QueryBuilder table
queryBuilder :: QueryBuilder table
queryBuilder, queryOrderByClause :: OrderByClause
queryOrderByClause = OrderByClause { orderByColumn :: ByteString
orderByColumn = ByteString
columnName, orderByDirection :: OrderByDirection
orderByDirection = OrderByDirection
Desc } }
    where
        columnName :: ByteString
columnName = forall record. Table record => ByteString
tableNameByteString @model ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"." ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Text -> ByteString
Text.encodeUtf8 (Text -> Text
fieldNameToColumnName (forall (symbol :: Symbol). KnownSymbol symbol => Text
symbolToText @name))
        queryBuilder :: QueryBuilder table
queryBuilder = queryBuilderProvider table -> QueryBuilder table
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
queryBuilderProvider table -> QueryBuilder table
forall (table :: Symbol).
queryBuilderProvider table -> QueryBuilder table
getQueryBuilder queryBuilderProvider table
queryBuilderProvider
{-# INLINE orderByDesc #-}

-- | Alias for 'orderByAsc'
orderBy :: (KnownSymbol table, KnownSymbol name, HasField name model value, model ~ GetModelByTableName table, HasQueryBuilder queryBuilderProvider joinRegister, Table model) => Proxy name -> queryBuilderProvider table -> queryBuilderProvider table
orderBy :: forall {k} (table :: Symbol) (name :: Symbol) model value
       (queryBuilderProvider :: Symbol -> *) (joinRegister :: k).
(KnownSymbol table, KnownSymbol name, HasField name model value,
 model ~ GetModelByTableName table,
 HasQueryBuilder queryBuilderProvider joinRegister, Table model) =>
Proxy name
-> queryBuilderProvider table -> queryBuilderProvider table
orderBy !Proxy name
name = Proxy name
-> queryBuilderProvider table -> queryBuilderProvider table
forall {k} (name :: Symbol) model (table :: Symbol) value
       (queryBuilderProvider :: Symbol -> *) (joinRegister :: k).
(KnownSymbol table, KnownSymbol name, HasField name model value,
 model ~ GetModelByTableName table,
 HasQueryBuilder queryBuilderProvider joinRegister, Table model) =>
Proxy name
-> queryBuilderProvider table -> queryBuilderProvider table
orderByAsc Proxy name
name
{-# INLINE orderBy #-}

-- | Adds an @LIMIT ..@ to your query.
--
--
-- __Example:__ Fetch 10 posts
--
-- > query @Post
-- >     |> limit 10
-- >     |> fetch
-- > -- SELECT * FROM posts LIMIT 10
limit :: (HasQueryBuilder queryBuilderProvider joinRegister) => Int -> queryBuilderProvider model -> queryBuilderProvider model
limit :: forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (model :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
Int -> queryBuilderProvider model -> queryBuilderProvider model
limit !Int
queryLimit queryBuilderProvider model
queryBuilderProvider = QueryBuilder model -> queryBuilderProvider model
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
QueryBuilder table -> queryBuilderProvider table
forall (table :: Symbol).
QueryBuilder table -> queryBuilderProvider table
injectQueryBuilder LimitQueryBuilder { QueryBuilder model
queryBuilder :: QueryBuilder model
queryBuilder :: QueryBuilder model
queryBuilder, Int
queryLimit :: Int
queryLimit :: Int
queryLimit }
    where
        queryBuilder :: QueryBuilder model
queryBuilder = queryBuilderProvider model -> QueryBuilder model
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
queryBuilderProvider table -> QueryBuilder table
forall (table :: Symbol).
queryBuilderProvider table -> QueryBuilder table
getQueryBuilder queryBuilderProvider model
queryBuilderProvider
{-# INLINE limit #-}

-- | Adds an @OFFSET ..@ to your query. Most often used together with @LIMIT...@
--
--
-- __Example:__ Fetch posts 10-20
--
-- > query @Post
-- >     |> limit 10
-- >     |> offset 10
-- >     |> fetch
-- > -- SELECT * FROM posts LIMIT 10 OFFSET 10
offset :: (HasQueryBuilder queryBuilderProvider joinRegister) => Int -> queryBuilderProvider model -> queryBuilderProvider model
offset :: forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (model :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
Int -> queryBuilderProvider model -> queryBuilderProvider model
offset !Int
queryOffset queryBuilderProvider model
queryBuilderProvider = QueryBuilder model -> queryBuilderProvider model
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
QueryBuilder table -> queryBuilderProvider table
forall (table :: Symbol).
QueryBuilder table -> queryBuilderProvider table
injectQueryBuilder OffsetQueryBuilder { QueryBuilder model
queryBuilder :: QueryBuilder model
queryBuilder :: QueryBuilder model
queryBuilder, Int
queryOffset :: Int
queryOffset :: Int
queryOffset }
    where
        queryBuilder :: QueryBuilder model
queryBuilder = queryBuilderProvider model -> QueryBuilder model
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
queryBuilderProvider table -> QueryBuilder table
forall (table :: Symbol).
queryBuilderProvider table -> QueryBuilder table
getQueryBuilder queryBuilderProvider model
queryBuilderProvider
{-# INLINE offset #-}

-- | Merges the results of two query builders.
--
-- Take a look at ‘queryOr'  as well, as this might be a bit shorter.
--
-- __Example:__ Return all pages owned by the user or owned by the users team.
--
-- > let userPages = query @Page |> filterWhere (#ownerId, currentUserId)
-- > let teamPages = query @Page |> filterWhere (#teamId, currentTeamId)
-- > pages <- queryUnion userPages teamPages |> fetch
-- > -- (SELECT * FROM pages WHERE owner_id = '..') UNION (SELECT * FROM pages WHERE team_id = '..')
queryUnion :: (HasQueryBuilder queryBuilderProvider joinRegister, HasQueryBuilder r joinRegister') => queryBuilderProvider model -> r model -> NoJoinQueryBuilderWrapper model
queryUnion :: forall {k} {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (r :: Symbol -> *) (joinRegister' :: k)
       (model :: Symbol).
(HasQueryBuilder queryBuilderProvider joinRegister,
 HasQueryBuilder r joinRegister') =>
queryBuilderProvider model
-> r model -> NoJoinQueryBuilderWrapper model
queryUnion queryBuilderProvider model
firstQueryBuilderProvider r model
secondQueryBuilderProvider = QueryBuilder model -> NoJoinQueryBuilderWrapper model
forall (table :: Symbol).
QueryBuilder table -> NoJoinQueryBuilderWrapper table
NoJoinQueryBuilderWrapper (UnionQueryBuilder { QueryBuilder model
firstQueryBuilder :: QueryBuilder model
firstQueryBuilder :: QueryBuilder model
firstQueryBuilder, QueryBuilder model
secondQueryBuilder :: QueryBuilder model
secondQueryBuilder :: QueryBuilder model
secondQueryBuilder })
    where
        firstQueryBuilder :: QueryBuilder model
firstQueryBuilder = queryBuilderProvider model -> QueryBuilder model
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
queryBuilderProvider table -> QueryBuilder table
forall (table :: Symbol).
queryBuilderProvider table -> QueryBuilder table
getQueryBuilder queryBuilderProvider model
firstQueryBuilderProvider
        secondQueryBuilder :: QueryBuilder model
secondQueryBuilder = r model -> QueryBuilder model
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
queryBuilderProvider table -> QueryBuilder table
forall (table :: Symbol). r table -> QueryBuilder table
getQueryBuilder r model
secondQueryBuilderProvider

    
{-# INLINE queryUnion #-}

-- | Like 'queryUnion', but applied on all the elements on the list
--
-- >  action ProjectsAction = do
-- >      let values :: [(ProjectType, Int)] = [(ProjectTypeOngoing, 3), (ProjectTypeNotStarted, 2)]
-- >
-- >          valuePairToCondition :: (ProjectType, Int) -> QueryBuilder "projects"
-- >          valuePairToCondition (projectType, participants) =
-- >              query @Project
-- >                  |> filterWhere (#projectType, projectType)
-- >                  |> filterWhere (#participants, participants)
-- >
-- >          theQuery = queryUnionList (map valuePairToCondition values)
-- >
-- >      projects <- fetch theQuery
-- >      render IndexView { .. }
queryUnionList :: forall table. (Table (GetModelByTableName table), KnownSymbol table, GetTableName (GetModelByTableName table) ~ table) => [QueryBuilder table] -> QueryBuilder table
queryUnionList :: forall (table :: Symbol).
(Table (GetModelByTableName table), KnownSymbol table,
 GetTableName (GetModelByTableName table) ~ table) =>
[QueryBuilder table] -> QueryBuilder table
queryUnionList [] = FilterByQueryBuilder { queryBuilder :: QueryBuilder table
queryBuilder = forall model (table :: Symbol).
(table ~ GetTableName model, Table model, DefaultScope table) =>
QueryBuilder table
query @(GetModelByTableName table) @table, queryFilter :: (ByteString, FilterOperator, Action)
queryFilter = (ByteString
"id", FilterOperator
NotEqOp, Builder -> Action
Plain Builder
"id"), applyLeft :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, applyRight :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing }
queryUnionList (QueryBuilder table
firstQueryBuilder:QueryBuilder table
secondQueryBuilder:[]) = UnionQueryBuilder { QueryBuilder table
firstQueryBuilder :: QueryBuilder table
firstQueryBuilder :: QueryBuilder table
firstQueryBuilder, QueryBuilder table
secondQueryBuilder :: QueryBuilder table
secondQueryBuilder :: QueryBuilder table
secondQueryBuilder }
queryUnionList (QueryBuilder table
firstQueryBuilder:[QueryBuilder table]
rest) = UnionQueryBuilder { QueryBuilder table
firstQueryBuilder :: QueryBuilder table
firstQueryBuilder :: QueryBuilder table
firstQueryBuilder, secondQueryBuilder :: QueryBuilder table
secondQueryBuilder = forall (table :: Symbol).
(Table (GetModelByTableName table), KnownSymbol table,
 GetTableName (GetModelByTableName table) ~ table) =>
[QueryBuilder table] -> QueryBuilder table
queryUnionList @table [QueryBuilder table]
rest }


-- | Adds an @a OR b@ condition
--
-- __Example:__ Return all pages owned by the user or public.
--
-- > query @Page
-- >     |> queryOr
-- >         (filterWhere (#createdBy, currentUserId))
-- >         (filterWhere (#public, True))
-- >     |> fetch
-- > -- SELECT * FROM pages WHERE created_by = '..' OR public = True
queryOr :: (HasQueryBuilder queryBuilderProvider joinRegister, HasQueryBuilder queryBuilderProvider'' joinRegister'', HasQueryBuilder queryBuilderProvider''' joinRegister''') => (queryBuilderProvider model -> queryBuilderProvider''' model) -> (queryBuilderProvider model -> queryBuilderProvider'' model) -> queryBuilderProvider model -> queryBuilderProvider model
queryOr :: forall {k} {k} {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (queryBuilderProvider'' :: Symbol -> *)
       (joinRegister'' :: k) (queryBuilderProvider''' :: Symbol -> *)
       (joinRegister''' :: k) (model :: Symbol).
(HasQueryBuilder queryBuilderProvider joinRegister,
 HasQueryBuilder queryBuilderProvider'' joinRegister'',
 HasQueryBuilder queryBuilderProvider''' joinRegister''') =>
(queryBuilderProvider model -> queryBuilderProvider''' model)
-> (queryBuilderProvider model -> queryBuilderProvider'' model)
-> queryBuilderProvider model
-> queryBuilderProvider model
queryOr queryBuilderProvider model -> queryBuilderProvider''' model
firstQuery queryBuilderProvider model -> queryBuilderProvider'' model
secondQuery queryBuilderProvider model
queryBuilder = QueryBuilder model -> queryBuilderProvider model
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
QueryBuilder table -> queryBuilderProvider table
forall (table :: Symbol).
QueryBuilder table -> queryBuilderProvider table
injectQueryBuilder
    (UnionQueryBuilder { 
        firstQueryBuilder :: QueryBuilder model
firstQueryBuilder = queryBuilderProvider''' model -> QueryBuilder model
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
queryBuilderProvider table -> QueryBuilder table
forall (table :: Symbol).
queryBuilderProvider''' table -> QueryBuilder table
getQueryBuilder (queryBuilderProvider''' model -> QueryBuilder model)
-> queryBuilderProvider''' model -> QueryBuilder model
forall a b. (a -> b) -> a -> b
$ queryBuilderProvider model -> queryBuilderProvider''' model
firstQuery queryBuilderProvider model
queryBuilder, 
        secondQueryBuilder :: QueryBuilder model
secondQueryBuilder = queryBuilderProvider'' model -> QueryBuilder model
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
queryBuilderProvider table -> QueryBuilder table
forall (table :: Symbol).
queryBuilderProvider'' table -> QueryBuilder table
getQueryBuilder (queryBuilderProvider'' model -> QueryBuilder model)
-> queryBuilderProvider'' model -> QueryBuilder model
forall a b. (a -> b) -> a -> b
$ queryBuilderProvider model -> queryBuilderProvider'' model
secondQuery queryBuilderProvider model
queryBuilder}
    )
{-# INLINE queryOr #-}

-- | Adds a @DISTINCT@ to your query.
--
-- Use 'distinct' to remove all duplicate rows from the result
--
-- __Example:__ Fetch distinct books
--
-- > query @Book
-- >     |> distinct
-- >     |> fetch
-- > -- SELECT DISTINCT * FROM books
distinct :: (HasQueryBuilder queryBuilderProvider joinRegister) => queryBuilderProvider table -> queryBuilderProvider table
distinct :: forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
queryBuilderProvider table -> queryBuilderProvider table
distinct = QueryBuilder table -> queryBuilderProvider table
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
QueryBuilder table -> queryBuilderProvider table
forall (table :: Symbol).
QueryBuilder table -> queryBuilderProvider table
injectQueryBuilder (QueryBuilder table -> queryBuilderProvider table)
-> (queryBuilderProvider table -> QueryBuilder table)
-> queryBuilderProvider table
-> queryBuilderProvider table
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. QueryBuilder table -> QueryBuilder table
forall (table :: Symbol). QueryBuilder table -> QueryBuilder table
DistinctQueryBuilder (QueryBuilder table -> QueryBuilder table)
-> (queryBuilderProvider table -> QueryBuilder table)
-> queryBuilderProvider table
-> QueryBuilder table
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. queryBuilderProvider table -> QueryBuilder table
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
queryBuilderProvider table -> QueryBuilder table
forall (table :: Symbol).
queryBuilderProvider table -> QueryBuilder table
getQueryBuilder
{-# INLINE distinct #-}

-- | Adds an @DISTINCT ON .. to your query.
--
-- Use 'distinctOn' to return a single row for each distinct value provided.
--
-- __Example:__ Fetch one book for each categoryId field
--
-- > query @Book
-- >     |> distinctOn #categoryId
-- >     |> fetch
-- > -- SELECT DISTINCT ON (category_id) * FROM books
distinctOn :: forall name model value table queryBuilderProvider joinRegister. (KnownSymbol table, KnownSymbol name, HasField name model value, model ~ GetModelByTableName table, HasQueryBuilder queryBuilderProvider joinRegister, Table model) => Proxy name -> queryBuilderProvider table -> queryBuilderProvider table
distinctOn :: forall {k} (name :: Symbol) model value (table :: Symbol)
       (queryBuilderProvider :: Symbol -> *) (joinRegister :: k).
(KnownSymbol table, KnownSymbol name, HasField name model value,
 model ~ GetModelByTableName table,
 HasQueryBuilder queryBuilderProvider joinRegister, Table model) =>
Proxy name
-> queryBuilderProvider table -> queryBuilderProvider table
distinctOn !Proxy name
name queryBuilderProvider table
queryBuilderProvider = QueryBuilder table -> queryBuilderProvider table
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
QueryBuilder table -> queryBuilderProvider table
forall (table :: Symbol).
QueryBuilder table -> queryBuilderProvider table
injectQueryBuilder DistinctOnQueryBuilder { distinctOnColumn :: ByteString
distinctOnColumn = ByteString
columnName, queryBuilder :: QueryBuilder table
queryBuilder = queryBuilderProvider table -> QueryBuilder table
forall {k} (queryBuilderProvider :: Symbol -> *)
       (joinRegister :: k) (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
queryBuilderProvider table -> QueryBuilder table
forall (table :: Symbol).
queryBuilderProvider table -> QueryBuilder table
getQueryBuilder queryBuilderProvider table
queryBuilderProvider}
    where
        columnName :: ByteString
columnName = forall record. Table record => ByteString
tableNameByteString @model ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"." ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Text -> ByteString
Text.encodeUtf8 (Text -> Text
fieldNameToColumnName (forall (symbol :: Symbol). KnownSymbol symbol => Text
symbolToText @name))
{-# INLINE distinctOn #-}



-- | Helper to deal with @some_field IS NULL@ and @some_field = 'some value'@
class EqOrIsOperator value where toEqOrIsOperator :: value -> FilterOperator
instance {-# OVERLAPS #-} EqOrIsOperator (Maybe something) where toEqOrIsOperator :: Maybe something -> FilterOperator
toEqOrIsOperator Maybe something
Nothing = FilterOperator
IsOp; toEqOrIsOperator (Just something
_) = FilterOperator
EqOp
instance {-# OVERLAPPABLE #-} EqOrIsOperator otherwise where toEqOrIsOperator :: otherwise -> FilterOperator
toEqOrIsOperator otherwise
_ = FilterOperator
EqOp