on dev help sign in
Merge branch 'dev' into dev-commons-schema-merge
3f3af94Eitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+14
@@ -88,6 +88,7 @@ module Squeal.PostgreSQL.Expression
88 , upper
88 , upper
89 , charLength
89 , charLength
90 , like
90 , like
91+ , ilike
91 -- ** Json
92 -- ** Json
92 -- *** Json and Jsonb operators
93 -- *** Json and Jsonb operators
93 , (.->)
94 , (.->)
@@ -958,6 +959,19 @@ like
958 -> Expression db from grouping params (nullity 'PGbool)
959 -> Expression db from grouping params (nullity 'PGbool)
959like = unsafeBinaryOp "LIKE"
960like = unsafeBinaryOp "LIKE"
960
961
962+-- | The key word ILIKE can be used instead of LIKE to make the
963+-- match case-insensitive according to the active locale.
964+--
965+-- >>> printSQL $ "abc" `ilike` "a%"
966+-- (E'abc' ILIKE E'a%')
967+ilike
968+ :: Expression schema from grouping params (nullity 'PGtext)
969+ -- ^ string
970+ -> Expression schema from grouping params (nullity 'PGtext)
971+ -- ^ pattern
972+ -> Expression schema from grouping params (nullity 'PGbool)
973+ilike = unsafeBinaryOp "ILIKE"
974+
961{-----------------------------------------
975{-----------------------------------------
962 -- json and jsonb support
976 -- json and jsonb support
963
977
squeal-postgresql/src/Squeal/PostgreSQL/Manipulation.hs+56−8
@@ -27,6 +27,7 @@ module Squeal.PostgreSQL.Manipulation
27 , ColumnValue (..)
27 , ColumnValue (..)
28 , ReturningClause (ReturningStar, Returning)
28 , ReturningClause (ReturningStar, Returning)
29 , ConflictClause (OnConflictDoRaise, OnConflictDoNothing, OnConflictDoUpdate)
29 , ConflictClause (OnConflictDoRaise, OnConflictDoNothing, OnConflictDoUpdate)
30+ , UsingClause (..)
30 -- * Insert
31 -- * Insert
31 , insertRows
32 , insertRows
32 , insertRow
33 , insertRow
@@ -42,6 +43,7 @@ module Squeal.PostgreSQL.Manipulation
42 -- * Delete
43 -- * Delete
43 , deleteFrom
44 , deleteFrom
44 , deleteFrom_
45 , deleteFrom_
46+ , also
45 ) where
47 ) where
46
48
47import Control.DeepSeq
49import Control.DeepSeq
@@ -148,11 +150,31 @@ let
148 manipulation :: Manipulation (DBof (Public Schema)) '[]
150 manipulation :: Manipulation (DBof (Public Schema)) '[]
149 '[ "col1" ::: 'NotNull 'PGint4
151 '[ "col1" ::: 'NotNull 'PGint4
150 , "col2" ::: 'NotNull 'PGint4 ]
152 , "col2" ::: 'NotNull 'PGint4 ]
151− manipulation = deleteFrom #tab (#col1 .== #col2) ReturningStar
153+ manipulation = deleteFrom #tab NoUsing (#col1 .== #col2) ReturningStar
152in printSQL manipulation
154in printSQL manipulation
153:}
155:}
154DELETE FROM "tab" WHERE ("col1" = "col2") RETURNING *
156DELETE FROM "tab" WHERE ("col1" = "col2") RETURNING *
155
157
158+delete and using clause:
159+
160+>>> :{
161+type Schema3 =
162+ '[ "tab" ::: 'Table ('[] :=> Columns)
163+ , "other_tab" ::: 'Table ('[] :=> Columns)
164+ , "third_tab" ::: 'Table ('[] :=> Columns) ]
165+:}
166+
167+>>> :{
168+let
169+ manipulation :: Manipulation (DBof (Public Schema3)) '[] '[]
170+ manipulation =
171+ deleteFrom_ #tab (Using (table #other_tab & also (table #third_tab)))
172+ ( (#tab ! #col2 .== #other_tab ! #col2)
173+ .&& (#tab ! #col2 .== #third_tab ! #col2) )
174+in printSQL manipulation
175+:}
176+DELETE FROM "tab" USING "other_tab" AS "other_tab", "third_tab" AS "third_tab" WHERE (("tab"."col2" = "other_tab"."col2") AND ("tab"."col2" = "third_tab"."col2"))
177+
156with manipulation:
178with manipulation:
157
179
158>>> type ProductsColumns = '["product" ::: 'NoDef :=> 'NotNull 'PGtext, "date" ::: 'Def :=> 'NotNull 'PGdate]
180>>> type ProductsColumns = '["product" ::: 'NoDef :=> 'NotNull 'PGtext, "date" ::: 'Def :=> 'NotNull 'PGdate]
@@ -161,7 +183,7 @@ with manipulation:
161let
183let
162 manipulation :: Manipulation (DBof (Public ProductsSchema)) '[ 'NotNull 'PGdate] '[]
184 manipulation :: Manipulation (DBof (Public ProductsSchema)) '[ 'NotNull 'PGdate] '[]
163 manipulation = with
185 manipulation = with
164− (deleteFrom #products (#date .< param @1) ReturningStar `as` #del)
186+ (deleteFrom #products NoUsing (#date .< param @1) ReturningStar `as` #del)
165 (insertQuery_ #products_deleted (selectStar (from (common (#del)))))
187 (insertQuery_ #products_deleted (selectStar (from (common (#del)))))
166in printSQL manipulation
188in printSQL manipulation
167:}
189:}
@@ -459,7 +481,13 @@ update_ tab columns wh = update tab columns wh (Returning Nil)
459DELETE statements
481DELETE statements
460-----------------------------------------}
482-----------------------------------------}
461
483
484+data UsingClause db params from where
485+ NoUsing :: UsingClause db params '[]
486+ Using
487+ :: FromClause db params from
488+ -> UsingClause db params from
489+
462−-- | Delete rows of a table.
490+-- | Delete rows from a table.
463deleteFrom
491deleteFrom
464 :: ( SOP.SListI results
492 :: ( SOP.SListI results
465 , db ~ (commons :=> schemas)
493 , db ~ (commons :=> schemas)
@@ -468,12 +496,17 @@ deleteFrom
468 , row ~ TableToRow table
496 , row ~ TableToRow table
469 , columns ~ TableToColumns table )
497 , columns ~ TableToColumns table )
470 => QualifiedAlias sch tab -- ^ table to delete from
498 => QualifiedAlias sch tab -- ^ table to delete from
499+ -> UsingClause db params from
471− -> Condition db '[tab ::: row] 'Ungrouped params
500+ -> Condition db (tab ::: row ': from) 'Ungrouped params
472 -- ^ condition under which to delete a row
501 -- ^ condition under which to delete a row
473 -> ReturningClause db params row results -- ^ results to return
502 -> ReturningClause db params row results -- ^ results to return
474 -> Manipulation db params results
503 -> Manipulation db params results
475−deleteFrom tab wh returning = UnsafeManipulation $
504+deleteFrom tab using wh returning = UnsafeManipulation $
505+ "DELETE FROM"
476− "DELETE FROM" <+> renderQualifiedAlias tab
506+ <+> renderQualifiedAlias tab
507+ <> case using of
508+ NoUsing -> ""
509+ Using tables -> " USING" <+> renderFromClause tables
477 <+> "WHERE" <+> renderExpression wh
510 <+> "WHERE" <+> renderExpression wh
478 <> renderReturningClause returning
511 <> renderReturningClause returning
479
512
@@ -485,7 +518,22 @@ deleteFrom_
485 , row ~ TableToRow table
518 , row ~ TableToRow table
486 , columns ~ TableToColumns table )
519 , columns ~ TableToColumns table )
487 => QualifiedAlias sch tab -- ^ table to delete from
520 => QualifiedAlias sch tab -- ^ table to delete from
521+ -> UsingClause db params from
488− -> (forall t. Condition db '[t ::: row] 'Ungrouped params)
522+ -> Condition db (tab ::: row ': from) 'Ungrouped params
489 -- ^ condition under which to delete a row
523 -- ^ condition under which to delete a row
490 -> Manipulation db params '[]
524 -> Manipulation db params '[]
491−deleteFrom_ tab wh = deleteFrom tab wh (Returning Nil)
525+deleteFrom_ tab uses wh = deleteFrom tab uses wh (Returning Nil)
526+
527+-- | This has the behaviour of a cartesian product, taking all
528+-- possible combinations between @left@ and @right@ - exactly like a
529+-- CROSS JOIN. Used when no "CROSS JOIN" syntax is required but simply
530+-- a comma separated list of tables. Typical case is the `USING` clause
531+-- of a DELETE query.
532+also
533+ :: FromClause schema params right
534+ -- ^ right
535+ -> FromClause schema params left
536+ -- ^ left
537+ -> FromClause schema params (Join left right)
538+also right left = UnsafeFromClause $
539+ renderFromClause left <> "," <+> renderFromClause right
squeal-postgresql/src/Squeal/PostgreSQL/Migration.hs+1−1
@@ -284,7 +284,7 @@ insertMigration = insertRow_ (#migrations ! #schema_migrations)
284deleteMigration
284deleteMigration
285 :: Has "migrations" schemas MigrationsSchema
285 :: Has "migrations" schemas MigrationsSchema
286 => Manipulation ('[] :=> schemas) '[ 'NotNull 'PGtext ] '[]
286 => Manipulation ('[] :=> schemas) '[ 'NotNull 'PGtext ] '[]
287−deleteMigration = deleteFrom_ (#migrations ! #schema_migrations) (#name .== param @1)
287+deleteMigration = deleteFrom_ (#migrations ! #schema_migrations) NoUsing (#name .== param @1)
288
288
289-- | Selects a `Migration` from the `MigrationsTable`, returning
289-- | Selects a `Migration` from the `MigrationsTable`, returning
290-- the time at which it was executed.
290-- the time at which it was executed.