{-# LANGUAGE DeriveAnyClass #-}
{-|
Module: IHP.DataSync.DynamicQuery
Description: The normal IHP query functionality is type-safe. This module provides type-unsafe access to the database.
Copyright: (c) digitally induced GmbH, 2021
-}
module IHP.DataSync.DynamicQuery where

import IHP.ControllerPrelude hiding (OrderByClause)
import Data.Aeson
import qualified Database.PostgreSQL.Simple.FromField as PG
import qualified Database.PostgreSQL.Simple.ToField as PG
import qualified Database.PostgreSQL.Simple.Types as PG
import qualified IHP.QueryBuilder as QueryBuilder
import Data.Aeson.TH
import qualified GHC.Generics
import qualified Control.DeepSeq as DeepSeq
import qualified Data.Aeson.KeyMap as Aeson
import qualified Data.Aeson.Key as Aeson

data Field = Field { Field -> Text
fieldName :: Text, Field -> DynamicValue
fieldValue :: DynamicValue }

data DynamicValue
    = IntValue !Int
    | DoubleValue !Double
    | TextValue !Text
    | BoolValue !Bool
    | UUIDValue !UUID
    | DateTimeValue !UTCTime
    | PointValue !Point
    | IntervalValue !PGInterval
    | ArrayValue ![DynamicValue]
    | Null
    deriving (Int -> DynamicValue -> ShowS
[DynamicValue] -> ShowS
DynamicValue -> String
(Int -> DynamicValue -> ShowS)
-> (DynamicValue -> String)
-> ([DynamicValue] -> ShowS)
-> Show DynamicValue
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DynamicValue -> ShowS
showsPrec :: Int -> DynamicValue -> ShowS
$cshow :: DynamicValue -> String
show :: DynamicValue -> String
$cshowList :: [DynamicValue] -> ShowS
showList :: [DynamicValue] -> ShowS
Show, DynamicValue -> DynamicValue -> Bool
(DynamicValue -> DynamicValue -> Bool)
-> (DynamicValue -> DynamicValue -> Bool) -> Eq DynamicValue
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DynamicValue -> DynamicValue -> Bool
== :: DynamicValue -> DynamicValue -> Bool
$c/= :: DynamicValue -> DynamicValue -> Bool
/= :: DynamicValue -> DynamicValue -> Bool
Eq)

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

-- | Similiar to IHP.QueryBuilder.SQLQuery, but is designed to be accessed by external users
--
-- When compiling to SQL we have to be extra careful to escape all identifers and variables in the query.
-- The normal IHP.QueryBuilder doesn't need to be that careful as parts of the input are derived from
-- generated code from the Schema.sql.
--
data DynamicSQLQuery = DynamicSQLQuery
    { DynamicSQLQuery -> Text
table :: !Text
    , DynamicSQLQuery -> SelectedColumns
selectedColumns :: SelectedColumns
    , DynamicSQLQuery -> Maybe ConditionExpression
whereCondition :: !(Maybe ConditionExpression)
    , DynamicSQLQuery -> [OrderByClause]
orderByClause :: ![OrderByClause]
    , DynamicSQLQuery -> Maybe ByteString
distinctOnColumn :: !(Maybe ByteString)
    , DynamicSQLQuery -> Maybe Int
limit :: !(Maybe Int)
    , DynamicSQLQuery -> Maybe Int
offset :: !(Maybe Int)
    } deriving (Int -> DynamicSQLQuery -> ShowS
[DynamicSQLQuery] -> ShowS
DynamicSQLQuery -> String
(Int -> DynamicSQLQuery -> ShowS)
-> (DynamicSQLQuery -> String)
-> ([DynamicSQLQuery] -> ShowS)
-> Show DynamicSQLQuery
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DynamicSQLQuery -> ShowS
showsPrec :: Int -> DynamicSQLQuery -> ShowS
$cshow :: DynamicSQLQuery -> String
show :: DynamicSQLQuery -> String
$cshowList :: [DynamicSQLQuery] -> ShowS
showList :: [DynamicSQLQuery] -> ShowS
Show, DynamicSQLQuery -> DynamicSQLQuery -> Bool
(DynamicSQLQuery -> DynamicSQLQuery -> Bool)
-> (DynamicSQLQuery -> DynamicSQLQuery -> Bool)
-> Eq DynamicSQLQuery
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DynamicSQLQuery -> DynamicSQLQuery -> Bool
== :: DynamicSQLQuery -> DynamicSQLQuery -> Bool
$c/= :: DynamicSQLQuery -> DynamicSQLQuery -> Bool
/= :: DynamicSQLQuery -> DynamicSQLQuery -> Bool
Eq)

data OrderByClause
    = OrderByClause
        { OrderByClause -> ByteString
orderByColumn :: !ByteString
        , OrderByClause -> OrderByDirection
orderByDirection :: !OrderByDirection }
    | OrderByTSRank { OrderByClause -> Text
tsvector :: Text, OrderByClause -> Text
tsquery :: !Text }
    deriving (Int -> OrderByClause -> ShowS
[OrderByClause] -> ShowS
OrderByClause -> String
(Int -> OrderByClause -> ShowS)
-> (OrderByClause -> String)
-> ([OrderByClause] -> ShowS)
-> Show OrderByClause
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> OrderByClause -> ShowS
showsPrec :: Int -> OrderByClause -> ShowS
$cshow :: OrderByClause -> String
show :: OrderByClause -> String
$cshowList :: [OrderByClause] -> ShowS
showList :: [OrderByClause] -> ShowS
Show, OrderByClause -> OrderByClause -> Bool
(OrderByClause -> OrderByClause -> Bool)
-> (OrderByClause -> OrderByClause -> Bool) -> Eq OrderByClause
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: OrderByClause -> OrderByClause -> Bool
== :: OrderByClause -> OrderByClause -> Bool
$c/= :: OrderByClause -> OrderByClause -> Bool
/= :: OrderByClause -> OrderByClause -> Bool
Eq, (forall x. OrderByClause -> Rep OrderByClause x)
-> (forall x. Rep OrderByClause x -> OrderByClause)
-> Generic OrderByClause
forall x. Rep OrderByClause x -> OrderByClause
forall x. OrderByClause -> Rep OrderByClause x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. OrderByClause -> Rep OrderByClause x
from :: forall x. OrderByClause -> Rep OrderByClause x
$cto :: forall x. Rep OrderByClause x -> OrderByClause
to :: forall x. Rep OrderByClause x -> OrderByClause
GHC.Generics.Generic, OrderByClause -> ()
(OrderByClause -> ()) -> NFData OrderByClause
forall a. (a -> ()) -> NFData a
$crnf :: OrderByClause -> ()
rnf :: OrderByClause -> ()
DeepSeq.NFData)

-- | Represents a WHERE conditions of a 'DynamicSQLQuery'
data ConditionExpression
    = ColumnExpression { ConditionExpression -> Text
field :: !Text }
    | InfixOperatorExpression
        { ConditionExpression -> ConditionExpression
left :: !ConditionExpression
        , ConditionExpression -> ConditionOperator
op :: !ConditionOperator
        , ConditionExpression -> ConditionExpression
right :: !ConditionExpression
        }
    | LiteralExpression { ConditionExpression -> DynamicValue
value :: !DynamicValue }
    | CallExpression { ConditionExpression -> FunctionCall
functionCall :: !FunctionCall }
    | ListExpression { ConditionExpression -> [DynamicValue]
values :: ![DynamicValue] }
    deriving (Int -> ConditionExpression -> ShowS
[ConditionExpression] -> ShowS
ConditionExpression -> String
(Int -> ConditionExpression -> ShowS)
-> (ConditionExpression -> String)
-> ([ConditionExpression] -> ShowS)
-> Show ConditionExpression
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ConditionExpression -> ShowS
showsPrec :: Int -> ConditionExpression -> ShowS
$cshow :: ConditionExpression -> String
show :: ConditionExpression -> String
$cshowList :: [ConditionExpression] -> ShowS
showList :: [ConditionExpression] -> ShowS
Show, ConditionExpression -> ConditionExpression -> Bool
(ConditionExpression -> ConditionExpression -> Bool)
-> (ConditionExpression -> ConditionExpression -> Bool)
-> Eq ConditionExpression
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ConditionExpression -> ConditionExpression -> Bool
== :: ConditionExpression -> ConditionExpression -> Bool
$c/= :: ConditionExpression -> ConditionExpression -> Bool
/= :: ConditionExpression -> ConditionExpression -> Bool
Eq)

data FunctionCall
    = ToTSQuery { FunctionCall -> Text
text :: !Text } -- ^ to_tsquery('english', text)
    deriving (Int -> FunctionCall -> ShowS
[FunctionCall] -> ShowS
FunctionCall -> String
(Int -> FunctionCall -> ShowS)
-> (FunctionCall -> String)
-> ([FunctionCall] -> ShowS)
-> Show FunctionCall
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> FunctionCall -> ShowS
showsPrec :: Int -> FunctionCall -> ShowS
$cshow :: FunctionCall -> String
show :: FunctionCall -> String
$cshowList :: [FunctionCall] -> ShowS
showList :: [FunctionCall] -> ShowS
Show, FunctionCall -> FunctionCall -> Bool
(FunctionCall -> FunctionCall -> Bool)
-> (FunctionCall -> FunctionCall -> Bool) -> Eq FunctionCall
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: FunctionCall -> FunctionCall -> Bool
== :: FunctionCall -> FunctionCall -> Bool
$c/= :: FunctionCall -> FunctionCall -> Bool
/= :: FunctionCall -> FunctionCall -> Bool
Eq, (forall x. FunctionCall -> Rep FunctionCall x)
-> (forall x. Rep FunctionCall x -> FunctionCall)
-> Generic FunctionCall
forall x. Rep FunctionCall x -> FunctionCall
forall x. FunctionCall -> Rep FunctionCall x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. FunctionCall -> Rep FunctionCall x
from :: forall x. FunctionCall -> Rep FunctionCall x
$cto :: forall x. Rep FunctionCall x -> FunctionCall
to :: forall x. Rep FunctionCall x -> FunctionCall
GHC.Generics.Generic, FunctionCall -> ()
(FunctionCall -> ()) -> NFData FunctionCall
forall a. (a -> ()) -> NFData a
$crnf :: FunctionCall -> ()
rnf :: FunctionCall -> ()
DeepSeq.NFData)

-- | Operators available in WHERE conditions
data ConditionOperator
    = OpEqual -- ^ a = b
    | OpGreaterThan -- ^ a > b
    | OpLessThan -- ^ a < b
    | OpGreaterThanOrEqual -- ^ a >= b
    | OpLessThanOrEqual -- ^ a <= b
    | OpNotEqual -- ^ a <> b
    | OpAnd -- ^ a AND b
    | OpOr -- ^ a OR b
    | OpIs -- ^ a IS b
    | OpIsNot -- ^ a IS NOT b
    | OpTSMatch -- ^ tsvec_a @@ tsvec_b
    | OpIn -- ^ a IN b
    deriving (Int -> ConditionOperator -> ShowS
[ConditionOperator] -> ShowS
ConditionOperator -> String
(Int -> ConditionOperator -> ShowS)
-> (ConditionOperator -> String)
-> ([ConditionOperator] -> ShowS)
-> Show ConditionOperator
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ConditionOperator -> ShowS
showsPrec :: Int -> ConditionOperator -> ShowS
$cshow :: ConditionOperator -> String
show :: ConditionOperator -> String
$cshowList :: [ConditionOperator] -> ShowS
showList :: [ConditionOperator] -> ShowS
Show, ConditionOperator -> ConditionOperator -> Bool
(ConditionOperator -> ConditionOperator -> Bool)
-> (ConditionOperator -> ConditionOperator -> Bool)
-> Eq ConditionOperator
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ConditionOperator -> ConditionOperator -> Bool
== :: ConditionOperator -> ConditionOperator -> Bool
$c/= :: ConditionOperator -> ConditionOperator -> Bool
/= :: ConditionOperator -> ConditionOperator -> Bool
Eq)

data SelectedColumns
    = SelectAll -- ^ SELECT * FROM table
    | SelectSpecific [Text] -- ^ SELECT a, b, c FROM table
    deriving (Int -> SelectedColumns -> ShowS
[SelectedColumns] -> ShowS
SelectedColumns -> String
(Int -> SelectedColumns -> ShowS)
-> (SelectedColumns -> String)
-> ([SelectedColumns] -> ShowS)
-> Show SelectedColumns
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SelectedColumns -> ShowS
showsPrec :: Int -> SelectedColumns -> ShowS
$cshow :: SelectedColumns -> String
show :: SelectedColumns -> String
$cshowList :: [SelectedColumns] -> ShowS
showList :: [SelectedColumns] -> ShowS
Show, SelectedColumns -> SelectedColumns -> Bool
(SelectedColumns -> SelectedColumns -> Bool)
-> (SelectedColumns -> SelectedColumns -> Bool)
-> Eq SelectedColumns
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SelectedColumns -> SelectedColumns -> Bool
== :: SelectedColumns -> SelectedColumns -> Bool
$c/= :: SelectedColumns -> SelectedColumns -> Bool
/= :: SelectedColumns -> SelectedColumns -> Bool
Eq)

instance FromJSON ByteString where
    parseJSON :: Value -> Parser ByteString
parseJSON (String Text
v) = ByteString -> Parser ByteString
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> Parser ByteString)
-> ByteString -> Parser ByteString
forall a b. (a -> b) -> a -> b
$ Text -> ByteString
forall a b. ConvertibleStrings a b => a -> b
cs Text
v

