packages feed

hasql-mapping-0.1.1.2 / src / library / Hasql / Mapping / IsStatement.hs

module Hasql.Mapping.IsStatement whereimport qualified Hasql.Pipeline as Pipelineimport qualified Hasql.Session as Sessionimport qualified Hasql.Statement as Statementimport qualified Hasql.Transaction as Transaction-- |-- Evidence that a data-structure models statement parameters determining the statement and its result type.---- Supports a modularisation pattern, where you define everything related to one statement in an isolated module.-- This pattern leads to high code cohesion and low coupling.---- ==== __Example of such a module__---- > module MusicCatalogueDb.Statements.SelectArtistIdsByName where-- >-- > import Data.Functor.Contravariant-- > import Data.Text (Text)-- > import Data.UUID (UUID)-- > import Data.Vector (Vector)-- > import qualified Hasql.Decoders as Decoders-- > import qualified Hasql.Encoders as Encoders-- > import Hasql.Mapping.IsStatement-- > import Prelude-- >-- > data SelectArtistIdsByName = SelectArtistIdsByName-- >   { name :: Text-- >   }-- >-- > type SelectArtistIdsByNameResult = Vector SelectArtistIdsByNameResultRow-- >-- > data SelectArtistIdsByNameResultRow = SelectArtistIdsByNameResultRow-- >   { id :: UUID-- >   }-- >-- > instance IsStatement SelectArtistIdsByName where-- >   type Result SelectArtistIdsByName = SelectArtistIdsByNameResult-- >   statement =-- >     Statement.preparable sql encoder decoder-- >     where-- >       sql =-- >         "select id from artist\n\-- >         \where name = $1\n\-- >         \limit 1"-- >       encoder =-- >         mconcat-- >           [ (\(SelectArtistIdsByName x) -> x)-- >               >$< Encoders.param (Encoders.nonNullable Encoders.text)-- >           ]-- >       decoder =-- >         Decoders.rowVector-- >           ( SelectArtistIdsByNameResultRow-- >               <$> Decoders.column (Decoders.nonNullable Decoders.uuid)-- >           )class IsStatement a where  type Result a  statement :: Statement.Statement a (Result a)-- |-- Runs the statement as a session, the construct one level up the capability ladder. Statements-- have no retry axis, unlike 'Hasql.Mapping.IsTransaction.IsTransaction's runners, so this is the-- one bare form.toSession :: (IsStatement a) => a -> Session.Session (Result a)toSession a = Session.statement a statement-- |-- Runs the statement as a step of a 'Hasql.Mapping.IsTransaction.IsTransaction's 'Transaction.Transaction'.toTransaction :: (IsStatement a) => a -> Transaction.Transaction (Result a)toTransaction a = Transaction.statement a statement-- |-- Runs the statement as a step of a 'Hasql.Session.Session's 'Pipeline.Pipeline', batching it-- with the other statements pipelined alongside it instead of round-tripping for each.toPipeline :: (IsStatement a) => a -> Pipeline.Pipeline (Result a)toPipeline a = Pipeline.statement a statement