on dev help sign in
more renderSQL
dbb403cEitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+7−7
@@ -331,7 +331,7 @@ unique
331 -- ^ specify subcolumns which together are unique for each row
331 -- ^ specify subcolumns which together are unique for each row
332 -> TableConstraintExpression sch tab schemas ('Unique aliases)
332 -> TableConstraintExpression sch tab schemas ('Unique aliases)
333unique columns = UnsafeTableConstraintExpression $
333unique columns = UnsafeTableConstraintExpression $
334− "UNIQUE" <+> parenthesized (commaSeparated (renderAliases columns))
334+ "UNIQUE" <+> parenthesized (renderSQL columns)
335
335
336{-| A `primaryKey` constraint indicates that a column, or group of columns,
336{-| A `primaryKey` constraint indicates that a column, or group of columns,
337can be used as a unique identifier for rows in the table.
337can be used as a unique identifier for rows in the table.
@@ -366,7 +366,7 @@ primaryKey
366 -- ^ specify the subcolumns which together form a primary key.
366 -- ^ specify the subcolumns which together form a primary key.
367 -> TableConstraintExpression sch tab schemas ('PrimaryKey aliases)
367 -> TableConstraintExpression sch tab schemas ('PrimaryKey aliases)
368primaryKey columns = UnsafeTableConstraintExpression $
368primaryKey columns = UnsafeTableConstraintExpression $
369− "PRIMARY KEY" <+> parenthesized (commaSeparated (renderAliases columns))
369+ "PRIMARY KEY" <+> parenthesized (renderSQL columns)
370
370
371{-| A `foreignKey` specifies that the values in a column
371{-| A `foreignKey` specifies that the values in a column
372(or a group of columns) must match the values appearing in some row of
372(or a group of columns) must match the values appearing in some row of
@@ -460,9 +460,9 @@ foreignKey
460 -> TableConstraintExpression sch child schemas
460 -> TableConstraintExpression sch child schemas
461 ('ForeignKey columns parent refcolumns)
461 ('ForeignKey columns parent refcolumns)
462foreignKey keys parent refs ondel onupd = UnsafeTableConstraintExpression $
462foreignKey keys parent refs ondel onupd = UnsafeTableConstraintExpression $
463− "FOREIGN KEY" <+> parenthesized (commaSeparated (renderAliases keys))
463+ "FOREIGN KEY" <+> parenthesized (renderSQL keys)
464 <+> "REFERENCES" <+> renderSQL parent
464 <+> "REFERENCES" <+> renderSQL parent
465− <+> parenthesized (commaSeparated (renderAliases refs))
465+ <+> parenthesized (renderSQL refs)
466 <+> renderSQL ondel
466 <+> renderSQL ondel
467 <+> renderSQL onupd
467 <+> renderSQL onupd
468
468
@@ -935,7 +935,7 @@ createTypeComposite ty fields = UnsafeDefinition $
935 where
935 where
936 renderField :: Aliased (TypeExpression schemas) x -> ByteString
936 renderField :: Aliased (TypeExpression schemas) x -> ByteString
937 renderField (typ `As` alias) =
937 renderField (typ `As` alias) =
938− renderSQL alias <+> renderTypeExpression typ
938+ renderSQL alias <+> renderSQL typ
939
939
940-- | Composite types can also be generated from a Haskell type, for example
940-- | Composite types can also be generated from a Haskell type, for example
941--
941--
@@ -995,14 +995,14 @@ instance RenderSQL (ColumnTypeExpression schemas ty) where
995nullable
995nullable
996 :: TypeExpression schemas (nullity ty)
996 :: TypeExpression schemas (nullity ty)
997 -> ColumnTypeExpression schemas ('NoDef :=> 'Null ty)
997 -> ColumnTypeExpression schemas ('NoDef :=> 'Null ty)
998−nullable ty = UnsafeColumnTypeExpression $ renderTypeExpression ty <+> "NULL"
998+nullable ty = UnsafeColumnTypeExpression $ renderSQL ty <+> "NULL"
999
999
1000-- | used in `createTable` commands as a column constraint to ensure
1000-- | used in `createTable` commands as a column constraint to ensure
1001-- @NULL@ is not present in a column
1001-- @NULL@ is not present in a column
1002notNullable
1002notNullable
1003 :: TypeExpression schemas (nullity ty)
1003 :: TypeExpression schemas (nullity ty)
1004 -> ColumnTypeExpression schemas ('NoDef :=> 'NotNull ty)
1004 -> ColumnTypeExpression schemas ('NoDef :=> 'NotNull ty)
1005−notNullable ty = UnsafeColumnTypeExpression $ renderTypeExpression ty <+> "NOT NULL"
1005+notNullable ty = UnsafeColumnTypeExpression $ renderSQL ty <+> "NOT NULL"
1006
1006
1007-- | used in `createTable` commands as a column constraint to give a default
1007-- | used in `createTable` commands as a column constraint to give a default
1008default_
1008default_
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+37−32
@@ -31,7 +31,7 @@ Squeal expressions are the atoms used to build statements.
31
31
32module Squeal.PostgreSQL.Expression
32module Squeal.PostgreSQL.Expression
33 ( -- * Expression
33 ( -- * Expression
34− Expression (UnsafeExpression, renderExpression)
34+ Expression (..)
35 , HasParameter (parameter)
35 , HasParameter (parameter)
36 , param
36 , param
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"
357notNull
357notNull
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
361
361
362-- | return the leftmost value which is not NULL
362-- | return the leftmost value which is not NULL
363--
363--
@@ -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]))
375
375
376-- | analagous to `Data.Maybe.fromMaybe` using @COALESCE@
376-- | analagous to `Data.Maybe.fromMaybe` using @COALESCE@
377--
377--
@@ -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"
394
394
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"
402
402
403-- | analagous to `maybe` using @IS NULL@
403-- | analagous to `maybe` using @IS NULL@
404--
404--
@@ -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)))
417
417
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)
434
434
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) <> "]"
443
443
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) <> "]"
452
452
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)
476
476
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
499
499
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)) <> ")"
520
520
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)) <> ")"
531
531
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
542
542
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
552
552
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)
562
562
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)))
572
572
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 <> ")"
628
628
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
643
643
644-- | integer division, truncates the result
644-- | integer division, truncates the result
645--
645--
@@ -779,12 +779,12 @@ caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
779 [ "CASE"
779 [ "CASE"
780 , mconcat
780 , mconcat
781 [ mconcat
781 [ mconcat
782− [ " WHEN ", renderExpression when_
782+ [ " WHEN ", renderSQL when_
783− , " THEN ", renderExpression then_
783+ , " THEN ", renderSQL then_
784 ]
784 ]
785 | (when_,then_) <- whenThens
785 | (when_,then_) <- whenThens
786 ]
786 ]
787− , " ELSE ", renderExpression else_
787+ , " ELSE ", renderSQL else_
788 , " END"
788 , " END"
789 ]
789 ]
790
790
@@ -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)
1177+ where
1178+ renderAliasString :: KnownSymbol alias => Alias alias -> ByteString
1179+ renderAliasString = singleQuotedText . fromString . symbolVal
1177
1180
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, ")"]
1385
1388
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, ")"]
1393
1396
1394-- | >>> :{
1397-- | >>> :{
1395-- let
1398-- let
@@ -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
1629
1634
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.
1631typedef
1636typedef
@@ -1730,17 +1735,17 @@ jsonb = UnsafeTypeExpression "jsonb"
1730vararray
1735vararray
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
1735--
1740--
1736−-- >>> renderTypeExpression (fixarray @2 json)
1741+-- >>> renderSQL (fixarray @2 json)
1737-- "json[2]"
1742-- "json[2]"
1738fixarray
1743fixarray
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 <> "]"
1744
1749
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
squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+22−22
@@ -439,7 +439,7 @@ instance
439
439
440instance RenderSQL (Selection db params grp from row) where
440instance RenderSQL (Selection db params grp from row) where
441 renderSQL = \case
441 renderSQL = \case
442− List list -> renderCommaSeparated (renderAliasedAs renderExpression) list
442+ List list -> renderCommaSeparated (renderAliasedAs renderSQL) list
443 Star -> "*"
443 Star -> "*"
444 DotStar tab -> renderSQL tab <> ".*"
444 DotStar tab -> renderSQL tab <> ".*"
445 Also right left -> renderSQL left <> ", " <> renderSQL right
445 Also right left -> renderSQL left <> ", " <> renderSQL right
@@ -519,7 +519,7 @@ values rw rws = UnsafeQuery $ "SELECT * FROM"
519 renderSQLPart, renderValuePart
519 renderSQLPart, renderValuePart
520 :: Aliased (Expression db params 'Ungrouped '[] ) ty -> ByteString
520 :: Aliased (Expression db params 'Ungrouped '[] ) ty -> ByteString
521 renderSQLPart (_ `As` name) = renderSQL name
521 renderSQLPart (_ `As` name) = renderSQL name
522− renderValuePart (value `As` _) = renderExpression value
522+ renderValuePart (value `As` _) = renderSQL value
523
523
524-- | `values_` computes a row value or set of row values
524-- | `values_` computes a row value or set of row values
525-- specified by value expressions.
525-- specified by value expressions.
@@ -604,8 +604,8 @@ renderTableExpression
604 where
604 where
605 renderWheres = \case
605 renderWheres = \case
606 [] -> ""
606 [] -> ""
607− wh:[] -> " WHERE" <+> renderExpression wh
607+ wh:[] -> " WHERE" <+> renderSQL wh
608− wh:whs -> " WHERE" <+> renderExpression (foldr (.&&) wh whs)
608+ wh:whs -> " WHERE" <+> renderSQL (foldr (.&&) wh whs)
609 renderOrderByClause = \case
609 renderOrderByClause = \case
610 [] -> ""
610 [] -> ""
611 srts -> " ORDER BY"
611 srts -> " ORDER BY"
@@ -695,7 +695,7 @@ unsafeSetOfFunction
695 -> Expression db params 'Ungrouped '[] ty
695 -> Expression db params 'Ungrouped '[] ty
696 -> Query db params row
696 -> Query db params row
697unsafeSetOfFunction fun expr = UnsafeQuery $
697unsafeSetOfFunction fun expr = UnsafeQuery $
698− "SELECT * FROM " <> fun <> "(" <> renderExpression expr <> ")"
698+ "SELECT * FROM " <> fun <> "(" <> renderSQL expr <> ")"
699
699
700-- | Expands the outermost JSON object into a set of key/value pairs.
700-- | Expands the outermost JSON object into a set of key/value pairs.
701jsonEach
701jsonEach
@@ -744,8 +744,8 @@ unsafePopulateFunction
744 -> Query (commons :=> schemas) params row
744 -> Query (commons :=> schemas) params row
745unsafePopulateFunction fun ty expr = UnsafeQuery $
745unsafePopulateFunction fun ty expr = UnsafeQuery $
746 "SELECT * FROM " <> fun <> "("
746 "SELECT * FROM " <> fun <> "("
747− <> "null::" <> renderTypeExpression ty <> ", "
747+ <> "null::" <> renderSQL ty <> ", "
748− <> renderExpression expr <> ")"
748+ <> renderSQL expr <> ")"
749
749
750-- | Expands the JSON expression to a row whose columns match the record
750-- | Expands the JSON expression to a row whose columns match the record
751-- type defined by the given table.
751-- type defined by the given table.
@@ -788,12 +788,12 @@ unsafeRecordFunction
788 -> Query (commons :=> schemas) params record
788 -> Query (commons :=> schemas) params record
789unsafeRecordFunction fun expr types = UnsafeQuery $
789unsafeRecordFunction fun expr types = UnsafeQuery $
790 "SELECT * FROM " <> fun <> "("
790 "SELECT * FROM " <> fun <> "("
791− <> renderExpression expr <> ")"
791+ <> renderSQL expr <> ")"
792 <+> "AS" <+> "x" <> parenthesized (renderCommaSeparated renderTy types)
792 <+> "AS" <+> "x" <> parenthesized (renderCommaSeparated renderTy types)
793 where
793 where
794 renderTy :: Aliased (TypeExpression db) ty -> ByteString
794 renderTy :: Aliased (TypeExpression db) ty -> ByteString
795 renderTy (ty `As` alias) =
795 renderTy (ty `As` alias) =
796− renderSQL alias <+> renderTypeExpression ty
796+ renderSQL alias <+> renderSQL ty
797
797
798-- | Builds an arbitrary record from a JSON object.
798-- | Builds an arbitrary record from a JSON object.
799jsonToRecord
799jsonToRecord
@@ -896,7 +896,7 @@ innerJoin
896 -> FromClause db params (Join left right)
896 -> FromClause db params (Join left right)
897innerJoin right on left = UnsafeFromClause $
897innerJoin right on left = UnsafeFromClause $
898 renderFromClause left <+> "INNER JOIN" <+> renderFromClause right
898 renderFromClause left <+> "INNER JOIN" <+> renderFromClause right
899− <+> "ON" <+> renderExpression on
899+ <+> "ON" <+> renderSQL on
900
900
901{- | @left & leftOuterJoin right on@. First, an inner join is performed.
901{- | @left & leftOuterJoin right on@. First, an inner join is performed.
902 Then, for each row in @left@ that does not satisfy the @on@ condition with
902 Then, for each row in @left@ that does not satisfy the @on@ condition with
@@ -913,7 +913,7 @@ leftOuterJoin
913 -> FromClause db params (Join left (NullifyFrom right))
913 -> FromClause db params (Join left (NullifyFrom right))
914leftOuterJoin right on left = UnsafeFromClause $
914leftOuterJoin right on left = UnsafeFromClause $
915 renderFromClause left <+> "LEFT OUTER JOIN" <+> renderFromClause right
915 renderFromClause left <+> "LEFT OUTER JOIN" <+> renderFromClause right
916− <+> "ON" <+> renderExpression on
916+ <+> "ON" <+> renderSQL on
917
917
918{- | @left & rightOuterJoin right on@. First, an inner join is performed.
918{- | @left & rightOuterJoin right on@. First, an inner join is performed.
919 Then, for each row in @right@ that does not satisfy the @on@ condition with
919 Then, for each row in @right@ that does not satisfy the @on@ condition with
@@ -931,7 +931,7 @@ rightOuterJoin
931 -> FromClause db params (Join (NullifyFrom left) right)
931 -> FromClause db params (Join (NullifyFrom left) right)
932rightOuterJoin right on left = UnsafeFromClause $
932rightOuterJoin right on left = UnsafeFromClause $
933 renderFromClause left <+> "RIGHT OUTER JOIN" <+> renderFromClause right
933 renderFromClause left <+> "RIGHT OUTER JOIN" <+> renderFromClause right
934− <+> "ON" <+> renderExpression on
934+ <+> "ON" <+> renderSQL on
935
935
936{- | @left & fullOuterJoin right on@. First, an inner join is performed.
936{- | @left & fullOuterJoin right on@. First, an inner join is performed.
937 Then, for each row in @left@ that does not satisfy the @on@ condition with
937 Then, for each row in @left@ that does not satisfy the @on@ condition with
@@ -951,7 +951,7 @@ fullOuterJoin
951 (Join (NullifyFrom left) (NullifyFrom right))
951 (Join (NullifyFrom left) (NullifyFrom right))
952fullOuterJoin right on left = UnsafeFromClause $
952fullOuterJoin right on left = UnsafeFromClause $
953 renderFromClause left <+> "FULL OUTER JOIN" <+> renderFromClause right
953 renderFromClause left <+> "FULL OUTER JOIN" <+> renderFromClause right
954− <+> "ON" <+> renderExpression on
954+ <+> "ON" <+> renderSQL on
955
955
956{-----------------------------------------
956{-----------------------------------------
957Grouping
957Grouping
@@ -1033,7 +1033,7 @@ renderHavingClause = \case
1033 NoHaving -> ""
1033 NoHaving -> ""
1034 Having [] -> ""
1034 Having [] -> ""
1035 Having conditions ->
1035 Having conditions ->
1036− " HAVING" <+> commaSeparated (renderExpression <$> conditions)
1036+ " HAVING" <+> commaSeparated (renderSQL <$> conditions)
1037
1037
1038{-----------------------------------------
1038{-----------------------------------------
1039Sorting
1039Sorting
@@ -1071,14 +1071,14 @@ deriving instance Show (SortExpression db params grp from)
1071-- | Render a `SortExpression`.
1071-- | Render a `SortExpression`.
1072renderSortExpression :: SortExpression db params grp from -> ByteString
1072renderSortExpression :: SortExpression db params grp from -> ByteString
1073renderSortExpression = \case
1073renderSortExpression = \case
1074− Asc expression -> renderExpression expression <+> "ASC"
1074+ Asc expression -> renderSQL expression <+> "ASC"
1075− Desc expression -> renderExpression expression <+> "DESC"
1075+ Desc expression -> renderSQL expression <+> "DESC"
1076− AscNullsFirst expression -> renderExpression expression
1076+ AscNullsFirst expression -> renderSQL expression
1077 <+> "ASC NULLS FIRST"
1077 <+> "ASC NULLS FIRST"
1078− DescNullsFirst expression -> renderExpression expression
1078+ DescNullsFirst expression -> renderSQL expression
1079 <+> "DESC NULLS FIRST"
1079 <+> "DESC NULLS FIRST"
1080− AscNullsLast expression -> renderExpression expression <+> "ASC NULLS LAST"
1080+ AscNullsLast expression -> renderSQL expression <+> "ASC NULLS LAST"
1081− DescNullsLast expression -> renderExpression expression <+> "DESC NULLS LAST"
1081+ DescNullsLast expression -> renderSQL expression <+> "DESC NULLS LAST"
1082
1082
1083unsafeSubqueryExpression
1083unsafeSubqueryExpression
1084 :: ByteString
1084 :: ByteString
@@ -1086,7 +1086,7 @@ unsafeSubqueryExpression
1086 -> Query db params '[alias ::: ty]
1086 -> Query db params '[alias ::: ty]
1087 -> Expression db params grp from (nullity 'PGbool)
1087 -> Expression db params grp from (nullity 'PGbool)
1088unsafeSubqueryExpression op x q = UnsafeExpression $
1088unsafeSubqueryExpression op x q = UnsafeExpression $
1089− renderExpression x <+> op <+> parenthesized (renderQuery q)
1089+ renderSQL x <+> op <+> parenthesized (renderQuery q)
1090
1090
1091unsafeRowSubqueryExpression
1091unsafeRowSubqueryExpression
1092 :: SListI row
1092 :: SListI row
@@ -1095,7 +1095,7 @@ unsafeRowSubqueryExpression
1095 -> Query db params row
1095 -> Query db params row
1096 -> Expression db params grp from (nullity 'PGbool)
1096 -> Expression db params grp from (nullity 'PGbool)
1097unsafeRowSubqueryExpression op xs q = UnsafeExpression $
1097unsafeRowSubqueryExpression op xs q = UnsafeExpression $
1098− renderExpression (row xs) <+> op <+> parenthesized (renderQuery q)
1098+ renderSQL (row xs) <+> op <+> parenthesized (renderQuery q)
1099
1099
1100-- | The right-hand side is a sub`Query`, which must return exactly one column.
1100-- | The right-hand side is a sub`Query`, which must return exactly one column.
1101-- The left-hand expression is evaluated and compared to each row of the
1101-- The left-hand expression is evaluated and compared to each row of the
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+5−11
@@ -68,8 +68,6 @@ module Squeal.PostgreSQL.Schema
68 , (:::)
68 , (:::)
69 , Alias (..)
69 , Alias (..)
70 , IsLabel (..)
70 , IsLabel (..)
71− , renderAliasString
72− , renderAliases
73 , Aliased (As)
71 , Aliased (As)
74 , Aliasable (as)
72 , Aliasable (as)
75 , renderAliasedAs
73 , renderAliasedAs
@@ -381,18 +379,14 @@ instance aliases ~ '[alias] => IsLabel alias (NP Alias aliases) where
381instance KnownSymbol alias => RenderSQL (Alias alias) where
379instance KnownSymbol alias => RenderSQL (Alias alias) where
382 renderSQL = doubleQuoted . fromString . symbolVal
380 renderSQL = doubleQuoted . fromString . symbolVal
383
381
384−-- | >>> renderAliasString #ohmahgerd
385−-- "'ohmahgerd'"
386−renderAliasString :: KnownSymbol alias => Alias alias -> ByteString
387−renderAliasString = singleQuotedText . fromString . symbolVal
388−
389-- | >>> import Generics.SOP (NP(..))
382-- | >>> import Generics.SOP (NP(..))
390-- >>> renderAliases (#jimbob :* #kandi)
383-- >>> renderAliases (#jimbob :* #kandi)
391-- ["\"jimbob\"","\"kandi\""]
384-- ["\"jimbob\"","\"kandi\""]
392−renderAliases
393− :: All KnownSymbol aliases => NP Alias aliases -> [ByteString]
385+instance All KnownSymbol aliases => RenderSQL (NP Alias aliases) where
386+ renderSQL
387+ = commaSeparated
394−renderAliases = hcollapse
388+ . hcollapse
395− . hcmap (Proxy @KnownSymbol) (K . renderSQL)
389+ . hcmap (Proxy @KnownSymbol) (K . renderSQL)
396
390
397-- | The `As` operator is used to name an expression. `As` is like a demoted
391-- | The `As` operator is used to name an expression. `As` is like a demoted
398-- version of `:::`.
392-- version of `:::`.