{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE CPP #-}

-- | Postgres-specific types, functions, and operators
module Database.Beam.Postgres.PgSpecific
  ( -- ** Full-text search
    -- $full-text-search

    -- *** @TSVECTOR@ data type
    TsVectorConfig, TsVector(..)
  , toTsVector, english

    -- *** @TSQUERY@ data type
  , TsQuery(..), (@@)
  , toTsQuery

    -- ** @JSON@ and @JSONB@ data types
    -- $json
  , PgJSON(..), PgJSONB(..)
  , IsPgJSON(..)
  , PgJSONEach(..), PgJSONKey(..), PgJSONElement(..)

  , (@>), (<@), (->#), (->$)
  , (->>#), (->>$), (#>), (#>>)
  , (?), (?|), (?&)

  , withoutKey, withoutIdx
  , withoutKeys

  , pgJsonArrayLength
  , pgArrayToJson
  , pgJsonbUpdate, pgJsonbSet
  , pgJsonbPretty

    -- ** @MONEY@ data type
  , PgMoney(..), pgMoney

  , pgScaleMoney_
  , pgDivideMoney_, pgDivideMoneys_

  , pgAddMoney_, pgSubtractMoney_
  , pgSumMoneyOver_, pgAvgMoneyOver_
  , pgSumMoney_, pgAvgMoney_

    -- ** Geometry types (not PostGIS)
  , PgPoint(..), PgLine(..), PgLineSegment(..)
  , PgBox(..), PgPath(..), PgPolygon(..)
  , PgCircle(..)

    -- ** Regular expressions
  , PgRegex(..), pgRegex_
  , (~.), (~*.), (!~.), (!~*.)
  , pgRegexpReplace_, pgRegexpMatch_
  , pgRegexpSplitToTable, pgRegexpSplitToArray

    -- ** Set-valued functions
    -- $set-valued-funs
  , PgSetOf, pgUnnest
  , pgUnnestArray, pgUnnestArrayWithOrdinality

    -- ** @ARRAY@ types
    -- $arrays
  , PgArrayValueContext, PgIsArrayContext

    -- *** Building @ARRAY@s
  , array_, arrayOf_, (++.)
  , pgArrayAgg, pgArrayAggOver

    -- *** Array operators and functions
  , (!.), arrayDims_
  , arrayUpper_, arrayLower_
  , arrayUpperUnsafe_, arrayLowerUnsafe_
  , arrayLength_, arrayLengthUnsafe_

  , isSupersetOf_, isSubsetOf_

    -- ** @RANGE@ types
    -- $ranges
  , PgRange(..), PgRangeBound(..), PgBoundType(..)
  , PgIsRange(..)
  , PgInt4Range, PgInt8Range, PgNumRange
  , PgTsRange, PgTsTzRange, PgDateRange

    -- *** Building ranges from expressions
  , range_

    -- *** Building @PgRangeBound@s
  , inclusive, exclusive, unbounded

    -- *** Range operators and functions
  , (-@>-), (-@>), (-<@-), (<@-)
  , (-&&-), (-<<-), (->>-)
  , (-&<-), (-&>-), (--|--)
  , (-+-), (-*-), (-.-)
  , rLower_, rUpper_, isEmpty_
  , lowerInc_, upperInc_, lowerInf_, upperInf_
  , rangeMerge_

    -- * Postgres @EXTRACT@ fields
  , century_, decade_, dow_, doy_, epoch_, isodow_, isoyear_
  , microseconds_, milliseconds_, millennium_, quarter_, week_

    -- ** Postgres functions and aggregates
  , pgBoolOr, pgBoolAnd, pgStringAgg, pgStringAggOver

  , pgNubBy_

  , now_, ilike_, ilike_'
  )
where

import           Database.Beam hiding (char, double)
import           Database.Beam.Backend.SQL
import           Database.Beam.Migrate ( HasDefaultSqlDataType(..) )
import           Database.Beam.Postgres.Syntax
import           Database.Beam.Postgres.Types
import           Database.Beam.Query.Internal
import           Database.Beam.Schema.Tables

import           Control.Monad.Free
import           Control.Monad.State.Strict (evalState, put, get)

import           Data.Aeson
import           Data.Attoparsec.ByteString
import           Data.Attoparsec.ByteString.Char8
import           Data.ByteString (ByteString)
import           Data.ByteString.Builder
import qualified Data.ByteString.Char8 as BC
import qualified Data.ByteString.Lazy as BL
import           Data.Foldable
import           Data.Functor
import           Data.Hashable
import           Data.Int
import qualified Data.List.NonEmpty as NE
import           Data.Proxy
import           Data.Scientific (Scientific, formatScientific, FPFormat(Fixed))
import           Data.String
import qualified Data.Text as T
import           Data.Time (LocalTime, NominalDiffTime)
import           Data.Type.Bool
import qualified Data.Vector as V
#if !MIN_VERSION_base(4, 11, 0)
import           Data.Semigroup
#endif

import qualified Database.PostgreSQL.Simple.FromField as Pg
import qualified Database.PostgreSQL.Simple.ToField as Pg
import qualified Database.PostgreSQL.Simple.TypeInfo.Static as Pg
import qualified Database.PostgreSQL.Simple.Range as Pg

import           GHC.TypeLits
import           GHC.Exts hiding (toList)

-- ** Postgres-specific functions

-- | Postgres @NOW()@ function. Returns the server's timestamp
now_ :: QExpr Postgres s LocalTime
now_ :: QExpr Postgres s LocalTime
now_ = (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QExpr Postgres s LocalTime
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (\TablePrefix
_ -> PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"NOW()"))

-- | Postgres @ILIKE@ operator. A case-insensitive version of 'like_'.
ilike_
  :: BeamSqlBackendIsString Postgres text
  => QExpr Postgres s text
  -> QExpr Postgres s text
  -> QExpr Postgres s Bool
ilike_ :: QExpr Postgres s text
-> QExpr Postgres s text -> QExpr Postgres s Bool
ilike_ = QExpr Postgres s text
-> QExpr Postgres s text -> QExpr Postgres s Bool
forall left right s.
(BeamSqlBackendIsString Postgres left,
 BeamSqlBackendIsString Postgres right) =>
QExpr Postgres s left
-> QExpr Postgres s right -> QExpr Postgres s Bool
ilike_'

-- | Postgres @ILIKE@ operator. A case-insensitive version of 'like_''.
ilike_'
  :: ( BeamSqlBackendIsString Postgres left
     , BeamSqlBackendIsString Postgres right
     )
  => QExpr Postgres s left
  -> QExpr Postgres s right
  -> QExpr Postgres s Bool
ilike_' :: QExpr Postgres s left
-> QExpr Postgres s right -> QExpr Postgres s Bool
ilike_' (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a) (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
b) = (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QExpr Postgres s Bool
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"ILIKE" (PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a (TablePrefix -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
b)

-- ** TsVector type

-- | The type of a document preprocessed for full-text search. The contained
-- 'ByteString' is the Postgres representation of the @TSVECTOR@ type. Use
-- 'toTsVector' to construct these on-the-fly from strings.
--
-- When this field is embedded in a beam table, 'defaultMigratableDbSettings'
-- will give the column the postgres @TSVECTOR@ type.
newtype TsVector = TsVector ByteString
  deriving (Int -> TsVector -> ShowS
[TsVector] -> ShowS
TsVector -> String
(Int -> TsVector -> ShowS)
-> (TsVector -> String) -> ([TsVector] -> ShowS) -> Show TsVector
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TsVector] -> ShowS
$cshowList :: [TsVector] -> ShowS
show :: TsVector -> String
$cshow :: TsVector -> String
showsPrec :: Int -> TsVector -> ShowS
$cshowsPrec :: Int -> TsVector -> ShowS
Show, TsVector -> TsVector -> Bool
(TsVector -> TsVector -> Bool)
-> (TsVector -> TsVector -> Bool) -> Eq TsVector
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TsVector -> TsVector -> Bool
$c/= :: TsVector -> TsVector -> Bool
== :: TsVector -> TsVector -> Bool
$c== :: TsVector -> TsVector -> Bool
Eq, Eq TsVector
Eq TsVector
-> (TsVector -> TsVector -> Ordering)
-> (TsVector -> TsVector -> Bool)
-> (TsVector -> TsVector -> Bool)
-> (TsVector -> TsVector -> Bool)
-> (TsVector -> TsVector -> Bool)
-> (TsVector -> TsVector -> TsVector)
-> (TsVector -> TsVector -> TsVector)
-> Ord TsVector
TsVector -> TsVector -> Bool
TsVector -> TsVector -> Ordering
TsVector -> TsVector -> TsVector
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: TsVector -> TsVector -> TsVector
$cmin :: TsVector -> TsVector -> TsVector
max :: TsVector -> TsVector -> TsVector
$cmax :: TsVector -> TsVector -> TsVector
>= :: TsVector -> TsVector -> Bool
$c>= :: TsVector -> TsVector -> Bool
> :: TsVector -> TsVector -> Bool
$c> :: TsVector -> TsVector -> Bool
<= :: TsVector -> TsVector -> Bool
$c<= :: TsVector -> TsVector -> Bool
< :: TsVector -> TsVector -> Bool
$c< :: TsVector -> TsVector -> Bool
compare :: TsVector -> TsVector -> Ordering
$ccompare :: TsVector -> TsVector -> Ordering
$cp1Ord :: Eq TsVector
Ord)

-- | The identifier of a Postgres text search configuration.
--
-- Use the 'IsString' instance to construct new values of this type
newtype TsVectorConfig = TsVectorConfig ByteString
  deriving (Int -> TsVectorConfig -> ShowS
[TsVectorConfig] -> ShowS
TsVectorConfig -> String
(Int -> TsVectorConfig -> ShowS)
-> (TsVectorConfig -> String)
-> ([TsVectorConfig] -> ShowS)
-> Show TsVectorConfig
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TsVectorConfig] -> ShowS
$cshowList :: [TsVectorConfig] -> ShowS
show :: TsVectorConfig -> String
$cshow :: TsVectorConfig -> String
showsPrec :: Int -> TsVectorConfig -> ShowS
$cshowsPrec :: Int -> TsVectorConfig -> ShowS
Show, TsVectorConfig -> TsVectorConfig -> Bool
(TsVectorConfig -> TsVectorConfig -> Bool)
-> (TsVectorConfig -> TsVectorConfig -> Bool) -> Eq TsVectorConfig
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TsVectorConfig -> TsVectorConfig -> Bool
$c/= :: TsVectorConfig -> TsVectorConfig -> Bool
== :: TsVectorConfig -> TsVectorConfig -> Bool
$c== :: TsVectorConfig -> TsVectorConfig -> Bool
Eq, Eq TsVectorConfig
Eq TsVectorConfig
-> (TsVectorConfig -> TsVectorConfig -> Ordering)
-> (TsVectorConfig -> TsVectorConfig -> Bool)
-> (TsVectorConfig -> TsVectorConfig -> Bool)
-> (TsVectorConfig -> TsVectorConfig -> Bool)
-> (TsVectorConfig -> TsVectorConfig -> Bool)
-> (TsVectorConfig -> TsVectorConfig -> TsVectorConfig)
-> (TsVectorConfig -> TsVectorConfig -> TsVectorConfig)
-> Ord TsVectorConfig
TsVectorConfig -> TsVectorConfig -> Bool
TsVectorConfig -> TsVectorConfig -> Ordering
TsVectorConfig -> TsVectorConfig -> TsVectorConfig
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: TsVectorConfig -> TsVectorConfig -> TsVectorConfig
$cmin :: TsVectorConfig -> TsVectorConfig -> TsVectorConfig
max :: TsVectorConfig -> TsVectorConfig -> TsVectorConfig
$cmax :: TsVectorConfig -> TsVectorConfig -> TsVectorConfig
>= :: TsVectorConfig -> TsVectorConfig -> Bool
$c>= :: TsVectorConfig -> TsVectorConfig -> Bool
> :: TsVectorConfig -> TsVectorConfig -> Bool
$c> :: TsVectorConfig -> TsVectorConfig -> Bool
<= :: TsVectorConfig -> TsVectorConfig -> Bool
$c<= :: TsVectorConfig -> TsVectorConfig -> Bool
< :: TsVectorConfig -> TsVectorConfig -> Bool
$c< :: TsVectorConfig -> TsVectorConfig -> Bool
compare :: TsVectorConfig -> TsVectorConfig -> Ordering
$ccompare :: TsVectorConfig -> TsVectorConfig -> Ordering
$cp1Ord :: Eq TsVectorConfig
Ord, String -> TsVectorConfig
(String -> TsVectorConfig) -> IsString TsVectorConfig
forall a. (String -> a) -> IsString a
fromString :: String -> TsVectorConfig
$cfromString :: String -> TsVectorConfig
IsString)

instance Pg.FromField TsVector where
  fromField :: FieldParser TsVector
fromField Field
field Maybe ByteString
d =
    if Field -> Oid
Pg.typeOid Field
field Oid -> Oid -> Bool
forall a. Eq a => a -> a -> Bool
/= TypeInfo -> Oid
Pg.typoid TypeInfo
pgTsVectorTypeInfo
    then (String -> Maybe Oid -> String -> String -> String -> ResultError)
-> Field -> String -> Conversion TsVector
forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
Pg.returnError String -> Maybe Oid -> String -> String -> String -> ResultError
Pg.Incompatible Field
field String
""
    else case Maybe ByteString
d of
           Just ByteString
d' -> TsVector -> Conversion TsVector
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> TsVector
TsVector ByteString
d')
           Maybe ByteString
Nothing -> (String -> Maybe Oid -> String -> String -> String -> ResultError)
-> Field -> String -> Conversion TsVector
forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
Pg.returnError String -> Maybe Oid -> String -> String -> String -> ResultError
Pg.UnexpectedNull Field
field String
""

instance Pg.ToField TsVector where
  toField :: TsVector -> Action
toField (TsVector ByteString
d) =
    [Action] -> Action
Pg.Many [ Builder -> Action
Pg.Plain Builder
"($$"
            , Builder -> Action
Pg.Plain (ByteString -> Builder
byteString ByteString
d)
            , Builder -> Action
Pg.Plain Builder
"$$::tsvector)" ]

instance FromBackendRow Postgres TsVector

instance HasSqlEqualityCheck Postgres TsVectorConfig
instance HasSqlQuantifiedEqualityCheck Postgres TsVectorConfig

instance HasSqlEqualityCheck Postgres TsVector
instance HasSqlQuantifiedEqualityCheck Postgres TsVector

-- | A full-text search configuration with sensible defaults for english
english :: TsVectorConfig
english :: TsVectorConfig
english = ByteString -> TsVectorConfig
TsVectorConfig ByteString
"english"

-- | The Postgres @to_tsvector@ function. Given a configuration and string,
-- return the @TSVECTOR@ that represents the contents of the string.
toTsVector :: BeamSqlBackendIsString Postgres str
           => Maybe TsVectorConfig -> QGenExpr context Postgres s str
           -> QGenExpr context Postgres s TsVector
toTsVector :: Maybe TsVectorConfig
-> QGenExpr context Postgres s str
-> QGenExpr context Postgres s TsVector
toTsVector Maybe TsVectorConfig
Nothing (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
x) =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr context Postgres s TsVector
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr ((PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(PgExpressionSyntax PgSyntax
x') ->
                 PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax) -> PgSyntax -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$
                 ByteString -> PgSyntax
emit ByteString
"to_tsvector(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSyntax
x' PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")") TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
x)
toTsVector (Just (TsVectorConfig ByteString
configNm)) (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
x) =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr context Postgres s TsVector
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr ((PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(PgExpressionSyntax PgSyntax
x') -> PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax) -> PgSyntax -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$
                 ByteString -> PgSyntax
emit ByteString
"to_tsvector('" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
escapeString ByteString
configNm PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
"', " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSyntax
x' PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")") TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
x)

-- | Determine if the given @TSQUERY@ matches the document represented by the
-- @TSVECTOR@. Behaves exactly like the similarly-named operator in postgres.
(@@) :: QGenExpr context Postgres s TsVector
     -> QGenExpr context Postgres s TsQuery
     -> QGenExpr context Postgres s Bool
QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
vec @@ :: QGenExpr context Postgres s TsVector
-> QGenExpr context Postgres s TsQuery
-> QGenExpr context Postgres s Bool
@@ QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
q =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr context Postgres s Bool
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"@@" (PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
vec (TablePrefix -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
q)

-- ** TsQuery type

-- | A query that can be run against a document contained in a 'TsVector'.
--
-- When this field is embedded in a beam table, 'defaultMigratableDbSettings'
-- will give the column the postgres @TSVECTOR@ type
newtype TsQuery = TsQuery ByteString
  deriving (Int -> TsQuery -> ShowS
[TsQuery] -> ShowS
TsQuery -> String
(Int -> TsQuery -> ShowS)
-> (TsQuery -> String) -> ([TsQuery] -> ShowS) -> Show TsQuery
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TsQuery] -> ShowS
$cshowList :: [TsQuery] -> ShowS
show :: TsQuery -> String
$cshow :: TsQuery -> String
showsPrec :: Int -> TsQuery -> ShowS
$cshowsPrec :: Int -> TsQuery -> ShowS
Show, TsQuery -> TsQuery -> Bool
(TsQuery -> TsQuery -> Bool)
-> (TsQuery -> TsQuery -> Bool) -> Eq TsQuery
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TsQuery -> TsQuery -> Bool
$c/= :: TsQuery -> TsQuery -> Bool
== :: TsQuery -> TsQuery -> Bool
$c== :: TsQuery -> TsQuery -> Bool
Eq, Eq TsQuery
Eq TsQuery
-> (TsQuery -> TsQuery -> Ordering)
-> (TsQuery -> TsQuery -> Bool)
-> (TsQuery -> TsQuery -> Bool)
-> (TsQuery -> TsQuery -> Bool)
-> (TsQuery -> TsQuery -> Bool)
-> (TsQuery -> TsQuery -> TsQuery)
-> (TsQuery -> TsQuery -> TsQuery)
-> Ord TsQuery
TsQuery -> TsQuery -> Bool
TsQuery -> TsQuery -> Ordering
TsQuery -> TsQuery -> TsQuery
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: TsQuery -> TsQuery -> TsQuery
$cmin :: TsQuery -> TsQuery -> TsQuery
max :: TsQuery -> TsQuery -> TsQuery
$cmax :: TsQuery -> TsQuery -> TsQuery
>= :: TsQuery -> TsQuery -> Bool
$c>= :: TsQuery -> TsQuery -> Bool
> :: TsQuery -> TsQuery -> Bool
$c> :: TsQuery -> TsQuery -> Bool
<= :: TsQuery -> TsQuery -> Bool
$c<= :: TsQuery -> TsQuery -> Bool
< :: TsQuery -> TsQuery -> Bool
$c< :: TsQuery -> TsQuery -> Bool
compare :: TsQuery -> TsQuery -> Ordering
$ccompare :: TsQuery -> TsQuery -> Ordering
$cp1Ord :: Eq TsQuery
Ord)

instance HasSqlEqualityCheck Postgres TsQuery
instance HasSqlQuantifiedEqualityCheck Postgres TsQuery

instance Pg.FromField TsQuery where
  fromField :: FieldParser TsQuery
fromField Field
field Maybe ByteString
d =
    if Field -> Oid
Pg.typeOid Field
field Oid -> Oid -> Bool
forall a. Eq a => a -> a -> Bool
/= TypeInfo -> Oid
Pg.typoid TypeInfo
pgTsQueryTypeInfo
    then (String -> Maybe Oid -> String -> String -> String -> ResultError)
-> Field -> String -> Conversion TsQuery
forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
Pg.returnError String -> Maybe Oid -> String -> String -> String -> ResultError
Pg.Incompatible Field
field String
""
    else case Maybe ByteString
d of
           Just ByteString
d' -> TsQuery -> Conversion TsQuery
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> TsQuery
TsQuery ByteString
d')
           Maybe ByteString
Nothing -> (String -> Maybe Oid -> String -> String -> String -> ResultError)
-> Field -> String -> Conversion TsQuery
forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
Pg.returnError String -> Maybe Oid -> String -> String -> String -> ResultError
Pg.UnexpectedNull Field
field String
""

instance FromBackendRow Postgres TsQuery

-- | The Postgres @to_tsquery@ function. Given a configuration and string,
-- return the @TSQUERY@ that represents the contents of the string.
toTsQuery :: BeamSqlBackendIsString Postgres str
          => Maybe TsVectorConfig -> QGenExpr context Postgres s str
          -> QGenExpr context Postgres s TsQuery
toTsQuery :: Maybe TsVectorConfig
-> QGenExpr context Postgres s str
-> QGenExpr context Postgres s TsQuery
toTsQuery Maybe TsVectorConfig
Nothing (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
x) =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr context Postgres s TsQuery
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr ((PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(PgExpressionSyntax PgSyntax
x') ->
                 PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax) -> PgSyntax -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$
                 ByteString -> PgSyntax
emit ByteString
"to_tsquery(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSyntax
x' PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")") TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
x)
toTsQuery (Just (TsVectorConfig ByteString
configNm)) (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
x) =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr context Postgres s TsQuery
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr ((PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(PgExpressionSyntax PgSyntax
x') -> PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax) -> PgSyntax -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$
                 ByteString -> PgSyntax
emit ByteString
"to_tsquery('" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
escapeString ByteString
configNm PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
"', " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSyntax
x' PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")") TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
x)

-- ** Array operators

-- TODO this should be robust to slices

-- | Index into the given array. This translates to the @<array>[<index>]@
-- syntax in postgres. The beam operator name has been chosen to match the
-- 'Data.Vector.(!)' operator.
(!.) :: Integral ix
     => QGenExpr context Postgres s (V.Vector a)
     -> QGenExpr context Postgres s ix
     -> QGenExpr context Postgres s a
QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
v !. :: QGenExpr context Postgres s (Vector a)
-> QGenExpr context Postgres s ix -> QGenExpr context Postgres s a
!. QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
ix =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr context Postgres s a
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
index (PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
v (TablePrefix -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
ix)
  where
    index :: PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
index (PgExpressionSyntax PgSyntax
v') (PgExpressionSyntax PgSyntax
ix') =
      PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSyntax
v' PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")[" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSyntax
ix' PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
"]")

-- | Postgres @array_dims()@ function. Returns a textual representation of the
-- dimensions of the array.
arrayDims_ :: BeamSqlBackendIsString Postgres text
           => QGenExpr context Postgres s (V.Vector a)
           -> QGenExpr context Postgres s text
arrayDims_ :: QGenExpr context Postgres s (Vector a)
-> QGenExpr context Postgres s text
arrayDims_ (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
v) = (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr context Postgres s text
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr ((PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(PgExpressionSyntax PgSyntax
v') -> PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"array_dims(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSyntax
v' PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")) TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
v)

type family CountDims (v :: *) :: Nat where
  CountDims (V.Vector a) = 1 + CountDims a
  CountDims a = 0
type family WithinBounds (dim :: Nat) (v :: *) :: Constraint where
  WithinBounds dim v =
    If ((dim <=? CountDims v) && (1 <=? dim))
       (() :: Constraint)
       (TypeError ( ('Text "Dimension " ':<>: 'ShowType dim ':<>: 'Text " is out of bounds.") ':$$:
                    ('Text "The type " ':<>: 'ShowType v ':<>: 'Text " has " ':<>: 'ShowType (CountDims v) ':<>: 'Text " dimension(s).") ':$$:
                    ('Text "Hint: The dimension should be a natural between 1 and " ':<>: 'ShowType (CountDims v)) ))

-- | Return the upper or lower bound of the given array at the given dimension
--  (statically supplied as a type application on a 'GHC.TypeLits.Nat'). Note
--  that beam will attempt to statically determine if the dimension is in range.
--  GHC errors will be thrown if this cannot be proved.
--
-- For example, to get the upper bound of the 2nd-dimension of an array:
--
-- @
-- arrayUpper_ @2 vectorValuedExpression
-- @
arrayUpper_, arrayLower_
  :: forall (dim :: Nat) context num v s.
     (KnownNat dim, WithinBounds dim (V.Vector v), Integral num)
  => QGenExpr context Postgres s (V.Vector v)
  -> QGenExpr context Postgres s num
arrayUpper_ :: QGenExpr context Postgres s (Vector v)
-> QGenExpr context Postgres s num
arrayUpper_ QGenExpr context Postgres s (Vector v)
v =
  QGenExpr context Postgres s (Maybe Integer)
-> QGenExpr context Postgres s num
forall ctxt be s a a'.
QGenExpr ctxt be s a -> QGenExpr ctxt be s a'
unsafeRetype (QGenExpr context Postgres s (Vector v)
-> QGenExpr context Postgres s Integer
-> QGenExpr context Postgres s (Maybe Integer)
forall dim length context s v.
(Integral dim, Integral length) =>
QGenExpr context Postgres s (Vector v)
-> QGenExpr context Postgres s dim
-> QGenExpr context Postgres s (Maybe length)
arrayUpperUnsafe_ QGenExpr context Postgres s (Vector v)
v (HaskellLiteralForQExpr (QGenExpr context Postgres s Integer)
-> QGenExpr context Postgres s Integer
forall a. SqlValable a => HaskellLiteralForQExpr a -> a
val_ (Proxy dim -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy dim
forall k (t :: k). Proxy t
Proxy @dim) :: Integer)) :: QGenExpr context Postgres s (Maybe Integer))
arrayLower_ :: QGenExpr context Postgres s (Vector v)
-> QGenExpr context Postgres s num
arrayLower_ QGenExpr context Postgres s (Vector v)
v =
  QGenExpr context Postgres s (Maybe Integer)
-> QGenExpr context Postgres s num
forall ctxt be s a a'.
QGenExpr ctxt be s a -> QGenExpr ctxt be s a'
unsafeRetype (QGenExpr context Postgres s (Vector v)
-> QGenExpr context Postgres s Integer
-> QGenExpr context Postgres s (Maybe Integer)
forall dim length context s v.
(Integral dim, Integral length) =>
QGenExpr context Postgres s (Vector v)
-> QGenExpr context Postgres s dim
-> QGenExpr context Postgres s (Maybe length)
arrayLowerUnsafe_ QGenExpr context Postgres s (Vector v)
v (HaskellLiteralForQExpr (QGenExpr context Postgres s Integer)
-> QGenExpr context Postgres s Integer
forall a. SqlValable a => HaskellLiteralForQExpr a -> a
val_ (Proxy dim -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy dim
forall k (t :: k). Proxy t
Proxy @dim) :: Integer)) :: QGenExpr context Postgres s (Maybe Integer))

-- | These functions can be used to find the lower and upper bounds of an array
-- where the dimension number is not known until run-time. They are marked
-- unsafe because they may cause query processing to fail at runtime, even if
-- they typecheck successfully.
arrayUpperUnsafe_, arrayLowerUnsafe_
  :: (Integral dim, Integral length)
  => QGenExpr context Postgres s (V.Vector v)
  -> QGenExpr context Postgres s dim
  -> QGenExpr context Postgres s (Maybe length)
arrayUpperUnsafe_ :: QGenExpr context Postgres s (Vector v)
-> QGenExpr context Postgres s dim
-> QGenExpr context Postgres s (Maybe length)
arrayUpperUnsafe_ (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
v) (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
dim) =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr context Postgres s (Maybe length)
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (([PgSyntax] -> PgExpressionSyntax)
-> (TablePrefix -> [PgSyntax]) -> TablePrefix -> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax)
-> ([PgSyntax] -> PgSyntax) -> [PgSyntax] -> PgExpressionSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [PgSyntax] -> PgSyntax
forall a. Monoid a => [a] -> a
mconcat) ((TablePrefix -> [PgSyntax]) -> TablePrefix -> PgExpressionSyntax)
-> ([TablePrefix -> PgSyntax] -> TablePrefix -> [PgSyntax])
-> [TablePrefix -> PgSyntax]
-> TablePrefix
-> PgExpressionSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [TablePrefix -> PgSyntax] -> TablePrefix -> [PgSyntax]
forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
sequenceA ([TablePrefix -> PgSyntax] -> TablePrefix -> PgExpressionSyntax)
-> [TablePrefix -> PgSyntax] -> TablePrefix -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$
         [ PgSyntax -> TablePrefix -> PgSyntax
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> PgSyntax
emit ByteString
"array_upper(")
         , PgExpressionSyntax -> PgSyntax
fromPgExpression (PgExpressionSyntax -> PgSyntax)
-> (TablePrefix -> PgExpressionSyntax) -> TablePrefix -> PgSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
v
         , PgSyntax -> TablePrefix -> PgSyntax
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> PgSyntax
emit ByteString
", ")
         , PgExpressionSyntax -> PgSyntax
fromPgExpression (PgExpressionSyntax -> PgSyntax)
-> (TablePrefix -> PgExpressionSyntax) -> TablePrefix -> PgSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
dim
         , PgSyntax -> TablePrefix -> PgSyntax
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> PgSyntax
emit ByteString
")") ])
arrayLowerUnsafe_ :: QGenExpr context Postgres s (Vector v)
-> QGenExpr context Postgres s dim
-> QGenExpr context Postgres s (Maybe length)
arrayLowerUnsafe_ (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
v) (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
dim) =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr context Postgres s (Maybe length)
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (([PgSyntax] -> PgExpressionSyntax)
-> (TablePrefix -> [PgSyntax]) -> TablePrefix -> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax)
-> ([PgSyntax] -> PgSyntax) -> [PgSyntax] -> PgExpressionSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [PgSyntax] -> PgSyntax
forall a. Monoid a => [a] -> a
mconcat) ((TablePrefix -> [PgSyntax]) -> TablePrefix -> PgExpressionSyntax)
-> ([TablePrefix -> PgSyntax] -> TablePrefix -> [PgSyntax])
-> [TablePrefix -> PgSyntax]
-> TablePrefix
-> PgExpressionSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [TablePrefix -> PgSyntax] -> TablePrefix -> [PgSyntax]
forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
sequenceA ([TablePrefix -> PgSyntax] -> TablePrefix -> PgExpressionSyntax)
-> [TablePrefix -> PgSyntax] -> TablePrefix -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$
         [ PgSyntax -> TablePrefix -> PgSyntax
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> PgSyntax
emit ByteString
"array_lower(")
         , PgExpressionSyntax -> PgSyntax
fromPgExpression (PgExpressionSyntax -> PgSyntax)
-> (TablePrefix -> PgExpressionSyntax) -> TablePrefix -> PgSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
v
         , PgSyntax -> TablePrefix -> PgSyntax
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> PgSyntax
emit ByteString
", ")
         , PgExpressionSyntax -> PgSyntax
fromPgExpression (PgExpressionSyntax -> PgSyntax)
-> (TablePrefix -> PgExpressionSyntax) -> TablePrefix -> PgSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
dim
         , PgSyntax -> TablePrefix -> PgSyntax
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> PgSyntax
emit ByteString
")") ])

-- | Get the size of the array at the given (statically known) dimension,
-- provided as a type-level 'Nat'. Like the 'arrayUpper_' and 'arrayLower_'
-- functions,throws a compile-time error if the dimension is out of bounds.
arrayLength_
  :: forall (dim :: Nat) ctxt num v s.
     (KnownNat dim, WithinBounds dim (V.Vector v), Integral num)
  => QGenExpr ctxt Postgres s (V.Vector v)
  -> QGenExpr ctxt Postgres s num
arrayLength_ :: QGenExpr ctxt Postgres s (Vector v) -> QGenExpr ctxt Postgres s num
arrayLength_ QGenExpr ctxt Postgres s (Vector v)
v =
  QGenExpr ctxt Postgres s (Maybe Integer)
-> QGenExpr ctxt Postgres s num
forall ctxt be s a a'.
QGenExpr ctxt be s a -> QGenExpr ctxt be s a'
unsafeRetype (QGenExpr ctxt Postgres s (Vector v)
-> QGenExpr ctxt Postgres s Integer
-> QGenExpr ctxt Postgres s (Maybe Integer)
forall dim length context s v.
(Integral dim, Integral length) =>
QGenExpr context Postgres s (Vector v)
-> QGenExpr context Postgres s dim
-> QGenExpr context Postgres s (Maybe length)
arrayLengthUnsafe_ QGenExpr ctxt Postgres s (Vector v)
v (HaskellLiteralForQExpr (QGenExpr ctxt Postgres s Integer)
-> QGenExpr ctxt Postgres s Integer
forall a. SqlValable a => HaskellLiteralForQExpr a -> a
val_ (Proxy dim -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy dim
forall k (t :: k). Proxy t
Proxy @dim) :: Integer)) :: QGenExpr ctxt Postgres s (Maybe Integer))

-- | Get the size of an array at a dimension not known until run-time. Marked
-- unsafe as this may cause runtime errors even if it type checks.
arrayLengthUnsafe_
  :: (Integral dim, Integral num)
  => QGenExpr ctxt Postgres s (V.Vector v)
  -> QGenExpr ctxt Postgres s dim
  -> QGenExpr ctxt Postgres s (Maybe num)
arrayLengthUnsafe_ :: QGenExpr ctxt Postgres s (Vector v)
-> QGenExpr ctxt Postgres s dim
-> QGenExpr ctxt Postgres s (Maybe num)
arrayLengthUnsafe_ (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a) (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
dim) =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s (Maybe num)
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr ((TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
 -> QGenExpr ctxt Postgres s (Maybe num))
-> (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s (Maybe num)
forall a b. (a -> b) -> a -> b
$ ([PgSyntax] -> PgExpressionSyntax)
-> (TablePrefix -> [PgSyntax]) -> TablePrefix -> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax)
-> ([PgSyntax] -> PgSyntax) -> [PgSyntax] -> PgExpressionSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [PgSyntax] -> PgSyntax
forall a. Monoid a => [a] -> a
mconcat) ((TablePrefix -> [PgSyntax]) -> TablePrefix -> PgExpressionSyntax)
-> (TablePrefix -> [PgSyntax]) -> TablePrefix -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$ [TablePrefix -> PgSyntax] -> TablePrefix -> [PgSyntax]
forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
sequenceA ([TablePrefix -> PgSyntax] -> TablePrefix -> [PgSyntax])
-> [TablePrefix -> PgSyntax] -> TablePrefix -> [PgSyntax]
forall a b. (a -> b) -> a -> b
$
  [ PgSyntax -> TablePrefix -> PgSyntax
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> PgSyntax
emit ByteString
"array_length(")
  , PgExpressionSyntax -> PgSyntax
fromPgExpression (PgExpressionSyntax -> PgSyntax)
-> (TablePrefix -> PgExpressionSyntax) -> TablePrefix -> PgSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a
  , PgSyntax -> TablePrefix -> PgSyntax
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> PgSyntax
emit ByteString
", ")
  , PgExpressionSyntax -> PgSyntax
fromPgExpression (PgExpressionSyntax -> PgSyntax)
-> (TablePrefix -> PgExpressionSyntax) -> TablePrefix -> PgSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
dim
  , PgSyntax -> TablePrefix -> PgSyntax
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> PgSyntax
emit ByteString
")") ]

-- | The Postgres @&#x40;>@ operator. Returns true if every member of the second
-- array is present in the first.
isSupersetOf_ :: QGenExpr ctxt Postgres s (V.Vector a)
              -> QGenExpr ctxt Postgres s (V.Vector a)
              -> QGenExpr ctxt Postgres s Bool
isSupersetOf_ :: QGenExpr ctxt Postgres s (Vector a)
-> QGenExpr ctxt Postgres s (Vector a)
-> QGenExpr ctxt Postgres s Bool
isSupersetOf_ (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
haystack) (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
needles) =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s Bool
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"@>" (PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
haystack (TablePrefix -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
needles)

-- | The Postgres @<&#x40;@ operator. Returns true if every member of the first
-- array is present in the second.
isSubsetOf_ :: QGenExpr ctxt Postgres s (V.Vector a)
            -> QGenExpr ctxt Postgres s (V.Vector a)
            -> QGenExpr ctxt Postgres s Bool
isSubsetOf_ :: QGenExpr ctxt Postgres s (Vector a)
-> QGenExpr ctxt Postgres s (Vector a)
-> QGenExpr ctxt Postgres s Bool
isSubsetOf_ (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
needles) (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
haystack) =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s Bool
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"<@" (PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
needles (TablePrefix -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
haystack)

-- | Postgres @||@ operator. Concatenates two vectors and returns their result.
(++.) :: QGenExpr ctxt Postgres s (V.Vector a)
      -> QGenExpr ctxt Postgres s (V.Vector a)
      -> QGenExpr ctxt Postgres s (V.Vector a)
QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a ++. :: QGenExpr ctxt Postgres s (Vector a)
-> QGenExpr ctxt Postgres s (Vector a)
-> QGenExpr ctxt Postgres s (Vector a)
++. QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
b =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s (Vector a)
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"||" (PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a (TablePrefix -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
b)

-- ** Array expressions

-- | An expression context that determines which types of expressions can be put
-- inside an array element. Any scalar, aggregate, or window expression can be
-- placed within an array.
data PgArrayValueContext

-- | If you are extending beam-postgres and provide another expression context
-- that can be represented in an array, provide an empty instance of this class.
class PgIsArrayContext ctxt where
  mkArraySyntax :: Proxy ctxt -> PgSyntax -> PgSyntax
  mkArraySyntax Proxy ctxt
_ PgSyntax
s = ByteString -> PgSyntax
emit ByteString
"ARRAY" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSyntax
s
instance PgIsArrayContext PgArrayValueContext where
  mkArraySyntax :: Proxy PgArrayValueContext -> PgSyntax -> PgSyntax
mkArraySyntax Proxy PgArrayValueContext
_ = PgSyntax -> PgSyntax
forall a. a -> a
id
instance PgIsArrayContext QValueContext
instance PgIsArrayContext QAggregateContext
instance PgIsArrayContext QWindowingContext

-- | Build a 1-dimensional postgres array from an arbitrary 'Foldable'
-- containing expressions.
array_ :: forall context f s a.
          (PgIsArrayContext context, Foldable f)
       => f (QGenExpr context Postgres s a)
       -> QGenExpr context Postgres s (V.Vector a)
array_ :: f (QGenExpr context Postgres s a)
-> QGenExpr context Postgres s (Vector a)
array_ f (QGenExpr context Postgres s a)
vs =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr context Postgres s (Vector a)
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr ((TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
 -> QGenExpr context Postgres s (Vector a))
-> (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr context Postgres s (Vector a)
forall a b. (a -> b) -> a -> b
$ ([PgSyntax] -> PgExpressionSyntax)
-> (TablePrefix -> [PgSyntax]) -> TablePrefix -> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax)
-> ([PgSyntax] -> PgSyntax) -> [PgSyntax] -> PgExpressionSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Proxy context -> PgSyntax -> PgSyntax
forall k (ctxt :: k).
PgIsArrayContext ctxt =>
Proxy ctxt -> PgSyntax -> PgSyntax
mkArraySyntax (Proxy context
forall k (t :: k). Proxy t
Proxy @context) (PgSyntax -> PgSyntax)
-> ([PgSyntax] -> PgSyntax) -> [PgSyntax] -> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [PgSyntax] -> PgSyntax
forall a. Monoid a => [a] -> a
mconcat) ((TablePrefix -> [PgSyntax]) -> TablePrefix -> PgExpressionSyntax)
-> (TablePrefix -> [PgSyntax]) -> TablePrefix -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$
  [TablePrefix -> PgSyntax] -> TablePrefix -> [PgSyntax]
forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
sequenceA [ PgSyntax -> TablePrefix -> PgSyntax
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> PgSyntax
emit ByteString
"[")
            , PgSyntax -> [PgSyntax] -> PgSyntax
pgSepBy (ByteString -> PgSyntax
emit ByteString
", ") ([PgSyntax] -> PgSyntax)
-> (TablePrefix -> [PgSyntax]) -> TablePrefix -> PgSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (QGenExpr context Postgres s a -> TablePrefix -> PgSyntax)
-> [QGenExpr context Postgres s a] -> TablePrefix -> [PgSyntax]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (\(QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
e) -> PgExpressionSyntax -> PgSyntax
fromPgExpression (PgExpressionSyntax -> PgSyntax)
-> (TablePrefix -> PgExpressionSyntax) -> TablePrefix -> PgSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
e) (f (QGenExpr context Postgres s a)
-> [QGenExpr context Postgres s a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList f (QGenExpr context Postgres s a)
vs)
            , PgSyntax -> TablePrefix -> PgSyntax
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> PgSyntax
emit ByteString
"]") ]

-- | Build a 1-dimensional postgres array from a subquery
arrayOf_ :: Q Postgres db s (QExpr Postgres s a)
         -> QGenExpr context Postgres s (V.Vector a)
arrayOf_ :: Q Postgres db s (QExpr Postgres s a)
-> QGenExpr context Postgres s (Vector a)
arrayOf_ Q Postgres db s (QExpr Postgres s a)
q =
  let QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
sub = Q Postgres db s (QExpr Postgres s a) -> QGenExpr Any Postgres s a
forall be s a (db :: (* -> *) -> *) ctxt.
(BeamSqlBackend be, HasQBuilder be,
 Projectible be (QExpr be s a)) =>
Q be db s (QExpr be s a) -> QGenExpr ctxt be s a
subquery_ Q Postgres db s (QExpr Postgres s a)
q
  in (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr context Postgres s (Vector a)
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (\TablePrefix
t -> let PgExpressionSyntax PgSyntax
sub' = TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
sub TablePrefix
t
                  in PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"ARRAY(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSyntax
sub' PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")"))

-- ** Ranges

-- | Represents the types of bounds a range can have. A range can and often does have mis-matched
-- bound types.
data PgBoundType
  = Inclusive
  | Exclusive
  deriving (PgBoundType -> PgBoundType -> Bool
(PgBoundType -> PgBoundType -> Bool)
-> (PgBoundType -> PgBoundType -> Bool) -> Eq PgBoundType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PgBoundType -> PgBoundType -> Bool
$c/= :: PgBoundType -> PgBoundType -> Bool
== :: PgBoundType -> PgBoundType -> Bool
$c== :: PgBoundType -> PgBoundType -> Bool
Eq, Int -> PgBoundType -> ShowS
[PgBoundType] -> ShowS
PgBoundType -> String
(Int -> PgBoundType -> ShowS)
-> (PgBoundType -> String)
-> ([PgBoundType] -> ShowS)
-> Show PgBoundType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PgBoundType] -> ShowS
$cshowList :: [PgBoundType] -> ShowS
show :: PgBoundType -> String
$cshow :: PgBoundType -> String
showsPrec :: Int -> PgBoundType -> ShowS
$cshowsPrec :: Int -> PgBoundType -> ShowS
Show, (forall x. PgBoundType -> Rep PgBoundType x)
-> (forall x. Rep PgBoundType x -> PgBoundType)
-> Generic PgBoundType
forall x. Rep PgBoundType x -> PgBoundType
forall x. PgBoundType -> Rep PgBoundType x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep PgBoundType x -> PgBoundType
$cfrom :: forall x. PgBoundType -> Rep PgBoundType x
Generic)
instance Hashable PgBoundType

lBound :: PgBoundType -> ByteString
lBound :: PgBoundType -> ByteString
lBound PgBoundType
Inclusive = ByteString
"["
lBound PgBoundType
Exclusive = ByteString
"("

uBound :: PgBoundType -> ByteString
uBound :: PgBoundType -> ByteString
uBound PgBoundType
Inclusive = ByteString
"]"
uBound PgBoundType
Exclusive = ByteString
")"

-- | Represents a single bound on a Range. A bound always has a type, but may not have a value
-- (the absense of a value represents unbounded).
data PgRangeBound a = PgRangeBound PgBoundType (Maybe a) deriving (PgRangeBound a -> PgRangeBound a -> Bool
(PgRangeBound a -> PgRangeBound a -> Bool)
-> (PgRangeBound a -> PgRangeBound a -> Bool)
-> Eq (PgRangeBound a)
forall a. Eq a => PgRangeBound a -> PgRangeBound a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PgRangeBound a -> PgRangeBound a -> Bool
$c/= :: forall a. Eq a => PgRangeBound a -> PgRangeBound a -> Bool
== :: PgRangeBound a -> PgRangeBound a -> Bool
$c== :: forall a. Eq a => PgRangeBound a -> PgRangeBound a -> Bool
Eq, Int -> PgRangeBound a -> ShowS
[PgRangeBound a] -> ShowS
PgRangeBound a -> String
(Int -> PgRangeBound a -> ShowS)
-> (PgRangeBound a -> String)
-> ([PgRangeBound a] -> ShowS)
-> Show (PgRangeBound a)
forall a. Show a => Int -> PgRangeBound a -> ShowS
forall a. Show a => [PgRangeBound a] -> ShowS
forall a. Show a => PgRangeBound a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PgRangeBound a] -> ShowS
$cshowList :: forall a. Show a => [PgRangeBound a] -> ShowS
show :: PgRangeBound a -> String
$cshow :: forall a. Show a => PgRangeBound a -> String
showsPrec :: Int -> PgRangeBound a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> PgRangeBound a -> ShowS
Show, (forall x. PgRangeBound a -> Rep (PgRangeBound a) x)
-> (forall x. Rep (PgRangeBound a) x -> PgRangeBound a)
-> Generic (PgRangeBound a)
forall x. Rep (PgRangeBound a) x -> PgRangeBound a
forall x. PgRangeBound a -> Rep (PgRangeBound a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (PgRangeBound a) x -> PgRangeBound a
forall a x. PgRangeBound a -> Rep (PgRangeBound a) x
$cto :: forall a x. Rep (PgRangeBound a) x -> PgRangeBound a
$cfrom :: forall a x. PgRangeBound a -> Rep (PgRangeBound a) x
Generic)

inclusive :: a -> PgRangeBound a
inclusive :: a -> PgRangeBound a
inclusive = PgBoundType -> Maybe a -> PgRangeBound a
forall a. PgBoundType -> Maybe a -> PgRangeBound a
PgRangeBound PgBoundType
Inclusive (Maybe a -> PgRangeBound a)
-> (a -> Maybe a) -> a -> PgRangeBound a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Maybe a
forall a. a -> Maybe a
Just

exclusive :: a -> PgRangeBound a
exclusive :: a -> PgRangeBound a
exclusive = PgBoundType -> Maybe a -> PgRangeBound a
forall a. PgBoundType -> Maybe a -> PgRangeBound a
PgRangeBound PgBoundType
Exclusive (Maybe a -> PgRangeBound a)
-> (a -> Maybe a) -> a -> PgRangeBound a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Maybe a
forall a. a -> Maybe a
Just

unbounded :: PgRangeBound a
unbounded :: PgRangeBound a
unbounded = PgBoundType -> Maybe a -> PgRangeBound a
forall a. PgBoundType -> Maybe a -> PgRangeBound a
PgRangeBound PgBoundType
Exclusive Maybe a
forall a. Maybe a
Nothing

-- | A range of a given Haskell type (represented by @a@) stored as a given Postgres Range Type
-- (represented by @n@).
--
-- A reasonable example might be @Range PgInt8Range Int64@.
-- This represents a range of Haskell @Int64@ values stored as a range of 'bigint' in Postgres.
data PgRange (n :: *) a
  = PgEmptyRange
  | PgRange (PgRangeBound a) (PgRangeBound a)
  deriving (PgRange n a -> PgRange n a -> Bool
(PgRange n a -> PgRange n a -> Bool)
-> (PgRange n a -> PgRange n a -> Bool) -> Eq (PgRange n a)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall n a. Eq a => PgRange n a -> PgRange n a -> Bool
/= :: PgRange n a -> PgRange n a -> Bool
$c/= :: forall n a. Eq a => PgRange n a -> PgRange n a -> Bool
== :: PgRange n a -> PgRange n a -> Bool
$c== :: forall n a. Eq a => PgRange n a -> PgRange n a -> Bool
Eq, Int -> PgRange n a -> ShowS
[PgRange n a] -> ShowS
PgRange n a -> String
(Int -> PgRange n a -> ShowS)
-> (PgRange n a -> String)
-> ([PgRange n a] -> ShowS)
-> Show (PgRange n a)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall n a. Show a => Int -> PgRange n a -> ShowS
forall n a. Show a => [PgRange n a] -> ShowS
forall n a. Show a => PgRange n a -> String
showList :: [PgRange n a] -> ShowS
$cshowList :: forall n a. Show a => [PgRange n a] -> ShowS
show :: PgRange n a -> String
$cshow :: forall n a. Show a => PgRange n a -> String
showsPrec :: Int -> PgRange n a -> ShowS
$cshowsPrec :: forall n a. Show a => Int -> PgRange n a -> ShowS
Show, (forall x. PgRange n a -> Rep (PgRange n a) x)
-> (forall x. Rep (PgRange n a) x -> PgRange n a)
-> Generic (PgRange n a)
forall x. Rep (PgRange n a) x -> PgRange n a
forall x. PgRange n a -> Rep (PgRange n a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall n a x. Rep (PgRange n a) x -> PgRange n a
forall n a x. PgRange n a -> Rep (PgRange n a) x
$cto :: forall n a x. Rep (PgRange n a) x -> PgRange n a
$cfrom :: forall n a x. PgRange n a -> Rep (PgRange n a) x
Generic)

instance Hashable a => Hashable (PgRangeBound a)

instance Hashable a => Hashable (PgRange n a)

-- | A class representing Postgres Range types and how to refer to them when speaking to the
-- database.
--
-- For custom Range types, create an uninhabited type, and make it an instance of this class.
class PgIsRange n where
  -- | The range type name in the database.
  rangeName :: ByteString

data PgInt4Range
instance PgIsRange PgInt4Range where
  rangeName :: ByteString
rangeName = ByteString
"int4range"

data PgInt8Range
instance PgIsRange PgInt8Range where
  rangeName :: ByteString
rangeName = ByteString
"int8range"

data PgNumRange
instance PgIsRange PgNumRange where
  rangeName :: ByteString
rangeName = ByteString
"numrange"

data PgTsRange
instance PgIsRange PgTsRange where
  rangeName :: ByteString
rangeName = ByteString
"tsrange"

data PgTsTzRange
instance PgIsRange PgTsTzRange where
  rangeName :: ByteString
rangeName = ByteString
"tstzrange"

data PgDateRange
instance PgIsRange PgDateRange where
  rangeName :: ByteString
rangeName = ByteString
"daterange"

instance (Pg.FromField a, Typeable a, Typeable n, Ord a) => Pg.FromField (PgRange n a) where
  fromField :: FieldParser (PgRange n a)
fromField Field
field Maybe ByteString
d = do
    PGRange a
pgR :: Pg.PGRange a <- FieldParser (PGRange a)
forall a. FromField a => FieldParser a
Pg.fromField Field
field Maybe ByteString
d
    if PGRange a -> Bool
forall a. Ord a => PGRange a -> Bool
Pg.isEmpty PGRange a
pgR
    then PgRange n a -> Conversion (PgRange n a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure PgRange n a
forall n a. PgRange n a
PgEmptyRange
    else let Pg.PGRange RangeBound a
lRange RangeBound a
rRange = PGRange a
pgR
         in PgRange n a -> Conversion (PgRange n a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PgRange n a -> Conversion (PgRange n a))
-> PgRange n a -> Conversion (PgRange n a)
forall a b. (a -> b) -> a -> b
$ PgRangeBound a -> PgRangeBound a -> PgRange n a
forall n a. PgRangeBound a -> PgRangeBound a -> PgRange n a
PgRange (RangeBound a -> PgRangeBound a
forall a. RangeBound a -> PgRangeBound a
boundConv RangeBound a
lRange) (RangeBound a -> PgRangeBound a
forall a. RangeBound a -> PgRangeBound a
boundConv RangeBound a
rRange)

-- According to Postgres docs, there is no such thing as an inclusive infinite bound.
-- https://www.postgresql.org/docs/10/static/rangetypes.html#RANGETYPES-INFINITE
boundConv :: Pg.RangeBound a -> PgRangeBound a
boundConv :: RangeBound a -> PgRangeBound a
boundConv RangeBound a
Pg.NegInfinity = PgBoundType -> Maybe a -> PgRangeBound a
forall a. PgBoundType -> Maybe a -> PgRangeBound a
PgRangeBound PgBoundType
Exclusive Maybe a
forall a. Maybe a
Nothing
boundConv RangeBound a
Pg.PosInfinity = PgBoundType -> Maybe a -> PgRangeBound a
forall a. PgBoundType -> Maybe a -> PgRangeBound a
PgRangeBound PgBoundType
Exclusive Maybe a
forall a. Maybe a
Nothing
boundConv (Pg.Inclusive a
a) = PgBoundType -> Maybe a -> PgRangeBound a
forall a. PgBoundType -> Maybe a -> PgRangeBound a
PgRangeBound PgBoundType
Inclusive (a -> Maybe a
forall a. a -> Maybe a
Just a
a)
boundConv (Pg.Exclusive a
a) = PgBoundType -> Maybe a -> PgRangeBound a
forall a. PgBoundType -> Maybe a -> PgRangeBound a
PgRangeBound PgBoundType
Exclusive (a -> Maybe a
forall a. a -> Maybe a
Just a
a)

instance (Pg.ToField (Pg.PGRange a)) => Pg.ToField (PgRange n a) where
  toField :: PgRange n a -> Action
toField PgRange n a
PgEmptyRange = PGRange a -> Action
forall a. ToField a => a -> Action
Pg.toField (PGRange a
forall a. PGRange a
Pg.empty :: Pg.PGRange a)
  toField (PgRange (PgRangeBound PgBoundType
lt Maybe a
lb) (PgRangeBound PgBoundType
ut Maybe a
ub)) = PGRange a -> Action
forall a. ToField a => a -> Action
Pg.toField PGRange a
r'
    where
      r' :: PGRange a
r' = RangeBound a -> RangeBound a -> PGRange a
forall a. RangeBound a -> RangeBound a -> PGRange a
Pg.PGRange RangeBound a
lb' RangeBound a
ub'
      lb' :: RangeBound a
lb' = case (PgBoundType
lt, Maybe a
lb) of (PgBoundType
_, Maybe a
Nothing) -> RangeBound a
forall a. RangeBound a
Pg.NegInfinity
                             (PgBoundType
Inclusive, Just a
a) -> a -> RangeBound a
forall a. a -> RangeBound a
Pg.Inclusive a
a
                             (PgBoundType
Exclusive, Just a
a) -> a -> RangeBound a
forall a. a -> RangeBound a
Pg.Exclusive a
a
      ub' :: RangeBound a
ub' = case (PgBoundType
ut, Maybe a
ub) of (PgBoundType
_, Maybe a
Nothing) -> RangeBound a
forall a. RangeBound a
Pg.PosInfinity
                             (PgBoundType
Inclusive, Just a
a) -> a -> RangeBound a
forall a. a -> RangeBound a
Pg.Inclusive a
a
                             (PgBoundType
Exclusive, Just a
a) -> a -> RangeBound a
forall a. a -> RangeBound a
Pg.Exclusive a
a

instance HasSqlEqualityCheck Postgres (PgRange n a)
instance HasSqlQuantifiedEqualityCheck Postgres (PgRange n a)

instance (Pg.FromField a, Typeable a, Typeable n, Ord a) => FromBackendRow Postgres (PgRange n a)
instance (HasSqlValueSyntax PgValueSyntax a, PgIsRange n) =>
  HasSqlValueSyntax PgValueSyntax (PgRange n a) where
  sqlValueSyntax :: PgRange n a -> PgValueSyntax
sqlValueSyntax PgRange n a
PgEmptyRange =
    PgSyntax -> PgValueSyntax
PgValueSyntax (PgSyntax -> PgValueSyntax) -> PgSyntax -> PgValueSyntax
forall a b. (a -> b) -> a -> b
$
    ByteString -> PgSyntax
emit ByteString
"'empty'::" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
escapeIdentifier (PgIsRange n => ByteString
forall k (n :: k). PgIsRange n => ByteString
rangeName @n)
  sqlValueSyntax (PgRange (PgRangeBound PgBoundType
lbt Maybe a
mlBound) (PgRangeBound PgBoundType
rbt Maybe a
muBound)) =
    PgSyntax -> PgValueSyntax
PgValueSyntax (PgSyntax -> PgValueSyntax) -> PgSyntax -> PgValueSyntax
forall a b. (a -> b) -> a -> b
$
    ByteString -> PgSyntax
escapeIdentifier (PgIsRange n => ByteString
forall k (n :: k). PgIsRange n => ByteString
rangeName @n) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSyntax -> PgSyntax
pgParens (PgSyntax -> [PgSyntax] -> PgSyntax
pgSepBy (ByteString -> PgSyntax
emit ByteString
", ") [PgSyntax
lb, PgSyntax
rb, PgSyntax
bounds])
    where
      lb :: PgSyntax
lb = Maybe a -> PgSyntax
sqlValueSyntax' Maybe a
mlBound
      rb :: PgSyntax
rb = Maybe a -> PgSyntax
sqlValueSyntax' Maybe a
muBound
      bounds :: PgSyntax
bounds = ByteString -> PgSyntax
emit ByteString
"'" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit (PgBoundType -> ByteString
lBound PgBoundType
lbt ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> PgBoundType -> ByteString
uBound PgBoundType
rbt) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
"'"
      sqlValueSyntax' :: Maybe a -> PgSyntax
sqlValueSyntax' = PgValueSyntax -> PgSyntax
fromPgValue (PgValueSyntax -> PgSyntax)
-> (Maybe a -> PgValueSyntax) -> Maybe a -> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe a -> PgValueSyntax
forall expr ty. HasSqlValueSyntax expr ty => ty -> expr
sqlValueSyntax


binOpDefault :: ByteString
             -> QGenExpr context Postgres s a
             -> QGenExpr context Postgres s b
             -> QGenExpr context Postgres s c
binOpDefault :: ByteString
-> QGenExpr context Postgres s a
-> QGenExpr context Postgres s b
-> QGenExpr context Postgres s c
binOpDefault ByteString
symbol (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
r1) (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
r2)  = (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr context Postgres s c
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
symbol (PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
r1 (TablePrefix -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
r2)

(-@>-) :: QGenExpr context Postgres s (PgRange n a)
      -> QGenExpr context Postgres s (PgRange n a)
      -> QGenExpr context Postgres s Bool
-@>- :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
(-@>-) = ByteString
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
forall context s a b c.
ByteString
-> QGenExpr context Postgres s a
-> QGenExpr context Postgres s b
-> QGenExpr context Postgres s c
binOpDefault ByteString
"@>"

(-@>) :: QGenExpr context Postgres s (PgRange n a)
      -> QGenExpr context Postgres s a
      -> QGenExpr context Postgres s Bool
-@> :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s a
-> QGenExpr context Postgres s Bool
(-@>) = ByteString
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s a
-> QGenExpr context Postgres s Bool
forall context s a b c.
ByteString
-> QGenExpr context Postgres s a
-> QGenExpr context Postgres s b
-> QGenExpr context Postgres s c
binOpDefault ByteString
"@>"

(-<@-) :: QGenExpr context Postgres s (PgRange n a)
       -> QGenExpr context Postgres s (PgRange n a)
       -> QGenExpr context Postgres s Bool
-<@- :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
(-<@-) = ByteString
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
forall context s a b c.
ByteString
-> QGenExpr context Postgres s a
-> QGenExpr context Postgres s b
-> QGenExpr context Postgres s c
binOpDefault ByteString
"<@"

(<@-) :: QGenExpr context Postgres s a
      -> QGenExpr context Postgres s (PgRange n a)
      -> QGenExpr context Postgres s Bool
<@- :: QGenExpr context Postgres s a
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
(<@-) = ByteString
-> QGenExpr context Postgres s a
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
forall context s a b c.
ByteString
-> QGenExpr context Postgres s a
-> QGenExpr context Postgres s b
-> QGenExpr context Postgres s c
binOpDefault ByteString
"<@"

(-&&-) :: QGenExpr context Postgres s (PgRange n a)
       -> QGenExpr context Postgres s (PgRange n a)
       -> QGenExpr context Postgres s Bool
-&&- :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
(-&&-) = ByteString
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
forall context s a b c.
ByteString
-> QGenExpr context Postgres s a
-> QGenExpr context Postgres s b
-> QGenExpr context Postgres s c
binOpDefault ByteString
"&&"

(-<<-) :: QGenExpr context Postgres s (PgRange n a)
       -> QGenExpr context Postgres s (PgRange n a)
       -> QGenExpr context Postgres s Bool
-<<- :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
(-<<-) = ByteString
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
forall context s a b c.
ByteString
-> QGenExpr context Postgres s a
-> QGenExpr context Postgres s b
-> QGenExpr context Postgres s c
binOpDefault ByteString
"<<"

(->>-) :: QGenExpr context Postgres s (PgRange n a)
       -> QGenExpr context Postgres s (PgRange n a)
       -> QGenExpr context Postgres s Bool
->>- :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
(->>-) = ByteString
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
forall context s a b c.
ByteString
-> QGenExpr context Postgres s a
-> QGenExpr context Postgres s b
-> QGenExpr context Postgres s c
binOpDefault ByteString
">>"

(-&<-) :: QGenExpr context Postgres s (PgRange n a)
       -> QGenExpr context Postgres s (PgRange n a)
       -> QGenExpr context Postgres s Bool
-&<- :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
(-&<-) = ByteString
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
forall context s a b c.
ByteString
-> QGenExpr context Postgres s a
-> QGenExpr context Postgres s b
-> QGenExpr context Postgres s c
binOpDefault ByteString
"&<"

(-&>-) :: QGenExpr context Postgres s (PgRange n a)
       -> QGenExpr context Postgres s (PgRange n a)
       -> QGenExpr context Postgres s Bool
-&>- :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
(-&>-) = ByteString
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
forall context s a b c.
ByteString
-> QGenExpr context Postgres s a
-> QGenExpr context Postgres s b
-> QGenExpr context Postgres s c
binOpDefault ByteString
"&>"

(--|--) :: QGenExpr context Postgres s (PgRange n a)
        -> QGenExpr context Postgres s (PgRange n a)
        -> QGenExpr context Postgres s Bool
--|-- :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
(--|--) = ByteString
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
forall context s a b c.
ByteString
-> QGenExpr context Postgres s a
-> QGenExpr context Postgres s b
-> QGenExpr context Postgres s c
binOpDefault ByteString
"-|-"

(-+-) :: QGenExpr context Postgres s (PgRange n a)
      -> QGenExpr context Postgres s (PgRange n a)
      -> QGenExpr context Postgres s (PgRange n a)
-+- :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
(-+-) = ByteString
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
forall context s a b c.
ByteString
-> QGenExpr context Postgres s a
-> QGenExpr context Postgres s b
-> QGenExpr context Postgres s c
binOpDefault ByteString
"+"

(-*-) :: QGenExpr context Postgres s (PgRange n a)
      -> QGenExpr context Postgres s (PgRange n a)
      -> QGenExpr context Postgres s (PgRange n a)
-*- :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
(-*-) = ByteString
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
forall context s a b c.
ByteString
-> QGenExpr context Postgres s a
-> QGenExpr context Postgres s b
-> QGenExpr context Postgres s c
binOpDefault ByteString
"*"

-- | The postgres range operator @-@ .
(-.-) :: QGenExpr context Postgres s (PgRange n a)
      -> QGenExpr context Postgres s (PgRange n a)
      -> QGenExpr context Postgres s (PgRange n a)
-.- :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
(-.-) = ByteString
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
forall context s a b c.
ByteString
-> QGenExpr context Postgres s a
-> QGenExpr context Postgres s b
-> QGenExpr context Postgres s c
binOpDefault ByteString
"-"

defUnaryFn :: ByteString
           -> QGenExpr context Postgres s a
           -> QGenExpr context Postgres s b
defUnaryFn :: ByteString
-> QGenExpr context Postgres s a -> QGenExpr context Postgres s b
defUnaryFn ByteString
fn (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
s) = (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr context Postgres s b
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (PgExpressionSyntax -> PgExpressionSyntax
pgExprFrom (PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
s)
  where
    pgExprFrom :: PgExpressionSyntax -> PgExpressionSyntax
pgExprFrom PgExpressionSyntax
s' = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
fn PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
"(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
s' PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")

rLower_ :: QGenExpr context Postgres s (PgRange n a)
       -> QGenExpr context Postgres s (Maybe a)
rLower_ :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (Maybe a)
rLower_ = ByteString
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (Maybe a)
forall context s a b.
ByteString
-> QGenExpr context Postgres s a -> QGenExpr context Postgres s b
defUnaryFn ByteString
"LOWER"

rUpper_ :: QGenExpr context Postgres s (PgRange n a)
       -> QGenExpr context Postgres s (Maybe a)
rUpper_ :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (Maybe a)
rUpper_ = ByteString
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (Maybe a)
forall context s a b.
ByteString
-> QGenExpr context Postgres s a -> QGenExpr context Postgres s b
defUnaryFn ByteString
"UPPER"

isEmpty_ :: QGenExpr context Postgres s (PgRange n a)
         -> QGenExpr context Postgres s Bool
isEmpty_ :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
isEmpty_ = ByteString
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
forall context s a b.
ByteString
-> QGenExpr context Postgres s a -> QGenExpr context Postgres s b
defUnaryFn ByteString
"ISEMPTY"

lowerInc_ :: QGenExpr context Postgres s (PgRange n a)
          -> QGenExpr context Postgres s Bool
lowerInc_ :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
lowerInc_ = ByteString
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
forall context s a b.
ByteString
-> QGenExpr context Postgres s a -> QGenExpr context Postgres s b
defUnaryFn ByteString
"LOWER_INC"

upperInc_ :: QGenExpr context Postgres s (PgRange n a)
          -> QGenExpr context Postgres s Bool
upperInc_ :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
upperInc_ = ByteString
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
forall context s a b.
ByteString
-> QGenExpr context Postgres s a -> QGenExpr context Postgres s b
defUnaryFn ByteString
"UPPER_INC"

lowerInf_ :: QGenExpr context Postgres s (PgRange n a)
          -> QGenExpr context Postgres s Bool
lowerInf_ :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
lowerInf_ = ByteString
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
forall context s a b.
ByteString
-> QGenExpr context Postgres s a -> QGenExpr context Postgres s b
defUnaryFn ByteString
"LOWER_INF"

upperInf_ :: QGenExpr context Postgres s (PgRange n a)
          -> QGenExpr context Postgres s Bool
upperInf_ :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
upperInf_ = ByteString
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s Bool
forall context s a b.
ByteString
-> QGenExpr context Postgres s a -> QGenExpr context Postgres s b
defUnaryFn ByteString
"UPPER_INF"

rangeMerge_ :: QGenExpr context Postgres s (PgRange n a)
            -> QGenExpr context Postgres s (PgRange n a)
            -> QGenExpr context Postgres s (PgRange n a)
rangeMerge_ :: QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
-> QGenExpr context Postgres s (PgRange n a)
rangeMerge_ (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
r1) (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
r2) = (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr context Postgres s (PgRange n a)
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgExprFrom (PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
r1 (TablePrefix -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
r2)
  where
    pgExprFrom :: PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgExprFrom PgExpressionSyntax
r1' PgExpressionSyntax
r2' =
      PgSyntax -> PgExpressionSyntax
PgExpressionSyntax
      (ByteString -> PgSyntax
emit ByteString
"RANGE_MERGE(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
       PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
r1' PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
       ByteString -> PgSyntax
emit ByteString
", " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
       PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
r2' PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
       ByteString -> PgSyntax
emit ByteString
")")

range_ :: forall n a context s. PgIsRange n
       => PgBoundType -- ^ Lower bound type
       -> PgBoundType -- ^ Upper bound type
       -> QGenExpr context Postgres s (Maybe a) -- ^. Lower bound value
       -> QGenExpr context Postgres s (Maybe a) -- ^. Upper bound value
       -> QGenExpr context Postgres s (PgRange n a)
range_ :: PgBoundType
-> PgBoundType
-> QGenExpr context Postgres s (Maybe a)
-> QGenExpr context Postgres s (Maybe a)
-> QGenExpr context Postgres s (PgRange n a)
range_ PgBoundType
lbt PgBoundType
ubt (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
e1) (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
e2) = (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr context Postgres s (PgRange n a)
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgExprFrom (PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
e1 (TablePrefix -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
e2)
  where
    bounds :: PgSyntax
bounds = ByteString -> PgSyntax
emit ByteString
"'" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit (PgBoundType -> ByteString
lBound PgBoundType
lbt ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> PgBoundType -> ByteString
uBound PgBoundType
ubt) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
"'"
    pgExprFrom :: PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgExprFrom PgExpressionSyntax
e1' PgExpressionSyntax
e2' =
      PgSyntax -> PgExpressionSyntax
PgExpressionSyntax
      (ByteString -> PgSyntax
escapeIdentifier (PgIsRange n => ByteString
forall k (n :: k). PgIsRange n => ByteString
rangeName @n) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
       PgSyntax -> PgSyntax
pgParens (PgSyntax -> [PgSyntax] -> PgSyntax
pgSepBy (ByteString -> PgSyntax
emit ByteString
", ") [PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
e1', PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
e2', PgSyntax
bounds]))

-- ** JSON

-- | The Postgres @JSON@ type, which stores textual values that represent JSON
-- objects. The type parameter indicates the Haskell type which the JSON
-- encodes. This type must be a member of 'FromJSON' and 'ToJSON' in order for
-- deserialization and serialization to work as expected.
--
-- The 'defaultMigratableDbSettings' function automatically assigns the postgres
-- @JSON@ type to fields with this type.
newtype PgJSON a = PgJSON a
  deriving ( Int -> PgJSON a -> ShowS
[PgJSON a] -> ShowS
PgJSON a -> String
(Int -> PgJSON a -> ShowS)
-> (PgJSON a -> String) -> ([PgJSON a] -> ShowS) -> Show (PgJSON a)
forall a. Show a => Int -> PgJSON a -> ShowS
forall a. Show a => [PgJSON a] -> ShowS
forall a. Show a => PgJSON a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PgJSON a] -> ShowS
$cshowList :: forall a. Show a => [PgJSON a] -> ShowS
show :: PgJSON a -> String
$cshow :: forall a. Show a => PgJSON a -> String
showsPrec :: Int -> PgJSON a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> PgJSON a -> ShowS
Show, PgJSON a -> PgJSON a -> Bool
(PgJSON a -> PgJSON a -> Bool)
-> (PgJSON a -> PgJSON a -> Bool) -> Eq (PgJSON a)
forall a. Eq a => PgJSON a -> PgJSON a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PgJSON a -> PgJSON a -> Bool
$c/= :: forall a. Eq a => PgJSON a -> PgJSON a -> Bool
== :: PgJSON a -> PgJSON a -> Bool
$c== :: forall a. Eq a => PgJSON a -> PgJSON a -> Bool
Eq, Eq (PgJSON a)
Eq (PgJSON a)
-> (PgJSON a -> PgJSON a -> Ordering)
-> (PgJSON a -> PgJSON a -> Bool)
-> (PgJSON a -> PgJSON a -> Bool)
-> (PgJSON a -> PgJSON a -> Bool)
-> (PgJSON a -> PgJSON a -> Bool)
-> (PgJSON a -> PgJSON a -> PgJSON a)
-> (PgJSON a -> PgJSON a -> PgJSON a)
-> Ord (PgJSON a)
PgJSON a -> PgJSON a -> Bool
PgJSON a -> PgJSON a -> Ordering
PgJSON a -> PgJSON a -> PgJSON a
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. Ord a => Eq (PgJSON a)
forall a. Ord a => PgJSON a -> PgJSON a -> Bool
forall a. Ord a => PgJSON a -> PgJSON a -> Ordering
forall a. Ord a => PgJSON a -> PgJSON a -> PgJSON a
min :: PgJSON a -> PgJSON a -> PgJSON a
$cmin :: forall a. Ord a => PgJSON a -> PgJSON a -> PgJSON a
max :: PgJSON a -> PgJSON a -> PgJSON a
$cmax :: forall a. Ord a => PgJSON a -> PgJSON a -> PgJSON a
>= :: PgJSON a -> PgJSON a -> Bool
$c>= :: forall a. Ord a => PgJSON a -> PgJSON a -> Bool
> :: PgJSON a -> PgJSON a -> Bool
$c> :: forall a. Ord a => PgJSON a -> PgJSON a -> Bool
<= :: PgJSON a -> PgJSON a -> Bool
$c<= :: forall a. Ord a => PgJSON a -> PgJSON a -> Bool
< :: PgJSON a -> PgJSON a -> Bool
$c< :: forall a. Ord a => PgJSON a -> PgJSON a -> Bool
compare :: PgJSON a -> PgJSON a -> Ordering
$ccompare :: forall a. Ord a => PgJSON a -> PgJSON a -> Ordering
$cp1Ord :: forall a. Ord a => Eq (PgJSON a)
Ord, Int -> PgJSON a -> Int
PgJSON a -> Int
(Int -> PgJSON a -> Int)
-> (PgJSON a -> Int) -> Hashable (PgJSON a)
forall a. Hashable a => Int -> PgJSON a -> Int
forall a. Hashable a => PgJSON a -> Int
forall a. (Int -> a -> Int) -> (a -> Int) -> Hashable a
hash :: PgJSON a -> Int
$chash :: forall a. Hashable a => PgJSON a -> Int
hashWithSalt :: Int -> PgJSON a -> Int
$chashWithSalt :: forall a. Hashable a => Int -> PgJSON a -> Int
Hashable, Semigroup (PgJSON a)
PgJSON a
Semigroup (PgJSON a)
-> PgJSON a
-> (PgJSON a -> PgJSON a -> PgJSON a)
-> ([PgJSON a] -> PgJSON a)
-> Monoid (PgJSON a)
[PgJSON a] -> PgJSON a
PgJSON a -> PgJSON a -> PgJSON a
forall a.
Semigroup a -> a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
forall a. Monoid a => Semigroup (PgJSON a)
forall a. Monoid a => PgJSON a
forall a. Monoid a => [PgJSON a] -> PgJSON a
forall a. Monoid a => PgJSON a -> PgJSON a -> PgJSON a
mconcat :: [PgJSON a] -> PgJSON a
$cmconcat :: forall a. Monoid a => [PgJSON a] -> PgJSON a
mappend :: PgJSON a -> PgJSON a -> PgJSON a
$cmappend :: forall a. Monoid a => PgJSON a -> PgJSON a -> PgJSON a
mempty :: PgJSON a
$cmempty :: forall a. Monoid a => PgJSON a
$cp1Monoid :: forall a. Monoid a => Semigroup (PgJSON a)
Monoid, b -> PgJSON a -> PgJSON a
NonEmpty (PgJSON a) -> PgJSON a
PgJSON a -> PgJSON a -> PgJSON a
(PgJSON a -> PgJSON a -> PgJSON a)
-> (NonEmpty (PgJSON a) -> PgJSON a)
-> (forall b. Integral b => b -> PgJSON a -> PgJSON a)
-> Semigroup (PgJSON a)
forall b. Integral b => b -> PgJSON a -> PgJSON a
forall a. Semigroup a => NonEmpty (PgJSON a) -> PgJSON a
forall a. Semigroup a => PgJSON a -> PgJSON a -> PgJSON a
forall a b. (Semigroup a, Integral b) => b -> PgJSON a -> PgJSON a
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
stimes :: b -> PgJSON a -> PgJSON a
$cstimes :: forall a b. (Semigroup a, Integral b) => b -> PgJSON a -> PgJSON a
sconcat :: NonEmpty (PgJSON a) -> PgJSON a
$csconcat :: forall a. Semigroup a => NonEmpty (PgJSON a) -> PgJSON a
<> :: PgJSON a -> PgJSON a -> PgJSON a
$c<> :: forall a. Semigroup a => PgJSON a -> PgJSON a -> PgJSON a
Semigroup )

instance HasSqlEqualityCheck Postgres (PgJSON a)
instance HasSqlQuantifiedEqualityCheck Postgres (PgJSON a)

instance (Typeable x, FromJSON x) => Pg.FromField (PgJSON x) where
  fromField :: FieldParser (PgJSON x)
fromField Field
field Maybe ByteString
d =
    if Field -> Oid
Pg.typeOid Field
field Oid -> Oid -> Bool
forall a. Eq a => a -> a -> Bool
/= TypeInfo -> Oid
Pg.typoid TypeInfo
Pg.json
    then (String -> Maybe Oid -> String -> String -> String -> ResultError)
-> Field -> String -> Conversion (PgJSON x)
forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
Pg.returnError String -> Maybe Oid -> String -> String -> String -> ResultError
Pg.Incompatible Field
field String
""
    else case ByteString -> Either String x
forall a. FromJSON a => ByteString -> Either String a
eitherDecodeStrict (ByteString -> Either String x)
-> Maybe ByteString -> Maybe (Either String x)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe ByteString
d of
           Maybe (Either String x)
Nothing -> (String -> Maybe Oid -> String -> String -> String -> ResultError)
-> Field -> String -> Conversion (PgJSON x)
forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
Pg.returnError String -> Maybe Oid -> String -> String -> String -> ResultError
Pg.UnexpectedNull Field
field String
""
           Just (Left String
e) -> (String -> Maybe Oid -> String -> String -> String -> ResultError)
-> Field -> String -> Conversion (PgJSON x)
forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
Pg.returnError String -> Maybe Oid -> String -> String -> String -> ResultError
Pg.ConversionFailed Field
field String
e
           Just (Right x
d') -> PgJSON x -> Conversion (PgJSON x)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (x -> PgJSON x
forall a. a -> PgJSON a
PgJSON x
d')

instance (Typeable a, FromJSON a) => FromBackendRow Postgres (PgJSON a)
instance ToJSON a => HasSqlValueSyntax PgValueSyntax (PgJSON a) where
  sqlValueSyntax :: PgJSON a -> PgValueSyntax
sqlValueSyntax (PgJSON a
a) =
    PgSyntax -> PgValueSyntax
PgValueSyntax (PgSyntax -> PgValueSyntax) -> PgSyntax -> PgValueSyntax
forall a b. (a -> b) -> a -> b
$
    ByteString -> PgSyntax
emit ByteString
"'" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
escapeString (ByteString -> ByteString
BL.toStrict (a -> ByteString
forall a. ToJSON a => a -> ByteString
encode a
a)) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
"'::json"

-- | The Postgres @JSONB@ type, which stores JSON-encoded data in a
-- postgres-specific binary format. Like 'PgJSON', the type parameter indicates
-- the Haskell type which the JSON encodes.
--
-- Fields with this type are automatically given the Postgres @JSONB@ type
newtype PgJSONB a = PgJSONB a
  deriving ( Int -> PgJSONB a -> ShowS
[PgJSONB a] -> ShowS
PgJSONB a -> String
(Int -> PgJSONB a -> ShowS)
-> (PgJSONB a -> String)
-> ([PgJSONB a] -> ShowS)
-> Show (PgJSONB a)
forall a. Show a => Int -> PgJSONB a -> ShowS
forall a. Show a => [PgJSONB a] -> ShowS
forall a. Show a => PgJSONB a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PgJSONB a] -> ShowS
$cshowList :: forall a. Show a => [PgJSONB a] -> ShowS
show :: PgJSONB a -> String
$cshow :: forall a. Show a => PgJSONB a -> String
showsPrec :: Int -> PgJSONB a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> PgJSONB a -> ShowS
Show, PgJSONB a -> PgJSONB a -> Bool
(PgJSONB a -> PgJSONB a -> Bool)
-> (PgJSONB a -> PgJSONB a -> Bool) -> Eq (PgJSONB a)
forall a. Eq a => PgJSONB a -> PgJSONB a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PgJSONB a -> PgJSONB a -> Bool
$c/= :: forall a. Eq a => PgJSONB a -> PgJSONB a -> Bool
== :: PgJSONB a -> PgJSONB a -> Bool
$c== :: forall a. Eq a => PgJSONB a -> PgJSONB a -> Bool
Eq, Eq (PgJSONB a)
Eq (PgJSONB a)
-> (PgJSONB a -> PgJSONB a -> Ordering)
-> (PgJSONB a -> PgJSONB a -> Bool)
-> (PgJSONB a -> PgJSONB a -> Bool)
-> (PgJSONB a -> PgJSONB a -> Bool)
-> (PgJSONB a -> PgJSONB a -> Bool)
-> (PgJSONB a -> PgJSONB a -> PgJSONB a)
-> (PgJSONB a -> PgJSONB a -> PgJSONB a)
-> Ord (PgJSONB a)
PgJSONB a -> PgJSONB a -> Bool
PgJSONB a -> PgJSONB a -> Ordering
PgJSONB a -> PgJSONB a -> PgJSONB a
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. Ord a => Eq (PgJSONB a)
forall a. Ord a => PgJSONB a -> PgJSONB a -> Bool
forall a. Ord a => PgJSONB a -> PgJSONB a -> Ordering
forall a. Ord a => PgJSONB a -> PgJSONB a -> PgJSONB a
min :: PgJSONB a -> PgJSONB a -> PgJSONB a
$cmin :: forall a. Ord a => PgJSONB a -> PgJSONB a -> PgJSONB a
max :: PgJSONB a -> PgJSONB a -> PgJSONB a
$cmax :: forall a. Ord a => PgJSONB a -> PgJSONB a -> PgJSONB a
>= :: PgJSONB a -> PgJSONB a -> Bool
$c>= :: forall a. Ord a => PgJSONB a -> PgJSONB a -> Bool
> :: PgJSONB a -> PgJSONB a -> Bool
$c> :: forall a. Ord a => PgJSONB a -> PgJSONB a -> Bool
<= :: PgJSONB a -> PgJSONB a -> Bool
$c<= :: forall a. Ord a => PgJSONB a -> PgJSONB a -> Bool
< :: PgJSONB a -> PgJSONB a -> Bool
$c< :: forall a. Ord a => PgJSONB a -> PgJSONB a -> Bool
compare :: PgJSONB a -> PgJSONB a -> Ordering
$ccompare :: forall a. Ord a => PgJSONB a -> PgJSONB a -> Ordering
$cp1Ord :: forall a. Ord a => Eq (PgJSONB a)
Ord, Int -> PgJSONB a -> Int
PgJSONB a -> Int
(Int -> PgJSONB a -> Int)
-> (PgJSONB a -> Int) -> Hashable (PgJSONB a)
forall a. Hashable a => Int -> PgJSONB a -> Int
forall a. Hashable a => PgJSONB a -> Int
forall a. (Int -> a -> Int) -> (a -> Int) -> Hashable a
hash :: PgJSONB a -> Int
$chash :: forall a. Hashable a => PgJSONB a -> Int
hashWithSalt :: Int -> PgJSONB a -> Int
$chashWithSalt :: forall a. Hashable a => Int -> PgJSONB a -> Int
Hashable, Semigroup (PgJSONB a)
PgJSONB a
Semigroup (PgJSONB a)
-> PgJSONB a
-> (PgJSONB a -> PgJSONB a -> PgJSONB a)
-> ([PgJSONB a] -> PgJSONB a)
-> Monoid (PgJSONB a)
[PgJSONB a] -> PgJSONB a
PgJSONB a -> PgJSONB a -> PgJSONB a
forall a.
Semigroup a -> a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
forall a. Monoid a => Semigroup (PgJSONB a)
forall a. Monoid a => PgJSONB a
forall a. Monoid a => [PgJSONB a] -> PgJSONB a
forall a. Monoid a => PgJSONB a -> PgJSONB a -> PgJSONB a
mconcat :: [PgJSONB a] -> PgJSONB a
$cmconcat :: forall a. Monoid a => [PgJSONB a] -> PgJSONB a
mappend :: PgJSONB a -> PgJSONB a -> PgJSONB a
$cmappend :: forall a. Monoid a => PgJSONB a -> PgJSONB a -> PgJSONB a
mempty :: PgJSONB a
$cmempty :: forall a. Monoid a => PgJSONB a
$cp1Monoid :: forall a. Monoid a => Semigroup (PgJSONB a)
Monoid, b -> PgJSONB a -> PgJSONB a
NonEmpty (PgJSONB a) -> PgJSONB a
PgJSONB a -> PgJSONB a -> PgJSONB a
(PgJSONB a -> PgJSONB a -> PgJSONB a)
-> (NonEmpty (PgJSONB a) -> PgJSONB a)
-> (forall b. Integral b => b -> PgJSONB a -> PgJSONB a)
-> Semigroup (PgJSONB a)
forall b. Integral b => b -> PgJSONB a -> PgJSONB a
forall a. Semigroup a => NonEmpty (PgJSONB a) -> PgJSONB a
forall a. Semigroup a => PgJSONB a -> PgJSONB a -> PgJSONB a
forall a b.
(Semigroup a, Integral b) =>
b -> PgJSONB a -> PgJSONB a
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
stimes :: b -> PgJSONB a -> PgJSONB a
$cstimes :: forall a b.
(Semigroup a, Integral b) =>
b -> PgJSONB a -> PgJSONB a
sconcat :: NonEmpty (PgJSONB a) -> PgJSONB a
$csconcat :: forall a. Semigroup a => NonEmpty (PgJSONB a) -> PgJSONB a
<> :: PgJSONB a -> PgJSONB a -> PgJSONB a
$c<> :: forall a. Semigroup a => PgJSONB a -> PgJSONB a -> PgJSONB a
Semigroup )

instance HasSqlEqualityCheck Postgres (PgJSONB a)
instance HasSqlQuantifiedEqualityCheck Postgres (PgJSONB a)

instance (Typeable x, FromJSON x) => Pg.FromField (PgJSONB x) where
  fromField :: FieldParser (PgJSONB x)
fromField Field
field Maybe ByteString
d =
    if Field -> Oid
Pg.typeOid Field
field Oid -> Oid -> Bool
forall a. Eq a => a -> a -> Bool
/= TypeInfo -> Oid
Pg.typoid TypeInfo
Pg.jsonb
    then (String -> Maybe Oid -> String -> String -> String -> ResultError)
-> Field -> String -> Conversion (PgJSONB x)
forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
Pg.returnError String -> Maybe Oid -> String -> String -> String -> ResultError
Pg.Incompatible Field
field String
""
    else case ByteString -> Either String x
forall a. FromJSON a => ByteString -> Either String a
eitherDecodeStrict (ByteString -> Either String x)
-> Maybe ByteString -> Maybe (Either String x)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe ByteString
d of
           Maybe (Either String x)
Nothing -> (String -> Maybe Oid -> String -> String -> String -> ResultError)
-> Field -> String -> Conversion (PgJSONB x)
forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
Pg.returnError String -> Maybe Oid -> String -> String -> String -> ResultError
Pg.UnexpectedNull Field
field String
""
           Just (Left String
e) -> (String -> Maybe Oid -> String -> String -> String -> ResultError)
-> Field -> String -> Conversion (PgJSONB x)
forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
Pg.returnError String -> Maybe Oid -> String -> String -> String -> ResultError
Pg.ConversionFailed Field
field String
e
           Just (Right x
d') -> PgJSONB x -> Conversion (PgJSONB x)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (x -> PgJSONB x
forall a. a -> PgJSONB a
PgJSONB x
d')

instance (Typeable a, FromJSON a) => FromBackendRow Postgres (PgJSONB a)
instance ToJSON a => HasSqlValueSyntax PgValueSyntax (PgJSONB a) where
  sqlValueSyntax :: PgJSONB a -> PgValueSyntax
sqlValueSyntax (PgJSONB a
a) =
    PgSyntax -> PgValueSyntax
PgValueSyntax (PgSyntax -> PgValueSyntax) -> PgSyntax -> PgValueSyntax
forall a b. (a -> b) -> a -> b
$
    ByteString -> PgSyntax
emit ByteString
"'" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
escapeString (ByteString -> ByteString
BL.toStrict (a -> ByteString
forall a. ToJSON a => a -> ByteString
encode a
a)) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
"'::jsonb"

-- | Key-value pair, used as output of 'pgJsonEachText' and 'pgJsonEach'
data PgJSONEach valType f
  = PgJSONEach
  { PgJSONEach valType f -> C f TablePrefix
pgJsonEachKey :: C f T.Text
  , PgJSONEach valType f -> C f valType
pgJsonEachValue :: C f valType
  } deriving (forall x. PgJSONEach valType f -> Rep (PgJSONEach valType f) x)
-> (forall x. Rep (PgJSONEach valType f) x -> PgJSONEach valType f)
-> Generic (PgJSONEach valType f)
forall x. Rep (PgJSONEach valType f) x -> PgJSONEach valType f
forall x. PgJSONEach valType f -> Rep (PgJSONEach valType f) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall valType (f :: * -> *) x.
Rep (PgJSONEach valType f) x -> PgJSONEach valType f
forall valType (f :: * -> *) x.
PgJSONEach valType f -> Rep (PgJSONEach valType f) x
$cto :: forall valType (f :: * -> *) x.
Rep (PgJSONEach valType f) x -> PgJSONEach valType f
$cfrom :: forall valType (f :: * -> *) x.
PgJSONEach valType f -> Rep (PgJSONEach valType f) x
Generic
instance Beamable (PgJSONEach valType)

-- | Output row of 'pgJsonKeys'
data PgJSONKey f = PgJSONKey { PgJSONKey f -> C f TablePrefix
pgJsonKey :: C f T.Text }
  deriving (forall x. PgJSONKey f -> Rep (PgJSONKey f) x)
-> (forall x. Rep (PgJSONKey f) x -> PgJSONKey f)
-> Generic (PgJSONKey f)
forall x. Rep (PgJSONKey f) x -> PgJSONKey f
forall x. PgJSONKey f -> Rep (PgJSONKey f) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (f :: * -> *) x. Rep (PgJSONKey f) x -> PgJSONKey f
forall (f :: * -> *) x. PgJSONKey f -> Rep (PgJSONKey f) x
$cto :: forall (f :: * -> *) x. Rep (PgJSONKey f) x -> PgJSONKey f
$cfrom :: forall (f :: * -> *) x. PgJSONKey f -> Rep (PgJSONKey f) x
Generic
instance Beamable PgJSONKey

-- | Output row of 'pgJsonArrayElements' and 'pgJsonArrayElementsText'
data PgJSONElement a f = PgJSONElement { PgJSONElement a f -> C f a
pgJsonElement :: C f a }
  deriving (forall x. PgJSONElement a f -> Rep (PgJSONElement a f) x)
-> (forall x. Rep (PgJSONElement a f) x -> PgJSONElement a f)
-> Generic (PgJSONElement a f)
forall x. Rep (PgJSONElement a f) x -> PgJSONElement a f
forall x. PgJSONElement a f -> Rep (PgJSONElement a f) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a (f :: * -> *) x.
Rep (PgJSONElement a f) x -> PgJSONElement a f
forall a (f :: * -> *) x.
PgJSONElement a f -> Rep (PgJSONElement a f) x
$cto :: forall a (f :: * -> *) x.
Rep (PgJSONElement a f) x -> PgJSONElement a f
$cfrom :: forall a (f :: * -> *) x.
PgJSONElement a f -> Rep (PgJSONElement a f) x
Generic
instance Beamable (PgJSONElement a)

-- | Postgres provides separate @json_@ and @jsonb_@ functions. However, we know
-- what we're dealing with based on the type of data, so we can be less obtuse.
--
-- For more information on how these functions behave, see the Postgres manual
-- section on
-- <https://www.postgresql.org/docs/current/static/functions-json.html JSON>.
--
class IsPgJSON (json :: * -> *) where
  -- | The @json_each@ or @jsonb_each@ function. Values returned as @json@ or
  -- @jsonb@ respectively. Use 'pgUnnest' to join against the result
  pgJsonEach     :: QGenExpr ctxt Postgres s (json a)
                 -> QGenExpr ctxt Postgres s (PgSetOf (PgJSONEach (json Value)))

  -- | Like 'pgJsonEach', but returning text values instead
  pgJsonEachText :: QGenExpr ctxt Postgres s (json a)
                 -> QGenExpr ctxt Postgres s (PgSetOf (PgJSONEach T.Text))

  -- | The @json_object_keys@ and @jsonb_object_keys@ function. Use 'pgUnnest'
  -- to join against the result.
  pgJsonKeys     :: QGenExpr ctxt Postgres s (json a)
                 -> QGenExpr ctxt Postgres s (PgSetOf PgJSONKey)

  -- | The @json_array_elements@ and @jsonb_array_elements@ function. Use
  -- 'pgUnnest' to join against the result
  pgJsonArrayElements :: QGenExpr ctxt Postgres s (json a)
                      -> QGenExpr ctxt Postgres s (PgSetOf (PgJSONElement (json Value)))

  -- | Like 'pgJsonArrayElements', but returning the values as 'T.Text'
  pgJsonArrayElementsText :: QGenExpr ctxt Postgres s (json a)
                          -> QGenExpr ctxt Postgres s (PgSetOf (PgJSONElement T.Text))
--  pgJsonToRecord
--  pgJsonToRecordSet

  -- | The @json_typeof@ or @jsonb_typeof@ function
  pgJsonTypeOf :: QGenExpr ctxt Postgres s (json a) -> QGenExpr ctxt Postgres s T.Text

  -- | The @json_strip_nulls@ or @jsonb_strip_nulls@ function.
  pgJsonStripNulls :: QGenExpr ctxt Postgres s (json a)
                   -> QGenExpr ctxt Postgres s (json b)

  -- | The @json_agg@ or @jsonb_agg@ aggregate.
  pgJsonAgg :: QExpr Postgres s a -> QAgg Postgres s (json a)

  -- | The @json_object_agg@ or @jsonb_object_agg@. The first argument gives the
  -- key source and the second the corresponding values.
  pgJsonObjectAgg :: QExpr Postgres s key -> QExpr Postgres s value
                  -> QAgg Postgres s (json a)

instance IsPgJSON PgJSON where
  pgJsonEach :: QGenExpr ctxt Postgres s (PgJSON a)
-> QGenExpr ctxt Postgres s (PgSetOf (PgJSONEach (PgJSON Value)))
pgJsonEach (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a) =
    (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s (PgSetOf (PgJSONEach (PgJSON Value)))
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr ((TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
 -> QGenExpr ctxt Postgres s (PgSetOf (PgJSONEach (PgJSON Value))))
-> (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s (PgSetOf (PgJSONEach (PgJSON Value)))
forall a b. (a -> b) -> a -> b
$ (PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgExpressionSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax -> PgSyntax
forall a. Monoid a => a -> a -> a
mappend (ByteString -> PgSyntax
emit ByteString
"json_each") (PgSyntax -> PgSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax
pgParens (PgSyntax -> PgSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgExpressionSyntax -> PgSyntax
fromPgExpression) TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a

  pgJsonEachText :: QGenExpr ctxt Postgres s (PgJSON a)
-> QGenExpr ctxt Postgres s (PgSetOf (PgJSONEach TablePrefix))
pgJsonEachText (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a) =
    (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s (PgSetOf (PgJSONEach TablePrefix))
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr ((TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
 -> QGenExpr ctxt Postgres s (PgSetOf (PgJSONEach TablePrefix)))
-> (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s (PgSetOf (PgJSONEach TablePrefix))
forall a b. (a -> b) -> a -> b
$ (PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgExpressionSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax -> PgSyntax
forall a. Monoid a => a -> a -> a
mappend (ByteString -> PgSyntax
emit ByteString
"json_each_text") (PgSyntax -> PgSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax
pgParens (PgSyntax -> PgSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgExpressionSyntax -> PgSyntax
fromPgExpression) TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a

  pgJsonKeys :: QGenExpr ctxt Postgres s (PgJSON a)
-> QGenExpr ctxt Postgres s (PgSetOf PgJSONKey)
pgJsonKeys (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a) =
    (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s (PgSetOf PgJSONKey)
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr ((TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
 -> QGenExpr ctxt Postgres s (PgSetOf PgJSONKey))
-> (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s (PgSetOf PgJSONKey)
forall a b. (a -> b) -> a -> b
$ (PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgExpressionSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax -> PgSyntax
forall a. Monoid a => a -> a -> a
mappend (ByteString -> PgSyntax
emit ByteString
"json_object_keys") (PgSyntax -> PgSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax
pgParens (PgSyntax -> PgSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgExpressionSyntax -> PgSyntax
fromPgExpression) TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a

  pgJsonArrayElements :: QGenExpr ctxt Postgres s (PgJSON a)
-> QGenExpr
     ctxt Postgres s (PgSetOf (PgJSONElement (PgJSON Value)))
pgJsonArrayElements (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a) =
    (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr
     ctxt Postgres s (PgSetOf (PgJSONElement (PgJSON Value)))
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr ((TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
 -> QGenExpr
      ctxt Postgres s (PgSetOf (PgJSONElement (PgJSON Value))))
-> (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr
     ctxt Postgres s (PgSetOf (PgJSONElement (PgJSON Value)))
forall a b. (a -> b) -> a -> b
$ (PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgExpressionSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax -> PgSyntax
forall a. Monoid a => a -> a -> a
mappend (ByteString -> PgSyntax
emit ByteString
"json_array_elements") (PgSyntax -> PgSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax
pgParens (PgSyntax -> PgSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgExpressionSyntax -> PgSyntax
fromPgExpression) TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a

  pgJsonArrayElementsText :: QGenExpr ctxt Postgres s (PgJSON a)
-> QGenExpr ctxt Postgres s (PgSetOf (PgJSONElement TablePrefix))
pgJsonArrayElementsText (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a) =
    (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s (PgSetOf (PgJSONElement TablePrefix))
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr ((TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
 -> QGenExpr ctxt Postgres s (PgSetOf (PgJSONElement TablePrefix)))
-> (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s (PgSetOf (PgJSONElement TablePrefix))
forall a b. (a -> b) -> a -> b
$ (PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgExpressionSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax -> PgSyntax
forall a. Monoid a => a -> a -> a
mappend (ByteString -> PgSyntax
emit ByteString
"json_array_elements_text") (PgSyntax -> PgSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax
pgParens (PgSyntax -> PgSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgExpressionSyntax -> PgSyntax
fromPgExpression) TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a

  pgJsonTypeOf :: QGenExpr ctxt Postgres s (PgJSON a)
-> QGenExpr ctxt Postgres s TablePrefix
pgJsonTypeOf (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a) =
    (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s TablePrefix
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr ((TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
 -> QGenExpr ctxt Postgres s TablePrefix)
-> (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s TablePrefix
forall a b. (a -> b) -> a -> b
$ (PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgExpressionSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax -> PgSyntax
forall a. Monoid a => a -> a -> a
mappend (ByteString -> PgSyntax
emit ByteString
"json_typeof") (PgSyntax -> PgSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax
pgParens (PgSyntax -> PgSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgExpressionSyntax -> PgSyntax
fromPgExpression) TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a

  pgJsonStripNulls :: QGenExpr ctxt Postgres s (PgJSON a)
-> QGenExpr ctxt Postgres s (PgJSON b)
pgJsonStripNulls (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a) =
    (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s (PgJSON b)
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr ((TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
 -> QGenExpr ctxt Postgres s (PgJSON b))
-> (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s (PgJSON b)
forall a b. (a -> b) -> a -> b
$ (PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgExpressionSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax -> PgSyntax
forall a. Monoid a => a -> a -> a
mappend (ByteString -> PgSyntax
emit ByteString
"json_strip_nulls") (PgSyntax -> PgSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax
pgParens (PgSyntax -> PgSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgExpressionSyntax -> PgSyntax
fromPgExpression) TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a

  pgJsonAgg :: QExpr Postgres s a -> QAgg Postgres s (PgJSON a)
pgJsonAgg (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a) =
    (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QAgg Postgres s (PgJSON a)
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr ((TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
 -> QAgg Postgres s (PgJSON a))
-> (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QAgg Postgres s (PgJSON a)
forall a b. (a -> b) -> a -> b
$ (PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgExpressionSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax -> PgSyntax
forall a. Monoid a => a -> a -> a
mappend (ByteString -> PgSyntax
emit ByteString
"json_agg") (PgSyntax -> PgSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax
pgParens (PgSyntax -> PgSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgExpressionSyntax -> PgSyntax
fromPgExpression) TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a

  pgJsonObjectAgg :: QExpr Postgres s key
-> QExpr Postgres s value -> QAgg Postgres s (PgJSON a)
pgJsonObjectAgg (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
keys) (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
values) =
    (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QAgg Postgres s (PgJSON a)
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr ((TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
 -> QAgg Postgres s (PgJSON a))
-> (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QAgg Postgres s (PgJSON a)
forall a b. (a -> b) -> a -> b
$ ([PgSyntax] -> PgExpressionSyntax)
-> (TablePrefix -> [PgSyntax]) -> TablePrefix -> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax)
-> ([PgSyntax] -> PgSyntax) -> [PgSyntax] -> PgExpressionSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax -> PgSyntax
forall a. Monoid a => a -> a -> a
mappend (ByteString -> PgSyntax
emit ByteString
"json_object_agg") (PgSyntax -> PgSyntax)
-> ([PgSyntax] -> PgSyntax) -> [PgSyntax] -> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax
pgParens (PgSyntax -> PgSyntax)
-> ([PgSyntax] -> PgSyntax) -> [PgSyntax] -> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [PgSyntax] -> PgSyntax
forall a. Monoid a => [a] -> a
mconcat) ((TablePrefix -> [PgSyntax]) -> TablePrefix -> PgExpressionSyntax)
-> (TablePrefix -> [PgSyntax]) -> TablePrefix -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$
    [TablePrefix -> PgSyntax] -> TablePrefix -> [PgSyntax]
forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
sequenceA ([TablePrefix -> PgSyntax] -> TablePrefix -> [PgSyntax])
-> [TablePrefix -> PgSyntax] -> TablePrefix -> [PgSyntax]
forall a b. (a -> b) -> a -> b
$ [ PgExpressionSyntax -> PgSyntax
fromPgExpression (PgExpressionSyntax -> PgSyntax)
-> (TablePrefix -> PgExpressionSyntax) -> TablePrefix -> PgSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
keys, PgSyntax -> TablePrefix -> PgSyntax
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> PgSyntax
emit ByteString
", ")
                , PgExpressionSyntax -> PgSyntax
fromPgExpression (PgExpressionSyntax -> PgSyntax)
-> (TablePrefix -> PgExpressionSyntax) -> TablePrefix -> PgSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
values ]

instance IsPgJSON PgJSONB where
  pgJsonEach :: QGenExpr ctxt Postgres s (PgJSONB a)
-> QGenExpr ctxt Postgres s (PgSetOf (PgJSONEach (PgJSONB Value)))
pgJsonEach (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a) =
    (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s (PgSetOf (PgJSONEach (PgJSONB Value)))
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr ((TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
 -> QGenExpr ctxt Postgres s (PgSetOf (PgJSONEach (PgJSONB Value))))
-> (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s (PgSetOf (PgJSONEach (PgJSONB Value)))
forall a b. (a -> b) -> a -> b
$ (PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgExpressionSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax -> PgSyntax
forall a. Monoid a => a -> a -> a
mappend (ByteString -> PgSyntax
emit ByteString
"jsonb_each") (PgSyntax -> PgSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax
pgParens (PgSyntax -> PgSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgExpressionSyntax -> PgSyntax
fromPgExpression) TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a

  pgJsonEachText :: QGenExpr ctxt Postgres s (PgJSONB a)
-> QGenExpr ctxt Postgres s (PgSetOf (PgJSONEach TablePrefix))
pgJsonEachText (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a) =
    (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s (PgSetOf (PgJSONEach TablePrefix))
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr ((TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
 -> QGenExpr ctxt Postgres s (PgSetOf (PgJSONEach TablePrefix)))
-> (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s (PgSetOf (PgJSONEach TablePrefix))
forall a b. (a -> b) -> a -> b
$ (PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgExpressionSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax -> PgSyntax
forall a. Monoid a => a -> a -> a
mappend (ByteString -> PgSyntax
emit ByteString
"jsonb_each_text") (PgSyntax -> PgSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax
pgParens (PgSyntax -> PgSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgExpressionSyntax -> PgSyntax
fromPgExpression) TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a

  pgJsonKeys :: QGenExpr ctxt Postgres s (PgJSONB a)
-> QGenExpr ctxt Postgres s (PgSetOf PgJSONKey)
pgJsonKeys (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a) =
    (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s (PgSetOf PgJSONKey)
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr ((TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
 -> QGenExpr ctxt Postgres s (PgSetOf PgJSONKey))
-> (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s (PgSetOf PgJSONKey)
forall a b. (a -> b) -> a -> b
$ (PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgExpressionSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax -> PgSyntax
forall a. Monoid a => a -> a -> a
mappend (ByteString -> PgSyntax
emit ByteString
"jsonb_object_keys") (PgSyntax -> PgSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax
pgParens (PgSyntax -> PgSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgExpressionSyntax -> PgSyntax
fromPgExpression) TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a

  pgJsonArrayElements :: QGenExpr ctxt Postgres s (PgJSONB a)
-> QGenExpr
     ctxt Postgres s (PgSetOf (PgJSONElement (PgJSONB Value)))
pgJsonArrayElements (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a) =
    (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr
     ctxt Postgres s (PgSetOf (PgJSONElement (PgJSONB Value)))
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr ((TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
 -> QGenExpr
      ctxt Postgres s (PgSetOf (PgJSONElement (PgJSONB Value))))
-> (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr
     ctxt Postgres s (PgSetOf (PgJSONElement (PgJSONB Value)))
forall a b. (a -> b) -> a -> b
$ (PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgExpressionSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax -> PgSyntax
forall a. Monoid a => a -> a -> a
mappend (ByteString -> PgSyntax
emit ByteString
"jsonb_array_elements") (PgSyntax -> PgSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax
pgParens (PgSyntax -> PgSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgExpressionSyntax -> PgSyntax
fromPgExpression) TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a

  pgJsonArrayElementsText :: QGenExpr ctxt Postgres s (PgJSONB a)
-> QGenExpr ctxt Postgres s (PgSetOf (PgJSONElement TablePrefix))
pgJsonArrayElementsText (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a) =
    (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s (PgSetOf (PgJSONElement TablePrefix))
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr ((TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
 -> QGenExpr ctxt Postgres s (PgSetOf (PgJSONElement TablePrefix)))
-> (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s (PgSetOf (PgJSONElement TablePrefix))
forall a b. (a -> b) -> a -> b
$ (PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgExpressionSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax -> PgSyntax
forall a. Monoid a => a -> a -> a
mappend (ByteString -> PgSyntax
emit ByteString
"jsonb_array_elements_text") (PgSyntax -> PgSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax
pgParens (PgSyntax -> PgSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgExpressionSyntax -> PgSyntax
fromPgExpression) TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a

  pgJsonTypeOf :: QGenExpr ctxt Postgres s (PgJSONB a)
-> QGenExpr ctxt Postgres s TablePrefix
pgJsonTypeOf (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a) =
    (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s TablePrefix
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr ((TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
 -> QGenExpr ctxt Postgres s TablePrefix)
-> (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s TablePrefix
forall a b. (a -> b) -> a -> b
$ (PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgExpressionSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax -> PgSyntax
forall a. Monoid a => a -> a -> a
mappend (ByteString -> PgSyntax
emit ByteString
"jsonb_typeof") (PgSyntax -> PgSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax
pgParens (PgSyntax -> PgSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgExpressionSyntax -> PgSyntax
fromPgExpression) TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a

  pgJsonStripNulls :: QGenExpr ctxt Postgres s (PgJSONB a)
-> QGenExpr ctxt Postgres s (PgJSONB b)
pgJsonStripNulls (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a) =
    (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s (PgJSONB b)
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr ((TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
 -> QGenExpr ctxt Postgres s (PgJSONB b))
-> (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s (PgJSONB b)
forall a b. (a -> b) -> a -> b
$ (PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgExpressionSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax -> PgSyntax
forall a. Monoid a => a -> a -> a
mappend (ByteString -> PgSyntax
emit ByteString
"jsonb_strip_nulls") (PgSyntax -> PgSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax
pgParens (PgSyntax -> PgSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgExpressionSyntax -> PgSyntax
fromPgExpression) TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a

  pgJsonAgg :: QExpr Postgres s a -> QAgg Postgres s (PgJSONB a)
pgJsonAgg (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a) =
    (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QAgg Postgres s (PgJSONB a)
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr ((TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
 -> QAgg Postgres s (PgJSONB a))
-> (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QAgg Postgres s (PgJSONB a)
forall a b. (a -> b) -> a -> b
$ (PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgExpressionSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax -> PgSyntax
forall a. Monoid a => a -> a -> a
mappend (ByteString -> PgSyntax
emit ByteString
"jsonb_agg") (PgSyntax -> PgSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax
pgParens (PgSyntax -> PgSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgExpressionSyntax -> PgSyntax
fromPgExpression) TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a

  pgJsonObjectAgg :: QExpr Postgres s key
-> QExpr Postgres s value -> QAgg Postgres s (PgJSONB a)
pgJsonObjectAgg (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
keys) (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
values) =
    (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QAgg Postgres s (PgJSONB a)
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr ((TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
 -> QAgg Postgres s (PgJSONB a))
-> (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QAgg Postgres s (PgJSONB a)
forall a b. (a -> b) -> a -> b
$ ([PgSyntax] -> PgExpressionSyntax)
-> (TablePrefix -> [PgSyntax]) -> TablePrefix -> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax)
-> ([PgSyntax] -> PgSyntax) -> [PgSyntax] -> PgExpressionSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax -> PgSyntax
forall a. Monoid a => a -> a -> a
mappend (ByteString -> PgSyntax
emit ByteString
"jsonb_object_agg") (PgSyntax -> PgSyntax)
-> ([PgSyntax] -> PgSyntax) -> [PgSyntax] -> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax
pgParens (PgSyntax -> PgSyntax)
-> ([PgSyntax] -> PgSyntax) -> [PgSyntax] -> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [PgSyntax] -> PgSyntax
forall a. Monoid a => [a] -> a
mconcat) ((TablePrefix -> [PgSyntax]) -> TablePrefix -> PgExpressionSyntax)
-> (TablePrefix -> [PgSyntax]) -> TablePrefix -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$
    [TablePrefix -> PgSyntax] -> TablePrefix -> [PgSyntax]
forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
sequenceA ([TablePrefix -> PgSyntax] -> TablePrefix -> [PgSyntax])
-> [TablePrefix -> PgSyntax] -> TablePrefix -> [PgSyntax]
forall a b. (a -> b) -> a -> b
$ [ PgExpressionSyntax -> PgSyntax
fromPgExpression (PgExpressionSyntax -> PgSyntax)
-> (TablePrefix -> PgExpressionSyntax) -> TablePrefix -> PgSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
keys, PgSyntax -> TablePrefix -> PgSyntax
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> PgSyntax
emit ByteString
", ")
                , PgExpressionSyntax -> PgSyntax
fromPgExpression (PgExpressionSyntax -> PgSyntax)
-> (TablePrefix -> PgExpressionSyntax) -> TablePrefix -> PgSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
values ]

-- | Postgres @&#x40;>@ and @<&#x40;@ operators for JSON. Return true if the
-- json object pointed to by the arrow is completely contained in the other. See
-- the Postgres documentation for more in formation on what this means.
(@>), (<@)
  :: QGenExpr ctxt Postgres s (PgJSONB a)
  -> QGenExpr ctxt Postgres s (PgJSONB b)
  -> QGenExpr ctxt Postgres s Bool
QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a @> :: QGenExpr ctxt Postgres s (PgJSONB a)
-> QGenExpr ctxt Postgres s (PgJSONB b)
-> QGenExpr ctxt Postgres s Bool
@> QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
b =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s Bool
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"@>" (PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a (TablePrefix -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
b)
QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a <@ :: QGenExpr ctxt Postgres s (PgJSONB a)
-> QGenExpr ctxt Postgres s (PgJSONB b)
-> QGenExpr ctxt Postgres s Bool
<@ QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
b =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s Bool
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"<@" (PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a (TablePrefix -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
b)

-- | Access a JSON array by index. Corresponds to the Postgres @->@ operator.
-- See '(->$)' for the corresponding operator for object access.
(->#) :: IsPgJSON json
      => QGenExpr ctxt Postgres s (json a)
      -> QGenExpr ctxt Postgres s Int32
      -> QGenExpr ctxt Postgres s (json b)
QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a -># :: QGenExpr ctxt Postgres s (json a)
-> QGenExpr ctxt Postgres s Int32
-> QGenExpr ctxt Postgres s (json b)
-># QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
b =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s (json b)
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"->" (PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a (TablePrefix -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
b)

-- | Acces a JSON object by key. Corresponds to the Postgres @->@ operator. See
-- '(->#)' for the corresponding operator for arrays.
(->$) :: IsPgJSON json
      => QGenExpr ctxt Postgres s (json a)
      -> QGenExpr ctxt Postgres s T.Text
      -> QGenExpr ctxt Postgres s (json b)
QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a ->$ :: QGenExpr ctxt Postgres s (json a)
-> QGenExpr ctxt Postgres s TablePrefix
-> QGenExpr ctxt Postgres s (json b)
->$ QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
b =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s (json b)
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"->" (PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a (TablePrefix -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
b)

-- | Access a JSON array by index, returning the embedded object as a string.
-- Corresponds to the Postgres @->>@ operator. See '(->>$)' for the
-- corresponding operator on objects.
(->>#) :: IsPgJSON json
       => QGenExpr ctxt Postgres s (json a)
       -> QGenExpr ctxt Postgres s Int32
       -> QGenExpr ctxt Postgres s T.Text
QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a ->># :: QGenExpr ctxt Postgres s (json a)
-> QGenExpr ctxt Postgres s Int32
-> QGenExpr ctxt Postgres s TablePrefix
->># QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
b =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s TablePrefix
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"->>" (PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a (TablePrefix -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
b)

-- | Access a JSON object by key, returning the embedded object as a string.
-- Corresponds to the Postgres @->>@ operator. See '(->>#)' for the
-- corresponding operator on arrays.
(->>$) :: IsPgJSON json
       => QGenExpr ctxt Postgres s (json a)
       -> QGenExpr ctxt Postgres s T.Text
       -> QGenExpr ctxt Postgres s T.Text
QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a ->>$ :: QGenExpr ctxt Postgres s (json a)
-> QGenExpr ctxt Postgres s TablePrefix
-> QGenExpr ctxt Postgres s TablePrefix
->>$ QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
b =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s TablePrefix
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"->>" (PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a (TablePrefix -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
b)

-- | Access a deeply nested JSON object. The first argument is the JSON object
-- to look within, the second is the path of keys from the first argument to the
-- target. Returns the result as a new json value. Note that the postgres
-- function allows etiher string keys or integer indices, but this function only
-- allows string keys. PRs to improve this functionality are welcome.
(#>) :: IsPgJSON json
     => QGenExpr ctxt Postgres s (json a)
     -> QGenExpr ctxt Postgres s (V.Vector T.Text)
     -> QGenExpr ctxt Postgres s (json b)
QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a #> :: QGenExpr ctxt Postgres s (json a)
-> QGenExpr ctxt Postgres s (Vector TablePrefix)
-> QGenExpr ctxt Postgres s (json b)
#> QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
b =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s (json b)
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"#>" (PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a (TablePrefix -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
b)

-- | Like '(#>)' but returns the result as a string.
(#>>) :: IsPgJSON json
      => QGenExpr ctxt Postgres s (json a)
      -> QGenExpr ctxt Postgres s (V.Vector T.Text)
      -> QGenExpr ctxt Postgres s T.Text
QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a #>> :: QGenExpr ctxt Postgres s (json a)
-> QGenExpr ctxt Postgres s (Vector TablePrefix)
-> QGenExpr ctxt Postgres s TablePrefix
#>> QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
b =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s TablePrefix
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"#>>" (PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a (TablePrefix -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
b)

-- | Postgres @?@ operator. Checks if the given string exists as top-level key
-- of the json object.
(?)
  :: QGenExpr ctxt Postgres s (PgJSONB a)
  -> QGenExpr ctxt Postgres s T.Text
  -> QGenExpr ctxt Postgres s Bool
QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a ? :: QGenExpr ctxt Postgres s (PgJSONB a)
-> QGenExpr ctxt Postgres s TablePrefix
-> QGenExpr ctxt Postgres s Bool
? QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
b =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s Bool
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"?" (PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a (TablePrefix -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
b)

-- | Postgres @?|@ and @?&@ operators. Check if any or all of the given strings
-- exist as top-level keys of the json object respectively.
(?|), (?&)
  :: QGenExpr ctxt Postgres s (PgJSONB a)
  -> QGenExpr ctxt Postgres s (V.Vector T.Text)
  -> QGenExpr ctxt Postgres s Bool
QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a ?| :: QGenExpr ctxt Postgres s (PgJSONB a)
-> QGenExpr ctxt Postgres s (Vector TablePrefix)
-> QGenExpr ctxt Postgres s Bool
?| QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
b =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s Bool
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"?|" (PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a (TablePrefix -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
b)
QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a ?& :: QGenExpr ctxt Postgres s (PgJSONB a)
-> QGenExpr ctxt Postgres s (Vector TablePrefix)
-> QGenExpr ctxt Postgres s Bool
?& QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
b =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s Bool
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"?&" (PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a (TablePrefix -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
b)

-- | Postgres @-@ operator on json objects. Returns the supplied json object
-- with the supplied key deleted. See 'withoutIdx' for the corresponding
-- operator on arrays.
withoutKey
  :: QGenExpr ctxt Postgres s (PgJSONB a)
  -> QGenExpr ctxt Postgres s T.Text
  -> QGenExpr ctxt Postgres s (PgJSONB b)
QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a withoutKey :: QGenExpr ctxt Postgres s (PgJSONB a)
-> QGenExpr ctxt Postgres s TablePrefix
-> QGenExpr ctxt Postgres s (PgJSONB b)
`withoutKey` QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
b =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s (PgJSONB b)
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"-" (PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a (TablePrefix -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
b)

-- | Postgres @-@ operator on json arrays. See 'withoutKey' for the
-- corresponding operator on objects.
withoutIdx
  :: QGenExpr ctxt Postgres s (PgJSONB a)
  -> QGenExpr ctxt Postgres s Int32
  -> QGenExpr ctxt Postgres s (PgJSONB b)
QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a withoutIdx :: QGenExpr ctxt Postgres s (PgJSONB a)
-> QGenExpr ctxt Postgres s Int32
-> QGenExpr ctxt Postgres s (PgJSONB b)
`withoutIdx` QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
b =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s (PgJSONB b)
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"-" (PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a (TablePrefix -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
b)

-- | Postgres @#-@ operator. Removes all the keys specificied from the JSON
-- object and returns the result.
withoutKeys
  :: QGenExpr ctxt Postgres s (PgJSONB a)
  -> QGenExpr ctxt Postgres s (V.Vector T.Text)
  -> QGenExpr ctxt Postgres s (PgJSONB b)
QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a withoutKeys :: QGenExpr ctxt Postgres s (PgJSONB a)
-> QGenExpr ctxt Postgres s (Vector TablePrefix)
-> QGenExpr ctxt Postgres s (PgJSONB b)
`withoutKeys` QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
b =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s (PgJSONB b)
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"#-" (PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a (TablePrefix -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
b)

-- | Postgres @json_array_length@ function. The supplied json object should be
-- an array, but this isn't checked at compile-time.
pgJsonArrayLength :: IsPgJSON json => QGenExpr ctxt Postgres s (json a)
                  -> QGenExpr ctxt Postgres s Int32
pgJsonArrayLength :: QGenExpr ctxt Postgres s (json a) -> QGenExpr ctxt Postgres s Int32
pgJsonArrayLength (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a) =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s Int32
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr ((TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
 -> QGenExpr ctxt Postgres s Int32)
-> (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s Int32
forall a b. (a -> b) -> a -> b
$ \TablePrefix
tbl ->
  PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"json_array_length(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a TablePrefix
tbl) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")

-- | Postgres @array_to_json@ function.
pgArrayToJson
  :: QGenExpr ctxt Postgres s (V.Vector e)
  -> QGenExpr ctxt Postgres s (PgJSON a)
pgArrayToJson :: QGenExpr ctxt Postgres s (Vector e)
-> QGenExpr ctxt Postgres s (PgJSON a)
pgArrayToJson (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a) = (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s (PgJSON a)
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr ((TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
 -> QGenExpr ctxt Postgres s (PgJSON a))
-> (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s (PgJSON a)
forall a b. (a -> b) -> a -> b
$ TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a (TablePrefix -> PgExpressionSyntax)
-> (PgExpressionSyntax -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgExpressionSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  PgSyntax -> PgSyntax -> PgSyntax
forall a. Monoid a => a -> a -> a
mappend (ByteString -> PgSyntax
emit ByteString
"array_to_json") (PgSyntax -> PgSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  PgSyntax -> PgSyntax
pgParens (PgSyntax -> PgSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  PgExpressionSyntax -> PgSyntax
fromPgExpression

-- | The postgres @jsonb_set@ function. 'pgJsonUpdate' expects the value
-- specified by the path in the second argument to exist. If it does not, the
-- first argument is not modified. 'pgJsonbSet' will create any intermediate
-- objects necessary. This corresponds to the @create_missing@ argument of
-- @jsonb_set@ being set to false or true respectively.
pgJsonbUpdate, pgJsonbSet
  :: QGenExpr ctxt Postgres s (PgJSONB a)
  -> QGenExpr ctxt Postgres s (V.Vector T.Text)
  -> QGenExpr ctxt Postgres s (PgJSONB b)
  -> QGenExpr ctxt Postgres s (PgJSONB a)
pgJsonbUpdate :: QGenExpr ctxt Postgres s (PgJSONB a)
-> QGenExpr ctxt Postgres s (Vector TablePrefix)
-> QGenExpr ctxt Postgres s (PgJSONB b)
-> QGenExpr ctxt Postgres s (PgJSONB a)
pgJsonbUpdate (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a) (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
path) (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
newVal) =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s (PgJSONB a)
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr ((TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
 -> QGenExpr ctxt Postgres s (PgJSONB a))
-> (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s (PgJSONB a)
forall a b. (a -> b) -> a -> b
$ ([PgSyntax] -> PgExpressionSyntax)
-> (TablePrefix -> [PgSyntax]) -> TablePrefix -> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax)
-> ([PgSyntax] -> PgSyntax) -> [PgSyntax] -> PgExpressionSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax -> PgSyntax
forall a. Monoid a => a -> a -> a
mappend (ByteString -> PgSyntax
emit ByteString
"jsonb_set") (PgSyntax -> PgSyntax)
-> ([PgSyntax] -> PgSyntax) -> [PgSyntax] -> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax
pgParens (PgSyntax -> PgSyntax)
-> ([PgSyntax] -> PgSyntax) -> [PgSyntax] -> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [PgSyntax] -> PgSyntax
forall a. Monoid a => [a] -> a
mconcat) ((TablePrefix -> [PgSyntax]) -> TablePrefix -> PgExpressionSyntax)
-> (TablePrefix -> [PgSyntax]) -> TablePrefix -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$ [TablePrefix -> PgSyntax] -> TablePrefix -> [PgSyntax]
forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
sequenceA ([TablePrefix -> PgSyntax] -> TablePrefix -> [PgSyntax])
-> [TablePrefix -> PgSyntax] -> TablePrefix -> [PgSyntax]
forall a b. (a -> b) -> a -> b
$
  [ PgExpressionSyntax -> PgSyntax
fromPgExpression (PgExpressionSyntax -> PgSyntax)
-> (TablePrefix -> PgExpressionSyntax) -> TablePrefix -> PgSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a, PgSyntax -> TablePrefix -> PgSyntax
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> PgSyntax
emit ByteString
", "), PgExpressionSyntax -> PgSyntax
fromPgExpression (PgExpressionSyntax -> PgSyntax)
-> (TablePrefix -> PgExpressionSyntax) -> TablePrefix -> PgSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
path, PgSyntax -> TablePrefix -> PgSyntax
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> PgSyntax
emit ByteString
", "), PgExpressionSyntax -> PgSyntax
fromPgExpression (PgExpressionSyntax -> PgSyntax)
-> (TablePrefix -> PgExpressionSyntax) -> TablePrefix -> PgSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
newVal ]
pgJsonbSet :: QGenExpr ctxt Postgres s (PgJSONB a)
-> QGenExpr ctxt Postgres s (Vector TablePrefix)
-> QGenExpr ctxt Postgres s (PgJSONB b)
-> QGenExpr ctxt Postgres s (PgJSONB a)
pgJsonbSet (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a) (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
path) (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
newVal) =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s (PgJSONB a)
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr ((TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
 -> QGenExpr ctxt Postgres s (PgJSONB a))
-> (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s (PgJSONB a)
forall a b. (a -> b) -> a -> b
$ ([PgSyntax] -> PgExpressionSyntax)
-> (TablePrefix -> [PgSyntax]) -> TablePrefix -> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax)
-> ([PgSyntax] -> PgSyntax) -> [PgSyntax] -> PgExpressionSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax -> PgSyntax
forall a. Monoid a => a -> a -> a
mappend (ByteString -> PgSyntax
emit ByteString
"jsonb_set") (PgSyntax -> PgSyntax)
-> ([PgSyntax] -> PgSyntax) -> [PgSyntax] -> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax
pgParens (PgSyntax -> PgSyntax)
-> ([PgSyntax] -> PgSyntax) -> [PgSyntax] -> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [PgSyntax] -> PgSyntax
forall a. Monoid a => [a] -> a
mconcat) ((TablePrefix -> [PgSyntax]) -> TablePrefix -> PgExpressionSyntax)
-> (TablePrefix -> [PgSyntax]) -> TablePrefix -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$ [TablePrefix -> PgSyntax] -> TablePrefix -> [PgSyntax]
forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
sequenceA ([TablePrefix -> PgSyntax] -> TablePrefix -> [PgSyntax])
-> [TablePrefix -> PgSyntax] -> TablePrefix -> [PgSyntax]
forall a b. (a -> b) -> a -> b
$
  [ PgExpressionSyntax -> PgSyntax
fromPgExpression (PgExpressionSyntax -> PgSyntax)
-> (TablePrefix -> PgExpressionSyntax) -> TablePrefix -> PgSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a, PgSyntax -> TablePrefix -> PgSyntax
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> PgSyntax
emit ByteString
", "), PgExpressionSyntax -> PgSyntax
fromPgExpression (PgExpressionSyntax -> PgSyntax)
-> (TablePrefix -> PgExpressionSyntax) -> TablePrefix -> PgSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
path, PgSyntax -> TablePrefix -> PgSyntax
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> PgSyntax
emit ByteString
", "), PgExpressionSyntax -> PgSyntax
fromPgExpression (PgExpressionSyntax -> PgSyntax)
-> (TablePrefix -> PgExpressionSyntax) -> TablePrefix -> PgSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
newVal, PgSyntax -> TablePrefix -> PgSyntax
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> PgSyntax
emit ByteString
", true") ]

-- | Postgres @jsonb_pretty@ function
pgJsonbPretty :: QGenExpr ctxt Postgres s (PgJSONB a)
              -> QGenExpr ctxt Postgres s T.Text
pgJsonbPretty :: QGenExpr ctxt Postgres s (PgJSONB a)
-> QGenExpr ctxt Postgres s TablePrefix
pgJsonbPretty (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a) =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s TablePrefix
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (\TablePrefix
tbl -> PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"jsonb_pretty" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSyntax -> PgSyntax
pgParens (PgExpressionSyntax -> PgSyntax
fromPgExpression (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a TablePrefix
tbl))))

-- ** Postgresql aggregates

-- | An aggregate that adds each value to the resulting array. See 'pgArrayOver'
-- if you want to specify a quantifier. Corresponds to the Postgres @ARRAY_AGG@
-- function.
pgArrayAgg :: QExpr Postgres s a
           -> QAgg Postgres s (V.Vector a)
pgArrayAgg :: QExpr Postgres s a -> QAgg Postgres s (Vector a)
pgArrayAgg = Maybe PgAggregationSetQuantifierSyntax
-> QExpr Postgres s a -> QAgg Postgres s (Vector a)
forall s a.
Maybe PgAggregationSetQuantifierSyntax
-> QExpr Postgres s a -> QAgg Postgres s (Vector a)
pgArrayAggOver Maybe PgAggregationSetQuantifierSyntax
forall s. IsSql92AggregationSetQuantifierSyntax s => Maybe s
allInGroup_

-- | Postgres @ARRAY_AGG@ with an explicit quantifier. Includes each row that
-- meets the quantification criteria in the result.
pgArrayAggOver :: Maybe PgAggregationSetQuantifierSyntax
               -> QExpr Postgres s a
               -> QAgg Postgres s (V.Vector a)
pgArrayAggOver :: Maybe PgAggregationSetQuantifierSyntax
-> QExpr Postgres s a -> QAgg Postgres s (Vector a)
pgArrayAggOver Maybe PgAggregationSetQuantifierSyntax
quantifier (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a) =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QAgg Postgres s (Vector a)
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr ((TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
 -> QAgg Postgres s (Vector a))
-> (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QAgg Postgres s (Vector a)
forall a b. (a -> b) -> a -> b
$ \TablePrefix
tbl ->
  PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax) -> PgSyntax -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$
    ByteString -> PgSyntax
emit ByteString
"array_agg" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
    PgSyntax -> PgSyntax
pgParens ( PgSyntax
-> (PgAggregationSetQuantifierSyntax -> PgSyntax)
-> Maybe PgAggregationSetQuantifierSyntax
-> PgSyntax
forall b a. b -> (a -> b) -> Maybe a -> b
maybe PgSyntax
forall a. Monoid a => a
mempty (\PgAggregationSetQuantifierSyntax
q -> PgAggregationSetQuantifierSyntax -> PgSyntax
fromPgAggregationSetQuantifier PgAggregationSetQuantifierSyntax
q PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
" ") Maybe PgAggregationSetQuantifierSyntax
quantifier PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
               PgExpressionSyntax -> PgSyntax
fromPgExpression (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a TablePrefix
tbl))

-- | Postgres @bool_or@ aggregate. Returns true if any of the rows are true.
pgBoolOr :: QExpr Postgres s a
         -> QAgg Postgres s (Maybe Bool)
pgBoolOr :: QExpr Postgres s a -> QAgg Postgres s (Maybe Bool)
pgBoolOr (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a) =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QAgg Postgres s (Maybe Bool)
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr ((TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
 -> QAgg Postgres s (Maybe Bool))
-> (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QAgg Postgres s (Maybe Bool)
forall a b. (a -> b) -> a -> b
$ \TablePrefix
tbl -> PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax) -> PgSyntax -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$
  ByteString -> PgSyntax
emit ByteString
"bool_or" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSyntax -> PgSyntax
pgParens (PgExpressionSyntax -> PgSyntax
fromPgExpression (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a TablePrefix
tbl))

-- | Postgres @bool_and@ aggregate. Returns false unless every row is true.
pgBoolAnd :: QExpr Postgres s a
          -> QAgg Postgres s (Maybe Bool)
pgBoolAnd :: QExpr Postgres s a -> QAgg Postgres s (Maybe Bool)
pgBoolAnd (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a) =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QAgg Postgres s (Maybe Bool)
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr ((TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
 -> QAgg Postgres s (Maybe Bool))
-> (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QAgg Postgres s (Maybe Bool)
forall a b. (a -> b) -> a -> b
$ \TablePrefix
tbl -> PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax) -> PgSyntax -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$
  ByteString -> PgSyntax
emit ByteString
"bool_and" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSyntax -> PgSyntax
pgParens (PgExpressionSyntax -> PgSyntax
fromPgExpression (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a TablePrefix
tbl))

-- *** String aggregations

-- | Joins the string value in each row of the first argument, using the second
-- argument as a delimiter. See 'pgStringAggOver' if you want to provide
-- explicit quantification.
pgStringAgg :: BeamSqlBackendIsString Postgres str
            => QExpr Postgres s str
            -> QExpr Postgres s str
            -> QAgg Postgres s (Maybe str)
pgStringAgg :: QExpr Postgres s str
-> QExpr Postgres s str -> QAgg Postgres s (Maybe str)
pgStringAgg = Maybe PgAggregationSetQuantifierSyntax
-> QExpr Postgres s str
-> QExpr Postgres s str
-> QAgg Postgres s (Maybe str)
forall str s.
BeamSqlBackendIsString Postgres str =>
Maybe PgAggregationSetQuantifierSyntax
-> QExpr Postgres s str
-> QExpr Postgres s str
-> QAgg Postgres s (Maybe str)
pgStringAggOver Maybe PgAggregationSetQuantifierSyntax
forall s. IsSql92AggregationSetQuantifierSyntax s => Maybe s
allInGroup_

-- | The Postgres @string_agg@ function, with an explicit quantifier. Joins the
-- values of the second argument using the delimiter given by the third.
pgStringAggOver :: BeamSqlBackendIsString Postgres str
                => Maybe PgAggregationSetQuantifierSyntax
                -> QExpr Postgres s str
                -> QExpr Postgres s str
                -> QAgg Postgres s (Maybe str)
pgStringAggOver :: Maybe PgAggregationSetQuantifierSyntax
-> QExpr Postgres s str
-> QExpr Postgres s str
-> QAgg Postgres s (Maybe str)
pgStringAggOver Maybe PgAggregationSetQuantifierSyntax
quantifier (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
v) (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
delim) =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QAgg Postgres s (Maybe str)
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr ((TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
 -> QAgg Postgres s (Maybe str))
-> (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QAgg Postgres s (Maybe str)
forall a b. (a -> b) -> a -> b
$ \TablePrefix
tbl -> PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax) -> PgSyntax -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$
  ByteString -> PgSyntax
emit ByteString
"string_agg" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
  PgSyntax -> PgSyntax
pgParens ( PgSyntax
-> (PgAggregationSetQuantifierSyntax -> PgSyntax)
-> Maybe PgAggregationSetQuantifierSyntax
-> PgSyntax
forall b a. b -> (a -> b) -> Maybe a -> b
maybe PgSyntax
forall a. Monoid a => a
mempty (\PgAggregationSetQuantifierSyntax
q -> PgAggregationSetQuantifierSyntax -> PgSyntax
fromPgAggregationSetQuantifier PgAggregationSetQuantifierSyntax
q PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
" ") Maybe PgAggregationSetQuantifierSyntax
quantifier PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
             PgExpressionSyntax -> PgSyntax
fromPgExpression (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
v TablePrefix
tbl) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
", " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
             PgExpressionSyntax -> PgSyntax
fromPgExpression (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
delim TablePrefix
tbl))

-- ** Postgresql SELECT DISTINCT ON

-- | Modify a query to only return rows where the supplied key function returns
-- a unique value. This corresponds to the Postgres @DISTINCT ON@ support.
pgNubBy_ :: ( Projectible Postgres key
            , Projectible Postgres r )
         => (r -> key)
         -> Q Postgres db s r
         -> Q Postgres db s r
pgNubBy_ :: (r -> key) -> Q Postgres db s r -> Q Postgres db s r
pgNubBy_ r -> key
mkKey (Q QM Postgres db s r
q) =
    QM Postgres db s r -> Q Postgres db s r
forall be (db :: (* -> *) -> *) s a. QM be db s a -> Q be db s a
Q (QM Postgres db s r -> Q Postgres db s r)
-> (QF Postgres db s r -> QM Postgres db s r)
-> QF Postgres db s r
-> Q Postgres db s r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. QF Postgres db s r -> QM Postgres db s r
forall (f :: * -> *) (m :: * -> *) a.
(Functor f, MonadFree f m) =>
f a -> m a
liftF (QF Postgres db s r -> Q Postgres db s r)
-> QF Postgres db s r -> Q Postgres db s r
forall a b. (a -> b) -> a -> b
$
    (r -> WithExprContext (BeamSqlBackendSetQuantifierSyntax Postgres))
-> QM Postgres db s r -> (r -> r) -> QF Postgres db s r
forall be r (db :: (* -> *) -> *) s next.
Projectible be r =>
(r -> WithExprContext (BeamSqlBackendSetQuantifierSyntax be))
-> QM be db s r -> (r -> next) -> QF be db s next
QDistinct (\r
r TablePrefix
pfx -> [PgExpressionSyntax] -> PgSelectSetQuantifierSyntax
pgSelectSetQuantifierDistinctOn
                            (Proxy Postgres
-> key -> WithExprContext [BeamSqlBackendExpressionSyntax Postgres]
forall be a.
Projectible be a =>
Proxy be
-> a -> WithExprContext [BeamSqlBackendExpressionSyntax be]
project (Proxy Postgres
forall k (t :: k). Proxy t
Proxy @Postgres) (r -> key
mkKey r
r) TablePrefix
pfx))
              QM Postgres db s r
q r -> r
forall a. a -> a
id

-- ** PostgreSql @MONEY@ data type

-- | Postgres @MONEY@ data type. A simple wrapper over 'ByteString', because
--   Postgres money format is locale-dependent, and we don't handle currency
--   symbol placement, digit grouping, or decimal separation.
--
--   The 'pgMoney' function can be used to convert a number to 'PgMoney'.
newtype PgMoney = PgMoney { PgMoney -> ByteString
fromPgMoney :: ByteString }
  deriving (Int -> PgMoney -> ShowS
[PgMoney] -> ShowS
PgMoney -> String
(Int -> PgMoney -> ShowS)
-> (PgMoney -> String) -> ([PgMoney] -> ShowS) -> Show PgMoney
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PgMoney] -> ShowS
$cshowList :: [PgMoney] -> ShowS
show :: PgMoney -> String
$cshow :: PgMoney -> String
showsPrec :: Int -> PgMoney -> ShowS
$cshowsPrec :: Int -> PgMoney -> ShowS
Show, ReadPrec [PgMoney]
ReadPrec PgMoney
Int -> ReadS PgMoney
ReadS [PgMoney]
(Int -> ReadS PgMoney)
-> ReadS [PgMoney]
-> ReadPrec PgMoney
-> ReadPrec [PgMoney]
-> Read PgMoney
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [PgMoney]
$creadListPrec :: ReadPrec [PgMoney]
readPrec :: ReadPrec PgMoney
$creadPrec :: ReadPrec PgMoney
readList :: ReadS [PgMoney]
$creadList :: ReadS [PgMoney]
readsPrec :: Int -> ReadS PgMoney
$creadsPrec :: Int -> ReadS PgMoney
Read, PgMoney -> PgMoney -> Bool
(PgMoney -> PgMoney -> Bool)
-> (PgMoney -> PgMoney -> Bool) -> Eq PgMoney
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PgMoney -> PgMoney -> Bool
$c/= :: PgMoney -> PgMoney -> Bool
== :: PgMoney -> PgMoney -> Bool
$c== :: PgMoney -> PgMoney -> Bool
Eq, Eq PgMoney
Eq PgMoney
-> (PgMoney -> PgMoney -> Ordering)
-> (PgMoney -> PgMoney -> Bool)
-> (PgMoney -> PgMoney -> Bool)
-> (PgMoney -> PgMoney -> Bool)
-> (PgMoney -> PgMoney -> Bool)
-> (PgMoney -> PgMoney -> PgMoney)
-> (PgMoney -> PgMoney -> PgMoney)
-> Ord PgMoney
PgMoney -> PgMoney -> Bool
PgMoney -> PgMoney -> Ordering
PgMoney -> PgMoney -> PgMoney
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: PgMoney -> PgMoney -> PgMoney
$cmin :: PgMoney -> PgMoney -> PgMoney
max :: PgMoney -> PgMoney -> PgMoney
$cmax :: PgMoney -> PgMoney -> PgMoney
>= :: PgMoney -> PgMoney -> Bool
$c>= :: PgMoney -> PgMoney -> Bool
> :: PgMoney -> PgMoney -> Bool
$c> :: PgMoney -> PgMoney -> Bool
<= :: PgMoney -> PgMoney -> Bool
$c<= :: PgMoney -> PgMoney -> Bool
< :: PgMoney -> PgMoney -> Bool
$c< :: PgMoney -> PgMoney -> Bool
compare :: PgMoney -> PgMoney -> Ordering
$ccompare :: PgMoney -> PgMoney -> Ordering
$cp1Ord :: Eq PgMoney
Ord)

instance Pg.FromField PgMoney where
 fromField :: FieldParser PgMoney
fromField Field
field Maybe ByteString
Nothing = (String -> Maybe Oid -> String -> String -> String -> ResultError)
-> Field -> String -> Conversion PgMoney
forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
Pg.returnError String -> Maybe Oid -> String -> String -> String -> ResultError
Pg.UnexpectedNull Field
field String
""
 fromField Field
field (Just ByteString
d) =
   if Field -> Oid
Pg.typeOid Field
field Oid -> Oid -> Bool
forall a. Eq a => a -> a -> Bool
/= TypeInfo -> Oid
Pg.typoid TypeInfo
Pg.money
   then (String -> Maybe Oid -> String -> String -> String -> ResultError)
-> Field -> String -> Conversion PgMoney
forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
Pg.returnError String -> Maybe Oid -> String -> String -> String -> ResultError
Pg.Incompatible Field
field String
""
   else PgMoney -> Conversion PgMoney
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> PgMoney
PgMoney ByteString
d)
instance Pg.ToField PgMoney where
  toField :: PgMoney -> Action
toField (PgMoney ByteString
a) = ByteString -> Action
forall a. ToField a => a -> Action
Pg.toField ByteString
a

instance HasSqlEqualityCheck Postgres PgMoney
instance HasSqlQuantifiedEqualityCheck Postgres PgMoney

instance FromBackendRow Postgres PgMoney
instance HasSqlValueSyntax PgValueSyntax PgMoney where
  sqlValueSyntax :: PgMoney -> PgValueSyntax
sqlValueSyntax (PgMoney ByteString
a) = ByteString -> PgValueSyntax
forall expr ty. HasSqlValueSyntax expr ty => ty -> expr
sqlValueSyntax ByteString
a

-- | Attempt to pack a floating point value as a 'PgMoney' value, paying no
-- attention to the locale-dependent currency symbol, digit grouping, or decimal
-- point. This will use the @.@ symbol as the decimal separator.
pgMoney :: Real a => a -> PgMoney
pgMoney :: a -> PgMoney
pgMoney a
val = ByteString -> PgMoney
PgMoney (String -> ByteString
BC.pack (FPFormat -> Maybe Int -> Scientific -> String
formatScientific FPFormat
Fixed Maybe Int
forall a. Maybe a
Nothing Scientific
exactVal))
  where
    exactVal :: Scientific
exactVal = Rational -> Scientific
forall a. Fractional a => Rational -> a
fromRational (a -> Rational
forall a. Real a => a -> Rational
toRational a
val) :: Scientific

-- | Multiply a @MONEY@ value by a numeric value. Corresponds to the Postgres
-- @*@ operator.
pgScaleMoney_ :: Num a
              => QGenExpr context Postgres s a
              -> QGenExpr context Postgres s PgMoney
              -> QGenExpr context Postgres s PgMoney
pgScaleMoney_ :: QGenExpr context Postgres s a
-> QGenExpr context Postgres s PgMoney
-> QGenExpr context Postgres s PgMoney
pgScaleMoney_ (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
scale) (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
v) =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr context Postgres s PgMoney
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"*" (PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
scale (TablePrefix -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
v)

-- | Divide a @MONEY@ value by a numeric value. Corresponds to Postgres @/@
-- where the numerator has type @MONEY@ and the denominator is a number. If you
-- would like to divide two @MONEY@ values and have their units cancel out, use
-- 'pgDivideMoneys_'.
pgDivideMoney_ :: Num a
               => QGenExpr context Postgres s PgMoney
               -> QGenExpr context Postgres s a
               -> QGenExpr context Postgres s PgMoney
pgDivideMoney_ :: QGenExpr context Postgres s PgMoney
-> QGenExpr context Postgres s a
-> QGenExpr context Postgres s PgMoney
pgDivideMoney_ (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
v) (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
scale) =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr context Postgres s PgMoney
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"/" (PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
v (TablePrefix -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
scale)

-- | Dividing two @MONEY@ value results in a number. Corresponds to Postgres @/@
-- on two @MONEY@ values. If you would like to divide @MONEY@ by a scalar, use 'pgDivideMoney_'
pgDivideMoneys_ :: Num a
                => QGenExpr context Postgres s PgMoney
                -> QGenExpr context Postgres s PgMoney
                -> QGenExpr context Postgres s a
pgDivideMoneys_ :: QGenExpr context Postgres s PgMoney
-> QGenExpr context Postgres s PgMoney
-> QGenExpr context Postgres s a
pgDivideMoneys_ (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a) (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
b) =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr context Postgres s a
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"/" (PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a (TablePrefix -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
b)

-- | Postgres @+@ and @-@ operators on money.
pgAddMoney_, pgSubtractMoney_
  :: QGenExpr context Postgres s PgMoney
  -> QGenExpr context Postgres s PgMoney
  -> QGenExpr context Postgres s PgMoney
pgAddMoney_ :: QGenExpr context Postgres s PgMoney
-> QGenExpr context Postgres s PgMoney
-> QGenExpr context Postgres s PgMoney
pgAddMoney_ (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a) (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
b) =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr context Postgres s PgMoney
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"+" (PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a (TablePrefix -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
b)
pgSubtractMoney_ :: QGenExpr context Postgres s PgMoney
-> QGenExpr context Postgres s PgMoney
-> QGenExpr context Postgres s PgMoney
pgSubtractMoney_ (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a) (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
b) =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr context Postgres s PgMoney
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"-" (PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a (TablePrefix -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
b)

-- | The Postgres @MONEY@ type can be summed or averaged in an aggregation.
-- These functions provide the quantified aggregations. See 'pgSumMoney_' and
-- 'pgAvgMoney_' for the unquantified versions.
pgSumMoneyOver_, pgAvgMoneyOver_
  :: Maybe PgAggregationSetQuantifierSyntax
  -> QExpr Postgres s PgMoney -> QExpr Postgres s PgMoney
pgSumMoneyOver_ :: Maybe PgAggregationSetQuantifierSyntax
-> QExpr Postgres s PgMoney -> QExpr Postgres s PgMoney
pgSumMoneyOver_ Maybe PgAggregationSetQuantifierSyntax
q (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a) = (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QExpr Postgres s PgMoney
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (Maybe (Sql92AggregationSetQuantifierSyntax PgExpressionSyntax)
-> PgExpressionSyntax -> PgExpressionSyntax
forall expr.
IsSql92AggregationExpressionSyntax expr =>
Maybe (Sql92AggregationSetQuantifierSyntax expr) -> expr -> expr
sumE Maybe (Sql92AggregationSetQuantifierSyntax PgExpressionSyntax)
Maybe PgAggregationSetQuantifierSyntax
q (PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a)
pgAvgMoneyOver_ :: Maybe PgAggregationSetQuantifierSyntax
-> QExpr Postgres s PgMoney -> QExpr Postgres s PgMoney
pgAvgMoneyOver_ Maybe PgAggregationSetQuantifierSyntax
q (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
a) = (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QExpr Postgres s PgMoney
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (Maybe (Sql92AggregationSetQuantifierSyntax PgExpressionSyntax)
-> PgExpressionSyntax -> PgExpressionSyntax
forall expr.
IsSql92AggregationExpressionSyntax expr =>
Maybe (Sql92AggregationSetQuantifierSyntax expr) -> expr -> expr
avgE Maybe (Sql92AggregationSetQuantifierSyntax PgExpressionSyntax)
Maybe PgAggregationSetQuantifierSyntax
q (PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
a)

-- | The Postgres @MONEY@ type can be summed or averaged in an aggregation. To
-- provide an explicit quantification, see 'pgSumMoneyOver_' and
-- 'pgAvgMoneyOver_'.
pgSumMoney_, pgAvgMoney_ :: QExpr Postgres s PgMoney
                         -> QExpr Postgres s PgMoney
pgSumMoney_ :: QExpr Postgres s PgMoney -> QExpr Postgres s PgMoney
pgSumMoney_ = Maybe PgAggregationSetQuantifierSyntax
-> QExpr Postgres s PgMoney -> QExpr Postgres s PgMoney
forall s.
Maybe PgAggregationSetQuantifierSyntax
-> QExpr Postgres s PgMoney -> QExpr Postgres s PgMoney
pgSumMoneyOver_ Maybe PgAggregationSetQuantifierSyntax
forall s. IsSql92AggregationSetQuantifierSyntax s => Maybe s
allInGroup_
pgAvgMoney_ :: QExpr Postgres s PgMoney -> QExpr Postgres s PgMoney
pgAvgMoney_ = Maybe PgAggregationSetQuantifierSyntax
-> QExpr Postgres s PgMoney -> QExpr Postgres s PgMoney
forall s.
Maybe PgAggregationSetQuantifierSyntax
-> QExpr Postgres s PgMoney -> QExpr Postgres s PgMoney
pgAvgMoneyOver_ Maybe PgAggregationSetQuantifierSyntax
forall s. IsSql92AggregationSetQuantifierSyntax s => Maybe s
allInGroup_

-- ** Geometry types

data PgPoint = PgPoint {-# UNPACK #-} !Double {-# UNPACK #-} !Double
  deriving (Int -> PgPoint -> ShowS
[PgPoint] -> ShowS
PgPoint -> String
(Int -> PgPoint -> ShowS)
-> (PgPoint -> String) -> ([PgPoint] -> ShowS) -> Show PgPoint
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PgPoint] -> ShowS
$cshowList :: [PgPoint] -> ShowS
show :: PgPoint -> String
$cshow :: PgPoint -> String
showsPrec :: Int -> PgPoint -> ShowS
$cshowsPrec :: Int -> PgPoint -> ShowS
Show, PgPoint -> PgPoint -> Bool
(PgPoint -> PgPoint -> Bool)
-> (PgPoint -> PgPoint -> Bool) -> Eq PgPoint
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PgPoint -> PgPoint -> Bool
$c/= :: PgPoint -> PgPoint -> Bool
== :: PgPoint -> PgPoint -> Bool
$c== :: PgPoint -> PgPoint -> Bool
Eq, Eq PgPoint
Eq PgPoint
-> (PgPoint -> PgPoint -> Ordering)
-> (PgPoint -> PgPoint -> Bool)
-> (PgPoint -> PgPoint -> Bool)
-> (PgPoint -> PgPoint -> Bool)
-> (PgPoint -> PgPoint -> Bool)
-> (PgPoint -> PgPoint -> PgPoint)
-> (PgPoint -> PgPoint -> PgPoint)
-> Ord PgPoint
PgPoint -> PgPoint -> Bool
PgPoint -> PgPoint -> Ordering
PgPoint -> PgPoint -> PgPoint
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: PgPoint -> PgPoint -> PgPoint
$cmin :: PgPoint -> PgPoint -> PgPoint
max :: PgPoint -> PgPoint -> PgPoint
$cmax :: PgPoint -> PgPoint -> PgPoint
>= :: PgPoint -> PgPoint -> Bool
$c>= :: PgPoint -> PgPoint -> Bool
> :: PgPoint -> PgPoint -> Bool
$c> :: PgPoint -> PgPoint -> Bool
<= :: PgPoint -> PgPoint -> Bool
$c<= :: PgPoint -> PgPoint -> Bool
< :: PgPoint -> PgPoint -> Bool
$c< :: PgPoint -> PgPoint -> Bool
compare :: PgPoint -> PgPoint -> Ordering
$ccompare :: PgPoint -> PgPoint -> Ordering
$cp1Ord :: Eq PgPoint
Ord)

data PgLine = PgLine {-# UNPACK #-} !Double -- A
                     {-# UNPACK #-} !Double -- B
                     {-# UNPACK #-} !Double -- C
  deriving (Int -> PgLine -> ShowS
[PgLine] -> ShowS
PgLine -> String
(Int -> PgLine -> ShowS)
-> (PgLine -> String) -> ([PgLine] -> ShowS) -> Show PgLine
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PgLine] -> ShowS
$cshowList :: [PgLine] -> ShowS
show :: PgLine -> String
$cshow :: PgLine -> String
showsPrec :: Int -> PgLine -> ShowS
$cshowsPrec :: Int -> PgLine -> ShowS
Show, PgLine -> PgLine -> Bool
(PgLine -> PgLine -> Bool)
-> (PgLine -> PgLine -> Bool) -> Eq PgLine
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PgLine -> PgLine -> Bool
$c/= :: PgLine -> PgLine -> Bool
== :: PgLine -> PgLine -> Bool
$c== :: PgLine -> PgLine -> Bool
Eq, Eq PgLine
Eq PgLine
-> (PgLine -> PgLine -> Ordering)
-> (PgLine -> PgLine -> Bool)
-> (PgLine -> PgLine -> Bool)
-> (PgLine -> PgLine -> Bool)
-> (PgLine -> PgLine -> Bool)
-> (PgLine -> PgLine -> PgLine)
-> (PgLine -> PgLine -> PgLine)
-> Ord PgLine
PgLine -> PgLine -> Bool
PgLine -> PgLine -> Ordering
PgLine -> PgLine -> PgLine
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: PgLine -> PgLine -> PgLine
$cmin :: PgLine -> PgLine -> PgLine
max :: PgLine -> PgLine -> PgLine
$cmax :: PgLine -> PgLine -> PgLine
>= :: PgLine -> PgLine -> Bool
$c>= :: PgLine -> PgLine -> Bool
> :: PgLine -> PgLine -> Bool
$c> :: PgLine -> PgLine -> Bool
<= :: PgLine -> PgLine -> Bool
$c<= :: PgLine -> PgLine -> Bool
< :: PgLine -> PgLine -> Bool
$c< :: PgLine -> PgLine -> Bool
compare :: PgLine -> PgLine -> Ordering
$ccompare :: PgLine -> PgLine -> Ordering
$cp1Ord :: Eq PgLine
Ord)

data PgLineSegment = PgLineSegment {-# UNPACK #-} !PgPoint {-# UNPACK #-} !PgPoint
  deriving (Int -> PgLineSegment -> ShowS
[PgLineSegment] -> ShowS
PgLineSegment -> String
(Int -> PgLineSegment -> ShowS)
-> (PgLineSegment -> String)
-> ([PgLineSegment] -> ShowS)
-> Show PgLineSegment
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PgLineSegment] -> ShowS
$cshowList :: [PgLineSegment] -> ShowS
show :: PgLineSegment -> String
$cshow :: PgLineSegment -> String
showsPrec :: Int -> PgLineSegment -> ShowS
$cshowsPrec :: Int -> PgLineSegment -> ShowS
Show, PgLineSegment -> PgLineSegment -> Bool
(PgLineSegment -> PgLineSegment -> Bool)
-> (PgLineSegment -> PgLineSegment -> Bool) -> Eq PgLineSegment
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PgLineSegment -> PgLineSegment -> Bool
$c/= :: PgLineSegment -> PgLineSegment -> Bool
== :: PgLineSegment -> PgLineSegment -> Bool
$c== :: PgLineSegment -> PgLineSegment -> Bool
Eq, Eq PgLineSegment
Eq PgLineSegment
-> (PgLineSegment -> PgLineSegment -> Ordering)
-> (PgLineSegment -> PgLineSegment -> Bool)
-> (PgLineSegment -> PgLineSegment -> Bool)
-> (PgLineSegment -> PgLineSegment -> Bool)
-> (PgLineSegment -> PgLineSegment -> Bool)
-> (PgLineSegment -> PgLineSegment -> PgLineSegment)
-> (PgLineSegment -> PgLineSegment -> PgLineSegment)
-> Ord PgLineSegment
PgLineSegment -> PgLineSegment -> Bool
PgLineSegment -> PgLineSegment -> Ordering
PgLineSegment -> PgLineSegment -> PgLineSegment
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: PgLineSegment -> PgLineSegment -> PgLineSegment
$cmin :: PgLineSegment -> PgLineSegment -> PgLineSegment
max :: PgLineSegment -> PgLineSegment -> PgLineSegment
$cmax :: PgLineSegment -> PgLineSegment -> PgLineSegment
>= :: PgLineSegment -> PgLineSegment -> Bool
$c>= :: PgLineSegment -> PgLineSegment -> Bool
> :: PgLineSegment -> PgLineSegment -> Bool
$c> :: PgLineSegment -> PgLineSegment -> Bool
<= :: PgLineSegment -> PgLineSegment -> Bool
$c<= :: PgLineSegment -> PgLineSegment -> Bool
< :: PgLineSegment -> PgLineSegment -> Bool
$c< :: PgLineSegment -> PgLineSegment -> Bool
compare :: PgLineSegment -> PgLineSegment -> Ordering
$ccompare :: PgLineSegment -> PgLineSegment -> Ordering
$cp1Ord :: Eq PgLineSegment
Ord)

data PgBox = PgBox {-# UNPACK #-} !PgPoint {-# UNPACK #-} !PgPoint
  deriving (Int -> PgBox -> ShowS
[PgBox] -> ShowS
PgBox -> String
(Int -> PgBox -> ShowS)
-> (PgBox -> String) -> ([PgBox] -> ShowS) -> Show PgBox
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PgBox] -> ShowS
$cshowList :: [PgBox] -> ShowS
show :: PgBox -> String
$cshow :: PgBox -> String
showsPrec :: Int -> PgBox -> ShowS
$cshowsPrec :: Int -> PgBox -> ShowS
Show)

instance Eq PgBox where
    PgBox PgPoint
a1 PgPoint
b1 == :: PgBox -> PgBox -> Bool
== PgBox PgPoint
a2 PgPoint
b2 =
        (PgPoint
a1 PgPoint -> PgPoint -> Bool
forall a. Eq a => a -> a -> Bool
== PgPoint
a2 Bool -> Bool -> Bool
&& PgPoint
b1 PgPoint -> PgPoint -> Bool
forall a. Eq a => a -> a -> Bool
== PgPoint
b2) Bool -> Bool -> Bool
||
        (PgPoint
a1 PgPoint -> PgPoint -> Bool
forall a. Eq a => a -> a -> Bool
== PgPoint
b2 Bool -> Bool -> Bool
&& PgPoint
b1 PgPoint -> PgPoint -> Bool
forall a. Eq a => a -> a -> Bool
== PgPoint
a2)

data PgPath
  = PgPathOpen   (NE.NonEmpty PgPoint)
  | PgPathClosed (NE.NonEmpty PgPoint)
  deriving (Int -> PgPath -> ShowS
[PgPath] -> ShowS
PgPath -> String
(Int -> PgPath -> ShowS)
-> (PgPath -> String) -> ([PgPath] -> ShowS) -> Show PgPath
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PgPath] -> ShowS
$cshowList :: [PgPath] -> ShowS
show :: PgPath -> String
$cshow :: PgPath -> String
showsPrec :: Int -> PgPath -> ShowS
$cshowsPrec :: Int -> PgPath -> ShowS
Show, PgPath -> PgPath -> Bool
(PgPath -> PgPath -> Bool)
-> (PgPath -> PgPath -> Bool) -> Eq PgPath
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PgPath -> PgPath -> Bool
$c/= :: PgPath -> PgPath -> Bool
== :: PgPath -> PgPath -> Bool
$c== :: PgPath -> PgPath -> Bool
Eq, Eq PgPath
Eq PgPath
-> (PgPath -> PgPath -> Ordering)
-> (PgPath -> PgPath -> Bool)
-> (PgPath -> PgPath -> Bool)
-> (PgPath -> PgPath -> Bool)
-> (PgPath -> PgPath -> Bool)
-> (PgPath -> PgPath -> PgPath)
-> (PgPath -> PgPath -> PgPath)
-> Ord PgPath
PgPath -> PgPath -> Bool
PgPath -> PgPath -> Ordering
PgPath -> PgPath -> PgPath
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: PgPath -> PgPath -> PgPath
$cmin :: PgPath -> PgPath -> PgPath
max :: PgPath -> PgPath -> PgPath
$cmax :: PgPath -> PgPath -> PgPath
>= :: PgPath -> PgPath -> Bool
$c>= :: PgPath -> PgPath -> Bool
> :: PgPath -> PgPath -> Bool
$c> :: PgPath -> PgPath -> Bool
<= :: PgPath -> PgPath -> Bool
$c<= :: PgPath -> PgPath -> Bool
< :: PgPath -> PgPath -> Bool
$c< :: PgPath -> PgPath -> Bool
compare :: PgPath -> PgPath -> Ordering
$ccompare :: PgPath -> PgPath -> Ordering
$cp1Ord :: Eq PgPath
Ord)

data PgPolygon
  = PgPolygon (NE.NonEmpty PgPoint)
  deriving (Int -> PgPolygon -> ShowS
[PgPolygon] -> ShowS
PgPolygon -> String
(Int -> PgPolygon -> ShowS)
-> (PgPolygon -> String)
-> ([PgPolygon] -> ShowS)
-> Show PgPolygon
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PgPolygon] -> ShowS
$cshowList :: [PgPolygon] -> ShowS
show :: PgPolygon -> String
$cshow :: PgPolygon -> String
showsPrec :: Int -> PgPolygon -> ShowS
$cshowsPrec :: Int -> PgPolygon -> ShowS
Show, PgPolygon -> PgPolygon -> Bool
(PgPolygon -> PgPolygon -> Bool)
-> (PgPolygon -> PgPolygon -> Bool) -> Eq PgPolygon
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PgPolygon -> PgPolygon -> Bool
$c/= :: PgPolygon -> PgPolygon -> Bool
== :: PgPolygon -> PgPolygon -> Bool
$c== :: PgPolygon -> PgPolygon -> Bool
Eq, Eq PgPolygon
Eq PgPolygon
-> (PgPolygon -> PgPolygon -> Ordering)
-> (PgPolygon -> PgPolygon -> Bool)
-> (PgPolygon -> PgPolygon -> Bool)
-> (PgPolygon -> PgPolygon -> Bool)
-> (PgPolygon -> PgPolygon -> Bool)
-> (PgPolygon -> PgPolygon -> PgPolygon)
-> (PgPolygon -> PgPolygon -> PgPolygon)
-> Ord PgPolygon
PgPolygon -> PgPolygon -> Bool
PgPolygon -> PgPolygon -> Ordering
PgPolygon -> PgPolygon -> PgPolygon
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: PgPolygon -> PgPolygon -> PgPolygon
$cmin :: PgPolygon -> PgPolygon -> PgPolygon
max :: PgPolygon -> PgPolygon -> PgPolygon
$cmax :: PgPolygon -> PgPolygon -> PgPolygon
>= :: PgPolygon -> PgPolygon -> Bool
$c>= :: PgPolygon -> PgPolygon -> Bool
> :: PgPolygon -> PgPolygon -> Bool
$c> :: PgPolygon -> PgPolygon -> Bool
<= :: PgPolygon -> PgPolygon -> Bool
$c<= :: PgPolygon -> PgPolygon -> Bool
< :: PgPolygon -> PgPolygon -> Bool
$c< :: PgPolygon -> PgPolygon -> Bool
compare :: PgPolygon -> PgPolygon -> Ordering
$ccompare :: PgPolygon -> PgPolygon -> Ordering
$cp1Ord :: Eq PgPolygon
Ord)

data PgCircle = PgCircle {-# UNPACK #-} !PgPoint {-# UNPACK #-} !Double
  deriving (Int -> PgCircle -> ShowS
[PgCircle] -> ShowS
PgCircle -> String
(Int -> PgCircle -> ShowS)
-> (PgCircle -> String) -> ([PgCircle] -> ShowS) -> Show PgCircle
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PgCircle] -> ShowS
$cshowList :: [PgCircle] -> ShowS
show :: PgCircle -> String
$cshow :: PgCircle -> String
showsPrec :: Int -> PgCircle -> ShowS
$cshowsPrec :: Int -> PgCircle -> ShowS
Show, PgCircle -> PgCircle -> Bool
(PgCircle -> PgCircle -> Bool)
-> (PgCircle -> PgCircle -> Bool) -> Eq PgCircle
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PgCircle -> PgCircle -> Bool
$c/= :: PgCircle -> PgCircle -> Bool
== :: PgCircle -> PgCircle -> Bool
$c== :: PgCircle -> PgCircle -> Bool
Eq, Eq PgCircle
Eq PgCircle
-> (PgCircle -> PgCircle -> Ordering)
-> (PgCircle -> PgCircle -> Bool)
-> (PgCircle -> PgCircle -> Bool)
-> (PgCircle -> PgCircle -> Bool)
-> (PgCircle -> PgCircle -> Bool)
-> (PgCircle -> PgCircle -> PgCircle)
-> (PgCircle -> PgCircle -> PgCircle)
-> Ord PgCircle
PgCircle -> PgCircle -> Bool
PgCircle -> PgCircle -> Ordering
PgCircle -> PgCircle -> PgCircle
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: PgCircle -> PgCircle -> PgCircle
$cmin :: PgCircle -> PgCircle -> PgCircle
max :: PgCircle -> PgCircle -> PgCircle
$cmax :: PgCircle -> PgCircle -> PgCircle
>= :: PgCircle -> PgCircle -> Bool
$c>= :: PgCircle -> PgCircle -> Bool
> :: PgCircle -> PgCircle -> Bool
$c> :: PgCircle -> PgCircle -> Bool
<= :: PgCircle -> PgCircle -> Bool
$c<= :: PgCircle -> PgCircle -> Bool
< :: PgCircle -> PgCircle -> Bool
$c< :: PgCircle -> PgCircle -> Bool
compare :: PgCircle -> PgCircle -> Ordering
$ccompare :: PgCircle -> PgCircle -> Ordering
$cp1Ord :: Eq PgCircle
Ord)

encodePgPoint :: PgPoint -> Builder
encodePgPoint :: PgPoint -> Builder
encodePgPoint (PgPoint Double
x Double
y) =
  Builder
"(" Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Double -> Builder
doubleDec Double
x Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
"," Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Double -> Builder
doubleDec Double
y Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
")"

instance HasSqlValueSyntax PgValueSyntax PgPoint where
  sqlValueSyntax :: PgPoint -> PgValueSyntax
sqlValueSyntax PgPoint
pt =
    PgSyntax -> PgValueSyntax
PgValueSyntax (PgSyntax -> PgValueSyntax) -> PgSyntax -> PgValueSyntax
forall a b. (a -> b) -> a -> b
$ Builder -> PgSyntax
emitBuilder (Builder
"'" Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> PgPoint -> Builder
encodePgPoint PgPoint
pt Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
"'")
instance HasSqlValueSyntax PgValueSyntax PgLine where
  sqlValueSyntax :: PgLine -> PgValueSyntax
sqlValueSyntax (PgLine Double
a Double
b Double
c) =
    PgSyntax -> PgValueSyntax
PgValueSyntax (PgSyntax -> PgValueSyntax) -> PgSyntax -> PgValueSyntax
forall a b. (a -> b) -> a -> b
$ Builder -> PgSyntax
emitBuilder (Builder
"'{" Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Double -> Builder
doubleDec Double
a Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
"," Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Double -> Builder
doubleDec Double
b Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
"," Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Double -> Builder
doubleDec Double
c Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
"}'")
instance HasSqlValueSyntax PgValueSyntax PgLineSegment where
  sqlValueSyntax :: PgLineSegment -> PgValueSyntax
sqlValueSyntax (PgLineSegment PgPoint
a PgPoint
b) =
    PgSyntax -> PgValueSyntax
PgValueSyntax (PgSyntax -> PgValueSyntax) -> PgSyntax -> PgValueSyntax
forall a b. (a -> b) -> a -> b
$ Builder -> PgSyntax
emitBuilder (Builder
"'(" Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> PgPoint -> Builder
encodePgPoint PgPoint
a Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
"," Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> PgPoint -> Builder
encodePgPoint PgPoint
b Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
")'")
instance HasSqlValueSyntax PgValueSyntax PgBox where
  sqlValueSyntax :: PgBox -> PgValueSyntax
sqlValueSyntax (PgBox PgPoint
a PgPoint
b) =
    PgSyntax -> PgValueSyntax
PgValueSyntax (PgSyntax -> PgValueSyntax) -> PgSyntax -> PgValueSyntax
forall a b. (a -> b) -> a -> b
$ Builder -> PgSyntax
emitBuilder (Builder
"'(" Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> PgPoint -> Builder
encodePgPoint PgPoint
a Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
"," Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> PgPoint -> Builder
encodePgPoint PgPoint
b Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
")'")

-- TODO Pg polygon and such

-- TODO frombackendrow

instance Pg.FromField PgPoint where
    fromField :: FieldParser PgPoint
fromField Field
field Maybe ByteString
Nothing = (String -> Maybe Oid -> String -> String -> String -> ResultError)
-> Field -> String -> Conversion PgPoint
forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
Pg.returnError String -> Maybe Oid -> String -> String -> String -> ResultError
Pg.UnexpectedNull Field
field String
""
    fromField Field
field (Just ByteString
d) =
        if Field -> Oid
Pg.typeOid Field
field Oid -> Oid -> Bool
forall a. Eq a => a -> a -> Bool
/= TypeInfo -> Oid
Pg.typoid TypeInfo
Pg.point
        then (String -> Maybe Oid -> String -> String -> String -> ResultError)
-> Field -> String -> Conversion PgPoint
forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
Pg.returnError String -> Maybe Oid -> String -> String -> String -> ResultError
Pg.Incompatible Field
field String
""
        else case Parser PgPoint -> ByteString -> Either String PgPoint
forall a. Parser a -> ByteString -> Either String a
parseOnly Parser PgPoint
pgPointParser ByteString
d of
               Left String
err -> (String -> Maybe Oid -> String -> String -> String -> ResultError)
-> Field -> String -> Conversion PgPoint
forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
Pg.returnError String -> Maybe Oid -> String -> String -> String -> ResultError
Pg.ConversionFailed Field
field (String
"PgPoint: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
err)
               Right PgPoint
pt -> PgPoint -> Conversion PgPoint
forall (f :: * -> *) a. Applicative f => a -> f a
pure PgPoint
pt
instance FromBackendRow Postgres PgPoint

pgPointParser :: Parser PgPoint
pgPointParser :: Parser PgPoint
pgPointParser = Double -> Double -> PgPoint
PgPoint (Double -> Double -> PgPoint)
-> Parser ByteString Double
-> Parser ByteString (Double -> PgPoint)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Char -> Parser Char
char Char
'(' Parser Char -> Parser ByteString Double -> Parser ByteString Double
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser ByteString Double
double Parser ByteString Double -> Parser Char -> Parser ByteString Double
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Char -> Parser Char
char Char
',')
                        Parser ByteString (Double -> PgPoint)
-> Parser ByteString Double -> Parser PgPoint
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Parser ByteString Double
double Parser ByteString Double -> Parser Char -> Parser ByteString Double
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Char -> Parser Char
char Char
')')

instance Pg.FromField PgBox where
    fromField :: FieldParser PgBox
fromField Field
field Maybe ByteString
Nothing = (String -> Maybe Oid -> String -> String -> String -> ResultError)
-> Field -> String -> Conversion PgBox
forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
Pg.returnError String -> Maybe Oid -> String -> String -> String -> ResultError
Pg.UnexpectedNull Field
field String
""
    fromField Field
field (Just ByteString
d) =
        if Field -> Oid
Pg.typeOid Field
field Oid -> Oid -> Bool
forall a. Eq a => a -> a -> Bool
/= TypeInfo -> Oid
Pg.typoid TypeInfo
Pg.box
        then (String -> Maybe Oid -> String -> String -> String -> ResultError)
-> Field -> String -> Conversion PgBox
forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
Pg.returnError String -> Maybe Oid -> String -> String -> String -> ResultError
Pg.Incompatible Field
field String
""
        else case Parser PgBox -> ByteString -> Either String PgBox
forall a. Parser a -> ByteString -> Either String a
parseOnly Parser PgBox
boxParser ByteString
d of
               Left  String
err -> (String -> Maybe Oid -> String -> String -> String -> ResultError)
-> Field -> String -> Conversion PgBox
forall a err.
(Typeable a, Exception err) =>
(String -> Maybe Oid -> String -> String -> String -> err)
-> Field -> String -> Conversion a
Pg.returnError String -> Maybe Oid -> String -> String -> String -> ResultError
Pg.ConversionFailed Field
field (String
"PgBox: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
err)
               Right PgBox
box -> PgBox -> Conversion PgBox
forall (f :: * -> *) a. Applicative f => a -> f a
pure PgBox
box

        where
          boxParser :: Parser PgBox
boxParser = PgPoint -> PgPoint -> PgBox
PgBox (PgPoint -> PgPoint -> PgBox)
-> Parser PgPoint -> Parser ByteString (PgPoint -> PgBox)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Parser PgPoint
pgPointParser Parser PgPoint -> Parser Char -> Parser PgPoint
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Char -> Parser Char
char Char
',')
                            Parser ByteString (PgPoint -> PgBox)
-> Parser PgPoint -> Parser PgBox
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser PgPoint
pgPointParser
instance FromBackendRow Postgres PgBox

-- ** Regular expressions

-- | The type of Postgres regular expressions. Only a
-- 'HasSqlValueSyntax' instance is supplied, because you won't need to
-- be reading these back from the database.
--
-- If you're generating regexes dynamically, then use 'pgRegex_' to
-- convert a string expression into a regex one.
newtype PgRegex = PgRegex T.Text
  deriving (Int -> PgRegex -> ShowS
[PgRegex] -> ShowS
PgRegex -> String
(Int -> PgRegex -> ShowS)
-> (PgRegex -> String) -> ([PgRegex] -> ShowS) -> Show PgRegex
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PgRegex] -> ShowS
$cshowList :: [PgRegex] -> ShowS
show :: PgRegex -> String
$cshow :: PgRegex -> String
showsPrec :: Int -> PgRegex -> ShowS
$cshowsPrec :: Int -> PgRegex -> ShowS
Show, PgRegex -> PgRegex -> Bool
(PgRegex -> PgRegex -> Bool)
-> (PgRegex -> PgRegex -> Bool) -> Eq PgRegex
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PgRegex -> PgRegex -> Bool
$c/= :: PgRegex -> PgRegex -> Bool
== :: PgRegex -> PgRegex -> Bool
$c== :: PgRegex -> PgRegex -> Bool
Eq, Eq PgRegex
Eq PgRegex
-> (PgRegex -> PgRegex -> Ordering)
-> (PgRegex -> PgRegex -> Bool)
-> (PgRegex -> PgRegex -> Bool)
-> (PgRegex -> PgRegex -> Bool)
-> (PgRegex -> PgRegex -> Bool)
-> (PgRegex -> PgRegex -> PgRegex)
-> (PgRegex -> PgRegex -> PgRegex)
-> Ord PgRegex
PgRegex -> PgRegex -> Bool
PgRegex -> PgRegex -> Ordering
PgRegex -> PgRegex -> PgRegex
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: PgRegex -> PgRegex -> PgRegex
$cmin :: PgRegex -> PgRegex -> PgRegex
max :: PgRegex -> PgRegex -> PgRegex
$cmax :: PgRegex -> PgRegex -> PgRegex
>= :: PgRegex -> PgRegex -> Bool
$c>= :: PgRegex -> PgRegex -> Bool
> :: PgRegex -> PgRegex -> Bool
$c> :: PgRegex -> PgRegex -> Bool
<= :: PgRegex -> PgRegex -> Bool
$c<= :: PgRegex -> PgRegex -> Bool
< :: PgRegex -> PgRegex -> Bool
$c< :: PgRegex -> PgRegex -> Bool
compare :: PgRegex -> PgRegex -> Ordering
$ccompare :: PgRegex -> PgRegex -> Ordering
$cp1Ord :: Eq PgRegex
Ord, String -> PgRegex
(String -> PgRegex) -> IsString PgRegex
forall a. (String -> a) -> IsString a
fromString :: String -> PgRegex
$cfromString :: String -> PgRegex
IsString)

instance HasSqlValueSyntax PgValueSyntax PgRegex where
  sqlValueSyntax :: PgRegex -> PgValueSyntax
sqlValueSyntax (PgRegex TablePrefix
r) = TablePrefix -> PgValueSyntax
forall expr ty. HasSqlValueSyntax expr ty => ty -> expr
sqlValueSyntax TablePrefix
r

-- | Convert a string valued expression (which could be generated
-- dynamically) into a 'PgRegex'-typed one.
pgRegex_ :: BeamSqlBackendIsString Postgres text => QGenExpr ctxt Postgres s text -> QGenExpr ctxt Postgres s PgRegex
pgRegex_ :: QGenExpr ctxt Postgres s text -> QGenExpr ctxt Postgres s PgRegex
pgRegex_ (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
e) = (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s PgRegex
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
e

-- | Match regular expression, case-sensitive
(~.) :: BeamSqlBackendIsString Postgres text
     => QGenExpr ctxt Postgres s text -> QGenExpr ctxt Postgres s PgRegex
     -> QGenExpr ctxt Postgres s Bool
QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
t ~. :: QGenExpr ctxt Postgres s text
-> QGenExpr ctxt Postgres s PgRegex
-> QGenExpr ctxt Postgres s Bool
~. QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
re = (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s Bool
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"~" (PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
t (TablePrefix -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
re)

-- | Match regular expression, case-insensitive
(~*.) :: BeamSqlBackendIsString Postgres text
      => QGenExpr ctxt Postgres s text -> QGenExpr ctxt Postgres s PgRegex
      -> QGenExpr ctxt Postgres s Bool
QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
t ~*. :: QGenExpr ctxt Postgres s text
-> QGenExpr ctxt Postgres s PgRegex
-> QGenExpr ctxt Postgres s Bool
~*. QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
re = (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s Bool
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"~*" (PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
t (TablePrefix -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
re)

-- | Does not match regular expression, case-sensitive
(!~.) :: BeamSqlBackendIsString Postgres text
      => QGenExpr ctxt Postgres s text -> QGenExpr ctxt Postgres s PgRegex
      -> QGenExpr ctxt Postgres s Bool
QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
t !~. :: QGenExpr ctxt Postgres s text
-> QGenExpr ctxt Postgres s PgRegex
-> QGenExpr ctxt Postgres s Bool
!~. QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
re = (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s Bool
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"!~" (PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
t (TablePrefix -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
re)

-- | Does not match regular expression, case-insensitive
(!~*.) :: BeamSqlBackendIsString Postgres text
       => QGenExpr ctxt Postgres s text -> QGenExpr ctxt Postgres s PgRegex
       -> QGenExpr ctxt Postgres s Bool
QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
t !~*. :: QGenExpr ctxt Postgres s text
-> QGenExpr ctxt Postgres s PgRegex
-> QGenExpr ctxt Postgres s Bool
!~*. QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
re = (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s Bool
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"!~*" (PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
-> PgExpressionSyntax
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
t (TablePrefix -> PgExpressionSyntax -> PgExpressionSyntax)
-> (TablePrefix -> PgExpressionSyntax)
-> TablePrefix
-> PgExpressionSyntax
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
re)

-- | Postgres @regexp_replace@. Replaces all instances of the regex in
-- the first argument with the third argument. The fourth argument is
-- the postgres regex options to provide.
pgRegexpReplace_ :: BeamSqlBackendIsString Postgres text
                 => QGenExpr ctxt Postgres s text -> QGenExpr ctxt Postgres s PgRegex
                 -> QGenExpr ctxt Postgres s text -> QGenExpr ctxt Postgres s T.Text
                 -> QGenExpr ctxt Postgres s txt
pgRegexpReplace_ :: QGenExpr ctxt Postgres s text
-> QGenExpr ctxt Postgres s PgRegex
-> QGenExpr ctxt Postgres s text
-> QGenExpr ctxt Postgres s TablePrefix
-> QGenExpr ctxt Postgres s txt
pgRegexpReplace_ (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
haystack) (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
needle) (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
replacement) (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
opts) =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s txt
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (\TablePrefix
t -> PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax) -> PgSyntax -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$
               ByteString -> PgSyntax
emit ByteString
"regexp_replace(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
haystack TablePrefix
t) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
", "
                                      PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
needle TablePrefix
t) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
", "
                                      PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
replacement TablePrefix
t) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
", "
                                      PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
opts TablePrefix
t) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")

-- | Postgres @regexp_match@. Matches the regular expression against
-- the string given and returns an array where each element
-- corresponds to a match in the string, or @NULL@ if nothing was
-- found
pgRegexpMatch_ :: BeamSqlBackendIsString Postgres text
               => QGenExpr ctxt Postgres s text -> QGenExpr ctxt Postgres s PgRegex
               -> QGenExpr ctxt Postgres s (Maybe (V.Vector text))
pgRegexpMatch_ :: QGenExpr ctxt Postgres s text
-> QGenExpr ctxt Postgres s PgRegex
-> QGenExpr ctxt Postgres s (Maybe (Vector text))
pgRegexpMatch_ (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
s) (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
re) =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s (Maybe (Vector text))
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (\TablePrefix
t -> PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax) -> PgSyntax -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$
               ByteString -> PgSyntax
emit ByteString
"regexp_match(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
s TablePrefix
t) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
", "
                                    PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
re TablePrefix
t) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")

-- | Postgres @regexp_split_to_array@. Splits the given string by the
-- given regex and returns the result as an array.
pgRegexpSplitToArray :: BeamSqlBackendIsString Postgres text
                     => QGenExpr ctxt Postgres s text -> QGenExpr ctxt Postgres s PgRegex
                     -> QGenExpr ctxt Postgres s (V.Vector text)
pgRegexpSplitToArray :: QGenExpr ctxt Postgres s text
-> QGenExpr ctxt Postgres s PgRegex
-> QGenExpr ctxt Postgres s (Vector text)
pgRegexpSplitToArray (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
s) (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
re) =
  (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
-> QGenExpr ctxt Postgres s (Vector text)
forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (\TablePrefix
t -> PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax) -> PgSyntax -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$
               ByteString -> PgSyntax
emit ByteString
"regexp_split_to_array(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
s TablePrefix
t) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
", "
                                             PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
re TablePrefix
t) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")

newtype PgReResultT f
  = PgReResultT { PgReResultT f -> C f TablePrefix
reResult :: C f T.Text }
  deriving (forall x. PgReResultT f -> Rep (PgReResultT f) x)
-> (forall x. Rep (PgReResultT f) x -> PgReResultT f)
-> Generic (PgReResultT f)
forall x. Rep (PgReResultT f) x -> PgReResultT f
forall x. PgReResultT f -> Rep (PgReResultT f) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (f :: * -> *) x. Rep (PgReResultT f) x -> PgReResultT f
forall (f :: * -> *) x. PgReResultT f -> Rep (PgReResultT f) x
$cto :: forall (f :: * -> *) x. Rep (PgReResultT f) x -> PgReResultT f
$cfrom :: forall (f :: * -> *) x. PgReResultT f -> Rep (PgReResultT f) x
Generic
instance Beamable PgReResultT

-- | Postgres @regexp_split_to_table@. Splits the given string by the
-- given regex and return a result set that can be joined against.
pgRegexpSplitToTable :: BeamSqlBackendIsString Postgres text
                     => QGenExpr ctxt Postgres s text -> QGenExpr ctxt Postgres s PgRegex

                     -> Q Postgres db s (QExpr Postgres s T.Text)
pgRegexpSplitToTable :: QGenExpr ctxt Postgres s text
-> QGenExpr ctxt Postgres s PgRegex
-> Q Postgres db s (QExpr Postgres s TablePrefix)
pgRegexpSplitToTable (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
s) (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
re) =
  (PgReResultT (QGenExpr QValueContext Postgres s)
 -> QExpr Postgres s TablePrefix)
-> Q Postgres
     db
     s
     (PgReResultT (QGenExpr QValueContext Postgres s))
-> Q Postgres db s (QExpr Postgres s TablePrefix)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap PgReResultT (QGenExpr QValueContext Postgres s)
-> QExpr Postgres s TablePrefix
forall (f :: * -> *). PgReResultT f -> C f TablePrefix
reResult (Q Postgres db s (PgReResultT (QGenExpr QValueContext Postgres s))
 -> Q Postgres db s (QExpr Postgres s TablePrefix))
-> Q Postgres
     db
     s
     (PgReResultT (QGenExpr QValueContext Postgres s))
-> Q Postgres db s (QExpr Postgres s TablePrefix)
forall a b. (a -> b) -> a -> b
$
  (TablePrefix -> PgSyntax)
-> Q Postgres
     db
     s
     (PgReResultT (QGenExpr QValueContext Postgres s))
forall (tbl :: (* -> *) -> *) (db :: (* -> *) -> *) s.
Beamable tbl =>
(TablePrefix -> PgSyntax)
-> Q Postgres db s (QExprTable Postgres s tbl)
pgUnnest' (\TablePrefix
t -> ByteString -> PgSyntax
emit ByteString
"regexp_split_to_table(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
s TablePrefix
t) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
", "
                                                 PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
re TablePrefix
t) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")

-- ** Set-valued functions

data PgSetOf (tbl :: (* -> *) -> *)

pgUnnest' :: forall tbl db s
           . Beamable tbl
          => (TablePrefix -> PgSyntax)
          -> Q Postgres db s (QExprTable Postgres s tbl)
pgUnnest' :: (TablePrefix -> PgSyntax)
-> Q Postgres db s (QExprTable Postgres s tbl)
pgUnnest' TablePrefix -> PgSyntax
q =
  QM Postgres db s (QExprTable Postgres s tbl)
-> Q Postgres db s (QExprTable Postgres s tbl)
forall be (db :: (* -> *) -> *) s a. QM be db s a -> Q be db s a
Q (QF Postgres db s (QExprTable Postgres s tbl)
-> QM Postgres db s (QExprTable Postgres s tbl)
forall (f :: * -> *) (m :: * -> *) a.
(Functor f, MonadFree f m) =>
f a -> m a
liftF ((TablePrefix -> TablePrefix -> BeamSqlBackendFromSyntax Postgres)
-> (TablePrefix -> QExprTable Postgres s tbl)
-> (QExprTable Postgres s tbl
    -> Maybe (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres))
-> ((TablePrefix, QExprTable Postgres s tbl)
    -> QExprTable Postgres s tbl)
-> QF Postgres db s (QExprTable Postgres s tbl)
forall be r next (db :: (* -> *) -> *) s.
Projectible be r =>
(TablePrefix -> TablePrefix -> BeamSqlBackendFromSyntax be)
-> (TablePrefix -> r)
-> (r
    -> Maybe (WithExprContext (BeamSqlBackendExpressionSyntax be)))
-> ((TablePrefix, r) -> next)
-> QF be db s next
QAll (\TablePrefix
pfx TablePrefix
alias ->
                    PgSyntax -> PgFromSyntax
PgFromSyntax (PgSyntax -> PgFromSyntax)
-> ([PgSyntax] -> PgSyntax) -> [PgSyntax] -> PgFromSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [PgSyntax] -> PgSyntax
forall a. Monoid a => [a] -> a
mconcat ([PgSyntax] -> PgFromSyntax) -> [PgSyntax] -> PgFromSyntax
forall a b. (a -> b) -> a -> b
$
                    [ TablePrefix -> PgSyntax
q TablePrefix
pfx, ByteString -> PgSyntax
emit ByteString
" "
                    , TablePrefix -> PgSyntax
pgQuotedIdentifier TablePrefix
alias
                    , PgSyntax -> PgSyntax
pgParens (PgSyntax -> [PgSyntax] -> PgSyntax
pgSepBy (ByteString -> PgSyntax
emit ByteString
", ") ((forall a. Columnar' (TableField tbl) a -> PgSyntax)
-> tbl (TableField tbl) -> [PgSyntax]
forall (table :: (* -> *) -> *) (f :: * -> *) b.
Beamable table =>
(forall a. Columnar' f a -> b) -> table f -> [b]
allBeamValues (\(Columnar' (TableField _ nm)) -> TablePrefix -> PgSyntax
pgQuotedIdentifier TablePrefix
nm) tbl (TableField tbl)
tblFields))
                    ])
                 (tbl (TableField tbl) -> TablePrefix -> QExprTable Postgres s tbl
forall be (table :: (* -> *) -> *) ctxt s.
(BeamSqlBackend be, Beamable table) =>
TableSettings table -> TablePrefix -> table (QGenExpr ctxt be s)
tableFieldsToExpressions tbl (TableField tbl)
tblFields)
                 (\QExprTable Postgres s tbl
_ -> Maybe (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres)
forall a. Maybe a
Nothing) (TablePrefix, QExprTable Postgres s tbl)
-> QExprTable Postgres s tbl
forall a b. (a, b) -> b
snd))
  where
    tblFields :: TableSettings tbl
    tblFields :: tbl (TableField tbl)
tblFields =
      State Int (tbl (TableField tbl)) -> Int -> tbl (TableField tbl)
forall s a. State s a -> s -> a
evalState ((forall a.
 Columnar' Ignored a
 -> Columnar' Ignored a
 -> StateT Int Identity (Columnar' (TableField tbl) a))
-> tbl Ignored -> tbl Ignored -> State Int (tbl (TableField tbl))
forall (table :: (* -> *) -> *) (m :: * -> *) (f :: * -> *)
       (g :: * -> *) (h :: * -> *).
(Beamable table, Applicative m) =>
(forall a. Columnar' f a -> Columnar' g a -> m (Columnar' h a))
-> table f -> table g -> m (table h)
zipBeamFieldsM (\Columnar' Ignored a
_ Columnar' Ignored a
_ ->
                                   do Int
i <- StateT Int Identity Int
forall s (m :: * -> *). MonadState s m => m s
get
                                      Int -> StateT Int Identity ()
forall s (m :: * -> *). MonadState s m => s -> m ()
put (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
                                      let fieldNm :: TablePrefix
fieldNm = String -> TablePrefix
forall a. IsString a => String -> a
fromString (String
"r" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
i)
                                      Columnar' (TableField tbl) a
-> StateT Int Identity (Columnar' (TableField tbl) a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Columnar (TableField tbl) a -> Columnar' (TableField tbl) a
forall (f :: * -> *) a. Columnar f a -> Columnar' f a
Columnar' (NonEmpty TablePrefix -> TablePrefix -> TableField tbl a
forall (table :: (* -> *) -> *) ty.
NonEmpty TablePrefix -> TablePrefix -> TableField table ty
TableField (TablePrefix -> NonEmpty TablePrefix
forall (f :: * -> *) a. Applicative f => a -> f a
pure TablePrefix
fieldNm) TablePrefix
fieldNm)))
                                tbl Ignored
forall (table :: (* -> *) -> *).
Beamable table =>
TableSkeleton table
tblSkeleton tbl Ignored
forall (table :: (* -> *) -> *).
Beamable table =>
TableSkeleton table
tblSkeleton) (Int
0 :: Int)

-- | Join the results of the given set-valued function to the query
pgUnnest :: forall tbl db s
          . Beamable tbl
         => QExpr Postgres s (PgSetOf tbl)
         -> Q Postgres db s (QExprTable Postgres s tbl)
pgUnnest :: QExpr Postgres s (PgSetOf tbl)
-> Q Postgres db s (QExprTable Postgres s tbl)
pgUnnest (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
q) = (TablePrefix -> PgSyntax)
-> Q Postgres db s (QExprTable Postgres s tbl)
forall (tbl :: (* -> *) -> *) (db :: (* -> *) -> *) s.
Beamable tbl =>
(TablePrefix -> PgSyntax)
-> Q Postgres db s (QExprTable Postgres s tbl)
pgUnnest' ((TablePrefix -> PgSyntax)
 -> Q Postgres db s (QExprTable Postgres s tbl))
-> (TablePrefix -> PgSyntax)
-> Q Postgres db s (QExprTable Postgres s tbl)
forall a b. (a -> b) -> a -> b
$ PgExpressionSyntax -> PgSyntax
fromPgExpression (PgExpressionSyntax -> PgSyntax)
-> (TablePrefix -> PgExpressionSyntax) -> TablePrefix -> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
TablePrefix -> PgExpressionSyntax
q

data PgUnnestArrayTbl a f = PgUnnestArrayTbl (C f a)
  deriving (forall x. PgUnnestArrayTbl a f -> Rep (PgUnnestArrayTbl a f) x)
-> (forall x. Rep (PgUnnestArrayTbl a f) x -> PgUnnestArrayTbl a f)
-> Generic (PgUnnestArrayTbl a f)
forall x. Rep (PgUnnestArrayTbl a f) x -> PgUnnestArrayTbl a f
forall x. PgUnnestArrayTbl a f -> Rep (PgUnnestArrayTbl a f) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a (f :: * -> *) x.
Rep (PgUnnestArrayTbl a f) x -> PgUnnestArrayTbl a f
forall a (f :: * -> *) x.
PgUnnestArrayTbl a f -> Rep (PgUnnestArrayTbl a f) x
$cto :: forall a (f :: * -> *) x.
Rep (PgUnnestArrayTbl a f) x -> PgUnnestArrayTbl a f
$cfrom :: forall a (f :: * -> *) x.
PgUnnestArrayTbl a f -> Rep (PgUnnestArrayTbl a f) x
Generic
instance Beamable (PgUnnestArrayTbl a)

-- | Introduce each element of the array as a row
pgUnnestArray :: QExpr Postgres s (V.Vector a)
              -> Q Postgres db s (QExpr Postgres s a)
pgUnnestArray :: QExpr Postgres s (Vector a) -> Q Postgres db s (QExpr Postgres s a)
pgUnnestArray (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
q) =
  (PgUnnestArrayTbl a (QGenExpr QValueContext Postgres s)
 -> QExpr Postgres s a)
-> Q Postgres
     db
     s
     (PgUnnestArrayTbl a (QGenExpr QValueContext Postgres s))
-> Q Postgres db s (QExpr Postgres s a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(PgUnnestArrayTbl C (QGenExpr QValueContext Postgres s) a
x) -> QExpr Postgres s a
C (QGenExpr QValueContext Postgres s) a
x) (Q Postgres
   db
   s
   (PgUnnestArrayTbl a (QGenExpr QValueContext Postgres s))
 -> Q Postgres db s (QExpr Postgres s a))
-> Q Postgres
     db
     s
     (PgUnnestArrayTbl a (QGenExpr QValueContext Postgres s))
-> Q Postgres db s (QExpr Postgres s a)
forall a b. (a -> b) -> a -> b
$
  (TablePrefix -> PgSyntax)
-> Q Postgres
     db
     s
     (PgUnnestArrayTbl a (QGenExpr QValueContext Postgres s))
forall (tbl :: (* -> *) -> *) (db :: (* -> *) -> *) s.
Beamable tbl =>
(TablePrefix -> PgSyntax)
-> Q Postgres db s (QExprTable Postgres s tbl)
pgUnnest' (\TablePrefix
t -> ByteString -> PgSyntax
emit ByteString
"UNNEST" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSyntax -> PgSyntax
pgParens (PgExpressionSyntax -> PgSyntax
fromPgExpression (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
q TablePrefix
t)))

data PgUnnestArrayWithOrdinalityTbl a f = PgUnnestArrayWithOrdinalityTbl (C f Int64) (C f a)
  deriving (forall x.
 PgUnnestArrayWithOrdinalityTbl a f
 -> Rep (PgUnnestArrayWithOrdinalityTbl a f) x)
-> (forall x.
    Rep (PgUnnestArrayWithOrdinalityTbl a f) x
    -> PgUnnestArrayWithOrdinalityTbl a f)
-> Generic (PgUnnestArrayWithOrdinalityTbl a f)
forall x.
Rep (PgUnnestArrayWithOrdinalityTbl a f) x
-> PgUnnestArrayWithOrdinalityTbl a f
forall x.
PgUnnestArrayWithOrdinalityTbl a f
-> Rep (PgUnnestArrayWithOrdinalityTbl a f) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a (f :: * -> *) x.
Rep (PgUnnestArrayWithOrdinalityTbl a f) x
-> PgUnnestArrayWithOrdinalityTbl a f
forall a (f :: * -> *) x.
PgUnnestArrayWithOrdinalityTbl a f
-> Rep (PgUnnestArrayWithOrdinalityTbl a f) x
$cto :: forall a (f :: * -> *) x.
Rep (PgUnnestArrayWithOrdinalityTbl a f) x
-> PgUnnestArrayWithOrdinalityTbl a f
$cfrom :: forall a (f :: * -> *) x.
PgUnnestArrayWithOrdinalityTbl a f
-> Rep (PgUnnestArrayWithOrdinalityTbl a f) x
Generic
instance Beamable (PgUnnestArrayWithOrdinalityTbl a)

-- | Introduce each element of the array as a row, along with the
-- element's index
pgUnnestArrayWithOrdinality :: QExpr Postgres s (V.Vector a)
                            -> Q Postgres db s (QExpr Postgres s Int64, QExpr Postgres s a)
pgUnnestArrayWithOrdinality :: QExpr Postgres s (Vector a)
-> Q Postgres db s (QExpr Postgres s Int64, QExpr Postgres s a)
pgUnnestArrayWithOrdinality (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
q) =
  (PgUnnestArrayWithOrdinalityTbl
   a (QGenExpr QValueContext Postgres s)
 -> (QExpr Postgres s Int64, QExpr Postgres s a))
-> Q Postgres
     db
     s
     (PgUnnestArrayWithOrdinalityTbl
        a (QGenExpr QValueContext Postgres s))
-> Q Postgres db s (QExpr Postgres s Int64, QExpr Postgres s a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(PgUnnestArrayWithOrdinalityTbl C (QGenExpr QValueContext Postgres s) Int64
i C (QGenExpr QValueContext Postgres s) a
x) -> (QExpr Postgres s Int64
C (QGenExpr QValueContext Postgres s) Int64
i, QExpr Postgres s a
C (QGenExpr QValueContext Postgres s) a
x)) (Q Postgres
   db
   s
   (PgUnnestArrayWithOrdinalityTbl
      a (QGenExpr QValueContext Postgres s))
 -> Q Postgres db s (QExpr Postgres s Int64, QExpr Postgres s a))
-> Q Postgres
     db
     s
     (PgUnnestArrayWithOrdinalityTbl
        a (QGenExpr QValueContext Postgres s))
-> Q Postgres db s (QExpr Postgres s Int64, QExpr Postgres s a)
forall a b. (a -> b) -> a -> b
$
  (TablePrefix -> PgSyntax)
-> Q Postgres
     db
     s
     (PgUnnestArrayWithOrdinalityTbl
        a (QGenExpr QValueContext Postgres s))
forall (tbl :: (* -> *) -> *) (db :: (* -> *) -> *) s.
Beamable tbl =>
(TablePrefix -> PgSyntax)
-> Q Postgres db s (QExprTable Postgres s tbl)
pgUnnest' (\TablePrefix
t -> ByteString -> PgSyntax
emit ByteString
"UNNEST" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSyntax -> PgSyntax
pgParens (PgExpressionSyntax -> PgSyntax
fromPgExpression (TablePrefix -> BeamSqlBackendExpressionSyntax Postgres
q TablePrefix
t)) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
" WITH ORDINALITY")

instance HasDefaultSqlDataType Postgres PgPoint where
  defaultSqlDataType :: Proxy PgPoint
-> Proxy Postgres -> Bool -> BeamSqlBackendDataTypeSyntax Postgres
defaultSqlDataType Proxy PgPoint
_ Proxy Postgres
_ Bool
_ = BeamSqlBackendDataTypeSyntax Postgres
PgDataTypeSyntax
pgPointType

instance HasDefaultSqlDataType Postgres PgLine where
  defaultSqlDataType :: Proxy PgLine
-> Proxy Postgres -> Bool -> BeamSqlBackendDataTypeSyntax Postgres
defaultSqlDataType Proxy PgLine
_ Proxy Postgres
_ Bool
_ = BeamSqlBackendDataTypeSyntax Postgres
PgDataTypeSyntax
pgLineType

instance HasDefaultSqlDataType Postgres PgLineSegment where
  defaultSqlDataType :: Proxy PgLineSegment
-> Proxy Postgres -> Bool -> BeamSqlBackendDataTypeSyntax Postgres
defaultSqlDataType Proxy PgLineSegment
_ Proxy Postgres
_ Bool
_ = BeamSqlBackendDataTypeSyntax Postgres
PgDataTypeSyntax
pgLineSegmentType

instance HasDefaultSqlDataType Postgres PgBox where
  defaultSqlDataType :: Proxy PgBox
-> Proxy Postgres -> Bool -> BeamSqlBackendDataTypeSyntax Postgres
defaultSqlDataType Proxy PgBox
_ Proxy Postgres
_ Bool
_ = BeamSqlBackendDataTypeSyntax Postgres
PgDataTypeSyntax
pgBoxType

instance HasDefaultSqlDataType Postgres TsQuery where
  defaultSqlDataType :: Proxy TsQuery
-> Proxy Postgres -> Bool -> BeamSqlBackendDataTypeSyntax Postgres
defaultSqlDataType Proxy TsQuery
_ Proxy Postgres
_ Bool
_ = BeamSqlBackendDataTypeSyntax Postgres
PgDataTypeSyntax
pgTsQueryType

instance HasDefaultSqlDataType Postgres TsVector where
  defaultSqlDataType :: Proxy TsVector
-> Proxy Postgres -> Bool -> BeamSqlBackendDataTypeSyntax Postgres
defaultSqlDataType Proxy TsVector
_ Proxy Postgres
_ Bool
_ = BeamSqlBackendDataTypeSyntax Postgres
PgDataTypeSyntax
pgTsVectorType

instance HasDefaultSqlDataType Postgres (PgJSON a) where
  defaultSqlDataType :: Proxy (PgJSON a)
-> Proxy Postgres -> Bool -> BeamSqlBackendDataTypeSyntax Postgres
defaultSqlDataType Proxy (PgJSON a)
_ Proxy Postgres
_ Bool
_ = BeamSqlBackendDataTypeSyntax Postgres
PgDataTypeSyntax
pgJsonType

instance HasDefaultSqlDataType Postgres (PgJSONB a) where
  defaultSqlDataType :: Proxy (PgJSONB a)
-> Proxy Postgres -> Bool -> BeamSqlBackendDataTypeSyntax Postgres
defaultSqlDataType Proxy (PgJSONB a)
_ Proxy Postgres
_ Bool
_ = BeamSqlBackendDataTypeSyntax Postgres
PgDataTypeSyntax
pgJsonbType

instance HasDefaultSqlDataType Postgres PgMoney where
  defaultSqlDataType :: Proxy PgMoney
-> Proxy Postgres -> Bool -> BeamSqlBackendDataTypeSyntax Postgres
defaultSqlDataType Proxy PgMoney
_ Proxy Postgres
_ Bool
_ = BeamSqlBackendDataTypeSyntax Postgres
PgDataTypeSyntax
pgMoneyType

instance HasDefaultSqlDataType Postgres a
    => HasDefaultSqlDataType Postgres (V.Vector a) where
  defaultSqlDataType :: Proxy (Vector a)
-> Proxy Postgres -> Bool -> BeamSqlBackendDataTypeSyntax Postgres
defaultSqlDataType Proxy (Vector a)
_ Proxy Postgres
be Bool
embedded =
      PgDataTypeSyntax -> PgDataTypeSyntax
pgUnboundedArrayType (Proxy a
-> Proxy Postgres -> Bool -> BeamSqlBackendDataTypeSyntax Postgres
forall be ty.
HasDefaultSqlDataType be ty =>
Proxy ty -> Proxy be -> Bool -> BeamSqlBackendDataTypeSyntax be
defaultSqlDataType (Proxy a
forall k (t :: k). Proxy t
Proxy :: Proxy a) Proxy Postgres
be Bool
embedded)

-- ** Extract

century_ :: HasSqlDate tgt => ExtractField Postgres tgt Int32
century_ :: ExtractField Postgres tgt Int32
century_ = Sql92ExtractFieldSyntax (BeamSqlBackendSyntax Postgres)
-> ExtractField Postgres tgt Int32
forall be tgt a.
Sql92ExtractFieldSyntax (BeamSqlBackendSyntax be)
-> ExtractField be tgt a
ExtractField (PgSyntax -> PgExtractFieldSyntax
PgExtractFieldSyntax (ByteString -> PgSyntax
emit ByteString
"CENTURY"))

decade_ :: HasSqlDate tgt => ExtractField Postgres tgt Int32
decade_ :: ExtractField Postgres tgt Int32
decade_ = Sql92ExtractFieldSyntax (BeamSqlBackendSyntax Postgres)
-> ExtractField Postgres tgt Int32
forall be tgt a.
Sql92ExtractFieldSyntax (BeamSqlBackendSyntax be)
-> ExtractField be tgt a
ExtractField (PgSyntax -> PgExtractFieldSyntax
PgExtractFieldSyntax (ByteString -> PgSyntax
emit ByteString
"DECADE"))

dow_ :: HasSqlDate tgt => ExtractField Postgres tgt Int32
dow_ :: ExtractField Postgres tgt Int32
dow_ = Sql92ExtractFieldSyntax (BeamSqlBackendSyntax Postgres)
-> ExtractField Postgres tgt Int32
forall be tgt a.
Sql92ExtractFieldSyntax (BeamSqlBackendSyntax be)
-> ExtractField be tgt a
ExtractField (PgSyntax -> PgExtractFieldSyntax
PgExtractFieldSyntax (ByteString -> PgSyntax
emit ByteString
"DOW"))

doy_ :: HasSqlDate tgt => ExtractField Postgres tgt Int32
doy_ :: ExtractField Postgres tgt Int32
doy_ = Sql92ExtractFieldSyntax (BeamSqlBackendSyntax Postgres)
-> ExtractField Postgres tgt Int32
forall be tgt a.
Sql92ExtractFieldSyntax (BeamSqlBackendSyntax be)
-> ExtractField be tgt a
ExtractField (PgSyntax -> PgExtractFieldSyntax
PgExtractFieldSyntax (ByteString -> PgSyntax
emit ByteString
"DOY"))

epoch_ :: HasSqlTime tgt => ExtractField Postgres tgt NominalDiffTime
epoch_ :: ExtractField Postgres tgt NominalDiffTime
epoch_ = Sql92ExtractFieldSyntax (BeamSqlBackendSyntax Postgres)
-> ExtractField Postgres tgt NominalDiffTime
forall be tgt a.
Sql92ExtractFieldSyntax (BeamSqlBackendSyntax be)
-> ExtractField be tgt a
ExtractField (PgSyntax -> PgExtractFieldSyntax
PgExtractFieldSyntax (ByteString -> PgSyntax
emit ByteString
"EPOCH"))

isodow_ :: HasSqlDate tgt => ExtractField Postgres tgt Int32
isodow_ :: ExtractField Postgres tgt Int32
isodow_ = Sql92ExtractFieldSyntax (BeamSqlBackendSyntax Postgres)
-> ExtractField Postgres tgt Int32
forall be tgt a.
Sql92ExtractFieldSyntax (BeamSqlBackendSyntax be)
-> ExtractField be tgt a
ExtractField (PgSyntax -> PgExtractFieldSyntax
PgExtractFieldSyntax (ByteString -> PgSyntax
emit ByteString
"ISODOW"))

isoyear_ :: HasSqlDate tgt => ExtractField Postgres tgt Int32
isoyear_ :: ExtractField Postgres tgt Int32
isoyear_ = Sql92ExtractFieldSyntax (BeamSqlBackendSyntax Postgres)
-> ExtractField Postgres tgt Int32
forall be tgt a.
Sql92ExtractFieldSyntax (BeamSqlBackendSyntax be)
-> ExtractField be tgt a
ExtractField (PgSyntax -> PgExtractFieldSyntax
PgExtractFieldSyntax (ByteString -> PgSyntax
emit ByteString
"ISOYEAR"))

microseconds_ :: HasSqlTime tgt => ExtractField Postgres tgt Int32
microseconds_ :: ExtractField Postgres tgt Int32
microseconds_ = Sql92ExtractFieldSyntax (BeamSqlBackendSyntax Postgres)
-> ExtractField Postgres tgt Int32
forall be tgt a.
Sql92ExtractFieldSyntax (BeamSqlBackendSyntax be)
-> ExtractField be tgt a
ExtractField (PgSyntax -> PgExtractFieldSyntax
PgExtractFieldSyntax (ByteString -> PgSyntax
emit ByteString
"MICROSECONDS"))

milliseconds_ :: HasSqlTime tgt => ExtractField Postgres tgt Int32
milliseconds_ :: ExtractField Postgres tgt Int32
milliseconds_ = Sql92ExtractFieldSyntax (BeamSqlBackendSyntax Postgres)
-> ExtractField Postgres tgt Int32
forall be tgt a.
Sql92ExtractFieldSyntax (BeamSqlBackendSyntax be)
-> ExtractField be tgt a
ExtractField (PgSyntax -> PgExtractFieldSyntax
PgExtractFieldSyntax (ByteString -> PgSyntax
emit ByteString
"MILLISECONDS"))

millennium_ :: HasSqlDate tgt => ExtractField Postgres tgt Int32
millennium_ :: ExtractField Postgres tgt Int32
millennium_ = Sql92ExtractFieldSyntax (BeamSqlBackendSyntax Postgres)
-> ExtractField Postgres tgt Int32
forall be tgt a.
Sql92ExtractFieldSyntax (BeamSqlBackendSyntax be)
-> ExtractField be tgt a
ExtractField (PgSyntax -> PgExtractFieldSyntax
PgExtractFieldSyntax (ByteString -> PgSyntax
emit ByteString
"MILLENNIUM"))

quarter_ :: HasSqlDate tgt => ExtractField Postgres tgt Int32
quarter_ :: ExtractField Postgres tgt Int32
quarter_ = Sql92ExtractFieldSyntax (BeamSqlBackendSyntax Postgres)
-> ExtractField Postgres tgt Int32
forall be tgt a.
Sql92ExtractFieldSyntax (BeamSqlBackendSyntax be)
-> ExtractField be tgt a
ExtractField (PgSyntax -> PgExtractFieldSyntax
PgExtractFieldSyntax (ByteString -> PgSyntax
emit ByteString
"QUARTER"))

week_ :: HasSqlDate tgt => ExtractField Postgres tgt Int32
week_ :: ExtractField Postgres tgt Int32
week_ = Sql92ExtractFieldSyntax (BeamSqlBackendSyntax Postgres)
-> ExtractField Postgres tgt Int32
forall be tgt a.
Sql92ExtractFieldSyntax (BeamSqlBackendSyntax be)
-> ExtractField be tgt a
ExtractField (PgSyntax -> PgExtractFieldSyntax
PgExtractFieldSyntax (ByteString -> PgSyntax
emit ByteString
"WEEK"))

-- $full-text-search
--
-- Postgres has comprehensive, and thus complicated, support for full text
-- search. The types and functions in this section map closely to the underlying
-- Postgres API, which is described in the
-- <https://www.postgresql.org/docs/current/static/textsearch-intro.html documentation>.
--

-- $arrays
--
-- The functions and types in this section map Postgres @ARRAY@ types to
-- Haskell. An array is serialized and deserialized to a 'Data.Vector.Vector'
-- object. This type most closely matches the semantics of Postgres @ARRAY@s. In
-- general, the names of functions in this section closely match names of the
-- native Postgres functions they map to. As with most beam expression
-- functions, names are suffixed with an underscore and CamelCased.
--
-- Note that Postgres supports arbitrary nesting of vectors. For example, two,
-- three, or higher dimensional arrays can be expressed, manipulated, and stored
-- in tables. Beam fully supports this use case. A two-dimensional postgres
-- array is represented as @Vector (Vector a)@. Simply nest another 'Vector' for
-- higher dimensions. Some functions that return data on arrays expect a
-- dimension number as a parameter. Since beam can check the dimension at
-- compile time, these functions expect a type-level 'Nat' in the expression
-- DSL. The unsafe versions of these functions are also provided with the
-- @Unsafe_@ suffix. The safe versions are guaranteed not to fail at run-time
-- due to dimension mismatches, the unsafe ones may.
--
-- For more information on Postgres array support, refer to the postgres
-- <https://www.postgresql.org/docs/current/static/functions-array.html manual>.

-- $ranges
--
-- Postgres supports storing Range types in columns. There are serveral
-- predefined Range types and users may create their own. @beam-postgres@
-- fully supports these types, including user-defined range types. In general,
-- the names of functions in this section closely match names of the native
-- Postgres functions they map to. As with most beam expression functions,
-- names are suffixed with an underscore and CamelCased. Where ambiguous,
-- functions are prefixed with an @r@. Operators closely match their native
-- Postgres counterparts, except they are prefixed and/or suffixed with an @-@
-- to indicate the expression on that side is a Range. For example @-<\@-@ maps
-- to the native operator @<\@@ when both arguments are Ranges, while @<\@-@ maps
-- to the same operator when the first argument is an element, not a range.
--
-- For more information on Postgres range support, refer to the postgres
-- <https://www.postgresql.org/docs/current/static/rangetypes.html manual>.

-- $json
--
-- Postgres supports storing JSON in columns, as either a text-based type
-- (@JSON@) or a specialized binary encoding (@JSONB@). @beam-postgres@
-- accordingly provides the 'PgJSON' and 'PgJSONB' data types. Each of these
-- types takes a type parameter indicating the Haskell object represented by the
-- JSON object stored in the column. In order for serialization to work, be sure
-- to provide 'FromJSON' and 'ToJSON' instances for this type. If you do not
-- know the shape of the data stored, substitute 'Value' for this type
-- parameter.
--
-- For more information on Postgres JSON support see the postgres
-- <https://www.postgresql.org/docs/current/static/functions-json.html manual>.


-- $set-valued-funs
--
-- Postgres supports functions that returns /sets/. We can join directly against
-- these sets or arrays. @beam-postgres@ supports this feature via the
-- 'pgUnnest' and 'pgUnnestArray' functions.
--
-- Any function that returns a set can be typed as an expression returning
-- 'PgSetOf'. This polymorphic type takes one argument, which is a 'Beamable'
-- type that represents the shape of the data in the rows. For example, the
-- @json_each@ function returns a key and a value, so the corresponding
-- @beam-postgres@ function ('pgJsonEach') returns a value of type 'PgSetOf
-- (PgJSONEach Value)', which represents a set containing 'PgJSONEach'
-- rows. 'PgJSONEach' is a table with a column for keys ('pgJsonEachKey') and
-- one for values ('pgJsonEachValue').
--
-- Any 'PgSetOf' value can be introduced into the 'Q' monad using the 'pgUnnest'
-- function.
--
-- Postgres arrays (represented by the 'V.Vector' type) can also be joined
-- against using the 'pgUnnestArray' function. This directly corresponds to the
-- SQL @UNNEST@ keyword. Unlike sets, arrays have a sense of order. The
-- 'pgUnnestArrayWithOrdinality' function allows you to join against the
-- elements of an array along with its index. This corresponds to the
-- @UNNEST .. WITH ORDINALITY@ clause.