on dev help sign in
insertIntoReturning
c1ac571Eitan Chatav
squeel-postgresql/src/Squeel/PostgreSQL/Statement.hs+52−23
@@ -586,29 +586,6 @@ subselect
586 -> TableExpression params schema '[table]
586 -> TableExpression params schema '[table]
587subselect selection = join (Subselect selection)
587subselect selection = join (Subselect selection)
588
588
589−{-----------------------------------------
590−INSERT statements
591−-----------------------------------------}
592−
593−insertInto
594− :: (SListI columns, HasTable table schema columns)
595− => Alias table
596− -> NP (Aliased (Expression params '[])) columns
597− -> Statement params '[] schema schema
598−insertInto (Alias table) expressions = UnsafeStatement $ "INSERT INTO "
599− <> fromString (symbolVal' table)
600− <> " (" <> ByteString.intercalate ", " aliases
601− <> ") VALUES ("
602− <> ByteString.intercalate ", " values
603− <> ");"
604− where
605− aliases = hcollapse $ hmap
606− (\ (_ `As` Alias name) -> K (fromString (symbolVal' name)))
607− expressions
608− values = hcollapse $ hmap
609− (\ (expression `As` _) -> K (renderExpression expression))
610− expressions
611−
612{-----------------------------------------
589{-----------------------------------------
613CREATE statements
590CREATE statements
614-----------------------------------------}
591-----------------------------------------}
@@ -773,6 +750,58 @@ instance {-# OVERLAPPABLE #-}
773 DropTable table schema0 schema1
750 DropTable table schema0 schema1
774 => DropTable table (table' ': schema0) (table' ': schema1)
751 => DropTable table (table' ': schema0) (table' ': schema1)
775
752
753+{-----------------------------------------
754+INSERT statements
755+-----------------------------------------}
756+
757+insertInto
758+ :: (SListI columns, HasTable table schema columns)
759+ => Alias table
760+ -> NP (Aliased (Expression params '[])) columns
761+ -> Statement params '[] schema schema
762+insertInto (Alias table) expressions = UnsafeStatement $ "INSERT INTO "
763+ <> fromString (symbolVal' table)
764+ <> " (" <> ByteString.intercalate ", " aliases
765+ <> ") VALUES ("
766+ <> ByteString.intercalate ", " values
767+ <> ");"
768+ where
769+ aliases = hcollapse $ hmap
770+ (\ (_ `As` Alias name) -> K (fromString (symbolVal' name)))
771+ expressions
772+ values = hcollapse $ hmap
773+ (\ (expression `As` _) -> K (renderExpression expression))
774+ expressions
775+
776+insertIntoReturning
777+ :: (SListI columns, SListI results, HasTable table schema columns)
778+ => Alias table
779+ -> NP (Aliased (Expression params '[])) columns
780+ -> NP (Aliased (Expression params '[table ::: columns])) results
781+ -> Statement params results schema schema
782+insertIntoReturning (Alias table) inserts results
783+ = UnsafeStatement . mconcat $
784+ [ "INSERT INTO "
785+ , fromString (symbolVal' table)
786+ , " (" <> ByteString.intercalate ", " aliases
787+ , ") VALUES ("
788+ , ByteString.intercalate ", " values
789+ , ") RETURNING "
790+ , renderList results
791+ , ";"
792+ ]
793+ where
794+ aliases = hcollapse $ hmap
795+ (\ (_ `As` Alias name) -> K (fromString (symbolVal' name)))
796+ inserts
797+ values = hcollapse $ hmap
798+ (\ (expression `As` _) -> K (renderExpression expression))
799+ inserts
800+ renderList
801+ = ByteString.intercalate ", "
802+ . hcollapse
803+ . hmap (K . renderAliased renderExpression)
804+
776{-----------------------------------------
805{-----------------------------------------
777UPDATE statements
806UPDATE statements
778-----------------------------------------}
807-----------------------------------------}
squeel-postgresql/test/Squeel/PostgreSQL/StatementSpec.hs+11−3
@@ -69,19 +69,27 @@ spec = do
69 statement :: Statement '[] Columns Tables Tables
69 statement :: Statement '[] Columns Tables Tables
70 statement = select $ starFrom (#table1 & offset 1 & offset 2)
70 statement = select $ starFrom (#table1 & offset 1 & offset 2)
71 statement `shouldRenderAs` "SELECT * FROM table1 AS table1 OFFSET (1 + 2);"
71 statement `shouldRenderAs` "SELECT * FROM table1 AS table1 OFFSET (1 + 2);"
72− it "correctly render simple INSERTs" $ do
72+ it "correctly renders simple INSERTs" $ do
73 let
73 let
74 statement :: Statement '[] '[] Tables Tables
74 statement :: Statement '[] '[] Tables Tables
75 statement = insertInto #table1 $ 2 `As` #col1 :* 4 `As` #col2 :* Nil
75 statement = insertInto #table1 $ 2 `As` #col1 :* 4 `As` #col2 :* Nil
76 statement `shouldRenderAs`
76 statement `shouldRenderAs`
77 "INSERT INTO table1 (col1, col2) VALUES (2, 4);"
77 "INSERT INTO table1 (col1, col2) VALUES (2, 4);"
78+ it "correctly renders returning INSERTs" $ do
79+ let
80+ statement :: Statement '[] SumAndCol1 Tables Tables
81+ statement = insertIntoReturning #table1
82+ (2 `As` #col1 :* 4 `As` #col2 :* Nil)
83+ ((#col1 + #col2) `As` #sum :* #col1 :* Nil)
84+ statement `shouldRenderAs`
85+ "INSERT INTO table1 (col1, col2) VALUES (2, 4) RETURNING (col1 + col2) AS sum, col1 AS col1;"
78− it "correctly render simple UPDATEs" $ do
86+ it "correctly renders simple UPDATEs" $ do
79 let
87 let
80 statement :: Statement '[] '[] Tables Tables
88 statement :: Statement '[] '[] Tables Tables
81 statement = update #table1 (set 2 `As` #col1 :* same `As` #col2 :* Nil) (#col1 /=* #col2)
89 statement = update #table1 (set 2 `As` #col1 :* same `As` #col2 :* Nil) (#col1 /=* #col2)
82 statement `shouldRenderAs`
90 statement `shouldRenderAs`
83 "UPDATE table1 SET col1 = 2 WHERE (col1 <> col2);"
91 "UPDATE table1 SET col1 = 2 WHERE (col1 <> col2);"
84− it "correctly render upsert INSERTs" $ do
92+ it "correctly renders upsert INSERTs" $ do
85 let
93 let
86 statement :: Statement '[] '[] Tables Tables
94 statement :: Statement '[] '[] Tables Tables
87 statement = upsertInto #table1
95 statement = upsertInto #table1