27 #include "swig-runtime.h" 32 #include "engine-helpers.h" 33 #include "gnc-engine-guile.h" 36 #include "gnc-session.h" 37 #include "guile-mappings.h" 38 #include "gnc-guile-utils.h" 40 #include <qofbookslots.h> 43 # include "strptime.h" 50 #include "qofquery-p.h" 51 #include "qofquerycore-p.h" 53 #define FUNC_NAME G_STRFUNC 55 static QofLogModule log_module = GNC_MOD_ENGINE;
58 GDate gnc_time64_to_GDate(SCM x)
60 time64 time = scm_to_int64 (x);
72 return scm_from_utf8_string(
string);
76 gnc_scm2guid(SCM guid_scm)
81 if (!scm_is_string(guid_scm)
86 str = gnc_scm_to_utf8_string (guid_scm);
93 gnc_guid_p(SCM guid_scm)
99 if (!scm_is_string(guid_scm))
106 str = gnc_scm_to_utf8_string (guid_scm);
149 gnc_query_scm2compare (SCM how_scm)
156 gnc_query_scm2string (SCM how_scm)
163 gnc_query_scm2date (SCM how_scm)
170 gnc_query_scm2numericop (SCM how_scm)
177 gnc_query_scm2guid (SCM how_scm)
184 gnc_query_scm2char (SCM how_scm)
190 gnc_scm2acct_match_how (SCM how_scm)
193 gchar *how = gnc_scm_symbol_to_locale_string (how_scm);
195 if (!g_strcmp0 (how,
"acct-match-all"))
197 else if (!g_strcmp0 (how,
"acct-match-any"))
199 else if (!g_strcmp0 (how,
"acct-match-none"))
200 res = QOF_GUID_MATCH_NONE;
203 PINFO (
"invalid account match: %s", how);
204 res = QOF_GUID_MATCH_NULL;
212 gnc_scm2amt_match_how (SCM how_scm)
215 gchar *how = gnc_scm_symbol_to_locale_string (how_scm);
217 if (!g_strcmp0 (how,
"amt-match-atleast"))
218 res = QOF_COMPARE_GTE;
219 else if (!g_strcmp0 (how,
"amt-match-atmost"))
220 res = QOF_COMPARE_LTE;
221 else if (!g_strcmp0 (how,
"amt-match-exactly"))
222 res = QOF_COMPARE_EQUAL;
225 PINFO (
"invalid amount match: %s", how);
226 res = QOF_COMPARE_EQUAL;
234 gnc_scm2bitfield (SCM field_scm)
238 if (!scm_is_list (field_scm))
241 while (!scm_is_null (field_scm))
246 scm = SCM_CAR (field_scm);
247 field_scm = SCM_CDR (field_scm);
249 bit = scm_to_int(scm);
256 static cleared_match_t
257 gnc_scm2cleared_match_how (SCM how_scm)
259 return static_cast<cleared_match_t
>(gnc_scm2bitfield (how_scm));
263 gnc_scm2balance_match_how (SCM how_scm, gboolean *resp)
267 if (!scm_is_list (how_scm))
270 if (scm_is_null (how_scm))
274 if (!scm_is_null (SCM_CDR (how_scm)))
277 how = gnc_scm_symbol_to_locale_string (SCM_CAR(how_scm));
279 if (!g_strcmp0 (how,
"balance-match-balanced"))
289 gnc_guid_glist2scm (
const GList *account_guids)
294 for (node = account_guids; node; node = node->next)
296 auto guid =
static_cast<GncGUID*
>(node->data);
299 guids = scm_cons (gnc_guid2scm (*guid), guids);
302 return scm_reverse (guids);
306 gnc_scm2guid_glist (SCM guids_scm)
308 GList *guids =
nullptr;
310 if (!scm_is_list (guids_scm))
313 while (!scm_is_null (guids_scm))
315 SCM guid_scm = SCM_CAR (guids_scm);
318 if (guid_scm != SCM_BOOL_F)
321 *guid = gnc_scm2guid (guid_scm);
324 guids = g_list_prepend (guids, guid);
326 guids_scm = SCM_CDR (guids_scm);
329 return g_list_reverse (guids);
333 gnc_guid_glist_free (GList *guids)
335 g_list_free_full (guids, (GDestroyNotify)guid_free);
339 gnc_query_numeric2scm (gnc_numeric val)
341 return scm_cons (scm_from_int64 (val.num),
342 scm_from_int64 (val.denom));
346 gnc_query_numeric_p (SCM pair)
348 return (scm_is_pair (pair));
352 gnc_query_scm2numeric (SCM pair)
357 num = SCM_CAR (pair);
358 denom = SCM_CDR (pair);
360 return gnc_numeric_create (scm_to_int64 (num),
361 scm_to_int64 (denom));
365 gnc_query_path2scm (
const GSList *path)
367 SCM path_scm = SCM_EOL;
370 for (node = path; node; node = node->next)
372 auto key =
static_cast<const char *
>(node->data);
375 path_scm = scm_cons (scm_from_utf8_string (key), path_scm);
378 return scm_reverse (path_scm);
382 gnc_query_scm2path (SCM path_scm)
384 GSList *path =
nullptr;
386 if (!scm_is_list (path_scm))
389 for (; !scm_is_null (path_scm); path_scm = scm_cdr (path_scm))
391 SCM key_scm = SCM_CAR (path_scm);
393 if (!scm_is_string (key_scm))
396 auto key = gnc_scm_to_utf8_string(key_scm);
401 return g_slist_reverse (path);
405 gnc_query_path_free (GSList *path)
412 gnc_queryterm2scm (
const QofQueryTerm *qt)
414 SCM qt_scm = SCM_EOL;
415 QofQueryPredData *pd =
nullptr;
417 qt_scm = scm_cons (gnc_query_path2scm (qof_query_term_get_param_path (qt)),
419 qt_scm = scm_cons (SCM_BOOL (qof_query_term_is_inverted (qt)), qt_scm);
421 pd = qof_query_term_get_pred_data (qt);
422 qt_scm = scm_cons (scm_from_locale_symbol (pd->type_name), qt_scm);
423 qt_scm = scm_cons (scm_from_long (pd->how), qt_scm);
425 if (!g_strcmp0 (pd->type_name, QOF_TYPE_STRING))
429 qt_scm = scm_cons (scm_from_long (pdata->options), qt_scm);
430 qt_scm = scm_cons (SCM_BOOL (pdata->is_regex), qt_scm);
431 qt_scm = scm_cons (pdata->matchstring ? scm_from_utf8_string (pdata->matchstring) : SCM_BOOL_F, qt_scm);
434 else if (!g_strcmp0 (pd->type_name, QOF_TYPE_DATE))
438 qt_scm = scm_cons (scm_from_long (pdata->options), qt_scm);
439 qt_scm = scm_cons (scm_from_int64 (pdata->date), qt_scm);
442 else if (!g_strcmp0 (pd->type_name, QOF_TYPE_NUMERIC))
446 qt_scm = scm_cons (scm_from_long (pdata->options), qt_scm);
447 qt_scm = scm_cons (gnc_query_numeric2scm (pdata->amount), qt_scm);
450 else if (!g_strcmp0 (pd->type_name, QOF_TYPE_GUID))
454 qt_scm = scm_cons (scm_from_long (pdata->options), qt_scm);
455 qt_scm = scm_cons (gnc_guid_glist2scm (pdata->guids), qt_scm);
458 else if (!g_strcmp0 (pd->type_name, QOF_TYPE_INT64))
462 qt_scm = scm_cons (scm_from_int64 (pdata->val), qt_scm);
465 else if (!g_strcmp0 (pd->type_name, QOF_TYPE_DOUBLE))
469 qt_scm = scm_cons (scm_from_double (pdata->val), qt_scm);
472 else if (!g_strcmp0 (pd->type_name, QOF_TYPE_BOOLEAN))
476 qt_scm = scm_cons (SCM_BOOL (pdata->val), qt_scm);
479 else if (!g_strcmp0 (pd->type_name, QOF_TYPE_CHAR))
483 qt_scm = scm_cons (scm_from_long (pdata->options), qt_scm);
484 qt_scm = scm_cons (pdata->char_list ? scm_from_utf8_string (pdata->char_list) : SCM_BOOL_F, qt_scm);
489 PWARN (
"query core type %s not supported", pd->type_name);
493 return scm_reverse (qt_scm);
497 gnc_scm2query_term_query_v2 (SCM qt_scm)
500 QofQueryPredData *pd =
nullptr;
502 gchar *type =
nullptr;
503 GSList *path =
nullptr;
504 gboolean inverted = FALSE;
507 if (!scm_is_list (qt_scm) || scm_is_null (qt_scm))
513 scm = SCM_CAR (qt_scm);
514 qt_scm = SCM_CDR (qt_scm);
515 if (!scm_is_list (scm))
517 path = gnc_query_scm2path (scm);
520 scm = SCM_CAR (qt_scm);
521 qt_scm = SCM_CDR (qt_scm);
522 if (!scm_is_bool (scm))
524 inverted = scm_is_true (scm);
527 scm = SCM_CAR (qt_scm);
528 qt_scm = SCM_CDR (qt_scm);
529 if (!scm_is_symbol (scm))
531 type = gnc_scm_symbol_to_locale_string (scm);
534 scm = SCM_CAR (qt_scm);
535 qt_scm = SCM_CDR (qt_scm);
536 if (scm_is_null (scm))
538 compare_how = gnc_query_scm2compare (scm);
542 if (!g_strcmp0 (type, QOF_TYPE_STRING))
548 scm = SCM_CAR (qt_scm);
549 qt_scm = SCM_CDR (qt_scm);
550 if (scm_is_null (scm))
break;
551 options = gnc_query_scm2string (scm);
553 scm = SCM_CAR (qt_scm);
554 qt_scm = SCM_CDR (qt_scm);
555 if (!scm_is_bool (scm))
break;
556 is_regex = scm_is_true (scm);
558 scm = SCM_CAR (qt_scm);
559 if (!scm_is_string (scm))
break;
561 matchstring = gnc_scm_to_utf8_string (scm);
563 pd = qof_query_string_predicate (compare_how, matchstring,
565 g_free (matchstring);
567 else if (!g_strcmp0 (type, QOF_TYPE_DATE))
572 scm = SCM_CAR (qt_scm);
573 qt_scm = SCM_CDR (qt_scm);
574 if (scm_is_null (scm))
576 options = gnc_query_scm2date (scm);
578 scm = SCM_CAR (qt_scm);
579 if (scm_is_null (scm))
581 date = scm_to_int64 (scm);
583 pd = qof_query_date_predicate (compare_how, options, date);
586 else if (!g_strcmp0 (type, QOF_TYPE_NUMERIC))
591 scm = SCM_CAR (qt_scm);
592 qt_scm = SCM_CDR (qt_scm);
593 if (scm_is_null (scm))
595 options = gnc_query_scm2numericop (scm);
597 scm = SCM_CAR (qt_scm);
598 if (!gnc_query_numeric_p (scm))
600 val = gnc_query_scm2numeric (scm);
602 pd = qof_query_numeric_predicate (compare_how, options, val);
605 else if (!g_strcmp0 (type, QOF_TYPE_GUID))
610 scm = SCM_CAR (qt_scm);
611 qt_scm = SCM_CDR (qt_scm);
612 if (scm_is_null (scm))
614 options = gnc_query_scm2guid (scm);
616 scm = SCM_CAR (qt_scm);
617 if (!scm_is_list (scm))
619 guids = gnc_scm2guid_glist (scm);
621 pd = qof_query_guid_predicate (options, guids);
623 gnc_guid_glist_free (guids);
626 else if (!g_strcmp0 (type, QOF_TYPE_INT64))
630 scm = SCM_CAR (qt_scm);
631 if (scm_is_null (scm))
633 val = scm_to_int64 (scm);
635 pd = qof_query_int64_predicate (compare_how, val);
638 else if (!g_strcmp0 (type, QOF_TYPE_DOUBLE))
642 scm = SCM_CAR (qt_scm);
643 if (!scm_is_number (scm))
645 val = scm_to_double (scm);
647 pd = qof_query_double_predicate (compare_how, val);
650 else if (!g_strcmp0 (type, QOF_TYPE_BOOLEAN))
654 scm = SCM_CAR (qt_scm);
655 if (!scm_is_bool (scm))
657 val = scm_is_true (scm);
659 pd = qof_query_boolean_predicate (compare_how, val);
662 else if (!g_strcmp0 (type, QOF_TYPE_CHAR))
667 scm = SCM_CAR (qt_scm);
668 qt_scm = SCM_CDR (qt_scm);
669 if (scm_is_null (scm))
671 options = gnc_query_scm2char (scm);
673 scm = SCM_CAR (qt_scm);
674 if (!scm_is_string (scm))
676 char_list = gnc_scm_to_utf8_string (scm);
678 pd = qof_query_char_predicate (options, char_list);
683 PWARN (
"query core type %s not supported", type);
705 gnc_query_path_free (path);
712 gnc_scm2query_term_query_v1 (SCM query_term_scm)
715 gchar * pd_type =
nullptr;
716 gchar * pr_type =
nullptr;
717 gboolean sense = FALSE;
721 if (!scm_is_list (query_term_scm) ||
722 scm_is_null (query_term_scm))
731 scm = SCM_CAR (query_term_scm);
732 query_term_scm = SCM_CDR (query_term_scm);
733 pd_type = gnc_scm_symbol_to_locale_string (scm);
736 if (scm_is_null (query_term_scm))
738 PINFO (
"null pr_type");
741 scm = SCM_CAR (query_term_scm);
742 query_term_scm = SCM_CDR (query_term_scm);
743 pr_type = gnc_scm_symbol_to_locale_string (scm);
746 if (scm_is_null (query_term_scm))
748 PINFO (
"null sense");
751 scm = SCM_CAR (query_term_scm);
752 query_term_scm = SCM_CDR (query_term_scm);
753 sense = scm_is_true (scm);
755 q = qof_query_create_for(GNC_ID_SPLIT);
757 if (!g_strcmp0 (pd_type,
"pd-date"))
765 if (scm_is_null (query_term_scm))
767 PINFO (
"null use_start");
771 scm = SCM_CAR (query_term_scm);
772 query_term_scm = SCM_CDR (query_term_scm);
773 use_start = scm_is_true (scm);
776 if (scm_is_null (query_term_scm))
779 scm = SCM_CAR (query_term_scm);
780 query_term_scm = SCM_CDR (query_term_scm);
781 start = scm_to_int64 (scm);
784 if (scm_is_null (query_term_scm))
787 scm = SCM_CAR (query_term_scm);
788 query_term_scm = SCM_CDR (query_term_scm);
789 use_end = scm_is_true (scm);
792 if (scm_is_null (query_term_scm))
795 scm = SCM_CAR (query_term_scm);
796 end = scm_to_int64 (scm);
798 xaccQueryAddDateMatchTT (q, use_start, start, use_end, end, QOF_QUERY_OR);
803 else if (!g_strcmp0 (pd_type,
"pd-amount"))
810 if (scm_is_null (query_term_scm))
812 scm = SCM_CAR (query_term_scm);
813 query_term_scm = SCM_CDR (query_term_scm);
814 how = gnc_scm2amt_match_how (scm);
817 if (scm_is_null (query_term_scm))
819 scm = SCM_CAR (query_term_scm);
820 query_term_scm = SCM_CDR (query_term_scm);
821 amt_sgn = gnc_query_scm2numericop (scm);
824 if (scm_is_null (query_term_scm))
826 scm = SCM_CAR (query_term_scm);
827 val = gnc_numeric_create (scm_to_int64(scm_numerator(scm)),
828 scm_to_int64(scm_denominator(scm)));
830 if (!g_strcmp0 (pr_type,
"pr-price"))
832 xaccQueryAddSharePriceMatch (q, val, how, QOF_QUERY_OR);
836 else if (!g_strcmp0 (pr_type,
"pr-shares"))
838 xaccQueryAddSharesMatch (q, val, how, QOF_QUERY_OR);
842 else if (!g_strcmp0 (pr_type,
"pr-value"))
844 xaccQueryAddValueMatch (q, val, amt_sgn, how, QOF_QUERY_OR);
850 PINFO (
"unknown amount predicate: %s", pr_type);
854 else if (!g_strcmp0 (pd_type,
"pd-account"))
857 GList *account_guids;
860 if (scm_is_null (query_term_scm))
862 PINFO (
"pd-account: null how");
866 scm = SCM_CAR (query_term_scm);
867 query_term_scm = SCM_CDR (query_term_scm);
868 how = gnc_scm2acct_match_how (scm);
871 if (scm_is_null (query_term_scm))
873 PINFO (
"pd-account: null guids");
877 scm = SCM_CAR (query_term_scm);
879 account_guids = gnc_scm2guid_glist (scm);
881 xaccQueryAddAccountGUIDMatch (q, account_guids, how, QOF_QUERY_OR);
883 gnc_guid_glist_free (account_guids);
888 else if (!g_strcmp0 (pd_type,
"pd-string"))
895 if (scm_is_null (query_term_scm))
898 scm = SCM_CAR (query_term_scm);
899 query_term_scm = SCM_CDR (query_term_scm);
900 case_sens = scm_is_true (scm);
903 if (scm_is_null (query_term_scm))
906 scm = SCM_CAR (query_term_scm);
907 query_term_scm = SCM_CDR (query_term_scm);
908 use_regexp = scm_is_true (scm);
911 if (scm_is_null (query_term_scm))
914 scm = SCM_CAR (query_term_scm);
915 matchstring = gnc_scm_to_utf8_string (scm);
917 if (!g_strcmp0 (pr_type,
"pr-action"))
919 xaccQueryAddActionMatch (q, matchstring, case_sens, use_regexp,
920 QOF_COMPARE_CONTAINS, QOF_QUERY_OR);
924 else if (!g_strcmp0 (pr_type,
"pr-desc"))
926 xaccQueryAddDescriptionMatch (q, matchstring, case_sens,
927 use_regexp, QOF_COMPARE_CONTAINS, QOF_QUERY_OR);
931 else if (!g_strcmp0 (pr_type,
"pr-memo"))
933 xaccQueryAddMemoMatch (q, matchstring, case_sens, use_regexp,
934 QOF_COMPARE_CONTAINS, QOF_QUERY_OR);
938 else if (!g_strcmp0 (pr_type,
"pr-num"))
940 xaccQueryAddNumberMatch (q, matchstring, case_sens, use_regexp,
941 QOF_COMPARE_CONTAINS, QOF_QUERY_OR);
947 PINFO (
"Unknown string predicate: %s", pr_type);
949 g_free (matchstring);
952 else if (!g_strcmp0 (pd_type,
"pd-cleared"))
957 if (scm_is_null (query_term_scm))
960 scm = SCM_CAR (query_term_scm);
961 how = gnc_scm2cleared_match_how (scm);
963 xaccQueryAddClearedMatch (q, how, QOF_QUERY_OR);
967 else if (!g_strcmp0 (pd_type,
"pd-balance"))
972 if (scm_is_null (query_term_scm))
975 scm = SCM_CAR (query_term_scm);
976 if (gnc_scm2balance_match_how (scm, &how) == FALSE)
979 xaccQueryAddBalanceMatch (q, static_cast<QofQueryCompare>(how), QOF_QUERY_OR);
983 else if (!g_strcmp0 (pd_type,
"pd-guid"))
989 if (scm_is_null (query_term_scm))
992 scm = SCM_CAR (query_term_scm);
993 query_term_scm = SCM_CDR (query_term_scm);
994 guid = gnc_scm2guid (scm);
997 scm = SCM_CAR (query_term_scm);
998 id_type = (
QofIdType) gnc_scm_to_utf8_string (scm);
1000 xaccQueryAddGUIDMatch (q, &guid, id_type, QOF_QUERY_OR);
1001 g_free ((
void *) id_type);
1007 PINFO (
"Unknown Predicate: %s", pd_type);
1036 gnc_scm2query_term_query (SCM query_term_scm, query_version_t vers)
1041 return gnc_scm2query_term_query_v1 (query_term_scm);
1043 return gnc_scm2query_term_query_v2 (query_term_scm);
1050 gnc_query_terms2scm (
const GList *terms)
1052 SCM or_terms = SCM_EOL;
1053 const GList *or_node;
1055 for (or_node = terms; or_node; or_node = or_node->next)
1057 SCM and_terms = SCM_EOL;
1060 for (and_node = static_cast<GList*>(or_node->data); and_node; and_node = and_node->next)
1062 auto qt =
static_cast<QofQueryTerm*
>(and_node->data);
1065 qt_scm = gnc_queryterm2scm (qt);
1067 and_terms = scm_cons (qt_scm, and_terms);
1070 and_terms = scm_reverse (and_terms);
1072 or_terms = scm_cons (and_terms, or_terms);
1075 return scm_reverse (or_terms);
1079 gnc_scm2query_and_terms (SCM and_terms, query_version_t vers)
1083 if (!scm_is_list (and_terms))
1086 while (!scm_is_null (and_terms))
1090 term = SCM_CAR (and_terms);
1091 and_terms = SCM_CDR (and_terms);
1094 q = gnc_scm2query_term_query (term, vers);
1100 q_and = gnc_scm2query_term_query (term, vers);
1120 gnc_scm2query_or_terms (SCM or_terms, query_version_t vers)
1124 if (!scm_is_list (or_terms))
1127 q = qof_query_create_for(GNC_ID_SPLIT);
1129 while (!scm_is_null (or_terms))
1133 and_terms = SCM_CAR (or_terms);
1134 or_terms = SCM_CDR (or_terms);
1137 q = gnc_scm2query_and_terms (and_terms, vers);
1143 q_or = gnc_scm2query_and_terms (and_terms, vers);
1163 gnc_query_sort2scm (
const QofQuerySort *qs)
1165 SCM sort_scm = SCM_EOL;
1168 path = qof_query_sort_get_param_path (qs);
1169 if (path ==
nullptr)
1172 sort_scm = scm_cons (gnc_query_path2scm (path), sort_scm);
1173 sort_scm = scm_cons (scm_from_int (qof_query_sort_get_sort_options (qs)), sort_scm);
1174 sort_scm = scm_cons (SCM_BOOL (qof_query_sort_get_increasing (qs)), sort_scm);
1176 return scm_reverse (sort_scm);
1180 gnc_query_scm2sort (SCM sort_scm, GSList **path, gint *options, gboolean *inc)
1187 g_return_val_if_fail (path && options && inc, FALSE);
1188 g_return_val_if_fail (*path ==
nullptr, FALSE);
1191 if (scm_is_bool (sort_scm))
1195 if (!scm_is_list (sort_scm))
1199 val = SCM_CAR (sort_scm);
1200 sort_scm = SCM_CDR (sort_scm);
1201 if (!scm_is_list (val))
1203 p = gnc_query_scm2path (val);
1206 val = SCM_CAR (sort_scm);
1207 sort_scm = SCM_CDR (sort_scm);
1208 if (!scm_is_number (val))
1210 gnc_query_path_free (p);
1213 o = scm_to_int (val);
1216 val = SCM_CAR (sort_scm);
1217 sort_scm = SCM_CDR (sort_scm);
1218 if (!scm_is_bool (val))
1220 gnc_query_path_free (p);
1223 i = scm_is_true (val);
1226 if (!scm_is_null (sort_scm))
1228 gnc_query_path_free (p);
1241 SCM query_scm = SCM_EOL;
1243 QofQuerySort *s1, *s2, *s3;
1245 if (!q)
return SCM_BOOL_F;
1248 pair = scm_cons (gnc_query_terms2scm (qof_query_get_terms (q)), SCM_EOL);
1249 pair = scm_cons (scm_from_locale_symbol (
"terms"), pair);
1250 query_scm = scm_cons (pair, query_scm);
1254 pair = scm_cons (scm_from_locale_symbol (
"search-for"), pair);
1255 query_scm = scm_cons (pair, query_scm);
1258 qof_query_get_sorts (q, &s1, &s2, &s3);
1261 pair = scm_cons (gnc_query_sort2scm (s1), SCM_EOL);
1262 pair = scm_cons (scm_from_locale_symbol (
"primary-sort"), pair);
1263 query_scm = scm_cons (pair, query_scm);
1266 pair = scm_cons (gnc_query_sort2scm (s2), SCM_EOL);
1267 pair = scm_cons (scm_from_locale_symbol (
"secondary-sort"), pair);
1268 query_scm = scm_cons (pair, query_scm);
1271 pair = scm_cons (gnc_query_sort2scm (s3), SCM_EOL);
1272 pair = scm_cons (scm_from_locale_symbol (
"tertiary-sort"), pair);
1273 query_scm = scm_cons (pair, query_scm);
1276 pair = scm_cons (scm_from_int (qof_query_get_max_results (q)), SCM_EOL);
1277 pair = scm_cons (scm_from_locale_symbol (
"max-results"), pair);
1278 query_scm = scm_cons (pair, query_scm);
1281 pair = scm_reverse (query_scm);
1282 return scm_cons (scm_from_locale_symbol (
"query-v2"), pair);
1286 gnc_query_sort_to_list (
const gchar * symbol)
1288 GSList *path =
nullptr;
1293 if (!g_strcmp0 (symbol,
"by-none"))
1297 else if (!g_strcmp0 (symbol,
"by-standard"))
1302 else if (!g_strcmp0 (symbol,
"by-date") ||
1303 !g_strcmp0 (symbol,
"by-date-rounded"))
1305 path = g_slist_prepend (path, (gpointer) TRANS_DATE_POSTED);
1306 path = g_slist_prepend (path, (gpointer) SPLIT_TRANS);
1309 else if (!g_strcmp0 (symbol,
"by-date-entered") ||
1310 !g_strcmp0 (symbol,
"by-date-entered-rounded"))
1312 path = g_slist_prepend (path, (gpointer) TRANS_DATE_ENTERED);
1313 path = g_slist_prepend (path, (gpointer) SPLIT_TRANS);
1316 else if (!g_strcmp0 (symbol,
"by-date-reconciled") ||
1317 !g_strcmp0 (symbol,
"by-date-reconciled-rounded"))
1319 path = g_slist_prepend (path, (gpointer) SPLIT_DATE_RECONCILED);
1322 else if (!g_strcmp0 (symbol,
"by-num"))
1324 path = g_slist_prepend (path, (gpointer) TRANS_NUM);
1325 path = g_slist_prepend (path, (gpointer) SPLIT_TRANS);
1328 else if (!g_strcmp0 (symbol,
"by-amount"))
1330 path = g_slist_prepend (path, (gpointer) SPLIT_VALUE);
1333 else if (!g_strcmp0 (symbol,
"by-memo"))
1335 path = g_slist_prepend (path, (gpointer) SPLIT_MEMO);
1338 else if (!g_strcmp0 (symbol,
"by-desc"))
1340 path = g_slist_prepend (path, (gpointer) TRANS_DESCRIPTION);
1341 path = g_slist_prepend (path, (gpointer) SPLIT_TRANS);
1344 else if (!g_strcmp0 (symbol,
"by-reconcile"))
1346 path = g_slist_prepend (path, (gpointer) SPLIT_RECONCILE);
1349 else if (!g_strcmp0 (symbol,
"by-account-full-name"))
1351 path = g_slist_prepend (path, (gpointer) SPLIT_ACCT_FULLNAME);
1354 else if (!g_strcmp0 (symbol,
"by-account-code"))
1356 path = g_slist_prepend (path, (gpointer) ACCOUNT_CODE_);
1357 path = g_slist_prepend (path, (gpointer) SPLIT_ACCOUNT);
1360 else if (!g_strcmp0 (symbol,
"by-corr-account-full-name"))
1362 path = g_slist_prepend (path, (gpointer) SPLIT_CORR_ACCT_NAME);
1365 else if (!g_strcmp0 (symbol,
"by-corr-account-code"))
1367 path = g_slist_prepend (path, (gpointer) SPLIT_CORR_ACCT_CODE);
1372 PERR (
"Unknown sort-type, %s", symbol);
1379 gnc_scm2query_v1 (SCM query_scm)
1383 gchar * primary_sort =
nullptr;
1384 gchar * secondary_sort =
nullptr;
1385 gchar * tertiary_sort =
nullptr;
1386 gboolean primary_increasing = TRUE;
1387 gboolean secondary_increasing = TRUE;
1388 gboolean tertiary_increasing = TRUE;
1389 int max_splits = -1;
1391 while (!scm_is_null (query_scm))
1398 pair = SCM_CAR (query_scm);
1399 query_scm = SCM_CDR (query_scm);
1401 if (!scm_is_pair (pair))
1403 PERR (
"Not a Pair");
1408 sym_scm = SCM_CAR (pair);
1409 value = SCM_CADR (pair);
1411 if (!scm_is_symbol (sym_scm))
1413 PERR (
"Not a symbol");
1418 symbol = gnc_scm_symbol_to_locale_string (sym_scm);
1421 PERR (
"No string found");
1426 if (g_strcmp0 (
"terms", symbol) == 0)
1431 q = gnc_scm2query_or_terms (value, gnc_QUERY_v1);
1434 PINFO (
"invalid terms");
1440 else if (g_strcmp0 (
"primary-sort", symbol) == 0)
1442 if (!scm_is_symbol (value))
1444 PINFO (
"Invalid primary sort");
1449 primary_sort = gnc_scm_symbol_to_locale_string (value);
1452 else if (g_strcmp0 (
"secondary-sort", symbol) == 0)
1454 if (!scm_is_symbol (value))
1456 PINFO (
"Invalid secondary sort");
1461 secondary_sort = gnc_scm_symbol_to_locale_string (value);
1464 else if (g_strcmp0 (
"tertiary-sort", symbol) == 0)
1466 if (!scm_is_symbol (value))
1468 PINFO (
"Invalid tertiary sort");
1473 tertiary_sort = gnc_scm_symbol_to_locale_string (value);
1476 else if (g_strcmp0 (
"primary-increasing", symbol) == 0)
1478 primary_increasing = scm_is_true (value);
1481 else if (g_strcmp0 (
"secondary-increasing", symbol) == 0)
1483 secondary_increasing = scm_is_true (value);
1486 else if (g_strcmp0 (
"tertiary-increasing", symbol) == 0)
1488 tertiary_increasing = scm_is_true (value);
1491 else if (g_strcmp0 (
"max-splits", symbol) == 0)
1493 if (!scm_is_number (value))
1495 PERR (
"invalid max-splits");
1500 max_splits = scm_to_int (value);
1505 PERR (
"Unknown symbol: %s", symbol);
1515 GSList *s1, *s2, *s3;
1516 s1 = gnc_query_sort_to_list (primary_sort);
1517 s2 = gnc_query_sort_to_list (secondary_sort);
1518 s3 = gnc_query_sort_to_list (tertiary_sort);
1522 tertiary_increasing);
1531 g_free (primary_sort);
1532 g_free (secondary_sort);
1533 g_free (tertiary_sort);
1539 gnc_scm2query_v2 (SCM query_scm)
1543 gchar * search_for =
nullptr;
1544 GSList *sp1 =
nullptr, *sp2 =
nullptr, *sp3 =
nullptr;
1545 gint so1 = 0, so2 = 0, so3 = 0;
1546 gboolean si1 = TRUE, si2 = TRUE, si3 = TRUE;
1547 int max_results = -1;
1549 while (!scm_is_null (query_scm))
1556 pair = SCM_CAR (query_scm);
1557 query_scm = SCM_CDR (query_scm);
1559 if (!scm_is_pair (pair))
1565 sym_scm = SCM_CAR (pair);
1566 value = SCM_CADR (pair);
1568 if (!scm_is_symbol (sym_scm))
1574 symbol = gnc_scm_symbol_to_locale_string (sym_scm);
1581 if (!g_strcmp0 (
"terms", symbol))
1586 q = gnc_scm2query_or_terms (value, gnc_QUERY_v2);
1594 else if (!g_strcmp0 (
"search-for", symbol))
1596 if (!scm_is_symbol (value))
1601 search_for = gnc_scm_symbol_to_locale_string (value);
1604 else if (g_strcmp0 (
"primary-sort", symbol) == 0)
1606 if (! gnc_query_scm2sort (value, &sp1, &so1, &si1))
1613 else if (!g_strcmp0 (
"secondary-sort", symbol))
1615 if (! gnc_query_scm2sort (value, &sp2, &so2, &si2))
1622 else if (!g_strcmp0 (
"tertiary-sort", symbol))
1624 if (! gnc_query_scm2sort (value, &sp3, &so3, &si3))
1631 else if (!g_strcmp0 (
"max-results", symbol))
1633 if (!scm_is_number (value))
1639 max_results = scm_to_int (value);
1651 if (ok && search_for)
1655 qof_query_set_sort_options (q, so1, so2, so3);
1665 g_free (search_for);
1671 gnc_scm2query (SCM query_scm)
1678 if (!scm_is_list (query_scm) || scm_is_null (query_scm))
1682 q_type = SCM_CAR (query_scm);
1684 if (!scm_is_symbol (q_type))
1686 if (scm_is_pair (q_type))
1689 return gnc_scm2query_v1 (query_scm);
1698 type = gnc_scm_symbol_to_locale_string (q_type);
1702 if (!g_strcmp0 (type,
"query-v2"))
1703 q = gnc_scm2query_v2 (SCM_CDR (query_scm));
1710 gnc_scm_to_numeric(SCM gncnum)
1715 if (!scm_is_number (gncnum))
1718 num = scm_numerator (gncnum);
1719 denom = scm_denominator (gncnum);
1722 if (!scm_is_signed_integer (num, INT64_MIN, INT64_MAX) ||
1723 !scm_is_signed_integer (denom, INT64_MIN, INT64_MAX))
1726 return gnc_numeric_create (scm_to_int64 (num), scm_to_int64 (denom));
1730 gnc_numeric_to_scm(gnc_numeric arg)
1733 scm_divide (scm_from_int64 (arg.num), scm_from_int64 (arg.denom));
1737 gnc_generic_to_scm(
const void *cx,
const gchar *type_str)
1739 swig_type_info * stype =
nullptr;
1740 void *x = (
void*) cx;
1742 if (!x)
return SCM_BOOL_F;
1743 stype = SWIG_TypeQuery(type_str);
1747 PERR(
"Unknown SWIG Type: %s ", type_str);
1751 return SWIG_NewPointerObj(x, stype, 0);
1755 gnc_scm_to_generic(SCM scm,
const gchar *type_str)
1757 swig_type_info * stype =
nullptr;
1759 stype = SWIG_TypeQuery(type_str);
1762 PERR(
"Unknown SWIG Type: %s ", type_str);
1766 if (!SWIG_IsPointerOfType(scm, stype))
1769 return SWIG_MustGetPtr(scm, stype, 1, 0);
1773 gnc_scm_to_commodity(SCM scm)
1775 return static_cast<gnc_commodity*
>(gnc_scm_to_generic(scm,
"_p_gnc_commodity"));
1779 gnc_commodity_to_scm (
const gnc_commodity *commodity)
1781 return gnc_generic_to_scm(commodity,
"_p_gnc_commodity");
1785 gnc_book_to_scm (
const QofBook *book)
1787 return gnc_generic_to_scm(book,
"_p_QofBook");
1790 static swig_type_info *
1793 static swig_type_info * account_type =
nullptr;
1796 account_type = SWIG_TypeQuery(
"_p_Account");
1798 return account_type;
1801 GncAccountValue * gnc_scm_to_account_value_ptr (SCM valuearg)
1803 GncAccountValue *res;
1806 swig_type_info * account_type = get_acct_type();
1810 val = SCM_CAR (valuearg);
1811 if (!SWIG_IsPointerOfType (val, account_type))
1814 acc =
static_cast<Account*
>(SWIG_MustGetPtr(val, account_type, 1, 0));
1817 val = SCM_CDR (valuearg);
1818 value = gnc_scm_to_numeric (val);
1821 res = g_new0 (GncAccountValue, 1);
1827 SCM gnc_account_value_ptr_to_scm (GncAccountValue *av)
1829 swig_type_info * account_type = get_acct_type();
1830 gnc_commodity * com;
1833 if (!av)
return SCM_BOOL_F;
1839 return scm_cons (SWIG_NewPointerObj(av->account, account_type, 0),
1840 gnc_numeric_to_scm (val));
1851 delete_scm_hook (gpointer data)
1854 scm_gc_unprotect_object(scm->proc);
1861 ENTER(
"data %p, cbarg %p", data, scm);
1863 if (scm->num_args == 0)
1864 scm_call_0 (scm->proc);
1868 scm_call_1 (scm->proc,
1869 SWIG_NewPointerObj(data, SWIG_TypeQuery(
"_p_QofSession"), 0));
1876 gnc_hook_add_scm_dangler (
const gchar *name, SCM proc)
1881 ENTER(
"list %s, proc ???", name);
1882 num_args = gnc_hook_num_args(name);
1883 g_return_if_fail(num_args >= 0);
1885 scm_gc_protect_object(proc);
1887 scm->num_args = num_args;
1888 gnc_hook_add_dangler(name, (GFunc)scm_hook_cb,
1889 (GDestroyNotify) delete_scm_hook, scm);
1894 gnc_parse_time_to_time64 (
const gchar *s,
const gchar *format)
1898 g_return_val_if_fail(s && format, -1);
1900 if (!strptime(s, format, &tm))
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.
int gnc_commodity_get_fraction(const gnc_commodity *cm)
Retrieve the fraction for the specified commodity.
Date and Time handling routines.
#define PINFO(format, args...)
Print an informational note.
void qof_query_set_sort_order(QofQuery *q, QofQueryParamList *params1, QofQueryParamList *params2, QofQueryParamList *params3)
When a query is run, the results are sorted before being returned.
gboolean string_to_guid(const gchar *string, GncGUID *guid)
Given a string, replace the given guid with the parsed one unless the given value is null...
GDate time64_to_gdate(time64 t)
Returns the GDate in which the time64 occurs.
Intermediate result overflow.
gchar * guid_to_string_buff(const GncGUID *guid, gchar *str)
The guid_to_string_buff() routine puts a null-terminated string encoding of the id into the memory po...
QofStringMatch
List of known core query data-types...
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.
These expect a single object and expect the QofAccessFunc returns GncGUID*.
#define PERR(format, args...)
Log a serious error.
#define ENTER(format, args...)
Print a function entry debugging message.
const char * qof_string_cache_insert(const char *key)
You can use this function with g_hash_table_insert(), for the key (or value), as long as you use the ...
void qof_query_set_max_results(QofQuery *q, int n)
Set the maximum number of results that should be returned.
#define PWARN(format, args...)
Log a warning.
const gchar * QofIdType
QofIdType declaration.
Account handling public routines.
gnc_numeric gnc_numeric_convert(gnc_numeric n, gint64 denom, gint how)
Change the denominator of a gnc_numeric value to the specified denominator under standard arguments '...
void qof_query_destroy(QofQuery *query)
Frees the resources associate with a Query object.
GncGUID * guid_malloc(void)
Allocate memory for a GUID.
#define GUID_ENCODING_LENGTH
Number of characters needed to encode a guid as a string not including the null terminator.
gnc_numeric gnc_numeric_error(GNCNumericErrorCode error_code)
Create a gnc_numeric object that signals the error condition noted by error_code, rather than a numbe...
Argument is not a valid number.
time64 gnc_mktime(struct tm *time)
calculate seconds from the epoch given a time struct
QofQuery * qof_query_merge(QofQuery *q1, QofQuery *q2, QofQueryOp op)
Combine two queries together using the Boolean set (logical) operator 'op'.
QofQueryCompare
Standard Query comparators, for how to compare objects in a predicate.
QofCharMatch
A CHAR type is for a RECNCell, Comparisons for QOF_TYPE_CHAR 'ANY' will match any character in the st...
QofQuery * qof_query_invert(QofQuery *q)
Make a copy of the indicated query, inverting the sense of the search.
All type declarations for the whole Gnucash engine.
These expect a GList* of objects and calls the QofAccessFunc routine on each item in the list to obta...
gnc_commodity * xaccAccountGetCommodity(const Account *acc)
Get the account's commodity.
const GncGUID * guid_null(void)
Returns a GncGUID which is guaranteed to never reference any entity.
QofIdType qof_query_get_search_for(const QofQuery *q)
Return the type of data we're querying for.
QofDateMatch
Comparisons for QOF_TYPE_DATE The QOF_DATE_MATCH_DAY comparison rounds the two time values to mid-day...
#define LEAVE(format, args...)
Print a function exit debugging message.
#define QUERY_DEFAULT_SORT
Default sort object type.
Round to the nearest integer, rounding away from zero when there are two equidistant nearest integers...
GNCNumericErrorCode gnc_numeric_check(gnc_numeric a)
Check for error signal in value.
gint64 time64
Most systems that are currently maintained, including Microsoft Windows, BSD-derived Unixes and Linux...
The type used to store guids in C.
QofNumericMatch
Comparisons for QOF_TYPE_NUMERIC, QOF_TYPE_DEBCRED.
QofQuery * qof_query_create(void)
Create a new query.
void qof_query_search_for(QofQuery *q, QofIdTypeConst obj_type)
Set the object type to be searched for.
void qof_string_cache_remove(const char *key)
You can use this function as a destroy notifier for a GHashTable that uses common strings as keys (or...