Safe Haskell | None |
---|---|
Language | Haskell2010 |
This module contains types and information necessary for a SQL database.
Database support libraries, like persistent-postgresql
, will be responsible
for constructing these values.
Synopsis
- data SqlBackend
- mkSqlBackend :: MkSqlBackendArgs -> SqlBackend
- data MkSqlBackendArgs = MkSqlBackendArgs {
- connPrepare :: Text -> IO Statement
- connInsertSql :: EntityDef -> [PersistValue] -> InsertSqlResult
- connStmtMap :: IORef (Map Text Statement)
- 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
- getEscapedFieldName :: (BackendCompatible SqlBackend backend, MonadReader backend m) => FieldNameDB -> m Text
- getEscapedRawName :: (BackendCompatible SqlBackend backend, MonadReader backend m) => Text -> m Text
- getEscapeRawNameFunction :: (BackendCompatible SqlBackend backend, MonadReader backend m) => m (Text -> Text)
- getConnLimitOffset :: (BackendCompatible SqlBackend backend, MonadReader backend m) => (Int, Int) -> Text -> m Text
- getConnUpsertSql :: (BackendCompatible SqlBackend backend, MonadReader backend m) => m (Maybe (EntityDef -> NonEmpty (FieldNameHS, FieldNameDB) -> Text -> Text))
- setConnMaxParams :: Int -> SqlBackend -> SqlBackend
- setConnRepsertManySql :: (EntityDef -> Int -> Text) -> SqlBackend -> SqlBackend
- setConnInsertManySql :: (EntityDef -> [[PersistValue]] -> InsertSqlResult) -> SqlBackend -> SqlBackend
- setConnUpsertSql :: (EntityDef -> NonEmpty (FieldNameHS, FieldNameDB) -> Text -> Text) -> SqlBackend -> SqlBackend
- setConnPutManySql :: (EntityDef -> Int -> Text) -> SqlBackend -> SqlBackend
The type and construction
data SqlBackend Source #
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
, known as a Pool
SqlBackend
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.
Instances
mkSqlBackend :: MkSqlBackendArgs -> SqlBackend Source #
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
data MkSqlBackendArgs Source #
This type shares many of the same field names as the SqlBackend
type.
It's useful for library authors to use this when migrating from using the
SqlBackend
constructor directly to the mkSqlBackend
function.
This type will only contain required fields for constructing a SqlBackend
.
For fields that aren't present on this record, you'll want to use the various
set
functions or
Since: 2.13.0.0
MkSqlBackendArgs | |
|
Utilities
The functions exported here are a bit more general than the record accessors.
The easiest way to use them is to provide the SqlBackend
directly to the
function. However, you can also use them in a ReaderT
context, and you can
even use them with any backend
type tht has a
instance.BackendCompatible
SqlBackend
backend
SqlBackend Getters
getEscapedFieldName :: (BackendCompatible SqlBackend backend, MonadReader backend m) => FieldNameDB -> m Text Source #
This function can be used directly with a SqlBackend
to escape
a FieldNameDB
.
let conn :: SqlBackend getEscapedFieldName (FieldNameDB "asdf") conn
Alternatively, you can use it in a
context, like
ReaderT
SqlBackend
SqlPersistT
:
query :: SqlPersistM Text query = do field <- getEscapedFieldName (FieldNameDB "asdf") pure field
Since: 2.13.0.0
getEscapedRawName :: (BackendCompatible SqlBackend backend, MonadReader backend m) => Text -> m Text Source #
This function can be used directly with a SqlBackend
to escape
a raw Text
.
let conn :: SqlBackend getEscapedRawName (FieldNameDB "asdf") conn
Alternatively, you can use it in a
context, like
ReaderT
SqlBackend
SqlPersistT
:
query :: SqlPersistM Text query = do field <- getEscapedRawName (FieldNameDB "asdf") pure field
Since: 2.13.0.0
getEscapeRawNameFunction :: (BackendCompatible SqlBackend backend, MonadReader backend m) => m (Text -> Text) Source #
Return the function for escaping a raw name.
Since: 2.13.0.0
:: (BackendCompatible SqlBackend backend, MonadReader backend m) | |
=> (Int, Int) | The |
-> Text | The SQL query that the LIMIT/OFFSET clause will be attached to. |
-> m Text |
Decorate the given SQL query with the (LIMIT, OFFSET)
specified.
Since: 2.13.0.0
getConnUpsertSql :: (BackendCompatible SqlBackend backend, MonadReader backend m) => m (Maybe (EntityDef -> NonEmpty (FieldNameHS, FieldNameDB) -> Text -> Text)) Source #
Retrieve the function for generating an upsert statement, if the backend supports it.
Since: 2.13.0.0
SqlBackend Setters
setConnMaxParams :: Int -> SqlBackend -> SqlBackend Source #
Set the maximum parameters that may be issued in a given SQL query. This should be used only if the database backend have this limitation.
Since: 2.13.0.0
setConnRepsertManySql :: (EntityDef -> Int -> Text) -> SqlBackend -> SqlBackend Source #
Set the connRepsertManySql
field on the SqlBackend
. This should only be
set by the database backend library. If this is not set, a slow default will
be used.
Since: 2.13.0.0
setConnInsertManySql :: (EntityDef -> [[PersistValue]] -> InsertSqlResult) -> SqlBackend -> SqlBackend Source #
Set the connInsertManySql
field on the SqlBackend
. This should only be
used by the database backend library to provide an efficient implementation
of a bulk insert function. If this is not set, a slow default will be used.
Since: 2.13.0.0
setConnUpsertSql :: (EntityDef -> NonEmpty (FieldNameHS, FieldNameDB) -> Text -> Text) -> SqlBackend -> SqlBackend Source #
Set the connUpsertSql
field on the SqlBackend
. This should only be used
by the database backend library to provide an efficient implementation of
a bulk insert function. If this is not set, a slow default will be used.
Since: 2.13.0.0
setConnPutManySql :: (EntityDef -> Int -> Text) -> SqlBackend -> SqlBackend Source #
Set the 'connPutManySql field on the SqlBackend
. This should only be used
by the database backend library to provide an efficient implementation of
a bulk insert function. If this is not set, a slow default will be used.
Since: 2.13.0.0