on dev help sign in
delete statements
commit76d7fbe35ae63d28bd39473ebb353236bd5f4fcb authorEitan Chatav <eitan.chatav@gmail.com> dateTue 18 Jul 2017, 12:36 (9 years ago) parenta3aa0c3
2 files changed+31−10
squeel-postgresql/src/Squeel/PostgreSQL/Statement.hs+16−1 squeel-postgresql/test/Squeel/PostgreSQL/StatementSpec.hs+15−9
squeel-postgresql/src/Squeel/PostgreSQL/Statement.hs+16−1
@@ -303,7 +303,7 @@ instance OrdB (Expression params tables (optionality ('NotNull ty))) where
303303 (<=*) = unsafeBinaryOp "<="
304304
305305{-----------------------------------------
306−table expressions
306+tables
307307-----------------------------------------}
308308
309309newtype Table
@@ -790,3 +790,18 @@ upsertInto (Alias table) inserts updates wh = UnsafeStatement . mconcat $
790790 values = hcollapse $ hmap
791791 (\ (expression `As` _) -> K (renderExpression expression))
792792 inserts
793+
794+{-----------------------------------------
795+DELETE statements
796+-----------------------------------------}
797+
798+deleteFrom
799+ :: (HasTable table schema columns, SListI columns)
800+ => Alias table
801+ -> Expression params '[table ::: columns] ('Required ('NotNull 'PGBool))
802+ -> Statement params '[] schema schema
803+deleteFrom (Alias table) wh = UnsafeStatement $ mconcat
804+ [ "DELETE FROM "
805+ , fromString $ symbolVal' table
806+ , " WHERE ", renderExpression wh, ";"
807+ ]
squeel-postgresql/test/Squeel/PostgreSQL/StatementSpec.hs+15−9
@@ -81,15 +81,21 @@ spec = do
8181 statement = update #table1 (set 2 `As` #col1 :* same `As` #col2 :* Nil) ((#col1 :: Expression '[] Tables ('Required ('NotNull 'PGInt4))) /=* #col2)
8282 statement `shouldRenderAs`
8383 "UPDATE table1 SET col1 = 2 WHERE (col1 <> col2);"
84− -- it "correctly render upsert INSERTs" $ do
85− -- let
86− -- statement :: Statement '[] '[] Tables Tables
87− -- statement = upsertInto #table1
88− -- (2 `As` #col1 :* 4 `As` #col2 :* Nil)
89− -- (set 2 `As` #col1 :* same `As` #col2 :* Nil)
90− -- (#col1 /=* #col2)
91− -- statement `shouldRenderAs`
92− -- "UPDATE table1 SET col1 = 2 WHERE TRUE;"
84+ it "correctly render upsert INSERTs" $ do
85+ let
86+ statement :: Statement '[] '[] Tables Tables
87+ statement = upsertInto #table1
88+ (2 `As` #col1 :* 4 `As` #col2 :* Nil)
89+ (set 2 `As` #col1 :* same `As` #col2 :* Nil)
90+ ((#col1 :: Expression '[] Tables ('Required ('NotNull 'PGInt4))) /=* #col2)
91+ statement `shouldRenderAs`
92+ "INSERT INTO table1 (col1, col2) VALUES (2, 4) ON CONFLICT UPDATE table1 SET col1 = 2 WHERE (col1 <> col2);"
93+ it "correctly renders DELETEs" $ do
94+ let
95+ statement :: Statement '[] '[] Tables Tables
96+ statement = delete #table1 ((#col1 :: Expression '[] Tables ('Required ('NotNull 'PGInt4))) ==* #col2)
97+ statement `shouldRenderAs`
98+ "DELETE FROM table1 WHERE (col1 = col2);"
9399 it "should be safe against SQL injection in literal text" $ do
94100 let
95101 statement :: Statement '[] '[] StudentsTable StudentsTable