{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}
{-# LANGUAGE
AllowAmbiguousTypes
, DeriveGeneric
, FlexibleContexts
, FlexibleInstances
, FunctionalDependencies
, GeneralizedNewtypeDeriving
, LambdaCase
, MagicHash
, OverloadedStrings
, ScopedTypeVariables
, TypeApplications
, TypeFamilies
, TypeInType
, TypeOperators
, UndecidableInstances
#-}
module Squeal.PostgreSQL.Expression
(
Expression (UnsafeExpression, renderExpression)
, HasParameter (parameter)
, param
, null_
, notNull
, coalesce
, fromNull
, isNull
, isNotNull
, matchNull
, nullIf
, array
, row
, unsafeBinaryOp
, unsafeUnaryOp
, unsafeFunction
, atan2_
, cast
, quot_
, rem_
, trunc
, round_
, ceiling_
, greatest
, least
, Condition
, true
, false
, not_
, (.&&)
, (.||)
, caseWhenThenElse
, ifThenElse
, (.==)
, (./=)
, (.>=)
, (.<)
, (.<=)
, (.>)
, currentDate
, currentTime
, currentTimestamp
, localTime
, localTimestamp
, lower
, upper
, charLength
, like
, 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)
, 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.Function ((&))
import Data.Semigroup
import Data.Ratio
import Data.String
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)
(relations :: RelationsType)
(grouping :: Grouping)
(params :: [NullityType])
(ty :: NullityType)
= UnsafeExpression { renderExpression :: ByteString }
deriving (GHC.Generic,Show,Eq,Ord,NFData)
instance RenderSQL (Expression schema relations 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 (PGTypeOf ty)
-> Expression schema relations grouping params ty
parameter ty = UnsafeExpression $ parenthesized $
"$" <> renderNat (Proxy @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 relations grouping ty
. (PGTyped schema (PGTypeOf ty), HasParameter n schema params ty)
=> Expression schema relations grouping params ty
param = parameter @n pgtype
instance (HasUnique relation relations columns, Has column columns ty)
=> IsLabel column (Expression schema relations 'Ungrouped params ty) where
fromLabel = UnsafeExpression $ renderAlias (Alias @column)
instance (HasUnique relation relations columns, Has column columns ty)
=> IsLabel column
(Aliased (Expression schema relations 'Ungrouped params) (column ::: ty)) where
fromLabel = fromLabel @column `As` Alias @column
instance (HasUnique relation relations columns, Has column columns ty)
=> IsLabel column
(NP (Aliased (Expression schema relations 'Ungrouped params)) '[column ::: ty]) where
fromLabel = fromLabel @column :* Nil
instance (Has relation relations columns, Has column columns ty)
=> IsQualified relation column (Expression schema relations 'Ungrouped params ty) where
relation ! column = UnsafeExpression $
renderAlias relation <> "." <> renderAlias column
instance (Has relation relations columns, Has column columns ty)
=> IsQualified relation column
(Aliased (Expression schema relations 'Ungrouped params) (column ::: ty)) where
relation ! column = relation ! column `As` column
instance (Has relation relations columns, Has column columns ty)
=> IsQualified relation column
(NP (Aliased (Expression schema relations 'Ungrouped params)) '[column ::: ty]) where
relation ! column = relation ! column :* Nil
instance
( HasUnique relation relations columns
, Has column columns ty
, GroupedBy relation column bys
) => IsLabel column
(Expression schema relations ('Grouped bys) params ty) where
fromLabel = UnsafeExpression $ renderAlias (Alias @column)
instance
( HasUnique relation relations columns
, Has column columns ty
, GroupedBy relation column bys
) => IsLabel column
( Aliased (Expression schema relations ('Grouped bys) params)
(column ::: ty) ) where
fromLabel = fromLabel @column `As` Alias @column
instance
( HasUnique relation relations columns
, Has column columns ty
, GroupedBy relation column bys
) => IsLabel column
( NP (Aliased (Expression schema relations ('Grouped bys) params))
'[column ::: ty] ) where
fromLabel = fromLabel @column :* Nil
instance
( Has relation relations columns
, Has column columns ty
, GroupedBy relation column bys
) => IsQualified relation column
(Expression schema relations ('Grouped bys) params ty) where
relation ! column = UnsafeExpression $
renderAlias relation <> "." <> renderAlias column
instance
( Has relation relations columns
, Has column columns ty
, GroupedBy relation column bys
) => IsQualified relation column
(Aliased (Expression schema relations ('Grouped bys) params)
(column ::: ty)) where
relation ! column = relation ! column `As` column
instance
( Has relation relations columns
, Has column columns ty
, GroupedBy relation column bys
) => IsQualified relation column
( NP (Aliased (Expression schema relations ('Grouped bys) params))
'[column ::: ty]) where
relation ! column = relation ! 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 relations grouping params ('Null ty)]
-> Expression schema relations grouping params ('NotNull ty)
-> Expression schema relations grouping params ('NotNull ty)
coalesce nullxs notNullx = UnsafeExpression $
"COALESCE" <> parenthesized (commaSeparated
((renderExpression <$> nullxs) <> [renderExpression notNullx]))
fromNull
:: Expression schema relations grouping params ('NotNull ty)
-> Expression schema relations grouping params ('Null ty)
-> Expression schema relations grouping params ('NotNull ty)
fromNull notNullx nullx = coalesce [nullx] notNullx
isNull
:: Expression schema relations grouping params ('Null ty)
-> Condition schema relations grouping params
isNull x = UnsafeExpression $ renderExpression x <+> "IS NULL"
isNotNull
:: Expression schema relations grouping params ('Null ty)
-> Condition schema relations grouping params
isNotNull x = UnsafeExpression $ renderExpression x <+> "IS NOT NULL"
matchNull
:: Expression schema relations grouping params (nullty)
-> ( Expression schema relations grouping params ('NotNull ty)
-> Expression schema relations grouping params (nullty) )
-> Expression schema relations grouping params ('Null ty)
-> Expression schema relations grouping params (nullty)
matchNull y f x = ifThenElse (isNull x) y
(f (UnsafeExpression (renderExpression x)))
nullIf
:: Expression schema relations grouping params ('NotNull ty)
-> Expression schema relations grouping params ('NotNull ty)
-> Expression schema relations grouping params ('Null ty)
nullIf x y = UnsafeExpression $ "NULL IF" <+> parenthesized
(renderExpression x <> ", " <> renderExpression y)
array
:: [Expression schema relations grouping params ('Null ty)]
-> Expression schema relations grouping params (nullity ('PGvararray ty))
array xs = UnsafeExpression $
"ARRAY[" <> commaSeparated (renderExpression <$> xs) <> "]"
instance (KnownSymbol label, label `In` labels) => IsPGlabel label
(Expression schema relations grouping params (nullity ('PGenum labels))) where
label = UnsafeExpression $ renderLabel (PGlabel @label)
row
:: SListI (Nulls fields)
=> NP (Aliased (Expression schema relations grouping params)) (Nulls fields)
-> Expression schema relations grouping params (nullity ('PGcomposite fields))
row exprs = UnsafeExpression $ "ROW" <> parenthesized
(renderCommaSeparated (\ (expr `As` _) -> renderExpression expr) exprs)
instance Has field fields ty => IsLabel field
( Expression schema relation grouping params (nullity ('PGcomposite fields))
-> Expression schema relation grouping params ('Null ty) ) where
fromLabel expr = UnsafeExpression $
parenthesized (renderExpression expr) <> "." <>
fromString (symbolVal (Proxy @field))
instance Semigroup
(Expression schema relations grouping params (nullity ('PGvararray ty))) where
(<>) = unsafeBinaryOp "||"
instance Monoid
(Expression schema relations grouping params (nullity ('PGvararray ty))) where
mempty = array []
mappend = (<>)
greatest
:: Expression schema relations grouping params (nullty)
-> [Expression schema relations grouping params (nullty)]
-> Expression schema relations grouping params (nullty)
greatest x xs = UnsafeExpression $ "GREATEST("
<> commaSeparated (renderExpression <$> (x:xs)) <> ")"
least
:: Expression schema relations grouping params (nullty)
-> [Expression schema relations grouping params (nullty)]
-> Expression schema relations grouping params (nullty)
least x xs = UnsafeExpression $ "LEAST("
<> commaSeparated (renderExpression <$> (x:xs)) <> ")"
unsafeBinaryOp
:: ByteString
-> Expression schema relations grouping params (ty0)
-> Expression schema relations grouping params (ty1)
-> Expression schema relations grouping params (ty2)
unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
renderExpression x <+> op <+> renderExpression y
unsafeUnaryOp
:: ByteString
-> Expression schema relations grouping params (ty0)
-> Expression schema relations grouping params (ty1)
unsafeUnaryOp op x = UnsafeExpression $ parenthesized $
op <+> renderExpression x
unsafeFunction
:: ByteString
-> Expression schema relations grouping params (xty)
-> Expression schema relations grouping params (yty)
unsafeFunction fun x = UnsafeExpression $
fun <> parenthesized (renderExpression x)
instance PGNum ty
=> Num (Expression schema relations grouping params (nullity ty)) where
(+) = unsafeBinaryOp "+"
(-) = unsafeBinaryOp "-"
(*) = unsafeBinaryOp "*"
abs = unsafeFunction "abs"
signum = unsafeFunction "sign"
fromInteger
= UnsafeExpression
. fromString
. show
instance (PGNum ty, PGFloating ty) => Fractional
(Expression schema relations grouping params (nullity ty)) where
(/) = unsafeBinaryOp "/"
fromRational x = fromInteger (numerator x) / fromInteger (denominator x)
instance (PGNum ty, PGFloating ty) => Floating
(Expression schema relations 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_
:: PGFloating float
=> Expression schema relations grouping params (nullity float)
-> Expression schema relations grouping params (nullity float)
-> Expression schema relations grouping params (nullity float)
atan2_ y x = UnsafeExpression $
"atan2(" <> renderExpression y <> ", " <> renderExpression x <> ")"
cast
:: TypeExpression schema ty1
-> Expression schema relations grouping params (nullity ty0)
-> Expression schema relations grouping params (nullity ty1)
cast ty x = UnsafeExpression $ parenthesized $
renderExpression x <+> "::" <+> renderTypeExpression ty
quot_
:: PGIntegral int
=> Expression schema relations grouping params (nullity int)
-> Expression schema relations grouping params (nullity int)
-> Expression schema relations grouping params (nullity int)
quot_ = unsafeBinaryOp "/"
rem_
:: PGIntegral int
=> Expression schema relations grouping params (nullity int)
-> Expression schema relations grouping params (nullity int)
-> Expression schema relations grouping params (nullity int)
rem_ = unsafeBinaryOp "%"
trunc
:: PGFloating frac
=> Expression schema relations grouping params (nullity frac)
-> Expression schema relations grouping params (nullity frac)
trunc = unsafeFunction "trunc"
round_
:: PGFloating frac
=> Expression schema relations grouping params (nullity frac)
-> Expression schema relations grouping params (nullity frac)
round_ = unsafeFunction "round"
ceiling_
:: PGFloating frac
=> Expression schema relations grouping params (nullity frac)
-> Expression schema relations grouping params (nullity frac)
ceiling_ = unsafeFunction "ceiling"
type Condition schema relations grouping params =
Expression schema relations grouping params ('NotNull 'PGbool)
true :: Condition schema relations grouping params
true = UnsafeExpression "TRUE"
false :: Condition schema relations grouping params
false = UnsafeExpression "FALSE"
not_
:: Condition schema relations grouping params
-> Condition schema relations grouping params
not_ = unsafeUnaryOp "NOT"
(.&&)
:: Condition schema relations grouping params
-> Condition schema relations grouping params
-> Condition schema relations grouping params
(.&&) = unsafeBinaryOp "AND"
(.||)
:: Condition schema relations grouping params
-> Condition schema relations grouping params
-> Condition schema relations grouping params
(.||) = unsafeBinaryOp "OR"
caseWhenThenElse
:: [ ( Condition schema relations grouping params
, Expression schema relations grouping params (ty)
) ]
-> Expression schema relations grouping params (ty)
-> Expression schema relations 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 relations grouping params
-> Expression schema relations grouping params (ty)
-> Expression schema relations grouping params (ty)
-> Expression schema relations grouping params (ty)
ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
(.==)
:: Expression schema relations grouping params (nullity ty)
-> Expression schema relations grouping params (nullity ty)
-> Expression schema relations grouping params (nullity 'PGbool)
(.==) = unsafeBinaryOp "="
infix 4 .==
(./=)
:: Expression schema relations grouping params (nullity ty)
-> Expression schema relations grouping params (nullity ty)
-> Expression schema relations grouping params (nullity 'PGbool)
(./=) = unsafeBinaryOp "<>"
infix 4 ./=
(.>=)
:: Expression schema relations grouping params (nullity ty)
-> Expression schema relations grouping params (nullity ty)
-> Expression schema relations grouping params (nullity 'PGbool)
(.>=) = unsafeBinaryOp ">="
infix 4 .>=
(.<)
:: Expression schema relations grouping params (nullity ty)
-> Expression schema relations grouping params (nullity ty)
-> Expression schema relations grouping params (nullity 'PGbool)
(.<) = unsafeBinaryOp "<"
infix 4 .<
(.<=)
:: Expression schema relations grouping params (nullity ty)
-> Expression schema relations grouping params (nullity ty)
-> Expression schema relations grouping params (nullity 'PGbool)
(.<=) = unsafeBinaryOp "<="
infix 4 .<=
(.>)
:: Expression schema relations grouping params (nullity ty)
-> Expression schema relations grouping params (nullity ty)
-> Expression schema relations grouping params (nullity 'PGbool)
(.>) = unsafeBinaryOp ">"
infix 4 .>
currentDate
:: Expression schema relations grouping params (nullity 'PGdate)
currentDate = UnsafeExpression "CURRENT_DATE"
currentTime
:: Expression schema relations grouping params (nullity 'PGtimetz)
currentTime = UnsafeExpression "CURRENT_TIME"
currentTimestamp
:: Expression schema relations grouping params (nullity 'PGtimestamptz)
currentTimestamp = UnsafeExpression "CURRENT_TIMESTAMP"
localTime
:: Expression schema relations grouping params (nullity 'PGtime)
localTime = UnsafeExpression "LOCALTIME"
localTimestamp
:: Expression schema relations grouping params (nullity 'PGtimestamp)
localTimestamp = UnsafeExpression "LOCALTIMESTAMP"
instance IsString
(Expression schema relations 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 relations grouping params (nullity 'PGtext)) where
(<>) = unsafeBinaryOp "||"
instance Monoid
(Expression schema relations grouping params (nullity 'PGtext)) where
mempty = fromString ""
mappend = (<>)
lower
:: Expression schema relations grouping params (nullity 'PGtext)
-> Expression schema relations grouping params (nullity 'PGtext)
lower = unsafeFunction "lower"
upper
:: Expression schema relations grouping params (nullity 'PGtext)
-> Expression schema relations grouping params (nullity 'PGtext)
upper = unsafeFunction "upper"
charLength
:: Expression schema relations grouping params (nullity 'PGtext)
-> Expression schema relations grouping params (nullity 'PGint4)
charLength = unsafeFunction "char_length"
like
:: Expression schema relations grouping params (nullity 'PGtext)
-> Expression schema relations grouping params (nullity 'PGtext)
-> Expression schema relations grouping params (nullity 'PGbool)
like = unsafeBinaryOp "LIKE"
unsafeAggregate
:: ByteString
-> Expression schema relations 'Ungrouped params (xty)
-> Expression schema relations ('Grouped bys) params (yty)
unsafeAggregate fun x = UnsafeExpression $ mconcat
[fun, "(", renderExpression x, ")"]
unsafeAggregateDistinct
:: ByteString
-> Expression schema relations 'Ungrouped params (xty)
-> Expression schema relations ('Grouped bys) params (yty)
unsafeAggregateDistinct fun x = UnsafeExpression $ mconcat
[fun, "(DISTINCT ", renderExpression x, ")"]
sum_
:: PGNum ty
=> Expression schema relations 'Ungrouped params (nullity ty)
-> Expression schema relations ('Grouped bys) params (nullity ty)
sum_ = unsafeAggregate "sum"
sumDistinct
:: PGNum ty
=> Expression schema relations 'Ungrouped params (nullity ty)
-> Expression schema relations ('Grouped bys) params (nullity ty)
sumDistinct = unsafeAggregateDistinct "sum"
class PGAvg ty avg | ty -> avg where
avg, avgDistinct
:: Expression schema relations 'Ungrouped params (nullity ty)
-> Expression schema relations ('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
:: PGIntegral int
=> Expression schema relations 'Ungrouped params (nullity int)
-> Expression schema relations ('Grouped bys) params (nullity int)
bitAnd = unsafeAggregate "bit_and"
bitOr
:: PGIntegral int
=> Expression schema relations 'Ungrouped params (nullity int)
-> Expression schema relations ('Grouped bys) params (nullity int)
bitOr = unsafeAggregate "bit_or"
bitAndDistinct
:: PGIntegral int
=> Expression schema relations 'Ungrouped params (nullity int)
-> Expression schema relations ('Grouped bys) params (nullity int)
bitAndDistinct = unsafeAggregateDistinct "bit_and"
bitOrDistinct
:: PGIntegral int
=> Expression schema relations 'Ungrouped params (nullity int)
-> Expression schema relations ('Grouped bys) params (nullity int)
bitOrDistinct = unsafeAggregateDistinct "bit_or"
boolAnd
:: Expression schema relations 'Ungrouped params (nullity 'PGbool)
-> Expression schema relations ('Grouped bys) params (nullity 'PGbool)
boolAnd = unsafeAggregate "bool_and"
boolOr
:: Expression schema relations 'Ungrouped params (nullity 'PGbool)
-> Expression schema relations ('Grouped bys) params (nullity 'PGbool)
boolOr = unsafeAggregate "bool_or"
boolAndDistinct
:: Expression schema relations 'Ungrouped params (nullity 'PGbool)
-> Expression schema relations ('Grouped bys) params (nullity 'PGbool)
boolAndDistinct = unsafeAggregateDistinct "bool_and"
boolOrDistinct
:: Expression schema relations 'Ungrouped params (nullity 'PGbool)
-> Expression schema relations ('Grouped bys) params (nullity 'PGbool)
boolOrDistinct = unsafeAggregateDistinct "bool_or"
countStar
:: Expression schema relations ('Grouped bys) params ('NotNull 'PGint8)
countStar = UnsafeExpression $ "count(*)"
count
:: Expression schema relations 'Ungrouped params ty
-> Expression schema relations ('Grouped bys) params ('NotNull 'PGint8)
count = unsafeAggregate "count"
countDistinct
:: Expression schema relations 'Ungrouped params ty
-> Expression schema relations ('Grouped bys) params ('NotNull 'PGint8)
countDistinct = unsafeAggregateDistinct "count"
every
:: Expression schema relations 'Ungrouped params (nullity 'PGbool)
-> Expression schema relations ('Grouped bys) params (nullity 'PGbool)
every = unsafeAggregate "every"
everyDistinct
:: Expression schema relations 'Ungrouped params (nullity 'PGbool)
-> Expression schema relations ('Grouped bys) params (nullity 'PGbool)
everyDistinct = unsafeAggregateDistinct "every"
max_, min_, maxDistinct, minDistinct
:: Expression schema relations 'Ungrouped params (nullity ty)
-> Expression schema relations ('Grouped bys) params (nullity ty)
max_ = unsafeAggregate "max"
min_ = unsafeAggregate "min"
maxDistinct = unsafeAggregateDistinct "max"
minDistinct = unsafeAggregateDistinct "min"
newtype TypeExpression (schema :: SchemaType) (ty :: PGType)
= UnsafeTypeExpression { renderTypeExpression :: ByteString }
deriving (GHC.Generic,Show,Eq,Ord,NFData)
instance (Has alias schema ('Typedef ty))
=> IsLabel alias (TypeExpression schema ty) where
fromLabel = UnsafeTypeExpression (renderAlias (fromLabel @alias))
bool :: TypeExpression schema 'PGbool
bool = UnsafeTypeExpression "bool"
int2, smallint :: TypeExpression schema 'PGint2
int2 = UnsafeTypeExpression "int2"
smallint = UnsafeTypeExpression "smallint"
int4, int, integer :: TypeExpression schema 'PGint4
int4 = UnsafeTypeExpression "int4"
int = UnsafeTypeExpression "int"
integer = UnsafeTypeExpression "integer"
int8, bigint :: TypeExpression schema 'PGint8
int8 = UnsafeTypeExpression "int8"
bigint = UnsafeTypeExpression "bigint"
numeric :: TypeExpression schema 'PGnumeric
numeric = UnsafeTypeExpression "numeric"
float4, real :: TypeExpression schema 'PGfloat4
float4 = UnsafeTypeExpression "float4"
real = UnsafeTypeExpression "real"
float8, doublePrecision :: TypeExpression schema 'PGfloat8
float8 = UnsafeTypeExpression "float8"
doublePrecision = UnsafeTypeExpression "double precision"
text :: TypeExpression schema 'PGtext
text = UnsafeTypeExpression "text"
char, character
:: (KnownNat n, 1 <= n)
=> proxy n
-> TypeExpression schema ('PGchar n)
char p = UnsafeTypeExpression $ "char(" <> renderNat p <> ")"
character p = UnsafeTypeExpression $ "character(" <> renderNat p <> ")"
varchar, characterVarying
:: (KnownNat n, 1 <= n)
=> proxy n
-> TypeExpression schema ('PGvarchar n)
varchar p = UnsafeTypeExpression $ "varchar(" <> renderNat p <> ")"
characterVarying p = UnsafeTypeExpression $
"character varying(" <> renderNat p <> ")"
bytea :: TypeExpression schema 'PGbytea
bytea = UnsafeTypeExpression "bytea"
timestamp :: TypeExpression schema 'PGtimestamp
timestamp = UnsafeTypeExpression "timestamp"
timestampWithTimeZone :: TypeExpression schema 'PGtimestamptz
timestampWithTimeZone = UnsafeTypeExpression "timestamp with time zone"
date :: TypeExpression schema 'PGdate
date = UnsafeTypeExpression "date"
time :: TypeExpression schema 'PGtime
time = UnsafeTypeExpression "time"
timeWithTimeZone :: TypeExpression schema 'PGtimetz
timeWithTimeZone = UnsafeTypeExpression "time with time zone"
interval :: TypeExpression schema 'PGinterval
interval = UnsafeTypeExpression "interval"
uuid :: TypeExpression schema 'PGuuid
uuid = UnsafeTypeExpression "uuid"
inet :: TypeExpression schema 'PGinet
inet = UnsafeTypeExpression "inet"
json :: TypeExpression schema 'PGjson
json = UnsafeTypeExpression "json"
jsonb :: TypeExpression schema 'PGjsonb
jsonb = UnsafeTypeExpression "jsonb"
vararray
:: TypeExpression schema pg
-> TypeExpression schema ('PGvararray pg)
vararray ty = UnsafeTypeExpression $ renderTypeExpression ty <> "[]"
fixarray
:: KnownNat n
=> proxy n
-> TypeExpression schema pg
-> TypeExpression schema ('PGfixarray n pg)
fixarray p ty = UnsafeTypeExpression $
renderTypeExpression ty <> "[" <> renderNat p <> "]"
class PGTyped schema (ty :: PGType) where pgtype :: TypeExpression schema ty
instance PGTyped schema 'PGbool where pgtype = bool
instance PGTyped schema 'PGint2 where pgtype = int2
instance PGTyped schema 'PGint4 where pgtype = int4
instance PGTyped schema 'PGint8 where pgtype = int8
instance PGTyped schema 'PGnumeric where pgtype = numeric
instance PGTyped schema 'PGfloat4 where pgtype = float4
instance PGTyped schema 'PGfloat8 where pgtype = float8
instance PGTyped schema 'PGtext where pgtype = text
instance (KnownNat n, 1 <= n)
=> PGTyped schema ('PGchar n) where pgtype = char (Proxy @n)
instance (KnownNat n, 1 <= n)
=> PGTyped schema ('PGvarchar n) where pgtype = varchar (Proxy @n)
instance PGTyped schema 'PGbytea where pgtype = bytea
instance PGTyped schema 'PGtimestamp where pgtype = timestamp
instance PGTyped schema 'PGtimestamptz where pgtype = timestampWithTimeZone
instance PGTyped schema 'PGdate where pgtype = date
instance PGTyped schema 'PGtime where pgtype = time
instance PGTyped schema 'PGtimetz where pgtype = timeWithTimeZone
instance PGTyped schema 'PGinterval where pgtype = interval
instance PGTyped schema 'PGuuid where pgtype = uuid
instance PGTyped schema 'PGjson where pgtype = json
instance PGTyped schema 'PGjsonb where pgtype = jsonb
instance PGTyped schema ty => PGTyped schema ('PGvararray ty) where
pgtype = vararray (pgtype @schema @ty)
instance (KnownNat n, PGTyped schema ty) => PGTyped schema ('PGfixarray n ty) where
pgtype = fixarray (Proxy @n) (pgtype @schema @ty)