on dev help sign in
returning statements
commitadc743bb351c1ff9bcbfb0ce2df30d25e363ecf4 authorEitan Chatav <eitan.chatav@gmail.com> dateTue 18 Jul 2017, 19:18 (9 years ago) parentc1ac571
2 files changed+83−8
squeel-postgresql/src/Squeel/PostgreSQL/Statement.hs+64−7 squeel-postgresql/test/Squeel/PostgreSQL/StatementSpec.hs+19−1
squeel-postgresql/src/Squeel/PostgreSQL/Statement.hs+64−7
@@ -418,22 +418,22 @@ data Join params schema tables where
418418 -> Join params schema (table ': tables)
419419 Inner
420420 :: Aliased (Table params schema) table
421− -> Expression params (table ': tables) ('Required ('NotNull 'PGBool))
421+ -> Predicate params (table ': tables)
422422 -> Join params schema tables
423423 -> Join params schema (table ': tables)
424424 LeftOuter
425425 :: Aliased (Table params schema) right
426− -> Expression params '[left,right] ('Required ('NotNull 'PGBool))
426+ -> Predicate params '[left,right]
427427 -> Join params schema (tables)
428428 -> Join params schema (NullifyTable right ': left ': tables)
429429 RightOuter
430430 :: Aliased (Table params schema) right
431− -> Expression params '[left,right] ('Required ('NotNull 'PGBool))
431+ -> Predicate params '[left,right]
432432 -> Join params schema (left : tables)
433433 -> Join params schema (right ': NullifyTable left ': tables)
434434 FullOuter
435435 :: Aliased (Table params schema) right
436− -> Expression params '[left,right] ('Required ('NotNull 'PGBool))
436+ -> Predicate params '[left,right]
437437 -> Join params schema (left : tables)
438438 -> Join params schema
439439 (NullifyTable right ': NullifyTable left ': tables)
@@ -827,7 +827,7 @@ update
827827 :: (HasTable table schema columns, SListI columns)
828828 => Alias table
829829 -> NP (Aliased (Maybe :.: Expression params '[table ::: columns])) columns
830− -> Expression params '[table ::: columns] ('Required ('NotNull 'PGBool))
830+ -> Predicate params '[table ::: columns]
831831 -> Statement params '[] schema schema
832832update (Alias table) columns wh = UnsafeStatement $ mconcat
833833 [ "UPDATE "
@@ -838,12 +838,35 @@ update (Alias table) columns wh = UnsafeStatement $ mconcat
838838 , " WHERE ", renderExpression wh, ";"
839839 ]
840840
841+updateReturning
842+ :: (SListI columns, SListI results, HasTable table schema columns)
843+ => Alias table
844+ -> NP (Aliased (Maybe :.: Expression params '[table ::: columns])) columns
845+ -> Predicate params '[table ::: columns]
846+ -> NP (Aliased (Expression params '[table ::: columns])) results
847+ -> Statement params results schema schema
848+updateReturning (Alias table) updates wh results = UnsafeStatement $ mconcat
849+ [ "UPDATE "
850+ , fromString $ symbolVal' table
851+ , " SET "
852+ , ByteString.intercalate ", " . catMaybes . hcollapse $
853+ hmap (K . renderSet) updates
854+ , " WHERE ", renderExpression wh
855+ , " RETURNING ", renderList results
856+ , ";"
857+ ]
858+ where
859+ renderList
860+ = ByteString.intercalate ", "
861+ . hcollapse
862+ . hmap (K . renderAliased renderExpression)
863+
841864upsertInto
842865 :: (SListI columns, HasTable table schema columns)
843866 => Alias table
844867 -> NP (Aliased (Expression params '[])) columns
845868 -> NP (Aliased (Maybe :.: Expression params '[table ::: columns])) columns
846− -> Expression params '[table ::: columns] ('Required ('NotNull 'PGBool))
869+ -> Predicate params '[table ::: columns]
847870 -> Statement params '[] schema schema
848871upsertInto (Alias table) inserts updates wh = UnsafeStatement . mconcat $
849872 [ "INSERT INTO "
@@ -865,6 +888,40 @@ upsertInto (Alias table) inserts updates wh = UnsafeStatement . mconcat $
865888 (\ (expression `As` _) -> K (renderExpression expression))
866889 inserts
867890
891+upsertIntoReturning
892+ :: (SListI columns, SListI results, HasTable table schema columns)
893+ => Alias table
894+ -> NP (Aliased (Expression params '[])) columns
895+ -> NP (Aliased (Maybe :.: Expression params '[table ::: columns])) columns
896+ -> Predicate params '[table ::: columns]
897+ -> NP (Aliased (Expression params '[table ::: columns])) results
898+ -> Statement params results schema schema
899+upsertIntoReturning (Alias table) inserts updates wh results = UnsafeStatement . mconcat $
900+ [ "INSERT INTO "
901+ , fromString (symbolVal' table)
902+ , " (" <> ByteString.intercalate ", " aliases
903+ , ") VALUES ("
904+ , ByteString.intercalate ", " values
905+ , ") ON CONFLICT UPDATE "
906+ , fromString $ symbolVal' table
907+ , " SET "
908+ , ByteString.intercalate ", " . catMaybes . hcollapse $
909+ hmap (K . renderSet) updates
910+ , " WHERE ", renderExpression wh
911+ , " RETURNING ", renderList results
912+ , ";"
913+ ] where
914+ aliases = hcollapse $ hmap
915+ (\ (_ `As` Alias name) -> K (fromString (symbolVal' name)))
916+ inserts
917+ values = hcollapse $ hmap
918+ (\ (expression `As` _) -> K (renderExpression expression))
919+ inserts
920+ renderList
921+ = ByteString.intercalate ", "
922+ . hcollapse
923+ . hmap (K . renderAliased renderExpression)
924+
868925{-----------------------------------------
869926DELETE statements
870927-----------------------------------------}
@@ -872,7 +929,7 @@ DELETE statements
872929deleteFrom
873930 :: (HasTable table schema columns, SListI columns)
874931 => Alias table
875− -> Expression params '[table ::: columns] ('Required ('NotNull 'PGBool))
932+ -> Predicate params '[table ::: columns]
876933 -> Statement params '[] schema schema
877934deleteFrom (Alias table) wh = UnsafeStatement $ mconcat
878935 [ "DELETE FROM "
squeel-postgresql/test/Squeel/PostgreSQL/StatementSpec.hs+19−1
@@ -8,7 +8,6 @@
88
99module Squeel.PostgreSQL.StatementSpec where
1010
11−-- import Data.Boolean
1211import Data.Function
1312import Generics.SOP hiding (from)
1413import Test.Hspec
@@ -89,6 +88,15 @@ spec = do
8988 statement = update #table1 (set 2 `As` #col1 :* same `As` #col2 :* Nil) (#col1 /=* #col2)
9089 statement `shouldRenderAs`
9190 "UPDATE table1 SET col1 = 2 WHERE (col1 <> col2);"
91+ it "correctly renders returning UPDATEs" $ do
92+ let
93+ statement :: Statement '[] SumAndCol1 Tables Tables
94+ statement = updateReturning #table1
95+ (set 2 `As` #col1 :* same `As` #col2 :* Nil)
96+ (#col1 /=* #col2)
97+ ((#col1 + #col2) `As` #sum :* #col1 :* Nil)
98+ statement `shouldRenderAs`
99+ "UPDATE table1 SET col1 = 2 WHERE (col1 <> col2) RETURNING (col1 + col2) AS sum, col1 AS col1;"
92100 it "correctly renders upsert INSERTs" $ do
93101 let
94102 statement :: Statement '[] '[] Tables Tables
@@ -98,6 +106,16 @@ spec = do
98106 (#col1 /=* #col2)
99107 statement `shouldRenderAs`
100108 "INSERT INTO table1 (col1, col2) VALUES (2, 4) ON CONFLICT UPDATE table1 SET col1 = 2 WHERE (col1 <> col2);"
109+ it "correctly renders returning upsert INSERTs" $ do
110+ let
111+ statement :: Statement '[] SumAndCol1 Tables Tables
112+ statement = upsertIntoReturning #table1
113+ (2 `As` #col1 :* 4 `As` #col2 :* Nil)
114+ (set 2 `As` #col1 :* same `As` #col2 :* Nil)
115+ (#col1 /=* #col2)
116+ ((#col1 + #col2) `As` #sum :* #col1 :* Nil)
117+ statement `shouldRenderAs`
118+ "INSERT INTO table1 (col1, col2) VALUES (2, 4) ON CONFLICT UPDATE table1 SET col1 = 2 WHERE (col1 <> col2) RETURNING (col1 + col2) AS sum, col1 AS col1;"
101119 it "correctly renders DELETEs" $ do
102120 let
103121 statement :: Statement '[] '[] Tables Tables