GnuCash  5.6-150-g038405b370+
Public Member Functions
GncSqlEmployeeBackend Class Reference
Inheritance diagram for GncSqlEmployeeBackend:
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 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)
 
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-employee-sql.h.

Member Function Documentation

◆ commit()

bool GncSqlEmployeeBackend::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 155 of file gnc-employee-sql.cpp.

156 {
157  GncEmployee* emp;
158  const GncGUID* guid;
159  E_DB_OPERATION op;
160  gboolean is_infant;
161  gboolean is_ok = TRUE;
162 
163  g_return_val_if_fail (inst != NULL, FALSE);
164  g_return_val_if_fail (GNC_IS_EMPLOYEE (inst), FALSE);
165  g_return_val_if_fail (sql_be != NULL, FALSE);
166 
167  emp = GNC_EMPLOYEE (inst);
168 
169  is_infant = qof_instance_get_infant (inst);
170  if (qof_instance_get_destroying (inst))
171  {
172  op = OP_DB_DELETE;
173  }
174  else if (sql_be->pristine() || is_infant)
175  {
176  op = OP_DB_INSERT;
177  }
178  else
179  {
180  op = OP_DB_UPDATE;
181  }
182  if (op != OP_DB_DELETE)
183  {
184  // Ensure the commodity is in the db
185  is_ok = sql_be->save_commodity(gncEmployeeGetCurrency (emp));
186  }
187 
188  if (is_ok)
189  {
190  is_ok = sql_be->do_db_operation(op, TABLE_NAME, GNC_ID_EMPLOYEE, emp,
191  col_table);
192  }
193 
194  if (is_ok)
195  {
196  // Now, commit or delete any slots
197  guid = qof_instance_get_guid (inst);
198  if (!qof_instance_get_destroying (inst))
199  {
200  is_ok = gnc_sql_slots_save (sql_be, guid, is_infant, inst);
201  }
202  else
203  {
204  is_ok = gnc_sql_slots_delete (sql_be, guid);
205  }
206  }
207 
208  return is_ok;
209 }
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

◆ create_tables()

void GncSqlEmployeeBackend::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 131 of file gnc-employee-sql.cpp.

132 {
133  gint version;
134 
135  g_return_if_fail (sql_be != NULL);
136 
137  version = sql_be->get_table_version( TABLE_NAME);
138  if (version == 0)
139  {
140  sql_be->create_table(TABLE_NAME, TABLE_VERSION, col_table);
141  }
142  else if (version < m_version)
143  {
144  /* Upgrade 64 bit int handling */
145  sql_be->upgrade_table(TABLE_NAME, col_table);
146  sql_be->set_table_version (TABLE_NAME, TABLE_VERSION);
147 
148  PINFO ("Employees table upgraded from version 1 to version %d\n",
149  TABLE_VERSION);
150  }
151 }
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 GncSqlEmployeeBackend::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 110 of file gnc-employee-sql.cpp.

111 {
112  g_return_if_fail (sql_be != NULL);
113 
114  std::string sql("SELECT * FROM " TABLE_NAME);
115  auto stmt = sql_be->create_statement_from_sql(sql);
116  auto result = sql_be->execute_select_statement(stmt);
117 
118  for (auto row : *result)
119  load_single_employee (sql_be, row);
120 
121  std::string pkey(col_table[0]->name());
122  sql = "SELECT DISTINCT ";
123  sql += pkey + " FROM " TABLE_NAME;
125  (BookLookupFn)gnc_employee_lookup);
126 }
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 GncSqlEmployeeBackend::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 245 of file gnc-employee-sql.cpp.

246 {
247  write_objects_t data;
248 
249  g_return_val_if_fail (sql_be != NULL, FALSE);
250 
251  data.be = sql_be;
252  data.is_ok = TRUE;
253  data.obe = this;
254  qof_object_foreach (GNC_ID_EMPLOYEE, sql_be->book(), write_single_employee, &data);
255 
256  return data.is_ok;
257 }
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: