{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}

{-# OPTIONS_GHC -fno-warn-orphans #-}

module Database.Persist.Sql.Orphan.PersistStore
    ( withRawQuery
    , BackendKey(..)
    , toSqlKey
    , fromSqlKey
    , getFieldName
    , getTableName
    , tableDBName
    , fieldDBName
    ) where

import Control.Exception (throwIO)
import Control.Monad.IO.Class
import Control.Monad.Trans.Reader (ReaderT, ask)
import Data.Acquire (with)
import qualified Data.Aeson as A
import Data.ByteString.Char8 (readInteger)
import Data.Conduit (ConduitM, runConduit, (.|))
import qualified Data.Conduit.List as CL
import qualified Data.Foldable as Foldable
import Data.Function (on)
import Data.Int (Int64)
import Data.List (find, nubBy)
import qualified Data.Map as Map
import Data.Maybe (isJust)
import Data.Text (Text, unpack)
import qualified Data.Text as T
import Data.Void (Void)
import GHC.Generics (Generic)
import Web.HttpApiData (FromHttpApiData, ToHttpApiData)
import Web.PathPieces (PathPiece)

import Database.Persist
import Database.Persist.Class ()
import Database.Persist.Sql.Class (PersistFieldSql)
import Database.Persist.Sql.Raw
import Database.Persist.Sql.Types
import Database.Persist.Sql.Types.Internal
import Database.Persist.Sql.Util
       ( commaSeparated
       , dbIdColumns
       , keyAndEntityColumnNames
       , mkInsertValues
       , mkUpdateText
       , parseEntityValues
       , updatePersistValue
       )

withRawQuery :: MonadIO m
             => Text
             -> [PersistValue]
             -> ConduitM [PersistValue] Void IO a
             -> ReaderT SqlBackend m a
withRawQuery :: forall (m :: * -> *) a.
MonadIO m =>
Text
-> [PersistValue]
-> ConduitM [PersistValue] Void IO a
-> ReaderT SqlBackend m a
withRawQuery Text
sql [PersistValue]
vals ConduitM [PersistValue] Void IO a
sink = do
    Acquire (ConduitM () [PersistValue] IO ())
srcRes <- forall (m1 :: * -> *) (m2 :: * -> *) env.
(MonadIO m1, MonadIO m2, BackendCompatible SqlBackend env) =>
Text
-> [PersistValue]
-> ReaderT env m1 (Acquire (ConduitM () [PersistValue] m2 ()))
rawQueryRes Text
sql [PersistValue]
vals
    forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a b.
MonadUnliftIO m =>
Acquire a -> (a -> m b) -> m b
with Acquire (ConduitM () [PersistValue] IO ())
srcRes (\ConduitM () [PersistValue] IO ()
src -> forall (m :: * -> *) r. Monad m => ConduitT () Void m r -> m r
runConduit forall a b. (a -> b) -> a -> b
$ ConduitM () [PersistValue] IO ()
src forall (m :: * -> *) a b c r.
Monad m =>
ConduitT a b m () -> ConduitT b c m r -> ConduitT a c m r
.| ConduitM [PersistValue] Void IO a
sink)

toSqlKey :: ToBackendKey SqlBackend record => Int64 -> Key record
toSqlKey :: forall record.
ToBackendKey SqlBackend record =>
Int64 -> Key record
toSqlKey = forall backend record.
ToBackendKey backend record =>
BackendKey backend -> Key record
fromBackendKey forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int64 -> BackendKey SqlBackend
SqlBackendKey

fromSqlKey :: ToBackendKey SqlBackend record => Key record -> Int64
fromSqlKey :: forall record.
ToBackendKey SqlBackend record =>
Key record -> Int64
fromSqlKey = BackendKey SqlBackend -> Int64
unSqlBackendKey forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall backend record.
ToBackendKey backend record =>
Key record -> BackendKey backend
toBackendKey

whereStmtForKey :: PersistEntity record => SqlBackend -> Key record -> Text
whereStmtForKey :: forall record.
PersistEntity record =>
SqlBackend -> Key record -> Text
whereStmtForKey SqlBackend
conn Key record
k =
    Text -> [Text] -> Text
T.intercalate Text
" AND "
  forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t a -> [a]
Foldable.toList
  forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a. Semigroup a => a -> a -> a
<> Text
"=? ")
  forall a b. (a -> b) -> a -> b
$ SqlBackend -> EntityDef -> NonEmpty Text
dbIdColumns SqlBackend
conn EntityDef
entDef
  where
    entDef :: EntityDef
entDef = forall record (proxy :: * -> *).
PersistEntity record =>
proxy record -> EntityDef
entityDef forall a b. (a -> b) -> a -> b
$ forall record. Key record -> Maybe record
dummyFromKey Key record
k

