persistent-postgresql-2.13.7.0: Backend for the persistent library using postgresql.

Safe Haskell | None
Language | Haskell2010

Database.Persist.Postgresql

Description

A postgresql backend for persistent.

Synopsis

- withPostgresqlPool :: (MonadLoggerIO m, MonadUnliftIO m) => ConnectionString -> Int -> (Pool SqlBackend -> m a) -> m a
- withPostgresqlPoolWithVersion :: (MonadUnliftIO m, MonadLoggerIO m) => (Connection -> IO (Maybe Double)) -> ConnectionString -> Int -> (Pool SqlBackend -> m a) -> m a
- withPostgresqlPoolWithConf :: (MonadUnliftIO m, MonadLoggerIO m) => PostgresConf -> PostgresConfHooks -> (Pool SqlBackend -> m a) -> m a
- withPostgresqlPoolModified :: (MonadUnliftIO m, MonadLoggerIO m) => (Connection -> IO ()) -> ConnectionString -> Int -> (Pool SqlBackend -> m t) -> m t
- withPostgresqlPoolModifiedWithVersion :: (MonadUnliftIO m, MonadLoggerIO m) => (Connection -> IO (Maybe Double)) -> (Connection -> IO ()) -> ConnectionString -> Int -> (Pool SqlBackend -> m t) -> m t
- withPostgresqlConn :: (MonadUnliftIO m, MonadLoggerIO m) => ConnectionString -> (SqlBackend -> m a) -> m a
- withPostgresqlConnWithVersion :: (MonadUnliftIO m, MonadLoggerIO m) => (Connection -> IO (Maybe Double)) -> ConnectionString -> (SqlBackend -> m a) -> m a
- createPostgresqlPool :: (MonadUnliftIO m, MonadLoggerIO m) => ConnectionString -> Int -> m (Pool SqlBackend)
- createPostgresqlPoolModified :: (MonadUnliftIO m, MonadLoggerIO m) => (Connection -> IO ()) -> ConnectionString -> Int -> m (Pool SqlBackend)
- createPostgresqlPoolModifiedWithVersion :: (MonadUnliftIO m, MonadLoggerIO m) => (Connection -> IO (Maybe Double)) -> (Connection -> IO ()) -> ConnectionString -> Int -> m (Pool SqlBackend)
- createPostgresqlPoolTailored :: (MonadUnliftIO m, MonadLoggerIO m) => ((Connection -> IO ()) -> (Connection -> IO (NonEmpty Word)) -> ((Connection -> SqlBackend) -> Connection -> SqlBackend) -> ConnectionString -> LogFunc -> IO SqlBackend) -> (Connection -> IO (Maybe Double)) -> (Connection -> IO ()) -> ConnectionString -> Int -> m (Pool SqlBackend)
- createPostgresqlPoolWithConf :: (MonadUnliftIO m, MonadLoggerIO m) => PostgresConf -> PostgresConfHooks -> m (Pool SqlBackend)
- module Database.Persist.Sql
- type ConnectionString = ByteString
- data HandleUpdateCollision record
- copyField :: PersistField typ => EntityField record typ -> HandleUpdateCollision record
- copyUnlessNull :: PersistField typ => EntityField record (Maybe typ) -> HandleUpdateCollision record
- copyUnlessEmpty :: (Monoid typ, PersistField typ) => EntityField record typ -> HandleUpdateCollision record
- copyUnlessEq :: PersistField typ => EntityField record typ -> typ -> HandleUpdateCollision record
- excludeNotEqualToOriginal :: (PersistField typ, PersistEntity rec) => EntityField rec typ -> Filter rec
- data PostgresConf = PostgresConf {
  - pgConnStr :: ConnectionString
  - pgPoolStripes :: Int
  - pgPoolIdleTimeout :: Integer
  - pgPoolSize :: Int
  }
- newtype PgInterval = PgInterval {
  - getPgInterval :: NominalDiffTime
  }
- upsertWhere :: forall backend record (m :: Type -> Type). (backend ~ PersistEntityBackend record, PersistEntity record, PersistEntityBackend record ~ SqlBackend, MonadIO m, PersistStore backend, BackendCompatible SqlBackend backend, OnlyOneUniqueKey record) => record -> [Update record] -> [Filter record] -> ReaderT backend m ()
- upsertManyWhere :: forall record backend (m :: Type -> Type). (backend ~ PersistEntityBackend record, BackendCompatible SqlBackend backend, PersistEntityBackend record ~ SqlBackend, PersistEntity record, OnlyOneUniqueKey record, MonadIO m) => [record] -> [HandleUpdateCollision record] -> [Update record] -> [Filter record] -> ReaderT backend m ()
- openSimpleConn :: LogFunc -> Connection -> IO SqlBackend
- openSimpleConnWithVersion :: (Connection -> IO (Maybe Double)) -> LogFunc -> Connection -> IO SqlBackend
- getServerVersion :: Connection -> IO (Maybe Double)
- getSimpleConn :: BackendCompatible SqlBackend backend => backend -> Maybe Connection
- tableName :: PersistEntity record => record -> Text
- fieldName :: PersistEntity record => EntityField record typ -> Text
- mockMigration :: Migration -> IO ()
- migrateEnableExtension :: Text -> Migration
- data PostgresConfHooks = PostgresConfHooks {
  - pgConfHooksGetServerVersion :: Connection -> IO (NonEmpty Word)
  - pgConfHooksAfterCreate :: Connection -> IO ()
  }
- defaultPostgresConfHooks :: PostgresConfHooks
- data RawPostgresql backend = RawPostgresql {
  - persistentBackend :: backend
  - rawPostgresqlConnection :: Connection
  }
