PageRenderTime 55ms CodeModel.GetById 47ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/boto-2.5.2/boto/sdb/db/query.py

#
Python | 85 lines | 51 code | 12 blank | 22 comment | 8 complexity | 62a2b202c81790c55c805eaea89488e9 MD5 | raw file
 1# Copyright (c) 2006,2007,2008 Mitch Garnaat http://garnaat.org/
 2#
 3# Permission is hereby granted, free of charge, to any person obtaining a
 4# copy of this software and associated documentation files (the
 5# "Software"), to deal in the Software without restriction, including
 6# without limitation the rights to use, copy, modify, merge, publish, dis-
 7# tribute, sublicense, and/or sell copies of the Software, and to permit
 8# persons to whom the Software is furnished to do so, subject to the fol-
 9# lowing conditions:
10#
11# The above copyright notice and this permission notice shall be included
12# in all copies or substantial portions of the Software.
13#
14# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
16# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
17# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
18# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20# IN THE SOFTWARE.
21
22class Query(object):
23    __local_iter__ = None
24    def __init__(self, model_class, limit=None, next_token=None, manager=None):
25        self.model_class = model_class
26        self.limit = limit
27        self.offset = 0
28        if manager:
29            self.manager = manager
30        else:
31            self.manager = self.model_class._manager
32        self.filters = []
33        self.select = None
34        self.sort_by = None
35        self.rs = None
36        self.next_token = next_token
37
38    def __iter__(self):
39        return iter(self.manager.query(self))
40
41    def next(self):
42        if self.__local_iter__ == None:
43            self.__local_iter__ = self.__iter__()
44        return self.__local_iter__.next()
45
46    def filter(self, property_operator, value):
47        self.filters.append((property_operator, value))
48        return self
49
50    def fetch(self, limit, offset=0):
51        """Not currently fully supported, but we can use this
52        to allow them to set a limit in a chainable method"""
53        self.limit = limit
54        self.offset = offset
55        return self
56
57    def count(self, quick=True):
58        return self.manager.count(self.model_class, self.filters, quick, self.sort_by, self.select)
59
60    def get_query(self):
61        return self.manager._build_filter_part(self.model_class, self.filters, self.sort_by, self.select)
62
63    def order(self, key):
64        self.sort_by = key
65        return self
66    
67    def to_xml(self, doc=None):
68        if not doc:
69            xmlmanager = self.model_class.get_xmlmanager()
70            doc = xmlmanager.new_doc()
71        for obj in self:
72            obj.to_xml(doc)
73        return doc
74
75    def get_next_token(self):
76        if self.rs:
77            return self.rs.next_token
78        if self._next_token:
79            return self._next_token
80        return None
81
82    def set_next_token(self, token):
83        self._next_token = token
84
85    next_token = property(get_next_token, set_next_token)