{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE UndecidableInstances #-}

-- | Parser and prettyprinter for native query code.
module Hasura.NativeQuery.InterpolatedQuery
  ( ArgumentName (..),
    InterpolatedItem (..),
    InterpolatedQuery (..),
    parseInterpolatedQuery,
    getUniqueVariables,
    trimQueryEnd,
    module Hasura.LogicalModel.NullableScalarType,
  )
where

import Autodocodec
import Autodocodec qualified as AC
import Control.Lens (over, _last)
import Data.Aeson (FromJSON, ToJSON)
import Data.Bifunctor (first)
import Data.Set (Set)
import Data.Set qualified as Set
import Data.Text qualified as T
import Hasura.LogicalModel.NullableScalarType (NullableScalarType (..), nullableScalarTypeMapCodec)
import Hasura.LogicalModelResolver.Types (ArgumentName (..))
import Hasura.Prelude hiding (first)
import Language.Haskell.TH.Syntax (Lift)

newtype RawQuery = RawQuery {RawQuery -> Text
getRawQuery :: Text}
  deriving newtype (RawQuery -> RawQuery -> Bool
(RawQuery -> RawQuery -> Bool)
-> (RawQuery -> RawQuery -> Bool) -> Eq RawQuery
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: RawQuery -> RawQuery -> Bool
== :: RawQuery -> RawQuery -> Bool
$c/= :: RawQuery -> RawQuery -> Bool
/= :: RawQuery -> RawQuery -> Bool
Eq, Eq RawQuery
Eq RawQuery
-> (RawQuery -> RawQuery -> Ordering)
-> (RawQuery -> RawQuery -> Bool)
-> (RawQuery -> RawQuery -> Bool)
-> (RawQuery -> RawQuery -> Bool)
-> (RawQuery -> RawQuery -> Bool)
-> (RawQuery -> RawQuery -> RawQuery)
-> (RawQuery -> RawQuery -> RawQuery)
-> Ord RawQuery
RawQuery -> RawQuery -> Bool
RawQuery -> RawQuery -> Ordering
RawQuery -> RawQuery -> RawQuery
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
$ccompare :: RawQuery -> RawQuery -> Ordering
compare :: RawQuery -> RawQuery -> Ordering
$c< :: RawQuery -> RawQuery -> Bool
< :: RawQuery -> RawQuery -> Bool
$c<= :: RawQuery -> RawQuery -> Bool
<= :: RawQuery -> RawQuery -> Bool
$c> :: RawQuery -> RawQuery -> Bool
> :: RawQuery -> RawQuery -> Bool
$c>= :: RawQuery -> RawQuery -> Bool
>= :: RawQuery -> RawQuery -> Bool
$cmax :: RawQuery -> RawQuery -> RawQuery
max :: RawQuery -> RawQuery -> RawQuery
$cmin :: RawQuery -> RawQuery -> RawQuery
min :: RawQuery -> RawQuery -> RawQuery
Ord, Int -> RawQuery -> ShowS
[RawQuery] -> ShowS
RawQuery -> String
(Int -> RawQuery -> ShowS)
-> (RawQuery -> String) -> ([RawQuery] -> ShowS) -> Show RawQuery
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> RawQuery -> ShowS
showsPrec :: Int -> RawQuery -> ShowS
$cshow :: RawQuery -> String
show :: RawQuery -> String
$cshowList :: [RawQuery] -> ShowS
showList :: [RawQuery] -> ShowS
Show, Value -> Parser [RawQuery]
Value -> Parser RawQuery
(Value -> Parser RawQuery)
-> (Value -> Parser [RawQuery]) -> FromJSON RawQuery
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
$cparseJSON :: Value -> Parser RawQuery
parseJSON :: Value -> Parser RawQuery
$cparseJSONList :: Value -> Parser [RawQuery]
parseJSONList :: Value -> Parser [RawQuery]
FromJSON, [RawQuery] -> Value
[RawQuery] -> Encoding
RawQuery -> Value
RawQuery -> Encoding
(RawQuery -> Value)
-> (RawQuery -> Encoding)
-> ([RawQuery] -> Value)
-> ([RawQuery] -> Encoding)
-> ToJSON RawQuery
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
$ctoJSON :: RawQuery -> Value
toJSON :: RawQuery -> Value
$ctoEncoding :: RawQuery -> Encoding
toEncoding :: RawQuery -> Encoding
$ctoJSONList :: [RawQuery] -> Value
toJSONList :: [RawQuery] -> Value
$ctoEncodingList :: [RawQuery] -> Encoding
toEncodingList :: [RawQuery] -> Encoding
ToJSON)

instance HasCodec RawQuery where
  codec :: JSONCodec RawQuery
codec = (Text -> RawQuery)
-> (RawQuery -> Text)
-> Codec Value Text Text
-> JSONCodec RawQuery
forall oldOutput newOutput newInput oldInput context.
(oldOutput -> newOutput)
-> (newInput -> oldInput)
-> Codec context oldInput oldOutput
-> Codec context newInput newOutput
AC.dimapCodec Text -> RawQuery
RawQuery RawQuery -> Text
getRawQuery Codec Value Text Text
forall value. HasCodec value => JSONCodec value
codec

---------------------------------------

-- | A component of an interpolated query
data InterpolatedItem variable
  = -- | normal text
    IIText Text
  | -- | a captured variable
    IIVariable variable
  deriving stock (InterpolatedItem variable -> InterpolatedItem variable -> Bool
(InterpolatedItem variable -> InterpolatedItem variable -> Bool)
-> (InterpolatedItem variable -> InterpolatedItem variable -> Bool)
-> Eq (InterpolatedItem variable)
forall variable.
Eq variable =>
InterpolatedItem variable -> InterpolatedItem variable -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall variable.
Eq variable =>
InterpolatedItem variable -> InterpolatedItem variable -> Bool
== :: InterpolatedItem variable -> InterpolatedItem variable -> Bool
$c/= :: forall variable.
Eq variable =>
InterpolatedItem variable -> InterpolatedItem variable -> Bool
/= :: InterpolatedItem variable -> InterpolatedItem variable -> Bool
Eq, Eq (InterpolatedItem variable)
Eq (InterpolatedItem variable)
-> (InterpolatedItem variable
    -> InterpolatedItem variable -> Ordering)
-> (InterpolatedItem variable -> InterpolatedItem variable -> Bool)
-> (InterpolatedItem variable -> InterpolatedItem variable -> Bool)
-> (InterpolatedItem variable -> InterpolatedItem variable -> Bool)
-> (InterpolatedItem variable -> InterpolatedItem variable -> Bool)
-> (InterpolatedItem variable
    -> InterpolatedItem variable -> InterpolatedItem variable)
-> (InterpolatedItem variable
    -> InterpolatedItem variable -> InterpolatedItem variable)
-> Ord (InterpolatedItem variable)
InterpolatedItem variable -> InterpolatedItem variable -> Bool
InterpolatedItem variable -> InterpolatedItem variable -> Ordering
InterpolatedItem variable
-> InterpolatedItem variable -> InterpolatedItem variable
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall {variable}. Ord variable => Eq (InterpolatedItem variable)
forall variable.
Ord variable =>
InterpolatedItem variable -> InterpolatedItem variable -> Bool
forall variable.
Ord variable =>
InterpolatedItem variable -> InterpolatedItem variable -> Ordering
forall variable.
Ord variable =>
InterpolatedItem variable
-> InterpolatedItem variable -> InterpolatedItem variable
$ccompare :: forall variable.
Ord variable =>
InterpolatedItem variable -> InterpolatedItem variable -> Ordering
compare :: InterpolatedItem variable -> InterpolatedItem variable -> Ordering
$c< :: forall variable.
Ord variable =>
InterpolatedItem variable -> InterpolatedItem variable -> Bool
< :: InterpolatedItem variable -> InterpolatedItem variable -> Bool
$c<= :: forall variable.
Ord variable =>
InterpolatedItem variable -> InterpolatedItem variable -> Bool
<= :: InterpolatedItem variable -> InterpolatedItem variable -> Bool
$c> :: forall variable.
Ord variable =>
InterpolatedItem variable -> InterpolatedItem variable -> Bool
> :: InterpolatedItem variable -> InterpolatedItem variable -> Bool
$c>= :: forall variable.
Ord variable =>
InterpolatedItem variable -> InterpolatedItem variable -> Bool
>= :: InterpolatedItem variable -> InterpolatedItem variable -> Bool
$cmax :: forall variable.
Ord variable =>
InterpolatedItem variable
-> InterpolatedItem variable -> InterpolatedItem variable
max :: InterpolatedItem variable
-> InterpolatedItem variable -> InterpolatedItem variable
$cmin :: forall variable.
Ord variable =>
InterpolatedItem variable
-> InterpolatedItem variable -> InterpolatedItem variable
min :: InterpolatedItem variable
-> InterpolatedItem variable -> InterpolatedItem variable
Ord, Int -> InterpolatedItem variable -> ShowS
[InterpolatedItem variable] -> ShowS
InterpolatedItem variable -> String
(Int -> InterpolatedItem variable -> ShowS)
-> (InterpolatedItem variable -> String)
-> ([InterpolatedItem variable] -> ShowS)
-> Show (InterpolatedItem variable)
forall variable.
Show variable =>
Int -> InterpolatedItem variable -> ShowS
forall variable.
Show variable =>
[InterpolatedItem variable] -> ShowS
forall variable.
Show variable =>
InterpolatedItem variable -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall variable.
Show variable =>
Int -> InterpolatedItem variable -> ShowS
showsPrec :: Int -> InterpolatedItem variable -> ShowS
$cshow :: forall variable.
Show variable =>
InterpolatedItem variable -> String
show :: InterpolatedItem variable -> String
$cshowList :: forall variable.
Show variable =>
[InterpolatedItem variable] -> ShowS
showList :: [InterpolatedItem variable] -> ShowS
Show, (forall a b. (a -> b) -> InterpolatedItem a -> InterpolatedItem b)
-> (forall a b. a -> InterpolatedItem b -> InterpolatedItem a)
-> Functor InterpolatedItem
forall a b. a -> InterpolatedItem b -> InterpolatedItem a
forall a b. (a -> b) -> InterpolatedItem a -> InterpolatedItem b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> InterpolatedItem a -> InterpolatedItem b
fmap :: forall a b. (a -> b) -> InterpolatedItem a -> InterpolatedItem b
$c<$ :: forall a b. a -> InterpolatedItem b -> InterpolatedItem a
<$ :: forall a b. a -> InterpolatedItem b -> InterpolatedItem a
Functor, (forall m. Monoid m => InterpolatedItem m -> m)
-> (forall m a. Monoid m => (a -> m) -> InterpolatedItem a -> m)
-> (forall m a. Monoid m => (a -> m) -> InterpolatedItem a -> m)
-> (forall a b. (a -> b -> b) -> b -> InterpolatedItem a -> b)
-> (forall a b. (a -> b -> b) -> b -> InterpolatedItem a -> b)
-> (forall b a. (b -> a -> b) -> b -> InterpolatedItem a -> b)
-> (forall b a. (b -> a -> b) -> b -> InterpolatedItem a -> b)
-> (forall a. (a -> a -> a) -> InterpolatedItem a -> a)
-> (forall a. (a -> a -> a) -> InterpolatedItem a -> a)
-> (forall a. InterpolatedItem a -> [a])
-> (forall a. InterpolatedItem a -> Bool)
-> (forall a. InterpolatedItem a -> Int)
-> (forall a. Eq a => a -> InterpolatedItem a -> Bool)
-> (forall a. Ord a => InterpolatedItem a -> a)
-> (forall a. Ord a => InterpolatedItem a -> a)
-> (forall a. Num a => InterpolatedItem a -> a)
-> (forall a. Num a => InterpolatedItem a -> a)
-> Foldable InterpolatedItem
forall a. Eq a => a -> InterpolatedItem a -> Bool
forall a. Num a => InterpolatedItem a -> a
forall a. Ord a => InterpolatedItem a -> a
forall m. Monoid m => InterpolatedItem m -> m
forall a. InterpolatedItem a -> Bool
forall a. InterpolatedItem a -> Int
forall a. InterpolatedItem a -> [a]
forall a. (a -> a -> a) -> InterpolatedItem a -> a
forall m a. Monoid m => (a -> m) -> InterpolatedItem a -> m
forall b a. (b -> a -> b) -> b -> InterpolatedItem a -> b
forall a b. (a -> b -> b) -> b -> InterpolatedItem a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
$cfold :: forall m. Monoid m => InterpolatedItem m -> m
fold :: forall m. Monoid m => InterpolatedItem m -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> InterpolatedItem a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> InterpolatedItem a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> InterpolatedItem a -> m
foldMap' :: forall m a. Monoid m => (a -> m) -> InterpolatedItem a -> m
$cfoldr :: forall a b. (a -> b -> b) -> b -> InterpolatedItem a -> b
foldr :: forall a b. (a -> b -> b) -> b -> InterpolatedItem a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> InterpolatedItem a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> InterpolatedItem a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> InterpolatedItem a -> b
foldl :: forall b a. (b -> a -> b) -> b -> InterpolatedItem a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> InterpolatedItem a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> InterpolatedItem a -> b
$cfoldr1 :: forall a. (a -> a -> a) -> InterpolatedItem a -> a
foldr1 :: forall a. (a -> a -> a) -> InterpolatedItem a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> InterpolatedItem a -> a
foldl1 :: forall a. (a -> a -> a) -> InterpolatedItem a -> a
$ctoList :: forall a. InterpolatedItem a -> [a]
toList :: forall a. InterpolatedItem a -> [a]
$cnull :: forall a. InterpolatedItem a -> Bool
null :: forall a. InterpolatedItem a -> Bool
$clength :: forall a. InterpolatedItem a -> Int
length :: forall a. InterpolatedItem a -> Int
$celem :: forall a. Eq a => a -> InterpolatedItem a -> Bool
elem :: forall a. Eq a => a -> InterpolatedItem a -> Bool
$cmaximum :: forall a. Ord a => InterpolatedItem a -> a
maximum :: forall a. Ord a => InterpolatedItem a -> a
$cminimum :: forall a. Ord a => InterpolatedItem a -> a
minimum :: forall a. Ord a => InterpolatedItem a -> a
$csum :: forall a. Num a => InterpolatedItem a -> a
sum :: forall a. Num a => InterpolatedItem a -> a
$cproduct :: forall a. Num a => InterpolatedItem a -> a
product :: forall a. Num a => InterpolatedItem a -> a
Foldable, Typeable (InterpolatedItem variable)
Typeable (InterpolatedItem variable)
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g)
    -> InterpolatedItem variable
    -> c (InterpolatedItem variable))
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (InterpolatedItem variable))
-> (InterpolatedItem variable -> Constr)
-> (InterpolatedItem variable -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d))
    -> Maybe (c (InterpolatedItem variable)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c (InterpolatedItem variable)))
-> ((forall b. Data b => b -> b)
    -> InterpolatedItem variable -> InterpolatedItem variable)
-> (forall r r'.
    (r -> r' -> r)
    -> r
    -> (forall d. Data d => d -> r')
    -> InterpolatedItem variable
    -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r
    -> (forall d. Data d => d -> r')
    -> InterpolatedItem variable
    -> r)
-> (forall u.
    (forall d. Data d => d -> u) -> InterpolatedItem variable -> [u])
-> (forall u.
    Int
    -> (forall d. Data d => d -> u) -> InterpolatedItem variable -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d)
    -> InterpolatedItem variable -> m (InterpolatedItem variable))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> InterpolatedItem variable -> m (InterpolatedItem variable))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> InterpolatedItem variable -> m (InterpolatedItem variable))
-> Data (InterpolatedItem variable)
InterpolatedItem variable -> Constr
InterpolatedItem variable -> DataType
(forall b. Data b => b -> b)
-> InterpolatedItem variable -> InterpolatedItem variable
forall {variable}.
Data variable =>
Typeable (InterpolatedItem variable)
forall variable.
Data variable =>
InterpolatedItem variable -> Constr
forall variable.
Data variable =>
InterpolatedItem variable -> DataType
forall variable.
Data variable =>
(forall b. Data b => b -> b)
-> InterpolatedItem variable -> InterpolatedItem variable
forall variable u.
Data variable =>
Int
-> (forall d. Data d => d -> u) -> InterpolatedItem variable -> u
forall variable u.
Data variable =>
(forall d. Data d => d -> u) -> InterpolatedItem variable -> [u]
forall variable r r'.
Data variable =>
(r -> r' -> r)
-> r
-> (forall d. Data d => d -> r')
-> InterpolatedItem variable
-> r
forall variable r r'.
Data variable =>
(r' -> r -> r)
-> r
-> (forall d. Data d => d -> r')
-> InterpolatedItem variable
-> r
forall variable (m :: * -> *).
(Data variable, Monad m) =>
(forall d. Data d => d -> m d)
-> InterpolatedItem variable -> m (InterpolatedItem variable)
forall variable (m :: * -> *).
(Data variable, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> InterpolatedItem variable -> m (InterpolatedItem variable)
forall variable (c :: * -> *).
Data variable =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (InterpolatedItem variable)
forall variable (c :: * -> *).
Data variable =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> InterpolatedItem variable
-> c (InterpolatedItem variable)
forall variable (t :: * -> *) (c :: * -> *).
(Data variable, Typeable t) =>
(forall d. Data d => c (t d))
-> Maybe (c (InterpolatedItem variable))
forall variable (t :: * -> * -> *) (c :: * -> *).
(Data variable, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (InterpolatedItem variable))
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u.
Int
-> (forall d. Data d => d -> u) -> InterpolatedItem variable -> u
forall u.
(forall d. Data d => d -> u) -> InterpolatedItem variable -> [u]
forall r r'.
(r -> r' -> r)
-> r
-> (forall d. Data d => d -> r')
-> InterpolatedItem variable
-> r
forall r r'.
(r' -> r -> r)
-> r
-> (forall d. Data d => d -> r')
-> InterpolatedItem variable
-> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> InterpolatedItem variable -> m (InterpolatedItem variable)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> InterpolatedItem variable -> m (InterpolatedItem variable)
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (InterpolatedItem variable)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> InterpolatedItem variable
-> c (InterpolatedItem variable)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d))
-> Maybe (c (InterpolatedItem variable))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (InterpolatedItem variable))
$cgfoldl :: forall variable (c :: * -> *).
Data variable =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> InterpolatedItem variable
-> c (InterpolatedItem variable)
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> InterpolatedItem variable
-> c (InterpolatedItem variable)
$cgunfold :: forall variable (c :: * -> *).
Data variable =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (InterpolatedItem variable)
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (InterpolatedItem variable)
$ctoConstr :: forall variable.
Data variable =>
InterpolatedItem variable -> Constr
toConstr :: InterpolatedItem variable -> Constr
$cdataTypeOf :: forall variable.
Data variable =>
InterpolatedItem variable -> DataType
dataTypeOf :: InterpolatedItem variable -> DataType
$cdataCast1 :: forall variable (t :: * -> *) (c :: * -> *).
(Data variable, Typeable t) =>
(forall d. Data d => c (t d))
-> Maybe (c (InterpolatedItem variable))
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d))
-> Maybe (c (InterpolatedItem variable))
$cdataCast2 :: forall variable (t :: * -> * -> *) (c :: * -> *).
(Data variable, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (InterpolatedItem variable))
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (InterpolatedItem variable))
$cgmapT :: forall variable.
Data variable =>
(forall b. Data b => b -> b)
-> InterpolatedItem variable -> InterpolatedItem variable
gmapT :: (forall b. Data b => b -> b)
-> InterpolatedItem variable -> InterpolatedItem variable
$cgmapQl :: forall variable r r'.
Data variable =>
(r -> r' -> r)
-> r
-> (forall d. Data d => d -> r')
-> InterpolatedItem variable
-> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r
-> (forall d. Data d => d -> r')
-> InterpolatedItem variable
-> r
$cgmapQr :: forall variable r r'.
Data variable =>
(r' -> r -> r)
-> r
-> (forall d. Data d => d -> r')
-> InterpolatedItem variable
-> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r
-> (forall d. Data d => d -> r')
-> InterpolatedItem variable
-> r
$cgmapQ :: forall variable u.
Data variable =>
(forall d. Data d => d -> u) -> InterpolatedItem variable -> [u]
gmapQ :: forall u.
(forall d. Data d => d -> u) -> InterpolatedItem variable -> [u]
$cgmapQi :: forall variable u.
Data variable =>
Int
-> (forall d. Data d => d -> u) -> InterpolatedItem variable -> u
gmapQi :: forall u.
Int
-> (forall d. Data d => d -> u) -> InterpolatedItem variable -> u
$cgmapM :: forall variable (m :: * -> *).
(Data variable, Monad m) =>
(forall d. Data d => d -> m d)
-> InterpolatedItem variable -> m (InterpolatedItem variable)
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> InterpolatedItem variable -> m (InterpolatedItem variable)
$cgmapMp :: forall variable (m :: * -> *).
(Data variable, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> InterpolatedItem variable -> m (InterpolatedItem variable)
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> InterpolatedItem variable -> m (InterpolatedItem variable)
$cgmapMo :: forall variable (m :: * -> *).
(Data variable, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> InterpolatedItem variable -> m (InterpolatedItem variable)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> InterpolatedItem variable -> m (InterpolatedItem variable)
Data, (forall x.
 InterpolatedItem variable -> Rep (InterpolatedItem variable) x)
-> (forall x.
    Rep (InterpolatedItem variable) x -> InterpolatedItem variable)
-> Generic (InterpolatedItem variable)
forall x.
Rep (InterpolatedItem variable) x -> InterpolatedItem variable
forall x.
InterpolatedItem variable -> Rep (InterpolatedItem variable) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall variable x.
Rep (InterpolatedItem variable) x -> InterpolatedItem variable
forall variable x.
InterpolatedItem variable -> Rep (InterpolatedItem variable) x
$cfrom :: forall variable x.
InterpolatedItem variable -> Rep (InterpolatedItem variable) x
from :: forall x.
InterpolatedItem variable -> Rep (InterpolatedItem variable) x
$cto :: forall variable x.
Rep (InterpolatedItem variable) x -> InterpolatedItem variable
to :: forall x.
Rep (InterpolatedItem variable) x -> InterpolatedItem variable
Generic, (forall (m :: * -> *).
 Quote m =>
 InterpolatedItem variable -> m Exp)
-> (forall (m :: * -> *).
    Quote m =>
    InterpolatedItem variable -> Code m (InterpolatedItem variable))
-> Lift (InterpolatedItem variable)
forall variable (m :: * -> *).
(Lift variable, Quote m) =>
InterpolatedItem variable -> m Exp
forall variable (m :: * -> *).
(Lift variable, Quote m) =>
InterpolatedItem variable -> Code m (InterpolatedItem variable)
forall t.
(forall (m :: * -> *). Quote m => t -> m Exp)
-> (forall (m :: * -> *). Quote m => t -> Code m t) -> Lift t
forall (m :: * -> *). Quote m => InterpolatedItem variable -> m Exp
forall (m :: * -> *).
Quote m =>
InterpolatedItem variable -> Code m (InterpolatedItem variable)
$clift :: forall variable (m :: * -> *).
(Lift variable, Quote m) =>
InterpolatedItem variable -> m Exp
lift :: forall (m :: * -> *). Quote m => InterpolatedItem variable -> m Exp
$cliftTyped :: forall variable (m :: * -> *).
(Lift variable, Quote m) =>
InterpolatedItem variable -> Code m (InterpolatedItem variable)
liftTyped :: forall (m :: * -> *).
Quote m =>
InterpolatedItem variable -> Code m (InterpolatedItem variable)
Lift, Functor InterpolatedItem
Foldable InterpolatedItem
Functor InterpolatedItem
-> Foldable InterpolatedItem
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> InterpolatedItem a -> f (InterpolatedItem b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    InterpolatedItem (f a) -> f (InterpolatedItem a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> InterpolatedItem a -> m (InterpolatedItem b))
-> (forall (m :: * -> *) a.
    Monad m =>
    InterpolatedItem (m a) -> m (InterpolatedItem a))
-> Traversable InterpolatedItem
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a.
Monad m =>
InterpolatedItem (m a) -> m (InterpolatedItem a)
forall (f :: * -> *) a.
Applicative f =>
InterpolatedItem (f a) -> f (InterpolatedItem a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> InterpolatedItem a -> m (InterpolatedItem b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> InterpolatedItem a -> f (InterpolatedItem b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> InterpolatedItem a -> f (InterpolatedItem b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> InterpolatedItem a -> f (InterpolatedItem b)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
InterpolatedItem (f a) -> f (InterpolatedItem a)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
InterpolatedItem (f a) -> f (InterpolatedItem a)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> InterpolatedItem a -> m (InterpolatedItem b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> InterpolatedItem a -> m (InterpolatedItem b)
$csequence :: forall (m :: * -> *) a.
Monad m =>
InterpolatedItem (m a) -> m (InterpolatedItem a)
sequence :: forall (m :: * -> *) a.
Monad m =>
InterpolatedItem (m a) -> m (InterpolatedItem a)
Traversable)

-- | Converting an interpolated query back to text.
--   Should roundtrip with the 'parseInterpolatedQuery'.
ppInterpolatedItem :: InterpolatedItem ArgumentName -> Text
ppInterpolatedItem :: InterpolatedItem ArgumentName -> Text
ppInterpolatedItem (IIText Text
t) = Text
t
ppInterpolatedItem (IIVariable ArgumentName
v) = Text
"{{" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> ArgumentName -> Text
getArgumentName ArgumentName
v Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"}}"

deriving instance (Hashable variable) => Hashable (InterpolatedItem variable)

deriving instance (NFData variable) => NFData (InterpolatedItem variable)

---------------------------------------

-- | A list of stored procedure components representing a single stored procedure,
--   separating the variables from the text.
newtype InterpolatedQuery variable = InterpolatedQuery
  { forall variable.
InterpolatedQuery variable -> [InterpolatedItem variable]
getInterpolatedQuery :: [InterpolatedItem variable]
  }
  deriving newtype (InterpolatedQuery variable -> InterpolatedQuery variable -> Bool
(InterpolatedQuery variable -> InterpolatedQuery variable -> Bool)
-> (InterpolatedQuery variable
    -> InterpolatedQuery variable -> Bool)
-> Eq (InterpolatedQuery variable)
forall variable.
Eq variable =>
InterpolatedQuery variable -> InterpolatedQuery variable -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall variable.
Eq variable =>
InterpolatedQuery variable -> InterpolatedQuery variable -> Bool
== :: InterpolatedQuery variable -> InterpolatedQuery variable -> Bool
$c/= :: forall variable.
Eq variable =>
InterpolatedQuery variable -> InterpolatedQuery variable -> Bool
/= :: InterpolatedQuery variable -> InterpolatedQuery variable -> Bool
Eq, Eq (InterpolatedQuery variable)
Eq (InterpolatedQuery variable)
-> (InterpolatedQuery variable
    -> InterpolatedQuery variable -> Ordering)
-> (InterpolatedQuery variable
    -> InterpolatedQuery variable -> Bool)
-> (InterpolatedQuery variable
    -> InterpolatedQuery variable -> Bool)
-> (InterpolatedQuery variable
    -> InterpolatedQuery variable -> Bool)
-> (InterpolatedQuery variable
    -> InterpolatedQuery variable -> Bool)
-> (InterpolatedQuery variable
    -> InterpolatedQuery variable -> InterpolatedQuery variable)
-> (InterpolatedQuery variable
    -> InterpolatedQuery variable -> InterpolatedQuery variable)
-> Ord (InterpolatedQuery variable)
InterpolatedQuery variable -> InterpolatedQuery variable -> Bool
InterpolatedQuery variable
-> InterpolatedQuery variable -> Ordering
InterpolatedQuery variable
-> InterpolatedQuery variable -> InterpolatedQuery variable
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall {variable}. Ord variable => Eq (InterpolatedQuery variable)
forall variable.
Ord variable =>
InterpolatedQuery variable -> InterpolatedQuery variable -> Bool
forall variable.
Ord variable =>
InterpolatedQuery variable
-> InterpolatedQuery variable -> Ordering
forall variable.
Ord variable =>
InterpolatedQuery variable
-> InterpolatedQuery variable -> InterpolatedQuery variable
$ccompare :: forall variable.
Ord variable =>
InterpolatedQuery variable
-> InterpolatedQuery variable -> Ordering
compare :: InterpolatedQuery variable
-> InterpolatedQuery variable -> Ordering
$c< :: forall variable.
Ord variable =>
InterpolatedQuery variable -> InterpolatedQuery variable -> Bool
< :: InterpolatedQuery variable -> InterpolatedQuery variable -> Bool
$c<= :: forall variable.
Ord variable =>
InterpolatedQuery variable -> InterpolatedQuery variable -> Bool
<= :: InterpolatedQuery variable -> InterpolatedQuery variable -> Bool
$c> :: forall variable.
Ord variable =>
InterpolatedQuery variable -> InterpolatedQuery variable -> Bool
> :: InterpolatedQuery variable -> InterpolatedQuery variable -> Bool
$c>= :: forall variable.
Ord variable =>
InterpolatedQuery variable -> InterpolatedQuery variable -> Bool
>= :: InterpolatedQuery variable -> InterpolatedQuery variable -> Bool
$cmax :: forall variable.
Ord variable =>
InterpolatedQuery variable
-> InterpolatedQuery variable -> InterpolatedQuery variable
max :: InterpolatedQuery variable
-> InterpolatedQuery variable -> InterpolatedQuery variable
$cmin :: forall variable.
Ord variable =>
InterpolatedQuery variable
-> InterpolatedQuery variable -> InterpolatedQuery variable
min :: InterpolatedQuery variable
-> InterpolatedQuery variable -> InterpolatedQuery variable
Ord, Int -> InterpolatedQuery variable -> ShowS
[InterpolatedQuery variable] -> ShowS
InterpolatedQuery variable -> String
(Int -> InterpolatedQuery variable -> ShowS)
-> (InterpolatedQuery variable -> String)
-> ([InterpolatedQuery variable] -> ShowS)
-> Show (InterpolatedQuery variable)
forall variable.
Show variable =>
Int -> InterpolatedQuery variable -> ShowS
forall variable.
Show variable =>
[InterpolatedQuery variable] -> ShowS
forall variable.
Show variable =>
InterpolatedQuery variable -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall variable.
Show variable =>
Int -> InterpolatedQuery variable -> ShowS
showsPrec :: Int -> InterpolatedQuery variable -> ShowS
$cshow :: forall variable.
Show variable =>
InterpolatedQuery variable -> String
show :: InterpolatedQuery variable -> String
$cshowList :: forall variable.
Show variable =>
[InterpolatedQuery variable] -> ShowS
showList :: [InterpolatedQuery variable] -> ShowS
Show, (forall x.
 InterpolatedQuery variable -> Rep (InterpolatedQuery variable) x)
-> (forall x.
    Rep (InterpolatedQuery variable) x -> InterpolatedQuery variable)
-> Generic (InterpolatedQuery variable)
forall x.
Rep (InterpolatedQuery variable) x -> InterpolatedQuery variable
forall x.
InterpolatedQuery variable -> Rep (InterpolatedQuery variable) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall variable x.
Rep (InterpolatedQuery variable) x -> InterpolatedQuery variable
forall variable x.
InterpolatedQuery variable -> Rep (InterpolatedQuery variable) x
$cfrom :: forall variable x.
InterpolatedQuery variable -> Rep (InterpolatedQuery variable) x
from :: forall x.
InterpolatedQuery variable -> Rep (InterpolatedQuery variable) x
$cto :: forall variable x.
Rep (InterpolatedQuery variable) x -> InterpolatedQuery variable
to :: forall x.
Rep (InterpolatedQuery variable) x -> InterpolatedQuery variable
Generic)
  deriving stock (Typeable (InterpolatedQuery variable)
Typeable (InterpolatedQuery variable)
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g)
    -> InterpolatedQuery variable
    -> c (InterpolatedQuery variable))
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r)
    -> Constr
    -> c (InterpolatedQuery variable))
-> (InterpolatedQuery variable -> Constr)
-> (InterpolatedQuery variable -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d))
    -> Maybe (c (InterpolatedQuery variable)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c (InterpolatedQuery variable)))
-> ((forall b. Data b => b -> b)
    -> InterpolatedQuery variable -> InterpolatedQuery variable)
-> (forall r r'.
    (r -> r' -> r)
    -> r
    -> (forall d. Data d => d -> r')
    -> InterpolatedQuery variable
    -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r
    -> (forall d. Data d => d -> r')
    -> InterpolatedQuery variable
    -> r)
-> (forall u.
    (forall d. Data d => d -> u) -> InterpolatedQuery variable -> [u])
-> (forall u.
    Int
    -> (forall d. Data d => d -> u) -> InterpolatedQuery variable -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d)
    -> InterpolatedQuery variable -> m (InterpolatedQuery variable))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> InterpolatedQuery variable -> m (InterpolatedQuery variable))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> InterpolatedQuery variable -> m (InterpolatedQuery variable))
-> Data (InterpolatedQuery variable)
InterpolatedQuery variable -> Constr
InterpolatedQuery variable -> DataType
(forall b. Data b => b -> b)
-> InterpolatedQuery variable -> InterpolatedQuery variable
forall {variable}.
Data variable =>
Typeable (InterpolatedQuery variable)
forall variable.
Data variable =>
InterpolatedQuery variable -> Constr
forall variable.
Data variable =>
InterpolatedQuery variable -> DataType
forall variable.
Data variable =>
(forall b. Data b => b -> b)
-> InterpolatedQuery variable -> InterpolatedQuery variable
forall variable u.
Data variable =>
Int
-> (forall d. Data d => d -> u) -> InterpolatedQuery variable -> u
forall variable u.
Data variable =>
(forall d. Data d => d -> u) -> InterpolatedQuery variable -> [u]
forall variable r r'.
Data variable =>
(r -> r' -> r)
-> r
-> (forall d. Data d => d -> r')
-> InterpolatedQuery variable
-> r
forall variable r r'.
Data variable =>
(r' -> r -> r)
-> r
-> (forall d. Data d => d -> r')
-> InterpolatedQuery variable
-> r
forall variable (m :: * -> *).
(Data variable, Monad m) =>
(forall d. Data d => d -> m d)
-> InterpolatedQuery variable -> m (InterpolatedQuery variable)
forall variable (m :: * -> *).
(Data variable, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> InterpolatedQuery variable -> m (InterpolatedQuery variable)
forall variable (c :: * -> *).
Data variable =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (InterpolatedQuery variable)
forall variable (c :: * -> *).
Data variable =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> InterpolatedQuery variable
-> c (InterpolatedQuery variable)
forall variable (t :: * -> *) (c :: * -> *).
(Data variable, Typeable t) =>
(forall d. Data d => c (t d))
-> Maybe (c (InterpolatedQuery variable))
forall variable (t :: * -> * -> *) (c :: * -> *).
(Data variable, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (InterpolatedQuery variable))
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u.
Int
-> (forall d. Data d => d -> u) -> InterpolatedQuery variable -> u
forall u.
(forall d. Data d => d -> u) -> InterpolatedQuery variable -> [u]
forall r r'.
(r -> r' -> r)
-> r
-> (forall d. Data d => d -> r')
-> InterpolatedQuery variable
-> r
forall r r'.
(r' -> r -> r)
-> r
-> (forall d. Data d => d -> r')
-> InterpolatedQuery variable
-> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> InterpolatedQuery variable -> m (InterpolatedQuery variable)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> InterpolatedQuery variable -> m (InterpolatedQuery variable)
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (InterpolatedQuery variable)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> InterpolatedQuery variable
-> c (InterpolatedQuery variable)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d))
-> Maybe (c (InterpolatedQuery variable))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (InterpolatedQuery variable))
$cgfoldl :: forall variable (c :: * -> *).
Data variable =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> InterpolatedQuery variable
-> c (InterpolatedQuery variable)
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> InterpolatedQuery variable
-> c (InterpolatedQuery variable)
$cgunfold :: forall variable (c :: * -> *).
Data variable =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (InterpolatedQuery variable)
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (InterpolatedQuery variable)
$ctoConstr :: forall variable.
Data variable =>
InterpolatedQuery variable -> Constr
toConstr :: InterpolatedQuery variable -> Constr
$cdataTypeOf :: forall variable.
Data variable =>
InterpolatedQuery variable -> DataType
dataTypeOf :: InterpolatedQuery variable -> DataType
$cdataCast1 :: forall variable (t :: * -> *) (c :: * -> *).
(Data variable, Typeable t) =>
(forall d. Data d => c (t d))
-> Maybe (c (InterpolatedQuery variable))
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d))
-> Maybe (c (InterpolatedQuery variable))
$cdataCast2 :: forall variable (t :: * -> * -> *) (c :: * -> *).
(Data variable, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (InterpolatedQuery variable))
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (InterpolatedQuery variable))
$cgmapT :: forall variable.
Data variable =>
(forall b. Data b => b -> b)
-> InterpolatedQuery variable -> InterpolatedQuery variable
gmapT :: (forall b. Data b => b -> b)
-> InterpolatedQuery variable -> InterpolatedQuery variable
$cgmapQl :: forall variable r r'.
Data variable =>
(r -> r' -> r)
-> r
-> (forall d. Data d => d -> r')
-> InterpolatedQuery variable
-> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r
-> (forall d. Data d => d -> r')
-> InterpolatedQuery variable
-> r
$cgmapQr :: forall variable r r'.
Data variable =>
(r' -> r -> r)
-> r
-> (forall d. Data d => d -> r')
-> InterpolatedQuery variable
-> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r
-> (forall d. Data d => d -> r')
-> InterpolatedQuery variable
-> r
$cgmapQ :: forall variable u.
Data variable =>
(forall d. Data d => d -> u) -> InterpolatedQuery variable -> [u]
gmapQ :: forall u.
(forall d. Data d => d -> u) -> InterpolatedQuery variable -> [u]
$cgmapQi :: forall variable u.
Data variable =>
Int
-> (forall d. Data d => d -> u) -> InterpolatedQuery variable -> u
gmapQi :: forall u.
Int
-> (forall d. Data d => d -> u) -> InterpolatedQuery variable -> u
$cgmapM :: forall variable (m :: * -> *).
(Data variable, Monad m) =>
(forall d. Data d => d -> m d)
-> InterpolatedQuery variable -> m (InterpolatedQuery variable)
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> InterpolatedQuery variable -> m (InterpolatedQuery variable)
$cgmapMp :: forall variable (m :: * -> *).
(Data variable, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> InterpolatedQuery variable -> m (InterpolatedQuery variable)
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> InterpolatedQuery variable -> m (InterpolatedQuery variable)
$cgmapMo :: forall variable (m :: * -> *).
(Data variable, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> InterpolatedQuery variable -> m (InterpolatedQuery variable)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> InterpolatedQuery variable -> m (InterpolatedQuery variable)
Data, (forall a b.
 (a -> b) -> InterpolatedQuery a -> InterpolatedQuery b)
-> (forall a b. a -> InterpolatedQuery b -> InterpolatedQuery a)
-> Functor InterpolatedQuery
forall a b. a -> InterpolatedQuery b -> InterpolatedQuery a
forall a b. (a -> b) -> InterpolatedQuery a -> InterpolatedQuery b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> InterpolatedQuery a -> InterpolatedQuery b
fmap :: forall a b. (a -> b) -> InterpolatedQuery a -> InterpolatedQuery b
$c<$ :: forall a b. a -> InterpolatedQuery b -> InterpolatedQuery a
<$ :: forall a b. a -> InterpolatedQuery b -> InterpolatedQuery a
Functor, (forall m. Monoid m => InterpolatedQuery m -> m)
-> (forall m a. Monoid m => (a -> m) -> InterpolatedQuery a -> m)
-> (forall m a. Monoid m => (a -> m) -> InterpolatedQuery a -> m)
-> (forall a b. (a -> b -> b) -> b -> InterpolatedQuery a -> b)
-> (forall a b. (a -> b -> b) -> b -> InterpolatedQuery a -> b)
-> (forall b a. (b -> a -> b) -> b -> InterpolatedQuery a -> b)
-> (forall b a. (b -> a -> b) -> b -> InterpolatedQuery a -> b)
-> (forall a. (a -> a -> a) -> InterpolatedQuery a -> a)
-> (forall a. (a -> a -> a) -> InterpolatedQuery a -> a)
-> (forall a. InterpolatedQuery a -> [a])
-> (forall a. InterpolatedQuery a -> Bool)
-> (forall a. InterpolatedQuery a -> Int)
-> (forall a. Eq a => a -> InterpolatedQuery a -> Bool)
-> (forall a. Ord a => InterpolatedQuery a -> a)
-> (forall a. Ord a => InterpolatedQuery a -> a)
-> (forall a. Num a => InterpolatedQuery a -> a)
-> (forall a. Num a => InterpolatedQuery a -> a)
-> Foldable InterpolatedQuery
forall a. Eq a => a -> InterpolatedQuery a -> Bool
forall a. Num a => InterpolatedQuery a -> a
forall a. Ord a => InterpolatedQuery a -> a
forall m. Monoid m => InterpolatedQuery m -> m
forall a. InterpolatedQuery a -> Bool
forall a. InterpolatedQuery a -> Int
forall a. InterpolatedQuery a -> [a]
forall a. (a -> a -> a) -> InterpolatedQuery a -> a
forall m a. Monoid m => (a -> m) -> InterpolatedQuery a -> m
forall b a. (b -> a -> b) -> b -> InterpolatedQuery a -> b
forall a b. (a -> b -> b) -> b -> InterpolatedQuery a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
$cfold :: forall m. Monoid m => InterpolatedQuery m -> m
fold :: forall m. Monoid m => InterpolatedQuery m -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> InterpolatedQuery a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> InterpolatedQuery a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> InterpolatedQuery a -> m
foldMap' :: forall m a. Monoid m => (a -> m) -> InterpolatedQuery a -> m
$cfoldr :: forall a b. (a -> b -> b) -> b -> InterpolatedQuery a -> b
foldr :: forall a b. (a -> b -> b) -> b -> InterpolatedQuery a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> InterpolatedQuery a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> InterpolatedQuery a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> InterpolatedQuery a -> b
foldl :: forall b a. (b -> a -> b) -> b -> InterpolatedQuery a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> InterpolatedQuery a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> InterpolatedQuery a -> b
$cfoldr1 :: forall a. (a -> a -> a) -> InterpolatedQuery a -> a
foldr1 :: forall a. (a -> a -> a) -> InterpolatedQuery a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> InterpolatedQuery a -> a
foldl1 :: forall a. (a -> a -> a) -> InterpolatedQuery a -> a
$ctoList :: forall a. InterpolatedQuery a -> [a]
toList :: forall a. InterpolatedQuery a -> [a]
$cnull :: forall a. InterpolatedQuery a -> Bool
null :: forall a. InterpolatedQuery a -> Bool
$clength :: forall a. InterpolatedQuery a -> Int
length :: forall a. InterpolatedQuery a -> Int
$celem :: forall a. Eq a => a -> InterpolatedQuery a -> Bool
elem :: forall a. Eq a => a -> InterpolatedQuery a -> Bool
$cmaximum :: forall a. Ord a => InterpolatedQuery a -> a
maximum :: forall a. Ord a => InterpolatedQuery a -> a
$cminimum :: forall a. Ord a => InterpolatedQuery a -> a
minimum :: forall a. Ord a => InterpolatedQuery a -> a
$csum :: forall a. Num a => InterpolatedQuery a -> a
sum :: forall a. Num a => InterpolatedQuery a -> a
$cproduct :: forall a. Num a => InterpolatedQuery a -> a
product :: forall a. Num a => InterpolatedQuery a -> a
Foldable, (forall (m :: * -> *).
 Quote m =>
 InterpolatedQuery variable -> m Exp)
-> (forall (m :: * -> *).
    Quote m =>
    InterpolatedQuery variable -> Code m (InterpolatedQuery variable))
-> Lift (InterpolatedQuery variable)
forall variable (m :: * -> *).
(Lift variable, Quote m) =>
InterpolatedQuery variable -> m Exp
forall variable (m :: * -> *).
(Lift variable, Quote m) =>
InterpolatedQuery variable -> Code m (InterpolatedQuery variable)
forall t.
(forall (m :: * -> *). Quote m => t -> m Exp)
-> (forall (m :: * -> *). Quote m => t -> Code m t) -> Lift t
forall (m :: * -> *).
Quote m =>
InterpolatedQuery variable -> m Exp
forall (m :: * -> *).
Quote m =>
InterpolatedQuery variable -> Code m (InterpolatedQuery variable)
$clift :: forall variable (m :: * -> *).
(Lift variable, Quote m) =>
InterpolatedQuery variable -> m Exp
lift :: forall (m :: * -> *).
Quote m =>
InterpolatedQuery variable -> m Exp
$cliftTyped :: forall variable (m :: * -> *).
(Lift variable, Quote m) =>
InterpolatedQuery variable -> Code m (InterpolatedQuery variable)
liftTyped :: forall (m :: * -> *).
Quote m =>
InterpolatedQuery variable -> Code m (InterpolatedQuery variable)
Lift, Functor InterpolatedQuery
Foldable InterpolatedQuery
Functor InterpolatedQuery
-> Foldable InterpolatedQuery
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> InterpolatedQuery a -> f (InterpolatedQuery b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    InterpolatedQuery (f a) -> f (InterpolatedQuery a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> InterpolatedQuery a -> m (InterpolatedQuery b))
-> (forall (m :: * -> *) a.
    Monad m =>
    InterpolatedQuery (m a) -> m (InterpolatedQuery a))
-> Traversable InterpolatedQuery
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a.
Monad m =>
InterpolatedQuery (m a) -> m (InterpolatedQuery a)
forall (f :: * -> *) a.
Applicative f =>
InterpolatedQuery (f a) -> f (InterpolatedQuery a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> InterpolatedQuery a -> m (InterpolatedQuery b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> InterpolatedQuery a -> f (InterpolatedQuery b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> InterpolatedQuery a -> f (InterpolatedQuery b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> InterpolatedQuery a -> f (InterpolatedQuery b)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
InterpolatedQuery (f a) -> f (InterpolatedQuery a)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
InterpolatedQuery (f a) -> f (InterpolatedQuery a)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> InterpolatedQuery a -> m (InterpolatedQuery b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> InterpolatedQuery a -> m (InterpolatedQuery b)
$csequence :: forall (m :: * -> *) a.
Monad m =>
InterpolatedQuery (m a) -> m (InterpolatedQuery a)
sequence :: forall (m :: * -> *) a.
Monad m =>
InterpolatedQuery (m a) -> m (InterpolatedQuery a)
Traversable)

deriving newtype instance (Hashable variable) => Hashable (InterpolatedQuery variable)

deriving newtype instance (NFData variable) => NFData (InterpolatedQuery variable)

ppInterpolatedQuery :: InterpolatedQuery ArgumentName -> Text
ppInterpolatedQuery :: InterpolatedQuery ArgumentName -> Text
ppInterpolatedQuery (InterpolatedQuery [InterpolatedItem ArgumentName]
parts) = (InterpolatedItem ArgumentName -> Text)
-> [InterpolatedItem ArgumentName] -> Text
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap InterpolatedItem ArgumentName -> Text
ppInterpolatedItem [InterpolatedItem ArgumentName]
parts

-- | We store the interpolated query as the user text and parse it back
--   when converting back to Haskell code.
instance (v ~ ArgumentName) => HasCodec (InterpolatedQuery v) where
  codec :: JSONCodec (InterpolatedQuery v)
codec =
    Text
-> JSONCodec (InterpolatedQuery v)
-> JSONCodec (InterpolatedQuery v)
forall input output.
Text -> ValueCodec input output -> ValueCodec input output
CommentCodec
      (Text
"An interpolated query expressed in native code (SQL)")
      (JSONCodec (InterpolatedQuery v)
 -> JSONCodec (InterpolatedQuery v))
-> JSONCodec (InterpolatedQuery v)
-> JSONCodec (InterpolatedQuery v)
forall a b. (a -> b) -> a -> b
$ (Text -> Either String (InterpolatedQuery v))
-> (InterpolatedQuery v -> Text)
-> Codec Value Text Text
-> JSONCodec (InterpolatedQuery v)
forall oldOutput newOutput newInput oldInput context.
(oldOutput -> Either String newOutput)
-> (newInput -> oldInput)
-> Codec context oldInput oldOutput
-> Codec context newInput newOutput
bimapCodec
        ((Text -> String)
-> Either Text (InterpolatedQuery v)
-> Either String (InterpolatedQuery v)
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first Text -> String
T.unpack (Either Text (InterpolatedQuery v)
 -> Either String (InterpolatedQuery v))
-> (Text -> Either Text (InterpolatedQuery v))
-> Text
-> Either String (InterpolatedQuery v)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either Text (InterpolatedQuery v)
Text -> Either Text (InterpolatedQuery ArgumentName)
parseInterpolatedQuery)
        InterpolatedQuery v -> Text
InterpolatedQuery ArgumentName -> Text
ppInterpolatedQuery
        Codec Value Text Text
textCodec

deriving via
  (Autodocodec (InterpolatedQuery ArgumentName))
  instance
    (v ~ ArgumentName) =>
    ToJSON (InterpolatedQuery v)

---------------------------------------

-- | extract all of the `{{ variable }}` inside our query string
parseInterpolatedQuery ::
  Text ->
  Either Text (InterpolatedQuery ArgumentName)
parseInterpolatedQuery :: Text -> Either Text (InterpolatedQuery ArgumentName)
parseInterpolatedQuery =
  ([InterpolatedItem ArgumentName] -> InterpolatedQuery ArgumentName)
-> Either Text [InterpolatedItem ArgumentName]
-> Either Text (InterpolatedQuery ArgumentName)
forall a b. (a -> b) -> Either Text a -> Either Text b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
    ( [InterpolatedItem ArgumentName] -> InterpolatedQuery ArgumentName
forall variable.
[InterpolatedItem variable] -> InterpolatedQuery variable
InterpolatedQuery
        ([InterpolatedItem ArgumentName] -> InterpolatedQuery ArgumentName)
-> ([InterpolatedItem ArgumentName]
    -> [InterpolatedItem ArgumentName])
-> [InterpolatedItem ArgumentName]
-> InterpolatedQuery ArgumentName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [InterpolatedItem ArgumentName] -> [InterpolatedItem ArgumentName]
forall {variable}.
[InterpolatedItem variable] -> [InterpolatedItem variable]
mergeAdjacent
        ([InterpolatedItem ArgumentName]
 -> [InterpolatedItem ArgumentName])
-> ([InterpolatedItem ArgumentName]
    -> [InterpolatedItem ArgumentName])
-> [InterpolatedItem ArgumentName]
-> [InterpolatedItem ArgumentName]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [InterpolatedItem ArgumentName] -> [InterpolatedItem ArgumentName]
trashEmpties
    )
    (Either Text [InterpolatedItem ArgumentName]
 -> Either Text (InterpolatedQuery ArgumentName))
-> (Text -> Either Text [InterpolatedItem ArgumentName])
-> Text
-> Either Text (InterpolatedQuery ArgumentName)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Either Text [InterpolatedItem ArgumentName]
consumeString
    (String -> Either Text [InterpolatedItem ArgumentName])
-> (Text -> String)
-> Text
-> Either Text [InterpolatedItem ArgumentName]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
T.unpack
  where
    trashEmpties :: [InterpolatedItem ArgumentName] -> [InterpolatedItem ArgumentName]
trashEmpties = (InterpolatedItem ArgumentName -> Bool)
-> [InterpolatedItem ArgumentName]
-> [InterpolatedItem ArgumentName]
forall a. (a -> Bool) -> [a] -> [a]
filter (InterpolatedItem ArgumentName
-> InterpolatedItem ArgumentName -> Bool
forall a. Eq a => a -> a -> Bool
/= Text -> InterpolatedItem ArgumentName
forall variable. Text -> InterpolatedItem variable
IIText Text
"")

    mergeAdjacent :: [InterpolatedItem variable] -> [InterpolatedItem variable]
mergeAdjacent = \case
      (IIText Text
a : IIText Text
b : [InterpolatedItem variable]
rest) ->
        [InterpolatedItem variable] -> [InterpolatedItem variable]
mergeAdjacent (Text -> InterpolatedItem variable
forall variable. Text -> InterpolatedItem variable
IIText (Text
a Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
b) InterpolatedItem variable
-> [InterpolatedItem variable] -> [InterpolatedItem variable]
forall a. a -> [a] -> [a]
: [InterpolatedItem variable]
rest)
      (InterpolatedItem variable
a : [InterpolatedItem variable]
rest) -> InterpolatedItem variable
a InterpolatedItem variable
-> [InterpolatedItem variable] -> [InterpolatedItem variable]
forall a. a -> [a] -> [a]
: [InterpolatedItem variable] -> [InterpolatedItem variable]
mergeAdjacent [InterpolatedItem variable]
rest
      [] -> []

    consumeString :: String -> Either Text [InterpolatedItem ArgumentName]
    consumeString :: String -> Either Text [InterpolatedItem ArgumentName]
consumeString String
str =
      let (String
beforeCurly, String
fromCurly) = (Char -> Bool) -> String -> (String, String)
forall a. (a -> Bool) -> [a] -> ([a], [a])
break (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'{') String
str
       in case String
fromCurly of
            (Char
'{' : Char
'{' : String
rest) ->
              (Text -> InterpolatedItem ArgumentName
forall variable. Text -> InterpolatedItem variable
IIText (String -> Text
T.pack String
beforeCurly) InterpolatedItem ArgumentName
-> [InterpolatedItem ArgumentName]
-> [InterpolatedItem ArgumentName]
forall a. a -> [a] -> [a]
:) ([InterpolatedItem ArgumentName]
 -> [InterpolatedItem ArgumentName])
-> Either Text [InterpolatedItem ArgumentName]
-> Either Text [InterpolatedItem ArgumentName]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> Either Text [InterpolatedItem ArgumentName]
consumeVar String
rest
            (Char
'{' : String
other) ->
              (Text -> InterpolatedItem ArgumentName
forall variable. Text -> InterpolatedItem variable
IIText (String -> Text
T.pack (String
beforeCurly String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"{")) InterpolatedItem ArgumentName
-> [InterpolatedItem ArgumentName]
-> [InterpolatedItem ArgumentName]
forall a. a -> [a] -> [a]
:) ([InterpolatedItem ArgumentName]
 -> [InterpolatedItem ArgumentName])
-> Either Text [InterpolatedItem ArgumentName]
-> Either Text [InterpolatedItem ArgumentName]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> Either Text [InterpolatedItem ArgumentName]
consumeString String
other
            String
_other -> [InterpolatedItem ArgumentName]
-> Either Text [InterpolatedItem ArgumentName]
forall a. a -> Either Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [Text -> InterpolatedItem ArgumentName
forall variable. Text -> InterpolatedItem variable
IIText (String -> Text
T.pack String
beforeCurly)]

    consumeVar :: String -> Either Text [InterpolatedItem ArgumentName]
    consumeVar :: String -> Either Text [InterpolatedItem ArgumentName]
consumeVar String
str =
      let (String
beforeCloseCurly, String
fromClosedCurly) = (Char -> Bool) -> String -> (String, String)
forall a. (a -> Bool) -> [a] -> ([a], [a])
break (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'}') String
str
       in case String
fromClosedCurly of
            (Char
'}' : Char
'}' : String
rest) ->
              (ArgumentName -> InterpolatedItem ArgumentName
forall variable. variable -> InterpolatedItem variable
IIVariable (Text -> ArgumentName
ArgumentName (Text -> ArgumentName) -> Text -> ArgumentName
forall a b. (a -> b) -> a -> b
$ String -> Text
T.pack String
beforeCloseCurly) InterpolatedItem ArgumentName
-> [InterpolatedItem ArgumentName]
-> [InterpolatedItem ArgumentName]
forall a. a -> [a] -> [a]
:) ([InterpolatedItem ArgumentName]
 -> [InterpolatedItem ArgumentName])
-> Either Text [InterpolatedItem ArgumentName]
-> Either Text [InterpolatedItem ArgumentName]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> Either Text [InterpolatedItem ArgumentName]
consumeString String
rest
            String
_ -> Text -> Either Text [InterpolatedItem ArgumentName]
forall a b. a -> Either a b
Left Text
"Found '{{' without a matching closing '}}'"

-- | Get a set of all arguments used in an interpolated query.
getUniqueVariables :: (Ord var) => InterpolatedQuery var -> Set var
getUniqueVariables :: forall var. Ord var => InterpolatedQuery var -> Set var
getUniqueVariables (InterpolatedQuery [InterpolatedItem var]
items) =
  ((InterpolatedItem var -> Set var)
 -> [InterpolatedItem var] -> Set var)
-> [InterpolatedItem var]
-> (InterpolatedItem var -> Set var)
-> Set var
forall a b c. (a -> b -> c) -> b -> a -> c
flip (InterpolatedItem var -> Set var)
-> [InterpolatedItem var] -> Set var
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap [InterpolatedItem var]
items \case
    IIText Text
_ -> Set var
forall a. Monoid a => a
mempty
    IIVariable var
variable -> var -> Set var
forall a. a -> Set a
Set.singleton var
variable

-- | Remove spaces and semicolon from the end of a query and add a newline, for sql backends.
trimQueryEnd :: InterpolatedQuery var -> InterpolatedQuery var
trimQueryEnd :: forall var. InterpolatedQuery var -> InterpolatedQuery var
trimQueryEnd (InterpolatedQuery [InterpolatedItem var]
parts) =
  [InterpolatedItem var] -> InterpolatedQuery var
forall variable.
[InterpolatedItem variable] -> InterpolatedQuery variable
InterpolatedQuery
    ([InterpolatedItem var] -> InterpolatedQuery var)
-> [InterpolatedItem var] -> InterpolatedQuery var
forall a b. (a -> b) -> a -> b
$ ASetter
  [InterpolatedItem var]
  [InterpolatedItem var]
  (InterpolatedItem var)
  (InterpolatedItem var)
-> (InterpolatedItem var -> InterpolatedItem var)
-> [InterpolatedItem var]
-> [InterpolatedItem var]
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over ASetter
  [InterpolatedItem var]
  [InterpolatedItem var]
  (InterpolatedItem var)
  (InterpolatedItem var)
forall s a. Snoc s s a a => Traversal' s a
Traversal' [InterpolatedItem var] (InterpolatedItem var)
_last InterpolatedItem var -> InterpolatedItem var
forall {variable}.
InterpolatedItem variable -> InterpolatedItem variable
dropIt [InterpolatedItem var]
parts
    -- if the user has a comment on the last line, this will make sure it doesn't interrupt the rest of the query
    [InterpolatedItem var]
-> [InterpolatedItem var] -> [InterpolatedItem var]
forall a. Semigroup a => a -> a -> a
<> [Text -> InterpolatedItem var
forall variable. Text -> InterpolatedItem variable
IIText Text
"\n"]
  where
    dropIt :: InterpolatedItem variable -> InterpolatedItem variable
dropIt = \case
      IIText Text
txt ->
        Text -> InterpolatedItem variable
forall variable. Text -> InterpolatedItem variable
IIText
          (Text -> InterpolatedItem variable)
-> (Text -> Text) -> Text -> InterpolatedItem variable
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> Text -> Text
T.dropWhileEnd (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
';')
          (Text -> Text) -> (Text -> Text) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> Text -> Text
T.dropWhileEnd (\Char
c -> Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
' ' Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\t' Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\n')
          (Text -> InterpolatedItem variable)
-> Text -> InterpolatedItem variable
forall a b. (a -> b) -> a -> b
$ Text
txt
      IIVariable variable
v -> variable -> InterpolatedItem variable
forall variable. variable -> InterpolatedItem variable
IIVariable variable
v