GnuCash  4.12-71-gadd45021a8+
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. More...
 
GNCPriceDB * gnc_collection_get_pricedb (QofCollection *col)
 Return the pricedb via the Book's collection. More...
 
void gnc_pricedb_destroy (GNCPriceDB *db)
 Destroy the given pricedb and unref all of the prices it contains. More...
 
void gnc_pricedb_begin_edit (GNCPriceDB *)
 Begin an edit. More...
 
void gnc_pricedb_commit_edit (GNCPriceDB *)
 Commit an edit. More...
 
void gnc_pricedb_set_bulk_update (GNCPriceDB *db, gboolean bulk_update)
 Set flag to indicate whether duplication checks should be performed. More...
 
gboolean gnc_pricedb_add_price (GNCPriceDB *db, GNCPrice *p)
 Add a price to the pricedb. More...
 
gboolean gnc_pricedb_remove_price (GNCPriceDB *db, GNCPrice *p)
 Remove a price from the pricedb and unref the price. More...
 
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. More...
 
GNCPrice * gnc_pricedb_lookup_latest (GNCPriceDB *db, const gnc_commodity *commodity, const gnc_commodity *currency)
 Find the most recent price between the two commodities. More...
 
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. More...
 
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. More...
 
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. More...
 
GNCPrice * gnc_pricedb_lookup_at_time64 (GNCPriceDB *db, const gnc_commodity *commodity, const gnc_commodity *currency, time64 t)
 Find the price between two commodities at a time64. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
int gnc_pricedb_num_prices (GNCPriceDB *db, const gnc_commodity *c)
 Get the number of prices, in any currency, for a given commodity. More...
 
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. More...
 
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. More...
 
gboolean gnc_pricedb_equal (GNCPriceDB *db1, GNCPriceDB *db2)
 Test equality of two pricedbs. More...
 

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.

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 960 of file gnc-pricedb.c.

961 {
962  if (!col) return NULL;
963  return qof_collection_get_data (col);
964 }
gpointer qof_collection_get_data(const QofCollection *col)
Store and retrieve arbitrary object-defined data.
Definition: qofid.cpp:291

◆ 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 1170 of file gnc-pricedb.c.

