30 #include <sys/types.h>
41 #include "TransactionP.h"
46 build_param_list_internal (
const char *first, va_list rest)
51 for (param = first; param; param = va_arg (rest,
const char *))
52 list = g_slist_prepend (list, (gpointer)param);
54 return (g_slist_reverse (list));
63 xaccQueryGetSplitsUniqueTrans(
QofQuery *q)
67 GList * result = NULL;
68 GHashTable * trans_hash = g_hash_table_new(g_direct_hash, g_direct_equal);
70 for (current = splits; current; current = current->next)
72 Split *split = current->data;
75 if (!g_hash_table_lookup (trans_hash, trans))
77 g_hash_table_insert (trans_hash, trans, trans);
78 result = g_list_prepend (result, split);
82 g_hash_table_destroy (trans_hash);
84 return g_list_reverse (result);
94 query_match_all_filter_func(gpointer key, gpointer value, gpointer user_data)
97 int num_matches = GPOINTER_TO_INT(value);
98 GList ** matches = user_data;
102 *matches = g_list_prepend(*matches, t);
107 query_match_any_filter_func(gpointer key, gpointer value, gpointer user_data)
110 GList ** matches = user_data;
111 *matches = g_list_prepend(*matches, t);
115 xaccQueryGetTransactions (
QofQuery * q, query_txn_match_t runtype)
118 GList * current = NULL;
119 GList * retval = NULL;
120 GHashTable * trans_hash = g_hash_table_new(g_direct_hash, g_direct_equal);
127 for (current = splits; current; current = current->next)
133 if (runtype == QUERY_TXN_MATCH_ALL)
135 val = g_hash_table_lookup(trans_hash, trans);
136 count = GPOINTER_TO_INT(val);
138 g_hash_table_insert(trans_hash, trans, GINT_TO_POINTER(count + 1));
142 if (runtype == QUERY_TXN_MATCH_ALL)
144 g_hash_table_foreach(trans_hash, query_match_all_filter_func,
149 g_hash_table_foreach(trans_hash, query_match_any_filter_func,
153 g_hash_table_destroy(trans_hash);
165 query_match_all_lot_filter_func(gpointer key, gpointer value, gpointer user_data)
168 int num_matches = GPOINTER_TO_INT(value);
169 GList ** matches = user_data;
171 if (num_matches == gnc_lot_count_splits(l))
173 *matches = g_list_prepend(*matches, l);
178 query_match_any_lot_filter_func(gpointer key, gpointer value, gpointer user_data)
181 GList ** matches = user_data;
182 *matches = g_list_prepend(*matches, t);
186 xaccQueryGetLots (
QofQuery * q, query_txn_match_t runtype)
189 GList * current = NULL;
190 GList * retval = NULL;
191 GHashTable * lot_hash = g_hash_table_new(g_direct_hash, g_direct_equal);
198 for (current = splits; current; current = current->next)
204 if (runtype == QUERY_TXN_MATCH_ALL)
206 val = g_hash_table_lookup(lot_hash, lot);
207 count = GPOINTER_TO_INT(val);
209 g_hash_table_insert(lot_hash, lot, GINT_TO_POINTER(count + 1));
213 if (runtype == QUERY_TXN_MATCH_ALL)
215 g_hash_table_foreach(lot_hash, query_match_all_lot_filter_func,
220 g_hash_table_foreach(lot_hash, query_match_any_lot_filter_func,
224 g_hash_table_destroy(lot_hash);
240 for (; acct_list; acct_list = acct_list->next)
242 Account *acc = acct_list->data;
247 PWARN (
"acct_list has NULL account");
254 PWARN (
"acct returns NULL GncGUID");
258 list = g_list_prepend (list, (gpointer)guid);
260 xaccQueryAddAccountGUIDMatch (q, list, how, op);
269 GSList *param_list = NULL;
273 if (!guid_list && how != QOF_GUID_MATCH_NULL)
275 g_warning(
"Got a NULL guid_list but the QofGuidMatch is not MATCH_NULL (but instead %d). In other words, the list of GUID matches is empty but it must contain something non-empty.", how);
279 pred_data = qof_query_guid_predicate (how, guid_list);
286 case QOF_GUID_MATCH_NONE:
287 param_list = qof_query_build_param_list (SPLIT_ACCOUNT, QOF_PARAM_GUID, NULL);
290 param_list = qof_query_build_param_list (SPLIT_TRANS, TRANS_SPLITLIST,
294 PERR (
"Invalid match type: %d", how);
311 g_return_if_fail (guid);
313 list = g_list_prepend (NULL, (gpointer)guid);
319 xaccQueryAddStringMatch (
QofQuery* q,
const char *matchstring,
320 gboolean case_sens, gboolean use_regexp,
322 const char * path, ...)
331 pred_data = qof_query_string_predicate (how, (
char *)matchstring,
332 (case_sens ? QOF_STRING_MATCH_NORMAL :
333 QOF_STRING_MATCH_CASEINSENSITIVE),
339 param_list = build_param_list_internal (path, ap);
348 const char * path, ...)
357 pred_data = qof_query_numeric_predicate (how, sign, amount);
362 param_list = build_param_list_internal (path, ap);
378 xaccQueryAddDateMatchTS (
QofQuery * q,
387 if (!q || (!use_start && !use_end))
394 pred_data = qof_query_date_predicate (QOF_COMPARE_GTE, QOF_DATE_MATCH_NORMAL, sts);
401 param_list = qof_query_build_param_list (SPLIT_TRANS, TRANS_DATE_POSTED, NULL);
407 pred_data = qof_query_date_predicate (QOF_COMPARE_LTE, QOF_DATE_MATCH_NORMAL, ets);
414 param_list = qof_query_build_param_list (SPLIT_TRANS, TRANS_DATE_POSTED, NULL);
423 xaccQueryGetDateMatchTS (
QofQuery * q,
431 sts->tv_sec = sts->tv_nsec = 0;
432 ets->tv_sec = ets->tv_nsec = 0;
434 param_list = qof_query_build_param_list (SPLIT_TRANS, TRANS_DATE_POSTED, NULL);
435 terms = qof_query_get_term_type (q, param_list);
436 g_slist_free(param_list);
438 for (tmp = terms; tmp; tmp = g_slist_next(tmp))
440 term_data = tmp->data;
441 if (term_data->how == QOF_COMPARE_GTE)
443 if (term_data->how == QOF_COMPARE_LTE)
456 gboolean use_start,
int sday,
int smonth,
int syear,
457 gboolean use_end,
int eday,
int emonth,
int eyear,
461 xaccQueryAddDateMatchTS (q, use_start,
474 xaccQueryAddDateMatchTT(
QofQuery * q,
491 xaccQueryAddDateMatchTS (q, use_start, sts,
497 xaccQueryGetDateMatchTT (
QofQuery * q,
504 xaccQueryGetDateMatchTS (q, &sts, &ets);
521 if (how & CLEARED_CLEARED)
523 if (how & CLEARED_RECONCILED)
525 if (how & CLEARED_FROZEN)
527 if (how & CLEARED_NO)
529 if (how & CLEARED_VOIDED)
533 pred_data = qof_query_char_predicate (QOF_CHAR_MATCH_ANY, chars);
537 param_list = qof_query_build_param_list (SPLIT_RECONCILE, NULL);
546 GSList *param_list = NULL;
548 if (!q || !guid || !id_type)
551 if (!g_strcmp0 (id_type, GNC_ID_SPLIT))
552 param_list = qof_query_build_param_list (QOF_PARAM_GUID, NULL);
553 else if (!g_strcmp0 (id_type, GNC_ID_TRANS))
554 param_list = qof_query_build_param_list (SPLIT_TRANS, QOF_PARAM_GUID, NULL);
555 else if (!g_strcmp0 (id_type, GNC_ID_ACCOUNT))
556 param_list = qof_query_build_param_list (SPLIT_ACCOUNT, QOF_PARAM_GUID, NULL);
558 PERR (
"Invalid match type: %s", id_type);
568 GSList *param_list = NULL;
571 if (!q || !path || !value || !id_type)
578 if (!g_strcmp0 (id_type, GNC_ID_SPLIT))
579 param_list = qof_query_build_param_list (SPLIT_KVP, NULL);
580 else if (!g_strcmp0 (id_type, GNC_ID_TRANS))
581 param_list = qof_query_build_param_list (SPLIT_TRANS, TRANS_KVP, NULL);
582 else if (!g_strcmp0 (id_type, GNC_ID_ACCOUNT))
583 param_list = qof_query_build_param_list (SPLIT_ACCOUNT, ACCOUNT_KVP, NULL);
585 PERR (
"Invalid match type: %s", id_type);
600 param_list = qof_query_build_param_list(SPLIT_TRANS, TRANS_IS_CLOSING, NULL);
609 xaccQueryGetEarliestDateFound(
QofQuery * q)
621 earliest = sp->parent->date_posted.tv_sec;
622 for (; spl; spl = spl->next)
625 if (sp->parent->date_posted.tv_sec < earliest)
627 earliest = sp->parent->date_posted.tv_sec;
638 xaccQueryGetLatestDateFound(
QofQuery * q)
648 for (; spl; spl = spl->next)
651 if (sp->parent->date_posted.tv_sec > latest)
653 latest = sp->parent->date_posted.tv_sec;
660 xaccQueryAddDescriptionMatch(
QofQuery *q,
const char *m, gboolean c, gboolean r,
663 xaccQueryAddStringMatch ((q), (m), (c), (r), (h), (o), SPLIT_TRANS,
664 TRANS_DESCRIPTION, NULL);
668 xaccQueryAddNotesMatch(
QofQuery *q,
const char *m, gboolean c, gboolean r,
671 xaccQueryAddStringMatch ((q), (m), (c), (r), (h), (o), SPLIT_TRANS,
676 xaccQueryAddNumberMatch(
QofQuery *q,
const char *m, gboolean c, gboolean r,
679 xaccQueryAddStringMatch ((q), (m), (c), (r), (h), (o), SPLIT_TRANS,
684 xaccQueryAddActionMatch(
QofQuery *q,
const char *m, gboolean c, gboolean r,
687 xaccQueryAddStringMatch ((q), (m), (c), (r), (h), (o), SPLIT_ACTION, NULL);
691 xaccQueryAddMemoMatch(
QofQuery *q,
const char *m, gboolean c, gboolean r,
694 xaccQueryAddStringMatch ((q), (m), (c), (r), (h), (o), SPLIT_MEMO, NULL);
701 xaccQueryAddNumericMatch ((q), (amt), (sgn), (how), (op),
709 xaccQueryAddNumericMatch ((q), (amt), QOF_NUMERIC_MATCH_ANY, (how), (op),
710 SPLIT_SHARE_PRICE, NULL);
717 xaccQueryAddNumericMatch ((q), (amt), QOF_NUMERIC_MATCH_ANY, (how), (op),
724 xaccQueryAddNumericMatch(
725 (q), gnc_numeric_zero(), QOF_NUMERIC_MATCH_ANY,
726 ((bal) ? QOF_COMPARE_EQUAL : QOF_COMPARE_NEQ), (op),
727 SPLIT_TRANS, TRANS_IMBALANCE, NULL);
void qof_query_add_term(QofQuery *query, QofQueryParamList *param_list, QofQueryPredData *pred_data, QofQueryOp op)
QofQueryPredData * qof_query_kvp_predicate(QofQueryCompare how, QofQueryParamList *path, const KvpValue *value)
Timespec gnc_dmy2timespec_end(gint day, gint month, gint year)
Use a 64-bit unsigned int timespec.
Transaction * xaccSplitGetParent(const Split *split)
Timespec gnc_dmy2timespec(gint day, gint month, gint year)
#define PERR(format, args...)
struct _QofQuery QofQuery
#define PWARN(format, args...)
int xaccTransCountSplits(const Transaction *trans)
Account handling public routines.
void qof_query_destroy(QofQuery *q)
#define SPLIT_ACCOUNT_GUID
const GncGUID * qof_entity_get_guid(gconstpointer)
void qof_query_add_guid_match(QofQuery *q, QofQueryParamList *param_list, const GncGUID *guid, QofQueryOp op)
GList * qof_query_last_run(QofQuery *query)
GList * qof_query_run(QofQuery *query)
gboolean qof_query_date_predicate_get_date(const QofQueryPredData *pd, Timespec *date)
void qof_query_add_boolean_match(QofQuery *q, QofQueryParamList *param_list, gboolean value, QofQueryOp op)
API for Transactions and Splits (journal entries)
QofQuery * qof_query_create(void)
struct KvpValueImpl KvpValue
GNCLot * xaccSplitGetLot(const Split *split)
const gchar * QofLogModule
void qof_query_merge_in_place(QofQuery *q1, QofQuery *q2, QofQueryOp op)