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

# · Python · 85 lines · 51 code · 12 blank · 22 comment · 7 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. class Query(object):
  22. __local_iter__ = None
  23. def __init__(self, model_class, limit=None, next_token=None, manager=None):
  24. self.model_class = model_class
  25. self.limit = limit
  26. self.offset = 0
  27. if manager:
  28. self.manager = manager
  29. else:
  30. self.manager = self.model_class._manager
  31. self.filters = []
  32. self.select = None
  33. self.sort_by = None
  34. self.rs = None
  35. self.next_token = next_token
  36. def __iter__(self):
  37. return iter(self.manager.query(self))
  38. def next(self):
  39. if self.__local_iter__ == None:
  40. self.__local_iter__ = self.__iter__()
  41. return self.__local_iter__.next()
  42. def filter(self, property_operator, value):
  43. self.filters.append((property_operator, value))
  44. return self
  45. def fetch(self, limit, offset=0):
  46. """Not currently fully supported, but we can use this
  47. to allow them to set a limit in a chainable method"""
  48. self.limit = limit
  49. self.offset = offset
  50. return self
  51. def count(self, quick=True):
  52. return self.manager.count(self.model_class, self.filters, quick, self.sort_by, self.select)
  53. def get_query(self):
  54. return self.manager._build_filter_part(self.model_class, self.filters, self.sort_by, self.select)
  55. def order(self, key):
  56. self.sort_by = key
  57. return self
  58. def to_xml(self, doc=None):
  59. if not doc:
  60. xmlmanager = self.model_class.get_xmlmanager()
  61. doc = xmlmanager.new_doc()
  62. for obj in self:
  63. obj.to_xml(doc)
  64. return doc
  65. def get_next_token(self):
  66. if self.rs:
  67. return self.rs.next_token
  68. if self._next_token:
  69. return self._next_token
  70. return None
  71. def set_next_token(self, token):
  72. self._next_token = token
  73. next_token = property(get_next_token, set_next_token)