GnuCash  5.6-150-g038405b370+
gnc-sql-object-backend.cpp
1 /***********************************************************************\
2  * gnc-sql-object-backend.hpp: Encapsulate per-class table schema. *
3  * *
4  * Copyright 2016 John Ralls <jralls@ceridwen.us> *
5  * *
6  * This program is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU General Public License as *
8  * published by the Free Software Foundation; either version 2 of *
9  * the License, or (at your option) any later version. *
10  * *
11  * This program is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License *
17  * along with this program; if not, contact: *
18  * *
19  * Free Software Foundation Voice: +1-617-542-5942 *
20  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
21  * Boston, MA 02110-1301, USA gnu@gnu.org *
22 \***********************************************************************/
23 
24 #include <config.h>
25 #include "gnc-sql-object-backend.hpp"
26 #include "gnc-sql-backend.hpp"
27 #include "gnc-sql-column-table-entry.hpp"
28 #include "gnc-slots-sql.h"
29 
30 static QofLogModule log_module = G_LOG_DOMAIN;
31 
32 bool
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 }
72 
73 void
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 }
87 
88 bool
90  QofInstance* inst) const noexcept
91 {
92  return sql_be->object_in_db(m_table_name.c_str(), m_type_name.c_str(),
93  inst, m_col_table);
94 }
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.
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.
const GncGUID * qof_instance_get_guid(gconstpointer inst)
Return the GncGUID of this instance.
#define G_LOG_DOMAIN
Functions providing the SX List as a plugin page.
load and save accounts data to SQL
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.
#define PERR(format, args...)
Log a serious error.
Definition: qoflog.h:244
virtual bool commit(GncSqlBackend *sql_be, QofInstance *inst)
UPDATE/INSERT a single instance of m_type_name into the database.
virtual void create_tables(GncSqlBackend *sql_be)
Conditionally create or update a database table from m_col_table.
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
uint_t get_table_version(const std::string &table_name) const noexcept
Returns the version number for a DB table.
Main SQL backend structure.
bool instance_in_db(const GncSqlBackend *sql_be, QofInstance *inst) const noexcept
Check the presence of an object in the backend&#39;s database.