persistent 1.1.4 → 1.1.5
raw patch · 6 files changed
+97/−39 lines, 6 filesdep ~basedep ~conduit
Dependency ranges changed: base, conduit
Files
- Database/Persist/GenericSql/Internal.hs +2/−1
- Database/Persist/GenericSql/Raw.hs +18/−2
- Database/Persist/Query/GenericSql.hs +59/−35
- Database/Persist/Query/Internal.hs +7/−0
- Database/Persist/Store.hs +10/−0
- persistent.cabal +1/−1
Database/Persist/GenericSql/Internal.hs view
@@ -35,6 +35,7 @@ import Language.Haskell.TH.Syntax (Q, Exp) import Control.Monad.Logger (logDebugS) import Data.Maybe (mapMaybe, listToMaybe)+import Data.Int (Int64) data InsertSqlResult = ISRSingle Text | ISRInsertGet Text Text@@ -59,7 +60,7 @@ data Statement = Statement { finalize :: IO () , reset :: IO ()- , execute :: [PersistValue] -> IO ()+ , execute :: [PersistValue] -> IO Int64 , withStmt :: forall m. C.MonadResource m => [PersistValue] -> C.Source m [PersistValue]
Database/Persist/GenericSql/Raw.hs view
@@ -12,6 +12,7 @@ module Database.Persist.GenericSql.Raw ( withStmt , execute+ , executeCount , SqlPersist (..) , getStmt' , getStmt@@ -39,9 +40,13 @@ import Control.Monad (MonadPlus) import Control.Monad.Trans.Resource (MonadResource (..)) import Data.Conduit+#if MIN_VERSION_conduit(1, 0, 0)+import Data.Conduit.Internal (Pipe, ConduitM)+#endif import Control.Monad.Logger (MonadLogger (..)) import Data.Monoid (Monoid) import Data.Typeable (Typeable)+import Data.Int (Int64) import Control.Monad.Logger (LoggingT) import Control.Monad.Trans.Identity ( IdentityT)@@ -105,6 +110,9 @@ GO(StateT s) GO(ResourceT) GO(Pipe l i o u)+#if MIN_VERSION_conduit(1, 0, 0)+GO(ConduitM i o)+#endif GOX(Monoid w, WriterT w) GOX(Monoid w, RWST r w s) GOX(Monoid w, Strict.RWST r w s)@@ -114,8 +122,12 @@ #undef GOX instance MonadLogger m => MonadLogger (SqlPersist m) where+#if MIN_VERSION_monad_logger(0, 3, 0)+ monadLoggerLog a b c = lift . monadLoggerLog a b c+#else monadLoggerLog a b c = lift $ monadLoggerLog a b c monadLoggerLogSource a b c = lift . monadLoggerLogSource a b c+#endif withStmt :: (MonadSqlPersist m, MonadResource m) => Text@@ -130,11 +142,15 @@ (flip I.withStmt vals) execute :: MonadSqlPersist m => Text -> [PersistValue] -> m ()-execute sql vals = do+execute x y = liftM (const ()) $ executeCount x y++executeCount :: MonadSqlPersist m => Text -> [PersistValue] -> m Int64+executeCount sql vals = do $logDebugS (pack "SQL") $ pack $ show sql ++ " " ++ show vals stmt <- getStmt sql- liftIO $ I.execute stmt vals+ res <- liftIO $ I.execute stmt vals liftIO $ reset stmt+ return res getStmt :: MonadSqlPersist m => Text -> m Statement getStmt sql = do
Database/Persist/Query/GenericSql.hs view
@@ -17,6 +17,8 @@ , selectSourceConn , dummyFromFilts , orderClause+ , deleteWhereCount+ , updateWhereCount ) where @@ -35,6 +37,7 @@ import Data.Conduit import qualified Data.Conduit.List as CL+import Data.Int (Int64) import Control.Exception (throwIO) import qualified Data.Text as T@@ -165,43 +168,64 @@ else " OFFSET " ++ show offset deleteWhere filts = do- conn <- SqlPersist ask- let t = entityDef $ dummyFromFilts filts- let wher = if null filts- then ""- else filterClause False conn filts- sql = concat- [ "DELETE FROM "- , escapeName conn $ entityDB t- , wher- ]- execute' sql $ getFiltsValues conn filts+ _ <- deleteWhereCount filts+ return () - updateWhere _ [] = return () updateWhere filts upds = do- conn <- SqlPersist ask- let wher = if null filts- then ""- else filterClause False conn filts- let sql = concat- [ "UPDATE "- , escapeName conn $ entityDB t- , " SET "- , T.intercalate "," $ map (go' conn . go) upds- , wher- ]- let dat = map updatePersistValue upds `mappend`- getFiltsValues conn filts- execute' sql dat- where- t = entityDef $ dummyFromFilts filts- go'' n Assign = n ++ "=?"- go'' n Add = concat [n, "=", n, "+?"]- go'' n Subtract = concat [n, "=", n, "-?"]- go'' n Multiply = concat [n, "=", n, "*?"]- go'' n Divide = concat [n, "=", n, "/?"]- go' conn (x, pu) = go'' (escapeName conn x) pu- go x = (fieldDB $ updateFieldDef x, updateUpdate x)+ _ <- updateWhereCount filts upds+ return ()++-- | Same as 'deleteWhere', but returns the number of rows affected.+--+-- Since 1.1.5+deleteWhereCount :: (PersistEntity val, MonadIO m, MonadLogger m)+ => [Filter val]+ -> SqlPersist m Int64+deleteWhereCount filts = do+ conn <- SqlPersist ask+ let t = entityDef $ dummyFromFilts filts+ let wher = if null filts+ then ""+ else filterClause False conn filts+ sql = concat+ [ "DELETE FROM "+ , escapeName conn $ entityDB t+ , wher+ ]+ R.executeCount sql $ getFiltsValues conn filts++-- | Same as 'updateWhere', but returns the number of rows affected.+--+-- Since 1.1.5+updateWhereCount :: (PersistEntity val, MonadIO m, MonadLogger m)+ => [Filter val]+ -> [Update val]+ -> SqlPersist m Int64+updateWhereCount _ [] = return 0+updateWhereCount filts upds = do+ conn <- SqlPersist ask+ let wher = if null filts+ then ""+ else filterClause False conn filts+ let sql = concat+ [ "UPDATE "+ , escapeName conn $ entityDB t+ , " SET "+ , T.intercalate "," $ map (go' conn . go) upds+ , wher+ ]+ let dat = map updatePersistValue upds `mappend`+ getFiltsValues conn filts+ R.executeCount sql dat+ where+ t = entityDef $ dummyFromFilts filts+ go'' n Assign = n ++ "=?"+ go'' n Add = concat [n, "=", n, "+?"]+ go'' n Subtract = concat [n, "=", n, "-?"]+ go'' n Multiply = concat [n, "=", n, "*?"]+ go'' n Divide = concat [n, "=", n, "/?"]+ go' conn (x, pu) = go'' (escapeName conn x) pu+ go x = (fieldDB $ updateFieldDef x, updateUpdate x) updatePersistValue :: Update v -> PersistValue updatePersistValue (Update _ v _) = toPersistValue v
Database/Persist/Query/Internal.hs view
@@ -37,7 +37,11 @@ import Control.Monad.Trans.Class (lift) import Data.Monoid (Monoid) +#if MIN_VERSION_conduit(1, 0, 0)+import Data.Conduit.Internal (Pipe, ConduitM)+#else import Data.Conduit (Pipe)+#endif import Control.Monad.Logger (LoggingT) import Control.Monad.Trans.Identity ( IdentityT) import Control.Monad.Trans.List ( ListT )@@ -124,6 +128,9 @@ GO(StateT s) GO(ResourceT) GO(Pipe l i o u)+#if MIN_VERSION_conduit(1, 0, 0)+GO(ConduitM i o)+#endif GOX(Monoid w, WriterT w) GOX(Monoid w, RWST r w s) GOX(Monoid w, Strict.RWST r w s)
Database/Persist/Store.hs view
@@ -108,7 +108,11 @@ import Control.Monad.IO.Class (MonadIO, liftIO) import Data.Monoid (Monoid) +#if MIN_VERSION_conduit(1, 0, 0)+import Data.Conduit.Internal (Pipe, ConduitM)+#else import Data.Conduit (Pipe)+#endif import Control.Monad.Logger (LoggingT) import Control.Monad.Trans.Identity ( IdentityT) import Control.Monad.Trans.List ( ListT )@@ -458,6 +462,9 @@ persistIdField :: EntityField val (Key val) + fieldLens :: EntityField val field+ -> (forall f. Functor f => (field -> f field) -> Entity val -> f (Entity val))+ instance PersistField a => PersistField [a] where toPersistValue = PersistList . map toPersistValue fromPersistValue (PersistList l) = fromPersistList l@@ -621,6 +628,9 @@ GO(StateT s) GO(ResourceT) GO(Pipe l i o u)+#if MIN_VERSION_conduit(1, 0, 0)+GO(ConduitM i o)+#endif GOX(Monoid w, WriterT w) GOX(Monoid w, RWST r w s) GOX(Monoid w, Strict.RWST r w s)
persistent.cabal view
@@ -1,5 +1,5 @@ name: persistent-version: 1.1.4+version: 1.1.5 license: MIT license-file: LICENSE author: Michael Snoyman <michael@snoyman.com>