GnuCash  4.13-177-g21dd8aa057+
Files | Data Structures | Macros | Typedefs | Enumerations
Query: Querying for Objects

BASIC QUERY API: With this API you can create arbitrary logical queries to find sets of arbitrary object. More...

Files

file  qofquery.h
 find objects that match a certain expression.
 
file  qofquerycore.h
 API for providing core Query data types.
 

Data Structures

struct  QofQueryPredData
 No extended comparisons for QOF_TYPE_INT32, QOF_TYPE_INT64, QOF_TYPE_DOUBLE, QOF_TYPE_BOOLEAN. More...
 

Macros

#define QOF_MOD_QUERY   "qof.query"
 
#define QOF_QUERY_FIRST_TERM   QOF_QUERY_AND
 First/only term is same as 'and'.
 
#define QUERY_DEFAULT_SORT   "QofQueryDefaultSort"
 Default sort object type.
 
#define QOF_PARAM_BOOK   "book"
 "Known" Object Parameters – all objects must support these
 
#define QOF_PARAM_GUID   "guid"
 
#define QOF_PARAM_KVP   "kvp"
 "Known" Object Parameters – some objects might support these
 
#define QOF_PARAM_ACTIVE   "active"
 
#define QOF_PARAM_VERSION   "version"
 

Typedefs

typedef GSList QofQueryParamList
 A list of parameters (QofIdType) used to describe a parameter to use in a predicate or when sorting.
 

Enumerations

enum  QofQueryOp {
  QOF_QUERY_AND = 1, QOF_QUERY_OR, QOF_QUERY_NAND, QOF_QUERY_NOR,
  QOF_QUERY_XOR
}
 Query Term Operators, for combining Query Terms.
 
enum  QofQueryCompare {
  QOF_COMPARE_LT = 1, QOF_COMPARE_LTE, QOF_COMPARE_EQUAL, QOF_COMPARE_GT,
  QOF_COMPARE_GTE, QOF_COMPARE_NEQ, QOF_COMPARE_CONTAINS, QOF_COMPARE_NCONTAINS
}
 Standard Query comparators, for how to compare objects in a predicate. More...
 
enum  QofStringMatch { QOF_STRING_MATCH_NORMAL = 1, QOF_STRING_MATCH_CASEINSENSITIVE }
 List of known core query data-types... More...
 
enum  QofDateMatch { QOF_DATE_MATCH_NORMAL = 1, QOF_DATE_MATCH_DAY }
 Comparisons for QOF_TYPE_DATE The QOF_DATE_MATCH_DAY comparison rounds the two time values to mid-day and then compares these rounded values. More...
 
enum  QofNumericMatch { QOF_NUMERIC_MATCH_DEBIT = 1, QOF_NUMERIC_MATCH_CREDIT, QOF_NUMERIC_MATCH_ANY }
 Comparisons for QOF_TYPE_NUMERIC, QOF_TYPE_DEBCRED. More...
 
enum  QofGuidMatch {
  QOF_GUID_MATCH_ANY = 1, QOF_GUID_MATCH_NONE, QOF_GUID_MATCH_NULL, QOF_GUID_MATCH_ALL,
  QOF_GUID_MATCH_LIST_ANY
}
 
enum  QofCharMatch { QOF_CHAR_MATCH_ANY = 1, QOF_CHAR_MATCH_NONE }
 A CHAR type is for a RECNCell, Comparisons for QOF_TYPE_CHAR 'ANY' will match any character in the string. More...
 

Query Subsystem Initialization and Shutdown

void qof_query_init (void)
 Subsystem initialization and shutdown. More...
 
void qof_query_shutdown (void)
 

Low-Level API Functions

QofQueryParamListqof_query_build_param_list (char const *param,...)
 
QofQuery * qof_query_create (void)
 Create a new query. More...
 
QofQuery * qof_query_create_for (QofIdTypeConst obj_type)
 
void qof_query_destroy (QofQuery *q)
 Frees the resources associate with a Query object. More...
 
void qof_query_search_for (QofQuery *query, QofIdTypeConst obj_type)
 Set the object type to be searched for. More...
 
void qof_query_set_book (QofQuery *q, QofBook *book)
 Set the book to be searched. More...
 
void qof_query_add_term (QofQuery *query, QofQueryParamList *param_list, QofQueryPredData *pred_data, QofQueryOp op)
 This is the general function that adds a new Query Term to a query. More...
 
void qof_query_add_guid_match (QofQuery *q, QofQueryParamList *param_list, const GncGUID *guid, QofQueryOp op)
 DOCUMENT ME !!
 
void qof_query_add_guid_list_match (QofQuery *q, QofQueryParamList *param_list, GList *guid_list, QofGuidMatch options, QofQueryOp op)
 DOCUMENT ME !!
 
void qof_query_add_boolean_match (QofQuery *q, QofQueryParamList *param_list, gboolean value, QofQueryOp op)
 Handy-dandy convenience routines, avoids having to create a separate predicate for boolean matches. More...
 
GList * qof_query_run (QofQuery *query)
 Perform the query, return the results. More...
 
GList * qof_query_last_run (QofQuery *query)
 Return the results of the last query, without causing the query to be re-run. More...
 
GList * qof_query_run_subquery (QofQuery *subquery, const QofQuery *primary_query)
 Perform a subquery, return the results. More...
 
void qof_query_clear (QofQuery *query)
 Remove all query terms from query. More...
 
void qof_query_purge_terms (QofQuery *q, QofQueryParamList *param_list)
 Remove query terms of a particular type from q. More...
 
int qof_query_has_terms (QofQuery *q)
 Return boolean FALSE if there are no terms in the query Can be used as a predicate to see if the query has been initialized (return value > 0) or is "blank" (return value == 0).
 
