persistent-2.10.4: Type-safe, multi-backend data serialization.

Safe HaskellNone
LanguageHaskell2010

Database.Persist.Sql

Contents

Synopsis

Documentation

newtype Single a Source #

A single column (see rawSql). Any PersistField may be used here, including PersistValue (which does not do any processing).

Constructors

Single 

Fields

Instances
Eq a => Eq (Single a) Source # 
Instance details

Defined in Database.Persist.Sql.Types

Methods

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

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

Ord a => Ord (Single a) Source # 
Instance details

Defined in Database.Persist.Sql.Types

Methods

compare :: Single a -> Single a -> Ordering #

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

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

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

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

max :: Single a -> Single a -> Single a #

min :: Single a -> Single a -> Single a #

Read a => Read (Single a) Source # 
Instance details

Defined in Database.Persist.Sql.Types

Show a => Show (Single a) Source # 
Instance details

Defined in Database.Persist.Sql.Types

Methods

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

show :: Single a -> String #

showList :: [Single a] -> ShowS #

PersistField a => RawSql (Single a) Source # 
Instance details

Defined in Database.Persist.Sql.Class

type Migration = WriterT [Text] (WriterT CautiousMigration (ReaderT SqlBackend IO)) () Source #

A Migration is a four level monad stack consisting of:

type Sql = Text Source #

data Column Source #

Instances
Eq Column Source # 
Instance details

Defined in Database.Persist.Sql.Types

Methods

(==) :: Column -> Column -> Bool #

(/=) :: Column -> Column -> Bool #

Ord Column Source # 
Instance details

Defined in Database.Persist.Sql.Types

Show Column Source # 
Instance details

Defined in Database.Persist.Sql.Types

data SqlBackend Source #

Constructors

SqlBackend 

Fields

Instances
PersistStoreWrite SqlBackend Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

PersistStoreRead SqlBackend Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Methods

get :: (MonadIO m, PersistRecordBackend record SqlBackend) => Key record -> ReaderT SqlBackend m (Maybe record) Source #

getMany :: (MonadIO m, PersistRecordBackend record SqlBackend) => [Key record] -> ReaderT SqlBackend m (Map (Key record) record) Source #

PersistCore SqlBackend Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Associated Types

data BackendKey SqlBackend :: Type Source #

IsPersistBackend SqlBackend Source # 
Instance details

Defined in Database.Persist.Sql.Types.Internal

HasPersistBackend SqlBackend Source # 
Instance details

Defined in Database.Persist.Sql.Types.Internal

Associated Types

type BaseBackend SqlBackend :: Type Source #

PersistUniqueWrite SqlBackend Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistUnique

Methods

deleteBy :: (MonadIO m, PersistRecordBackend record SqlBackend) => Unique record -> ReaderT SqlBackend m () Source #

insertUnique :: (MonadIO m, PersistRecordBackend record SqlBackend) => record -> ReaderT SqlBackend m (Maybe (Key record)) Source #

upsert :: (MonadIO m, PersistRecordBackend record SqlBackend, OnlyOneUniqueKey record) => record -> [Update record] -> ReaderT SqlBackend m (Entity record) Source #

upsertBy :: (MonadIO m, PersistRecordBackend record SqlBackend) => Unique record -> record -> [Update record] -> ReaderT SqlBackend m (Entity record) Source #

putMany :: (MonadIO m, PersistRecordBackend record SqlBackend) => [record] -> ReaderT SqlBackend m () Source #

PersistUniqueRead SqlBackend Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistUnique

Methods

getBy :: (MonadIO m, PersistRecordBackend record SqlBackend) => Unique record -> ReaderT SqlBackend m (Maybe (Entity record)) Source #

PersistQueryWrite SqlBackend Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistQuery

PersistQueryRead SqlBackend Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistQuery

Methods

selectSourceRes :: (PersistRecordBackend record SqlBackend, MonadIO m1, MonadIO m2) => [Filter record] -> [SelectOpt record] -> ReaderT SqlBackend m1 (Acquire (ConduitM () (Entity record) m2 ())) Source #

selectFirst :: (MonadIO m, PersistRecordBackend record SqlBackend) => [Filter record] -> [SelectOpt record] -> ReaderT SqlBackend m (Maybe (Entity record)) Source #

selectKeysRes :: (MonadIO m1, MonadIO m2, PersistRecordBackend record SqlBackend) => [Filter record] -> [SelectOpt record] -> ReaderT SqlBackend m1 (Acquire (ConduitM () (Key record) m2 ())) Source #

count :: (MonadIO m, PersistRecordBackend record SqlBackend) => [Filter record] -> ReaderT SqlBackend m Int Source #

BackendCompatible SqlBackend SqlWriteBackend Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

BackendCompatible SqlBackend SqlReadBackend Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

