GnuCash  4.11-517-g41de4cefce
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 50 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, GncSqlCommodityBackend, GncSqlRecurrenceBackend, GncSqlTransBackend, GncSqlSchedXactionBackend, GncSqlEmployeeBackend, GncSqlInvoiceBackend, GncSqlTaxTableBackend, GncSqlBudgetBackend, GncSqlPriceBackend, GncSqlVendorBackend, and GncSqlAccountBackend.

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

37 {
38  const GncGUID* guid;
39  gboolean is_infant;
40  E_DB_OPERATION op;
41  gboolean is_ok;
42 
43  is_infant = qof_instance_get_infant (inst);
44  if (qof_instance_get_destroying (inst))
45  {
46  op = OP_DB_DELETE;
47  }
48  else if (sql_be->pristine() || is_infant)
49  {
50  op = OP_DB_INSERT;
51  }
52  else
53  {
54  op = OP_DB_UPDATE;
55  }
56  is_ok = sql_be->do_db_operation(op, m_table_name.c_str(),
57  m_type_name.c_str(), inst, m_col_table);
58 
59  if (is_ok)
60  {
61  // Now, commit any slots
62  guid = qof_instance_get_guid (inst);
63  if (!qof_instance_get_destroying (inst))
64  {
65  is_ok = gnc_sql_slots_save (sql_be, guid, is_infant, inst);
66  }
67  else
68  {
69  is_ok = gnc_sql_slots_delete (sql_be, guid);
70  }
71  }
72 
73  return is_ok;
74 }
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 77 of file gnc-sql-object-backend.cpp.

78 {
79  g_return_if_fail (sql_be != nullptr);
80  int version = sql_be->get_table_version (m_table_name);
81  if (version == 0) //No tables, otherwise version will sql_be >= 1.
82  {
83  sql_be->create_table(m_table_name, m_col_table);
84  sql_be->set_table_version(m_table_name, m_version);
85  }
86  else if (version != m_version)
87  PERR("Version mismatch in table %s, expecting %d but backend is %d."
88  "Table creation aborted.", m_table_name.c_str(), m_version, version);
89 }
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 92 of file gnc-sql-object-backend.cpp.

94 {
95  return sql_be->object_in_db(m_table_name.c_str(), m_type_name.c_str(),
96  inst, m_col_table);
97 }
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 97 of file gnc-sql-object-backend.hpp.

97  {
98  return version == m_version;
99  }

◆ 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 92 of file gnc-sql-object-backend.hpp.

92 { 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 85 of file gnc-sql-object-backend.hpp.

85 { return true; }

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