int qof_query_num_terms (QofQuery *q)
 Return the number of terms in the canonical form of the query.
 
gboolean qof_query_has_term_type (QofQuery *q, QofQueryParamList *term_param)
 DOCUMENT ME !!
 
GSList * qof_query_get_term_type (QofQuery *q, QofQueryParamList *term_param)
 
QofQuery * qof_query_copy (QofQuery *q)
 Make a copy of the indicated query.
 
QofQuery * qof_query_invert (QofQuery *q)
 Make a copy of the indicated query, inverting the sense of the search. More...
 
QofQuery * qof_query_merge (QofQuery *q1, QofQuery *q2, QofQueryOp op)
 Combine two queries together using the Boolean set (logical) operator 'op'. More...
 
void qof_query_merge_in_place (QofQuery *q1, QofQuery *q2, QofQueryOp op)
 Like qof_query_merge, but this will merge a copy of q2 into q1. More...
 
void qof_query_set_sort_order (QofQuery *q, QofQueryParamList *primary_sort_params, QofQueryParamList *secondary_sort_params, QofQueryParamList *tertiary_sort_params)
 When a query is run, the results are sorted before being returned. More...
 
void qof_query_set_sort_options (QofQuery *q, gint prim_op, gint sec_op, gint tert_op)
 
void qof_query_set_sort_increasing (QofQuery *q, gboolean prim_inc, gboolean sec_inc, gboolean tert_inc)
 When a query is run, the results are sorted before being returned. More...
 
void qof_query_set_max_results (QofQuery *q, int n)
 Set the maximum number of results that should be returned. More...
 
gboolean qof_query_equal (const QofQuery *q1, const QofQuery *q2)
 Compare two queries for equality. More...
 
void qof_query_print (QofQuery *query)
 Log the Query. More...
 
QofIdType qof_query_get_search_for (const QofQuery *q)
 Return the type of data we're querying for.
 
GList * qof_query_get_books (QofQuery *q)
 Return the list of books we're using.
 

Core Data Type Predicates

QofQueryPredData * qof_query_string_predicate (QofQueryCompare how, const gchar *str, QofStringMatch options, gboolean is_regex)
 
QofQueryPredData * qof_query_date_predicate (QofQueryCompare how, QofDateMatch options, time64 date)
 
QofQueryPredData * qof_query_numeric_predicate (QofQueryCompare how, QofNumericMatch options, gnc_numeric value)
 
QofQueryPredData * qof_query_guid_predicate (QofGuidMatch options, GList *guids)
 
QofQueryPredData * qof_query_int32_predicate (QofQueryCompare how, gint32 val)
 
QofQueryPredData * qof_query_int64_predicate (QofQueryCompare how, gint64 val)
 
QofQueryPredData * qof_query_double_predicate (QofQueryCompare how, double val)
 
QofQueryPredData * qof_query_boolean_predicate (QofQueryCompare how, gboolean val)
 
QofQueryPredData * qof_query_char_predicate (QofCharMatch options, const gchar *chars)
 
QofQueryPredData * qof_query_collect_predicate (QofGuidMatch options, QofCollection *coll)
 
QofQueryPredData * qof_query_choice_predicate (QofGuidMatch options, GList *guids)
 
QofQueryPredData * qof_query_core_predicate_copy (const QofQueryPredData *pdata)
 Copy a predicate. More...
 
void qof_query_core_predicate_free (QofQueryPredData *pdata)
 Destroy a predicate. More...
 
gboolean qof_query_date_predicate_get_date (const QofQueryPredData *pd, time64 *date)
 Retrieve a predicate. More...
 
char * qof_query_core_to_string (QofType, gpointer object, QofParam *getter)
 Return a printable string for a core data object. More...
 
int qof_string_number_compare_func (gpointer a, gpointer b, gint options, QofParam *this_param)
 Compare two parameter(strings) as if they are numbers! the two objects, a and b, are the objects being compared this_param is the QofParam for this parameter in the objects.
 

Detailed Description

BASIC QUERY API: With this API you can create arbitrary logical queries to find sets of arbitrary object.

To make simple queries (1 term, such as a search for a parameter with one value), create the appropriate QueryTerm structure and stick it in a Query object using xaccInitQuery. The QueryTerm should be malloc'd but the Query object will handle freeing it. To make compound queries, make multiple simple queries and combine them using qof_query_merge() and the logical operations of your choice.

SQL QUERY API: As an alternative to building queries one predicate at a time, you can use the SQL query interface. This interface will accept a string containing an SQL query, parse it, convert it into the core representation, and execute it.

STRUCTURE OF A QUERY: A Query is a logical function of any number of QueryTerms. A QueryTerm consists of a C function pointer (the Predicate) and a PredicateData structure containing data passed to the predicate function. The PredicateData structure is a constant associated with the Term and is identical for every object that is tested.

The terms of the Query may represent any logical function and are stored in canonical form, i.e. the function is expressed as a logical sum of logical products. So if you have QueryTerms a, b, c, d, e and you have the logical function a(b+c) + !(c(d+e)), it gets stored as ab + ac + !c + !c!e +!d!c + !d!e. This may not be optimal for evaluation of some functions but it's easy to store, easy to manipulate, and it doesn't require a complete algebra system to deal with.

The representation is of a GList of GLists of QueryTerms. The "backbone" GList q->terms represents the OR-chain, and every item on the backbone is a GList of QueryTerms representing an AND-chain corresponding to a single product-term in the canonical representation. QueryTerms are duplicated when necessary to fill out the canonical form, and the same predicate may be evaluated multiple times per split for complex queries. This is a place where we could probably optimize.

Enumeration Type Documentation