1171 {
1172  if (!db || !p) return FALSE;
1173 
1174  ENTER ("db=%p, pr=%p dirty=%d destroying=%d",
1175  db, p, qof_instance_get_dirty_flag(p),
1177 
1178  if (FALSE == add_price(db, p))
1179  {
1180  LEAVE (" failed to add price");
1181  return FALSE;
1182  }
1183 
1185  qof_instance_set_dirty(&db->inst);
1187 
1188  LEAVE ("db=%p, pr=%p dirty=%d destroying=%d",
1189  db, p, qof_instance_get_dirty_flag(p),
1191 
1192  return TRUE;
1193 }
gboolean qof_instance_get_destroying(gconstpointer ptr)
Retrieve the flag that indicates whether or not this object is about to be destroyed.
#define ENTER(format, args...)
Print a function entry debugging message.
Definition: qoflog.h:272
gboolean qof_instance_get_dirty_flag(gconstpointer ptr)
Retrieve the flag that indicates whether or not this object has been modified.
void gnc_pricedb_begin_edit(GNCPriceDB *pdb)
Begin an edit.
Definition: gnc-pricedb.c:447
#define LEAVE(format, args...)
Print a function exit debugging message.
Definition: qoflog.h:282
void gnc_pricedb_commit_edit(GNCPriceDB *pdb)
Commit an edit.
Definition: gnc-pricedb.c:453

◆ gnc_pricedb_begin_edit()

void gnc_pricedb_begin_edit ( GNCPriceDB *  )

Begin an edit.

Definition at line 447 of file gnc-pricedb.c.

448 {
449  qof_begin_edit(&pdb->inst);
450 }
gboolean qof_begin_edit(QofInstance *inst)
begin_edit

◆ gnc_pricedb_commit_edit()

void gnc_pricedb_commit_edit ( GNCPriceDB *  )

Commit an edit.

Definition at line 453 of file gnc-pricedb.c.

454 {
455  if (!qof_commit_edit (QOF_INSTANCE(pdb))) return;
456  qof_commit_edit_part2 (&pdb->inst, commit_err, noop, noop);
457 }
gboolean qof_commit_edit(QofInstance *inst)
commit_edit helpers
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

◆ 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 2659 of file gnc-pricedb.c.

2663 {
2664  return convert_amount_at_date
2665  (pdb, balance, balance_currency, new_currency, INT64_MAX, FALSE);
2666 }

◆ 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 2680 of file gnc-pricedb.c.

2685 {
2686  return convert_amount_at_date
2687  (pdb, balance, balance_currency, new_currency, t, TRUE);
2688 }

◆ 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 2669 of file gnc-pricedb.c.

2674 {
2675  return convert_amount_at_date
2676  (pdb, balance, balance_currency, new_currency, t, FALSE);
2677 }

◆ 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 923 of file gnc-pricedb.c.

924 {
925  if (!db) return;
926  if (db->commodity_hash)
927  {
928  g_hash_table_foreach (db->commodity_hash,
929  destroy_pricedb_commodity_hash_data,
930  NULL);
931  }
932  g_hash_table_destroy (db->commodity_hash);
933  db->commodity_hash = NULL;
934  /* qof_instance_release (&db->inst); */
935  g_object_unref(db);
936 }

◆ gnc_pricedb_equal()

gboolean gnc_pricedb_equal ( GNCPriceDB *  db1,
GNCPriceDB *  db2 
)

Test equality of two pricedbs.

For XML Backend Testing

Definition at line 1044 of file gnc-pricedb.c.

1045 {
1046  GNCPriceDBEqualData equal_data;
1047 
1048  if (db1 == db2) return TRUE;
1049 
1050  if (!db1 || !db2)
1051  {
1052  PWARN ("one is NULL");
1053  return FALSE;
1054  }
1055 
1056  equal_data.equal = TRUE;
1057  equal_data.db2 = db2;
1058 
1059  g_hash_table_foreach (db1->commodity_hash,
1060  pricedb_equal_foreach_currencies_hash,
1061  &equal_data);
1062 
1063  return equal_data.equal;
1064 }
#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 2875 of file gnc-pricedb.c.

2879 {
2880  ENTER ("db=%p f=%p", db, f);
2881  if (stable_order)
2882  {
2883  LEAVE (" stable order found");
2884  return stable_price_traversal(db, f, user_data);
2885  }
2886  LEAVE (" use unstable order");
2887  return unstable_price_traversal(db, f, user_data);
2888 }
#define ENTER(format, args...)
Print a function entry debugging message.
Definition: qoflog.h:272
#define LEAVE(format, args...)
Print a function exit debugging message.
Definition: qoflog.h:282

◆ 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 967 of file gnc-pricedb.c.

968 {
969  QofCollection *col;
970 
971  if (!book) return NULL;
972  col = qof_book_get_collection (book, GNC_ID_PRICEDB);
973  return gnc_collection_get_pricedb (col);
974 }
GNCPriceDB * gnc_collection_get_pricedb(QofCollection *col)
Return the pricedb via the Book's collection.
Definition: gnc-pricedb.c:960
QofCollection * qof_book_get_collection(const QofBook *book, QofIdType entity_type)
Return The table of entities of the given type.
Definition: qofbook.cpp:606

◆ 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 2624 of file gnc-pricedb.c.

2627 {
2628  return get_nearest_price (pdb, orig_currency, new_currency, INT64_MAX, FALSE);
2629 }

◆ 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 2606 of file gnc-pricedb.c.

2610 {
2611  return get_nearest_price (pdb, orig_currency, new_currency, t, TRUE);
2612 }

◆ 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 2615 of file gnc-pricedb.c.

2619 {
2620  return get_nearest_price (pdb, orig_currency, new_currency, t, FALSE);
2621 }

◆ 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 989 of file gnc-pricedb.c.

990 {
991  guint count;
992 
993  if (!db) return 0;
994 
995  count = 0;
996 
997  gnc_pricedb_foreach_price(db, num_prices_helper, &count, FALSE);
998 
999  return count;
1000 }
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...
Definition: gnc-pricedb.c:2875

◆ 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 2109 of file gnc-pricedb.c.

2112 {
2113  GList *result;
2114  GList *node;
2115 
2116 
2117  if (!db || !commodity) return NULL;
2118  ENTER ("db=%p commodity=%p currency=%p", db, commodity, currency);
2119  result = pricedb_get_prices_internal (db, commodity, currency, FALSE);
2120  if (!result) return NULL;
2121  for (node = result; node; node = node->next)
2122  gnc_price_ref (node->data);
2123 
2124  LEAVE (" ");
2125  return result;
2126 }
#define ENTER(format, args...)
Print a function entry debugging message.
Definition: qoflog.h:272
#define LEAVE(format, args...)
Print a function exit debugging message.
Definition: qoflog.h:282
void gnc_price_ref(GNCPrice *p)
gnc_price_ref - indicate your need for a given price to stick around (i.e.
Definition: gnc-pricedb.c:337

◆ 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 2070 of file gnc-pricedb.c.

2073 {
2074  GList *price_list;
2075  GHashTable *currency_hash;
2076  gint size;
2077 
2078  if (!db || !commodity) return FALSE;
2079  ENTER ("db=%p commodity=%p currency=%p", db, commodity, currency);
2080  currency_hash = g_hash_table_lookup(db->commodity_hash, commodity);
2081  if (!currency_hash)
2082  {
2083  LEAVE("no, no currency_hash table");
2084  return FALSE;
2085  }
2086 
2087  if (currency)
2088  {
2089  price_list = g_hash_table_lookup(currency_hash, currency);
2090  if (price_list)
2091  {
2092  LEAVE("yes");
2093  return TRUE;
2094  }
2095  LEAVE("no, no price list");
2096  return FALSE;
2097  }
2098 
2099  size = g_hash_table_size (currency_hash);
2100  LEAVE("%s", size > 0 ? "yes" : "no");
2101  return size > 0;
2102 }
#define ENTER(format, args...)
Print a function entry debugging message.
Definition: qoflog.h:272
#define LEAVE(format, args...)
Print a function exit debugging message.
Definition: qoflog.h:282

◆ gnc_pricedb_lookup_at_time64()

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

Find the price between two commodities at a time64.

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
tThe time64 at which to retrieve the price.
Returns
A GNCPrice or NULL if none matches.

Definition at line 2247 of file gnc-pricedb.c.

2251 {
2252  GList *price_list;
2253  GList *item = NULL;
2254 
2255  if (!db || !c || !currency) return NULL;
2256  ENTER ("db=%p commodity=%p currency=%p", db, c, currency);
2257  price_list = pricedb_get_prices_internal (db, c, currency, TRUE);
2258  item = price_list;
2259  while (item)
2260  {
2261  GNCPrice *p = item->data;
2262  time64 price_time = gnc_price_get_time64(p);
2263  if (price_time == t)
2264  {
2265  gnc_price_ref(p);
2266  g_list_free (price_list);
2267  LEAVE("price is %p", p);
2268  return p;
2269  }
2270  item = item->next;
2271  }
2272  g_list_free (price_list);
2273  LEAVE (" ");
2274  return NULL;
2275 }
#define ENTER(format, args...)
Print a function entry debugging message.
Definition: qoflog.h:272
#define LEAVE(format, args...)
Print a function exit debugging message.
Definition: qoflog.h:282
gint64 time64
Many systems, including Microsoft Windows and BSD-derived Unixes like Darwin, are retaining the int-3...
Definition: gnc-date.h:93
void gnc_price_ref(GNCPrice *p)
gnc_price_ref - indicate your need for a given price to stick around (i.e.
Definition: gnc-pricedb.c:337

◆ 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 2238 of file gnc-pricedb.c.

2242 {
2243  return lookup_nearest_in_time(db, c, currency, t, TRUE);
2244 }

◆ 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 1792 of file gnc-pricedb.c.

1795 {
1796  GList *price_list;
1797  GNCPrice *result;
1798 
1799  if (!db || !commodity || !currency) return NULL;
1800  ENTER ("db=%p commodity=%p currency=%p", db, commodity, currency);
1801 
1802  price_list = pricedb_get_prices_internal(db, commodity, currency, TRUE);
1803  if (!price_list) return NULL;
1804  /* This works magically because prices are inserted in date-sorted
1805  * order, and the latest date always comes first. So return the
1806  * first in the list. */
1807  result = price_list->data;
1808  gnc_price_ref(result);
1809  g_list_free (price_list);
1810  LEAVE("price is %p", result);
1811  return result;
1812 }
#define ENTER(format, args...)
Print a function entry debugging message.
Definition: qoflog.h:272
#define LEAVE(format, args...)
Print a function exit debugging message.
Definition: qoflog.h:282
void gnc_price_ref(GNCPrice *p)
gnc_price_ref - indicate your need for a given price to stick around (i.e.
Definition: gnc-pricedb.c:337

◆ 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 2017 of file gnc-pricedb.c.

2019 {
2021  gnc_time(NULL));
2022 }
time64 gnc_time(time64 *tbuf)
get the current local time
Definition: gnc-date.cpp:273
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...
Definition: gnc-pricedb.c:2045

◆ 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 2045 of file gnc-pricedb.c.

2048 {
2049  GList *prices = NULL, *result;
2050  UsesCommodity helper = {&prices, commodity, t};
2051  result = NULL;
2052 
2053  if (!db || !commodity) return NULL;
2054  ENTER ("db=%p commodity=%p", db, commodity);
2055 
2056  pricedb_pricelist_traversal(db, price_list_scan_any_currency,
2057  &helper);
2058  prices = g_list_sort(prices, compare_prices_by_date);
2059  result = latest_before(prices, commodity, t);
2060  gnc_price_list_destroy(prices);
2061  LEAVE(" ");
2062  return result;
2063 }
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...
Definition: gnc-pricedb.c:791
#define ENTER(format, args...)
Print a function entry debugging message.
Definition: qoflog.h:272
#define LEAVE(format, args...)
Print a function exit debugging message.
Definition: qoflog.h:282

◆ 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 2401 of file gnc-pricedb.c.

2405 {
2406  GList *price_list;
2407  GNCPrice *current_price = NULL;
2408  /* GNCPrice *next_price = NULL;
2409  GNCPrice *result = NULL;*/
2410  GList *item = NULL;
2411  time64 price_time;
2412 
2413  if (!db || !c || !currency) return NULL;
2414  ENTER ("db=%p commodity=%p currency=%p", db, c, currency);
2415  price_list = pricedb_get_prices_internal (db, c, currency, TRUE);
2416  if (!price_list) return NULL;
2417  item = price_list;
2418  do
2419  {
2420  price_time = gnc_price_get_time64 (item->data);
2421  if (price_time <= t)
2422  current_price = item->data;
2423  item = item->next;
2424  }
2425  while (price_time > t && item);
2426  gnc_price_ref(current_price);
2427  g_list_free (price_list);
2428  LEAVE (" ");
2429  return current_price;
2430 }
#define ENTER(format, args...)
Print a function entry debugging message.
Definition: qoflog.h:272
#define LEAVE(format, args...)
Print a function exit debugging message.
Definition: qoflog.h:282
gint64 time64
Many systems, including Microsoft Windows and BSD-derived Unixes like Darwin, are retaining the int-3...
Definition: gnc-date.h:93
void gnc_price_ref(GNCPrice *p)
gnc_price_ref - indicate your need for a given price to stick around (i.e.
Definition: gnc-pricedb.c:337

◆ 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 2391 of file gnc-pricedb.c.

2395 {
2396  return lookup_nearest_in_time(db, c, currency, t, FALSE);
2397 }

◆ 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 2025 of file gnc-pricedb.c.

2028 {
2029  GList *prices = NULL, *result;
2030  UsesCommodity helper = {&prices, commodity, t};
2031  result = NULL;
2032 
2033  if (!db || !commodity) return NULL;
2034  ENTER ("db=%p commodity=%p", db, commodity);
2035 
2036  pricedb_pricelist_traversal(db, price_list_scan_any_currency, &helper);
2037  prices = g_list_sort(prices, compare_prices_by_date);
2038  result = nearest_to(prices, commodity, t);
2039  gnc_price_list_destroy(prices);
2040  LEAVE(" ");
2041  return result;
2042 }
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...
Definition: gnc-pricedb.c:791
#define ENTER(format, args...)
Print a function entry debugging message.
Definition: qoflog.h:272
#define LEAVE(format, args...)
Print a function exit debugging message.
Definition: qoflog.h:282

◆ 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 2186 of file gnc-pricedb.c.

2189 {
2190  static const gnc_commodity *last_c = NULL;
2191  static GList *prices = NULL;
2192 
2193  GNCPrice *result = NULL;
2194  GHashTable *currency_hash;
2195  g_return_val_if_fail (GNC_IS_COMMODITY (c), NULL);
2196 
2197  if (!db || !c || n < 0) return NULL;
2198  ENTER ("db=%p commodity=%s index=%d", db, gnc_commodity_get_mnemonic(c), n);
2199 
2200  if (last_c && prices && last_c == c && db->reset_nth_price_cache == FALSE)
2201  {
2202  result = g_list_nth_data (prices, n);
2203  LEAVE ("price=%p", result);
2204  return result;
2205  }
2206 
2207  last_c = c;
2208 
2209  if (prices)
2210  {
2211  g_list_free (prices);
2212  prices = NULL;
2213  }
2214 
2215  db->reset_nth_price_cache = FALSE;
2216 
2217  currency_hash = g_hash_table_lookup (db->commodity_hash, c);
2218  if (currency_hash)
2219  {
2220  GList *currencies = g_hash_table_get_values (currency_hash);
2221  g_list_foreach (currencies, list_combine, &prices);
2222  result = g_list_nth_data (prices, n);
2223  g_list_free (currencies);
2224  }
2225 
2226  LEAVE ("price=%p", result);
2227  return result;
2228 }
const char * gnc_commodity_get_mnemonic(const gnc_commodity *cm)
Retrieve the mnemonic for the specified commodity.
#define ENTER(format, args...)
Print a function entry debugging message.
Definition: qoflog.h:272
#define LEAVE(format, args...)
Print a function exit debugging message.
Definition: qoflog.h:282

◆ 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 2140 of file gnc-pricedb.c.

2142 {
2143  int result = 0;
2144  GHashTable *currency_hash;
2145 
2146  if (!db || !c) return 0;
2147  ENTER ("db=%p commodity=%p", db, c);
2148 
2149  currency_hash = g_hash_table_lookup(db->commodity_hash, c);
2150  if (currency_hash)
2151  {
2152  g_hash_table_foreach(currency_hash, price_count_helper, (gpointer)&result);
2153  }
2154 
2155  LEAVE ("count=%d", result);
2156  return result;
2157 }
#define ENTER(format, args...)
Print a function entry debugging message.
Definition: qoflog.h:272
#define LEAVE(format, args...)
Print a function exit debugging message.
Definition: qoflog.h:282

◆ 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 1606 of file gnc-pricedb.c.

1610 {
1611  remove_info data;
1612  GList *node;
1613  char datebuff[MAX_DATE_LENGTH + 1];
1614  memset (datebuff, 0, sizeof(datebuff));
1615 
1616  data.db = db;
1617  data.cutoff = cutoff;
1618  data.list = NULL;
1619  data.delete_fq = FALSE;
1620  data.delete_user = FALSE;
1621  data.delete_app = FALSE;
1622 
1623  ENTER("Remove Prices for Source %d, keeping %d", source, keep);
1624 
1625  // setup the source options
1626  if (source & PRICE_REMOVE_SOURCE_APP)
1627  data.delete_app = TRUE;
1628 
1629  if (source & PRICE_REMOVE_SOURCE_FQ)
1630  data.delete_fq = TRUE;
1631 
1632  if (source & PRICE_REMOVE_SOURCE_USER)
1633  data.delete_user = TRUE;
1634 
1635  // Walk the list of commodities
1636  for (node = g_list_first (comm_list); node; node = g_list_next (node))
1637  {
1638  GHashTable *currencies_hash = g_hash_table_lookup (db->commodity_hash, node->data);
1639  g_hash_table_foreach (currencies_hash, pricedb_remove_foreach_pricelist, &data);
1640  }
1641 
1642  if (data.list == NULL)
1643  {
1644  LEAVE("Empty price list");
1645  return FALSE;
1646  }
1647  qof_print_date_buff (datebuff, sizeof(datebuff), cutoff);
1648  DEBUG("Number of Prices in list is %d, Cutoff date is %s",
1649  g_slist_length (data.list), datebuff);
1650 
1651  // Check for a valid fiscal end of year date
1652  if (fiscal_end_date == NULL)
1653  {
1654  GDateYear year_now = g_date_get_year (gnc_g_date_new_today ());
1655  fiscal_end_date = g_date_new ();
1656  g_date_set_dmy (fiscal_end_date, 31, 12, year_now);
1657  }
1658  else if (g_date_valid (fiscal_end_date) == FALSE)
1659  {
1660  GDateYear year_now = g_date_get_year (gnc_g_date_new_today ());
1661  g_date_clear (fiscal_end_date, 1);
1662  g_date_set_dmy (fiscal_end_date, 31, 12, year_now);
1663  }
1664  gnc_pricedb_process_removal_list (db, fiscal_end_date, data, keep);
1665 
1666  g_slist_free (data.list);
1667  LEAVE(" ");
1668  return TRUE;
1669 }
#define DEBUG(format, args...)
Print a debugging message.
Definition: qoflog.h:264
#define ENTER(format, args...)
Print a function entry debugging message.
Definition: qoflog.h:272
#define MAX_DATE_LENGTH
The maximum length of a string created by the date printers.
Definition: gnc-date.h:114
#define LEAVE(format, args...)
Print a function exit debugging message.
Definition: qoflog.h:282
size_t qof_print_date_buff(char *buff, size_t buflen, time64 secs)
Convenience: calls through to qof_print_date_dmy_buff().
Definition: gnc-date.cpp:581
GDate * gnc_g_date_new_today()
Returns a newly allocated date of the current clock time, taken from time(2).
Definition: gnc-date.cpp:1229

◆ 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 1277 of file gnc-pricedb.c.

1278 {
1279  gboolean rc;
1280  char datebuff[MAX_DATE_LENGTH + 1];
1281  memset(datebuff, 0, sizeof(datebuff));
1282  if (!db || !p) return FALSE;
1283  ENTER ("db=%p, pr=%p dirty=%d destroying=%d",
1284  db, p, qof_instance_get_dirty_flag(p),
1286 
1287  gnc_price_ref(p);
1288  qof_print_date_buff(datebuff, sizeof(datebuff), gnc_price_get_time64 (p));
1289  DEBUG("Remove Date is %s, Commodity is %s, Source is %s", datebuff,
1290  gnc_commodity_get_fullname (gnc_price_get_commodity (p)),
1291  gnc_price_get_source_string (p));
1292 
1293  rc = remove_price (db, p, TRUE);
1295  qof_instance_set_dirty(&db->inst);
1297 
1298  /* invoke the backend to delete this price */
1299  gnc_price_begin_edit (p);
1300  qof_instance_set_destroying(p, TRUE);
1301  gnc_price_commit_edit (p);
1302  p->db = NULL;
1303  gnc_price_unref(p);
1304  LEAVE ("db=%p, pr=%p", db, p);
1305  return rc;
1306 }
void gnc_price_unref(GNCPrice *p)
gnc_price_unref - indicate you&#39;re finished with a price (i.e.
Definition: gnc-pricedb.c:344
#define DEBUG(format, args...)
Print a debugging message.
Definition: qoflog.h:264
gboolean qof_instance_get_destroying(gconstpointer ptr)
Retrieve the flag that indicates whether or not this object is about to be destroyed.
#define ENTER(format, args...)
Print a function entry debugging message.
Definition: qoflog.h:272
gboolean qof_instance_get_dirty_flag(gconstpointer ptr)
Retrieve the flag that indicates whether or not this object has been modified.
const char * gnc_commodity_get_fullname(const gnc_commodity *cm)
Retrieve the full name for the specified commodity.
void gnc_pricedb_begin_edit(GNCPriceDB *pdb)
Begin an edit.
Definition: gnc-pricedb.c:447
#define MAX_DATE_LENGTH
The maximum length of a string created by the date printers.
Definition: gnc-date.h:114
#define LEAVE(format, args...)
Print a function exit debugging message.
Definition: qoflog.h:282
void gnc_price_ref(GNCPrice *p)
gnc_price_ref - indicate your need for a given price to stick around (i.e.
Definition: gnc-pricedb.c:337
size_t qof_print_date_buff(char *buff, size_t buflen, time64 secs)
Convenience: calls through to qof_print_date_dmy_buff().
Definition: gnc-date.cpp:581
void gnc_pricedb_commit_edit(GNCPriceDB *pdb)
Commit an edit.
Definition: gnc-pricedb.c:453

◆ 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 939 of file gnc-pricedb.c.

940 {
941  db->bulk_update = bulk_update;
942 }