persistent-sqlite-2.10.6.2: Backend for the persistent library using sqlite3.
Safe HaskellNone
LanguageHaskell2010

Database.Persist.Sqlite

Description

A sqlite backend for persistent.

Note: If you prepend WAL=off to your connection string, it will disable the write-ahead log. This functionality is now deprecated in favour of using SqliteConnectionInfo.

Synopsis

Documentation

withSqlitePool Source #

Arguments

:: (MonadUnliftIO m, MonadLogger m) 
=> Text 
-> Int

number of connections to open

-> (Pool SqlBackend -> m a) 
-> m a 

Run the given action with a connection pool.

Like createSqlitePool, this should not be used with :memory:.

withSqlitePoolInfo Source #

Arguments

:: (MonadUnliftIO m, MonadLogger m) 
=> SqliteConnectionInfo 
-> Int

number of connections to open

-> (Pool SqlBackend -> m a) 
-> m a 

Run the given action with a connection pool.

Like createSqlitePool, this should not be used with :memory:.

Since: 2.6.2

withSqliteConn :: (MonadUnliftIO m, MonadLogger m) => Text -> (SqlBackend -> m a) -> m a Source #

withSqliteConnInfo :: (MonadUnliftIO m, MonadLogger m) => SqliteConnectionInfo -> (SqlBackend -> m a) -> m a Source #

Since: 2.6.2

createSqlitePool :: (MonadLogger m, MonadUnliftIO m) => Text -> Int -> m (Pool SqlBackend) Source #

Create a pool of SQLite connections.

Note that this should not be used with the :memory: connection string, as the pool will regularly remove connections, destroying your database. Instead, use withSqliteConn.

createSqlitePoolFromInfo :: (MonadLogger m, MonadUnliftIO m) => SqliteConnectionInfo -> Int -> m (Pool SqlBackend) Source #

Create a pool of SQLite connections.

Note that this should not be used with the :memory: connection string, as the pool will regularly remove connections, destroying your database. Instead, use withSqliteConn.

Since: 2.6.2

data SqliteConf Source #

Information required to setup a connection pool.

Constructors

SqliteConf 

Fields

SqliteConfInfo 

Instances

Instances details
Show SqliteConf Source # 
Instance details

Defined in Database.Persist.Sqlite

Methods

showsPrec :: Int -> SqliteConf -> ShowS

show :: SqliteConf -> String

showList :: [SqliteConf] -> ShowS

PersistConfig SqliteConf Source # 
Instance details

Defined in Database.Persist.Sqlite

Associated Types

type PersistConfigBackend SqliteConf :: (Type -> Type) -> Type -> Type

type PersistConfigPool SqliteConf

Methods

loadConfig :: Value -> Parser SqliteConf

applyEnv :: SqliteConf -> IO SqliteConf

createPoolConfig :: SqliteConf -> IO (PersistConfigPool SqliteConf)

runPool :: MonadUnliftIO m => SqliteConf -> PersistConfigBackend SqliteConf m a -> PersistConfigPool SqliteConf -> m a

FromJSON SqliteConf Source # 
Instance details

Defined in Database.Persist.Sqlite

Methods

parseJSON :: Value -> Parser SqliteConf

parseJSONList :: Value -> Parser [SqliteConf]

type PersistConfigBackend SqliteConf Source # 
Instance details

Defined in Database.Persist.Sqlite

type PersistConfigBackend SqliteConf = SqlPersistT
type PersistConfigPool SqliteConf Source # 
Instance details

Defined in Database.Persist.Sqlite

type PersistConfigPool SqliteConf = ConnectionPool

data SqliteConnectionInfo Source #

Information required to connect to a sqlite database. We export lenses instead of fields to avoid being limited to the current implementation.

Since: 2.6.2

Instances

Instances details
Show SqliteConnectionInfo Source # 
Instance details

Defined in Database.Persist.Sqlite

Methods

showsPrec :: Int -> SqliteConnectionInfo -> ShowS

show :: SqliteConnectionInfo -> String

showList :: [SqliteConnectionInfo] -> ShowS

FromJSON SqliteConnectionInfo Source # 
Instance details

Defined in Database.Persist.Sqlite

Methods

parseJSON :: Value -> Parser SqliteConnectionInfo

parseJSONList :: Value -> Parser [SqliteConnectionInfo]

mkSqliteConnectionInfo :: Text -> SqliteConnectionInfo Source #

Creates a SqliteConnectionInfo from a connection string, with the default settings.

Since: 2.6.2

runSqlite Source #

Arguments

:: MonadUnliftIO m 
=> Text

connection string

-> ReaderT SqlBackend (NoLoggingT (ResourceT m)) a