BackendCompatible SqlBackend SqlBackend Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Bounded (BackendKey SqlBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Enum (BackendKey SqlBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Eq (BackendKey SqlBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Integral (BackendKey SqlBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Num (BackendKey SqlBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Ord (BackendKey SqlBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Read (BackendKey SqlBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Real (BackendKey SqlBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Show (BackendKey SqlBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

ToJSON (BackendKey SqlBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

FromJSON (BackendKey SqlBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

ToHttpApiData (BackendKey SqlBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

FromHttpApiData (BackendKey SqlBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

PathPiece (BackendKey SqlBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

PersistField (BackendKey SqlBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

PersistFieldSql (BackendKey SqlBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

newtype BackendKey SqlBackend Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

type BaseBackend SqlBackend Source # 
Instance details

Defined in Database.Persist.Sql.Types.Internal

newtype SqlReadBackend Source #

An SQL backend which can only handle read queries

The constructor was exposed in 2.10.0.

Instances
PersistStoreRead SqlReadBackend Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Methods

get :: (MonadIO m, PersistRecordBackend record SqlReadBackend) => Key record -> ReaderT SqlReadBackend m (Maybe record) Source #

getMany :: (MonadIO m, PersistRecordBackend record SqlReadBackend) => [Key record] -> ReaderT SqlReadBackend m (Map (Key record) record) Source #

PersistCore SqlReadBackend Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Associated Types

data BackendKey SqlReadBackend :: Type Source #

IsPersistBackend SqlReadBackend Source # 
Instance details

Defined in Database.Persist.Sql.Types.Internal

HasPersistBackend SqlReadBackend Source # 
Instance details

Defined in Database.Persist.Sql.Types.Internal

Associated Types

type BaseBackend SqlReadBackend :: Type Source #

PersistUniqueRead SqlReadBackend Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistUnique

PersistQueryRead SqlReadBackend Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistQuery

BackendCompatible SqlBackend SqlReadBackend Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Bounded (BackendKey SqlReadBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Enum (BackendKey SqlReadBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Eq (BackendKey SqlReadBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Integral (BackendKey SqlReadBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Num (BackendKey SqlReadBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Ord (BackendKey SqlReadBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Read (BackendKey SqlReadBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Real (BackendKey SqlReadBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Show (BackendKey SqlReadBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

ToJSON (BackendKey SqlReadBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

FromJSON (BackendKey SqlReadBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

ToHttpApiData (BackendKey SqlReadBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

FromHttpApiData (BackendKey SqlReadBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

PathPiece (BackendKey SqlReadBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

PersistField (BackendKey SqlReadBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

PersistFieldSql (BackendKey SqlReadBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

newtype BackendKey SqlReadBackend Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

type BaseBackend SqlReadBackend Source # 
Instance details

Defined in Database.Persist.Sql.Types.Internal

newtype SqlWriteBackend Source #

An SQL backend which can handle read or write queries

The constructor was exposed in 2.10.0

Instances
PersistStoreWrite SqlWriteBackend Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Methods

insert :: (MonadIO m, PersistRecordBackend record SqlWriteBackend) => record -> ReaderT SqlWriteBackend m (Key record) Source #

insert_ :: (MonadIO m, PersistRecordBackend record SqlWriteBackend) => record -> ReaderT SqlWriteBackend m () Source #

insertMany :: (MonadIO m, PersistRecordBackend record SqlWriteBackend) => [record] -> ReaderT SqlWriteBackend m [Key record] Source #

insertMany_ :: (MonadIO m, PersistRecordBackend record SqlWriteBackend) => [record] -> ReaderT SqlWriteBackend m () Source #

insertEntityMany :: (MonadIO m, PersistRecordBackend record SqlWriteBackend) => [Entity record] -> ReaderT SqlWriteBackend m () Source #

insertKey :: (MonadIO m, PersistRecordBackend record SqlWriteBackend) => Key record -> record -> ReaderT SqlWriteBackend m () Source #

repsert :: (MonadIO m, PersistRecordBackend record SqlWriteBackend) => Key record -> record -> ReaderT SqlWriteBackend m () Source #

repsertMany :: (MonadIO m, PersistRecordBackend record SqlWriteBackend) => [(Key record, record)] -> ReaderT SqlWriteBackend m () Source #

replace :: (MonadIO m, PersistRecordBackend record SqlWriteBackend) => Key record -> record -> ReaderT SqlWriteBackend m () Source #

delete :: (MonadIO m, PersistRecordBackend record SqlWriteBackend) => Key record -> ReaderT SqlWriteBackend m () Source #

update :: (MonadIO m, PersistRecordBackend record SqlWriteBackend) => Key record -> [Update record] -> ReaderT SqlWriteBackend m () Source #

updateGet :: (MonadIO m, PersistRecordBackend record SqlWriteBackend) => Key record -> [Update record] -> ReaderT SqlWriteBackend m record Source #

PersistStoreRead SqlWriteBackend Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Methods

get :: (MonadIO m, PersistRecordBackend record SqlWriteBackend) => Key record -> ReaderT SqlWriteBackend m (Maybe record) Source #

getMany :: (MonadIO m, PersistRecordBackend record SqlWriteBackend) => [Key record] -> ReaderT SqlWriteBackend m (Map (Key record) record) Source #

PersistCore SqlWriteBackend Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Associated Types

data BackendKey SqlWriteBackend :: Type Source #

IsPersistBackend SqlWriteBackend Source # 
Instance details

Defined in Database.Persist.Sql.Types.Internal

HasPersistBackend SqlWriteBackend Source # 
Instance details

Defined in Database.Persist.Sql.Types.Internal

Associated Types

type BaseBackend SqlWriteBackend :: Type Source #

PersistUniqueWrite SqlWriteBackend Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistUnique

PersistUniqueRead SqlWriteBackend Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistUnique

PersistQueryWrite SqlWriteBackend Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistQuery

PersistQueryRead SqlWriteBackend Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistQuery

BackendCompatible SqlBackend SqlWriteBackend Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Bounded (BackendKey SqlWriteBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Enum (BackendKey SqlWriteBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Eq (BackendKey SqlWriteBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Integral (BackendKey SqlWriteBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Num (BackendKey SqlWriteBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Ord (BackendKey SqlWriteBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Read (BackendKey SqlWriteBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Real (BackendKey SqlWriteBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Show (BackendKey SqlWriteBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

ToJSON (BackendKey SqlWriteBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

FromJSON (BackendKey SqlWriteBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

ToHttpApiData (BackendKey SqlWriteBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

FromHttpApiData (BackendKey SqlWriteBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

PathPiece (BackendKey SqlWriteBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

PersistField (BackendKey SqlWriteBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

PersistFieldSql (BackendKey SqlWriteBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

newtype BackendKey SqlWriteBackend Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

type BaseBackend SqlWriteBackend Source # 
Instance details

Defined in Database.Persist.Sql.Types.Internal

data Statement Source #

Constructors

Statement 

Fields

type LogFunc = Loc -> LogSource -> LogLevel -> LogStr -> IO () Source #

readToUnknown :: Monad m => ReaderT SqlReadBackend m a -> ReaderT SqlBackend m a Source #

Useful for running a read query against a backend with unknown capabilities.

readToWrite :: Monad m => ReaderT SqlReadBackend m a -> ReaderT SqlWriteBackend m a Source #

Useful for running a read query against a backend with read and write capabilities.

writeToUnknown :: Monad m => ReaderT SqlWriteBackend m a -> ReaderT SqlBackend m a Source #

Useful for running a write query against an untagged backend with unknown capabilities.

type SqlBackendCanRead backend = (BackendCompatible SqlBackend backend, PersistQueryRead backend, PersistStoreRead backend, PersistUniqueRead backend) Source #

A constraint synonym which witnesses that a backend is SQL and can run read queries.

type SqlBackendCanWrite backend = (SqlBackendCanRead backend, PersistQueryWrite backend, PersistStoreWrite backend, PersistUniqueWrite backend) Source #

A constraint synonym which witnesses that a backend is SQL and can run read and write queries.

type SqlReadT m a = forall backend. SqlBackendCanRead backend => ReaderT backend m a Source #

Like SqlPersistT but compatible with any SQL backend which can handle read queries.

type SqlWriteT m a = forall backend. SqlBackendCanWrite backend => ReaderT backend m a Source #

Like SqlPersistT but compatible with any SQL backend which can handle read and write queries.

type IsSqlBackend backend = (IsPersistBackend backend, BaseBackend backend ~ SqlBackend) Source #

A backend which is a wrapper around SqlBackend.

class RawSql a where Source #

Class for data types that may be retrived from a rawSql query.

Methods

rawSqlCols :: (DBName -> Text) -> a -> (Int, [Text]) Source #

Number of columns that this data type needs and the list of substitutions for SELECT placeholders ??.

rawSqlColCountReason :: a -> String Source #

A string telling the user why the column count is what it is.

rawSqlProcessRow :: [PersistValue] -> Either Text a Source #

Transform a row of the result into the data type.

Instances
RawSql a => RawSql (Maybe a) Source #

Since: 1.0.1

Instance details

Defined in Database.Persist.Sql.Class

(PersistEntity record, PersistEntityBackend record ~ backend, IsPersistBackend backend) => RawSql (Entity record) Source # 
Instance details

Defined in Database.Persist.Sql.Class

(PersistEntity a, PersistEntityBackend a ~ backend, IsPersistBackend backend) => RawSql (Key a) Source # 
Instance details

Defined in Database.Persist.Sql.Class

PersistField a => RawSql (Single a) Source # 
Instance details

Defined in Database.Persist.Sql.Class

(RawSql a, RawSql b) => RawSql (a, b) Source # 
Instance details

Defined in Database.Persist.Sql.Class

Methods

rawSqlCols :: (DBName -> Text) -> (a, b) -> (Int, [Text]) Source #

rawSqlColCountReason :: (a, b) -> String Source #

rawSqlProcessRow :: [PersistValue] -> Either Text (a, b) Source #

(RawSql a, RawSql b, RawSql c) => RawSql (a, b, c) Source # 
Instance details

Defined in Database.Persist.Sql.Class

Methods

rawSqlCols :: (DBName -> Text) -> (a, b, c) -> (Int, [Text]) Source #

rawSqlColCountReason :: (a, b, c) -> String Source #

rawSqlProcessRow :: [PersistValue] -> Either Text (a, b, c) Source #

(RawSql a, RawSql b, RawSql c, RawSql d) => RawSql (a, b, c, d) Source # 
Instance details

Defined in Database.Persist.Sql.Class

Methods

rawSqlCols :: (DBName -> Text) -> (a, b, c, d) -> (Int, [Text]) Source #

rawSqlColCountReason :: (a, b, c, d) -> String Source #

rawSqlProcessRow :: [PersistValue] -> Either Text (a, b, c, d) Source #

(RawSql a, RawSql b, RawSql c, RawSql d, RawSql e) => RawSql (a, b, c, d, e) Source # 
Instance details

Defined in Database.Persist.Sql.Class

Methods

rawSqlCols :: (DBName -> Text) -> (a, b, c, d, e) -> (Int, [Text]) Source #

rawSqlColCountReason :: (a, b, c, d, e) -> String Source #

rawSqlProcessRow :: [PersistValue] -> Either Text (a, b, c, d, e) Source #

(RawSql a, RawSql b, RawSql c, RawSql d, RawSql e, RawSql f) => RawSql (a, b, c, d, e, f) Source # 
Instance details

Defined in Database.Persist.Sql.Class

Methods

rawSqlCols :: (DBName -> Text) -> (a, b, c, d, e, f) -> (Int, [Text]) Source #

rawSqlColCountReason :: (a, b, c, d, e, f) -> String Source #

rawSqlProcessRow :: [PersistValue] -> Either Text (a, b, c, d, e, f) Source #

(RawSql a, RawSql b, RawSql c, RawSql d, RawSql e, RawSql f, RawSql g) => RawSql (a, b, c, d, e, f, g) Source # 
Instance details

Defined in Database.Persist.Sql.Class

Methods

rawSqlCols :: (DBName -> Text) -> (a, b, c, d, e, f, g) -> (Int, [Text]) Source #

rawSqlColCountReason :: (a, b, c, d, e, f, g) -> String Source #

rawSqlProcessRow :: [PersistValue] -> Either Text (a, b, c, d, e, f, g) Source #

(RawSql a, RawSql b, RawSql c, RawSql d, RawSql e, RawSql f, RawSql g, RawSql h) => RawSql (a, b, c, d, e, f, g, h) Source # 
Instance details

Defined in Database.Persist.Sql.Class

Methods

rawSqlCols :: (DBName -> Text) -> (a, b, c, d, e, f, g, h) -> (Int, [Text]) Source #

rawSqlColCountReason :: (a, b, c, d, e, f, g, h) -> String Source #

rawSqlProcessRow :: [PersistValue] -> Either Text (a, b, c, d, e, f, g, h) Source #

(RawSql a, RawSql b, RawSql c, RawSql d, RawSql e, RawSql f, RawSql g, RawSql h, RawSql i) => RawSql (a, b, c, d, e, f, g, h, i) Source #

Since: 2.10.2

Instance details

Defined in Database.Persist.Sql.Class

Methods

rawSqlCols :: (DBName -> Text) -> (a, b, c, d, e, f, g, h, i) -> (Int, [Text]) Source #

rawSqlColCountReason :: (a, b, c, d, e, f, g, h, i) -> String Source #

rawSqlProcessRow :: [PersistValue] -> Either Text (a, b, c, d, e, f, g, h, i) Source #

(RawSql a, RawSql b, RawSql c, RawSql d, RawSql e, RawSql f, RawSql g, RawSql h, RawSql i, RawSql j) => RawSql (a, b, c, d, e, f, g, h, i, j) Source #

Since: 2.10.2

Instance details

Defined in Database.Persist.Sql.Class

Methods

rawSqlCols :: (DBName -> Text) -> (a, b, c, d, e, f, g, h, i, j) -> (Int, [Text]) Source #

rawSqlColCountReason :: (a, b, c, d, e, f, g, h, i, j) -> String Source #

rawSqlProcessRow :: [PersistValue] -> Either Text (a, b, c, d, e, f, g, h, i, j) Source #

(RawSql a, RawSql b, RawSql c, RawSql d, RawSql e, RawSql f, RawSql g, RawSql h, RawSql i, RawSql j, RawSql k) => RawSql (a, b, c, d, e, f, g, h, i, j, k) Source #

Since: 2.10.2

Instance details

Defined in Database.Persist.Sql.Class

Methods

rawSqlCols :: (DBName -> Text) -> (a, b, c, d, e, f, g, h, i, j, k) -> (Int, [Text]) Source #

rawSqlColCountReason :: (a, b, c, d, e, f, g, h, i, j, k) -> String Source #

rawSqlProcessRow :: [PersistValue] -> Either Text (a, b, c, d, e, f, g, h, i, j, k) Source #

(RawSql a, RawSql b, RawSql c, RawSql d, RawSql e, RawSql f, RawSql g, RawSql h, RawSql i, RawSql j, RawSql k, RawSql l) => RawSql (a, b, c, d, e, f, g, h, i, j, k, l) Source #

Since: 2.10.2

Instance details

Defined in Database.Persist.Sql.Class

Methods

rawSqlCols :: (DBName -> Text) -> (a, b, c, d, e, f, g, h, i, j, k, l) -> (Int, [Text]) Source #

rawSqlColCountReason :: (a, b, c, d, e, f, g, h, i, j, k, l) -> String Source #

rawSqlProcessRow :: [PersistValue] -> Either Text (a, b, c, d, e, f, g, h, i, j, k, l) Source #

class PersistField a => PersistFieldSql a where Source #

Tells Persistent what database column type should be used to store a Haskell type.

Examples

Expand
Simple Boolean Alternative
data Switch = On | Off
  deriving (Show, Eq)

instance PersistField Switch where
  toPersistValue s = case s of
    On -> PersistBool True
    Off -> PersistBool False
  fromPersistValue (PersistBool b) = if b then Right On else Right Off
  fromPersistValue x = Left $ "File.hs: When trying to deserialize a Switch: expected PersistBool, received: " <> T.pack (show x)

instance PersistFieldSql Switch where
  sqlType _ = SqlBool
Non-Standard Database Types

If your database supports non-standard types, such as Postgres' uuid, you can use SqlOther to use them:

import qualified Data.UUID as UUID
instance PersistField UUID where
  toPersistValue = PersistDbSpecific . toASCIIBytes
  fromPersistValue (PersistDbSpecific uuid) =
    case fromASCIIBytes uuid of
      Nothing -> Left $ "Model/CustomTypes.hs: Failed to deserialize a UUID; received: " <> T.pack (show uuid)
      Just uuid' -> Right uuid'
  fromPersistValue x = Left $ "File.hs: When trying to deserialize a UUID: expected PersistDbSpecific, received: "-- >  <> T.pack (show x)

instance PersistFieldSql UUID where
  sqlType _ = SqlOther "uuid"
User Created Database Types

Similarly, some databases support creating custom types, e.g. Postgres' DOMAIN and ENUM features. You can use SqlOther to specify a custom type:

CREATE DOMAIN ssn AS text
      CHECK ( value ~ '^[0-9]{9}$');
instance PersistFieldSQL SSN where
  sqlType _ = SqlOther "ssn"
CREATE TYPE rainbow_color AS ENUM ('red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet');
instance PersistFieldSQL RainbowColor where
  sqlType _ = SqlOther "rainbow_color"

Methods

sqlType :: Proxy a -> SqlType Source #

Instances
PersistFieldSql Bool Source # 
Instance details

Defined in Database.Persist.Sql.Class

PersistFieldSql Double Source # 
Instance details

Defined in Database.Persist.Sql.Class

PersistFieldSql Int Source # 
Instance details

Defined in Database.Persist.Sql.Class

PersistFieldSql Int8 Source # 
Instance details

Defined in Database.Persist.Sql.Class

PersistFieldSql Int16 Source # 
Instance details

Defined in Database.Persist.Sql.Class

PersistFieldSql Int32 Source # 
Instance details

Defined in Database.Persist.Sql.Class

PersistFieldSql Int64 Source # 
Instance details

Defined in Database.Persist.Sql.Class

PersistFieldSql Natural Source # 
Instance details

Defined in Database.Persist.Sql.Class

PersistFieldSql Rational Source # 
Instance details

Defined in Database.Persist.Sql.Class

PersistFieldSql Word Source # 
Instance details

Defined in Database.Persist.Sql.Class

PersistFieldSql Word8 Source # 
Instance details

Defined in Database.Persist.Sql.Class

PersistFieldSql Word16 Source # 
Instance details

Defined in Database.Persist.Sql.Class

PersistFieldSql Word32 Source # 
Instance details

Defined in Database.Persist.Sql.Class

PersistFieldSql Word64 Source # 
Instance details

Defined in Database.Persist.Sql.Class

PersistFieldSql ByteString Source # 
Instance details

Defined in Database.Persist.Sql.Class

PersistFieldSql Text Source # 
Instance details

Defined in Database.Persist.Sql.Class

PersistFieldSql UTCTime Source # 
Instance details

Defined in Database.Persist.Sql.Class

PersistFieldSql Text Source # 
Instance details

Defined in Database.Persist.Sql.Class

PersistFieldSql Html Source # 
Instance details

Defined in Database.Persist.Sql.Class

PersistFieldSql TimeOfDay Source # 
Instance details

Defined in Database.Persist.Sql.Class

PersistFieldSql Day Source # 
Instance details

Defined in Database.Persist.Sql.Class

PersistFieldSql PersistValue Source # 
Instance details

Defined in Database.Persist.Sql.Class

PersistFieldSql Checkmark Source # 
Instance details

Defined in Database.Persist.Sql.Class

PersistFieldSql [Char] Source # 
Instance details

Defined in Database.Persist.Sql.Class

Methods

sqlType :: Proxy [Char] -> SqlType Source #

PersistFieldSql a => PersistFieldSql [a] Source # 
Instance details

Defined in Database.Persist.Sql.Class

Methods

sqlType :: Proxy [a] -> SqlType Source #

HasResolution a => PersistFieldSql (Fixed a) Source # 
Instance details

Defined in Database.Persist.Sql.Class

Methods

sqlType :: Proxy (Fixed a) -> SqlType Source #

PersistFieldSql v => PersistFieldSql (IntMap v) Source # 
Instance details

Defined in Database.Persist.Sql.Class

Methods

sqlType :: Proxy (IntMap v) -> SqlType Source #

(Ord a, PersistFieldSql a) => PersistFieldSql (Set a) Source # 
Instance details

Defined in Database.Persist.Sql.Class

Methods

sqlType :: Proxy (Set a) -> SqlType Source #

PersistFieldSql a => PersistFieldSql (Vector a) Source # 
Instance details

Defined in Database.Persist.Sql.Class

Methods

sqlType :: Proxy (Vector a) -> SqlType Source #

(PersistField record, PersistEntity record) => PersistFieldSql (Entity record) Source # 
Instance details

Defined in Database.Persist.Sql.Class

Methods

sqlType :: Proxy (Entity record) -> SqlType Source #

PersistFieldSql (BackendKey SqlWriteBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

PersistFieldSql (BackendKey SqlReadBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

PersistFieldSql (BackendKey SqlBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

(PersistFieldSql a, PersistFieldSql b) => PersistFieldSql (a, b) Source # 
Instance details

Defined in Database.Persist.Sql.Class

Methods

sqlType :: Proxy (a, b) -> SqlType Source #

PersistFieldSql v => PersistFieldSql (Map Text v) Source # 
Instance details

Defined in Database.Persist.Sql.Class

Methods

sqlType :: Proxy (Map Text v) -> SqlType Source #

runSqlPool :: (MonadUnliftIO m, BackendCompatible SqlBackend backend) => ReaderT backend m a -> Pool backend -> m a Source #

Get a connection from the pool, run the given action, and then return the connection to the pool.

Note: This function previously timed out after 2 seconds, but this behavior was buggy and caused more problems than it solved. Since version 2.1.2, it performs no timeout checks.

runSqlPoolWithIsolation :: (MonadUnliftIO m, BackendCompatible SqlBackend backend) => ReaderT backend m a -> Pool backend -> IsolationLevel -> m a Source #

Like runSqlPool, but supports specifying an isolation level.

Since: 2.9.0

runSqlConn :: (MonadUnliftIO m, BackendCompatible SqlBackend backend) => ReaderT backend m a -> backend -> m a Source #

runSqlConnWithIsolation :: (MonadUnliftIO m, BackendCompatible SqlBackend backend) => ReaderT backend m a -> backend -> IsolationLevel -> m a Source #

Like runSqlConn, but supports specifying an isolation level.

Since: 2.9.0

runSqlPersistM :: BackendCompatible SqlBackend backend => ReaderT backend (NoLoggingT (ResourceT IO)) a -> backend -> IO a Source #

withSqlPool Source #

Arguments

:: (MonadLogger m, MonadUnliftIO m, BackendCompatible SqlBackend backend) 
=> (LogFunc -> IO backend)

create a new connection

-> Int

connection count

-> (Pool backend -> m a) 
-> m a 

createSqlPool :: forall m backend. (MonadLogger m, MonadUnliftIO m, BackendCompatible SqlBackend backend) => (LogFunc -> IO backend) -> Int -> m (Pool backend) Source #

withSqlConn :: (MonadUnliftIO m, MonadLogger m, BackendCompatible SqlBackend backend) => (LogFunc -> IO backend) -> (backend -> m a) -> m a Source #

Create a connection and run sql queries within it. This function automatically closes the connection on it's completion.

Example usage

Expand
{-# LANGUAGE GADTs #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies#-}
{-# LANGUAGE TemplateHaskell#-}
{-# LANGUAGE QuasiQuotes#-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}

import Control.Monad.IO.Class  (liftIO)
import Control.Monad.Logger
import Conduit
import Database.Persist
import Database.Sqlite
import Database.Persist.Sqlite
import Database.Persist.TH

share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
Person
  name String
  age Int Maybe
  deriving Show
|]

openConnection :: LogFunc -> IO SqlBackend
openConnection logfn = do
 conn <- open "/home/sibi/test.db"
 wrapConnection conn logfn

main :: IO ()
main = do
  runNoLoggingT $ runResourceT $ withSqlConn openConnection (\backend ->
                                      flip runSqlConn backend $ do
                                        runMigration migrateAll
                                        insert_ $ Person "John doe" $ Just 35
                                        insert_ $ Person "Divya" $ Just 36
                                        (pers :: [Entity Person]) <- selectList [] []
                                        liftIO $ print pers
                                        return ()
                                     )

On executing it, you get this output:

Migrating: CREATE TABLE "person"("id" INTEGER PRIMARY KEY,"name" VARCHAR NOT NULL,"age" INTEGER NULL)
[Entity {entityKey = PersonKey {unPersonKey = SqlBackendKey {unSqlBackendKey = 1}}, entityVal = Person {personName = "John doe", personAge = Just 35}},Entity {entityKey = PersonKey {unPersonKey = SqlBackendKey {unSqlBackendKey = 2}}, entityVal = Person {personName = "Hema", personAge = Just 36}}]

close' :: BackendCompatible SqlBackend backend => backend -> IO () Source #

parseMigration :: MonadIO m => Migration -> ReaderT SqlBackend m (Either [Text] CautiousMigration) Source #

Given a Migration, this parses it and returns either a list of errors associated with the migration or a list of migrations to do.

parseMigration' :: MonadIO m => Migration -> ReaderT SqlBackend m CautiousMigration Source #

Like parseMigration, but instead of returning the value in an Either value, it calls error on the error values.

printMigration :: MonadIO m => Migration -> ReaderT SqlBackend m () Source #

Prints a migration.

showMigration :: MonadIO m => Migration -> ReaderT SqlBackend m [Text] Source #

Convert a Migration to a list of Text values corresponding to their Sql statements.

getMigration :: MonadIO m => Migration -> ReaderT SqlBackend m [Sql] Source #

Return all of the Sql values associated with the given migration. Calls error if there's a parse error on any migration.

runMigration :: MonadIO m => Migration -> ReaderT SqlBackend m () Source #

Runs a migration. If the migration fails to parse or if any of the migrations are unsafe, then this calls error to halt the program.

runMigrationQuiet :: MonadIO m => Migration -> ReaderT SqlBackend m [Text] Source #

Same as runMigration, but does not report the individual migrations on stderr. Instead it returns a list of the executed SQL commands.

This is a safer/more robust alternative to runMigrationSilent, but may be less silent for some persistent implementations, most notably persistent-postgresql

Since: 2.10.2

runMigrationSilent :: MonadUnliftIO m => Migration -> ReaderT SqlBackend m [Text] Source #

Same as runMigration, but returns a list of the SQL commands executed instead of printing them to stderr.

This function silences the migration by remapping stderr. As a result, it is not thread-safe and can clobber output from other parts of the program. This implementation method was chosen to also silence postgresql migration output on stderr, but is not recommended!

runMigrationUnsafe :: MonadIO m => Migration -> ReaderT SqlBackend m () Source #

Like runMigration, but this will perform the unsafe database migrations instead of erroring out.

runMigrationUnsafeQuiet :: MonadIO m => Migration -> ReaderT SqlBackend m [Text] Source #

Same as runMigrationUnsafe, but returns a list of the SQL commands executed instead of printing them to stderr.

Since: 2.10.2

migrate :: [EntityDef] -> EntityDef -> Migration Source #

Given a list of old entity definitions and a new EntityDef in val, this creates a Migration to update the old list of definitions with the new one.

Utilities for constructing migrations

reportErrors :: [Text] -> Migration Source #

Report multiple errors in a Migration.

Since: 2.9.2

reportError :: Text -> Migration Source #

Report a single error in a Migration.

Since: 2.9.2

addMigrations :: CautiousMigration -> Migration Source #

Add a CautiousMigration (aka a [(Bool, Text)]) to the migration plan.

Since: 2.9.2

addMigration Source #

Arguments

:: Bool

Is the migration safe to run? (eg a non-destructive and idempotent update on the schema)

-> Sql

A Text value representing the command to run on the database.

-> Migration 

Add a migration to the migration plan.

Since: 2.9.2

data family BackendKey backend Source #

Instances
Bounded (BackendKey SqlWriteBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Bounded (BackendKey SqlReadBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Bounded (BackendKey SqlBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Enum (BackendKey SqlWriteBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Enum (BackendKey SqlReadBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Enum (BackendKey SqlBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Eq (BackendKey SqlWriteBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Eq (BackendKey SqlReadBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Eq (BackendKey SqlBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Integral (BackendKey SqlWriteBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Integral (BackendKey SqlReadBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Integral (BackendKey SqlBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Num (BackendKey SqlWriteBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Num (BackendKey SqlReadBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Num (BackendKey SqlBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Ord (BackendKey SqlWriteBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Ord (BackendKey SqlReadBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Ord (BackendKey SqlBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Read (BackendKey SqlWriteBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Read (BackendKey SqlReadBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Read (BackendKey SqlBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Real (BackendKey SqlWriteBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Real (BackendKey SqlReadBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Real (BackendKey SqlBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Show (BackendKey SqlWriteBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Show (BackendKey SqlReadBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

Show (BackendKey SqlBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

ToJSON (BackendKey SqlWriteBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

ToJSON (BackendKey SqlReadBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

ToJSON (BackendKey SqlBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

FromJSON (BackendKey SqlWriteBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

FromJSON (BackendKey SqlReadBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

FromJSON (BackendKey SqlBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

ToHttpApiData (BackendKey SqlWriteBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

ToHttpApiData (BackendKey SqlReadBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

ToHttpApiData (BackendKey SqlBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

FromHttpApiData (BackendKey SqlWriteBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

FromHttpApiData (BackendKey SqlReadBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

FromHttpApiData (BackendKey SqlBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

PathPiece (BackendKey SqlWriteBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

PathPiece (BackendKey SqlReadBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

PathPiece (BackendKey SqlBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

PersistField (BackendKey SqlWriteBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

PersistField (BackendKey SqlReadBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

PersistField (BackendKey SqlBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

PersistFieldSql (BackendKey SqlWriteBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

PersistFieldSql (BackendKey SqlReadBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

PersistFieldSql (BackendKey SqlBackend) Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

newtype BackendKey SqlWriteBackend Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

newtype BackendKey SqlReadBackend Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

newtype BackendKey SqlBackend Source # 
Instance details

Defined in Database.Persist.Sql.Orphan.PersistStore

getFieldName :: forall record typ m backend. (PersistEntity record, PersistEntityBackend record ~ SqlBackend, BackendCompatible SqlBackend backend, Monad m) => EntityField record typ -> ReaderT backend m Text Source #

get the SQL string for the field that an EntityField represents Useful for raw SQL queries

Your backend may provide a more convenient fieldName function which does not operate in a Monad

getTableName :: forall record m backend. (PersistEntity record, BackendCompatible SqlBackend backend, Monad m) => record -> ReaderT backend m Text Source #

get the SQL string for the table that a PeristEntity represents Useful for raw SQL queries

Your backend may provide a more convenient tableName function which does not operate in a Monad

tableDBName :: PersistEntity record => record -> DBName Source #

useful for a backend to implement tableName by adding escaping

fieldDBName :: forall record typ. PersistEntity record => EntityField record typ -> DBName Source #

useful for a backend to implement fieldName by adding escaping

rawExecute Source #

Arguments

:: (MonadIO m, BackendCompatible SqlBackend backend) 
=> Text

SQL statement, possibly with placeholders.

-> [PersistValue]

Values to fill the placeholders.

-> ReaderT backend m () 

Execute a raw SQL statement

rawExecuteCount Source #

Arguments

:: (MonadIO m, BackendCompatible SqlBackend backend) 
=> Text

SQL statement, possibly with placeholders.

-> [PersistValue]

Values to fill the placeholders.

-> ReaderT backend m Int64 

Execute a raw SQL statement and return the number of rows it has modified.

rawSql Source #

Arguments

:: (RawSql a, MonadIO m, BackendCompatible SqlBackend backend) 
=> Text

SQL statement, possibly with placeholders.

-> [PersistValue]

Values to fill the placeholders.

-> ReaderT backend m [a] 

Execute a raw SQL statement and return its results as a list. If you do not expect a return value, use of rawExecute is recommended.

If you're using Entitys (which is quite likely), then you must use entity selection placeholders (double question mark, ??). These ?? placeholders are then replaced for the names of the columns that we need for your entities. You'll receive an error if you don't use the placeholders. Please see the Entitys documentation for more details.

You may put value placeholders (question marks, ?) in your SQL query. These placeholders are then replaced by the values you pass on the second parameter, already correctly escaped. You may want to use toPersistValue to help you constructing the placeholder values.

Since you're giving a raw SQL statement, you don't get any guarantees regarding safety. If rawSql is not able to parse the results of your query back, then an exception is raised. However, most common problems are mitigated by using the entity selection placeholder ??, and you shouldn't see any error at all if you're not using Single.

Some example of rawSql based on this schema:

share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
Person
    name String
    age Int Maybe
    deriving Show
BlogPost
    title String
    authorId PersonId
    deriving Show
|]

Examples based on the above schema:

getPerson :: MonadIO m => ReaderT SqlBackend m [Entity Person]
getPerson = rawSql "select ?? from person where name=?" [PersistText "john"]

getAge :: MonadIO m => ReaderT SqlBackend m [Single Int]
getAge = rawSql "select person.age from person where name=?" [PersistText "john"]

getAgeName :: MonadIO m => ReaderT SqlBackend m [(Single Int, Single Text)]
getAgeName = rawSql "select person.age, person.name from person where name=?" [PersistText "john"]

getPersonBlog :: MonadIO m => ReaderT SqlBackend m [(Entity Person, Entity BlogPost)]
getPersonBlog = rawSql "select ??,?? from person,blog_post where person.id = blog_post.author_id" []

Minimal working program for PostgreSQL backend based on the above concepts:

{-# LANGUAGE EmptyDataDecls             #-}
{-# LANGUAGE FlexibleContexts           #-}
{-# LANGUAGE GADTs                      #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses      #-}
{-# LANGUAGE OverloadedStrings          #-}
{-# LANGUAGE QuasiQuotes                #-}
{-# LANGUAGE TemplateHaskell            #-}
{-# LANGUAGE TypeFamilies               #-}

import           Control.Monad.IO.Class  (liftIO)
import           Control.Monad.Logger    (runStderrLoggingT)
import           Database.Persist
import           Control.Monad.Reader
import           Data.Text
import           Database.Persist.Sql
import           Database.Persist.Postgresql
import           Database.Persist.TH

share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
Person
    name String
    age Int Maybe
    deriving Show
|]

conn = "host=localhost dbname=new_db user=postgres password=postgres port=5432"

getPerson :: MonadIO m => ReaderT SqlBackend m [Entity Person]
getPerson = rawSql "select ?? from person where name=?" [PersistText "sibi"]

liftSqlPersistMPool y x = liftIO (runSqlPersistMPool y x)

main :: IO ()
main = runStderrLoggingT $ withPostgresqlPool conn 10 $ liftSqlPersistMPool $ do
         runMigration migrateAll
         xs <- getPerson
         liftIO (print xs)

deleteWhereCount :: (PersistEntity val, MonadIO m, PersistEntityBackend val ~ SqlBackend, BackendCompatible SqlBackend backend) => [Filter val] -> ReaderT backend m Int64 Source #

Same as deleteWhere, but returns the number of rows affected.

Since: 1.1.5

updateWhereCount :: (PersistEntity val, MonadIO m, SqlBackend ~ PersistEntityBackend val, BackendCompatible SqlBackend backend) => [Filter val] -> [Update val] -> ReaderT backend m Int64 Source #

Same as updateWhere, but returns the number of rows affected.

Since: 1.1.5

transactionSave :: MonadIO m => ReaderT SqlBackend m () Source #

Commit the current transaction and begin a new one. This is used when a transaction commit is required within the context of runSqlConn (which brackets its provided action with a transaction begin/commit pair).

Since: 1.2.0

transactionSaveWithIsolation :: MonadIO m => IsolationLevel -> ReaderT SqlBackend m () Source #

Commit the current transaction and begin a new one with the specified isolation level.

Since: 2.9.0

transactionUndo :: MonadIO m => ReaderT SqlBackend m () Source #

Roll back the current transaction and begin a new one. This rolls back to the state of the last call to transactionSave or the enclosing runSqlConn call.

Since: 1.2.0

transactionUndoWithIsolation :: MonadIO m => IsolationLevel -> ReaderT SqlBackend m () Source #

Roll back the current transaction and begin a new one with the specified isolation level.

Since: 2.9.0

data IsolationLevel Source #

Please refer to the documentation for the database in question for a full overview of the semantics of the varying isloation levels

Instances
Bounded IsolationLevel Source # 
Instance details

Defined in Database.Persist.Sql.Types.Internal

Enum IsolationLevel Source # 
Instance details

Defined in Database.Persist.Sql.Types.Internal

Eq IsolationLevel Source # 
Instance details

Defined in Database.Persist.Sql.Types.Internal

Ord IsolationLevel Source # 
Instance details

Defined in Database.Persist.Sql.Types.Internal

Show IsolationLevel Source # 
Instance details

Defined in Database.Persist.Sql.Types.Internal

Internal

mkColumns :: [EntityDef] -> EntityDef -> ([Column], [UniqueDef], [ForeignDef]) Source #

Create the list of columns for the given entity.

decorateSQLWithLimitOffset :: Text -> (Int, Int) -> Bool -> Text -> Text Source #

Generates sql for limit and offset for postgres, sqlite and mysql.