{-# LANGUAGE TemplateHaskell #-}

module Hasura.RQL.DML.Types
  ( OrderByExp (..),
    DMLQuery (..),
    getSourceDMLQuery,
    SelectG (..),
    Wildcard (..),
    SelCol (..),
    SelectQ,
    SelectQT,
    SelectQuery,
    SelectQueryT,
    InsObj,
    InsertQuery (..),
    OnConflict (..),
    ConflictAction (..),
    ConstraintOn (..),
    UpdVals,
    UpdateQuery (..),
    DeleteQuery (..),
    CountQuery (..),
    QueryT (..),
  )
where

import Data.Aeson
import Data.Aeson.Casing
import Data.Aeson.TH
import Data.Attoparsec.Text qualified as AT
import Data.HashMap.Strict qualified as M
import Hasura.Backends.Postgres.Instances.Types ()
import Hasura.Backends.Postgres.SQL.DML qualified as PG
import Hasura.Backends.Postgres.SQL.Types
import Hasura.Prelude
import Hasura.RQL.IR.BoolExp
import Hasura.RQL.IR.OrderBy
import Hasura.RQL.Types.Column
import Hasura.RQL.Types.Common
import Hasura.SQL.Backend

newtype OrderByExp = OrderByExp {OrderByExp -> [OrderByItem ('Postgres 'Vanilla)]
getOrderByItems :: [OrderByItem ('Postgres 'Vanilla)]}
  deriving (Int -> OrderByExp -> ShowS
[OrderByExp] -> ShowS
OrderByExp -> String
(Int -> OrderByExp -> ShowS)
-> (OrderByExp -> String)
-> ([OrderByExp] -> ShowS)
-> Show OrderByExp
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [OrderByExp] -> ShowS
$cshowList :: [OrderByExp] -> ShowS
show :: OrderByExp -> String
$cshow :: OrderByExp -> String
showsPrec :: Int -> OrderByExp -> ShowS
$cshowsPrec :: Int -> OrderByExp -> ShowS
Show, OrderByExp -> OrderByExp -> Bool
(OrderByExp -> OrderByExp -> Bool)
-> (OrderByExp -> OrderByExp -> Bool) -> Eq OrderByExp
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: OrderByExp -> OrderByExp -> Bool
$c/= :: OrderByExp -> OrderByExp -> Bool
== :: OrderByExp -> OrderByExp -> Bool
$c== :: OrderByExp -> OrderByExp -> Bool
Eq)

instance FromJSON OrderByExp where
  parseJSON :: Value -> Parser OrderByExp
parseJSON = \case
    String Text
s -> [OrderByItem ('Postgres 'Vanilla)] -> OrderByExp
OrderByExp ([OrderByItem ('Postgres 'Vanilla)] -> OrderByExp)
-> (OrderByItem ('Postgres 'Vanilla)
    -> [OrderByItem ('Postgres 'Vanilla)])
-> OrderByItem ('Postgres 'Vanilla)
-> OrderByExp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. OrderByItem ('Postgres 'Vanilla)
-> [OrderByItem ('Postgres 'Vanilla)]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (OrderByItem ('Postgres 'Vanilla) -> OrderByExp)
-> Parser (OrderByItem ('Postgres 'Vanilla)) -> Parser OrderByExp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Parser (OrderByItem ('Postgres 'Vanilla))
parseString Text
s
    Object Object
o -> [OrderByItem ('Postgres 'Vanilla)] -> OrderByExp
OrderByExp ([OrderByItem ('Postgres 'Vanilla)] -> OrderByExp)
-> (OrderByItem ('Postgres 'Vanilla)
    -> [OrderByItem ('Postgres 'Vanilla)])
-> OrderByItem ('Postgres 'Vanilla)
-> OrderByExp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. OrderByItem ('Postgres 'Vanilla)
-> [OrderByItem ('Postgres 'Vanilla)]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (OrderByItem ('Postgres 'Vanilla) -> OrderByExp)
-> Parser (OrderByItem ('Postgres 'Vanilla)) -> Parser OrderByExp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object -> Parser (OrderByItem ('Postgres 'Vanilla))
forall (b :: BackendType) a.
(FromJSON (BasicOrderType b), FromJSON a,
 FromJSON (NullsOrderType b)) =>
Object -> Parser (OrderByItemG b a)
parseObject Object
o
    Array Array
a ->
      [OrderByItem ('Postgres 'Vanilla)] -> OrderByExp
OrderByExp ([OrderByItem ('Postgres 'Vanilla)] -> OrderByExp)
-> Parser [OrderByItem ('Postgres 'Vanilla)] -> Parser OrderByExp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Value]
-> (Value -> Parser (OrderByItem ('Postgres 'Vanilla)))
-> Parser [OrderByItem ('Postgres 'Vanilla)]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
t a -> (a -> f b) -> f (t b)
for (Array -> [Value]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList Array
a) \case
        String Text
s -> Text -> Parser (OrderByItem ('Postgres 'Vanilla))
parseString Text
s
        Object Object
o -> Object -> Parser (OrderByItem ('Postgres 'Vanilla))
forall (b :: BackendType) a.
(FromJSON (BasicOrderType b), FromJSON a,
 FromJSON (NullsOrderType b)) =>
Object -> Parser (OrderByItemG b a)
parseObject Object
o
        Value
_ -> String -> Parser (OrderByItem ('Postgres 'Vanilla))
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"expecting an object or string for order by"
    Value
_ -> String -> Parser OrderByExp
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Expecting : array/string/object"
    where
      parseString :: Text -> Parser (OrderByItem ('Postgres 'Vanilla))
parseString Text
s =
        Parser (OrderByItem ('Postgres 'Vanilla))
-> Text -> Either String (OrderByItem ('Postgres 'Vanilla))
forall a. Parser a -> Text -> Either String a
AT.parseOnly Parser (OrderByItem ('Postgres 'Vanilla))
orderByParser Text
s
          Either String (OrderByItem ('Postgres 'Vanilla))
