GnuCash fec5643+
Loading...
Searching...
No Matches
Files | Typedefs | Enumerations | Functions

The PriceDB is intended to be a database of price quotes, or more specifically, a database of GNCPrices. More...

Files

file  gnc-pricedb.h
 a simple price database for gnucash
 

Typedefs

typedef gboolean(* GncPriceForeachFunc) (GNCPrice *p, gpointer user_data)
 

Enumerations

enum  PriceRemoveSourceFlags { PRICE_REMOVE_SOURCE_FQ = 1 , PRICE_REMOVE_SOURCE_USER = 2 , PRICE_REMOVE_SOURCE_APP = 4 , PRICE_REMOVE_SOURCE_COMM = 8 }
 
enum  PriceRemoveKeepOptions {
  PRICE_REMOVE_KEEP_NONE , PRICE_REMOVE_KEEP_LAST_WEEKLY , PRICE_REMOVE_KEEP_LAST_MONTHLY , PRICE_REMOVE_KEEP_LAST_QUARTERLY ,
  PRICE_REMOVE_KEEP_LAST_PERIOD , PRICE_REMOVE_KEEP_SCALED
}
 

Functions

GNCPriceDB * gnc_pricedb_get_db (QofBook *book)
 Return the pricedb associated with the book.
 
GNCPriceDB * gnc_collection_get_pricedb (QofCollection *col)
 Return the pricedb via the Book's collection.
 
void gnc_pricedb_destroy (GNCPriceDB *db)
 Destroy the given pricedb and unref all of the prices it contains.
 
void gnc_pricedb_begin_edit (GNCPriceDB *)
 Begin an edit.
 
void gnc_pricedb_commit_edit (GNCPriceDB *)
 Commit an edit.
 
void gnc_pricedb_set_bulk_update (GNCPriceDB *db, gboolean bulk_update)
 Set flag to indicate whether duplication checks should be performed.
 
gboolean gnc_pricedb_add_price (GNCPriceDB *db, GNCPrice *p)
 Add a price to the pricedb.
 
gboolean gnc_pricedb_remove_price (GNCPriceDB *db, GNCPrice *p)
 Remove a price from the pricedb and unref the price.
 
gboolean gnc_pricedb_remove_old_prices (GNCPriceDB *db, GList *comm_list, GDate *fiscal_end_date, time64 cutoff, PriceRemoveSourceFlags source, PriceRemoveKeepOptions keep)
 Remove and unref prices older than a certain time.
 
GNCPrice * gnc_pricedb_lookup_latest (GNCPriceDB *db, const gnc_commodity *commodity, const gnc_commodity *currency)
 Find the most recent price between the two commodities.
 
PriceList * gnc_pricedb_lookup_latest_any_currency (GNCPriceDB *db, const gnc_commodity *commodity)
 Find the most recent price between a commodity and all other commodities.
 
gboolean gnc_pricedb_has_prices (GNCPriceDB *db, const gnc_commodity *commodity, const gnc_commodity *currency)
 Report whether the pricedb contains prices for one commodity in another.
 
PriceList * gnc_pricedb_get_prices (GNCPriceDB *db, const gnc_commodity *commodity, const gnc_commodity *currency)
 Return all the prices for a given commodity in another.
 
GNCPrice * gnc_pricedb_lookup_day_t64 (GNCPriceDB *db, const gnc_commodity *commodity, const gnc_commodity *currency, time64 t)
 Return the price between the two commodities on the indicated day.
 
GNCPrice * gnc_pricedb_lookup_nearest_in_time64 (GNCPriceDB *db, const gnc_commodity *c, const gnc_commodity *currency, time64 t)
 Return the price between the two commoditiesz nearest to the given time.
 
PriceList * gnc_pricedb_lookup_nearest_in_time_any_currency_t64 (GNCPriceDB *db, const gnc_commodity *c, time64 t)
 Return the price nearest in time to that given between the given commodity and every other.
 
GNCPrice * gnc_pricedb_lookup_nearest_before_t64 (GNCPriceDB *db, const gnc_commodity *c, const gnc_commodity *currency, time64 t)
 Return the nearest price between the given commodities before the given time.
 
PriceList * gnc_pricedb_lookup_nearest_before_any_currency_t64 (GNCPriceDB *db, const gnc_commodity *c, time64 t)
 Return the nearest price between the given commodity and any other before the given time.
 
gnc_numeric gnc_pricedb_get_nearest_before_price (GNCPriceDB *pdb, const gnc_commodity *orig_currency, const gnc_commodity *new_currency, const time64 t)
 Retrieve the price one currency to another using the price nearest to before the given time.
 
gnc_numeric gnc_pricedb_get_nearest_price (GNCPriceDB *pdb, const gnc_commodity *orig_currency, const gnc_commodity *new_currency, const time64 t)
 Retrieve the price one currency to another using the price nearest to the given time.
 
gnc_numeric gnc_pricedb_get_latest_price (GNCPriceDB *pdb, const gnc_commodity *orig_currency, const gnc_commodity *new_currency)
 Retrieve the price one currency to another using the latest price.
 