- createRawPostgresqlPool :: (MonadUnliftIO m, MonadLoggerIO m) => ConnectionString -> Int -> m (Pool (RawPostgresql SqlBackend))
- createRawPostgresqlPoolModified :: (MonadUnliftIO m, MonadLoggerIO m) => (Connection -> IO ()) -> ConnectionString -> Int -> m (Pool (RawPostgresql SqlBackend))
- createRawPostgresqlPoolModifiedWithVersion :: (MonadUnliftIO m, MonadLoggerIO m) => (Connection -> IO (Maybe Double)) -> (Connection -> IO ()) -> ConnectionString -> Int -> m (Pool (RawPostgresql SqlBackend))
- createRawPostgresqlPoolWithConf :: (MonadUnliftIO m, MonadLoggerIO m) => PostgresConf -> PostgresConfHooks -> m (Pool (RawPostgresql SqlBackend))
- createBackend :: LogFunc -> NonEmpty Word -> IORef (Map Text Statement) -> Connection -> SqlBackend

# Documentation

withPostgresqlPool Source #

Arguments

:: (MonadLoggerIO m, MonadUnliftIO m) |
=> ConnectionString | Connection string to the database.
-> Int | Number of connections to be kept open in the pool.
-> (Pool SqlBackend -> m a) | Action to be executed that uses the connection pool.
-> m a |

Create a PostgreSQL connection pool and run the given action. The pool is properly released after the action finishes using it. Note that you should not use the given `ConnectionPool` outside the action since it may already have been released. The provided action should use `runSqlConn` and *not* `runReaderT` because the former brackets the database action with transaction begin/commit.

withPostgresqlPoolWithVersion Source #

Arguments

:: (MonadUnliftIO m, MonadLoggerIO m) |
=> (Connection -> IO (Maybe Double)) | Action to perform to get the server version.
-> ConnectionString | Connection string to the database.
-> Int | Number of connections to be kept open in the pool.
-> (Pool SqlBackend -> m a) | Action to be executed that uses the connection pool.
-> m a |

Same as `withPostgresPool`, but takes a callback for obtaining the server version (to work around an Amazon Redshift bug).

Since: 2.6.2

withPostgresqlPoolWithConf Source #

Arguments

:: (MonadUnliftIO m, MonadLoggerIO m) |
=> PostgresConf | Configuration for connecting to Postgres
-> PostgresConfHooks | Record of callback functions
-> (Pool SqlBackend -> m a) | Action to be executed that uses the connection pool.
-> m a |

Same as `withPostgresqlPool`, but can be configured with `PostgresConf` and `PostgresConfHooks`.

Since: 2.11.0.0

withPostgresqlPoolModified Source #

Arguments

:: (MonadUnliftIO m, MonadLoggerIO m) |
=> (Connection -> IO ()) | Action to perform after connection is created.
-> ConnectionString | Connection string to the database.
-> Int | Number of connections to be kept open in the pool.
-> (Pool SqlBackend -> m t) |
-> m t |

Same as `withPostgresqlPool`, but with the `createPostgresqlPoolModified` feature.

Since: 2.13.5.0

withPostgresqlPoolModifiedWithVersion Source #

Arguments

:: (MonadUnliftIO m, MonadLoggerIO m) |
=> (Connection -> IO (Maybe Double)) | Action to perform to get the server version.
-> (Connection -> IO ()) | Action to perform after connection is created.
-> ConnectionString | Connection string to the database.
-> Int | Number of connections to be kept open in the pool.
-> (Pool SqlBackend -> m t) |
-> m t |

Same as `withPostgresqlPool`, but with the `createPostgresqlPoolModifiedWithVersion` feature.

Since: 2.13.5.0

withPostgresqlConn :: (MonadUnliftIO m, MonadLoggerIO m) => ConnectionString -> (SqlBackend -> m a) -> m a Source #

Same as `withPostgresqlPool`, but instead of opening a pool of connections, only one connection is opened. The provided action should use `runSqlConn` and *not* `runReaderT` because the former brackets the database action with transaction begin/commit.

withPostgresqlConnWithVersion :: (MonadUnliftIO m, MonadLoggerIO m) => (Connection -> IO (Maybe Double)) -> ConnectionString -> (SqlBackend -> m a) -> m a Source #

Same as `withPostgresqlConn`, but takes a callback for obtaining the server version (to work around an Amazon Redshift bug).

Since: 2.6.2

createPostgresqlPool Source #

Arguments

:: (MonadUnliftIO m, MonadLoggerIO m) |
=> ConnectionString | Connection string to the database.
-> Int | Number of connections to be kept open in the pool.
-> m (Pool SqlBackend) |

Create a PostgreSQL connection pool. Note that it's your responsibility to properly close the connection pool when unneeded. Use `withPostgresqlPool` for an automatic resource control.

createPostgresqlPoolModified Source #

Arguments

:: (MonadUnliftIO m, MonadLoggerIO m) |
=> (Connection -> IO ()) | Action to perform after connection is created.
-> ConnectionString | Connection string to the database.
-> Int | Number of connections to be kept open in the pool.
-> m (Pool SqlBackend) |

Same as `createPostgresqlPool`, but additionally takes a callback function for some connection-specific tweaking to be performed after connection creation. This could be used, for example, to change the schema. For more information, see:

https://groups.google.com/d/msg/yesodweb/qUXrEN_swEo/O0pFwqwQIdcJ

Since: 2.1.3

createPostgresqlPoolModifiedWithVersion Source #

Arguments

:: (MonadUnliftIO m, MonadLoggerIO m) |
=> (Connection -> IO (Maybe Double)) | Action to perform to get the server version.
-> (Connection -> IO ()) | Action to perform after connection is created.
-> ConnectionString | Connection string to the database.
-> Int | Number of connections to be kept open in the pool.
-> m (Pool SqlBackend) |

Same as other similarly-named functions in this module, but takes callbacks for obtaining the server version (to work around an Amazon Redshift bug) and connection-specific tweaking (to change the schema).

Since: 2.6.2

createPostgresqlPoolTailored Source #

Arguments

:: (MonadUnliftIO m, MonadLoggerIO m) |
=> ((Connection -> IO ()) -> (Connection -> IO (NonEmpty Word)) -> ((Connection -> SqlBackend) -> Connection -> SqlBackend) -> ConnectionString -> LogFunc -> IO SqlBackend) | Action that creates a postgresql connection (please see documentation on the un-exported `open'` function in this same module.
-> (Connection -> IO (Maybe Double)) | Action to perform to get the server version.
-> (Connection -> IO ()) | Action to perform after connection is created.
-> ConnectionString | Connection string to the database.
-> Int | Number of connections to be kept open in the pool.
-> m (Pool SqlBackend) |

Same as `createPostgresqlPoolModifiedWithVersion`, but takes a custom connection-creation function.

The only time you should reach for this function is if you need to write custom logic for creating a connection to the database.

Since: 2.13.6

createPostgresqlPoolWithConf Source #

Arguments

:: (MonadUnliftIO m, MonadLoggerIO m) |
=> PostgresConf | Configuration for connecting to Postgres
-> PostgresConfHooks | Record of callback functions
-> m (Pool SqlBackend) |

Same as `createPostgresqlPool`, but can be configured with `PostgresConf` and `PostgresConfHooks`.

Since: 2.11.0.0

module Database.Persist.Sql

type ConnectionString = ByteString Source #

A `libpq` connection string. A simple example of connection string would be `"host=localhost port=5432 user=test dbname=test password=test"`. Please read libpq's documentation at https://www.postgresql.org/docs/current/static/libpq-connect.html for more details on how to create such strings.

data HandleUpdateCollision record Source #

This type is used to determine how to update rows using Postgres' `INSERT ... ON CONFLICT KEY UPDATE` functionality, exposed via `upsertWhere` and `upsertManyWhere` in this library.

Since: 2.12.1.0

copyField :: PersistField typ => EntityField record typ -> HandleUpdateCollision record Source #

Copy the field directly from the record.

Since: 2.12.1.0

copyUnlessNull :: PersistField typ => EntityField record (Maybe typ) -> HandleUpdateCollision record Source #

Copy the field into the database only if the value in the corresponding record is non-`NULL`.

@since 2.12.1.0

copyUnlessEmpty :: (Monoid typ, PersistField typ) => EntityField record typ -> HandleUpdateCollision record Source #

Copy the field into the database only if the value in the corresponding record is non-empty, where "empty" means the Monoid definition for `mempty`. Useful for `Text`, `String`, `ByteString`, etc.

The resulting `HandleUpdateCollision` type is useful for the `upsertManyWhere` function.

@since 2.12.1.0

copyUnlessEq :: PersistField typ => EntityField record typ -> typ -> HandleUpdateCollision record Source #

Copy the field into the database only if the field is not equal to the provided value. This is useful to avoid copying weird nullary data into the database.

The resulting `HandleUpdateCollision` type is useful for the `upsertMany` function.

@since 2.12.1.0

excludeNotEqualToOriginal :: (PersistField typ, PersistEntity rec) => EntityField rec typ -> Filter rec Source #

Exclude any record field if it doesn't match the filter record. Used only in `upsertWhere` and `upsertManyWhere`

TODO: we could probably make a sum type for the `Filter` record that's passed into the `upsertWhere` and `upsertManyWhere` methods that has similar behavior to the HandleCollisionUpdate type.

Since: 2.12.1.0

data PostgresConf Source #

Information required to connect to a PostgreSQL database using `persistent`'s generic facilities. These values are the same that are given to `withPostgresqlPool`.

Constructors

PostgresConf |

Fields

- pgConnStr :: ConnectionString

  The connection string.

- pgPoolStripes :: Int

  How many stripes to divide the pool into. See Data.Pool for details. @since 2.11.0.0

- pgPoolIdleTimeout :: Integer

  How long connections can remain idle before being disposed of, in seconds. @since 2.11.0.0

- pgPoolSize :: Int

  How many connections should be held in the connection pool.

#### Instances

Instances details

FromJSON PostgresConf Source # |
Instance details Defined in Database.Persist.Postgresql Methods parseJSON :: Value -> Parser PostgresConf Source # parseJSONList :: Value -> Parser [PostgresConf] Source # omittedField :: Maybe PostgresConf Source #
Data PostgresConf Source # |

Instance details

Defined in Database.Persist.Postgresql

Methods

gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> PostgresConf -> c PostgresConf Source #

gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c PostgresConf Source #

toConstr :: PostgresConf -> Constr Source #

dataTypeOf :: PostgresConf -> DataType Source #

dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c PostgresConf) Source #

dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c PostgresConf) Source #

gmapT :: (forall b. Data b => b -> b) -> PostgresConf -> PostgresConf Source #

gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> PostgresConf -> r Source #

gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> PostgresConf -> r Source #

gmapQ :: (forall d. Data d => d -> u) -> PostgresConf -> [u] Source #

gmapQi :: Int -> (forall d. Data d => d -> u) -> PostgresConf -> u Source #

gmapM :: Monad m => (forall d. Data d => d -> m d) -> PostgresConf -> m PostgresConf Source #

gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> PostgresConf -> m PostgresConf Source #

gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> PostgresConf -> m PostgresConf Source #

Read PostgresConf Source # |
Instance details Defined in Database.Persist.Postgresql Methods readsPrec :: Int -> ReadS PostgresConf Source # readList :: ReadS [PostgresConf] Source # readPrec :: ReadPrec PostgresConf Source # readListPrec :: ReadPrec [PostgresConf] Source #
Show PostgresConf Source # |
Instance details Defined in Database.Persist.Postgresql Methods showsPrec :: Int -> PostgresConf -> ShowS Source # show :: PostgresConf -> String Source # showList :: [PostgresConf] -> ShowS Source #
PersistConfig PostgresConf Source # |

Instance details

Defined in Database.Persist.Postgresql

Associated Types

type PersistConfigBackend PostgresConf |
Instance details Defined in Database.Persist.Postgresql type PersistConfigBackend PostgresConf = SqlPersistT
type PersistConfigPool PostgresConf |
Instance details Defined in Database.Persist.Postgresql type PersistConfigPool PostgresConf = ConnectionPool

Methods

loadConfig :: Value -> Parser PostgresConf Source #

applyEnv :: PostgresConf -> IO PostgresConf Source #

createPoolConfig :: PostgresConf -> IO (PersistConfigPool PostgresConf) Source #

runPool :: MonadUnliftIO m => PostgresConf -> PersistConfigBackend PostgresConf m a -> PersistConfigPool PostgresConf -> m a Source #

type PersistConfigBackend PostgresConf Source # |
Instance details Defined in Database.Persist.Postgresql type PersistConfigBackend PostgresConf = SqlPersistT
type PersistConfigPool PostgresConf Source # |
Instance details Defined in Database.Persist.Postgresql type PersistConfigPool PostgresConf = ConnectionPool

newtype PgInterval Source #

Represent Postgres interval using NominalDiffTime

Since: 2.11.0.0

Constructors

PgInterval |

Fields

- getPgInterval :: NominalDiffTime

#### Instances

Instances details

Show PgInterval Source # |
Instance details Defined in Database.Persist.Postgresql.Internal Methods showsPrec :: Int -> PgInterval -> ShowS Source # show :: PgInterval -> String Source # showList :: [PgInterval] -> ShowS Source #
Eq PgInterval Source # |
Instance details Defined in Database.Persist.Postgresql.Internal Methods (==) :: PgInterval -> PgInterval -> Bool Source # (/=) :: PgInterval -> PgInterval -> Bool Source #
PersistField PgInterval Source # |
Instance details Defined in Database.Persist.Postgresql.Internal Methods toPersistValue :: PgInterval -> PersistValue Source # fromPersistValue :: PersistValue -> Either Text PgInterval Source #
PersistFieldSql PgInterval Source # |
Instance details Defined in Database.Persist.Postgresql.Internal Methods sqlType :: Proxy PgInterval -> SqlType Source #
FromField PgInterval Source # |
Instance details Defined in Database.Persist.Postgresql.Internal Methods fromField :: FieldParser PgInterval Source #
ToField PgInterval Source # |
Instance details Defined in Database.Persist.Postgresql.Internal Methods toField :: PgInterval -> Action Source #

upsertWhere :: forall backend record (m :: Type -> Type). (backend ~ PersistEntityBackend record, PersistEntity record, PersistEntityBackend record ~ SqlBackend, MonadIO m, PersistStore backend, BackendCompatible SqlBackend backend, OnlyOneUniqueKey record) => record -> [Update record] -> [Filter record] -> ReaderT backend m () Source #

Postgres specific `upsertWhere`. This method does the following: It will insert a record if no matching unique key exists. If a unique key exists, it will update the relevant field with a user-supplied value, however, it will only do this update on a user-supplied condition. For example, here's how this method could be called like such:

    upsertWhere record [recordField =. newValue] [recordField /= newValue]

Called thusly, this method will insert a new record (if none exists) OR update a recordField with a new value assuming the condition in the last block is met.

Since: 2.12.1.0

upsertManyWhere Source #

Arguments

:: forall record backend (m :: Type -> Type). (backend ~ PersistEntityBackend record, BackendCompatible SqlBackend backend, PersistEntityBackend record ~ SqlBackend, PersistEntity record, OnlyOneUniqueKey record, MonadIO m) |
=> [record] | A list of the records you want to insert, or update
-> [HandleUpdateCollision record] | A list of the fields you want to copy over.
-> [Update record] | A list of the updates to apply that aren't dependent on the record being inserted.
-> [Filter record] | A filter condition that dictates the scope of the updates
-> ReaderT backend m () |

Postgres specific `upsertManyWhere`.

The first argument is a list of new records to insert.

If there's unique key collisions for some or all of the proposed insertions, you can use the second argument to specify which fields, under which conditions (using `HandleUpdateCollision` strategies,) will be copied from each proposed, conflicting new record onto its corresponding row already present in the database. Helpers such as `copyField`, `copyUnlessEq`, `copyUnlessNull` and `copyUnlessEmpty` are exported from this module to help with this, as well as the constructors for `HandleUpdateCollision`. For example, you may have a patch of updates to apply, some of which are NULL to represent "no change"; you'd use `copyUnlessNull` for each applicable field in this case so any new values in the patch which are NULL don't accidentally overwrite existing values in the database which are non-NULL.

Further updates to the matching records already in the database can be specified in the third argument, these are arbitrary updates independent of the new records proposed for insertion. For example, you can use this to update a timestamp or a counter for all conflicting rows.

You can use the fourth argument to provide arbitrary conditions to constrain the above updates. This way, you can choose to only alter a subset of the conflicting existing rows while leaving the others alone. For example, you may want to copy fields from new records and update a timestamp only when the corresponding existing row hasn't been soft-deleted.