-> (String -> Parser (OrderByItem ('Postgres 'Vanilla)))
-> Parser (OrderByItem ('Postgres 'Vanilla))
forall (m :: * -> *) e a.
Applicative m =>
Either e a -> (e -> m a) -> m a
`onLeft` Parser (OrderByItem ('Postgres 'Vanilla))
-> String -> Parser (OrderByItem ('Postgres 'Vanilla))
forall a b. a -> b -> a
const (String -> Parser (OrderByItem ('Postgres 'Vanilla))
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"string format for 'order_by' entry : {+/-}column Eg : +posted")
      parseObject :: Object -> Parser (OrderByItemG b a)
parseObject Object
o =
        Maybe (BasicOrderType b)
-> a -> Maybe (NullsOrderType b) -> OrderByItemG b a
forall (b :: BackendType) a.
Maybe (BasicOrderType b)
-> a -> Maybe (NullsOrderType b) -> OrderByItemG b a
OrderByItemG
          (Maybe (BasicOrderType b)
 -> a -> Maybe (NullsOrderType b) -> OrderByItemG b a)
-> Parser (Maybe (BasicOrderType b))
-> Parser (a -> Maybe (NullsOrderType b) -> OrderByItemG b a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser (Maybe (BasicOrderType b))
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"type"
          Parser (a -> Maybe (NullsOrderType b) -> OrderByItemG b a)
-> Parser a
-> Parser (Maybe (NullsOrderType b) -> OrderByItemG b a)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser a
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"column"
          Parser (Maybe (NullsOrderType b) -> OrderByItemG b a)
-> Parser (Maybe (NullsOrderType b)) -> Parser (OrderByItemG b a)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser (Maybe (NullsOrderType b))
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"nulls"
      orderByParser :: Parser (OrderByItem ('Postgres 'Vanilla))
orderByParser =
        Maybe OrderType
-> OrderByCol
-> Maybe NullsOrder
-> OrderByItem ('Postgres 'Vanilla)
forall (b :: BackendType) a.
Maybe (BasicOrderType b)
-> a -> Maybe (NullsOrderType b) -> OrderByItemG b a
OrderByItemG
          (Maybe OrderType
 -> OrderByCol
 -> Maybe NullsOrder
 -> OrderByItem ('Postgres 'Vanilla))
-> Parser Text (Maybe OrderType)
-> Parser
     Text
     (OrderByCol
      -> Maybe NullsOrder -> OrderByItem ('Postgres 'Vanilla))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Text (Maybe OrderType)
orderTypeParser
          Parser
  Text
  (OrderByCol
   -> Maybe NullsOrder -> OrderByItem ('Postgres 'Vanilla))
-> Parser Text OrderByCol
-> Parser
     Text (Maybe NullsOrder -> OrderByItem ('Postgres 'Vanilla))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Text OrderByCol
orderColumnParser
          Parser Text (Maybe NullsOrder -> OrderByItem ('Postgres 'Vanilla))
-> Parser Text (Maybe NullsOrder)
-> Parser (OrderByItem ('Postgres 'Vanilla))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Maybe NullsOrder -> Parser Text (Maybe NullsOrder)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe NullsOrder
forall a. Maybe a
Nothing
      orderTypeParser :: Parser Text (Maybe OrderType)
orderTypeParser =
        [Parser Text (Maybe OrderType)] -> Parser Text (Maybe OrderType)
forall (f :: * -> *) a. Alternative f => [f a] -> f a
choice
          [ Parser Text Text
"+" Parser Text Text
-> Parser Text (Maybe OrderType) -> Parser Text (Maybe OrderType)
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Maybe OrderType -> Parser Text (Maybe OrderType)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (OrderType -> Maybe OrderType
forall a. a -> Maybe a
Just OrderType
PG.OTAsc),
            Parser Text Text
"-" Parser Text Text
-> Parser Text (Maybe OrderType) -> Parser Text (Maybe OrderType)
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Maybe OrderType -> Parser Text (Maybe OrderType)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (OrderType -> Maybe OrderType
forall a. a -> Maybe a
Just OrderType
PG.OTDesc),
            Maybe OrderType -> Parser Text (Maybe OrderType)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe OrderType
forall a. Maybe a
Nothing
          ]
      orderColumnParser :: Parser Text OrderByCol
orderColumnParser = Parser Text Text
AT.takeText Parser Text Text
-> (Text -> Parser Text OrderByCol) -> Parser Text OrderByCol
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Text -> Parser Text OrderByCol
forall (m :: * -> *). MonadFail m => Text -> m OrderByCol
orderByColFromTxt

data DMLQuery a
  = DMLQuery SourceName QualifiedTable a
  deriving (Int -> DMLQuery a -> ShowS
[DMLQuery a] -> ShowS
DMLQuery a -> String
(Int -> DMLQuery a -> ShowS)
-> (DMLQuery a -> String)
-> ([DMLQuery a] -> ShowS)
-> Show (DMLQuery a)
forall a. Show a => Int -> DMLQuery a -> ShowS
forall a. Show a => [DMLQuery a] -> ShowS
forall a. Show a => DMLQuery a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [DMLQuery a] -> ShowS
$cshowList :: forall a. Show a => [DMLQuery a] -> ShowS
show :: DMLQuery a -> String
$cshow :: forall a. Show a => DMLQuery a -> String
showsPrec :: Int -> DMLQuery a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> DMLQuery a -> ShowS
Show, DMLQuery a -> DMLQuery a -> Bool
(DMLQuery a -> DMLQuery a -> Bool)
-> (DMLQuery a -> DMLQuery a -> Bool) -> Eq (DMLQuery a)
forall a. Eq a => DMLQuery a -> DMLQuery a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: DMLQuery a -> DMLQuery a -> Bool
$c/= :: forall a. Eq a => DMLQuery a -> DMLQuery a -> Bool
== :: DMLQuery a -> DMLQuery a -> Bool
$c== :: forall a. Eq a => DMLQuery a -> DMLQuery a -> Bool
Eq)

instance (FromJSON a) => FromJSON (DMLQuery a) where
  parseJSON :: Value -> Parser (DMLQuery a)
parseJSON = String
-> (Object -> Parser (DMLQuery a)) -> Value -> Parser (DMLQuery a)
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"query" \Object
o ->
    SourceName -> QualifiedTable -> a -> DMLQuery a
forall a. SourceName -> QualifiedTable -> a -> DMLQuery a
DMLQuery
      (SourceName -> QualifiedTable -> a -> DMLQuery a)
-> Parser SourceName -> Parser (QualifiedTable -> a -> DMLQuery a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser (Maybe SourceName)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"source" Parser (Maybe SourceName) -> SourceName -> Parser SourceName
forall a. Parser (Maybe a) -> a -> Parser a
.!= SourceName
defaultSource
      Parser (QualifiedTable -> a -> DMLQuery a)
-> Parser QualifiedTable -> Parser (a -> DMLQuery a)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser QualifiedTable
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"table"
      Parser (a -> DMLQuery a) -> Parser a -> Parser (DMLQuery a)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Value -> Parser a
forall a. FromJSON a => Value -> Parser a
parseJSON (Object -> Value
Object Object
o)

getSourceDMLQuery :: forall a. DMLQuery a -> SourceName
getSourceDMLQuery :: DMLQuery a -> SourceName
getSourceDMLQuery (DMLQuery SourceName
source QualifiedTable
_ a
_) = SourceName
source

data SelectG a b c = SelectG
  { SelectG a b c -> [a]
sqColumns :: [a], -- Postgres columns and relationships
    SelectG a b c -> Maybe b
sqWhere :: Maybe b, -- Filter
    SelectG a b c -> Maybe OrderByExp
sqOrderBy :: Maybe OrderByExp, -- Ordering
    SelectG a b c -> Maybe c
sqLimit :: Maybe c, -- Limit
    SelectG a b c -> Maybe c
sqOffset :: Maybe c -- Offset
  }
  deriving (Int -> SelectG a b c -> ShowS
[SelectG a b c] -> ShowS
SelectG a b c -> String
(Int -> SelectG a b c -> ShowS)
-> (SelectG a b c -> String)
-> ([SelectG a b c] -> ShowS)
-> Show (SelectG a b c)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall a b c.
(Show a, Show b, Show c) =>
Int -> SelectG a b c -> ShowS
forall a b c. (Show a, Show b, Show c) => [SelectG a b c] -> ShowS
forall a b c. (Show a, Show b, Show c) => SelectG a b c -> String
showList :: [SelectG a b c] -> ShowS
$cshowList :: forall a b c. (Show a, Show b, Show c) => [SelectG a b c] -> ShowS
show :: SelectG a b c -> String
$cshow :: forall a b c. (Show a, Show b, Show c) => SelectG a b c -> String
showsPrec :: Int -> SelectG a b c -> ShowS
$cshowsPrec :: forall a b c.
(Show a, Show b, Show c) =>
Int -> SelectG a b c -> ShowS
Show, SelectG a b c -> SelectG a b c -> Bool
(SelectG a b c -> SelectG a b c -> Bool)
-> (SelectG a b c -> SelectG a b c -> Bool) -> Eq (SelectG a b c)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall a b c.
(Eq a, Eq b, Eq c) =>
SelectG a b c -> SelectG a b c -> Bool
/= :: SelectG a b c -> SelectG a b c -> Bool
$c/= :: forall a b c.
(Eq a, Eq b, Eq c) =>
SelectG a b c -> SelectG a b c -> Bool
== :: SelectG a b c -> SelectG a b c -> Bool
$c== :: forall a b c.
(Eq a, Eq b, Eq c) =>
SelectG a b c -> SelectG a b c -> Bool
Eq)

$(deriveFromJSON hasuraJSON {omitNothingFields = True} ''SelectG)

data Wildcard
  = Star
  | StarDot Wildcard
  deriving (Int -> Wildcard -> ShowS
[Wildcard] -> ShowS
Wildcard -> String
(Int -> Wildcard -> ShowS)
-> (Wildcard -> String) -> ([Wildcard] -> ShowS) -> Show Wildcard
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Wildcard] -> ShowS
$cshowList :: [Wildcard] -> ShowS
show :: Wildcard -> String
$cshow :: Wildcard -> String
showsPrec :: Int -> Wildcard -> ShowS
$cshowsPrec :: Int -> Wildcard -> ShowS
Show, Wildcard -> Wildcard -> Bool
(Wildcard -> Wildcard -> Bool)
-> (Wildcard -> Wildcard -> Bool) -> Eq Wildcard
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Wildcard -> Wildcard -> Bool
$c/= :: Wildcard -> Wildcard -> Bool
== :: Wildcard -> Wildcard -> Bool
$c== :: Wildcard -> Wildcard -> Bool
Eq, Eq Wildcard
Eq Wildcard
-> (Wildcard -> Wildcard -> Ordering)
-> (Wildcard -> Wildcard -> Bool)
-> (Wildcard -> Wildcard -> Bool)
-> (Wildcard -> Wildcard -> Bool)
-> (Wildcard -> Wildcard -> Bool)
-> (Wildcard -> Wildcard -> Wildcard)
-> (Wildcard -> Wildcard -> Wildcard)
-> Ord Wildcard
Wildcard -> Wildcard -> Bool
Wildcard -> Wildcard -> Ordering
Wildcard -> Wildcard -> Wildcard
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 :: Wildcard -> Wildcard -> Wildcard
$cmin :: Wildcard -> Wildcard -> Wildcard
max :: Wildcard -> Wildcard -> Wildcard
$cmax :: Wildcard -> Wildcard -> Wildcard
>= :: Wildcard -> Wildcard -> Bool
$c>= :: Wildcard -> Wildcard -> Bool
> :: Wildcard -> Wildcard -> Bool
$c> :: Wildcard -> Wildcard -> Bool
<= :: Wildcard -> Wildcard -> Bool
$c<= :: Wildcard -> Wildcard -> Bool
< :: Wildcard -> Wildcard -> Bool
$c< :: Wildcard -> Wildcard -> Bool
compare :: Wildcard -> Wildcard -> Ordering
$ccompare :: Wildcard -> Wildcard -> Ordering
$cp1Ord :: Eq Wildcard
Ord)

parseWildcard :: AT.Parser Wildcard
parseWildcard :: Parser Wildcard
parseWildcard =
  [Wildcard] -> Wildcard
fromList ([Wildcard] -> Wildcard)
-> Parser Text [Wildcard] -> Parser Wildcard
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((Parser Wildcard
starParser Parser Wildcard -> Parser Text Char -> Parser Text [Wildcard]
forall (f :: * -> *) a s. Alternative f => f a -> f s -> f [a]
`AT.sepBy1` Char -> Parser Text Char
AT.char Char
'.') Parser Text [Wildcard] -> Parser Text () -> Parser Text [Wildcard]
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser Text ()
forall t. Chunk t => Parser t ()
AT.endOfInput)
  where
    starParser :: Parser Wildcard
starParser = Char -> Parser Text Char
AT.char Char
'*' Parser Text Char -> Parser Wildcard -> Parser Wildcard
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Wildcard -> Parser Wildcard
forall (f :: * -> *) a. Applicative f => a -> f a
pure Wildcard
Star
    fromList :: [Wildcard] -> Wildcard
fromList = (Wildcard -> Wildcard -> Wildcard) -> [Wildcard] -> Wildcard
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 (\Wildcard
_ Wildcard
x -> Wildcard -> Wildcard
StarDot Wildcard
x)

-- Columns in RQL
data SelCol
  = SCStar Wildcard
  | SCExtSimple PGCol
  | SCExtRel RelName (Maybe RelName) SelectQ
  deriving (Int -> SelCol -> ShowS
[SelCol] -> ShowS
SelCol -> String
(Int -> SelCol -> ShowS)
-> (SelCol -> String) -> ([SelCol] -> ShowS) -> Show SelCol
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SelCol] -> ShowS
$cshowList :: [SelCol] -> ShowS
show :: SelCol -> String
$cshow :: SelCol -> String
showsPrec :: Int -> SelCol -> ShowS
$cshowsPrec :: Int -> SelCol -> ShowS
Show, SelCol -> SelCol -> Bool
(SelCol -> SelCol -> Bool)
-> (SelCol -> SelCol -> Bool) -> Eq SelCol
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SelCol -> SelCol -> Bool
$c/= :: SelCol -> SelCol -> Bool
== :: SelCol -> SelCol -> Bool
$c== :: SelCol -> SelCol -> Bool
Eq)