gnc_numeric gnc_pricedb_convert_balance_latest_price (GNCPriceDB *pdb, gnc_numeric balance, const gnc_commodity *balance_currency, const gnc_commodity *new_currency)
 Convert a balance from one currency to another using the most recent price between the two.
 
gnc_numeric gnc_pricedb_convert_balance_nearest_price_t64 (GNCPriceDB *pdb, gnc_numeric balance, const gnc_commodity *balance_currency, const gnc_commodity *new_currency, time64 t)
 Convert a balance from one currency to another using the price nearest to the given time.
 
gnc_numeric gnc_pricedb_convert_balance_nearest_before_price_t64 (GNCPriceDB *pdb, gnc_numeric balance, const gnc_commodity *balance_currency, const gnc_commodity *new_currency, time64 t)
 Convert a balance from one currency to another using the price nearest to before the given time.
 
gboolean gnc_pricedb_foreach_price (GNCPriceDB *db, GncPriceForeachFunc f, gpointer user_data, gboolean stable_order)
 Call a GncPriceForeachFunction once for each price in db, until the function returns FALSE.
 
int gnc_pricedb_num_prices (GNCPriceDB *db, const gnc_commodity *c)
 Get the number of prices, in any currency, for a given commodity.
 
GNCPrice * gnc_pricedb_nth_price (GNCPriceDB *db, const gnc_commodity *c, const int n)
 Get the nth price for the given commodity in reverse date order.
 
void gnc_pricedb_nth_price_reset_cache (GNCPriceDB *db)
 
guint gnc_pricedb_get_num_prices (GNCPriceDB *db)
 Return the number of prices in the database.
 
gboolean gnc_pricedb_equal (GNCPriceDB *db1, GNCPriceDB *db2)
 Test equality of two pricedbs.
 

Internal/Debugging

void gnc_pricedb_print_contents (GNCPriceDB *db, FILE *f)
 This simple function can be useful for debugging the pricedb code.
 

Price Parameter Names

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"
 

Detailed Description

The PriceDB is intended to be a database of price quotes, or more specifically, a database of GNCPrices.

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.

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.

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.

Macro Definition Documentation

◆ PRICE_COMMODITY

#define PRICE_COMMODITY   "price-commodity"

Definition at line 700 of file gnc-pricedb.h.

◆ PRICE_CURRENCY

#define PRICE_CURRENCY   "price-currency"

Definition at line 701 of file gnc-pricedb.h.

◆ PRICE_DATE

#define PRICE_DATE   "price-date"

Definition at line 702 of file gnc-pricedb.h.

◆ PRICE_SOURCE

#define PRICE_SOURCE   "price-source"

Definition at line 703 of file gnc-pricedb.h.

◆ PRICE_TYPE

#define PRICE_TYPE   "price-type"

Definition at line 704 of file gnc-pricedb.h.

◆ PRICE_VALUE

#define PRICE_VALUE   "price-value"

Definition at line 705 of file gnc-pricedb.h.

Typedef Documentation

◆ GncPriceForeachFunc

typedef gboolean(* GncPriceForeachFunc) (GNCPrice *p, gpointer user_data)

Definition at line 634 of file gnc-pricedb.h.

Enumeration Type Documentation

◆ PriceRemoveKeepOptions

enum PriceRemoveKeepOptions

Definition at line 389 of file gnc-pricedb.h.

390{
391 PRICE_REMOVE_KEEP_NONE, // keep none
392 PRICE_REMOVE_KEEP_LAST_WEEKLY, // leave last one of every week
393 PRICE_REMOVE_KEEP_LAST_MONTHLY, // leave last one of every month
394 PRICE_REMOVE_KEEP_LAST_QUARTERLY, // leave last one of every quarter
395 PRICE_REMOVE_KEEP_LAST_PERIOD, // leave last one of every annual period
396 PRICE_REMOVE_KEEP_SCALED, // leave one every week then one a month
397} PriceRemoveKeepOptions;

◆ PriceRemoveSourceFlags

enum PriceRemoveSourceFlags

Definition at line 381 of file gnc-pricedb.h.

382{
383 PRICE_REMOVE_SOURCE_FQ = 1, // this flag is set when added by F:Q checked
384 PRICE_REMOVE_SOURCE_USER = 2, // this flag is set when added by the user checked
385 PRICE_REMOVE_SOURCE_APP = 4, // this flag is set when added by the app checked
386 PRICE_REMOVE_SOURCE_COMM = 8, // this flag is set when we have commodities selected
387} PriceRemoveSourceFlags;

Function Documentation

◆ gnc_collection_get_pricedb()

GNCPriceDB * gnc_collection_get_pricedb ( QofCollection *  col)

Return the pricedb via the Book's collection.

Parameters
colThe QofCollection holding the pricedb
Returns
The GNCPriceDB in the QofCollection
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 918 of file gnc-pricedb.cpp.

919{
920 if (!col) return nullptr;
921 return static_cast<GNCPriceDB*>(qof_collection_get_data (col));
922}
gpointer qof_collection_get_data(const QofCollection *col)
Store and retrieve arbitrary object-defined data.
Definition qofid.cpp:266

◆ gnc_pricedb_add_price()

gboolean gnc_pricedb_add_price ( GNCPriceDB *  db,
GNCPrice *  p 
)

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.