instance FromJSON PG.Action where
    parseJSON :: Value -> Parser Action
parseJSON (String Text
v) = Action -> Parser Action
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> Action
PG.Escape (Text -> ByteString
forall a b. ConvertibleStrings a b => a -> b
cs Text
v))

instance {-# OVERLAPS #-} ToJSON [Field] where
    toJSON :: [Field] -> Value
toJSON [Field]
fields = [Pair] -> Value
object ((Field -> Pair) -> [Field] -> [Pair]
forall a b. (a -> b) -> [a] -> [b]
map (\Field { Text
$sel:fieldName:Field :: Field -> Text
fieldName :: Text
fieldName, DynamicValue
$sel:fieldValue:Field :: Field -> DynamicValue
fieldValue :: DynamicValue
fieldValue } -> (Text -> Key
forall a b. ConvertibleStrings a b => a -> b
cs Text
fieldName) Key -> Value -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= (DynamicValue -> Value
forall a. ToJSON a => a -> Value
toJSON DynamicValue
fieldValue)) [Field]
fields)
    toEncoding :: [Field] -> Encoding
toEncoding [Field]
fields = Series -> Encoding
pairs (Series -> Encoding) -> Series -> Encoding
forall a b. (a -> b) -> a -> b
$ (Series -> Series -> Series) -> Series -> [Series] -> Series
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Series -> Series -> Series
forall a. Semigroup a => a -> a -> a
(<>) Series
forall a. Monoid a => a
mempty [Series]
encodedFields
        where
            encodedFields :: [Series]
encodedFields = ((Field -> Series) -> [Field] -> [Series]
forall a b. (a -> b) -> [a] -> [b]
map (\Field { Text
$sel:fieldName:Field :: Field -> Text
fieldName :: Text
fieldName, DynamicValue
$sel:fieldValue:Field :: Field -> DynamicValue
fieldValue :: DynamicValue
fieldValue } -> (Text -> Key
forall a b. ConvertibleStrings a b => a -> b
cs Text
fieldName) Key -> Value -> Series
forall v. ToJSON v => Key -> v -> Series
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= (DynamicValue -> Value
forall a. ToJSON a => a -> Value
toJSON DynamicValue
fieldValue)) [Field]
fields)

instance ToJSON DynamicValue where
    toJSON :: DynamicValue -> Value
toJSON (IntValue Int
value) = Int -> Value
forall a. ToJSON a => a -> Value
toJSON Int
value
    toJSON (DoubleValue Double
value) = Double -> Value
forall a. ToJSON a => a -> Value
toJSON Double
value
    toJSON (TextValue Text
value) = Text -> Value
forall a. ToJSON a => a -> Value
toJSON Text
value
    toJSON (BoolValue Bool
value) = Bool -> Value
forall a. ToJSON a => a -> Value
toJSON Bool
value
    toJSON (UUIDValue UUID
value) = UUID -> Value
forall a. ToJSON a => a -> Value
toJSON UUID
value
    toJSON (DateTimeValue UTCTime
value) = UTCTime -> Value
forall a. ToJSON a => a -> Value
toJSON UTCTime
value
    toJSON (PointValue Point
value) = Point -> Value
forall a. ToJSON a => a -> Value
toJSON Point
value
    toJSON (IntervalValue PGInterval
value) = PGInterval -> Value
forall a. ToJSON a => a -> Value
toJSON PGInterval
value
    toJSON (ArrayValue [DynamicValue]
value) = [DynamicValue] -> Value
forall a. ToJSON a => a -> Value
toJSON [DynamicValue]
value
    toJSON DynamicValue
IHP.DataSync.DynamicQuery.Null = Value -> Value
forall a. ToJSON a => a -> Value
toJSON Value
Data.Aeson.Null

instance PG.FromField Field where
    fromField :: FieldParser Field
fromField Field
field Maybe ByteString
fieldValue' = do
            DynamicValue
fieldValue <- FieldParser DynamicValue
forall a. FromField a => FieldParser a
PG.fromField Field
field Maybe ByteString
fieldValue'
            Field -> Conversion Field
forall a. a -> Conversion a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Field { Text
DynamicValue
$sel:fieldName:Field :: Text
$sel:fieldValue:Field :: DynamicValue
fieldValue :: DynamicValue
fieldName :: Text
.. }
        where
            fieldName :: Text
fieldName = (Field -> Maybe ByteString
PG.name Field
field)
                Maybe ByteString -> (Maybe ByteString -> Maybe Text) -> Maybe Text
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> (ByteString -> Text) -> Maybe ByteString -> Maybe Text
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Text -> Text
columnNameToFieldName (Text -> Text) -> (ByteString -> Text) -> ByteString -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ByteString -> Text
forall a b. ConvertibleStrings a b => a -> b
cs)
                Maybe Text -> (Maybe Text -> Text) -> Text
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
""

