{-# OPTIONS_GHC -fno-warn-unused-binds -fno-warn-name-shadowing #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE UndecidableInstances #-}

-- | Data types for Postgres syntax. Access is given mainly for extension
-- modules. The types and definitions here are likely to change.
module Database.Beam.Postgres.Syntax
    ( PgSyntaxF(..), PgSyntaxM
    , PgSyntax(..)

    , emit, emitBuilder, escapeString
    , escapeBytea, escapeIdentifier
    , pgParens

    , nextSyntaxStep

    , PgCommandSyntax(..), PgCommandType(..)
    , PgSelectSyntax(..), PgSelectSetQuantifierSyntax(..)
    , PgInsertSyntax(..)
    , PgDeleteSyntax(..)
    , PgUpdateSyntax(..)

    , PgExpressionSyntax(..), PgFromSyntax(..), PgTableNameSyntax(..)
    , PgComparisonQuantifierSyntax(..)
    , PgExtractFieldSyntax(..)
    , PgProjectionSyntax(..), PgGroupingSyntax(..)
    , PgOrderingSyntax(..), PgValueSyntax(..)
    , PgTableSourceSyntax(..), PgFieldNameSyntax(..)
    , PgAggregationSetQuantifierSyntax(..)
    , PgInsertValuesSyntax(..), PgInsertOnConflictSyntax(..)
    , PgInsertOnConflictTargetSyntax(..), PgConflictActionSyntax(..)
    , PgCreateTableSyntax(..), PgTableOptionsSyntax(..), PgColumnSchemaSyntax(..)
    , PgDataTypeSyntax(..), PgColumnConstraintDefinitionSyntax(..), PgColumnConstraintSyntax(..)
    , PgTableConstraintSyntax(..), PgMatchTypeSyntax(..), PgReferentialActionSyntax(..)

    , PgAlterTableSyntax(..), PgAlterTableActionSyntax(..), PgAlterColumnActionSyntax(..)

    , PgWindowFrameSyntax(..), PgWindowFrameBoundsSyntax(..), PgWindowFrameBoundSyntax(..)

    , PgSelectLockingClauseSyntax(..)
    , PgSelectLockingStrength(..)
    , PgSelectLockingOptions(..)
    , fromPgSelectLockingClause
    , pgSelectStmt
    , defaultPgValueSyntax

    , PgDataTypeDescr(..)
    , PgHasEnum(..)

    , pgCreateExtensionSyntax, pgDropExtensionSyntax
    , pgCreateEnumSyntax, pgDropTypeSyntax

    , pgSimpleMatchSyntax

    , pgSelectSetQuantifierDistinctOn

    , pgDataTypeJSON

    , pgTsQueryType, pgTsVectorType
    , pgJsonType, pgJsonbType, pgUuidType
    , pgMoneyType
    , pgTsQueryTypeInfo, pgTsVectorTypeInfo

    , pgByteaType, pgTextType, pgUnboundedArrayType
    , pgSerialType, pgSmallSerialType, pgBigSerialType

    , pgPointType, pgLineType, pgLineSegmentType, pgBoxType

    , pgQuotedIdentifier, pgSepBy, pgDebugRenderSyntax
    , pgRenderSyntaxScript, pgBuildAction

    , pgBinOp, pgCompOp, pgUnOp, pgPostFix

    , pgTestSyntax

    , PostgresInaccessible ) where

import           Database.Beam hiding (insert)
import           Database.Beam.Backend.Internal.Compat
import           Database.Beam.Backend.SQL
import           Database.Beam.Migrate
import           Database.Beam.Migrate.SQL.Builder hiding (fromSqlConstraintAttributes)
import           Database.Beam.Migrate.Serialization

import           Control.Monad (guard)
import           Control.Monad.Free
import           Control.Monad.Free.Church

import           Data.Aeson (Value, object, (.=))
import           Data.Bits
import           Data.ByteString (ByteString)
import           Data.ByteString.Builder (Builder, byteString, char8, toLazyByteString)
import qualified Data.ByteString.Char8 as B
import           Data.ByteString.Lazy.Char8 (toStrict)
import qualified Data.ByteString.Lazy.Char8 as BL
import           Data.CaseInsensitive (CI)
import qualified Data.CaseInsensitive as CI
import           Data.Coerce
import           Data.Functor.Classes
import           Data.Hashable
import           Data.Int
import           Data.Maybe
#if !MIN_VERSION_base(4, 11, 0)
import           Data.Semigroup
#endif
import           Data.Scientific (Scientific)
import           Data.String (IsString(..), fromString)
import qualified Data.Text as T
import qualified Data.Text.Encoding as TE
import qualified Data.Text.Lazy as TL
import           Data.Time (LocalTime, UTCTime, TimeOfDay, NominalDiffTime, Day)
import           Data.UUID.Types (UUID, toASCIIBytes)
import           Data.Word
import qualified Data.Vector as V
import           GHC.TypeLits

import qualified Database.PostgreSQL.Simple.ToField as Pg
import qualified Database.PostgreSQL.Simple.TypeInfo.Static as Pg
import qualified Database.PostgreSQL.Simple.Types as Pg (Oid(..), Binary(..), Null(..))
import qualified Database.PostgreSQL.Simple.Time as Pg (Date, LocalTimestamp, UTCTimestamp)
import qualified Database.PostgreSQL.Simple.HStore as Pg (HStoreList, HStoreMap, HStoreBuilder)

data PostgresInaccessible

-- TODO This probably shouldn't be a free monad... oh well.
data PgSyntaxF f where
  EmitByteString :: ByteString -> f -> PgSyntaxF f
  EmitBuilder    :: Builder -> f -> PgSyntaxF f

  EscapeString :: ByteString -> f -> PgSyntaxF f
  EscapeBytea  :: ByteString -> f -> PgSyntaxF f
  EscapeIdentifier :: ByteString -> f -> PgSyntaxF f
deriving instance Functor PgSyntaxF

instance Eq1 PgSyntaxF where
  liftEq :: (a -> b -> Bool) -> PgSyntaxF a -> PgSyntaxF b -> Bool
liftEq a -> b -> Bool
eq (EmitByteString ByteString
b1 a
next1) (EmitByteString ByteString
b2 b
next2) =
      ByteString
b1 ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
b2 Bool -> Bool -> Bool
&& a
next1 a -> b -> Bool
`eq` b
next2
  liftEq a -> b -> Bool
eq (EmitBuilder Builder
b1 a
next1) (EmitBuilder Builder
b2 b
next2) =
      Builder -> ByteString
toLazyByteString Builder
b1 ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== Builder -> ByteString
toLazyByteString Builder
b2 Bool -> Bool -> Bool
&& a
next1 a -> b -> Bool
`eq` b
next2
  liftEq a -> b -> Bool
eq (EscapeString ByteString
b1 a
next1) (EscapeString ByteString
b2 b
next2) =
      ByteString
b1 ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
b2 Bool -> Bool -> Bool
&& a
next1 a -> b -> Bool
`eq` b
next2
  liftEq a -> b -> Bool
eq (EscapeBytea ByteString
b1 a
next1) (EscapeBytea ByteString
b2 b
next2) =
      ByteString
b1 ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
b2 Bool -> Bool -> Bool
&& a
next1 a -> b -> Bool
`eq` b
next2
  liftEq a -> b -> Bool
eq (EscapeIdentifier ByteString
b1 a
next1) (EscapeIdentifier ByteString
b2 b
next2) =
      ByteString
b1 ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
b2 Bool -> Bool -> Bool
&& a
next1 a -> b -> Bool
`eq` b
next2
  liftEq a -> b -> Bool
_ PgSyntaxF a
_ PgSyntaxF b
_ = Bool
False

instance Eq f => Eq (PgSyntaxF f) where
  == :: PgSyntaxF f -> PgSyntaxF f -> Bool
(==) = PgSyntaxF f -> PgSyntaxF f -> Bool
forall (f :: * -> *) a. (Eq1 f, Eq a) => f a -> f a -> Bool
eq1

instance Hashable PgSyntax where
  hashWithSalt :: Int -> PgSyntax -> Int
hashWithSalt Int
salt (PgSyntax PgSyntaxM ()
s) = PgSyntaxM ()
-> (() -> Int -> Int)
-> (PgSyntaxF (Int -> Int) -> Int -> Int)
-> Int
-> Int
forall (f :: * -> *) a.
F f a -> forall r. (a -> r) -> (f r -> r) -> r
runF PgSyntaxM ()
s () -> Int -> Int
forall p. p -> Int -> Int
finish PgSyntaxF (Int -> Int) -> Int -> Int
forall p. PgSyntaxF (Int -> p) -> Int -> p
step Int
salt
    where
      finish :: p -> Int -> Int
finish p
_ Int
salt = Int -> () -> Int
forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
salt ()
      step :: PgSyntaxF (Int -> p) -> Int -> p
step (EmitByteString ByteString
b Int -> p
hashRest) Int
salt = Int -> p
hashRest (Int -> (Int, ByteString) -> Int
forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
salt (Int
0 :: Int, ByteString
b))
      step (EmitBuilder Builder
b Int -> p
hashRest)    Int
salt = Int -> p
hashRest (Int -> (Int, ByteString) -> Int
forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
salt (Int
1 :: Int, Builder -> ByteString
toLazyByteString Builder
b))
      step (EscapeString  ByteString
b Int -> p
hashRest)  Int
salt = Int -> p
hashRest (Int -> (Int, ByteString) -> Int
forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
salt (Int
2 :: Int, ByteString
b))
      step (EscapeBytea  ByteString
b Int -> p
hashRest)   Int
salt = Int -> p
hashRest (Int -> (Int, ByteString) -> Int
forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
salt (Int
3 :: Int, ByteString
b))
      step (EscapeIdentifier ByteString
b Int -> p
hashRest) Int
salt = Int -> p
hashRest (Int -> (Int, ByteString) -> Int
forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
salt (Int
4 :: Int, ByteString
b))

instance Sql92DisplaySyntax PgSyntax where
  displaySyntax :: PgSyntax -> String
displaySyntax = ByteString -> String
BL.unpack (ByteString -> String)
-> (PgSyntax -> ByteString) -> PgSyntax -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> ByteString
pgRenderSyntaxScript

type PgSyntaxM = F PgSyntaxF

-- | A piece of Postgres SQL syntax, which may contain embedded escaped byte and
-- text sequences. 'PgSyntax' composes monoidally, and may be created with
-- 'emit', 'emitBuilder', 'escapeString', 'escapBytea', and 'escapeIdentifier'.
newtype PgSyntax
  = PgSyntax { PgSyntax -> PgSyntaxM ()
buildPgSyntax :: PgSyntaxM () }

instance Semigroup PgSyntax where
  PgSyntax
a <> :: PgSyntax -> PgSyntax -> PgSyntax
<> PgSyntax
b = PgSyntaxM () -> PgSyntax
PgSyntax (PgSyntax -> PgSyntaxM ()
buildPgSyntax PgSyntax
a PgSyntaxM () -> PgSyntaxM () -> PgSyntaxM ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> PgSyntax -> PgSyntaxM ()
buildPgSyntax PgSyntax
b)

instance Monoid PgSyntax where
  mempty :: PgSyntax
mempty = PgSyntaxM () -> PgSyntax
PgSyntax (() -> PgSyntaxM ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ())
  mappend :: PgSyntax -> PgSyntax -> PgSyntax
mappend = PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
(<>)

instance Eq PgSyntax where
  PgSyntax PgSyntaxM ()
x == :: PgSyntax -> PgSyntax -> Bool
== PgSyntax PgSyntaxM ()
y = (PgSyntaxM () -> Free PgSyntaxF ()
forall (f :: * -> *) (m :: * -> *) a. MonadFree f m => F f a -> m a
fromF PgSyntaxM ()
x :: Free PgSyntaxF ()) Free PgSyntaxF () -> Free PgSyntaxF () -> Bool
forall a. Eq a => a -> a -> Bool
== PgSyntaxM () -> Free PgSyntaxF ()
forall (f :: * -> *) (m :: * -> *) a. MonadFree f m => F f a -> m a
fromF PgSyntaxM ()
y

instance Show PgSyntax where
  showsPrec :: Int -> PgSyntax -> ShowS
showsPrec Int
prec PgSyntax
s =
    Bool -> ShowS -> ShowS
showParen (Int
prec Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
10) (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$
    String -> ShowS
showString String
"PgSyntax <" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    [PgSyntaxPrim] -> ShowS
forall a. Show a => a -> ShowS
shows (PgSyntax -> [PgSyntaxPrim]
pgTestSyntax PgSyntax
s) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    String -> ShowS
showString String
">"

emit :: ByteString -> PgSyntax
emit :: ByteString -> PgSyntax
emit ByteString
bs = PgSyntaxM () -> PgSyntax
PgSyntax (PgSyntaxF () -> PgSyntaxM ()
forall (f :: * -> *) (m :: * -> *) a.
(Functor f, MonadFree f m) =>
f a -> m a
liftF (ByteString -> () -> PgSyntaxF ()
forall f. ByteString -> f -> PgSyntaxF f
EmitByteString ByteString
bs ()))

emitBuilder :: Builder -> PgSyntax
emitBuilder :: Builder -> PgSyntax
emitBuilder Builder
b = PgSyntaxM () -> PgSyntax
PgSyntax (PgSyntaxF () -> PgSyntaxM ()
forall (f :: * -> *) (m :: * -> *) a.
(Functor f, MonadFree f m) =>
f a -> m a
liftF (Builder -> () -> PgSyntaxF ()
forall f. Builder -> f -> PgSyntaxF f
EmitBuilder Builder
b ()))

escapeString, escapeBytea, escapeIdentifier :: ByteString -> PgSyntax
escapeString :: ByteString -> PgSyntax
escapeString ByteString
bs = PgSyntaxM () -> PgSyntax
PgSyntax (PgSyntaxF () -> PgSyntaxM ()
forall (f :: * -> *) (m :: * -> *) a.
(Functor f, MonadFree f m) =>
f a -> m a
liftF (ByteString -> () -> PgSyntaxF ()
forall f. ByteString -> f -> PgSyntaxF f
EscapeString ByteString
bs ()))
escapeBytea :: ByteString -> PgSyntax
escapeBytea ByteString
bin = PgSyntaxM () -> PgSyntax
PgSyntax (PgSyntaxF () -> PgSyntaxM ()
forall (f :: * -> *) (m :: * -> *) a.
(Functor f, MonadFree f m) =>
f a -> m a
liftF (ByteString -> () -> PgSyntaxF ()
forall f. ByteString -> f -> PgSyntaxF f
EscapeBytea ByteString
bin ()))
escapeIdentifier :: ByteString -> PgSyntax
escapeIdentifier ByteString
id = PgSyntaxM () -> PgSyntax
PgSyntax (PgSyntaxF () -> PgSyntaxM ()
forall (f :: * -> *) (m :: * -> *) a.
(Functor f, MonadFree f m) =>
f a -> m a
liftF (ByteString -> () -> PgSyntaxF ()
forall f. ByteString -> f -> PgSyntaxF f
EscapeIdentifier ByteString
id ()))

nextSyntaxStep :: PgSyntaxF f -> f
nextSyntaxStep :: PgSyntaxF f -> f
nextSyntaxStep (EmitByteString ByteString
_ f
next) = f
next
nextSyntaxStep (EmitBuilder Builder
_ f
next) = f
next
nextSyntaxStep (EscapeString ByteString
_ f
next) = f
next
nextSyntaxStep (EscapeBytea ByteString
_ f
next) = f
next
nextSyntaxStep (EscapeIdentifier ByteString
_ f
next) = f
next

-- * Syntax types

data PgCommandType
    = PgCommandTypeQuery
    | PgCommandTypeDdl
    | PgCommandTypeDataUpdate
    | PgCommandTypeDataUpdateReturning
      deriving Int -> PgCommandType -> ShowS
[PgCommandType] -> ShowS
PgCommandType -> String
(Int -> PgCommandType -> ShowS)
-> (PgCommandType -> String)
-> ([PgCommandType] -> ShowS)
-> Show PgCommandType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PgCommandType] -> ShowS
$cshowList :: [PgCommandType] -> ShowS
show :: PgCommandType -> String
$cshow :: PgCommandType -> String
showsPrec :: Int -> PgCommandType -> ShowS
$cshowsPrec :: Int -> PgCommandType -> ShowS
Show

-- | Representation of an arbitrary Postgres command. This is the combination of
-- the command syntax (repesented by 'PgSyntax'), as well as the type of command
-- (represented by 'PgCommandType'). The command type is necessary for us to
-- know how to retrieve results from the database.
data PgCommandSyntax
    = PgCommandSyntax
    { PgCommandSyntax -> PgCommandType
pgCommandType :: PgCommandType
    , PgCommandSyntax -> PgSyntax
fromPgCommand :: PgSyntax }

-- | 'IsSql92SelectSyntax' for Postgres
newtype PgSelectSyntax = PgSelectSyntax { PgSelectSyntax -> PgSyntax
fromPgSelect :: PgSyntax }

newtype PgSelectTableSyntax = PgSelectTableSyntax { PgSelectTableSyntax -> PgSyntax
fromPgSelectTable :: PgSyntax }

-- | 'IsSql92InsertSyntax' for Postgres
newtype PgInsertSyntax = PgInsertSyntax { PgInsertSyntax -> PgSyntax
fromPgInsert :: PgSyntax }

-- | 'IsSql92DeleteSyntax' for Postgres
newtype PgDeleteSyntax = PgDeleteSyntax { PgDeleteSyntax -> PgSyntax
fromPgDelete :: PgSyntax }

-- | 'IsSql92UpdateSyntax' for Postgres
newtype PgUpdateSyntax = PgUpdateSyntax { PgUpdateSyntax -> PgSyntax
fromPgUpdate :: PgSyntax }

newtype PgExpressionSyntax = PgExpressionSyntax { PgExpressionSyntax -> PgSyntax
fromPgExpression :: PgSyntax } deriving PgExpressionSyntax -> PgExpressionSyntax -> Bool
(PgExpressionSyntax -> PgExpressionSyntax -> Bool)
-> (PgExpressionSyntax -> PgExpressionSyntax -> Bool)
-> Eq PgExpressionSyntax
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PgExpressionSyntax -> PgExpressionSyntax -> Bool
$c/= :: PgExpressionSyntax -> PgExpressionSyntax -> Bool
== :: PgExpressionSyntax -> PgExpressionSyntax -> Bool
$c== :: PgExpressionSyntax -> PgExpressionSyntax -> Bool
Eq
newtype PgAggregationSetQuantifierSyntax = PgAggregationSetQuantifierSyntax { PgAggregationSetQuantifierSyntax -> PgSyntax
fromPgAggregationSetQuantifier :: PgSyntax }
newtype PgSelectSetQuantifierSyntax = PgSelectSetQuantifierSyntax { PgSelectSetQuantifierSyntax -> PgSyntax
fromPgSelectSetQuantifier :: PgSyntax }
newtype PgFromSyntax = PgFromSyntax { PgFromSyntax -> PgSyntax
fromPgFrom :: PgSyntax }
newtype PgTableNameSyntax = PgTableNameSyntax { PgTableNameSyntax -> PgSyntax
fromPgTableName :: PgSyntax }
newtype PgComparisonQuantifierSyntax = PgComparisonQuantifierSyntax { PgComparisonQuantifierSyntax -> PgSyntax
fromPgComparisonQuantifier :: PgSyntax }
newtype PgExtractFieldSyntax = PgExtractFieldSyntax { PgExtractFieldSyntax -> PgSyntax
fromPgExtractField :: PgSyntax }
newtype PgProjectionSyntax = PgProjectionSyntax { PgProjectionSyntax -> PgSyntax
fromPgProjection :: PgSyntax }
newtype PgGroupingSyntax = PgGroupingSyntax { PgGroupingSyntax -> PgSyntax
fromPgGrouping :: PgSyntax }
newtype PgValueSyntax = PgValueSyntax { PgValueSyntax -> PgSyntax
fromPgValue :: PgSyntax }
newtype PgTableSourceSyntax = PgTableSourceSyntax { PgTableSourceSyntax -> PgSyntax
fromPgTableSource :: PgSyntax }
newtype PgFieldNameSyntax = PgFieldNameSyntax { PgFieldNameSyntax -> PgSyntax
fromPgFieldName :: PgSyntax }
newtype PgInsertValuesSyntax = PgInsertValuesSyntax { PgInsertValuesSyntax -> PgSyntax
fromPgInsertValues :: PgSyntax }
newtype PgInsertOnConflictSyntax = PgInsertOnConflictSyntax { PgInsertOnConflictSyntax -> PgSyntax
fromPgInsertOnConflict :: PgSyntax }
newtype PgInsertOnConflictTargetSyntax = PgInsertOnConflictTargetSyntax { PgInsertOnConflictTargetSyntax -> PgSyntax
fromPgInsertOnConflictTarget :: PgSyntax }
newtype PgInsertOnConflictUpdateSyntax = PgInsertOnConflictUpdateSyntax { PgInsertOnConflictUpdateSyntax -> PgSyntax
fromPgInsertOnConflictUpdate :: PgSyntax }
newtype PgConflictActionSyntax = PgConflictActionSyntax { PgConflictActionSyntax -> PgSyntax
fromPgConflictAction :: PgSyntax }
data PgOrderingSyntax = PgOrderingSyntax { PgOrderingSyntax -> PgSyntax
pgOrderingSyntax :: PgSyntax, PgOrderingSyntax -> Maybe PgNullOrdering
pgOrderingNullOrdering :: Maybe PgNullOrdering }
data PgSelectLockingClauseSyntax = PgSelectLockingClauseSyntax { PgSelectLockingClauseSyntax -> PgSelectLockingStrength
pgSelectLockingClauseStrength :: PgSelectLockingStrength
                                                               , PgSelectLockingClauseSyntax -> [Text]
pgSelectLockingTables :: [T.Text]
                                                               , PgSelectLockingClauseSyntax -> Maybe PgSelectLockingOptions
pgSelectLockingClauseOptions :: Maybe PgSelectLockingOptions }
newtype PgCommonTableExpressionSyntax
    = PgCommonTableExpressionSyntax { PgCommonTableExpressionSyntax -> PgSyntax
fromPgCommonTableExpression :: PgSyntax }

fromPgOrdering :: PgOrderingSyntax -> PgSyntax
fromPgOrdering :: PgOrderingSyntax -> PgSyntax
fromPgOrdering (PgOrderingSyntax PgSyntax
s Maybe PgNullOrdering
Nothing) = PgSyntax
s
fromPgOrdering (PgOrderingSyntax PgSyntax
s (Just PgNullOrdering
PgNullOrderingNullsFirst)) = PgSyntax
s PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
" NULLS FIRST"
fromPgOrdering (PgOrderingSyntax PgSyntax
s (Just PgNullOrdering
PgNullOrderingNullsLast)) = PgSyntax
s PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
" NULLS LAST"

data PgNullOrdering
  = PgNullOrderingNullsFirst
  | PgNullOrderingNullsLast
  deriving (Int -> PgNullOrdering -> ShowS
[PgNullOrdering] -> ShowS
PgNullOrdering -> String
(Int -> PgNullOrdering -> ShowS)
-> (PgNullOrdering -> String)
-> ([PgNullOrdering] -> ShowS)
-> Show PgNullOrdering
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PgNullOrdering] -> ShowS
$cshowList :: [PgNullOrdering] -> ShowS
show :: PgNullOrdering -> String
$cshow :: PgNullOrdering -> String
showsPrec :: Int -> PgNullOrdering -> ShowS
$cshowsPrec :: Int -> PgNullOrdering -> ShowS
Show, PgNullOrdering -> PgNullOrdering -> Bool
(PgNullOrdering -> PgNullOrdering -> Bool)
-> (PgNullOrdering -> PgNullOrdering -> Bool) -> Eq PgNullOrdering
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PgNullOrdering -> PgNullOrdering -> Bool
$c/= :: PgNullOrdering -> PgNullOrdering -> Bool
== :: PgNullOrdering -> PgNullOrdering -> Bool
$c== :: PgNullOrdering -> PgNullOrdering -> Bool
Eq, (forall x. PgNullOrdering -> Rep PgNullOrdering x)
-> (forall x. Rep PgNullOrdering x -> PgNullOrdering)
-> Generic PgNullOrdering
forall x. Rep PgNullOrdering x -> PgNullOrdering
forall x. PgNullOrdering -> Rep PgNullOrdering x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep PgNullOrdering x -> PgNullOrdering
$cfrom :: forall x. PgNullOrdering -> Rep PgNullOrdering x
Generic)

fromPgSelectLockingClause :: PgSelectLockingClauseSyntax -> PgSyntax
fromPgSelectLockingClause :: PgSelectLockingClauseSyntax -> PgSyntax
fromPgSelectLockingClause PgSelectLockingClauseSyntax
s =
  ByteString -> PgSyntax
emit ByteString
" FOR " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
  (case PgSelectLockingClauseSyntax -> PgSelectLockingStrength
pgSelectLockingClauseStrength PgSelectLockingClauseSyntax
s of
    PgSelectLockingStrength
PgSelectLockingStrengthUpdate -> ByteString -> PgSyntax
emit ByteString
"UPDATE"
    PgSelectLockingStrength
PgSelectLockingStrengthNoKeyUpdate -> ByteString -> PgSyntax
emit ByteString
"NO KEY UPDATE"
    PgSelectLockingStrength
PgSelectLockingStrengthShare -> ByteString -> PgSyntax
emit ByteString
"SHARE"
    PgSelectLockingStrength
PgSelectLockingStrengthKeyShare -> ByteString -> PgSyntax
emit ByteString
"KEY SHARE") PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
  PgSyntax
emitTables PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
  (PgSyntax
-> (PgSelectLockingOptions -> PgSyntax)
-> Maybe PgSelectLockingOptions
-> PgSyntax
forall b a. b -> (a -> b) -> Maybe a -> b
maybe PgSyntax
forall a. Monoid a => a
mempty PgSelectLockingOptions -> PgSyntax
emitOptions (Maybe PgSelectLockingOptions -> PgSyntax)
-> Maybe PgSelectLockingOptions -> PgSyntax
forall a b. (a -> b) -> a -> b
$ PgSelectLockingClauseSyntax -> Maybe PgSelectLockingOptions
pgSelectLockingClauseOptions PgSelectLockingClauseSyntax
s)
  where
    emitTables :: PgSyntax
emitTables = case PgSelectLockingClauseSyntax -> [Text]
pgSelectLockingTables PgSelectLockingClauseSyntax
s of
      [] -> PgSyntax
forall a. Monoid a => a
mempty
      [Text]
tableNames -> ByteString -> PgSyntax
emit ByteString
" OF " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> (PgSyntax -> [PgSyntax] -> PgSyntax
pgSepBy (ByteString -> PgSyntax
emit ByteString
", ") ((Text -> PgSyntax) -> [Text] -> [PgSyntax]
forall a b. (a -> b) -> [a] -> [b]
map Text -> PgSyntax
pgQuotedIdentifier [Text]
tableNames))

    emitOptions :: PgSelectLockingOptions -> PgSyntax
emitOptions PgSelectLockingOptions
PgSelectLockingOptionsNoWait = ByteString -> PgSyntax
emit ByteString
" NOWAIT"
    emitOptions PgSelectLockingOptions
PgSelectLockingOptionsSkipLocked = ByteString -> PgSyntax
emit ByteString
" SKIP LOCKED"

-- | Specifies the level of lock that will be taken against a row. See
-- <https://www.postgresql.org/docs/current/static/explicit-locking.html#LOCKING-ROWS the manual section>
-- for more information.
data PgSelectLockingStrength
  = PgSelectLockingStrengthUpdate
  -- ^ @UPDATE@
  | PgSelectLockingStrengthNoKeyUpdate
  -- ^ @NO KEY UPDATE@
  | PgSelectLockingStrengthShare
  -- ^ @SHARE@
  | PgSelectLockingStrengthKeyShare
  -- ^ @KEY SHARE@
  deriving (Int -> PgSelectLockingStrength -> ShowS
[PgSelectLockingStrength] -> ShowS
PgSelectLockingStrength -> String
(Int -> PgSelectLockingStrength -> ShowS)
-> (PgSelectLockingStrength -> String)
-> ([PgSelectLockingStrength] -> ShowS)
-> Show PgSelectLockingStrength
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PgSelectLockingStrength] -> ShowS
$cshowList :: [PgSelectLockingStrength] -> ShowS
show :: PgSelectLockingStrength -> String
$cshow :: PgSelectLockingStrength -> String
showsPrec :: Int -> PgSelectLockingStrength -> ShowS
$cshowsPrec :: Int -> PgSelectLockingStrength -> ShowS
Show, PgSelectLockingStrength -> PgSelectLockingStrength -> Bool
(PgSelectLockingStrength -> PgSelectLockingStrength -> Bool)
-> (PgSelectLockingStrength -> PgSelectLockingStrength -> Bool)
-> Eq PgSelectLockingStrength
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PgSelectLockingStrength -> PgSelectLockingStrength -> Bool
$c/= :: PgSelectLockingStrength -> PgSelectLockingStrength -> Bool
== :: PgSelectLockingStrength -> PgSelectLockingStrength -> Bool
$c== :: PgSelectLockingStrength -> PgSelectLockingStrength -> Bool
Eq, (forall x.
 PgSelectLockingStrength -> Rep PgSelectLockingStrength x)
-> (forall x.
    Rep PgSelectLockingStrength x -> PgSelectLockingStrength)
-> Generic PgSelectLockingStrength
forall x. Rep PgSelectLockingStrength x -> PgSelectLockingStrength
forall x. PgSelectLockingStrength -> Rep PgSelectLockingStrength x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep PgSelectLockingStrength x -> PgSelectLockingStrength
$cfrom :: forall x. PgSelectLockingStrength -> Rep PgSelectLockingStrength x
Generic)

-- | Specifies how we should handle lock conflicts.
--
-- See
-- <https://www.postgresql.org/docs/9.5/static/sql-select.html#SQL-FOR-UPDATE-SHARE the manual section>
-- for more information
data PgSelectLockingOptions
  = PgSelectLockingOptionsNoWait
  -- ^ @NOWAIT@. Report an error rather than waiting for the lock
  | PgSelectLockingOptionsSkipLocked
  -- ^ @SKIP LOCKED@. Rather than wait for a lock, skip the row instead
  deriving (Int -> PgSelectLockingOptions -> ShowS
[PgSelectLockingOptions] -> ShowS
PgSelectLockingOptions -> String
(Int -> PgSelectLockingOptions -> ShowS)
-> (PgSelectLockingOptions -> String)
-> ([PgSelectLockingOptions] -> ShowS)
-> Show PgSelectLockingOptions
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PgSelectLockingOptions] -> ShowS
$cshowList :: [PgSelectLockingOptions] -> ShowS
show :: PgSelectLockingOptions -> String
$cshow :: PgSelectLockingOptions -> String
showsPrec :: Int -> PgSelectLockingOptions -> ShowS
$cshowsPrec :: Int -> PgSelectLockingOptions -> ShowS
Show, PgSelectLockingOptions -> PgSelectLockingOptions -> Bool
(PgSelectLockingOptions -> PgSelectLockingOptions -> Bool)
-> (PgSelectLockingOptions -> PgSelectLockingOptions -> Bool)
-> Eq PgSelectLockingOptions
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PgSelectLockingOptions -> PgSelectLockingOptions -> Bool
$c/= :: PgSelectLockingOptions -> PgSelectLockingOptions -> Bool
== :: PgSelectLockingOptions -> PgSelectLockingOptions -> Bool
$c== :: PgSelectLockingOptions -> PgSelectLockingOptions -> Bool
Eq, (forall x. PgSelectLockingOptions -> Rep PgSelectLockingOptions x)
-> (forall x.
    Rep PgSelectLockingOptions x -> PgSelectLockingOptions)
