/********************************************************************\ * gnc_query_list_set_query_sort * * sets the sorting order of entries in the list * * * * Args: list - list to change the sort order for * * new_column - is this a new column (so should we set the * * query sort order or just set the 'increasing' * * Returns: nothing * \********************************************************************/ static void gnc_query_list_set_query_sort (GNCQueryList *list, gboolean new_column) { gboolean sort_order = list->increasing; GList *node; GNCSearchParam *param; /* Find the column parameter definition */ node = g_list_nth(list->column_params, list->sort_column); param = node->data; /* If we're asked to invert numerics, and if this is a numeric or * debred column, then invert the sort order. */ if (list->numeric_inv_sort) { const char *type = gnc_search_param_get_param_type (param); if (!safe_strcmp(type, QOF_TYPE_NUMERIC) || !safe_strcmp(type, QOF_TYPE_DEBCRED)) sort_order = !sort_order; } /* Set the sort order for the engine, if the key changed */ if (new_column) { GSList *p1, *p2; p1 = gnc_search_param_get_param_path(param); p2 = g_slist_prepend(NULL, QUERY_DEFAULT_SORT); qof_query_set_sort_order (list->query, p1, p2, NULL); } qof_query_set_sort_increasing (list->query, sort_order, sort_order, sort_order); /* * Recompute the list. Is this really necessary? Why not just sort * the rows already in the clist? Answer: it would be an n-squared * algorithm to get the clist to match the resulting list. */ gnc_query_list_refresh(list); }
/** Creates a new query that searches for an GncEntry item with * description string equal to the given "desc" argument. The query * will find the single GncEntry with the latest (=newest) * DATE_ENTERED. */ static QofQuery *new_query_for_entry_desc(GncEntryLedger *reg, const char* desc, gboolean use_invoice) { QofQuery *query = NULL; QofQueryPredData *predData = NULL; GSList *param_list = NULL; GSList *primary_sort_params = NULL; const char* should_be_null = (use_invoice ? ENTRY_BILL : ENTRY_INVOICE); g_assert(reg); g_assert(desc); /* The query itself and its book */ query = qof_query_create_for (GNC_ID_ENTRY); qof_query_set_book (query, reg->book); /* Predicate data: We want to compare one string, namely the given * argument */ predData = qof_query_string_predicate (QOF_COMPARE_EQUAL, desc, QOF_STRING_MATCH_CASEINSENSITIVE, FALSE); /* Search Parameter: We want to query on the ENTRY_DESC column */ param_list = qof_query_build_param_list (ENTRY_DESC, NULL); /* Register this in the query */ qof_query_add_term (query, param_list, predData, QOF_QUERY_FIRST_TERM); /* For invoice entries, Entry->Bill must be NULL, and vice versa */ qof_query_add_guid_match (query, qof_query_build_param_list (should_be_null, QOF_PARAM_GUID, NULL), NULL, QOF_QUERY_AND); /* Set the sort order: By DATE_ENTERED, increasing, and returning * only one single resulting item. */ primary_sort_params = qof_query_build_param_list(ENTRY_DATE_ENTERED, NULL); qof_query_set_sort_order (query, primary_sort_params, NULL, NULL); qof_query_set_sort_increasing (query, TRUE, TRUE, TRUE); qof_query_set_max_results(query, 1); return query; }
/******************************************************************* * get_earliest_in_book * * Find the earliest date occuring in the book. Do this by making * a query and sorting by date. Since the truncated sort returns * only the *last* search results, sort in decreasing order. *******************************************************************/ static time64 get_earliest_in_book (QofBook *book) { QofQuery *q; GSList *p1, *p2; GList *res; time64 earliest; q = qof_query_create_for (GNC_ID_SPLIT); qof_query_set_max_results (q, 1); qof_query_set_book (q, book); /* Sort by transaction date */ p1 = g_slist_prepend (NULL, TRANS_DATE_POSTED); p1 = g_slist_prepend (p1, SPLIT_TRANS); p2 = g_slist_prepend (NULL, QUERY_DEFAULT_SORT); qof_query_set_sort_order (q, p1, p2, NULL); /* Reverse the sort order */ qof_query_set_sort_increasing (q, FALSE, FALSE, FALSE); /* Run the query, find the earliest transaction date */ res = qof_query_run (q); if (res) { earliest = xaccQueryGetEarliestDateFound (q); } else { /* If no results, we don't want to bomb totally */ earliest = gnc_time (0); } qof_query_destroy (q); return earliest; }