{-# LANGUAGE BangPatterns, TypeFamilies, DataKinds, PolyKinds, TypeApplications, ScopedTypeVariables, TypeInType, 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
, queryOr
, DefaultScope (..)
, filterWhere
, filterWhereCaseInsensitive
, filterWhereNot
, filterWhereIn
, 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 (..)
)
where
import qualified Prelude
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 Debug.Trace
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 :: forall (table :: Symbol).
ByteString -> [ByteString] -> QueryBuilder table
NewQueryBuilder { $sel:selectFrom:NewQueryBuilder :: ByteString
selectFrom = Table (GetModelByTableName table) => ByteString
forall record. Table record => ByteString
tableNameByteString @(GetModelByTableName table), $sel:columns:NewQueryBuilder :: [ByteString]
columns = Table (GetModelByTableName table) => [ByteString]
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
showList :: [MatchSensitivity] -> ShowS
$cshowList :: [MatchSensitivity] -> ShowS
show :: MatchSensitivity -> String
$cshow :: MatchSensitivity -> String
showsPrec :: Int -> MatchSensitivity -> ShowS
$cshowsPrec :: Int -> MatchSensitivity -> ShowS
Show, MatchSensitivity -> MatchSensitivity -> Bool
(MatchSensitivity -> MatchSensitivity -> Bool)
-> (MatchSensitivity -> MatchSensitivity -> Bool)
-> Eq MatchSensitivity
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: MatchSensitivity -> MatchSensitivity -> Bool
$c/= :: MatchSensitivity -> MatchSensitivity -> Bool
== :: MatchSensitivity -> MatchSensitivity -> Bool
$c== :: 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
showList :: [FilterOperator] -> ShowS
$cshowList :: [FilterOperator] -> ShowS
show :: FilterOperator -> String
$cshow :: FilterOperator -> String
showsPrec :: Int -> FilterOperator -> ShowS
$cshowsPrec :: Int -> FilterOperator -> ShowS
Show, FilterOperator -> FilterOperator -> Bool
(FilterOperator -> FilterOperator -> Bool)
-> (FilterOperator -> FilterOperator -> Bool) -> Eq FilterOperator
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: FilterOperator -> FilterOperator -> Bool
$c/= :: FilterOperator -> FilterOperator -> Bool
== :: FilterOperator -> FilterOperator -> Bool
$c== :: 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
showList :: [OrderByClause] -> ShowS
$cshowList :: [OrderByClause] -> ShowS
show :: OrderByClause -> String
$cshow :: OrderByClause -> String
showsPrec :: Int -> OrderByClause -> ShowS
$cshowsPrec :: Int -> OrderByClause -> ShowS
Show, OrderByClause -> OrderByClause -> Bool
(OrderByClause -> OrderByClause -> Bool)
-> (OrderByClause -> OrderByClause -> Bool) -> Eq OrderByClause
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: OrderByClause -> OrderByClause -> Bool
$c/= :: OrderByClause -> OrderByClause -> Bool
== :: OrderByClause -> OrderByClause -> Bool
$c== :: 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
$cto :: forall x. Rep OrderByClause x -> OrderByClause
$cfrom :: forall x. OrderByClause -> Rep OrderByClause x
GHC.Generics.Generic, OrderByClause -> ()
(OrderByClause -> ()) -> NFData OrderByClause
forall a. (a -> ()) -> NFData a
rnf :: OrderByClause -> ()
$crnf :: 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 

-- 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 :: QueryBuilder table -> QueryBuilder table
getQueryBuilder = QueryBuilder table -> QueryBuilder table
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id
    injectQueryBuilder :: QueryBuilder table -> QueryBuilder table
injectQueryBuilder = QueryBuilder table -> QueryBuilder table
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id

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

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

instance (KnownSymbol foreignTable, foreignModel ~ GetModelByTableName foreignTable , KnownSymbol indexColumn, HasField indexColumn foreignModel indexValue) => HasQueryBuilder (LabeledQueryBuilderWrapper foreignTable indexColumn indexValue) NoJoins where
    getQueryBuilder :: LabeledQueryBuilderWrapper
  foreignTable indexColumn indexValue table
-> QueryBuilder table
getQueryBuilder (LabeledQueryBuilderWrapper QueryBuilder table
queryBuilder) = QueryBuilder table
queryBuilder
    injectQueryBuilder :: 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
    getQueryIndex :: 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
$ KnownSymbol foreignTable => ByteString
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 k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Text -> Text
fieldNameToColumnName) (KnownSymbol indexColumn => Text
forall (symbol :: Symbol). KnownSymbol symbol => Text
symbolToText @indexColumn)


data QueryBuilder (table :: Symbol) =
    NewQueryBuilder { QueryBuilder table -> ByteString
selectFrom :: !ByteString, QueryBuilder table -> [ByteString]
columns :: ![ByteString] }
    | DistinctQueryBuilder   { QueryBuilder table -> QueryBuilder table
queryBuilder :: !(QueryBuilder table) }
    | DistinctOnQueryBuilder { queryBuilder :: !(QueryBuilder table), QueryBuilder table -> ByteString
distinctOnColumn :: !ByteString }
    | FilterByQueryBuilder   { queryBuilder :: !(QueryBuilder table), QueryBuilder table -> (ByteString, FilterOperator, Action)
queryFilter :: !(ByteString, FilterOperator, Action), QueryBuilder table -> Maybe ByteString
applyLeft :: !(Maybe ByteString), QueryBuilder table -> Maybe ByteString
applyRight :: !(Maybe ByteString) }
    | OrderByQueryBuilder    { queryBuilder :: !(QueryBuilder table), QueryBuilder table -> OrderByClause
queryOrderByClause :: !OrderByClause }
    | LimitQueryBuilder      { queryBuilder :: !(QueryBuilder table), QueryBuilder table -> Int
queryLimit :: !Int }
    | OffsetQueryBuilder     { queryBuilder :: !(QueryBuilder table), QueryBuilder table -> Int
queryOffset :: !Int }
    | UnionQueryBuilder      { QueryBuilder table -> QueryBuilder table
firstQueryBuilder :: !(QueryBuilder table), QueryBuilder table -> QueryBuilder table
secondQueryBuilder :: !(QueryBuilder table) }
    | JoinQueryBuilder       { queryBuilder :: !(QueryBuilder table), 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
showList :: [QueryBuilder table] -> ShowS
$cshowList :: forall (table :: Symbol). [QueryBuilder table] -> ShowS
show :: QueryBuilder table -> String
$cshow :: forall (table :: Symbol). QueryBuilder table -> String
showsPrec :: Int -> QueryBuilder table -> ShowS
$cshowsPrec :: forall (table :: Symbol). Int -> 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
/= :: 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
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
showList :: [Condition] -> ShowS
$cshowList :: [Condition] -> ShowS
show :: Condition -> String
$cshow :: Condition -> String
showsPrec :: Int -> Condition -> ShowS
$cshowsPrec :: Int -> Condition -> ShowS
Show, Condition -> Condition -> Bool
(Condition -> Condition -> Bool)
-> (Condition -> Condition -> Bool) -> Eq Condition
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Condition -> Condition -> Bool
$c/= :: Condition -> Condition -> Bool
== :: Condition -> Condition -> Bool
$c== :: 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
showList :: [Join] -> ShowS
$cshowList :: [Join] -> ShowS
show :: Join -> String
$cshow :: Join -> String
showsPrec :: Int -> Join -> ShowS
$cshowsPrec :: Int -> Join -> ShowS
Show, Join -> Join -> Bool
(Join -> Join -> Bool) -> (Join -> Join -> Bool) -> Eq Join
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Join -> Join -> Bool
$c/= :: Join -> Join -> Bool
== :: Join -> Join -> Bool
$c== :: 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
/= :: OrderByDirection -> OrderByDirection -> Bool
$c/= :: OrderByDirection -> OrderByDirection -> Bool
== :: OrderByDirection -> OrderByDirection -> Bool
$c== :: 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
showList :: [OrderByDirection] -> ShowS
$cshowList :: [OrderByDirection] -> ShowS
show :: OrderByDirection -> String
$cshow :: OrderByDirection -> String
showsPrec :: Int -> OrderByDirection -> ShowS
$cshowsPrec :: Int -> 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
$cto :: forall x. Rep OrderByDirection x -> OrderByDirection
$cfrom :: forall x. OrderByDirection -> Rep OrderByDirection x
GHC.Generics.Generic, OrderByDirection -> ()
(OrderByDirection -> ()) -> NFData OrderByDirection
forall a. (a -> ()) -> NFData a
rnf :: OrderByDirection -> ()
$crnf :: 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
showList :: [SQLQuery] -> ShowS
$cshowList :: [SQLQuery] -> ShowS
show :: SQLQuery -> String
$cshow :: SQLQuery -> String
showsPrec :: Int -> SQLQuery -> ShowS
$cshowsPrec :: Int -> SQLQuery -> ShowS
Show, SQLQuery -> SQLQuery -> Bool
(SQLQuery -> SQLQuery -> Bool)
-> (SQLQuery -> SQLQuery -> Bool) -> Eq SQLQuery
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SQLQuery -> SQLQuery -> Bool
$c/= :: SQLQuery -> SQLQuery -> Bool
== :: SQLQuery -> SQLQuery -> Bool
$c== :: 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, get #id self)@ to all users by default.
--
-- This is needed to support syntax like this:
--
-- > user
-- >     |> get #posts
-- >     |> fetch
--
instance Eq Builder.Builder where
    Builder
a == :: Builder -> Builder -> Bool
== Builder
b = (Builder -> ByteString
Builder.toLazyByteString Builder
a) ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== (Builder -> ByteString
Builder.toLazyByteString Builder
b)

instance SetField "queryIndex" SQLQuery (Maybe ByteString) where setField :: Maybe ByteString -> SQLQuery -> SQLQuery
setField Maybe ByteString
value SQLQuery
sqlQuery = SQLQuery
sqlQuery { $sel:queryIndex:SQLQuery :: Maybe ByteString
queryIndex = Maybe ByteString
value }
instance SetField "selectFrom" SQLQuery ByteString where setField :: ByteString -> SQLQuery -> SQLQuery
setField ByteString
value SQLQuery
sqlQuery = SQLQuery
sqlQuery { $sel:selectFrom:SQLQuery :: ByteString
selectFrom = ByteString
value }
instance SetField "distinctClause" SQLQuery (Maybe ByteString) where setField :: Maybe ByteString -> SQLQuery -> SQLQuery
setField Maybe ByteString
value SQLQuery
sqlQuery = SQLQuery
sqlQuery { $sel:distinctClause:SQLQuery :: Maybe ByteString
distinctClause = Maybe ByteString
value }
instance SetField "distinctOnClause" SQLQuery (Maybe ByteString) where setField :: Maybe ByteString -> SQLQuery -> SQLQuery
setField Maybe ByteString
value SQLQuery
sqlQuery = SQLQuery
sqlQuery { $sel:distinctOnClause:SQLQuery :: Maybe ByteString
distinctOnClause = Maybe ByteString
value }
instance SetField "whereCondition" SQLQuery (Maybe Condition) where setField :: Maybe Condition -> SQLQuery -> SQLQuery
setField Maybe Condition
value SQLQuery
sqlQuery = SQLQuery
sqlQuery { $sel:whereCondition:SQLQuery :: Maybe Condition
whereCondition = Maybe Condition
value }
instance SetField "orderByClause" SQLQuery [OrderByClause] where setField :: [OrderByClause] -> SQLQuery -> SQLQuery
setField [OrderByClause]
value SQLQuery
sqlQuery = SQLQuery
sqlQuery { $sel:orderByClause:SQLQuery :: [OrderByClause]
orderByClause = [OrderByClause]
value }
instance SetField "limitClause" SQLQuery (Maybe ByteString) where setField :: Maybe ByteString -> SQLQuery -> SQLQuery
setField Maybe ByteString
value SQLQuery
sqlQuery = SQLQuery
sqlQuery { $sel:limitClause:SQLQuery :: Maybe ByteString
limitClause = Maybe ByteString
value }
instance SetField "offsetClause" SQLQuery (Maybe ByteString) where setField :: Maybe ByteString -> SQLQuery -> SQLQuery
setField Maybe ByteString
value SQLQuery
sqlQuery = SQLQuery
sqlQuery { $sel:offsetClause:SQLQuery :: Maybe ByteString
offsetClause = Maybe ByteString
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 :: QueryBuilder table
query = (QueryBuilder table -> QueryBuilder table
forall (table :: Symbol).
DefaultScope table =>
QueryBuilder table -> QueryBuilder table
defaultScope @table) NewQueryBuilder :: forall (table :: Symbol).
ByteString -> [ByteString] -> QueryBuilder table
NewQueryBuilder { $sel:selectFrom:NewQueryBuilder :: ByteString
selectFrom = Table model => ByteString
forall record. Table record => ByteString
tableNameByteString @model, $sel:columns:NewQueryBuilder :: [ByteString]
columns = Table model => [ByteString]
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 :: 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
getQueryBuilder queryBuilderProvider table
queryBuilderProvider
    where
    buildQueryHelper :: QueryBuilder table -> SQLQuery
buildQueryHelper NewQueryBuilder { ByteString
selectFrom :: ByteString
$sel:selectFrom:NewQueryBuilder :: forall (table :: Symbol). QueryBuilder table -> ByteString
selectFrom, [ByteString]
columns :: [ByteString]
$sel:columns:NewQueryBuilder :: forall (table :: Symbol). QueryBuilder table -> [ByteString]
columns } =
        SQLQuery :: Maybe ByteString
-> ByteString
-> Maybe ByteString
-> Maybe ByteString
-> Maybe Condition
-> [Join]
-> [OrderByClause]
-> Maybe ByteString
-> Maybe ByteString
-> [ByteString]
-> SQLQuery
SQLQuery
            {     $sel:queryIndex:SQLQuery :: Maybe ByteString
queryIndex = queryBuilderProvider table -> Maybe ByteString
forall k (queryBuilderProvider :: Symbol -> *) (joinRegister :: k)
       (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
queryBuilderProvider table -> Maybe ByteString
getQueryIndex queryBuilderProvider table
queryBuilderProvider 
                , $sel:selectFrom:SQLQuery :: ByteString
selectFrom = ByteString
selectFrom
                , $sel:distinctClause:SQLQuery :: Maybe ByteString
distinctClause = Maybe ByteString
forall a. Maybe a
Nothing
                , $sel:distinctOnClause:SQLQuery :: Maybe ByteString
distinctOnClause = Maybe ByteString
forall a. Maybe a
Nothing
                , $sel:whereCondition:SQLQuery :: Maybe Condition
whereCondition = Maybe Condition
forall a. Maybe a
Nothing
                , $sel:joins:SQLQuery :: [Join]
joins = []
                , $sel:orderByClause:SQLQuery :: [OrderByClause]
orderByClause = []
                , $sel:limitClause:SQLQuery :: Maybe ByteString
limitClause = Maybe ByteString
forall a. Maybe a
Nothing
                , $sel:offsetClause:SQLQuery :: Maybe ByteString
offsetClause = Maybe ByteString
forall a. Maybe a
Nothing
                , [ByteString]
columns :: [ByteString]
$sel:columns:SQLQuery :: [ByteString]
columns
                }
    buildQueryHelper DistinctQueryBuilder { QueryBuilder table
queryBuilder :: QueryBuilder table
$sel:queryBuilder:NewQueryBuilder :: forall (table :: Symbol). QueryBuilder table -> 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 IsLabel "distinctClause" (Proxy "distinctClause")
Proxy "distinctClause"
#distinctClause ByteString
"DISTINCT"
    buildQueryHelper DistinctOnQueryBuilder { QueryBuilder table
queryBuilder :: QueryBuilder table
$sel:queryBuilder:NewQueryBuilder :: forall (table :: Symbol). QueryBuilder table -> QueryBuilder table
queryBuilder, ByteString
distinctOnColumn :: ByteString
$sel:distinctOnColumn:NewQueryBuilder :: forall (table :: Symbol). QueryBuilder table -> 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 IsLabel "distinctOnClause" (Proxy "distinctOnClause")
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 :: QueryBuilder table
$sel:queryBuilder:NewQueryBuilder :: forall (table :: Symbol). QueryBuilder table -> QueryBuilder table
queryBuilder, $sel:queryFilter:NewQueryBuilder :: forall (table :: Symbol).
QueryBuilder table -> (ByteString, FilterOperator, Action)
queryFilter = (ByteString
columnName, FilterOperator
operator, Action
value), Maybe ByteString
applyLeft :: Maybe ByteString
$sel:applyLeft:NewQueryBuilder :: forall (table :: Symbol). QueryBuilder table -> Maybe ByteString
applyLeft, Maybe ByteString
applyRight :: Maybe ByteString
$sel:applyRight:NewQueryBuilder :: forall (table :: Symbol). QueryBuilder table -> Maybe ByteString
applyRight } =
                let
                    applyFn :: Maybe p -> p -> p
applyFn Maybe p
fn p
value = case Maybe p
fn of
                            Just p
fn -> p
fn p -> p -> p
forall a. Semigroup a => a -> a -> a
<> p
"(" p -> p -> p
forall a. Semigroup a => a -> a -> a
<> p
value p -> p -> p
forall a. Semigroup a => a -> a -> a
<> p
")"
                            Maybe p
Nothing -> p
value

                    condition :: Condition
condition = ByteString -> Action -> Condition
VarCondition (Maybe ByteString -> ByteString -> ByteString
forall p. (Semigroup p, IsString p) => Maybe p -> p -> p
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 p. (Semigroup p, IsString p) => Maybe p -> p -> p
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 IsLabel "whereCondition" (Proxy "whereCondition")
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 :: QueryBuilder table
$sel:queryBuilder:NewQueryBuilder :: forall (table :: Symbol). QueryBuilder table -> QueryBuilder table
queryBuilder, OrderByClause
queryOrderByClause :: OrderByClause
$sel:queryOrderByClause:NewQueryBuilder :: forall (table :: Symbol). QueryBuilder table -> 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 IsLabel "orderByClause" (Proxy "orderByClause")
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 :: QueryBuilder table
$sel:queryBuilder:NewQueryBuilder :: forall (table :: Symbol). QueryBuilder table -> QueryBuilder table
queryBuilder, Int
queryLimit :: Int
$sel:queryLimit:NewQueryBuilder :: forall (table :: Symbol). QueryBuilder table -> 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 IsLabel "limitClause" (Proxy "limitClause")
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 -> ByteString) -> ByteString
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> Builder -> ByteString
Builder.toLazyByteString
                            ByteString -> (ByteString -> ByteString) -> ByteString
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> ByteString -> ByteString
LByteString.toStrict
                        )
    buildQueryHelper OffsetQueryBuilder { QueryBuilder table
queryBuilder :: QueryBuilder table
$sel:queryBuilder:NewQueryBuilder :: forall (table :: Symbol). QueryBuilder table -> QueryBuilder table
queryBuilder, Int
queryOffset :: Int
$sel:queryOffset:NewQueryBuilder :: forall (table :: Symbol). QueryBuilder table -> 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 IsLabel "offsetClause" (Proxy "offsetClause")
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 -> ByteString) -> ByteString
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> Builder -> ByteString
Builder.toLazyByteString
                    ByteString -> (ByteString -> ByteString) -> ByteString
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> ByteString -> ByteString
LByteString.toStrict
                )
    buildQueryHelper UnionQueryBuilder { QueryBuilder table
firstQueryBuilder :: QueryBuilder table
$sel:firstQueryBuilder:NewQueryBuilder :: forall (table :: Symbol). QueryBuilder table -> QueryBuilder table
firstQueryBuilder, QueryBuilder table
secondQueryBuilder :: QueryBuilder table
$sel:secondQueryBuilder:NewQueryBuilder :: forall (table :: Symbol). QueryBuilder table -> 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 { $sel:whereCondition:SQLQuery :: Maybe Condition
whereCondition = Maybe Condition
unionWhere }
                    else
                        Text -> SQLQuery
forall a. Text -> a
error Text
"buildQuery: Union of complex queries not supported yet"
    
    buildQueryHelper JoinQueryBuilder { QueryBuilder table
queryBuilder :: QueryBuilder table
$sel:queryBuilder:NewQueryBuilder :: forall (table :: Symbol). QueryBuilder table -> QueryBuilder table
queryBuilder, Join
joinData :: Join
$sel:joinData:NewQueryBuilder :: forall (table :: Symbol). QueryBuilder table -> Join
joinData } =
        let 
            firstQuery :: SQLQuery
firstQuery = QueryBuilder table -> SQLQuery
buildQueryHelper QueryBuilder table
queryBuilder
         in SQLQuery
firstQuery { $sel:joins:SQLQuery :: [Join]
joins = Join
joinDataJoin -> [Join] -> [Join]
forall a. a -> [a] -> [a]
:SQLQuery -> [Join]
joins SQLQuery
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 :: 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 :: Maybe ByteString
$sel:queryIndex:SQLQuery :: SQLQuery -> Maybe ByteString
queryIndex, ByteString
selectFrom :: ByteString
$sel:selectFrom:SQLQuery :: SQLQuery -> ByteString
selectFrom, Maybe ByteString
distinctClause :: Maybe ByteString
$sel:distinctClause:SQLQuery :: SQLQuery -> Maybe ByteString
distinctClause, Maybe ByteString
distinctOnClause :: Maybe ByteString
$sel:distinctOnClause:SQLQuery :: SQLQuery -> Maybe ByteString
distinctOnClause, [OrderByClause]
orderByClause :: [OrderByClause]
$sel:orderByClause:SQLQuery :: SQLQuery -> [OrderByClause]
orderByClause, Maybe ByteString
limitClause :: Maybe ByteString
$sel:limitClause:SQLQuery :: SQLQuery -> Maybe ByteString
limitClause, Maybe ByteString
offsetClause :: Maybe ByteString
$sel:offsetClause:SQLQuery :: SQLQuery -> Maybe ByteString
offsetClause, [ByteString]
columns :: [ByteString]
$sel:columns:SQLQuery :: SQLQuery -> [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 :: ByteString
$sel:orderByColumn:OrderByClause :: OrderByClause -> ByteString
orderByColumn, OrderByDirection
orderByDirection :: OrderByDirection
$sel:orderByDirection:OrderByClause :: OrderByClause -> 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
" " ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<>) ([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 :: (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
injectQueryBuilder FilterByQueryBuilder :: forall (table :: Symbol).
QueryBuilder table
-> (ByteString, FilterOperator, Action)
-> Maybe ByteString
-> Maybe ByteString
-> QueryBuilder table
FilterByQueryBuilder { QueryBuilder table
queryBuilder :: QueryBuilder table
$sel:queryBuilder:NewQueryBuilder :: QueryBuilder table
queryBuilder, $sel:queryFilter:NewQueryBuilder :: (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), $sel:applyLeft:NewQueryBuilder :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, $sel:applyRight:NewQueryBuilder :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing }
    where
        columnName :: ByteString
columnName = Table model => ByteString
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 (KnownSymbol name => Text
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
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 :: (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
injectQueryBuilder FilterByQueryBuilder :: forall (table :: Symbol).
QueryBuilder table
-> (ByteString, FilterOperator, Action)
-> Maybe ByteString
-> Maybe ByteString
-> QueryBuilder table
FilterByQueryBuilder { QueryBuilder table'
queryBuilder :: QueryBuilder table'
$sel:queryBuilder:NewQueryBuilder :: QueryBuilder table'
queryBuilder, $sel:queryFilter:NewQueryBuilder :: (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), $sel:applyLeft:NewQueryBuilder :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, $sel:applyRight:NewQueryBuilder :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing }
    where
        columnName :: ByteString
columnName = Table model => ByteString
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 (KnownSymbol name => Text
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
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 :: (Proxy name, value) -> QueryBuilder table -> QueryBuilder table
filterWhereNot (Proxy name
name, value
value) QueryBuilder table
queryBuilder = FilterByQueryBuilder :: forall (table :: Symbol).
QueryBuilder table
-> (ByteString, FilterOperator, Action)
-> Maybe ByteString
-> Maybe ByteString
-> QueryBuilder table
FilterByQueryBuilder { QueryBuilder table
queryBuilder :: QueryBuilder table
$sel:queryBuilder:NewQueryBuilder :: QueryBuilder table
queryBuilder, $sel:queryFilter:NewQueryBuilder :: (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), $sel:applyLeft:NewQueryBuilder :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, $sel:applyRight:NewQueryBuilder :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing }
    where
        columnName :: ByteString
columnName = Table model => ByteString
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 (KnownSymbol name => Text
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 :: (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
injectQueryBuilder FilterByQueryBuilder :: forall (table :: Symbol).
QueryBuilder table
-> (ByteString, FilterOperator, Action)
-> Maybe ByteString
-> Maybe ByteString
-> QueryBuilder table
FilterByQueryBuilder { QueryBuilder table'
queryBuilder :: QueryBuilder table'
$sel:queryBuilder:NewQueryBuilder :: QueryBuilder table'
queryBuilder, $sel:queryFilter:NewQueryBuilder :: (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), $sel:applyLeft:NewQueryBuilder :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, $sel:applyRight:NewQueryBuilder :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing }
    where
        columnName :: ByteString
columnName = Table model => ByteString
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 (KnownSymbol name => Text
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
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 :: *). (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, [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
injectQueryBuilder QueryBuilder table
whereInQuery -- All values non null
            Just value
nullValue ->
                let
                    isNullValueExpr :: QueryBuilder table
isNullValueExpr = FilterByQueryBuilder :: forall (table :: Symbol).
QueryBuilder table
-> (ByteString, FilterOperator, Action)
-> Maybe ByteString
-> Maybe ByteString
-> QueryBuilder table
FilterByQueryBuilder { QueryBuilder table
queryBuilder :: QueryBuilder table
$sel:queryBuilder:NewQueryBuilder :: QueryBuilder table
queryBuilder, $sel:queryFilter:NewQueryBuilder :: (ByteString, FilterOperator, Action)
queryFilter = (ByteString
columnName, FilterOperator
IsOp, value -> Action
forall a. ToField a => a -> Action
toField value
nullValue), $sel:applyLeft:NewQueryBuilder :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, $sel:applyRight:NewQueryBuilder :: 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
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
injectQueryBuilder QueryBuilder table
whereInQuery)
                                (QueryBuilder table -> QueryBuilder table
forall k (queryBuilderProvider :: Symbol -> *) (joinRegister :: k)
       (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
QueryBuilder table -> queryBuilderProvider 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
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 :: forall (table :: Symbol).
QueryBuilder table
-> (ByteString, FilterOperator, Action)
-> Maybe ByteString
-> Maybe ByteString
-> QueryBuilder table
FilterByQueryBuilder { QueryBuilder table
queryBuilder :: QueryBuilder table
$sel:queryBuilder:NewQueryBuilder :: QueryBuilder table
queryBuilder, $sel:queryFilter:NewQueryBuilder :: (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)), $sel:applyLeft:NewQueryBuilder :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, $sel:applyRight:NewQueryBuilder :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing }

        columnName :: ByteString
columnName = Table model => ByteString
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 (KnownSymbol name => Text
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
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 :: (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
injectQueryBuilder FilterByQueryBuilder :: forall (table :: Symbol).
QueryBuilder table
-> (ByteString, FilterOperator, Action)
-> Maybe ByteString
-> Maybe ByteString
-> QueryBuilder table
FilterByQueryBuilder { QueryBuilder table'
queryBuilder :: QueryBuilder table'
$sel:queryBuilder:NewQueryBuilder :: QueryBuilder table'
queryBuilder, $sel:queryFilter:NewQueryBuilder :: (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)), $sel:applyLeft:NewQueryBuilder :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, $sel:applyRight:NewQueryBuilder :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing }
    where
        columnName :: ByteString
columnName = Table model => ByteString
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 (KnownSymbol name => Text
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
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 :: (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
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
injectQueryBuilder FilterByQueryBuilder :: forall (table :: Symbol).
QueryBuilder table
-> (ByteString, FilterOperator, Action)
-> Maybe ByteString
-> Maybe ByteString
-> QueryBuilder table
FilterByQueryBuilder { $sel:queryBuilder:NewQueryBuilder :: QueryBuilder table
queryBuilder = QueryBuilder table
whereNotInQuery, $sel:queryFilter:NewQueryBuilder :: (ByteString, FilterOperator, Action)
queryFilter = (ByteString
columnName, FilterOperator
IsNotOp, value -> Action
forall a. ToField a => a -> Action
toField value
nullValue), $sel:applyLeft:NewQueryBuilder :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, $sel:applyRight:NewQueryBuilder :: 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
injectQueryBuilder FilterByQueryBuilder :: forall (table :: Symbol).
QueryBuilder table
-> (ByteString, FilterOperator, Action)
-> Maybe ByteString
-> Maybe ByteString
-> QueryBuilder table
FilterByQueryBuilder { QueryBuilder table
queryBuilder :: QueryBuilder table
$sel:queryBuilder:NewQueryBuilder :: QueryBuilder table
queryBuilder, $sel:queryFilter:NewQueryBuilder :: (ByteString, FilterOperator, Action)
queryFilter = (ByteString
columnName, FilterOperator
IsNotOp, value -> Action
forall a. ToField a => a -> Action
toField value
nullValue), $sel:applyLeft:NewQueryBuilder :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, $sel:applyRight:NewQueryBuilder :: 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 :: forall (table :: Symbol).
QueryBuilder table
-> (ByteString, FilterOperator, Action)
-> Maybe ByteString
-> Maybe ByteString
-> QueryBuilder table
FilterByQueryBuilder { QueryBuilder table
queryBuilder :: QueryBuilder table
$sel:queryBuilder:NewQueryBuilder :: QueryBuilder table
queryBuilder, $sel:queryFilter:NewQueryBuilder :: (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)), $sel:applyLeft:NewQueryBuilder :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, $sel:applyRight:NewQueryBuilder :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing }

        columnName :: ByteString
columnName = Text -> ByteString
Text.encodeUtf8 (KnownSymbol table => Text
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 (KnownSymbol name => Text
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
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 :: (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
injectQueryBuilder FilterByQueryBuilder :: forall (table :: Symbol).
QueryBuilder table
-> (ByteString, FilterOperator, Action)
-> Maybe ByteString
-> Maybe ByteString
-> QueryBuilder table
FilterByQueryBuilder { QueryBuilder table'
queryBuilder :: QueryBuilder table'
$sel:queryBuilder:NewQueryBuilder :: QueryBuilder table'
queryBuilder, $sel:queryFilter:NewQueryBuilder :: (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)), $sel:applyLeft:NewQueryBuilder :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, $sel:applyRight:NewQueryBuilder :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing }
    where
        columnName :: ByteString
columnName = Table model => ByteString
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 (KnownSymbol name => Text
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
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 :: (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
injectQueryBuilder FilterByQueryBuilder :: forall (table :: Symbol).
QueryBuilder table
-> (ByteString, FilterOperator, Action)
-> Maybe ByteString
-> Maybe ByteString
-> QueryBuilder table
FilterByQueryBuilder { QueryBuilder table
queryBuilder :: QueryBuilder table
$sel:queryBuilder:NewQueryBuilder :: QueryBuilder table
queryBuilder, $sel:queryFilter:NewQueryBuilder :: (ByteString, FilterOperator, Action)
queryFilter = (ByteString
columnName, MatchSensitivity -> FilterOperator
LikeOp MatchSensitivity
CaseSensitive, value -> Action
forall a. ToField a => a -> Action
toField value
value), $sel:applyLeft:NewQueryBuilder :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, $sel:applyRight:NewQueryBuilder :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing }
    where
        columnName :: ByteString
columnName = Table model => ByteString
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 (KnownSymbol name => Text
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
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 :: (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
injectQueryBuilder FilterByQueryBuilder :: forall (table :: Symbol).
QueryBuilder table
-> (ByteString, FilterOperator, Action)
-> Maybe ByteString
-> Maybe ByteString
-> QueryBuilder table
FilterByQueryBuilder { QueryBuilder table'
queryBuilder :: QueryBuilder table'
$sel:queryBuilder:NewQueryBuilder :: QueryBuilder table'
queryBuilder, $sel:queryFilter:NewQueryBuilder :: (ByteString, FilterOperator, Action)
queryFilter = (ByteString
columnName, MatchSensitivity -> FilterOperator
LikeOp MatchSensitivity
CaseSensitive, value -> Action
forall a. ToField a => a -> Action
toField value
value), $sel:applyLeft:NewQueryBuilder :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, $sel:applyRight:NewQueryBuilder :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing }
    where
        columnName :: ByteString
columnName = Table model => ByteString
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 (KnownSymbol name => Text
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
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 :: (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
injectQueryBuilder FilterByQueryBuilder :: forall (table :: Symbol).
QueryBuilder table
-> (ByteString, FilterOperator, Action)
-> Maybe ByteString
-> Maybe ByteString
-> QueryBuilder table
FilterByQueryBuilder { QueryBuilder table
queryBuilder :: QueryBuilder table
$sel:queryBuilder:NewQueryBuilder :: QueryBuilder table
queryBuilder, $sel:queryFilter:NewQueryBuilder :: (ByteString, FilterOperator, Action)
queryFilter = (ByteString
columnName, MatchSensitivity -> FilterOperator
LikeOp MatchSensitivity
CaseInsensitive, value -> Action
forall a. ToField a => a -> Action
toField value
value), $sel:applyLeft:NewQueryBuilder :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, $sel:applyRight:NewQueryBuilder :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing }
    where
        columnName :: ByteString
columnName = Table model => ByteString
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 (KnownSymbol name => Text
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
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 :: (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
injectQueryBuilder FilterByQueryBuilder :: forall (table :: Symbol).
QueryBuilder table
-> (ByteString, FilterOperator, Action)
-> Maybe ByteString
-> Maybe ByteString
-> QueryBuilder table
FilterByQueryBuilder { QueryBuilder table'
queryBuilder :: QueryBuilder table'
$sel:queryBuilder:NewQueryBuilder :: QueryBuilder table'
queryBuilder, $sel:queryFilter:NewQueryBuilder :: (ByteString, FilterOperator, Action)
queryFilter = (ByteString
columnName, MatchSensitivity -> FilterOperator
LikeOp MatchSensitivity
CaseInsensitive, value -> Action
forall a. ToField a => a -> Action
toField value
value), $sel:applyLeft:NewQueryBuilder :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, $sel:applyRight:NewQueryBuilder :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing }
    where
        columnName :: ByteString
columnName = Table model => ByteString
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 (KnownSymbol name => Text
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
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 :: (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
injectQueryBuilder FilterByQueryBuilder :: forall (table :: Symbol).
QueryBuilder table
-> (ByteString, FilterOperator, Action)
-> Maybe ByteString
-> Maybe ByteString
-> QueryBuilder table
FilterByQueryBuilder { QueryBuilder table
queryBuilder :: QueryBuilder table
$sel:queryBuilder:NewQueryBuilder :: QueryBuilder table
queryBuilder, $sel:queryFilter:NewQueryBuilder :: (ByteString, FilterOperator, Action)
queryFilter = (ByteString
columnName, MatchSensitivity -> FilterOperator
MatchesOp MatchSensitivity
CaseSensitive, value -> Action
forall a. ToField a => a -> Action
toField value
value), $sel:applyLeft:NewQueryBuilder :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, $sel:applyRight:NewQueryBuilder :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing }
    where
        columnName :: ByteString
columnName = Table model => ByteString
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 (KnownSymbol name => Text
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
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 :: (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
injectQueryBuilder FilterByQueryBuilder :: forall (table :: Symbol).
QueryBuilder table
-> (ByteString, FilterOperator, Action)
-> Maybe ByteString
-> Maybe ByteString
-> QueryBuilder table
FilterByQueryBuilder { QueryBuilder table'
queryBuilder :: QueryBuilder table'
$sel:queryBuilder:NewQueryBuilder :: QueryBuilder table'
queryBuilder, $sel:queryFilter:NewQueryBuilder :: (ByteString, FilterOperator, Action)
queryFilter = (ByteString
columnName, MatchSensitivity -> FilterOperator
MatchesOp MatchSensitivity
CaseSensitive, value -> Action
forall a. ToField a => a -> Action
toField value
value), $sel:applyLeft:NewQueryBuilder :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, $sel:applyRight:NewQueryBuilder :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing }
    where
        columnName :: ByteString
columnName = Table model => ByteString
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 (KnownSymbol name => Text
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
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 :: (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
injectQueryBuilder FilterByQueryBuilder :: forall (table :: Symbol).
QueryBuilder table
-> (ByteString, FilterOperator, Action)
-> Maybe ByteString
-> Maybe ByteString
-> QueryBuilder table
FilterByQueryBuilder { QueryBuilder table
queryBuilder :: QueryBuilder table
$sel:queryBuilder:NewQueryBuilder :: QueryBuilder table
queryBuilder, $sel:queryFilter:NewQueryBuilder :: (ByteString, FilterOperator, Action)
queryFilter = (ByteString
columnName, MatchSensitivity -> FilterOperator
MatchesOp MatchSensitivity
CaseInsensitive, value -> Action
forall a. ToField a => a -> Action
toField value
value), $sel:applyLeft:NewQueryBuilder :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, $sel:applyRight:NewQueryBuilder :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing }
    where
        columnName :: ByteString
columnName = Table model => ByteString
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 (KnownSymbol name => Text
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
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 :: (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
injectQueryBuilder FilterByQueryBuilder :: forall (table :: Symbol).
QueryBuilder table
-> (ByteString, FilterOperator, Action)
-> Maybe ByteString
-> Maybe ByteString
-> QueryBuilder table
FilterByQueryBuilder { QueryBuilder table'
queryBuilder :: QueryBuilder table'
$sel:queryBuilder:NewQueryBuilder :: QueryBuilder table'
queryBuilder, $sel:queryFilter:NewQueryBuilder :: (ByteString, FilterOperator, Action)
queryFilter = (ByteString
columnName, MatchSensitivity -> FilterOperator
MatchesOp MatchSensitivity
CaseInsensitive, value -> Action
forall a. ToField a => a -> Action
toField value
value), $sel:applyLeft:NewQueryBuilder :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, $sel:applyRight:NewQueryBuilder :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing }
    where
        columnName :: ByteString
columnName = Table model => ByteString
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 (KnownSymbol name => Text
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
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 :: 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 :: 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 :: (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
injectQueryBuilder FilterByQueryBuilder :: forall (table :: Symbol).
QueryBuilder table
-> (ByteString, FilterOperator, Action)
-> Maybe ByteString
-> Maybe ByteString
-> QueryBuilder table
FilterByQueryBuilder { QueryBuilder table
queryBuilder :: QueryBuilder table
$sel:queryBuilder:NewQueryBuilder :: QueryBuilder table
queryBuilder, $sel:queryFilter:NewQueryBuilder :: (ByteString, FilterOperator, Action)
queryFilter = (ByteString
columnName, FilterOperator
SqlOp, Builder -> Action
Plain (ByteString -> Builder
Builder.byteString ByteString
sqlCondition)), $sel:applyLeft:NewQueryBuilder :: Maybe ByteString
applyLeft = Maybe ByteString
forall a. Maybe a
Nothing, $sel:applyRight:NewQueryBuilder :: Maybe ByteString
applyRight = Maybe ByteString
forall a. Maybe a
Nothing }
    where
        columnName :: ByteString
columnName = Table model => ByteString
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 (KnownSymbol name => Text
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
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 :: (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
injectQueryBuilder FilterByQueryBuilder :: forall (table :: Symbol).
QueryBuilder table
-> (ByteString, FilterOperator, Action)
-> Maybe ByteString
-> Maybe ByteString
-> QueryBuilder table
FilterByQueryBuilder { QueryBuilder table
queryBuilder :: QueryBuilder table
$sel:queryBuilder:NewQueryBuilder :: QueryBuilder table
queryBuilder, $sel:queryFilter:NewQueryBuilder :: (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), $sel:applyLeft:NewQueryBuilder :: Maybe ByteString
applyLeft = ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just ByteString
"LOWER", $sel:applyRight:NewQueryBuilder :: Maybe ByteString
applyRight = ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just ByteString
"LOWER" }
    where
        columnName :: ByteString
columnName = Table model => ByteString
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 (KnownSymbol name => Text
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
getQueryBuilder queryBuilderProvider table
queryBuilderProvider
{-# INLINE filterWhereCaseInsensitive #-}

-- | 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 :: (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
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
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 = KnownSymbol table => ByteString
forall (symbol :: Symbol). KnownSymbol symbol => ByteString
symbolToByteString @table
        joinTableName :: ByteString
joinTableName = KnownSymbol table' => ByteString
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 k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Text -> Text
fieldNameToColumnName) (KnownSymbol name => Text
forall (symbol :: Symbol). KnownSymbol symbol => Text
symbolToText @name)
        rightJoinColumn :: ByteString
rightJoinColumn = (Text -> ByteString
Text.encodeUtf8 (Text -> ByteString) -> (Text -> Text) -> Text -> ByteString
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) (KnownSymbol name' => Text
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 :: 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
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 :: (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
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
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 = KnownSymbol table' => ByteString
forall (symbol :: Symbol). KnownSymbol symbol => ByteString
symbolToByteString @table'
        joinTableName :: ByteString
joinTableName = KnownSymbol table => ByteString
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 k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Text -> Text
fieldNameToColumnName) (KnownSymbol name' => Text
forall (symbol :: Symbol). KnownSymbol symbol => Text
symbolToText @name')
        rightJoinColumn :: ByteString
rightJoinColumn = (Text -> ByteString
Text.encodeUtf8 (Text -> ByteString) -> (Text -> Text) -> Text -> ByteString
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) (KnownSymbol name => Text
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 :: 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
injectQueryBuilder OrderByQueryBuilder :: forall (table :: Symbol).
QueryBuilder table -> OrderByClause -> QueryBuilder table
OrderByQueryBuilder { QueryBuilder table
queryBuilder :: QueryBuilder table
$sel:queryBuilder:NewQueryBuilder :: QueryBuilder table
queryBuilder, $sel:queryOrderByClause:NewQueryBuilder :: OrderByClause
queryOrderByClause = OrderByClause :: ByteString -> OrderByDirection -> OrderByClause
OrderByClause { $sel:orderByColumn:OrderByClause :: ByteString
orderByColumn = ByteString
columnName, $sel:orderByDirection:OrderByClause :: OrderByDirection
orderByDirection = OrderByDirection
Asc } }
    where
        columnName :: ByteString
columnName = Table model => ByteString
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 (KnownSymbol name => Text
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
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 :: 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
injectQueryBuilder OrderByQueryBuilder :: forall (table :: Symbol).
QueryBuilder table -> OrderByClause -> QueryBuilder table
OrderByQueryBuilder { QueryBuilder table
queryBuilder :: QueryBuilder table
$sel:queryBuilder:NewQueryBuilder :: QueryBuilder table
queryBuilder, $sel:queryOrderByClause:NewQueryBuilder :: OrderByClause
queryOrderByClause = OrderByClause :: ByteString -> OrderByDirection -> OrderByClause
OrderByClause { $sel:orderByColumn:OrderByClause :: ByteString
orderByColumn = ByteString
columnName, $sel:orderByDirection:OrderByClause :: OrderByDirection
orderByDirection = OrderByDirection
Desc } }
    where
        columnName :: ByteString
columnName = Table model => ByteString
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 (KnownSymbol name => Text
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
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 :: 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 :: 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
injectQueryBuilder LimitQueryBuilder :: forall (table :: Symbol).
QueryBuilder table -> Int -> QueryBuilder table
LimitQueryBuilder { QueryBuilder model
queryBuilder :: QueryBuilder model
$sel:queryBuilder:NewQueryBuilder :: QueryBuilder model
queryBuilder, Int
queryLimit :: Int
$sel:queryLimit:NewQueryBuilder :: 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
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 :: 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
injectQueryBuilder OffsetQueryBuilder :: forall (table :: Symbol).
QueryBuilder table -> Int -> QueryBuilder table
OffsetQueryBuilder { QueryBuilder model
queryBuilder :: QueryBuilder model
$sel:queryBuilder:NewQueryBuilder :: QueryBuilder model
queryBuilder, Int
queryOffset :: Int
$sel:queryOffset:NewQueryBuilder :: 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
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 :: 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 :: forall (table :: Symbol).
QueryBuilder table -> QueryBuilder table -> QueryBuilder table
UnionQueryBuilder { QueryBuilder model
firstQueryBuilder :: QueryBuilder model
$sel:firstQueryBuilder:NewQueryBuilder :: QueryBuilder model
firstQueryBuilder, QueryBuilder model
secondQueryBuilder :: QueryBuilder model
$sel:secondQueryBuilder:NewQueryBuilder :: 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
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
getQueryBuilder r model
secondQueryBuilderProvider

    
{-# INLINE queryUnion #-}


-- | 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 :: (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
injectQueryBuilder
    (UnionQueryBuilder :: forall (table :: Symbol).
QueryBuilder table -> QueryBuilder table -> QueryBuilder table
UnionQueryBuilder { 
        $sel:firstQueryBuilder:NewQueryBuilder :: QueryBuilder model
firstQueryBuilder = queryBuilderProvider''' model -> QueryBuilder model
forall k (queryBuilderProvider :: Symbol -> *) (joinRegister :: k)
       (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
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, 
        $sel:secondQueryBuilder:NewQueryBuilder :: QueryBuilder model
secondQueryBuilder = queryBuilderProvider'' model -> QueryBuilder model
forall k (queryBuilderProvider :: Symbol -> *) (joinRegister :: k)
       (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
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 :: queryBuilderProvider table -> queryBuilderProvider table
distinct = QueryBuilder table -> queryBuilderProvider table
forall k (queryBuilderProvider :: Symbol -> *) (joinRegister :: k)
       (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
QueryBuilder table -> queryBuilderProvider table
injectQueryBuilder (QueryBuilder table -> queryBuilderProvider table)
-> (queryBuilderProvider table -> QueryBuilder table)
-> queryBuilderProvider table
-> queryBuilderProvider table
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 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
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 :: 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
injectQueryBuilder DistinctOnQueryBuilder :: forall (table :: Symbol).
QueryBuilder table -> ByteString -> QueryBuilder table
DistinctOnQueryBuilder { $sel:distinctOnColumn:NewQueryBuilder :: ByteString
distinctOnColumn = ByteString
columnName, $sel:queryBuilder:NewQueryBuilder :: QueryBuilder table
queryBuilder = queryBuilderProvider table -> QueryBuilder table
forall k (queryBuilderProvider :: Symbol -> *) (joinRegister :: k)
       (table :: Symbol).
HasQueryBuilder queryBuilderProvider joinRegister =>
queryBuilderProvider table -> QueryBuilder table
getQueryBuilder queryBuilderProvider table
queryBuilderProvider}
    where
        columnName :: ByteString
columnName = Table model => ByteString
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 (KnownSymbol name => Text
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