38 #include "gncTaxTableP.h" 44 #include "gnc-sql-connection.hpp" 45 #include "gnc-sql-backend.hpp" 46 #include "gnc-sql-object-backend.hpp" 47 #include "gnc-sql-column-table-entry.hpp" 51 #define _GNC_MOD_NAME GNC_ID_TAXTABLE 61 static gpointer get_obj_guid (gpointer pObject,
const QofParam* param);
62 static void set_obj_guid (gpointer pObject, gpointer pValue);
63 static gpointer bt_get_parent (gpointer pObject);
64 static void tt_set_parent (gpointer pObject, gpointer pValue);
65 static void tt_set_parent_guid (gpointer pObject, gpointer pValue);
67 #define MAX_NAME_LEN 50 69 #define TT_TABLE_NAME "taxtables" 70 #define TT_TABLE_VERSION 2 72 static EntryVec tt_col_table
74 gnc_sql_make_table_entry<CT_GUID>(
"guid", 0, COL_NNUL | COL_PKEY,
"guid" ),
75 gnc_sql_make_table_entry<CT_STRING>(
"name", MAX_NAME_LEN, COL_NNUL,
"name" ),
76 gnc_sql_make_table_entry<CT_INT64>(
"refcount", 0, COL_NNUL,
"ref-count" ),
77 gnc_sql_make_table_entry<CT_BOOLEAN>(
"invisible", 0, COL_NNUL,
"invisible" ),
80 gnc_sql_make_table_entry<CT_GUID>(
"parent", 0, 0,
85 static EntryVec tt_parent_col_table
87 gnc_sql_make_table_entry<CT_GUID>(
"parent", 0, 0,
nullptr,
91 #define TTENTRIES_TABLE_NAME "taxtable_entries" 92 #define TTENTRIES_TABLE_VERSION 3 94 static EntryVec ttentries_col_table
96 gnc_sql_make_table_entry<CT_INT>(
97 "id", 0, COL_PKEY | COL_NNUL | COL_AUTOINC),
98 gnc_sql_make_table_entry<CT_TAXTABLEREF>(
"taxtable", 0, COL_NNUL,
101 gnc_sql_make_table_entry<CT_ACCOUNTREF>(
"account", 0, COL_NNUL,
104 gnc_sql_make_table_entry<CT_NUMERIC>(
"amount", 0, COL_NNUL,
107 gnc_sql_make_table_entry<CT_INT>(
"type", 0, COL_NNUL,
114 static EntryVec guid_col_table
116 gnc_sql_make_table_entry<CT_GUID>(
"taxtable", 0, 0,
117 get_obj_guid, set_obj_guid),
120 GncSqlTaxTableBackend::GncSqlTaxTableBackend() :
122 TT_TABLE_NAME, tt_col_table) {}
132 using TaxTblParentGuidVec = std::vector<TaxTblParentGuidPtr>;
135 get_obj_guid (gpointer pObject,
const QofParam* param)
139 g_return_val_if_fail (pInfo != NULL, NULL);
141 return (gpointer)pInfo->guid;
145 set_obj_guid (gpointer pObject, gpointer pValue)
151 bt_get_parent (gpointer pObject)
157 g_return_val_if_fail (pObject != NULL, NULL);
158 g_return_val_if_fail (GNC_IS_TAXTABLE (pObject), NULL);
160 tt = GNC_TAXTABLE (pObject);
161 pParent = gncTaxTableGetParent (tt);
171 return (gpointer)parent_guid;
175 tt_set_parent (gpointer data, gpointer value)
182 g_return_if_fail (data != NULL);
183 g_return_if_fail (GNC_IS_TAXTABLE (data));
185 tt = GNC_TAXTABLE (data);
189 parent = gncTaxTableLookup (pBook, guid);
192 gncTaxTableSetParent (tt, parent);
193 gncTaxTableSetChild (parent, tt);
199 tt_set_parent_guid (gpointer pObject, gpointer pValue)
201 g_return_if_fail (pObject != NULL);
202 g_return_if_fail (pValue != NULL);
205 s->guid = *
static_cast<GncGUID*
>(pValue);
214 g_return_if_fail (sql_be != NULL);
215 g_return_if_fail (tt != NULL);
217 gnc_sql_load_object (sql_be, row, GNC_ID_TAXTABLE, e, ttentries_col_table);
218 gncTaxTableAddEntry (tt, e);
228 g_return_if_fail (sql_be != NULL);
229 g_return_if_fail (tt != NULL);
232 memset (&value, 0,
sizeof (GValue));
233 g_value_init (&value, G_TYPE_STRING);
234 g_value_set_string (&value, guid_buf);
235 buf = g_strdup_printf (
"SELECT * FROM %s WHERE taxtable='%s'",
236 TTENTRIES_TABLE_NAME, guid_buf);
237 auto stmt = sql_be->create_statement_from_sql (buf);
240 for (
auto row : *result)
241 load_single_ttentry (sql_be, row, tt);
246 TaxTblParentGuidVec& l_tt_needing_parents)
251 g_return_if_fail (sql_be != NULL);
253 guid = gnc_sql_load_guid (sql_be, row);
254 tt = gncTaxTableLookup (sql_be->book(), guid);
257 tt = gncTaxTableCreate (sql_be->book());
259 gnc_sql_load_object (sql_be, row, GNC_ID_TAXTABLE, tt, tt_col_table);
261 load_taxtable_entries (sql_be, tt);
267 if (gncTaxTableGetParent (tt) == NULL)
273 gnc_sql_load_object (sql_be, row, GNC_ID_TAXTABLE, &s,
274 tt_parent_col_table);
280 qof_instance_mark_clean (QOF_INSTANCE (tt));
286 g_return_if_fail (sql_be != NULL);
289 std::stringstream sql;
290 sql <<
"SELECT * FROM " << TT_TABLE_NAME;
291 auto stmt = sql_be->create_statement_from_sql(sql.str());
293 TaxTblParentGuidVec tt_needing_parents;
295 for (
auto row : *result)
296 load_single_taxtable (sql_be, row, tt_needing_parents);
303 if (!tt_needing_parents.empty())
305 bool progress_made =
true;
306 std::reverse(tt_needing_parents.begin(),
307 tt_needing_parents.end());
308 auto end = tt_needing_parents.end();
309 while (progress_made)
311 progress_made =
false;
312 end = std::remove_if(tt_needing_parents.begin(), end,
316 auto parent = gncTaxTableLookup (pBook,
318 if (parent !=
nullptr)
320 tt_set_parent (s->tt, &s->guid);
321 progress_made =
true;
338 g_return_if_fail (sql_be != NULL);
343 sql_be->
create_table(TT_TABLE_NAME, TT_TABLE_VERSION, tt_col_table);
345 else if (version < m_version)
350 PINFO (
"Taxtables table upgraded from version 1 to version %d\n",
357 sql_be->
create_table(TTENTRIES_TABLE_NAME, TTENTRIES_TABLE_VERSION,
358 ttentries_col_table);
360 else if (version < TTENTRIES_TABLE_VERSION)
363 sql_be->
upgrade_table(TTENTRIES_TABLE_NAME, ttentries_col_table);
365 PINFO (
"Taxtable entries table upgraded from version 1 to version %d\n",
366 TTENTRIES_TABLE_VERSION);
376 g_return_val_if_fail (sql_be != NULL, FALSE);
377 g_return_val_if_fail (guid != NULL, FALSE);
379 guid_info.be = sql_be;
380 guid_info.guid = guid;
382 TTENTRIES_TABLE_NAME, &guid_info, guid_col_table);
391 g_return_val_if_fail (sql_be != NULL, FALSE);
392 g_return_val_if_fail (guid != NULL, FALSE);
395 is_ok = delete_all_tt_entries (sql_be, guid);
397 for (entry = entries; entry != NULL && is_ok; entry = entry->next)
402 ttentries_col_table);
417 g_return_val_if_fail (inst != NULL, FALSE);
418 g_return_val_if_fail (GNC_IS_TAXTABLE (inst), FALSE);
419 g_return_val_if_fail (sql_be != NULL, FALSE);
421 tt = GNC_TAXTABLE (inst);
423 is_infant = qof_instance_get_infant (inst);
428 else if (sql_be->pristine() || is_infant)
436 is_ok = sql_be->
do_db_operation(op, TT_TABLE_NAME, GNC_ID_TAXTABLE, tt,
448 is_ok = save_tt_entries (sql_be, guid, gncTaxTableGetEntries (tt));
456 is_ok = delete_all_tt_entries (sql_be, guid);
466 save_next_taxtable (
QofInstance* inst, gpointer data)
479 g_return_val_if_fail (sql_be != NULL, FALSE);
492 gpointer pObject)
const noexcept
494 load_from_guid_ref(row, obj_name, pObject,
496 return gncTaxTableLookup(sql_be->book(), g);
503 add_objectref_guid_to_table(vec);
508 const gpointer pObject,
509 PairVec& vec)
const noexcept
511 add_objectref_guid_to_query(obj_name, pObject, vec);
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.
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.
const GncGUID * qof_instance_get_guid(gconstpointer inst)
Return the GncGUID of this instance.
void create_tables(GncSqlBackend *) override
Conditionally create or update a database table from m_col_table.
bool write(GncSqlBackend *) override
Write all objects of m_type_name to the database.
QofBook * qof_instance_get_book(gconstpointer inst)
Return the book pointer.
#define G_LOG_DOMAIN
Functions providing the SX List as a plugin page.
#define PINFO(format, args...)
Print an informational note.
const gchar * QofIdTypeConst
QofIdTypeConst declaration.
load and save accounts data to SQL
gboolean qof_instance_get_destroying(gconstpointer ptr)
Retrieve the flag that indicates whether or not this object is about to be destroyed.
gboolean gnc_sql_slots_save(GncSqlBackend *sql_be, const GncGUID *guid, gboolean is_infant, QofInstance *inst)
gnc_sql_slots_save - Saves slots for an object to the db.
void add_to_query(QofIdTypeConst obj_name, void *pObject, PairVec &vec) const noexcept override
Add a pair of the table column heading and object's value's string representation to a PairVec; used ...
gchar * guid_to_string_buff(const GncGUID *guid, gchar *str)
The guid_to_string_buff() routine puts a null-terminated string encoding of the id into the memory po...
void(* QofSetterFunc)(gpointer, gpointer)
The QofSetterFunc defines an function pointer for parameter setters.
bool commit(GncSqlBackend *sql_be, QofInstance *inst) override
UPDATE/INSERT a single instance of m_type_name into the database.
void load(const GncSqlBackend *sql_be, GncSqlRow &row, QofIdTypeConst obj_name, void *pObject) const noexcept override
Load a value into an object from the database row.
Row of SQL Query results.
void load_all(GncSqlBackend *) override
Load all objects of m_type in the database into memory.
void upgrade_table(const std::string &table_name, const EntryVec &col_table) noexcept
Upgrades a table to a new structure.
#define GUID_ENCODING_LENGTH
Number of characters needed to encode a guid as a string not including the null terminator.
void qof_object_foreach(QofIdTypeConst type_name, QofBook *book, QofInstanceForeachCB cb, gpointer user_data)
Invoke the callback 'cb' on every instance ov a particular object type.
gpointer(* QofAccessFunc)(gpointer object, const QofParam *param)
The QofAccessFunc defines an arbitrary function pointer for access functions.
Encapsulates per-class table schema with functions to load, create a table, commit a changed front-en...
Data-passing struct for callbacks to qof_object_foreach() used in GncSqlObjectBackend::write().
gboolean gnc_sql_slots_delete(GncSqlBackend *sql_be, const GncGUID *guid)
gnc_sql_slots_delete - Deletes slots for an object from the db.
void gnc_sql_slots_load(GncSqlBackend *sql_be, QofInstance *inst)
Loads slots for an object from the db.
void add_to_table(ColVec &vec) const noexcept override
Add a GncSqlColumnInfo structure for the column type to a ColVec.
Business Entry Interface.
The type used to store guids in C.
uint_t get_table_version(const std::string &table_name) const noexcept
Returns the version number for a DB table.
modtime is the internal date of the last modtime See libgnucash/engine/TaxTableBillTermImmutability.txt for an explanation of the following Code that handles refcount, parent, child, invisible and children is identical to that in ::GncBillTerm
Main SQL backend structure.
load and save tax table data to SQL