@@ -1,3 +1,13 @@
2+Module: Squeal.PostgreSQL.Query
3+Description: Squeal expressions
4+Copyright: (c) Eitan Chatav, 2017
5+Maintainer: eitan@morphism.tech
6+Stability: experimental
8+Squeal expressions are the atoms used to build statements.
1{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}
11{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}
2{-# LANGUAGE
12{-# LANGUAGE
3 AllowAmbiguousTypes
13 AllowAmbiguousTypes
@@ -24,7 +34,7 @@
26module Squeal.PostgreSQL.Expression
36module Squeal.PostgreSQL.Expression
27− ( -- * Column Expressions
37+ ( -- * Expression
28 Expression (UnsafeExpression, renderExpression)
38 Expression (UnsafeExpression, renderExpression)
29 , HasParameter (param)
39 , HasParameter (param)
30 , HasColumn (getColumn)
40 , HasColumn (getColumn)
@@ -49,8 +59,8 @@ module Squeal.PostgreSQL.Expression
49 , unsafeFunction
59 , unsafeFunction
56 , ceiling_
66 , ceiling_
@@ -83,25 +93,15 @@ module Squeal.PostgreSQL.Expression
83 , charLength
93 , charLength
85 -- ** Aggregation
95 -- ** Aggregation
87− , unsafeAggregateDistinct
96+ , unsafeAggregate, unsafeAggregateDistinct
89− , sumDistinct
97+ , sum_, sumDistinct
90 , PGAvg (avg, avgDistinct)
98 , PGAvg (avg, avgDistinct)
93− , boolAnd
99+ , bitAnd, bitOr, boolAnd, boolOr
94− , boolOr
100+ , bitAndDistinct, bitOrDistinct, boolAndDistinct, boolOrDistinct
95 , countStar
101 , countStar
97− , count
102+ , count, countDistinct
98− , every
103+ , every, everyDistinct
99− , jsonAgg
104+ , max_, maxDistinct, min_, minDistinct
105 -- * Tables
105 -- * Tables
106 , Table (UnsafeTable, renderTable)
106 , Table (UnsafeTable, renderTable)
107 , HasTable (getTable)
107 , HasTable (getTable)
@@ -140,6 +140,7 @@ module Squeal.PostgreSQL.Expression
140 , timeWithTimeZone
140 , timeWithTimeZone
141 , interval
141 , interval
145 , notNull
146 , notNull
@@ -233,6 +234,10 @@ instance (HasTable table tables columns, HasColumn column columns ty)
233 table ! column = UnsafeExpression $
234 table ! column = UnsafeExpression $
234 renderAlias table <> "." <> renderAlias column
235 renderAlias table <> "." <> renderAlias column
237+-- | A `Column` is a witness to a `HasColumn` constraint. It's used
238+-- in `Squeel.PostgreSQL.Definition.unique` and other
239+-- `Squeel.PostgreSQL.Definition.TableConstraint`s to witness a
240+-- subcolumns relationship.
236data Column
241data Column
237 (columns :: ColumnsType)
242 (columns :: ColumnsType)
238 (columnty :: (Symbol,ColumnType))
243 (columnty :: (Symbol,ColumnType))
@@ -245,9 +250,14 @@ deriving instance Show (Column columns columnty)
245deriving instance Eq (Column columns columnty)
250deriving instance Eq (Column columns columnty)
246deriving instance Ord (Column columns columnty)
251deriving instance Ord (Column columns columnty)
253+-- | Render a `Column`.
248renderColumn :: Column columns columnty -> ByteString
254renderColumn :: Column columns columnty -> ByteString
249renderColumn (Column column) = renderAlias column
255renderColumn (Column column) = renderAlias column
257+{- | A `GroupedBy` constraint indicates that a table qualified column is
258+a member of the auxiliary namespace created by @GROUP BY@ clauses and thus,
259+may be called in an output `Expression` without aggregating.
251class (KnownSymbol table, KnownSymbol column)
261class (KnownSymbol table, KnownSymbol column)
252 => GroupedBy table column bys where
262 => GroupedBy table column bys where
253 getGroup1
263 getGroup1
@@ -294,6 +304,7 @@ def = UnsafeExpression "DEFAULT"
294-- "FALSE"
304-- "FALSE"
296 :: Expression '[] 'Ungrouped params ('Required (nullity ty))
306 :: Expression '[] 'Ungrouped params ('Required (nullity ty))
297 -> Expression '[] 'Ungrouped params ('Optional (nullity ty))
308 -> Expression '[] 'Ungrouped params ('Optional (nullity ty))
298unDef = UnsafeExpression . renderExpression
309unDef = UnsafeExpression . renderExpression
@@ -310,6 +321,7 @@ null_ = UnsafeExpression "NULL"
312 :: Expression tables grouping params (optionality ('NotNull ty))
323 :: Expression tables grouping params (optionality ('NotNull ty))
313 -> Expression tables grouping params (optionality ('Null ty))
325 -> Expression tables grouping params (optionality ('Null ty))
314unNull = UnsafeExpression . renderExpression
326unNull = UnsafeExpression . renderExpression
@@ -319,7 +331,9 @@ unNull = UnsafeExpression . renderExpression
319-- "COALESCE(NULL, TRUE, FALSE)"
331-- "COALESCE(NULL, TRUE, FALSE)"
321 :: [Expression tables grouping params ('Required ('Null ty))]
333 :: [Expression tables grouping params ('Required ('Null ty))]
334+ -- ^ @NULL@s may be present
322 -> Expression tables grouping params ('Required ('NotNull ty))
335 -> Expression tables grouping params ('Required ('NotNull ty))
336+ -- ^ @NULL@ is absent
323 -> Expression tables grouping params ('Required ('NotNull ty))
337 -> Expression tables grouping params ('Required ('NotNull ty))
324coalesce nullxs notNullx = UnsafeExpression $
338coalesce nullxs notNullx = UnsafeExpression $
325 "COALESCE" <> parenthesized (commaSeparated
339 "COALESCE" <> parenthesized (commaSeparated
@@ -331,6 +345,7 @@ coalesce nullxs notNullx = UnsafeExpression $
331-- "COALESCE(NULL, TRUE)"
345-- "COALESCE(NULL, TRUE)"
333 :: Expression tables grouping params ('Required ('NotNull ty))
347 :: Expression tables grouping params ('Required ('NotNull ty))
348+ -- ^ what to convert @NULL@ to
334 -> Expression tables grouping params ('Required ('Null ty))
349 -> Expression tables grouping params ('Required ('Null ty))
335 -> Expression tables grouping params ('Required ('NotNull ty))
350 -> Expression tables grouping params ('Required ('NotNull ty))
336fromNull notNullx nullx = coalesce [nullx] notNullx
351fromNull notNullx nullx = coalesce [nullx] notNullx
@@ -339,6 +354,7 @@ fromNull notNullx nullx = coalesce [nullx] notNullx
339-- "NULL IS NULL"
354-- "NULL IS NULL"
341 :: Expression tables grouping params ('Required ('Null ty))
356 :: Expression tables grouping params ('Required ('Null ty))
357+ -- ^ possibly @NULL@
342 -> Condition tables grouping params
358 -> Condition tables grouping params
343isNull x = UnsafeExpression $ renderExpression x <+> "IS NULL"
359isNull x = UnsafeExpression $ renderExpression x <+> "IS NULL"
@@ -346,6 +362,7 @@ isNull x = UnsafeExpression $ renderExpression x <+> "IS NULL"
346-- "NULL IS NOT NULL"
362-- "NULL IS NOT NULL"
348 :: Expression tables grouping params ('Required ('Null ty))
364 :: Expression tables grouping params ('Required ('Null ty))
365+ -- ^ possibly @NULL@
349 -> Condition tables grouping params
366 -> Condition tables grouping params
350isn'tNull x = UnsafeExpression $ renderExpression x <+> "IS NOT NULL"
367isn'tNull x = UnsafeExpression $ renderExpression x <+> "IS NOT NULL"
@@ -355,21 +372,26 @@ isn'tNull x = UnsafeExpression $ renderExpression x <+> "IS NOT NULL"
355-- "CASE WHEN NULL IS NULL THEN TRUE ELSE (NOT NULL) END"
372-- "CASE WHEN NULL IS NULL THEN TRUE ELSE (NOT NULL) END"
357 :: Expression tables grouping params ('Required nullty)
374 :: Expression tables grouping params ('Required nullty)
375+ -- ^ what to convert @NULL@ to
358 -> ( Expression tables grouping params ('Required ('NotNull ty))
376 -> ( Expression tables grouping params ('Required ('NotNull ty))
359 -> Expression tables grouping params ('Required nullty) )
377 -> Expression tables grouping params ('Required nullty) )
378+ -- ^ function to perform when @NULL@ is absent
360 -> Expression tables grouping params ('Required ('Null ty))
379 -> Expression tables grouping params ('Required ('Null ty))
361 -> Expression tables grouping params ('Required nullty)
380 -> Expression tables grouping params ('Required nullty)
362matchNull y f x = ifThenElse (isNull x) y
381matchNull y f x = ifThenElse (isNull x) y
363 (f (UnsafeExpression (renderExpression x)))
382 (f (UnsafeExpression (renderExpression x)))
365−-- | right inverse to `fromNull`
384+-- | right inverse to `fromNull`, if its arguments are equal then
385+-- `nullIf` gives @NULL@.
367-- >>> :set -XTypeApplications -XDataKinds
387-- >>> :set -XTypeApplications -XDataKinds
368-- >>> renderExpression @_ @_ @'[_] $ fromNull false (nullIf false (param @1))
388-- >>> renderExpression @_ @_ @'[_] $ fromNull false (nullIf false (param @1))
369-- "COALESCE(NULL IF (FALSE, ($1 :: bool)), FALSE)"
389-- "COALESCE(NULL IF (FALSE, ($1 :: bool)), FALSE)"
371 :: Expression tables grouping params ('Required ('NotNull ty))
391 :: Expression tables grouping params ('Required ('NotNull ty))
392+ -- ^ @NULL@ is absent
372 -> Expression tables grouping params ('Required ('NotNull ty))
393 -> Expression tables grouping params ('Required ('NotNull ty))
394+ -- ^ @NULL@ is absent
373 -> Expression tables grouping params ('Required ('Null ty))
395 -> Expression tables grouping params ('Required ('Null ty))
374nullIf x y = UnsafeExpression $ "NULL IF" <+> parenthesized
396nullIf x y = UnsafeExpression $ "NULL IF" <+> parenthesized
375 (renderExpression x <> ", " <> renderExpression y)
397 (renderExpression x <> ", " <> renderExpression y)
@@ -378,7 +400,9 @@ nullIf x y = UnsafeExpression $ "NULL IF" <+> parenthesized
378-- "GREATEST(CURRENT_TIMESTAMP, ($1 :: timestamp with time zone))"
400-- "GREATEST(CURRENT_TIMESTAMP, ($1 :: timestamp with time zone))"
380 :: Expression tables grouping params ('Required nullty)
402 :: Expression tables grouping params ('Required nullty)
403+ -- ^ needs at least 1 argument
381 -> [Expression tables grouping params ('Required nullty)]
404 -> [Expression tables grouping params ('Required nullty)]
382 -> Expression tables grouping params ('Required nullty)
406 -> Expression tables grouping params ('Required nullty)
383greatest x xs = UnsafeExpression $ "GREATEST("
407greatest x xs = UnsafeExpression $ "GREATEST("
384 <> commaSeparated (renderExpression <$> (x:xs)) <> ")"
408 <> commaSeparated (renderExpression <$> (x:xs)) <> ")"
@@ -387,28 +411,39 @@ greatest x xs = UnsafeExpression $ "GREATEST("
387-- "LEAST(CURRENT_TIMESTAMP, NULL)"
411-- "LEAST(CURRENT_TIMESTAMP, NULL)"
389 :: Expression tables grouping params ('Required nullty)
413 :: Expression tables grouping params ('Required nullty)
414+ -- ^ needs at least 1 argument
390 -> [Expression tables grouping params ('Required nullty)]
415 -> [Expression tables grouping params ('Required nullty)]
391 -> Expression tables grouping params ('Required nullty)
417 -> Expression tables grouping params ('Required nullty)
392least x xs = UnsafeExpression $ "LEAST("
418least x xs = UnsafeExpression $ "LEAST("
393 <> commaSeparated (renderExpression <$> (x:xs)) <> ")"
419 <> commaSeparated (renderExpression <$> (x:xs)) <> ")"
421+-- | >>> renderExpression $ unsafeBinaryOp "OR" true false
395unsafeBinaryOp
423unsafeBinaryOp
396 :: ByteString
424 :: ByteString
397 -> Expression tables grouping params ('Required ty0)
426 -> Expression tables grouping params ('Required ty0)
398 -> Expression tables grouping params ('Required ty1)
427 -> Expression tables grouping params ('Required ty1)
399 -> Expression tables grouping params ('Required ty2)
428 -> Expression tables grouping params ('Required ty2)
400unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
429unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
401 renderExpression x <+> op <+> renderExpression y
430 renderExpression x <+> op <+> renderExpression y
432+-- | >>> renderExpression $ unsafeUnaryOp "NOT" true
403unsafeUnaryOp
434unsafeUnaryOp
404 :: ByteString
435 :: ByteString
405 -> Expression tables grouping params ('Required ty0)
437 -> Expression tables grouping params ('Required ty0)
406 -> Expression tables grouping params ('Required ty1)
438 -> Expression tables grouping params ('Required ty1)
407unsafeUnaryOp op x = UnsafeExpression $ parenthesized $
439unsafeUnaryOp op x = UnsafeExpression $ parenthesized $
408 op <+> renderExpression x
440 op <+> renderExpression x
442+-- | >>> renderExpression $ unsafeFunction "f" true
410unsafeFunction
444unsafeFunction
411 :: ByteString
445 :: ByteString
412 -> Expression tables grouping params ('Required xty)
447 -> Expression tables grouping params ('Required xty)
413 -> Expression tables grouping params ('Required yty)
448 -> Expression tables grouping params ('Required yty)
414unsafeFunction fun x = UnsafeExpression $
449unsafeFunction fun x = UnsafeExpression $
@@ -453,79 +488,128 @@ instance (PGNum ty, PGFloating ty) => Floating
453 acosh x = log (x + sqrt (x*x - 1))
488 acosh x = log (x + sqrt (x*x - 1))
454 atanh x = log ((1 + x) / (1 - x)) / 2
489 atanh x = log ((1 + x) / (1 - x)) / 2
491+-- | >>> renderExpression @_ @_ @_ @(_ (_ 'PGfloat4)) $ atan2_ pi 2
457 :: PGFloating float
494 :: PGFloating float
458 => Expression tables grouping params ('Required (nullity float))
495 => Expression tables grouping params ('Required (nullity float))
459 -> Expression tables grouping params ('Required (nullity float))
497 -> Expression tables grouping params ('Required (nullity float))
460 -> Expression tables grouping params ('Required (nullity float))
499 -> Expression tables grouping params ('Required (nullity float))
461atan2_ y x = UnsafeExpression $
500atan2_ y x = UnsafeExpression $
462 "atan2(" <> renderExpression y <> ", " <> renderExpression x <> ")"
501 "atan2(" <> renderExpression y <> ", " <> renderExpression x <> ")"
503+-- When a `cast` is applied to an `Expression` of a known type, it
504+-- represents a run-time type conversion. The cast will succeed only if a
505+-- suitable type conversion operation has been defined.
507+-- | >>> renderExpression $ true & cast int4
465 :: TypeExpression ('Required ('Null ty1))
510 :: TypeExpression ('Required ('Null ty1))
511+ -- ^ type to cast as
466 -> Expression tables grouping params ('Required (nullity ty0))
512 -> Expression tables grouping params ('Required (nullity ty0))
513+ -- ^ value to convert
467 -> Expression tables grouping params ('Required (nullity ty1))
514 -> Expression tables grouping params ('Required (nullity ty1))
468cast ty x = UnsafeExpression $ parenthesized $
515cast ty x = UnsafeExpression $ parenthesized $
469 renderExpression x <+> "::" <+> renderTypeExpression ty
516 renderExpression x <+> "::" <+> renderTypeExpression ty
471−div_
518+-- | integer division, truncates the result
520+-- >>> renderExpression @_ @_ @_ @(_(_ 'PGint2)) $ 5 `quot_` 2
472 :: PGIntegral int
523 :: PGIntegral int
473 => Expression tables grouping params ('Required (nullity int))
524 => Expression tables grouping params ('Required (nullity int))
474 -> Expression tables grouping params ('Required (nullity int))
526 -> Expression tables grouping params ('Required (nullity int))
475 -> Expression tables grouping params ('Required (nullity int))
528 -> Expression tables grouping params ('Required (nullity int))
476−div_ = unsafeBinaryOp "/"
529+quot_ = unsafeBinaryOp "/"
478−mod_
531+-- | remainder upon integer division
533+-- >>> renderExpression @_ @_ @_ @(_ (_ 'PGint2)) $ 5 `rem_` 2
479 :: PGIntegral int
536 :: PGIntegral int
480 => Expression tables grouping params ('Required (nullity int))
537 => Expression tables grouping params ('Required (nullity int))
481 -> Expression tables grouping params ('Required (nullity int))
539 -> Expression tables grouping params ('Required (nullity int))
482 -> Expression tables grouping params ('Required (nullity int))
541 -> Expression tables grouping params ('Required (nullity int))
483−mod_ = unsafeBinaryOp "%"
542+rem_ = unsafeBinaryOp "%"
544+-- | >>> renderExpression @_ @_ @_ @(_ (_ 'PGfloat4)) $ trunc pi
486 :: PGFloating frac
547 :: PGFloating frac
487 => Expression tables grouping params ('Required (nullity frac))
548 => Expression tables grouping params ('Required (nullity frac))
549+ -- ^ fractional number
488 -> Expression tables grouping params ('Required (nullity frac))
550 -> Expression tables grouping params ('Required (nullity frac))
489trunc = unsafeFunction "trunc"
551trunc = unsafeFunction "trunc"
553+-- | >>> renderExpression @_ @_ @_ @(_ (_ 'PGfloat4)) $ round_ pi
492 :: PGFloating frac
556 :: PGFloating frac
493 => Expression tables grouping params ('Required (nullity frac))
557 => Expression tables grouping params ('Required (nullity frac))
558+ -- ^ fractional number
494 -> Expression tables grouping params ('Required (nullity frac))
559 -> Expression tables grouping params ('Required (nullity frac))
495round_ = unsafeFunction "round"
560round_ = unsafeFunction "round"
562+-- | >>> renderExpression @_ @_ @_ @(_ (_ 'PGfloat4)) $ ceiling_ pi
498 :: PGFloating frac
565 :: PGFloating frac
499 => Expression tables grouping params ('Required (nullity frac))
566 => Expression tables grouping params ('Required (nullity frac))
567+ -- ^ fractional number
500 -> Expression tables grouping params ('Required (nullity frac))
568 -> Expression tables grouping params ('Required (nullity frac))
501ceiling_ = unsafeFunction "ceiling"
569ceiling_ = unsafeFunction "ceiling"
571+-- | A `Condition` is a boolean valued `Expression`. While SQL allows
572+-- conditions to have @NULL@, squeal instead chooses to disallow @NULL@,
573+-- forcing one to handle the case of @NULL@ explicitly to produce
503type Condition tables grouping params =
575type Condition tables grouping params =
504 Expression tables grouping params ('Required ('NotNull 'PGbool))
576 Expression tables grouping params ('Required ('NotNull 'PGbool))
578+-- | >>> renderExpression true
506true :: Condition tables grouping params
580true :: Condition tables grouping params
507true = UnsafeExpression "TRUE"
581true = UnsafeExpression "TRUE"
583+-- | >>> renderExpression false
509false :: Condition tables grouping params
585false :: Condition tables grouping params
510false = UnsafeExpression "FALSE"
586false = UnsafeExpression "FALSE"
588+-- | >>> renderExpression $ not_ true
513 :: Condition tables grouping params
591 :: Condition tables grouping params
514 -> Condition tables grouping params
592 -> Condition tables grouping params
515not_ = unsafeUnaryOp "NOT"
593not_ = unsafeUnaryOp "NOT"
595+-- | >>> renderExpression $ true .&& false
596+-- "(TRUE AND FALSE)"
518 :: Condition tables grouping params
598 :: Condition tables grouping params
519 -> Condition tables grouping params
599 -> Condition tables grouping params
520 -> Condition tables grouping params
600 -> Condition tables grouping params
521(.&&) = unsafeBinaryOp "AND"
601(.&&) = unsafeBinaryOp "AND"
603+-- | >>> renderExpression $ true .|| false
524 :: Condition tables grouping params
606 :: Condition tables grouping params
525 -> Condition tables grouping params
607 -> Condition tables grouping params
526 -> Condition tables grouping params
608 -> Condition tables grouping params
527(.||) = unsafeBinaryOp "OR"
609(.||) = unsafeBinaryOp "OR"
611+-- | >>> renderExpression @_ @_ @_ @(_ (_ 'PGint2)) $ caseWhenThenElse [(true, 1), (false, 2)] 3
612+-- "CASE WHEN TRUE THEN 1 WHEN FALSE THEN 2 ELSE 3 END"
529caseWhenThenElse
613caseWhenThenElse
530 :: [ ( Condition tables grouping params
614 :: [ ( Condition tables grouping params
531 , Expression tables grouping params ('Required ty)
615 , Expression tables grouping params ('Required ty)
@@ -545,6 +629,8 @@ caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
632+-- | >>> renderExpression @_ @_ @_ @(_ (_ 'PGint2)) $ ifThenElse true 1 0
633+-- "CASE WHEN TRUE THEN 1 ELSE 0 END"
548ifThenElse
634ifThenElse
549 :: Condition tables grouping params
635 :: Condition tables grouping params
550 -> Expression tables grouping params ('Required ty)
636 -> Expression tables grouping params ('Required ty)
@@ -552,64 +638,89 @@ ifThenElse
552 -> Expression tables grouping params ('Required ty)
638 -> Expression tables grouping params ('Required ty)
553ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
639ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
641+-- | Comparison operations like `.==`, `./=`, `.>`, `.>=`, `.<` and `.<=`
642+-- will produce @NULL@s if one of their arguments is @NULL@.
644+-- >>> renderExpression $ unNull true .== null_
556− :: Expression tables grouping params ('Required (nullity ty))
647+ :: Expression tables grouping params ('Required (nullity ty)) -- ^ lhs
557− -> Expression tables grouping params ('Required (nullity ty))
648+ -> Expression tables grouping params ('Required (nullity ty)) -- ^ rhs
558 -> Expression tables grouping params ('Required (nullity 'PGbool))
649 -> Expression tables grouping params ('Required (nullity 'PGbool))
559(.==) = unsafeBinaryOp "="
650(.==) = unsafeBinaryOp "="
560infix 4 .==
651infix 4 .==
653+-- | >>> renderExpression $ unNull true ./= null_
563− :: Expression tables grouping params ('Required (nullity ty))
656+ :: Expression tables grouping params ('Required (nullity ty)) -- ^ lhs
564− -> Expression tables grouping params ('Required (nullity ty))
657+ -> Expression tables grouping params ('Required (nullity ty)) -- ^ rhs
565 -> Expression tables grouping params ('Required (nullity 'PGbool))
658 -> Expression tables grouping params ('Required (nullity 'PGbool))
566(./=) = unsafeBinaryOp "<>"
659(./=) = unsafeBinaryOp "<>"
567infix 4 ./=
660infix 4 ./=
662+-- | >>> renderExpression $ unNull true .>= null_
570− :: Expression tables grouping params ('Required (nullity ty))
665+ :: Expression tables grouping params ('Required (nullity ty)) -- ^ lhs
571− -> Expression tables grouping params ('Required (nullity ty))
666+ -> Expression tables grouping params ('Required (nullity ty)) -- ^ rhs
572 -> Expression tables grouping params ('Required (nullity 'PGbool))
667 -> Expression tables grouping params ('Required (nullity 'PGbool))
573(.>=) = unsafeBinaryOp ">="
668(.>=) = unsafeBinaryOp ">="
574infix 4 .>=
669infix 4 .>=
671+-- | >>> renderExpression $ unNull true .< null_
577− :: Expression tables grouping params ('Required (nullity ty))
674+ :: Expression tables grouping params ('Required (nullity ty)) -- ^ lhs
578− -> Expression tables grouping params ('Required (nullity ty))
675+ -> Expression tables grouping params ('Required (nullity ty)) -- ^ rhs
579 -> Expression tables grouping params ('Required (nullity 'PGbool))
676 -> Expression tables grouping params ('Required (nullity 'PGbool))
580(.<) = unsafeBinaryOp "<"
677(.<) = unsafeBinaryOp "<"
581infix 4 .<
678infix 4 .<
680+-- | >>> renderExpression $ unNull true .<= null_
584− :: Expression tables grouping params ('Required (nullity ty))
683+ :: Expression tables grouping params ('Required (nullity ty)) -- ^ lhs
585− -> Expression tables grouping params ('Required (nullity ty))
684+ -> Expression tables grouping params ('Required (nullity ty)) -- ^ rhs
586 -> Expression tables grouping params ('Required (nullity 'PGbool))
685 -> Expression tables grouping params ('Required (nullity 'PGbool))
587(.<=) = unsafeBinaryOp "<="
686(.<=) = unsafeBinaryOp "<="
588infix 4 .<=
687infix 4 .<=
689+-- | >>> renderExpression $ unNull true .> null_
591− :: Expression tables grouping params ('Required (nullity ty))
692+ :: Expression tables grouping params ('Required (nullity ty)) -- ^ lhs
592− -> Expression tables grouping params ('Required (nullity ty))
693+ -> Expression tables grouping params ('Required (nullity ty)) -- ^ rhs
593 -> Expression tables grouping params ('Required (nullity 'PGbool))
694 -> Expression tables grouping params ('Required (nullity 'PGbool))
594(.>) = unsafeBinaryOp ">"
695(.>) = unsafeBinaryOp ">"
595infix 4 .>
696infix 4 .>
698+-- | >>> renderExpression $ currentDate
597currentDate
700currentDate
598 :: Expression tables grouping params ('Required (nullity 'PGdate))
701 :: Expression tables grouping params ('Required (nullity 'PGdate))
599currentDate = UnsafeExpression "CURRENT_DATE"
702currentDate = UnsafeExpression "CURRENT_DATE"
704+-- | >>> renderExpression $ currentTime
601currentTime
706currentTime
602 :: Expression tables grouping params ('Required (nullity 'PGtimetz))
707 :: Expression tables grouping params ('Required (nullity 'PGtimetz))
603currentTime = UnsafeExpression "CURRENT_TIME"
708currentTime = UnsafeExpression "CURRENT_TIME"
710+-- | >>> renderExpression $ currentTimestamp
711+-- "CURRENT_TIMESTAMP"
605currentTimestamp
712currentTimestamp
606 :: Expression tables grouping params ('Required (nullity 'PGtimestamptz))
713 :: Expression tables grouping params ('Required (nullity 'PGtimestamptz))
607currentTimestamp = UnsafeExpression "CURRENT_TIMESTAMP"
714currentTimestamp = UnsafeExpression "CURRENT_TIMESTAMP"
716+-- | >>> renderExpression $ localTime
610 :: Expression tables grouping params ('Required (nullity 'PGtime))
719 :: Expression tables grouping params ('Required (nullity 'PGtime))
611localTime = UnsafeExpression "LOCALTIME"
720localTime = UnsafeExpression "LOCALTIME"
722+-- | >>> renderExpression $ localTimestamp
613localTimestamp
724localTimestamp
614 :: Expression tables grouping params ('Required (nullity 'PGtimestamp))
725 :: Expression tables grouping params ('Required (nullity 'PGtimestamp))
615localTimestamp = UnsafeExpression "LOCALTIMESTAMP"
726localTimestamp = UnsafeExpression "LOCALTIMESTAMP"
@@ -639,24 +750,44 @@ instance Monoid
639 mempty = fromString ""
750 mempty = fromString ""
640 mappend = unsafeBinaryOp "||"
751 mappend = unsafeBinaryOp "||"
753+-- | >>> renderExpression $ lower "ARRRGGG"
754+-- "lower(E'ARRRGGG')"
643 :: Expression tables grouping params ('Required (nullity 'PGtext))
756 :: Expression tables grouping params ('Required (nullity 'PGtext))
757+ -- ^ string to lower case
644 -> Expression tables grouping params ('Required (nullity 'PGtext))
758 -> Expression tables grouping params ('Required (nullity 'PGtext))
645lower = unsafeFunction "lower"
759lower = unsafeFunction "lower"
761+-- | >>> renderExpression $ upper "eeee"
648 :: Expression tables grouping params ('Required (nullity 'PGtext))
764 :: Expression tables grouping params ('Required (nullity 'PGtext))
765+ -- ^ string to upper case
649 -> Expression tables grouping params ('Required (nullity 'PGtext))
766 -> Expression tables grouping params ('Required (nullity 'PGtext))
650upper = unsafeFunction "upper"
767upper = unsafeFunction "upper"
769+-- | >>> renderExpression $ charLength "four"
770+-- "char_length(E'four')"
652charLength
771charLength
653 :: Expression tables grouping params ('Required (nullity 'PGtext))
772 :: Expression tables grouping params ('Required (nullity 'PGtext))
773+ -- ^ string to measure
654 -> Expression tables grouping params ('Required (nullity 'PGint4))
774 -> Expression tables grouping params ('Required (nullity 'PGint4))
655charLength = unsafeFunction "char_length"
775charLength = unsafeFunction "char_length"
777+-- | The `like` expression returns true if the @string@ matches
778+-- the supplied @pattern@. If @pattern@ does not contain percent signs
779+-- or underscores, then the pattern only represents the string itself;
780+-- in that case `like` acts like the equals operator. An underscore (_)
781+-- in pattern stands for (matches) any single character; a percent sign (%)
782+-- matches any sequence of zero or more characters.
784+-- >>> renderExpression $ "abc" `like` "a%"
785+-- "(E'abc' LIKE E'a%')"
658 :: Expression tables grouping params ('Required (nullity 'PGtext))
787 :: Expression tables grouping params ('Required (nullity 'PGtext))
659 -> Expression tables grouping params ('Required (nullity 'PGtext))
789 -> Expression tables grouping params ('Required (nullity 'PGtext))
660 -> Expression tables grouping params ('Required (nullity 'PGbool))
791 -> Expression tables grouping params ('Required (nullity 'PGbool))
661like = unsafeBinaryOp "LIKE"
792like = unsafeBinaryOp "LIKE"
@@ -664,25 +795,46 @@ like = unsafeBinaryOp "LIKE"
664aggregation
795aggregation
665-----------------------------------------}
796-----------------------------------------}
798+-- | escape hatch to define aggregate functions
667−unsafeAggregate, unsafeAggregateDistinct
799+unsafeAggregate
668− :: ByteString
800+ :: ByteString -- ^ aggregate function
669 -> Expression tables 'Ungrouped params ('Required xty)
801 -> Expression tables 'Ungrouped params ('Required xty)
670 -> Expression tables ('Grouped bys) params ('Required yty)
802 -> Expression tables ('Grouped bys) params ('Required yty)
671unsafeAggregate fun x = UnsafeExpression $ mconcat
803unsafeAggregate fun x = UnsafeExpression $ mconcat
672 [fun, "(", renderExpression x, ")"]
804 [fun, "(", renderExpression x, ")"]
806+-- | escape hatch to define aggregate functions over distinct values
807+unsafeAggregateDistinct
808+ :: ByteString -- ^ aggregate function
809+ -> Expression tables 'Ungrouped params ('Required xty)
810+ -> Expression tables ('Grouped bys) params ('Required yty)
673unsafeAggregateDistinct fun x = UnsafeExpression $ mconcat
811unsafeAggregateDistinct fun x = UnsafeExpression $ mconcat
674 [fun, "(DISTINCT ", renderExpression x, ")"]
812 [fun, "(DISTINCT ", renderExpression x, ")"]
676−sum_, sumDistinct
814+-- | >>> renderExpression @'[_ ::: '["col" ::: 'Required (_ 'PGnumeric)]] $ sum_ #col
677 :: PGNum ty
817 :: PGNum ty
678 => Expression tables 'Ungrouped params ('Required (nullity ty))
818 => Expression tables 'Ungrouped params ('Required (nullity ty))
679 -> Expression tables ('Grouped bys) params ('Required (nullity ty))
820 -> Expression tables ('Grouped bys) params ('Required (nullity ty))
680sum_ = unsafeAggregate "sum"
821sum_ = unsafeAggregate "sum"
823+-- | >>> renderExpression @'[_ ::: '["col" ::: 'Required (_ 'PGnumeric)]] $ sumDistinct #col
824+-- "sum(DISTINCT col)"
827+ => Expression tables 'Ungrouped params ('Required (nullity ty))
829+ -> Expression tables ('Grouped bys) params ('Required (nullity ty))
681sumDistinct = unsafeAggregateDistinct "sum"
830sumDistinct = unsafeAggregateDistinct "sum"
832+-- | A constraint for `PGType`s that you can take averages of and the resulting
683class PGAvg ty avg | ty -> avg where
834class PGAvg ty avg | ty -> avg where
684 avg, avgDistinct
835 avg, avgDistinct
685 :: Expression tables 'Ungrouped params ('Required (nullity ty))
836 :: Expression tables 'Ungrouped params ('Required (nullity ty))
837+ -- ^ what to average
686 -> Expression tables ('Grouped bys) params ('Required (nullity avg))
838 -> Expression tables ('Grouped bys) params ('Required (nullity avg))
687 avg = unsafeAggregate "avg"
839 avg = unsafeAggregate "avg"
688 avgDistinct = unsafeAggregateDistinct "avg"
840 avgDistinct = unsafeAggregateDistinct "avg"
@@ -694,76 +846,141 @@ instance PGAvg 'PGfloat4 'PGfloat8
694instance PGAvg 'PGfloat8 'PGfloat8
846instance PGAvg 'PGfloat8 'PGfloat8
695instance PGAvg 'PGinterval 'PGinterval
847instance PGAvg 'PGinterval 'PGinterval
697−bitAnd, bitOr
849+-- | >>> renderExpression @'[_ ::: '["col" ::: 'Required (_ 'PGint4)]] $ bitAnd #col
698 :: PGIntegral int
852 :: PGIntegral int
699 => Expression tables 'Ungrouped params ('Required (nullity int))
853 => Expression tables 'Ungrouped params ('Required (nullity int))
854+ -- ^ what to aggregate
700 -> Expression tables ('Grouped bys) params ('Required (nullity int))
855 -> Expression tables ('Grouped bys) params ('Required (nullity int))
701bitAnd = unsafeAggregate "bit_and"
856bitAnd = unsafeAggregate "bit_and"
858+-- | >>> renderExpression @'[_ ::: '["col" ::: 'Required (_ 'PGint4)]] $ bitOr #col
862+ => Expression tables 'Ungrouped params ('Required (nullity int))
863+ -- ^ what to aggregate
864+ -> Expression tables ('Grouped bys) params ('Required (nullity int))
702bitOr = unsafeAggregate "bit_or"
865bitOr = unsafeAggregate "bit_or"
704−boolAnd, boolOr
867+-- | >>> renderExpression @'[_ ::: '["col" ::: 'Required (_ 'PGint4)]] $ bitAndDistinct #col
868+-- "bit_and(DISTINCT col)"
871+ => Expression tables 'Ungrouped params ('Required (nullity int))
872+ -- ^ what to aggregate
873+ -> Expression tables ('Grouped bys) params ('Required (nullity int))
874+bitAndDistinct = unsafeAggregateDistinct "bit_and"
876+-- | >>> renderExpression @'[_ ::: '["col" ::: 'Required (_ 'PGint4)]] $ bitOrDistinct #col
877+-- "bit_or(DISTINCT col)"
880+ => Expression tables 'Ungrouped params ('Required (nullity int))
881+ -- ^ what to aggregate
882+ -> Expression tables ('Grouped bys) params ('Required (nullity int))
883+bitOrDistinct = unsafeAggregateDistinct "bit_or"
885+-- | >>> renderExpression @'[_ ::: '["col" ::: 'Required (_ 'PGbool)]] $ boolAnd #col
705 :: Expression tables 'Ungrouped params ('Required (nullity 'PGbool))
888 :: Expression tables 'Ungrouped params ('Required (nullity 'PGbool))
889+ -- ^ what to aggregate
706 -> Expression tables ('Grouped bys) params ('Required (nullity 'PGbool))
890 -> Expression tables ('Grouped bys) params ('Required (nullity 'PGbool))
707boolAnd = unsafeAggregate "bool_and"
891boolAnd = unsafeAggregate "bool_and"
893+-- | >>> renderExpression @'[_ ::: '["col" ::: 'Required (_ 'PGbool)]] $ boolOr #col
896+ :: Expression tables 'Ungrouped params ('Required (nullity 'PGbool))
897+ -- ^ what to aggregate
898+ -> Expression tables ('Grouped bys) params ('Required (nullity 'PGbool))
708boolOr = unsafeAggregate "bool_or"
899boolOr = unsafeAggregate "bool_or"
710−countStar, countDistinctStar
901+-- | >>> renderExpression @'[_ ::: '["col" ::: 'Required (_ 'PGbool)]] $ boolAndDistinct #col
902+-- "bool_and(DISTINCT col)"
904+ :: Expression tables 'Ungrouped params ('Required (nullity 'PGbool))
905+ -- ^ what to aggregate
906+ -> Expression tables ('Grouped bys) params ('Required (nullity 'PGbool))
907+boolAndDistinct = unsafeAggregateDistinct "bool_and"
909+-- | >>> renderExpression @'[_ ::: '["col" ::: 'Required (_ 'PGbool)]] $ boolOrDistinct #col
910+-- "bool_or(DISTINCT col)"
912+ :: Expression tables 'Ungrouped params ('Required (nullity 'PGbool))
913+ -- ^ what to aggregate
914+ -> Expression tables ('Grouped bys) params ('Required (nullity 'PGbool))
915+boolOrDistinct = unsafeAggregateDistinct "bool_or"
917+-- | A special aggregation that does not require an input
919+-- >>> renderExpression countStar
711 :: Expression tables ('Grouped bys) params ('Required ('NotNull 'PGint8))
922 :: Expression tables ('Grouped bys) params ('Required ('NotNull 'PGint8))
712countStar = UnsafeExpression $ "count(*)"
923countStar = UnsafeExpression $ "count(*)"
713−countDistinctStar = UnsafeExpression $ "count(DISTINCT *)"
925+-- | >>> renderExpression @'[_ ::: '["col" ::: 'Optional _]] $ count #col
716 :: Expression tables 'Ungrouped params ('Required ty)
928 :: Expression tables 'Ungrouped params ('Required ty)
717 -> Expression tables ('Grouped bys) params ('Required ('NotNull 'PGint8))
930 -> Expression tables ('Grouped bys) params ('Required ('NotNull 'PGint8))
718count = unsafeAggregate "count"
931count = unsafeAggregate "count"
933+-- | >>> renderExpression @'[_ ::: '["col" ::: 'Required _]] $ countDistinct #col
934+-- "count(DISTINCT col)"
936+ :: Expression tables 'Ungrouped params ('Required ty)
938+ -> Expression tables ('Grouped bys) params ('Required ('NotNull 'PGint8))
939+countDistinct = unsafeAggregateDistinct "count"
941+-- | synonym for `boolAnd`
943+-- >>> renderExpression @'[_ ::: '["col" ::: 'Required (_ 'PGbool)]] $ every #col
721 :: Expression tables 'Ungrouped params ('Required (nullity 'PGbool))
946 :: Expression tables 'Ungrouped params ('Required (nullity 'PGbool))
947+ -- ^ what to aggregate
722 -> Expression tables ('Grouped bys) params ('Required (nullity 'PGbool))
948 -> Expression tables ('Grouped bys) params ('Required (nullity 'PGbool))
723every = unsafeAggregate "every"
949every = unsafeAggregate "every"
725−jsonAgg
951+-- | synonym for `boolAndDistinct`
953+-- >>> renderExpression @'[_ ::: '["col" ::: 'Required (_ 'PGbool)]] $ everyDistinct #col
954+-- "every(DISTINCT col)"
726− :: Expression tables 'Ungrouped params ('Required ty)
956+ :: Expression tables 'Ungrouped params ('Required (nullity 'PGbool))
957+ -- ^ what to aggregate
727− -> Expression tables ('Grouped bys) params ('Required ('NotNull 'PGjson))
958+ -> Expression tables ('Grouped bys) params ('Required (nullity 'PGbool))
728−jsonAgg = unsafeAggregate "json_agg"
959+everyDistinct = unsafeAggregateDistinct "every"
730−jsonbAgg
961+-- | minimum and maximum aggregation
731− :: Expression tables 'Ungrouped params ('Required ty)
732− -> Expression tables ('Grouped bys) params ('Required ('NotNull 'PGjsonb))
733−jsonbAgg = unsafeAggregate "jsonb_agg"
736− :: Expression tables 'Ungrouped params ('Required ('NotNull keyty))
737− -> Expression tables 'Ungrouped params ('Required valty)
738− -> Expression tables ('Grouped bys) params ('Required ('NotNull 'PGjson))
739−jsonObjectAgg k v = UnsafeExpression $
740− "json_object_agg(" <> renderExpression k
741− <> ", " <> renderExpression v <> ")"
744− :: Expression tables 'Ungrouped params ('Required ('NotNull keyty))
745− -> Expression tables 'Ungrouped params ('Required valty)
746− -> Expression tables ('Grouped bys) params ('Required ('NotNull 'PGjsonb))
747−jsonbObjectAgg k v = UnsafeExpression $
749− <> renderExpression k <> ", " <> renderExpression v <> ")"
751−max_, min_
962+max_, min_, maxDistinct, minDistinct
752 :: Expression tables 'Ungrouped params ('Required (nullity ty))
963 :: Expression tables 'Ungrouped params ('Required (nullity ty))
964+ -- ^ what to aggregate
753 -> Expression tables ('Grouped bys) params ('Required (nullity ty))
965 -> Expression tables ('Grouped bys) params ('Required (nullity ty))
754max_ = unsafeAggregate "max"
966max_ = unsafeAggregate "max"
755min_ = unsafeAggregate "min"
967min_ = unsafeAggregate "min"
968+maxDistinct = unsafeAggregateDistinct "max"
969+minDistinct = unsafeAggregateDistinct "min"
757{-----------------------------------------
971{-----------------------------------------
759-----------------------------------------}
973-----------------------------------------}
975+-- | A `Table` from a schema without its alias with an `IsLabel` instance
976+-- to call a table reference by its alias.
761newtype Table
977newtype Table
762 (schema :: TablesType)
978 (schema :: TablesType)
763 (columns :: ColumnsType)
979 (columns :: ColumnsType)
764 = UnsafeTable { renderTable :: ByteString }
980 = UnsafeTable { renderTable :: ByteString }
765 deriving (GHC.Generic,Show,Eq,Ord,NFData)
981 deriving (GHC.Generic,Show,Eq,Ord,NFData)
983+-- | A `HasTable` constraint indicates a table reference.
767class KnownSymbol table => HasTable table tables columns
984class KnownSymbol table => HasTable table tables columns
768 | table tables -> columns where
985 | table tables -> columns where
769 getTable :: Alias table -> Table tables columns
986 getTable :: Alias table -> Table tables columns
@@ -782,96 +999,113 @@ instance HasTable table schema columns
782type expressions
999type expressions
783-----------------------------------------}
1000-----------------------------------------}
1002+-- | `TypeExpression`s are used in `cast`s and `createTable` commands.
785newtype TypeExpression (ty :: ColumnType)
1003newtype TypeExpression (ty :: ColumnType)
786 = UnsafeTypeExpression { renderTypeExpression :: ByteString }
1004 = UnsafeTypeExpression { renderTypeExpression :: ByteString }
787 deriving (GHC.Generic,Show,Eq,Ord,NFData)
1005 deriving (GHC.Generic,Show,Eq,Ord,NFData)
1007+-- | logical Boolean (true/false)
789bool :: TypeExpression ('Required ('Null 'PGbool))
1008bool :: TypeExpression ('Required ('Null 'PGbool))
790bool = UnsafeTypeExpression "bool"
1009bool = UnsafeTypeExpression "bool"
1010+-- | signed two-byte integer
791−int2 :: TypeExpression ('Required ('Null 'PGint2))
1011+int2, smallint :: TypeExpression ('Required ('Null 'PGint2))
792int2 = UnsafeTypeExpression "int2"
1012int2 = UnsafeTypeExpression "int2"
793−smallint :: TypeExpression ('Required ('Null 'PGint2))
794smallint = UnsafeTypeExpression "smallint"
1013smallint = UnsafeTypeExpression "smallint"
1014+-- | signed four-byte integer
795−int4 :: TypeExpression ('Required ('Null 'PGint4))
1015+int4, int, integer :: TypeExpression ('Required ('Null 'PGint4))
796int4 = UnsafeTypeExpression "int4"
1016int4 = UnsafeTypeExpression "int4"
797−int :: TypeExpression ('Required ('Null 'PGint4))
798int = UnsafeTypeExpression "int"
1017int = UnsafeTypeExpression "int"
799−integer :: TypeExpression ('Required ('Null 'PGint4))
800integer = UnsafeTypeExpression "integer"
1018integer = UnsafeTypeExpression "integer"
1019+-- | signed eight-byte integer
801−int8 :: TypeExpression ('Required ('Null 'PGint8))
1020+int8, bigint :: TypeExpression ('Required ('Null 'PGint8))
802int8 = UnsafeTypeExpression "int8"
1021int8 = UnsafeTypeExpression "int8"
803−bigint :: TypeExpression ('Required ('Null 'PGint8))
804bigint = UnsafeTypeExpression "bigint"
1022bigint = UnsafeTypeExpression "bigint"
1023+-- | arbitrary precision numeric type
805numeric :: TypeExpression ('Required ('Null 'PGnumeric))
1024numeric :: TypeExpression ('Required ('Null 'PGnumeric))
806numeric = UnsafeTypeExpression "numeric"
1025numeric = UnsafeTypeExpression "numeric"
1026+-- | single precision floating-point number (4 bytes)
807−float4 :: TypeExpression ('Required ('Null 'PGfloat4))
1027+float4, real :: TypeExpression ('Required ('Null 'PGfloat4))
808float4 = UnsafeTypeExpression "float4"
1028float4 = UnsafeTypeExpression "float4"
809−real :: TypeExpression ('Required ('Null 'PGfloat4))
810real = UnsafeTypeExpression "real"
1029real = UnsafeTypeExpression "real"
1030+-- | double precision floating-point number (8 bytes)
811−float8 :: TypeExpression ('Required ('Null 'PGfloat8))
1031+float8, doublePrecision :: TypeExpression ('Required ('Null 'PGfloat8))
812float8 = UnsafeTypeExpression "float8"
1032float8 = UnsafeTypeExpression "float8"
813−doublePrecision :: TypeExpression ('Required ('Null 'PGfloat8))
814doublePrecision = UnsafeTypeExpression "double precision"
1033doublePrecision = UnsafeTypeExpression "double precision"
1034+-- | not a true type, but merely a notational convenience for creating
1035+-- unique identifier columns with type `'PGint2`
815−serial2 :: TypeExpression ('Optional ('NotNull 'PGint2))
1036+serial2, smallserial :: TypeExpression ('Optional ('NotNull 'PGint2))
816serial2 = UnsafeTypeExpression "serial2"
1037serial2 = UnsafeTypeExpression "serial2"
817−smallserial :: TypeExpression ('Optional ('NotNull 'PGint2))
818smallserial = UnsafeTypeExpression "smallserial"
1038smallserial = UnsafeTypeExpression "smallserial"
1039+-- | not a true type, but merely a notational convenience for creating
1040+-- unique identifier columns with type `'PGint4`
819−serial4 :: TypeExpression ('Optional ('NotNull 'PGint4))
1041+serial4, serial :: TypeExpression ('Optional ('NotNull 'PGint4))
820serial4 = UnsafeTypeExpression "serial4"
1042serial4 = UnsafeTypeExpression "serial4"
821−serial :: TypeExpression ('Optional ('NotNull 'PGint4))
822serial = UnsafeTypeExpression "serial"
1043serial = UnsafeTypeExpression "serial"
1044+-- | not a true type, but merely a notational convenience for creating
1045+-- unique identifier columns with type `'PGint8`
823−serial8 :: TypeExpression ('Optional ('NotNull 'PGint8))
1046+serial8, bigserial :: TypeExpression ('Optional ('NotNull 'PGint8))
824serial8 = UnsafeTypeExpression "serial8"
1047serial8 = UnsafeTypeExpression "serial8"
825−bigserial :: TypeExpression ('Optional ('NotNull 'PGint8))
826bigserial = UnsafeTypeExpression "bigserial"
1048bigserial = UnsafeTypeExpression "bigserial"
1049+-- | variable-length character string
827text :: TypeExpression ('Required ('Null 'PGtext))
1050text :: TypeExpression ('Required ('Null 'PGtext))
828text = UnsafeTypeExpression "text"
1051text = UnsafeTypeExpression "text"
1052+-- | fixed-length character string
829−char
1053+char, character
830 :: (KnownNat n, 1 <= n)
1054 :: (KnownNat n, 1 <= n)
831 => proxy n
1055 => proxy n
832 -> TypeExpression ('Required ('Null ('PGchar n)))
1056 -> TypeExpression ('Required ('Null ('PGchar n)))
833char p = UnsafeTypeExpression $ "char(" <> renderNat p <> ")"
1057char p = UnsafeTypeExpression $ "char(" <> renderNat p <> ")"
835− :: (KnownNat n, 1 <= n)
837− -> TypeExpression ('Required ('Null ('PGchar n)))
838character p = UnsafeTypeExpression $ "character(" <> renderNat p <> ")"
1058character p = UnsafeTypeExpression $ "character(" <> renderNat p <> ")"
1059+-- | variable-length character string
839−varchar
1060+varchar, characterVarying
840 :: (KnownNat n, 1 <= n)
1061 :: (KnownNat n, 1 <= n)
841 => proxy n
1062 => proxy n
842 -> TypeExpression ('Required ('Null ('PGvarchar n)))
1063 -> TypeExpression ('Required ('Null ('PGvarchar n)))
843varchar p = UnsafeTypeExpression $ "varchar(" <> renderNat p <> ")"
1064varchar p = UnsafeTypeExpression $ "varchar(" <> renderNat p <> ")"
845− :: (KnownNat n, 1 <= n)
847− -> TypeExpression ('Required ('Null ('PGvarchar n)))
848characterVarying p = UnsafeTypeExpression $
1065characterVarying p = UnsafeTypeExpression $
849 "character varying(" <> renderNat p <> ")"
1066 "character varying(" <> renderNat p <> ")"
1067+-- | binary data ("byte array")
850−bytea :: TypeExpression ('Required ('Null ('PGbytea)))
1068+bytea :: TypeExpression ('Required ('Null 'PGbytea))
851bytea = UnsafeTypeExpression "bytea"
1069bytea = UnsafeTypeExpression "bytea"
1070+-- | date and time (no time zone)
852−timestamp :: TypeExpression ('Required ('Null ('PGtimestamp)))
1071+timestamp :: TypeExpression ('Required ('Null 'PGtimestamp))
853timestamp = UnsafeTypeExpression "timestamp"
1072timestamp = UnsafeTypeExpression "timestamp"
1073+-- | date and time, including time zone
854−timestampWithTimeZone :: TypeExpression ('Required ('Null ('PGtimestamptz)))
1074+timestampWithTimeZone :: TypeExpression ('Required ('Null 'PGtimestamptz))
855timestampWithTimeZone = UnsafeTypeExpression "timestamp with time zone"
1075timestampWithTimeZone = UnsafeTypeExpression "timestamp with time zone"
1076+-- | calendar date (year, month, day)
856−date :: TypeExpression ('Required ('Null ('PGdate)))
1077+date :: TypeExpression ('Required ('Null 'PGdate))
857date = UnsafeTypeExpression "date"
1078date = UnsafeTypeExpression "date"
1079+-- | time of day (no time zone)
858−time :: TypeExpression ('Required ('Null ('PGtime)))
1080+time :: TypeExpression ('Required ('Null 'PGtime))
859time = UnsafeTypeExpression "time"
1081time = UnsafeTypeExpression "time"
1082+-- | time of day, including time zone
860−timeWithTimeZone :: TypeExpression ('Required ('Null ('PGtimetz)))
1083+timeWithTimeZone :: TypeExpression ('Required ('Null 'PGtimetz))
861timeWithTimeZone = UnsafeTypeExpression "time with time zone"
1084timeWithTimeZone = UnsafeTypeExpression "time with time zone"
862−interval :: TypeExpression ('Required ('Null ('PGinterval)))
1086+interval :: TypeExpression ('Required ('Null 'PGinterval))
863interval = UnsafeTypeExpression "interval"
1087interval = UnsafeTypeExpression "interval"
1088+-- | universally unique identifier
864−uuid :: TypeExpression ('Required ('Null ('PGuuid)))
1089+uuid :: TypeExpression ('Required ('Null 'PGuuid))
865uuid = UnsafeTypeExpression "uuid"
1090uuid = UnsafeTypeExpression "uuid"
1091+-- | IPv4 or IPv6 host address
1092+inet :: TypeExpression ('Required ('Null 'PGinet))
1093+inet = UnsafeTypeExpression "inet"
1094+-- | textual JSON data
866−json :: TypeExpression ('Required ('Null ('PGjson)))
1095+json :: TypeExpression ('Required ('Null 'PGjson))
867json = UnsafeTypeExpression "json"
1096json = UnsafeTypeExpression "json"
1097+-- | binary JSON data, decomposed
868−jsonb :: TypeExpression ('Required ('Null ('PGjsonb)))
1098+jsonb :: TypeExpression ('Required ('Null 'PGjsonb))
869jsonb = UnsafeTypeExpression "jsonb"
1099jsonb = UnsafeTypeExpression "jsonb"
1101+-- | used in `createTable` commands as a column constraint to ensure
1102+-- @NULL@ is not present
872 :: TypeExpression (optionality ('Null ty))
1104 :: TypeExpression (optionality ('Null ty))
873 -> TypeExpression (optionality ('NotNull ty))
1105 -> TypeExpression (optionality ('NotNull ty))
874notNull ty = UnsafeTypeExpression $ renderTypeExpression ty <+> "NOT NULL"
1106notNull ty = UnsafeTypeExpression $ renderTypeExpression ty <+> "NOT NULL"
1108+-- | used in `createTable` commands as a column constraint to give a default
876 :: Expression '[] 'Ungrouped '[] ('Required ty)
1110 :: Expression '[] 'Ungrouped '[] ('Required ty)
877 -> TypeExpression ('Required ty)
1111 -> TypeExpression ('Required ty)
@@ -879,6 +1113,7 @@ default_
879default_ x ty = UnsafeTypeExpression $
1113default_ x ty = UnsafeTypeExpression $
880 renderTypeExpression ty <+> "DEFAULT" <+> renderExpression x
1114 renderTypeExpression ty <+> "DEFAULT" <+> renderExpression x
1116+-- | `pgtype` is a demoted version of a `PGType`
882class PGTyped (ty :: PGType) where
1117class PGTyped (ty :: PGType) where
883 pgtype :: TypeExpression ('Required ('Null ty))
1118 pgtype :: TypeExpression ('Required ('Null ty))
884instance PGTyped 'PGbool where pgtype = bool
1119instance PGTyped 'PGbool where pgtype = bool