database action

-> m a 

A convenience helper which creates a new database connection and runs the given block, handling MonadResource and MonadLogger requirements. Note that all log messages are discarded.

Since: 1.1.4

runSqliteInfo Source #

Arguments

:: MonadUnliftIO m 
=> SqliteConnectionInfo 
-> ReaderT SqlBackend (NoLoggingT (ResourceT m)) a

database action

-> m a 

A convenience helper which creates a new database connection and runs the given block, handling MonadResource and MonadLogger requirements. Note that all log messages are discarded.

Since: 2.6.2

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

Wrap up a raw Connection as a Persistent SQL Connection.

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 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
|]

main :: IO ()
main = do
  conn <- open "/home/sibi/test.db"
  (backend :: SqlBackend) <- wrapConnection conn (\_ _ _ _ -> return ())
  flip runSqlPersistM backend $ do
         runMigration migrateAll
         insert_ $ Person "John doe" $ Just 35
         insert_ $ Person "Hema" $ Just 36
         (pers :: [Entity Person]) <- selectList [] []
         liftIO $ print pers
  close' backend

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}}]

Since: 1.1.5

wrapConnectionInfo :: SqliteConnectionInfo -> Connection -> LogFunc -> IO SqlBackend Source #

Wrap up a raw Connection as a Persistent SQL Connection, allowing full control over WAL and FK constraints.

Since: 2.6.2

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 isn't needed for it.

retryOnBusy :: (MonadUnliftIO m, MonadLogger m) => m a -> m a Source #

Retry if a Busy is thrown, following an exponential backoff strategy.

Since: 2.9.3

waitForDatabase :: (MonadUnliftIO m, MonadLogger m, BackendCompatible SqlBackend backend) => ReaderT backend m () Source #

Wait until some noop action on the database does not return an ErrorBusy. See retryOnBusy.

Since: 2.9.3

data RawSqlite backend Source #

Wrapper for persistent SqlBackends that carry the corresponding Connection.

Since: 2.10.2

Instances

Instances details
BackendCompatible b (RawSqlite b) Source # 
Instance details

Defined in Database.Persist.Sqlite

Methods

projectBackend :: RawSqlite b -> b

Bounded (BackendKey b) => Bounded (BackendKey (RawSqlite b)) Source # 
Instance details

Defined in Database.Persist.Sqlite

Methods

minBound :: BackendKey (RawSqlite b)

maxBound :: BackendKey (RawSqlite b)

Enum (BackendKey b) => Enum (BackendKey (RawSqlite b)) Source # 
Instance details

Defined in Database.Persist.Sqlite

Methods

succ :: BackendKey (RawSqlite b) -> BackendKey (RawSqlite b)

pred :: BackendKey (RawSqlite b) -> BackendKey (RawSqlite b)

toEnum :: Int -> BackendKey (RawSqlite b)

fromEnum :: BackendKey (RawSqlite b) -> Int

enumFrom :: BackendKey (RawSqlite b) -> [BackendKey (RawSqlite b)]

enumFromThen :: BackendKey (RawSqlite b) -> BackendKey (RawSqlite b) -> [BackendKey (RawSqlite b)]

enumFromTo :: BackendKey (RawSqlite b) -> BackendKey (RawSqlite b) -> [BackendKey (RawSqlite b)]

enumFromThenTo :: BackendKey (RawSqlite b) -> BackendKey (RawSqlite b) -> BackendKey (RawSqlite b) -> [BackendKey (RawSqlite b)]

Eq (BackendKey b) => Eq (BackendKey (RawSqlite b)) Source # 
Instance details

Defined in Database.Persist.Sqlite

Methods

(==) :: BackendKey (RawSqlite b) -> BackendKey (RawSqlite b) -> Bool

(/=) :: BackendKey (RawSqlite b) -> BackendKey (RawSqlite b) -> Bool

Integral (BackendKey b) => Integral (BackendKey (RawSqlite b)) Source # 
Instance details

Defined in Database.Persist.Sqlite

Methods

quot :: BackendKey (RawSqlite b) -> BackendKey (RawSqlite b) -> BackendKey (RawSqlite b)

rem :: BackendKey (RawSqlite b) -> BackendKey (RawSqlite b) -> BackendKey (RawSqlite b)

div :: BackendKey (RawSqlite b) -> BackendKey (RawSqlite b) -> BackendKey (RawSqlite b)

mod :: BackendKey (RawSqlite b) -> BackendKey (RawSqlite b) -> BackendKey (RawSqlite b)

quotRem :: BackendKey (RawSqlite b) -> BackendKey (RawSqlite b) -> (BackendKey (RawSqlite b), BackendKey (RawSqlite b))

