{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}
{-# LANGUAGE
AllowAmbiguousTypes
, ConstraintKinds
, DeriveGeneric
, FlexibleContexts
, FlexibleInstances
, FunctionalDependencies
, GeneralizedNewtypeDeriving
, LambdaCase
, MagicHash
, OverloadedStrings
, ScopedTypeVariables
, TypeApplications
, TypeFamilies
, TypeInType
, TypeOperators
, UndecidableInstances
, RankNTypes
#-}
module Squeal.PostgreSQL.Expression
(
Expression (UnsafeExpression, renderExpression)
, HasParameter (parameter)
, param
, null_
, notNull
, coalesce
, fromNull
, isNull
, isNotNull
, matchNull
, nullIf
, array
, index
, row
, field
, unsafeBinaryOp
, unsafeUnaryOp
, unsafeFunction
, unsafeVariadicFunction
, atan2_
, cast
, quot_
, rem_
, trunc
, round_
, ceiling_
, greatest
, least
, true
, false
, not_
, (.&&)
, (.||)
, Condition
, caseWhenThenElse
, ifThenElse
, (.==)
, (./=)
, (.>=)
, (.<)
, (.<=)
, (.>)
, currentDate
, currentTime
, currentTimestamp
, localTime
, localTimestamp
, lower
, upper
, charLength
, like
, (.->)
, (.->>)
, (.#>)
, (.#>>)
, (.@>)
, (.<@)
, (.?)
, (.?|)
, (.?&)
, (.-.)
, (#-.)
, jsonLit
, jsonbLit
, toJson
, toJsonb
, arrayToJson
, rowToJson
, jsonBuildArray
, jsonbBuildArray
, jsonBuildObject
, jsonbBuildObject
, jsonObject
, jsonbObject
, jsonZipObject
, jsonbZipObject
, jsonArrayLength
, jsonbArrayLength
, jsonExtractPath
, jsonbExtractPath
, jsonExtractPathAsText
, jsonbExtractPathAsText
, jsonTypeof
, jsonbTypeof
, jsonStripNulls
, jsonbStripNulls
, jsonbSet
, jsonbInsert
, jsonbPretty
, unsafeAggregate, unsafeAggregateDistinct
, sum_, sumDistinct
, PGAvg (avg, avgDistinct)
, bitAnd, bitOr, boolAnd, boolOr
, bitAndDistinct, bitOrDistinct, boolAndDistinct, boolOrDistinct
, countStar
, count, countDistinct
, every, everyDistinct
, max_, maxDistinct, min_, minDistinct
, TypeExpression (UnsafeTypeExpression, renderTypeExpression)
, PGTyped (pgtype)
, typedef
, typetable
, typeview
, bool
, int2
, smallint
, int4
, int
, integer
, int8
, bigint
, numeric
, float4
, real
, float8
, doublePrecision
, text
, char
, character
, varchar
, characterVarying
, bytea
, timestamp
, timestampWithTimeZone
, date
, time
, timeWithTimeZone
, interval
, uuid
, inet
, json
, jsonb
, vararray
, fixarray
, (&)
, NP ((:*), Nil)
) where
import Control.Category
import Control.DeepSeq
import Data.ByteString (ByteString)
import Data.ByteString.Lazy (toStrict)
import Data.Function ((&))
import Data.Semigroup hiding (All)
import qualified Data.Aeson as JSON
import Data.Ratio
import Data.String
import Data.Word
import Generics.SOP hiding (from)
import GHC.OverloadedLabels
import GHC.TypeLits
import Prelude hiding (id, (.))
import qualified GHC.Generics as GHC
import Squeal.PostgreSQL.Render
import Squeal.PostgreSQL.Schema
newtype Expression
(schema :: SchemaType)
(from :: FromType)
(grouping :: Grouping)
(params :: [NullityType])
(ty :: NullityType)
= UnsafeExpression { renderExpression :: ByteString }
deriving (GHC.Generic,Show,Eq,Ord,NFData)
instance RenderSQL (Expression schema from grouping params ty) where
renderSQL = renderExpression
class KnownNat n => HasParameter
(n :: Nat)
(schema :: SchemaType)
(params :: [NullityType])
(ty :: NullityType)
| n params -> ty where
parameter
:: TypeExpression schema ty
-> Expression schema from grouping params ty
parameter ty = UnsafeExpression $ parenthesized $
"$" <> renderNat @n <+> "::"
<+> renderTypeExpression ty
instance {-# OVERLAPPING #-} HasParameter 1 schema (ty1:tys) ty1
instance {-# OVERLAPPABLE #-} (KnownNat n, HasParameter (n-1) schema params ty)
=> HasParameter n schema (ty' : params) ty
param
:: forall n schema params from grouping ty
. (PGTyped schema ty, HasParameter n schema params ty)
=> Expression schema from grouping params ty
param = parameter @n pgtype
instance (HasUnique table from columns, Has column columns ty)
=> IsLabel column (Expression schema from 'Ungrouped params ty) where
fromLabel = UnsafeExpression $ renderAlias (Alias @column)
instance (HasUnique table from columns, Has column columns ty)
=> IsLabel column
(Aliased (Expression schema from 'Ungrouped params) (column ::: ty)) where
fromLabel = fromLabel @column `As` Alias @column
instance (HasUnique table from columns, Has column columns ty)
=> IsLabel column
(NP (Aliased (Expression schema from 'Ungrouped params)) '[column ::: ty]) where
fromLabel = fromLabel @column :* Nil
instance (Has table from columns, Has column columns ty)
=> IsQualified table column (Expression schema from 'Ungrouped params ty) where
table ! column = UnsafeExpression $
renderAlias table <> "." <> renderAlias column
instance (Has table from columns, Has column columns ty)
=> IsQualified table column
(Aliased (Expression schema from 'Ungrouped params) (column ::: ty)) where
table ! column = table ! column `As` column
instance (Has table from columns, Has column columns ty)
=> IsQualified table column
(NP (Aliased (Expression schema from 'Ungrouped params)) '[column ::: ty]) where
table ! column = table ! column :* Nil
instance
( HasUnique table from columns
, Has column columns ty
, GroupedBy table column bys
) => IsLabel column
(Expression schema from ('Grouped bys) params ty) where
fromLabel = UnsafeExpression $ renderAlias (Alias @column)
instance
( HasUnique table from columns
, Has column columns ty
, GroupedBy table column bys
) => IsLabel column
( Aliased (Expression schema from ('Grouped bys) params)
(column ::: ty) ) where
fromLabel = fromLabel @column `As` Alias @column
instance
( HasUnique table from columns
, Has column columns ty
, GroupedBy table column bys
) => IsLabel column
( NP (Aliased (Expression schema from ('Grouped bys) params))
'[column ::: ty] ) where
fromLabel = fromLabel @column :* Nil
instance
( Has table from columns
, Has column columns ty
, GroupedBy table column bys
) => IsQualified table column
(Expression schema from ('Grouped bys) params ty) where
table ! column = UnsafeExpression $
renderAlias table <> "." <> renderAlias column
instance
( Has table from columns
, Has column columns ty
, GroupedBy table column bys
) => IsQualified table column
(Aliased (Expression schema from ('Grouped bys) params)
(column ::: ty)) where
table ! column = table ! column `As` column
instance
( Has table from columns
, Has column columns ty
, GroupedBy table column bys
) => IsQualified table column
( NP (Aliased (Expression schema from ('Grouped bys) params))
'[column ::: ty]) where
table ! column = table ! column :* Nil
null_ :: Expression schema rels grouping params ('Null ty)
null_ = UnsafeExpression "NULL"
notNull
:: Expression schema rels grouping params ('NotNull ty)
-> Expression schema rels grouping params ('Null ty)
notNull = UnsafeExpression . renderExpression
coalesce
:: [Expression schema from grouping params ('Null ty)]
-> Expression schema from grouping params ('NotNull ty)
-> Expression schema from grouping params ('NotNull ty)
coalesce nullxs notNullx = UnsafeExpression $
"COALESCE" <> parenthesized (commaSeparated
((renderExpression <$> nullxs) <> [renderExpression notNullx]))
fromNull
:: Expression schema from grouping params ('NotNull ty)
-> Expression schema from grouping params ('Null ty)
-> Expression schema from grouping params ('NotNull ty)
fromNull notNullx nullx = coalesce [nullx] notNullx
isNull
:: Expression schema from grouping params ('Null ty)
-> Condition schema from grouping params
isNull x = UnsafeExpression $ renderExpression x <+> "IS NULL"
isNotNull
:: Expression schema from grouping params ('Null ty)
-> Condition schema from grouping params
isNotNull x = UnsafeExpression $ renderExpression x <+> "IS NOT NULL"
matchNull
:: Expression schema from grouping params (nullty)
-> ( Expression schema from grouping params ('NotNull ty)
-> Expression schema from grouping params (nullty) )
-> Expression schema from grouping params ('Null ty)
-> Expression schema from grouping params (nullty)
matchNull y f x = ifThenElse (isNull x) y
(f (UnsafeExpression (renderExpression x)))
nullIf
:: Expression schema from grouping params ('NotNull ty)
-> Expression schema from grouping params ('NotNull ty)
-> Expression schema from grouping params ('Null ty)
nullIf x y = UnsafeExpression $ "NULL IF" <+> parenthesized
(renderExpression x <> ", " <> renderExpression y)
array
:: [Expression schema from grouping params ty]
-> Expression schema from grouping params (nullity ('PGvararray ty))
array xs = UnsafeExpression $
"ARRAY[" <> commaSeparated (renderExpression <$> xs) <> "]"
index
:: Word64
-> Expression schema from grouping params (nullity ('PGvararray ty))
-> Expression schema from grouping params (NullifyType ty)
index n expr = UnsafeExpression $
parenthesized (renderExpression expr) <> "[" <> fromString (show n) <> "]"
instance (KnownSymbol label, label `In` labels) => IsPGlabel label
(Expression schema from grouping params (nullity ('PGenum labels))) where
label = UnsafeExpression $ renderLabel (PGlabel @label)
row
:: SListI row
=> NP (Aliased (Expression schema from grouping params)) row
-> Expression schema from grouping params (nullity ('PGcomposite row))
row exprs = UnsafeExpression $ "ROW" <> parenthesized
(renderCommaSeparated (\ (expr `As` _) -> renderExpression expr) exprs)
field
:: (Has tydef schema ('Typedef ('PGcomposite row)), Has field row ty)
=> Alias tydef
-> Alias field
-> Expression schema from grouping params ('NotNull ('PGcomposite row))
-> Expression schema from grouping params ty
field td fld expr = UnsafeExpression $
parenthesized (renderExpression expr <> "::" <> renderAlias td)
<> "." <> renderAlias fld
instance Semigroup
(Expression schema from grouping params (nullity ('PGvararray ty))) where
(<>) = unsafeBinaryOp "||"
instance Monoid
(Expression schema from grouping params (nullity ('PGvararray ty))) where
mempty = array []
mappend = (<>)
greatest
:: Expression schema from grouping params (nullty)
-> [Expression schema from grouping params (nullty)]
-> Expression schema from grouping params (nullty)
greatest x xs = UnsafeExpression $ "GREATEST("
<> commaSeparated (renderExpression <$> (x:xs)) <> ")"
least
:: Expression schema from grouping params (nullty)
-> [Expression schema from grouping params (nullty)]
-> Expression schema from grouping params (nullty)
least x xs = UnsafeExpression $ "LEAST("
<> commaSeparated (renderExpression <$> (x:xs)) <> ")"
unsafeBinaryOp
:: ByteString
-> Expression schema from grouping params (ty0)
-> Expression schema from grouping params (ty1)
-> Expression schema from grouping params (ty2)
unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
renderExpression x <+> op <+> renderExpression y
unsafeUnaryOp
:: ByteString
-> Expression schema from grouping params (ty0)
-> Expression schema from grouping params (ty1)
unsafeUnaryOp op x = UnsafeExpression $ parenthesized $
op <+> renderExpression x
unsafeFunction
:: ByteString
-> Expression schema from grouping params (xty)
-> Expression schema from grouping params (yty)
unsafeFunction fun x = UnsafeExpression $
fun <> parenthesized (renderExpression x)
unsafeVariadicFunction
:: SListI elems
=> ByteString
-> NP (Expression schema from grouping params) elems
-> Expression schema from grouping params ret
unsafeVariadicFunction fun x = UnsafeExpression $
fun <> parenthesized (commaSeparated (hcollapse (hmap (K . renderExpression) x)))
instance ty `In` PGNum
=> Num (Expression schema from grouping params (nullity ty)) where
(+) = unsafeBinaryOp "+"
(-) = unsafeBinaryOp "-"
(*) = unsafeBinaryOp "*"
abs = unsafeFunction "abs"
signum = unsafeFunction "sign"
fromInteger
= UnsafeExpression
. fromString
. show
instance (ty `In` PGNum, ty `In` PGFloating) => Fractional
(Expression schema from grouping params (nullity ty)) where
(/) = unsafeBinaryOp "/"
fromRational x = fromInteger (numerator x) / fromInteger (denominator x)
instance (ty `In` PGNum, ty `In` PGFloating) => Floating
(Expression schema from grouping params (nullity ty)) where
pi = UnsafeExpression "pi()"
exp = unsafeFunction "exp"
log = unsafeFunction "ln"
sqrt = unsafeFunction "sqrt"
b ** x = UnsafeExpression $
"power(" <> renderExpression b <> ", " <> renderExpression x <> ")"
logBase b y = log y / log b
sin = unsafeFunction "sin"
cos = unsafeFunction "cos"
tan = unsafeFunction "tan"
asin = unsafeFunction "asin"
acos = unsafeFunction "acos"
atan = unsafeFunction "atan"
sinh x = (exp x - exp (-x)) / 2
cosh x = (exp x + exp (-x)) / 2
tanh x = sinh x / cosh x
asinh x = log (x + sqrt (x*x + 1))
acosh x = log (x + sqrt (x*x - 1))
atanh x = log ((1 + x) / (1 - x)) / 2
atan2_
:: float `In` PGFloating
=> Expression schema from grouping params (nullity float)
-> Expression schema from grouping params (nullity float)
-> Expression schema from grouping params (nullity float)
atan2_ y x = UnsafeExpression $
"atan2(" <> renderExpression y <> ", " <> renderExpression x <> ")"
cast
:: TypeExpression schema ty1
-> Expression schema from grouping params ty0
-> Expression schema from grouping params ty1
cast ty x = UnsafeExpression $ parenthesized $
renderExpression x <+> "::" <+> renderTypeExpression ty
quot_
:: int `In` PGIntegral
=> Expression schema from grouping params (nullity int)
-> Expression schema from grouping params (nullity int)
-> Expression schema from grouping params (nullity int)
quot_ = unsafeBinaryOp "/"
rem_
:: int `In` PGIntegral
=> Expression schema from grouping params (nullity int)
-> Expression schema from grouping params (nullity int)
-> Expression schema from grouping params (nullity int)
rem_ = unsafeBinaryOp "%"
trunc
:: frac `In` PGFloating
=> Expression schema from grouping params (nullity frac)
-> Expression schema from grouping params (nullity frac)
trunc = unsafeFunction "trunc"
round_
:: frac `In` PGFloating
=> Expression schema from grouping params (nullity frac)
-> Expression schema from grouping params (nullity frac)
round_ = unsafeFunction "round"
ceiling_
:: frac `In` PGFloating
=> Expression schema from grouping params (nullity frac)
-> Expression schema from grouping params (nullity frac)
ceiling_ = unsafeFunction "ceiling"
type Condition schema from grouping params =
Expression schema from grouping params ('Null 'PGbool)
true :: Expression schema from grouping params (nullity 'PGbool)
true = UnsafeExpression "TRUE"
false :: Expression schema from grouping params (nullity 'PGbool)
false = UnsafeExpression "FALSE"
not_
:: Expression schema from grouping params (nullity 'PGbool)
-> Expression schema from grouping params (nullity 'PGbool)
not_ = unsafeUnaryOp "NOT"
(.&&)
:: Expression schema from grouping params (nullity 'PGbool)
-> Expression schema from grouping params (nullity 'PGbool)
-> Expression schema from grouping params (nullity 'PGbool)
infixr 3 .&&
(.&&) = unsafeBinaryOp "AND"
(.||)
:: Expression schema from grouping params (nullity 'PGbool)
-> Expression schema from grouping params (nullity 'PGbool)
-> Expression schema from grouping params (nullity 'PGbool)
infixr 2 .||
(.||) = unsafeBinaryOp "OR"
caseWhenThenElse
:: [ ( Condition schema from grouping params
, Expression schema from grouping params ty
) ]
-> Expression schema from grouping params ty
-> Expression schema from grouping params ty
caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
[ "CASE"
, mconcat
[ mconcat
[ " WHEN ", renderExpression when_
, " THEN ", renderExpression then_
]
| (when_,then_) <- whenThens
]
, " ELSE ", renderExpression else_
, " END"
]
ifThenElse
:: Condition schema from grouping params
-> Expression schema from grouping params ty
-> Expression schema from grouping params ty
-> Expression schema from grouping params ty
ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
(.==)
:: Expression schema from grouping params (nullity0 ty)
-> Expression schema from grouping params (nullity1 ty)
-> Condition schema from grouping params
(.==) = unsafeBinaryOp "="
infix 4 .==
(./=)
:: Expression schema from grouping params (nullity0 ty)
-> Expression schema from grouping params (nullity1 ty)
-> Condition schema from grouping params
(./=) = unsafeBinaryOp "<>"
infix 4 ./=
(.>=)
:: Expression schema from grouping params (nullity0 ty)
-> Expression schema from grouping params (nullity1 ty)
-> Condition schema from grouping params
(.>=) = unsafeBinaryOp ">="
infix 4 .>=
(.<)
:: Expression schema from grouping params (nullity0 ty)
-> Expression schema from grouping params (nullity1 ty)
-> Condition schema from grouping params
(.<) = unsafeBinaryOp "<"
infix 4 .<
(.<=)
:: Expression schema from grouping params (nullity0 ty)
-> Expression schema from grouping params (nullity1 ty)
-> Condition schema from grouping params
(.<=) = unsafeBinaryOp "<="
infix 4 .<=
(.>)
:: Expression schema from grouping params (nullity0 ty)
-> Expression schema from grouping params (nullity1 ty)
-> Condition schema from grouping params
(.>) = unsafeBinaryOp ">"
infix 4 .>
currentDate
:: Expression schema from grouping params (nullity 'PGdate)
currentDate = UnsafeExpression "CURRENT_DATE"
currentTime
:: Expression schema from grouping params (nullity 'PGtimetz)
currentTime = UnsafeExpression "CURRENT_TIME"
currentTimestamp
:: Expression schema from grouping params (nullity 'PGtimestamptz)
currentTimestamp = UnsafeExpression "CURRENT_TIMESTAMP"
localTime
:: Expression schema from grouping params (nullity 'PGtime)
localTime = UnsafeExpression "LOCALTIME"
localTimestamp
:: Expression schema from grouping params (nullity 'PGtimestamp)
localTimestamp = UnsafeExpression "LOCALTIMESTAMP"
instance IsString
(Expression schema from grouping params (nullity 'PGtext)) where
fromString str = UnsafeExpression $
"E\'" <> fromString (escape =<< str) <> "\'"
where
escape = \case
'\NUL' -> "\\0"
'\'' -> "''"
'"' -> "\\\""
'\b' -> "\\b"
'\n' -> "\\n"
'\r' -> "\\r"
'\t' -> "\\t"
'\\' -> "\\\\"
c -> [c]
instance Semigroup
(Expression schema from grouping params (nullity 'PGtext)) where
(<>) = unsafeBinaryOp "||"
instance Monoid
(Expression schema from grouping params (nullity 'PGtext)) where
mempty = fromString ""
mappend = (<>)
lower
:: Expression schema from grouping params (nullity 'PGtext)
-> Expression schema from grouping params (nullity 'PGtext)
lower = unsafeFunction "lower"
upper
:: Expression schema from grouping params (nullity 'PGtext)
-> Expression schema from grouping params (nullity 'PGtext)
upper = unsafeFunction "upper"
charLength
:: Expression schema from grouping params (nullity 'PGtext)
-> Expression schema from grouping params (nullity 'PGint4)
charLength = unsafeFunction "char_length"
like
:: Expression schema from grouping params (nullity 'PGtext)
-> Expression schema from grouping params (nullity 'PGtext)
-> Expression schema from grouping params (nullity 'PGbool)
like = unsafeBinaryOp "LIKE"
(.->)
:: (json `In` PGJsonType, key `In` PGJsonKey)
=> Expression schema from grouping params (nullity json)
-> Expression schema from grouping params (nullity key)
-> Expression schema from grouping params ('Null json)
infixl 8 .->
(.->) = unsafeBinaryOp "->"
(.->>)
:: (json `In` PGJsonType, key `In` PGJsonKey)
=> Expression schema from grouping params (nullity json)
-> Expression schema from grouping params (nullity key)
-> Expression schema from grouping params ('Null 'PGtext)
infixl 8 .->>
(.->>) = unsafeBinaryOp "->>"
(.#>)
:: (json `In` PGJsonType, PGTextArray "(.#>)" path)
=> Expression schema from grouping params (nullity json)
-> Expression schema from grouping params (nullity path)
-> Expression schema from grouping params ('Null json)
infixl 8 .#>
(.#>) = unsafeBinaryOp "#>"
(.#>>)
:: (json `In` PGJsonType, PGTextArray "(.#>>)" path)
=> Expression schema from grouping params (nullity json)
-> Expression schema from grouping params (nullity path)
-> Expression schema from grouping params ('Null 'PGtext)
infixl 8 .#>>
(.#>>) = unsafeBinaryOp "#>>"
(.@>)
:: Expression schema from grouping params (nullity 'PGjsonb)
-> Expression schema from grouping params (nullity 'PGjsonb)
-> Condition schema from grouping params
infixl 9 .@>
(.@>) = unsafeBinaryOp "@>"
(.<@)
:: Expression schema from grouping params (nullity 'PGjsonb)
-> Expression schema from grouping params (nullity 'PGjsonb)
-> Condition schema from grouping params
infixl 9 .<@
(.<@) = unsafeBinaryOp "<@"
(.?)
:: Expression schema from grouping params (nullity 'PGjsonb)
-> Expression schema from grouping params (nullity 'PGtext)
-> Condition schema from grouping params
infixl 9 .?
(.?) = unsafeBinaryOp "?"
(.?|)
:: Expression schema from grouping params (nullity 'PGjsonb)
-> Expression schema from grouping params (nullity ('PGvararray ('NotNull 'PGtext)))
-> Condition schema from grouping params
infixl 9 .?|
(.?|) = unsafeBinaryOp "?|"
(.?&)
:: Expression schema from grouping params (nullity 'PGjsonb)
-> Expression schema from grouping params (nullity ('PGvararray ('NotNull 'PGtext)))
-> Condition schema from grouping params
infixl 9 .?&
(.?&) = unsafeBinaryOp "?&"
instance
Semigroup (Expression schema from grouping param (nullity 'PGjsonb)) where
(<>) = unsafeBinaryOp "||"
(.-.)
:: (key `In` '[ 'PGtext, 'PGvararray ('NotNull 'PGtext), 'PGint4, 'PGint2 ])
=> Expression schema from grouping params (nullity 'PGjsonb)
-> Expression schema from grouping params (nullity key)
-> Expression schema from grouping params (nullity 'PGjsonb)
infixl 6 .-.
(.-.) = unsafeBinaryOp "-"
(#-.)
:: PGTextArray "(#-.)" arrayty
=> Expression schema from grouping params (nullity 'PGjsonb)
-> Expression schema from grouping params (nullity arrayty)
-> Expression schema from grouping params (nullity 'PGjsonb)
infixl 6 #-.
(#-.) = unsafeBinaryOp "#-"
jsonbLit
:: JSON.ToJSON x
=> x -> Expression schema from grouping params (nullity 'PGjsonb)
jsonbLit = cast jsonb . UnsafeExpression
. singleQuotedUtf8 . toStrict . JSON.encode
jsonLit
:: JSON.ToJSON x
=> x -> Expression schema from grouping params (nullity 'PGjson)
jsonLit = cast json . UnsafeExpression
. singleQuotedUtf8 . toStrict . JSON.encode
toJson
:: Expression schema from grouping params (nullity ty)
-> Expression schema from grouping params (nullity 'PGjson)
toJson = unsafeFunction "to_json"
toJsonb
:: Expression schema from grouping params (nullity ty)
-> Expression schema from grouping params (nullity 'PGjsonb)
toJsonb = unsafeFunction "to_jsonb"
arrayToJson
:: PGArray "arrayToJson" arr
=> Expression schema from grouping params (nullity arr)
-> Expression schema from grouping params (nullity 'PGjson)
arrayToJson = unsafeFunction "array_to_json"
rowToJson
:: Expression schema from grouping params (nullity ('PGcomposite ty))
-> Expression schema from grouping params (nullity 'PGjson)
rowToJson = unsafeFunction "row_to_json"
jsonBuildArray
:: SListI elems
=> NP (Expression schema from grouping params) elems
-> Expression schema from grouping params (nullity 'PGjson)
jsonBuildArray = unsafeVariadicFunction "json_build_array"
jsonbBuildArray
:: SListI elems
=> NP (Expression schema from grouping params) elems
-> Expression schema from grouping params (nullity 'PGjsonb)
jsonbBuildArray = unsafeVariadicFunction "jsonb_build_array"
unsafeRowFunction
:: All Top elems
=> NP (Aliased (Expression schema from grouping params)) elems
-> [ByteString]
unsafeRowFunction =
(`appEndo` []) . hcfoldMap (Proxy :: Proxy Top)
(\(col `As` name) -> Endo $ \xs ->
renderAliasString name : renderExpression col : xs)
jsonBuildObject
:: All Top elems
=> NP (Aliased (Expression schema from grouping params)) elems
-> Expression schema from grouping params (nullity 'PGjson)
jsonBuildObject
= unsafeFunction "json_build_object"
. UnsafeExpression
. commaSeparated
. unsafeRowFunction
jsonbBuildObject
:: All Top elems
=> NP (Aliased (Expression schema from grouping params)) elems
-> Expression schema from grouping params (nullity 'PGjsonb)
jsonbBuildObject
= unsafeFunction "jsonb_build_object"
. UnsafeExpression
. commaSeparated
. unsafeRowFunction
jsonObject
:: PGArrayOf "jsonObject" arr ('NotNull 'PGtext)
=> Expression schema from grouping params (nullity arr)
-> Expression schema from grouping params (nullity 'PGjson)
jsonObject = unsafeFunction "json_object"
jsonbObject
:: PGArrayOf "jsonbObject" arr ('NotNull 'PGtext)
=> Expression schema from grouping params (nullity arr)
-> Expression schema from grouping params (nullity 'PGjsonb)
jsonbObject = unsafeFunction "jsonb_object"
jsonZipObject
:: ( PGArrayOf "jsonZipObject" keysArray ('NotNull 'PGtext)
, PGArrayOf "jsonZipObject" valuesArray ('NotNull 'PGtext))
=> Expression schema from grouping params (nullity keysArray)
-> Expression schema from grouping params (nullity valuesArray)
-> Expression schema from grouping params (nullity 'PGjson)
jsonZipObject ks vs =
unsafeVariadicFunction "json_object" (ks :* vs :* Nil)
jsonbZipObject
:: ( PGArrayOf "jsonbZipObject" keysArray ('NotNull 'PGtext)
, PGArrayOf "jsonbZipObject" valuesArray ('NotNull 'PGtext))
=> Expression schema from grouping params (nullity keysArray)
-> Expression schema from grouping params (nullity valuesArray)
-> Expression schema from grouping params (nullity 'PGjsonb)
jsonbZipObject ks vs =
unsafeVariadicFunction "jsonb_object" (ks :* vs :* Nil)
jsonArrayLength
:: Expression schema from grouping params (nullity 'PGjson)
-> Expression schema from grouping params (nullity 'PGint4)
jsonArrayLength = unsafeFunction "json_array_length"
jsonbArrayLength
:: Expression schema from grouping params (nullity 'PGjsonb)
-> Expression schema from grouping params (nullity 'PGint4)
jsonbArrayLength = unsafeFunction "jsonb_array_length"
jsonExtractPath
:: SListI elems
=> Expression schema from grouping params (nullity 'PGjson)
-> NP (Expression schema from grouping params) elems
-> Expression schema from grouping params (nullity 'PGjsonb)
jsonExtractPath x xs =
unsafeVariadicFunction "json_extract_path" (x :* xs)
jsonbExtractPath
:: SListI elems
=> Expression schema from grouping params (nullity 'PGjsonb)
-> NP (Expression schema from grouping params) elems
-> Expression schema from grouping params (nullity 'PGjsonb)
jsonbExtractPath x xs =
unsafeVariadicFunction "jsonb_extract_path" (x :* xs)
jsonExtractPathAsText
:: SListI elems
=> Expression schema from grouping params (nullity 'PGjson)
-> NP (Expression schema from grouping params) elems
-> Expression schema from grouping params (nullity 'PGjson)
jsonExtractPathAsText x xs =
unsafeVariadicFunction "json_extract_path_text" (x :* xs)
jsonbExtractPathAsText
:: SListI elems
=> Expression schema from grouping params (nullity 'PGjsonb)
-> NP (Expression schema from grouping params) elems
-> Expression schema from grouping params (nullity 'PGjsonb)
jsonbExtractPathAsText x xs =
unsafeVariadicFunction "jsonb_extract_path_text" (x :* xs)
jsonTypeof
:: Expression schema from grouping params (nullity 'PGjson)
-> Expression schema from grouping params (nullity 'PGtext)
jsonTypeof = unsafeFunction "json_typeof"
jsonbTypeof
:: Expression schema from grouping params (nullity 'PGjsonb)
-> Expression schema from grouping params (nullity 'PGtext)
jsonbTypeof = unsafeFunction "jsonb_typeof"
jsonStripNulls
:: Expression schema from grouping params (nullity 'PGjson)
-> Expression schema from grouping params (nullity 'PGjson)
jsonStripNulls = unsafeFunction "json_strip_nulls"
jsonbStripNulls
:: Expression schema from grouping params (nullity 'PGjsonb)
-> Expression schema from grouping params (nullity 'PGjsonb)
jsonbStripNulls = unsafeFunction "jsonb_strip_nulls"
jsonbSet
:: PGTextArray "jsonbSet" arr
=> Expression schema from grouping params (nullity 'PGjsonb)
-> Expression schema from grouping params (nullity arr)
-> Expression schema from grouping params (nullity 'PGjsonb)
-> Maybe (Expression schema from grouping params (nullity 'PGbool))
-> Expression schema from grouping params (nullity 'PGjsonb)
jsonbSet tgt path val createMissing = case createMissing of
Just m -> unsafeVariadicFunction "jsonb_set" (tgt :* path :* val :* m :* Nil)
Nothing -> unsafeVariadicFunction "jsonb_set" (tgt :* path :* val :* Nil)
jsonbInsert
:: PGTextArray "jsonbInsert" arr
=> Expression schema from grouping params (nullity 'PGjsonb)
-> Expression schema from grouping params (nullity arr)
-> Expression schema from grouping params (nullity 'PGjsonb)
-> Maybe (Expression schema from grouping params (nullity 'PGbool))
-> Expression schema from grouping params (nullity 'PGjsonb)
jsonbInsert tgt path val insertAfter = case insertAfter of
Just i -> unsafeVariadicFunction "jsonb_insert" (tgt :* path :* val :* i :* Nil)
Nothing -> unsafeVariadicFunction "jsonb_insert" (tgt :* path :* val :* Nil)
jsonbPretty
:: Expression schema from grouping params (nullity 'PGjsonb)
-> Expression schema from grouping params (nullity 'PGtext)
jsonbPretty = unsafeFunction "jsonb_pretty"
unsafeAggregate
:: ByteString
-> Expression schema from 'Ungrouped params (xty)
-> Expression schema from ('Grouped bys) params (yty)
unsafeAggregate fun x = UnsafeExpression $ mconcat
[fun, "(", renderExpression x, ")"]
unsafeAggregateDistinct
:: ByteString
-> Expression schema from 'Ungrouped params (xty)
-> Expression schema from ('Grouped bys) params (yty)
unsafeAggregateDistinct fun x = UnsafeExpression $ mconcat
[fun, "(DISTINCT ", renderExpression x, ")"]
sum_
:: ty `In` PGNum
=> Expression schema from 'Ungrouped params (nullity ty)
-> Expression schema from ('Grouped bys) params (nullity ty)
sum_ = unsafeAggregate "sum"
sumDistinct
:: ty `In` PGNum
=> Expression schema from 'Ungrouped params (nullity ty)
-> Expression schema from ('Grouped bys) params (nullity ty)
sumDistinct = unsafeAggregateDistinct "sum"
class PGAvg ty avg | ty -> avg where
avg, avgDistinct
:: Expression schema from 'Ungrouped params (nullity ty)
-> Expression schema from ('Grouped bys) params (nullity avg)
avg = unsafeAggregate "avg"
avgDistinct = unsafeAggregateDistinct "avg"
instance PGAvg 'PGint2 'PGnumeric
instance PGAvg 'PGint4 'PGnumeric
instance PGAvg 'PGint8 'PGnumeric
instance PGAvg 'PGnumeric 'PGnumeric
instance PGAvg 'PGfloat4 'PGfloat8
instance PGAvg 'PGfloat8 'PGfloat8
instance PGAvg 'PGinterval 'PGinterval
bitAnd
:: int `In` PGIntegral
=> Expression schema from 'Ungrouped params (nullity int)
-> Expression schema from ('Grouped bys) params (nullity int)
bitAnd = unsafeAggregate "bit_and"
bitOr
:: int `In` PGIntegral
=> Expression schema from 'Ungrouped params (nullity int)
-> Expression schema from ('Grouped bys) params (nullity int)
bitOr = unsafeAggregate "bit_or"
bitAndDistinct
:: int `In` PGIntegral
=> Expression schema from 'Ungrouped params (nullity int)
-> Expression schema from ('Grouped bys) params (nullity int)
bitAndDistinct = unsafeAggregateDistinct "bit_and"
bitOrDistinct
:: int `In` PGIntegral
=> Expression schema from 'Ungrouped params (nullity int)
-> Expression schema from ('Grouped bys) params (nullity int)
bitOrDistinct = unsafeAggregateDistinct "bit_or"
boolAnd
:: Expression schema from 'Ungrouped params (nullity 'PGbool)
-> Expression schema from ('Grouped bys) params (nullity 'PGbool)
boolAnd = unsafeAggregate "bool_and"
boolOr
:: Expression schema from 'Ungrouped params (nullity 'PGbool)
-> Expression schema from ('Grouped bys) params (nullity 'PGbool)
boolOr = unsafeAggregate "bool_or"
boolAndDistinct
:: Expression schema from 'Ungrouped params (nullity 'PGbool)
-> Expression schema from ('Grouped bys) params (nullity 'PGbool)
boolAndDistinct = unsafeAggregateDistinct "bool_and"
boolOrDistinct
:: Expression schema from 'Ungrouped params (nullity 'PGbool)
-> Expression schema from ('Grouped bys) params (nullity 'PGbool)
boolOrDistinct = unsafeAggregateDistinct "bool_or"
countStar
:: Expression schema from ('Grouped bys) params ('NotNull 'PGint8)
countStar = UnsafeExpression $ "count(*)"
count
:: Expression schema from 'Ungrouped params ty
-> Expression schema from ('Grouped bys) params ('NotNull 'PGint8)
count = unsafeAggregate "count"
countDistinct
:: Expression schema from 'Ungrouped params ty
-> Expression schema from ('Grouped bys) params ('NotNull 'PGint8)
countDistinct = unsafeAggregateDistinct "count"
every
:: Expression schema from 'Ungrouped params (nullity 'PGbool)
-> Expression schema from ('Grouped bys) params (nullity 'PGbool)
every = unsafeAggregate "every"
everyDistinct
:: Expression schema from 'Ungrouped params (nullity 'PGbool)
-> Expression schema from ('Grouped bys) params (nullity 'PGbool)
everyDistinct = unsafeAggregateDistinct "every"
max_, min_, maxDistinct, minDistinct
:: Expression schema from 'Ungrouped params (nullity ty)
-> Expression schema from ('Grouped bys) params (nullity ty)
max_ = unsafeAggregate "max"
min_ = unsafeAggregate "min"
maxDistinct = unsafeAggregateDistinct "max"
minDistinct = unsafeAggregateDistinct "min"
newtype TypeExpression (schema :: SchemaType) (ty :: NullityType)
= UnsafeTypeExpression { renderTypeExpression :: ByteString }
deriving (GHC.Generic,Show,Eq,Ord,NFData)
typedef
:: Has alias schema ('Typedef ty)
=> Alias alias
-> TypeExpression schema (nullity ty)
typedef = UnsafeTypeExpression . renderAlias
typetable
:: Has alias schema ('Table tab)
=> Alias alias
-> TypeExpression schema (nullity ('PGcomposite (TableToRow tab)))
typetable = UnsafeTypeExpression . renderAlias
typeview
:: Has alias schema ('View view)
=> Alias alias
-> TypeExpression schema (nullity ('PGcomposite view))
typeview = UnsafeTypeExpression . renderAlias
bool :: TypeExpression schema (nullity 'PGbool)
bool = UnsafeTypeExpression "bool"
int2, smallint :: TypeExpression schema (nullity 'PGint2)
int2 = UnsafeTypeExpression "int2"
smallint = UnsafeTypeExpression "smallint"
int4, int, integer :: TypeExpression schema (nullity 'PGint4)
int4 = UnsafeTypeExpression "int4"
int = UnsafeTypeExpression "int"
integer = UnsafeTypeExpression "integer"
int8, bigint :: TypeExpression schema (nullity 'PGint8)
int8 = UnsafeTypeExpression "int8"
bigint = UnsafeTypeExpression "bigint"
numeric :: TypeExpression schema (nullity 'PGnumeric)
numeric = UnsafeTypeExpression "numeric"
float4, real :: TypeExpression schema (nullity 'PGfloat4)
float4 = UnsafeTypeExpression "float4"
real = UnsafeTypeExpression "real"
float8, doublePrecision :: TypeExpression schema (nullity 'PGfloat8)
float8 = UnsafeTypeExpression "float8"
doublePrecision = UnsafeTypeExpression "double precision"
text :: TypeExpression schema (nullity 'PGtext)
text = UnsafeTypeExpression "text"
char, character
:: forall n schema nullity. (KnownNat n, 1 <= n)
=> TypeExpression schema (nullity ('PGchar n))
char = UnsafeTypeExpression $ "char(" <> renderNat @n <> ")"
character = UnsafeTypeExpression $ "character(" <> renderNat @n <> ")"
varchar, characterVarying
:: forall n schema nullity. (KnownNat n, 1 <= n)
=> TypeExpression schema (nullity ('PGvarchar n))
varchar = UnsafeTypeExpression $ "varchar(" <> renderNat @n <> ")"
characterVarying = UnsafeTypeExpression $
"character varying(" <> renderNat @n <> ")"
bytea :: TypeExpression schema (nullity 'PGbytea)
bytea = UnsafeTypeExpression "bytea"
timestamp :: TypeExpression schema (nullity 'PGtimestamp)
timestamp = UnsafeTypeExpression "timestamp"
timestampWithTimeZone :: TypeExpression schema (nullity 'PGtimestamptz)
timestampWithTimeZone = UnsafeTypeExpression "timestamp with time zone"
date :: TypeExpression schema (nullity 'PGdate)
date = UnsafeTypeExpression "date"
time :: TypeExpression schema (nullity 'PGtime)
time = UnsafeTypeExpression "time"
timeWithTimeZone :: TypeExpression schema (nullity 'PGtimetz)
timeWithTimeZone = UnsafeTypeExpression "time with time zone"
interval :: TypeExpression schema (nullity 'PGinterval)
interval = UnsafeTypeExpression "interval"
uuid :: TypeExpression schema (nullity 'PGuuid)
uuid = UnsafeTypeExpression "uuid"
inet :: TypeExpression schema (nullity 'PGinet)
inet = UnsafeTypeExpression "inet"
json :: TypeExpression schema (nullity 'PGjson)
json = UnsafeTypeExpression "json"
jsonb :: TypeExpression schema (nullity 'PGjsonb)
jsonb = UnsafeTypeExpression "jsonb"
vararray
:: TypeExpression schema pg
-> TypeExpression schema (nullity ('PGvararray pg))
vararray ty = UnsafeTypeExpression $ renderTypeExpression ty <> "[]"
fixarray
:: forall n schema nullity pg. KnownNat n
=> TypeExpression schema pg
-> TypeExpression schema (nullity ('PGfixarray n pg))
fixarray ty = UnsafeTypeExpression $
renderTypeExpression ty <> "[" <> renderNat @n <> "]"
class PGTyped schema (ty :: NullityType) where
pgtype :: TypeExpression schema ty
instance PGTyped schema (nullity 'PGbool) where pgtype = bool
instance PGTyped schema (nullity 'PGint2) where pgtype = int2
instance PGTyped schema (nullity 'PGint4) where pgtype = int4
instance PGTyped schema (nullity 'PGint8) where pgtype = int8
instance PGTyped schema (nullity 'PGnumeric) where pgtype = numeric
instance PGTyped schema (nullity 'PGfloat4) where pgtype = float4
instance PGTyped schema (nullity 'PGfloat8) where pgtype = float8
instance PGTyped schema (nullity 'PGtext) where pgtype = text
instance (KnownNat n, 1 <= n)
=> PGTyped schema (nullity ('PGchar n)) where pgtype = char @n
instance (KnownNat n, 1 <= n)
=> PGTyped schema (nullity ('PGvarchar n)) where pgtype = varchar @n
instance PGTyped schema (nullity 'PGbytea) where pgtype = bytea
instance PGTyped schema (nullity 'PGtimestamp) where pgtype = timestamp
instance PGTyped schema (nullity 'PGtimestamptz) where pgtype = timestampWithTimeZone
instance PGTyped schema (nullity 'PGdate) where pgtype = date
instance PGTyped schema (nullity 'PGtime) where pgtype = time
instance PGTyped schema (nullity 'PGtimetz) where pgtype = timeWithTimeZone
instance PGTyped schema (nullity 'PGinterval) where pgtype = interval
instance PGTyped schema (nullity 'PGuuid) where pgtype = uuid
instance PGTyped schema (nullity 'PGjson) where pgtype = json
instance PGTyped schema (nullity 'PGjsonb) where pgtype = jsonb
instance PGTyped schema ty
=> PGTyped schema (nullity ('PGvararray ty)) where
pgtype = vararray (pgtype @schema @ty)
instance (KnownNat n, PGTyped schema ty)
=> PGTyped schema (nullity ('PGfixarray n ty)) where
pgtype = fixarray @n (pgtype @schema @ty)