@@ -30,14 +30,20 @@ module Squeal.PostgreSQL.Manipulation
3030 , queryStatement
3131 -- * Insert
3232 , insertRows
33+ , insertRow
34+ , insertRows_
35+ , insertRow_
3336 , insertQuery
37+ , insertQuery_
3438 , ReturningClause (ReturningStar, Returning)
3539 , renderReturningClause
3640 , ConflictClause (OnConflictDoRaise, OnConflictDoNothing, OnConflictDoUpdate)
3741 , renderConflictClause
3842 -- * Update
3943 , update
44+ , update_
4045 , deleteFrom
46+ , deleteFrom_
4147 , ColumnValue (..)
4248 ) where
4349
@@ -86,30 +92,27 @@ simple insert:
8692>>> :{
8793let
8894 manipulation :: Manipulation
89− '[ "tab" :::
90− '[ "col1" ::: 'Required ('NotNull 'PGint4)
91− , "col2" ::: 'Required ('NotNull 'PGint4) ]] '[] '[]
95+ '[ "tab" ::: '[] :=>
96+ '[ "col1" ::: 'NoDef :=> 'NotNull 'PGint4
97+ , "col2" ::: 'Def :=> 'NotNull 'PGint4 ]] '[] '[]
9298 manipulation =
93− insertInto #tab (ColumnValues (2 `As` #col1 :* 4 `As` #col2 :* Nil) [])
94− OnConflictDoRaise (Returning Nil)
99+ insertRow_ #tab (Set 2 `As` #col1 :* Default `As` #col2 :* Nil)
95100in renderManipulation manipulation
96101:}
97−"INSERT INTO tab (col1, col2) VALUES (2, 4);"
102+"INSERT INTO tab (col1, col2) VALUES (2, DEFAULT);"
98103
99104parameterized insert:
100105
101106>>> :{
102107let
103108 manipulation :: Manipulation
104− '[ "tab" :::
105− '[ "col1" ::: 'Required ('NotNull 'PGint4)
106− , "col2" ::: 'Required ('NotNull 'PGint4) ]]
107− '[ 'Required ('NotNull 'PGint4)
108− , 'Required ('NotNull 'PGint4) ] '[]
109+ '[ "tab" ::: '[] :=>
110+ '[ "col1" ::: 'NoDef :=> 'NotNull 'PGint4
111+ , "col2" ::: 'NoDef :=> 'NotNull 'PGint4 ]]
112+ '[ 'NotNull 'PGint4, 'NotNull 'PGint4 ] '[]
109113 manipulation =
110− insertInto #tab
111− (ColumnValues (param @1 `As` #col1 :* param @2 `As` #col2 :* Nil) [])
112− OnConflictDoRaise (Returning Nil)
114+ insertRow_ #tab
115+ (Set (param @1) `As` #col1 :* Set (param @2) `As` #col2 :* Nil)
113116in renderManipulation manipulation
114117:}
115118"INSERT INTO tab (col1, col2) VALUES (($1 :: int4), ($2 :: int4));"
@@ -119,36 +122,34 @@ returning insert:
119122>>> :{
120123let
121124 manipulation :: Manipulation
122− '[ "tab" :::
123− '[ "col1" ::: 'Required ('NotNull 'PGint4)
124− , "col2" ::: 'Required ('NotNull 'PGint4) ]] '[]
125− '["fromOnly" ::: 'Required ('NotNull 'PGint4)]
125+ '[ "tab" ::: '[] :=>
126+ '[ "col1" ::: 'NoDef :=> 'NotNull 'PGint4
127+ , "col2" ::: 'Def :=> 'NotNull 'PGint4 ]] '[]
128+ '["fromOnly" ::: 'NotNull 'PGint4]
126129 manipulation =
127− insertInto #tab (ColumnValues (2 `As` #col1 :* 4 `As` #col2 :* Nil) [])
130+ insertRow #tab (Set 2 `As` #col1 :* Default `As` #col2 :* Nil)
128131 OnConflictDoRaise (Returning (#col1 `As` #fromOnly :* Nil))
129132in renderManipulation manipulation
130133:}
131−"INSERT INTO tab (col1, col2) VALUES (2, 4) RETURNING col1 AS fromOnly;"
134+"INSERT INTO tab (col1, col2) VALUES (2, DEFAULT) RETURNING col1 AS fromOnly;"
132135
133136query insert:
134137
135138>>> :{
136139let
137140 manipulation :: Manipulation
138− '[ "tab" :::
139− '[ "col1" ::: 'Required ('NotNull 'PGint4)
140− , "col2" ::: 'Required ('NotNull 'PGint4)
141+ '[ "tab" ::: '[] :=>
142+ '[ "col1" ::: 'NoDef :=> 'NotNull 'PGint4
143+ , "col2" ::: 'NoDef :=> 'NotNull 'PGint4
141144 ]
142− , "other_tab" :::
143− '[ "col1" ::: 'Required ('NotNull 'PGint4)
144− , "col2" ::: 'Required ('NotNull 'PGint4)
145+ , "other_tab" ::: '[] :=>
146+ '[ "col1" ::: 'NoDef :=> 'NotNull 'PGint4
147+ , "col2" ::: 'NoDef :=> 'NotNull 'PGint4
145148 ]
146149 ] '[] '[]
147150 manipulation =
148− insertInto #tab
149− ( ColumnValuesQuery $
150− selectStar (from (Table (#other_tab `As` #t))) )
151− OnConflictDoRaise (Returning Nil)
151+ insertQuery_ #tab
152+ (selectStar (from (Table (#other_tab `As` #t))))
152153in renderManipulation manipulation
153154:}
154155"INSERT INTO tab SELECT * FROM other_tab AS t;"
@@ -158,18 +159,17 @@ upsert:
158159>>> :{
159160let
160161 manipulation :: Manipulation
161− '[ "tab" :::
162− '[ "col1" ::: 'Required ('NotNull 'PGint4)
163− , "col2" ::: 'Required ('NotNull 'PGint4) ]]
164− '[] '[ "sum" ::: 'Required ('NotNull 'PGint4)]
162+ '[ "tab" ::: '[] :=>
163+ '[ "col1" ::: 'NoDef :=> 'NotNull 'PGint4
164+ , "col2" ::: 'NoDef :=> 'NotNull 'PGint4 ]]
165+ '[] '[ "sum" ::: 'NotNull 'PGint4]
165166 manipulation =
166− insertInto #tab
167− (ColumnValues
168− (2 `As` #col1 :* 4 `As` #col2 :* Nil)
169− [6 `As` #col1 :* 8 `As` #col2 :* Nil])
167+ insertRows #tab
168+ (Set 2 `As` #col1 :* Set 4 `As` #col2 :* Nil)
169+ [Set 6 `As` #col1 :* Set 8 `As` #col2 :* Nil]
170170 (OnConflictDoUpdate
171171 (Set 2 `As` #col1 :* Same `As` #col2 :* Nil)
172− (Just (#col1 .== #col2)))
172+ [#col1 .== #col2])
173173 (Returning $ (#col1 + #col2) `As` #sum :* Nil)
174174in renderManipulation manipulation
175175:}
@@ -204,6 +204,39 @@ insertRows tab row rows conflict returning = UnsafeManipulation $
204204 Default -> "DEFAULT"
205205 Set expression -> renderExpression expression
206206
207+insertRow
208+ :: ( SOP.SListI columns
209+ , SOP.SListI results
210+ , Has tab schema table
211+ , columns ~ TableToColumns table )
212+ => Alias tab -- ^ table to insert into
213+ -> NP (Aliased (ColumnValue '[] params)) columns
214+ -> ConflictClause columns params
215+ -- ^ what to do in case of constraint conflict
216+ -> ReturningClause columns params results -- ^ results to return
217+ -> Manipulation schema params results
218+insertRow tab row = insertRows tab row []
219+
220+insertRows_
221+ :: ( SOP.SListI columns
222+ , Has tab schema table
223+ , columns ~ TableToColumns table )
224+ => Alias tab -- ^ table to insert into
225+ -> NP (Aliased (ColumnValue '[] params)) columns
226+ -> [NP (Aliased (ColumnValue '[] params)) columns]
227+ -> Manipulation schema params '[]
228+insertRows_ tab row rows =
229+ insertRows tab row rows OnConflictDoRaise (Returning Nil)
230+
231+insertRow_
232+ :: ( SOP.SListI columns
233+ , Has tab schema table
234+ , columns ~ TableToColumns table )
235+ => Alias tab -- ^ table to insert into
236+ -> NP (Aliased (ColumnValue '[] params)) columns
237+ -> Manipulation schema params '[]
238+insertRow_ tab row = insertRow tab row OnConflictDoRaise (Returning Nil)
239+
207240insertQuery
208241 :: ( SOP.SListI columns
209242 , SOP.SListI results
@@ -221,6 +254,16 @@ insertQuery tab query conflict returning = UnsafeManipulation $
221254 <> renderConflictClause conflict
222255 <> renderReturningClause returning
223256
257+insertQuery_
258+ :: ( SOP.SListI columns
259+ , Has tab schema table
260+ , columns ~ TableToColumns table )
261+ => Alias tab -- ^ table to insert into
262+ -> Query schema params (ColumnsToRelation columns)
263+ -> Manipulation schema params '[]
264+insertQuery_ tab query =
265+ insertQuery tab query OnConflictDoRaise (Returning Nil)
266+
224267data ColumnValue
225268 (columns :: RelationType)
226269 (params :: [NullityType])
@@ -248,11 +291,11 @@ data ReturningClause
248291 (results :: RelationType)
249292 where
250293 ReturningStar
251− :: ReturningClause columns params (ColumnsToRelation columns)
294+ :: results ~ ColumnsToRelation columns
295+ => ReturningClause columns params results
252296 Returning
253− :: NP
254− (Aliased (Expression '[table ::: (ColumnsToRelation columns)] 'Ungrouped params))
255− results
297+ :: rel ~ ColumnsToRelation columns
298+ => NP (Aliased (Expression '[table ::: rel] 'Ungrouped params)) results
256299 -> ReturningClause columns params results
257300
258301-- | Render a `ReturningClause`.
@@ -314,12 +357,12 @@ UPDATE statements
314357-- >>> :{
315358-- let
316359-- manipulation :: Manipulation
317−-- '[ "tab" :::
318−-- '[ "col1" ::: 'Required ('NotNull 'PGint4)
319−-- , "col2" ::: 'Required ('NotNull 'PGint4) ]] '[] '[]
360+-- '[ "tab" ::: '[] :=>
361+-- '[ "col1" ::: 'NoDef :=> 'NotNull 'PGint4
362+-- , "col2" ::: 'NoDef :=> 'NotNull 'PGint4 ]] '[] '[]
320363-- manipulation =
321−-- update #tab (Set 2 `As` #col1 :* Same `As` #col2 :* Nil)
322−-- (#col1 ./= #col2) (Returning Nil)
364+-- update_ #tab (Set 2 `As` #col1 :* Same `As` #col2 :* Nil)
365+-- (#col1 ./= #col2)
323366-- in renderManipulation manipulation
324367-- :}
325368-- "UPDATE tab SET col1 = 2 WHERE (col1 <> col2);"
@@ -353,6 +396,18 @@ update table columns wh returning = UnsafeManipulation $
353396 Set expression `As` column -> Just $
354397 renderAlias column <+> "=" <+> renderExpression expression
355398
399+update_
400+ :: ( SOP.SListI columns
401+ , Has tab schema table
402+ , columns ~ TableToColumns table )
403+ => Alias tab -- ^ table to update
404+ -> NP (Aliased (ColumnValue (ColumnsToRelation columns) params)) columns
405+ -- ^ modified values to replace old values
406+ -> Condition '[tab ::: ColumnsToRelation columns] 'Ungrouped params
407+ -- ^ condition under which to perform update on a row
408+ -> Manipulation schema params '[]
409+update_ table columns wh = update table columns wh (Returning Nil)
410+
356411{-----------------------------------------
357412DELETE statements
358413-----------------------------------------}
@@ -362,11 +417,11 @@ DELETE statements
362417-- >>> :{
363418-- let
364419-- manipulation :: Manipulation
365−-- '[ "tab" :::
366−-- '[ "col1" ::: 'Required ('NotNull 'PGint4)
367−-- , "col2" ::: 'Required ('NotNull 'PGint4) ]] '[]
368−-- '[ "col1" ::: 'Required ('NotNull 'PGint4)
369−-- , "col2" ::: 'Required ('NotNull 'PGint4) ]
420+-- '[ "tab" ::: '[] :=>
421+-- '[ "col1" ::: 'NoDef :=> 'NotNull 'PGint4
422+-- , "col2" ::: 'NoDef :=> 'NotNull 'PGint4 ]] '[]
423+-- '[ "col1" ::: 'NotNull 'PGint4
424+-- , "col2" ::: 'NotNull 'PGint4 ]
370425-- manipulation = deleteFrom #tab (#col1 .== #col2) ReturningStar
371426-- in renderManipulation manipulation
372427-- :}
@@ -384,3 +439,12 @@ deleteFrom table wh returning = UnsafeManipulation $
384439 "DELETE FROM" <+> renderAlias table
385440 <+> "WHERE" <+> renderExpression wh
386441 <> renderReturningClause returning
442+
443+deleteFrom_
444+ :: ( Has tab schema table
445+ , columns ~ TableToColumns table )
446+ => Alias tab -- ^ table to delete from
447+ -> Condition '[tab ::: ColumnsToRelation columns] 'Ungrouped params
448+ -- ^ condition under which to delete a row
449+ -> Manipulation schema params '[]
450+deleteFrom_ tab wh = deleteFrom tab wh (Returning Nil)