hasql-mapping
Safe HaskellNone
LanguageHaskell2010

Hasql.Mapping.IsStatement

Synopsis

Documentation

class IsStatement a where #

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

Expand
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)
          )

Associated Types

type Result a #

Methods

statement :: Statement a (Result a) #

toSession :: IsStatement a => a -> Session (Result a) #

Runs the statement as a session, the construct one level up the capability ladder. Statements have no retry axis, unlike IsTransactions runners, so this is the one bare form.

toTransaction :: IsStatement a => a -> Transaction (Result a) #

Runs the statement as a step of a IsTransactions Transaction.

toPipeline :: IsStatement a => a -> Pipeline (Result a) #

Runs the statement as a step of a Sessions Pipeline, batching it with the other statements pipelined alongside it instead of round-tripping for each.