GnuCash  4.12-527-g05ffd3d4eb
Public Member Functions
GncSqlVendorBackend Class Reference
Inheritance diagram for GncSqlVendorBackend:
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...
 
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 void create_tables (GncSqlBackend *sql_be)
 Conditionally create or update a database table from m_col_table. 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-vendor-sql.h.

Member Function Documentation

◆ commit()

bool GncSqlVendorBackend::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 137 of file gnc-vendor-sql.cpp.

138 {
139  GncVendor* v;
140  const GncGUID* guid;
141  E_DB_OPERATION op;
142  gboolean is_infant;
143  gboolean is_ok = TRUE;
144 
145  g_return_val_if_fail (inst != NULL, FALSE);
146  g_return_val_if_fail (GNC_IS_VENDOR (inst), FALSE);
147  g_return_val_if_fail (sql_be != NULL, FALSE);
148 
149  v = GNC_VENDOR (inst);
150 
151  is_infant = qof_instance_get_infant (inst);
152  if (qof_instance_get_destroying (inst))
153  {
154  op = OP_DB_DELETE;
155  }
156  else if (sql_be->pristine() || is_infant)
157  {
158  op = OP_DB_INSERT;
159  }
160  else
161  {
162  op = OP_DB_UPDATE;
163  }
164  if (op != OP_DB_DELETE)
165  {
166  // Ensure the commodity is in the db
167  is_ok = sql_be->save_commodity (gncVendorGetCurrency(v));
168  }
169  if (is_ok)
170  {
171  is_ok = sql_be->do_db_operation(op, TABLE_NAME, GNC_ID_VENDOR, v,
172  col_table);
173  }
174 
175  if (is_ok)
176  {
177  // Now, commit or delete any slots
178  guid = qof_instance_get_guid (inst);
179  if (!qof_instance_get_destroying (inst))
180  {
181  is_ok = gnc_sql_slots_save (sql_be, guid, is_infant, inst);
182  }
183  else
184  {
185  is_ok = gnc_sql_slots_delete (sql_be, guid);
186  }
187  }
188 
189  return is_ok;
190 }
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.
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.
bool save_commodity(gnc_commodity *comm) noexcept
Ensure that a commodity referenced in another object is in fact saved in the database.
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 GncSqlVendorBackend::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 117 of file gnc-vendor-sql.cpp.

118 {
119  g_return_if_fail (sql_be != NULL);
120 
121  std::string sql("SELECT * FROM " TABLE_NAME);
122  auto stmt = sql_be->create_statement_from_sql(sql);
123  auto result = sql_be->execute_select_statement(stmt);
124 
125  for (auto row : *result)
126  load_single_vendor (sql_be, row);
127 
128  std::string pkey(col_table[0]->name());
129  sql = "SELECT DISTINCT ";
130  sql += pkey + " FROM " TABLE_NAME;
132  (BookLookupFn)gnc_vendor_lookup);
133 }
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...

◆ write()

bool GncSqlVendorBackend::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 226 of file gnc-vendor-sql.cpp.

227 {
228  g_return_val_if_fail (sql_be != NULL, FALSE);
229  write_objects_t data{sql_be, true, this};
230 
231  qof_object_foreach (GNC_ID_VENDOR, sql_be->book(), write_single_vendor, &data);
232 
233  return data.is_ok;
234 }
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.
Definition: qofobject.cpp:197
Data-passing struct for callbacks to qof_object_foreach() used in GncSqlObjectBackend::write().

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