-> Generic PgSelectLockingOptions
forall x. Rep PgSelectLockingOptions x -> PgSelectLockingOptions
forall x. PgSelectLockingOptions -> Rep PgSelectLockingOptions x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep PgSelectLockingOptions x -> PgSelectLockingOptions
$cfrom :: forall x. PgSelectLockingOptions -> Rep PgSelectLockingOptions x
Generic)

data PgDataTypeDescr
  = PgDataTypeDescrOid Pg.Oid (Maybe Int32)
  | PgDataTypeDescrDomain T.Text
  deriving (Int -> PgDataTypeDescr -> ShowS
[PgDataTypeDescr] -> ShowS
PgDataTypeDescr -> String
(Int -> PgDataTypeDescr -> ShowS)
-> (PgDataTypeDescr -> String)
-> ([PgDataTypeDescr] -> ShowS)
-> Show PgDataTypeDescr
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PgDataTypeDescr] -> ShowS
$cshowList :: [PgDataTypeDescr] -> ShowS
show :: PgDataTypeDescr -> String
$cshow :: PgDataTypeDescr -> String
showsPrec :: Int -> PgDataTypeDescr -> ShowS
$cshowsPrec :: Int -> PgDataTypeDescr -> ShowS
Show, PgDataTypeDescr -> PgDataTypeDescr -> Bool
(PgDataTypeDescr -> PgDataTypeDescr -> Bool)
-> (PgDataTypeDescr -> PgDataTypeDescr -> Bool)
-> Eq PgDataTypeDescr
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PgDataTypeDescr -> PgDataTypeDescr -> Bool
$c/= :: PgDataTypeDescr -> PgDataTypeDescr -> Bool
== :: PgDataTypeDescr -> PgDataTypeDescr -> Bool
$c== :: PgDataTypeDescr -> PgDataTypeDescr -> Bool
Eq, (forall x. PgDataTypeDescr -> Rep PgDataTypeDescr x)
-> (forall x. Rep PgDataTypeDescr x -> PgDataTypeDescr)
-> Generic PgDataTypeDescr
forall x. Rep PgDataTypeDescr x -> PgDataTypeDescr
forall x. PgDataTypeDescr -> Rep PgDataTypeDescr x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep PgDataTypeDescr x -> PgDataTypeDescr
$cfrom :: forall x. PgDataTypeDescr -> Rep PgDataTypeDescr x
Generic)
instance Hashable PgDataTypeDescr where
  hashWithSalt :: Int -> PgDataTypeDescr -> Int
hashWithSalt Int
salt (PgDataTypeDescrOid (Pg.Oid CUInt
oid) Maybe Int32
dim) =
    Int -> (Int, Word32, Maybe Int32) -> Int
forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
salt (Int
0 :: Int, CUInt -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral CUInt
oid :: Word32, Maybe Int32
dim)
  hashWithSalt Int
salt (PgDataTypeDescrDomain Text
t) =
    Int -> (Int, Text) -> Int
forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
salt (Int
1 :: Int, Text
t)

newtype PgCreateTableSyntax = PgCreateTableSyntax { PgCreateTableSyntax -> PgSyntax
fromPgCreateTable :: PgSyntax }
data PgTableOptionsSyntax = PgTableOptionsSyntax PgSyntax PgSyntax
newtype PgColumnSchemaSyntax = PgColumnSchemaSyntax { PgColumnSchemaSyntax -> PgSyntax
fromPgColumnSchema :: PgSyntax } deriving (Int -> PgColumnSchemaSyntax -> ShowS
[PgColumnSchemaSyntax] -> ShowS
PgColumnSchemaSyntax -> String
(Int -> PgColumnSchemaSyntax -> ShowS)
-> (PgColumnSchemaSyntax -> String)
-> ([PgColumnSchemaSyntax] -> ShowS)
-> Show PgColumnSchemaSyntax
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PgColumnSchemaSyntax] -> ShowS
$cshowList :: [PgColumnSchemaSyntax] -> ShowS
show :: PgColumnSchemaSyntax -> String
$cshow :: PgColumnSchemaSyntax -> String
showsPrec :: Int -> PgColumnSchemaSyntax -> ShowS
$cshowsPrec :: Int -> PgColumnSchemaSyntax -> ShowS
Show, PgColumnSchemaSyntax -> PgColumnSchemaSyntax -> Bool
(PgColumnSchemaSyntax -> PgColumnSchemaSyntax -> Bool)
-> (PgColumnSchemaSyntax -> PgColumnSchemaSyntax -> Bool)
-> Eq PgColumnSchemaSyntax
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PgColumnSchemaSyntax -> PgColumnSchemaSyntax -> Bool
$c/= :: PgColumnSchemaSyntax -> PgColumnSchemaSyntax -> Bool
== :: PgColumnSchemaSyntax -> PgColumnSchemaSyntax -> Bool
$c== :: PgColumnSchemaSyntax -> PgColumnSchemaSyntax -> Bool
Eq)
instance Sql92DisplaySyntax PgColumnSchemaSyntax where
  displaySyntax :: PgColumnSchemaSyntax -> String
displaySyntax = PgSyntax -> String
forall syntax. Sql92DisplaySyntax syntax => syntax -> String
displaySyntax (PgSyntax -> String)
-> (PgColumnSchemaSyntax -> PgSyntax)
-> PgColumnSchemaSyntax
-> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgColumnSchemaSyntax -> PgSyntax
fromPgColumnSchema

data PgDataTypeSyntax
  = PgDataTypeSyntax
  { PgDataTypeSyntax -> PgDataTypeDescr
pgDataTypeDescr :: PgDataTypeDescr
  , PgDataTypeSyntax -> PgSyntax
fromPgDataType :: PgSyntax
  , PgDataTypeSyntax -> BeamSerializedDataType
pgDataTypeSerialized :: BeamSerializedDataType
  } deriving Int -> PgDataTypeSyntax -> ShowS
[PgDataTypeSyntax] -> ShowS
PgDataTypeSyntax -> String
(Int -> PgDataTypeSyntax -> ShowS)
-> (PgDataTypeSyntax -> String)
-> ([PgDataTypeSyntax] -> ShowS)
-> Show PgDataTypeSyntax
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PgDataTypeSyntax] -> ShowS
$cshowList :: [PgDataTypeSyntax] -> ShowS
show :: PgDataTypeSyntax -> String
$cshow :: PgDataTypeSyntax -> String
showsPrec :: Int -> PgDataTypeSyntax -> ShowS
$cshowsPrec :: Int -> PgDataTypeSyntax -> ShowS
Show
instance Sql92DisplaySyntax PgDataTypeSyntax where
  displaySyntax :: PgDataTypeSyntax -> String
displaySyntax = PgSyntax -> String
forall syntax. Sql92DisplaySyntax syntax => syntax -> String
displaySyntax (PgSyntax -> String)
-> (PgDataTypeSyntax -> PgSyntax) -> PgDataTypeSyntax -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgDataTypeSyntax -> PgSyntax
fromPgDataType

data PgColumnConstraintDefinitionSyntax
  = PgColumnConstraintDefinitionSyntax
  { PgColumnConstraintDefinitionSyntax -> PgSyntax
fromPgColumnConstraintDefinition :: PgSyntax
  , PgColumnConstraintDefinitionSyntax
-> BeamSerializedConstraintDefinition
pgColumnConstraintDefinitionSerialized :: BeamSerializedConstraintDefinition
  } deriving Int -> PgColumnConstraintDefinitionSyntax -> ShowS
[PgColumnConstraintDefinitionSyntax] -> ShowS
PgColumnConstraintDefinitionSyntax -> String
(Int -> PgColumnConstraintDefinitionSyntax -> ShowS)
-> (PgColumnConstraintDefinitionSyntax -> String)
-> ([PgColumnConstraintDefinitionSyntax] -> ShowS)
-> Show PgColumnConstraintDefinitionSyntax
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PgColumnConstraintDefinitionSyntax] -> ShowS
$cshowList :: [PgColumnConstraintDefinitionSyntax] -> ShowS
show :: PgColumnConstraintDefinitionSyntax -> String
$cshow :: PgColumnConstraintDefinitionSyntax -> String
showsPrec :: Int -> PgColumnConstraintDefinitionSyntax -> ShowS
$cshowsPrec :: Int -> PgColumnConstraintDefinitionSyntax -> ShowS
Show
instance Sql92DisplaySyntax PgColumnConstraintDefinitionSyntax where
  displaySyntax :: PgColumnConstraintDefinitionSyntax -> String
displaySyntax = PgSyntax -> String
forall syntax. Sql92DisplaySyntax syntax => syntax -> String
displaySyntax (PgSyntax -> String)
-> (PgColumnConstraintDefinitionSyntax -> PgSyntax)
-> PgColumnConstraintDefinitionSyntax
-> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgColumnConstraintDefinitionSyntax -> PgSyntax
fromPgColumnConstraintDefinition

data PgColumnConstraintSyntax
  = PgColumnConstraintSyntax
  { PgColumnConstraintSyntax -> PgSyntax
fromPgColumnConstraint :: PgSyntax
  , PgColumnConstraintSyntax -> BeamSerializedConstraint
pgColumnConstraintSerialized :: BeamSerializedConstraint
  }
newtype PgTableConstraintSyntax = PgTableConstraintSyntax { PgTableConstraintSyntax -> PgSyntax
fromPgTableConstraint :: PgSyntax }
data PgMatchTypeSyntax
  = PgMatchTypeSyntax
  { PgMatchTypeSyntax -> PgSyntax
fromPgMatchType :: PgSyntax
  , PgMatchTypeSyntax -> BeamSerializedMatchType
pgMatchTypeSerialized :: BeamSerializedMatchType
  }
data PgReferentialActionSyntax
  = PgReferentialActionSyntax
  { PgReferentialActionSyntax -> PgSyntax
fromPgReferentialAction :: PgSyntax
  , PgReferentialActionSyntax -> BeamSerializedReferentialAction
pgReferentialActionSerialized :: BeamSerializedReferentialAction
  }
newtype PgDropTableSyntax = PgDropTableSyntax { PgDropTableSyntax -> PgSyntax
fromPgDropTable :: PgSyntax }
newtype PgAlterTableSyntax = PgAlterTableSyntax { PgAlterTableSyntax -> PgSyntax
fromPgAlterTable :: PgSyntax }
newtype PgAlterTableActionSyntax = PgAlterTableActionSyntax { PgAlterTableActionSyntax -> PgSyntax
fromPgAlterTableAction :: PgSyntax }
newtype PgAlterColumnActionSyntax = PgAlterColumnActionSyntax { PgAlterColumnActionSyntax -> PgSyntax
fromPgAlterColumnAction :: PgSyntax }
newtype PgWindowFrameSyntax = PgWindowFrameSyntax { PgWindowFrameSyntax -> PgSyntax
fromPgWindowFrame :: PgSyntax }
newtype PgWindowFrameBoundsSyntax = PgWindowFrameBoundsSyntax { PgWindowFrameBoundsSyntax -> PgSyntax
fromPgWindowFrameBounds :: PgSyntax }
newtype PgWindowFrameBoundSyntax = PgWindowFrameBoundSyntax { PgWindowFrameBoundSyntax -> ByteString -> PgSyntax
fromPgWindowFrameBound :: ByteString -> PgSyntax }

instance Hashable PgDataTypeSyntax where
  hashWithSalt :: Int -> PgDataTypeSyntax -> Int
hashWithSalt Int
salt (PgDataTypeSyntax PgDataTypeDescr
a PgSyntax
_ BeamSerializedDataType
_) = Int -> PgDataTypeDescr -> Int
forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
salt PgDataTypeDescr
a
instance Eq PgDataTypeSyntax where
  PgDataTypeSyntax PgDataTypeDescr
a PgSyntax
_ BeamSerializedDataType
_ == :: PgDataTypeSyntax -> PgDataTypeSyntax -> Bool
== PgDataTypeSyntax PgDataTypeDescr
b PgSyntax
_ BeamSerializedDataType
_ = PgDataTypeDescr
a PgDataTypeDescr -> PgDataTypeDescr -> Bool
forall a. Eq a => a -> a -> Bool
== PgDataTypeDescr
b

instance HasDataTypeCreatedCheck PgDataTypeSyntax where
  dataTypeHasBeenCreated :: PgDataTypeSyntax
-> (forall preCondition. Typeable preCondition => [preCondition])
-> Bool
dataTypeHasBeenCreated (PgDataTypeSyntax (PgDataTypeDescrOid {}) PgSyntax
_ BeamSerializedDataType
_) forall preCondition. Typeable preCondition => [preCondition]
_ = Bool
True
  dataTypeHasBeenCreated (PgDataTypeSyntax (PgDataTypeDescrDomain Text
d) PgSyntax
_ BeamSerializedDataType
_) forall preCondition. Typeable preCondition => [preCondition]
pre =
    Bool -> Bool
not (Bool -> Bool) -> ([()] -> Bool) -> [()] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [()] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([()] -> Bool) -> [()] -> Bool
forall a b. (a -> b) -> a -> b
$
    do PgHasEnum Text
nm [Text]
_ <- [PgHasEnum]
forall preCondition. Typeable preCondition => [preCondition]
pre
       Bool -> [()]
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Text
nm Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
d)

instance Eq PgColumnConstraintDefinitionSyntax where
  PgColumnConstraintDefinitionSyntax PgSyntax
a BeamSerializedConstraintDefinition
_ == :: PgColumnConstraintDefinitionSyntax
-> PgColumnConstraintDefinitionSyntax -> Bool
==
    PgColumnConstraintDefinitionSyntax PgSyntax
b BeamSerializedConstraintDefinition
_ =
      PgSyntax
a PgSyntax -> PgSyntax -> Bool
forall a. Eq a => a -> a -> Bool
== PgSyntax
b

instance IsSql92Syntax PgCommandSyntax where
  type Sql92SelectSyntax PgCommandSyntax = PgSelectSyntax
  type Sql92InsertSyntax PgCommandSyntax = PgInsertSyntax
  type Sql92UpdateSyntax PgCommandSyntax = PgUpdateSyntax
  type Sql92DeleteSyntax PgCommandSyntax = PgDeleteSyntax

  selectCmd :: Sql92SelectSyntax PgCommandSyntax -> PgCommandSyntax
selectCmd = PgCommandType -> PgSyntax -> PgCommandSyntax
PgCommandSyntax PgCommandType
PgCommandTypeQuery      (PgSyntax -> PgCommandSyntax)
-> (PgSelectSyntax -> PgSyntax)
-> PgSelectSyntax
-> PgCommandSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSelectSyntax -> PgSyntax
coerce
  insertCmd :: Sql92InsertSyntax PgCommandSyntax -> PgCommandSyntax
insertCmd = PgCommandType -> PgSyntax -> PgCommandSyntax
PgCommandSyntax PgCommandType
PgCommandTypeDataUpdate (PgSyntax -> PgCommandSyntax)
-> (PgInsertSyntax -> PgSyntax)
-> PgInsertSyntax
-> PgCommandSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgInsertSyntax -> PgSyntax
coerce
  deleteCmd :: Sql92DeleteSyntax PgCommandSyntax -> PgCommandSyntax
deleteCmd = PgCommandType -> PgSyntax -> PgCommandSyntax
PgCommandSyntax PgCommandType
PgCommandTypeDataUpdate (PgSyntax -> PgCommandSyntax)
-> (PgDeleteSyntax -> PgSyntax)
-> PgDeleteSyntax
-> PgCommandSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgDeleteSyntax -> PgSyntax
coerce
  updateCmd :: Sql92UpdateSyntax PgCommandSyntax -> PgCommandSyntax
updateCmd = PgCommandType -> PgSyntax -> PgCommandSyntax
PgCommandSyntax PgCommandType
PgCommandTypeDataUpdate (PgSyntax -> PgCommandSyntax)
-> (PgUpdateSyntax -> PgSyntax)
-> PgUpdateSyntax
-> PgCommandSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgUpdateSyntax -> PgSyntax
coerce

instance IsSql92DdlCommandSyntax PgCommandSyntax where
  type Sql92DdlCommandCreateTableSyntax PgCommandSyntax = PgCreateTableSyntax
  type Sql92DdlCommandDropTableSyntax PgCommandSyntax = PgDropTableSyntax
  type Sql92DdlCommandAlterTableSyntax PgCommandSyntax = PgAlterTableSyntax

  createTableCmd :: Sql92DdlCommandCreateTableSyntax PgCommandSyntax -> PgCommandSyntax
createTableCmd = PgCommandType -> PgSyntax -> PgCommandSyntax
PgCommandSyntax PgCommandType
PgCommandTypeDdl (PgSyntax -> PgCommandSyntax)
-> (PgCreateTableSyntax -> PgSyntax)
-> PgCreateTableSyntax
-> PgCommandSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgCreateTableSyntax -> PgSyntax
coerce
  dropTableCmd :: Sql92DdlCommandDropTableSyntax PgCommandSyntax -> PgCommandSyntax
dropTableCmd   = PgCommandType -> PgSyntax -> PgCommandSyntax
PgCommandSyntax PgCommandType
PgCommandTypeDdl (PgSyntax -> PgCommandSyntax)
-> (PgDropTableSyntax -> PgSyntax)
-> PgDropTableSyntax
-> PgCommandSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgDropTableSyntax -> PgSyntax
coerce
  alterTableCmd :: Sql92DdlCommandAlterTableSyntax PgCommandSyntax -> PgCommandSyntax
alterTableCmd  = PgCommandType -> PgSyntax -> PgCommandSyntax
PgCommandSyntax PgCommandType
PgCommandTypeDdl (PgSyntax -> PgCommandSyntax)
-> (PgAlterTableSyntax -> PgSyntax)
-> PgAlterTableSyntax
-> PgCommandSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgAlterTableSyntax -> PgSyntax
coerce

instance IsSql92TableNameSyntax PgTableNameSyntax where
  tableName :: Maybe Text -> Text -> PgTableNameSyntax
tableName Maybe Text
Nothing Text
t = PgSyntax -> PgTableNameSyntax
PgTableNameSyntax (Text -> PgSyntax
pgQuotedIdentifier Text
t)
  tableName (Just Text
s) Text
t = PgSyntax -> PgTableNameSyntax
PgTableNameSyntax (Text -> PgSyntax
pgQuotedIdentifier Text
s PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
"." PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> Text -> PgSyntax
pgQuotedIdentifier Text
t)

instance IsSql92UpdateSyntax PgUpdateSyntax where
  type Sql92UpdateFieldNameSyntax PgUpdateSyntax = PgFieldNameSyntax
  type Sql92UpdateExpressionSyntax PgUpdateSyntax = PgExpressionSyntax
  type Sql92UpdateTableNameSyntax PgUpdateSyntax = PgTableNameSyntax

  updateStmt :: Sql92UpdateTableNameSyntax PgUpdateSyntax
-> [(Sql92UpdateFieldNameSyntax PgUpdateSyntax,
     Sql92UpdateExpressionSyntax PgUpdateSyntax)]
-> Maybe (Sql92UpdateExpressionSyntax PgUpdateSyntax)
-> PgUpdateSyntax
updateStmt Sql92UpdateTableNameSyntax PgUpdateSyntax
tbl [(Sql92UpdateFieldNameSyntax PgUpdateSyntax,
  Sql92UpdateExpressionSyntax PgUpdateSyntax)]
fields Maybe (Sql92UpdateExpressionSyntax PgUpdateSyntax)
where_ =
    PgSyntax -> PgUpdateSyntax
PgUpdateSyntax (PgSyntax -> PgUpdateSyntax) -> PgSyntax -> PgUpdateSyntax
forall a b. (a -> b) -> a -> b
$
    ByteString -> PgSyntax
emit ByteString
"UPDATE " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgTableNameSyntax -> PgSyntax
fromPgTableName Sql92UpdateTableNameSyntax PgUpdateSyntax
PgTableNameSyntax
tbl PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
    (case [(Sql92UpdateFieldNameSyntax PgUpdateSyntax,
  Sql92UpdateExpressionSyntax PgUpdateSyntax)]
fields of
       [] -> PgSyntax
forall a. Monoid a => a
mempty
       [(Sql92UpdateFieldNameSyntax PgUpdateSyntax,
  Sql92UpdateExpressionSyntax PgUpdateSyntax)]
fields ->
         ByteString -> PgSyntax
emit ByteString
" SET " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
         PgSyntax -> [PgSyntax] -> PgSyntax
pgSepBy (ByteString -> PgSyntax
emit ByteString
", ") (((PgFieldNameSyntax, PgExpressionSyntax) -> PgSyntax)
-> [(PgFieldNameSyntax, PgExpressionSyntax)] -> [PgSyntax]
forall a b. (a -> b) -> [a] -> [b]
map (\(PgFieldNameSyntax
field, PgExpressionSyntax
val) -> PgFieldNameSyntax -> PgSyntax
fromPgFieldName PgFieldNameSyntax
field 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
val) [(Sql92UpdateFieldNameSyntax PgUpdateSyntax,
  Sql92UpdateExpressionSyntax PgUpdateSyntax)]
[(PgFieldNameSyntax, PgExpressionSyntax)]
fields)) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
    PgSyntax
-> (PgExpressionSyntax -> PgSyntax)
-> Maybe PgExpressionSyntax
-> PgSyntax
forall b a. b -> (a -> b) -> Maybe a -> b
maybe PgSyntax
forall a. Monoid a => a
mempty (\PgExpressionSyntax
where_ -> ByteString -> PgSyntax
emit ByteString
" WHERE " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
where_) Maybe (Sql92UpdateExpressionSyntax PgUpdateSyntax)
Maybe PgExpressionSyntax
where_

instance IsSql92DeleteSyntax PgDeleteSyntax where
  type Sql92DeleteExpressionSyntax PgDeleteSyntax = PgExpressionSyntax
  type Sql92DeleteTableNameSyntax PgDeleteSyntax = PgTableNameSyntax

  deleteStmt :: Sql92DeleteTableNameSyntax PgDeleteSyntax
-> Maybe Text
-> Maybe (Sql92DeleteExpressionSyntax PgDeleteSyntax)
-> PgDeleteSyntax
deleteStmt Sql92DeleteTableNameSyntax PgDeleteSyntax
tbl Maybe Text
alias Maybe (Sql92DeleteExpressionSyntax PgDeleteSyntax)
where_ =
    PgSyntax -> PgDeleteSyntax
PgDeleteSyntax (PgSyntax -> PgDeleteSyntax) -> PgSyntax -> PgDeleteSyntax
forall a b. (a -> b) -> a -> b
$
    ByteString -> PgSyntax
emit ByteString
"DELETE FROM " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgTableNameSyntax -> PgSyntax
fromPgTableName Sql92DeleteTableNameSyntax PgDeleteSyntax
PgTableNameSyntax
tbl PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
    PgSyntax -> (Text -> PgSyntax) -> Maybe Text -> PgSyntax
forall b a. b -> (a -> b) -> Maybe a -> b
maybe PgSyntax
forall a. Monoid a => a
mempty (\Text
alias_ -> ByteString -> PgSyntax
emit ByteString
" AS " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> Text -> PgSyntax
pgQuotedIdentifier Text
alias_) Maybe Text
alias PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
    PgSyntax
-> (PgExpressionSyntax -> PgSyntax)
-> Maybe PgExpressionSyntax
-> PgSyntax
forall b a. b -> (a -> b) -> Maybe a -> b
maybe PgSyntax
forall a. Monoid a => a
mempty (\PgExpressionSyntax
where_ -> ByteString -> PgSyntax
emit ByteString
" WHERE " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
where_) Maybe (Sql92DeleteExpressionSyntax PgDeleteSyntax)
Maybe PgExpressionSyntax
where_

  deleteSupportsAlias :: Proxy PgDeleteSyntax -> Bool
deleteSupportsAlias Proxy PgDeleteSyntax
_ = Bool
True

instance IsSql92SelectSyntax PgSelectSyntax where
  type Sql92SelectSelectTableSyntax PgSelectSyntax = PgSelectTableSyntax
  type Sql92SelectOrderingSyntax PgSelectSyntax = PgOrderingSyntax

  selectStmt :: Sql92SelectSelectTableSyntax PgSelectSyntax
-> [Sql92SelectOrderingSyntax PgSelectSyntax]
-> Maybe Integer
-> Maybe Integer
-> PgSelectSyntax
selectStmt Sql92SelectSelectTableSyntax PgSelectSyntax
tbl [Sql92SelectOrderingSyntax PgSelectSyntax]
ordering Maybe Integer
limit Maybe Integer
offset =
    PgSelectTableSyntax
-> [PgOrderingSyntax]
-> Maybe Integer
-> Maybe Integer
-> Maybe PgSelectLockingClauseSyntax
-> PgSelectSyntax
pgSelectStmt Sql92SelectSelectTableSyntax PgSelectSyntax
PgSelectTableSyntax
tbl [Sql92SelectOrderingSyntax PgSelectSyntax]
[PgOrderingSyntax]
ordering Maybe Integer
limit Maybe Integer
offset Maybe PgSelectLockingClauseSyntax
forall a. Maybe a
Nothing

instance IsSql92SelectTableSyntax PgSelectTableSyntax where
  type Sql92SelectTableSelectSyntax PgSelectTableSyntax = PgSelectSyntax
  type Sql92SelectTableExpressionSyntax PgSelectTableSyntax = PgExpressionSyntax
  type Sql92SelectTableProjectionSyntax PgSelectTableSyntax = PgProjectionSyntax
  type Sql92SelectTableFromSyntax PgSelectTableSyntax = PgFromSyntax
  type Sql92SelectTableGroupingSyntax PgSelectTableSyntax = PgGroupingSyntax
  type Sql92SelectTableSetQuantifierSyntax PgSelectTableSyntax = PgSelectSetQuantifierSyntax

  selectTableStmt :: Maybe (Sql92SelectTableSetQuantifierSyntax PgSelectTableSyntax)
-> Sql92SelectTableProjectionSyntax PgSelectTableSyntax
-> Maybe (Sql92SelectTableFromSyntax PgSelectTableSyntax)
-> Maybe (Sql92SelectTableExpressionSyntax PgSelectTableSyntax)
-> Maybe (Sql92SelectTableGroupingSyntax PgSelectTableSyntax)
-> Maybe (Sql92SelectTableExpressionSyntax PgSelectTableSyntax)
-> PgSelectTableSyntax
selectTableStmt Maybe (Sql92SelectTableSetQuantifierSyntax PgSelectTableSyntax)
setQuantifier Sql92SelectTableProjectionSyntax PgSelectTableSyntax
proj Maybe (Sql92SelectTableFromSyntax PgSelectTableSyntax)
from Maybe (Sql92SelectTableExpressionSyntax PgSelectTableSyntax)
where_ Maybe (Sql92SelectTableGroupingSyntax PgSelectTableSyntax)
grouping Maybe (Sql92SelectTableExpressionSyntax PgSelectTableSyntax)
having =
    PgSyntax -> PgSelectTableSyntax
PgSelectTableSyntax (PgSyntax -> PgSelectTableSyntax)
-> PgSyntax -> PgSelectTableSyntax
forall a b. (a -> b) -> a -> b
$
    ByteString -> PgSyntax
emit ByteString
"SELECT " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
    PgSyntax
-> (PgSelectSetQuantifierSyntax -> PgSyntax)
-> Maybe PgSelectSetQuantifierSyntax
-> PgSyntax
forall b a. b -> (a -> b) -> Maybe a -> b
maybe PgSyntax
forall a. Monoid a => a
mempty (\PgSelectSetQuantifierSyntax
setQuantifier' -> PgSelectSetQuantifierSyntax -> PgSyntax
fromPgSelectSetQuantifier PgSelectSetQuantifierSyntax
setQuantifier' PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
" ") Maybe (Sql92SelectTableSetQuantifierSyntax PgSelectTableSyntax)
Maybe PgSelectSetQuantifierSyntax
setQuantifier PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
    PgProjectionSyntax -> PgSyntax
fromPgProjection Sql92SelectTableProjectionSyntax PgSelectTableSyntax
PgProjectionSyntax
proj PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
    (PgSyntax -> (PgSyntax -> PgSyntax) -> Maybe PgSyntax -> PgSyntax
forall b a. b -> (a -> b) -> Maybe a -> b
maybe PgSyntax
forall a. Monoid a => a
mempty (ByteString -> PgSyntax
emit ByteString
" FROM " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ) (Maybe PgFromSyntax -> Maybe PgSyntax
coerce Maybe (Sql92SelectTableFromSyntax PgSelectTableSyntax)
Maybe PgFromSyntax
from)) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
    (PgSyntax -> (PgSyntax -> PgSyntax) -> Maybe PgSyntax -> PgSyntax
forall b a. b -> (a -> b) -> Maybe a -> b
maybe PgSyntax
forall a. Monoid a => a
mempty (ByteString -> PgSyntax
emit ByteString
" WHERE " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>) (Maybe PgExpressionSyntax -> Maybe PgSyntax
coerce Maybe (Sql92SelectTableExpressionSyntax PgSelectTableSyntax)
Maybe PgExpressionSyntax
where_)) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
    (PgSyntax -> (PgSyntax -> PgSyntax) -> Maybe PgSyntax -> PgSyntax
forall b a. b -> (a -> b) -> Maybe a -> b
maybe PgSyntax
forall a. Monoid a => a
mempty (ByteString -> PgSyntax
emit ByteString
" GROUP BY " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>) (Maybe PgGroupingSyntax -> Maybe PgSyntax
coerce Maybe (Sql92SelectTableGroupingSyntax PgSelectTableSyntax)
Maybe PgGroupingSyntax
grouping)) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
    (PgSyntax -> (PgSyntax -> PgSyntax) -> Maybe PgSyntax -> PgSyntax
forall b a. b -> (a -> b) -> Maybe a -> b
maybe PgSyntax
forall a. Monoid a => a
mempty (ByteString -> PgSyntax
emit ByteString
" HAVING " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>) (Maybe PgExpressionSyntax -> Maybe PgSyntax
coerce Maybe (Sql92SelectTableExpressionSyntax PgSelectTableSyntax)
Maybe PgExpressionSyntax
having))

  unionTables :: Bool
