GnuCash c935c2f+
Loading...
Searching...
No Matches
Public Member Functions
GncSqlTaxTableBackend Class Reference
Inheritance diagram for GncSqlTaxTableBackend:
GncSqlObjectBackend

Public Member Functions

void load_all (GncSqlBackend *) override
 Load all objects of m_type in the database into memory.
 
void create_tables (GncSqlBackend *) override
 Conditionally create or update a database table from m_col_table.
 
bool commit (GncSqlBackend *sql_be, QofInstance *inst) override
 UPDATE/INSERT a single instance of m_type_name into the database.
 
bool write (GncSqlBackend *) override
 Write all objects of m_type_name to the database.
 
- Public Member Functions inherited from GncSqlObjectBackend
 GncSqlObjectBackend (int version, const std::string &type, const std::string &table, const EntryVec &vec)
 
const char * type () const noexcept
 Return the m_type_name for the class.
 
const bool is_version (int version) const noexcept
 Compare a version with the compiled version (m_version).
 
bool instance_in_db (const GncSqlBackend *sql_be, QofInstance *inst) const noexcept
 Check the presence of an object in the backend's database.
 

Additional Inherited Members

- Protected Attributes inherited from GncSqlObjectBackend
const std::string m_table_name
 
const int m_version
 
const std::string m_type_name
 
const EntryVec & m_col_table
 The front-end QofIdType.
 

Detailed Description

Definition at line 35 of file gnc-tax-table-sql.h.

Constructor & Destructor Documentation

◆ GncSqlTaxTableBackend()

GncSqlTaxTableBackend::GncSqlTaxTableBackend ( )

Definition at line 120 of file gnc-tax-table-sql.cpp.

120 :
121 GncSqlObjectBackend(TT_TABLE_VERSION, GNC_ID_TAXTABLE,
122 TT_TABLE_NAME, tt_col_table) {}
Encapsulates per-class table schema with functions to load, create a table, commit a changed front-en...

Member Function Documentation

◆ commit()

bool GncSqlTaxTableBackend::commit ( GncSqlBackend sql_be,
QofInstance inst 
)
overridevirtual

UPDATE/INSERT a single instance of m_type_name into the database.

Parameters
sql_beThe GncSqlBackend containing the database.
instThe QofInstance to be written out.

Reimplemented from GncSqlObjectBackend.

Definition at line 409 of file gnc-tax-table-sql.cpp.

410{
411 GncTaxTable* tt;
412 const GncGUID* guid;
413 E_DB_OPERATION op;
414 gboolean is_infant;
415 gboolean is_ok;
416
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);
420
421 tt = GNC_TAXTABLE (inst);
422
423 is_infant = qof_instance_get_infant (inst);
425 {
426 op = OP_DB_DELETE;
427 }
428 else if (sql_be->pristine() || is_infant)
429 {
430 op = OP_DB_INSERT;
431 }
432 else
433 {
434 op = OP_DB_UPDATE;
435 }
436 is_ok = sql_be->do_db_operation(op, TT_TABLE_NAME, GNC_ID_TAXTABLE, tt,
437 tt_col_table);
438
439 if (is_ok)
440 {
441 // Now, commit or delete any slots and tax table entries
442 guid = qof_instance_get_guid (inst);
443 if (!qof_instance_get_destroying (inst))
444 {
445 is_ok = gnc_sql_slots_save (sql_be, guid, is_infant, inst);
446 if (is_ok)
447 {
448 is_ok = save_tt_entries (sql_be, guid, gncTaxTableGetEntries (tt));
449 }
450 }
451 else
452 {
453 is_ok = gnc_sql_slots_delete (sql_be, guid);
454 if (is_ok)
455 {
456 is_ok = delete_all_tt_entries (sql_be, guid);
457 }
458 }
459 }
460
461 return is_ok;
462}
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.
const GncGUID * qof_instance_get_guid(gconstpointer inst)
Return the GncGUID of this instance.
gboolean qof_instance_get_destroying(gconstpointer ptr)
Retrieve the flag that indicates whether or not this object is about to be destroyed.
The type used to store guids in C.
Definition guid.h:75
modtime is the internal date of the last modtime See libgnucash/engine/TaxTableBillTermImmutability....

◆ create_tables()

void GncSqlTaxTableBackend::create_tables ( GncSqlBackend sql_be)
overridevirtual

Conditionally create or update a database table from m_col_table.

The condition is the version returned by querying the database's version table: If it's 0 then the table wasn't found and will be created; All tables areat least version 1. If the database's version is less than the compiled version then the table schema is upgraded but the data isn't, that's the engine's responsibility when the object is loaded. If the version is greater than the compiled version then nothing is touched.

Parameters
sql_beThe GncSqlBackend containing the database connection.