◆ QofCharMatch

A CHAR type is for a RECNCell, Comparisons for QOF_TYPE_CHAR 'ANY' will match any character in the string.

Match 'ANY' is a convenience/performance-enhanced predicate for the compound statement (value==char1) || (value==char2) || etc. Match 'NONE' is equivalent to (value != char1) && (value != char2) && etc.

Definition at line 132 of file qofquerycore.h.

133 {
134  QOF_CHAR_MATCH_ANY = 1,
135  QOF_CHAR_MATCH_NONE
136 } QofCharMatch;
QofCharMatch
A CHAR type is for a RECNCell, Comparisons for QOF_TYPE_CHAR 'ANY' will match any character in the st...
Definition: qofquerycore.h:132

◆ QofDateMatch

Comparisons for QOF_TYPE_DATE The QOF_DATE_MATCH_DAY comparison rounds the two time values to mid-day and then compares these rounded values.

The QOF_DATE_MATCH_NORMAL comparison matches the time values, down to the second.

Definition at line 83 of file qofquerycore.h.

84 {
85  QOF_DATE_MATCH_NORMAL = 1,
86  QOF_DATE_MATCH_DAY
87 } QofDateMatch;
QofDateMatch
Comparisons for QOF_TYPE_DATE The QOF_DATE_MATCH_DAY comparison rounds the two time values to mid-day...
Definition: qofquerycore.h:83

◆ QofGuidMatch

Enumerator
QOF_GUID_MATCH_ANY 

These expect a single object and expect the QofAccessFunc returns GncGUID*.

QOF_GUID_MATCH_ALL 

These expect a GList* of objects and calls the QofAccessFunc routine on each item in the list to obtain a GncGUID* for each object.

QOF_GUID_MATCH_LIST_ANY 

These expect a single object and expect the QofAccessFunc function to return a GList* of GncGUID* (the list is the property of the caller)

Definition at line 109 of file qofquerycore.h.