instance FromJSON SelCol where
  parseJSON :: Value -> Parser SelCol
parseJSON (String Text
s) =
    case Parser Wildcard -> Text -> Either String Wildcard
forall a. Parser a -> Text -> Either String a
AT.parseOnly Parser Wildcard
parseWildcard Text
s of
      Left String
_ -> PGCol -> SelCol
SCExtSimple (PGCol -> SelCol) -> Parser PGCol -> Parser SelCol
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser PGCol
forall a. FromJSON a => Value -> Parser a
parseJSON (Text -> Value
String Text
s)
      Right Wildcard
x -> SelCol -> Parser SelCol
forall (m :: * -> *) a. Monad m => a -> m a
return (SelCol -> Parser SelCol) -> SelCol -> Parser SelCol
forall a b. (a -> b) -> a -> b
$ Wildcard -> SelCol
SCStar Wildcard
x
  parseJSON v :: Value
v@(Object Object
o) =
    RelName -> Maybe RelName -> SelectQ -> SelCol
SCExtRel
      (RelName -> Maybe RelName -> SelectQ -> SelCol)
-> Parser RelName -> Parser (Maybe RelName -> SelectQ -> SelCol)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser RelName
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"name"
      Parser (Maybe RelName -> SelectQ -> SelCol)