-> PgSelectTableSyntax
-> PgSelectTableSyntax
-> PgSelectTableSyntax
unionTables Bool
all = ByteString
-> PgSelectTableSyntax
-> PgSelectTableSyntax
-> PgSelectTableSyntax
pgTableOp (if Bool
all then ByteString
"UNION ALL" else ByteString
"UNION")
  intersectTables :: Bool
-> PgSelectTableSyntax
-> PgSelectTableSyntax
-> PgSelectTableSyntax
intersectTables Bool
all = ByteString
-> PgSelectTableSyntax
-> PgSelectTableSyntax
-> PgSelectTableSyntax
pgTableOp (if Bool
all then ByteString
"INTERSECT ALL" else ByteString
"INTERSECT")
  exceptTable :: Bool
-> PgSelectTableSyntax
-> PgSelectTableSyntax
-> PgSelectTableSyntax
exceptTable Bool
all = ByteString
-> PgSelectTableSyntax
-> PgSelectTableSyntax
-> PgSelectTableSyntax
pgTableOp (if Bool
all then ByteString
"EXCEPT ALL" else ByteString
"EXCEPT")

instance IsSql92GroupingSyntax PgGroupingSyntax where
  type Sql92GroupingExpressionSyntax PgGroupingSyntax = PgExpressionSyntax

  groupByExpressions :: [Sql92GroupingExpressionSyntax PgGroupingSyntax]
-> PgGroupingSyntax
groupByExpressions [Sql92GroupingExpressionSyntax PgGroupingSyntax]
es =
      PgSyntax -> PgGroupingSyntax
PgGroupingSyntax (PgSyntax -> PgGroupingSyntax) -> PgSyntax -> PgGroupingSyntax
forall a b. (a -> b) -> a -> b
$
      PgSyntax -> [PgSyntax] -> PgSyntax
pgSepBy (ByteString -> PgSyntax
emit ByteString
", ") ((PgExpressionSyntax -> PgSyntax)
-> [PgExpressionSyntax] -> [PgSyntax]
forall a b. (a -> b) -> [a] -> [b]
map PgExpressionSyntax -> PgSyntax
fromPgExpression [Sql92GroupingExpressionSyntax PgGroupingSyntax]
[PgExpressionSyntax]
es)

instance IsSql92FromSyntax PgFromSyntax where
  type Sql92FromExpressionSyntax PgFromSyntax = PgExpressionSyntax
  type Sql92FromTableSourceSyntax PgFromSyntax = PgTableSourceSyntax

  fromTable :: Sql92FromTableSourceSyntax PgFromSyntax
-> Maybe (Text, Maybe [Text]) -> PgFromSyntax
fromTable Sql92FromTableSourceSyntax PgFromSyntax
tableSrc Maybe (Text, Maybe [Text])
Nothing = PgTableSourceSyntax -> PgFromSyntax
coerce Sql92FromTableSourceSyntax PgFromSyntax
PgTableSourceSyntax
tableSrc
  fromTable Sql92FromTableSourceSyntax PgFromSyntax
tableSrc (Just (Text
nm, Maybe [Text]
colNms)) =
      PgSyntax -> PgFromSyntax
PgFromSyntax (PgSyntax -> PgFromSyntax) -> PgSyntax -> PgFromSyntax
forall a b. (a -> b) -> a -> b
$
      PgTableSourceSyntax -> PgSyntax
coerce Sql92FromTableSourceSyntax PgFromSyntax
PgTableSourceSyntax
tableSrc PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
" AS " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> Text -> PgSyntax
pgQuotedIdentifier Text
nm PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
      PgSyntax -> ([Text] -> PgSyntax) -> Maybe [Text] -> PgSyntax
forall b a. b -> (a -> b) -> Maybe a -> b
maybe PgSyntax
forall a. Monoid a => a
mempty (\[Text]
colNms' -> PgSyntax -> PgSyntax
pgParens (PgSyntax -> [PgSyntax] -> PgSyntax
pgSepBy (ByteString -> PgSyntax
emit ByteString
",") ((Text -> PgSyntax) -> [Text] -> [PgSyntax]
forall a b. (a -> b) -> [a] -> [b]
map Text -> PgSyntax
pgQuotedIdentifier [Text]
colNms'))) Maybe [Text]
colNms

  innerJoin :: PgFromSyntax
-> PgFromSyntax
-> Maybe (Sql92FromExpressionSyntax PgFromSyntax)
-> PgFromSyntax
innerJoin PgFromSyntax
a PgFromSyntax
b Maybe (Sql92FromExpressionSyntax PgFromSyntax)
Nothing = PgSyntax -> PgFromSyntax
PgFromSyntax (PgFromSyntax -> PgSyntax
fromPgFrom PgFromSyntax
a PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
" CROSS JOIN " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgFromSyntax -> PgSyntax
fromPgFrom PgFromSyntax
b)
  innerJoin PgFromSyntax
a PgFromSyntax
b (Just Sql92FromExpressionSyntax PgFromSyntax
e) = ByteString
-> PgFromSyntax
-> PgFromSyntax
-> Maybe PgExpressionSyntax
-> PgFromSyntax
pgJoin ByteString
"INNER JOIN" PgFromSyntax
a PgFromSyntax
b (PgExpressionSyntax -> Maybe PgExpressionSyntax
forall a. a -> Maybe a
Just Sql92FromExpressionSyntax PgFromSyntax
PgExpressionSyntax
e)

  leftJoin :: PgFromSyntax
-> PgFromSyntax
-> Maybe (Sql92FromExpressionSyntax PgFromSyntax)
-> PgFromSyntax
leftJoin = ByteString
-> PgFromSyntax
-> PgFromSyntax
-> Maybe PgExpressionSyntax
-> PgFromSyntax
pgJoin ByteString
"LEFT JOIN"
  rightJoin :: PgFromSyntax
-> PgFromSyntax
-> Maybe (Sql92FromExpressionSyntax PgFromSyntax)
-> PgFromSyntax
rightJoin = ByteString
-> PgFromSyntax
-> PgFromSyntax
-> Maybe PgExpressionSyntax
-> PgFromSyntax
pgJoin ByteString
"RIGHT JOIN"

instance IsSql92FromOuterJoinSyntax PgFromSyntax where
  outerJoin :: PgFromSyntax
-> PgFromSyntax
-> Maybe (Sql92FromExpressionSyntax PgFromSyntax)
-> PgFromSyntax
outerJoin = ByteString
-> PgFromSyntax
-> PgFromSyntax
-> Maybe PgExpressionSyntax
-> PgFromSyntax
pgJoin ByteString
"FULL OUTER JOIN"

instance IsSql92OrderingSyntax PgOrderingSyntax where
  type Sql92OrderingExpressionSyntax PgOrderingSyntax = PgExpressionSyntax

  ascOrdering :: Sql92OrderingExpressionSyntax PgOrderingSyntax -> PgOrderingSyntax
ascOrdering Sql92OrderingExpressionSyntax PgOrderingSyntax
e = PgSyntax -> Maybe PgNullOrdering -> PgOrderingSyntax
PgOrderingSyntax (PgExpressionSyntax -> PgSyntax
fromPgExpression Sql92OrderingExpressionSyntax PgOrderingSyntax
PgExpressionSyntax
e PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
" ASC") Maybe PgNullOrdering
forall a. Maybe a
Nothing
  descOrdering :: Sql92OrderingExpressionSyntax PgOrderingSyntax -> PgOrderingSyntax
descOrdering Sql92OrderingExpressionSyntax PgOrderingSyntax
e = PgSyntax -> Maybe PgNullOrdering -> PgOrderingSyntax
PgOrderingSyntax (PgExpressionSyntax -> PgSyntax
fromPgExpression Sql92OrderingExpressionSyntax PgOrderingSyntax
PgExpressionSyntax
e PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
" DESC") Maybe PgNullOrdering
forall a. Maybe a
Nothing

instance IsSql2003OrderingElementaryOLAPOperationsSyntax PgOrderingSyntax where
  nullsFirstOrdering :: PgOrderingSyntax -> PgOrderingSyntax
nullsFirstOrdering PgOrderingSyntax
o = PgOrderingSyntax
o { pgOrderingNullOrdering :: Maybe PgNullOrdering
pgOrderingNullOrdering = PgNullOrdering -> Maybe PgNullOrdering
forall a. a -> Maybe a
Just PgNullOrdering
PgNullOrderingNullsFirst }
  nullsLastOrdering :: PgOrderingSyntax -> PgOrderingSyntax
nullsLastOrdering PgOrderingSyntax
o = PgOrderingSyntax
o { pgOrderingNullOrdering :: Maybe PgNullOrdering
pgOrderingNullOrdering = PgNullOrdering -> Maybe PgNullOrdering
forall a. a -> Maybe a
Just PgNullOrdering
PgNullOrderingNullsLast }

instance IsSql92DataTypeSyntax PgDataTypeSyntax where
  domainType :: Text -> PgDataTypeSyntax
domainType Text
nm = PgDataTypeDescr
-> PgSyntax -> BeamSerializedDataType -> PgDataTypeSyntax
PgDataTypeSyntax (Text -> PgDataTypeDescr
PgDataTypeDescrDomain Text
nm) (Text -> PgSyntax
pgQuotedIdentifier Text
nm)
                                   (Text -> BeamSerializedDataType
forall dataType. IsSql92DataTypeSyntax dataType => Text -> dataType
domainType Text
nm)

  charType :: Maybe Word -> Maybe Text -> PgDataTypeSyntax
charType Maybe Word
prec Maybe Text
charSet = PgDataTypeDescr
-> PgSyntax -> BeamSerializedDataType -> PgDataTypeSyntax
PgDataTypeSyntax (Oid -> Maybe Int32 -> PgDataTypeDescr
PgDataTypeDescrOid (TypeInfo -> Oid
Pg.typoid TypeInfo
Pg.bpchar) (Int32 -> Maybe Int32
forall a. a -> Maybe a
Just (Word -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word -> Maybe Word -> Word
forall a. a -> Maybe a -> a
fromMaybe Word
1 Maybe Word
prec))))
                                           (ByteString -> PgSyntax
emit ByteString
"CHAR" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> Maybe Word -> PgSyntax
pgOptPrec Maybe Word
prec PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> Maybe Text -> PgSyntax
pgOptCharSet Maybe Text
charSet)
                                           (Maybe Word -> Maybe Text -> BeamSerializedDataType
forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe Word -> Maybe Text -> dataType
charType Maybe Word
prec Maybe Text
charSet)
  varCharType :: Maybe Word -> Maybe Text -> PgDataTypeSyntax
varCharType Maybe Word
prec Maybe Text
charSet = PgDataTypeDescr
-> PgSyntax -> BeamSerializedDataType -> PgDataTypeSyntax
PgDataTypeSyntax (Oid -> Maybe Int32 -> PgDataTypeDescr
PgDataTypeDescrOid (TypeInfo -> Oid
Pg.typoid TypeInfo
Pg.varchar) ((Word -> Int32) -> Maybe Word -> Maybe Int32
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Word -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Maybe Word
prec))
                                              (ByteString -> PgSyntax
emit ByteString
"VARCHAR" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> Maybe Word -> PgSyntax
pgOptPrec Maybe Word
prec PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> Maybe Text -> PgSyntax
pgOptCharSet Maybe Text
charSet)
                                              (Maybe Word -> Maybe Text -> BeamSerializedDataType
forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe Word -> Maybe Text -> dataType
varCharType Maybe Word
prec Maybe Text
charSet)
  nationalCharType :: Maybe Word -> PgDataTypeSyntax
nationalCharType Maybe Word
prec = PgDataTypeDescr
-> PgSyntax -> BeamSerializedDataType -> PgDataTypeSyntax
PgDataTypeSyntax (Oid -> Maybe Int32 -> PgDataTypeDescr
PgDataTypeDescrOid (TypeInfo -> Oid
Pg.typoid TypeInfo
Pg.bpchar) ((Word -> Int32) -> Maybe Word -> Maybe Int32
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Word -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Maybe Word
prec))
                                           (ByteString -> PgSyntax
emit ByteString
"NATIONAL CHAR" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> Maybe Word -> PgSyntax
pgOptPrec Maybe Word
prec)
                                           (Maybe Word -> BeamSerializedDataType
forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe Word -> dataType
nationalCharType Maybe Word
prec)
  nationalVarCharType :: Maybe Word -> PgDataTypeSyntax
nationalVarCharType Maybe Word
prec = PgDataTypeDescr
-> PgSyntax -> BeamSerializedDataType -> PgDataTypeSyntax
PgDataTypeSyntax (Oid -> Maybe Int32 -> PgDataTypeDescr
PgDataTypeDescrOid (TypeInfo -> Oid
Pg.typoid TypeInfo
Pg.varchar) ((Word -> Int32) -> Maybe Word -> Maybe Int32
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Word -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Maybe Word
prec))
                                              (ByteString -> PgSyntax
emit ByteString
"NATIONAL CHARACTER VARYING" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> Maybe Word -> PgSyntax
pgOptPrec Maybe Word
prec)
                                              (Maybe Word -> BeamSerializedDataType
forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe Word -> dataType
nationalVarCharType Maybe Word
prec)

  bitType :: Maybe Word -> PgDataTypeSyntax
bitType Maybe Word
prec = PgDataTypeDescr
-> PgSyntax -> BeamSerializedDataType -> PgDataTypeSyntax
PgDataTypeSyntax (Oid -> Maybe Int32 -> PgDataTypeDescr
PgDataTypeDescrOid (TypeInfo -> Oid
Pg.typoid TypeInfo
Pg.bit) ((Word -> Int32) -> Maybe Word -> Maybe Int32
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Word -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Maybe Word
prec))
                                  (ByteString -> PgSyntax
emit ByteString
"BIT" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> Maybe Word -> PgSyntax
pgOptPrec Maybe Word
prec)
                                  (Maybe Word -> BeamSerializedDataType
forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe Word -> dataType
bitType Maybe Word
prec)
  varBitType :: Maybe Word -> PgDataTypeSyntax
varBitType Maybe Word
prec = PgDataTypeDescr
-> PgSyntax -> BeamSerializedDataType -> PgDataTypeSyntax
PgDataTypeSyntax (Oid -> Maybe Int32 -> PgDataTypeDescr
PgDataTypeDescrOid (TypeInfo -> Oid
Pg.typoid TypeInfo
Pg.varbit) ((Word -> Int32) -> Maybe Word -> Maybe Int32
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Word -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Maybe Word
prec))
                                     (ByteString -> PgSyntax
emit ByteString
"BIT VARYING" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> Maybe Word -> PgSyntax
pgOptPrec Maybe Word
prec)
                                     (Maybe Word -> BeamSerializedDataType
forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe Word -> dataType
varBitType Maybe Word
prec)

  numericType :: Maybe (Word, Maybe Word) -> PgDataTypeSyntax
numericType Maybe (Word, Maybe Word)
prec = PgDataTypeDescr
-> PgSyntax -> BeamSerializedDataType -> PgDataTypeSyntax
PgDataTypeSyntax (Oid -> Maybe Int32 -> PgDataTypeDescr
PgDataTypeDescrOid (TypeInfo -> Oid
Pg.typoid TypeInfo
Pg.numeric) (Maybe (Word, Maybe Word) -> Maybe Int32
mkNumericPrec Maybe (Word, Maybe Word)
prec))
                                      (ByteString -> PgSyntax
emit ByteString
"NUMERIC" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> Maybe (Word, Maybe Word) -> PgSyntax
pgOptNumericPrec Maybe (Word, Maybe Word)
prec)
                                      (Maybe (Word, Maybe Word) -> BeamSerializedDataType
forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe (Word, Maybe Word) -> dataType
numericType Maybe (Word, Maybe Word)
prec)
  decimalType :: Maybe (Word, Maybe Word) -> PgDataTypeSyntax
decimalType Maybe (Word, Maybe Word)
prec = PgDataTypeDescr
-> PgSyntax -> BeamSerializedDataType -> PgDataTypeSyntax
PgDataTypeSyntax (Oid -> Maybe Int32 -> PgDataTypeDescr
PgDataTypeDescrOid (TypeInfo -> Oid
Pg.typoid TypeInfo
Pg.numeric) (Maybe (Word, Maybe Word) -> Maybe Int32
mkNumericPrec Maybe (Word, Maybe Word)
prec))
                                      (ByteString -> PgSyntax
emit ByteString
"DECIMAL" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> Maybe (Word, Maybe Word) -> PgSyntax
pgOptNumericPrec Maybe (Word, Maybe Word)
prec)
                                      (Maybe (Word, Maybe Word) -> BeamSerializedDataType
forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe (Word, Maybe Word) -> dataType
decimalType Maybe (Word, Maybe Word)
prec)

  intType :: PgDataTypeSyntax
intType = PgDataTypeDescr
-> PgSyntax -> BeamSerializedDataType -> PgDataTypeSyntax
PgDataTypeSyntax (Oid -> Maybe Int32 -> PgDataTypeDescr
PgDataTypeDescrOid (TypeInfo -> Oid
Pg.typoid TypeInfo
Pg.int4) Maybe Int32
forall a. Maybe a
Nothing) (ByteString -> PgSyntax
emit ByteString
"INT") BeamSerializedDataType
forall dataType. IsSql92DataTypeSyntax dataType => dataType
intType
  smallIntType :: PgDataTypeSyntax
smallIntType = PgDataTypeDescr
-> PgSyntax -> BeamSerializedDataType -> PgDataTypeSyntax
PgDataTypeSyntax (Oid -> Maybe Int32 -> PgDataTypeDescr
PgDataTypeDescrOid (TypeInfo -> Oid
Pg.typoid TypeInfo
Pg.int2) Maybe Int32
forall a. Maybe a
Nothing) (ByteString -> PgSyntax
emit ByteString
"SMALLINT") BeamSerializedDataType
forall dataType. IsSql92DataTypeSyntax dataType => dataType
smallIntType

  floatType :: Maybe Word -> PgDataTypeSyntax
floatType Maybe Word
prec = PgDataTypeDescr
-> PgSyntax -> BeamSerializedDataType -> PgDataTypeSyntax
PgDataTypeSyntax (Oid -> Maybe Int32 -> PgDataTypeDescr
PgDataTypeDescrOid (TypeInfo -> Oid
Pg.typoid TypeInfo
Pg.float4) Maybe Int32
forall a. Maybe a
Nothing) (ByteString -> PgSyntax
emit ByteString
"FLOAT" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> Maybe Word -> PgSyntax
pgOptPrec Maybe Word
prec)
                                    (Maybe Word -> BeamSerializedDataType
forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe Word -> dataType
floatType Maybe Word
prec)
  doubleType :: PgDataTypeSyntax
doubleType = PgDataTypeDescr
-> PgSyntax -> BeamSerializedDataType -> PgDataTypeSyntax
PgDataTypeSyntax (Oid -> Maybe Int32 -> PgDataTypeDescr
PgDataTypeDescrOid (TypeInfo -> Oid
Pg.typoid TypeInfo
Pg.float8) Maybe Int32
forall a. Maybe a
Nothing) (ByteString -> PgSyntax
emit ByteString
"DOUBLE PRECISION") BeamSerializedDataType
forall dataType. IsSql92DataTypeSyntax dataType => dataType
doubleType
  realType :: PgDataTypeSyntax
realType = PgDataTypeDescr
-> PgSyntax -> BeamSerializedDataType -> PgDataTypeSyntax
PgDataTypeSyntax (Oid -> Maybe Int32 -> PgDataTypeDescr
PgDataTypeDescrOid (TypeInfo -> Oid
Pg.typoid TypeInfo
Pg.float4) Maybe Int32
forall a. Maybe a
Nothing) (ByteString -> PgSyntax
emit ByteString
"REAL") BeamSerializedDataType
forall dataType. IsSql92DataTypeSyntax dataType => dataType
realType
  dateType :: PgDataTypeSyntax
dateType = PgDataTypeDescr
-> PgSyntax -> BeamSerializedDataType -> PgDataTypeSyntax
PgDataTypeSyntax (Oid -> Maybe Int32 -> PgDataTypeDescr
PgDataTypeDescrOid (TypeInfo -> Oid
Pg.typoid TypeInfo
Pg.date) Maybe Int32
forall a. Maybe a
Nothing) (ByteString -> PgSyntax
emit ByteString
"DATE") BeamSerializedDataType
forall dataType. IsSql92DataTypeSyntax dataType => dataType
dateType
  timeType :: Maybe Word -> Bool -> PgDataTypeSyntax
timeType Maybe Word
prec Bool
withTz = PgDataTypeDescr
-> PgSyntax -> BeamSerializedDataType -> PgDataTypeSyntax
PgDataTypeSyntax (Oid -> Maybe Int32 -> PgDataTypeDescr
PgDataTypeDescrOid (TypeInfo -> Oid
Pg.typoid TypeInfo
Pg.time) Maybe Int32
forall a. Maybe a
Nothing)
                                          (ByteString -> PgSyntax
emit ByteString
"TIME" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> Maybe Word -> PgSyntax
pgOptPrec Maybe Word
prec PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> if Bool
withTz then ByteString -> PgSyntax
emit ByteString
" WITH TIME ZONE" else PgSyntax
forall a. Monoid a => a
mempty)
                                          (Maybe Word -> Bool -> BeamSerializedDataType
forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe Word -> Bool -> dataType
timeType Maybe Word
prec Bool
withTz)
  timestampType :: Maybe Word -> Bool -> PgDataTypeSyntax
timestampType Maybe Word
prec Bool
withTz = PgDataTypeDescr
-> PgSyntax -> BeamSerializedDataType -> PgDataTypeSyntax
PgDataTypeSyntax (Oid -> Maybe Int32 -> PgDataTypeDescr
PgDataTypeDescrOid (TypeInfo -> Oid
Pg.typoid (if Bool
withTz then TypeInfo
Pg.timestamptz else TypeInfo
Pg.timestamp)) Maybe Int32
forall a. Maybe a
Nothing)
                                               (ByteString -> PgSyntax
emit ByteString
"TIMESTAMP" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> Maybe Word -> PgSyntax
pgOptPrec Maybe Word
prec PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> if Bool
withTz then ByteString -> PgSyntax
emit ByteString
" WITH TIME ZONE" else PgSyntax
forall a. Monoid a => a
mempty)
                                               (Maybe Word -> Bool -> BeamSerializedDataType
forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe Word -> Bool -> dataType
timestampType Maybe Word
prec Bool
withTz)

instance IsSql99DataTypeSyntax PgDataTypeSyntax where
  characterLargeObjectType :: PgDataTypeSyntax
characterLargeObjectType = PgDataTypeSyntax
pgTextType { pgDataTypeSerialized :: BeamSerializedDataType
pgDataTypeSerialized = BeamSerializedDataType
forall dataType. IsSql99DataTypeSyntax dataType => dataType
characterLargeObjectType }
  binaryLargeObjectType :: PgDataTypeSyntax
binaryLargeObjectType = PgDataTypeSyntax
pgByteaType { pgDataTypeSerialized :: BeamSerializedDataType
pgDataTypeSerialized = BeamSerializedDataType
forall dataType. IsSql99DataTypeSyntax dataType => dataType
binaryLargeObjectType }
  booleanType :: PgDataTypeSyntax
booleanType = PgDataTypeDescr
-> PgSyntax -> BeamSerializedDataType -> PgDataTypeSyntax
PgDataTypeSyntax (Oid -> Maybe Int32 -> PgDataTypeDescr
PgDataTypeDescrOid (TypeInfo -> Oid
Pg.typoid TypeInfo
Pg.bool) Maybe Int32
forall a. Maybe a
Nothing) (ByteString -> PgSyntax
emit ByteString
"BOOLEAN")
                                 BeamSerializedDataType
forall dataType. IsSql99DataTypeSyntax dataType => dataType
booleanType
  arrayType :: PgDataTypeSyntax -> Int -> PgDataTypeSyntax
arrayType (PgDataTypeSyntax PgDataTypeDescr
_ PgSyntax
syntax BeamSerializedDataType
serialized) Int
sz =
    PgDataTypeDescr
-> PgSyntax -> BeamSerializedDataType -> PgDataTypeSyntax
PgDataTypeSyntax (String -> PgDataTypeDescr
forall a. HasCallStack => String -> a
error String
"TODO: array migrations")
                     (PgSyntax
syntax PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
"[" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit (String -> ByteString
forall a. IsString a => String -> a
fromString (Int -> String
forall a. Show a => a -> String
show Int
sz)) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
"]")
                     (BeamSerializedDataType -> Int -> BeamSerializedDataType
forall dataType.
IsSql99DataTypeSyntax dataType =>
dataType -> Int -> dataType
arrayType BeamSerializedDataType
serialized Int
sz)
  rowType :: [(Text, PgDataTypeSyntax)] -> PgDataTypeSyntax
rowType = String -> [(Text, PgDataTypeSyntax)] -> PgDataTypeSyntax
forall a. HasCallStack => String -> a
error String
"rowType"

instance IsSql99CommonTableExpressionSelectSyntax PgSelectSyntax where
    type Sql99SelectCTESyntax PgSelectSyntax = PgCommonTableExpressionSyntax

    withSyntax :: [Sql99SelectCTESyntax PgSelectSyntax]
-> PgSelectSyntax -> PgSelectSyntax
withSyntax [Sql99SelectCTESyntax PgSelectSyntax]
ctes (PgSelectSyntax PgSyntax
select) =
        PgSyntax -> PgSelectSyntax
PgSelectSyntax (PgSyntax -> PgSelectSyntax) -> PgSyntax -> PgSelectSyntax
forall a b. (a -> b) -> a -> b
$
        ByteString -> PgSyntax
emit ByteString
"WITH " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
        PgSyntax -> [PgSyntax] -> PgSyntax
pgSepBy (ByteString -> PgSyntax
emit ByteString
", ") ((PgCommonTableExpressionSyntax -> PgSyntax)
-> [PgCommonTableExpressionSyntax] -> [PgSyntax]
forall a b. (a -> b) -> [a] -> [b]
map PgCommonTableExpressionSyntax -> PgSyntax
fromPgCommonTableExpression [Sql99SelectCTESyntax PgSelectSyntax]
[PgCommonTableExpressionSyntax]
ctes) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
        PgSyntax
select

instance IsSql99RecursiveCommonTableExpressionSelectSyntax PgSelectSyntax where
    withRecursiveSyntax :: [Sql99SelectCTESyntax PgSelectSyntax]
-> PgSelectSyntax -> PgSelectSyntax
withRecursiveSyntax [Sql99SelectCTESyntax PgSelectSyntax]
ctes (PgSelectSyntax PgSyntax
select) =
        PgSyntax -> PgSelectSyntax
PgSelectSyntax (PgSyntax -> PgSelectSyntax) -> PgSyntax -> PgSelectSyntax
forall a b. (a -> b) -> a -> b
$
        ByteString -> PgSyntax
emit ByteString
"WITH RECURSIVE " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
        PgSyntax -> [PgSyntax] -> PgSyntax
pgSepBy (ByteString -> PgSyntax
emit ByteString
", ") ((PgCommonTableExpressionSyntax -> PgSyntax)
-> [PgCommonTableExpressionSyntax] -> [PgSyntax]
forall a b. (a -> b) -> [a] -> [b]
map PgCommonTableExpressionSyntax -> PgSyntax
fromPgCommonTableExpression [Sql99SelectCTESyntax PgSelectSyntax]
[PgCommonTableExpressionSyntax]
ctes) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
        PgSyntax
select

instance IsSql99CommonTableExpressionSyntax PgCommonTableExpressionSyntax where
    type Sql99CTESelectSyntax PgCommonTableExpressionSyntax = PgSelectSyntax

    cteSubquerySyntax :: Text
-> [Text]
-> Sql99CTESelectSyntax PgCommonTableExpressionSyntax
-> PgCommonTableExpressionSyntax
cteSubquerySyntax Text
tbl [Text]
fields (PgSelectSyntax select) =
        PgSyntax -> PgCommonTableExpressionSyntax
PgCommonTableExpressionSyntax (PgSyntax -> PgCommonTableExpressionSyntax)
-> PgSyntax -> PgCommonTableExpressionSyntax
forall a b. (a -> b) -> a -> b
$
        Text -> PgSyntax
pgQuotedIdentifier Text
tbl PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSyntax -> PgSyntax
pgParens (PgSyntax -> [PgSyntax] -> PgSyntax
pgSepBy (ByteString -> PgSyntax
emit ByteString
",") ((Text -> PgSyntax) -> [Text] -> [PgSyntax]
forall a b. (a -> b) -> [a] -> [b]
map Text -> PgSyntax
pgQuotedIdentifier [Text]
fields)) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
        ByteString -> PgSyntax
emit ByteString
" AS " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSyntax -> PgSyntax
pgParens PgSyntax
select

instance IsSql2008BigIntDataTypeSyntax PgDataTypeSyntax where
  bigIntType :: PgDataTypeSyntax
bigIntType = PgDataTypeDescr
-> PgSyntax -> BeamSerializedDataType -> PgDataTypeSyntax
PgDataTypeSyntax (Oid -> Maybe Int32 -> PgDataTypeDescr
PgDataTypeDescrOid (TypeInfo -> Oid
Pg.typoid TypeInfo
Pg.int8) Maybe Int32
forall a. Maybe a
Nothing) (ByteString -> PgSyntax
emit ByteString
"BIGINT") BeamSerializedDataType
forall dataType. IsSql2008BigIntDataTypeSyntax dataType => dataType
bigIntType

instance Sql92SerializableDataTypeSyntax PgDataTypeSyntax where
  serializeDataType :: PgDataTypeSyntax -> Value
serializeDataType = BeamSerializedDataType -> Value
fromBeamSerializedDataType (BeamSerializedDataType -> Value)
-> (PgDataTypeSyntax -> BeamSerializedDataType)
-> PgDataTypeSyntax
-> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgDataTypeSyntax -> BeamSerializedDataType
pgDataTypeSerialized

pgOptPrec :: Maybe Word -> PgSyntax
pgOptPrec :: Maybe Word -> PgSyntax
pgOptPrec Maybe Word
Nothing = PgSyntax
forall a. Monoid a => a
mempty
pgOptPrec (Just Word
x) = ByteString -> PgSyntax
emit ByteString
"(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit (String -> ByteString
forall a. IsString a => String -> a
fromString (Word -> String
forall a. Show a => a -> String
show Word
x)) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")"

pgOptCharSet :: Maybe T.Text -> PgSyntax
pgOptCharSet :: Maybe Text -> PgSyntax
pgOptCharSet Maybe Text
Nothing = PgSyntax
forall a. Monoid a => a
mempty
pgOptCharSet (Just Text
cs) = ByteString -> PgSyntax
emit ByteString
" CHARACTER SET " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit (Text -> ByteString
TE.encodeUtf8 Text
cs)

pgOptNumericPrec :: Maybe (Word, Maybe Word) -> PgSyntax
pgOptNumericPrec :: Maybe (Word, Maybe Word) -> PgSyntax
pgOptNumericPrec Maybe (Word, Maybe Word)
Nothing = PgSyntax
forall a. Monoid a => a
mempty
pgOptNumericPrec (Just (Word
prec, Maybe Word
Nothing)) = Maybe Word -> PgSyntax
pgOptPrec (Word -> Maybe Word
forall a. a -> Maybe a
Just Word
prec)
pgOptNumericPrec (Just (Word
prec, Just Word
dec)) = ByteString -> PgSyntax
emit ByteString
"(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit (String -> ByteString
forall a. IsString a => String -> a
fromString (Word -> String
forall a. Show a => a -> String
show Word
prec)) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
", " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit (String -> ByteString
forall a. IsString a => String -> a
fromString (Word -> String
forall a. Show a => a -> String
show Word
dec)) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")"

