1 #include "database-private.h"
5 #include "unicode-util.h"
7 /* _sexp is used for file scope symbols to avoid clashing with
8 * definitions from sexp.h */
12 SEXP_FLAG_FIELD = 1 << 0,
13 SEXP_FLAG_BOOLEAN = 1 << 1,
14 SEXP_FLAG_SINGLE = 1 << 2,
15 SEXP_FLAG_WILDCARD = 1 << 3,
19 * define bitwise operators to hide casts */
22 operator| (_sexp_flag_t a, _sexp_flag_t b)
24 return static_cast<_sexp_flag_t>(
25 static_cast<unsigned>(a) | static_cast<unsigned>(b));
29 operator& (_sexp_flag_t a, _sexp_flag_t b)
31 return static_cast<_sexp_flag_t>(
32 static_cast<unsigned>(a) & static_cast<unsigned>(b));
37 Xapian::Query::op xapian_op;
38 Xapian::Query initial;
42 static _sexp_prefix_t prefixes[] =
44 { "and", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
46 { "attachment", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
47 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD },
48 { "body", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
50 { "from", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
51 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD },
52 { "folder", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
53 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD },
54 { "id", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
55 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD },
56 { "is", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
57 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD },
58 { "mid", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
59 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD },
60 { "mimetype", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
61 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD },
62 { "not", Xapian::Query::OP_AND_NOT, Xapian::Query::MatchAll,
64 { "or", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
66 { "path", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
67 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD },
68 { "property", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
69 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD },
70 { "starts-with", Xapian::Query::OP_WILDCARD, Xapian::Query::MatchAll,
72 { "subject", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
73 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD },
74 { "tag", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
75 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD },
76 { "thread", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
77 SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD },
78 { "to", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
79 SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD },
83 static notmuch_status_t _sexp_to_xapian_query (notmuch_database_t *notmuch,
84 const _sexp_prefix_t *parent,
86 Xapian::Query &output);
88 static notmuch_status_t
89 _sexp_combine_query (notmuch_database_t *notmuch,
90 const _sexp_prefix_t *parent,
91 Xapian::Query::op operation,
94 Xapian::Query &output)
96 Xapian::Query subquery;
98 notmuch_status_t status;
100 /* if we run out elements, return accumulator */
104 return NOTMUCH_STATUS_SUCCESS;
107 status = _sexp_to_xapian_query (notmuch, parent, sx, subquery);
111 return _sexp_combine_query (notmuch,
114 Xapian::Query (operation, left, subquery),
118 static notmuch_status_t
119 _sexp_parse_phrase (std::string term_prefix, const char *phrase, Xapian::Query &output)
121 Xapian::Utf8Iterator p (phrase);
122 Xapian::Utf8Iterator end;
123 std::vector<std::string> terms;
126 Xapian::Utf8Iterator start;
127 while (p != end && ! Xapian::Unicode::is_wordchar (*p))
135 while (p != end && Xapian::Unicode::is_wordchar (*p))
139 std::string word (start, p);
140 word = Xapian::Unicode::tolower (word);
141 terms.push_back (term_prefix + word);
144 output = Xapian::Query (Xapian::Query::OP_PHRASE, terms.begin (), terms.end ());
145 return NOTMUCH_STATUS_SUCCESS;
148 static notmuch_status_t
149 _sexp_parse_wildcard (notmuch_database_t *notmuch,
150 const _sexp_prefix_t *parent,
152 Xapian::Query &output)
155 std::string term_prefix = parent ? _find_prefix (parent->name) : "";
157 if (parent && ! (parent->flags & SEXP_FLAG_WILDCARD)) {
158 _notmuch_database_log (notmuch, "'%s' does not support wildcard queries\n", parent->name);
159 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
162 output = Xapian::Query (Xapian::Query::OP_WILDCARD,
163 term_prefix + Xapian::Unicode::tolower (match));
164 return NOTMUCH_STATUS_SUCCESS;
167 static notmuch_status_t
168 _sexp_parse_one_term (notmuch_database_t *notmuch, std::string term_prefix, const sexp_t *sx,
169 Xapian::Query &output)
171 Xapian::Stem stem = *(notmuch->stemmer);
173 if (sx->aty == SEXP_BASIC && unicode_word_utf8 (sx->val)) {
174 std::string term = Xapian::Unicode::tolower (sx->val);
176 output = Xapian::Query ("Z" + term_prefix + stem (term));
177 return NOTMUCH_STATUS_SUCCESS;
179 return _sexp_parse_phrase (term_prefix, sx->val, output);
183 /* Here we expect the s-expression to be a proper list, with first
184 * element defining and operation, or as a special case the empty
187 static notmuch_status_t
188 _sexp_to_xapian_query (notmuch_database_t *notmuch, const _sexp_prefix_t *parent, const sexp_t *sx,
189 Xapian::Query &output)
191 if (sx->ty == SEXP_VALUE) {
192 std::string term = Xapian::Unicode::tolower (sx->val);
193 Xapian::Stem stem = *(notmuch->stemmer);
194 std::string term_prefix = parent ? _find_prefix (parent->name) : "";
196 if (sx->aty == SEXP_BASIC && strcmp (sx->val, "*") == 0) {
197 return _sexp_parse_wildcard (notmuch, parent, "", output);
200 if (parent && (parent->flags & SEXP_FLAG_BOOLEAN)) {
201 output = Xapian::Query (term_prefix + sx->val);
202 return NOTMUCH_STATUS_SUCCESS;
205 return _sexp_parse_one_term (notmuch, term_prefix, sx, output);
207 Xapian::Query accumulator;
208 for (_sexp_prefix_t *prefix = prefixes; prefix->name; prefix++) {
209 if (prefix->flags & SEXP_FLAG_FIELD) {
210 notmuch_status_t status;
211 Xapian::Query subquery;
212 term_prefix = _find_prefix (prefix->name);
213 status = _sexp_parse_one_term (notmuch, term_prefix, sx, subquery);
216 accumulator = Xapian::Query (Xapian::Query::OP_OR, accumulator, subquery);
219 output = accumulator;
220 return NOTMUCH_STATUS_SUCCESS;
226 output = Xapian::Query::MatchAll;
227 return NOTMUCH_STATUS_SUCCESS;
230 if (sx->list->ty == SEXP_LIST) {
231 _notmuch_database_log (notmuch, "unexpected list in field/operation position\n",
233 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
236 for (_sexp_prefix_t *prefix = prefixes; prefix && prefix->name; prefix++) {
237 if (strcmp (prefix->name, sx->list->val) == 0) {
238 if (prefix->flags & SEXP_FLAG_FIELD) {
240 _notmuch_database_log (notmuch, "nested field: '%s' inside '%s'\n",
241 prefix->name, parent->name);
242 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
247 if ((prefix->flags & SEXP_FLAG_SINGLE) &&
248 (! sx->list->next || sx->list->next->next || sx->list->next->ty != SEXP_VALUE)) {
249 _notmuch_database_log (notmuch, "'%s' expects single atom as argument\n",
251 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
254 if (prefix->xapian_op == Xapian::Query::OP_WILDCARD)
255 return _sexp_parse_wildcard (notmuch, parent, sx->list->next->val, output);
257 return _sexp_combine_query (notmuch, parent, prefix->xapian_op, prefix->initial,
258 sx->list->next, output);
262 _notmuch_database_log (notmuch, "unknown prefix '%s'\n", sx->list->val);
264 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
268 _notmuch_sexp_string_to_xapian_query (notmuch_database_t *notmuch, const char *querystr,
269 Xapian::Query &output)
271 const sexp_t *sx = NULL;
272 char *buf = talloc_strdup (notmuch, querystr);
274 sx = parse_sexp (buf, strlen (querystr));
276 _notmuch_database_log (notmuch, "invalid s-expression: '%s'\n", querystr);
277 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
280 return _sexp_to_xapian_query (notmuch, NULL, sx, output);