Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. diff --git a/src/PostgREST/App.hs b/src/PostgREST/App.hs
  2. index d1d27b4..abf3d8f 100644
  3. --- a/src/PostgREST/App.hs
  4. +++ b/src/PostgREST/App.hs
  5. @@ -53,6 +53,7 @@ import PostgREST.QueryBuilder ( callProc
  6. , addRelations
  7. , createReadStatement
  8. , createWriteStatement
  9. + , lockRowExclusive
  10. , ResultsWithCount
  11. )
  12.  
  13. @@ -119,19 +120,12 @@ app dbStructure conf reqBody req =
  14. ]
  15. $ if iPreferRepresentation apiRequest == Full then cs body else ""
  16.  
  17. - (ActionUpdate, TargetIdent qi, Just payload@(PayloadJSON uniform)) ->
  18. + (ActionUpdate, TargetIdent qi, Just (PayloadJSON _)) ->
  19. case mutateSqlParts of
  20. Left e -> return $ responseLBS status400 [jsonH] $ cs e
  21. - Right (sq,mq) -> do
  22. - let stm = createWriteStatement qi sq mq False (iPreferRepresentation apiRequest) [] (contentType == TextCSV) payload
  23. - row <- H.query uniform stm
  24. - let (_, queryTotal, _, body) = extractQueryResult row
  25. - r = contentRangeH 0 (toInteger $ queryTotal-1) (toInteger <$> Just queryTotal)
  26. - s = case () of _ | queryTotal == 0 -> status404
  27. - | iPreferRepresentation apiRequest == Full -> status200
  28. - | otherwise -> status204
  29. - return $ responseLBS s [contentTypeH, r]
  30. - $ if iPreferRepresentation apiRequest == Full then cs body else ""
  31. + Right _ -> do
  32. + lockRowExclusive qi
  33. + return $ responseLBS status200 [] ""
  34.  
  35. (ActionDelete, TargetIdent qi, Nothing) ->
  36. case mutateSqlParts of
  37. diff --git a/src/PostgREST/QueryBuilder.hs b/src/PostgREST/QueryBuilder.hs
  38. index 0da8118..103999e 100644
  39. --- a/src/PostgREST/QueryBuilder.hs
  40. +++ b/src/PostgREST/QueryBuilder.hs
  41. @@ -19,6 +19,7 @@ module PostgREST.QueryBuilder (
  42. , createReadStatement
  43. , createWriteStatement
  44. , inTransaction
  45. + , lockRowExclusive
  46. , operators
  47. , pgFmtIdent
  48. , pgFmtLit
  49. @@ -361,6 +362,11 @@ requestToQuery schema (DbMutate (Delete mainTbl conditions)) =
  50. ("WHERE " <> intercalate " AND " ( map (pgFmtCondition qi ) conditions )) `emptyOnNull` conditions
  51. ]
  52.  
  53. +lockRowExclusive :: QualifiedIdentifier -> H.Session ()
  54. +lockRowExclusive qi =
  55. + H.sql [qc| LOCK TABLE {fromQi qi} IN SHARE ROW EXCLUSIVE MODE; |]
  56. +
  57. +
  58. sourceCTEName :: SqlFragment
  59. sourceCTEName = "pg_source"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement