Copyright(c) digitally induced GmbH 2020
Safe HaskellNone

IHP.Fetch

Description

This modules builds on top of QueryBuilder and provides functions to fetch a query builder.

For more complex sql queries, use sqlQuery.

Synopsis

Documentation

findManyBy :: forall value (name :: Symbol) (table :: Symbol). (ToField value, EqOrIsOperator value, HasField name (GetModelByTableName table) value, KnownSymbol name, KnownSymbol table, KnownSymbol (GetTableName (GetModelByTableName table)), FromRow (GetModelByTableName table), ?modelContext :: ModelContext) => Proxy name -> value -> QueryBuilder table -> IO [GetModelByTableName table] Source #

findMaybeBy :: forall value (name :: Symbol) (table :: Symbol). (ToField value, EqOrIsOperator value, HasField name (GetModelByTableName table) value, KnownSymbol name, KnownSymbol table, KnownSymbol (GetTableName (GetModelByTableName table)), FromRow (GetModelByTableName table), ?modelContext :: ModelContext) => Proxy name -> value -> QueryBuilder table -> IO (Maybe (GetModelByTableName table)) Source #

findBy :: forall value (name :: Symbol) (table :: Symbol). (ToField value, EqOrIsOperator value, HasField name (GetModelByTableName table) value, KnownSymbol name, KnownSymbol table, KnownSymbol (GetTableName (GetModelByTableName table)), FromRow (GetModelByTableName table), ?modelContext :: ModelContext) => Proxy name -> value -> QueryBuilder table -> IO (GetModelByTableName table) Source #

newtype In a #

Constructors

In a 

Instances

Instances details
Functor In 
Instance details

Defined in Database.PostgreSQL.Simple.Types

Methods

fmap :: (a -> b) -> In a -> In b #

(<$) :: a -> In b -> In a #

Eq a => Eq (In a) 
Instance details

Defined in Database.PostgreSQL.Simple.Types

Methods

(==) :: In a -> In a -> Bool #

(/=) :: In a -> In a -> Bool #

Ord a => Ord (In a) 
Instance details

Defined in Database.PostgreSQL.Simple.Types

Methods

compare :: In a -> In a -> Ordering #

(<) :: In a -> In a -> Bool #

(<=) :: In a -> In a -> Bool #

(>) :: In a -> In a -> Bool #

(>=) :: In a -> In a -> Bool #

max :: In a -> In a -> In a #

min :: In a -> In a -> In a #

Read a => Read (In a) 
Instance details

Defined in Database.PostgreSQL.Simple.Types

Methods

readsPrec :: Int -> ReadS (In a)

readList :: ReadS [In a]

readPrec :: ReadPrec (In a)

readListPrec :: ReadPrec [In a]

Show a => Show (In a) 
Instance details

Defined in Database.PostgreSQL.Simple.Types

Methods

showsPrec :: Int -> In a -> ShowS #

show :: In a -> String

showList :: [In a] -> ShowS #

ToField a => ToField (In [a]) 
Instance details

Defined in Database.PostgreSQL.Simple.ToField

Methods

toField :: In [a] -> Action

genericFetchId :: forall table model. (KnownSymbol table, FromRow model, ?modelContext :: ModelContext, FilterPrimaryKey table, model ~ GetModelByTableName table, GetTableName model ~ table) => Id' table -> IO [model] Source #

genericfetchIdOneOrNothing :: forall table model. (KnownSymbol table, FromRow model, ?modelContext :: ModelContext, FilterPrimaryKey table, model ~ GetModelByTableName table, GetTableName model ~ table) => Id' table -> IO (Maybe model) Source #

genericFetchIdOne :: forall table model. (KnownSymbol table, FromRow model, ?modelContext :: ModelContext, FilterPrimaryKey table, model ~ GetModelByTableName table, GetTableName model ~ table) => Id' table -> IO model Source #

class Fetchable fetchable model | fetchable -> model where Source #

Associated Types

type FetchResult fetchable model Source #

Methods

fetch :: (KnownSymbol (GetTableName model), FromRow model, ?modelContext :: ModelContext) => fetchable -> IO (FetchResult fetchable model) Source #

fetchOneOrNothing :: (KnownSymbol (GetTableName model), FromRow model, ?modelContext :: ModelContext) => fetchable -> IO (Maybe model) Source #

fetchOne :: (KnownSymbol (GetTableName model), FromRow model, ?modelContext :: ModelContext) => fetchable -> IO model Source #

Instances

Instances details
(model ~ GetModelById (Id' table), value ~ Id' table, HasField "id" model value, ToField (PrimaryKey table), GetModelByTableName (GetTableName model) ~ model) => Fetchable [Id' table] model Source # 
Instance details

Defined in IHP.Fetch

Associated Types

type FetchResult [Id' table] model Source #

Methods

fetch :: [Id' table] -> IO (FetchResult [Id' table] model) Source #

fetchOneOrNothing :: [Id' table] -> IO (Maybe model) Source #

fetchOne :: [Id' table] -> IO model Source #

(model ~ GetModelById (Id' table), GetTableName model ~ table, FilterPrimaryKey table) => Fetchable (Maybe (Id' table)) model Source # 
Instance details

Defined in IHP.Fetch

Associated Types

type FetchResult (Maybe (Id' table)) model Source #

Methods

fetch :: Maybe (Id' table) -> IO (FetchResult (Maybe (Id' table)) model) Source #

fetchOneOrNothing :: Maybe (Id' table) -> IO (Maybe model) Source #

fetchOne :: Maybe (Id' table) -> IO model Source #

(model ~ GetModelById (Id' table), GetTableName model ~ table, FilterPrimaryKey table) => Fetchable (Id' table) model Source # 
Instance details

Defined in IHP.Fetch

Associated Types

type FetchResult (Id' table) model Source #

Methods

fetch :: Id' table -> IO (FetchResult (Id' table) model) Source #

fetchOneOrNothing :: Id' table -> IO (Maybe model) Source #

fetchOne :: Id' table -> IO model Source #

(model ~ GetModelByTableName table, KnownSymbol table) => Fetchable (QueryBuilder table) model Source # 
Instance details

Defined in IHP.Fetch

Associated Types

type FetchResult (QueryBuilder table) model Source #

Methods

fetch :: QueryBuilder table -> IO (FetchResult (QueryBuilder table) model) Source #

fetchOneOrNothing :: QueryBuilder table -> IO (Maybe model) Source #

fetchOne :: QueryBuilder table -> IO model Source #

genericFetchIds :: forall table model value. (KnownSymbol table, FromRow model, ?modelContext :: ModelContext, ToField value, EqOrIsOperator value, HasField "id" model value, model ~ GetModelByTableName table, GetTableName model ~ table) => [value] -> IO [model] Source #

genericfetchIdsOneOrNothing :: forall model value table. (KnownSymbol table, FromRow model, ?modelContext :: ModelContext, ToField value, EqOrIsOperator value, HasField "id" model value, model ~ GetModelByTableName table, GetTableName model ~ table) => [value] -> IO (Maybe model) Source #

genericFetchIdsOne :: forall model value table. (KnownSymbol table, FromRow model, ?modelContext :: ModelContext, ToField value, EqOrIsOperator value, HasField "id" model value, model ~ GetModelByTableName table, GetTableName model ~ table) => [value] -> IO model Source #

fetchCount :: forall table. (?modelContext :: ModelContext, KnownSymbol table) => QueryBuilder table -> IO Int Source #

Returns the count of records selected by the query builder.

Example: Counting all users.

allUsersCount <- query @User |> fetchCount -- SELECT COUNT(*) FROM users

Example: Counting all active projects

    activeProjectsCount <- query @Project
        |> filterWhere (#isActive, True)
        |> fetchCount
    -- SELECT COUNT(*) FROM projects WHERE is_active = true

fetchExists :: forall table. (?modelContext :: ModelContext, KnownSymbol table) => QueryBuilder table -> IO Bool Source #

Checks whether the query has any results.

Returns True when there is at least one row matching the conditions of the query. Returns False otherwise.

Example: Checking whether there are unread messages

    hasUnreadMessages <- query @Message
        |> filterWhere (#isUnread, True)
        |> fetchExists
    -- SELECT EXISTS (SELECT * FROM messages WHERE is_unread = true)