1 from notmuch2 import _base as base
2 from notmuch2 import _capi as capi
3 from notmuch2 import _errors as errors
4 from notmuch2 import _message as message
5 from notmuch2 import _thread as thread
11 class Query(base.NotmuchObject):
12 """Private, minimal query object.
14 This is not meant for users and is not a full implementation of
15 the query API. It is only an intermediate used internally to
16 match libnotmuch's memory management.
18 _query_p = base.MemoryPointer()
20 def __init__(self, db, query_p):
22 self._query_p = query_p
26 if not self._db.alive:
30 except errors.ObjectDestroyedError:
40 capi.lib.notmuch_query_destroy(self._query_p)
45 """The query string as seen by libnotmuch."""
46 q = capi.lib.notmuch_query_get_query_string(self._query_p)
47 return base.BinString.from_cffi(q)
50 """Return an iterator over all the messages found by the query.
52 This executes the query and returns an iterator over the
53 :class:`Message` objects found.
55 msgs_pp = capi.ffi.new('notmuch_messages_t**')
56 ret = capi.lib.notmuch_query_search_messages(self._query_p, msgs_pp)
57 if ret != capi.lib.NOTMUCH_STATUS_SUCCESS:
58 raise errors.NotmuchError(ret)
59 return message.MessageIter(self, msgs_pp[0], db=self._db)
61 def count_messages(self):
62 """Return the number of messages matching this query."""
63 count_p = capi.ffi.new('unsigned int *')
64 ret = capi.lib.notmuch_query_count_messages(self._query_p, count_p)
65 if ret != capi.lib.NOTMUCH_STATUS_SUCCESS:
66 raise errors.NotmuchError(ret)
70 """Return an iterator over all the threads found by the query."""
71 threads_pp = capi.ffi.new('notmuch_threads_t **')
72 ret = capi.lib.notmuch_query_search_threads(self._query_p, threads_pp)
73 if ret != capi.lib.NOTMUCH_STATUS_SUCCESS:
74 raise errors.NotmuchError(ret)
75 return thread.ThreadIter(self, threads_pp[0], db=self._db)
77 def count_threads(self):
78 """Return the number of threads matching this query."""
79 count_p = capi.ffi.new('unsigned int *')
80 ret = capi.lib.notmuch_query_count_threads(self._query_p, count_p)
81 if ret != capi.lib.NOTMUCH_STATUS_SUCCESS:
82 raise errors.NotmuchError(ret)