Parameters
dbThe pricedb
pThe GNCPrice to add.
Returns
TRUE if the price was added, FALSE otherwise.

Definition at line 1126 of file gnc-pricedb.cpp.

1127{
1128 if (!db || !p) return FALSE;
1129
1130 ENTER ("db=%p, pr=%p dirty=%d destroying=%d",
1133
1134 if (FALSE == add_price(db, p))
1135 {
1136 LEAVE (" failed to add price");
1137 return FALSE;
1138 }
1139
1141 qof_instance_set_dirty(&db->inst);
1143
1144 LEAVE ("db=%p, pr=%p dirty=%d destroying=%d",
1147
1148 return TRUE;
1149}
gboolean qof_instance_get_dirty_flag(gconstpointer ptr)
Retrieve the flag that indicates whether or not this object has been modified.
gboolean qof_instance_get_destroying(gconstpointer ptr)
Retrieve the flag that indicates whether or not this object is about to be destroyed.
#define LEAVE(format, args...)
Print a function exit debugging message.
Definition qoflog.h:282
#define ENTER(format, args...)
Print a function entry debugging message.
Definition qoflog.h:272
void gnc_pricedb_commit_edit(GNCPriceDB *pdb)
Commit an edit.
void gnc_pricedb_begin_edit(GNCPriceDB *pdb)
Begin an edit.

◆ gnc_pricedb_begin_edit()

void gnc_pricedb_begin_edit ( GNCPriceDB *  pdb)

Begin an edit.

Definition at line 434 of file gnc-pricedb.cpp.

435{
436 qof_begin_edit(&pdb->inst);
437}
gboolean qof_begin_edit(QofInstance *inst)
begin_edit

◆ gnc_pricedb_commit_edit()

void gnc_pricedb_commit_edit ( GNCPriceDB *  pdb)

Commit an edit.

Definition at line 440 of file gnc-pricedb.cpp.

441{
442 if (!qof_commit_edit (QOF_INSTANCE(pdb))) return;
443 qof_commit_edit_part2 (&pdb->inst, commit_err, noop, noop);
444}
gboolean qof_commit_edit_part2(QofInstance *inst, void(*on_error)(QofInstance *, QofBackendError), void(*on_done)(QofInstance *), void(*on_free)(QofInstance *))
part2 – deal with the backend
gboolean qof_commit_edit(QofInstance *inst)
commit_edit helpers

◆ gnc_pricedb_convert_balance_latest_price()

gnc_numeric gnc_pricedb_convert_balance_latest_price ( GNCPriceDB *  pdb,
gnc_numeric  balance,
const gnc_commodity *  balance_currency,
const gnc_commodity *  new_currency 
)

Convert a balance from one currency to another using the most recent price between the two.

Parameters
pdbThe pricedb
balanceThe balance to be converted
balance_currencyThe commodity in which the balance is currently expressed
new_currencyThe commodity to which the balance should be converted
Returns
A new balance or gnc_numeric_zero if no price is available.

Definition at line 2571 of file gnc-pricedb.cpp.

2575{
2576 return convert_amount_at_date
2577 (pdb, balance, balance_currency, new_currency, INT64_MAX, FALSE);
2578}

◆ gnc_pricedb_convert_balance_nearest_before_price_t64()

gnc_numeric gnc_pricedb_convert_balance_nearest_before_price_t64 ( GNCPriceDB *  pdb,
gnc_numeric  balance,
const gnc_commodity *  balance_currency,
const gnc_commodity *  new_currency,
time64  t 
)

Convert a balance from one currency to another using the price nearest to before the given time.

Parameters
pdbThe pricedb
balanceThe balance to be converted
balance_currencyThe commodity in which the balance is currently expressed
new_currencyThe commodity to which the balance should be converted
tThe time in which the last price before it should be used.
Returns
A new balance or gnc_numeric_zero if no price is available.

Definition at line 2592 of file gnc-pricedb.cpp.

2597{
2598 return convert_amount_at_date
2599 (pdb, balance, balance_currency, new_currency, t, TRUE);
2600}

◆ gnc_pricedb_convert_balance_nearest_price_t64()

gnc_numeric gnc_pricedb_convert_balance_nearest_price_t64 ( GNCPriceDB *  pdb,
gnc_numeric  balance,
const gnc_commodity *  balance_currency,
const gnc_commodity *  new_currency,
time64  t 
)

Convert a balance from one currency to another using the price nearest to the given time.

Parameters
pdbThe pricedb
balanceThe balance to be converted
balance_currencyThe commodity in which the balance is currently expressed
new_currencyThe commodity to which the balance should be converted
tThe time nearest to which price should be used.
Returns
A new balance or gnc_numeric_zero if no price is available.

Definition at line 2581 of file gnc-pricedb.cpp.

2586{
2587 return convert_amount_at_date
2588 (pdb, balance, balance_currency, new_currency, t, FALSE);
2589}

◆ gnc_pricedb_destroy()

void gnc_pricedb_destroy ( GNCPriceDB *  db)

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.

Parameters
dbThe pricedb to destroy.

Definition at line 881 of file gnc-pricedb.cpp.

882{
883 if (!db) return;
884 if (db->commodity_hash)
885 {
886 g_hash_table_foreach (db->commodity_hash,
887 destroy_pricedb_commodity_hash_data,
888 nullptr);
889 }
890 g_hash_table_destroy (db->commodity_hash);
891 db->commodity_hash = nullptr;
892 /* qof_instance_release (&db->inst); */
893 g_object_unref(db);
894}

◆ gnc_pricedb_equal()

gboolean gnc_pricedb_equal ( GNCPriceDB *  db1,
GNCPriceDB *  db2 
)

Test equality of two pricedbs.

For XML Backend Testing

Definition at line 1000 of file gnc-pricedb.cpp.

1001{
1002 GNCPriceDBEqualData equal_data;
1003
1004 if (db1 == db2) return TRUE;
1005
1006 if (!db1 || !db2)
1007 {
1008 PWARN ("one is nullptr");
1009 return FALSE;
1010 }
1011
1012 equal_data.equal = TRUE;
1013 equal_data.db2 = db2;
1014
1015 g_hash_table_foreach (db1->commodity_hash,
1016 pricedb_equal_foreach_currencies_hash,
1017 &equal_data);
1018
1019 return equal_data.equal;
1020}
#define PWARN(format, args...)
Log a warning.
Definition qoflog.h:250

◆ gnc_pricedb_foreach_price()

gboolean gnc_pricedb_foreach_price ( GNCPriceDB *  db,
GncPriceForeachFunc  f,
gpointer  user_data,
gboolean  stable_order 
)

Call a GncPriceForeachFunction once for each price in db, until the function 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).

Parameters
dbThe pricedb
fThe function to call
user_dataA data to pass to each invocation of f
stable_orderEnsure that the traversal is performed in the same order each time.
Returns
TRUE if all calls to f succeeded (unstable) or if the order of processing was the same as the previous invocation (stable), FALSE otherwise.

Definition at line 2745 of file gnc-pricedb.cpp.

2749{
2750 ENTER ("db=%p f=%p", db, f);
2751 if (stable_order)
2752 {
2753 LEAVE (" stable order found");
2754 return stable_price_traversal(db, f, user_data);
2755 }
2756 LEAVE (" use unstable order");
2757 return unstable_price_traversal(db, f, user_data);
2758}

◆ gnc_pricedb_get_db()

GNCPriceDB * gnc_pricedb_get_db ( QofBook *  book)

Return the pricedb associated with the book.

Parameters
bookThe QofBook holding the pricedb
Returns
The GNCPriceDB associated with the book.

Definition at line 925 of file gnc-pricedb.cpp.

926{
927 QofCollection *col;
928
929 if (!book) return nullptr;
930 col = qof_book_get_collection (book, GNC_ID_PRICEDB);
931 return gnc_collection_get_pricedb (col);
932}
QofCollection * qof_book_get_collection(const QofBook *book, QofIdType entity_type)
Return The table of entities of the given type.
Definition qofbook.cpp:521
GNCPriceDB * gnc_collection_get_pricedb(QofCollection *col)
Return the pricedb via the Book's collection.

◆ gnc_pricedb_get_latest_price()

gnc_numeric gnc_pricedb_get_latest_price ( GNCPriceDB *  pdb,
const gnc_commodity *  orig_currency,
const gnc_commodity *  new_currency 
)

Retrieve the price one currency to another using the latest price.

Parameters
pdbThe pricedb
orig_currencyThe commodity in which the balance is currently expressed
new_currencyThe commodity to which the balance should be converted
Returns
A price, or gnc_numeric_zero if no price is available.

Definition at line 2536 of file gnc-pricedb.cpp.

2539{
2540 return get_nearest_price (pdb, orig_currency, new_currency, INT64_MAX, FALSE);
2541}

◆ gnc_pricedb_get_nearest_before_price()

gnc_numeric gnc_pricedb_get_nearest_before_price ( GNCPriceDB *  pdb,
const gnc_commodity *  orig_currency,
const gnc_commodity *  new_currency,
const time64  t 
)

Retrieve the price one currency to another using the price nearest to before the given time.

Parameters
pdbThe pricedb
orig_currencyThe commodity in which the balance is currently expressed
new_currencyThe commodity to which the balance should be converted
tThe time to be used for for comparison
Returns
A price, or gnc_numeric_zero if no price is available.

Definition at line 2518 of file gnc-pricedb.cpp.

2522{
2523 return get_nearest_price (pdb, orig_currency, new_currency, t, TRUE);
2524}

◆ gnc_pricedb_get_nearest_price()

gnc_numeric gnc_pricedb_get_nearest_price ( GNCPriceDB *  pdb,
const gnc_commodity *  orig_currency,
const gnc_commodity *  new_currency,
const time64  t 
)

Retrieve the price one currency to another using the price nearest to the given time.

Parameters
pdbThe pricedb
orig_currencyThe commodity in which the balance is currently expressed
new_currencyThe commodity to which the balance should be converted
tThe time in which the nearest price should be used.
Returns
A price, or gnc_numeric_zero if no price is available.

Definition at line 2527 of file gnc-pricedb.cpp.

2531{
2532 return get_nearest_price (pdb, orig_currency, new_currency, t, FALSE);
2533}

◆ gnc_pricedb_get_num_prices()

guint gnc_pricedb_get_num_prices ( GNCPriceDB *  db)

Return the number of prices in the database.

For XML Backend Testing

Definition at line 947 of file gnc-pricedb.cpp.

948{
949 guint count;
950
951 if (!db) return 0;
952
953 count = 0;
954
955 gnc_pricedb_foreach_price(db, num_prices_helper, &count, FALSE);
956
957 return count;
958}
gboolean gnc_pricedb_foreach_price(GNCPriceDB *db, GncPriceForeachFunc f, gpointer user_data, gboolean stable_order)
Call a GncPriceForeachFunction once for each price in db, until the function returns FALSE.

◆ gnc_pricedb_get_prices()

PriceList * gnc_pricedb_get_prices ( GNCPriceDB *  db,
const gnc_commodity *  commodity,
const gnc_commodity *  currency 
)

Return all the prices for a given commodity in another.

Does not retrieve reverse prices, i.e. prices of the second commodity in the first.

Parameters
dbThe pricedb from which to retrieve prices.
commodityThe commodity for which prices should be retrieved.
currencyThe commodity in which prices should be quoted. If NULL, all prices in any commodity are included.
Returns
A PriceList of matching prices or NULL if none were found.

Definition at line 2061 of file gnc-pricedb.cpp.

2064{
2065 if (!db || !commodity) return nullptr;
2066 ENTER ("db=%p commodity=%p currency=%p", db, commodity, currency);
2067 auto result = pricedb_get_prices_internal (db, commodity, currency, FALSE);
2068 if (!result) return nullptr;
2069 g_list_foreach (result, (GFunc)gnc_price_ref, nullptr);
2070 LEAVE (" ");
2071 return result;
2072}
void gnc_price_ref(GNCPrice *p)
gnc_price_ref - indicate your need for a given price to stick around (i.e.

◆ gnc_pricedb_has_prices()

gboolean gnc_pricedb_has_prices ( GNCPriceDB *  db,
const gnc_commodity *  commodity,
const gnc_commodity *  currency 
)

Report whether the pricedb contains prices for one commodity in another.

Does not check the reverse direction.

Parameters
dbThe pricedb to check
commodityThe commodity to check for the existence of prices
currencyThe commodity in which prices are sought. If NULL reports all commodities.
Returns
TRUE if matching prices are found, FALSE otherwise.

Definition at line 2022 of file gnc-pricedb.cpp.

2025{
2026 GList *price_list;
2027 GHashTable *currency_hash;
2028 gint size;
2029
2030 if (!db || !commodity) return FALSE;
2031 ENTER ("db=%p commodity=%p currency=%p", db, commodity, currency);
2032 currency_hash = static_cast<GHashTable*>(g_hash_table_lookup(db->commodity_hash, commodity));
2033 if (!currency_hash)
2034 {
2035 LEAVE("no, no currency_hash table");
2036 return FALSE;
2037 }
2038
2039 if (currency)
2040 {
2041 price_list = static_cast<GList*>(g_hash_table_lookup(currency_hash, currency));
2042 if (price_list)
2043 {
2044 LEAVE("yes");
2045 return TRUE;
2046 }
2047 LEAVE("no, no price list");
2048 return FALSE;
2049 }
2050
2051 size = g_hash_table_size (currency_hash);
2052 LEAVE("%s", size > 0 ? "yes" : "no");
2053 return size > 0;
2054}

◆ gnc_pricedb_lookup_day_t64()

GNCPrice * gnc_pricedb_lookup_day_t64 ( GNCPriceDB *  db,
const gnc_commodity *  commodity,
const gnc_commodity *  currency,
time64  t 
)

Return the price between the two commodities on the indicated day.

Note that the notion of day might be distorted by changes in timezone.

The returned GNCPrice may be in either direction so check to ensure that its value is correctly applied.

Parameters
dbThe pricedb
commodityThe first commodity
currencyThe second commodity
tA time. The price returned will be in the same day as this time according to the local timezone.
Returns
A GNCPrice or NULL on failure.

Definition at line 2185 of file gnc-pricedb.cpp.

2189{
2190 return lookup_nearest_in_time(db, c, currency, t, TRUE);
2191}

◆ gnc_pricedb_lookup_latest()

GNCPrice * gnc_pricedb_lookup_latest ( GNCPriceDB *  db,
const gnc_commodity *  commodity,
const gnc_commodity *  currency 
)

Find the most recent price between the two commodities.

The returned GNCPrice may be in either direction so check to ensure that its value is correctly applied.

Parameters
dbThe pricedb
commodityThe first commodity
currencyThe second commodity
Returns
A GNCPrice or NULL if no price exists.

Definition at line 1745 of file gnc-pricedb.cpp.

1748{
1749 GList *price_list;
1750 GNCPrice *result;
1751
1752 if (!db || !commodity || !currency) return nullptr;
1753 ENTER ("db=%p commodity=%p currency=%p", db, commodity, currency);
1754
1755 price_list = pricedb_get_prices_internal(db, commodity, currency, TRUE);
1756 if (!price_list) return nullptr;
1757 /* This works magically because prices are inserted in date-sorted
1758 * order, and the latest date always comes first. So return the
1759 * first in the list. */
1760 result = static_cast<GNCPrice*>(price_list->data);
1761 gnc_price_ref(result);
1762 g_list_free (price_list);
1763 LEAVE("price is %p", result);
1764 return result;
1765}

◆ gnc_pricedb_lookup_latest_any_currency()

PriceList * gnc_pricedb_lookup_latest_any_currency ( GNCPriceDB *  db,
const gnc_commodity *  commodity 
)

Find the most recent price between a commodity and all other commodities.

The returned GNCPrices may be in either direction so check to ensure that their values are correctly applied.

Parameters
dbThe pricedb
commodityThe commodity for which to obtain prices
Returns
A PriceList of prices found, or NULL if none found.

Definition at line 1969 of file gnc-pricedb.cpp.

1971{
1973 gnc_time(nullptr));
1974}
time64 gnc_time(time64 *tbuf)
get the current time
Definition gnc-date.cpp:262
PriceList * gnc_pricedb_lookup_nearest_before_any_currency_t64(GNCPriceDB *db, const gnc_commodity *commodity, time64 t)
Return the nearest price between the given commodity and any other before the given time.

◆ gnc_pricedb_lookup_nearest_before_any_currency_t64()

PriceList * gnc_pricedb_lookup_nearest_before_any_currency_t64 ( GNCPriceDB *  db,
const gnc_commodity *  c,
time64  t 
)

Return the nearest price between the given commodity and any other before the given time.

The returned GNCPrice may be in either direction so check to ensure that its value is correctly applied.

Parameters
dbThe pricedb
cThe commodity
tThe time before which to find prices
Returns
A PriceList of prices for each commodity found or NULL if none are.

Definition at line 1997 of file gnc-pricedb.cpp.

2000{
2001 GList *prices = nullptr, *result;
2002 UsesCommodity helper = {&prices, commodity, t};
2003 result = nullptr;
2004
2005 if (!db || !commodity) return nullptr;
2006 ENTER ("db=%p commodity=%p", db, commodity);
2007
2008 pricedb_pricelist_traversal(db, price_list_scan_any_currency,
2009 &helper);
2010 prices = g_list_sort(prices, compare_prices_by_date);
2011 result = latest_before(prices, commodity, t);
2012 gnc_price_list_destroy(prices);
2013 LEAVE(" ");
2014 return result;
2015}
void gnc_price_list_destroy(PriceList *prices)
gnc_price_list_destroy - destroy the given price list, calling gnc_price_unref on all the prices incl...

◆ gnc_pricedb_lookup_nearest_before_t64()

GNCPrice * gnc_pricedb_lookup_nearest_before_t64 ( GNCPriceDB *  db,
const gnc_commodity *  c,
const gnc_commodity *  currency,
time64  t 
)

Return the nearest price between the given commodities before the given time.

The returned GNCPrice may be in either direction so check to ensure that its value is correctly applied.

Parameters
dbThe pricedb
cThe first commodity
currencyThe second commodity
tThe time before which to find the price
Returns
A GNCPrice or NULL if no prices are found before t.

Definition at line 2319 of file gnc-pricedb.cpp.

2323{
2324 GNCPrice *current_price = nullptr;
2325 if (!db || !c || !currency) return nullptr;
2326 ENTER ("db=%p commodity=%p currency=%p", db, c, currency);
2327 auto price_list = pricedb_get_prices_internal (db, c, currency, TRUE);
2328 if (!price_list) return nullptr;
2329 auto p = g_list_find_custom (price_list, &t, (GCompareFunc)price_time64_less_or_equal);
2330 if (p)
2331 {
2332 current_price = GNC_PRICE (p->data);
2333 gnc_price_ref (current_price);
2334 }
2335 g_list_free (price_list);
2336 LEAVE (" ");
2337 return current_price;
2338}

◆ gnc_pricedb_lookup_nearest_in_time64()

GNCPrice * gnc_pricedb_lookup_nearest_in_time64 ( GNCPriceDB *  db,
const gnc_commodity *  c,
const gnc_commodity *  currency,
time64  t 
)

Return the price between the two commoditiesz nearest to the given time.

The returned GNCPrice may be in either direction so check to ensure that its value is correctly applied.

Parameters
dbThe pricedb
cThe first commodity
currencyThe second commodity
tThe time nearest to which the returned price should be.
Returns
A GNCPrice or NULL if no prices exist between the two commodities.

Definition at line 2304 of file gnc-pricedb.cpp.

2308{
2309 return lookup_nearest_in_time(db, c, currency, t, FALSE);
2310}

◆ gnc_pricedb_lookup_nearest_in_time_any_currency_t64()

PriceList * gnc_pricedb_lookup_nearest_in_time_any_currency_t64 ( GNCPriceDB *  db,
const gnc_commodity *  c,
time64  t 
)

Return the price nearest in time to that given between the given commodity and every other.

The returned GNCPrices may be in either direction so check to ensure that their values are correctly applied.

Parameters
db,Thepricedb
c,Thecommodity for which prices should be obtained.
t,Thetime nearest to which the prices should be obtained.
Returns
A PriceList of prices for each commodity pair found or NULL if none are.

Definition at line 1977 of file gnc-pricedb.cpp.

1980{
1981 GList *prices = nullptr, *result;
1982 UsesCommodity helper = {&prices, commodity, t};
1983 result = nullptr;
1984
1985 if (!db || !commodity) return nullptr;
1986 ENTER ("db=%p commodity=%p", db, commodity);
1987
1988 pricedb_pricelist_traversal(db, price_list_scan_any_currency, &helper);
1989 prices = g_list_sort(prices, compare_prices_by_date);
1990 result = nearest_to(prices, commodity, t);
1991 gnc_price_list_destroy(prices);
1992 LEAVE(" ");
1993 return result;
1994}

◆ gnc_pricedb_nth_price()

GNCPrice * gnc_pricedb_nth_price ( GNCPriceDB *  db,
const gnc_commodity *  c,
const int  n 
)

Get the nth price for the given commodity in reverse date order.

Parameters
dbThe pricedb
cThe commodity whose nth price is needed
nZero based index of the price wanted
Returns
The nth price for this commodity in reverse chronological order, without regard for what currency the price is in

Definition at line 2133 of file gnc-pricedb.cpp.

2136{
2137 static const gnc_commodity *last_c = nullptr;
2138 static GList *prices = nullptr;
2139
2140 GNCPrice *result = nullptr;
2141 GHashTable *currency_hash;
2142 g_return_val_if_fail (GNC_IS_COMMODITY (c), nullptr);
2143
2144 if (!db || !c || n < 0) return nullptr;
2145 ENTER ("db=%p commodity=%s index=%d", db, gnc_commodity_get_mnemonic(c), n);
2146
2147 if (last_c && prices && last_c == c && db->reset_nth_price_cache == FALSE)
2148 {
2149 result = static_cast<GNCPrice*>(g_list_nth_data (prices, n));
2150 LEAVE ("price=%p", result);
2151 return result;
2152 }
2153
2154 last_c = c;
2155
2156 if (prices)
2157 {
2158 g_list_free (prices);
2159 prices = nullptr;
2160 }
2161
2162 db->reset_nth_price_cache = FALSE;
2163
2164 currency_hash = static_cast<GHashTable*>(g_hash_table_lookup (db->commodity_hash, c));
2165 if (currency_hash)
2166 {
2167 GList *currencies = g_hash_table_get_values (currency_hash);
2168 g_list_foreach (currencies, list_combine, &prices);
2169 result = static_cast<GNCPrice*>(g_list_nth_data (prices, n));
2170 g_list_free (currencies);
2171 }
2172
2173 LEAVE ("price=%p", result);
2174 return result;
2175}
const char * gnc_commodity_get_mnemonic(const gnc_commodity *cm)
Retrieve the mnemonic for the specified commodity.

◆ gnc_pricedb_nth_price_reset_cache()

void gnc_pricedb_nth_price_reset_cache ( GNCPriceDB *  db)

Definition at line 2178 of file gnc-pricedb.cpp.

2179{
2180 if (db)
2181 db->reset_nth_price_cache = TRUE;
2182}

◆ gnc_pricedb_num_prices()

int gnc_pricedb_num_prices ( GNCPriceDB *  db,
const gnc_commodity *  c 
)

Get the number of prices, in any currency, for a given commodity.

Parameters
dbThe pricedb
cThe commodity
Returns
The number of prices in the database for this commody, zero if none

Definition at line 2086 of file gnc-pricedb.cpp.

2088{
2089 int result = 0;
2090 GHashTable *currency_hash;
2091
2092 if (!db || !c) return 0;
2093 ENTER ("db=%p commodity=%p", db, c);
2094
2095 currency_hash = static_cast<GHashTable*>(g_hash_table_lookup(db->commodity_hash, c));
2096 if (currency_hash)
2097 {
2098 g_hash_table_foreach(currency_hash, price_count_helper, (gpointer)&result);
2099 }
2100
2101 LEAVE ("count=%d", result);
2102 return result;
2103}

◆ gnc_pricedb_print_contents()

void gnc_pricedb_print_contents ( GNCPriceDB *  db,
FILE *  f 
)

This simple function can be useful for debugging the pricedb code.

Definition at line 2821 of file gnc-pricedb.cpp.

2822{
2823 if (!db)
2824 {
2825 PERR("nullptr PriceDB\n");
2826 return;
2827 }
2828 if (!f)
2829 {
2830 PERR("nullptr FILE*\n");
2831 return;
2832 }
2833
2834 fprintf(f, "<gnc:pricedb>\n");
2835 gnc_pricedb_foreach_price(db, print_pricedb_adapter, f, FALSE);
2836 fprintf(f, "</gnc:pricedb>\n");
2837}
#define PERR(format, args...)
Log a serious error.
Definition qoflog.h:244

◆ gnc_pricedb_remove_old_prices()

gboolean gnc_pricedb_remove_old_prices ( GNCPriceDB *  db,
GList *  comm_list,
GDate *  fiscal_end_date,
time64  cutoff,
PriceRemoveSourceFlags  source,
PriceRemoveKeepOptions  keep 
)

Remove and unref prices older than a certain time.

Parameters
dbThe pricedb
comm_listA list of commodities
fiscal_end_datethe end date of the current accounting period
cutoffThe time before which prices should be deleted.
sourceWhether Finance::Quote, user or all prices should be deleted.
keepWhether scaled, monthly, weekly or no prices should be left.
Returns
True if there were prices to process, False if not.

Definition at line 1563 of file gnc-pricedb.cpp.

1567{
1568 remove_info data;
1569 GList *node;
1570 char datebuff[MAX_DATE_LENGTH + 1];
1571 memset (datebuff, 0, sizeof(datebuff));
1572
1573 data.db = db;
1574 data.cutoff = cutoff;
1575 data.list = nullptr;
1576 data.delete_fq = FALSE;
1577 data.delete_user = FALSE;
1578 data.delete_app = FALSE;
1579
1580 ENTER("Remove Prices for Source %d, keeping %d", source, keep);
1581
1582 // setup the source options
1583 if (source & PRICE_REMOVE_SOURCE_APP)
1584 data.delete_app = TRUE;
1585
1586 if (source & PRICE_REMOVE_SOURCE_FQ)
1587 data.delete_fq = TRUE;
1588
1589 if (source & PRICE_REMOVE_SOURCE_USER)
1590 data.delete_user = TRUE;
1591
1592 // Walk the list of commodities
1593 for (node = g_list_first (comm_list); node; node = g_list_next (node))
1594 {
1595 auto currencies_hash = static_cast<GHashTable*>(g_hash_table_lookup (db->commodity_hash, node->data));
1596 g_hash_table_foreach (currencies_hash, pricedb_remove_foreach_pricelist, &data);
1597 }
1598
1599 if (data.list == nullptr)
1600 {
1601 LEAVE("Empty price list");
1602 return FALSE;
1603 }
1604 qof_print_date_buff (datebuff, sizeof(datebuff), cutoff);
1605 DEBUG("Number of Prices in list is %d, Cutoff date is %s",
1606 g_slist_length (data.list), datebuff);
1607
1608 // Check for a valid fiscal end of year date
1609 if (fiscal_end_date == nullptr || !g_date_valid (fiscal_end_date))
1610 {
1611 auto ymd = GncDate().year_month_day();
1612 GDate end_this_year;
1613 g_date_set_dmy (&end_this_year, 31, GDateMonth(12), ymd.year);
1614 gnc_pricedb_process_removal_list (db, &end_this_year, data, keep);
1615 }
1616 else
1617 gnc_pricedb_process_removal_list (db, fiscal_end_date, data, keep);
1618
1619 g_slist_free (data.list);
1620 LEAVE(" ");
1621 return TRUE;
1622}
GnuCash Date class.
gnc_ymd year_month_day() const
Get the year, month, and day from the date as a gnc_ymd.
#define MAX_DATE_LENGTH
The maximum length of a string created by the date printers.
Definition gnc-date.h:108
size_t qof_print_date_buff(char *buff, const size_t len, time64 t)
Convenience: calls through to qof_print_date_dmy_buff().
Definition gnc-date.cpp:574
#define DEBUG(format, args...)
Print a debugging message.
Definition qoflog.h:264

◆ gnc_pricedb_remove_price()

gboolean gnc_pricedb_remove_price ( GNCPriceDB *  db,
GNCPrice *  p 
)

Remove a price from the pricedb and unref the price.

Parameters
dbThe Pricedb
pThe price to remove.

Definition at line 1233 of file gnc-pricedb.cpp.

1234{
1235 gboolean rc;
1236 char datebuff[MAX_DATE_LENGTH + 1];
1237 memset(datebuff, 0, sizeof(datebuff));
1238 if (!db || !p) return FALSE;
1239 ENTER ("db=%p, pr=%p dirty=%d destroying=%d",
1242
1243 gnc_price_ref(p);
1244 qof_print_date_buff(datebuff, sizeof(datebuff), gnc_price_get_time64 (p));
1245 DEBUG("Remove Date is %s, Commodity is %s, Source is %s", datebuff,
1246 gnc_commodity_get_fullname (gnc_price_get_commodity (p)),
1247 gnc_price_get_source_string (p));
1248
1249 rc = remove_price (db, p, TRUE);
1251 qof_instance_set_dirty(&db->inst);
1253
1254 /* invoke the backend to delete this price */
1255 gnc_price_begin_edit (p);
1256 qof_instance_set_destroying(p, TRUE);
1257 gnc_price_commit_edit (p);
1258 p->db = nullptr;
1259 gnc_price_unref(p);
1260 LEAVE ("db=%p, pr=%p", db, p);
1261 return rc;
1262}
const char * gnc_commodity_get_fullname(const gnc_commodity *cm)
Retrieve the full name for the specified commodity.
void gnc_price_unref(GNCPrice *p)
gnc_price_unref - indicate you're finished with a price (i.e.

◆ gnc_pricedb_set_bulk_update()

void gnc_pricedb_set_bulk_update ( GNCPriceDB *  db,
gboolean  bulk_update 
)

Set flag to indicate whether duplication checks should be performed.

Normally used at load time to speed up loading the pricedb.

Parameters
dbThe pricedb
bulk_updateTRUE to disable duplication checks, FALSE to enable them.

Definition at line 897 of file gnc-pricedb.cpp.

898{
899 db->bulk_update = bulk_update;
900}