GnuCash  5.6-150-g038405b370+
gnc-sql-object-backend.hpp
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 #ifndef __GNC_SQL_OBJECT_BACKEND_HPP__
25 #define __GNC_SQL_OBJECT_BACKEND_HPP__
26 
27 #include <qof.h>
28 
29 #include <memory>
30 #include <string>
31 #include <vector>
32 
33 class GncSqlBackend;
35 using GncSqlColumnTableEntryPtr = std::shared_ptr<GncSqlColumnTableEntry>;
36 using EntryVec = std::vector<GncSqlColumnTableEntryPtr>;
37 
38 #define GNC_SQL_BACKEND "gnc:sql:1"
39 
49 {
50 public:
51  GncSqlObjectBackend (int version, const std::string& type,
52  const std::string& table, const EntryVec& vec) :
53  m_table_name{table}, m_version{version}, m_type_name{type},
54  m_col_table(vec) {}
55  virtual ~GncSqlObjectBackend() = default;
60  virtual void load_all (GncSqlBackend* sql_be) = 0;
71  virtual void create_tables (GncSqlBackend* sql_be);
77  virtual bool commit (GncSqlBackend* sql_be, QofInstance* inst);
83  virtual bool write (GncSqlBackend* sql_be) { return true; }
90  const char* type () const noexcept { return m_type_name.c_str(); }
95  const bool is_version (int version) const noexcept {
96  return version == m_version;
97  }
104  bool instance_in_db(const GncSqlBackend* sql_be,
105  QofInstance* inst) const noexcept;
106 protected:
107  const std::string m_table_name;
108  const int m_version;
109  const std::string m_type_name;
110  const EntryVec& m_col_table;
111 };
112 
113 using GncSqlObjectBackendPtr = std::shared_ptr<GncSqlObjectBackend>;
114 
115 using OBEEntry = std::tuple<std::string, GncSqlObjectBackendPtr>;
116 using OBEVec = std::vector<OBEEntry>;
117 
125 {
126  write_objects_t() = default;
127  write_objects_t (GncSqlBackend* sql_be, bool o, GncSqlObjectBackend* e) :
128  be{sql_be}, is_ok{o}, obe{e} {}
129  void commit (QofInstance* inst) {
130  if (is_ok) is_ok = obe->commit (be, inst);
131  }
132  GncSqlBackend* be = nullptr;
133  bool is_ok = false;
134  GncSqlObjectBackend* obe = nullptr;
135 };
136 
137 
138 #endif //__GNC_SQL_OBJECT_BACKEND_HPP__
const EntryVec & m_col_table
The front-end QofIdType.
virtual bool write(GncSqlBackend *sql_be)
Write all objects of m_type_name to the database.
virtual void load_all(GncSqlBackend *sql_be)=0
Load all objects of m_type in the database into memory.
const bool is_version(int version) const noexcept
Compare a version with the compiled version (m_version).
const char * type() const noexcept
Return the m_type_name for the class.
Encapsulates per-class table schema with functions to load, create a table, commit a changed front-en...
virtual bool commit(GncSqlBackend *sql_be, QofInstance *inst)
UPDATE/INSERT a single instance of m_type_name into the database.
Data-passing struct for callbacks to qof_object_foreach() used in GncSqlObjectBackend::write().
virtual void create_tables(GncSqlBackend *sql_be)
Conditionally create or update a database table from m_col_table.
Contains all of the information required to copy information between an object and the database for a...
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.