@@ -47,7 +47,7 @@ module Squeal.PostgreSQL.Query
4747 , except
4848 , exceptAll
4949 -- ** With
50− -- , With (with)
50+ , With (with)
5151 , CommonTableExpression (..)
5252 , renderCommonTableExpression
5353 , renderCommonTableExpressions
@@ -1362,35 +1362,43 @@ rowGteAny = unsafeRowSubqueryExpression ">= ANY"
13621362-- | A `CommonTableExpression` is an auxiliary statement in a `with` clause.
13631363data CommonTableExpression statement
13641364 (params :: [NullityType])
1365+ (db :: DBType)
13651366 (schema0 :: SchemaType)
13661367 (schema1 :: SchemaType) where
13671368 CommonTableExpression
1368− :: Aliased (statement schema params) (alias ::: cte)
1369+ :: Has "public" db schema
1370+ => Aliased (statement db params) (alias ::: cte)
13691371 -> CommonTableExpression
1370− statement params schema (alias ::: 'View cte ': schema)
1371−instance (KnownSymbol alias, schema1 ~ (alias ::: 'View cte ': schema))
1372− => Aliasable alias
1373− (statement schema params cte)
1374− (CommonTableExpression statement params schema schema1) where
1375− statement `as` alias = CommonTableExpression (statement `as` alias)
1376−instance (KnownSymbol alias, schema1 ~ (alias ::: 'View cte ': schema))
1377− => Aliasable alias (statement schema params cte)
1378− (AlignedList (CommonTableExpression statement params) schema schema1) where
1379− statement `as` alias = single (statement `as` alias)
1372+ statement params db schema (alias ::: 'View cte ': schema)
1373+instance
1374+ ( KnownSymbol vw
1375+ , Has "public" db schema
1376+ , schema1 ~ (vw ::: 'View cte ': schema)
1377+ ) => Aliasable vw
1378+ (statement db params cte)
1379+ (CommonTableExpression statement params db schema schema1) where
1380+ statement `as` vw = CommonTableExpression (statement `as` vw)
1381+instance
1382+ ( KnownSymbol vw
1383+ , Has "public" db schema
1384+ , schema1 ~ (vw ::: 'View cte ': schema)
1385+ ) => Aliasable vw (statement db params cte)
1386+ (AlignedList (CommonTableExpression statement params db) schema schema1) where
1387+ statement `as` vw = single (statement `as` vw)
13801388
13811389-- | render a `CommonTableExpression`.
13821390renderCommonTableExpression
1383− :: (forall sch ps row. statement ps sch row -> ByteString) -- ^ render statement
1384− -> CommonTableExpression statement params schema0 schema1 -> ByteString
1391+ :: (forall db ps row. statement db ps row -> ByteString) -- ^ render statement
1392+ -> CommonTableExpression statement params db0 schema0 schema1 -> ByteString
13851393renderCommonTableExpression renderStatement
13861394 (CommonTableExpression (statement `As` alias)) =
13871395 renderAlias alias <+> "AS" <+> parenthesized (renderStatement statement)
13881396
13891397-- | render a non-empty `AlignedList` of `CommonTableExpression`s.
13901398renderCommonTableExpressions
1391− :: (forall sch ps row. statement ps sch row -> ByteString) -- ^ render statement
1392− -> CommonTableExpression statement params schema0 schema1
1393− -> AlignedList (CommonTableExpression statement params) schema1 schema2
1399+ :: (forall db ps row. statement db ps row -> ByteString) -- ^ render statement
1400+ -> CommonTableExpression statement params db0 schema0 schema1
1401+ -> AlignedList (CommonTableExpression statement params db0) schema1 schema2
13941402 -> ByteString
13951403renderCommonTableExpressions renderStatement cte ctes =
13961404 renderCommonTableExpression renderStatement cte <> case ctes of
@@ -1398,22 +1406,19 @@ renderCommonTableExpressions renderStatement cte ctes =
13981406 cte' :>> ctes' -> "," <+>
13991407 renderCommonTableExpressions renderStatement cte' ctes'
14001408
1401−{---------------FIX WITH
1402−
14031409-- | `with` provides a way to write auxiliary statements for use in a larger query.
14041410-- These statements, referred to as `CommonTableExpression`s, can be thought of as
14051411-- defining temporary tables that exist just for one query.
14061412class With statement where
14071413 with
1408− :: AlignedList (CommonTableExpression statement params) schema0 schema1
1414+ :: Has "public" db0 schema0
1415+ => AlignedList (CommonTableExpression statement params db0) schema0 schema1
14091416 -- ^ common table expressions
1410− -> statement schema1 params row
1417+ -> statement (Alter "public" schema1 db0) params row
14111418 -- ^ larger query
1412− -> statement schema0 params row
1419+ -> statement db0 params row
14131420instance With Query where
1414− with Done query = query
1421+ with Done query = UnsafeQuery $ renderQuery query
14151422 with (cte :>> ctes) query = UnsafeQuery $
14161423 "WITH" <+> renderCommonTableExpressions renderQuery cte ctes
14171424 <+> renderQuery query
1418−
1419−-}