-> Parser (Maybe RelName) -> Parser (SelectQ -> SelCol)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser (Maybe RelName)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"alias"
      Parser (SelectQ -> SelCol) -> Parser SelectQ -> Parser SelCol
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Value -> Parser SelectQ
forall a. FromJSON a => Value -> Parser a
parseJSON Value
v
  parseJSON Value
_ =
    String -> Parser SelCol
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser SelCol) -> String -> Parser SelCol
forall a b. (a -> b) -> a -> b
$
      [String] -> String
forall a. Monoid a => [a] -> a
mconcat
        [ String
"A column should either be a string or an ",
          String
"object (relationship)"
        ]

type SelectQ = SelectG SelCol (BoolExp ('Postgres 'Vanilla)) Int

type SelectQT = SelectG SelCol (BoolExp ('Postgres 'Vanilla)) Value

type SelectQuery = DMLQuery SelectQ

type SelectQueryT = DMLQuery SelectQT

type InsObj b = ColumnValues b Value

data ConflictAction
  = CAIgnore
  | CAUpdate
  deriving (Int -> ConflictAction -> ShowS
[ConflictAction] -> ShowS
ConflictAction -> String
(Int -> ConflictAction -> ShowS)
-> (ConflictAction -> String)
-> ([ConflictAction] -> ShowS)
-> Show ConflictAction
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ConflictAction] -> ShowS
$cshowList :: [ConflictAction] -> ShowS
show :: ConflictAction -> String
$cshow :: ConflictAction -> String
showsPrec :: Int -> ConflictAction -> ShowS
$cshowsPrec :: Int -> ConflictAction -> ShowS
Show, ConflictAction -> ConflictAction -> Bool
(ConflictAction -> ConflictAction -> Bool)
-> (ConflictAction -> ConflictAction -> Bool) -> Eq ConflictAction
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ConflictAction -> ConflictAction -> Bool
$c/= :: ConflictAction -> ConflictAction -> Bool
== :: ConflictAction -> ConflictAction -> Bool
$c== :: ConflictAction -> ConflictAction -> Bool
Eq)

instance FromJSON ConflictAction where
  parseJSON :: Value -> Parser ConflictAction
