GnuCash de0bfde+
Loading...
Searching...
No Matches
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.
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.
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.
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-bill-term-sql.h.

Constructor & Destructor Documentation

◆ GncSqlBillTermBackend()

GncSqlBillTermBackend::GncSqlBillTermBackend ( )

Definition at line 102 of file gnc-bill-term-sql.cpp.

102 :
103 GncSqlObjectBackend(TABLE_VERSION, GNC_ID_BILLTERM,
104 TABLE_NAME, col_table) {}

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 307 of file gnc-bill-term-sql.cpp.

308{
309 gint version;
310
311 g_return_if_fail (sql_be != NULL);
312
313 version = sql_be->get_table_version( TABLE_NAME);
314 if (version == 0)
315 {
316 sql_be->create_table(TABLE_NAME, TABLE_VERSION, col_table);
317 }
318 else if (version < m_version)
319 {
320 /* Upgrade 64 bit int handling */
321 sql_be->upgrade_table(TABLE_NAME, col_table);
322 sql_be->set_table_version (TABLE_NAME, TABLE_VERSION);
323
324 PINFO ("Billterms table upgraded from version 1 to version %d\n",
325 TABLE_VERSION);
326 }
327}
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 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 std::string pkey(col_table[0]->name());
247 sql = "SELECT DISTINCT ";
248 sql += pkey + " FROM " TABLE_NAME;
249 gnc_sql_slots_load_for_sql_subquery (sql_be, sql,
250 (BookLookupFn)gnc_billterm_lookup);
251
252 /* While there are items on the list of billterms needing parents,
253 try to see if the parent has now been loaded. Theory says that if
254 items are removed from the front and added to the back if the
255 parent is still not available, then eventually, the list will
256 shrink to size 0. */
257 if (!l_billterms_needing_parents.empty())
258 {
259 bool progress_made = true;
260 std::reverse(l_billterms_needing_parents.begin(),
261 l_billterms_needing_parents.end());
262 auto end = l_billterms_needing_parents.end();
263 while (progress_made)
264 {
265 progress_made = false;
266 end = std::remove_if(l_billterms_needing_parents.begin(), end,
267 [&](BillTermParentGuidPtr s)
268 {
269 auto pBook = qof_instance_get_book (QOF_INSTANCE (s->billterm));
270 auto parent = gncBillTermLookup (pBook,
271 &s->guid);
272 if (parent != nullptr)
273 {
274 gncBillTermSetParent (s->billterm, parent);
275 gncBillTermSetChild (parent, s->billterm);
276 progress_made = true;
277 delete s;
278 return true;
279 }
280 return false;
281 });
282 }
283 }
284}
GncSqlResultPtr execute_select_statement(const GncSqlStatementPtr &stmt) const noexcept
Executes an SQL SELECT statement and returns the result rows.

◆ 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 296 of file gnc-bill-term-sql.cpp.

297{
298 g_return_val_if_fail (sql_be != NULL, FALSE);
299
300 write_objects_t data {sql_be, true, this};
301 qof_object_foreach (GNC_ID_BILLTERM, sql_be->book(), do_save_billterm, &data);
302 return data.is_ok;
303}
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.

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