Database.Persist.SqlBackend.Internal

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

Safe Haskell | None
Language | Haskell2010

Database.Persist.SqlBackend.Internal

Synopsis

- data SqlBackend = SqlBackend {
  - connPrepare :: Text -> IO Statement
  - connInsertSql :: EntityDef -> [PersistValue] -> InsertSqlResult
  - connInsertManySql :: Maybe (EntityDef -> [[PersistValue]] -> InsertSqlResult)
  - connUpsertSql :: Maybe (EntityDef -> NonEmpty (FieldNameHS, FieldNameDB) -> Text -> Text)
  - connPutManySql :: Maybe (EntityDef -> Int -> Text)
  - connStmtMap :: StatementCache
  - connClose :: IO ()
  - connMigrateSql :: [EntityDef] -> (Text -> IO Statement) -> EntityDef -> IO (Either [Text] [(Bool, Text)])
  - connBegin :: (Text -> IO Statement) -> Maybe IsolationLevel -> IO ()
  - connCommit :: (Text -> IO Statement) -> IO ()
  - connRollback :: (Text -> IO Statement) -> IO ()
  - connEscapeFieldName :: FieldNameDB -> Text
  - connEscapeTableName :: EntityDef -> Text
  - connEscapeRawName :: Text -> Text
  - connNoLimit :: Text
  - connRDBMS :: Text
  - connLimitOffset :: (Int, Int) -> Text -> Text
  - connLogFunc :: LogFunc
  - connMaxParams :: Maybe Int
  - connRepsertManySql :: Maybe (EntityDef -> Int -> Text)
  - connVault :: Vault
  - connHooks :: SqlBackendHooks
  }
- newtype SqlBackendHooks = SqlBackendHooks {
  - hookGetStatement :: SqlBackend -> Text -> Statement -> IO Statement
  }
- emptySqlBackendHooks :: SqlBackendHooks
- mkSqlBackend :: MkSqlBackendArgs -> SqlBackend

# Documentation

data SqlBackend #

A `SqlBackend` represents a handle or connection to a database. It contains functions and values that allow databases to have more optimized implementations, as well as references that benefit performance and sharing.

Instead of using the `SqlBackend` constructor directly, use the `mkSqlBackend` function.

A `SqlBackend` is *not* thread-safe. You should not assume that a `SqlBackend` can be shared among threads and run concurrent queries. This *will* result in problems. Instead, you should create a ``Pool` `SqlBackend``, known as a `ConnectionPool`, and pass that around in multi-threaded applications.

To run actions in the `persistent` library, you should use the `runSqlConn` function. If you're using a multithreaded application, use the `runSqlPool` function.

Constructors