whereStmtForKeys :: PersistEntity record => SqlBackend -> [Key record] -> Text
whereStmtForKeys :: forall record.
PersistEntity record =>
SqlBackend -> [Key record] -> Text
whereStmtForKeys SqlBackend
conn [Key record]
ks = Text -> [Text] -> Text
T.intercalate Text
" OR " forall a b. (a -> b) -> a -> b
$ forall record.
PersistEntity record =>
SqlBackend -> Key record -> Text
whereStmtForKey SqlBackend
conn forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` [Key record]
ks

-- | get the SQL string for the table that a PeristEntity represents
-- Useful for raw SQL queries
--
-- Your backend may provide a more convenient tableName function
-- which does not operate in a Monad
getTableName :: forall record m backend.
             ( PersistEntity record
             , BackendCompatible SqlBackend backend
             , Monad m
             ) => record -> ReaderT backend m Text
getTableName :: forall record (m :: * -> *) backend.
(PersistEntity record, BackendCompatible SqlBackend backend,
 Monad m) =>
record -> ReaderT backend m Text
getTableName record
rec = forall sup sub (m :: * -> *) a.
BackendCompatible sup sub =>
ReaderT sup m a -> ReaderT sub m a
withCompatibleBackend forall a b. (a -> b) -> a -> b
$ do
    SqlBackend
conn <- forall (m :: * -> *) r. Monad m => ReaderT r m r
ask
    forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ SqlBackend -> EntityDef -> Text
connEscapeTableName SqlBackend
conn (forall record (proxy :: * -> *).
PersistEntity record =>
proxy record -> EntityDef
entityDef forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just record
rec)

-- | useful for a backend to implement tableName by adding escaping
tableDBName :: (PersistEntity record) => record -> EntityNameDB
tableDBName :: forall record. PersistEntity record => record -> EntityNameDB
tableDBName record
rec = EntityDef -> EntityNameDB
getEntityDBName forall a b. (a -> b) -> a -> b
$ forall record (proxy :: * -> *).
PersistEntity record =>
proxy record -> EntityDef
entityDef (forall a. a -> Maybe a
Just record
rec)

-- | get the SQL string for the field that an EntityField represents
-- Useful for raw SQL queries
--
-- Your backend may provide a more convenient fieldName function
-- which does not operate in a Monad
getFieldName :: forall record typ m backend.
             ( PersistEntity record
             , PersistEntityBackend record ~ SqlBackend
             , BackendCompatible SqlBackend backend
             , Monad m
             )
             => EntityField record typ -> ReaderT backend m Text
getFieldName :: forall record typ (m :: * -> *) backend.
(PersistEntity record, PersistEntityBackend record ~ SqlBackend,
 BackendCompatible SqlBackend backend, Monad m) =>
EntityField record typ -> ReaderT backend m Text
getFieldName EntityField record typ
rec = forall sup sub (m :: * -> *) a.
BackendCompatible sup sub =>
ReaderT sup m a -> ReaderT sub m a
withCompatibleBackend forall a b. (a -> b) -> a -> b
$ do
    SqlBackend
conn <- forall (m :: * -> *) r. Monad m => ReaderT r m r
ask
    forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ SqlBackend -> FieldNameDB -> Text
connEscapeFieldName SqlBackend
conn (FieldDef -> FieldNameDB
fieldDB forall a b. (a -> b) -> a -> b
$ forall record typ.
PersistEntity record =>
EntityField record typ -> FieldDef
persistFieldDef EntityField record typ
rec)

-- | useful for a backend to implement fieldName by adding escaping
fieldDBName :: forall record typ. (PersistEntity record) => EntityField record typ -> FieldNameDB
fieldDBName :: forall record typ.
PersistEntity record =>
EntityField record typ -> FieldNameDB
fieldDBName = FieldDef -> FieldNameDB
fieldDB forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall record typ.
PersistEntity record =>
EntityField record typ -> FieldDef
persistFieldDef


instance PersistCore SqlBackend where
    newtype BackendKey SqlBackend = SqlBackendKey { BackendKey SqlBackend -> Int64
unSqlBackendKey :: Int64 }
        deriving stock (Int -> BackendKey SqlBackend -> ShowS
[BackendKey SqlBackend] -> ShowS
BackendKey SqlBackend -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BackendKey SqlBackend] -> ShowS
$cshowList :: [BackendKey SqlBackend] -> ShowS
show :: BackendKey SqlBackend -> String
$cshow :: BackendKey SqlBackend -> String
showsPrec :: Int -> BackendKey SqlBackend -> ShowS
$cshowsPrec :: Int -> BackendKey SqlBackend -> ShowS
Show, ReadPrec [BackendKey SqlBackend]
ReadPrec (BackendKey SqlBackend)
Int -> ReadS (BackendKey SqlBackend)
ReadS [BackendKey SqlBackend]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [BackendKey SqlBackend]
$creadListPrec :: ReadPrec [BackendKey SqlBackend]
readPrec :: ReadPrec (BackendKey SqlBackend)
$creadPrec :: ReadPrec (BackendKey SqlBackend)
readList :: ReadS [BackendKey SqlBackend]
$creadList :: ReadS [BackendKey SqlBackend]
readsPrec :: Int -> ReadS (BackendKey SqlBackend)
$creadsPrec :: Int -> ReadS (BackendKey SqlBackend)
Read, BackendKey SqlBackend -> BackendKey SqlBackend -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BackendKey SqlBackend -> BackendKey SqlBackend -> Bool
$c/= :: BackendKey SqlBackend -> BackendKey SqlBackend -> Bool
== :: BackendKey SqlBackend -> BackendKey SqlBackend -> Bool
$c== :: BackendKey SqlBackend -> BackendKey SqlBackend -> Bool
Eq, Eq (BackendKey SqlBackend)
BackendKey SqlBackend -> BackendKey SqlBackend -> Bool
BackendKey SqlBackend -> BackendKey SqlBackend -> Ordering
BackendKey SqlBackend
-> BackendKey SqlBackend -> BackendKey SqlBackend
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: BackendKey SqlBackend
-> BackendKey SqlBackend -> BackendKey SqlBackend
$cmin :: BackendKey SqlBackend
-> BackendKey SqlBackend -> BackendKey SqlBackend
max :: BackendKey SqlBackend
-> BackendKey SqlBackend -> BackendKey SqlBackend
$cmax :: BackendKey SqlBackend
-> BackendKey SqlBackend -> BackendKey SqlBackend
>= :: BackendKey SqlBackend -> BackendKey SqlBackend -> Bool
$c>= :: BackendKey SqlBackend -> BackendKey SqlBackend -> Bool
> :: BackendKey SqlBackend -> BackendKey SqlBackend -> Bool
$c> :: BackendKey SqlBackend -> BackendKey SqlBackend -> Bool
<= :: BackendKey SqlBackend -> BackendKey SqlBackend -> Bool
$c<= :: BackendKey SqlBackend -> BackendKey SqlBackend -> Bool
< :: BackendKey SqlBackend -> BackendKey SqlBackend -> Bool
$c< :: BackendKey SqlBackend -> BackendKey SqlBackend -> Bool
compare :: BackendKey SqlBackend -> BackendKey SqlBackend -> Ordering
$ccompare :: BackendKey SqlBackend -> BackendKey SqlBackend -> Ordering
Ord, forall x. Rep (BackendKey SqlBackend) x -> BackendKey SqlBackend
forall x. BackendKey SqlBackend -> Rep (BackendKey SqlBackend) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep (BackendKey SqlBackend) x -> BackendKey SqlBackend
$cfrom :: forall x. BackendKey SqlBackend -> Rep (BackendKey SqlBackend) x
Generic)
        deriving newtype (Integer -> BackendKey SqlBackend
BackendKey SqlBackend -> BackendKey SqlBackend
BackendKey SqlBackend
-> BackendKey SqlBackend -> BackendKey SqlBackend
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
fromInteger :: Integer -> BackendKey SqlBackend
$cfromInteger :: Integer -> BackendKey SqlBackend
signum :: BackendKey SqlBackend -> BackendKey SqlBackend
$csignum :: BackendKey SqlBackend -> BackendKey SqlBackend
abs :: BackendKey SqlBackend -> BackendKey SqlBackend
$cabs :: BackendKey SqlBackend -> BackendKey SqlBackend
negate :: BackendKey SqlBackend -> BackendKey SqlBackend
$cnegate :: BackendKey SqlBackend -> BackendKey SqlBackend
* :: BackendKey SqlBackend
-> BackendKey SqlBackend -> BackendKey SqlBackend
$c* :: BackendKey SqlBackend
-> BackendKey SqlBackend -> BackendKey SqlBackend
- :: BackendKey SqlBackend
-> BackendKey SqlBackend -> BackendKey SqlBackend
$c- :: BackendKey SqlBackend
-> BackendKey SqlBackend -> BackendKey SqlBackend
+ :: BackendKey SqlBackend
-> BackendKey SqlBackend -> BackendKey SqlBackend
$c+ :: BackendKey SqlBackend
-> BackendKey SqlBackend -> BackendKey SqlBackend
Num, Enum (BackendKey SqlBackend)
Real (BackendKey SqlBackend)
BackendKey SqlBackend -> Integer
BackendKey SqlBackend
-> BackendKey SqlBackend
-> (BackendKey SqlBackend, BackendKey SqlBackend)
BackendKey SqlBackend
-> BackendKey SqlBackend -> BackendKey SqlBackend
forall a.
Real a
-> Enum a
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> (a, a))
-> (a -> a -> (a, a))
-> (a -> Integer)
-> Integral a
toInteger :: BackendKey SqlBackend -> Integer
$ctoInteger :: BackendKey SqlBackend -> Integer
divMod :: BackendKey SqlBackend
-> BackendKey SqlBackend
-> (BackendKey SqlBackend, BackendKey SqlBackend)
$cdivMod :: BackendKey SqlBackend
-> BackendKey SqlBackend
-> (BackendKey SqlBackend, BackendKey SqlBackend)
quotRem :: BackendKey SqlBackend
-> BackendKey SqlBackend
-> (BackendKey SqlBackend, BackendKey SqlBackend)
$cquotRem :: BackendKey SqlBackend
-> BackendKey SqlBackend
-> (BackendKey SqlBackend, BackendKey SqlBackend)
mod :: BackendKey SqlBackend
-> BackendKey SqlBackend -> BackendKey SqlBackend
$cmod :: BackendKey SqlBackend
-> BackendKey SqlBackend -> BackendKey SqlBackend
div :: BackendKey SqlBackend
-> BackendKey SqlBackend -> BackendKey SqlBackend
$cdiv :: BackendKey SqlBackend
-> BackendKey SqlBackend -> BackendKey SqlBackend
rem :: BackendKey SqlBackend
-> BackendKey SqlBackend -> BackendKey SqlBackend
$crem :: BackendKey SqlBackend
-> BackendKey SqlBackend -> BackendKey SqlBackend
quot :: BackendKey SqlBackend
-> BackendKey SqlBackend -> BackendKey SqlBackend
$cquot :: BackendKey SqlBackend
-> BackendKey SqlBackend -> BackendKey SqlBackend
Integral, PersistValue -> Either Text (BackendKey SqlBackend)
BackendKey SqlBackend -> PersistValue
forall a.
(a -> PersistValue)
-> (PersistValue -> Either Text a) -> PersistField a
fromPersistValue :: PersistValue -> Either Text (BackendKey SqlBackend)
$cfromPersistValue :: PersistValue -> Either Text (BackendKey SqlBackend)
toPersistValue :: BackendKey SqlBackend -> PersistValue
$ctoPersistValue :: BackendKey SqlBackend -> PersistValue
PersistField, PersistField (BackendKey SqlBackend)
Proxy (BackendKey SqlBackend) -> SqlType
forall a.
PersistField a -> (Proxy a -> SqlType) -> PersistFieldSql a
sqlType :: Proxy (BackendKey SqlBackend) -> SqlType
$csqlType :: Proxy (BackendKey SqlBackend) -> SqlType
PersistFieldSql, Text -> Maybe (BackendKey SqlBackend)
BackendKey SqlBackend -> Text
forall s. (Text -> Maybe s) -> (s -> Text) -> PathPiece s
toPathPiece :: BackendKey SqlBackend -> Text
$ctoPathPiece :: BackendKey SqlBackend -> Text
fromPathPiece :: Text -> Maybe (BackendKey SqlBackend)
$cfromPathPiece :: Text -> Maybe (BackendKey SqlBackend)
PathPiece, BackendKey SqlBackend -> ByteString
BackendKey SqlBackend -> Builder
BackendKey SqlBackend -> Text
forall a.
(a -> Text)
-> (a -> Builder)
-> (a -> ByteString)
-> (a -> Text)
-> ToHttpApiData a
toQueryParam :: BackendKey SqlBackend -> Text
$ctoQueryParam :: BackendKey SqlBackend -> Text
toHeader :: BackendKey SqlBackend -> ByteString
$ctoHeader :: BackendKey SqlBackend -> ByteString
toEncodedUrlPiece :: BackendKey SqlBackend -> Builder
$ctoEncodedUrlPiece :: BackendKey SqlBackend -> Builder
toUrlPiece :: BackendKey SqlBackend -> Text
$ctoUrlPiece :: BackendKey SqlBackend -> Text
ToHttpApiData, ByteString -> Either Text (BackendKey SqlBackend)
Text -> Either Text (BackendKey SqlBackend)
forall a.
(Text -> Either Text a)
-> (ByteString -> Either Text a)
-> (Text -> Either Text a)
-> FromHttpApiData a
parseQueryParam :: Text -> Either Text (BackendKey SqlBackend)
$cparseQueryParam :: Text -> Either Text (BackendKey SqlBackend)
parseHeader :: ByteString -> Either Text (BackendKey SqlBackend)
$cparseHeader :: ByteString -> Either Text (BackendKey SqlBackend)
parseUrlPiece :: Text -> Either Text (BackendKey SqlBackend)
$cparseUrlPiece :: Text -> Either Text (BackendKey SqlBackend)
FromHttpApiData, Num (BackendKey SqlBackend)
Ord (BackendKey SqlBackend)
BackendKey SqlBackend -> Rational
forall a. Num a -> Ord a -> (a -> Rational) -> Real a
toRational :: BackendKey SqlBackend -> Rational
$ctoRational :: BackendKey SqlBackend -> Rational
Real, Int -> BackendKey SqlBackend
BackendKey SqlBackend -> Int
BackendKey SqlBackend -> [BackendKey SqlBackend]
BackendKey SqlBackend -> BackendKey SqlBackend
BackendKey SqlBackend
-> BackendKey SqlBackend -> [BackendKey SqlBackend]
BackendKey SqlBackend
-> BackendKey SqlBackend
-> BackendKey SqlBackend
-> [BackendKey SqlBackend]
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: BackendKey SqlBackend
-> BackendKey SqlBackend
-> BackendKey SqlBackend
-> [BackendKey SqlBackend]
$cenumFromThenTo :: BackendKey SqlBackend
-> BackendKey SqlBackend
-> BackendKey SqlBackend
-> [BackendKey SqlBackend]
enumFromTo :: BackendKey SqlBackend
-> BackendKey SqlBackend -> [BackendKey SqlBackend]
$cenumFromTo :: BackendKey SqlBackend
-> BackendKey SqlBackend -> [BackendKey SqlBackend]
enumFromThen :: BackendKey SqlBackend
-> BackendKey SqlBackend -> [BackendKey SqlBackend]
$cenumFromThen :: BackendKey SqlBackend
-> BackendKey SqlBackend -> [BackendKey SqlBackend]
enumFrom :: BackendKey SqlBackend -> [BackendKey SqlBackend]
$cenumFrom :: BackendKey SqlBackend -> [BackendKey SqlBackend]
fromEnum :: BackendKey SqlBackend -> Int
$cfromEnum :: BackendKey SqlBackend -> Int
toEnum :: Int -> BackendKey SqlBackend
$ctoEnum :: Int -> BackendKey SqlBackend
pred :: BackendKey SqlBackend -> BackendKey SqlBackend
$cpred :: BackendKey SqlBackend -> BackendKey SqlBackend
succ :: BackendKey SqlBackend -> BackendKey SqlBackend
$csucc :: BackendKey SqlBackend -> BackendKey SqlBackend
Enum, BackendKey SqlBackend
forall a. a -> a -> Bounded a
maxBound :: BackendKey SqlBackend
$cmaxBound :: BackendKey SqlBackend
minBound :: BackendKey SqlBackend
$cminBound :: BackendKey SqlBackend
Bounded, [BackendKey SqlBackend] -> Encoding
[BackendKey SqlBackend] -> Value
BackendKey SqlBackend -> Encoding
BackendKey SqlBackend -> Value
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [BackendKey SqlBackend] -> Encoding
$ctoEncodingList :: [BackendKey SqlBackend] -> Encoding
toJSONList :: [BackendKey SqlBackend] -> Value
$ctoJSONList :: [BackendKey SqlBackend] -> Value
toEncoding :: BackendKey SqlBackend -> Encoding
$ctoEncoding :: BackendKey SqlBackend -> Encoding
toJSON :: BackendKey SqlBackend -> Value
$ctoJSON :: BackendKey SqlBackend -> Value
A.ToJSON, Value -> Parser [BackendKey SqlBackend]
Value -> Parser (BackendKey SqlBackend)
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [BackendKey SqlBackend]
$cparseJSONList :: Value -> Parser [BackendKey SqlBackend]
parseJSON :: Value -> Parser (BackendKey SqlBackend)
$cparseJSON :: Value -> Parser (BackendKey SqlBackend)
A.FromJSON)

instance PersistCore SqlReadBackend where
    newtype BackendKey SqlReadBackend = SqlReadBackendKey { BackendKey SqlReadBackend -> Int64
unSqlReadBackendKey :: Int64 }
        deriving stock (Int -> BackendKey SqlReadBackend -> ShowS
[BackendKey SqlReadBackend] -> ShowS
BackendKey SqlReadBackend -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BackendKey SqlReadBackend] -> ShowS
$cshowList :: [BackendKey SqlReadBackend] -> ShowS
show :: BackendKey SqlReadBackend -> String
$cshow :: BackendKey SqlReadBackend -> String
showsPrec :: Int -> BackendKey SqlReadBackend -> ShowS
$cshowsPrec :: Int -> BackendKey SqlReadBackend -> ShowS
Show, ReadPrec [BackendKey SqlReadBackend]
ReadPrec (BackendKey SqlReadBackend)
Int -> ReadS (BackendKey SqlReadBackend)
ReadS [BackendKey SqlReadBackend]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [BackendKey SqlReadBackend]
$creadListPrec :: ReadPrec [BackendKey SqlReadBackend]
readPrec :: ReadPrec (BackendKey SqlReadBackend)
$creadPrec :: ReadPrec (BackendKey SqlReadBackend)
readList :: ReadS [BackendKey SqlReadBackend]
$creadList :: ReadS [BackendKey SqlReadBackend]
readsPrec :: Int -> ReadS (BackendKey SqlReadBackend)
$creadsPrec :: Int -> ReadS (BackendKey SqlReadBackend)
Read, BackendKey SqlReadBackend -> BackendKey SqlReadBackend -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BackendKey SqlReadBackend -> BackendKey SqlReadBackend -> Bool
$c/= :: BackendKey SqlReadBackend -> BackendKey SqlReadBackend -> Bool
== :: BackendKey SqlReadBackend -> BackendKey SqlReadBackend -> Bool
$c== :: BackendKey SqlReadBackend -> BackendKey SqlReadBackend -> Bool
Eq, Eq (BackendKey SqlReadBackend)
BackendKey SqlReadBackend -> BackendKey SqlReadBackend -> Bool
BackendKey SqlReadBackend -> BackendKey SqlReadBackend -> Ordering
BackendKey SqlReadBackend
-> BackendKey SqlReadBackend -> BackendKey SqlReadBackend
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: BackendKey SqlReadBackend
-> BackendKey SqlReadBackend -> BackendKey SqlReadBackend
$cmin :: BackendKey SqlReadBackend
-> BackendKey SqlReadBackend -> BackendKey SqlReadBackend
max :: BackendKey SqlReadBackend
-> BackendKey SqlReadBackend -> BackendKey SqlReadBackend
$cmax :: BackendKey SqlReadBackend
-> BackendKey SqlReadBackend -> BackendKey SqlReadBackend
>= :: BackendKey SqlReadBackend -> BackendKey SqlReadBackend -> Bool
$c>= :: BackendKey SqlReadBackend -> BackendKey SqlReadBackend -> Bool
> :: BackendKey SqlReadBackend -> BackendKey SqlReadBackend -> Bool
$c> :: BackendKey SqlReadBackend -> BackendKey SqlReadBackend -> Bool
<= :: BackendKey SqlReadBackend -> BackendKey SqlReadBackend -> Bool
$c<= :: BackendKey SqlReadBackend -> BackendKey SqlReadBackend -> Bool
< :: BackendKey SqlReadBackend -> BackendKey SqlReadBackend -> Bool
$c< :: BackendKey SqlReadBackend -> BackendKey SqlReadBackend -> Bool
compare :: BackendKey SqlReadBackend -> BackendKey SqlReadBackend -> Ordering
$ccompare :: BackendKey SqlReadBackend -> BackendKey SqlReadBackend -> Ordering
Ord, forall x.
Rep (BackendKey SqlReadBackend) x -> BackendKey SqlReadBackend
forall x.
BackendKey SqlReadBackend -> Rep (BackendKey SqlReadBackend) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x.
Rep (BackendKey SqlReadBackend) x -> BackendKey SqlReadBackend
$cfrom :: forall x.
BackendKey SqlReadBackend -> Rep (BackendKey SqlReadBackend) x
Generic)
        deriving newtype (Integer -> BackendKey SqlReadBackend
BackendKey SqlReadBackend -> BackendKey SqlReadBackend
BackendKey SqlReadBackend
-> BackendKey SqlReadBackend -> BackendKey SqlReadBackend
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
fromInteger :: Integer -> BackendKey SqlReadBackend
$cfromInteger :: Integer -> BackendKey SqlReadBackend
signum :: BackendKey SqlReadBackend -> BackendKey SqlReadBackend
$csignum :: BackendKey SqlReadBackend -> BackendKey SqlReadBackend
abs :: BackendKey SqlReadBackend -> BackendKey SqlReadBackend
$cabs :: BackendKey SqlReadBackend -> BackendKey SqlReadBackend
negate :: BackendKey SqlReadBackend -> BackendKey SqlReadBackend
$cnegate :: BackendKey SqlReadBackend -> BackendKey SqlReadBackend
* :: BackendKey SqlReadBackend
-> BackendKey SqlReadBackend -> BackendKey SqlReadBackend
$c* :: BackendKey SqlReadBackend
-> BackendKey SqlReadBackend -> BackendKey SqlReadBackend
- :: BackendKey SqlReadBackend
-> BackendKey SqlReadBackend -> BackendKey SqlReadBackend
$c- :: BackendKey SqlReadBackend
-> BackendKey SqlReadBackend -> BackendKey SqlReadBackend
+ :: BackendKey SqlReadBackend
-> BackendKey SqlReadBackend -> BackendKey SqlReadBackend
$c+ :: BackendKey SqlReadBackend
-> BackendKey SqlReadBackend -> BackendKey SqlReadBackend
Num, Enum (BackendKey SqlReadBackend)
Real (BackendKey SqlReadBackend)
BackendKey SqlReadBackend -> Integer
BackendKey SqlReadBackend
-> BackendKey SqlReadBackend
-> (BackendKey SqlReadBackend, BackendKey SqlReadBackend)
BackendKey SqlReadBackend
-> BackendKey SqlReadBackend -> BackendKey SqlReadBackend
forall a.
Real a
-> Enum a
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> (a, a))
-> (a -> a -> (a, a))
-> (a -> Integer)
-> Integral a
toInteger :: BackendKey SqlReadBackend -> Integer
$ctoInteger :: BackendKey SqlReadBackend -> Integer
divMod :: BackendKey SqlReadBackend
-> BackendKey SqlReadBackend
-> (BackendKey SqlReadBackend, BackendKey SqlReadBackend)
$cdivMod :: BackendKey SqlReadBackend
-> BackendKey SqlReadBackend
-> (BackendKey SqlReadBackend, BackendKey SqlReadBackend)
quotRem :: BackendKey SqlReadBackend
-> BackendKey SqlReadBackend
-> (BackendKey SqlReadBackend, BackendKey SqlReadBackend)
$cquotRem :: BackendKey SqlReadBackend
-> BackendKey SqlReadBackend
-> (BackendKey SqlReadBackend, BackendKey SqlReadBackend)
mod :: BackendKey SqlReadBackend
-> BackendKey SqlReadBackend -> BackendKey SqlReadBackend
$cmod :: BackendKey SqlReadBackend
-> BackendKey SqlReadBackend -> BackendKey SqlReadBackend
div :: BackendKey SqlReadBackend
-> BackendKey SqlReadBackend -> BackendKey SqlReadBackend
$cdiv :: BackendKey SqlReadBackend
-> BackendKey SqlReadBackend -> BackendKey SqlReadBackend
rem :: BackendKey SqlReadBackend
-> BackendKey SqlReadBackend -> BackendKey SqlReadBackend
$crem :: BackendKey SqlReadBackend
-> BackendKey SqlReadBackend -> BackendKey SqlReadBackend
quot :: BackendKey SqlReadBackend
-> BackendKey SqlReadBackend -> BackendKey SqlReadBackend
$cquot :: BackendKey SqlReadBackend
-> BackendKey SqlReadBackend -> BackendKey SqlReadBackend
Integral, PersistValue -> Either Text (BackendKey SqlReadBackend)
BackendKey SqlReadBackend -> PersistValue
forall a.
(a -> PersistValue)
-> (PersistValue -> Either Text a) -> PersistField a
fromPersistValue :: PersistValue -> Either Text (BackendKey SqlReadBackend)
$cfromPersistValue :: PersistValue -> Either Text (BackendKey SqlReadBackend)
toPersistValue :: BackendKey SqlReadBackend -> PersistValue
$ctoPersistValue :: BackendKey SqlReadBackend -> PersistValue
PersistField, PersistField (BackendKey SqlReadBackend)
Proxy (BackendKey SqlReadBackend) -> SqlType
forall a.
PersistField a -> (Proxy a -> SqlType) -> PersistFieldSql a
sqlType :: Proxy (BackendKey SqlReadBackend) -> SqlType
$csqlType :: Proxy (BackendKey SqlReadBackend) -> SqlType
PersistFieldSql, Text -> Maybe (BackendKey SqlReadBackend)
BackendKey SqlReadBackend -> Text
forall s. (Text -> Maybe s) -> (s -> Text) -> PathPiece s
toPathPiece :: BackendKey SqlReadBackend -> Text
$ctoPathPiece :: BackendKey SqlReadBackend -> Text
fromPathPiece :: Text -> Maybe (BackendKey SqlReadBackend)
$cfromPathPiece :: Text -> Maybe (BackendKey SqlReadBackend)
PathPiece, BackendKey SqlReadBackend -> ByteString
BackendKey SqlReadBackend -> Builder
BackendKey SqlReadBackend -> Text
forall a.
(a -> Text)
-> (a -> Builder)
-> (a -> ByteString)
-> (a -> Text)
-> ToHttpApiData a
toQueryParam :: BackendKey SqlReadBackend -> Text
$ctoQueryParam :: BackendKey SqlReadBackend -> Text
toHeader :: BackendKey SqlReadBackend -> ByteString
$ctoHeader :: BackendKey SqlReadBackend -> ByteString
toEncodedUrlPiece :: BackendKey SqlReadBackend -> Builder
$ctoEncodedUrlPiece :: BackendKey SqlReadBackend -> Builder
toUrlPiece :: BackendKey SqlReadBackend -> Text
$ctoUrlPiece :: BackendKey SqlReadBackend -> Text
ToHttpApiData, ByteString -> Either Text (BackendKey SqlReadBackend)
Text -> Either Text (BackendKey SqlReadBackend)
forall a.
(Text -> Either Text a)
-> (ByteString -> Either Text a)
-> (Text -> Either Text a)
-> FromHttpApiData a
parseQueryParam :: Text -> Either Text (BackendKey SqlReadBackend)
$cparseQueryParam :: Text -> Either Text (BackendKey SqlReadBackend)
parseHeader :: ByteString -> Either Text (BackendKey SqlReadBackend)
$cparseHeader :: ByteString -> Either Text (BackendKey SqlReadBackend)
parseUrlPiece :: Text -> Either Text (BackendKey SqlReadBackend)
$cparseUrlPiece :: Text -> Either Text (BackendKey SqlReadBackend)
FromHttpApiData, Num (BackendKey SqlReadBackend)
Ord (BackendKey SqlReadBackend)
BackendKey SqlReadBackend -> Rational
forall a. Num a -> Ord a -> (a -> Rational) -> Real a
toRational :: BackendKey SqlReadBackend -> Rational
$ctoRational :: BackendKey SqlReadBackend -> Rational
Real, Int -> BackendKey SqlReadBackend
BackendKey SqlReadBackend -> Int
BackendKey SqlReadBackend -> [BackendKey SqlReadBackend]
BackendKey SqlReadBackend -> BackendKey SqlReadBackend
BackendKey SqlReadBackend
-> BackendKey SqlReadBackend -> [BackendKey SqlReadBackend]
BackendKey SqlReadBackend
-> BackendKey SqlReadBackend
-> BackendKey SqlReadBackend
-> [BackendKey SqlReadBackend]
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: BackendKey SqlReadBackend
-> BackendKey SqlReadBackend
-> BackendKey SqlReadBackend
-> [BackendKey SqlReadBackend]
$cenumFromThenTo :: BackendKey SqlReadBackend
-> BackendKey SqlReadBackend
-> BackendKey SqlReadBackend
-> [BackendKey SqlReadBackend]
enumFromTo :: BackendKey SqlReadBackend
-> BackendKey SqlReadBackend -> [BackendKey SqlReadBackend]
$cenumFromTo :: BackendKey SqlReadBackend
-> BackendKey SqlReadBackend -> [BackendKey SqlReadBackend]
enumFromThen :: BackendKey SqlReadBackend
-> BackendKey SqlReadBackend -> [BackendKey SqlReadBackend]
$cenumFromThen :: BackendKey SqlReadBackend
-> BackendKey SqlReadBackend -> [BackendKey SqlReadBackend]
enumFrom :: BackendKey SqlReadBackend -> [BackendKey SqlReadBackend]
$cenumFrom :: BackendKey SqlReadBackend -> [BackendKey SqlReadBackend]
fromEnum :: BackendKey SqlReadBackend -> Int
$cfromEnum :: BackendKey SqlReadBackend -> Int
toEnum :: Int -> BackendKey SqlReadBackend
$ctoEnum :: Int -> BackendKey SqlReadBackend
pred :: BackendKey SqlReadBackend -> BackendKey SqlReadBackend
$cpred :: BackendKey SqlReadBackend -> BackendKey SqlReadBackend
succ :: BackendKey SqlReadBackend -> BackendKey SqlReadBackend
$csucc :: BackendKey SqlReadBackend -> BackendKey SqlReadBackend
Enum, BackendKey SqlReadBackend
forall a. a -> a -> Bounded a
maxBound :: BackendKey SqlReadBackend
$cmaxBound :: BackendKey SqlReadBackend
minBound :: BackendKey SqlReadBackend
$cminBound :: BackendKey SqlReadBackend
Bounded, [BackendKey SqlReadBackend] -> Encoding
[BackendKey SqlReadBackend] -> Value
BackendKey SqlReadBackend -> Encoding
BackendKey SqlReadBackend -> Value
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [BackendKey SqlReadBackend] -> Encoding
$ctoEncodingList :: [BackendKey SqlReadBackend] -> Encoding
toJSONList :: [BackendKey SqlReadBackend] -> Value
$ctoJSONList :: [BackendKey SqlReadBackend] -> Value
toEncoding :: BackendKey SqlReadBackend -> Encoding
$ctoEncoding :: BackendKey SqlReadBackend -> Encoding
toJSON :: BackendKey SqlReadBackend -> Value
$ctoJSON :: BackendKey SqlReadBackend -> Value
A.ToJSON, Value -> Parser [BackendKey SqlReadBackend]
Value -> Parser (BackendKey SqlReadBackend)
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [BackendKey SqlReadBackend]
$cparseJSONList :: Value -> Parser [BackendKey SqlReadBackend]
parseJSON :: Value -> Parser (BackendKey SqlReadBackend)
$cparseJSON :: Value -> Parser (BackendKey SqlReadBackend)
A.FromJSON)

instance PersistCore SqlWriteBackend where
    newtype BackendKey SqlWriteBackend = SqlWriteBackendKey { BackendKey SqlWriteBackend -> Int64
unSqlWriteBackendKey :: Int64 }
        deriving stock (Int -> BackendKey SqlWriteBackend -> ShowS
[BackendKey SqlWriteBackend] -> ShowS
BackendKey SqlWriteBackend -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BackendKey SqlWriteBackend] -> ShowS
$cshowList :: [BackendKey SqlWriteBackend] -> ShowS
show :: BackendKey SqlWriteBackend -> String
$cshow :: BackendKey SqlWriteBackend -> String
showsPrec :: Int -> BackendKey SqlWriteBackend -> ShowS
$cshowsPrec :: Int -> BackendKey SqlWriteBackend -> ShowS
Show, ReadPrec [BackendKey SqlWriteBackend]
ReadPrec (BackendKey SqlWriteBackend)
Int -> ReadS (BackendKey SqlWriteBackend)
ReadS [BackendKey SqlWriteBackend]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [BackendKey SqlWriteBackend]
$creadListPrec :: ReadPrec [BackendKey SqlWriteBackend]
readPrec :: ReadPrec (BackendKey SqlWriteBackend)
$creadPrec :: ReadPrec (BackendKey SqlWriteBackend)
readList :: ReadS [BackendKey SqlWriteBackend]
$creadList :: ReadS [BackendKey SqlWriteBackend]
readsPrec :: Int -> ReadS (BackendKey SqlWriteBackend)
$creadsPrec :: Int -> ReadS (BackendKey SqlWriteBackend)
Read, BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend -> Bool
$c/= :: BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend -> Bool
== :: BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend -> Bool
$c== :: BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend -> Bool
Eq, Eq (BackendKey SqlWriteBackend)
BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend -> Bool
BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend -> Ordering
BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend
$cmin :: BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend
max :: BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend
$cmax :: BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend
>= :: BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend -> Bool
$c>= :: BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend -> Bool
> :: BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend -> Bool
$c> :: BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend -> Bool
<= :: BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend -> Bool
$c<= :: BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend -> Bool
< :: BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend -> Bool
$c< :: BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend -> Bool
compare :: BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend -> Ordering
$ccompare :: BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend -> Ordering
Ord, forall x.
Rep (BackendKey SqlWriteBackend) x -> BackendKey SqlWriteBackend
forall x.
BackendKey SqlWriteBackend -> Rep (BackendKey SqlWriteBackend) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x.
Rep (BackendKey SqlWriteBackend) x -> BackendKey SqlWriteBackend
$cfrom :: forall x.
BackendKey SqlWriteBackend -> Rep (BackendKey SqlWriteBackend) x
Generic)
        deriving newtype (Integer -> BackendKey SqlWriteBackend
BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend
BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
fromInteger :: Integer -> BackendKey SqlWriteBackend
$cfromInteger :: Integer -> BackendKey SqlWriteBackend
signum :: BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend
$csignum :: BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend
abs :: BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend
$cabs :: BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend
negate :: BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend
$cnegate :: BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend
* :: BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend
$c* :: BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend
- :: BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend
$c- :: BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend
+ :: BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend
$c+ :: BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend
Num, Enum (BackendKey SqlWriteBackend)
Real (BackendKey SqlWriteBackend)
BackendKey SqlWriteBackend -> Integer
BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend
-> (BackendKey SqlWriteBackend, BackendKey SqlWriteBackend)
BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend
forall a.
Real a
-> Enum a
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> (a, a))
-> (a -> a -> (a, a))
-> (a -> Integer)
-> Integral a
toInteger :: BackendKey SqlWriteBackend -> Integer
$ctoInteger :: BackendKey SqlWriteBackend -> Integer
divMod :: BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend
-> (BackendKey SqlWriteBackend, BackendKey SqlWriteBackend)
$cdivMod :: BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend
-> (BackendKey SqlWriteBackend, BackendKey SqlWriteBackend)
quotRem :: BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend
-> (BackendKey SqlWriteBackend, BackendKey SqlWriteBackend)
$cquotRem :: BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend
-> (BackendKey SqlWriteBackend, BackendKey SqlWriteBackend)
mod :: BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend
$cmod :: BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend
div :: BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend
$cdiv :: BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend
rem :: BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend
$crem :: BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend
quot :: BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend
$cquot :: BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend
Integral, PersistValue -> Either Text (BackendKey SqlWriteBackend)
BackendKey SqlWriteBackend -> PersistValue
forall a.
(a -> PersistValue)
-> (PersistValue -> Either Text a) -> PersistField a
fromPersistValue :: PersistValue -> Either Text (BackendKey SqlWriteBackend)
$cfromPersistValue :: PersistValue -> Either Text (BackendKey SqlWriteBackend)
toPersistValue :: BackendKey SqlWriteBackend -> PersistValue
$ctoPersistValue :: BackendKey SqlWriteBackend -> PersistValue
PersistField, PersistField (BackendKey SqlWriteBackend)
Proxy (BackendKey SqlWriteBackend) -> SqlType
forall a.
PersistField a -> (Proxy a -> SqlType) -> PersistFieldSql a
sqlType :: Proxy (BackendKey SqlWriteBackend) -> SqlType
$csqlType :: Proxy (BackendKey SqlWriteBackend) -> SqlType
PersistFieldSql, Text -> Maybe (BackendKey SqlWriteBackend)
BackendKey SqlWriteBackend -> Text
forall s. (Text -> Maybe s) -> (s -> Text) -> PathPiece s
toPathPiece :: BackendKey SqlWriteBackend -> Text
$ctoPathPiece :: BackendKey SqlWriteBackend -> Text
fromPathPiece :: Text -> Maybe (BackendKey SqlWriteBackend)
$cfromPathPiece :: Text -> Maybe (BackendKey SqlWriteBackend)
PathPiece, BackendKey SqlWriteBackend -> ByteString
BackendKey SqlWriteBackend -> Builder
BackendKey SqlWriteBackend -> Text
forall a.
(a -> Text)
-> (a -> Builder)
-> (a -> ByteString)
-> (a -> Text)
-> ToHttpApiData a
toQueryParam :: BackendKey SqlWriteBackend -> Text
$ctoQueryParam :: BackendKey SqlWriteBackend -> Text
toHeader :: BackendKey SqlWriteBackend -> ByteString
$ctoHeader :: BackendKey SqlWriteBackend -> ByteString
toEncodedUrlPiece :: BackendKey SqlWriteBackend -> Builder
$ctoEncodedUrlPiece :: BackendKey SqlWriteBackend -> Builder
toUrlPiece :: BackendKey SqlWriteBackend -> Text
$ctoUrlPiece :: BackendKey SqlWriteBackend -> Text
ToHttpApiData, ByteString -> Either Text (BackendKey SqlWriteBackend)
Text -> Either Text (BackendKey SqlWriteBackend)
forall a.
(Text -> Either Text a)
-> (ByteString -> Either Text a)
-> (Text -> Either Text a)
-> FromHttpApiData a
parseQueryParam :: Text -> Either Text (BackendKey SqlWriteBackend)
$cparseQueryParam :: Text -> Either Text (BackendKey SqlWriteBackend)
parseHeader :: ByteString -> Either Text (BackendKey SqlWriteBackend)
$cparseHeader :: ByteString -> Either Text (BackendKey SqlWriteBackend)
parseUrlPiece :: Text -> Either Text (BackendKey SqlWriteBackend)
$cparseUrlPiece :: Text -> Either Text (BackendKey SqlWriteBackend)
FromHttpApiData, Num (BackendKey SqlWriteBackend)
Ord (BackendKey SqlWriteBackend)
BackendKey SqlWriteBackend -> Rational
forall a. Num a -> Ord a -> (a -> Rational) -> Real a
toRational :: BackendKey SqlWriteBackend -> Rational
$ctoRational :: BackendKey SqlWriteBackend -> Rational
Real, Int -> BackendKey SqlWriteBackend
BackendKey SqlWriteBackend -> Int
BackendKey SqlWriteBackend -> [BackendKey SqlWriteBackend]
BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend
BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend -> [BackendKey SqlWriteBackend]
BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend
-> [BackendKey SqlWriteBackend]
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend
-> [BackendKey SqlWriteBackend]
$cenumFromThenTo :: BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend
-> [BackendKey SqlWriteBackend]
enumFromTo :: BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend -> [BackendKey SqlWriteBackend]
$cenumFromTo :: BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend -> [BackendKey SqlWriteBackend]
enumFromThen :: BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend -> [BackendKey SqlWriteBackend]
$cenumFromThen :: BackendKey SqlWriteBackend
-> BackendKey SqlWriteBackend -> [BackendKey SqlWriteBackend]
enumFrom :: BackendKey SqlWriteBackend -> [BackendKey SqlWriteBackend]
$cenumFrom :: BackendKey SqlWriteBackend -> [BackendKey SqlWriteBackend]
fromEnum :: BackendKey SqlWriteBackend -> Int
$cfromEnum :: BackendKey SqlWriteBackend -> Int
toEnum :: Int -> BackendKey SqlWriteBackend
$ctoEnum :: Int -> BackendKey SqlWriteBackend
pred :: BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend
$cpred :: BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend
succ :: BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend
$csucc :: BackendKey SqlWriteBackend -> BackendKey SqlWriteBackend
Enum, BackendKey SqlWriteBackend
forall a. a -> a -> Bounded a
maxBound :: BackendKey SqlWriteBackend
$cmaxBound :: BackendKey SqlWriteBackend
minBound :: BackendKey SqlWriteBackend
$cminBound :: BackendKey SqlWriteBackend
Bounded, [BackendKey SqlWriteBackend] -> Encoding
[BackendKey SqlWriteBackend] -> Value
BackendKey SqlWriteBackend -> Encoding
BackendKey SqlWriteBackend -> Value
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [BackendKey SqlWriteBackend] -> Encoding
$ctoEncodingList :: [BackendKey SqlWriteBackend] -> Encoding
toJSONList :: [BackendKey SqlWriteBackend] -> Value
$ctoJSONList :: [BackendKey SqlWriteBackend] -> Value
toEncoding :: BackendKey SqlWriteBackend -> Encoding
$ctoEncoding :: BackendKey SqlWriteBackend -> Encoding
toJSON :: BackendKey SqlWriteBackend -> Value
$ctoJSON :: BackendKey SqlWriteBackend -> Value
A.ToJSON, Value -> Parser [BackendKey SqlWriteBackend]
Value -> Parser (BackendKey SqlWriteBackend)
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [BackendKey SqlWriteBackend]
$cparseJSONList :: Value -> Parser [BackendKey SqlWriteBackend]
parseJSON :: Value -> Parser (BackendKey SqlWriteBackend)
$cparseJSON :: Value -> Parser (BackendKey SqlWriteBackend)
A.FromJSON)

instance BackendCompatible SqlBackend SqlBackend where
    projectBackend :: SqlBackend -> SqlBackend
projectBackend = forall a. a -> a
id

instance BackendCompatible SqlBackend SqlReadBackend where
    projectBackend :: SqlReadBackend -> SqlBackend
projectBackend = SqlReadBackend -> SqlBackend
unSqlReadBackend

instance BackendCompatible SqlBackend SqlWriteBackend where
    projectBackend :: SqlWriteBackend -> SqlBackend
projectBackend = SqlWriteBackend -> SqlBackend
unSqlWriteBackend

instance PersistStoreWrite SqlBackend where
    update :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record SqlBackend) =>
Key record -> [Update record] -> ReaderT SqlBackend m ()
update Key record
_ [] = forall (m :: * -> *) a. Monad m => a -> m a
return ()
    update Key record
k [Update record]
upds = do
        SqlBackend
conn <- forall (m :: * -> *) r. Monad m => ReaderT r m r
ask
        let wher :: Text
wher = forall record.
PersistEntity record =>
SqlBackend -> Key record -> Text
whereStmtForKey SqlBackend
conn Key record
k
        let sql :: Text
sql = [Text] -> Text
T.concat
                [ Text
"UPDATE "
                , SqlBackend -> EntityDef -> Text
connEscapeTableName SqlBackend
conn (forall record (proxy :: * -> *).
PersistEntity record =>
proxy record -> EntityDef
entityDef forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall record. Key record -> record
recordTypeFromKey Key record
k)
                , Text
" SET "
                , Text -> [Text] -> Text
T.intercalate Text
"," forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (forall record.
PersistEntity record =>
SqlBackend -> Update record -> Text
mkUpdateText SqlBackend
conn) [Update record]
upds
                , Text
" WHERE "
                , Text
wher
                ]
        forall (m :: * -> *) backend.
(MonadIO m, BackendCompatible SqlBackend backend) =>
Text -> [PersistValue] -> ReaderT backend m ()
rawExecute Text
sql forall a b. (a -> b) -> a -> b
$
            forall a b. (a -> b) -> [a] -> [b]
map forall v. Update v -> PersistValue
updatePersistValue [Update record]
upds forall a. Monoid a => a -> a -> a
`mappend` forall record. PersistEntity record => Key record -> [PersistValue]
keyToValues Key record
k

    insert_ :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record SqlBackend,
 SafeToInsert record) =>
record -> ReaderT SqlBackend m ()
insert_ record
val = do
        SqlBackend
conn <- forall (m :: * -> *) r. Monad m => ReaderT r m r
ask
        let vals :: [PersistValue]
vals = forall rec. PersistEntity rec => rec -> [PersistValue]
mkInsertValues record
val
        case SqlBackend -> EntityDef -> [PersistValue] -> InsertSqlResult
connInsertSql SqlBackend
conn (forall record (proxy :: * -> *).
PersistEntity record =>
proxy record -> EntityDef
entityDef (forall a. a -> Maybe a
Just record
val)) [PersistValue]
vals  of
            ISRSingle Text
sql -> do
                forall (m :: * -> *) a.
MonadIO m =>
Text
-> [PersistValue]
-> ConduitM [PersistValue] Void IO a
-> ReaderT SqlBackend m a
withRawQuery Text
sql [PersistValue]
vals forall a b. (a -> b) -> a -> b
$ do
                    forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
            ISRInsertGet Text
sql1 Text
_sql2 -> do
                forall (m :: * -> *) backend.
(MonadIO m, BackendCompatible SqlBackend backend) =>
Text -> [PersistValue] -> ReaderT backend m ()
rawExecute Text
sql1 [PersistValue]
vals
            ISRManyKeys Text
sql [PersistValue]
_fs -> do
                forall (m :: * -> *) backend.
(MonadIO m, BackendCompatible SqlBackend backend) =>
Text -> [PersistValue] -> ReaderT backend m ()
rawExecute Text
sql [PersistValue]
vals

    insert :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record SqlBackend,
 SafeToInsert record) =>
record -> ReaderT SqlBackend m (Key record)
insert record
val = do
        SqlBackend
conn <- forall (m :: * -> *) r. Monad m => ReaderT r m r
ask
        let esql :: InsertSqlResult
esql = SqlBackend -> EntityDef -> [PersistValue] -> InsertSqlResult
connInsertSql SqlBackend
conn EntityDef
t [PersistValue]
vals
        Key record
key <-
            case InsertSqlResult
esql of
                ISRSingle Text
sql -> forall (m :: * -> *) a.
MonadIO m =>
Text
-> [PersistValue]
-> ConduitM [PersistValue] Void IO a
-> ReaderT SqlBackend m a
withRawQuery Text
sql [PersistValue]
vals forall a b. (a -> b) -> a -> b
$ do
                    Maybe [PersistValue]
x <- forall (m :: * -> *) a o. Monad m => ConduitT a o m (Maybe a)
CL.head
                    case Maybe [PersistValue]
x of
                        Just [PersistInt64 Int64
i] -> case forall record.
PersistEntity record =>
[PersistValue] -> Either Text (Key record)
keyFromValues [Int64 -> PersistValue
PersistInt64 Int64
i] of
                            Left Text
err -> forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$ String
"SQL insert: keyFromValues: PersistInt64 " forall a. Monoid a => a -> a -> a
`mappend` forall a. Show a => a -> String
show Int64
i forall a. Monoid a => a -> a -> a
`mappend` String
" " forall a. Monoid a => a -> a -> a
`mappend` Text -> String
unpack Text
err
                            Right Key record
k -> forall (m :: * -> *) a. Monad m => a -> m a
return Key record
k
                        Maybe [PersistValue]
Nothing -> forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$ String
"SQL insert did not return a result giving the generated ID"
                        Just [PersistValue]
vals' -> case forall record.
PersistEntity record =>
[PersistValue] -> Either Text (Key record)
keyFromValues [PersistValue]
vals' of
                            Left Text
e -> forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$ String
"Invalid result from a SQL insert, got: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show [PersistValue]
vals' forall a. [a] -> [a] -> [a]
++ String
". Error was: " forall a. [a] -> [a] -> [a]
++ Text -> String
unpack Text
e
                            Right Key record
k -> forall (m :: * -> *) a. Monad m => a -> m a
return Key record
k

                ISRInsertGet Text
sql1 Text
sql2 -> do
                    forall (m :: * -> *) backend.
(MonadIO m, BackendCompatible SqlBackend backend) =>
Text -> [PersistValue] -> ReaderT backend m ()
rawExecute Text
sql1 [PersistValue]
vals
                    forall (m :: * -> *) a.
MonadIO m =>
Text
-> [PersistValue]
-> ConduitM [PersistValue] Void IO a
-> ReaderT SqlBackend m a
withRawQuery Text
sql2 [] forall a b. (a -> b) -> a -> b
$ do
                        Maybe [PersistValue]
mm <- forall (m :: * -> *) a o. Monad m => ConduitT a o m (Maybe a)
CL.head
                        let m :: Either Text [PersistValue]
m = forall b a. b -> (a -> b) -> Maybe a -> b
maybe
                                  (forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ Text
"No results from ISRInsertGet: " forall a. Monoid a => a -> a -> a
`mappend` forall a. Show a => a -> Text
tshow (Text
sql1, Text
sql2))
                                  forall a b. b -> Either a b
Right Maybe [PersistValue]
mm

                        -- TODO: figure out something better for MySQL
                        let convert :: [PersistValue] -> [PersistValue]
convert [PersistValue]
x =
                                case [PersistValue]
x of
                                    [PersistByteString ByteString
i] -> case ByteString -> Maybe (Integer, ByteString)
readInteger ByteString
i of -- mssql
                                                            Just (Integer
ret,ByteString
"") -> [Int64 -> PersistValue
PersistInt64 forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
ret]
                                                            Maybe (Integer, ByteString)
_ -> [PersistValue]
x
                                    [PersistValue]
_ -> [PersistValue]
x
                            -- Yes, it's just <|>. Older bases don't have the
                            -- instance for Either.
                            onLeft :: Either a b -> Either a b -> Either a b
onLeft Left{} Either a b
x = Either a b
x
                            onLeft Either a b
x Either a b
_ = Either a b
x

                        case Either Text [PersistValue]
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (\[PersistValue]
x -> forall record.
PersistEntity record =>
[PersistValue] -> Either Text (Key record)
keyFromValues [PersistValue]
x forall {a} {b}. Either a b -> Either a b -> Either a b
`onLeft` forall record.
PersistEntity record =>
[PersistValue] -> Either Text (Key record)
keyFromValues ([PersistValue] -> [PersistValue]
convert [PersistValue]
x)) of
                            Right Key record
k -> forall (m :: * -> *) a. Monad m => a -> m a
return Key record
k
                            Left Text
err -> forall {a}. Text -> ConduitT [PersistValue] Void IO a
throw forall a b. (a -> b) -> a -> b
$ Text
"ISRInsertGet: keyFromValues failed: " forall a. Monoid a => a -> a -> a
`mappend` Text
err
                ISRManyKeys Text
sql [PersistValue]
fs -> do
                    forall (m :: * -> *) backend.
(MonadIO m, BackendCompatible SqlBackend backend) =>
Text -> [PersistValue] -> ReaderT backend m ()
rawExecute Text
sql [PersistValue]
vals
                    case EntityDef -> Maybe CompositeDef
entityPrimary EntityDef
t of
                       Maybe CompositeDef
Nothing ->
                           forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$ String
"ISRManyKeys is used when Primary is defined " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Text
sql
                       Just CompositeDef
pdef ->
                            let pks :: [FieldNameHS]
pks = forall (t :: * -> *) a. Foldable t => t a -> [a]
Foldable.toList forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap FieldDef -> FieldNameHS
fieldHaskell forall a b. (a -> b) -> a -> b
$ CompositeDef -> NonEmpty FieldDef
compositeFields CompositeDef
pdef
                                keyvals :: [PersistValue]
keyvals = forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> b
snd forall a b. (a -> b) -> a -> b
$ forall a. (a -> Bool) -> [a] -> [a]
filter (\(FieldNameHS
a, PersistValue
_) -> let ret :: Bool
ret=forall a. Maybe a -> Bool
isJust (forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (forall a. Eq a => a -> a -> Bool
== FieldNameHS
a) [FieldNameHS]
pks) in Bool
ret) forall a b. (a -> b) -> a -> b
$ forall a b. [a] -> [b] -> [(a, b)]
zip (forall a b. (a -> b) -> [a] -> [b]
map FieldDef -> FieldNameHS
fieldHaskell forall a b. (a -> b) -> a -> b
$ EntityDef -> [FieldDef]
getEntityFields EntityDef
t) [PersistValue]
fs
                            in  case forall record.
PersistEntity record =>
[PersistValue] -> Either Text (Key record)
keyFromValues [PersistValue]
keyvals of
                                    Right Key record
k -> forall (m :: * -> *) a. Monad m => a -> m a
return Key record
k
                                    Left Text
e  -> forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$ String
"ISRManyKeys: unexpected keyvals result: " forall a. Monoid a => a -> a -> a
`mappend` Text -> String
unpack Text
e

        forall (m :: * -> *) a. Monad m => a -> m a
return Key record
key
      where
        tshow :: Show a => a -> Text
        tshow :: forall a. Show a => a -> Text
tshow = String -> Text
T.pack forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show
        throw :: Text -> ConduitT [PersistValue] Void IO a
throw = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a. Exception e => e -> IO a
throwIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> IOError
userError forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
T.unpack
        t :: EntityDef
t = forall record (proxy :: * -> *).
PersistEntity record =>
proxy record -> EntityDef
entityDef forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just record
val
        vals :: [PersistValue]
vals = forall rec. PersistEntity rec => rec -> [PersistValue]
mkInsertValues record
val

    insertMany :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record SqlBackend,
 SafeToInsert record) =>
[record] -> ReaderT SqlBackend m [Key record]
insertMany [] = forall (m :: * -> *) a. Monad m => a -> m a
return []
    insertMany [record]
vals = do
        SqlBackend
conn <- forall (m :: * -> *) r. Monad m => ReaderT r m r
ask

        case SqlBackend
-> Maybe (EntityDef -> [[PersistValue]] -> InsertSqlResult)
connInsertManySql SqlBackend
conn of
            Maybe (EntityDef -> [[PersistValue]] -> InsertSqlResult)
Nothing -> forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall backend record (m :: * -> *).
(PersistStoreWrite backend, MonadIO m,
 PersistRecordBackend record backend, SafeToInsert record) =>
record -> ReaderT backend m (Key record)
insert [record]
vals
            Just EntityDef -> [[PersistValue]] -> InsertSqlResult
insertManyFn ->
                case EntityDef -> [[PersistValue]] -> InsertSqlResult
insertManyFn EntityDef
ent [[PersistValue]]
valss of
                    ISRSingle Text
sql -> forall a (m :: * -> *) backend.
(RawSql a, MonadIO m, BackendCompatible SqlBackend backend) =>
Text -> [PersistValue] -> ReaderT backend m [a]
rawSql Text
sql (forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[PersistValue]]
valss)
                    InsertSqlResult
_ -> forall a. HasCallStack => String -> a
error String
"ISRSingle is expected from the connInsertManySql function"
                where
                    ent :: EntityDef
ent = forall record (proxy :: * -> *).
PersistEntity record =>
proxy record -> EntityDef
entityDef [record]
vals
                    valss :: [[PersistValue]]
valss = forall a b. (a -> b) -> [a] -> [b]
map forall rec. PersistEntity rec => rec -> [PersistValue]
mkInsertValues [record]
vals

    insertMany_ :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record SqlBackend,
 SafeToInsert record) =>
[record] -> ReaderT SqlBackend m ()
insertMany_ [record]
vals0 = forall (m :: * -> *) a.
Monad m =>
Int
-> ([a] -> ReaderT SqlBackend m ())
-> [a]
-> ReaderT SqlBackend m ()
runChunked (forall (t :: * -> *) a. Foldable t => t a -> Int
length forall a b. (a -> b) -> a -> b
$ EntityDef -> [FieldDef]
getEntityFields EntityDef
t) [record] -> ReaderT SqlBackend m ()
insertMany_' [record]
vals0
      where
        t :: EntityDef
t = forall record (proxy :: * -> *).
PersistEntity record =>
proxy record -> EntityDef
entityDef [record]
vals0
        insertMany_' :: [record] -> ReaderT SqlBackend m ()
insertMany_' [record]
vals = do
          SqlBackend
conn <- forall (m :: * -> *) r. Monad m => ReaderT r m r
ask
          let valss :: [[PersistValue]]
valss = forall a b. (a -> b) -> [a] -> [b]
map forall rec. PersistEntity rec => rec -> [PersistValue]
mkInsertValues [record]
vals
          let sql :: Text
sql = [Text] -> Text
T.concat
                  [ Text
"INSERT INTO "
                  , SqlBackend -> EntityDef -> Text
connEscapeTableName SqlBackend
conn EntityDef
t
                  , Text
"("
                  , Text -> [Text] -> Text
T.intercalate Text
"," forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (SqlBackend -> FieldNameDB -> Text
connEscapeFieldName SqlBackend
conn forall b c a. (b -> c) -> (a -> b) -> a -> c
. FieldDef -> FieldNameDB
fieldDB) forall a b. (a -> b) -> a -> b
$ EntityDef -> [FieldDef]
getEntityFields EntityDef
t
                  , Text
") VALUES ("
                  , Text -> [Text] -> Text
T.intercalate Text
"),(" forall a b. (a -> b) -> a -> b
$ forall a. Int -> a -> [a]
replicate (forall (t :: * -> *) a. Foldable t => t a -> Int
length [[PersistValue]]
valss) forall a b. (a -> b) -> a -> b
$ Text -> [Text] -> Text
T.intercalate Text
"," forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (forall a b. a -> b -> a
const Text
"?") (EntityDef -> [FieldDef]
getEntityFields EntityDef
t)
                  , Text
")"
                  ]
          forall (m :: * -> *) backend.
(MonadIO m, BackendCompatible SqlBackend backend) =>
Text -> [PersistValue] -> ReaderT backend m ()
rawExecute Text
sql (forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[PersistValue]]
valss)

    replace :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record SqlBackend) =>
Key record -> record -> ReaderT SqlBackend m ()
replace Key record
k record
val = do
        SqlBackend
conn <- forall (m :: * -> *) r. Monad m => ReaderT r m r
ask
        let t :: EntityDef
t = forall record (proxy :: * -> *).
PersistEntity record =>
proxy record -> EntityDef
entityDef forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just record
val
        let wher :: Text
wher = forall record.
PersistEntity record =>
SqlBackend -> Key record -> Text
whereStmtForKey SqlBackend
conn Key record
k
        let sql :: Text
sql = [Text] -> Text
T.concat
                [ Text
"UPDATE "
                , SqlBackend -> EntityDef -> Text
connEscapeTableName SqlBackend
conn EntityDef
t
                , Text
" SET "
                , Text -> [Text] -> Text
T.intercalate Text
"," (forall a b. (a -> b) -> [a] -> [b]
map (SqlBackend -> FieldNameDB -> Text
go SqlBackend
conn forall b c a. (b -> c) -> (a -> b) -> a -> c
. FieldDef -> FieldNameDB
fieldDB) forall a b. (a -> b) -> a -> b
$ EntityDef -> [FieldDef]
getEntityFields EntityDef
t)
                , Text
" WHERE "
                , Text
wher
                ]
            vals :: [PersistValue]
vals = forall rec. PersistEntity rec => rec -> [PersistValue]
mkInsertValues record
val forall a. Monoid a => a -> a -> a
`mappend` forall record. PersistEntity record => Key record -> [PersistValue]
keyToValues Key record
k
        forall (m :: * -> *) backend.
(MonadIO m, BackendCompatible SqlBackend backend) =>
Text -> [PersistValue] -> ReaderT backend m ()
rawExecute Text
sql [PersistValue]
vals
      where
        go :: SqlBackend -> FieldNameDB -> Text
go SqlBackend
conn FieldNameDB
x = SqlBackend -> FieldNameDB -> Text
connEscapeFieldName SqlBackend
conn FieldNameDB
x Text -> Text -> Text
`T.append` Text
"=?"

    insertKey :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record SqlBackend) =>
Key record -> record -> ReaderT SqlBackend m ()
insertKey Key record
k record
v = forall (m :: * -> *) val.
(MonadIO m, PersistEntity val) =>
Text -> [Entity val] -> ReaderT SqlBackend m ()
insrepHelper Text
"INSERT" [forall record. Key record -> record -> Entity record
Entity Key record
k record
v]

    insertEntityMany :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record SqlBackend) =>
[Entity record] -> ReaderT SqlBackend m ()
insertEntityMany [Entity record]
es' = do
        SqlBackend
conn <- forall (m :: * -> *) r. Monad m => ReaderT r m r
ask
        let entDef :: EntityDef
entDef = forall record (proxy :: * -> *).
PersistEntity record =>
proxy record -> EntityDef
entityDef forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall record. Entity record -> record
entityVal [Entity record]
es'
        let columnNames :: NonEmpty Text
columnNames = EntityDef -> SqlBackend -> NonEmpty Text
keyAndEntityColumnNames EntityDef
entDef SqlBackend
conn
        forall (m :: * -> *) a.
Monad m =>
Int
-> ([a] -> ReaderT SqlBackend m ())
-> [a]
-> ReaderT SqlBackend m ()
runChunked (forall (t :: * -> *) a. Foldable t => t a -> Int
length NonEmpty Text
columnNames) [Entity record] -> ReaderT SqlBackend m ()
go [Entity record]
es'
      where
        go :: [Entity record] -> ReaderT SqlBackend m ()
go = forall (m :: * -> *) val.
(MonadIO m, PersistEntity val) =>
Text -> [Entity val] -> ReaderT SqlBackend m ()
insrepHelper Text
"INSERT"

    repsert :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record SqlBackend) =>
Key record -> record -> ReaderT SqlBackend m ()
repsert Key record
key record
value = forall backend record (m :: * -> *).
(PersistStoreWrite backend, MonadIO m,
 PersistRecordBackend record backend) =>
[(Key record, record)] -> ReaderT backend m ()
repsertMany [(Key record
key, record
value)]

    repsertMany :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record SqlBackend) =>
[(Key record, record)] -> ReaderT SqlBackend m ()
repsertMany [] = forall (m :: * -> *) a. Monad m => a -> m a
return ()
    repsertMany [(Key record, record)]
krsDups = do
        SqlBackend
conn <- forall (m :: * -> *) r. Monad m => ReaderT r m r
ask
        let krs :: [(Key record, record)]
krs = forall a. (a -> a -> Bool) -> [a] -> [a]
nubBy (forall a. Eq a => a -> a -> Bool
(==) forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` forall a b. (a, b) -> a
fst) (forall a. [a] -> [a]
reverse [(Key record, record)]
krsDups)
        let rs :: [record]