pgDataTypeJSON :: Value -> BeamSerializedDataType
pgDataTypeJSON :: Value -> BeamSerializedDataType
pgDataTypeJSON Value
v = Value -> BeamSerializedDataType
BeamSerializedDataType (Text -> Value -> Value
beamSerializeJSON Text
"postgres" Value
v)

pgByteaType :: PgDataTypeSyntax
pgByteaType :: PgDataTypeSyntax
pgByteaType = PgDataTypeDescr
-> PgSyntax -> BeamSerializedDataType -> PgDataTypeSyntax
PgDataTypeSyntax (Oid -> Maybe Int32 -> PgDataTypeDescr
PgDataTypeDescrOid (TypeInfo -> Oid
Pg.typoid TypeInfo
Pg.bytea) Maybe Int32
forall a. Maybe a
Nothing) (ByteString -> PgSyntax
emit ByteString
"BYTEA")
                               (Value -> BeamSerializedDataType
pgDataTypeJSON Value
"bytea")

pgSmallSerialType, pgSerialType, pgBigSerialType :: PgDataTypeSyntax
pgSmallSerialType :: PgDataTypeSyntax
pgSmallSerialType = PgDataTypeDescr
-> PgSyntax -> BeamSerializedDataType -> PgDataTypeSyntax
PgDataTypeSyntax (PgDataTypeSyntax -> PgDataTypeDescr
pgDataTypeDescr PgDataTypeSyntax
forall dataType. IsSql92DataTypeSyntax dataType => dataType
smallIntType) (ByteString -> PgSyntax
emit ByteString
"SMALLSERIAL") (Value -> BeamSerializedDataType
pgDataTypeJSON Value
"smallserial")
pgSerialType :: PgDataTypeSyntax
pgSerialType = PgDataTypeDescr
-> PgSyntax -> BeamSerializedDataType -> PgDataTypeSyntax
PgDataTypeSyntax (PgDataTypeSyntax -> PgDataTypeDescr
pgDataTypeDescr PgDataTypeSyntax
forall dataType. IsSql92DataTypeSyntax dataType => dataType
intType) (ByteString -> PgSyntax
emit ByteString
"SERIAL") (Value -> BeamSerializedDataType
pgDataTypeJSON Value
"serial")
pgBigSerialType :: PgDataTypeSyntax
pgBigSerialType = PgDataTypeDescr
-> PgSyntax -> BeamSerializedDataType -> PgDataTypeSyntax
PgDataTypeSyntax (Oid -> Maybe Int32 -> PgDataTypeDescr
PgDataTypeDescrOid (TypeInfo -> Oid
Pg.typoid TypeInfo
Pg.int8) Maybe Int32
forall a. Maybe a
Nothing) (ByteString -> PgSyntax
emit ByteString
"BIGSERIAL") (Value -> BeamSerializedDataType
pgDataTypeJSON Value
"bigserial")

pgPointType, pgLineType, pgLineSegmentType, pgBoxType :: PgDataTypeSyntax
pgPointType :: PgDataTypeSyntax
pgPointType = PgDataTypeDescr
-> PgSyntax -> BeamSerializedDataType -> PgDataTypeSyntax
PgDataTypeSyntax (Oid -> Maybe Int32 -> PgDataTypeDescr
PgDataTypeDescrOid (TypeInfo -> Oid
Pg.typoid TypeInfo
Pg.point) Maybe Int32
forall a. Maybe a
Nothing) (ByteString -> PgSyntax
emit ByteString
"POINT") (Value -> BeamSerializedDataType
pgDataTypeJSON Value
"point")
pgLineType :: PgDataTypeSyntax
pgLineType = PgDataTypeDescr
-> PgSyntax -> BeamSerializedDataType -> PgDataTypeSyntax
PgDataTypeSyntax (Oid -> Maybe Int32 -> PgDataTypeDescr
PgDataTypeDescrOid (TypeInfo -> Oid
Pg.typoid TypeInfo
Pg.line) Maybe Int32
forall a. Maybe a
Nothing) (ByteString -> PgSyntax
emit ByteString
"LINE") (Value -> BeamSerializedDataType
pgDataTypeJSON Value
"line")
pgLineSegmentType :: PgDataTypeSyntax
pgLineSegmentType = PgDataTypeDescr
-> PgSyntax -> BeamSerializedDataType -> PgDataTypeSyntax
PgDataTypeSyntax (Oid -> Maybe Int32 -> PgDataTypeDescr
PgDataTypeDescrOid (TypeInfo -> Oid
Pg.typoid TypeInfo
Pg.lseg) Maybe Int32
forall a. Maybe a
Nothing) (ByteString -> PgSyntax
emit ByteString
"LSEG") (Value -> BeamSerializedDataType
pgDataTypeJSON Value
"lseg")
pgBoxType :: PgDataTypeSyntax
pgBoxType = PgDataTypeDescr
-> PgSyntax -> BeamSerializedDataType -> PgDataTypeSyntax
PgDataTypeSyntax (Oid -> Maybe Int32 -> PgDataTypeDescr
PgDataTypeDescrOid (TypeInfo -> Oid
Pg.typoid TypeInfo
Pg.box) Maybe Int32
forall a. Maybe a
Nothing) (ByteString -> PgSyntax
emit ByteString
"BOX") (Value -> BeamSerializedDataType
pgDataTypeJSON Value
"box")

pgUnboundedArrayType :: PgDataTypeSyntax -> PgDataTypeSyntax
pgUnboundedArrayType :: PgDataTypeSyntax -> PgDataTypeSyntax
pgUnboundedArrayType (PgDataTypeSyntax PgDataTypeDescr
_ PgSyntax
syntax BeamSerializedDataType
serialized) =
    PgDataTypeDescr
-> PgSyntax -> BeamSerializedDataType -> PgDataTypeSyntax
PgDataTypeSyntax (String -> PgDataTypeDescr
forall a. HasCallStack => String -> a
error String
"Can't do array migrations yet")
                     (PgSyntax
syntax PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
"[]")
                     (Value -> BeamSerializedDataType
pgDataTypeJSON ([Pair] -> Value
object [ Key
"unbounded-array" Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= BeamSerializedDataType -> Value
fromBeamSerializedDataType BeamSerializedDataType
serialized ]))

pgTsQueryTypeInfo :: Pg.TypeInfo
pgTsQueryTypeInfo :: TypeInfo
pgTsQueryTypeInfo = Oid -> Char -> Char -> ByteString -> TypeInfo
Pg.Basic (CUInt -> Oid
Pg.Oid CUInt
3615) Char
'U' Char
',' ByteString
"tsquery"

pgTsQueryType :: PgDataTypeSyntax
pgTsQueryType :: PgDataTypeSyntax
pgTsQueryType = PgDataTypeDescr
-> PgSyntax -> BeamSerializedDataType -> PgDataTypeSyntax
PgDataTypeSyntax (Oid -> Maybe Int32 -> PgDataTypeDescr
PgDataTypeDescrOid (TypeInfo -> Oid
Pg.typoid TypeInfo
pgTsQueryTypeInfo) Maybe Int32
forall a. Maybe a
Nothing)
                                 (ByteString -> PgSyntax
emit ByteString
"TSQUERY") (Value -> BeamSerializedDataType
pgDataTypeJSON Value
"tsquery")

-- | Postgres TypeInfo for tsvector
-- TODO Is the Oid stable from postgres instance to postgres instance?
pgTsVectorTypeInfo :: Pg.TypeInfo
pgTsVectorTypeInfo :: TypeInfo
pgTsVectorTypeInfo = Oid -> Char -> Char -> ByteString -> TypeInfo
Pg.Basic (CUInt -> Oid
Pg.Oid CUInt
3614) Char
'U' Char
',' ByteString
"tsvector"

pgTsVectorType :: PgDataTypeSyntax
pgTsVectorType :: PgDataTypeSyntax
pgTsVectorType = PgDataTypeDescr
-> PgSyntax -> BeamSerializedDataType -> PgDataTypeSyntax
PgDataTypeSyntax (Oid -> Maybe Int32 -> PgDataTypeDescr
PgDataTypeDescrOid (TypeInfo -> Oid
Pg.typoid TypeInfo
pgTsVectorTypeInfo) Maybe Int32
forall a. Maybe a
Nothing)
                                  (ByteString -> PgSyntax
emit ByteString
"TSVECTOR")
                                  (Value -> BeamSerializedDataType
pgDataTypeJSON Value
"tsvector")

pgTextType :: PgDataTypeSyntax
pgTextType :: PgDataTypeSyntax
pgTextType = PgDataTypeDescr
-> PgSyntax -> BeamSerializedDataType -> PgDataTypeSyntax
PgDataTypeSyntax (Oid -> Maybe Int32 -> PgDataTypeDescr
PgDataTypeDescrOid (TypeInfo -> Oid
Pg.typoid TypeInfo
Pg.text) Maybe Int32
forall a. Maybe a
Nothing) (ByteString -> PgSyntax
emit ByteString
"TEXT")
                              (Value -> BeamSerializedDataType
pgDataTypeJSON Value
"text")

pgJsonType, pgJsonbType :: PgDataTypeSyntax
pgJsonType :: PgDataTypeSyntax
pgJsonType = PgDataTypeDescr
-> PgSyntax -> BeamSerializedDataType -> PgDataTypeSyntax
PgDataTypeSyntax (Oid -> Maybe Int32 -> PgDataTypeDescr
PgDataTypeDescrOid (TypeInfo -> Oid
Pg.typoid TypeInfo
Pg.json) Maybe Int32
forall a. Maybe a
Nothing) (ByteString -> PgSyntax
emit ByteString
"JSON") (Value -> BeamSerializedDataType
pgDataTypeJSON Value
"json")
pgJsonbType :: PgDataTypeSyntax
pgJsonbType = PgDataTypeDescr
-> PgSyntax -> BeamSerializedDataType -> PgDataTypeSyntax
PgDataTypeSyntax (Oid -> Maybe Int32 -> PgDataTypeDescr
PgDataTypeDescrOid (TypeInfo -> Oid
Pg.typoid TypeInfo
Pg.jsonb) Maybe Int32
forall a. Maybe a
Nothing) (ByteString -> PgSyntax
emit ByteString
"JSONB") (Value -> BeamSerializedDataType
pgDataTypeJSON Value
"jsonb")

pgUuidType :: PgDataTypeSyntax
pgUuidType :: PgDataTypeSyntax
pgUuidType = PgDataTypeDescr
-> PgSyntax -> BeamSerializedDataType -> PgDataTypeSyntax
PgDataTypeSyntax (Oid -> Maybe Int32 -> PgDataTypeDescr
PgDataTypeDescrOid (TypeInfo -> Oid
Pg.typoid TypeInfo
Pg.uuid) Maybe Int32
forall a. Maybe a
Nothing) (ByteString -> PgSyntax
emit ByteString
"UUID") (Value -> BeamSerializedDataType
pgDataTypeJSON Value
"uuid")

pgMoneyType :: PgDataTypeSyntax
pgMoneyType :: PgDataTypeSyntax
pgMoneyType = PgDataTypeDescr
-> PgSyntax -> BeamSerializedDataType -> PgDataTypeSyntax
PgDataTypeSyntax (Oid -> Maybe Int32 -> PgDataTypeDescr
PgDataTypeDescrOid (TypeInfo -> Oid
Pg.typoid TypeInfo
Pg.money) Maybe Int32
forall a. Maybe a
Nothing) (ByteString -> PgSyntax
emit ByteString
"MONEY") (Value -> BeamSerializedDataType
pgDataTypeJSON Value
"money")

mkNumericPrec :: Maybe (Word, Maybe Word) -> Maybe Int32
mkNumericPrec :: Maybe (Word, Maybe Word) -> Maybe Int32
mkNumericPrec Maybe (Word, Maybe Word)
Nothing = Maybe Int32
forall a. Maybe a
Nothing
mkNumericPrec (Just (Word
whole, Maybe Word
dec)) = Int32 -> Maybe Int32
forall a. a -> Maybe a
Just (Int32 -> Maybe Int32) -> Int32 -> Maybe Int32
forall a b. (a -> b) -> a -> b
$ (Word -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
whole Int32 -> Int -> Int32
forall a. Bits a => a -> Int -> a
`shiftL` Int
16) Int32 -> Int32 -> Int32
forall a. Bits a => a -> a -> a
.|. (Word -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word -> Maybe Word -> Word
forall a. a -> Maybe a -> a
fromMaybe Word
0 Maybe Word
dec) Int32 -> Int32 -> Int32
forall a. Bits a => a -> a -> a
.&. Int32
0xFFFF)

instance IsCustomSqlSyntax PgExpressionSyntax where
  newtype CustomSqlSyntax PgExpressionSyntax =
    PgCustomExpressionSyntax { CustomSqlSyntax PgExpressionSyntax -> PgSyntax
fromPgCustomExpression :: PgSyntax }
    deriving Semigroup (CustomSqlSyntax PgExpressionSyntax)
CustomSqlSyntax PgExpressionSyntax
Semigroup (CustomSqlSyntax PgExpressionSyntax)
-> CustomSqlSyntax PgExpressionSyntax
-> (CustomSqlSyntax PgExpressionSyntax
    -> CustomSqlSyntax PgExpressionSyntax
    -> CustomSqlSyntax PgExpressionSyntax)
-> ([CustomSqlSyntax PgExpressionSyntax]
    -> CustomSqlSyntax PgExpressionSyntax)
-> Monoid (CustomSqlSyntax PgExpressionSyntax)
[CustomSqlSyntax PgExpressionSyntax]
-> CustomSqlSyntax PgExpressionSyntax
CustomSqlSyntax PgExpressionSyntax
-> CustomSqlSyntax PgExpressionSyntax
-> CustomSqlSyntax PgExpressionSyntax
forall a.
Semigroup a -> a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
mconcat :: [CustomSqlSyntax PgExpressionSyntax]
-> CustomSqlSyntax PgExpressionSyntax
$cmconcat :: [CustomSqlSyntax PgExpressionSyntax]
-> CustomSqlSyntax PgExpressionSyntax
mappend :: CustomSqlSyntax PgExpressionSyntax
-> CustomSqlSyntax PgExpressionSyntax
-> CustomSqlSyntax PgExpressionSyntax
$cmappend :: CustomSqlSyntax PgExpressionSyntax
-> CustomSqlSyntax PgExpressionSyntax
-> CustomSqlSyntax PgExpressionSyntax
mempty :: CustomSqlSyntax PgExpressionSyntax
$cmempty :: CustomSqlSyntax PgExpressionSyntax
$cp1Monoid :: Semigroup (CustomSqlSyntax PgExpressionSyntax)
Monoid
  customExprSyntax :: CustomSqlSyntax PgExpressionSyntax -> PgExpressionSyntax
customExprSyntax = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax)
-> (CustomSqlSyntax PgExpressionSyntax -> PgSyntax)
-> CustomSqlSyntax PgExpressionSyntax
-> PgExpressionSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CustomSqlSyntax PgExpressionSyntax -> PgSyntax
fromPgCustomExpression
  renderSyntax :: PgExpressionSyntax -> CustomSqlSyntax PgExpressionSyntax
renderSyntax = PgSyntax -> CustomSqlSyntax PgExpressionSyntax
PgCustomExpressionSyntax (PgSyntax -> CustomSqlSyntax PgExpressionSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> CustomSqlSyntax PgExpressionSyntax
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

instance Semigroup (CustomSqlSyntax PgExpressionSyntax) where
  <> :: CustomSqlSyntax PgExpressionSyntax
-> CustomSqlSyntax PgExpressionSyntax
-> CustomSqlSyntax PgExpressionSyntax
(<>) = CustomSqlSyntax PgExpressionSyntax
-> CustomSqlSyntax PgExpressionSyntax
-> CustomSqlSyntax PgExpressionSyntax
forall a. Monoid a => a -> a -> a
mappend

instance IsString (CustomSqlSyntax PgExpressionSyntax) where
  fromString :: String -> CustomSqlSyntax PgExpressionSyntax
fromString = PgSyntax -> CustomSqlSyntax PgExpressionSyntax
PgCustomExpressionSyntax (PgSyntax -> CustomSqlSyntax PgExpressionSyntax)
-> (String -> PgSyntax)
-> String
-> CustomSqlSyntax PgExpressionSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> PgSyntax
emit (ByteString -> PgSyntax)
-> (String -> ByteString) -> String -> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ByteString
forall a. IsString a => String -> a
fromString

instance IsSql92QuantifierSyntax PgComparisonQuantifierSyntax where
  quantifyOverAll :: PgComparisonQuantifierSyntax
quantifyOverAll = PgSyntax -> PgComparisonQuantifierSyntax
PgComparisonQuantifierSyntax (ByteString -> PgSyntax
emit ByteString
"ALL")
  quantifyOverAny :: PgComparisonQuantifierSyntax
quantifyOverAny = PgSyntax -> PgComparisonQuantifierSyntax
PgComparisonQuantifierSyntax (ByteString -> PgSyntax
emit ByteString
"ANY")

instance IsSql92ExtractFieldSyntax PgExtractFieldSyntax where
  secondsField :: PgExtractFieldSyntax
secondsField = PgSyntax -> PgExtractFieldSyntax
PgExtractFieldSyntax (ByteString -> PgSyntax
emit ByteString
"SECOND")
  minutesField :: PgExtractFieldSyntax
minutesField = PgSyntax -> PgExtractFieldSyntax
PgExtractFieldSyntax (ByteString -> PgSyntax
emit ByteString
"MINUTE")
  hourField :: PgExtractFieldSyntax
hourField    = PgSyntax -> PgExtractFieldSyntax
PgExtractFieldSyntax (ByteString -> PgSyntax
emit ByteString
"HOUR")
  dayField :: PgExtractFieldSyntax
dayField     = PgSyntax -> PgExtractFieldSyntax
PgExtractFieldSyntax (ByteString -> PgSyntax
emit ByteString
"DAY")
  monthField :: PgExtractFieldSyntax
monthField   = PgSyntax -> PgExtractFieldSyntax
PgExtractFieldSyntax (ByteString -> PgSyntax
emit ByteString
"MONTH")
  yearField :: PgExtractFieldSyntax
yearField    = PgSyntax -> PgExtractFieldSyntax
PgExtractFieldSyntax (ByteString -> PgSyntax
emit ByteString
"YEAR")

instance IsSql92ExpressionSyntax PgExpressionSyntax where
  type Sql92ExpressionValueSyntax PgExpressionSyntax = PgValueSyntax
  type Sql92ExpressionSelectSyntax PgExpressionSyntax = PgSelectSyntax
  type Sql92ExpressionFieldNameSyntax PgExpressionSyntax = PgFieldNameSyntax
  type Sql92ExpressionQuantifierSyntax PgExpressionSyntax = PgComparisonQuantifierSyntax
  type Sql92ExpressionCastTargetSyntax PgExpressionSyntax = PgDataTypeSyntax
  type Sql92ExpressionExtractFieldSyntax PgExpressionSyntax = PgExtractFieldSyntax

  addE :: PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
addE = ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"+"
  subE :: PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
subE = ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"-"
  mulE :: PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
mulE = ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"*"
  divE :: PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
divE = ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"/"
  modE :: PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
modE = ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"%"
  orE :: PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
orE = ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"OR"
  andE :: PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
andE = ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"AND"
  likeE :: PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
likeE = ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"LIKE"
  overlapsE :: PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
overlapsE = ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"OVERLAPS"
  eqE :: Maybe (Sql92ExpressionQuantifierSyntax PgExpressionSyntax)
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
eqE = ByteString
-> Maybe PgComparisonQuantifierSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
pgCompOp ByteString
"="
  neqE :: Maybe (Sql92ExpressionQuantifierSyntax PgExpressionSyntax)
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
neqE = ByteString
-> Maybe PgComparisonQuantifierSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
pgCompOp ByteString
"<>"
  eqMaybeE :: PgExpressionSyntax
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
eqMaybeE PgExpressionSyntax
a PgExpressionSyntax
b PgExpressionSyntax
_ = ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"IS NOT DISTINCT FROM" PgExpressionSyntax
a PgExpressionSyntax
b
  neqMaybeE :: PgExpressionSyntax
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
neqMaybeE PgExpressionSyntax
a PgExpressionSyntax
b PgExpressionSyntax
_ = ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"IS DISTINCT FROM" PgExpressionSyntax
a PgExpressionSyntax
b
  ltE :: Maybe (Sql92ExpressionQuantifierSyntax PgExpressionSyntax)
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
ltE = ByteString
-> Maybe PgComparisonQuantifierSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
pgCompOp ByteString
"<"
  gtE :: Maybe (Sql92ExpressionQuantifierSyntax PgExpressionSyntax)
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
gtE = ByteString
-> Maybe PgComparisonQuantifierSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
pgCompOp ByteString
">"
  leE :: Maybe (Sql92ExpressionQuantifierSyntax PgExpressionSyntax)
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
leE = ByteString
-> Maybe PgComparisonQuantifierSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
pgCompOp ByteString
"<="
  geE :: Maybe (Sql92ExpressionQuantifierSyntax PgExpressionSyntax)
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
geE = ByteString
-> Maybe PgComparisonQuantifierSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
pgCompOp ByteString
">="
  negateE :: PgExpressionSyntax -> PgExpressionSyntax
negateE = ByteString -> PgExpressionSyntax -> PgExpressionSyntax
pgUnOp ByteString
"-"
  notE :: PgExpressionSyntax -> PgExpressionSyntax
notE = ByteString -> PgExpressionSyntax -> PgExpressionSyntax
pgUnOp ByteString
"NOT"
  existsE :: Sql92ExpressionSelectSyntax PgExpressionSyntax
-> PgExpressionSyntax
existsE Sql92ExpressionSelectSyntax PgExpressionSyntax
select = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"EXISTS (" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSelectSyntax -> PgSyntax
fromPgSelect Sql92ExpressionSelectSyntax PgExpressionSyntax
PgSelectSyntax
select PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")
  uniqueE :: Sql92ExpressionSelectSyntax PgExpressionSyntax
-> PgExpressionSyntax
uniqueE Sql92ExpressionSelectSyntax PgExpressionSyntax
select = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"UNIQUE (" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSelectSyntax -> PgSyntax
fromPgSelect Sql92ExpressionSelectSyntax PgExpressionSyntax
PgSelectSyntax
select PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")
  isNotNullE :: PgExpressionSyntax -> PgExpressionSyntax
isNotNullE = ByteString -> PgExpressionSyntax -> PgExpressionSyntax
pgPostFix ByteString
"IS NOT NULL"
  isNullE :: PgExpressionSyntax -> PgExpressionSyntax
isNullE = ByteString -> PgExpressionSyntax -> PgExpressionSyntax
pgPostFix ByteString
"IS NULL"
  isTrueE :: PgExpressionSyntax -> PgExpressionSyntax
isTrueE = ByteString -> PgExpressionSyntax -> PgExpressionSyntax
pgPostFix ByteString
"IS TRUE"
  isFalseE :: PgExpressionSyntax -> PgExpressionSyntax
isFalseE = ByteString -> PgExpressionSyntax -> PgExpressionSyntax
pgPostFix ByteString
"IS FALSE"
  isNotTrueE :: PgExpressionSyntax -> PgExpressionSyntax
isNotTrueE = ByteString -> PgExpressionSyntax -> PgExpressionSyntax
pgPostFix ByteString
"IS NOT TRUE"
  isNotFalseE :: PgExpressionSyntax -> PgExpressionSyntax
isNotFalseE = ByteString -> PgExpressionSyntax -> PgExpressionSyntax
pgPostFix ByteString
"IS NOT FALSE"
  isUnknownE :: PgExpressionSyntax -> PgExpressionSyntax
isUnknownE = ByteString -> PgExpressionSyntax -> PgExpressionSyntax
pgPostFix ByteString
"IS UNKNOWN"
  isNotUnknownE :: PgExpressionSyntax -> PgExpressionSyntax
isNotUnknownE = ByteString -> PgExpressionSyntax -> PgExpressionSyntax
pgPostFix ByteString
"IS NOT UNKNOWN"
  betweenE :: PgExpressionSyntax
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
betweenE PgExpressionSyntax
a PgExpressionSyntax
b PgExpressionSyntax
c = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
a PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
") BETWEEN (" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
                                       PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
b PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
") AND (" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
c PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")
  valueE :: Sql92ExpressionValueSyntax PgExpressionSyntax -> PgExpressionSyntax
valueE = Sql92ExpressionValueSyntax PgExpressionSyntax -> PgExpressionSyntax
coerce
  rowE :: [PgExpressionSyntax] -> PgExpressionSyntax
rowE [PgExpressionSyntax]
vs = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax) -> PgSyntax -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$
            ByteString -> PgSyntax
emit ByteString
"(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
            PgSyntax -> [PgSyntax] -> PgSyntax
pgSepBy (ByteString -> PgSyntax
emit ByteString
", ") ([PgExpressionSyntax] -> [PgSyntax]
coerce [PgExpressionSyntax]
vs) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
            ByteString -> PgSyntax
emit ByteString
")"
  quantifierListE :: [PgExpressionSyntax] -> PgExpressionSyntax
quantifierListE [PgExpressionSyntax]
vs =
    PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax) -> PgSyntax -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$
    ByteString -> PgSyntax
emit ByteString
"(VALUES " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSyntax -> [PgSyntax] -> PgSyntax
pgSepBy (ByteString -> PgSyntax
emit ByteString
", ") ((PgExpressionSyntax -> PgSyntax)
-> [PgExpressionSyntax] -> [PgSyntax]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (PgSyntax -> PgSyntax
pgParens (PgSyntax -> PgSyntax)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgExpressionSyntax -> PgSyntax
fromPgExpression) [PgExpressionSyntax]
vs) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")"
  fieldE :: Sql92ExpressionFieldNameSyntax PgExpressionSyntax
-> PgExpressionSyntax
fieldE = Sql92ExpressionFieldNameSyntax PgExpressionSyntax
-> PgExpressionSyntax
coerce
  subqueryE :: Sql92ExpressionSelectSyntax PgExpressionSyntax
-> PgExpressionSyntax
subqueryE Sql92ExpressionSelectSyntax PgExpressionSyntax
s = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSelectSyntax -> PgSyntax
fromPgSelect Sql92ExpressionSelectSyntax PgExpressionSyntax
PgSelectSyntax
s PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")
  positionE :: PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
positionE PgExpressionSyntax
needle PgExpressionSyntax
haystack =
      PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax) -> PgSyntax -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$
      ByteString -> PgSyntax
emit ByteString
"POSITION((" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
needle PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
") IN (" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
haystack PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
"))"
  nullIfE :: PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
nullIfE PgExpressionSyntax
a PgExpressionSyntax
b = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"NULLIF(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
a 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
b PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")
  absE :: PgExpressionSyntax -> PgExpressionSyntax
absE PgExpressionSyntax
x = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"ABS(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
x PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")
  bitLengthE :: PgExpressionSyntax -> PgExpressionSyntax
bitLengthE PgExpressionSyntax
x = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"BIT_LENGTH(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
x PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")
  charLengthE :: PgExpressionSyntax -> PgExpressionSyntax
charLengthE PgExpressionSyntax
x = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"CHAR_LENGTH(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
x PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")
  octetLengthE :: PgExpressionSyntax -> PgExpressionSyntax
octetLengthE PgExpressionSyntax
x = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"OCTET_LENGTH(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
x PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")
  lowerE :: PgExpressionSyntax -> PgExpressionSyntax
lowerE PgExpressionSyntax
x = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"LOWER(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
x PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")
  upperE :: PgExpressionSyntax -> PgExpressionSyntax
upperE PgExpressionSyntax
x = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"UPPER(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
x PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")
  trimE :: PgExpressionSyntax -> PgExpressionSyntax
trimE PgExpressionSyntax
x = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"TRIM(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
x PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")
  coalesceE :: [PgExpressionSyntax] -> PgExpressionSyntax
coalesceE [PgExpressionSyntax]
es = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"COALESCE(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSyntax -> [PgSyntax] -> PgSyntax
pgSepBy (ByteString -> PgSyntax
emit ByteString
", ") ((PgExpressionSyntax -> PgSyntax)
-> [PgExpressionSyntax] -> [PgSyntax]
forall a b. (a -> b) -> [a] -> [b]
map PgExpressionSyntax -> PgSyntax
fromPgExpression [PgExpressionSyntax]
es) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")
  extractE :: Sql92ExpressionExtractFieldSyntax PgExpressionSyntax
-> PgExpressionSyntax -> PgExpressionSyntax
extractE Sql92ExpressionExtractFieldSyntax PgExpressionSyntax
field PgExpressionSyntax
from = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"EXTRACT(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExtractFieldSyntax -> PgSyntax
fromPgExtractField Sql92ExpressionExtractFieldSyntax PgExpressionSyntax
PgExtractFieldSyntax
field PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
" FROM (" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
from PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
"))")
  castE :: PgExpressionSyntax
-> Sql92ExpressionCastTargetSyntax PgExpressionSyntax
-> PgExpressionSyntax
castE PgExpressionSyntax
e Sql92ExpressionCastTargetSyntax PgExpressionSyntax
to = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"CAST((" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
e PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
") AS " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgDataTypeSyntax -> PgSyntax
fromPgDataType Sql92ExpressionCastTargetSyntax PgExpressionSyntax
PgDataTypeSyntax
to PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")
  caseE :: [(PgExpressionSyntax, PgExpressionSyntax)]
-> PgExpressionSyntax -> PgExpressionSyntax
caseE [(PgExpressionSyntax, PgExpressionSyntax)]
cases PgExpressionSyntax
else_ =
      PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax) -> PgSyntax -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$
      ByteString -> PgSyntax
emit ByteString
"CASE " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
      ((PgExpressionSyntax, PgExpressionSyntax) -> PgSyntax)
-> [(PgExpressionSyntax, PgExpressionSyntax)] -> PgSyntax
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (\(PgExpressionSyntax
cond, PgExpressionSyntax
res) -> ByteString -> PgSyntax
emit ByteString
"WHEN " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
cond PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
" THEN " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
res PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
" ") [(PgExpressionSyntax, PgExpressionSyntax)]
cases PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
      ByteString -> PgSyntax
emit ByteString
"ELSE " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
else_ PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
" END"

  currentTimestampE :: PgExpressionSyntax