parseJSON (String Text
"ignore") = ConflictAction -> Parser ConflictAction
forall (m :: * -> *) a. Monad m => a -> m a
return ConflictAction
CAIgnore
  parseJSON (String Text
"update") = ConflictAction -> Parser ConflictAction
forall (m :: * -> *) a. Monad m => a -> m a
return ConflictAction
CAUpdate
  parseJSON Value
_ = String -> Parser ConflictAction
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Expecting 'ignore' or 'update'"

newtype ConstraintOn = ConstraintOn {ConstraintOn -> [PGCol]
getPGCols :: [PGCol]}
  deriving (Int -> ConstraintOn -> ShowS
[ConstraintOn] -> ShowS
ConstraintOn -> String
(Int -> ConstraintOn -> ShowS)
-> (ConstraintOn -> String)
-> ([ConstraintOn] -> ShowS)
-> Show ConstraintOn
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ConstraintOn] -> ShowS
$cshowList :: [ConstraintOn] -> ShowS
show :: ConstraintOn -> String
$cshow :: ConstraintOn -> String
showsPrec :: Int -> ConstraintOn -> ShowS
$cshowsPrec :: Int -> ConstraintOn -> ShowS
Show, ConstraintOn -> ConstraintOn -> Bool
(ConstraintOn -> ConstraintOn -> Bool)
-> (ConstraintOn -> ConstraintOn -> Bool) -> Eq ConstraintOn
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ConstraintOn -> ConstraintOn -> Bool
$c/= :: ConstraintOn -> ConstraintOn -> Bool
== :: ConstraintOn -> ConstraintOn -> Bool
$c== :: ConstraintOn -> ConstraintOn -> Bool
Eq)

instance FromJSON ConstraintOn where
  parseJSON :: Value -> Parser ConstraintOn
parseJSON v :: Value
v@(String Text
_) =
    [PGCol] -> ConstraintOn
ConstraintOn ([PGCol] -> ConstraintOn)
-> (PGCol -> [PGCol]) -> PGCol -> ConstraintOn
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (PGCol -> [PGCol] -> [PGCol]
forall a. a -> [a] -> [a]
: []) (PGCol -> ConstraintOn) -> Parser PGCol -> Parser ConstraintOn
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser PGCol
forall a. FromJSON a => Value -> Parser a
parseJSON Value
v
  parseJSON v :: Value
v@(Array Array
_) =
    [PGCol] -> ConstraintOn
ConstraintOn ([PGCol] -> ConstraintOn) -> Parser [PGCol] -> Parser ConstraintOn
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser [PGCol]
forall a. FromJSON a => Value -> Parser a
parseJSON Value
v
  parseJSON Value
_ =
    String -> Parser ConstraintOn
forall (m :: * -> *) a. MonadFail m => String -> m a
fail
      String
"Expecting String or Array"

data OnConflict = OnConflict
  { OnConflict -> Maybe ConstraintOn
ocConstraintOn :: Maybe ConstraintOn,
    OnConflict -> Maybe ConstraintName
ocConstraint :: Maybe ConstraintName,
    OnConflict -> ConflictAction
ocAction :: ConflictAction
  }
  deriving (Int -> OnConflict -> ShowS
[OnConflict] -> ShowS
OnConflict -> String
(Int -> OnConflict -> ShowS)
-> (OnConflict -> String)
-> ([OnConflict] -> ShowS)
-> Show OnConflict
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [OnConflict] -> ShowS
$cshowList :: [OnConflict] -> ShowS
show :: OnConflict -> String
$cshow :: OnConflict -> String
showsPrec :: Int -> OnConflict -> ShowS
$cshowsPrec :: Int -> OnConflict -> ShowS
Show, OnConflict -> OnConflict -> Bool
(OnConflict -> OnConflict -> Bool)
-> (OnConflict -> OnConflict -> Bool) -> Eq OnConflict
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: OnConflict -> OnConflict -> Bool
$c/= :: OnConflict -> OnConflict -> Bool
== :: OnConflict -> OnConflict -> Bool
$c== :: OnConflict -> OnConflict -> Bool
Eq)

$(deriveFromJSON hasuraJSON {omitNothingFields = True} ''OnConflict)

data InsertQuery = InsertQuery
  { InsertQuery -> QualifiedTable
iqTable :: QualifiedTable,
    InsertQuery -> SourceName
iqSource :: SourceName,
    InsertQuery -> Value
iqObjects :: Value,
    InsertQuery -> Maybe OnConflict
iqOnConflict :: Maybe OnConflict,
    InsertQuery -> Maybe [PGCol]
iqReturning :: Maybe [PGCol]
  }
  deriving (Int -> InsertQuery -> ShowS
[InsertQuery] -> ShowS
InsertQuery -> String
(Int -> InsertQuery -> ShowS)
-> (InsertQuery -> String)
-> ([InsertQuery] -> ShowS)
-> Show InsertQuery
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [InsertQuery] -> ShowS
$cshowList :: [InsertQuery] -> ShowS
show :: InsertQuery -> String
$cshow :: InsertQuery -> String
showsPrec :: Int -> InsertQuery -> ShowS
$cshowsPrec :: Int -> InsertQuery -> ShowS
Show, InsertQuery -> InsertQuery -> Bool
(InsertQuery -> InsertQuery -> Bool)
-> (InsertQuery -> InsertQuery -> Bool) -> Eq InsertQuery
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: InsertQuery -> InsertQuery -> Bool
$c/= :: InsertQuery -> InsertQuery -> Bool
== :: InsertQuery -> InsertQuery -> Bool
$c== :: InsertQuery -> InsertQuery -> Bool
Eq)

instance FromJSON InsertQuery where
  parseJSON :: Value -> Parser InsertQuery
parseJSON = String
-> (Object -> Parser InsertQuery) -> Value -> Parser InsertQuery
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"insert query" ((Object -> Parser InsertQuery) -> Value -> Parser InsertQuery)
-> (Object -> Parser InsertQuery) -> Value -> Parser InsertQuery
forall a b. (a -> b) -> a -> b
$ \Object
o ->
    QualifiedTable
