GnuCash  5.6-150-g038405b370+
Public Member Functions
GncSqlBillTermBackend Class Reference
Inheritance diagram for GncSqlBillTermBackend:
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 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)
 
virtual bool commit (GncSqlBackend *sql_be, QofInstance *inst)
 UPDATE/INSERT a single instance of m_type_name into 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 35 of file gnc-bill-term-sql.h.

Member Function Documentation

◆ create_tables()

void GncSqlBillTermBackend::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 308 of file gnc-bill-term-sql.cpp.

309 {
310  gint version;
311 
312  g_return_if_fail (sql_be != NULL);
313 
314  version = sql_be->get_table_version( TABLE_NAME);
315  if (version == 0)
316  {
317  sql_be->create_table(TABLE_NAME, TABLE_VERSION, col_table);
318  }
319  else if (version < m_version)
320  {
321  /* Upgrade 64 bit int handling */
322  sql_be->upgrade_table(TABLE_NAME, col_table);
323  sql_be->set_table_version (TABLE_NAME, TABLE_VERSION);
324 
325  PINFO ("Billterms table upgraded from version 1 to version %d\n",
326  TABLE_VERSION);
327  }
328 }
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.
#define PINFO(format, args...)
Print an informational note.
Definition: qoflog.h:256
void upgrade_table(const std::string &table_name, const EntryVec &col_table) noexcept
Upgrades a table to a new structure.
uint_t get_table_version(const std::string &table_name) const noexcept
Returns the version number for a DB table.

◆ load_all()

void GncSqlBillTermBackend::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 232 of file gnc-bill-term-sql.cpp.

233 {
234 
235  g_return_if_fail (sql_be != NULL);
236 
237  std::string sql("SELECT * FROM " TABLE_NAME);
238  auto stmt = sql_be->create_statement_from_sql(sql);
239  auto result = sql_be->execute_select_statement(stmt);
240  BillTermParentGuidVec l_billterms_needing_parents;
241 
242  for (auto row : *result)
243  {
244  load_single_billterm (sql_be, row, l_billterms_needing_parents);
245  }
246  delete result;
247  std::string pkey(col_table[0]->name());
248  sql = "SELECT DISTINCT ";
249  sql += pkey + " FROM " TABLE_NAME;
251  (BookLookupFn)gnc_billterm_lookup);
252 
253  /* While there are items on the list of billterms needing parents,
254  try to see if the parent has now been loaded. Theory says that if
255  items are removed from the front and added to the back if the
256  parent is still not available, then eventually, the list will
257  shrink to size 0. */
258  if (!l_billterms_needing_parents.empty())
259  {
260  bool progress_made = true;
261  std::reverse(l_billterms_needing_parents.begin(),
262  l_billterms_needing_parents.end());
263  auto end = l_billterms_needing_parents.end();
264  while (progress_made)
265  {
266  progress_made = false;
267  end = std::remove_if(l_billterms_needing_parents.begin(), end,
268  [&](BillTermParentGuidPtr s)
269  {
270  auto pBook = qof_instance_get_book (QOF_INSTANCE (s->billterm));
271  auto parent = gncBillTermLookup (pBook,
272  &s->guid);
273  if (parent != nullptr)
274  {
275  gncBillTermSetParent (s->billterm, parent);
276  gncBillTermSetChild (parent, s->billterm);
277  progress_made = true;
278  delete s;
279  return true;
280  }
281  return false;
282  });
283  }
284  }
285 }
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...
QofBook * qof_instance_get_book(gconstpointer inst)
Return the book pointer.

◆ write()

bool GncSqlBillTermBackend::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 297 of file gnc-bill-term-sql.cpp.

298 {
299  g_return_val_if_fail (sql_be != NULL, FALSE);
300 
301  write_objects_t data {sql_be, true, this};
302  qof_object_foreach (GNC_ID_BILLTERM, sql_be->book(), do_save_billterm, &data);
303  return data.is_ok;
304 }
void qof_object_foreach(QofIdTypeConst type_name, QofBook *book, QofInstanceForeachCB cb, gpointer user_data)
Invoke the callback &#39;cb&#39; on every instance ov a particular object type.
Definition: qofobject.cpp:185
Data-passing struct for callbacks to qof_object_foreach() used in GncSqlObjectBackend::write().

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