5 Module N_QUERY
gullradriel edited this page 2026-10-09 00:55:18 +02:00

QUERY: a small boolean field-query language

A compact, field-agnostic query language for filtering records. More...

Types

Name
typedef const char ()(const char *field, void *user_data) N_QUERY_GET
Field-value getter: return the value of field for the record being evaluated (NUL-terminated, borrowed), or NULL when the field is unknown/empty.

Functions

Name
void n_query_free(N_QUERY ** query)
Free a compiled query and set the pointer to NULL.
int n_query_eval(const N_QUERY * query, N_QUERY_GET get, void * user_data)
Evaluate a compiled query against one record via get.
N_QUERY * n_query_compile_ex(const char * expr, const char *const * fields, size_t nfields, char * errbuf, size_t errlen)
Compile expr, rejecting any field not in fields.
N_QUERY * n_query_compile(const char * expr, char * errbuf, size_t errlen)
Compile an expression into a query.

Detailed Description

A compact, field-agnostic query language for filtering records.

The grammar is:

query := or or := and ( "OR" and )* and := not ( "AND" not )* not := "NOT" not | primary primary := "(" or ")" | comparison comparison := FIELD OP VALUE

FIELD is an identifier (letters, digits, '_' and '.', e.g. "host" or "req.path"). VALUE is a bareword (run of non-space, non-operator, non-paren characters) or a double-quoted string. The operators are:

= case-insensitive string equality != case-insensitive string inequality ~ case-insensitive substring contains !~ does not contain =~ regex match (PCRE via n_pcre) !~~ regex does not match

< >= <= numeric comparison (both sides parsed as numbers)

AND/OR/NOT are case-insensitive keywords. Evaluation pulls each field's value through a caller-supplied getter, so the language is independent of any record type: the caller maps field names to its own data.

Types Documentation

typedef N_QUERY_GET

typedef const char *(* N_QUERY_GET) (const char *field, void *user_data);

Field-value getter: return the value of field for the record being evaluated (NUL-terminated, borrowed), or NULL when the field is unknown/empty.

Functions Documentation

function n_query_free

void n_query_free(
    N_QUERY ** query
)

Free a compiled query and set the pointer to NULL.

function n_query_eval

int n_query_eval(
    const N_QUERY * query,
    N_QUERY_GET get,
    void * user_data
)

Evaluate a compiled query against one record via get.

Returns 1 on match, 0 otherwise. A NULL query matches everything (returns 1).

function n_query_compile_ex

N_QUERY * n_query_compile_ex(
    const char * expr,
    const char *const * fields,
    size_t nfields,
    char * errbuf,
    size_t errlen
)

Compile expr, rejecting any field not in fields.

Parameters:

  • expr The query text.
  • fields The known field names, or NULL to skip validation (which is what n_query_compile does, so existing callers are unchanged).
  • nfields Length of fields.
  • errbuf Receives the reason on failure.
  • errlen Size of errbuf.

Return: The compiled query, or NULL on a parse error or an unknown field.

n_query_compile cannot tell a misspelled field from an empty one: the getter returns NULL for both, which evaluates as the empty string, so field != value and field !~ value on a TYPO match every record and the operator is told nothing. Passing the vocabulary catches it at compile time, with the offending name in errbuf, which is the only point at which it can still be reported.

function n_query_compile

N_QUERY * n_query_compile(
    const char * expr,
    char * errbuf,
    size_t errlen
)

Compile an expression into a query.

On error returns NULL and, when errbuf is non-NULL, writes a short message into it. Free with n_query_free. An empty/whitespace expression compiles to a match-all query.