~justin-fathomdb/nova/justinsb-openstack-api-volumes

« back to all changes in this revision

Viewing changes to vendor/boto/boto/sdb/db/query.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
22
class 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
        if manager:
 
28
            self.manager = manager
 
29
        else:
 
30
            self.manager = self.model_class._manager
 
31
        self.filters = []
 
32
        self.sort_by = None
 
33
        self.rs = None
 
34
        self.next_token = next_token
 
35
 
 
36
    def __iter__(self):
 
37
        return iter(self.manager.query(self))
 
38
 
 
39
    def next(self):
 
40
        if self.__local_iter__ == None:
 
41
            self.__local_iter__ = self.__iter__()
 
42
        return self.__local_iter__.next()
 
43
 
 
44
    def filter(self, property_operator, value):
 
45
        self.filters.append((property_operator, value))
 
46
        return self
 
47
 
 
48
    def fetch(self, limit, offset=0):
 
49
        raise NotImplementedError, "fetch mode is not currently supported"
 
50
 
 
51
    def count(self):
 
52
        return self.manager.count(self.model_class, self.filters)
 
53
 
 
54
    def get_query(self):
 
55
        return self.manager._build_filter_part(self.model_class, self.filters, self.sort_by)
 
56
 
 
57
    def order(self, key):
 
58
        self.sort_by = key
 
59
        return self
 
60
    
 
61
    def to_xml(self, doc=None):
 
62
        if not doc:
 
63
            xmlmanager = self.model_class.get_xmlmanager()
 
64
            doc = xmlmanager.new_doc()
 
65
        for obj in self:
 
66
            obj.to_xml(doc)
 
67
        return doc
 
68
 
 
69
    def get_next_token(self):
 
70
        if self.rs:
 
71
            return self.rs.next_token
 
72
        if self._next_token:
 
73
            return self._next_token
 
74
        return None
 
75
 
 
76
    def set_next_token(self, token):
 
77
        self._next_token = token
 
78
 
 
79
    next_token = property(get_next_token, set_next_token)