divMod :: BackendKey (RawSqlite b) -> BackendKey (RawSqlite b) -> (BackendKey (RawSqlite b), BackendKey (RawSqlite b))

toInteger :: BackendKey (RawSqlite b) -> Integer

Num (BackendKey b) => Num (BackendKey (RawSqlite b)) Source # 
Instance details

Defined in Database.Persist.Sqlite

Methods

(+) :: BackendKey (RawSqlite b) -> BackendKey (RawSqlite b) -> BackendKey (RawSqlite b)

(-) :: BackendKey (RawSqlite b) -> BackendKey (RawSqlite b) -> BackendKey (RawSqlite b)

(*) :: BackendKey (RawSqlite b) -> BackendKey (RawSqlite b) -> BackendKey (RawSqlite b)

negate :: BackendKey (RawSqlite b) -> BackendKey (RawSqlite b)

abs :: BackendKey (RawSqlite b) -> BackendKey (RawSqlite b)

signum :: BackendKey (RawSqlite b) -> BackendKey (RawSqlite b)

fromInteger :: Integer -> BackendKey (RawSqlite b)

Ord (BackendKey b) => Ord (BackendKey (RawSqlite b)) Source # 
Instance details

Defined in Database.Persist.Sqlite

Methods

compare :: BackendKey (RawSqlite b) -> BackendKey (RawSqlite b) -> Ordering

(<) :: BackendKey (RawSqlite b) -> BackendKey (RawSqlite b) -> Bool

(<=) :: BackendKey (RawSqlite b) -> BackendKey (RawSqlite b) -> Bool

(>) :: BackendKey (RawSqlite b) -> BackendKey (RawSqlite b) -> Bool

(>=) :: BackendKey (RawSqlite b) -> BackendKey (RawSqlite b) -> Bool

max :: BackendKey (RawSqlite b) -> BackendKey (RawSqlite b) -> BackendKey (RawSqlite b)

min :: BackendKey (RawSqlite b) -> BackendKey (RawSqlite b) -> BackendKey (RawSqlite b)

Read (BackendKey b) => Read (BackendKey (RawSqlite b)) Source # 
Instance details

Defined in Database.Persist.Sqlite

Methods

readsPrec :: Int -> ReadS (BackendKey (RawSqlite b))

readList :: ReadS [BackendKey (RawSqlite b)]

readPrec :: ReadPrec (BackendKey (RawSqlite b))

readListPrec :: ReadPrec [BackendKey (RawSqlite b)]

Real (BackendKey b) => Real (BackendKey (RawSqlite b)) Source # 
Instance details

Defined in Database.Persist.Sqlite

Methods

toRational :: BackendKey (RawSqlite b) -> Rational

Show (BackendKey b) => Show (BackendKey (RawSqlite b)) Source # 
Instance details

Defined in Database.Persist.Sqlite

Methods

showsPrec :: Int -> BackendKey (RawSqlite b) -> ShowS

show :: BackendKey (RawSqlite b) -> String

showList :: [BackendKey (RawSqlite b)] -> ShowS

PersistField (BackendKey b) => PersistField (BackendKey (RawSqlite b)) Source # 
Instance details

Defined in Database.Persist.Sqlite

Methods

toPersistValue :: BackendKey (RawSqlite b) -> PersistValue

fromPersistValue :: PersistValue -> Either Text (BackendKey (RawSqlite b))

PersistQueryRead b => PersistQueryRead (RawSqlite b) Source # 
Instance details

Defined in Database.Persist.Sqlite

Methods

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

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

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

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

PersistQueryWrite b => PersistQueryWrite (RawSqlite b) Source # 
Instance details

Defined in Database.Persist.Sqlite

Methods

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

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

HasPersistBackend b => HasPersistBackend (RawSqlite b) Source # 
Instance details

Defined in Database.Persist.Sqlite

Associated Types

type BaseBackend (RawSqlite b)

Methods

persistBackend :: RawSqlite b -> BaseBackend (RawSqlite b)

PersistCore b => PersistCore (RawSqlite b) Source # 
Instance details

Defined in Database.Persist.Sqlite

Associated Types

data BackendKey (RawSqlite b)

PersistStoreRead b => PersistStoreRead (RawSqlite b) Source # 
Instance details

Defined in Database.Persist.Sqlite

Methods

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

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

PersistStoreWrite b => PersistStoreWrite (RawSqlite b) Source # 
Instance details

Defined in Database.Persist.Sqlite

Methods

insert :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record (RawSqlite b)) => record -> ReaderT (RawSqlite b) m (Key record)

insert_ :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record (RawSqlite b)) => record -> ReaderT (RawSqlite b) m ()

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

