@@ -31,7 +31,7 @@ Squeal expressions are the atoms used to build statements.
32module Squeal.PostgreSQL.Expression
32module Squeal.PostgreSQL.Expression
33 ( -- * Expression
33 ( -- * Expression
34− Expression (UnsafeExpression, renderExpression)
34+ Expression (..)
35 , HasParameter (parameter)
35 , HasParameter (parameter)
37 -- ** Null
37 -- ** Null
@@ -142,8 +142,8 @@ module Squeal.PostgreSQL.Expression
142 , every, everyDistinct
142 , every, everyDistinct
143 , max_, maxDistinct, min_, minDistinct
143 , max_, maxDistinct, min_, minDistinct
144 -- * Types
144 -- * Types
145− , TypeExpression (UnsafeTypeExpression, renderTypeExpression)
145+ , TypeExpression (..)
146− , PGTyped (pgtype)
146+ , PGTyped (..)
147 , typedef
147 , typedef
148 , typetable
148 , typetable
149 , typeview
149 , typeview
@@ -252,7 +252,7 @@ class KnownNat n => HasParameter
252 -> Expression (commons :=> schemas) params grp from ty
252 -> Expression (commons :=> schemas) params grp from ty
253 parameter ty = UnsafeExpression $ parenthesized $
253 parameter ty = UnsafeExpression $ parenthesized $
254 "$" <> renderNat @n <+> "::"
254 "$" <> renderNat @n <+> "::"
255− <+> renderTypeExpression ty
255+ <+> renderSQL ty
256instance {-# OVERLAPPING #-} HasParameter 1 (ty1:tys) ty1
256instance {-# OVERLAPPING #-} HasParameter 1 (ty1:tys) ty1
257instance {-# OVERLAPPABLE #-} (KnownNat n, HasParameter (n-1) params ty)
257instance {-# OVERLAPPABLE #-} (KnownNat n, HasParameter (n-1) params ty)
258 => HasParameter n (ty' : params) ty
258 => HasParameter n (ty' : params) ty
@@ -357,7 +357,7 @@ null_ = UnsafeExpression "NULL"
358 :: Expression db rels grouping params ('NotNull ty)
358 :: Expression db rels grouping params ('NotNull ty)
359 -> Expression db rels grouping params ('Null ty)
359 -> Expression db rels grouping params ('Null ty)
360−notNull = UnsafeExpression . renderExpression
360+notNull = UnsafeExpression . renderSQL
362-- | return the leftmost value which is not NULL
362-- | return the leftmost value which is not NULL
@@ -371,7 +371,7 @@ coalesce
371 -> Expression db params grp from ('NotNull ty)
371 -> Expression db params grp from ('NotNull ty)
372coalesce nullxs notNullx = UnsafeExpression $
372coalesce nullxs notNullx = UnsafeExpression $
373 "COALESCE" <> parenthesized (commaSeparated
373 "COALESCE" <> parenthesized (commaSeparated
374− ((renderExpression <$> nullxs) <> [renderExpression notNullx]))
374+ ((renderSQL <$> nullxs) <> [renderSQL notNullx]))
376-- | analagous to `Data.Maybe.fromMaybe` using @COALESCE@
376-- | analagous to `Data.Maybe.fromMaybe` using @COALESCE@
@@ -390,7 +390,7 @@ isNull
390 :: Expression db params grp from ('Null ty)
390 :: Expression db params grp from ('Null ty)
391 -- ^ possibly @NULL@
391 -- ^ possibly @NULL@
392 -> Condition db params grp from
392 -> Condition db params grp from
393−isNull x = UnsafeExpression $ renderExpression x <+> "IS NULL"
393+isNull x = UnsafeExpression $ renderSQL x <+> "IS NULL"
395-- | >>> printSQL $ null_ & isNotNull
395-- | >>> printSQL $ null_ & isNotNull
396-- NULL IS NOT NULL
396-- NULL IS NOT NULL
@@ -398,7 +398,7 @@ isNotNull
398 :: Expression db params grp from ('Null ty)
398 :: Expression db params grp from ('Null ty)
399 -- ^ possibly @NULL@
399 -- ^ possibly @NULL@
400 -> Condition db params grp from
400 -> Condition db params grp from
401−isNotNull x = UnsafeExpression $ renderExpression x <+> "IS NOT NULL"
401+isNotNull x = UnsafeExpression $ renderSQL x <+> "IS NOT NULL"
403-- | analagous to `maybe` using @IS NULL@
403-- | analagous to `maybe` using @IS NULL@
@@ -413,7 +413,7 @@ matchNull
413 -> Expression db params grp from ('Null ty)
413 -> Expression db params grp from ('Null ty)
414 -> Expression db params grp from (nullty)
414 -> Expression db params grp from (nullty)
415matchNull y f x = ifThenElse (isNull x) y
415matchNull y f x = ifThenElse (isNull x) y
416− (f (UnsafeExpression (renderExpression x)))
416+ (f (UnsafeExpression (renderSQL x)))
418{-| right inverse to `fromNull`, if its arguments are equal then
418{-| right inverse to `fromNull`, if its arguments are equal then
419`nullIf` gives @NULL@.
419`nullIf` gives @NULL@.
@@ -430,7 +430,7 @@ nullIf
430 -- ^ @NULL@ is absent
430 -- ^ @NULL@ is absent
431 -> Expression db params grp from ('Null ty)
431 -> Expression db params grp from ('Null ty)
432nullIf x y = UnsafeExpression $ "NULL IF" <+> parenthesized
432nullIf x y = UnsafeExpression $ "NULL IF" <+> parenthesized
433− (renderExpression x <> ", " <> renderExpression y)
433+ (renderSQL x <> ", " <> renderSQL y)
435-- | >>> printSQL $ array [null_, false, true]
435-- | >>> printSQL $ array [null_, false, true]
436-- ARRAY[NULL, FALSE, TRUE]
436-- ARRAY[NULL, FALSE, TRUE]
@@ -439,7 +439,7 @@ array
439 -- ^ array elements
439 -- ^ array elements
440 -> Expression db params grp from (nullity ('PGvararray ty))
440 -> Expression db params grp from (nullity ('PGvararray ty))
441array xs = UnsafeExpression $
441array xs = UnsafeExpression $
442− "ARRAY[" <> commaSeparated (renderExpression <$> xs) <> "]"
442+ "ARRAY[" <> commaSeparated (renderSQL <$> xs) <> "]"
444-- | >>> printSQL $ array [null_, false, true] & index 2
444-- | >>> printSQL $ array [null_, false, true] & index 2
445-- (ARRAY[NULL, FALSE, TRUE])[2]
445-- (ARRAY[NULL, FALSE, TRUE])[2]
@@ -448,7 +448,7 @@ index
448 -> Expression db params grp from (nullity ('PGvararray ty)) -- ^ array
448 -> Expression db params grp from (nullity ('PGvararray ty)) -- ^ array
449 -> Expression db params grp from (NullifyType ty)
449 -> Expression db params grp from (NullifyType ty)
450index n expr = UnsafeExpression $
450index n expr = UnsafeExpression $
451− parenthesized (renderExpression expr) <> "[" <> fromString (show n) <> "]"
451+ parenthesized (renderSQL expr) <> "[" <> fromString (show n) <> "]"
453instance (KnownSymbol label, label `In` labels) => IsPGlabel label
453instance (KnownSymbol label, label `In` labels) => IsPGlabel label
454 (Expression db params grp from (nullity ('PGenum labels))) where
454 (Expression db params grp from (nullity ('PGenum labels))) where
@@ -472,7 +472,7 @@ row
472 -- ^ zero or more expressions for the row field values
472 -- ^ zero or more expressions for the row field values
473 -> Expression db params grp from (nullity ('PGcomposite row))
473 -> Expression db params grp from (nullity ('PGcomposite row))
474row exprs = UnsafeExpression $ "ROW" <> parenthesized
474row exprs = UnsafeExpression $ "ROW" <> parenthesized
475− (renderCommaSeparated (\ (expr `As` _) -> renderExpression expr) exprs)
475+ (renderCommaSeparated (\ (expr `As` _) -> renderSQL expr) exprs)
477-- | >>> :{
477-- | >>> :{
478-- type Complex = 'PGcomposite
478-- type Complex = 'PGcomposite
@@ -494,7 +494,7 @@ field
494 -> Expression (commons :=> schemas) params grp from ('NotNull ('PGcomposite row))
494 -> Expression (commons :=> schemas) params grp from ('NotNull ('PGcomposite row))
495 -> Expression (commons :=> schemas) params grp from ty
495 -> Expression (commons :=> schemas) params grp from ty
496field td fld expr = UnsafeExpression $
496field td fld expr = UnsafeExpression $
497− parenthesized (renderExpression expr <> "::" <> renderQualifiedAlias td)
497+ parenthesized (renderSQL expr <> "::" <> renderQualifiedAlias td)
498 <> "." <> renderSQL fld
498 <> "." <> renderSQL fld
500instance Semigroup
500instance Semigroup
@@ -516,7 +516,7 @@ greatest
516 -- ^ or more
516 -- ^ or more
517 -> Expression db params grp from (nullty)
517 -> Expression db params grp from (nullty)
518greatest x xs = UnsafeExpression $ "GREATEST("
518greatest x xs = UnsafeExpression $ "GREATEST("
519− <> commaSeparated (renderExpression <$> (x:xs)) <> ")"
519+ <> commaSeparated (renderSQL <$> (x:xs)) <> ")"
521-- | >>> printSQL $ least currentTimestamp [null_]
521-- | >>> printSQL $ least currentTimestamp [null_]
522-- LEAST(CURRENT_TIMESTAMP, NULL)
522-- LEAST(CURRENT_TIMESTAMP, NULL)
@@ -527,7 +527,7 @@ least
527 -- ^ or more
527 -- ^ or more
528 -> Expression db params grp from (nullty)
528 -> Expression db params grp from (nullty)
529least x xs = UnsafeExpression $ "LEAST("
529least x xs = UnsafeExpression $ "LEAST("
530− <> commaSeparated (renderExpression <$> (x:xs)) <> ")"
530+ <> commaSeparated (renderSQL <$> (x:xs)) <> ")"
532-- | >>> printSQL $ unsafeBinaryOp "OR" true false
532-- | >>> printSQL $ unsafeBinaryOp "OR" true false
533-- (TRUE OR FALSE)
533-- (TRUE OR FALSE)
@@ -538,7 +538,7 @@ unsafeBinaryOp
538 -> Expression db params grp from (ty1)
538 -> Expression db params grp from (ty1)
539 -> Expression db params grp from (ty2)
539 -> Expression db params grp from (ty2)
540unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
540unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
541− renderExpression x <+> op <+> renderExpression y
541+ renderSQL x <+> op <+> renderSQL y
543-- | >>> printSQL $ unsafeUnaryOp "NOT" true
543-- | >>> printSQL $ unsafeUnaryOp "NOT" true
544-- (NOT TRUE)
544-- (NOT TRUE)
@@ -548,7 +548,7 @@ unsafeUnaryOp
548 -> Expression db params grp from (ty0)
548 -> Expression db params grp from (ty0)
549 -> Expression db params grp from (ty1)
549 -> Expression db params grp from (ty1)
550unsafeUnaryOp op x = UnsafeExpression $ parenthesized $
550unsafeUnaryOp op x = UnsafeExpression $ parenthesized $
551− op <+> renderExpression x
551+ op <+> renderSQL x
553-- | >>> printSQL $ unsafeFunction "f" true
553-- | >>> printSQL $ unsafeFunction "f" true
554-- f(TRUE)
554-- f(TRUE)
@@ -558,7 +558,7 @@ unsafeFunction
558 -> Expression db params grp from (xty)
558 -> Expression db params grp from (xty)
559 -> Expression db params grp from (yty)
559 -> Expression db params grp from (yty)
560unsafeFunction fun x = UnsafeExpression $
560unsafeFunction fun x = UnsafeExpression $
561− fun <> parenthesized (renderExpression x)
561+ fun <> parenthesized (renderSQL x)
563-- | Helper for defining variadic functions.
563-- | Helper for defining variadic functions.
564unsafeVariadicFunction
564unsafeVariadicFunction
@@ -568,7 +568,7 @@ unsafeVariadicFunction
568 -> NP (Expression db params grp from) elems
568 -> NP (Expression db params grp from) elems
569 -> Expression db params grp from ret
569 -> Expression db params grp from ret
570unsafeVariadicFunction fun x = UnsafeExpression $
570unsafeVariadicFunction fun x = UnsafeExpression $
571− fun <> parenthesized (commaSeparated (hcollapse (hmap (K . renderExpression) x)))
571+ fun <> parenthesized (commaSeparated (hcollapse (hmap (K . renderSQL) x)))
573instance ty `In` PGNum
573instance ty `In` PGNum
574 => Num (Expression db params grp from (nullity ty)) where
574 => Num (Expression db params grp from (nullity ty)) where
@@ -594,7 +594,7 @@ instance (ty `In` PGNum, ty `In` PGFloating) => Floating
594 log = unsafeFunction "ln"
594 log = unsafeFunction "ln"
595 sqrt = unsafeFunction "sqrt"
595 sqrt = unsafeFunction "sqrt"
596 b ** x = UnsafeExpression $
596 b ** x = UnsafeExpression $
597− "power(" <> renderExpression b <> ", " <> renderExpression x <> ")"
597+ "power(" <> renderSQL b <> ", " <> renderSQL x <> ")"
598 logBase b y = log y / log b
598 logBase b y = log y / log b
599 sin = unsafeFunction "sin"
599 sin = unsafeFunction "sin"
600 cos = unsafeFunction "cos"
600 cos = unsafeFunction "cos"
@@ -624,7 +624,7 @@ atan2_
624 -- ^ denominator
624 -- ^ denominator
625 -> Expression db params grp from (nullity float)
625 -> Expression db params grp from (nullity float)
626atan2_ y x = UnsafeExpression $
626atan2_ y x = UnsafeExpression $
627− "atan2(" <> renderExpression y <> ", " <> renderExpression x <> ")"
627+ "atan2(" <> renderSQL y <> ", " <> renderSQL x <> ")"
629-- When a `cast` is applied to an `Expression` of a known type, it
629-- When a `cast` is applied to an `Expression` of a known type, it
630-- represents a run-time type conversion. The cast will succeed only if a
630-- represents a run-time type conversion. The cast will succeed only if a
@@ -639,7 +639,7 @@ cast
639 -- ^ value to convert
639 -- ^ value to convert
640 -> Expression (commons :=> schemas) params grp from ty1
640 -> Expression (commons :=> schemas) params grp from ty1
641cast ty x = UnsafeExpression $ parenthesized $
641cast ty x = UnsafeExpression $ parenthesized $
642− renderExpression x <+> "::" <+> renderTypeExpression ty
642+ renderSQL x <+> "::" <+> renderSQL ty
644-- | integer division, truncates the result
644-- | integer division, truncates the result
@@ -779,12 +779,12 @@ caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
780 , mconcat
780 , mconcat
781 [ mconcat
781 [ mconcat
782− [ " WHEN ", renderExpression when_
782+ [ " WHEN ", renderSQL when_
783− , " THEN ", renderExpression then_
783+ , " THEN ", renderSQL then_
785 | (when_,then_) <- whenThens
785 | (when_,then_) <- whenThens
787− , " ELSE ", renderExpression else_
787+ , " ELSE ", renderSQL else_
@@ -1173,7 +1173,10 @@ unsafeRowFunction
1173unsafeRowFunction =
1173unsafeRowFunction =
1174 (`appEndo` []) . hcfoldMap (Proxy :: Proxy Top)
1174 (`appEndo` []) . hcfoldMap (Proxy :: Proxy Top)
1175 (\(col `As` name) -> Endo $ \xs ->
1175 (\(col `As` name) -> Endo $ \xs ->
1176− renderAliasString name : renderExpression col : xs)
1176+ renderAliasString name : renderSQL col : xs)
1178+ renderAliasString :: KnownSymbol alias => Alias alias -> ByteString
1179+ renderAliasString = singleQuotedText . fromString . symbolVal
1178-- | Builds a possibly-heterogeneously-typed JSON object out of a variadic
1181-- | Builds a possibly-heterogeneously-typed JSON object out of a variadic
1179-- argument list. The elements of the argument list must alternate between text
1182-- argument list. The elements of the argument list must alternate between text
@@ -1381,7 +1384,7 @@ unsafeAggregate
1381 -> Expression db from 'Ungrouped params (xty)
1384 -> Expression db from 'Ungrouped params (xty)
1382 -> Expression db from ('Grouped bys) params (yty)
1385 -> Expression db from ('Grouped bys) params (yty)
1383unsafeAggregate fun x = UnsafeExpression $ mconcat
1386unsafeAggregate fun x = UnsafeExpression $ mconcat
1384− [fun, "(", renderExpression x, ")"]
1387+ [fun, "(", renderSQL x, ")"]
1386-- | escape hatch to define aggregate functions over distinct values
1389-- | escape hatch to define aggregate functions over distinct values
1387unsafeAggregateDistinct
1390unsafeAggregateDistinct
@@ -1389,7 +1392,7 @@ unsafeAggregateDistinct
1389 -> Expression db from 'Ungrouped params (xty)
1392 -> Expression db from 'Ungrouped params (xty)
1390 -> Expression db from ('Grouped bys) params (yty)
1393 -> Expression db from ('Grouped bys) params (yty)
1391unsafeAggregateDistinct fun x = UnsafeExpression $ mconcat
1394unsafeAggregateDistinct fun x = UnsafeExpression $ mconcat
1392− [fun, "(DISTINCT ", renderExpression x, ")"]
1395+ [fun, "(DISTINCT ", renderSQL x, ")"]
1394-- | >>> :{
1397-- | >>> :{
@@ -1626,6 +1629,8 @@ type expressions
1626newtype TypeExpression (schemas :: SchemasType) (ty :: NullityType)
1629newtype TypeExpression (schemas :: SchemasType) (ty :: NullityType)
1627 = UnsafeTypeExpression { renderTypeExpression :: ByteString }
1630 = UnsafeTypeExpression { renderTypeExpression :: ByteString }
1628 deriving (GHC.Generic,Show,Eq,Ord,NFData)
1631 deriving (GHC.Generic,Show,Eq,Ord,NFData)
1632+instance RenderSQL (TypeExpression schemas ty) where
1633+ renderSQL = renderTypeExpression
1630-- | The enum or composite type in a `Typedef` can be expressed by its alias.
1635-- | The enum or composite type in a `Typedef` can be expressed by its alias.
@@ -1730,17 +1735,17 @@ jsonb = UnsafeTypeExpression "jsonb"
1731 :: TypeExpression schemas pg
1736 :: TypeExpression schemas pg
1732 -> TypeExpression schemas (nullity ('PGvararray pg))
1737 -> TypeExpression schemas (nullity ('PGvararray pg))
1733−vararray ty = UnsafeTypeExpression $ renderTypeExpression ty <> "[]"
1738+vararray ty = UnsafeTypeExpression $ renderSQL ty <> "[]"
1734-- | fixed length array
1739-- | fixed length array
1736−-- >>> renderTypeExpression (fixarray @2 json)
1741+-- >>> renderSQL (fixarray @2 json)
1737-- "json[2]"
1742-- "json[2]"
1739 :: forall n schemas nullity pg. KnownNat n
1744 :: forall n schemas nullity pg. KnownNat n
1740 => TypeExpression schemas pg
1745 => TypeExpression schemas pg
1741 -> TypeExpression schemas (nullity ('PGfixarray n pg))
1746 -> TypeExpression schemas (nullity ('PGfixarray n pg))
1742fixarray ty = UnsafeTypeExpression $
1747fixarray ty = UnsafeTypeExpression $
1743− renderTypeExpression ty <> "[" <> renderNat @n <> "]"
1748+ renderSQL ty <> "[" <> renderNat @n <> "]"
1745-- | `pgtype` is a demoted version of a `PGType`
1750-- | `pgtype` is a demoted version of a `PGType`
1746class PGTyped schemas (ty :: NullityType) where
1751class PGTyped schemas (ty :: NullityType) where