For example,

      upsertManyWhere
        [record]                         -- (1)
        [ copyField recordField1         -- (2)
        , copyUnlessEq recordField2      -- (3)
        , copyUnlessNull recordField3    -- (4)
        ]
        [recordField4 =. arbitraryValue] -- (5)
        [recordField4 !=. anotherValue]  -- (6)

1. new records to insert if there's no conflicts
2. for each conflicting existing row, replace the value of recordField1 with the one present in the conflicting new record
3. only replace the existing value if it's different from the one present in the conflicting new record
4. only replace the existing value if the new value is non-NULL (i.e. don't replace existing values with NULLs.)
5. update recordField4 with an arbitrary new value
6. only apply the above updates for conflicting rows that meet this condition

Called thusly, this method will insert a new record (if none exists) OR it will copy three fields (`recordField1`, `recordField2`, `recordField3`) from the record proposed from insertion onto the existing row in the database following three different `HandleUpdateCollision` strategies, it will also update `recordField4` with an arbitrary `arbitraryValue`. Both sets of updates will only be applied if the row already present in the database has a `recordField4` whose value is not `arbitraryValue`. The corresponding SQL might look like this:

      INSERT INTO table(recordField1, recordField2, recordField3, recordField4, recordField5)
      VALUES
        (newValue1, newValue2, newValue3, newValue4, newValue5)
      ON CONFLICT (recordField2, recordField3) -- let's imagine these two columns define a unique constraint
      DO UPDATE
        SET
          recordField1 = EXCLUDED.newValue1, -- EXCLUDED points to the new, conflicting, record; so we're always replacing the old value.
          recordField2 = COALESCE(NULLIF(EXCLUDED.newValue2, table.recordField2), table.recordField2), -- if the values are the same, the NULLIF returns NULL and we coalesce into setting the column to the value it already had (i.e. no change)
          recordField3 = COALESCE(NULLIF(EXCLUDED.newValue3, NULL), table.recordField3), -- if the new value is not null, it will be set; if it's null, we coalesce into not changing the existing value.
          recordField4 = arbitraryValue -- an arbitrary update independent of what's in the EXCLUDED (new) record
        WHERE
          recordField4 <> anotherValue -- only do the above updates if the value for the existing row's recordField4 is not "anotherValue"

Since: 2.12.1.0

openSimpleConn :: LogFunc -> Connection -> IO SqlBackend Source #

Generate a `SqlBackend` from a `Connection`.

openSimpleConnWithVersion :: (Connection -> IO (Maybe Double)) -> LogFunc -> Connection -> IO SqlBackend Source #

Generate a `SqlBackend` from a `Connection`, but takes a callback for obtaining the server version.

Since: 2.9.1

getServerVersion :: Connection -> IO (Maybe Double) Source #

Gets the PostgreSQL server version

Since: 2.13.6

getSimpleConn :: BackendCompatible SqlBackend backend => backend -> Maybe Connection Source #

Access underlying connection, returning `Nothing` if the `SqlBackend` provided isn't backed by postgresql-simple.

Since: 2.13.0

tableName :: PersistEntity record => record -> Text Source #

Get the SQL string for the table that a PersistEntity represents. Useful for raw SQL queries.

fieldName :: PersistEntity record => EntityField record typ -> Text Source #

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

mockMigration :: Migration -> IO () Source #

Mock a migration even when the database is not present. This function performs the same functionality of `printMigration` with the difference that an actual database is not needed.

migrateEnableExtension :: Text -> Migration Source #

Enable a Postgres extension. See https://www.postgresql.org/docs/current/static/contrib.html for a list.

data PostgresConfHooks Source #

Hooks for configuring the Persistent/its connection to Postgres

Since: 2.11.0

Constructors

PostgresConfHooks |

Fields

- pgConfHooksGetServerVersion :: Connection -> IO (NonEmpty Word)

  Function to get the version of Postgres

  The default implementation queries the server with "show server_version". Some variants of Postgres, such as Redshift, don't support showing the version. It's recommended you return a hardcoded version in those cases.

  Since: 2.11.0

- pgConfHooksAfterCreate :: Connection -> IO ()

  Action to perform after a connection is created.

  Typical uses of this are modifying the connection (e.g. to set the schema) or logging a connection being created.

  The default implementation does nothing.

  Since: 2.11.0

defaultPostgresConfHooks :: PostgresConfHooks Source #

Default settings for `PostgresConfHooks`. See the individual fields of `PostgresConfHooks` for the default values.

Since: 2.11.0

data RawPostgresql backend Source #

Wrapper for persistent SqlBackends that carry the corresponding `Connection`.

Since: 2.13.1.0

Constructors

RawPostgresql |

Fields

- persistentBackend :: backend

  The persistent backend

  Since: 2.13.1.0

- rawPostgresqlConnection :: Connection

  The underlying `Connection`

  Since: 2.13.1.0

#### Instances

Instances details

BackendCompatible b (RawPostgresql b) Source # |
Instance details Defined in Database.Persist.Postgresql Methods projectBackend :: RawPostgresql b -> b Source #
(PersistCore b, PersistCore (RawPostgresql b), FromJSON (BackendKey b)) => FromJSON (BackendKey (RawPostgresql b)) Source # |
Instance details Defined in Database.Persist.Postgresql Methods parseJSON :: Value -> Parser (BackendKey (RawPostgresql b)) Source # parseJSONList :: Value -> Parser [BackendKey (RawPostgresql b)] Source # omittedField :: Maybe (BackendKey (RawPostgresql b)) Source #
(PersistCore b, PersistCore (RawPostgresql b), ToJSON (BackendKey b)) => ToJSON (BackendKey (RawPostgresql b)) Source # |
Instance details Defined in Database.Persist.Postgresql Methods toJSON :: BackendKey (RawPostgresql b) -> Value Source # toEncoding :: BackendKey (RawPostgresql b) -> Encoding Source # toJSONList :: [BackendKey (RawPostgresql b)] -> Value Source # toEncodingList :: [BackendKey (RawPostgresql b)] -> Encoding Source # omitField :: BackendKey (RawPostgresql b) -> Bool Source #
(PersistCore b, PersistCore (RawPostgresql b), Bounded (BackendKey b)) => Bounded (BackendKey (RawPostgresql b)) Source # |
Instance details Defined in Database.Persist.Postgresql Methods minBound :: BackendKey (RawPostgresql b) Source # maxBound :: BackendKey (RawPostgresql b) Source #
(PersistCore b, PersistCore (RawPostgresql b), Enum (BackendKey b)) => Enum (BackendKey (RawPostgresql b)) Source # |

Instance details

Defined in Database.Persist.Postgresql

Methods

succ :: BackendKey (RawPostgresql b) -> BackendKey (RawPostgresql b) Source #

pred :: BackendKey (RawPostgresql b) -> BackendKey (RawPostgresql b) Source #

toEnum :: Int -> BackendKey (RawPostgresql b) Source #

fromEnum :: BackendKey (RawPostgresql b) -> Int Source #

enumFrom :: BackendKey (RawPostgresql b) -> [BackendKey (RawPostgresql b)] Source #

enumFromThen :: BackendKey (RawPostgresql b) -> BackendKey (RawPostgresql b) -> [BackendKey (RawPostgresql b)] Source #

enumFromTo :: BackendKey (RawPostgresql b) -> BackendKey (RawPostgresql b) -> [BackendKey (RawPostgresql b)] Source #

enumFromThenTo :: BackendKey (RawPostgresql b) -> BackendKey (RawPostgresql b) -> BackendKey (RawPostgresql b) -> [BackendKey (RawPostgresql b)] Source #

(PersistCore b, PersistCore (RawPostgresql b), Num (BackendKey b)) => Num (BackendKey (RawPostgresql b)) Source # |

Instance details

Defined in Database.Persist.Postgresql

Methods

(+) :: BackendKey (RawPostgresql b) -> BackendKey (RawPostgresql b) -> BackendKey (RawPostgresql b) Source #

(-) :: BackendKey (RawPostgresql b) -> BackendKey (RawPostgresql b) -> BackendKey (RawPostgresql b) Source #

(*) :: BackendKey (RawPostgresql b) -> BackendKey (RawPostgresql b) -> BackendKey (RawPostgresql b) Source #

negate :: BackendKey (RawPostgresql b) -> BackendKey (RawPostgresql b) Source #

abs :: BackendKey (RawPostgresql b) -> BackendKey (RawPostgresql b) Source #

signum :: BackendKey (RawPostgresql b) -> BackendKey (RawPostgresql b) Source #

fromInteger :: Integer -> BackendKey (RawPostgresql b) Source #

(PersistCore b, PersistCore (RawPostgresql b), Read (BackendKey b)) => Read (BackendKey (RawPostgresql b)) Source # |
Instance details Defined in Database.Persist.Postgresql Methods readsPrec :: Int -> ReadS (BackendKey (RawPostgresql b)) Source # readList :: ReadS [BackendKey (RawPostgresql b)] Source # readPrec :: ReadPrec (BackendKey (RawPostgresql b)) Source # readListPrec :: ReadPrec [BackendKey (RawPostgresql b)] Source #
(PersistCore b, PersistCore (RawPostgresql b), Integral (BackendKey b)) => Integral (BackendKey (RawPostgresql b)) Source # |

Instance details

Defined in Database.Persist.Postgresql

Methods

quot :: BackendKey (RawPostgresql b) -> BackendKey (RawPostgresql b) -> BackendKey (RawPostgresql b) Source #

rem :: BackendKey (RawPostgresql b) -> BackendKey (RawPostgresql b) -> BackendKey (RawPostgresql b) Source #

div :: BackendKey (RawPostgresql b) -> BackendKey (RawPostgresql b) -> BackendKey (RawPostgresql b) Source #

mod :: BackendKey (RawPostgresql b) -> BackendKey (RawPostgresql b) -> BackendKey (RawPostgresql b) Source #

quotRem :: BackendKey (RawPostgresql b) -> BackendKey (RawPostgresql b) -> (BackendKey (RawPostgresql b), BackendKey (RawPostgresql b)) Source #

divMod :: BackendKey (RawPostgresql b) -> BackendKey (RawPostgresql b) -> (BackendKey (RawPostgresql b), BackendKey (RawPostgresql b)) Source #

toInteger :: BackendKey (RawPostgresql b) -> Integer Source #

(PersistCore b, PersistCore (RawPostgresql b), Real (BackendKey b)) => Real (BackendKey (RawPostgresql b)) Source # |
Instance details Defined in Database.Persist.Postgresql Methods toRational :: BackendKey (RawPostgresql b) -> Rational Source #
(PersistCore b, PersistCore (RawPostgresql b), Show (BackendKey b)) => Show (BackendKey (RawPostgresql b)) Source # |
Instance details Defined in Database.Persist.Postgresql Methods showsPrec :: Int -> BackendKey (RawPostgresql b) -> ShowS Source # show :: BackendKey (RawPostgresql b) -> String Source # showList :: [BackendKey (RawPostgresql b)] -> ShowS Source #
(PersistCore b, PersistCore (RawPostgresql b), Eq (BackendKey b)) => Eq (BackendKey (RawPostgresql b)) Source # |
Instance details Defined in Database.Persist.Postgresql Methods (==) :: BackendKey (RawPostgresql b) -> BackendKey (RawPostgresql b) -> Bool Source # (/=) :: BackendKey (RawPostgresql b) -> BackendKey (RawPostgresql b) -> Bool Source #
(PersistCore b, PersistCore (RawPostgresql b), Ord (BackendKey b)) => Ord (BackendKey (RawPostgresql b)) Source # |

Instance details

Defined in Database.Persist.Postgresql

Methods

compare :: BackendKey (RawPostgresql b) -> BackendKey (RawPostgresql b) -> Ordering Source #

(<) :: BackendKey (RawPostgresql b) -> BackendKey (RawPostgresql b) -> Bool Source #

(<=) :: BackendKey (RawPostgresql b) -> BackendKey (RawPostgresql b) -> Bool Source #

(>) :: BackendKey (RawPostgresql b) -> BackendKey (RawPostgresql b) -> Bool Source #

(>=) :: BackendKey (RawPostgresql b) -> BackendKey (RawPostgresql b) -> Bool Source #

max :: BackendKey (RawPostgresql b) -> BackendKey (RawPostgresql b) -> BackendKey (RawPostgresql b) Source #

min :: BackendKey (RawPostgresql b) -> BackendKey (RawPostgresql b) -> BackendKey (RawPostgresql b) Source #

(PersistCore b, PersistCore (RawPostgresql b), PersistField (BackendKey b)) => PersistField (BackendKey (RawPostgresql b)) Source # |
Instance details Defined in Database.Persist.Postgresql Methods toPersistValue :: BackendKey (RawPostgresql b) -> PersistValue Source # fromPersistValue :: PersistValue -> Either Text (BackendKey (RawPostgresql b)) Source #
(HasPersistBackend b, PersistQueryRead b) => PersistQueryRead (RawPostgresql b) Source # |

Instance details

Defined in Database.Persist.Postgresql

Methods

selectSourceRes :: forall record (m1 :: Type -> Type) (m2 :: Type -> Type). (PersistRecordBackend record (RawPostgresql b), MonadIO m1, MonadIO m2) => [Filter record] -> [SelectOpt record] -> ReaderT (RawPostgresql b) m1 (Acquire (ConduitM () (Entity record) m2 ())) Source #

selectFirst :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record (RawPostgresql b)) => [Filter record] -> [SelectOpt record] -> ReaderT (RawPostgresql b) m (Maybe (Entity record)) Source #

selectKeysRes :: forall (m1 :: Type -> Type) (m2 :: Type -> Type) record. (MonadIO m1, MonadIO m2, PersistRecordBackend record (RawPostgresql b)) => [Filter record] -> [SelectOpt record] -> ReaderT (RawPostgresql b) m1 (Acquire (ConduitM () (Key record) m2 ())) Source #

count :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record (RawPostgresql b)) => [Filter record] -> ReaderT (RawPostgresql b) m Int Source #

exists :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record (RawPostgresql b)) => [Filter record] -> ReaderT (RawPostgresql b) m Bool Source #

(HasPersistBackend b, PersistQueryWrite b) => PersistQueryWrite (RawPostgresql b) Source # |

Instance details

Defined in Database.Persist.Postgresql

Methods

updateWhere :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record (RawPostgresql b)) => [Filter record] -> [Update record] -> ReaderT (RawPostgresql b) m () Source #

deleteWhere :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record (RawPostgresql b)) => [Filter record] -> ReaderT (RawPostgresql b) m () Source #

HasPersistBackend b => HasPersistBackend (RawPostgresql b) Source # |

Instance details

Defined in Database.Persist.Postgresql

Associated Types

type BaseBackend (RawPostgresql b0) |
Instance details Defined in Database.Persist.Postgresql type BaseBackend (RawPostgresql b0) = BaseBackend (Compatible b0 (RawPostgresql b0))

