1from boto.compat import six
2# Copyright (c) 2006,2007 Mitch Garnaat http://garnaat.org/
3#
4# Permission is hereby granted, free of charge, to any person obtaining a
5# copy of this software and associated documentation files (the
6# "Software"), to deal in the Software without restriction, including
7# without limitation the rights to use, copy, modify, merge, publish, dis-
8# tribute, sublicense, and/or sell copies of the Software, and to permit
9# persons to whom the Software is furnished to do so, subject to the fol-
10# lowing conditions:
11#
12# The above copyright notice and this permission notice shall be included
13# in all copies or substantial portions of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
17# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
18# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21# IN THE SOFTWARE.
22
23def query_lister(domain, query='', max_items=None, attr_names=None):
24    more_results = True
25    num_results = 0
26    next_token = None
27    while more_results:
28        rs = domain.connection.query_with_attributes(domain, query, attr_names,
29                                                     next_token=next_token)
30        for item in rs:
31            if max_items:
32                if num_results == max_items:
33                    raise StopIteration
34            yield item
35            num_results += 1
36        next_token = rs.next_token
37        more_results = next_token is not None
38
39class QueryResultSet(object):
40
41    def __init__(self, domain=None, query='', max_items=None, attr_names=None):
42        self.max_items = max_items
43        self.domain = domain
44        self.query = query
45        self.attr_names = attr_names
46
47    def __iter__(self):
48        return query_lister(self.domain, self.query, self.max_items, self.attr_names)
49
50def select_lister(domain, query='', max_items=None):
51    more_results = True
52    num_results = 0
53    next_token = None
54    while more_results:
55        rs = domain.connection.select(domain, query, next_token=next_token)
56        for item in rs:
57            if max_items:
58                if num_results == max_items:
59                    raise StopIteration
60            yield item
61            num_results += 1
62        next_token = rs.next_token
63        more_results = next_token is not None
64
65class SelectResultSet(object):
66
67    def __init__(self, domain=None, query='', max_items=None,
68                 next_token=None, consistent_read=False):
69        self.domain = domain
70        self.query = query
71        self.consistent_read = consistent_read
72        self.max_items = max_items
73        self.next_token = next_token
74
75    def __iter__(self):
76        more_results = True
77        num_results = 0
78        while more_results:
79            rs = self.domain.connection.select(self.domain, self.query,
80                                               next_token=self.next_token,
81                                               consistent_read=self.consistent_read)
82            for item in rs:
83                if self.max_items and num_results >= self.max_items:
84                    raise StopIteration
85                yield item
86                num_results += 1
87            self.next_token = rs.next_token
88            if self.max_items and num_results >= self.max_items:
89                raise StopIteration
90            more_results = self.next_token is not None
91
92    def next(self):
93        return next(self.__iter__())
94