@@ -295,33 +295,45 @@ newtype Expression
295instance RenderSQL (Expression outer grp commons schemas params from ty) where
295instance RenderSQL (Expression outer grp commons schemas params from ty) where
296 renderSQL = renderExpression
296 renderSQL = renderExpression
298+-- | An `Expr` is a closed `Expression`.
298type Expr x
299type Expr x
299 = forall outer grp commons schemas params from
300 = forall outer grp commons schemas params from
300 . Expression outer grp commons schemas params from x
301 . Expression outer grp commons schemas params from x
302+ -- ^ cannot reference aliases
302type Operator x1 x2 y
304type Operator x1 x2 y
303 = forall outer grp commons schemas params from
305 = forall outer grp commons schemas params from
304 . Expression outer grp commons schemas params from x1
306 . Expression outer grp commons schemas params from x1
305 -> Expression outer grp commons schemas params from x2
308 -> Expression outer grp commons schemas params from x2
306 -> Expression outer grp commons schemas params from y
310 -> Expression outer grp commons schemas params from y
308type (:-->) x y
313type (:-->) x y
309 = forall outer grp commons schemas params from
314 = forall outer grp commons schemas params from
310 . Expression outer grp commons schemas params from x
315 . Expression outer grp commons schemas params from x
311 -> Expression outer grp commons schemas params from y
317 -> Expression outer grp commons schemas params from y
313type FunctionHet xs y
320type FunctionHet xs y
314 = forall outer grp commons schemas params from
321 = forall outer grp commons schemas params from
315 . NP (Expression outer grp commons schemas params from) xs
322 . NP (Expression outer grp commons schemas params from) xs
316 -> Expression outer grp commons schemas params from y
324 -> Expression outer grp commons schemas params from y
318−type FunctionHom x y
327+type FunctionHom x0 x1 y
319 = forall outer grp commons schemas params from
328 = forall outer grp commons schemas params from
320− . [Expression outer grp commons schemas params from x]
329+ . [Expression outer grp commons schemas params from x0]
321− -> Expression outer grp commons schemas params from x
331+ -> Expression outer grp commons schemas params from x1
332+ -- ^ must have at least 1 input
322 -> Expression outer grp commons schemas params from y
333 -> Expression outer grp commons schemas params from y
324−unsafeFunctionHom :: ByteString -> FunctionHom x y
336+unsafeFunctionHom :: ByteString -> FunctionHom x0 x1 y
325unsafeFunctionHom fun xs x = UnsafeExpression $ fun <> parenthesized
337unsafeFunctionHom fun xs x = UnsafeExpression $ fun <> parenthesized
326 (commaSeparated (renderSQL <$> xs) <> ", " <> renderSQL x)
338 (commaSeparated (renderSQL <$> xs) <> ", " <> renderSQL x)
@@ -479,12 +491,7 @@ notNull = UnsafeExpression . renderSQL
480-- >>> printSQL $ coalesce [null_, true] false
492-- >>> printSQL $ coalesce [null_, true] false
481-- COALESCE(NULL, TRUE, FALSE)
493-- COALESCE(NULL, TRUE, FALSE)
482−coalesce
494+coalesce :: FunctionHom ('Null ty) ('NotNull ty) ('NotNull ty)
483− :: [Expression outer grp commons schemas params from ('Null ty)]
484− -- ^ @NULL@s may be present
485− -> Expression outer grp commons schemas params from ('NotNull ty)
486− -- ^ @NULL@ is absent
487− -> Expression outer grp commons schemas params from ('NotNull ty)
488coalesce nullxs notNullx = UnsafeExpression $
495coalesce nullxs notNullx = UnsafeExpression $
489 "COALESCE" <> parenthesized (commaSeparated
496 "COALESCE" <> parenthesized (commaSeparated
490 ((renderSQL <$> nullxs) <> [renderSQL notNullx]))
497 ((renderSQL <$> nullxs) <> [renderSQL notNullx]))
@@ -502,18 +509,12 @@ fromNull notNullx nullx = coalesce [nullx] notNullx
503-- | >>> printSQL $ null_ & isNull
510-- | >>> printSQL $ null_ & isNull
504-- NULL IS NULL
511-- NULL IS NULL
505−isNull
512+isNull :: 'Null ty :--> null 'PGbool
506− :: Expression outer grp commons schemas params from ('Null ty)
507− -- ^ possibly @NULL@
508− -> Condition outer grp commons schemas params from
509isNull x = UnsafeExpression $ renderSQL x <+> "IS NULL"
513isNull x = UnsafeExpression $ renderSQL x <+> "IS NULL"
511-- | >>> printSQL $ null_ & isNotNull
515-- | >>> printSQL $ null_ & isNotNull
512-- NULL IS NOT NULL
516-- NULL IS NOT NULL
513−isNotNull
517+isNotNull :: 'Null ty :--> null 'PGbool
514− :: Expression outer grp commons schemas params from ('Null ty)
515− -- ^ possibly @NULL@
516− -> Condition outer grp commons schemas params from
517isNotNull x = UnsafeExpression $ renderSQL x <+> "IS NOT NULL"
518isNotNull x = UnsafeExpression $ renderSQL x <+> "IS NOT NULL"
519-- | analagous to `maybe` using @IS NULL@
520-- | analagous to `maybe` using @IS NULL@
@@ -535,18 +536,12 @@ matchNull y f x = ifThenElse (isNull x) y
535`nullIf` gives @NULL@.
536`nullIf` gives @NULL@.
537>>> :set -XTypeApplications -XDataKinds
538>>> :set -XTypeApplications -XDataKinds
538−>>> let expr = nullIf false (param @1) :: Expression outer grp commons schemas '[ 'NotNull 'PGbool] from ('Null 'PGbool)
539+>>> let expr = nullIf (false *: param @1) :: Expression outer grp commons schemas '[ 'NotNull 'PGbool] from ('Null 'PGbool)
539>>> printSQL expr
540>>> printSQL expr
540−NULL IF (FALSE, ($1 :: bool))
541+NULLIF(FALSE, ($1 :: bool))
542−nullIf
543+nullIf :: FunctionHet '[ 'NotNull ty, 'NotNull ty] ('Null ty)
543− :: Expression outer grp commons schemas params from ('NotNull ty)
544+nullIf = functionHet "NULLIF"
544− -- ^ @NULL@ is absent
545− -> Expression outer grp commons schemas params from ('NotNull ty)
546− -- ^ @NULL@ is absent
547− -> Expression outer grp commons schemas params from ('Null ty)
548−nullIf x y = UnsafeExpression $ "NULL IF" <+> parenthesized
549− (renderSQL x <> ", " <> renderSQL y)
551-- | >>> printSQL $ array [null_, false, true]
546-- | >>> printSQL $ array [null_, false, true]
552-- ARRAY[NULL, FALSE, TRUE]
547-- ARRAY[NULL, FALSE, TRUE]
@@ -628,12 +623,12 @@ instance Monoid
628-- | >>> let expr = greatest [param @1] currentTimestamp :: Expression outer grp commons schemas '[ 'NotNull 'PGtimestamptz] from ('NotNull 'PGtimestamptz)
623-- | >>> let expr = greatest [param @1] currentTimestamp :: Expression outer grp commons schemas '[ 'NotNull 'PGtimestamptz] from ('NotNull 'PGtimestamptz)
629-- >>> printSQL expr
624-- >>> printSQL expr
630-- GREATEST(($1 :: timestamp with time zone), CURRENT_TIMESTAMP)
625-- GREATEST(($1 :: timestamp with time zone), CURRENT_TIMESTAMP)
631−greatest :: FunctionHom ty ty
626+greatest :: FunctionHom ty ty ty
632greatest = unsafeFunctionHom "GREATEST"
627greatest = unsafeFunctionHom "GREATEST"
634-- | >>> printSQL $ least [null_] currentTimestamp
629-- | >>> printSQL $ least [null_] currentTimestamp
635-- LEAST(NULL, CURRENT_TIMESTAMP)
630-- LEAST(NULL, CURRENT_TIMESTAMP)
636−least :: FunctionHom ty ty
631+least :: FunctionHom ty ty ty
637least = unsafeFunctionHom "LEAST"
632least = unsafeFunctionHom "LEAST"
639-- | >>> printSQL $ unsafeBinaryOp "OR" true false
634-- | >>> printSQL $ unsafeBinaryOp "OR" true false