X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=lib%2Fquery.cc;h=435f72296dc72ee495cdc20567e73ca121f10ddd;hb=be7e83de96b706af418fc9f139ded4d50bf342f6;hp=792aba219fa48d53e3b761089eac1d4349b5c0fb;hpb=74a1b5ac65b31f7ebc1258b259b8c355023e21b4;p=notmuch diff --git a/lib/query.cc b/lib/query.cc index 792aba21..435f7229 100644 --- a/lib/query.cc +++ b/lib/query.cc @@ -30,6 +30,7 @@ struct _notmuch_query { notmuch_string_list_t *exclude_terms; notmuch_exclude_t omit_excluded; bool parsed; + notmuch_query_syntax_t syntax; Xapian::Query xapian_query; std::set terms; }; @@ -84,9 +85,9 @@ _notmuch_query_destructor (notmuch_query_t *query) return 0; } -notmuch_query_t * -notmuch_query_create (notmuch_database_t *notmuch, - const char *query_string) +static notmuch_query_t * +_notmuch_query_constructor (notmuch_database_t *notmuch, + const char *query_string) { notmuch_query_t *query; @@ -105,7 +106,10 @@ notmuch_query_create (notmuch_database_t *notmuch, query->notmuch = notmuch; - query->query_string = talloc_strdup (query, query_string); + if (query_string) + query->query_string = talloc_strdup (query, query_string); + else + query->query_string = NULL; query->sort = NOTMUCH_SORT_NEWEST_FIRST; @@ -116,12 +120,54 @@ notmuch_query_create (notmuch_database_t *notmuch, return query; } -static notmuch_status_t -_notmuch_query_ensure_parsed (notmuch_query_t *query) +notmuch_query_t * +notmuch_query_create (notmuch_database_t *notmuch, + const char *query_string) { - if (query->parsed) - return NOTMUCH_STATUS_SUCCESS; + notmuch_query_t *query; + notmuch_status_t status; + + status = notmuch_query_create_with_syntax (notmuch, query_string, + NOTMUCH_QUERY_SYNTAX_XAPIAN, + &query); + if (status) + return NULL; + + return query; +} + +notmuch_status_t +notmuch_query_create_with_syntax (notmuch_database_t *notmuch, + const char *query_string, + notmuch_query_syntax_t syntax, + notmuch_query_t **output) +{ + + notmuch_query_t *query; + + if (! output) + return NOTMUCH_STATUS_NULL_POINTER; + + query = _notmuch_query_constructor (notmuch, query_string); + if (! query) + return NOTMUCH_STATUS_OUT_OF_MEMORY; + + if (syntax == NOTMUCH_QUERY_SYNTAX_SEXP && ! HAVE_SFSEXP) { + _notmuch_database_log (notmuch, "sexp query parser not available"); + return NOTMUCH_STATUS_ILLEGAL_ARGUMENT; + } + + query->syntax = syntax; + + *output = query; + + return NOTMUCH_STATUS_SUCCESS; +} + +static notmuch_status_t +_notmuch_query_ensure_parsed_xapian (notmuch_query_t *query) +{ try { query->xapian_query = query->notmuch->query_parser-> @@ -154,6 +200,30 @@ _notmuch_query_ensure_parsed (notmuch_query_t *query) return NOTMUCH_STATUS_SUCCESS; } +static notmuch_status_t +_notmuch_query_ensure_parsed_sexpr (notmuch_query_t *query) +{ + if (query->parsed) + return NOTMUCH_STATUS_SUCCESS; + + return _notmuch_sexp_string_to_xapian_query (query->notmuch, query->query_string, + query->xapian_query); +} + +static notmuch_status_t +_notmuch_query_ensure_parsed (notmuch_query_t *query) +{ + if (query->parsed) + return NOTMUCH_STATUS_SUCCESS; + +#if HAVE_SFSEXP + if (query->syntax == NOTMUCH_QUERY_SYNTAX_SEXP) + return _notmuch_query_ensure_parsed_sexpr (query); +#endif + + return _notmuch_query_ensure_parsed_xapian (query); +} + const char * notmuch_query_get_query_string (const notmuch_query_t *query) {