35 #if defined( S_SPLINT_S ) 36 #include "splint-defs.h" 39 #include "gnc-sql-connection.hpp" 40 #include "gnc-sql-backend.hpp" 41 #include "gnc-sql-object-backend.hpp" 42 #include "gnc-sql-column-table-entry.hpp" 50 #define TABLE_NAME "prices" 51 #define TABLE_VERSION 3 53 #define PRICE_MAX_SOURCE_LEN 2048 54 #define PRICE_MAX_TYPE_LEN 2048 56 static const EntryVec col_table
58 gnc_sql_make_table_entry<CT_GUID>(
"guid", 0, COL_NNUL | COL_PKEY,
"guid"),
59 gnc_sql_make_table_entry<CT_COMMODITYREF>(
"commodity_guid", 0, COL_NNUL,
61 gnc_sql_make_table_entry<CT_COMMODITYREF>(
"currency_guid", 0, COL_NNUL,
63 gnc_sql_make_table_entry<CT_TIME>(
"date", 0, COL_NNUL,
"date"),
64 gnc_sql_make_table_entry<CT_STRING>(
"source", PRICE_MAX_SOURCE_LEN, 0,
66 gnc_sql_make_table_entry<CT_STRING>(
"type", PRICE_MAX_TYPE_LEN, 0,
"type"),
67 gnc_sql_make_table_entry<CT_NUMERIC>(
"value", 0, COL_NNUL,
"value")
70 GncSqlPriceBackend::GncSqlPriceBackend() :
72 TABLE_NAME, col_table) {}
81 g_return_val_if_fail (sql_be != NULL, NULL);
85 gnc_price_begin_edit (pPrice);
86 gnc_sql_load_object (sql_be, row, GNC_ID_PRICE, pPrice, col_table);
87 gnc_price_commit_edit (pPrice);
98 g_return_if_fail (sql_be != NULL);
100 pBook = sql_be->book();
102 std::string sql(
"SELECT * FROM " TABLE_NAME);
103 auto stmt = sql_be->create_statement_from_sql(sql);
107 if (result->begin() == result->end())
113 for (
auto row : *result)
115 pPrice = load_single_price (sql_be, row);
124 std::string pkey(col_table[0]->name());
125 sql =
"SELECT DISTINCT ";
126 sql += pkey +
" FROM " TABLE_NAME;
128 (BookLookupFn)gnc_price_lookup);
138 g_return_if_fail (sql_be != NULL);
143 (void)sql_be->
create_table(TABLE_NAME, TABLE_VERSION, col_table);
145 else if (version < m_version)
154 PINFO (
"Prices table upgraded from version 1 to version %d\n", TABLE_VERSION);
163 GNCPrice* pPrice = GNC_PRICE (inst);
166 gboolean is_ok = TRUE;
168 g_return_val_if_fail (sql_be != NULL, FALSE);
169 g_return_val_if_fail (inst != NULL, FALSE);
170 g_return_val_if_fail (GNC_IS_PRICE (inst), FALSE);
172 is_infant = qof_instance_get_infant (inst);
177 else if (sql_be->pristine() || is_infant)
186 if (op != OP_DB_DELETE)
203 write_price (GNCPrice* p, gpointer data)
207 g_return_val_if_fail (p != NULL, FALSE);
208 g_return_val_if_fail (data != NULL, FALSE);
210 if (s->is_ok && gnc_price_get_source (p) != PRICE_SOURCE_TEMP)
212 s->commit (QOF_INSTANCE(p));
221 g_return_val_if_fail (sql_be != NULL, FALSE);
bool do_db_operation(E_DB_OPERATION op, const char *table_name, QofIdTypeConst obj_name, gpointer pObject, const EntryVec &table) const noexcept
Performs an operation on the database.
GNCPrice * gnc_price_create(QofBook *book)
gnc_price_create - returns a newly allocated and initialized price with a reference count of 1...
bool create_table(const std::string &table_name, const EntryVec &col_table) const noexcept
Creates a table in the database.
bool set_table_version(const std::string &table_name, uint_t version) noexcept
Registers the version for a table.
GncSqlResultPtr execute_select_statement(const GncSqlStatementPtr &stmt) const noexcept
Executes an SQL SELECT statement and returns the result rows.
void gnc_sql_slots_load_for_sql_subquery(GncSqlBackend *sql_be, const std::string subquery, BookLookupFn lookup_fn)
gnc_sql_slots_load_for_sql_subquery - Loads slots for all objects whose guid is supplied by a subquer...
a simple price database for gnucash
#define G_LOG_DOMAIN
Functions providing the SX List as a plugin page.
#define PINFO(format, args...)
Print an informational note.
load and save data to SQL
load and save accounts data to SQL
void gnc_price_unref(GNCPrice *p)
gnc_price_unref - indicate you're finished with a price (i.e.
gboolean qof_instance_get_destroying(gconstpointer ptr)
Retrieve the flag that indicates whether or not this object is about to be destroyed.
gboolean gnc_pricedb_add_price(GNCPriceDB *db, GNCPrice *p)
Add a price to the pricedb.
load and save data to SQL
GNCPriceDB * gnc_pricedb_get_db(QofBook *book)
Return the pricedb associated with the book.
bool commit(GncSqlBackend *sql_be, QofInstance *inst) override
UPDATE/INSERT a single instance of m_type_name into the database.
void load_all(GncSqlBackend *) override
Load all objects of m_type in the database into memory.
bool save_commodity(gnc_commodity *comm) noexcept
Ensure that a commodity referenced in another object is in fact saved in the database.
Row of SQL Query results.
void upgrade_table(const std::string &table_name, const EntryVec &col_table) noexcept
Upgrades a table to a new structure.
Encapsulates per-class table schema with functions to load, create a table, commit a changed front-en...
void create_tables(GncSqlBackend *) override
Conditionally create or update a database table from m_col_table.
Data-passing struct for callbacks to qof_object_foreach() used in GncSqlObjectBackend::write().
bool write(GncSqlBackend *) override
Write all objects of m_type_name to the database.
void gnc_pricedb_set_bulk_update(GNCPriceDB *db, gboolean bulk_update)
Set flag to indicate whether duplication checks should be performed.
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...
uint_t get_table_version(const std::string &table_name) const noexcept
Returns the version number for a DB table.
Main SQL backend structure.