1 #include "database-private.h"
7 /* _sexp is used for file scope symbols to avoid clashing with
8 * definitions from sexp.h */
12 SEXP_FLAG_FIELD = 1 << 0,
17 Xapian::Query::op xapian_op;
18 Xapian::Query initial;
22 static _sexp_prefix_t prefixes[] =
24 { "and", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
26 { "not", Xapian::Query::OP_AND_NOT, Xapian::Query::MatchAll,
28 { "or", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
30 { "subject", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
35 static notmuch_status_t _sexp_to_xapian_query (notmuch_database_t *notmuch,
36 const _sexp_prefix_t *parent,
38 Xapian::Query &output);
40 static notmuch_status_t
41 _sexp_combine_query (notmuch_database_t *notmuch,
42 const _sexp_prefix_t *parent,
43 Xapian::Query::op operation,
46 Xapian::Query &output)
48 Xapian::Query subquery;
50 notmuch_status_t status;
52 /* if we run out elements, return accumulator */
56 return NOTMUCH_STATUS_SUCCESS;
59 status = _sexp_to_xapian_query (notmuch, parent, sx, subquery);
63 return _sexp_combine_query (notmuch,
66 Xapian::Query (operation, left, subquery),
70 /* Here we expect the s-expression to be a proper list, with first
71 * element defining and operation, or as a special case the empty
74 static notmuch_status_t
75 _sexp_to_xapian_query (notmuch_database_t *notmuch, const _sexp_prefix_t *parent, const sexp_t *sx,
76 Xapian::Query &output)
79 if (sx->ty == SEXP_VALUE) {
80 std::string term = Xapian::Unicode::tolower (sx->val);
81 Xapian::Stem stem = *(notmuch->stemmer);
82 std::string term_prefix = parent ? _find_prefix (parent->name) : "";
83 if (sx->aty == SEXP_BASIC)
84 term = "Z" + term_prefix + stem (term);
86 term = term_prefix + term;
88 output = Xapian::Query (term);
89 return NOTMUCH_STATUS_SUCCESS;
94 output = Xapian::Query::MatchAll;
95 return NOTMUCH_STATUS_SUCCESS;
98 if (sx->list->ty == SEXP_LIST) {
99 _notmuch_database_log (notmuch, "unexpected list in field/operation position\n",
101 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
104 for (_sexp_prefix_t *prefix = prefixes; prefix && prefix->name; prefix++) {
105 if (strcmp (prefix->name, sx->list->val) == 0) {
106 if (prefix->flags & SEXP_FLAG_FIELD) {
108 _notmuch_database_log (notmuch, "nested field: '%s' inside '%s'\n",
109 prefix->name, parent->name);
110 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
115 return _sexp_combine_query (notmuch, parent, prefix->xapian_op, prefix->initial,
116 sx->list->next, output);
120 _notmuch_database_log (notmuch, "unknown prefix '%s'\n", sx->list->val);
122 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
126 _notmuch_sexp_string_to_xapian_query (notmuch_database_t *notmuch, const char *querystr,
127 Xapian::Query &output)
129 const sexp_t *sx = NULL;
130 char *buf = talloc_strdup (notmuch, querystr);
132 sx = parse_sexp (buf, strlen (querystr));
134 _notmuch_database_log (notmuch, "invalid s-expression: '%s'\n", querystr);
135 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
138 return _sexp_to_xapian_query (notmuch, NULL, sx, output);