SqlBackend |
Fields connPrepare :: Text -> IO Statement This function should prepare a `Statement` in the target database, which should allow for efficient query reuse. connInsertSql :: EntityDef -> [PersistValue] -> InsertSqlResult This function generates the SQL and values necessary for performing an insert against the database. connInsertManySql :: Maybe (EntityDef -> [[PersistValue]] -> InsertSqlResult) SQL for inserting many rows and returning their primary keys, for backends that support this functionality. If `Nothing`, rows will be inserted one-at-a-time using `connInsertSql`. connUpsertSql :: Maybe (EntityDef -> NonEmpty (FieldNameHS, FieldNameDB) -> Text -> Text) Some databases support performing UPSERT _and_ RETURN entity in a single call. This field when set will be used to generate the UPSERT+RETURN sql given * an entity definition * updates to be run on unique key(s) collision When left as `Nothing`, we find the unique key from entity def before * trying to fetch an entity by said key * perform an update when result found, else issue an insert * return new entity from db Since: 2.6 connPutManySql :: Maybe (EntityDef -> Int -> Text) Some databases support performing bulk UPSERT, specifically "insert or replace many records" in a single call. This field when set, given * an entity definition * number of records to be inserted should produce a PUT MANY sql with placeholders for records When left as `Nothing`, we default to using `defaultPutMany`. Since: 2.8.1 connStmtMap :: StatementCache A reference to the cache of statements. `Statement`s are keyed by the `Text` queries that generated them. connClose :: IO () Close the underlying connection. connMigrateSql :: [EntityDef] -> (Text -> IO Statement) -> EntityDef -> IO (Either [Text] [(Bool, Text)]) This function returns the migrations required to include the `EntityDef` parameter in the `[`EntityDef`]` database. This might include creating a new table if the entity is not present, or altering an existing table if it is. connBegin :: (Text -> IO Statement) -> Maybe IsolationLevel -> IO () A function to begin a transaction for the underlying database. connCommit :: (Text -> IO Statement) -> IO () A function to commit a transaction to the underlying database. connRollback :: (Text -> IO Statement) -> IO () A function to roll back a transaction on the underlying database. connEscapeFieldName :: FieldNameDB -> Text A function to extract and escape the name of the column corresponding to the provided field. Since: 2.12.0.0 connEscapeTableName :: EntityDef -> Text A function to extract and escape the name of the table corresponding to the provided entity. PostgreSQL uses this to support schemas. Since: 2.12.0.0 connEscapeRawName :: Text -> Text A function to escape raw DB identifiers. MySQL uses backticks, while PostgreSQL uses quotes, and so on. Since: 2.12.0.0 connNoLimit :: Text connRDBMS :: Text A tag displaying what database the `SqlBackend` is for. Can be used to differentiate features in downstream libraries for different database backends. connLimitOffset :: (Int, Int) -> Text -> Text Attach a 'LIMIT/OFFSET' clause to a SQL query. Note that LIMIT/OFFSET is problematic for performance, and indexed range queries are the superior way to offer pagination. connLogFunc :: LogFunc A log function for the `SqlBackend` to use. connMaxParams :: Maybe Int Some databases (probably only Sqlite) have a limit on how many question-mark parameters may be used in a statement Since: 2.6.1 connRepsertManySql :: Maybe (EntityDef -> Int -> Text) Some databases support performing bulk an atomic+bulk INSERT where constraint conflicting entities can replace existing entities. This field when set, given * an entity definition * number of records to be inserted should produce a INSERT sql with placeholders for primary+record fields When left as `Nothing`, we default to using `defaultRepsertMany`. Since: 2.9.0 connVault :: Vault Carry arbitrary payloads for the connection that may be used to propagate information into hooks. connHooks :: SqlBackendHooks Instrumentation hooks that may be used to track the behaviour of a backend.

#### Instances

Instances details

