@@ -31,7 +31,7 @@ Squeal expressions are the atoms used to build statements.
3131
3232module Squeal.PostgreSQL.Expression
3333 ( -- * Expression
34− Expression (UnsafeExpression, renderExpression)
34+ Expression (..)
3535 , HasParameter (parameter)
3636 , param
3737 -- ** Null
@@ -142,8 +142,8 @@ module Squeal.PostgreSQL.Expression
142142 , every, everyDistinct
143143 , max_, maxDistinct, min_, minDistinct
144144 -- * Types
145− , TypeExpression (UnsafeTypeExpression, renderTypeExpression)
146− , PGTyped (pgtype)
145+ , TypeExpression (..)
146+ , PGTyped (..)
147147 , typedef
148148 , typetable
149149 , typeview
@@ -252,7 +252,7 @@ class KnownNat n => HasParameter
252252 -> Expression (commons :=> schemas) params grp from ty
253253 parameter ty = UnsafeExpression $ parenthesized $
254254 "$" <> renderNat @n <+> "::"
255− <+> renderTypeExpression ty
255+ <+> renderSQL ty
256256instance {-# OVERLAPPING #-} HasParameter 1 (ty1:tys) ty1
257257instance {-# OVERLAPPABLE #-} (KnownNat n, HasParameter (n-1) params ty)
258258 => HasParameter n (ty' : params) ty
@@ -357,7 +357,7 @@ null_ = UnsafeExpression "NULL"
357357notNull
358358 :: Expression db rels grouping params ('NotNull ty)
359359 -> Expression db rels grouping params ('Null ty)
360−notNull = UnsafeExpression . renderExpression
360+notNull = UnsafeExpression . renderSQL
361361
362362-- | return the leftmost value which is not NULL
363363--
@@ -371,7 +371,7 @@ coalesce
371371 -> Expression db params grp from ('NotNull ty)
372372coalesce nullxs notNullx = UnsafeExpression $
373373 "COALESCE" <> parenthesized (commaSeparated
374− ((renderExpression <$> nullxs) <> [renderExpression notNullx]))
374+ ((renderSQL <$> nullxs) <> [renderSQL notNullx]))
375375
376376-- | analagous to `Data.Maybe.fromMaybe` using @COALESCE@
377377--
@@ -390,7 +390,7 @@ isNull
390390 :: Expression db params grp from ('Null ty)
391391 -- ^ possibly @NULL@
392392 -> Condition db params grp from
393−isNull x = UnsafeExpression $ renderExpression x <+> "IS NULL"
393+isNull x = UnsafeExpression $ renderSQL x <+> "IS NULL"
394394
395395-- | >>> printSQL $ null_ & isNotNull
396396-- NULL IS NOT NULL
@@ -398,7 +398,7 @@ isNotNull
398398 :: Expression db params grp from ('Null ty)
399399 -- ^ possibly @NULL@
400400 -> Condition db params grp from
401−isNotNull x = UnsafeExpression $ renderExpression x <+> "IS NOT NULL"
401+isNotNull x = UnsafeExpression $ renderSQL x <+> "IS NOT NULL"
402402
403403-- | analagous to `maybe` using @IS NULL@
404404--
@@ -413,7 +413,7 @@ matchNull
413413 -> Expression db params grp from ('Null ty)
414414 -> Expression db params grp from (nullty)
415415matchNull y f x = ifThenElse (isNull x) y
416− (f (UnsafeExpression (renderExpression x)))
416+ (f (UnsafeExpression (renderSQL x)))
417417
418418{-| right inverse to `fromNull`, if its arguments are equal then
419419`nullIf` gives @NULL@.
@@ -430,7 +430,7 @@ nullIf
430430 -- ^ @NULL@ is absent
431431 -> Expression db params grp from ('Null ty)
432432nullIf x y = UnsafeExpression $ "NULL IF" <+> parenthesized
433− (renderExpression x <> ", " <> renderExpression y)
433+ (renderSQL x <> ", " <> renderSQL y)
434434
435435-- | >>> printSQL $ array [null_, false, true]
436436-- ARRAY[NULL, FALSE, TRUE]
@@ -439,7 +439,7 @@ array
439439 -- ^ array elements
440440 -> Expression db params grp from (nullity ('PGvararray ty))
441441array xs = UnsafeExpression $
442− "ARRAY[" <> commaSeparated (renderExpression <$> xs) <> "]"
442+ "ARRAY[" <> commaSeparated (renderSQL <$> xs) <> "]"
443443
444444-- | >>> printSQL $ array [null_, false, true] & index 2
445445-- (ARRAY[NULL, FALSE, TRUE])[2]
@@ -448,7 +448,7 @@ index
448448 -> Expression db params grp from (nullity ('PGvararray ty)) -- ^ array
449449 -> Expression db params grp from (NullifyType ty)
450450index n expr = UnsafeExpression $
451− parenthesized (renderExpression expr) <> "[" <> fromString (show n) <> "]"
451+ parenthesized (renderSQL expr) <> "[" <> fromString (show n) <> "]"
452452
453453instance (KnownSymbol label, label `In` labels) => IsPGlabel label
454454 (Expression db params grp from (nullity ('PGenum labels))) where
@@ -472,7 +472,7 @@ row
472472 -- ^ zero or more expressions for the row field values
473473 -> Expression db params grp from (nullity ('PGcomposite row))
474474row exprs = UnsafeExpression $ "ROW" <> parenthesized
475− (renderCommaSeparated (\ (expr `As` _) -> renderExpression expr) exprs)
475+ (renderCommaSeparated (\ (expr `As` _) -> renderSQL expr) exprs)
476476
477477-- | >>> :{
478478-- type Complex = 'PGcomposite
@@ -494,7 +494,7 @@ field
494494 -> Expression (commons :=> schemas) params grp from ('NotNull ('PGcomposite row))
495495 -> Expression (commons :=> schemas) params grp from ty
496496field td fld expr = UnsafeExpression $
497− parenthesized (renderExpression expr <> "::" <> renderQualifiedAlias td)
497+ parenthesized (renderSQL expr <> "::" <> renderQualifiedAlias td)
498498 <> "." <> renderSQL fld
499499
500500instance Semigroup
@@ -516,7 +516,7 @@ greatest
516516 -- ^ or more
517517 -> Expression db params grp from (nullty)
518518greatest x xs = UnsafeExpression $ "GREATEST("
519− <> commaSeparated (renderExpression <$> (x:xs)) <> ")"
519+ <> commaSeparated (renderSQL <$> (x:xs)) <> ")"
520520
521521-- | >>> printSQL $ least currentTimestamp [null_]
522522-- LEAST(CURRENT_TIMESTAMP, NULL)
@@ -527,7 +527,7 @@ least
527527 -- ^ or more
528528 -> Expression db params grp from (nullty)
529529least x xs = UnsafeExpression $ "LEAST("
530− <> commaSeparated (renderExpression <$> (x:xs)) <> ")"
530+ <> commaSeparated (renderSQL <$> (x:xs)) <> ")"
531531
532532-- | >>> printSQL $ unsafeBinaryOp "OR" true false
533533-- (TRUE OR FALSE)
@@ -538,7 +538,7 @@ unsafeBinaryOp
538538 -> Expression db params grp from (ty1)
539539 -> Expression db params grp from (ty2)
540540unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
541− renderExpression x <+> op <+> renderExpression y
541+ renderSQL x <+> op <+> renderSQL y
542542
543543-- | >>> printSQL $ unsafeUnaryOp "NOT" true
544544-- (NOT TRUE)
@@ -548,7 +548,7 @@ unsafeUnaryOp
548548 -> Expression db params grp from (ty0)
549549 -> Expression db params grp from (ty1)
550550unsafeUnaryOp op x = UnsafeExpression $ parenthesized $
551− op <+> renderExpression x
551+ op <+> renderSQL x
552552
553553-- | >>> printSQL $ unsafeFunction "f" true
554554-- f(TRUE)
@@ -558,7 +558,7 @@ unsafeFunction
558558 -> Expression db params grp from (xty)
559559 -> Expression db params grp from (yty)
560560unsafeFunction fun x = UnsafeExpression $
561− fun <> parenthesized (renderExpression x)
561+ fun <> parenthesized (renderSQL x)
562562
563563-- | Helper for defining variadic functions.
564564unsafeVariadicFunction
@@ -568,7 +568,7 @@ unsafeVariadicFunction
568568 -> NP (Expression db params grp from) elems
569569 -> Expression db params grp from ret
570570unsafeVariadicFunction fun x = UnsafeExpression $
571− fun <> parenthesized (commaSeparated (hcollapse (hmap (K . renderExpression) x)))
571+ fun <> parenthesized (commaSeparated (hcollapse (hmap (K . renderSQL) x)))
572572
573573instance ty `In` PGNum
574574 => Num (Expression db params grp from (nullity ty)) where
@@ -594,7 +594,7 @@ instance (ty `In` PGNum, ty `In` PGFloating) => Floating
594594 log = unsafeFunction "ln"
595595 sqrt = unsafeFunction "sqrt"
596596 b ** x = UnsafeExpression $
597− "power(" <> renderExpression b <> ", " <> renderExpression x <> ")"
597+ "power(" <> renderSQL b <> ", " <> renderSQL x <> ")"
598598 logBase b y = log y / log b
599599 sin = unsafeFunction "sin"
600600 cos = unsafeFunction "cos"
@@ -624,7 +624,7 @@ atan2_
624624 -- ^ denominator
625625 -> Expression db params grp from (nullity float)
626626atan2_ y x = UnsafeExpression $
627− "atan2(" <> renderExpression y <> ", " <> renderExpression x <> ")"
627+ "atan2(" <> renderSQL y <> ", " <> renderSQL x <> ")"
628628
629629-- When a `cast` is applied to an `Expression` of a known type, it
630630-- represents a run-time type conversion. The cast will succeed only if a
@@ -639,7 +639,7 @@ cast
639639 -- ^ value to convert
640640 -> Expression (commons :=> schemas) params grp from ty1
641641cast ty x = UnsafeExpression $ parenthesized $
642− renderExpression x <+> "::" <+> renderTypeExpression ty
642+ renderSQL x <+> "::" <+> renderSQL ty
643643
644644-- | integer division, truncates the result
645645--
@@ -779,12 +779,12 @@ caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
779779 [ "CASE"
780780 , mconcat
781781 [ mconcat
782− [ " WHEN ", renderExpression when_
783− , " THEN ", renderExpression then_
782+ [ " WHEN ", renderSQL when_
783+ , " THEN ", renderSQL then_
784784 ]
785785 | (when_,then_) <- whenThens
786786 ]
787− , " ELSE ", renderExpression else_
787+ , " ELSE ", renderSQL else_
788788 , " END"
789789 ]
790790
@@ -1173,7 +1173,10 @@ unsafeRowFunction
11731173unsafeRowFunction =
11741174 (`appEndo` []) . hcfoldMap (Proxy :: Proxy Top)
11751175 (\(col `As` name) -> Endo $ \xs ->
1176− renderAliasString name : renderExpression col : xs)
1176+ renderAliasString name : renderSQL col : xs)
1177+ where
1178+ renderAliasString :: KnownSymbol alias => Alias alias -> ByteString
1179+ renderAliasString = singleQuotedText . fromString . symbolVal
11771180
11781181-- | Builds a possibly-heterogeneously-typed JSON object out of a variadic
11791182-- argument list. The elements of the argument list must alternate between text
@@ -1381,7 +1384,7 @@ unsafeAggregate
13811384 -> Expression db from 'Ungrouped params (xty)
13821385 -> Expression db from ('Grouped bys) params (yty)
13831386unsafeAggregate fun x = UnsafeExpression $ mconcat
1384− [fun, "(", renderExpression x, ")"]
1387+ [fun, "(", renderSQL x, ")"]
13851388
13861389-- | escape hatch to define aggregate functions over distinct values
13871390unsafeAggregateDistinct
@@ -1389,7 +1392,7 @@ unsafeAggregateDistinct
13891392 -> Expression db from 'Ungrouped params (xty)
13901393 -> Expression db from ('Grouped bys) params (yty)
13911394unsafeAggregateDistinct fun x = UnsafeExpression $ mconcat
1392− [fun, "(DISTINCT ", renderExpression x, ")"]
1395+ [fun, "(DISTINCT ", renderSQL x, ")"]
13931396
13941397-- | >>> :{
13951398-- let
@@ -1626,6 +1629,8 @@ type expressions
16261629newtype TypeExpression (schemas :: SchemasType) (ty :: NullityType)
16271630 = UnsafeTypeExpression { renderTypeExpression :: ByteString }
16281631 deriving (GHC.Generic,Show,Eq,Ord,NFData)
1632+instance RenderSQL (TypeExpression schemas ty) where
1633+ renderSQL = renderTypeExpression
16291634
16301635-- | The enum or composite type in a `Typedef` can be expressed by its alias.
16311636typedef
@@ -1730,17 +1735,17 @@ jsonb = UnsafeTypeExpression "jsonb"
17301735vararray
17311736 :: TypeExpression schemas pg
17321737 -> TypeExpression schemas (nullity ('PGvararray pg))
1733−vararray ty = UnsafeTypeExpression $ renderTypeExpression ty <> "[]"
1738+vararray ty = UnsafeTypeExpression $ renderSQL ty <> "[]"
17341739-- | fixed length array
17351740--
1736−-- >>> renderTypeExpression (fixarray @2 json)
1741+-- >>> renderSQL (fixarray @2 json)
17371742-- "json[2]"
17381743fixarray
17391744 :: forall n schemas nullity pg. KnownNat n
17401745 => TypeExpression schemas pg
17411746 -> TypeExpression schemas (nullity ('PGfixarray n pg))
17421747fixarray ty = UnsafeTypeExpression $
1743− renderTypeExpression ty <> "[" <> renderNat @n <> "]"
1748+ renderSQL ty <> "[" <> renderNat @n <> "]"
17441749
17451750-- | `pgtype` is a demoted version of a `PGType`
17461751class PGTyped schemas (ty :: NullityType) where