-> SourceName
-> Value
-> Maybe OnConflict
-> Maybe [PGCol]
-> InsertQuery
InsertQuery
      (QualifiedTable
 -> SourceName
 -> Value
 -> Maybe OnConflict
 -> Maybe [PGCol]
 -> InsertQuery)
-> Parser QualifiedTable
-> Parser
     (SourceName
      -> Value -> Maybe OnConflict -> Maybe [PGCol] -> InsertQuery)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser QualifiedTable
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"table"
      Parser
  (SourceName
   -> Value -> Maybe OnConflict -> Maybe [PGCol] -> InsertQuery)
-> Parser SourceName
-> Parser
     (Value -> Maybe OnConflict -> Maybe [PGCol] -> InsertQuery)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser (Maybe SourceName)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"source" Parser (Maybe SourceName) -> SourceName -> Parser SourceName
forall a. Parser (Maybe a) -> a -> Parser a
.!= SourceName
defaultSource
      Parser (Value -> Maybe OnConflict -> Maybe [PGCol] -> InsertQuery)
-> Parser Value
-> Parser (Maybe OnConflict -> Maybe [PGCol] -> InsertQuery)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser Value
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"objects"
      Parser (Maybe OnConflict -> Maybe [PGCol] -> InsertQuery)
-> Parser (Maybe OnConflict)
-> Parser (Maybe [PGCol] -> InsertQuery)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser (Maybe OnConflict)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"on_conflict"
      Parser (Maybe [PGCol] -> InsertQuery)
-> Parser (Maybe [PGCol]) -> Parser InsertQuery
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser (Maybe [PGCol])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"returning"

type UpdVals b = ColumnValues b Value

data UpdateQuery = UpdateQuery
  { UpdateQuery -> QualifiedTable
uqTable :: QualifiedTable,
    UpdateQuery -> SourceName
uqSource :: SourceName,
    UpdateQuery -> BoolExp ('Postgres 'Vanilla)
uqWhere :: BoolExp ('Postgres 'Vanilla),
    UpdateQuery -> UpdVals ('Postgres 'Vanilla)
uqSet :: UpdVals ('Postgres 'Vanilla),
    UpdateQuery -> UpdVals ('Postgres 'Vanilla)
uqInc :: UpdVals ('Postgres 'Vanilla),
    UpdateQuery -> UpdVals ('Postgres 'Vanilla)
uqMul :: UpdVals ('Postgres 'Vanilla),
    UpdateQuery -> [PGCol]
uqDefault :: [PGCol],
    UpdateQuery -> Maybe [PGCol]
uqReturning :: Maybe [PGCol]
  }
  deriving (Int -> UpdateQuery -> ShowS
[UpdateQuery] -> ShowS
UpdateQuery -> String
(Int -> UpdateQuery -> ShowS)
-> (UpdateQuery -> String)
-> ([UpdateQuery] -> ShowS)
-> Show UpdateQuery
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [UpdateQuery] -> ShowS
$cshowList :: [UpdateQuery] -> ShowS
show :: UpdateQuery -> String
$cshow :: UpdateQuery -> String
showsPrec :: Int -> UpdateQuery -> ShowS
$cshowsPrec :: Int -> UpdateQuery -> ShowS
Show, UpdateQuery -> UpdateQuery -> Bool
(UpdateQuery -> UpdateQuery -> Bool)
-> (UpdateQuery -> UpdateQuery -> Bool) -> Eq UpdateQuery
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: UpdateQuery -> UpdateQuery -> Bool
$c/= :: UpdateQuery -> UpdateQuery -> Bool
== :: UpdateQuery -> UpdateQuery -> Bool
$c== :: UpdateQuery -> UpdateQuery -> Bool
Eq)

instance FromJSON UpdateQuery where
  parseJSON :: Value -> Parser UpdateQuery
parseJSON = String
-> (Object -> Parser UpdateQuery) -> Value -> Parser UpdateQuery
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"update query" \Object
o ->
    QualifiedTable
-> SourceName
-> BoolExp ('Postgres 'Vanilla)
-> UpdVals ('Postgres 'Vanilla)
-> UpdVals ('Postgres 'Vanilla)
-> UpdVals ('Postgres 'Vanilla)
-> [PGCol]
-> Maybe [PGCol]
-> UpdateQuery
QualifiedTable
-> SourceName
-> BoolExp ('Postgres 'Vanilla)
-> HashMap PGCol Value
-> HashMap PGCol Value
-> HashMap PGCol Value
-> [PGCol]
-> Maybe [PGCol]
-> UpdateQuery
UpdateQuery
      (QualifiedTable
 -> SourceName
 -> BoolExp ('Postgres 'Vanilla)
 -> HashMap PGCol Value
 -> HashMap PGCol Value
 -> HashMap PGCol Value
 -> [PGCol]
 -> Maybe [PGCol]
 -> UpdateQuery)
-> Parser QualifiedTable
-> Parser
     (SourceName
      -> BoolExp ('Postgres 'Vanilla)
      -> HashMap PGCol Value
      -> HashMap PGCol Value
      -> HashMap PGCol Value
      -> [PGCol]
      -> Maybe [PGCol]
      -> UpdateQuery)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser QualifiedTable
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"table"
      Parser
  (SourceName
   -> BoolExp ('Postgres 'Vanilla)
   -> HashMap PGCol Value
   -> HashMap PGCol Value
   -> HashMap PGCol Value
   -> [PGCol]
   -> Maybe [PGCol]
   -> UpdateQuery)
-> Parser SourceName
-> Parser
     (BoolExp ('Postgres 'Vanilla)
      -> HashMap PGCol Value
      -> HashMap PGCol Value
      -> HashMap PGCol Value
      -> [PGCol]
      -> Maybe [PGCol]
      -> UpdateQuery)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser (Maybe SourceName)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"source" Parser (Maybe SourceName) -> SourceName -> Parser SourceName
forall a. Parser (Maybe a) -> a -> Parser a
.!= SourceName
defaultSource
      Parser
  (BoolExp ('Postgres 'Vanilla)
   -> HashMap PGCol Value
   -> HashMap PGCol Value
   -> HashMap PGCol Value
   -> [PGCol]
   -> Maybe [PGCol]
   -> UpdateQuery)
-> Parser (BoolExp ('Postgres 'Vanilla))
-> Parser
     (HashMap PGCol Value
      -> HashMap PGCol Value
      -> HashMap PGCol Value
      -> [PGCol]
      -> Maybe [PGCol]
      -> UpdateQuery)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser (BoolExp ('Postgres 'Vanilla))
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"where"
      Parser
  (HashMap PGCol Value
   -> HashMap PGCol Value
   -> HashMap PGCol Value
   -> [PGCol]
   -> Maybe [PGCol]
   -> UpdateQuery)
-> Parser (HashMap PGCol Value)
-> Parser
     (HashMap PGCol Value
      -> HashMap PGCol Value -> [PGCol] -> Maybe [PGCol] -> UpdateQuery)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ((Object
o Object -> Key -> Parser (Maybe (HashMap PGCol Value))
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"$set" Parser (Maybe (HashMap PGCol Value))
-> Parser (Maybe (HashMap PGCol Value))
-> Parser (Maybe (HashMap PGCol Value))
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Object
o Object -> Key -> Parser (Maybe (HashMap PGCol Value))
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"values") Parser (Maybe (HashMap PGCol Value))
-> HashMap PGCol Value -> Parser (HashMap PGCol Value)
forall a. Parser (Maybe a) -> a -> Parser a
.!= HashMap PGCol Value
forall k v. HashMap k v
M.empty)
      Parser
  (HashMap PGCol Value
   -> HashMap PGCol Value -> [PGCol] -> Maybe [PGCol] -> UpdateQuery)
-> Parser (HashMap PGCol Value)
-> Parser
     (HashMap PGCol Value -> [PGCol] -> Maybe [PGCol] -> UpdateQuery)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Object
o Object -> Key -> Parser (Maybe (HashMap PGCol Value))
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"$inc" Parser (Maybe (HashMap PGCol Value))
-> HashMap PGCol Value -> Parser (HashMap PGCol Value)
forall a. Parser (Maybe a) -> a -> Parser a
.!= HashMap PGCol Value
forall k v. HashMap k v
M.empty)
      Parser
  (HashMap PGCol Value -> [PGCol] -> Maybe [PGCol] -> UpdateQuery)
-> Parser (HashMap PGCol Value)
-> Parser ([PGCol] -> Maybe [PGCol] -> UpdateQuery)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Object
o Object -> Key -> Parser (Maybe (HashMap PGCol Value))
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"$mul" Parser (Maybe (HashMap PGCol Value))
-> HashMap PGCol Value -> Parser (HashMap PGCol Value)
forall a. Parser (Maybe a) -> a -> Parser a
.!= HashMap PGCol Value
forall k v. HashMap k v
M.empty)
      Parser ([PGCol] -> Maybe [PGCol] -> UpdateQuery)
-> Parser [PGCol] -> Parser (Maybe [PGCol] -> UpdateQuery)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser (Maybe [PGCol])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"$default" Parser (Maybe [PGCol]) -> [PGCol] -> Parser [PGCol]
forall a. Parser (Maybe a) -> a -> Parser a
.!= []
      Parser (Maybe [PGCol] -> UpdateQuery)
-> Parser (Maybe [PGCol]) -> Parser UpdateQuery
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser (Maybe [PGCol])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"returning"

data DeleteQuery = DeleteQuery
  { DeleteQuery -> QualifiedTable
doTable :: QualifiedTable,
    DeleteQuery -> SourceName
doSource :: SourceName,
    DeleteQuery -> BoolExp ('Postgres 'Vanilla)
doWhere :: BoolExp ('Postgres 'Vanilla), -- where clause
    DeleteQuery -> Maybe [PGCol]
doReturning :: Maybe [PGCol] -- columns returning
  }
  deriving (Int -> DeleteQuery -> ShowS
[DeleteQuery] -> ShowS
DeleteQuery -> String
(Int -> DeleteQuery -> ShowS)
-> (DeleteQuery -> String)
-> ([DeleteQuery] -> ShowS)
-> Show DeleteQuery
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [DeleteQuery] -> ShowS
$cshowList :: [DeleteQuery] -> ShowS
show :: DeleteQuery -> String
$cshow :: DeleteQuery -> String
showsPrec :: Int -> DeleteQuery -> ShowS
$cshowsPrec :: Int -> DeleteQuery -> ShowS
Show, DeleteQuery -> DeleteQuery -> Bool
(DeleteQuery -> DeleteQuery -> Bool)
-> (DeleteQuery -> DeleteQuery -> Bool) -> Eq DeleteQuery
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: DeleteQuery -> DeleteQuery -> Bool
$c/= :: DeleteQuery -> DeleteQuery -> Bool
== :: DeleteQuery -> DeleteQuery -> Bool
$c== :: DeleteQuery -> DeleteQuery -> Bool
Eq)

instance FromJSON DeleteQuery where
  parseJSON :: Value -> Parser DeleteQuery
parseJSON = String
-> (Object -> Parser DeleteQuery) -> Value -> Parser DeleteQuery
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"delete query" ((Object -> Parser DeleteQuery) -> Value -> Parser DeleteQuery)
-> (Object -> Parser DeleteQuery) -> Value -> Parser DeleteQuery
forall a b. (a -> b) -> a -> b
$ \Object
o ->
    QualifiedTable
-> SourceName
-> BoolExp ('Postgres 'Vanilla)
-> Maybe [PGCol]
-> DeleteQuery
DeleteQuery
      (QualifiedTable
 -> SourceName
 -> BoolExp ('Postgres 'Vanilla)
 -> Maybe [PGCol]
 -> DeleteQuery)