110 {
113  QOF_GUID_MATCH_ANY = 1,
114  QOF_GUID_MATCH_NONE,
115  QOF_GUID_MATCH_NULL,
122 } QofGuidMatch;
These expect a single object and expect the QofAccessFunc returns GncGUID*.
Definition: qofquerycore.h:113
QofGuidMatch
Definition: qofquerycore.h:109
These expect a single object and expect the QofAccessFunc function to return a GList* of GncGUID* (th...
Definition: qofquerycore.h:121
These expect a GList* of objects and calls the QofAccessFunc routine on each item in the list to obta...
Definition: qofquerycore.h:118

◆ QofNumericMatch

Comparisons for QOF_TYPE_NUMERIC, QOF_TYPE_DEBCRED.

XXX Should be deprecated, or at least wrapped up as a convenience function, this is based on the old bill gribble code, which assumed the amount was always positive, and then specified a funds-flow direction (credit, debit, or either).

The point being that 'match credit' is equivalent to the compound predicate (amount >= 0) && (amount 'op' value) while the 'match debit' predicate is equivalent to (amount <= 0) && (abs(amount) 'op' value)

Definition at line 101 of file qofquerycore.h.

102 {
103  QOF_NUMERIC_MATCH_DEBIT = 1,
104  QOF_NUMERIC_MATCH_CREDIT,
105  QOF_NUMERIC_MATCH_ANY
QofNumericMatch
Comparisons for QOF_TYPE_NUMERIC, QOF_TYPE_DEBCRED.
Definition: qofquerycore.h:101

◆ QofQueryCompare

Standard Query comparators, for how to compare objects in a predicate.

Note that not all core types implement all comparators

Definition at line 54 of file qofquerycore.h.

55 {
56  QOF_COMPARE_LT = 1,
57  QOF_COMPARE_LTE,
58  QOF_COMPARE_EQUAL,
59  QOF_COMPARE_GT,
60  QOF_COMPARE_GTE,
61  QOF_COMPARE_NEQ,
62  QOF_COMPARE_CONTAINS,
63  QOF_COMPARE_NCONTAINS
QofQueryCompare
Standard Query comparators, for how to compare objects in a predicate.
Definition: qofquerycore.h:54

◆ QofStringMatch

List of known core query data-types...

Each core query type defines it's set of optional "comparator qualifiers".

Definition at line 70 of file qofquerycore.h.

71 {
72  QOF_STRING_MATCH_NORMAL = 1,
73  QOF_STRING_MATCH_CASEINSENSITIVE
QofStringMatch
List of known core query data-types...
Definition: qofquerycore.h:70

Function Documentation

◆ qof_query_add_boolean_match()

void qof_query_add_boolean_match ( QofQuery *  q,
QofQueryParamList param_list,
gboolean  value,
QofQueryOp  op 
)

Handy-dandy convenience routines, avoids having to create a separate predicate for boolean matches.

We might want to create handy-dandy sugar routines for the other predicate types as well.

Definition at line 1330 of file qofquery.cpp.

1332 {
1333  QofQueryPredData *pdata;
1334  if (!q || !param_list) return;
1335 
1336  pdata = qof_query_boolean_predicate (QOF_COMPARE_EQUAL, value);
1337  qof_query_add_term (q, param_list, pdata, op);
1338 }
void qof_query_add_term(QofQuery *q, QofQueryParamList *param_list, QofQueryPredData *pred_data, QofQueryOp op)
This is the general function that adds a new Query Term to a query.
Definition: qofquery.cpp:665

◆ qof_query_add_term()

void qof_query_add_term ( QofQuery *  query,
QofQueryParamList param_list,
QofQueryPredData *  pred_data,
QofQueryOp  op 
)

This is the general function that adds a new Query Term to a query.

It will find the 'obj_type' object of the search item and compare the 'param_list' parameter to the predicate data via the comparator.

The param_list is a recursive list of parameters. For example, you can say 'split->memo' by creating a list of one element, "SPLIT_MEMO". You can say 'split->account->name' by creating a list of two elements, "SPLIT_ACCOUNT" and "ACCOUNT_NAME". The list becomes the property of the Query.

For example:

acct_name_pred_data = make_string_pred_data(QOF_STRING_MATCH_CASEINSENSITIVE, account_name); param_list = make_list (SPLIT_ACCOUNT, ACCOUNT_NAME, NULL); qof_query_add_term (query, param_list, QOF_COMPARE_EQUAL, acct_name_pred_data, QOF_QUERY_AND);

Please note that QofQuery does not, at this time, support joins. That is, one cannot specify a predicate that is a parameter list. Put another way, one cannot search for objects where obja->thingy == objb->stuff

Definition at line 665 of file qofquery.cpp.

667 {
668  QofQueryTerm *qt;
669  QofQuery *qr, *qs;
670 
671  if (!q || !param_list || !pred_data) return;
672 
673  qt = g_new0 (QofQueryTerm, 1);
674  qt->param_list = param_list;
675  qt->pdata = pred_data;
676  qs = qof_query_create ();
677  query_init (qs, qt);
678 
679  if (q->terms != NULL)
680  qr = qof_query_merge (q, qs, op);
681  else
682  qr = qof_query_merge (q, qs, QOF_QUERY_OR);
683 
684  swap_terms (q, qr);
685  qof_query_destroy (qs);
686  qof_query_destroy (qr);
687 }
void qof_query_destroy(QofQuery *q)
Frees the resources associate with a Query object.
Definition: qofquery.cpp:993
QofQuery * qof_query_merge(QofQuery *q1, QofQuery *q2, QofQueryOp op)
Combine two queries together using the Boolean set (logical) operator &#39;op&#39;.
Definition: qofquery.cpp:1113
QofQuery * qof_query_create(void)
Create a new query.
Definition: qofquery.cpp:902

◆ qof_query_clear()

void qof_query_clear ( QofQuery *  query)

Remove all query terms from query.

query matches nothing after qof_query_clear().

Definition at line 889 of file qofquery.cpp.

890 {
891  QofQuery *q2 = qof_query_create ();
892  swap_terms (query, q2);
893  qof_query_destroy (q2);
894 
895  g_list_free (query->books);
896  query->books = NULL;
897  g_list_free (query->results);
898  query->results = NULL;
899  query->changed = 1;
900 }
void qof_query_destroy(QofQuery *q)
Frees the resources associate with a Query object.
Definition: qofquery.cpp:993
QofQuery * qof_query_create(void)
Create a new query.
Definition: qofquery.cpp:902

◆ qof_query_core_predicate_copy()

QofQueryPredData* qof_query_core_predicate_copy ( const QofQueryPredData *  pdata)

Copy a predicate.

Definition at line 1777 of file qofquerycore.cpp.

1778 {
1779  QueryPredicateCopyFunc copy;
1780 
1781  g_return_val_if_fail (pdata, NULL);
1782  g_return_val_if_fail (pdata->type_name, NULL);
1783 
1784  copy = qof_query_copy_predicate (pdata->type_name);
1785  return (copy (pdata));
1786 }

◆ qof_query_core_predicate_free()

void qof_query_core_predicate_free ( QofQueryPredData *  pdata)

Destroy a predicate.

Definition at line 1765 of file qofquerycore.cpp.

1766 {
1767  QueryPredDataFree free_fcn;
1768 
1769  g_return_if_fail (pdata);
1770  g_return_if_fail (pdata->type_name);
1771 
1772  free_fcn = qof_query_predicate_free (pdata->type_name);
1773  free_fcn (pdata);
1774 }

◆ qof_query_core_to_string()

char* qof_query_core_to_string ( QofType  ,
gpointer  object,
QofParam *  getter 
)

Return a printable string for a core data object.

Caller needs to g_free() the returned string.

Definition at line 1789 of file qofquerycore.cpp.

1791 {
1792  QueryToString toString;
1793 
1794  g_return_val_if_fail (type, NULL);
1795  g_return_val_if_fail (object, NULL);
1796  g_return_val_if_fail (getter, NULL);
1797 
1798  toString = reinterpret_cast<QueryToString>(g_hash_table_lookup (toStringTable, type));
1799  g_return_val_if_fail (toString, NULL);
1800 
1801  return toString (object, getter);
1802 }

◆ qof_query_create()

QofQuery* qof_query_create ( void  )

Create a new query.

Before running the query, a 'search-for' type must be set otherwise nothing will be returned. The results of the query is a list of the indicated search-for type.

Allocates and initializes a Query structure which must be freed by the user with qof_query_destroy(). A newly-allocated QofQuery object matches nothing (qof_query_run() will return NULL).

Definition at line 902 of file qofquery.cpp.

903 {
904  QofQuery *qp = g_new0 (QofQuery, 1);
905  qp->be_compiled = g_hash_table_new (g_direct_hash, g_direct_equal);
906  query_init (qp, NULL);
907  return qp;
908 }

◆ qof_query_date_predicate_get_date()

gboolean qof_query_date_predicate_get_date ( const QofQueryPredData *  pd,
time64 date 
)

Retrieve a predicate.

Definition at line 437 of file qofquerycore.cpp.

438 {
439  const query_date_t pdata = (const query_date_t)pd;
440 
441  if (pdata->pd.type_name != query_date_type)
442  return FALSE;
443  *date = pdata->date;
444  return TRUE;
445 }

◆ qof_query_destroy()

void qof_query_destroy ( QofQuery *  q)

Frees the resources associate with a Query object.

Definition at line 114 of file fake-qofquery.cpp.

115 {
116  queryPool.release_query((QofFakeQuery*)query);
117 }
Fake object providing functionality similar to QofQuery.
Definition: fake-qofquery.h:57

◆ qof_query_equal()

gboolean qof_query_equal ( const QofQuery *  q1,
const QofQuery *  q2 
)

Compare two queries for equality.

Query terms are compared each to each. This is a simplistic implementation – logical equivalences between different and/or trees are ignored.

Definition at line 1455 of file qofquery.cpp.

1456 {
1457  GList *or1, *or2;
1458 
1459  if (q1 == q2) return TRUE;
1460  if (!q1 || !q2) return FALSE;
1461 
1462  if (q1->max_results != q2->max_results) return FALSE;
1463 
1464  for (or1 = q1->terms, or2 = q2->terms; or1 || or2;
1465  or1 = or1->next, or2 = or2->next)
1466  {
1467  GList *and1, *and2;
1468 
1469  if (!or1 || !or2)
1470  return FALSE;
1471  and1 = static_cast<GList*>(or1->data);
1472  and2 = static_cast<GList*>(or2->data);
1473 
1474  for (; and1 || and2; and1 = and1->next, and2 = and2->next)
1475  {
1476  if (!and1 || !and2)
1477  return FALSE;
1478  if (!qof_query_term_equal (static_cast<QofQueryTerm*>(and1->data),
1479  static_cast<QofQueryTerm*>(and2->data)))
1480  return FALSE;
1481  }
1482  }
1483 
1484  if (!qof_query_sort_equal (&(q1->primary_sort), &(q2->primary_sort)))
1485  return FALSE;
1486  if (!qof_query_sort_equal (&(q1->secondary_sort), &(q2->secondary_sort)))
1487  return FALSE;
1488  if (!qof_query_sort_equal (&(q1->tertiary_sort), &(q2->tertiary_sort)))
1489  return FALSE;
1490 
1491  return TRUE;
1492 }

◆ qof_query_init()

void qof_query_init ( void  )

Subsystem initialization and shutdown.

Call init() once to initialize the query subsystem; call shutdown() to free up any resources associated with the query subsystem. Typically called during application startup, shutdown.

Definition at line 1343 of file qofquery.cpp.

1344 {
1345  ENTER (" ");
1346  qof_query_core_init ();
1347  qof_class_init ();
1348  LEAVE ("Completed initialization of QofQuery");
1349 }
#define ENTER(format, args...)
Print a function entry debugging message.
Definition: qoflog.h:272
#define LEAVE(format, args...)
Print a function exit debugging message.
Definition: qoflog.h:282

◆ qof_query_invert()

QofQuery* qof_query_invert ( QofQuery *  q)

Make a copy of the indicated query, inverting the sense of the search.

In other words, if the original query search for all objects with a certain condition, the inverted query will search for all object with NOT that condition. The union of the results returned by the original and inverted queries equals the set of all searched objects. These to sets are disjoint (share no members in common).

This will return a newly allocated QofQuery object, or NULL on error. Free it with qof_query_destroy() when no longer needed.

Definition at line 1034 of file qofquery.cpp.

1035 {
1036  QofQuery * retval;
1037  QofQuery * right, * left, * iright, * ileft;
1038  QofQueryTerm * qt;
1039  GList * aterms;
1040  GList * cur;
1041  GList * new_oterm;
1042  int num_or_terms;
1043 
1044  if (!q)
1045  return NULL;
1046 
1047  num_or_terms = g_list_length(q->terms);
1048 
1049  switch (num_or_terms)
1050  {
1051  case 0:
1052  retval = qof_query_create();
1053  retval->max_results = q->max_results;
1054  break;
1055 
1056  /* This is the DeMorgan expansion for a single AND expression. */
1057  /* !(abc) = !a + !b + !c */
1058  case 1:
1059  retval = qof_query_create();
1060  retval->max_results = q->max_results;
1061  retval->books = g_list_copy (q->books);
1062  retval->search_for = q->search_for;
1063  retval->changed = 1;
1064 
1065  aterms = static_cast<GList*>(g_list_nth_data(q->terms, 0));
1066  new_oterm = NULL;
1067  for (cur = aterms; cur; cur = cur->next)
1068  {
1069  qt = copy_query_term(static_cast<QofQueryTerm*>(cur->data));
1070  qt->invert = !(qt->invert);
1071  new_oterm = g_list_append(NULL, qt);
1072  retval->terms = g_list_prepend(retval->terms, new_oterm);
1073  }
1074  retval->terms = g_list_reverse(retval->terms);
1075  break;
1076 
1077  /* If there are multiple OR-terms, we just recurse by
1078  * breaking it down to !(a + b + c) =
1079  * !a * !(b + c) = !a * !b * !c. */
1080  default:
1081  right = qof_query_create();
1082  right->terms = copy_or_terms(g_list_nth(q->terms, 1));
1083 
1084  left = qof_query_create();
1085  left->terms = g_list_append(NULL,
1086  copy_and_terms(static_cast<GList*>(g_list_nth_data(q->terms, 0))));
1087 
1088  iright = qof_query_invert(right);
1089  ileft = qof_query_invert(left);
1090 
1091  retval = qof_query_merge(iright, ileft, QOF_QUERY_AND);
1092  retval->books = g_list_copy (q->books);
1093  retval->max_results = q->max_results;
1094  retval->search_for = q->search_for;
1095  retval->changed = 1;
1096 
1097  qof_query_destroy(iright);
1098  qof_query_destroy(ileft);
1099  qof_query_destroy(right);
1100  qof_query_destroy(left);
1101  break;
1102  }
1103 
1104  return retval;
1105 }
void qof_query_destroy(QofQuery *q)
Frees the resources associate with a Query object.
Definition: qofquery.cpp:993
QofQuery * qof_query_merge(QofQuery *q1, QofQuery *q2, QofQueryOp op)
Combine two queries together using the Boolean set (logical) operator &#39;op&#39;.
Definition: qofquery.cpp:1113
QofQuery * qof_query_invert(QofQuery *q)
Make a copy of the indicated query, inverting the sense of the search.
Definition: qofquery.cpp:1034
QofQuery * qof_query_create(void)
Create a new query.
Definition: qofquery.cpp:902

◆ qof_query_last_run()

GList* qof_query_last_run ( QofQuery *  query)

Return the results of the last query, without causing the query to be re-run.

Do NOT free the resulting list. This list is managed internally by QofQuery.

Definition at line 881 of file qofquery.cpp.

882 {
883  if (!query)
884  return NULL;
885 
886  return query->results;
887 }

◆ qof_query_merge()

QofQuery* qof_query_merge ( QofQuery *  q1,
QofQuery *  q2,
QofQueryOp  op 
)

Combine two queries together using the Boolean set (logical) operator 'op'.

For example, if the operator 'op' is set to QUERY_AND, then the set of results returned by the query will will be the Boolean set intersection of the results returned by q1 and q2. Similarly, QUERY_OR maps to set union, etc.

Both queries must have compatible search-types. If both queries are set, they must search for the same object type. If only one is set, the resulting query will search for the set type. If neither query has the search-type set, the result will be unset as well.

This will return a newly allocated QofQuery object, or NULL on error. Free it with qof_query_destroy() when no longer needed. Note that if either input query is NULL then the returned query is NOT newly allocated – it will return the non-NULL query. You only need to call this function when both q1 and q2 are non-NULL.

Definition at line 1113 of file qofquery.cpp.

1114 {
1115 
1116  QofQuery * retval = NULL;
1117  QofQuery * i1, * i2;
1118  QofQuery * t1, * t2;
1119  GList * i, * j;
1120  QofIdType search_for;
1121 
1122  if (!q1) return q2;
1123  if (!q2) return q1;
1124 
1125  if (q1->search_for && q2->search_for)
1126  g_return_val_if_fail (g_strcmp0 (q1->search_for, q2->search_for) == 0,
1127  NULL);
1128 
1129  search_for = (q1->search_for ? q1->search_for : q2->search_for);
1130 
1131  /* Avoid merge surprises if op==QOF_QUERY_AND but q1 is empty.
1132  * The goal of this tweak is to allow the user to start with
1133  * an empty q1 and then append to it recursively
1134  * (and q1 (and q2 (and q3 (and q4 ....))))
1135  * without bombing out because the append started with an
1136  * empty list.
1137  * We do essentially the same check in qof_query_add_term()
1138  * so that the first term added to an empty query doesn't screw up.
1139  */
1140  if ((QOF_QUERY_AND == op) &&
1141  (q1->terms == NULL || q2->terms == NULL))
1142  {
1143  op = QOF_QUERY_OR;
1144  }
1145 
1146  switch (op)
1147  {
1148  case QOF_QUERY_OR:
1149  retval = qof_query_create();
1150  retval->terms =
1151  g_list_concat(copy_or_terms(q1->terms), copy_or_terms(q2->terms));
1152  retval->books = merge_books (q1->books, q2->books);
1153  retval->max_results = q1->max_results;
1154  retval->changed = 1;
1155  break;
1156 
1157  case QOF_QUERY_AND:
1158  retval = qof_query_create();
1159  retval->books = merge_books (q1->books, q2->books);
1160  retval->max_results = q1->max_results;
1161  retval->changed = 1;
1162 
1163  /* g_list_append() can take forever, so let's build the list in
1164  * reverse and then reverse it at the end, to deal better with
1165  * "large" queries.
1166  */
1167  for (i = q1->terms; i; i = i->next)
1168  {
1169  for (j = q2->terms; j; j = j->next)
1170  {
1171  retval->terms =
1172  g_list_prepend(retval->terms,
1173  g_list_concat
1174  (copy_and_terms(static_cast<GList*>(i->data)),
1175  copy_and_terms(static_cast<GList*>(j->data))));
1176  }
1177  }
1178  retval->terms = g_list_reverse(retval->terms);
1179  break;
1180 
1181  case QOF_QUERY_NAND:
1182  /* !(a*b) = (!a + !b) */
1183  i1 = qof_query_invert(q1);
1184  i2 = qof_query_invert(q2);
1185  retval = qof_query_merge(i1, i2, QOF_QUERY_OR);
1186  qof_query_destroy(i1);
1187  qof_query_destroy(i2);
1188  break;
1189 
1190  case QOF_QUERY_NOR:
1191  /* !(a+b) = (!a*!b) */
1192  i1 = qof_query_invert(q1);
1193  i2 = qof_query_invert(q2);
1194  retval = qof_query_merge(i1, i2, QOF_QUERY_AND);
1195  qof_query_destroy(i1);
1196  qof_query_destroy(i2);
1197  break;
1198 
1199  case QOF_QUERY_XOR:
1200  /* a xor b = (a * !b) + (!a * b) */
1201  i1 = qof_query_invert(q1);
1202  i2 = qof_query_invert(q2);
1203  t1 = qof_query_merge(q1, i2, QOF_QUERY_AND);
1204  t2 = qof_query_merge(i1, q2, QOF_QUERY_AND);
1205  retval = qof_query_merge(t1, t2, QOF_QUERY_OR);
1206 
1207  qof_query_destroy(i1);
1208  qof_query_destroy(i2);
1209  qof_query_destroy(t1);
1210  qof_query_destroy(t2);
1211  break;
1212  }
1213 
1214  retval->search_for = search_for;
1215  return retval;
1216 }
const gchar * QofIdType
QofIdType declaration.
Definition: qofid.h:85
void qof_query_destroy(QofQuery *q)
Frees the resources associate with a Query object.
Definition: qofquery.cpp:993
QofQuery * qof_query_merge(QofQuery *q1, QofQuery *q2, QofQueryOp op)
Combine two queries together using the Boolean set (logical) operator &#39;op&#39;.
Definition: qofquery.cpp:1113
QofQuery * qof_query_invert(QofQuery *q)
Make a copy of the indicated query, inverting the sense of the search.
Definition: qofquery.cpp:1034
QofQuery * qof_query_create(void)
Create a new query.
Definition: qofquery.cpp:902

◆ qof_query_merge_in_place()

void qof_query_merge_in_place ( QofQuery *  q1,
QofQuery *  q2,
QofQueryOp  op 
)

Like qof_query_merge, but this will merge a copy of q2 into q1.

q2 remains unchanged.

Definition at line 1219 of file qofquery.cpp.

1220 {
1221  QofQuery *tmp_q;
1222 
1223  if (!q1 || !q2)
1224  return;
1225 
1226  tmp_q = qof_query_merge (q1, q2, op);
1227  swap_terms (q1, tmp_q);
1228  qof_query_destroy (tmp_q);
1229 }
void qof_query_destroy(QofQuery *q)
Frees the resources associate with a Query object.
Definition: qofquery.cpp:993
QofQuery * qof_query_merge(QofQuery *q1, QofQuery *q2, QofQueryOp op)
Combine two queries together using the Boolean set (logical) operator &#39;op&#39;.
Definition: qofquery.cpp:1113

◆ qof_query_print()

void qof_query_print ( QofQuery *  query)

Log the Query.

Deprecated:
Do not call directly, use the standard log module code: ::qof_log_set_level(QOF_MOD_QUERY, QOF_LOG_DEBUG);
Deprecated:
access via qof_log instead.

The query will be logged automatically if qof_log_set_level(QOF_MOD_QUERY, ...) is set to QOF_LOG_DEBUG.

This function cycles through a QofQuery object, and prints out the values of the various members of the query

Definition at line 1523 of file qofquery.cpp.

1524 {
1525  GList *output;
1526  GString *str;
1527  QofQuerySort *s[3];
1528  gint maxResults = 0, numSorts = 3;
1529 
1530  ENTER (" ");
1531 
1532  if (!query)
1533  {
1534  LEAVE("query is (null)");
1535  return;
1536  }
1537 
1538  output = NULL;
1539  str = NULL;
1540  maxResults = qof_query_get_max_results (query);
1541 
1542  output = qof_query_printSearchFor (query, output);
1543  output = qof_query_printTerms (query, output);
1544 
1545  qof_query_get_sorts (query, &s[0], &s[1], &s[2]);
1546 
1547  if (s[0])
1548  {
1549  output = qof_query_printSorts (s, numSorts, output);
1550  }
1551 
1552  str = g_string_new (" ");
1553  g_string_printf (str, "Maximum number of results: %d", maxResults);
1554  output = g_list_append (output, str);
1555 
1556  qof_query_printOutput (output);
1557  LEAVE (" ");
1558 }
#define ENTER(format, args...)
Print a function entry debugging message.
Definition: qoflog.h:272
#define LEAVE(format, args...)
Print a function exit debugging message.
Definition: qoflog.h:282

◆ qof_query_purge_terms()

void qof_query_purge_terms ( QofQuery *  q,
QofQueryParamList param_list 
)

Remove query terms of a particular type from q.

The "type" of a term is determined by the type of data that gets passed to the predicate function. XXX ??? Huh? remove anything of that predicate type, or just the particular predicate ?

Definition at line 689 of file qofquery.cpp.

690 {
691  QofQueryTerm *qt;
692  GList *_or_, *_and_;
693 
694  if (!q || !param_list) return;
695 
696  for (_or_ = q->terms; _or_; _or_ = _or_->next)
697  {
698  for (_and_ = static_cast<GList*>(_or_->data); _and_;
699  _and_ = static_cast<GList*>(_and_->next))
700  {
701  qt = static_cast<QofQueryTerm*>(_and_->data);
702  if (!param_list_cmp (qt->param_list, param_list))
703  {
704  auto or_list = static_cast<GList*> (_or_->data);
705  if (or_list && !or_list->next)
706  {
707  q->terms = g_list_remove_link (static_cast<GList*>(q->terms), _or_);
708  g_list_free_1 (_or_);
709  _or_ = q->terms;
710  break;
711  }
712  else
713  {
714  _or_->data = g_list_remove_link (or_list, _and_);
715  g_list_free_1 (_and_);
716  _and_ = static_cast<GList*>(_or_->data);
717  if (!_and_) break;
718  }
719  q->changed = 1;
720  free_query_term (qt);
721  }
722  }
723  if (!_or_) break;
724  }
725 }

◆ qof_query_run()

GList* qof_query_run ( QofQuery *  query)

Perform the query, return the results.

The returned list is a list of the 'search-for' type that was previously set with the qof_query_search_for() or the qof_query_create_for() routines. The returned list will have been sorted using the indicated sort order, and trimmed to the max_results length.

Do NOT free the resulting list. This list is managed internally by QofQuery.

Definition at line 152 of file fake-qofquery.cpp.

153 {
154  GList *matching_objects = NULL;
155  bool query_used = queryPool.query_used(query);
156 
157  EXPECT_TRUE(query_used);
158  if (query_used)
159  {
160  auto matchingObjects = ((QofFakeQuery*)query)->run();
161 
162  for (auto object : matchingObjects)
163  {
164  matching_objects = g_list_append(matching_objects, static_cast<gpointer>(object));
165  }
166  }
167 
168  return matching_objects;
169 }
Fake object providing functionality similar to QofQuery.
Definition: fake-qofquery.h:57

◆ qof_query_run_subquery()

GList* qof_query_run_subquery ( QofQuery *  subquery,
const QofQuery *  primary_query 
)

Perform a subquery, return the results.

Instead of running over a book, the subquery runs over the results of the primary query.

Do NOT free the resulting list. This list is managed internally by QofQuery.

Definition at line 864 of file qofquery.cpp.

865 {
866  if (!subq) return NULL;
867  if (!primaryq) return NULL;
868 
869  /* Make sure we're searching for the same thing */
870  g_return_val_if_fail (subq->search_for, NULL);
871  g_return_val_if_fail (primaryq->search_for, NULL);
872  g_return_val_if_fail(!g_strcmp0(subq->search_for, primaryq->search_for),
873  NULL);
874 
875  /* Perform the subquery */
876  return qof_query_run_internal(subq, qof_query_run_subq_cb,
877  (gpointer)primaryq);
878 }

◆ qof_query_search_for()

void qof_query_search_for ( QofQuery *  query,
QofIdTypeConst  obj_type 
)

Set the object type to be searched for.

The results of performing the query will be a list of this obj_type.

Definition at line 910 of file qofquery.cpp.

911 {
912  if (!q || !obj_type)
913  return;
914 
915  if (g_strcmp0 (q->search_for, obj_type))
916  {
917  q->search_for = (QofIdType) obj_type;
918  q->changed = 1;
919  }
920 }
const gchar * QofIdType
QofIdType declaration.
Definition: qofid.h:85

◆ qof_query_set_book()

void qof_query_set_book ( QofQuery *  q,
QofBook *  book 
)

Set the book to be searched.

Books contain/identify collections of objects; the search will be performed over those books specified with this function. If no books are set, no results will be returned (since there is nothing to search over).

You can search multiple books. To specify multiple books, call this function multiple times with different arguments. XXX needed qof_query_clear_books() to reset the list ...

Definition at line 120 of file fake-qofquery.cpp.

121 {
122  ASSERT_TRUE(queryPool.query_used(query));
123  ASSERT_TRUE(QOF_IS_BOOK(book));
124  ((QofFakeQuery*)query)->set_book(book);
125 }
Fake object providing functionality similar to QofQuery.
Definition: fake-qofquery.h:57

◆ qof_query_set_max_results()

void qof_query_set_max_results ( QofQuery *  q,
int  n 
)

Set the maximum number of results that should be returned.

If 'max-results' is set to -1, then all of the results are returned. If there are more results than 'max-results', then the result list is trimmed. Note that there is an important interplay between 'max-results' and the sort order: only the last bit of results are returned. For example, if the sort order is set to be increasing date order, then only the objects with the most recent dates will be returned.

Definition at line 1272 of file qofquery.cpp.

1273 {
1274  if (!q) return;
1275  q->max_results = n;
1276 }

◆ qof_query_set_sort_increasing()

void qof_query_set_sort_increasing ( QofQuery *  q,
gboolean  prim_inc,
gboolean  sec_inc,
gboolean  tert_inc 
)

When a query is run, the results are sorted before being returned.

This routine can be used to control the direction of the ordering. A value of TRUE indicates the sort will be in increasing order, a value of FALSE will order results in decreasing order.

Note that if there are more results than the 'max-results' value, then only the last max-results will be returned. For example, if the sort is set to be increasing date order, then only the objects with the most recent dates will be returned.

Definition at line 1263 of file qofquery.cpp.

1265 {
1266  if (!q) return;
1267  q->primary_sort.increasing = prim_inc;
1268  q->secondary_sort.increasing = sec_inc;
1269  q->tertiary_sort.increasing = tert_inc;
1270 }

◆ qof_query_set_sort_order()

void qof_query_set_sort_order ( QofQuery *  q,
QofQueryParamList primary_sort_params,
QofQueryParamList secondary_sort_params,
QofQueryParamList tertiary_sort_params 
)

When a query is run, the results are sorted before being returned.

This routine can be used to set the parameters on which the sort will be performed. Two objects in the result list will be compared using the 'primary_sort_params', and sorted based on that order. If the comparison shows that they are equal, then the 'secondary_sort_params' will be used. If still equal, then the tertiary parameters will be compared. Any or all of these parameter lists may be NULL. Any of these parameter lists may be set to QUERY_DEFAULT_SORT.

Note that if there are more results than the 'max-results' value, then only the last max-results will be returned. For example, if the sort is set to be increasing date order, then only the objects with the most recent dates will be returned.

The input lists become the property of QofQuery and are managed by it. They will be freed when the query is destroyed (or when new lists are set).

Definition at line 1232 of file qofquery.cpp.

1234 {
1235  if (!q) return;
1236  if (q->primary_sort.param_list)
1237  g_slist_free (q->primary_sort.param_list);
1238  q->primary_sort.param_list = params1;
1239  q->primary_sort.options = 0;
1240 
1241  if (q->secondary_sort.param_list)
1242  g_slist_free (q->secondary_sort.param_list);
1243  q->secondary_sort.param_list = params2;
1244  q->secondary_sort.options = 0;
1245 
1246  if (q->tertiary_sort.param_list)
1247  g_slist_free (q->tertiary_sort.param_list);
1248  q->tertiary_sort.param_list = params3;
1249  q->tertiary_sort.options = 0;
1250 
1251  q->changed = 1;
1252 }