module Aws.SimpleDb.Commands.Select
where

import           Aws.Core
import           Aws.SimpleDb.Core
import           Control.Applicative
import           Control.Monad
import           Data.Maybe
import           Prelude
import           Text.XML.Cursor            (($//), (&|))
import qualified Data.Text                  as T
import qualified Data.Text.Encoding         as T
import qualified Text.XML.Cursor            as Cu

data Select
    = Select {
        Select -> Text
sSelectExpression :: T.Text
      , Select -> Bool
sConsistentRead :: Bool
      , Select -> Maybe Text
sNextToken :: Maybe T.Text
      }
    deriving (Int -> Select -> ShowS
[Select] -> ShowS
Select -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Select] -> ShowS
$cshowList :: [Select] -> ShowS
show :: Select -> String
$cshow :: Select -> String
showsPrec :: Int -> Select -> ShowS
$cshowsPrec :: Int -> Select -> ShowS
Show)

data SelectResponse
    = SelectResponse {
        SelectResponse -> [Item [Attribute Text]]
srItems :: [Item [Attribute T.Text]]
      , SelectResponse -> Maybe Text
srNextToken :: Maybe T.Text
      }
    deriving (Int -> SelectResponse -> ShowS
[SelectResponse] -> ShowS
SelectResponse -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SelectResponse] -> ShowS
$cshowList :: [SelectResponse] -> ShowS
show :: SelectResponse -> String
$cshow :: SelectResponse -> String
showsPrec :: Int -> SelectResponse -> ShowS
$cshowsPrec :: Int -> SelectResponse -> ShowS
Show)

select :: T.Text -> Select
select :: Text -> Select
select Text
expr = Select { sSelectExpression :: Text
sSelectExpression = Text
expr, sConsistentRead :: Bool
sConsistentRead = Bool
False, sNextToken :: Maybe Text
sNextToken = forall a. Maybe a
Nothing }

-- | ServiceConfiguration: 'SdbConfiguration'
instance SignQuery Select where
    type ServiceConfiguration Select = SdbConfiguration
    signQuery :: forall queryType.
Select
-> ServiceConfiguration Select queryType
-> SignatureData
-> SignedQuery
signQuery Select{Bool
Maybe Text
Text
sNextToken :: Maybe Text
sConsistentRead :: Bool
sSelectExpression :: Text
sNextToken :: Select -> Maybe Text
sConsistentRead :: Select -> Bool
sSelectExpression :: Select -> Text
..}
        = forall qt.
[(ByteString, ByteString)]
-> SdbConfiguration qt -> SignatureData -> SignedQuery
sdbSignQuery forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [Maybe a] -> [a]
catMaybes forall a b. (a -> b) -> a -> b
$
            [ forall a. a -> Maybe a
Just (ByteString
"Action", ByteString
"Select")
            , forall a. a -> Maybe a
Just (ByteString
"SelectExpression", Text -> ByteString
T.encodeUtf8 Text
sSelectExpression)
            , (ByteString
"ConsistentRead", ByteString
awsTrue) forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall (f :: * -> *). Alternative f => Bool -> f ()
guard Bool
sConsistentRead
            , ((ByteString
"NextToken",) forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
T.encodeUtf8) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Text
sNextToken
            ]

instance ResponseConsumer r SelectResponse where
    type ResponseMetadata SelectResponse = SdbMetadata
    responseConsumer :: Request
-> r
-> IORef (ResponseMetadata SelectResponse)
-> HTTPResponseConsumer SelectResponse
responseConsumer Request
_ r
_ = forall a.
(Cursor -> Response SdbMetadata a)
-> IORef SdbMetadata -> HTTPResponseConsumer a
sdbResponseConsumer forall {m :: * -> *}. MonadThrow m => Cursor -> m SelectResponse
parse
        where parse :: Cursor -> m SelectResponse
parse Cursor
cursor = do
                forall (m :: * -> *) a. MonadThrow m => a -> Text -> Cursor -> m a
sdbCheckResponseType () Text
"SelectResponse" Cursor
cursor
                [Item [Attribute Text]]
items <- forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence forall a b. (a -> b) -> a -> b
$ Cursor
cursor forall node a. Cursor node -> (Cursor node -> [a]) -> [a]
$// Text -> Axis
Cu.laxElement Text
"Item" forall node a b.
(Cursor node -> [a]) -> (a -> b) -> Cursor node -> [b]
&| forall (m :: * -> *).
MonadThrow m =>
Cursor -> m (Item [Attribute Text])
readItem
                let nextToken :: Maybe Text
nextToken = forall a. [a] -> Maybe a
listToMaybe forall a b. (a -> b) -> a -> b
$ Cursor
cursor forall node a. Cursor node -> (Cursor node -> [a]) -> [a]
$// Text -> Cursor -> [Text]
elContent Text
"NextToken"
                forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ [Item [Attribute Text]] -> Maybe Text -> SelectResponse
SelectResponse [Item [Attribute Text]]
items Maybe Text
nextToken

instance Transaction Select SelectResponse

instance AsMemoryResponse SelectResponse where
    type MemoryResponse SelectResponse = SelectResponse
    loadToMemory :: SelectResponse -> ResourceT IO (MemoryResponse SelectResponse)
loadToMemory = forall (m :: * -> *) a. Monad m => a -> m a
return

instance ListResponse SelectResponse (Item [Attribute T.Text]) where
    listResponse :: SelectResponse -> [Item [Attribute Text]]
listResponse = SelectResponse -> [Item [Attribute Text]]
srItems

instance IteratedTransaction Select SelectResponse where
  nextIteratedRequest :: Select -> SelectResponse -> Maybe Select
nextIteratedRequest Select
req SelectResponse{srNextToken :: SelectResponse -> Maybe Text
srNextToken=Maybe Text
nt} = Select
req{sNextToken :: Maybe Text
sNextToken=Maybe Text
nt} forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Maybe Text
nt
--  combineIteratedResponse (SelectResponse s1 _) (SelectResponse s2 nt2) = SelectResponse (s1 ++ s2) nt2