GnuCash  4.12-399-g11d395185c
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 extern "C"
28 {
29 #include <qof.h>
30 }
31 #include <memory>
32 #include <string>
33 #include <vector>
34 
35 class GncSqlBackend;
37 using GncSqlColumnTableEntryPtr = std::shared_ptr<GncSqlColumnTableEntry>;
38 using EntryVec = std::vector<GncSqlColumnTableEntryPtr>;
39 
40 #define GNC_SQL_BACKEND "gnc:sql:1"
41 
51 {
52 public:
53  GncSqlObjectBackend (int version, const std::string& type,
54  const std::string& table, const EntryVec& vec) :
55  m_table_name{table}, m_version{version}, m_type_name{type},
56  m_col_table(vec) {}
57  virtual ~GncSqlObjectBackend() = default;
62  virtual void load_all (GncSqlBackend* sql_be) = 0;
73  virtual void create_tables (GncSqlBackend* sql_be);
79  virtual bool commit (GncSqlBackend* sql_be, QofInstance* inst);
85  virtual bool write (GncSqlBackend* sql_be) { return true; }
92  const char* type () const noexcept { return m_type_name.c_str(); }
97  const bool is_version (int version) const noexcept {
98  return version == m_version;
99  }
106  bool instance_in_db(const GncSqlBackend* sql_be,
107  QofInstance* inst) const noexcept;
108 protected:
109  const std::string m_table_name;
110  const int m_version;
111  const std::string m_type_name;
112  const EntryVec& m_col_table;
113 };
114 
115 using GncSqlObjectBackendPtr = std::shared_ptr<GncSqlObjectBackend>;
116 
117 using OBEEntry = std::tuple<std::string, GncSqlObjectBackendPtr>;
118 using OBEVec = std::vector<OBEEntry>;
119 
127 {
128  write_objects_t() = default;
129  write_objects_t (GncSqlBackend* sql_be, bool o, GncSqlObjectBackend* e) :
130  be{sql_be}, is_ok{o}, obe{e} {}
131  void commit (QofInstance* inst) {
132  if (is_ok) is_ok = obe->commit (be, inst);
133  }
134  GncSqlBackend* be = nullptr;
135  bool is_ok = false;
136  GncSqlObjectBackend* obe = nullptr;
137 };
138 
139 
140 #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.