insertMany_ :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record (RawSqlite b)) => [record] -> ReaderT (RawSqlite b) m ()

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

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

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

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

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

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

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

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

PersistUniqueRead b => PersistUniqueRead (RawSqlite b) Source # 
Instance details

Defined in Database.Persist.Sqlite

Methods

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

PersistUniqueWrite b => PersistUniqueWrite (RawSqlite b) Source # 
Instance details

Defined in Database.Persist.Sqlite

Methods

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

insertUnique :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record (RawSqlite b)) => record -> ReaderT (RawSqlite b) m (Maybe (Key record))

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

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

putMany :: forall (m :: Type -> Type) record. (MonadIO m, PersistRecordBackend record (RawSqlite b)) => [record] -> ReaderT (RawSqlite b) m ()

ToJSON (BackendKey b) => ToJSON (BackendKey (RawSqlite b)) Source # 
Instance details

Defined in Database.Persist.Sqlite

Methods

toJSON :: BackendKey (RawSqlite b) -> Value

toEncoding :: BackendKey (RawSqlite b) -> Encoding

toJSONList :: [BackendKey (RawSqlite b)] -> Value

toEncodingList :: [BackendKey (RawSqlite b)] -> Encoding

FromJSON (BackendKey b) => FromJSON (BackendKey (RawSqlite b)) Source # 
Instance details

Defined in Database.Persist.Sqlite

Methods

parseJSON :: Value -> Parser (BackendKey (RawSqlite b))

parseJSONList :: Value -> Parser [BackendKey (RawSqlite b)]

PersistFieldSql (BackendKey b) => PersistFieldSql (BackendKey (RawSqlite b)) Source # 
Instance details

Defined in Database.Persist.Sqlite

Methods

sqlType :: Proxy (BackendKey (RawSqlite b)) -> SqlType

newtype BackendKey (RawSqlite b) Source # 
Instance details

Defined in Database.Persist.Sqlite

newtype BackendKey (RawSqlite b) = RawSqliteKey (BackendKey b)
type BaseBackend (RawSqlite b) Source # 
Instance details

Defined in Database.Persist.Sqlite

type BaseBackend (RawSqlite b) = BaseBackend b

persistentBackend :: forall backend backend. Lens (RawSqlite backend) (RawSqlite backend) backend backend Source #

rawSqliteConnection :: forall backend. Lens' (RawSqlite backend) Connection Source #

withRawSqliteConnInfo :: (MonadUnliftIO m, MonadLogger m) => SqliteConnectionInfo -> (RawSqlite SqlBackend -> m a) -> m a Source #

Like withSqliteConnInfo, but exposes the internal Connection. For power users who want to manually interact with SQLite's C API via internals exposed by Database.Sqlite.Internal

Since: 2.10.2

createRawSqlitePoolFromInfo Source #

Arguments

:: (MonadLogger m, MonadUnliftIO m) 
=> SqliteConnectionInfo 
-> (RawSqlite SqlBackend -> m ())

An action that is run whenever a new RawSqlite connection is allocated in the pool. The main use of this function is to register custom functions with the SQLite connection upon creation.

-> Int 
-> m (Pool (RawSqlite SqlBackend)) 

Like createSqlitePoolFromInfo, but like withRawSqliteConnInfo it exposes the internal Connection.

For power users who want to manually interact with SQLite's C API via internals exposed by Database.Sqlite.Internal. The callback can be used to run arbitrary actions on the connection upon allocation from the pool.

Since: 2.10.6

createRawSqlitePoolFromInfo_ :: (MonadLogger m, MonadUnliftIO m) => SqliteConnectionInfo -> Int -> m (Pool (RawSqlite SqlBackend)) Source #

Like createRawSqlitePoolFromInfo, but doesn't require a callback operating on the connection.

Since: 2.10.6

withRawSqlitePoolInfo Source #

Arguments

:: (MonadUnliftIO m, MonadLogger m) 
=> SqliteConnectionInfo 
-> (RawSqlite SqlBackend -> m ()) 
-> Int

number of connections to open

-> (Pool (RawSqlite SqlBackend) -> m a) 
-> m a 

Like createSqlitePoolInfo, but based on createRawSqlitePoolFromInfo.

Since: 2.10.6

withRawSqlitePoolInfo_ Source #

Arguments

:: (MonadUnliftIO m, MonadLogger m) 
=> SqliteConnectionInfo 
-> Int

number of connections to open

-> (Pool (RawSqlite SqlBackend) -> m a) 
-> m a 

Like createSqlitePoolInfo, but based on createRawSqlitePoolFromInfo_.

Since: 2.10.6