{-# LANGUAGE
AllowAmbiguousTypes
, DataKinds
, DeriveGeneric
, FlexibleContexts
, FlexibleInstances
, GADTs
, GeneralizedNewtypeDeriving
, KindSignatures
, MultiParamTypeClasses
, OverloadedStrings
, RankNTypes
, ScopedTypeVariables
, TypeApplications
, TypeOperators
#-}
module Squeal.PostgreSQL.Expression.Type
( TypeExpression (..)
, cast
, astype
, inferredtype
, PGTyped (..)
, typedef
, typetable
, typeview
, bool
, int2
, smallint
, int4
, int
, integer
, int8
, bigint
, numeric
, float4
, real
, float8
, doublePrecision
, money
, text
, char
, character
, varchar
, characterVarying
, bytea
, timestamp
, timestampWithTimeZone
, date
, time
, timeWithTimeZone
, interval
, uuid
, inet
, json
, jsonb
, vararray
, fixarray
, tsvector
, tsquery
) where
import Control.DeepSeq
import Data.ByteString
import Data.String
import GHC.TypeLits
import qualified Data.ByteString as ByteString
import qualified GHC.Generics as GHC
import qualified Generics.SOP as SOP
import Squeal.PostgreSQL.Alias
import Squeal.PostgreSQL.Expression
import Squeal.PostgreSQL.Render
import Squeal.PostgreSQL.Schema
cast
:: TypeExpression schemas ty1
-> Expression outer commons grp schemas params from ty0
-> Expression outer commons grp schemas params from ty1
cast ty x = UnsafeExpression $ parenthesized $
renderSQL x <+> "::" <+> renderSQL ty
astype
:: TypeExpression schemas ty
-> Expression outer commons grp schemas params from ty
-> Expression outer commons grp schemas params from ty
astype = cast
inferredtype
:: PGTyped schemas ty
=> Expression outer common grp schemas params from ty
-> Expression outer common grp schemas params from ty
inferredtype = astype pgtype
newtype TypeExpression (schemas :: SchemasType) (ty :: NullityType)
= UnsafeTypeExpression { renderTypeExpression :: ByteString }
deriving (GHC.Generic,Show,Eq,Ord,NFData)
instance RenderSQL (TypeExpression schemas ty) where
renderSQL = renderTypeExpression
typedef
:: (Has sch schemas schema, Has td schema ('Typedef ty))
=> QualifiedAlias sch td
-> TypeExpression schemas (null ty)
typedef = UnsafeTypeExpression . renderSQL
typetable
:: (Has sch schemas schema, Has tab schema ('Table table))
=> QualifiedAlias sch tab
-> TypeExpression schemas (null ('PGcomposite (TableToRow table)))
typetable = UnsafeTypeExpression . renderSQL
typeview
:: (Has sch schemas schema, Has vw schema ('View view))
=> QualifiedAlias sch vw
-> TypeExpression schemas (null ('PGcomposite view))
typeview = UnsafeTypeExpression . renderSQL
bool :: TypeExpression schemas (null 'PGbool)
bool = UnsafeTypeExpression "bool"
int2, smallint :: TypeExpression schemas (null 'PGint2)
int2 = UnsafeTypeExpression "int2"
smallint = UnsafeTypeExpression "smallint"
int4, int, integer :: TypeExpression schemas (null 'PGint4)
int4 = UnsafeTypeExpression "int4"
int = UnsafeTypeExpression "int"
integer = UnsafeTypeExpression "integer"
int8, bigint :: TypeExpression schemas (null 'PGint8)
int8 = UnsafeTypeExpression "int8"
bigint = UnsafeTypeExpression "bigint"
numeric :: TypeExpression schemas (null 'PGnumeric)
numeric = UnsafeTypeExpression "numeric"
float4, real :: TypeExpression schemas (null 'PGfloat4)
float4 = UnsafeTypeExpression "float4"
real = UnsafeTypeExpression "real"
float8, doublePrecision :: TypeExpression schemas (null 'PGfloat8)
float8 = UnsafeTypeExpression "float8"
doublePrecision = UnsafeTypeExpression "double precision"
money :: TypeExpression schema (null 'PGmoney)
money = UnsafeTypeExpression "money"
text :: TypeExpression schemas (null 'PGtext)
text = UnsafeTypeExpression "text"
char, character
:: forall n schemas null. (KnownNat n, 1 <= n)
=> TypeExpression schemas (null ('PGchar n))
char = UnsafeTypeExpression $ "char(" <> renderNat @n <> ")"
character = UnsafeTypeExpression $ "character(" <> renderNat @n <> ")"
varchar, characterVarying
:: forall n schemas null. (KnownNat n, 1 <= n)
=> TypeExpression schemas (null ('PGvarchar n))
varchar = UnsafeTypeExpression $ "varchar(" <> renderNat @n <> ")"
characterVarying = UnsafeTypeExpression $
"character varying(" <> renderNat @n <> ")"
bytea :: TypeExpression schemas (null 'PGbytea)
bytea = UnsafeTypeExpression "bytea"
timestamp :: TypeExpression schemas (null 'PGtimestamp)
timestamp = UnsafeTypeExpression "timestamp"
timestampWithTimeZone :: TypeExpression schemas (null 'PGtimestamptz)
timestampWithTimeZone = UnsafeTypeExpression "timestamp with time zone"
date :: TypeExpression schemas (null 'PGdate)
date = UnsafeTypeExpression "date"
time :: TypeExpression schemas (null 'PGtime)
time = UnsafeTypeExpression "time"
timeWithTimeZone :: TypeExpression schemas (null 'PGtimetz)
timeWithTimeZone = UnsafeTypeExpression "time with time zone"
interval :: TypeExpression schemas (null 'PGinterval)
interval = UnsafeTypeExpression "interval"
uuid :: TypeExpression schemas (null 'PGuuid)
uuid = UnsafeTypeExpression "uuid"
inet :: TypeExpression schemas (null 'PGinet)
inet = UnsafeTypeExpression "inet"
json :: TypeExpression schemas (null 'PGjson)
json = UnsafeTypeExpression "json"
jsonb :: TypeExpression schemas (null 'PGjsonb)
jsonb = UnsafeTypeExpression "jsonb"
vararray
:: TypeExpression schemas pg
-> TypeExpression schemas (null ('PGvararray pg))
vararray ty = UnsafeTypeExpression $ renderSQL ty <> "[]"
fixarray
:: forall dims schemas null pg. SOP.All KnownNat dims
=> TypeExpression schemas pg
-> TypeExpression schemas (null ('PGfixarray dims pg))
fixarray ty = UnsafeTypeExpression $
renderSQL ty <> renderDims @dims
where
renderDims :: forall ns. SOP.All KnownNat ns => ByteString
renderDims =
("[" <>)
. (<> "]")
. ByteString.intercalate "]["
. SOP.hcollapse
$ SOP.hcmap (SOP.Proxy @KnownNat)
(K . fromString . show . natVal)
(SOP.hpure SOP.Proxy :: SOP.NP SOP.Proxy ns)
tsvector :: TypeExpression schemas (null 'PGtsvector)
tsvector = UnsafeTypeExpression "tsvector"
tsquery :: TypeExpression schemas (null 'PGtsquery)
tsquery = UnsafeTypeExpression "tsquery"
class PGTyped schemas (ty :: NullityType) where
pgtype :: TypeExpression schemas ty
instance PGTyped schemas (null 'PGbool) where pgtype = bool
instance PGTyped schemas (null 'PGint2) where pgtype = int2
instance PGTyped schemas (null 'PGint4) where pgtype = int4
instance PGTyped schemas (null 'PGint8) where pgtype = int8
instance PGTyped schemas (null 'PGnumeric) where pgtype = numeric
instance PGTyped schemas (null 'PGfloat4) where pgtype = float4
instance PGTyped schemas (null 'PGfloat8) where pgtype = float8
instance PGTyped schemas (null 'PGmoney) where pgtype = money
instance PGTyped schemas (null 'PGtext) where pgtype = text
instance (KnownNat n, 1 <= n)
=> PGTyped schemas (null ('PGchar n)) where pgtype = char @n
instance (KnownNat n, 1 <= n)
=> PGTyped schemas (null ('PGvarchar n)) where pgtype = varchar @n
instance PGTyped schemas (null 'PGbytea) where pgtype = bytea
instance PGTyped schemas (null 'PGtimestamp) where pgtype = timestamp
instance PGTyped schemas (null 'PGtimestamptz) where pgtype = timestampWithTimeZone
instance PGTyped schemas (null 'PGdate) where pgtype = date
instance PGTyped schemas (null 'PGtime) where pgtype = time
instance PGTyped schemas (null 'PGtimetz) where pgtype = timeWithTimeZone
instance PGTyped schemas (null 'PGinterval) where pgtype = interval
instance PGTyped schemas (null 'PGuuid) where pgtype = uuid
instance PGTyped schemas (null 'PGjson) where pgtype = json
instance PGTyped schemas (null 'PGjsonb) where pgtype = jsonb
instance PGTyped schemas ty
=> PGTyped schemas (null ('PGvararray ty)) where
pgtype = vararray (pgtype @schemas @ty)
instance (SOP.All KnownNat dims, PGTyped schemas ty)
=> PGTyped schemas (null ('PGfixarray dims ty)) where
pgtype = fixarray @dims (pgtype @schemas @ty)
instance PGTyped schemas (null 'PGtsvector) where pgtype = tsvector
instance PGTyped schemas (null 'PGtsquery) where pgtype = tsquery