Methods

persistBackend :: RawPostgresql b -> BaseBackend (RawPostgresql b) Source #

PersistCore b => PersistCore (RawPostgresql b) Source # |

Instance details

Defined in Database.Persist.Postgresql

Associated Types

newtype BackendKey (RawPostgresql b) |

Instance details

Defined in Database.Persist.Postgresql

newtype BackendKey (RawPostgresql b) = RawPostgresqlKey {

- unRawPostgresqlKey :: BackendKey (Compatible b (RawPostgresql b))

}

(HasPersistBackend b, PersistStoreRead b) => PersistStoreRead (RawPostgresql b) Source # |

Instance details

Defined in Database.Persist.Postgresql

Methods

get :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record (RawPostgresql b)) => Key record -> ReaderT (RawPostgresql b) m (Maybe record) Source #

getMany :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record (RawPostgresql b)) => [Key record] -> ReaderT (RawPostgresql b) m (Map (Key record) record) Source #

(HasPersistBackend b, PersistStoreWrite b) => PersistStoreWrite (RawPostgresql b) Source # |

Instance details

Defined in Database.Persist.Postgresql

Methods

insert :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record (RawPostgresql b), SafeToInsert record) => record -> ReaderT (RawPostgresql b) m (Key record) Source #

insert_ :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record (RawPostgresql b), SafeToInsert record) => record -> ReaderT (RawPostgresql b) m () Source #

insertMany :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record (RawPostgresql b), SafeToInsert record) => [record] -> ReaderT (RawPostgresql b) m [Key record] Source #

insertMany_ :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record (RawPostgresql b), SafeToInsert record) => [record] -> ReaderT (RawPostgresql b) m () Source #

