1 #include "database-private.h"
7 /* _sexp is used for file scope symbols to avoid clashing with
8 * definitions from sexp.h */
16 Xapian::Query::op xapian_op;
17 Xapian::Query initial;
21 static _sexp_prefix_t prefixes[] =
23 { "and", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
25 { "not", Xapian::Query::OP_AND_NOT, Xapian::Query::MatchAll,
27 { "or", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
32 static notmuch_status_t _sexp_to_xapian_query (notmuch_database_t *notmuch,
33 const _sexp_prefix_t *parent,
35 Xapian::Query &output);
37 static notmuch_status_t
38 _sexp_combine_query (notmuch_database_t *notmuch,
39 const _sexp_prefix_t *parent,
40 Xapian::Query::op operation,
43 Xapian::Query &output)
45 Xapian::Query subquery;
47 notmuch_status_t status;
49 /* if we run out elements, return accumulator */
53 return NOTMUCH_STATUS_SUCCESS;
56 status = _sexp_to_xapian_query (notmuch, parent, sx, subquery);
60 return _sexp_combine_query (notmuch,
63 Xapian::Query (operation, left, subquery),
67 /* Here we expect the s-expression to be a proper list, with first
68 * element defining and operation, or as a special case the empty
71 static notmuch_status_t
72 _sexp_to_xapian_query (notmuch_database_t *notmuch, const _sexp_prefix_t *parent, const sexp_t *sx,
73 Xapian::Query &output)
76 if (sx->ty == SEXP_VALUE) {
77 std::string term = Xapian::Unicode::tolower (sx->val);
78 Xapian::Stem stem = *(notmuch->stemmer);
79 if (sx->aty == SEXP_BASIC)
80 term = "Z" + stem (term);
82 output = Xapian::Query (term);
83 return NOTMUCH_STATUS_SUCCESS;
88 output = Xapian::Query::MatchAll;
89 return NOTMUCH_STATUS_SUCCESS;
92 if (sx->list->ty == SEXP_LIST) {
93 _notmuch_database_log (notmuch, "unexpected list in field/operation position\n",
95 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
98 for (_sexp_prefix_t *prefix = prefixes; prefix && prefix->name; prefix++) {
99 if (strcmp (prefix->name, sx->list->val) == 0) {
100 return _sexp_combine_query (notmuch, parent, prefix->xapian_op, prefix->initial,
101 sx->list->next, output);
105 _notmuch_database_log (notmuch, "unknown prefix '%s'\n", sx->list->val);
107 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
111 _notmuch_sexp_string_to_xapian_query (notmuch_database_t *notmuch, const char *querystr,
112 Xapian::Query &output)
114 const sexp_t *sx = NULL;
115 char *buf = talloc_strdup (notmuch, querystr);
117 sx = parse_sexp (buf, strlen (querystr));
119 _notmuch_database_log (notmuch, "invalid s-expression: '%s'\n", querystr);
120 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
123 return _sexp_to_xapian_query (notmuch, NULL, sx, output);