currentTimestampE = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax) -> PgSyntax -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$ ByteString -> PgSyntax
emit ByteString
"CURRENT_TIMESTAMP"

  defaultE :: PgExpressionSyntax
defaultE = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax) -> PgSyntax -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$ ByteString -> PgSyntax
emit ByteString
"DEFAULT"

  inE :: PgExpressionSyntax -> [PgExpressionSyntax] -> PgExpressionSyntax
inE PgExpressionSyntax
e [PgExpressionSyntax]
es = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax) -> PgSyntax -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$ PgSyntax -> PgSyntax
pgParens (PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
e) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
" IN " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
                                  PgSyntax -> PgSyntax
pgParens (PgSyntax -> [PgSyntax] -> PgSyntax
pgSepBy (ByteString -> PgSyntax
emit ByteString
", ") ((PgExpressionSyntax -> PgSyntax)
-> [PgExpressionSyntax] -> [PgSyntax]
forall a b. (a -> b) -> [a] -> [b]
map PgExpressionSyntax -> PgSyntax
fromPgExpression [PgExpressionSyntax]
es))

instance IsSql99FunctionExpressionSyntax PgExpressionSyntax where
  functionCallE :: PgExpressionSyntax -> [PgExpressionSyntax] -> PgExpressionSyntax
functionCallE PgExpressionSyntax
name [PgExpressionSyntax]
args =
    PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax) -> PgSyntax -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$
    PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
name PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
    PgSyntax -> PgSyntax
pgParens (PgSyntax -> [PgSyntax] -> PgSyntax
pgSepBy (ByteString -> PgSyntax
emit ByteString
", ") ((PgExpressionSyntax -> PgSyntax)
-> [PgExpressionSyntax] -> [PgSyntax]
forall a b. (a -> b) -> [a] -> [b]
map PgExpressionSyntax -> PgSyntax
fromPgExpression [PgExpressionSyntax]
args))
  functionNameE :: Text -> PgExpressionSyntax
functionNameE Text
nm = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit (Text -> ByteString
TE.encodeUtf8 Text
nm))

instance IsSql99ExpressionSyntax PgExpressionSyntax where
  distinctE :: Sql92ExpressionSelectSyntax PgExpressionSyntax
-> PgExpressionSyntax
distinctE Sql92ExpressionSelectSyntax PgExpressionSyntax
select = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"DISTINCT (" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSelectSyntax -> PgSyntax
fromPgSelect Sql92ExpressionSelectSyntax PgExpressionSyntax
PgSelectSyntax
select PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")
  similarToE :: PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
similarToE = ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
"SIMILAR TO"

  instanceFieldE :: PgExpressionSyntax -> Text -> PgExpressionSyntax
instanceFieldE PgExpressionSyntax
i Text
nm =
    PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax) -> PgSyntax -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$
    PgSyntax -> PgSyntax
pgParens (PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
i) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
"." PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
escapeIdentifier (Text -> ByteString
TE.encodeUtf8 Text
nm)

  refFieldE :: PgExpressionSyntax -> Text -> PgExpressionSyntax
refFieldE PgExpressionSyntax
i Text
nm =
    PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax) -> PgSyntax -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$
    PgSyntax -> PgSyntax
pgParens (PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
i) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
"->" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
escapeIdentifier (Text -> ByteString
TE.encodeUtf8 Text
nm)

instance IsSql99ConcatExpressionSyntax PgExpressionSyntax where
  concatE :: [PgExpressionSyntax] -> PgExpressionSyntax
concatE [] = Sql92ExpressionValueSyntax PgExpressionSyntax -> PgExpressionSyntax
forall expr.
IsSql92ExpressionSyntax expr =>
Sql92ExpressionValueSyntax expr -> expr
valueE (Text -> PgValueSyntax
forall expr ty. HasSqlValueSyntax expr ty => ty -> expr
sqlValueSyntax (Text
"" :: T.Text))
  concatE [PgExpressionSyntax
x] = PgExpressionSyntax
x
  concatE [PgExpressionSyntax]
es =
    PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax) -> PgSyntax -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$
    ByteString -> PgSyntax
emit ByteString
"CONCAT" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSyntax -> PgSyntax
pgParens (PgSyntax -> [PgSyntax] -> PgSyntax
pgSepBy (ByteString -> PgSyntax
emit ByteString
", ") ((PgExpressionSyntax -> PgSyntax)
-> [PgExpressionSyntax] -> [PgSyntax]
forall a b. (a -> b) -> [a] -> [b]
map PgExpressionSyntax -> PgSyntax
fromPgExpression [PgExpressionSyntax]
es))

instance IsSql2003ExpressionSyntax PgExpressionSyntax where
  type Sql2003ExpressionWindowFrameSyntax PgExpressionSyntax =
    PgWindowFrameSyntax

  overE :: PgExpressionSyntax
-> Sql2003ExpressionWindowFrameSyntax PgExpressionSyntax
-> PgExpressionSyntax
overE PgExpressionSyntax
expr Sql2003ExpressionWindowFrameSyntax PgExpressionSyntax
frame =
    PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax) -> PgSyntax -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$
    PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
expr PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
" " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgWindowFrameSyntax -> PgSyntax
fromPgWindowFrame Sql2003ExpressionWindowFrameSyntax PgExpressionSyntax
PgWindowFrameSyntax
frame
  rowNumberE :: PgExpressionSyntax
rowNumberE = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax) -> PgSyntax -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$ ByteString -> PgSyntax
emit ByteString
"ROW_NUMBER()"

instance IsSql2003EnhancedNumericFunctionsExpressionSyntax PgExpressionSyntax where
  lnE :: PgExpressionSyntax -> PgExpressionSyntax
lnE    PgExpressionSyntax
x = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"LN("    PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
x PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")
  expE :: PgExpressionSyntax -> PgExpressionSyntax
expE   PgExpressionSyntax
x = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"EXP("   PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
x PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")
  sqrtE :: PgExpressionSyntax -> PgExpressionSyntax
sqrtE  PgExpressionSyntax
x = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"SQRT("  PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
x PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")
  ceilE :: PgExpressionSyntax -> PgExpressionSyntax
ceilE  PgExpressionSyntax
x = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"CEIL("  PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
x PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")
  floorE :: PgExpressionSyntax -> PgExpressionSyntax
floorE PgExpressionSyntax
x = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"FLOOR(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
x PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")
  powerE :: PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
powerE PgExpressionSyntax
x PgExpressionSyntax
y = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"POWER(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
x 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
y PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")

instance IsSql2003ExpressionAdvancedOLAPOperationsSyntax PgExpressionSyntax where
  denseRankAggE :: PgExpressionSyntax
denseRankAggE = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax) -> PgSyntax -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$ ByteString -> PgSyntax
emit ByteString
"DENSE_RANK()"
  percentRankAggE :: PgExpressionSyntax
percentRankAggE = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax) -> PgSyntax -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$ ByteString -> PgSyntax
emit ByteString
"PERCENT_RANK()"
  cumeDistAggE :: PgExpressionSyntax
cumeDistAggE = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax) -> PgSyntax -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$ ByteString -> PgSyntax
emit ByteString
"CUME_DIST()"

instance IsSql2003ExpressionElementaryOLAPOperationsSyntax PgExpressionSyntax where
  rankAggE :: PgExpressionSyntax
rankAggE = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax) -> PgSyntax -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$ ByteString -> PgSyntax
emit ByteString
"RANK()"
  filterAggE :: PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
filterAggE PgExpressionSyntax
agg PgExpressionSyntax
filter =
    PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax) -> PgSyntax -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$
    PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
agg PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
" FILTER (WHERE " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
filter PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")"

instance IsSql2003EnhancedNumericFunctionsAggregationExpressionSyntax PgExpressionSyntax where
  stddevPopE :: Maybe (Sql92AggregationSetQuantifierSyntax PgExpressionSyntax)
-> PgExpressionSyntax -> PgExpressionSyntax
stddevPopE = ByteString
-> Maybe PgAggregationSetQuantifierSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
pgUnAgg ByteString
"STDDEV_POP"
  stddevSampE :: Maybe (Sql92AggregationSetQuantifierSyntax PgExpressionSyntax)
-> PgExpressionSyntax -> PgExpressionSyntax
stddevSampE = ByteString
-> Maybe PgAggregationSetQuantifierSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
pgUnAgg ByteString
"STDDEV_SAMP"
  varPopE :: Maybe (Sql92AggregationSetQuantifierSyntax PgExpressionSyntax)
-> PgExpressionSyntax -> PgExpressionSyntax
varPopE = ByteString
-> Maybe PgAggregationSetQuantifierSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
pgUnAgg ByteString
"VAR_POP"
  varSampE :: Maybe (Sql92AggregationSetQuantifierSyntax PgExpressionSyntax)
-> PgExpressionSyntax -> PgExpressionSyntax
varSampE = ByteString
-> Maybe PgAggregationSetQuantifierSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
pgUnAgg ByteString
"VAR_SAMP"

  covarPopE :: Maybe (Sql92AggregationSetQuantifierSyntax PgExpressionSyntax)
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
covarPopE = ByteString
-> Maybe PgAggregationSetQuantifierSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
pgBinAgg ByteString
"COVAR_POP"
  covarSampE :: Maybe (Sql92AggregationSetQuantifierSyntax PgExpressionSyntax)
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
covarSampE = ByteString
-> Maybe PgAggregationSetQuantifierSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
pgBinAgg ByteString
"COVAR_SAMP"
  corrE :: Maybe (Sql92AggregationSetQuantifierSyntax PgExpressionSyntax)
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
corrE = ByteString
-> Maybe PgAggregationSetQuantifierSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
pgBinAgg ByteString
"CORR"
  regrSlopeE :: Maybe (Sql92AggregationSetQuantifierSyntax PgExpressionSyntax)
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
regrSlopeE = ByteString
-> Maybe PgAggregationSetQuantifierSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
pgBinAgg ByteString
"REGR_SLOPE"
  regrInterceptE :: Maybe (Sql92AggregationSetQuantifierSyntax PgExpressionSyntax)
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
regrInterceptE = ByteString
-> Maybe PgAggregationSetQuantifierSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
pgBinAgg ByteString
"REGR_INTERCEPT"
  regrCountE :: Maybe (Sql92AggregationSetQuantifierSyntax PgExpressionSyntax)
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
regrCountE = ByteString
-> Maybe PgAggregationSetQuantifierSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
pgBinAgg ByteString
"REGR_COUNT"
  regrRSquaredE :: Maybe (Sql92AggregationSetQuantifierSyntax PgExpressionSyntax)
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
regrRSquaredE = ByteString
-> Maybe PgAggregationSetQuantifierSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
pgBinAgg ByteString
"REGR_R2"
  regrAvgXE :: Maybe (Sql92AggregationSetQuantifierSyntax PgExpressionSyntax)
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
regrAvgXE = ByteString
-> Maybe PgAggregationSetQuantifierSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
pgBinAgg ByteString
"REGR_AVGX"
  regrAvgYE :: Maybe (Sql92AggregationSetQuantifierSyntax PgExpressionSyntax)
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
regrAvgYE = ByteString
-> Maybe PgAggregationSetQuantifierSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
pgBinAgg ByteString
"REGR_AVGY"
  regrSXXE :: Maybe (Sql92AggregationSetQuantifierSyntax PgExpressionSyntax)
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
regrSXXE = ByteString
-> Maybe PgAggregationSetQuantifierSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
pgBinAgg ByteString
"REGR_SXX"
  regrSYYE :: Maybe (Sql92AggregationSetQuantifierSyntax PgExpressionSyntax)
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
regrSYYE = ByteString
-> Maybe PgAggregationSetQuantifierSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
pgBinAgg ByteString
"REGR_SYY"
  regrSXYE :: Maybe (Sql92AggregationSetQuantifierSyntax PgExpressionSyntax)
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
regrSXYE = ByteString
-> Maybe PgAggregationSetQuantifierSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
pgBinAgg ByteString
"REGR_SXY"

instance IsSql2003NtileExpressionSyntax PgExpressionSyntax where
  ntileE :: PgExpressionSyntax -> PgExpressionSyntax
ntileE PgExpressionSyntax
x = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"NTILE(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
x PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")

instance IsSql2003LeadAndLagExpressionSyntax PgExpressionSyntax where
  leadE :: PgExpressionSyntax
-> Maybe PgExpressionSyntax
-> Maybe PgExpressionSyntax
-> PgExpressionSyntax
leadE PgExpressionSyntax
x Maybe PgExpressionSyntax
Nothing Maybe PgExpressionSyntax
Nothing =
    PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"LEAD(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
x PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")
  leadE PgExpressionSyntax
x (Just PgExpressionSyntax
n) Maybe PgExpressionSyntax
Nothing =
    PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"LEAD(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
x 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
n PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")
  leadE PgExpressionSyntax
x (Just PgExpressionSyntax
n) (Just PgExpressionSyntax
def) =
    PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"LEAD(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
x 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
n 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
def PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")
  leadE PgExpressionSyntax
x Maybe PgExpressionSyntax
Nothing (Just PgExpressionSyntax
def) =
    PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"LEAD(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
x PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
", 1, " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
def PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")

  lagE :: PgExpressionSyntax
-> Maybe PgExpressionSyntax
-> Maybe PgExpressionSyntax
-> PgExpressionSyntax
lagE PgExpressionSyntax
x Maybe PgExpressionSyntax
Nothing Maybe PgExpressionSyntax
Nothing =
    PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"LAG(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
x PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")
  lagE PgExpressionSyntax
x (Just PgExpressionSyntax
n) Maybe PgExpressionSyntax
Nothing =
    PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"LAG(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
x 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
n PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")
  lagE PgExpressionSyntax
x (Just PgExpressionSyntax
n) (Just PgExpressionSyntax
def) =
    PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"LAG(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
x 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
n 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
def PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")
  lagE PgExpressionSyntax
x Maybe PgExpressionSyntax
Nothing (Just PgExpressionSyntax
def) =
    PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"LAG(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
x PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
", 1, " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
def PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")

instance IsSql2003FirstValueAndLastValueExpressionSyntax PgExpressionSyntax where
  firstValueE :: PgExpressionSyntax -> PgExpressionSyntax
firstValueE PgExpressionSyntax
x = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"FIRST_VALUE(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
x PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")
  lastValueE :: PgExpressionSyntax -> PgExpressionSyntax
lastValueE PgExpressionSyntax
x = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"LAST_VALUE(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
x PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")

instance IsSql2003NthValueExpressionSyntax PgExpressionSyntax where
  nthValueE :: PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
nthValueE PgExpressionSyntax
x PgExpressionSyntax
n = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"NTH_VALUE(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
x 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
n PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")

instance IsSql2003WindowFrameSyntax PgWindowFrameSyntax where
  type Sql2003WindowFrameExpressionSyntax PgWindowFrameSyntax = PgExpressionSyntax
  type Sql2003WindowFrameOrderingSyntax PgWindowFrameSyntax = PgOrderingSyntax
  type Sql2003WindowFrameBoundsSyntax PgWindowFrameSyntax = PgWindowFrameBoundsSyntax

  frameSyntax :: Maybe [Sql2003WindowFrameExpressionSyntax PgWindowFrameSyntax]
-> Maybe [Sql2003WindowFrameOrderingSyntax PgWindowFrameSyntax]
-> Maybe (Sql2003WindowFrameBoundsSyntax PgWindowFrameSyntax)
-> PgWindowFrameSyntax
frameSyntax Maybe [Sql2003WindowFrameExpressionSyntax PgWindowFrameSyntax]
partition_ Maybe [Sql2003WindowFrameOrderingSyntax PgWindowFrameSyntax]
ordering_ Maybe (Sql2003WindowFrameBoundsSyntax PgWindowFrameSyntax)
bounds_ =
    PgSyntax -> PgWindowFrameSyntax
PgWindowFrameSyntax (PgSyntax -> PgWindowFrameSyntax)
-> PgSyntax -> PgWindowFrameSyntax
forall a b. (a -> b) -> a -> b
$
    ByteString -> PgSyntax
emit ByteString
"OVER " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
    PgSyntax -> PgSyntax
pgParens
    (
      PgSyntax
-> ([PgExpressionSyntax] -> PgSyntax)
-> Maybe [PgExpressionSyntax]
-> PgSyntax
forall b a. b -> (a -> b) -> Maybe a -> b
maybe PgSyntax
forall a. Monoid a => a
mempty (\[PgExpressionSyntax]
p -> ByteString -> PgSyntax
emit ByteString
"PARTITION BY " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSyntax -> [PgSyntax] -> PgSyntax
pgSepBy (ByteString -> PgSyntax
emit ByteString
", ") ((PgExpressionSyntax -> PgSyntax)
-> [PgExpressionSyntax] -> [PgSyntax]
forall a b. (a -> b) -> [a] -> [b]
map PgExpressionSyntax -> PgSyntax
fromPgExpression [PgExpressionSyntax]
p)) Maybe [Sql2003WindowFrameExpressionSyntax PgWindowFrameSyntax]
Maybe [PgExpressionSyntax]
partition_ PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
      PgSyntax
-> ([PgOrderingSyntax] -> PgSyntax)
-> Maybe [PgOrderingSyntax]
-> PgSyntax
forall b a. b -> (a -> b) -> Maybe a -> b
maybe PgSyntax
forall a. Monoid a => a
mempty (\[PgOrderingSyntax]
o -> ByteString -> PgSyntax
emit ByteString
" ORDER BY " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSyntax -> [PgSyntax] -> PgSyntax
pgSepBy (ByteString -> PgSyntax
emit ByteString
", ") ((PgOrderingSyntax -> PgSyntax) -> [PgOrderingSyntax] -> [PgSyntax]
forall a b. (a -> b) -> [a] -> [b]
map PgOrderingSyntax -> PgSyntax
fromPgOrdering [PgOrderingSyntax]
o)) Maybe [Sql2003WindowFrameOrderingSyntax PgWindowFrameSyntax]
Maybe [PgOrderingSyntax]
ordering_ PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
      PgSyntax
-> (PgWindowFrameBoundsSyntax -> PgSyntax)
-> Maybe PgWindowFrameBoundsSyntax
-> PgSyntax
forall b a. b -> (a -> b) -> Maybe a -> b
maybe PgSyntax
forall a. Monoid a => a
mempty (\PgWindowFrameBoundsSyntax
b -> ByteString -> PgSyntax
emit ByteString
" ROWS " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgWindowFrameBoundsSyntax -> PgSyntax
fromPgWindowFrameBounds PgWindowFrameBoundsSyntax
b) Maybe (Sql2003WindowFrameBoundsSyntax PgWindowFrameSyntax)
Maybe PgWindowFrameBoundsSyntax
bounds_
    )

instance IsSql2003WindowFrameBoundsSyntax PgWindowFrameBoundsSyntax where
  type Sql2003WindowFrameBoundsBoundSyntax PgWindowFrameBoundsSyntax = PgWindowFrameBoundSyntax

  fromToBoundSyntax :: Sql2003WindowFrameBoundsBoundSyntax PgWindowFrameBoundsSyntax
-> Maybe
     (Sql2003WindowFrameBoundsBoundSyntax PgWindowFrameBoundsSyntax)
-> PgWindowFrameBoundsSyntax
fromToBoundSyntax Sql2003WindowFrameBoundsBoundSyntax PgWindowFrameBoundsSyntax
from Maybe
  (Sql2003WindowFrameBoundsBoundSyntax PgWindowFrameBoundsSyntax)
Nothing =
    PgSyntax -> PgWindowFrameBoundsSyntax
PgWindowFrameBoundsSyntax (PgWindowFrameBoundSyntax -> ByteString -> PgSyntax
fromPgWindowFrameBound Sql2003WindowFrameBoundsBoundSyntax PgWindowFrameBoundsSyntax
PgWindowFrameBoundSyntax
from ByteString
"PRECEDING")
  fromToBoundSyntax Sql2003WindowFrameBoundsBoundSyntax PgWindowFrameBoundsSyntax
from (Just Sql2003WindowFrameBoundsBoundSyntax PgWindowFrameBoundsSyntax
to) =
    PgSyntax -> PgWindowFrameBoundsSyntax
PgWindowFrameBoundsSyntax (PgSyntax -> PgWindowFrameBoundsSyntax)
-> PgSyntax -> PgWindowFrameBoundsSyntax
forall a b. (a -> b) -> a -> b
$
    ByteString -> PgSyntax
emit ByteString
"BETWEEN " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgWindowFrameBoundSyntax -> ByteString -> PgSyntax
fromPgWindowFrameBound Sql2003WindowFrameBoundsBoundSyntax PgWindowFrameBoundsSyntax
PgWindowFrameBoundSyntax
from ByteString
"PRECEDING" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
" AND " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgWindowFrameBoundSyntax -> ByteString -> PgSyntax
fromPgWindowFrameBound Sql2003WindowFrameBoundsBoundSyntax PgWindowFrameBoundsSyntax
PgWindowFrameBoundSyntax
to ByteString
"FOLLOWING"

instance IsSql2003WindowFrameBoundSyntax PgWindowFrameBoundSyntax where
  unboundedSyntax :: PgWindowFrameBoundSyntax
unboundedSyntax = (ByteString -> PgSyntax) -> PgWindowFrameBoundSyntax
PgWindowFrameBoundSyntax ((ByteString -> PgSyntax) -> PgWindowFrameBoundSyntax)
-> (ByteString -> PgSyntax) -> PgWindowFrameBoundSyntax
forall a b. (a -> b) -> a -> b
$ \ByteString
where_ -> ByteString -> PgSyntax
emit ByteString
"UNBOUNDED " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
where_
  nrowsBoundSyntax :: Int -> PgWindowFrameBoundSyntax
nrowsBoundSyntax Int
0 = (ByteString -> PgSyntax) -> PgWindowFrameBoundSyntax
PgWindowFrameBoundSyntax ((ByteString -> PgSyntax) -> PgWindowFrameBoundSyntax)
-> (ByteString -> PgSyntax) -> PgWindowFrameBoundSyntax
forall a b. (a -> b) -> a -> b
$ \ByteString
_ -> ByteString -> PgSyntax
emit ByteString
"CURRENT ROW"
  nrowsBoundSyntax Int
n = (ByteString -> PgSyntax) -> PgWindowFrameBoundSyntax
PgWindowFrameBoundSyntax ((ByteString -> PgSyntax) -> PgWindowFrameBoundSyntax)
-> (ByteString -> PgSyntax) -> PgWindowFrameBoundSyntax
forall a b. (a -> b) -> a -> b
$ \ByteString
where_ -> ByteString -> PgSyntax
emit (String -> ByteString
forall a. IsString a => String -> a
fromString (Int -> String
forall a. Show a => a -> String
show Int
n)) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
" " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
where_

instance IsSql92AggregationExpressionSyntax PgExpressionSyntax where
  type Sql92AggregationSetQuantifierSyntax PgExpressionSyntax = PgAggregationSetQuantifierSyntax

  countAllE :: PgExpressionSyntax
countAllE = PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (ByteString -> PgSyntax
emit ByteString
"COUNT(*)")
  countE :: Maybe (Sql92AggregationSetQuantifierSyntax PgExpressionSyntax)
-> PgExpressionSyntax -> PgExpressionSyntax
countE = ByteString
-> Maybe PgAggregationSetQuantifierSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
pgUnAgg ByteString
"COUNT"
  avgE :: Maybe (Sql92AggregationSetQuantifierSyntax PgExpressionSyntax)
-> PgExpressionSyntax -> PgExpressionSyntax
avgE = ByteString
-> Maybe PgAggregationSetQuantifierSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
pgUnAgg ByteString
"AVG"
  sumE :: Maybe (Sql92AggregationSetQuantifierSyntax PgExpressionSyntax)
-> PgExpressionSyntax -> PgExpressionSyntax
sumE = ByteString
-> Maybe PgAggregationSetQuantifierSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
pgUnAgg ByteString
"SUM"
  minE :: Maybe (Sql92AggregationSetQuantifierSyntax PgExpressionSyntax)
-> PgExpressionSyntax -> PgExpressionSyntax
minE = ByteString
-> Maybe PgAggregationSetQuantifierSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
pgUnAgg ByteString
"MIN"
  maxE :: Maybe (Sql92AggregationSetQuantifierSyntax PgExpressionSyntax)
-> PgExpressionSyntax -> PgExpressionSyntax
maxE = ByteString
-> Maybe PgAggregationSetQuantifierSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
pgUnAgg ByteString
"MAX"

instance IsSql99AggregationExpressionSyntax PgExpressionSyntax where
  everyE :: Maybe (Sql92AggregationSetQuantifierSyntax PgExpressionSyntax)
-> PgExpressionSyntax -> PgExpressionSyntax
everyE = ByteString
-> Maybe PgAggregationSetQuantifierSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
pgUnAgg ByteString
"EVERY"

  -- According to the note at <https://www.postgresql.org/docs/9.2/static/functions-aggregate.html>
  -- the following functions are equivalent.
  someE :: Maybe (Sql92AggregationSetQuantifierSyntax PgExpressionSyntax)
-> PgExpressionSyntax -> PgExpressionSyntax
someE = ByteString
-> Maybe PgAggregationSetQuantifierSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
pgUnAgg ByteString
"BOOL_ANY"
  anyE :: Maybe (Sql92AggregationSetQuantifierSyntax PgExpressionSyntax)
-> PgExpressionSyntax -> PgExpressionSyntax
anyE = ByteString
-> Maybe PgAggregationSetQuantifierSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
pgUnAgg ByteString
"BOOL_ANY"

instance IsSql92AggregationSetQuantifierSyntax PgAggregationSetQuantifierSyntax where
  setQuantifierDistinct :: PgAggregationSetQuantifierSyntax
setQuantifierDistinct = PgSyntax -> PgAggregationSetQuantifierSyntax
PgAggregationSetQuantifierSyntax (PgSyntax -> PgAggregationSetQuantifierSyntax)
-> PgSyntax -> PgAggregationSetQuantifierSyntax
forall a b. (a -> b) -> a -> b
$ ByteString -> PgSyntax
emit ByteString
"DISTINCT"
  setQuantifierAll :: PgAggregationSetQuantifierSyntax
setQuantifierAll = PgSyntax -> PgAggregationSetQuantifierSyntax
PgAggregationSetQuantifierSyntax (PgSyntax -> PgAggregationSetQuantifierSyntax)
-> PgSyntax -> PgAggregationSetQuantifierSyntax
forall a b. (a -> b) -> a -> b
$ ByteString -> PgSyntax
emit ByteString
"ALL"

instance IsSql92AggregationSetQuantifierSyntax PgSelectSetQuantifierSyntax where
  setQuantifierDistinct :: PgSelectSetQuantifierSyntax
setQuantifierDistinct = PgSyntax -> PgSelectSetQuantifierSyntax
PgSelectSetQuantifierSyntax (PgSyntax -> PgSelectSetQuantifierSyntax)
-> PgSyntax -> PgSelectSetQuantifierSyntax
forall a b. (a -> b) -> a -> b
$ ByteString -> PgSyntax
emit ByteString
"DISTINCT"
  setQuantifierAll :: PgSelectSetQuantifierSyntax
setQuantifierAll = PgSyntax -> PgSelectSetQuantifierSyntax
PgSelectSetQuantifierSyntax (PgSyntax -> PgSelectSetQuantifierSyntax)
-> PgSyntax -> PgSelectSetQuantifierSyntax
forall a b. (a -> b) -> a -> b
$ ByteString -> PgSyntax
emit ByteString
"ALL"

pgSelectSetQuantifierDistinctOn :: [PgExpressionSyntax] -> PgSelectSetQuantifierSyntax
pgSelectSetQuantifierDistinctOn :: [PgExpressionSyntax] -> PgSelectSetQuantifierSyntax
pgSelectSetQuantifierDistinctOn [PgExpressionSyntax]
exprs =
  PgSyntax -> PgSelectSetQuantifierSyntax
PgSelectSetQuantifierSyntax (PgSyntax -> PgSelectSetQuantifierSyntax)
-> PgSyntax -> PgSelectSetQuantifierSyntax
forall a b. (a -> b) -> a -> b
$
  ByteString -> PgSyntax
emit ByteString
"DISTINCT ON " 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 -> PgSyntax)
-> [PgExpressionSyntax] -> [PgSyntax]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [PgExpressionSyntax]
exprs))

pgUnAgg :: ByteString -> Maybe PgAggregationSetQuantifierSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgUnAgg :: ByteString
-> Maybe PgAggregationSetQuantifierSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
pgUnAgg ByteString
fn Maybe PgAggregationSetQuantifierSyntax
q PgExpressionSyntax
e =
  PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax) -> PgSyntax -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$
  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
<> 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
q PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
e PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")"

pgBinAgg :: ByteString -> Maybe PgAggregationSetQuantifierSyntax -> PgExpressionSyntax -> PgExpressionSyntax
         -> PgExpressionSyntax
pgBinAgg :: ByteString
-> Maybe PgAggregationSetQuantifierSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
pgBinAgg ByteString
fn Maybe PgAggregationSetQuantifierSyntax
q PgExpressionSyntax
x PgExpressionSyntax
y =
  PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax) -> PgSyntax -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$
  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
<> 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
q
          PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
x 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
y PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")"

instance IsSql92FieldNameSyntax PgFieldNameSyntax where
  qualifiedField :: Text -> Text -> PgFieldNameSyntax
qualifiedField Text
a Text
b =
    PgSyntax -> PgFieldNameSyntax
PgFieldNameSyntax (PgSyntax -> PgFieldNameSyntax) -> PgSyntax -> PgFieldNameSyntax
forall a b. (a -> b) -> a -> b
$
    Text -> PgSyntax
pgQuotedIdentifier Text
a PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
"." PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> Text -> PgSyntax
pgQuotedIdentifier Text
b
  unqualifiedField :: Text -> PgFieldNameSyntax
unqualifiedField = PgSyntax -> PgFieldNameSyntax
PgFieldNameSyntax (PgSyntax -> PgFieldNameSyntax)
-> (Text -> PgSyntax) -> Text -> PgFieldNameSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> PgSyntax
pgQuotedIdentifier

instance IsSql92TableSourceSyntax PgTableSourceSyntax where
  type Sql92TableSourceSelectSyntax PgTableSourceSyntax = PgSelectSyntax
  type Sql92TableSourceExpressionSyntax PgTableSourceSyntax = PgExpressionSyntax
  type Sql92TableSourceTableNameSyntax PgTableSourceSyntax = PgTableNameSyntax

  tableNamed :: Sql92TableSourceTableNameSyntax PgTableSourceSyntax
-> PgTableSourceSyntax
tableNamed = PgSyntax -> PgTableSourceSyntax
PgTableSourceSyntax (PgSyntax -> PgTableSourceSyntax)
-> (PgTableNameSyntax -> PgSyntax)
-> PgTableNameSyntax
-> PgTableSourceSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgTableNameSyntax -> PgSyntax
fromPgTableName
  tableFromSubSelect :: Sql92TableSourceSelectSyntax PgTableSourceSyntax