insertEntityMany :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record (RawPostgresql b)) => [Entity record] -> ReaderT (RawPostgresql b) m () Source #

insertKey :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record (RawPostgresql b)) => Key record -> record -> ReaderT (RawPostgresql b) m () Source #

repsert :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record (RawPostgresql b)) => Key record -> record -> ReaderT (RawPostgresql b) m () Source #

repsertMany :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record (RawPostgresql b)) => [(Key record, record)] -> ReaderT (RawPostgresql b) m () Source #

replace :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record (RawPostgresql b)) => Key record -> record -> ReaderT (RawPostgresql b) m () Source #

delete :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record (RawPostgresql b)) => Key record -> ReaderT (RawPostgresql b) m () Source #

update :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record (RawPostgresql b)) => Key record -> [Update record] -> ReaderT (RawPostgresql b) m () Source #

updateGet :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record (RawPostgresql b)) => Key record -> [Update record] -> ReaderT (RawPostgresql b) m record Source #

(HasPersistBackend b, PersistUniqueRead b) => PersistUniqueRead (RawPostgresql b) Source # |

Instance details

Defined in Database.Persist.Postgresql

Methods

getBy :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record (RawPostgresql b)) => Unique record -> ReaderT (RawPostgresql b) m (Maybe (Entity record)) Source #

existsBy :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record (RawPostgresql b)) => Unique record -> ReaderT (RawPostgresql b) m Bool Source #

(HasPersistBackend b, PersistUniqueWrite b) => PersistUniqueWrite (RawPostgresql b) Source # |

Instance details

Defined in Database.Persist.Postgresql

Methods

deleteBy :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record (RawPostgresql b)) => Unique record -> ReaderT (RawPostgresql b) m () Source #

insertUnique :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record (RawPostgresql b), SafeToInsert record) => record -> ReaderT (RawPostgresql b) m (Maybe (Key record)) Source #

insertUnique_ :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record (RawPostgresql b), SafeToInsert record) => record -> ReaderT (RawPostgresql b) m (Maybe ()) Source #

upsert :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record (RawPostgresql b), OnlyOneUniqueKey record, SafeToInsert record) => record -> [Update record] -> ReaderT (RawPostgresql b) m (Entity record) Source #

upsertBy :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record (RawPostgresql b), SafeToInsert record) => Unique record -> record -> [Update record] -> ReaderT (RawPostgresql b) m (Entity record) Source #

putMany :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record (RawPostgresql b), SafeToInsert record) => [record] -> ReaderT (RawPostgresql b) m () Source #

(PersistCore b, PersistCore (RawPostgresql b), PersistFieldSql (BackendKey b)) => PersistFieldSql (BackendKey (RawPostgresql b)) Source # |
Instance details Defined in Database.Persist.Postgresql Methods sqlType :: Proxy (BackendKey (RawPostgresql b)) -> SqlType Source #
BackendCompatible (RawPostgresql b) (RawPostgresql b) Source # |
Instance details Defined in Database.Persist.Postgresql Methods projectBackend :: RawPostgresql b -> RawPostgresql b Source #
newtype BackendKey (RawPostgresql b) Source # |

Instance details

Defined in Database.Persist.Postgresql

newtype BackendKey (RawPostgresql b) = RawPostgresqlKey {

- unRawPostgresqlKey :: BackendKey (Compatible b (RawPostgresql b))

}

type BaseBackend (RawPostgresql b0) Source # |
Instance details Defined in Database.Persist.Postgresql type BaseBackend (RawPostgresql b0) = BaseBackend (Compatible b0 (RawPostgresql b0))

createRawPostgresqlPool Source #

Arguments

:: (MonadUnliftIO m, MonadLoggerIO m) |
=> ConnectionString | Connection string to the database.
-> Int | Number of connections to be kept open in the pool.
-> m (Pool (RawPostgresql SqlBackend)) |

Create a PostgreSQL connection pool which also exposes the raw connection. The raw counterpart to `createPostgresqlPool`.

Since: 2.13.1.0

createRawPostgresqlPoolModified Source #

Arguments

:: (MonadUnliftIO m, MonadLoggerIO m) |
=> (Connection -> IO ()) | Action to perform after connection is created.
-> ConnectionString | Connection string to the database.
-> Int | Number of connections to be kept open in the pool.
-> m (Pool (RawPostgresql SqlBackend)) |

The raw counterpart to `createPostgresqlPoolModified`.

Since: 2.13.1.0

createRawPostgresqlPoolModifiedWithVersion Source #

Arguments

:: (MonadUnliftIO m, MonadLoggerIO m) |
=> (Connection -> IO (Maybe Double)) | Action to perform to get the server version.
-> (Connection -> IO ()) | Action to perform after connection is created.
-> ConnectionString | Connection string to the database.
-> Int | Number of connections to be kept open in the pool.
-> m (Pool (RawPostgresql SqlBackend)) |

The raw counterpart to `createPostgresqlPoolModifiedWithVersion`.

Since: 2.13.1.0

createRawPostgresqlPoolWithConf Source #

Arguments

:: (MonadUnliftIO m, MonadLoggerIO m) |
=> PostgresConf | Configuration for connecting to Postgres
-> PostgresConfHooks | Record of callback functions
-> m (Pool (RawPostgresql SqlBackend)) |

The raw counterpart to `createPostgresqlPoolWithConf`.

Since: 2.13.1.0

createBackend :: LogFunc -> NonEmpty Word -> IORef (Map Text Statement) -> Connection -> SqlBackend Source #

Create the backend given a logging function, server version, mutable statement cell, and connection.

Since: 2.13.6