|
GNCPriceDB * | gnc_pricedb_get_db (QofBook *book) |
|
GNCPriceDB * | gnc_collection_get_pricedb (QofCollection *col) |
|
void | gnc_pricedb_destroy (GNCPriceDB *db) |
|
void | gnc_pricedb_begin_edit (GNCPriceDB *) |
|
void | gnc_pricedb_commit_edit (GNCPriceDB *) |
|
void | gnc_pricedb_set_bulk_update (GNCPriceDB *db, gboolean bulk_update) |
|
gboolean | gnc_pricedb_add_price (GNCPriceDB *db, GNCPrice *p) |
|
gboolean | gnc_pricedb_remove_price (GNCPriceDB *db, GNCPrice *p) |
|
gboolean | gnc_pricedb_remove_old_prices (GNCPriceDB *db, Timespec cutoff, const gboolean delete_user, gboolean delete_last) |
|
GNCPrice * | gnc_pricedb_lookup_latest (GNCPriceDB *db, const gnc_commodity *commodity, const gnc_commodity *currency) |
|
PriceList * | gnc_pricedb_lookup_latest_any_currency (GNCPriceDB *db, const gnc_commodity *commodity) |
|
gboolean | gnc_pricedb_has_prices (GNCPriceDB *db, const gnc_commodity *commodity, const gnc_commodity *currency) |
|
PriceList * | gnc_pricedb_get_prices (GNCPriceDB *db, const gnc_commodity *commodity, const gnc_commodity *currency) |
|
PriceList * | gnc_pricedb_lookup_at_time (GNCPriceDB *db, const gnc_commodity *commodity, const gnc_commodity *currency, Timespec t) |
|
GNCPrice * | gnc_pricedb_lookup_day (GNCPriceDB *db, const gnc_commodity *commodity, const gnc_commodity *currency, Timespec t) |
|
GNCPrice * | gnc_pricedb_lookup_nearest_in_time (GNCPriceDB *db, const gnc_commodity *c, const gnc_commodity *currency, Timespec t) |
|
PriceList * | gnc_pricedb_lookup_nearest_in_time_any_currency (GNCPriceDB *db, const gnc_commodity *c, Timespec t) |
|
GNCPrice * | gnc_pricedb_lookup_latest_before (GNCPriceDB *db, gnc_commodity *c, gnc_commodity *currency, Timespec t) |
|
PriceList * | gnc_pricedb_lookup_latest_before_any_currency (GNCPriceDB *db, gnc_commodity *c, Timespec t) |
|
gnc_numeric | gnc_pricedb_convert_balance_latest_price (GNCPriceDB *pdb, gnc_numeric balance, const gnc_commodity *balance_currency, const gnc_commodity *new_currency) |
|
gnc_numeric | gnc_pricedb_convert_balance_nearest_price (GNCPriceDB *pdb, gnc_numeric balance, const gnc_commodity *balance_currency, const gnc_commodity *new_currency, Timespec t) |
|
gboolean | gnc_pricedb_foreach_price (GNCPriceDB *db, gboolean(*f)(GNCPrice *p, gpointer user_data), gpointer user_data, gboolean stable_order) |
|
guint | gnc_pricedb_get_num_prices (GNCPriceDB *db) |
|
gboolean | gnc_pricedb_equal (GNCPriceDB *db1, GNCPriceDB *db2) |
|
|
For use with QofQuery
|
#define | PRICE_COMMODITY "price-commodity" |
|
#define | PRICE_CURRENCY "price-currency" |
|
#define | PRICE_DATE "price-date" |
|
#define | PRICE_SOURCE "price-source" |
|
#define | PRICE_TYPE "price-type" |
|
#define | PRICE_VALUE "price-value" |
|
The PriceDB is intended to be a database of price quotes, or more specifically, a database of GNCPrices. For the time being, it is still a fairly simple database supporting only fairly simple queries. It is expected that new queries will be added as needed, and that there is some advantage to delaying complex queries for now in the hope that we get a real DB implementation before they're really needed.
Every QofBook contains a GNCPriceDB, accessible via gnc_pricedb_get_db.
- Warning
- The PriceDB does not currently use the object system used elsewhere in the GnuCash Engine, i.e. it does not use GUISD's, Entities and Collections. It should. In particular, this means that currently prices cannot be queried with the same mechanism as everything else.
Whenever a you store a price in the pricedb, the pricedb adds its own reference to the price, so you can safely unref that price after inserting it into the DB if you're finished with it otherwise.
Similarly, when the pricedb returns a price to you, either singly, or in a price list, the price will have had a ref added for you, so you only need to unref the price(s) when you're finished with it/them.
GNCPriceDB* gnc_collection_get_pricedb |
( |
QofCollection * |
col | ) |
|
- Todo:
- Collections of prices are not destroyed fully.
- gnc_pricedb_destroy does not clean up properly because gnc_pricedb_create reports an existing PriceDB after running gnc_pricedb_destroy. To change the pricedb, we need to destroy and recreate the book. Yuk.
Definition at line 865 of file gnc-pricedb.c.
867 if (!col)
return NULL;
gpointer qof_collection_get_data(const QofCollection *col)
gnc_pricedb_add_price - add a price to the pricedb, you may drop your reference to the price (i.e. call unref) after this succeeds, whenever you're finished with the price.
Definition at line 1053 of file gnc-pricedb.c.
1055 if (!db || !p)
return FALSE;
1057 ENTER (
"db=%p, pr=%p dirty=%d destroying=%d",
1061 if (FALSE == add_price(db, p))
1063 LEAVE (
" failed to add price");
1068 qof_instance_set_dirty(&db->inst);
1069 gnc_pricedb_commit_edit(db);
1071 LEAVE (
"db=%p, pr=%p dirty=%d destroying=%d",
gboolean qof_instance_get_destroying(gconstpointer ptr)
#define ENTER(format, args...)
gboolean qof_instance_get_dirty_flag(gconstpointer ptr)
void gnc_pricedb_begin_edit(GNCPriceDB *pdb)
#define LEAVE(format, args...)
Used for editing the pricedb en-mass
Definition at line 358 of file gnc-pricedb.c.
gboolean qof_begin_edit(QofInstance *inst)
gnc_pricedb_convert_balance_latest_price - Convert a balance from one currency to another.
Definition at line 2013 of file gnc-pricedb.c.
2019 GList *price_list, *list_helper;
2058 balance = gnc_numeric_zero ();
2062 list_helper = price_list;
2063 currency_price_value = gnc_numeric_zero();
2067 price = (
GNCPrice *)(list_helper->data);
2069 intermediate_currency = gnc_price_get_currency(price);
2074 currency_price_value = gnc_price_get_value(currency_price);
2080 intermediate_currency);
2085 gnc_price_get_value(currency_price),
2092 list_helper = list_helper->next;
2094 while ((list_helper != NULL) &&
void gnc_price_list_destroy(PriceList *prices)
int gnc_commodity_get_fraction(const gnc_commodity *cm)
void gnc_price_unref(GNCPrice *p)
gboolean gnc_numeric_zero_p(gnc_numeric a)
gnc_numeric gnc_numeric_mul(gnc_numeric a, gnc_numeric b, gint64 denom, gint how)
PriceList * gnc_pricedb_lookup_latest_any_currency(GNCPriceDB *db, const gnc_commodity *commodity)
gnc_numeric gnc_numeric_div(gnc_numeric x, gnc_numeric y, gint64 denom, gint how)
GNCPrice * gnc_pricedb_lookup_latest(GNCPriceDB *db, const gnc_commodity *commodity, const gnc_commodity *currency)
gboolean gnc_commodity_equiv(const gnc_commodity *a, const gnc_commodity *b)
gnc_pricedb_convert_balance_nearest_price - Convert a balance from one currency to another.
Definition at line 2109 of file gnc-pricedb.c.
2116 GList *price_list, *list_helper;
2155 balance = gnc_numeric_zero ();
2159 list_helper = price_list;
2160 currency_price_value = gnc_numeric_zero();
2164 price = (
GNCPrice *)(list_helper->data);
2166 intermediate_currency = gnc_price_get_currency(price);
2171 currency_price_value = gnc_price_get_value(currency_price);
2177 intermediate_currency, t);
2182 gnc_price_get_value(currency_price),
2189 list_helper = list_helper->next;
2191 while ((list_helper != NULL) &&
void gnc_price_list_destroy(PriceList *prices)
int gnc_commodity_get_fraction(const gnc_commodity *cm)
PriceList * gnc_pricedb_lookup_nearest_in_time_any_currency(GNCPriceDB *db, const gnc_commodity *c, Timespec t)
void gnc_price_unref(GNCPrice *p)
gboolean gnc_numeric_zero_p(gnc_numeric a)
gnc_numeric gnc_numeric_mul(gnc_numeric a, gnc_numeric b, gint64 denom, gint how)
GNCPrice * gnc_pricedb_lookup_nearest_in_time(GNCPriceDB *db, const gnc_commodity *c, const gnc_commodity *currency, Timespec t)
gnc_numeric gnc_numeric_div(gnc_numeric x, gnc_numeric y, gint64 denom, gint how)
gboolean gnc_commodity_equiv(const gnc_commodity *a, const gnc_commodity *b)
gnc_pricedb_destroy - destroy the given pricedb and unref all of the prices it contains. This may not deallocate all of those prices. Other code may still be holding references to them.
Definition at line 828 of file gnc-pricedb.c.
831 if (db->commodity_hash)
833 g_hash_table_foreach (db->commodity_hash,
834 destroy_pricedb_commodity_hash_data,
837 g_hash_table_destroy (db->commodity_hash);
838 db->commodity_hash = NULL;
gnc_pricedb_equal - test equality of two pricedbs
Definition at line 949 of file gnc-pricedb.c.
953 if (db1 == db2)
return TRUE;
957 PWARN (
"one is NULL");
961 equal_data.equal = TRUE;
962 equal_data.db2 = db2;
964 g_hash_table_foreach (db1->commodity_hash,
965 pricedb_equal_foreach_currencies_hash,
968 return equal_data.equal;
#define PWARN(format, args...)
gboolean gnc_pricedb_foreach_price |
( |
GNCPriceDB * |
db, |
|
|
gboolean(*)(GNCPrice *p, gpointer user_data) |
f, |
|
|
gpointer |
user_data, |
|
|
gboolean |
stable_order |
|
) |
| |
gnc_pricedb_foreach_price - call f once for each price in db, until and unless f returns FALSE. If stable_order is not FALSE, make sure the ordering of the traversal is stable (i.e. the same order every time given the same db contents – stable traversals may be less efficient).
Definition at line 2344 of file gnc-pricedb.c.
2349 ENTER (
"db=%p f=%p", db, f);
2352 LEAVE (
" stable order found");
2353 return stable_price_traversal(db, f, user_data);
2355 LEAVE (
" use unstable order");
2356 return unstable_price_traversal(db, f, user_data);
#define ENTER(format, args...)
#define LEAVE(format, args...)
return the pricedb associated with the book
Definition at line 872 of file gnc-pricedb.c.
876 if (!book)
return NULL;
GNCPriceDB * gnc_collection_get_pricedb(QofCollection *col)
QofCollection * qof_book_get_collection(const QofBook *, QofIdType)
guint gnc_pricedb_get_num_prices |
( |
GNCPriceDB * |
db | ) |
|
gnc_pricedb_get_num_prices - return the number of prices in the database.
Definition at line 894 of file gnc-pricedb.c.
gboolean gnc_pricedb_foreach_price(GNCPriceDB *db, gboolean(*f)(GNCPrice *p, gpointer user_data), gpointer user_data, gboolean stable_order)
gnc_pricedb_get_prices - return all the prices for a given commodity in the given currency. Returns NULL on failure. The result is a GNCPrice list (see above).
Definition at line 1479 of file gnc-pricedb.c.
1486 GHashTable *currency_hash;
1490 if (!db || !commodity)
return NULL;
1491 ENTER (
"db=%p commodity=%p currency=%p", db, commodity, currency);
1494 #ifdef GNUCASH_MAJOR_VERSION
1498 pl.type = LOOKUP_ALL;
1500 pl.commodity = commodity;
1501 pl.currency = currency;
1505 currency_hash = g_hash_table_lookup(db->commodity_hash, commodity);
1508 LEAVE (
" no currency hash");
1514 price_list = g_hash_table_lookup(currency_hash, currency);
1517 LEAVE (
" no price list");
1520 result = g_list_copy (price_list);
1525 g_hash_table_foreach(currency_hash, hash_values_helper, (gpointer)&result);
1527 for (node = result; node; node = node->next)
QofBook * qof_instance_get_book(gconstpointer)
void(* price_lookup)(QofBackend *, gpointer)
#define ENTER(format, args...)
#define LEAVE(format, args...)
QofBackend * qof_book_get_backend(const QofBook *book)
Retrieve the backend used by this book.
void gnc_price_ref(GNCPrice *p)
gnc_pricedb_has_prices - return an indication of whether or not there are any prices for a given commodity in the given currency. Returns TRUE if there are prices, FALSE otherwise.
Definition at line 1428 of file gnc-pricedb.c.
1433 GHashTable *currency_hash;
1438 if (!db || !commodity)
return FALSE;
1439 ENTER (
"db=%p commodity=%p currency=%p", db, commodity, currency);
1442 #ifdef GNUCASH_MAJOR_VERSION
1446 pl.type = LOOKUP_ALL;
1448 pl.commodity = commodity;
1449 pl.currency = currency;
1453 currency_hash = g_hash_table_lookup(db->commodity_hash, commodity);
1456 LEAVE(
"no, no currency_hash table");
1462 price_list = g_hash_table_lookup(currency_hash, currency);
1468 LEAVE(
"no, no price list");
1472 size = g_hash_table_size (currency_hash);
1473 LEAVE(
"%s", size > 0 ?
"yes" :
"no");
QofBook * qof_instance_get_book(gconstpointer)
void(* price_lookup)(QofBackend *, gpointer)
#define ENTER(format, args...)
#define LEAVE(format, args...)
QofBackend * qof_book_get_backend(const QofBook *book)
Retrieve the backend used by this book.
gnc_pricedb_lookup_at_time - return all prices that match the given commodity, currency, and timespec. Prices will be returned as a GNCPrice list (see above).
Definition at line 1545 of file gnc-pricedb.c.
1551 GList *result = NULL;
1553 GHashTable *currency_hash;
1557 if (!db || !c || !currency)
return NULL;
1558 ENTER (
"db=%p commodity=%p currency=%p", db, c, currency);
1561 #ifdef GNUCASH_MAJOR_VERSION
1565 pl.type = LOOKUP_AT_TIME;
1568 pl.currency = currency;
1573 currency_hash = g_hash_table_lookup(db->commodity_hash, c);
1576 LEAVE (
" no currency hash");
1580 price_list = g_hash_table_lookup(currency_hash, currency);
1583 LEAVE (
" no price list");
1591 Timespec price_time = gnc_price_get_time(p);
1594 result = g_list_prepend(result, p);
QofBook * qof_instance_get_book(gconstpointer)
void(* price_lookup)(QofBackend *, gpointer)
gboolean timespec_equal(const Timespec *ta, const Timespec *tb)
Use a 64-bit unsigned int timespec.
#define ENTER(format, args...)
#define LEAVE(format, args...)
QofBackend * qof_book_get_backend(const QofBook *book)
Retrieve the backend used by this book.
void gnc_price_ref(GNCPrice *p)
gnc_pricedb_lookup_day - return the price that matchex the given commodity, currency, and timespec which is on the same day. If no prices are on that day, returns a null value.
Definition at line 1536 of file gnc-pricedb.c.
1541 return lookup_nearest_in_time(db, c, currency, t, TRUE);
gnc_pricedb_lookup_latest - find the most recent price for the given commodity in the given currency. Returns NULL on failure.
Definition at line 1309 of file gnc-pricedb.c.
1315 GHashTable *currency_hash;
1319 if (!db || !commodity || !currency)
return NULL;
1320 ENTER (
"db=%p commodity=%p currency=%p", db, commodity, currency);
1323 #ifdef GNUCASH_MAJOR_VERSION
1327 pl.type = LOOKUP_LATEST;
1329 pl.commodity = commodity;
1330 pl.currency = currency;
1335 currency_hash = g_hash_table_lookup(db->commodity_hash, commodity);
1338 LEAVE (
" no currency hash");
1342 price_list = g_hash_table_lookup(currency_hash, currency);
1345 LEAVE (
" no price list");
1352 result = price_list->data;
QofBook * qof_instance_get_book(gconstpointer)
void(* price_lookup)(QofBackend *, gpointer)
#define ENTER(format, args...)
#define LEAVE(format, args...)
QofBackend * qof_book_get_backend(const QofBook *book)
Retrieve the backend used by this book.
void gnc_price_ref(GNCPrice *p)
gnc_pricedb_lookup_latest_any_currency - find the most recent prices for the given commodity in any available currency. Prices will be returned as a GNCPrice list (see above).
Definition at line 1373 of file gnc-pricedb.c.
1377 GHashTable *currency_hash;
1383 if (!db || !commodity)
return NULL;
1384 ENTER (
"db=%p commodity=%p", db, commodity);
1387 #ifdef GNUCASH_MAJOR_VERSION
1391 pl.type = LOOKUP_LATEST;
1393 pl.commodity = commodity;
1398 currency_hash = g_hash_table_lookup(db->commodity_hash, commodity);
1401 LEAVE (
" no currency hash");
1405 g_hash_table_foreach(currency_hash, lookup_latest, &result);
1413 result = g_list_sort(result, compare_prices_by_date);
QofBook * qof_instance_get_book(gconstpointer)
void(* price_lookup)(QofBackend *, gpointer)
#define ENTER(format, args...)
#define LEAVE(format, args...)
QofBackend * qof_book_get_backend(const QofBook *book)
Retrieve the backend used by this book.
gnc_pricedb_lookup_latest_before - return the latest price for the given commodity in the given currency up to and including time t.
Definition at line 1753 of file gnc-pricedb.c.
1763 GHashTable *currency_hash;
1768 if (!db || !c || !currency)
return NULL;
1769 ENTER (
"db=%p commodity=%p currency=%p", db, c, currency);
1772 #ifdef GNUCASH_MAJOR_VERSION
1776 pl.type = LOOKUP_LATEST_BEFORE;
1779 pl.currency = currency;
1784 currency_hash = g_hash_table_lookup(db->commodity_hash, c);
1787 LEAVE (
"no currency hash");
1791 price_list = g_hash_table_lookup(currency_hash, currency);
1794 LEAVE (
"no price list");
1801 price_time = gnc_price_get_time (item->data);
1803 current_price = item->data;
1809 return current_price;
QofBook * qof_instance_get_book(gconstpointer)
void(* price_lookup)(QofBackend *, gpointer)
Use a 64-bit unsigned int timespec.
#define ENTER(format, args...)
gint timespec_cmp(const Timespec *ta, const Timespec *tb)
#define LEAVE(format, args...)
QofBackend * qof_book_get_backend(const QofBook *book)
Retrieve the backend used by this book.
void gnc_price_ref(GNCPrice *p)
gnc_pricedb_lookup_latest_before_any_currency - return recent prices that match the given commodity up to and including time t in any available currency. Prices will be returned as a GNCPrice list (see above).
Definition at line 1959 of file gnc-pricedb.c.
1963 GList *result = NULL;
1964 GHashTable *currency_hash;
1969 if (!db || !c)
return NULL;
1970 ENTER (
"db=%p commodity=%p", db, c);
1973 #ifdef GNUCASH_MAJOR_VERSION
1977 pl.type = LOOKUP_LATEST_BEFORE;
1985 currency_hash = g_hash_table_lookup(db->commodity_hash, c);
1988 LEAVE (
" no currency hash");
1992 lookup_helper.return_list = &result;
1993 lookup_helper.time = t;
1994 g_hash_table_foreach(currency_hash, lookup_latest_before, &lookup_helper);
2002 result = g_list_sort(result, compare_prices_by_date);
QofBook * qof_instance_get_book(gconstpointer)
void(* price_lookup)(QofBackend *, gpointer)
#define ENTER(format, args...)
#define LEAVE(format, args...)
QofBackend * qof_book_get_backend(const QofBook *book)
Retrieve the backend used by this book.
gnc_pricedb_lookup_nearest_in_time - return the price for the given commodity in the given currency nearest to the given time t.
Definition at line 1744 of file gnc-pricedb.c.
1749 return lookup_nearest_in_time(db, c, currency, t, FALSE);
gnc_pricedb_lookup_nearest_in_time_any_currency - return all prices that match the given commodity and timespec in any available currency. Prices will be returned as a GNCPrice list (see above).
Definition at line 1908 of file gnc-pricedb.c.
1912 GList *result = NULL;
1913 GHashTable *currency_hash;
1918 if (!db || !c)
return NULL;
1919 ENTER (
"db=%p commodity=%p", db, c);
1922 #ifdef GNUCASH_MAJOR_VERSION
1926 pl.type = LOOKUP_NEAREST_IN_TIME;
1934 currency_hash = g_hash_table_lookup(db->commodity_hash, c);
1937 LEAVE (
" no currency hash");
1941 lookup_helper.return_list = &result;
1942 lookup_helper.time = t;
1943 g_hash_table_foreach(currency_hash, lookup_nearest, &lookup_helper);
1951 result = g_list_sort(result, compare_prices_by_date);
QofBook * qof_instance_get_book(gconstpointer)
void(* price_lookup)(QofBackend *, gpointer)
#define ENTER(format, args...)
#define LEAVE(format, args...)
QofBackend * qof_book_get_backend(const QofBook *book)
Retrieve the backend used by this book.
void gnc_pricedb_print_contents |
( |
GNCPriceDB * |
db, |
|
|
FILE * |
f |
|
) |
| |
This simple function can be useful for debugging the pricedb code
Definition at line 2480 of file gnc-pricedb.c.
2484 PERR(
"NULL PriceDB\n");
2489 PERR(
"NULL FILE*\n");
2493 fprintf(f,
"<gnc:pricedb>\n");
2495 fprintf(f,
"</gnc:pricedb>\n");
#define PERR(format, args...)
gboolean gnc_pricedb_foreach_price(GNCPriceDB *db, gboolean(*f)(GNCPrice *p, gpointer user_data), gpointer user_data, gboolean stable_order)
gnc_pricedb_remove_price - removes the given price, p, from the pricedb. Returns TRUE if successful, FALSE otherwise.
Definition at line 1160 of file gnc-pricedb.c.
1163 if (!db || !p)
return FALSE;
1164 ENTER (
"db=%p, pr=%p dirty=%d destroying=%d",
1169 rc = remove_price (db, p, TRUE);
1171 qof_instance_set_dirty(&db->inst);
1172 gnc_pricedb_commit_edit(db);
1175 gnc_price_begin_edit (p);
1176 qof_instance_set_destroying(p, TRUE);
1177 gnc_price_commit_edit (p);
1180 LEAVE (
"db=%p, pr=%p", db, p);
void gnc_price_unref(GNCPrice *p)
gboolean qof_instance_get_destroying(gconstpointer ptr)
#define ENTER(format, args...)
gboolean qof_instance_get_dirty_flag(gconstpointer ptr)
void gnc_pricedb_begin_edit(GNCPriceDB *pdb)
#define LEAVE(format, args...)
void gnc_price_ref(GNCPrice *p)
void gnc_pricedb_set_bulk_update |
( |
GNCPriceDB * |
db, |
|
|
gboolean |
bulk_update |
|
) |
| |
Indicate whether or not the database is in the middle of a bulk update. Setting this flag will disable checks for duplicate entries.
Definition at line 844 of file gnc-pricedb.c.
846 db->bulk_update = bulk_update;