on dev help sign in
exists

and doctest

commit3906bfb945e8f3ce3fd1412673181a253b24abc9 authorEitan Chatav <eitan@Eitans-MacBook-Pro.local> dateWed 20 Feb 2019, 14:52 (8 years ago) parentf08c960
1 file changed+20
squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+20
squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+20
@@ -91,6 +91,7 @@ module Squeal.PostgreSQL.Query
9191 , GroupByClause (..)
9292 , HavingClause (..)
9393 -- * Subquery Expressions
94+ , exists
9495 , in_
9596 , rowIn
9697 , eqAll
@@ -321,6 +322,20 @@ let
321322in printSQL query
322323:}
323324SELECT "col1" AS "col1", rank() OVER (PARTITION BY "col1" ORDER BY "col2" ASC) AS "rank" FROM "tab" AS "tab"
325+
326+correlated subqueries
327+
328+>>> :{
329+let
330+ query :: Query '[] '[] (Public Schema) '[] '["col1" ::: 'NotNull 'PGint4]
331+ query =
332+ select #col1 (from (table (#tab `as` #t1))
333+ & where_ (exists (
334+ select Star (from (table (#tab `as` #t2))
335+ & where_ (#t2 ! #col2 .== #t1 ! #col1)))))
336+in printSQL query
337+:}
338+SELECT "col1" AS "col1" FROM "tab" AS "t1" WHERE EXISTS (SELECT * FROM "tab" AS "t2" WHERE ("t2"."col2" = "t1"."col1"))
324339-}
325340newtype Query
326341 (outer :: FromType)
@@ -1075,6 +1090,11 @@ unsafeRowSubqueryExpression
10751090unsafeRowSubqueryExpression op xs q = UnsafeExpression $
10761091 renderSQL (row xs) <+> op <+> parenthesized (renderSQL q)
10771092
1093+exists
1094+ :: Query (Join outer from) commons schemas params row
1095+ -> Condition outer grp commons schemas params from
1096+exists query = UnsafeExpression $ "EXISTS" <+> parenthesized (renderSQL query)
1097+
10781098-- | The right-hand side is a sub`Query`, which must return exactly one column.
10791099-- The left-hand expression is evaluated and compared to each row of the
10801100-- sub`Query` result. The result of `in_` is `true` if any equal subquery row is found.