{-# LANGUAGE
AllowAmbiguousTypes
, DataKinds
, FlexibleContexts
, FlexibleInstances
, FunctionalDependencies
, KindSignatures
, MultiParamTypeClasses
, OverloadedStrings
, RankNTypes
, ScopedTypeVariables
, TypeApplications
, TypeOperators
, UndecidableInstances
#-}
module Squeal.PostgreSQL.Expression.Parameter
( HasParameter (parameter)
, param
) where
import GHC.TypeLits
import Squeal.PostgreSQL.Expression
import Squeal.PostgreSQL.Expression.Type
import Squeal.PostgreSQL.Render
import Squeal.PostgreSQL.Schema
class KnownNat n => HasParameter
(n :: Nat)
(params :: [NullityType])
(ty :: NullityType)
| n params -> ty where
parameter
:: TypeExpression schemas ty
-> Expression outer commons grp schemas params from ty
parameter ty = UnsafeExpression $ parenthesized $
"$" <> renderNat @n <+> "::"
<+> renderSQL ty
instance {-# OVERLAPPING #-} HasParameter 1 (ty1:tys) ty1
instance {-# OVERLAPPABLE #-} (KnownNat n, HasParameter (n-1) params ty)
=> HasParameter n (ty' : params) ty
param
:: forall n outer commons schemas params from grp ty
. (PGTyped schemas ty, HasParameter n params ty)
=> Expression outer commons grp schemas params from ty
param = parameter @n (pgtype @schemas)