-> PgTableSourceSyntax
tableFromSubSelect Sql92TableSourceSelectSyntax PgTableSourceSyntax
s = PgSyntax -> PgTableSourceSyntax
PgTableSourceSyntax (PgSyntax -> PgTableSourceSyntax)
-> PgSyntax -> PgTableSourceSyntax
forall a b. (a -> b) -> a -> b
$ ByteString -> PgSyntax
emit ByteString
"(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSelectSyntax -> PgSyntax
fromPgSelect Sql92TableSourceSelectSyntax PgTableSourceSyntax
PgSelectSyntax
s PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")"
  tableFromValues :: [[Sql92TableSourceExpressionSyntax PgTableSourceSyntax]]
-> PgTableSourceSyntax
tableFromValues [[Sql92TableSourceExpressionSyntax PgTableSourceSyntax]]
vss = PgSyntax -> PgTableSourceSyntax
PgTableSourceSyntax (PgSyntax -> PgTableSourceSyntax)
-> (PgSyntax -> PgSyntax) -> PgSyntax -> PgTableSourceSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> PgSyntax
pgParens (PgSyntax -> PgTableSourceSyntax)
-> PgSyntax -> PgTableSourceSyntax
forall a b. (a -> b) -> a -> b
$
                        ByteString -> PgSyntax
emit ByteString
"VALUES " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
                        PgSyntax -> [PgSyntax] -> PgSyntax
pgSepBy (ByteString -> PgSyntax
emit ByteString
", ")
                                (([PgExpressionSyntax] -> PgSyntax)
-> [[PgExpressionSyntax]] -> [PgSyntax]
forall a b. (a -> b) -> [a] -> [b]
map (\[PgExpressionSyntax]
vs -> PgSyntax -> PgSyntax
pgParens (PgSyntax -> [PgSyntax] -> PgSyntax
pgSepBy (ByteString -> PgSyntax
emit ByteString
", ")
                                                               ((PgExpressionSyntax -> PgSyntax)
-> [PgExpressionSyntax] -> [PgSyntax]
forall a b. (a -> b) -> [a] -> [b]
map PgExpressionSyntax -> PgSyntax
fromPgExpression [PgExpressionSyntax]
vs))) [[Sql92TableSourceExpressionSyntax PgTableSourceSyntax]]
[[PgExpressionSyntax]]
vss)

instance IsSql92ProjectionSyntax PgProjectionSyntax where
  type Sql92ProjectionExpressionSyntax PgProjectionSyntax = PgExpressionSyntax

  projExprs :: [(Sql92ProjectionExpressionSyntax PgProjectionSyntax, Maybe Text)]
-> PgProjectionSyntax
projExprs [(Sql92ProjectionExpressionSyntax PgProjectionSyntax, Maybe Text)]
exprs =
    PgSyntax -> PgProjectionSyntax
PgProjectionSyntax (PgSyntax -> PgProjectionSyntax) -> PgSyntax -> PgProjectionSyntax
forall a b. (a -> b) -> a -> b
$
    PgSyntax -> [PgSyntax] -> PgSyntax
pgSepBy (ByteString -> PgSyntax
emit ByteString
", ")
            (((PgExpressionSyntax, Maybe Text) -> PgSyntax)
-> [(PgExpressionSyntax, Maybe Text)] -> [PgSyntax]
forall a b. (a -> b) -> [a] -> [b]
map (\(PgExpressionSyntax
expr, Maybe Text
nm) -> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
expr PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
                                 PgSyntax -> (Text -> PgSyntax) -> Maybe Text -> PgSyntax
forall b a. b -> (a -> b) -> Maybe a -> b
maybe PgSyntax
forall a. Monoid a => a
mempty (\Text
nm -> ByteString -> PgSyntax
emit ByteString
" AS " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> Text -> PgSyntax
pgQuotedIdentifier Text
nm) Maybe Text
nm) [(Sql92ProjectionExpressionSyntax PgProjectionSyntax, Maybe Text)]
[(PgExpressionSyntax, Maybe Text)]
exprs)

instance IsSql92InsertSyntax PgInsertSyntax where
  type Sql92InsertTableNameSyntax PgInsertSyntax = PgTableNameSyntax
  type Sql92InsertValuesSyntax PgInsertSyntax = PgInsertValuesSyntax

  insertStmt :: Sql92InsertTableNameSyntax PgInsertSyntax
-> [Text]
-> Sql92InsertValuesSyntax PgInsertSyntax
-> PgInsertSyntax
insertStmt Sql92InsertTableNameSyntax PgInsertSyntax
tblName [Text]
fields Sql92InsertValuesSyntax PgInsertSyntax
values =
      PgSyntax -> PgInsertSyntax
PgInsertSyntax (PgSyntax -> PgInsertSyntax) -> PgSyntax -> PgInsertSyntax
forall a b. (a -> b) -> a -> b
$
      ByteString -> PgSyntax
emit ByteString
"INSERT INTO " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgTableNameSyntax -> PgSyntax
fromPgTableName Sql92InsertTableNameSyntax PgInsertSyntax
PgTableNameSyntax
tblName 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 -> [PgSyntax] -> PgSyntax
pgSepBy (ByteString -> PgSyntax
emit ByteString
", ") ((Text -> PgSyntax) -> [Text] -> [PgSyntax]
forall a b. (a -> b) -> [a] -> [b]
map Text -> PgSyntax
pgQuotedIdentifier [Text]
fields) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
      ByteString -> PgSyntax
emit ByteString
") " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgInsertValuesSyntax -> PgSyntax
fromPgInsertValues Sql92InsertValuesSyntax PgInsertSyntax
PgInsertValuesSyntax
values

instance IsSql92InsertValuesSyntax PgInsertValuesSyntax where
  type Sql92InsertValuesExpressionSyntax PgInsertValuesSyntax = PgExpressionSyntax
  type Sql92InsertValuesSelectSyntax PgInsertValuesSyntax = PgSelectSyntax

  insertSqlExpressions :: [[Sql92InsertValuesExpressionSyntax PgInsertValuesSyntax]]
-> PgInsertValuesSyntax
insertSqlExpressions [[Sql92InsertValuesExpressionSyntax PgInsertValuesSyntax]]
es =
      PgSyntax -> PgInsertValuesSyntax
PgInsertValuesSyntax (PgSyntax -> PgInsertValuesSyntax)
-> PgSyntax -> PgInsertValuesSyntax
forall a b. (a -> b) -> a -> b
$
      ByteString -> PgSyntax
emit ByteString
"VALUES " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
      PgSyntax -> [PgSyntax] -> PgSyntax
pgSepBy (ByteString -> PgSyntax
emit ByteString
", ")
              (([PgExpressionSyntax] -> PgSyntax)
-> [[PgExpressionSyntax]] -> [PgSyntax]
forall a b. (a -> b) -> [a] -> [b]
map (\[PgExpressionSyntax]
es -> ByteString -> PgSyntax
emit ByteString
"(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSyntax -> [PgSyntax] -> PgSyntax
pgSepBy (ByteString -> PgSyntax
emit ByteString
", ") ([PgExpressionSyntax] -> [PgSyntax]
coerce [PgExpressionSyntax]
es) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")
                   [[Sql92InsertValuesExpressionSyntax PgInsertValuesSyntax]]
[[PgExpressionSyntax]]
es)
  insertFromSql :: Sql92InsertValuesSelectSyntax PgInsertValuesSyntax
-> PgInsertValuesSyntax
insertFromSql (PgSelectSyntax a) = PgSyntax -> PgInsertValuesSyntax
PgInsertValuesSyntax PgSyntax
a

instance IsSql92DropTableSyntax PgDropTableSyntax where
  type Sql92DropTableTableNameSyntax PgDropTableSyntax = PgTableNameSyntax

  dropTableSyntax :: Sql92DropTableTableNameSyntax PgDropTableSyntax
-> PgDropTableSyntax
dropTableSyntax Sql92DropTableTableNameSyntax PgDropTableSyntax
tblNm =
    PgSyntax -> PgDropTableSyntax
PgDropTableSyntax (PgSyntax -> PgDropTableSyntax) -> PgSyntax -> PgDropTableSyntax
forall a b. (a -> b) -> a -> b
$
    ByteString -> PgSyntax
emit ByteString
"DROP TABLE " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgTableNameSyntax -> PgSyntax
fromPgTableName Sql92DropTableTableNameSyntax PgDropTableSyntax
PgTableNameSyntax
tblNm

instance IsSql92AlterTableSyntax PgAlterTableSyntax where
  type Sql92AlterTableAlterTableActionSyntax PgAlterTableSyntax = PgAlterTableActionSyntax
  type Sql92AlterTableTableNameSyntax PgAlterTableSyntax = PgTableNameSyntax

  alterTableSyntax :: Sql92AlterTableTableNameSyntax PgAlterTableSyntax
-> Sql92AlterTableAlterTableActionSyntax PgAlterTableSyntax
-> PgAlterTableSyntax
alterTableSyntax Sql92AlterTableTableNameSyntax PgAlterTableSyntax
tblNm Sql92AlterTableAlterTableActionSyntax PgAlterTableSyntax
action =
    PgSyntax -> PgAlterTableSyntax
PgAlterTableSyntax (PgSyntax -> PgAlterTableSyntax) -> PgSyntax -> PgAlterTableSyntax
forall a b. (a -> b) -> a -> b
$
    ByteString -> PgSyntax
emit ByteString
"ALTER TABLE " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgTableNameSyntax -> PgSyntax
fromPgTableName Sql92AlterTableTableNameSyntax PgAlterTableSyntax
PgTableNameSyntax
tblNm PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
" " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgAlterTableActionSyntax -> PgSyntax
fromPgAlterTableAction Sql92AlterTableAlterTableActionSyntax PgAlterTableSyntax
PgAlterTableActionSyntax
action

instance IsSql92AlterTableActionSyntax PgAlterTableActionSyntax where
  type Sql92AlterTableAlterColumnActionSyntax PgAlterTableActionSyntax = PgAlterColumnActionSyntax
  type Sql92AlterTableColumnSchemaSyntax PgAlterTableActionSyntax = PgColumnSchemaSyntax

  alterColumnSyntax :: Text
-> Sql92AlterTableAlterColumnActionSyntax PgAlterTableActionSyntax
-> PgAlterTableActionSyntax
alterColumnSyntax Text
colNm Sql92AlterTableAlterColumnActionSyntax PgAlterTableActionSyntax
action =
    PgSyntax -> PgAlterTableActionSyntax
PgAlterTableActionSyntax (PgSyntax -> PgAlterTableActionSyntax)
-> PgSyntax -> PgAlterTableActionSyntax
forall a b. (a -> b) -> a -> b
$
    ByteString -> PgSyntax
emit ByteString
"ALTER COLUMN " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> Text -> PgSyntax
pgQuotedIdentifier Text
colNm PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
" " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgAlterColumnActionSyntax -> PgSyntax
fromPgAlterColumnAction Sql92AlterTableAlterColumnActionSyntax PgAlterTableActionSyntax
PgAlterColumnActionSyntax
action

  addColumnSyntax :: Text
-> Sql92AlterTableColumnSchemaSyntax PgAlterTableActionSyntax
-> PgAlterTableActionSyntax
addColumnSyntax Text
colNm Sql92AlterTableColumnSchemaSyntax PgAlterTableActionSyntax
schema =
    PgSyntax -> PgAlterTableActionSyntax
PgAlterTableActionSyntax (PgSyntax -> PgAlterTableActionSyntax)
-> PgSyntax -> PgAlterTableActionSyntax
forall a b. (a -> b) -> a -> b
$
    ByteString -> PgSyntax
emit ByteString
"ADD COLUMN " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> Text -> PgSyntax
pgQuotedIdentifier Text
colNm PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
" " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgColumnSchemaSyntax -> PgSyntax
fromPgColumnSchema Sql92AlterTableColumnSchemaSyntax PgAlterTableActionSyntax
PgColumnSchemaSyntax
schema

  dropColumnSyntax :: Text -> PgAlterTableActionSyntax
dropColumnSyntax Text
colNm =
    PgSyntax -> PgAlterTableActionSyntax
PgAlterTableActionSyntax (PgSyntax -> PgAlterTableActionSyntax)
-> PgSyntax -> PgAlterTableActionSyntax
forall a b. (a -> b) -> a -> b
$
    ByteString -> PgSyntax
emit ByteString
"DROP COLUMN " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> Text -> PgSyntax
pgQuotedIdentifier Text
colNm

  renameTableToSyntax :: Text -> PgAlterTableActionSyntax
renameTableToSyntax Text
newNm =
    PgSyntax -> PgAlterTableActionSyntax
PgAlterTableActionSyntax (PgSyntax -> PgAlterTableActionSyntax)
-> PgSyntax -> PgAlterTableActionSyntax
forall a b. (a -> b) -> a -> b
$
    ByteString -> PgSyntax
emit ByteString
"RENAME TO " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> Text -> PgSyntax
pgQuotedIdentifier Text
newNm

  renameColumnToSyntax :: Text -> Text -> PgAlterTableActionSyntax
renameColumnToSyntax Text
oldNm Text
newNm =
    PgSyntax -> PgAlterTableActionSyntax
PgAlterTableActionSyntax (PgSyntax -> PgAlterTableActionSyntax)
-> PgSyntax -> PgAlterTableActionSyntax
forall a b. (a -> b) -> a -> b
$
    ByteString -> PgSyntax
emit ByteString
"RENAME COLUMN " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> Text -> PgSyntax
pgQuotedIdentifier Text
oldNm PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
" TO " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> Text -> PgSyntax
pgQuotedIdentifier Text
newNm

instance IsSql92AlterColumnActionSyntax PgAlterColumnActionSyntax where
  setNullSyntax :: PgAlterColumnActionSyntax
setNullSyntax = PgSyntax -> PgAlterColumnActionSyntax
PgAlterColumnActionSyntax (ByteString -> PgSyntax
emit ByteString
"DROP NOT NULL")
  setNotNullSyntax :: PgAlterColumnActionSyntax
setNotNullSyntax = PgSyntax -> PgAlterColumnActionSyntax
PgAlterColumnActionSyntax (ByteString -> PgSyntax
emit ByteString
"SET NOT NULL")

instance IsSql92CreateTableSyntax PgCreateTableSyntax where
  type Sql92CreateTableTableNameSyntax PgCreateTableSyntax = PgTableNameSyntax
  type Sql92CreateTableColumnSchemaSyntax PgCreateTableSyntax = PgColumnSchemaSyntax
  type Sql92CreateTableTableConstraintSyntax PgCreateTableSyntax = PgTableConstraintSyntax
  type Sql92CreateTableOptionsSyntax PgCreateTableSyntax = PgTableOptionsSyntax

  createTableSyntax :: Maybe (Sql92CreateTableOptionsSyntax PgCreateTableSyntax)
-> Sql92CreateTableTableNameSyntax PgCreateTableSyntax
-> [(Text, Sql92CreateTableColumnSchemaSyntax PgCreateTableSyntax)]
-> [Sql92CreateTableTableConstraintSyntax PgCreateTableSyntax]
-> PgCreateTableSyntax
createTableSyntax Maybe (Sql92CreateTableOptionsSyntax PgCreateTableSyntax)
options Sql92CreateTableTableNameSyntax PgCreateTableSyntax
tblNm [(Text, Sql92CreateTableColumnSchemaSyntax PgCreateTableSyntax)]
fieldTypes [Sql92CreateTableTableConstraintSyntax PgCreateTableSyntax]
constraints =
    let (PgSyntax
beforeOptions, PgSyntax
afterOptions) =
          case Maybe (Sql92CreateTableOptionsSyntax PgCreateTableSyntax)
options of
            Maybe (Sql92CreateTableOptionsSyntax PgCreateTableSyntax)
Nothing -> (ByteString -> PgSyntax
emit ByteString
" ", ByteString -> PgSyntax
emit ByteString
" ")
            Just (PgTableOptionsSyntax before after) ->
              ( ByteString -> PgSyntax
emit ByteString
" " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSyntax
before PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
" "
              , ByteString -> PgSyntax
emit ByteString
" " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSyntax
after PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
" " )
    in PgSyntax -> PgCreateTableSyntax
PgCreateTableSyntax (PgSyntax -> PgCreateTableSyntax)
-> PgSyntax -> PgCreateTableSyntax
forall a b. (a -> b) -> a -> b
$
       ByteString -> PgSyntax
emit ByteString
"CREATE" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSyntax
beforeOptions PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
"TABLE " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgTableNameSyntax -> PgSyntax
fromPgTableName Sql92CreateTableTableNameSyntax PgCreateTableSyntax
PgTableNameSyntax
tblNm 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 -> [PgSyntax] -> PgSyntax
pgSepBy (ByteString -> PgSyntax
emit ByteString
", ")
               (((Text, PgColumnSchemaSyntax) -> PgSyntax)
-> [(Text, PgColumnSchemaSyntax)] -> [PgSyntax]
forall a b. (a -> b) -> [a] -> [b]
map (\(Text
nm, PgColumnSchemaSyntax
type_) -> Text -> PgSyntax
pgQuotedIdentifier Text
nm PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
" " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgColumnSchemaSyntax -> PgSyntax
fromPgColumnSchema PgColumnSchemaSyntax
type_)  [(Text, Sql92CreateTableColumnSchemaSyntax PgCreateTableSyntax)]
[(Text, PgColumnSchemaSyntax)]
fieldTypes [PgSyntax] -> [PgSyntax] -> [PgSyntax]
forall a. Semigroup a => a -> a -> a
<>
                (PgTableConstraintSyntax -> PgSyntax)
-> [PgTableConstraintSyntax] -> [PgSyntax]
forall a b. (a -> b) -> [a] -> [b]
map PgTableConstraintSyntax -> PgSyntax
fromPgTableConstraint [Sql92CreateTableTableConstraintSyntax PgCreateTableSyntax]
[PgTableConstraintSyntax]
constraints)
       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
afterOptions

instance IsSql92TableConstraintSyntax PgTableConstraintSyntax where
  primaryKeyConstraintSyntax :: [Text] -> PgTableConstraintSyntax
primaryKeyConstraintSyntax [Text]
fieldNames =
    PgSyntax -> PgTableConstraintSyntax
PgTableConstraintSyntax (PgSyntax -> PgTableConstraintSyntax)
-> PgSyntax -> PgTableConstraintSyntax
forall a b. (a -> b) -> a -> b
$
    ByteString -> PgSyntax
emit ByteString
"PRIMARY KEY(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSyntax -> [PgSyntax] -> PgSyntax
pgSepBy (ByteString -> PgSyntax
emit ByteString
", ") ((Text -> PgSyntax) -> [Text] -> [PgSyntax]
forall a b. (a -> b) -> [a] -> [b]
map Text -> PgSyntax
pgQuotedIdentifier [Text]
fieldNames) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")"

instance Hashable PgColumnSchemaSyntax where
  hashWithSalt :: Int -> PgColumnSchemaSyntax -> Int
hashWithSalt Int
salt = Int -> PgSyntax -> Int
forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
salt (PgSyntax -> Int)
-> (PgColumnSchemaSyntax -> PgSyntax)
-> PgColumnSchemaSyntax
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgColumnSchemaSyntax -> PgSyntax
fromPgColumnSchema
instance IsSql92ColumnSchemaSyntax PgColumnSchemaSyntax where
  type Sql92ColumnSchemaColumnTypeSyntax PgColumnSchemaSyntax = PgDataTypeSyntax
  type Sql92ColumnSchemaExpressionSyntax PgColumnSchemaSyntax = PgExpressionSyntax
  type Sql92ColumnSchemaColumnConstraintDefinitionSyntax PgColumnSchemaSyntax = PgColumnConstraintDefinitionSyntax

  columnSchemaSyntax :: Sql92ColumnSchemaColumnTypeSyntax PgColumnSchemaSyntax
-> Maybe (Sql92ColumnSchemaExpressionSyntax PgColumnSchemaSyntax)
-> [Sql92ColumnSchemaColumnConstraintDefinitionSyntax
      PgColumnSchemaSyntax]
-> Maybe Text
-> PgColumnSchemaSyntax
columnSchemaSyntax Sql92ColumnSchemaColumnTypeSyntax PgColumnSchemaSyntax
colType Maybe (Sql92ColumnSchemaExpressionSyntax PgColumnSchemaSyntax)
defaultClause [Sql92ColumnSchemaColumnConstraintDefinitionSyntax
   PgColumnSchemaSyntax]
constraints Maybe Text
collation =
    PgSyntax -> PgColumnSchemaSyntax
PgColumnSchemaSyntax PgSyntax
syntax
    where
      syntax :: PgSyntax
syntax =
        PgDataTypeSyntax -> PgSyntax
fromPgDataType Sql92ColumnSchemaColumnTypeSyntax PgColumnSchemaSyntax
PgDataTypeSyntax
colType PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
        PgSyntax
-> (PgExpressionSyntax -> PgSyntax)
-> Maybe PgExpressionSyntax
-> PgSyntax
forall b a. b -> (a -> b) -> Maybe a -> b
maybe PgSyntax
forall a. Monoid a => a
mempty (\PgExpressionSyntax
d -> ByteString -> PgSyntax
emit ByteString
" DEFAULT " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
d) Maybe (Sql92ColumnSchemaExpressionSyntax PgColumnSchemaSyntax)
Maybe PgExpressionSyntax
defaultClause PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
        (case [Sql92ColumnSchemaColumnConstraintDefinitionSyntax
   PgColumnSchemaSyntax]
constraints of
           [] -> PgSyntax
forall a. Monoid a => a
mempty
           [Sql92ColumnSchemaColumnConstraintDefinitionSyntax
   PgColumnSchemaSyntax]
_ -> (PgColumnConstraintDefinitionSyntax -> PgSyntax)
-> [PgColumnConstraintDefinitionSyntax] -> PgSyntax
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (\PgColumnConstraintDefinitionSyntax
c -> ByteString -> PgSyntax
emit ByteString
" " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgColumnConstraintDefinitionSyntax -> PgSyntax
fromPgColumnConstraintDefinition PgColumnConstraintDefinitionSyntax
c) [Sql92ColumnSchemaColumnConstraintDefinitionSyntax
   PgColumnSchemaSyntax]
[PgColumnConstraintDefinitionSyntax]
constraints) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
        PgSyntax -> (Text -> PgSyntax) -> Maybe Text -> PgSyntax
forall b a. b -> (a -> b) -> Maybe a -> b
maybe PgSyntax
forall a. Monoid a => a
mempty (\Text
nm -> ByteString -> PgSyntax
emit ByteString
" COLLATE " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> Text -> PgSyntax
pgQuotedIdentifier Text
nm) Maybe Text
collation

instance IsSql92MatchTypeSyntax PgMatchTypeSyntax where
  fullMatchSyntax :: PgMatchTypeSyntax
fullMatchSyntax = PgSyntax -> BeamSerializedMatchType -> PgMatchTypeSyntax
PgMatchTypeSyntax (ByteString -> PgSyntax
emit ByteString
"FULL") BeamSerializedMatchType
forall match. IsSql92MatchTypeSyntax match => match
fullMatchSyntax
  partialMatchSyntax :: PgMatchTypeSyntax
partialMatchSyntax = PgSyntax -> BeamSerializedMatchType -> PgMatchTypeSyntax
PgMatchTypeSyntax (ByteString -> PgSyntax
emit ByteString
"PARTIAL") BeamSerializedMatchType
forall match. IsSql92MatchTypeSyntax match => match
partialMatchSyntax

pgMatchTypeJSON :: Value -> BeamSerializedMatchType
pgMatchTypeJSON :: Value -> BeamSerializedMatchType
pgMatchTypeJSON Value
v = Value -> BeamSerializedMatchType
BeamSerializedMatchType (Text -> Value -> Value
beamSerializeJSON Text
"postgres" Value
v)

pgSimpleMatchSyntax :: PgMatchTypeSyntax
pgSimpleMatchSyntax :: PgMatchTypeSyntax
pgSimpleMatchSyntax = PgSyntax -> BeamSerializedMatchType -> PgMatchTypeSyntax
PgMatchTypeSyntax (ByteString -> PgSyntax
emit ByteString
"SIMPLE") (Value -> BeamSerializedMatchType
pgMatchTypeJSON Value
"simple")

instance IsSql92ReferentialActionSyntax PgReferentialActionSyntax where
  referentialActionCascadeSyntax :: PgReferentialActionSyntax
referentialActionCascadeSyntax = PgSyntax
-> BeamSerializedReferentialAction -> PgReferentialActionSyntax
PgReferentialActionSyntax (ByteString -> PgSyntax
emit ByteString
"CASCADE") BeamSerializedReferentialAction
forall refAction.
IsSql92ReferentialActionSyntax refAction =>
refAction
referentialActionCascadeSyntax
  referentialActionNoActionSyntax :: PgReferentialActionSyntax
referentialActionNoActionSyntax = PgSyntax
-> BeamSerializedReferentialAction -> PgReferentialActionSyntax
PgReferentialActionSyntax (ByteString -> PgSyntax
emit ByteString
"NO ACTION") BeamSerializedReferentialAction
forall refAction.
IsSql92ReferentialActionSyntax refAction =>
refAction
referentialActionNoActionSyntax
  referentialActionSetDefaultSyntax :: PgReferentialActionSyntax
referentialActionSetDefaultSyntax = PgSyntax
-> BeamSerializedReferentialAction -> PgReferentialActionSyntax
PgReferentialActionSyntax (ByteString -> PgSyntax
emit ByteString
"SET DEFAULT") BeamSerializedReferentialAction
forall refAction.
IsSql92ReferentialActionSyntax refAction =>
refAction
referentialActionSetDefaultSyntax
  referentialActionSetNullSyntax :: PgReferentialActionSyntax
referentialActionSetNullSyntax = PgSyntax
-> BeamSerializedReferentialAction -> PgReferentialActionSyntax
PgReferentialActionSyntax (ByteString -> PgSyntax
emit ByteString
"SET NULL") BeamSerializedReferentialAction
forall refAction.
IsSql92ReferentialActionSyntax refAction =>
refAction
referentialActionSetNullSyntax

fromSqlConstraintAttributes :: SqlConstraintAttributesBuilder -> PgSyntax
fromSqlConstraintAttributes :: SqlConstraintAttributesBuilder -> PgSyntax
fromSqlConstraintAttributes (SqlConstraintAttributesBuilder Maybe ConstraintAttributeTiming
timing Maybe Bool
deferrable) =
  PgSyntax
-> (ConstraintAttributeTiming -> PgSyntax)
-> Maybe ConstraintAttributeTiming
-> PgSyntax
forall b a. b -> (a -> b) -> Maybe a -> b
maybe PgSyntax
forall a. Monoid a => a
mempty ConstraintAttributeTiming -> PgSyntax
timingBuilder Maybe ConstraintAttributeTiming
timing PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSyntax -> (Bool -> PgSyntax) -> Maybe Bool -> PgSyntax
forall b a. b -> (a -> b) -> Maybe a -> b
maybe PgSyntax
forall a. Monoid a => a
mempty Bool -> PgSyntax
deferrableBuilder Maybe Bool
deferrable
  where timingBuilder :: ConstraintAttributeTiming -> PgSyntax
timingBuilder ConstraintAttributeTiming
InitiallyDeferred = ByteString -> PgSyntax
emit ByteString
"INITIALLY DEFERRED"
        timingBuilder ConstraintAttributeTiming
InitiallyImmediate = ByteString -> PgSyntax
emit ByteString
"INITIALLY IMMEDIATE"
        deferrableBuilder :: Bool -> PgSyntax
deferrableBuilder Bool
False = ByteString -> PgSyntax
emit ByteString
"NOT DEFERRABLE"
        deferrableBuilder Bool
True = ByteString -> PgSyntax
emit ByteString
"DEFERRABLE"

instance Hashable PgColumnConstraintDefinitionSyntax where
  hashWithSalt :: Int -> PgColumnConstraintDefinitionSyntax -> Int
hashWithSalt Int
salt = Int -> PgSyntax -> Int
forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
salt (PgSyntax -> Int)
-> (PgColumnConstraintDefinitionSyntax -> PgSyntax)
-> PgColumnConstraintDefinitionSyntax
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgColumnConstraintDefinitionSyntax -> PgSyntax
fromPgColumnConstraintDefinition

instance IsSql92ColumnConstraintDefinitionSyntax PgColumnConstraintDefinitionSyntax where
  type Sql92ColumnConstraintDefinitionConstraintSyntax PgColumnConstraintDefinitionSyntax = PgColumnConstraintSyntax
  type Sql92ColumnConstraintDefinitionAttributesSyntax PgColumnConstraintDefinitionSyntax = SqlConstraintAttributesBuilder

  constraintDefinitionSyntax :: Maybe Text
-> Sql92ColumnConstraintDefinitionConstraintSyntax
     PgColumnConstraintDefinitionSyntax
-> Maybe
     (Sql92ColumnConstraintDefinitionAttributesSyntax
        PgColumnConstraintDefinitionSyntax)
-> PgColumnConstraintDefinitionSyntax
constraintDefinitionSyntax Maybe Text
nm Sql92ColumnConstraintDefinitionConstraintSyntax
  PgColumnConstraintDefinitionSyntax
constraint Maybe
  (Sql92ColumnConstraintDefinitionAttributesSyntax
     PgColumnConstraintDefinitionSyntax)
attrs =
    PgSyntax
-> BeamSerializedConstraintDefinition
-> PgColumnConstraintDefinitionSyntax
PgColumnConstraintDefinitionSyntax PgSyntax
syntax
      (Maybe Text
-> Sql92ColumnConstraintDefinitionConstraintSyntax
     BeamSerializedConstraintDefinition
-> Maybe
     (Sql92ColumnConstraintDefinitionAttributesSyntax
        BeamSerializedConstraintDefinition)
-> BeamSerializedConstraintDefinition
forall constraint.
IsSql92ColumnConstraintDefinitionSyntax constraint =>
Maybe Text
-> Sql92ColumnConstraintDefinitionConstraintSyntax constraint
-> Maybe
     (Sql92ColumnConstraintDefinitionAttributesSyntax constraint)
-> constraint
constraintDefinitionSyntax Maybe Text
nm (PgColumnConstraintSyntax -> BeamSerializedConstraint
pgColumnConstraintSerialized Sql92ColumnConstraintDefinitionConstraintSyntax
  PgColumnConstraintDefinitionSyntax
PgColumnConstraintSyntax
constraint) ((SqlConstraintAttributesBuilder
 -> BeamSerializedConstraintAttributes)
-> Maybe SqlConstraintAttributesBuilder
-> Maybe BeamSerializedConstraintAttributes
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SqlConstraintAttributesBuilder
-> BeamSerializedConstraintAttributes
sqlConstraintAttributesSerialized Maybe SqlConstraintAttributesBuilder
Maybe
  (Sql92ColumnConstraintDefinitionAttributesSyntax
     PgColumnConstraintDefinitionSyntax)
attrs))
    where
      syntax :: PgSyntax
syntax =
        PgSyntax -> (Text -> PgSyntax) -> Maybe Text -> PgSyntax
forall b a. b -> (a -> b) -> Maybe a -> b
maybe PgSyntax
forall a. Monoid a => a
mempty (\Text
nm -> ByteString -> PgSyntax
emit ByteString
"CONSTRAINT " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> Text -> PgSyntax
pgQuotedIdentifier Text
nm PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
" " ) Maybe Text
nm PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
        PgColumnConstraintSyntax -> PgSyntax
fromPgColumnConstraint Sql92ColumnConstraintDefinitionConstraintSyntax
  PgColumnConstraintDefinitionSyntax
PgColumnConstraintSyntax
constraint PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
        PgSyntax
-> (SqlConstraintAttributesBuilder -> PgSyntax)
-> Maybe SqlConstraintAttributesBuilder
-> PgSyntax
forall b a. b -> (a -> b) -> Maybe a -> b
maybe PgSyntax
forall a. Monoid a => a
mempty (\SqlConstraintAttributesBuilder
a -> ByteString -> PgSyntax
emit ByteString
" " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> SqlConstraintAttributesBuilder -> PgSyntax
fromSqlConstraintAttributes SqlConstraintAttributesBuilder
a) Maybe SqlConstraintAttributesBuilder
Maybe
  (Sql92ColumnConstraintDefinitionAttributesSyntax
     PgColumnConstraintDefinitionSyntax)
attrs

instance Sql92SerializableConstraintDefinitionSyntax PgColumnConstraintDefinitionSyntax where
  serializeConstraint :: PgColumnConstraintDefinitionSyntax -> Value
serializeConstraint = BeamSerializedConstraintDefinition -> Value
fromBeamSerializedConstraintDefinition (BeamSerializedConstraintDefinition -> Value)
-> (PgColumnConstraintDefinitionSyntax
    -> BeamSerializedConstraintDefinition)
-> PgColumnConstraintDefinitionSyntax
-> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgColumnConstraintDefinitionSyntax
-> BeamSerializedConstraintDefinition
pgColumnConstraintDefinitionSerialized

instance IsSql92ColumnConstraintSyntax PgColumnConstraintSyntax where
  type Sql92ColumnConstraintMatchTypeSyntax PgColumnConstraintSyntax = PgMatchTypeSyntax
  type Sql92ColumnConstraintReferentialActionSyntax PgColumnConstraintSyntax = PgReferentialActionSyntax
  type Sql92ColumnConstraintExpressionSyntax PgColumnConstraintSyntax = PgExpressionSyntax

  notNullConstraintSyntax :: PgColumnConstraintSyntax
notNullConstraintSyntax = PgSyntax -> BeamSerializedConstraint -> PgColumnConstraintSyntax
PgColumnConstraintSyntax (ByteString -> PgSyntax
emit ByteString
"NOT NULL") BeamSerializedConstraint
forall constraint.
IsSql92ColumnConstraintSyntax constraint =>
constraint
notNullConstraintSyntax
  uniqueColumnConstraintSyntax :: PgColumnConstraintSyntax
uniqueColumnConstraintSyntax = PgSyntax -> BeamSerializedConstraint -> PgColumnConstraintSyntax
PgColumnConstraintSyntax (ByteString -> PgSyntax
emit ByteString
"UNIQUE") BeamSerializedConstraint
forall constraint.
IsSql92ColumnConstraintSyntax constraint =>
constraint
uniqueColumnConstraintSyntax
  primaryKeyColumnConstraintSyntax :: PgColumnConstraintSyntax
primaryKeyColumnConstraintSyntax = PgSyntax -> BeamSerializedConstraint -> PgColumnConstraintSyntax
PgColumnConstraintSyntax (ByteString -> PgSyntax
emit ByteString
"PRIMARY KEY") BeamSerializedConstraint
forall constraint.
IsSql92ColumnConstraintSyntax constraint =>
constraint
primaryKeyColumnConstraintSyntax
  checkColumnConstraintSyntax :: Sql92ColumnConstraintExpressionSyntax PgColumnConstraintSyntax
-> PgColumnConstraintSyntax
checkColumnConstraintSyntax Sql92ColumnConstraintExpressionSyntax PgColumnConstraintSyntax
expr =
    PgSyntax -> BeamSerializedConstraint -> PgColumnConstraintSyntax
PgColumnConstraintSyntax (ByteString -> PgSyntax
emit ByteString
"CHECK(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression Sql92ColumnConstraintExpressionSyntax PgColumnConstraintSyntax
PgExpressionSyntax
expr PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")
                             (BeamSerializedExpression -> BeamSerializedConstraint
forall constraint.
IsSql92ColumnConstraintSyntax constraint =>
Sql92ColumnConstraintExpressionSyntax constraint -> constraint
checkColumnConstraintSyntax (BeamSerializedExpression -> BeamSerializedConstraint)
-> (PgExpressionSyntax -> BeamSerializedExpression)
-> PgExpressionSyntax
-> BeamSerializedConstraint
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> BeamSerializedExpression
BeamSerializedExpression (Text -> BeamSerializedExpression)
-> (PgExpressionSyntax -> Text)
-> PgExpressionSyntax
-> BeamSerializedExpression
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Text
TE.decodeUtf8 (ByteString -> Text)
-> (PgExpressionSyntax -> ByteString) -> PgExpressionSyntax -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
                              ByteString -> ByteString
toStrict (ByteString -> ByteString)
-> (PgExpressionSyntax -> ByteString)
-> PgExpressionSyntax
-> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgSyntax -> ByteString
pgRenderSyntaxScript (PgSyntax -> ByteString)
-> (PgExpressionSyntax -> PgSyntax)
-> PgExpressionSyntax
-> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PgExpressionSyntax -> PgSyntax
fromPgExpression (PgExpressionSyntax -> BeamSerializedConstraint)
-> PgExpressionSyntax -> BeamSerializedConstraint
forall a b. (a -> b) -> a -> b
$ Sql92ColumnConstraintExpressionSyntax PgColumnConstraintSyntax
PgExpressionSyntax
expr)
  referencesConstraintSyntax :: Text
-> [Text]
-> Maybe
     (Sql92ColumnConstraintMatchTypeSyntax PgColumnConstraintSyntax)
-> Maybe
     (Sql92ColumnConstraintReferentialActionSyntax
        PgColumnConstraintSyntax)
-> Maybe
     (Sql92ColumnConstraintReferentialActionSyntax
        PgColumnConstraintSyntax)
-> PgColumnConstraintSyntax
referencesConstraintSyntax Text
tbl [Text]
fields Maybe
  (Sql92ColumnConstraintMatchTypeSyntax PgColumnConstraintSyntax)
matchType Maybe
  (Sql92ColumnConstraintReferentialActionSyntax
     PgColumnConstraintSyntax)
onUpdate Maybe
  (Sql92ColumnConstraintReferentialActionSyntax
     PgColumnConstraintSyntax)
onDelete =
    PgSyntax -> BeamSerializedConstraint -> PgColumnConstraintSyntax
PgColumnConstraintSyntax PgSyntax
syntax
      (Text
-> [Text]
-> Maybe
     (Sql92ColumnConstraintMatchTypeSyntax BeamSerializedConstraint)
-> Maybe
     (Sql92ColumnConstraintReferentialActionSyntax
        BeamSerializedConstraint)
-> Maybe
     (Sql92ColumnConstraintReferentialActionSyntax
        BeamSerializedConstraint)
-> BeamSerializedConstraint
forall constraint.
IsSql92ColumnConstraintSyntax constraint =>
Text
-> [Text]
-> Maybe (Sql92ColumnConstraintMatchTypeSyntax constraint)
-> Maybe (Sql92ColumnConstraintReferentialActionSyntax constraint)
-> Maybe (Sql92ColumnConstraintReferentialActionSyntax constraint)
-> constraint
referencesConstraintSyntax Text
tbl [Text]
fields ((PgMatchTypeSyntax -> BeamSerializedMatchType)
-> Maybe PgMatchTypeSyntax -> Maybe BeamSerializedMatchType
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap PgMatchTypeSyntax -> BeamSerializedMatchType
pgMatchTypeSerialized Maybe
  (Sql92ColumnConstraintMatchTypeSyntax PgColumnConstraintSyntax)
Maybe PgMatchTypeSyntax
matchType)
                                  ((PgReferentialActionSyntax -> BeamSerializedReferentialAction)
-> Maybe PgReferentialActionSyntax
-> Maybe BeamSerializedReferentialAction
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap PgReferentialActionSyntax -> BeamSerializedReferentialAction
pgReferentialActionSerialized Maybe
  (Sql92ColumnConstraintReferentialActionSyntax
     PgColumnConstraintSyntax)
Maybe PgReferentialActionSyntax
onUpdate)
                                  ((PgReferentialActionSyntax -> BeamSerializedReferentialAction)
-> Maybe PgReferentialActionSyntax
-> Maybe BeamSerializedReferentialAction
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap PgReferentialActionSyntax -> BeamSerializedReferentialAction
pgReferentialActionSerialized Maybe
  (Sql92ColumnConstraintReferentialActionSyntax
     PgColumnConstraintSyntax)
Maybe PgReferentialActionSyntax
onDelete))
    where
      syntax :: PgSyntax
syntax =
        ByteString -> PgSyntax
emit ByteString
"REFERENCES " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> Text -> PgSyntax
pgQuotedIdentifier Text
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
<> PgSyntax -> [PgSyntax] -> PgSyntax
pgSepBy (ByteString -> PgSyntax
emit ByteString
", ") ((Text -> PgSyntax) -> [Text] -> [PgSyntax]
forall a b. (a -> b) -> [a] -> [b]
map Text -> PgSyntax
pgQuotedIdentifier [Text]
fields) 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
-> (PgMatchTypeSyntax -> PgSyntax)
-> Maybe PgMatchTypeSyntax
-> PgSyntax
forall b a. b -> (a -> b) -> Maybe a -> b
maybe PgSyntax
forall a. Monoid a => a
mempty (\PgMatchTypeSyntax
m -> ByteString -> PgSyntax
emit ByteString
" " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgMatchTypeSyntax -> PgSyntax
fromPgMatchType PgMatchTypeSyntax
m) Maybe
  (Sql92ColumnConstraintMatchTypeSyntax PgColumnConstraintSyntax)
Maybe PgMatchTypeSyntax
matchType PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
        PgSyntax
-> (PgReferentialActionSyntax -> PgSyntax)
-> Maybe PgReferentialActionSyntax
-> PgSyntax
forall b a. b -> (a -> b) -> Maybe a -> b
maybe PgSyntax
forall a. Monoid a => a
mempty (\PgReferentialActionSyntax
a -> ByteString -> PgSyntax
emit ByteString
" ON UPDATE " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgReferentialActionSyntax -> PgSyntax
fromPgReferentialAction PgReferentialActionSyntax
a) Maybe
  (Sql92ColumnConstraintReferentialActionSyntax
     PgColumnConstraintSyntax)
Maybe PgReferentialActionSyntax
onUpdate PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
        PgSyntax
-> (PgReferentialActionSyntax -> PgSyntax)
-> Maybe PgReferentialActionSyntax
-> PgSyntax
forall b a. b -> (a -> b) -> Maybe a -> b
maybe PgSyntax
forall a. Monoid a => a
mempty (\PgReferentialActionSyntax
a -> ByteString -> PgSyntax
emit ByteString
" ON DELETE " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgReferentialActionSyntax -> PgSyntax
fromPgReferentialAction PgReferentialActionSyntax
a) Maybe
  (Sql92ColumnConstraintReferentialActionSyntax
     PgColumnConstraintSyntax)
Maybe PgReferentialActionSyntax
onDelete

defaultPgValueSyntax :: Pg.ToField a => a -> PgValueSyntax
defaultPgValueSyntax :: a -> PgValueSyntax
defaultPgValueSyntax =
    PgSyntax -> PgValueSyntax
PgValueSyntax (PgSyntax -> PgValueSyntax)
-> (a -> PgSyntax) -> a -> PgValueSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Action] -> PgSyntax
pgBuildAction ([Action] -> PgSyntax) -> (a -> [Action]) -> a -> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Action -> [Action]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Action -> [Action]) -> (a -> Action) -> a -> [Action]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Action
forall a. ToField a => a -> Action
Pg.toField

