{-# LANGUAGE
ConstraintKinds
, DeriveGeneric
, DerivingStrategies
, FlexibleContexts
, FlexibleInstances
, GADTs
, GeneralizedNewtypeDeriving
, LambdaCase
, MultiParamTypeClasses
, OverloadedLabels
, OverloadedStrings
, QuantifiedConstraints
, ScopedTypeVariables
, StandaloneDeriving
, TypeApplications
, TypeFamilies
, TypeInType
, TypeOperators
, RankNTypes
, UndecidableInstances
#-}
module Squeal.PostgreSQL.Query.From.Set
(
type (-|->)
, type (--|->)
, SetFun
, SetFunN
, generateSeries
, generateSeriesStep
, generateSeriesTimestamp
, unsafeSetFunction
, setFunction
, unsafeSetFunctionN
, setFunctionN
) where
import Data.ByteString (ByteString)
import Generics.SOP hiding (from)
import GHC.TypeLits
import qualified Generics.SOP as SOP
import Squeal.PostgreSQL.Type.Alias
import Squeal.PostgreSQL.Expression
import Squeal.PostgreSQL.Query.From
import Squeal.PostgreSQL.Render
import Squeal.PostgreSQL.Type.List
import Squeal.PostgreSQL.Type.Schema
type (-|->) arg set = forall db. SetFun db arg set
type (--|->) arg set = forall db. SetFunN db arg set
type SetFun db arg row
= forall lat with params
. Expression 'Ungrouped lat with db params '[] arg
-> FromClause lat with db params '[row]
type SetFunN db args set
= forall lat with params
. NP (Expression 'Ungrouped lat with db params '[]) args
-> FromClause lat with db params '[set]
unsafeSetFunction
:: forall fun ty row. KnownSymbol fun
=> ByteString
-> ty -|-> (fun ::: row)
unsafeSetFunction fun x = UnsafeFromClause $
fun <> parenthesized (renderSQL x)
setFunction
:: ( Has sch db schema
, Has fun schema ('Function ('[ty] :=> 'ReturnsTable row)) )
=> QualifiedAlias sch fun
-> SetFun db ty (fun ::: row)
setFunction fun = unsafeSetFunction (renderSQL fun)
unsafeSetFunctionN
:: forall fun tys row. (SOP.SListI tys, KnownSymbol fun)
=> ByteString
-> tys --|-> (fun ::: row)
unsafeSetFunctionN fun xs = UnsafeFromClause $
fun <> parenthesized (renderCommaSeparated renderSQL xs)
setFunctionN
:: ( Has sch db schema
, Has fun schema ('Function (tys :=> 'ReturnsTable row))
, SOP.SListI tys )
=> QualifiedAlias sch fun
-> SetFunN db tys (fun ::: row)
setFunctionN fun = unsafeSetFunctionN (renderSQL fun)
generateSeries
:: ty `In` '[ 'PGint4, 'PGint8, 'PGnumeric]
=> '[ null ty, null ty] --|->
("generate_series" ::: '["generate_series" ::: null ty])
generateSeries = unsafeSetFunctionN "generate_series"
generateSeriesStep
:: ty `In` '[ 'PGint4, 'PGint8, 'PGnumeric]
=> '[null ty, null ty, null ty] --|->
("generate_series" ::: '["generate_series" ::: null ty])
generateSeriesStep = unsafeSetFunctionN "generate_series"
generateSeriesTimestamp
:: ty `In` '[ 'PGtimestamp, 'PGtimestamptz]
=> '[null ty, null ty, null 'PGinterval] --|->
("generate_series" ::: '["generate_series" ::: null ty])
generateSeriesTimestamp = unsafeSetFunctionN "generate_series"