[Patch v2 13/13] lib: add support for named queries
[notmuch-archives.git] / 3a / eb3f6de7fb98c76fa6e8f262aab9a65e0f2697
1 Return-Path: <bremner@tesseract.cs.unb.ca>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5  by arlo.cworth.org (Postfix) with ESMTP id B95B66DE0962\r
6  for <notmuch@notmuchmail.org>; Sat, 26 Mar 2016 11:24:44 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at cworth.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -0.02\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-0.02 tagged_above=-999 required=5 tests=[AWL=-0.021,\r
12   HEADER_FROM_DIFFERENT_DOMAINS=0.001] autolearn=disabled\r
13 Received: from arlo.cworth.org ([127.0.0.1])\r
14  by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
15  with ESMTP id Q47M4LcfCaXq for <notmuch@notmuchmail.org>;\r
16  Sat, 26 Mar 2016 11:24:36 -0700 (PDT)\r
17 Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197])\r
18  by arlo.cworth.org (Postfix) with ESMTPS id 2B28A6DE02DA\r
19  for <notmuch@notmuchmail.org>; Sat, 26 Mar 2016 11:23:26 -0700 (PDT)\r
20 Received: from remotemail by fethera.tethera.net with local (Exim 4.84)\r
21  (envelope-from <bremner@tesseract.cs.unb.ca>)\r
22  id 1ajsTT-0000us-S4; Sat, 26 Mar 2016 13:58:11 -0400\r
23 Received: (nullmailer pid 8830 invoked by uid 1000);\r
24  Sat, 26 Mar 2016 17:57:27 -0000\r
25 From: David Bremner <david@tethera.net>\r
26 To: notmuch@notmuchmail.org\r
27 Subject: [Patch v2 13/13] lib: add support for named queries\r
28 Date: Sat, 26 Mar 2016 14:57:23 -0300\r
29 Message-Id: <1459015043-8460-14-git-send-email-david@tethera.net>\r
30 X-Mailer: git-send-email 2.6.4\r
31 In-Reply-To: <1459015043-8460-1-git-send-email-david@tethera.net>\r
32 References: <1459015043-8460-1-git-send-email-david@tethera.net>\r
33 MIME-Version: 1.0\r
34 Content-Type: text/plain; charset=UTF-8\r
35 Content-Transfer-Encoding: 8bit\r
36 X-BeenThere: notmuch@notmuchmail.org\r
37 X-Mailman-Version: 2.1.20\r
38 Precedence: list\r
39 List-Id: "Use and development of the notmuch mail system."\r
40  <notmuch.notmuchmail.org>\r
41 List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,\r
42  <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
43 List-Archive: <http://notmuchmail.org/pipermail/notmuch/>\r
44 List-Post: <mailto:notmuch@notmuchmail.org>\r
45 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
46 List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
47  <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
48 X-List-Received-Date: Sat, 26 Mar 2016 18:24:44 -0000\r
49 \r
50 This relies on the optional presense of xapian field processors, and the\r
51 library config API.\r
52 ---\r
53  doc/man7/notmuch-search-terms.rst |  8 ++++++++\r
54  lib/Makefile.local                |  1 +\r
55  lib/database-private.h            |  1 +\r
56  lib/database.cc                   |  3 +++\r
57  lib/query-fp.cc                   | 42 +++++++++++++++++++++++++++++++++++++++\r
58  lib/query-fp.h                    | 42 +++++++++++++++++++++++++++++++++++++++\r
59  test/T600-named-queries.sh        | 17 ++++++++++++++++\r
60  7 files changed, 114 insertions(+)\r
61  create mode 100644 lib/query-fp.cc\r
62  create mode 100644 lib/query-fp.h\r
63 \r
64 diff --git a/doc/man7/notmuch-search-terms.rst b/doc/man7/notmuch-search-terms.rst\r
65 index 29e8b03..7474f53 100644\r
66 --- a/doc/man7/notmuch-search-terms.rst\r
67 +++ b/doc/man7/notmuch-search-terms.rst\r
68 @@ -56,6 +56,8 @@ indicate user-supplied values):\r
69  \r
70  -  lastmod:<initial-revision>..<final-revision>\r
71  \r
72 +-  query:<name>\r
73 +\r
74  The **from:** prefix is used to match the name or address of the sender\r
75  of an email message.\r
76  \r
77 @@ -132,6 +134,11 @@ were added/removed or filenames changed).  This is usually used in\r
78  conjunction with the **--uuid** argument to **notmuch search**\r
79  to find messages that have changed since an earlier query.\r
80  \r
81 +The **query:** prefix allows queries to refer to previously saved\r
82 +queries added with **notmuch-config(1)**. Named queries are only\r
83 +available if notmuch is built with **Xapian Field Processors** (see\r
84 +below).\r
85 +\r
86  Operators\r
87  ---------\r
88  \r
89 @@ -385,6 +392,7 @@ notmuch was built against a sufficiently recent version of Xapian by running\r
90  Currently the following features require field processor support:\r
91  \r
92  - non-range date queries, e.g. "date:today"\r
93 +- named queries e.g. "query:my_special_query"\r
94  \r
95  SEE ALSO\r
96  ========\r
97 diff --git a/lib/Makefile.local b/lib/Makefile.local\r
98 index 96899bf..fab1242 100644\r
99 --- a/lib/Makefile.local\r
100 +++ b/lib/Makefile.local\r
101 @@ -49,6 +49,7 @@ libnotmuch_cxx_srcs =         \\r
102         $(dir)/index.cc         \\r
103         $(dir)/message.cc       \\r
104         $(dir)/query.cc         \\r
105 +       $(dir)/query-fp.cc \\r
106         $(dir)/config.cc        \\r
107         $(dir)/thread.cc\r
108  \r
109 diff --git a/lib/database-private.h b/lib/database-private.h\r
110 index b5c1f90..5ab4001 100644\r
111 --- a/lib/database-private.h\r
112 +++ b/lib/database-private.h\r
113 @@ -182,6 +182,7 @@ struct _notmuch_database {\r
114      Xapian::ValueRangeProcessor *date_range_processor;\r
115  #if HAVE_XAPIAN_FIELD_PROCESSOR\r
116      Xapian::FieldProcessor *date_field_processor;\r
117 +    Xapian::FieldProcessor *query_field_processor;\r
118  #endif\r
119      Xapian::ValueRangeProcessor *last_mod_range_processor;\r
120  };\r
121 diff --git a/lib/database.cc b/lib/database.cc\r
122 index 0e3c90c..8df5555 100644\r
123 --- a/lib/database.cc\r
124 +++ b/lib/database.cc\r
125 @@ -20,6 +20,7 @@\r
126  \r
127  #include "database-private.h"\r
128  #include "parse-time-vrp.h"\r
129 +#include "query-fp.h"\r
130  #include "string-util.h"\r
131  \r
132  #include <iostream>\r
133 @@ -1005,6 +1006,8 @@ notmuch_database_open_verbose (const char *path,\r
134          * with a .. to the range processor */\r
135         notmuch->date_field_processor = new DateFieldProcessor();\r
136         notmuch->query_parser->add_boolean_prefix("date", notmuch->date_field_processor);\r
137 +       notmuch->query_field_processor = new QueryFieldProcessor (*notmuch->query_parser, notmuch);\r
138 +       notmuch->query_parser->add_boolean_prefix("query", notmuch->query_field_processor);\r
139  #endif\r
140         notmuch->last_mod_range_processor = new Xapian::NumberValueRangeProcessor (NOTMUCH_VALUE_LAST_MOD, "lastmod:");\r
141  \r
142 diff --git a/lib/query-fp.cc b/lib/query-fp.cc\r
143 new file mode 100644\r
144 index 0000000..ee5a306\r
145 --- /dev/null\r
146 +++ b/lib/query-fp.cc\r
147 @@ -0,0 +1,42 @@\r
148 +/* query-fp.cc -  "query:" field processor glue glue\r
149 + *\r
150 + * This file is part of notmuch.\r
151 + *\r
152 + * Copyright © 2016 David Bremner\r
153 + *\r
154 + * This program is free software: you can redistribute it and/or modify\r
155 + * it under the terms of the GNU General Public License as published by\r
156 + * the Free Software Foundation, either version 3 of the License, or\r
157 + * (at your option) any later version.\r
158 + *\r
159 + * This program is distributed in the hope that it will be useful,\r
160 + * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
161 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
162 + * GNU General Public License for more details.\r
163 + *\r
164 + * You should have received a copy of the GNU General Public License\r
165 + * along with this program.  If not, see http://www.gnu.org/licenses/ .\r
166 + *\r
167 + * Author: David Bremner <david@tethera.net>\r
168 + */\r
169 +\r
170 +#include "database-private.h"\r
171 +#include "query-fp.h"\r
172 +#include <iostream>\r
173 +\r
174 +#if HAVE_XAPIAN_FIELD_PROCESSOR\r
175 +\r
176 +Xapian::Query QueryFieldProcessor::operator()(const std::string & name) {\r
177 +    std::string key = "query." + name;\r
178 +    char *expansion;\r
179 +    notmuch_status_t status;\r
180 +\r
181 +    status = notmuch_database_get_config (notmuch, key.c_str(), &expansion);\r
182 +    if (status) {\r
183 +       throw Xapian::QueryParserError("error looking up key" + name);\r
184 +\r
185 +    }\r
186 +\r
187 +    return parser.parse_query (expansion, NOTMUCH_QUERY_PARSER_FLAGS);\r
188 +}\r
189 +#endif\r
190 diff --git a/lib/query-fp.h b/lib/query-fp.h\r
191 new file mode 100644\r
192 index 0000000..67f8705\r
193 --- /dev/null\r
194 +++ b/lib/query-fp.h\r
195 @@ -0,0 +1,42 @@\r
196 +/* query-fp.h - query field processor glue\r
197 + *\r
198 + * This file is part of notmuch.\r
199 + *\r
200 + * Copyright © 2016 David Bremner\r
201 + *\r
202 + * This program is free software: you can redistribute it and/or modify\r
203 + * it under the terms of the GNU General Public License as published by\r
204 + * the Free Software Foundation, either version 3 of the License, or\r
205 + * (at your option) any later version.\r
206 + *\r
207 + * This program is distributed in the hope that it will be useful,\r
208 + * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
209 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
210 + * GNU General Public License for more details.\r
211 + *\r
212 + * You should have received a copy of the GNU General Public License\r
213 + * along with this program.  If not, see http://www.gnu.org/licenses/ .\r
214 + *\r
215 + * Author: David Bremner <david@tethera.net>\r
216 + */\r
217 +\r
218 +#ifndef NOTMUCH_QUERY_FP_H\r
219 +#define NOTMUCH_QUERY_FP_H\r
220 +\r
221 +#include <xapian.h>\r
222 +#include "notmuch.h"\r
223 +\r
224 +#if HAVE_XAPIAN_FIELD_PROCESSOR\r
225 +class QueryFieldProcessor : public Xapian::FieldProcessor {\r
226 + protected:\r
227 +    Xapian::QueryParser &parser;\r
228 +    notmuch_database_t *notmuch;\r
229 +\r
230 + public:\r
231 +    QueryFieldProcessor (Xapian::QueryParser &parser_, notmuch_database_t *notmuch_)\r
232 +       : parser(parser_), notmuch(notmuch_) { };\r
233 +\r
234 +    Xapian::Query operator()(const std::string & str);\r
235 +};\r
236 +#endif\r
237 +#endif /* NOTMUCH_QUERY_FP_H */\r
238 diff --git a/test/T600-named-queries.sh b/test/T600-named-queries.sh\r
239 index 0922620..f0ae24f 100755\r
240 --- a/test/T600-named-queries.sh\r
241 +++ b/test/T600-named-queries.sh\r
242 @@ -50,4 +50,21 @@ notmuch restore < BEFORE\r
243  notmuch dump | grep '^#@' > OUTPUT\r
244  test_expect_equal_file QUERIES.BEFORE OUTPUT\r
245  \r
246 +if [ $NOTMUCH_HAVE_XAPIAN_FIELD_PROCESSOR -eq 1 ]; then\r
247 +    test_begin_subtest "search named query"\r
248 +    notmuch search query:test > OUTPUT\r
249 +    notmuch search $QUERYSTR > EXPECTED\r
250 +    test_expect_equal_file EXPECTED OUTPUT\r
251 +\r
252 +    test_begin_subtest "search named query with other terms"\r
253 +    notmuch search query:test and subject:Maildir > OUTPUT\r
254 +    notmuch search $QUERYSTR and subject:Maildir > EXPECTED\r
255 +    test_expect_equal_file EXPECTED OUTPUT\r
256 +\r
257 +    test_begin_subtest "search nested named query"\r
258 +    notmuch search query:test2 > OUTPUT\r
259 +    notmuch search $QUERYSTR2 > EXPECTED\r
260 +    test_expect_equal_file EXPECTED OUTPUT\r
261 +fi\r
262 +\r
263  test_done\r
264 -- \r
265 2.6.4\r
266 \r