rs = forall a b. (a, b) -> b
snd forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` [(Key record, record)]
krs
        let ent :: EntityDef
ent = forall record (proxy :: * -> *).
PersistEntity record =>
proxy record -> EntityDef
entityDef [record]
rs
        let nr :: Int
nr  = forall (t :: * -> *) a. Foldable t => t a -> Int
length [(Key record, record)]
krs
        let toVals :: (Key record, record) -> [PersistValue]
toVals (Key record
k,record
r)
                = case EntityDef -> Maybe CompositeDef
entityPrimary EntityDef
ent of
                    Maybe CompositeDef
Nothing -> forall record. PersistEntity record => Key record -> [PersistValue]
keyToValues Key record
k forall a. Semigroup a => a -> a -> a
<> (forall rec. PersistEntity rec => rec -> [PersistValue]
mkInsertValues record
r)
                    Just CompositeDef
_  -> forall rec. PersistEntity rec => rec -> [PersistValue]
mkInsertValues record
r
        case SqlBackend -> Maybe (EntityDef -> Int -> Text)
connRepsertManySql SqlBackend
conn of
            (Just EntityDef -> Int -> Text
mkSql) -> forall (m :: * -> *) backend.
(MonadIO m, BackendCompatible SqlBackend backend) =>
Text -> [PersistValue] -> ReaderT backend m ()
rawExecute (EntityDef -> Int -> Text
mkSql EntityDef
ent Int
nr) (forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Key record, record) -> [PersistValue]
toVals [(Key record, record)]
krs)
            Maybe (EntityDef -> Int -> Text)
Nothing -> forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ forall {record} {backend} {m :: * -> *}.
(PersistEntityBackend record ~ BaseBackend backend, MonadIO m,
 PersistEntity record, PersistStoreWrite backend) =>
(Key record, record) -> ReaderT backend m ()
repsert' [(Key record, record)]
krs
              where
                repsert' :: (Key record, record) -> ReaderT backend m ()
repsert' (Key record
key, record
value) = do
                  Maybe record
mExisting <- forall backend record (m :: * -> *).
(PersistStoreRead backend, MonadIO m,
 PersistRecordBackend record backend) =>
Key record -> ReaderT backend m (Maybe record)
get Key record
key
                  case Maybe record
mExisting of
                    Maybe record
Nothing -> forall backend record (m :: * -> *).
(PersistStoreWrite backend, MonadIO m,
 PersistRecordBackend record backend) =>
Key record -> record -> ReaderT backend m ()
insertKey Key record
key record
value
                    Just record
_ -> forall backend record (m :: * -> *).
(PersistStoreWrite backend, MonadIO m,
 PersistRecordBackend record backend) =>
Key record -> record -> ReaderT backend m ()
replace Key record
key record
value

    delete :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record SqlBackend) =>
Key record -> ReaderT SqlBackend m ()
delete Key record
k = do
        SqlBackend
conn <- forall (m :: * -> *) r. Monad m => ReaderT r m r
ask
        forall (m :: * -> *) backend.
(MonadIO m, BackendCompatible SqlBackend backend) =>
Text -> [PersistValue] -> ReaderT backend m ()
rawExecute (SqlBackend -> Text
sql SqlBackend
conn) (forall record. PersistEntity record => Key record -> [PersistValue]
keyToValues Key record
k)
      where
        wher :: SqlBackend -> Text
wher SqlBackend
conn = forall record.
PersistEntity record =>
SqlBackend -> Key record -> Text
whereStmtForKey SqlBackend
conn Key record
k
        sql :: SqlBackend -> Text
sql SqlBackend
conn = [Text] -> Text
T.concat
            [ Text
"DELETE FROM "
            , SqlBackend -> EntityDef -> Text
connEscapeTableName SqlBackend
conn (forall record (proxy :: * -> *).
PersistEntity record =>
proxy record -> EntityDef
entityDef forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall record. Key record -> record
recordTypeFromKey Key record
k)
            , Text
" WHERE "
            , SqlBackend -> Text
wher SqlBackend
conn
            ]
instance PersistStoreWrite SqlWriteBackend where
    insert :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record SqlWriteBackend,
 SafeToInsert record) =>
record -> ReaderT SqlWriteBackend m (Key record)
insert record
v = forall backend (m :: * -> *) a.
HasPersistBackend backend =>
ReaderT (BaseBackend backend) m a -> ReaderT backend m a
withBaseBackend forall a b. (a -> b) -> a -> b
$ forall backend record (m :: * -> *).
(PersistStoreWrite backend, MonadIO m,
 PersistRecordBackend record backend, SafeToInsert record) =>
record -> ReaderT backend m (Key record)
insert record
v
    insertMany :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record SqlWriteBackend,
 SafeToInsert record) =>
[record] -> ReaderT SqlWriteBackend m [Key record]
insertMany [record]
vs = forall backend (m :: * -> *) a.
HasPersistBackend backend =>
ReaderT (BaseBackend backend) m a -> ReaderT backend m a
withBaseBackend forall a b. (a -> b) -> a -> b
$ forall backend record (m :: * -> *).
(PersistStoreWrite backend, MonadIO m,
 PersistRecordBackend record backend, SafeToInsert record) =>
[record] -> ReaderT backend m [Key record]
insertMany [record]
vs
    insertMany_ :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record SqlWriteBackend,
 SafeToInsert record) =>
[record] -> ReaderT SqlWriteBackend m ()
insertMany_ [record]
vs = forall backend (m :: * -> *) a.
HasPersistBackend backend =>
ReaderT (BaseBackend backend) m a -> ReaderT backend m a
withBaseBackend forall a b. (a -> b) -> a -> b
$ forall backend record (m :: * -> *).
(PersistStoreWrite backend, MonadIO m,
 PersistRecordBackend record backend, SafeToInsert record) =>
[record] -> ReaderT backend m ()
insertMany_ [record]
vs
    insertEntityMany :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record SqlWriteBackend) =>
[Entity record] -> ReaderT SqlWriteBackend m ()
insertEntityMany [Entity record]
vs = forall backend (m :: * -> *) a.
HasPersistBackend backend =>
ReaderT (BaseBackend backend) m a -> ReaderT backend m a
withBaseBackend forall a b. (a -> b) -> a -> b
$ forall backend record (m :: * -> *).
(PersistStoreWrite backend, MonadIO m,
 PersistRecordBackend record backend) =>
[Entity record] -> ReaderT backend m ()
insertEntityMany [Entity record]
vs
    insertKey :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record SqlWriteBackend) =>
Key record -> record -> ReaderT SqlWriteBackend m ()
insertKey Key record
k record
v = forall backend (m :: * -> *) a.
HasPersistBackend backend =>
ReaderT (BaseBackend backend) m a -> ReaderT backend m a
withBaseBackend forall a b. (a -> b) -> a -> b
$ forall backend record (m :: * -> *).
(PersistStoreWrite backend, MonadIO m,
 PersistRecordBackend record backend) =>
Key record -> record -> ReaderT backend m ()
insertKey Key record
k record
v
    repsert :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record SqlWriteBackend) =>
Key record -> record -> ReaderT SqlWriteBackend m ()
repsert Key record
k record
v = forall backend (m :: * -> *) a.
HasPersistBackend backend =>
ReaderT (BaseBackend backend) m a -> ReaderT backend m a
withBaseBackend forall a b. (a -> b) -> a -> b
$ forall backend record (m :: * -> *).
(PersistStoreWrite backend, MonadIO m,
 PersistRecordBackend record backend) =>
Key record -> record -> ReaderT backend m ()
repsert Key record
k record
v
    replace :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record SqlWriteBackend) =>
Key record -> record -> ReaderT SqlWriteBackend m ()
replace Key record
k record
v = forall backend (m :: * -> *) a.
HasPersistBackend backend =>
ReaderT (BaseBackend backend) m a -> ReaderT backend m a
withBaseBackend forall a b. (a -> b) -> a -> b
$ forall backend record (m :: * -> *).
(PersistStoreWrite backend, MonadIO m,
 PersistRecordBackend record backend) =>
Key record -> record -> ReaderT backend m ()
replace Key record
k record
v
    delete :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record SqlWriteBackend) =>
Key record -> ReaderT SqlWriteBackend m ()
delete Key record
k = forall backend (m :: * -> *) a.
HasPersistBackend backend =>
ReaderT (BaseBackend backend) m a -> ReaderT backend m a
withBaseBackend forall a b. (a -> b) -> a -> b
$ forall backend record (m :: * -> *).
(PersistStoreWrite backend, MonadIO m,
 PersistRecordBackend record backend) =>
Key record -> ReaderT backend m ()
delete Key record
k
    update :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record SqlWriteBackend) =>
Key record -> [Update record] -> ReaderT SqlWriteBackend m ()
update Key record
k [Update record]
upds = forall backend (m :: * -> *) a.
HasPersistBackend backend =>
ReaderT (BaseBackend backend) m a -> ReaderT backend m a
withBaseBackend forall a b. (a -> b) -> a -> b
$ forall backend record (m :: * -> *).
(PersistStoreWrite backend, MonadIO m,
 PersistRecordBackend record backend) =>
Key record -> [Update record] -> ReaderT backend m ()
update Key record
k [Update record]
upds
    repsertMany :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record SqlWriteBackend) =>
[(Key record, record)] -> ReaderT SqlWriteBackend m ()
repsertMany [(Key record, record)]
krs = forall backend (m :: * -> *) a.
HasPersistBackend backend =>
ReaderT (BaseBackend backend) m a -> ReaderT backend m a
withBaseBackend forall a b. (a -> b) -> a -> b
$ forall backend record (m :: * -> *).
(PersistStoreWrite backend, MonadIO m,
 PersistRecordBackend record backend) =>
[(Key record, record)] -> ReaderT backend m ()
repsertMany [(Key record, record)]
krs

instance PersistStoreRead SqlBackend where
    get :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record SqlBackend) =>
Key record -> ReaderT SqlBackend m (Maybe record)
get Key record
k = do
        Map (Key record) record
mEs <- forall backend record (m :: * -> *).
(PersistStoreRead backend, MonadIO m,
 PersistRecordBackend record backend) =>
[Key record] -> ReaderT backend m (Map (Key record) record)
getMany [Key record
k]
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Key record
k Map (Key record) record
mEs

    -- inspired by Database.Persist.Sql.Orphan.PersistQuery.selectSourceRes
    getMany :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record SqlBackend) =>
[Key record] -> ReaderT SqlBackend m (Map (Key record) record)
getMany []      = forall (m :: * -> *) a. Monad m => a -> m a
return forall k a. Map k a
Map.empty
    getMany ks :: [Key record]
ks@(Key record
k:[Key record]
_)= do
        SqlBackend
conn <- forall (m :: * -> *) r. Monad m => ReaderT r m r
ask
        let t :: EntityDef
t = forall record (proxy :: * -> *).
PersistEntity record =>
proxy record -> EntityDef
entityDef forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall record. Key record -> Maybe record
dummyFromKey forall a b. (a -> b) -> a -> b
$ Key record
k
        let cols :: SqlBackend -> Text
cols = [Text] -> Text
commaSeparated forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> [a]
Foldable.toList forall b c a. (b -> c) -> (a -> b) -> a -> c
. EntityDef -> SqlBackend -> NonEmpty Text
keyAndEntityColumnNames EntityDef
t
        let wher :: Text
wher = forall record.
PersistEntity record =>
SqlBackend -> [Key record] -> Text
whereStmtForKeys SqlBackend
conn [Key record]
ks
        let sql :: Text
sql = [Text] -> Text
T.concat
                [ Text
"SELECT "
                , SqlBackend -> Text
cols SqlBackend
conn
                , Text
" FROM "
                , SqlBackend -> EntityDef -> Text
connEscapeTableName SqlBackend
conn EntityDef
t
                , Text
" WHERE "
                , Text
wher
                ]
        let parse :: [PersistValue] -> IO (Entity record)
parse [PersistValue]
vals
                = case forall record.
PersistEntity record =>
EntityDef -> [PersistValue] -> Either Text (Entity record)
parseEntityValues EntityDef
t [PersistValue]
vals of
                    Left Text
s -> forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall e a. Exception e => e -> IO a
throwIO forall a b. (a -> b) -> a -> b
$
                        Text -> PersistException
PersistMarshalError (Text
"getBy: " forall a. Semigroup a => a -> a -> a
<> Text
s)
                    Right Entity record
row -> forall (m :: * -> *) a. Monad m => a -> m a
return Entity record
row
        forall (m :: * -> *) a.
MonadIO m =>
Text
-> [PersistValue]
-> ConduitM [PersistValue] Void IO a
-> ReaderT SqlBackend m a
withRawQuery Text
sql (forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
Foldable.foldMap forall record. PersistEntity record => Key record -> [PersistValue]
keyToValues [Key record]
ks) forall a b. (a -> b) -> a -> b
$ do
            [Entity record]
es <- forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> ConduitT a b m ()
CL.mapM [PersistValue] -> IO (Entity record)
parse forall (m :: * -> *) a b c r.
Monad m =>
ConduitT a b m () -> ConduitT b c m r -> ConduitT a c m r
.| forall (m :: * -> *) a o. Monad m => ConduitT a o m [a]
CL.consume
            forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Entity record
e -> (forall record. Entity record -> Key record
entityKey Entity record
e, forall record. Entity record -> record
entityVal Entity record
e)) [Entity record]
es

instance PersistStoreRead SqlReadBackend where
    get :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record SqlReadBackend) =>
Key record -> ReaderT SqlReadBackend m (Maybe record)
get Key record
k = forall backend (m :: * -> *) a.
HasPersistBackend backend =>
ReaderT (BaseBackend backend) m a -> ReaderT backend m a
withBaseBackend forall a b. (a -> b) -> a -> b
$ forall backend record (m :: * -> *).
(PersistStoreRead backend, MonadIO m,
 PersistRecordBackend record backend) =>
Key record -> ReaderT backend m (Maybe record)
get Key record
k
    getMany :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record SqlReadBackend) =>
[Key record] -> ReaderT SqlReadBackend m (Map (Key record) record)
getMany [Key record]
ks = forall backend (m :: * -> *) a.
HasPersistBackend backend =>
ReaderT (BaseBackend backend) m a -> ReaderT backend m a
withBaseBackend forall a b. (a -> b) -> a -> b
$ forall backend record (m :: * -> *).
(PersistStoreRead backend, MonadIO m,
 PersistRecordBackend record backend) =>
[Key record] -> ReaderT backend m (Map (Key record) record)
getMany [Key record]
ks
instance PersistStoreRead SqlWriteBackend where
    get :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record SqlWriteBackend) =>
Key record -> ReaderT SqlWriteBackend m (Maybe record)
get Key record
k = forall backend (m :: * -> *) a.
HasPersistBackend backend =>
ReaderT (BaseBackend backend) m a -> ReaderT backend m a
withBaseBackend forall a b. (a -> b) -> a -> b
$ forall backend record (m :: * -> *).
(PersistStoreRead backend, MonadIO m,
 PersistRecordBackend record backend) =>
Key record -> ReaderT backend m (Maybe record)
get Key record
k
    getMany :: forall record (m :: * -> *).
(MonadIO m, PersistRecordBackend record SqlWriteBackend) =>
[Key record] -> ReaderT SqlWriteBackend m (Map (Key record) record)
getMany [Key record]
ks = forall backend (m :: * -> *) a.
HasPersistBackend backend =>
ReaderT (BaseBackend backend) m a -> ReaderT backend m a
withBaseBackend forall a b. (a -> b) -> a -> b
$ forall backend record (m :: * -> *).
(PersistStoreRead backend, MonadIO m,
 PersistRecordBackend record backend) =>
[Key record] -> ReaderT backend m (Map (Key record) record)
getMany [Key record]
ks

dummyFromKey :: Key record -> Maybe record
dummyFromKey :: forall record. Key record -> Maybe record
dummyFromKey = forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall record. Key record -> record
recordTypeFromKey

recordTypeFromKey :: Key record -> record
recordTypeFromKey :: forall record. Key record -> record
recordTypeFromKey Key record
_ = forall a. HasCallStack => String -> a
error String
"dummyFromKey"

insrepHelper :: (MonadIO m, PersistEntity val)
             => Text
             -> [Entity val]
             -> ReaderT SqlBackend m ()
insrepHelper :: forall (m :: * -> *) val.
(MonadIO m, PersistEntity val) =>
Text -> [Entity val] -> ReaderT SqlBackend m ()
insrepHelper Text
_       []  = forall (m :: * -> *) a. Monad m => a -> m a
return ()
insrepHelper Text
command [Entity val]
es = do
    SqlBackend
conn <- forall (m :: * -> *) r. Monad m => ReaderT r m r
ask
    let columnNames :: [Text]
columnNames = forall (t :: * -> *) a. Foldable t => t a -> [a]
Foldable.toList forall a b. (a -> b) -> a -> b
$ EntityDef -> SqlBackend -> NonEmpty Text
keyAndEntityColumnNames EntityDef
entDef SqlBackend
conn
    forall (m :: * -> *) backend.
(MonadIO m, BackendCompatible SqlBackend backend) =>
Text -> [PersistValue] -> ReaderT backend m ()
rawExecute (SqlBackend -> [Text] -> Text
sql SqlBackend
conn [Text]
columnNames) [PersistValue]
vals
  where
    entDef :: EntityDef
entDef = forall record (proxy :: * -> *).
PersistEntity record =>
proxy record -> EntityDef
entityDef forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall record. Entity record -> record
entityVal [Entity val]
es
    sql :: SqlBackend -> [Text] -> Text
sql SqlBackend
conn [Text]
columnNames = [Text] -> Text
T.concat
        [ Text
command
        , Text
" INTO "
        , SqlBackend -> EntityDef -> Text
connEscapeTableName SqlBackend
conn EntityDef
entDef
        , Text
"("
        , Text -> [Text] -> Text
T.intercalate Text
"," [Text]
columnNames
        , Text
") VALUES ("
        , Text -> [Text] -> Text
T.intercalate Text
"),(" forall a b. (a -> b) -> a -> b
$ forall a. Int -> a -> [a]
replicate (forall (t :: * -> *) a. Foldable t => t a -> Int
length [Entity val]
es) forall a b. (a -> b) -> a -> b
$ Text -> [Text] -> Text
T.intercalate Text
"," forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a b. a -> b -> a
const Text
"?") [Text]
columnNames
        , Text
")"
        ]
    vals :: [PersistValue]
vals = forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
Foldable.foldMap forall record.
PersistEntity record =>
Entity record -> [PersistValue]
entityValues [Entity val]
es

runChunked
    :: (Monad m)
    => Int
    -> ([a] -> ReaderT SqlBackend m ())
    -> [a]
    -> ReaderT SqlBackend m ()
runChunked :: forall (m :: * -> *) a.
Monad m =>
Int
-> ([a] -> ReaderT SqlBackend m ())
-> [a]
-> ReaderT SqlBackend m ()
runChunked Int
_ [a] -> ReaderT SqlBackend m ()
_ []     = forall (m :: * -> *) a. Monad m => a -> m a
return ()
runChunked Int
width [a] -> ReaderT SqlBackend m ()
m [a]
xs = do
    SqlBackend
conn <- forall (m :: * -> *) r. Monad m => ReaderT r m r
ask
    case SqlBackend -> Maybe Int
connMaxParams SqlBackend
conn of
        Maybe Int
Nothing -> [a] -> ReaderT SqlBackend m ()
m [a]
xs
        Just Int
maxParams -> let chunkSize :: Int
chunkSize = Int
maxParams forall a. Integral a => a -> a -> a
`div` Int
width in
            forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ [a] -> ReaderT SqlBackend m ()
m (forall a. Int -> [a] -> [[a]]
chunksOf Int
chunkSize [a]
xs)

-- Implement this here to avoid depending on the split package
chunksOf :: Int -> [a] -> [[a]]
chunksOf :: forall a. Int -> [a] -> [[a]]
chunksOf Int
_ [] = []
chunksOf Int
size [a]
xs = let ([a]
chunk, [a]
rest) = forall a. Int -> [a] -> ([a], [a])
splitAt Int
size [a]
xs in [a]
chunk forall a. a -> [a] -> [a]
: forall a. Int -> [a] -> [[a]]
chunksOf Int
size [a]
rest