GnuCash  5.6-150-g038405b370+
Public Member Functions | Protected Attributes
GncSqlObjectBackend Class Referenceabstract

Encapsulates per-class table schema with functions to load, create a table, commit a changed front-end object (note that database transaction semantics are not yet implemented; edit/commit applies to the front-end object!) and write all front-end objects of the type to the database. More...

#include <gnc-sql-object-backend.hpp>

Inheritance diagram for GncSqlObjectBackend:
GncSqlAccountBackend GncSqlBillTermBackend GncSqlBookBackend GncSqlBudgetBackend GncSqlCommodityBackend GncSqlCustomerBackend GncSqlEmployeeBackend GncSqlEntryBackend GncSqlInvoiceBackend GncSqlJobBackend GncSqlLotsBackend GncSqlOrderBackend GncSqlPriceBackend GncSqlRecurrenceBackend GncSqlSchedXactionBackend GncSqlSlotsBackend GncSqlSplitBackend GncSqlTaxTableBackend GncSqlTransBackend GncSqlVendorBackend

Public Member Functions

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

Protected Attributes

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

Encapsulates per-class table schema with functions to load, create a table, commit a changed front-end object (note that database transaction semantics are not yet implemented; edit/commit applies to the front-end object!) and write all front-end objects of the type to the database.

Additional functions for creating and running queries existed but were unused and untested. They've been temporarily removed until the front end is ready to use them.

Definition at line 48 of file gnc-sql-object-backend.hpp.

Member Function Documentation

◆ commit()

bool GncSqlObjectBackend::commit ( GncSqlBackend sql_be,
QofInstance inst 
)
virtual

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 in GncSqlSplitBackend, GncSqlSlotsBackend, GncSqlRecurrenceBackend, GncSqlCommodityBackend, GncSqlTransBackend, GncSqlEmployeeBackend, GncSqlInvoiceBackend, GncSqlTaxTableBackend, GncSqlBudgetBackend, GncSqlPriceBackend, GncSqlSchedXactionBackend, GncSqlVendorBackend, and GncSqlAccountBackend.

Definition at line 33 of file gnc-sql-object-backend.cpp.

34 {
35  const GncGUID* guid;
36  gboolean is_infant;
37  E_DB_OPERATION op;
38  gboolean is_ok;
39 
40  is_infant = qof_instance_get_infant (inst);
41  if (qof_instance_get_destroying (inst))
42  {
43  op = OP_DB_DELETE;
44  }
45  else if (sql_be->pristine() || is_infant)
46  {
47  op = OP_DB_INSERT;
48  }
49  else
50  {
51  op = OP_DB_UPDATE;
52  }
53  is_ok = sql_be->do_db_operation(op, m_table_name.c_str(),
54  m_type_name.c_str(), inst, m_col_table);
55 
56  if (is_ok)
57  {
58  // Now, commit any slots
59  guid = qof_instance_get_guid (inst);
60  if (!qof_instance_get_destroying (inst))
61  {
62  is_ok = gnc_sql_slots_save (sql_be, guid, is_infant, inst);
63  }
64  else
65  {
66  is_ok = gnc_sql_slots_delete (sql_be, guid);
67  }
68  }
69 
70  return is_ok;
71 }
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 EntryVec & m_col_table
The front-end QofIdType.
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 GncSqlObjectBackend::create_tables ( GncSqlBackend sql_be)
virtual

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 in GncSqlSplitBackend, GncSqlSlotsBackend, GncSqlRecurrenceBackend, GncSqlTransBackend, GncSqlBillTermBackend, GncSqlCustomerBackend, GncSqlEmployeeBackend, GncSqlEntryBackend, GncSqlInvoiceBackend, GncSqlTaxTableBackend, GncSqlBudgetBackend, GncSqlLotsBackend, and GncSqlPriceBackend.

Definition at line 74 of file gnc-sql-object-backend.cpp.

75 {
76  g_return_if_fail (sql_be != nullptr);
77  int version = sql_be->get_table_version (m_table_name);
78  if (version == 0) //No tables, otherwise version will sql_be >= 1.
79  {
80  sql_be->create_table(m_table_name, m_col_table);
81  sql_be->set_table_version(m_table_name, m_version);
82  }
83  else if (version != m_version)
84  PERR("Version mismatch in table %s, expecting %d but backend is %d."
85  "Table creation aborted.", m_table_name.c_str(), m_version, version);
86 }
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.
const EntryVec & m_col_table
The front-end QofIdType.
#define PERR(format, args...)
Log a serious error.
Definition: qoflog.h:244
uint_t get_table_version(const std::string &table_name) const noexcept
Returns the version number for a DB table.

◆ instance_in_db()

bool GncSqlObjectBackend::instance_in_db ( const GncSqlBackend sql_be,
QofInstance inst 
) const
noexcept

Check the presence of an object in the backend's database.

Parameters
sql_beBackend owning the database
instQofInstance to be checked.

Definition at line 89 of file gnc-sql-object-backend.cpp.

91 {
92  return sql_be->object_in_db(m_table_name.c_str(), m_type_name.c_str(),
93  inst, m_col_table);
94 }
const EntryVec & m_col_table
The front-end QofIdType.
bool object_in_db(const char *table_name, QofIdTypeConst obj_name, const gpointer pObject, const EntryVec &table) const noexcept
Checks whether an object is in the database or not.

◆ is_version()

const bool GncSqlObjectBackend::is_version ( int  version) const
inlinenoexcept

Compare a version with the compiled version (m_version).

Returns
true if they match.

Definition at line 95 of file gnc-sql-object-backend.hpp.

95  {
96  return version == m_version;
97  }

◆ load_all()

virtual void GncSqlObjectBackend::load_all ( GncSqlBackend sql_be)
pure virtual

◆ type()

const char* GncSqlObjectBackend::type ( ) const
inlinenoexcept

Return the m_type_name for the class.

This value is created at compilation time and is called QofIdType or QofIdTypeConst in other parts of GnuCash. Most values are defined in src/engine/gnc-engine.h.

Returns
m_type_name.

Definition at line 90 of file gnc-sql-object-backend.hpp.

90 { return m_type_name.c_str(); }

◆ write()

virtual bool GncSqlObjectBackend::write ( GncSqlBackend sql_be)
inlinevirtual

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 in GncSqlEmployeeBackend, GncSqlInvoiceBackend, GncSqlTaxTableBackend, GncSqlBillTermBackend, GncSqlBudgetBackend, GncSqlCustomerBackend, GncSqlEntryBackend, GncSqlPriceBackend, GncSqlVendorBackend, GncSqlJobBackend, GncSqlLotsBackend, and GncSqlOrderBackend.

Definition at line 83 of file gnc-sql-object-backend.hpp.

83 { return true; }

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