Reimplemented from GncSqlObjectBackend.

Definition at line 334 of file gnc-tax-table-sql.cpp.

335{
336 gint version;
337
338 g_return_if_fail (sql_be != NULL);
339
340 version = sql_be->get_table_version( TT_TABLE_NAME);
341 if (version == 0)
342 {
343 sql_be->create_table(TT_TABLE_NAME, TT_TABLE_VERSION, tt_col_table);
344 }
345 else if (version < m_version)
346 {
347 /* Upgrade 64 bit int handling */
348 sql_be->upgrade_table(TT_TABLE_NAME, tt_col_table);
349 sql_be->set_table_version (TT_TABLE_NAME, TT_TABLE_VERSION);
350 PINFO ("Taxtables table upgraded from version 1 to version %d\n",
351 TT_TABLE_VERSION);
352 }
353
354 version = sql_be->get_table_version( TTENTRIES_TABLE_NAME);
355 if (version == 0)
356 {
357 sql_be->create_table(TTENTRIES_TABLE_NAME, TTENTRIES_TABLE_VERSION,
358 ttentries_col_table);
359 }
360 else if (version < TTENTRIES_TABLE_VERSION)
361 {
362 /* Upgrade 64 bit int handling */
363 sql_be->upgrade_table(TTENTRIES_TABLE_NAME, ttentries_col_table);
364 sql_be->set_table_version (TTENTRIES_TABLE_NAME, TTENTRIES_TABLE_VERSION);
365 PINFO ("Taxtable entries table upgraded from version 1 to version %d\n",
366 TTENTRIES_TABLE_VERSION);
367 }
368}
uint_t get_table_version(const std::string &table_name) const noexcept
Returns the version number for a DB table.
bool create_table(const std::string &table_name, const EntryVec &col_table) const noexcept
Creates a table in the database.
void upgrade_table(const std::string &table_name, const EntryVec &col_table) noexcept
Upgrades a table to a new structure.
bool set_table_version(const std::string &table_name, uint_t version) noexcept
Registers the version for a table.
#define PINFO(format, args...)
Print an informational note.
Definition qoflog.h:256

◆ load_all()

void GncSqlTaxTableBackend::load_all ( GncSqlBackend sql_be)
overridevirtual

Load all objects of m_type in the database into memory.

Parameters
sql_beThe GncSqlBackend containing the database connection.

Implements GncSqlObjectBackend.

Definition at line 284 of file gnc-tax-table-sql.cpp.

285{
286 g_return_if_fail (sql_be != NULL);
287
288 /* First time, create the query */
289 std::stringstream sql;
290 sql << "SELECT * FROM " << TT_TABLE_NAME;
291 auto stmt = sql_be->create_statement_from_sql(sql.str());
292 auto result = sql_be->execute_select_statement(stmt);
293 TaxTblParentGuidVec tt_needing_parents;
294
295 for (auto row : *result)
296 load_single_taxtable (sql_be, row, tt_needing_parents);
297
298 /* While there are items on the list of taxtables needing parents,
299 try to see if the parent has now been loaded. Theory says that if
300 items are removed from the front and added to the back if the
301 parent is still not available, then eventually, the list will
302 shrink to size 0. */
303 if (!tt_needing_parents.empty())
304 {
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)
310 {
311 progress_made = false;
312 end = std::remove_if(tt_needing_parents.begin(), end,
314 {
315 auto pBook = qof_instance_get_book (QOF_INSTANCE (s->tt));
316 auto parent = gncTaxTableLookup (pBook,
317 &s->guid);
318 if (parent != nullptr)
319 {
320 tt_set_parent (s->tt, &s->guid);
321 progress_made = true;
322 delete s;
323 return true;
324 }
325 return false;
326 });
327
328 }
329 }
330}
GncSqlResultPtr execute_select_statement(const GncSqlStatementPtr &stmt) const noexcept
Executes an SQL SELECT statement and returns the result rows.

◆ write()

bool GncSqlTaxTableBackend::write ( GncSqlBackend sql_be)
overridevirtual

Write all objects of m_type_name to the database.

Parameters
sql_beThe GncSqlBackend containing the database.
Returns
true if the objects were successfully written, false otherwise.

Reimplemented from GncSqlObjectBackend.

Definition at line 477 of file gnc-tax-table-sql.cpp.

478{
479 g_return_val_if_fail (sql_be != NULL, FALSE);
480 write_objects_t data{sql_be, true, this};
481
482 qof_object_foreach (GNC_ID_TAXTABLE, sql_be->book(), save_next_taxtable, &data);
483
484 return data.is_ok;
485}
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.
Data-passing struct for callbacks to qof_object_foreach() used in GncSqlObjectBackend::write().

The documentation for this class was generated from the following files: