on dev help sign in
returning statements
adc743bEitan Chatav
squeel-postgresql/src/Squeel/PostgreSQL/Statement.hs+64−7
@@ -418,22 +418,22 @@ data Join params schema tables where
418 -> Join params schema (table ': tables)
418 -> Join params schema (table ': tables)
419 Inner
419 Inner
420 :: Aliased (Table params schema) table
420 :: Aliased (Table params schema) table
421− -> Expression params (table ': tables) ('Required ('NotNull 'PGBool))
421+ -> Predicate params (table ': tables)
422 -> Join params schema tables
422 -> Join params schema tables
423 -> Join params schema (table ': tables)
423 -> Join params schema (table ': tables)
424 LeftOuter
424 LeftOuter
425 :: Aliased (Table params schema) right
425 :: Aliased (Table params schema) right
426− -> Expression params '[left,right] ('Required ('NotNull 'PGBool))
426+ -> Predicate params '[left,right]
427 -> Join params schema (tables)
427 -> Join params schema (tables)
428 -> Join params schema (NullifyTable right ': left ': tables)
428 -> Join params schema (NullifyTable right ': left ': tables)
429 RightOuter
429 RightOuter
430 :: Aliased (Table params schema) right
430 :: Aliased (Table params schema) right
431− -> Expression params '[left,right] ('Required ('NotNull 'PGBool))
431+ -> Predicate params '[left,right]
432 -> Join params schema (left : tables)
432 -> Join params schema (left : tables)
433 -> Join params schema (right ': NullifyTable left ': tables)
433 -> Join params schema (right ': NullifyTable left ': tables)
434 FullOuter
434 FullOuter
435 :: Aliased (Table params schema) right
435 :: Aliased (Table params schema) right
436− -> Expression params '[left,right] ('Required ('NotNull 'PGBool))
436+ -> Predicate params '[left,right]
437 -> Join params schema (left : tables)
437 -> Join params schema (left : tables)
438 -> Join params schema
438 -> Join params schema
439 (NullifyTable right ': NullifyTable left ': tables)
439 (NullifyTable right ': NullifyTable left ': tables)
@@ -827,7 +827,7 @@ update
827 :: (HasTable table schema columns, SListI columns)
827 :: (HasTable table schema columns, SListI columns)
828 => Alias table
828 => Alias table
829 -> NP (Aliased (Maybe :.: Expression params '[table ::: columns])) columns
829 -> NP (Aliased (Maybe :.: Expression params '[table ::: columns])) columns
830− -> Expression params '[table ::: columns] ('Required ('NotNull 'PGBool))
830+ -> Predicate params '[table ::: columns]
831 -> Statement params '[] schema schema
831 -> Statement params '[] schema schema
832update (Alias table) columns wh = UnsafeStatement $ mconcat
832update (Alias table) columns wh = UnsafeStatement $ mconcat
833 [ "UPDATE "
833 [ "UPDATE "
@@ -838,12 +838,35 @@ update (Alias table) columns wh = UnsafeStatement $ mconcat
838 , " WHERE ", renderExpression wh, ";"
838 , " WHERE ", renderExpression wh, ";"
839 ]
839 ]
840
840
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+
841upsertInto
864upsertInto
842 :: (SListI columns, HasTable table schema columns)
865 :: (SListI columns, HasTable table schema columns)
843 => Alias table
866 => Alias table
844 -> NP (Aliased (Expression params '[])) columns
867 -> NP (Aliased (Expression params '[])) columns
845 -> NP (Aliased (Maybe :.: Expression params '[table ::: columns])) columns
868 -> NP (Aliased (Maybe :.: Expression params '[table ::: columns])) columns
846− -> Expression params '[table ::: columns] ('Required ('NotNull 'PGBool))
869+ -> Predicate params '[table ::: columns]
847 -> Statement params '[] schema schema
870 -> Statement params '[] schema schema
848upsertInto (Alias table) inserts updates wh = UnsafeStatement . mconcat $
871upsertInto (Alias table) inserts updates wh = UnsafeStatement . mconcat $
849 [ "INSERT INTO "
872 [ "INSERT INTO "
@@ -865,6 +888,40 @@ upsertInto (Alias table) inserts updates wh = UnsafeStatement . mconcat $
865 (\ (expression `As` _) -> K (renderExpression expression))
888 (\ (expression `As` _) -> K (renderExpression expression))
866 inserts
889 inserts
867
890
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+
868{-----------------------------------------
925{-----------------------------------------
869DELETE statements
926DELETE statements
870-----------------------------------------}
927-----------------------------------------}
@@ -872,7 +929,7 @@ DELETE statements
872deleteFrom
929deleteFrom
873 :: (HasTable table schema columns, SListI columns)
930 :: (HasTable table schema columns, SListI columns)
874 => Alias table
931 => Alias table
875− -> Expression params '[table ::: columns] ('Required ('NotNull 'PGBool))
932+ -> Predicate params '[table ::: columns]
876 -> Statement params '[] schema schema
933 -> Statement params '[] schema schema
877deleteFrom (Alias table) wh = UnsafeStatement $ mconcat
934deleteFrom (Alias table) wh = UnsafeStatement $ mconcat
878 [ "DELETE FROM "
935 [ "DELETE FROM "
squeel-postgresql/test/Squeel/PostgreSQL/StatementSpec.hs+19−1
@@ -8,7 +8,6 @@
8
8
9module Squeel.PostgreSQL.StatementSpec where
9module Squeel.PostgreSQL.StatementSpec where
10
10
11−-- import Data.Boolean
12import Data.Function
11import Data.Function
13import Generics.SOP hiding (from)
12import Generics.SOP hiding (from)
14import Test.Hspec
13import Test.Hspec
@@ -89,6 +88,15 @@ spec = do
89 statement = update #table1 (set 2 `As` #col1 :* same `As` #col2 :* Nil) (#col1 /=* #col2)
88 statement = update #table1 (set 2 `As` #col1 :* same `As` #col2 :* Nil) (#col1 /=* #col2)
90 statement `shouldRenderAs`
89 statement `shouldRenderAs`
91 "UPDATE table1 SET col1 = 2 WHERE (col1 <> col2);"
90 "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;"
92 it "correctly renders upsert INSERTs" $ do
100 it "correctly renders upsert INSERTs" $ do
93 let
101 let
94 statement :: Statement '[] '[] Tables Tables
102 statement :: Statement '[] '[] Tables Tables
@@ -98,6 +106,16 @@ spec = do
98 (#col1 /=* #col2)
106 (#col1 /=* #col2)
99 statement `shouldRenderAs`
107 statement `shouldRenderAs`
100 "INSERT INTO table1 (col1, col2) VALUES (2, 4) ON CONFLICT UPDATE table1 SET col1 = 2 WHERE (col1 <> col2);"
108 "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;"
101 it "correctly renders DELETEs" $ do
119 it "correctly renders DELETEs" $ do
102 let
120 let
103 statement :: Statement '[] '[] Tables Tables
121 statement :: Statement '[] '[] Tables Tables