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

Public Member Functions

void load_all (GncSqlBackend *) override
 Load all objects of m_type in the database into memory. More...
 
bool commit (GncSqlBackend *, QofInstance *) override
 UPDATE/INSERT a single instance of m_type_name into the database. More...
 
- Public Member Functions inherited from GncSqlObjectBackend
 GncSqlObjectBackend (int version, const std::string &type, const std::string &table, const EntryVec &vec)
 
virtual void create_tables (GncSqlBackend *sql_be)
 Conditionally create or update a database table from m_col_table. More...
 
virtual bool write (GncSqlBackend *sql_be)
 Write all objects of m_type_name to the database. More...
 
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-account-sql.h.

Member Function Documentation

◆ commit()

bool GncSqlAccountBackend::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 278 of file gnc-account-sql.cpp.

279 {
280  Account* pAcc = GNC_ACCOUNT (inst);
281  const GncGUID* guid;
282  gboolean is_infant;
283  gboolean is_ok = FALSE;
284  gnc_commodity* commodity;
285  E_DB_OPERATION op;
286 
287  g_return_val_if_fail (sql_be != NULL, FALSE);
288  g_return_val_if_fail (inst != NULL, FALSE);
289  g_return_val_if_fail (GNC_IS_ACCOUNT (inst), FALSE);
290 
291  ENTER ("inst=%p", inst);
292 
293  is_infant = qof_instance_get_infant (inst);
294 
295  // If there is no commodity yet, this might be because a new account name
296  // has been entered directly into the register and an account window will
297  // be opened. The account info is not complete yet, but the name has been
298  // set, triggering this commit
299  commodity = xaccAccountGetCommodity (pAcc);
300 
301  is_ok = TRUE;
302  if (qof_instance_get_destroying (inst))
303  {
304  op = OP_DB_DELETE;
305  }
306  else if (sql_be->pristine() || is_infant)
307  {
308  op = OP_DB_INSERT;
309  }
310  else
311  {
312  op = OP_DB_UPDATE;
313  }
314 
315  // If not deleting the account, ensure the commodity is in the db
316  if (op != OP_DB_DELETE && commodity != NULL)
317  {
318  is_ok = sql_be->save_commodity(commodity);
319  }
320 
321  if (is_ok)
322  {
323  is_ok = sql_be->do_db_operation (op, TABLE_NAME, GNC_ID_ACCOUNT, pAcc,
324  col_table);
325  }
326 
327  if (is_ok)
328  {
329  // Now, commit or delete any slots
330  guid = qof_instance_get_guid (inst);
331  if (!qof_instance_get_destroying (inst))
332  {
333  is_ok = gnc_sql_slots_save (sql_be, guid, is_infant, inst);
334  }
335  else
336  {
337  is_ok = gnc_sql_slots_delete (sql_be, guid);
338  }
339  }
340 
341  LEAVE ("is_ok=%d", is_ok);
342 
343  return is_ok;
344 }
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.
STRUCTS.
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.
#define ENTER(format, args...)
Print a function entry debugging message.
Definition: qoflog.h:272
bool save_commodity(gnc_commodity *comm) noexcept
Ensure that a commodity referenced in another object is in fact saved in the database.
gnc_commodity * xaccAccountGetCommodity(const Account *acc)
Get the account's commodity.
Definition: Account.cpp:3413
#define LEAVE(format, args...)
Print a function exit debugging message.
Definition: qoflog.h:282
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

◆ load_all()

void GncSqlAccountBackend::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 210 of file gnc-account-sql.cpp.

211 {
212  QofBook* pBook;
213  ParentGuidVec l_accounts_needing_parents;
214  g_return_if_fail (sql_be != NULL);
215 
216  ENTER ("");
217 
218  pBook = sql_be->book();
219 
220  std::string sql("SELECT * FROM " TABLE_NAME);
221  auto stmt = sql_be->create_statement_from_sql(sql);
222  auto result = sql_be->execute_select_statement(stmt);
223  for (auto row : *result)
224  load_single_account (sql_be, row, l_accounts_needing_parents);
225 
226  sql = "SELECT DISTINCT guid FROM " TABLE_NAME;
228  (BookLookupFn)xaccAccountLookup);
229 
230  /* While there are items on the list of accounts needing parents,
231  try to see if the parent has now been loaded. Theory says that if
232  items are removed from the front and added to the back if the
233  parent is still not available, then eventually, the list will
234  shrink to size 0. */
235  if (!l_accounts_needing_parents.empty())
236  {
237  auto progress_made = true;
238  std::reverse(l_accounts_needing_parents.begin(),
239  l_accounts_needing_parents.end());
240  auto end = l_accounts_needing_parents.end();
241  while (progress_made)
242  {
243  progress_made = false;
244  end = std::remove_if(l_accounts_needing_parents.begin(), end,
245  [&](ParentGuidPtr s)
246  {
247  auto pParent = xaccAccountLookup (&s->guid,
248  sql_be->book());
249  if (pParent != nullptr)
250  {
251  gnc_account_append_child (pParent,
252  s->pAccount);
253  progress_made = true;
254  delete s;
255  return true;
256  }
257  return false;
258  });
259  }
260 
261  /* Any non-ROOT accounts left over must be parented by the root account */
262  auto root = gnc_book_get_root_account (pBook);
263  end = std::remove_if(l_accounts_needing_parents.begin(), end,
264  [&](ParentGuidPtr s)
265  {
266  if (xaccAccountGetType (s->pAccount) != ACCT_TYPE_ROOT)
267  gnc_account_append_child (root, s->pAccount);
268  delete s;
269  return true;
270  });
271  }
272 
273  LEAVE ("");
274 }
GncSqlResultPtr execute_select_statement(const GncSqlStatementPtr &stmt) const noexcept
Executes an SQL SELECT statement and returns the result rows.
void gnc_account_append_child(Account *new_parent, Account *child)
This function will remove from the child account any pre-existing parent relationship, and will then add the account as a child of the new parent.
Definition: Account.cpp:2776
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...
GNCAccountType xaccAccountGetType(const Account *acc)
Returns the account's account type.
Definition: Account.cpp:3228
#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
The hidden root account of an account tree.
Definition: Account.h:153
Account * xaccAccountLookup(const GncGUID *guid, QofBook *book)
The xaccAccountLookup() subroutine will return the account associated with the given id...
Definition: Account.cpp:2048

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