PersistQueryRead SqlBackend # |
Instance details Defined in Database.Persist.Sql.Orphan.PersistQuery Methods selectSourceRes :: forall record (m1 :: Type -> Type) (m2 :: Type -> Type). (PersistRecordBackend record SqlBackend, MonadIO m1, MonadIO m2) => [Filter record] -> [SelectOpt record] -> ReaderT SqlBackend m1 (Acquire (ConduitM () (Entity record) m2 ())) # selectFirst :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record SqlBackend) => [Filter record] -> [SelectOpt record] -> ReaderT SqlBackend m (Maybe (Entity record)) # selectKeysRes :: forall (m1 :: Type -> Type) (m2 :: Type -> Type) record. (MonadIO m1, MonadIO m2, PersistRecordBackend record SqlBackend) => [Filter record] -> [SelectOpt record] -> ReaderT SqlBackend m1 (Acquire (ConduitM () (Key record) m2 ())) # count :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record SqlBackend) => [Filter record] -> ReaderT SqlBackend m Int # exists :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record SqlBackend) => [Filter record] -> ReaderT SqlBackend m Bool #
PersistQueryWrite SqlBackend # |
Instance details Defined in Database.Persist.Sql.Orphan.PersistQuery Methods updateWhere :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record SqlBackend) => [Filter record] -> [Update record] -> ReaderT SqlBackend m () # deleteWhere :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record SqlBackend) => [Filter record] -> ReaderT SqlBackend m () #
HasPersistBackend SqlBackend # |
Instance details Defined in Database.Persist.SqlBackend.Internal Associated Types
type BaseBackend SqlBackend |
Instance details Defined in Database.Persist.SqlBackend.Internal type BaseBackend SqlBackend = SqlBackend
Methods persistBackend :: SqlBackend -> BaseBackend SqlBackend #
IsPersistBackend SqlBackend # |
Instance details Defined in Database.Persist.SqlBackend.Internal Methods mkPersistBackend :: BaseBackend SqlBackend -> SqlBackend #
PersistCore SqlBackend # |
Instance details Defined in Database.Persist.Sql.Orphan.PersistStore Associated Types
newtype BackendKey SqlBackend |
Instance details Defined in Database.Persist.Sql.Orphan.PersistStore newtype BackendKey SqlBackend = SqlBackendKey { unSqlBackendKey :: Int64 }
PersistStoreRead SqlBackend # |
Instance details Defined in Database.Persist.Sql.Orphan.PersistStore Methods get :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record SqlBackend) => Key record -> ReaderT SqlBackend m (Maybe record) # getMany :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record SqlBackend) => [Key record] -> ReaderT SqlBackend m (Map (Key record) record) #
PersistStoreWrite SqlBackend # |
Instance details Defined in Database.Persist.Sql.Orphan.PersistStore Methods insert :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record SqlBackend, SafeToInsert record) => record -> ReaderT SqlBackend m (Key record) # insert_ :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record SqlBackend, SafeToInsert record) => record -> ReaderT SqlBackend m () # insertMany :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record SqlBackend, SafeToInsert record) => [record] -> ReaderT SqlBackend m [Key record] # insertMany_ :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record SqlBackend, SafeToInsert record) => [record] -> ReaderT SqlBackend m () # insertEntityMany :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record SqlBackend) => [Entity record] -> ReaderT SqlBackend m () # insertKey :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record SqlBackend) => Key record -> record -> ReaderT SqlBackend m () # repsert :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record SqlBackend) => Key record -> record -> ReaderT SqlBackend m () # repsertMany :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record SqlBackend) => [(Key record, record)] -> ReaderT SqlBackend m () # replace :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record SqlBackend) => Key record -> record -> ReaderT SqlBackend m () # delete :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record SqlBackend) => Key record -> ReaderT SqlBackend m () # update :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record SqlBackend) => Key record -> [Update record] -> ReaderT SqlBackend m () # updateGet :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record SqlBackend) => Key record -> [Update record] -> ReaderT SqlBackend m record #
PersistUniqueRead SqlBackend # |
Instance details Defined in Database.Persist.Sql.Orphan.PersistUnique Methods getBy :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record SqlBackend) => Unique record -> ReaderT SqlBackend m (Maybe (Entity record)) # existsBy :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record SqlBackend) => Unique record -> ReaderT SqlBackend m Bool #
PersistUniqueWrite SqlBackend # |
Instance details Defined in Database.Persist.Sql.Orphan.PersistUnique Methods deleteBy :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record SqlBackend) => Unique record -> ReaderT SqlBackend m () # insertUnique :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record SqlBackend, SafeToInsert record) => record -> ReaderT SqlBackend m (Maybe (Key record)) # insertUnique_ :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record SqlBackend, SafeToInsert record) => record -> ReaderT SqlBackend m (Maybe ()) # upsert :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record SqlBackend, OnlyOneUniqueKey record, SafeToInsert record) => record -> [Update record] -> ReaderT SqlBackend m (Entity record) # upsertBy :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record SqlBackend, SafeToInsert record) => Unique record -> record -> [Update record] -> ReaderT SqlBackend m (Entity record) # putMany :: forall record (m :: Type -> Type). (MonadIO m, PersistRecordBackend record SqlBackend, SafeToInsert record) => [record] -> ReaderT SqlBackend m () #
BackendCompatible SqlBackend SqlReadBackend # |
Instance details Defined in Database.Persist.Sql.Orphan.PersistStore Methods projectBackend :: SqlReadBackend -> SqlBackend #
BackendCompatible SqlBackend SqlWriteBackend # |
Instance details Defined in Database.Persist.Sql.Orphan.PersistStore Methods projectBackend :: SqlWriteBackend -> SqlBackend #
BackendCompatible SqlBackend SqlBackend # |
Instance details Defined in Database.Persist.Sql.Orphan.PersistStore Methods projectBackend :: SqlBackend -> SqlBackend #
FromJSON (BackendKey SqlBackend) # |
Instance details Defined in Database.Persist.Sql.Orphan.PersistStore Methods parseJSON :: Value -> Parser (BackendKey SqlBackend) # parseJSONList :: Value -> Parser [BackendKey SqlBackend] # omittedField :: Maybe (BackendKey SqlBackend) #
ToJSON (BackendKey SqlBackend) # |
Instance details Defined in Database.Persist.Sql.Orphan.PersistStore Methods toJSON :: BackendKey SqlBackend -> Value # toEncoding :: BackendKey SqlBackend -> Encoding # toJSONList :: [BackendKey SqlBackend] -> Value # toEncodingList :: [BackendKey SqlBackend] -> Encoding # omitField :: BackendKey SqlBackend -> Bool #
Bounded (BackendKey SqlBackend) # |
Instance details Defined in Database.Persist.Sql.Orphan.PersistStore Methods minBound :: BackendKey SqlBackend # maxBound :: BackendKey SqlBackend #
Enum (BackendKey SqlBackend) # |
Instance details Defined in Database.Persist.Sql.Orphan.PersistStore Methods succ :: BackendKey SqlBackend -> BackendKey SqlBackend # pred :: BackendKey SqlBackend -> BackendKey SqlBackend # toEnum :: Int -> BackendKey SqlBackend # fromEnum :: BackendKey SqlBackend -> Int # enumFrom :: BackendKey SqlBackend -> [BackendKey SqlBackend] # enumFromThen :: BackendKey SqlBackend -> BackendKey SqlBackend -> [BackendKey SqlBackend] # enumFromTo :: BackendKey SqlBackend -> BackendKey SqlBackend -> [BackendKey SqlBackend] # enumFromThenTo :: BackendKey SqlBackend -> BackendKey SqlBackend -> BackendKey SqlBackend -> [BackendKey SqlBackend] #
Generic (BackendKey SqlBackend) # |
Instance details Defined in Database.Persist.Sql.Orphan.PersistStore Associated Types
type Rep (BackendKey SqlBackend) |
Instance details Defined in Database.Persist.Sql.Orphan.PersistStore type Rep (BackendKey SqlBackend) = D1 ('MetaData "BackendKey" "Database.Persist.Sql.Orphan.PersistStore" "persistent-2.14.6.3-JVVGzpBhz0IC64DMc3fyym" 'True) (C1 ('MetaCons "SqlBackendKey" 'PrefixI 'True) (S1 ('MetaSel ('Just "unSqlBackendKey") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Int64)))
Methods from :: BackendKey SqlBackend -> Rep (BackendKey SqlBackend) x # to :: Rep (BackendKey SqlBackend) x -> BackendKey SqlBackend #
Num (BackendKey SqlBackend) # |
Instance details Defined in Database.Persist.Sql.Orphan.PersistStore Methods (+) :: BackendKey SqlBackend -> BackendKey SqlBackend -> BackendKey SqlBackend # (-) :: BackendKey SqlBackend -> BackendKey SqlBackend -> BackendKey SqlBackend # (*) :: BackendKey SqlBackend -> BackendKey SqlBackend -> BackendKey SqlBackend # negate :: BackendKey SqlBackend -> BackendKey SqlBackend # abs :: BackendKey SqlBackend -> BackendKey SqlBackend # signum :: BackendKey SqlBackend -> BackendKey SqlBackend # fromInteger :: Integer -> BackendKey SqlBackend #
Read (BackendKey SqlBackend) # |
Instance details Defined in Database.Persist.Sql.Orphan.PersistStore Methods readsPrec :: Int -> ReadS (BackendKey SqlBackend) # readList :: ReadS [BackendKey SqlBackend] # readPrec :: ReadPrec (BackendKey SqlBackend) # readListPrec :: ReadPrec [BackendKey SqlBackend] #
Integral (BackendKey SqlBackend) # |
Instance details Defined in Database.Persist.Sql.Orphan.PersistStore Methods quot :: BackendKey SqlBackend -> BackendKey SqlBackend -> BackendKey SqlBackend # rem :: BackendKey SqlBackend -> BackendKey SqlBackend -> BackendKey SqlBackend # div :: BackendKey SqlBackend -> BackendKey SqlBackend -> BackendKey SqlBackend # mod :: BackendKey SqlBackend -> BackendKey SqlBackend -> BackendKey SqlBackend # quotRem :: BackendKey SqlBackend -> BackendKey SqlBackend -> (BackendKey SqlBackend, BackendKey SqlBackend) # divMod :: BackendKey SqlBackend -> BackendKey SqlBackend -> (BackendKey SqlBackend, BackendKey SqlBackend) # toInteger :: BackendKey SqlBackend -> Integer #
Real (BackendKey SqlBackend) # |
Instance details Defined in Database.Persist.Sql.Orphan.PersistStore Methods toRational :: BackendKey SqlBackend -> Rational #
Show (BackendKey SqlBackend) # |
Instance details Defined in Database.Persist.Sql.Orphan.PersistStore Methods showsPrec :: Int -> BackendKey SqlBackend -> ShowS # show :: BackendKey SqlBackend -> String # showList :: [BackendKey SqlBackend] -> ShowS #
Eq (BackendKey SqlBackend) # |
Instance details Defined in Database.Persist.Sql.Orphan.PersistStore Methods (==) :: BackendKey SqlBackend -> BackendKey SqlBackend -> Bool # (/=) :: BackendKey SqlBackend -> BackendKey SqlBackend -> Bool #
Ord (BackendKey SqlBackend) # |
Instance details Defined in Database.Persist.Sql.Orphan.PersistStore Methods compare :: BackendKey SqlBackend -> BackendKey SqlBackend -> Ordering # (<) :: BackendKey SqlBackend -> BackendKey SqlBackend -> Bool # (<=) :: BackendKey SqlBackend -> BackendKey SqlBackend -> Bool # (>) :: BackendKey SqlBackend -> BackendKey SqlBackend -> Bool # (>=) :: BackendKey SqlBackend -> BackendKey SqlBackend -> Bool # max :: BackendKey SqlBackend -> BackendKey SqlBackend -> BackendKey SqlBackend # min :: BackendKey SqlBackend -> BackendKey SqlBackend -> BackendKey SqlBackend #
FromHttpApiData (BackendKey SqlBackend) # |
Instance details Defined in Database.Persist.Sql.Orphan.PersistStore Methods parseUrlPiece :: Text -> Either Text (BackendKey SqlBackend) # parseHeader :: ByteString -> Either Text (BackendKey SqlBackend) # parseQueryParam :: Text -> Either Text (BackendKey SqlBackend) #
ToHttpApiData (BackendKey SqlBackend) # |
Instance details Defined in Database.Persist.Sql.Orphan.PersistStore Methods toUrlPiece :: BackendKey SqlBackend -> Text # toEncodedUrlPiece :: BackendKey SqlBackend -> Builder # toHeader :: BackendKey SqlBackend -> ByteString # toQueryParam :: BackendKey SqlBackend -> Text # toEncodedQueryParam :: BackendKey SqlBackend -> Builder #
PathPiece (BackendKey SqlBackend) # |
Instance details Defined in Database.Persist.Sql.Orphan.PersistStore Methods fromPathPiece :: Text -> Maybe (BackendKey SqlBackend) # toPathPiece :: BackendKey SqlBackend -> Text #
PersistField (BackendKey SqlBackend) # |
Instance details Defined in Database.Persist.Sql.Orphan.PersistStore Methods toPersistValue :: BackendKey SqlBackend -> PersistValue # fromPersistValue :: PersistValue -> Either Text (BackendKey SqlBackend) #
PersistFieldSql (BackendKey SqlBackend) # |
Instance details Defined in Database.Persist.Sql.Orphan.PersistStore Methods sqlType :: Proxy (BackendKey SqlBackend) -> SqlType #
newtype BackendKey SqlBackend # |
Instance details Defined in Database.Persist.Sql.Orphan.PersistStore newtype BackendKey SqlBackend = SqlBackendKey { unSqlBackendKey :: Int64 }
type BaseBackend SqlBackend # |
Instance details Defined in Database.Persist.SqlBackend.Internal type BaseBackend SqlBackend = SqlBackend
type Rep (BackendKey SqlBackend) # |
Instance details Defined in Database.Persist.Sql.Orphan.PersistStore type Rep (BackendKey SqlBackend) = D1 ('MetaData "BackendKey" "Database.Persist.Sql.Orphan.PersistStore" "persistent-2.14.6.3-JVVGzpBhz0IC64DMc3fyym" 'True) (C1 ('MetaCons "SqlBackendKey" 'PrefixI 'True) (S1 ('MetaSel ('Just "unSqlBackendKey") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Int64)))

newtype SqlBackendHooks #

Constructors

SqlBackendHooks |
Fields hookGetStatement :: SqlBackend -> Text -> Statement -> IO Statement

emptySqlBackendHooks :: SqlBackendHooks #

mkSqlBackend :: MkSqlBackendArgs -> SqlBackend #

A function for creating a value of the `SqlBackend` type. You should prefer to use this instead of the constructor for `SqlBackend`, because default values for this will be provided for new fields on the record when new functionality is added.

Since: 2.13.0.0