GnuCash  5.6-150-g038405b370+
Public Member Functions
GncSqlBudgetBackend Class Reference
Inheritance diagram for GncSqlBudgetBackend:
GncSqlObjectBackend

Public Member Functions

void load_all (GncSqlBackend *) override
 Load all objects of m_type in the database into memory. More...
 
void create_tables (GncSqlBackend *) override
 Conditionally create or update a database table from m_col_table. More...
 
bool commit (GncSqlBackend *sql_be, QofInstance *inst) override
 UPDATE/INSERT a single instance of m_type_name into the database. More...
 
bool write (GncSqlBackend *) override
 Write all objects of m_type_name to the database. More...
 
- 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. More...
 
const bool is_version (int version) const noexcept
 Compare a version with the compiled version (m_version). More...
 
bool instance_in_db (const GncSqlBackend *sql_be, QofInstance *inst) const noexcept
 Check the presence of an object in the backend's database. More...
 

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 34 of file gnc-budget-sql.h.

Member Function Documentation

◆ commit()

bool GncSqlBudgetBackend::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 366 of file gnc-budget-sql.cpp.

367 {
368  GncBudget* pBudget = GNC_BUDGET (inst);
369  const GncGUID* guid;
370  E_DB_OPERATION op;
371  gboolean is_infant;
372  gboolean is_ok;
373 
374  g_return_val_if_fail (sql_be != NULL, FALSE);
375  g_return_val_if_fail (inst != NULL, FALSE);
376  g_return_val_if_fail (GNC_IS_BUDGET (inst), FALSE);
377 
378  is_infant = qof_instance_get_infant (inst);
379  if (qof_instance_get_destroying (inst))
380  {
381  op = OP_DB_DELETE;
382  }
383  else if (sql_be->pristine() || is_infant)
384  {
385  op = OP_DB_INSERT;
386  }
387  else
388  {
389  op = OP_DB_UPDATE;
390  }
391  is_ok = sql_be->do_db_operation(op, BUDGET_TABLE, GNC_ID_BUDGET, pBudget,
392  col_table);
393 
394  // Now, commit any slots and recurrence
395  if (is_ok)
396  {
397  guid = qof_instance_get_guid (inst);
398  if (!qof_instance_get_destroying (inst))
399  {
400  is_ok = save_budget_amounts (sql_be, pBudget);
401  if (is_ok)
402  {
403  is_ok = gnc_sql_recurrence_save (sql_be, guid,
404  gnc_budget_get_recurrence (pBudget));
405  }
406  if (is_ok)
407  {
408  is_ok = gnc_sql_slots_save (sql_be, guid, is_infant, inst);
409  }
410  }
411  else
412  {
413  is_ok = delete_budget_amounts (sql_be, pBudget);
414  if (is_ok)
415  {
416  is_ok = gnc_sql_recurrence_delete (sql_be, guid);
417  }
418  if (is_ok)
419  {
420  (void)gnc_sql_slots_delete (sql_be, guid);
421  }
422  }
423  }
424 
425  return is_ok;
426 }
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.
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.
gboolean gnc_sql_slots_delete(GncSqlBackend *sql_be, const GncGUID *guid)
gnc_sql_slots_delete - Deletes slots for an object from the db.
The type used to store guids in C.
Definition: guid.h:75

◆ create_tables()

void GncSqlBudgetBackend::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 344 of file gnc-budget-sql.cpp.

345 {
346  gint version;
347 
348  g_return_if_fail (sql_be != NULL);
349 
350  version = sql_be->get_table_version( BUDGET_TABLE);
351  if (version == 0)
352  {
353  (void)sql_be->create_table(BUDGET_TABLE, TABLE_VERSION, col_table);
354  }
355 
356  version = sql_be->get_table_version( AMOUNTS_TABLE);
357  if (version == 0)
358  {
359  (void)sql_be->create_table(AMOUNTS_TABLE, AMOUNTS_TABLE_VERSION,
360  budget_amounts_col_table);
361  }
362 }
bool create_table(const std::string &table_name, const EntryVec &col_table) const noexcept
Creates a table in the database.
uint_t get_table_version(const std::string &table_name) const noexcept
Returns the version number for a DB table.

◆ load_all()

void GncSqlBudgetBackend::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 325 of file gnc-budget-sql.cpp.

326 {
327  g_return_if_fail (sql_be != NULL);
328 
329  std::string sql("SELECT * FROM " BUDGET_TABLE);
330  auto stmt = sql_be->create_statement_from_sql(sql);
331  auto result = sql_be->execute_select_statement(stmt);
332  for (auto row : *result)
333  load_single_budget (sql_be, row);
334 
335  std::string pkey(col_table[0]->name());
336  sql = "SELECT DISTINCT ";
337  sql += pkey + " FROM " BUDGET_TABLE;
339  (BookLookupFn)gnc_budget_lookup);
340 }
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...

◆ write()

bool GncSqlBudgetBackend::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 440 of file gnc-budget-sql.cpp.

441 {
442  write_objects_t data;
443 
444  g_return_val_if_fail (sql_be != NULL, FALSE);
445 
446  data.be = sql_be;
447  data.is_ok = TRUE;
448  data.obe = this;
449  qof_collection_foreach (qof_book_get_collection (sql_be->book(), GNC_ID_BUDGET),
450  (QofInstanceForeachCB)do_save, &data);
451 
452  return data.is_ok;
453 }
void(* QofInstanceForeachCB)(QofInstance *, gpointer user_data)
Callback type for qof_collection_foreach.
Definition: qofid.h:146
Data-passing struct for callbacks to qof_object_foreach() used in GncSqlObjectBackend::write().
QofCollection * qof_book_get_collection(const QofBook *book, QofIdType entity_type)
Return The table of entities of the given type.
Definition: qofbook.cpp:521

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