{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeFamilies #-}
module Aws.DynamoDb.Commands.Query
( Query (..)
, Slice (..)
, query
, QueryResponse (..)
) where
import Control.Applicative
import Data.Aeson
import Data.Default
import Data.Maybe
import qualified Data.Text as T
import Data.Typeable
import qualified Data.Vector as V
import Aws.Core
import Aws.DynamoDb.Core
data Slice = Slice {
Slice -> Attribute
sliceHash :: Attribute
, Slice -> Maybe Condition
sliceCond :: Maybe Condition
} deriving (Slice -> Slice -> Bool
(Slice -> Slice -> Bool) -> (Slice -> Slice -> Bool) -> Eq Slice
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Slice -> Slice -> Bool
== :: Slice -> Slice -> Bool
$c/= :: Slice -> Slice -> Bool
/= :: Slice -> Slice -> Bool
Eq,Int -> Slice -> ShowS
[Slice] -> ShowS
Slice -> String
(Int -> Slice -> ShowS)
-> (Slice -> String) -> ([Slice] -> ShowS) -> Show Slice
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Slice -> ShowS
showsPrec :: Int -> Slice -> ShowS
$cshow :: Slice -> String
show :: Slice -> String
$cshowList :: [Slice] -> ShowS
showList :: [Slice] -> ShowS
Show,ReadPrec [Slice]
ReadPrec Slice
Int -> ReadS Slice
ReadS [Slice]
(Int -> ReadS Slice)
-> ReadS [Slice]
-> ReadPrec Slice
-> ReadPrec [Slice]
-> Read Slice
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS Slice
readsPrec :: Int -> ReadS Slice
$creadList :: ReadS [Slice]
readList :: ReadS [Slice]
$creadPrec :: ReadPrec Slice
readPrec :: ReadPrec Slice
$creadListPrec :: ReadPrec [Slice]
readListPrec :: ReadPrec [Slice]
Read,Eq Slice
Eq Slice =>
(Slice -> Slice -> Ordering)
-> (Slice -> Slice -> Bool)
-> (Slice -> Slice -> Bool)
-> (Slice -> Slice -> Bool)
-> (Slice -> Slice -> Bool)
-> (Slice -> Slice -> Slice)
-> (Slice -> Slice -> Slice)
-> Ord Slice
Slice -> Slice -> Bool
Slice -> Slice -> Ordering
Slice -> Slice -> Slice
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 :: Slice -> Slice -> Ordering
compare :: Slice -> Slice -> Ordering
$c< :: Slice -> Slice -> Bool
< :: Slice -> Slice -> Bool
$c<= :: Slice -> Slice -> Bool
<= :: Slice -> Slice -> Bool
$c> :: Slice -> Slice -> Bool
> :: Slice -> Slice -> Bool
$c>= :: Slice -> Slice -> Bool
>= :: Slice -> Slice -> Bool
$cmax :: Slice -> Slice -> Slice
max :: Slice -> Slice -> Slice
$cmin :: Slice -> Slice -> Slice
min :: Slice -> Slice -> Slice
Ord,Typeable)
data Query = Query {
Query -> Text
qTableName :: T.Text
, Query -> Slice
qKeyConditions :: Slice
, Query -> Conditions
qFilter :: Conditions
, Query -> Maybe [Attribute]
qStartKey :: Maybe [Attribute]
, Query -> Maybe Int
qLimit :: Maybe Int
, Query -> Bool
qForwardScan :: Bool
, Query -> QuerySelect
qSelect :: QuerySelect
, Query -> ReturnConsumption
qRetCons :: ReturnConsumption
, Query -> Maybe Text
qIndex :: Maybe T.Text
, Query -> Bool
qConsistent :: Bool
} deriving (Query -> Query -> Bool
(Query -> Query -> Bool) -> (Query -> Query -> Bool) -> Eq Query
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Query -> Query -> Bool
== :: Query -> Query -> Bool
$c/= :: Query -> Query -> Bool
/= :: Query -> Query -> Bool
Eq,Int -> Query -> ShowS
[Query] -> ShowS
Query -> String
(Int -> Query -> ShowS)
-> (Query -> String) -> ([Query] -> ShowS) -> Show Query
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Query -> ShowS
showsPrec :: Int -> Query -> ShowS
$cshow :: Query -> String
show :: Query -> String
$cshowList :: [Query] -> ShowS
showList :: [Query] -> ShowS
Show,ReadPrec [Query]
ReadPrec Query
Int -> ReadS Query
ReadS [Query]
(Int -> ReadS Query)
-> ReadS [Query]
-> ReadPrec Query
-> ReadPrec [Query]
-> Read Query
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS Query
readsPrec :: Int -> ReadS Query
$creadList :: ReadS [Query]
readList :: ReadS [Query]
$creadPrec :: ReadPrec Query
readPrec :: ReadPrec Query
$creadListPrec :: ReadPrec [Query]
readListPrec :: ReadPrec [Query]
Read,Eq Query
Eq Query =>
(Query -> Query -> Ordering)
-> (Query -> Query -> Bool)
-> (Query -> Query -> Bool)
-> (Query -> Query -> Bool)
-> (Query -> Query -> Bool)
-> (Query -> Query -> Query)
-> (Query -> Query -> Query)
-> Ord Query
Query -> Query -> Bool
Query -> Query -> Ordering
Query -> Query -> Query
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 :: Query -> Query -> Ordering
compare :: Query -> Query -> Ordering
$c< :: Query -> Query -> Bool
< :: Query -> Query -> Bool
$c<= :: Query -> Query -> Bool
<= :: Query -> Query -> Bool
$c> :: Query -> Query -> Bool
> :: Query -> Query -> Bool
$c>= :: Query -> Query -> Bool
>= :: Query -> Query -> Bool
$cmax :: Query -> Query -> Query
max :: Query -> Query -> Query
$cmin :: Query -> Query -> Query
min :: Query -> Query -> Query
Ord,Typeable)
instance ToJSON Query where
toJSON :: Query -> Value
toJSON Query{Bool
Maybe Int
Maybe [Attribute]
Maybe Text
Text
QuerySelect
ReturnConsumption
Conditions
Slice
qTableName :: Query -> Text
qKeyConditions :: Query -> Slice
qFilter :: Query -> Conditions
qStartKey :: Query -> Maybe [Attribute]
qLimit :: Query -> Maybe Int
qForwardScan :: Query -> Bool
qSelect :: Query -> QuerySelect
qRetCons :: Query -> ReturnConsumption
qIndex :: Query -> Maybe Text
qConsistent :: Query -> Bool
qTableName :: Text
qKeyConditions :: Slice
qFilter :: Conditions
qStartKey :: Maybe [Attribute]
qLimit :: Maybe Int
qForwardScan :: Bool
qSelect :: QuerySelect
qRetCons :: ReturnConsumption
qIndex :: Maybe Text
qConsistent :: Bool
..} = [Pair] -> Value
object ([Pair] -> Value) -> [Pair] -> Value
forall a b. (a -> b) -> a -> b
$
[Maybe Pair] -> [Pair]
forall a. [Maybe a] -> [a]
catMaybes
[ ((Key
"ExclusiveStartKey" Key -> Value -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= ) (Value -> Pair) -> ([Attribute] -> Value) -> [Attribute] -> Pair
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Attribute] -> Value
attributesJson) ([Attribute] -> Pair) -> Maybe [Attribute] -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe [Attribute]
qStartKey
, (Key
"Limit" Key -> Int -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= ) (Int -> Pair) -> Maybe Int -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Int
qLimit
, (Key
"IndexName" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= ) (Text -> Pair) -> Maybe Text -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Text
qIndex
] [Pair] -> [Pair] -> [Pair]
forall a. [a] -> [a] -> [a]
++
Text -> Conditions -> [Pair]
conditionsJson Text
"QueryFilter" Conditions
qFilter [Pair] -> [Pair] -> [Pair]
forall a. [a] -> [a] -> [a]
++
QuerySelect -> [Pair]
forall t. KeyValue Value t => QuerySelect -> [t]
querySelectJson QuerySelect
qSelect [Pair] -> [Pair] -> [Pair]
forall a. [a] -> [a] -> [a]
++
[ Key
"ScanIndexForward" Key -> Bool -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Bool
qForwardScan
, Key
"TableName"Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text
qTableName
, Key
"KeyConditions" Key -> Value -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Slice -> Value
sliceJson Slice
qKeyConditions
, Key
"ReturnConsumedCapacity" Key -> ReturnConsumption -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= ReturnConsumption
qRetCons
, Key
"ConsistentRead" Key -> Bool -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Bool
qConsistent
]
query
:: T.Text
-> Slice
-> Query
query :: Text -> Slice -> Query
query Text
tn Slice
sl = Text
-> Slice
-> Conditions
-> Maybe [Attribute]
-> Maybe Int
-> Bool
-> QuerySelect
-> ReturnConsumption
-> Maybe Text
-> Bool
-> Query
Query Text
tn Slice
sl Conditions
forall a. Default a => a
def Maybe [Attribute]
forall a. Maybe a
Nothing Maybe Int
forall a. Maybe a
Nothing Bool
True QuerySelect
forall a. Default a => a
def ReturnConsumption
forall a. Default a => a
def Maybe Text
forall a. Maybe a
Nothing Bool
False
data QueryResponse = QueryResponse {
QueryResponse -> Vector Item
qrItems :: V.Vector Item
, QueryResponse -> Maybe [Attribute]
qrLastKey :: Maybe [Attribute]
, QueryResponse -> Int
qrCount :: Int
, QueryResponse -> Int
qrScanned :: Int
, QueryResponse -> Maybe ConsumedCapacity
qrConsumed :: Maybe ConsumedCapacity
} deriving (QueryResponse -> QueryResponse -> Bool
(QueryResponse -> QueryResponse -> Bool)
-> (QueryResponse -> QueryResponse -> Bool) -> Eq QueryResponse
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: QueryResponse -> QueryResponse -> Bool
== :: QueryResponse -> QueryResponse -> Bool
$c/= :: QueryResponse -> QueryResponse -> Bool
/= :: QueryResponse -> QueryResponse -> Bool
Eq,Int -> QueryResponse -> ShowS
[QueryResponse] -> ShowS
QueryResponse -> String
(Int -> QueryResponse -> ShowS)
-> (QueryResponse -> String)
-> ([QueryResponse] -> ShowS)
-> Show QueryResponse
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> QueryResponse -> ShowS
showsPrec :: Int -> QueryResponse -> ShowS
$cshow :: QueryResponse -> String
show :: QueryResponse -> String
$cshowList :: [QueryResponse] -> ShowS
showList :: [QueryResponse] -> ShowS
Show,ReadPrec [QueryResponse]
ReadPrec QueryResponse
Int -> ReadS QueryResponse
ReadS [QueryResponse]
(Int -> ReadS QueryResponse)
-> ReadS [QueryResponse]
-> ReadPrec QueryResponse
-> ReadPrec [QueryResponse]
-> Read QueryResponse
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS QueryResponse
readsPrec :: Int -> ReadS QueryResponse
$creadList :: ReadS [QueryResponse]
readList :: ReadS [QueryResponse]
$creadPrec :: ReadPrec QueryResponse
readPrec :: ReadPrec QueryResponse
$creadListPrec :: ReadPrec [QueryResponse]
readListPrec :: ReadPrec [QueryResponse]
Read,Eq QueryResponse
Eq QueryResponse =>
(QueryResponse -> QueryResponse -> Ordering)
-> (QueryResponse -> QueryResponse -> Bool)
-> (QueryResponse -> QueryResponse -> Bool)
-> (QueryResponse -> QueryResponse -> Bool)
-> (QueryResponse -> QueryResponse -> Bool)
-> (QueryResponse -> QueryResponse -> QueryResponse)
-> (QueryResponse -> QueryResponse -> QueryResponse)
-> Ord QueryResponse
QueryResponse -> QueryResponse -> Bool
QueryResponse -> QueryResponse -> Ordering
QueryResponse -> QueryResponse -> QueryResponse
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 :: QueryResponse -> QueryResponse -> Ordering
compare :: QueryResponse -> QueryResponse -> Ordering
$c< :: QueryResponse -> QueryResponse -> Bool
< :: QueryResponse -> QueryResponse -> Bool
$c<= :: QueryResponse -> QueryResponse -> Bool
<= :: QueryResponse -> QueryResponse -> Bool
$c> :: QueryResponse -> QueryResponse -> Bool
> :: QueryResponse -> QueryResponse -> Bool
$c>= :: QueryResponse -> QueryResponse -> Bool
>= :: QueryResponse -> QueryResponse -> Bool
$cmax :: QueryResponse -> QueryResponse -> QueryResponse
max :: QueryResponse -> QueryResponse -> QueryResponse
$cmin :: QueryResponse -> QueryResponse -> QueryResponse
min :: QueryResponse -> QueryResponse -> QueryResponse
Ord)
instance FromJSON QueryResponse where
parseJSON :: Value -> Parser QueryResponse
parseJSON (Object Object
v) = Vector Item
-> Maybe [Attribute]
-> Int
-> Int
-> Maybe ConsumedCapacity
-> QueryResponse
QueryResponse
(Vector Item
-> Maybe [Attribute]
-> Int
-> Int
-> Maybe ConsumedCapacity
-> QueryResponse)
-> Parser (Vector Item)
-> Parser
(Maybe [Attribute]
-> Int -> Int -> Maybe ConsumedCapacity -> QueryResponse)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v Object -> Key -> Parser (Maybe (Vector Item))
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"Items" Parser (Maybe (Vector Item)) -> Vector Item -> Parser (Vector Item)
forall a. Parser (Maybe a) -> a -> Parser a
.!= Vector Item
forall a. Vector a
V.empty
Parser
(Maybe [Attribute]
-> Int -> Int -> Maybe ConsumedCapacity -> QueryResponse)
-> Parser (Maybe [Attribute])
-> Parser (Int -> Int -> Maybe ConsumedCapacity -> QueryResponse)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ((do Value
o <- Object
v Object -> Key -> Parser Value
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"LastEvaluatedKey"
[Attribute] -> Maybe [Attribute]
forall a. a -> Maybe a
Just ([Attribute] -> Maybe [Attribute])
-> Parser [Attribute] -> Parser (Maybe [Attribute])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser [Attribute]
parseAttributeJson Value
o)
Parser (Maybe [Attribute])
-> Parser (Maybe [Attribute]) -> Parser (Maybe [Attribute])
forall a. Parser a -> Parser a -> Parser a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Maybe [Attribute] -> Parser (Maybe [Attribute])
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe [Attribute]
forall a. Maybe a
Nothing)
Parser (Int -> Int -> Maybe ConsumedCapacity -> QueryResponse)
-> Parser Int
-> Parser (Int -> Maybe ConsumedCapacity -> QueryResponse)
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 Int
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"Count"
Parser (Int -> Maybe ConsumedCapacity -> QueryResponse)
-> Parser Int -> Parser (Maybe ConsumedCapacity -> QueryResponse)
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 Int
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"ScannedCount"
Parser (Maybe ConsumedCapacity -> QueryResponse)
-> Parser (Maybe ConsumedCapacity) -> Parser QueryResponse
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 ConsumedCapacity)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"ConsumedCapacity"
parseJSON Value
_ = String -> Parser QueryResponse
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"QueryResponse must be an object."
instance Transaction Query QueryResponse
instance SignQuery Query where
type ServiceConfiguration Query = DdbConfiguration
signQuery :: forall queryType.
Query
-> ServiceConfiguration Query queryType
-> SignatureData
-> SignedQuery
signQuery Query
gi = ByteString
-> Query
-> DdbConfiguration queryType
-> SignatureData
-> SignedQuery
forall a qt.
ToJSON a =>
ByteString
-> a -> DdbConfiguration qt -> SignatureData -> SignedQuery
ddbSignQuery ByteString
"Query" Query
gi
instance ResponseConsumer r QueryResponse where
type ResponseMetadata QueryResponse = DdbResponse
responseConsumer :: Request
-> r
-> IORef (ResponseMetadata QueryResponse)
-> HTTPResponseConsumer QueryResponse
responseConsumer Request
_ r
_ IORef (ResponseMetadata QueryResponse)
ref Response (ConduitM () ByteString (ResourceT IO) ())
resp
= IORef DdbResponse -> HTTPResponseConsumer QueryResponse
forall a. FromJSON a => IORef DdbResponse -> HTTPResponseConsumer a
ddbResponseConsumer IORef (ResponseMetadata QueryResponse)
IORef DdbResponse
ref Response (ConduitM () ByteString (ResourceT IO) ())
resp
instance AsMemoryResponse QueryResponse where
type MemoryResponse QueryResponse = QueryResponse
loadToMemory :: QueryResponse -> ResourceT IO (MemoryResponse QueryResponse)
loadToMemory = QueryResponse -> ResourceT IO (MemoryResponse QueryResponse)
QueryResponse -> ResourceT IO QueryResponse
forall a. a -> ResourceT IO a
forall (m :: * -> *) a. Monad m => a -> m a
return
instance ListResponse QueryResponse Item where
listResponse :: QueryResponse -> [Item]
listResponse = Vector Item -> [Item]
forall a. Vector a -> [a]
V.toList (Vector Item -> [Item])
-> (QueryResponse -> Vector Item) -> QueryResponse -> [Item]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. QueryResponse -> Vector Item
qrItems
instance IteratedTransaction Query QueryResponse where
nextIteratedRequest :: Query -> QueryResponse -> Maybe Query
nextIteratedRequest Query
request QueryResponse
response = case QueryResponse -> Maybe [Attribute]
qrLastKey QueryResponse
response of
Maybe [Attribute]
Nothing -> Maybe Query
forall a. Maybe a
Nothing
Maybe [Attribute]
key -> Query -> Maybe Query
forall a. a -> Maybe a
Just Query
request { qStartKey = key }
sliceJson :: Slice -> Value
sliceJson :: Slice -> Value
sliceJson Slice{Maybe Condition
Attribute
sliceHash :: Slice -> Attribute
sliceCond :: Slice -> Maybe Condition
sliceHash :: Attribute
sliceCond :: Maybe Condition
..} = [Pair] -> Value
object ((Condition -> Pair) -> [Condition] -> [Pair]
forall a b. (a -> b) -> [a] -> [b]
map Condition -> Pair
conditionJson [Condition]
cs)
where
cs :: [Condition]
cs = [Condition]
-> (Condition -> [Condition]) -> Maybe Condition -> [Condition]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] Condition -> [Condition]
forall a. a -> [a]
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Condition
sliceCond [Condition] -> [Condition] -> [Condition]
forall a. [a] -> [a] -> [a]
++ [Condition
hashCond]
hashCond :: Condition
hashCond = Text -> CondOp -> Condition
Condition (Attribute -> Text
attrName Attribute
sliceHash) (DValue -> CondOp
DEq (Attribute -> DValue
attrVal Attribute
sliceHash))