instance PG.FromField DynamicValue where
    fromField :: FieldParser DynamicValue
fromField Field
field Maybe ByteString
fieldValue' = Conversion DynamicValue
fieldValue
        where
            fieldValue :: Conversion DynamicValue
fieldValue =
                    (Int -> DynamicValue
IntValue (Int -> DynamicValue) -> Conversion Int -> Conversion DynamicValue
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FieldParser Int
forall a. FromField a => FieldParser a
PG.fromField Field
field Maybe ByteString
fieldValue')
                Conversion DynamicValue
-> Conversion DynamicValue -> Conversion DynamicValue
forall a. Conversion a -> Conversion a -> Conversion a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Text -> DynamicValue
TextValue (Text -> DynamicValue)
-> Conversion Text -> Conversion DynamicValue
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FieldParser Text
forall a. FromField a => FieldParser a
PG.fromField Field
field Maybe ByteString
fieldValue')
                Conversion DynamicValue
-> Conversion DynamicValue -> Conversion DynamicValue
forall a. Conversion a -> Conversion a -> Conversion a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Bool -> DynamicValue
BoolValue (Bool -> DynamicValue)
-> Conversion Bool -> Conversion DynamicValue
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FieldParser Bool
forall a. FromField a => FieldParser a
PG.fromField Field
field Maybe ByteString
fieldValue')
                Conversion DynamicValue
-> Conversion DynamicValue -> Conversion DynamicValue
forall a. Conversion a -> Conversion a -> Conversion a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (UUID -> DynamicValue
UUIDValue (UUID -> DynamicValue)
-> Conversion UUID -> Conversion DynamicValue
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FieldParser UUID
forall a. FromField a => FieldParser a
PG.fromField Field
field Maybe ByteString
fieldValue')
                Conversion DynamicValue
-> Conversion DynamicValue -> Conversion DynamicValue
forall a. Conversion a -> Conversion a -> Conversion a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Double -> DynamicValue
DoubleValue (Double -> DynamicValue)
-> Conversion Double -> Conversion DynamicValue
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FieldParser Double
forall a. FromField a => FieldParser a
PG.fromField Field
field Maybe ByteString
fieldValue')
                Conversion DynamicValue
-> Conversion DynamicValue -> Conversion DynamicValue
forall a. Conversion a -> Conversion a -> Conversion a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (UTCTime -> DynamicValue
DateTimeValue (UTCTime -> DynamicValue)
-> Conversion UTCTime -> Conversion DynamicValue
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FieldParser UTCTime
forall a. FromField a => FieldParser a
PG.fromField Field
field Maybe ByteString
fieldValue')
                Conversion DynamicValue
-> Conversion DynamicValue -> Conversion DynamicValue
forall a. Conversion a -> Conversion a -> Conversion a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Point -> DynamicValue
PointValue (Point -> DynamicValue)
-> Conversion Point -> Conversion DynamicValue
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FieldParser Point
forall a. FromField a => FieldParser a
PG.fromField Field
field Maybe ByteString
fieldValue')
                Conversion DynamicValue
-> Conversion DynamicValue -> Conversion DynamicValue
forall a. Conversion a -> Conversion a -> Conversion a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (PGInterval -> DynamicValue
IntervalValue (PGInterval -> DynamicValue)
-> Conversion PGInterval -> Conversion DynamicValue
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FieldParser PGInterval
forall a. FromField a => FieldParser a
PG.fromField Field
field Maybe ByteString
fieldValue')
                Conversion DynamicValue
-> Conversion DynamicValue -> Conversion DynamicValue
forall a. Conversion a -> Conversion a -> Conversion a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ([DynamicValue] -> DynamicValue
ArrayValue ([DynamicValue] -> DynamicValue)
-> Conversion [DynamicValue] -> Conversion DynamicValue
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FieldParser [DynamicValue]
forall a. FromField a => FieldParser a
PG.fromField Field
field Maybe ByteString
fieldValue')
                Conversion DynamicValue
-> Conversion DynamicValue -> Conversion DynamicValue
forall a. Conversion a -> Conversion a -> Conversion a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (forall a. FromField a => FieldParser a
PG.fromField @PG.Null Field
field Maybe ByteString
fieldValue' Conversion Null
-> Conversion DynamicValue -> Conversion DynamicValue
forall a b. Conversion a -> Conversion b -> Conversion b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> DynamicValue -> Conversion DynamicValue
forall a. a -> Conversion a
forall (f :: * -> *) a. Applicative f => a -> f a
pure DynamicValue
IHP.DataSync.DynamicQuery.Null)
                Conversion DynamicValue
-> Conversion DynamicValue -> Conversion DynamicValue
forall a. Conversion a -> Conversion a -> Conversion a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> FieldParser DynamicValue
forall {f :: * -> *} {a} {p}.
(Applicative f, ConvertibleStrings a Text) =>
p -> Maybe a -> f DynamicValue
fromFieldCustomEnum Field
field Maybe ByteString
fieldValue'

            fromFieldCustomEnum :: p -> Maybe a -> f DynamicValue
fromFieldCustomEnum p
field (Just a
value) = DynamicValue -> f DynamicValue
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text -> DynamicValue
TextValue (a -> Text
forall a b. ConvertibleStrings a b => a -> b
cs a
value))
            fromFieldCustomEnum p
field Maybe a
Nothing      = DynamicValue -> f DynamicValue
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure DynamicValue
IHP.DataSync.DynamicQuery.Null

instance PG.FromField UndecodedJSON where
    fromField :: FieldParser UndecodedJSON
fromField Field
field (Just ByteString
value) = UndecodedJSON -> Conversion UndecodedJSON
forall a. a -> Conversion a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> UndecodedJSON
UndecodedJSON ByteString
value)
    fromField Field
field Maybe ByteString
Nothing = UndecodedJSON -> Conversion UndecodedJSON
forall a. a -> Conversion a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> UndecodedJSON
UndecodedJSON ByteString
"null")

-- | Returns a list of all id's in a result
recordIds :: [[Field]] -> [UUID]
recordIds :: [[Field]] -> [UUID]
recordIds [[Field]]
result = [[Field]]
result
        [[Field]] -> ([[Field]] -> [Field]) -> [Field]
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> [[Field]] -> [Field]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
        [Field] -> ([Field] -> [Field]) -> [Field]
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> (Field -> Bool) -> [Field] -> [Field]
forall a. (a -> Bool) -> [a] -> [a]
filter (\Field { Text
$sel:fieldName:Field :: Field -> Text
fieldName :: Text
fieldName } -> Text
fieldName Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"id")
        [Field] -> ([Field] -> [DynamicValue]) -> [DynamicValue]
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> (Field -> DynamicValue) -> [Field] -> [DynamicValue]
forall a b. (a -> b) -> [a] -> [b]
map (.fieldValue)
        [DynamicValue] -> ([DynamicValue] -> [UUID]) -> [UUID]
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> (DynamicValue -> Maybe UUID) -> [DynamicValue] -> [UUID]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe \case
            UUIDValue UUID
uuid -> UUID -> Maybe UUID
forall a. a -> Maybe a
Just UUID
uuid
            DynamicValue
otherwise      -> Maybe UUID
forall a. Maybe a
Nothing



-- Here you can add functions which are available in all your controllers

-- | Transforms the keys of a JSON object from field name to column name
--
-- >>> transformColumnNamesToFieldNames [json|{"isCompleted": true}|]
-- [json|{"is_completed": true}|]
transformColumnNamesToFieldNames :: Value -> Value
transformColumnNamesToFieldNames :: Value -> Value
transformColumnNamesToFieldNames (Object Object
hashMap) =
        Object
hashMap
        Object -> (Object -> [Pair]) -> [Pair]
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> Object -> [Pair]
forall v. KeyMap v -> [(Key, v)]
Aeson.toList
        [Pair] -> ([Pair] -> [Pair]) -> [Pair]
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> (Pair -> Pair) -> [Pair] -> [Pair]
forall a b. (a -> b) -> [a] -> [b]
map (\(Key
key, Value
value) -> ((Text -> Text) -> Key -> Key
applyKey Text -> Text
columnNameToFieldName Key
key, Value
value))
        [Pair] -> ([Pair] -> Object) -> Object
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> [Pair] -> Object
forall v. [(Key, v)] -> KeyMap v
Aeson.fromList
        Object -> (Object -> Value) -> Value
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> Object -> Value
Object
    where
        applyKey :: (Text -> Text) -> Key -> Key
applyKey Text -> Text
function Key
key =
            Key
key
                Key -> (Key -> Text) -> Text
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> Key -> Text
Aeson.toText
                Text -> (Text -> Text) -> Text
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> Text -> Text
function
                Text -> (Text -> Key) -> Key
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> Text -> Key
Aeson.fromText


$(deriveFromJSON defaultOptions ''FunctionCall)
$(deriveFromJSON defaultOptions 'QueryBuilder.OrCondition)
$(deriveFromJSON defaultOptions 'QueryBuilder.Join)
$(deriveFromJSON defaultOptions ''QueryBuilder.OrderByDirection)
$(deriveFromJSON defaultOptions 'QueryBuilder.OrderByClause)
$(deriveFromJSON defaultOptions 'SelectAll)
$(deriveFromJSON defaultOptions ''ConditionOperator)
$(deriveFromJSON defaultOptions ''DynamicValue)
$(deriveFromJSON defaultOptions ''ConditionExpression)

instance FromJSON DynamicSQLQuery where
    parseJSON :: Value -> Parser DynamicSQLQuery
parseJSON = String
-> (Object -> Parser DynamicSQLQuery)
-> Value
-> Parser DynamicSQLQuery
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"DynamicSQLQuery" ((Object -> Parser DynamicSQLQuery)
 -> Value -> Parser DynamicSQLQuery)
-> (Object -> Parser DynamicSQLQuery)
-> Value
-> Parser DynamicSQLQuery
forall a b. (a -> b) -> a -> b
$ \Object
v -> Text
-> SelectedColumns
-> Maybe ConditionExpression
-> [OrderByClause]
-> Maybe ByteString
-> Maybe Int
-> Maybe Int
-> DynamicSQLQuery
DynamicSQLQuery
        (Text
 -> SelectedColumns
 -> Maybe ConditionExpression
 -> [OrderByClause]
 -> Maybe ByteString
 -> Maybe Int
 -> Maybe Int
 -> DynamicSQLQuery)
-> Parser Text
-> Parser
     (SelectedColumns
      -> Maybe ConditionExpression
      -> [OrderByClause]
      -> Maybe ByteString
      -> Maybe Int
      -> Maybe Int
      -> DynamicSQLQuery)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"table"
        Parser
  (SelectedColumns
   -> Maybe ConditionExpression
   -> [OrderByClause]
   -> Maybe ByteString
   -> Maybe Int
   -> Maybe Int
   -> DynamicSQLQuery)
-> Parser SelectedColumns
-> Parser
     (Maybe ConditionExpression
      -> [OrderByClause]
      -> Maybe ByteString
      -> Maybe Int
      -> Maybe Int
      -> DynamicSQLQuery)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser SelectedColumns
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"selectedColumns"
        Parser
  (Maybe ConditionExpression
   -> [OrderByClause]
   -> Maybe ByteString
   -> Maybe Int
   -> Maybe Int
   -> DynamicSQLQuery)
-> Parser (Maybe ConditionExpression)
-> Parser
     ([OrderByClause]
      -> Maybe ByteString -> Maybe Int -> Maybe Int -> DynamicSQLQuery)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Maybe ConditionExpression)
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"whereCondition"
        Parser
  ([OrderByClause]
   -> Maybe ByteString -> Maybe Int -> Maybe Int -> DynamicSQLQuery)
-> Parser [OrderByClause]
-> Parser
     (Maybe ByteString -> Maybe Int -> Maybe Int -> DynamicSQLQuery)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser [OrderByClause]
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"orderByClause"
        Parser
  (Maybe ByteString -> Maybe Int -> Maybe Int -> DynamicSQLQuery)
-> Parser (Maybe ByteString)
-> Parser (Maybe Int -> Maybe Int -> DynamicSQLQuery)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Maybe ByteString)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"distinctOnColumn" -- distinctOnColumn can be absent in older versions of ihp-datasync.js
        Parser (Maybe Int -> Maybe Int -> DynamicSQLQuery)
-> Parser (Maybe Int) -> Parser (Maybe Int -> DynamicSQLQuery)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"limit" -- Limit can be absent in older versions of ihp-datasync.js
        Parser (Maybe Int -> DynamicSQLQuery)
-> Parser (Maybe Int) -> Parser DynamicSQLQuery
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"offset" -- Offset can be absent in older versions of ihp-datasync.js


instance FromJSON OrderByClause where
    parseJSON :: Value -> Parser OrderByClause
parseJSON = String
-> (Object -> Parser OrderByClause)
-> Value
-> Parser OrderByClause
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"OrderByClause" ((Object -> Parser OrderByClause) -> Value -> Parser OrderByClause)
-> (Object -> Parser OrderByClause)
-> Value
-> Parser OrderByClause
forall a b. (a -> b) -> a -> b
$ \Object
v -> do
        let oldFormat :: Parser OrderByClause
oldFormat = ByteString -> OrderByDirection -> OrderByClause
OrderByClause
                (ByteString -> OrderByDirection -> OrderByClause)
-> Parser ByteString -> Parser (OrderByDirection -> OrderByClause)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v Object -> Key -> Parser ByteString
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"orderByColumn"
                Parser (OrderByDirection -> OrderByClause)
-> Parser OrderByDirection -> Parser OrderByClause
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser OrderByDirection
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"orderByDirection"
        let tagged :: Parser OrderByClause
tagged = do
                Text
tag <- Object
v Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"tag"
                case Text
tag of
                    Text
"OrderByClause" -> Parser OrderByClause
oldFormat
                    Text
"OrderByTSRank" -> Text -> Text -> OrderByClause
OrderByTSRank (Text -> Text -> OrderByClause)
-> Parser Text -> Parser (Text -> OrderByClause)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"tsvector" Parser (Text -> OrderByClause)
-> Parser Text -> Parser OrderByClause
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"tsquery"
                    Text
otherwise -> Text -> Parser OrderByClause
forall a. Text -> a
error (Text
"Invalid tag: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
otherwise)
        Parser OrderByClause
tagged Parser OrderByClause
-> Parser OrderByClause -> Parser OrderByClause
forall a. Parser a -> Parser a -> Parser a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser OrderByClause
oldFormat