-> Parser QualifiedTable
-> Parser
     (SourceName
      -> BoolExp ('Postgres 'Vanilla) -> Maybe [PGCol] -> DeleteQuery)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser QualifiedTable
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"table"
      Parser
  (SourceName
   -> BoolExp ('Postgres 'Vanilla) -> Maybe [PGCol] -> DeleteQuery)
-> Parser SourceName
-> Parser
     (BoolExp ('Postgres 'Vanilla) -> Maybe [PGCol] -> DeleteQuery)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser (Maybe SourceName)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"source" Parser (Maybe SourceName) -> SourceName -> Parser SourceName
forall a. Parser (Maybe a) -> a -> Parser a
.!= SourceName
defaultSource
      Parser
  (BoolExp ('Postgres 'Vanilla) -> Maybe [PGCol] -> DeleteQuery)
-> Parser (BoolExp ('Postgres 'Vanilla))
-> Parser (Maybe [PGCol] -> DeleteQuery)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser (BoolExp ('Postgres 'Vanilla))
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"where"
      Parser (Maybe [PGCol] -> DeleteQuery)
-> Parser (Maybe [PGCol]) -> Parser DeleteQuery
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser (Maybe [PGCol])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"returning"

data CountQuery = CountQuery
  { CountQuery -> QualifiedTable
cqTable :: QualifiedTable,
    CountQuery -> SourceName
cqSource :: SourceName,
    CountQuery -> Maybe [PGCol]
cqDistinct :: Maybe [PGCol],
    CountQuery -> Maybe (BoolExp ('Postgres 'Vanilla))
cqWhere :: Maybe (BoolExp ('Postgres 'Vanilla))
  }
  deriving (Int -> CountQuery -> ShowS
[CountQuery] -> ShowS
CountQuery -> String
(Int -> CountQuery -> ShowS)
-> (CountQuery -> String)
-> ([CountQuery] -> ShowS)
-> Show CountQuery
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CountQuery] -> ShowS
$cshowList :: [CountQuery] -> ShowS
show :: CountQuery -> String
$cshow :: CountQuery -> String
showsPrec :: Int -> CountQuery -> ShowS
$cshowsPrec :: Int -> CountQuery -> ShowS
Show, CountQuery -> CountQuery -> Bool
(CountQuery -> CountQuery -> Bool)
-> (CountQuery -> CountQuery -> Bool) -> Eq CountQuery
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CountQuery -> CountQuery -> Bool
$c/= :: CountQuery -> CountQuery -> Bool
== :: CountQuery -> CountQuery -> Bool
$c== :: CountQuery -> CountQuery -> Bool
Eq)

instance FromJSON CountQuery where
  parseJSON :: Value -> Parser CountQuery
parseJSON = String
-> (Object -> Parser CountQuery) -> Value -> Parser CountQuery
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"count query" ((Object -> Parser CountQuery) -> Value -> Parser CountQuery)
-> (Object -> Parser CountQuery) -> Value -> Parser CountQuery
forall a b. (a -> b) -> a -> b
$ \Object
o ->
    QualifiedTable
-> SourceName
-> Maybe [PGCol]
-> Maybe (BoolExp ('Postgres 'Vanilla))
-> CountQuery
CountQuery
      (QualifiedTable
 -> SourceName
 -> Maybe [PGCol]
 -> Maybe (BoolExp ('Postgres 'Vanilla))
 -> CountQuery)
-> Parser QualifiedTable
-> Parser
     (SourceName
      -> Maybe [PGCol]
      -> Maybe (BoolExp ('Postgres 'Vanilla))
      -> CountQuery)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser QualifiedTable
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"table"
      Parser
  (SourceName
   -> Maybe [PGCol]
   -> Maybe (BoolExp ('Postgres 'Vanilla))
   -> CountQuery)
-> Parser SourceName
-> Parser
     (Maybe [PGCol]
      -> Maybe (BoolExp ('Postgres 'Vanilla)) -> CountQuery)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser (Maybe SourceName)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"source" Parser (Maybe SourceName) -> SourceName -> Parser SourceName
forall a. Parser (Maybe a) -> a -> Parser a
.!= SourceName
defaultSource
      Parser
  (Maybe [PGCol]
   -> Maybe (BoolExp ('Postgres 'Vanilla)) -> CountQuery)
-> Parser (Maybe [PGCol])
-> Parser (Maybe (BoolExp ('Postgres 'Vanilla)) -> CountQuery)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser (Maybe [PGCol])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"distinct"
      Parser (Maybe (BoolExp ('Postgres 'Vanilla)) -> CountQuery)
-> Parser (Maybe (BoolExp ('Postgres 'Vanilla)))
-> Parser CountQuery
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser (Maybe (BoolExp ('Postgres 'Vanilla)))
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"where"

data QueryT
  = QTInsert InsertQuery
  | QTSelect SelectQueryT
  | QTUpdate UpdateQuery
  | QTDelete DeleteQuery
  | QTCount CountQuery
  | QTBulk [QueryT]
  deriving (Int -> QueryT -> ShowS
[QueryT] -> ShowS
QueryT -> String
(Int -> QueryT -> ShowS)
-> (QueryT -> String) -> ([QueryT] -> ShowS) -> Show QueryT
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [QueryT] -> ShowS
$cshowList :: [QueryT] -> ShowS
show :: QueryT -> String
$cshow :: QueryT -> String
showsPrec :: Int -> QueryT -> ShowS
$cshowsPrec :: Int -> QueryT -> ShowS
Show, QueryT -> QueryT -> Bool
(QueryT -> QueryT -> Bool)
-> (QueryT -> QueryT -> Bool) -> Eq QueryT
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: QueryT -> QueryT -> Bool
$c/= :: QueryT -> QueryT -> Bool
== :: QueryT -> QueryT -> Bool
$c== :: QueryT -> QueryT -> Bool
Eq)

$( deriveFromJSON
     defaultOptions
       { constructorTagModifier = snakeCase . drop 2,
         sumEncoding = TaggedObject "type" "args"
       }
     ''QueryT
 )