mirror of
https://git.notmuchmail.org/git/notmuch
synced 2024-11-22 02:48:08 +01:00
lib: define notmuch_query_create_with_syntax
Set the parsing syntax when the (notmuch) query object is created. Initially the library always returns a trivial query that matches all messages when using s-expression syntax. It seems better to select the syntax at query creation time because the lazy parsing is an implementation detail.
This commit is contained in:
parent
34733fa25e
commit
c4f2f33a50
2 changed files with 78 additions and 7 deletions
|
@ -961,6 +961,16 @@ notmuch_query_t *
|
||||||
notmuch_query_create (notmuch_database_t *database,
|
notmuch_query_create (notmuch_database_t *database,
|
||||||
const char *query_string);
|
const char *query_string);
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
NOTMUCH_QUERY_SYNTAX_XAPIAN,
|
||||||
|
NOTMUCH_QUERY_SYNTAX_SEXP
|
||||||
|
} notmuch_query_syntax_t;
|
||||||
|
|
||||||
|
notmuch_status_t
|
||||||
|
notmuch_query_create_with_syntax (notmuch_database_t *database,
|
||||||
|
const char *query_string,
|
||||||
|
notmuch_query_syntax_t syntax,
|
||||||
|
notmuch_query_t **output);
|
||||||
/**
|
/**
|
||||||
* Sort values for notmuch_query_set_sort.
|
* Sort values for notmuch_query_set_sort.
|
||||||
*/
|
*/
|
||||||
|
|
73
lib/query.cc
73
lib/query.cc
|
@ -23,6 +23,10 @@
|
||||||
|
|
||||||
#include <glib.h> /* GHashTable, GPtrArray */
|
#include <glib.h> /* GHashTable, GPtrArray */
|
||||||
|
|
||||||
|
#if HAVE_SFSEXP
|
||||||
|
#include "sexp.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
struct _notmuch_query {
|
struct _notmuch_query {
|
||||||
notmuch_database_t *notmuch;
|
notmuch_database_t *notmuch;
|
||||||
const char *query_string;
|
const char *query_string;
|
||||||
|
@ -30,6 +34,7 @@ struct _notmuch_query {
|
||||||
notmuch_string_list_t *exclude_terms;
|
notmuch_string_list_t *exclude_terms;
|
||||||
notmuch_exclude_t omit_excluded;
|
notmuch_exclude_t omit_excluded;
|
||||||
bool parsed;
|
bool parsed;
|
||||||
|
notmuch_query_syntax_t syntax;
|
||||||
Xapian::Query xapian_query;
|
Xapian::Query xapian_query;
|
||||||
std::set<std::string> terms;
|
std::set<std::string> terms;
|
||||||
};
|
};
|
||||||
|
@ -105,7 +110,10 @@ _notmuch_query_constructor (notmuch_database_t *notmuch,
|
||||||
|
|
||||||
query->notmuch = notmuch;
|
query->notmuch = notmuch;
|
||||||
|
|
||||||
|
if (query_string)
|
||||||
query->query_string = talloc_strdup (query, query_string);
|
query->query_string = talloc_strdup (query, query_string);
|
||||||
|
else
|
||||||
|
query->query_string = NULL;
|
||||||
|
|
||||||
query->sort = NOTMUCH_SORT_NEWEST_FIRST;
|
query->sort = NOTMUCH_SORT_NEWEST_FIRST;
|
||||||
|
|
||||||
|
@ -121,20 +129,49 @@ notmuch_query_create (notmuch_database_t *notmuch,
|
||||||
const char *query_string)
|
const char *query_string)
|
||||||
{
|
{
|
||||||
|
|
||||||
notmuch_query_t *query = _notmuch_query_constructor (notmuch, query_string);
|
notmuch_query_t *query;
|
||||||
|
notmuch_status_t status;
|
||||||
|
|
||||||
if (! query)
|
status = notmuch_query_create_with_syntax (notmuch, query_string,
|
||||||
|
NOTMUCH_QUERY_SYNTAX_XAPIAN,
|
||||||
|
&query);
|
||||||
|
if (status)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
return query;
|
return query;
|
||||||
}
|
}
|
||||||
|
|
||||||
static notmuch_status_t
|
notmuch_status_t
|
||||||
_notmuch_query_ensure_parsed (notmuch_query_t *query)
|
notmuch_query_create_with_syntax (notmuch_database_t *notmuch,
|
||||||
|
const char *query_string,
|
||||||
|
notmuch_query_syntax_t syntax,
|
||||||
|
notmuch_query_t **output)
|
||||||
{
|
{
|
||||||
if (query->parsed)
|
|
||||||
return NOTMUCH_STATUS_SUCCESS;
|
|
||||||
|
|
||||||
|
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 {
|
try {
|
||||||
query->xapian_query =
|
query->xapian_query =
|
||||||
query->notmuch->query_parser->
|
query->notmuch->query_parser->
|
||||||
|
@ -167,6 +204,30 @@ _notmuch_query_ensure_parsed (notmuch_query_t *query)
|
||||||
return NOTMUCH_STATUS_SUCCESS;
|
return NOTMUCH_STATUS_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static notmuch_status_t
|
||||||
|
_notmuch_query_ensure_parsed_sexpr (notmuch_query_t *query)
|
||||||
|
{
|
||||||
|
if (query->parsed)
|
||||||
|
return NOTMUCH_STATUS_SUCCESS;
|
||||||
|
|
||||||
|
query->xapian_query = Xapian::Query::MatchAll;
|
||||||
|
return NOTMUCH_STATUS_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 *
|
const char *
|
||||||
notmuch_query_get_query_string (const notmuch_query_t *query)
|
notmuch_query_get_query_string (const notmuch_query_t *query)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue