persistent-mysql-haskell 0.4.2 → 0.5.0
raw patch · 3 files changed
+52/−27 lines, 3 filesdep ~persistent
Dependency ranges changed: persistent
Files
- ChangeLog.md +8/−0
- Database/Persist/MySQL.hs +42/−25
- persistent-mysql-haskell.cabal +2/−2
ChangeLog.md view
@@ -1,4 +1,12 @@+# ChangeLog for `persistent-mysql-haskell`++## 0.5.0++- Port [#812](https://github.com/yesodweb/persistent/pull/812) from `persistent-mysql`: Add support for SQL isolation levels+- Port [#833](https://github.com/yesodweb/persistent/pull/833) from `persistent-mysql`: `repsertMany` now matches `mapM_ (uncurry repsert)` and is atomic.+ ## 0.4.2+ - [#7](https://github.com/naushadh/persistent/pull/7) Fix boolean conversion bug. ## 0.4.1
Database/Persist/MySQL.hs view
@@ -76,8 +76,8 @@ import qualified Data.Text.Encoding as T import Database.Persist.Sql-import Database.Persist.Sql.Types.Internal (mkPersistBackend)-import Database.Persist.Sql.Util (commaSeparated, mkUpdateText', parenWrapped)+import Database.Persist.Sql.Types.Internal (mkPersistBackend, makeIsolationLevelStatement)+import qualified Database.Persist.Sql.Util as Util import Database.Persist.MySQLConnectInfoShowInstance () import Data.Int (Int64) @@ -91,6 +91,7 @@ import qualified Network.Socket as NetworkSocket import qualified Data.Word as Word import Control.Monad.IO.Unlift (MonadUnliftIO)+import Data.String (fromString) import Prelude -- | Create a MySQL connection pool and run the given action.@@ -164,6 +165,7 @@ , connLimitOffset = decorateSQLWithLimitOffset "LIMIT 18446744073709551615" , connLogFunc = logFunc , connMaxParams = Nothing+ , connRepsertManySql = Just repsertManySql } -- | Set autocommit setting@@ -171,8 +173,11 @@ autocommit' conn bool = void $ MySQL.execute conn "SET autocommit=?" [encodeBool bool] -- | Start a transaction.-begin' :: MySQL.MySQLConn -> IO ()-begin' conn = void $ MySQL.execute_ conn "BEGIN"+begin' :: MySQL.MySQLConn -> Maybe IsolationLevel -> IO ()+begin' conn mIso+ = void+ $ mapM_ (MySQL.execute_ conn . fromString . makeIsolationLevelStatement) mIso+ >> MySQL.execute_ conn "BEGIN" -- | Commit the current transaction. commit' :: MySQL.MySQLConn -> IO ()@@ -892,6 +897,8 @@ ---------------------------------------------------------------------- +escape :: DBName -> Text+escape = T.pack . escapeDBName -- | Escape a database name to be included on a query. escapeDBName :: DBName -> String@@ -1100,7 +1107,9 @@ connLogFunc = undefined, connUpsertSql = undefined, connPutManySql = undefined,- connMaxParams = Nothing}+ connMaxParams = Nothing,+ connRepsertManySql = Nothing+ } result = runReaderT . runWriterT . runWriterT $ mig resp <- result sqlbackend mapM_ T.putStrLn $ map snd $ snd resp@@ -1311,7 +1320,7 @@ tableName = T.pack . escapeDBName . entityDB $ entityDef' copyUnlessValues = map snd fieldsToMaybeCopy recordValues = concatMap (map toPersistValue . toPersistFields) records- recordPlaceholders = commaSeparated $ map (parenWrapped . commaSeparated . map (const "?") . toPersistFields) records+ recordPlaceholders = Util.commaSeparated $ map (Util.parenWrapped . Util.commaSeparated . map (const "?") . toPersistFields) records mkCondFieldSet n _ = T.concat [ n , "=COALESCE("@@ -1324,16 +1333,16 @@ ] condFieldSets = map (uncurry mkCondFieldSet) fieldsToMaybeCopy fieldSets = map (\n -> T.concat [n, "=VALUES(", n, ")"]) updateFieldNames- upds = map (mkUpdateText' (pack . escapeDBName) id) updates+ upds = map (Util.mkUpdateText' (pack . escapeDBName) id) updates updsValues = map (\(Update _ val _) -> toPersistValue val) updates updateText = case fieldSets <> upds <> condFieldSets of [] -> T.concat [firstField, "=", firstField]- xs -> commaSeparated xs+ xs -> Util.commaSeparated xs q = T.concat [ "INSERT INTO " , tableName , " ("- , commaSeparated entityFieldNames+ , Util.commaSeparated entityFieldNames , ") " , " VALUES " , recordPlaceholders@@ -1342,25 +1351,33 @@ ] putManySql :: EntityDef -> Int -> Text-putManySql entityDef' numRecords- | numRecords > 0 = q- | otherwise = error "putManySql: numRecords MUST be greater than 0!"+putManySql ent n = putManySql' fields ent n where- tableName = T.pack . escapeDBName . entityDB $ entityDef'- fieldDbToText = T.pack . escapeDBName . fieldDB- entityFieldNames = map fieldDbToText (entityFields entityDef')- recordPlaceholders= parenWrapped . commaSeparated- $ map (const "?") (entityFields entityDef')- mkAssignment n = T.concat [n, "=VALUES(", n, ")"]- fieldSets = map (mkAssignment . fieldDbToText) (entityFields entityDef')+ fields = entityFields ent++repsertManySql :: EntityDef -> Int -> Text+repsertManySql ent n = putManySql' fields ent n+ where+ fields = keyAndEntityFields ent++putManySql' :: [FieldDef] -> EntityDef -> Int -> Text+putManySql' fields ent n = q+ where+ fieldDbToText = escape . fieldDB+ mkAssignment f = T.concat [f, "=VALUES(", f, ")"]++ table = escape . entityDB $ ent+ columns = Util.commaSeparated $ map fieldDbToText fields+ placeholders = map (const "?") fields+ updates = map (mkAssignment . fieldDbToText) fields+ q = T.concat [ "INSERT INTO "- , tableName- , " ("- , commaSeparated entityFieldNames- , ") "+ , table+ , Util.parenWrapped columns , " VALUES "- , commaSeparated (replicate numRecords recordPlaceholders)+ , Util.commaSeparated . replicate n+ . Util.parenWrapped . Util.commaSeparated $ placeholders , " ON DUPLICATE KEY UPDATE "- , commaSeparated fieldSets+ , Util.commaSeparated updates ]
persistent-mysql-haskell.cabal view
@@ -1,5 +1,5 @@ name: persistent-mysql-haskell-version: 0.4.2+version: 0.5.0 license: MIT license-file: LICENSE author: Naushadh <naushadh@protonmail.com>, Felipe Lessa <felipe.lessa@gmail.com>, Michael Snoyman@@ -28,7 +28,7 @@ library build-depends: base >= 4.6 && < 5 , transformers >= 0.2.1- , persistent >= 2.8.1 && < 3+ , persistent >= 2.9.0 && < 3 , containers >= 0.2 , bytestring >= 0.9 , text >= 0.11.0.6