-- Database Predicates

data PgHasEnum = PgHasEnum T.Text {- Enumeration name -} [T.Text] {- enum values -}
    deriving (Int -> PgHasEnum -> ShowS
[PgHasEnum] -> ShowS
PgHasEnum -> String
(Int -> PgHasEnum -> ShowS)
-> (PgHasEnum -> String)
-> ([PgHasEnum] -> ShowS)
-> Show PgHasEnum
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PgHasEnum] -> ShowS
$cshowList :: [PgHasEnum] -> ShowS
show :: PgHasEnum -> String
$cshow :: PgHasEnum -> String
showsPrec :: Int -> PgHasEnum -> ShowS
$cshowsPrec :: Int -> PgHasEnum -> ShowS
Show, PgHasEnum -> PgHasEnum -> Bool
(PgHasEnum -> PgHasEnum -> Bool)
-> (PgHasEnum -> PgHasEnum -> Bool) -> Eq PgHasEnum
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PgHasEnum -> PgHasEnum -> Bool
$c/= :: PgHasEnum -> PgHasEnum -> Bool
== :: PgHasEnum -> PgHasEnum -> Bool
$c== :: PgHasEnum -> PgHasEnum -> Bool
Eq, (forall x. PgHasEnum -> Rep PgHasEnum x)
-> (forall x. Rep PgHasEnum x -> PgHasEnum) -> Generic PgHasEnum
forall x. Rep PgHasEnum x -> PgHasEnum
forall x. PgHasEnum -> Rep PgHasEnum x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep PgHasEnum x -> PgHasEnum
$cfrom :: forall x. PgHasEnum -> Rep PgHasEnum x
Generic)
instance Hashable PgHasEnum
instance DatabasePredicate PgHasEnum where
    englishDescription :: PgHasEnum -> String
englishDescription (PgHasEnum Text
enumName [Text]
values) =
        String
"Has postgres enumeration " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Text -> String
forall a. Show a => a -> String
show Text
enumName String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" with values " String -> ShowS
forall a. [a] -> [a] -> [a]
++ [Text] -> String
forall a. Show a => a -> String
show [Text]
values

    predicateSpecificity :: proxy PgHasEnum -> PredicateSpecificity
predicateSpecificity proxy PgHasEnum
_ = String -> PredicateSpecificity
PredicateSpecificityOnlyBackend String
"postgres"
    serializePredicate :: PgHasEnum -> Value
serializePredicate (PgHasEnum Text
name [Text]
values) =
        [Pair] -> Value
object [ Key
"has-postgres-enum" Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= [Pair] -> Value
object [ Key
"name" Key -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Text
name
                                               , Key
"values" Key -> [Text] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= [Text]
values ] ]

#define DEFAULT_SQL_SYNTAX(ty)                                  \
           instance HasSqlValueSyntax PgValueSyntax ty where    \
             sqlValueSyntax = defaultPgValueSyntax

DEFAULT_SQL_SYNTAX(Bool)
DEFAULT_SQL_SYNTAX(Double)
DEFAULT_SQL_SYNTAX(Float)
DEFAULT_SQL_SYNTAX(Int8)
DEFAULT_SQL_SYNTAX(Int16)
DEFAULT_SQL_SYNTAX(Int32)
DEFAULT_SQL_SYNTAX(Int64)
DEFAULT_SQL_SYNTAX(Integer)
DEFAULT_SQL_SYNTAX(Word8)
DEFAULT_SQL_SYNTAX(Word16)
DEFAULT_SQL_SYNTAX(Word32)
DEFAULT_SQL_SYNTAX(Word64)
DEFAULT_SQL_SYNTAX(T.Text)
DEFAULT_SQL_SYNTAX(TL.Text)
DEFAULT_SQL_SYNTAX(Value)
DEFAULT_SQL_SYNTAX(Pg.Oid)
DEFAULT_SQL_SYNTAX(LocalTime)
DEFAULT_SQL_SYNTAX(UTCTime)
DEFAULT_SQL_SYNTAX(TimeOfDay)
DEFAULT_SQL_SYNTAX(NominalDiffTime)
DEFAULT_SQL_SYNTAX(Day)
DEFAULT_SQL_SYNTAX([Char])
DEFAULT_SQL_SYNTAX(Pg.HStoreMap)
DEFAULT_SQL_SYNTAX(Pg.HStoreList)
DEFAULT_SQL_SYNTAX(Pg.HStoreBuilder)
DEFAULT_SQL_SYNTAX(Pg.Date)
DEFAULT_SQL_SYNTAX(Pg.LocalTimestamp)
DEFAULT_SQL_SYNTAX(Pg.UTCTimestamp)
DEFAULT_SQL_SYNTAX(Scientific)

instance HasSqlValueSyntax PgValueSyntax (CI T.Text) where
  sqlValueSyntax :: CI Text -> PgValueSyntax
sqlValueSyntax = Text -> PgValueSyntax
forall expr ty. HasSqlValueSyntax expr ty => ty -> expr
sqlValueSyntax (Text -> PgValueSyntax)
-> (CI Text -> Text) -> CI Text -> PgValueSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CI Text -> Text
forall s. CI s -> s
CI.original
instance HasSqlValueSyntax PgValueSyntax (CI TL.Text) where
  sqlValueSyntax :: CI Text -> PgValueSyntax
sqlValueSyntax = Text -> PgValueSyntax
forall expr ty. HasSqlValueSyntax expr ty => ty -> expr
sqlValueSyntax (Text -> PgValueSyntax)
-> (CI Text -> Text) -> CI Text -> PgValueSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CI Text -> Text
forall s. CI s -> s
CI.original

instance HasSqlValueSyntax PgValueSyntax SqlNull where
  sqlValueSyntax :: SqlNull -> PgValueSyntax
sqlValueSyntax SqlNull
_ = Null -> PgValueSyntax
forall a. ToField a => a -> PgValueSyntax
defaultPgValueSyntax Null
Pg.Null

instance HasSqlValueSyntax PgValueSyntax x => HasSqlValueSyntax PgValueSyntax (Maybe x) where
  sqlValueSyntax :: Maybe x -> PgValueSyntax
sqlValueSyntax Maybe x
Nothing = SqlNull -> PgValueSyntax
forall expr ty. HasSqlValueSyntax expr ty => ty -> expr
sqlValueSyntax SqlNull
SqlNull
  sqlValueSyntax (Just x
x) = x -> PgValueSyntax
forall expr ty. HasSqlValueSyntax expr ty => ty -> expr
sqlValueSyntax x
x

instance HasSqlValueSyntax PgValueSyntax B.ByteString where
  sqlValueSyntax :: ByteString -> PgValueSyntax
sqlValueSyntax = Binary ByteString -> PgValueSyntax
forall a. ToField a => a -> PgValueSyntax
defaultPgValueSyntax (Binary ByteString -> PgValueSyntax)
-> (ByteString -> Binary ByteString) -> ByteString -> PgValueSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Binary ByteString
forall a. a -> Binary a
Pg.Binary

instance HasSqlValueSyntax PgValueSyntax BL.ByteString where
  sqlValueSyntax :: ByteString -> PgValueSyntax
sqlValueSyntax = Binary ByteString -> PgValueSyntax
forall a. ToField a => a -> PgValueSyntax
defaultPgValueSyntax (Binary ByteString -> PgValueSyntax)
-> (ByteString -> Binary ByteString) -> ByteString -> PgValueSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Binary ByteString
forall a. a -> Binary a
Pg.Binary

-- This should be removed in favor of the default syntax if/when
-- https://github.com/lpsmith/postgresql-simple/issues/277 is fixed upstream.
instance HasSqlValueSyntax PgValueSyntax UUID where
  sqlValueSyntax :: UUID -> PgValueSyntax
sqlValueSyntax UUID
v = 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
emit (UUID -> ByteString
toASCIIBytes UUID
v) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
"'::uuid"

instance Pg.ToField a => HasSqlValueSyntax PgValueSyntax (V.Vector a) where
  sqlValueSyntax :: Vector a -> PgValueSyntax
sqlValueSyntax = Vector a -> PgValueSyntax
forall a. ToField a => a -> PgValueSyntax
defaultPgValueSyntax

instance TypeError (PreferExplicitSize Int Int32) => HasSqlValueSyntax PgValueSyntax Int where
  sqlValueSyntax :: Int -> PgValueSyntax
sqlValueSyntax = Int -> PgValueSyntax
forall a. ToField a => a -> PgValueSyntax
defaultPgValueSyntax

instance TypeError (PreferExplicitSize Word Word32) => HasSqlValueSyntax PgValueSyntax Word where
  sqlValueSyntax :: Word -> PgValueSyntax
sqlValueSyntax = Word -> PgValueSyntax
forall a. ToField a => a -> PgValueSyntax
defaultPgValueSyntax

pgQuotedIdentifier :: T.Text -> PgSyntax
pgQuotedIdentifier :: Text -> PgSyntax
pgQuotedIdentifier Text
t =
  ByteString -> PgSyntax
escapeIdentifier (Text -> ByteString
TE.encodeUtf8 Text
t)

pgParens :: PgSyntax -> PgSyntax
pgParens :: PgSyntax -> PgSyntax
pgParens PgSyntax
a = ByteString -> PgSyntax
emit ByteString
"(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSyntax
a PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")"

pgTableOp :: ByteString -> PgSelectTableSyntax -> PgSelectTableSyntax
          -> PgSelectTableSyntax
pgTableOp :: ByteString
-> PgSelectTableSyntax
-> PgSelectTableSyntax
-> PgSelectTableSyntax
pgTableOp ByteString
op PgSelectTableSyntax
tbl1 PgSelectTableSyntax
tbl2 =
    PgSyntax -> PgSelectTableSyntax
PgSelectTableSyntax (PgSyntax -> PgSelectTableSyntax)
-> PgSyntax -> PgSelectTableSyntax
forall a b. (a -> b) -> a -> b
$
    ByteString -> PgSyntax
emit ByteString
"(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSelectTableSyntax -> PgSyntax
fromPgSelectTable PgSelectTableSyntax
tbl1 PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
") " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
op PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
    ByteString -> PgSyntax
emit ByteString
" (" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSelectTableSyntax -> PgSyntax
fromPgSelectTable PgSelectTableSyntax
tbl2 PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")"

pgCompOp :: ByteString -> Maybe PgComparisonQuantifierSyntax
         -> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgCompOp :: ByteString
-> Maybe PgComparisonQuantifierSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
-> PgExpressionSyntax
pgCompOp ByteString
op Maybe PgComparisonQuantifierSyntax
quantifier PgExpressionSyntax
a PgExpressionSyntax
b =
  PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax) -> PgSyntax -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$
  ByteString -> PgSyntax
emit ByteString
"(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
a PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
  ByteString -> PgSyntax
emit (ByteString
") " ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
op) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
  PgSyntax
-> (PgComparisonQuantifierSyntax -> PgSyntax)
-> Maybe PgComparisonQuantifierSyntax
-> PgSyntax
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (ByteString -> PgSyntax
emit ByteString
" (" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
b PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")")
        (\PgComparisonQuantifierSyntax
q -> ByteString -> PgSyntax
emit ByteString
" " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgComparisonQuantifierSyntax -> PgSyntax
fromPgComparisonQuantifier PgComparisonQuantifierSyntax
q 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
b)
        Maybe PgComparisonQuantifierSyntax
quantifier

pgBinOp :: ByteString -> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp :: ByteString
-> PgExpressionSyntax -> PgExpressionSyntax -> PgExpressionSyntax
pgBinOp ByteString
op PgExpressionSyntax
a PgExpressionSyntax
b =
  PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax) -> PgSyntax -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$
  ByteString -> PgSyntax
emit ByteString
"(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
a PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit (ByteString
") " ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
op ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
" (") PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
b PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")"

pgPostFix, pgUnOp :: ByteString -> PgExpressionSyntax -> PgExpressionSyntax
pgPostFix :: ByteString -> PgExpressionSyntax -> PgExpressionSyntax
pgPostFix ByteString
op PgExpressionSyntax
a =
  PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax) -> PgSyntax -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$
  ByteString -> PgSyntax
emit ByteString
"(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
a PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
") " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
op
pgUnOp :: ByteString -> PgExpressionSyntax -> PgExpressionSyntax
pgUnOp ByteString
op PgExpressionSyntax
a =
  PgSyntax -> PgExpressionSyntax
PgExpressionSyntax (PgSyntax -> PgExpressionSyntax) -> PgSyntax -> PgExpressionSyntax
forall a b. (a -> b) -> a -> b
$
  ByteString -> PgSyntax
emit (ByteString
op ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"(") PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
a PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")"

pgJoin :: ByteString -> PgFromSyntax -> PgFromSyntax -> Maybe PgExpressionSyntax -> PgFromSyntax
pgJoin :: ByteString
-> PgFromSyntax
-> PgFromSyntax
-> Maybe PgExpressionSyntax
-> PgFromSyntax
pgJoin ByteString
joinType PgFromSyntax
a PgFromSyntax
b Maybe PgExpressionSyntax
Nothing =
  PgSyntax -> PgFromSyntax
PgFromSyntax (PgSyntax -> PgFromSyntax) -> PgSyntax -> PgFromSyntax
forall a b. (a -> b) -> a -> b
$
  PgFromSyntax -> PgSyntax
fromPgFrom PgFromSyntax
a PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit (ByteString
" " ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
joinType ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
" ") PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgFromSyntax -> PgSyntax
fromPgFrom PgFromSyntax
b PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
" ON TRUE"
pgJoin ByteString
joinType PgFromSyntax
a PgFromSyntax
b (Just PgExpressionSyntax
on) =
  PgSyntax -> PgFromSyntax
PgFromSyntax (PgSyntax -> PgFromSyntax) -> PgSyntax -> PgFromSyntax
forall a b. (a -> b) -> a -> b
$
  PgFromSyntax -> PgSyntax
fromPgFrom PgFromSyntax
a PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit (ByteString
" " ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
joinType ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
" ") PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgFromSyntax -> PgSyntax
fromPgFrom PgFromSyntax
b PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
  ByteString -> PgSyntax
emit ByteString
" ON " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgExpressionSyntax -> PgSyntax
fromPgExpression PgExpressionSyntax
on

pgSepBy :: PgSyntax -> [PgSyntax] -> PgSyntax
pgSepBy :: PgSyntax -> [PgSyntax] -> PgSyntax
pgSepBy PgSyntax
_ [] = PgSyntax
forall a. Monoid a => a
mempty
pgSepBy PgSyntax
_ [PgSyntax
x] = PgSyntax
x
pgSepBy PgSyntax
sep (PgSyntax
x:[PgSyntax]
xs) = PgSyntax
x PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSyntax
sep PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSyntax -> [PgSyntax] -> PgSyntax
pgSepBy PgSyntax
sep [PgSyntax]
xs

pgDebugRenderSyntax :: PgSyntax -> IO ()
pgDebugRenderSyntax :: PgSyntax -> IO ()
pgDebugRenderSyntax (PgSyntax PgSyntaxM ()
p) = PgSyntaxM () -> Maybe (PgSyntaxF ()) -> IO ()
go PgSyntaxM ()
p Maybe (PgSyntaxF ())
forall a. Maybe a
Nothing
  where go :: PgSyntaxM () -> Maybe (PgSyntaxF ()) -> IO ()
        go :: PgSyntaxM () -> Maybe (PgSyntaxF ()) -> IO ()
go PgSyntaxM ()
p = PgSyntaxM ()
-> (() -> Maybe (PgSyntaxF ()) -> IO ())
-> (PgSyntaxF (Maybe (PgSyntaxF ()) -> IO ())
    -> Maybe (PgSyntaxF ()) -> IO ())
-> Maybe (PgSyntaxF ())
-> IO ()
forall (f :: * -> *) a.
F f a -> forall r. (a -> r) -> (f r -> r) -> r
runF PgSyntaxM ()
p () -> Maybe (PgSyntaxF ()) -> IO ()
forall b f. b -> Maybe (PgSyntaxF f) -> IO b
finish PgSyntaxF (Maybe (PgSyntaxF ()) -> IO ())
-> Maybe (PgSyntaxF ()) -> IO ()
forall b f.
PgSyntaxF (Maybe (PgSyntaxF ()) -> IO b)
-> Maybe (PgSyntaxF f) -> IO b
step
        step :: PgSyntaxF (Maybe (PgSyntaxF ()) -> IO b)
-> Maybe (PgSyntaxF f) -> IO b
step PgSyntaxF (Maybe (PgSyntaxF ()) -> IO b)
x Maybe (PgSyntaxF f)
lastBs =
          case (PgSyntaxF (Maybe (PgSyntaxF ()) -> IO b)
x, Maybe (PgSyntaxF f)
lastBs) of
            (EmitBuilder Builder
s Maybe (PgSyntaxF ()) -> IO b
next, Maybe (PgSyntaxF f)
lastBs) ->
              PgSyntaxF (Maybe (PgSyntaxF ()) -> IO b)
-> Maybe (PgSyntaxF f) -> IO b
step (ByteString
-> (Maybe (PgSyntaxF ()) -> IO b)
-> PgSyntaxF (Maybe (PgSyntaxF ()) -> IO b)
forall f. ByteString -> f -> PgSyntaxF f
EmitByteString (ByteString -> ByteString
toStrict (Builder -> ByteString
toLazyByteString Builder
s)) Maybe (PgSyntaxF ()) -> IO b
next) Maybe (PgSyntaxF f)
lastBs
            (PgSyntaxF (Maybe (PgSyntaxF ()) -> IO b)
x, Maybe (PgSyntaxF f)
Nothing) ->
              PgSyntaxF (Maybe (PgSyntaxF ()) -> IO b)
-> Maybe (PgSyntaxF ()) -> IO b
forall f. PgSyntaxF f -> f
nextSyntaxStep PgSyntaxF (Maybe (PgSyntaxF ()) -> IO b)
x (PgSyntaxF () -> Maybe (PgSyntaxF ())
forall a. a -> Maybe a
Just (((Maybe (PgSyntaxF ()) -> IO b) -> ())
-> PgSyntaxF (Maybe (PgSyntaxF ()) -> IO b) -> PgSyntaxF ()
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (() -> (Maybe (PgSyntaxF ()) -> IO b) -> ()
forall a b. a -> b -> a
const ()) PgSyntaxF (Maybe (PgSyntaxF ()) -> IO b)
x))
            (EmitByteString ByteString
x Maybe (PgSyntaxF ()) -> IO b
next, Just (EmitByteString ByteString
before f
_)) ->
              Maybe (PgSyntaxF ()) -> IO b
next (PgSyntaxF () -> Maybe (PgSyntaxF ())
forall a. a -> Maybe a
Just (ByteString -> () -> PgSyntaxF ()
forall f. ByteString -> f -> PgSyntaxF f
EmitByteString (ByteString
before ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
x) ()))
            (EscapeString ByteString
x Maybe (PgSyntaxF ()) -> IO b
next, Just (EscapeString ByteString
before f
_)) ->
              Maybe (PgSyntaxF ()) -> IO b
next (PgSyntaxF () -> Maybe (PgSyntaxF ())
forall a. a -> Maybe a
Just (ByteString -> () -> PgSyntaxF ()
forall f. ByteString -> f -> PgSyntaxF f
EscapeString (ByteString
before ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
x) ()))
            (EscapeBytea ByteString
x Maybe (PgSyntaxF ()) -> IO b
next, Just (EscapeBytea ByteString
before f
_)) ->
              Maybe (PgSyntaxF ()) -> IO b
next (PgSyntaxF () -> Maybe (PgSyntaxF ())
forall a. a -> Maybe a
Just (ByteString -> () -> PgSyntaxF ()
forall f. ByteString -> f -> PgSyntaxF f
EscapeBytea (ByteString
before ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
x) ()))
            (EscapeIdentifier ByteString
x Maybe (PgSyntaxF ()) -> IO b
next, Just (EscapeIdentifier ByteString
before f
_)) ->
              Maybe (PgSyntaxF ()) -> IO b
next (PgSyntaxF () -> Maybe (PgSyntaxF ())
forall a. a -> Maybe a
Just (ByteString -> () -> PgSyntaxF ()
forall f. ByteString -> f -> PgSyntaxF f
EscapeIdentifier (ByteString
before ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
x) ()))
            (PgSyntaxF (Maybe (PgSyntaxF ()) -> IO b)
s, Just PgSyntaxF f
e) ->
              PgSyntaxF f -> IO ()
forall f. PgSyntaxF f -> IO ()
renderStep PgSyntaxF f
e IO () -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
              PgSyntaxF (Maybe (PgSyntaxF ()) -> IO b)
-> Maybe (PgSyntaxF ()) -> IO b
forall f. PgSyntaxF f -> f
nextSyntaxStep PgSyntaxF (Maybe (PgSyntaxF ()) -> IO b)
s (PgSyntaxF () -> Maybe (PgSyntaxF ())
forall a. a -> Maybe a
Just (((Maybe (PgSyntaxF ()) -> IO b) -> ())
-> PgSyntaxF (Maybe (PgSyntaxF ()) -> IO b) -> PgSyntaxF ()
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (() -> (Maybe (PgSyntaxF ()) -> IO b) -> ()
forall a b. a -> b -> a
const ()) PgSyntaxF (Maybe (PgSyntaxF ()) -> IO b)
s))

        renderStep :: PgSyntaxF f -> IO ()
renderStep (EmitByteString ByteString
x f
_) = String -> IO ()
putStrLn (String
"EmitByteString " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> ByteString -> String
forall a. Show a => a -> String
show ByteString
x)
        renderStep (EmitBuilder Builder
x f
_) = String -> IO ()
putStrLn (String
"EmitBuilder " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> ByteString -> String
forall a. Show a => a -> String
show (Builder -> ByteString
toLazyByteString Builder
x))
        renderStep (EscapeString ByteString
x f
_) = String -> IO ()
putStrLn (String
"EscapeString " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> ByteString -> String
forall a. Show a => a -> String
show ByteString
x)
        renderStep (EscapeBytea ByteString
x f
_) = String -> IO ()
putStrLn (String
"EscapeBytea " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> ByteString -> String
forall a. Show a => a -> String
show ByteString
x)
        renderStep (EscapeIdentifier ByteString
x f
_) = String -> IO ()
putStrLn (String
"EscapeIdentifier " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> ByteString -> String
forall a. Show a => a -> String
show ByteString
x)

        finish :: b -> Maybe (PgSyntaxF f) -> IO b
finish b
x Maybe (PgSyntaxF f)
Nothing = b -> IO b
forall (f :: * -> *) a. Applicative f => a -> f a
pure b
x
        finish b
x (Just PgSyntaxF f
s) = PgSyntaxF f -> IO ()
forall f. PgSyntaxF f -> IO ()
renderStep PgSyntaxF f
s IO () -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> b -> IO b
forall (f :: * -> *) a. Applicative f => a -> f a
pure b
x

pgBuildAction :: [ Pg.Action ] -> PgSyntax
pgBuildAction :: [Action] -> PgSyntax
pgBuildAction =
  (Action -> PgSyntax) -> [Action] -> PgSyntax
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap ((Action -> PgSyntax) -> [Action] -> PgSyntax)
-> (Action -> PgSyntax) -> [Action] -> PgSyntax
forall a b. (a -> b) -> a -> b
$ \Action
action ->
  case Action
action of
    Pg.Plain Builder
x -> Builder -> PgSyntax
emitBuilder Builder
x
    Pg.Escape ByteString
str -> ByteString -> PgSyntax
emit ByteString
"'" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
escapeString ByteString
str PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
"'"
    Pg.EscapeByteA ByteString
bin -> ByteString -> PgSyntax
emit ByteString
"'" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
escapeBytea ByteString
bin PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
"'"
    Pg.EscapeIdentifier ByteString
id -> ByteString -> PgSyntax
escapeIdentifier ByteString
id
    Pg.Many [Action]
as -> [Action] -> PgSyntax
pgBuildAction [Action]
as

-- * Postgres-specific extensions

-- * Postgres specific commands

pgSelectStmt :: PgSelectTableSyntax
             -> [PgOrderingSyntax]
             -> Maybe Integer {-^ LIMIT -}
             -> Maybe Integer {-^ OFFSET -}
             -> Maybe PgSelectLockingClauseSyntax
             -> PgSelectSyntax
pgSelectStmt :: PgSelectTableSyntax
-> [PgOrderingSyntax]
-> Maybe Integer
-> Maybe Integer
-> Maybe PgSelectLockingClauseSyntax
-> PgSelectSyntax
pgSelectStmt PgSelectTableSyntax
tbl [PgOrderingSyntax]
ordering Maybe Integer
limit Maybe Integer
offset Maybe PgSelectLockingClauseSyntax
locking =
    PgSyntax -> PgSelectSyntax
PgSelectSyntax (PgSyntax -> PgSelectSyntax) -> PgSyntax -> PgSelectSyntax
forall a b. (a -> b) -> a -> b
$
    [PgSyntax] -> PgSyntax
forall a. Monoid a => [a] -> a
mconcat [ PgSelectTableSyntax -> PgSyntax
coerce PgSelectTableSyntax
tbl
            , case [PgOrderingSyntax]
ordering of
                [] -> PgSyntax
forall a. Monoid a => a
mempty
                [PgOrderingSyntax]
ordering -> ByteString -> PgSyntax
emit ByteString
" ORDER BY " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> PgSyntax -> [PgSyntax] -> PgSyntax
pgSepBy (ByteString -> PgSyntax
emit ByteString
", ") ((PgOrderingSyntax -> PgSyntax) -> [PgOrderingSyntax] -> [PgSyntax]
forall a b. (a -> b) -> [a] -> [b]
map PgOrderingSyntax -> PgSyntax
fromPgOrdering [PgOrderingSyntax]
ordering)
            , PgSyntax -> (Integer -> PgSyntax) -> Maybe Integer -> PgSyntax
forall b a. b -> (a -> b) -> Maybe a -> b
maybe PgSyntax
forall a. Monoid a => a
mempty (ByteString -> PgSyntax
emit (ByteString -> PgSyntax)
-> (Integer -> ByteString) -> Integer -> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ByteString
forall a. IsString a => String -> a
fromString (String -> ByteString)
-> (Integer -> String) -> Integer -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
" LIMIT " String -> ShowS
forall a. Semigroup a => a -> a -> a
<>) ShowS -> (Integer -> String) -> Integer -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> String
forall a. Show a => a -> String
show) Maybe Integer
limit
            , PgSyntax -> (Integer -> PgSyntax) -> Maybe Integer -> PgSyntax
forall b a. b -> (a -> b) -> Maybe a -> b
maybe PgSyntax
forall a. Monoid a => a
mempty (ByteString -> PgSyntax
emit (ByteString -> PgSyntax)
-> (Integer -> ByteString) -> Integer -> PgSyntax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ByteString
forall a. IsString a => String -> a
fromString (String -> ByteString)
-> (Integer -> String) -> Integer -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
" OFFSET " String -> ShowS
forall a. Semigroup a => a -> a -> a
<>) ShowS -> (Integer -> String) -> Integer -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> String
forall a. Show a => a -> String
show) Maybe Integer
offset
            , PgSyntax
-> (PgSelectLockingClauseSyntax -> PgSyntax)
-> Maybe PgSelectLockingClauseSyntax
-> PgSyntax
forall b a. b -> (a -> b) -> Maybe a -> b
maybe PgSyntax
forall a. Monoid a => a
mempty PgSelectLockingClauseSyntax -> PgSyntax
fromPgSelectLockingClause Maybe PgSelectLockingClauseSyntax
locking ]

pgCreateExtensionSyntax :: T.Text -> PgCommandSyntax
pgCreateExtensionSyntax :: Text -> PgCommandSyntax
pgCreateExtensionSyntax Text
extName =
  PgCommandType -> PgSyntax -> PgCommandSyntax
PgCommandSyntax PgCommandType
PgCommandTypeDdl (PgSyntax -> PgCommandSyntax) -> PgSyntax -> PgCommandSyntax
forall a b. (a -> b) -> a -> b
$ ByteString -> PgSyntax
emit ByteString
"CREATE EXTENSION " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> Text -> PgSyntax
pgQuotedIdentifier Text
extName

pgDropExtensionSyntax :: T.Text -> PgCommandSyntax
pgDropExtensionSyntax :: Text -> PgCommandSyntax
pgDropExtensionSyntax Text
extName =
  PgCommandType -> PgSyntax -> PgCommandSyntax
PgCommandSyntax PgCommandType
PgCommandTypeDdl (PgSyntax -> PgCommandSyntax) -> PgSyntax -> PgCommandSyntax
forall a b. (a -> b) -> a -> b
$ ByteString -> PgSyntax
emit ByteString
"DROP EXTENSION " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> Text -> PgSyntax
pgQuotedIdentifier Text
extName

pgCreateEnumSyntax :: T.Text -> [PgValueSyntax] -> PgCommandSyntax
pgCreateEnumSyntax :: Text -> [PgValueSyntax] -> PgCommandSyntax
pgCreateEnumSyntax Text
enumName [PgValueSyntax]
vals =
    PgCommandType -> PgSyntax -> PgCommandSyntax
PgCommandSyntax PgCommandType
PgCommandTypeDdl (PgSyntax -> PgCommandSyntax) -> PgSyntax -> PgCommandSyntax
forall a b. (a -> b) -> a -> b
$
    ByteString -> PgSyntax
emit ByteString
"CREATE TYPE " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> Text -> PgSyntax
pgQuotedIdentifier Text
enumName PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
" AS ENUM(" PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<>
    PgSyntax -> [PgSyntax] -> PgSyntax
pgSepBy (ByteString -> PgSyntax
emit ByteString
", ") ((PgValueSyntax -> PgSyntax) -> [PgValueSyntax] -> [PgSyntax]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap PgValueSyntax -> PgSyntax
fromPgValue [PgValueSyntax]
vals) PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> ByteString -> PgSyntax
emit ByteString
")"

pgDropTypeSyntax :: T.Text -> PgCommandSyntax
pgDropTypeSyntax :: Text -> PgCommandSyntax
pgDropTypeSyntax Text
typeName =
    PgCommandType -> PgSyntax -> PgCommandSyntax
PgCommandSyntax PgCommandType
PgCommandTypeDdl (PgSyntax -> PgCommandSyntax) -> PgSyntax -> PgCommandSyntax
forall a b. (a -> b) -> a -> b
$
    ByteString -> PgSyntax
emit ByteString
"DROP TYPE " PgSyntax -> PgSyntax -> PgSyntax
forall a. Semigroup a => a -> a -> a
<> Text -> PgSyntax
pgQuotedIdentifier Text
typeName

-- -- * Pg-specific Q monad


data PgEscapeType = PgEscapeString | PgEscapeBytea | PgEscapeIdentifier
  deriving (Int -> PgEscapeType -> ShowS
[PgEscapeType] -> ShowS
PgEscapeType -> String
(Int -> PgEscapeType -> ShowS)
-> (PgEscapeType -> String)
-> ([PgEscapeType] -> ShowS)
-> Show PgEscapeType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PgEscapeType] -> ShowS
$cshowList :: [PgEscapeType] -> ShowS
show :: PgEscapeType -> String
$cshow :: PgEscapeType -> String
showsPrec :: Int -> PgEscapeType -> ShowS
$cshowsPrec :: Int -> PgEscapeType -> ShowS
Show, PgEscapeType -> PgEscapeType -> Bool
(PgEscapeType -> PgEscapeType -> Bool)
-> (PgEscapeType -> PgEscapeType -> Bool) -> Eq PgEscapeType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PgEscapeType -> PgEscapeType -> Bool
$c/= :: PgEscapeType -> PgEscapeType -> Bool
== :: PgEscapeType -> PgEscapeType -> Bool
$c== :: PgEscapeType -> PgEscapeType -> Bool
Eq, Eq PgEscapeType
Eq PgEscapeType
-> (PgEscapeType -> PgEscapeType -> Ordering)
-> (PgEscapeType -> PgEscapeType -> Bool)
-> (PgEscapeType -> PgEscapeType -> Bool)
-> (PgEscapeType -> PgEscapeType -> Bool)
-> (PgEscapeType -> PgEscapeType -> Bool)
-> (PgEscapeType -> PgEscapeType -> PgEscapeType)
-> (PgEscapeType -> PgEscapeType -> PgEscapeType)
-> Ord PgEscapeType
PgEscapeType -> PgEscapeType -> Bool
PgEscapeType -> PgEscapeType -> Ordering
PgEscapeType -> PgEscapeType -> PgEscapeType
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 :: PgEscapeType -> PgEscapeType -> PgEscapeType
$cmin :: PgEscapeType -> PgEscapeType -> PgEscapeType
max :: PgEscapeType -> PgEscapeType -> PgEscapeType
$cmax :: PgEscapeType -> PgEscapeType -> PgEscapeType
>= :: PgEscapeType -> PgEscapeType -> Bool
$c>= :: PgEscapeType -> PgEscapeType -> Bool
> :: PgEscapeType -> PgEscapeType -> Bool
$c> :: PgEscapeType -> PgEscapeType -> Bool
<= :: PgEscapeType -> PgEscapeType -> Bool
$c<= :: PgEscapeType -> PgEscapeType -> Bool
< :: PgEscapeType -> PgEscapeType -> Bool
$c< :: PgEscapeType -> PgEscapeType -> Bool
compare :: PgEscapeType -> PgEscapeType -> Ordering
$ccompare :: PgEscapeType -> PgEscapeType -> Ordering
$cp1Ord :: Eq PgEscapeType
Ord, Int -> PgEscapeType
PgEscapeType -> Int
PgEscapeType -> [PgEscapeType]
PgEscapeType -> PgEscapeType
PgEscapeType -> PgEscapeType -> [PgEscapeType]
PgEscapeType -> PgEscapeType -> PgEscapeType -> [PgEscapeType]
(PgEscapeType -> PgEscapeType)
-> (PgEscapeType -> PgEscapeType)
-> (Int -> PgEscapeType)
-> (PgEscapeType -> Int)
-> (PgEscapeType -> [PgEscapeType])
-> (PgEscapeType -> PgEscapeType -> [PgEscapeType])
-> (PgEscapeType -> PgEscapeType -> [PgEscapeType])
-> (PgEscapeType -> PgEscapeType -> PgEscapeType -> [PgEscapeType])
-> Enum PgEscapeType
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: PgEscapeType -> PgEscapeType -> PgEscapeType -> [PgEscapeType]
$cenumFromThenTo :: PgEscapeType -> PgEscapeType -> PgEscapeType -> [PgEscapeType]
enumFromTo :: PgEscapeType -> PgEscapeType -> [PgEscapeType]
$cenumFromTo :: PgEscapeType -> PgEscapeType -> [PgEscapeType]
enumFromThen :: PgEscapeType -> PgEscapeType -> [PgEscapeType]
$cenumFromThen :: PgEscapeType -> PgEscapeType -> [PgEscapeType]
enumFrom :: PgEscapeType -> [PgEscapeType]
$cenumFrom :: PgEscapeType -> [PgEscapeType]
fromEnum :: PgEscapeType -> Int
$cfromEnum :: PgEscapeType -> Int
toEnum :: Int -> PgEscapeType
$ctoEnum :: Int -> PgEscapeType
pred :: PgEscapeType -> PgEscapeType
$cpred :: PgEscapeType -> PgEscapeType
succ :: PgEscapeType -> PgEscapeType
$csucc :: PgEscapeType -> PgEscapeType
Enum, PgEscapeType
PgEscapeType -> PgEscapeType -> Bounded PgEscapeType
forall a. a -> a -> Bounded a
maxBound :: PgEscapeType
$cmaxBound :: PgEscapeType
minBound :: PgEscapeType
$cminBound :: PgEscapeType
Bounded)
data PgSyntaxPrim = PgSyntaxPrim (Maybe PgEscapeType) BL.ByteString deriving Int -> PgSyntaxPrim -> ShowS
[PgSyntaxPrim] -> ShowS
PgSyntaxPrim -> String
(Int -> PgSyntaxPrim -> ShowS)
-> (PgSyntaxPrim -> String)
-> ([PgSyntaxPrim] -> ShowS)
-> Show PgSyntaxPrim
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PgSyntaxPrim] -> ShowS
$cshowList :: [PgSyntaxPrim] -> ShowS
show :: PgSyntaxPrim -> String
$cshow :: PgSyntaxPrim -> String
showsPrec :: Int -> PgSyntaxPrim -> ShowS
$cshowsPrec :: Int -> PgSyntaxPrim -> ShowS
Show

instance IsString PgSyntaxPrim where
  fromString :: String -> PgSyntaxPrim
fromString = Maybe PgEscapeType -> ByteString -> PgSyntaxPrim
PgSyntaxPrim Maybe PgEscapeType
forall a. Maybe a
Nothing (ByteString -> PgSyntaxPrim)
-> (String -> ByteString) -> String -> PgSyntaxPrim
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ByteString
forall a. IsString a => String -> a
fromString

pgTestSyntax :: PgSyntax -> [ PgSyntaxPrim ]
pgTestSyntax :: PgSyntax -> [PgSyntaxPrim]
pgTestSyntax (PgSyntax PgSyntaxM ()
syntax) = PgSyntaxM ()
-> (()
    -> Maybe PgEscapeType
    -> Builder
    -> ([PgSyntaxPrim] -> [PgSyntaxPrim])
    -> [PgSyntaxPrim])
-> (PgSyntaxF
      (Maybe PgEscapeType
       -> Builder -> ([PgSyntaxPrim] -> [PgSyntaxPrim]) -> [PgSyntaxPrim])
    -> Maybe PgEscapeType
    -> Builder
    -> ([PgSyntaxPrim] -> [PgSyntaxPrim])
    -> [PgSyntaxPrim])
-> Maybe PgEscapeType
-> Builder
-> ([PgSyntaxPrim] -> [PgSyntaxPrim])
-> [PgSyntaxPrim]
forall (f :: * -> *) a.
F f a -> forall r. (a -> r) -> (f r -> r) -> r
runF PgSyntaxM ()
syntax ()
-> Maybe PgEscapeType
-> Builder
-> ([PgSyntaxPrim] -> [PgSyntaxPrim])
-> [PgSyntaxPrim]
forall p p.
p -> Maybe PgEscapeType -> Builder -> ([PgSyntaxPrim] -> p) -> p
finish PgSyntaxF
  (Maybe PgEscapeType
   -> Builder -> ([PgSyntaxPrim] -> [PgSyntaxPrim]) -> [PgSyntaxPrim])
-> Maybe PgEscapeType
-> Builder
-> ([PgSyntaxPrim] -> [PgSyntaxPrim])
-> [PgSyntaxPrim]
forall c p.
PgSyntaxF
  (Maybe PgEscapeType -> Builder -> ([PgSyntaxPrim] -> c) -> p)
-> Maybe PgEscapeType -> Builder -> ([PgSyntaxPrim] -> c) -> p
step Maybe PgEscapeType
forall a. Maybe a
Nothing Builder
forall a. Monoid a => a
mempty [PgSyntaxPrim] -> [PgSyntaxPrim]
forall a. a -> a
id
  where
    finish :: p -> Maybe PgEscapeType -> Builder -> ([PgSyntaxPrim] -> p) -> p
finish p
_ Maybe PgEscapeType
escapeType Builder
curBuilder [PgSyntaxPrim] -> p
a =
      let chunk :: ByteString
chunk = Builder -> ByteString
toLazyByteString Builder
curBuilder
      in if ByteString -> Bool
BL.null ByteString
chunk then [PgSyntaxPrim] -> p
a []
         else [PgSyntaxPrim] -> p
a [ Maybe PgEscapeType -> ByteString -> PgSyntaxPrim
PgSyntaxPrim Maybe PgEscapeType
escapeType ByteString
chunk ]

    go :: (Maybe PgEscapeType -> Builder -> ([PgSyntaxPrim] -> c) -> p)
-> Maybe PgEscapeType
-> Maybe PgEscapeType
-> Builder
-> Builder
-> ([PgSyntaxPrim] -> c)
-> p
go Maybe PgEscapeType -> Builder -> ([PgSyntaxPrim] -> c) -> p
next Maybe PgEscapeType
curType Maybe PgEscapeType
nextType Builder
curBuilder Builder
nextBuilder [PgSyntaxPrim] -> c
a
      | Maybe PgEscapeType
curType Maybe PgEscapeType -> Maybe PgEscapeType -> Bool
forall a. Eq a => a -> a -> Bool
== Maybe PgEscapeType
nextType = Maybe PgEscapeType -> Builder -> ([PgSyntaxPrim] -> c) -> p
next Maybe PgEscapeType
curType (Builder
curBuilder Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
nextBuilder) [PgSyntaxPrim] -> c
a
      | Bool
otherwise = Maybe PgEscapeType -> Builder -> ([PgSyntaxPrim] -> c) -> p
next Maybe PgEscapeType
nextType Builder
forall a. Monoid a => a
mempty ([PgSyntaxPrim] -> c
a ([PgSyntaxPrim] -> c)
-> ([PgSyntaxPrim] -> [PgSyntaxPrim]) -> [PgSyntaxPrim] -> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe PgEscapeType -> ByteString -> PgSyntaxPrim
PgSyntaxPrim Maybe PgEscapeType
curType (Builder -> ByteString
toLazyByteString Builder
curBuilder)PgSyntaxPrim -> [PgSyntaxPrim] -> [PgSyntaxPrim]
forall a. a -> [a] -> [a]
:))

    step :: PgSyntaxF
  (Maybe PgEscapeType -> Builder -> ([PgSyntaxPrim] -> c) -> p)
-> Maybe PgEscapeType -> Builder -> ([PgSyntaxPrim] -> c) -> p
step (EmitByteString ByteString
bs Maybe PgEscapeType -> Builder -> ([PgSyntaxPrim] -> c) -> p
next) Maybe PgEscapeType
curType Builder
curBuilder [PgSyntaxPrim] -> c
a =
      (Maybe PgEscapeType -> Builder -> ([PgSyntaxPrim] -> c) -> p)
-> Maybe PgEscapeType
-> Maybe PgEscapeType
-> Builder
-> Builder
-> ([PgSyntaxPrim] -> c)
-> p
forall c p.
(Maybe PgEscapeType -> Builder -> ([PgSyntaxPrim] -> c) -> p)
-> Maybe PgEscapeType
-> Maybe PgEscapeType
-> Builder
-> Builder
-> ([PgSyntaxPrim] -> c)
-> p
go Maybe PgEscapeType -> Builder -> ([PgSyntaxPrim] -> c) -> p
next Maybe PgEscapeType
curType Maybe PgEscapeType
forall a. Maybe a
Nothing Builder
curBuilder (ByteString -> Builder
byteString ByteString
bs) [PgSyntaxPrim] -> c
a
    step (EmitBuilder Builder
bs Maybe PgEscapeType -> Builder -> ([PgSyntaxPrim] -> c) -> p
next) Maybe PgEscapeType
curType Builder
curBuilder [PgSyntaxPrim] -> c
a =
      (Maybe PgEscapeType -> Builder -> ([PgSyntaxPrim] -> c) -> p)
-> Maybe PgEscapeType
-> Maybe PgEscapeType
-> Builder
-> Builder
-> ([PgSyntaxPrim] -> c)
-> p
forall c p.
(Maybe PgEscapeType -> Builder -> ([PgSyntaxPrim] -> c) -> p)
-> Maybe PgEscapeType
-> Maybe PgEscapeType
-> Builder
-> Builder
-> ([PgSyntaxPrim] -> c)
-> p
go Maybe PgEscapeType -> Builder -> ([PgSyntaxPrim] -> c) -> p
next Maybe PgEscapeType
curType Maybe PgEscapeType
forall a. Maybe a
Nothing Builder
curBuilder Builder
bs [PgSyntaxPrim] -> c
a
    step (EscapeString ByteString
s Maybe PgEscapeType -> Builder -> ([PgSyntaxPrim] -> c) -> p
next) Maybe PgEscapeType
curType Builder
curBuilder [PgSyntaxPrim] -> c
a =
      (Maybe PgEscapeType -> Builder -> ([PgSyntaxPrim] -> c) -> p)
-> Maybe PgEscapeType
-> Maybe PgEscapeType
-> Builder
-> Builder
-> ([PgSyntaxPrim] -> c)
-> p
forall c p.
(Maybe PgEscapeType -> Builder -> ([PgSyntaxPrim] -> c) -> p)
-> Maybe PgEscapeType
-> Maybe PgEscapeType
-> Builder
-> Builder
-> ([PgSyntaxPrim] -> c)
-> p
go Maybe PgEscapeType -> Builder -> ([PgSyntaxPrim] -> c) -> p
next Maybe PgEscapeType
curType (PgEscapeType -> Maybe PgEscapeType
forall a. a -> Maybe a
Just PgEscapeType
PgEscapeString) Builder
curBuilder (ByteString -> Builder
byteString ByteString
s) [PgSyntaxPrim] -> c
a
    step (EscapeBytea ByteString
s Maybe PgEscapeType -> Builder -> ([PgSyntaxPrim] -> c) -> p
next) Maybe PgEscapeType
curType Builder
curBuilder [PgSyntaxPrim] -> c
a =
      (Maybe PgEscapeType -> Builder -> ([PgSyntaxPrim] -> c) -> p)
-> Maybe PgEscapeType
-> Maybe PgEscapeType
-> Builder
-> Builder
-> ([PgSyntaxPrim] -> c)
-> p
forall c p.
(Maybe PgEscapeType -> Builder -> ([PgSyntaxPrim] -> c) -> p)
-> Maybe PgEscapeType
-> Maybe PgEscapeType
-> Builder
-> Builder
-> ([PgSyntaxPrim] -> c)
-> p
go Maybe PgEscapeType -> Builder -> ([PgSyntaxPrim] -> c) -> p
next Maybe PgEscapeType
curType (PgEscapeType -> Maybe PgEscapeType
forall a. a -> Maybe a
Just PgEscapeType
PgEscapeBytea) Builder
curBuilder (ByteString -> Builder
byteString ByteString
s) [PgSyntaxPrim] -> c
a
    step (EscapeIdentifier ByteString
s Maybe PgEscapeType -> Builder -> ([PgSyntaxPrim] -> c) -> p
next) Maybe PgEscapeType
curType Builder
curBuilder [PgSyntaxPrim] -> c
a =
      (Maybe PgEscapeType -> Builder -> ([PgSyntaxPrim] -> c) -> p)
-> Maybe PgEscapeType
-> Maybe PgEscapeType
-> Builder
-> Builder
-> ([PgSyntaxPrim] -> c)
-> p
forall c p.
(Maybe PgEscapeType -> Builder -> ([PgSyntaxPrim] -> c) -> p)
-> Maybe PgEscapeType
-> Maybe PgEscapeType
-> Builder
-> Builder
-> ([PgSyntaxPrim] -> c)
-> p
go Maybe PgEscapeType -> Builder -> ([PgSyntaxPrim] -> c) -> p
next Maybe PgEscapeType
curType (PgEscapeType -> Maybe PgEscapeType
forall a. a -> Maybe a
Just PgEscapeType
PgEscapeIdentifier) Builder
curBuilder (ByteString -> Builder
byteString ByteString
s) [PgSyntaxPrim] -> c
a

pgRenderSyntaxScript :: PgSyntax -> BL.ByteString
pgRenderSyntaxScript :: PgSyntax -> ByteString
pgRenderSyntaxScript (PgSyntax PgSyntaxM ()
mkQuery) =
  Builder -> ByteString
toLazyByteString (PgSyntaxM ()
-> (() -> Builder) -> (PgSyntaxF Builder -> Builder) -> Builder
forall (f :: * -> *) a.
F f a -> forall r. (a -> r) -> (f r -> r) -> r
runF PgSyntaxM ()
mkQuery () -> Builder
forall a p. Monoid a => p -> a
finish PgSyntaxF Builder -> Builder
step)
  where
    finish :: p -> a
finish p
_ = a
forall a. Monoid a => a
mempty
    step :: PgSyntaxF Builder -> Builder
step (EmitBuilder Builder
b Builder
next) = Builder
b Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
next
    step (EmitByteString ByteString
b Builder
next) = ByteString -> Builder
byteString ByteString
b Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
next
    step (EscapeString ByteString
b Builder
next) = ByteString -> Builder
escapePgString ByteString
b Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
next
    step (EscapeBytea ByteString
b Builder
next) = ByteString -> Builder
forall p a. p -> a
escapePgBytea ByteString
b Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
next
    step (EscapeIdentifier ByteString
b Builder
next) = ByteString -> Builder
escapePgIdentifier ByteString
b Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
next

    escapePgString :: ByteString -> Builder
escapePgString ByteString
b = ByteString -> Builder
byteString ((Char -> ByteString) -> ByteString -> ByteString
B.concatMap (\Char
w -> if Char
w Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\'' then ByteString
"''" else Char -> ByteString
B.singleton Char
w) ByteString
b)
    escapePgBytea :: p -> a
escapePgBytea p
_ = String -> a
forall a. HasCallStack => String -> a
error String
"escapePgBytea: no connection"
    escapePgIdentifier :: ByteString -> Builder
escapePgIdentifier ByteString
bs = Char -> Builder
char8 Char
'"' Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> (Char -> Builder) -> String -> Builder
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap Char -> Builder
quoteIdentifierChar (ByteString -> String
B.unpack ByteString
bs) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Char -> Builder
char8 Char
'"'
      where
        quoteIdentifierChar :: Char -> Builder
quoteIdentifierChar Char
'"' = Char -> Builder
char8 Char
'"' Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Char -> Builder
char8 Char
'"'
        quoteIdentifierChar Char
c = Char -> Builder
char8 Char
c