Branch data Line data Source code
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; 34 : : class GncSqlColumnTableEntry; 35 : : using GncSqlColumnTableEntryPtr = std::shared_ptr<GncSqlColumnTableEntry>; 36 : : using EntryVec = std::vector<GncSqlColumnTableEntryPtr>; 37 : : 38 : : #define GNC_SQL_BACKEND "gnc:sql:1" 39 : : 40 : : /** 41 : : * Encapsulates per-class table schema with functions to load, create a table, 42 : : * commit a changed front-end object (note that database transaction semantics 43 : : * are not yet implemented; edit/commit applies to the front-end object!) and 44 : : * write all front-end objects of the type to the database. Additional functions 45 : : * for creating and running queries existed but were unused and untested. They've 46 : : * been temporarily removed until the front end is ready to use them. 47 : : */ 48 : : class GncSqlObjectBackend 49 : : { 50 : : public: 51 : 200 : GncSqlObjectBackend (int version, const std::string& type, 52 : 200 : const std::string& table, const EntryVec& vec) : 53 : 200 : m_table_name{table}, m_version{version}, m_type_name{type}, 54 : 400 : m_col_table(vec) {} 55 : 200 : virtual ~GncSqlObjectBackend() = default; 56 : : /** 57 : : * Load all objects of m_type in the database into memory. 58 : : * @param sql_be The GncSqlBackend containing the database connection. 59 : : */ 60 : : virtual void load_all (GncSqlBackend* sql_be) = 0; 61 : : /** 62 : : * Conditionally create or update a database table from m_col_table. The 63 : : * condition is the version returned by querying the database's version 64 : : * table: If it's 0 then the table wasn't found and will be created; All 65 : : * tables areat least version 1. If the database's version is less than the 66 : : * compiled version then the table schema is upgraded but the data isn't, 67 : : * that's the engine's responsibility when the object is loaded. If the 68 : : * version is greater than the compiled version then nothing is touched. 69 : : * @param sql_be The GncSqlBackend containing the database connection. 70 : : */ 71 : : virtual void create_tables (GncSqlBackend* sql_be); 72 : : /** 73 : : * UPDATE/INSERT a single instance of m_type_name into the database. 74 : : * @param sql_be The GncSqlBackend containing the database. 75 : : * @param inst The QofInstance to be written out. 76 : : */ 77 : : virtual bool commit (GncSqlBackend* sql_be, QofInstance* inst); 78 : : /** 79 : : * Write all objects of m_type_name to the database. 80 : : * @param sql_be The GncSqlBackend containing the database. 81 : : * @return true if the objects were successfully written, false otherwise. 82 : : */ 83 : 40 : virtual bool write (GncSqlBackend* sql_be) { return true; } 84 : : /** 85 : : * Return the m_type_name for the class. This value is created at 86 : : * compilation time and is called QofIdType or QofIdTypeConst in other parts 87 : : * of GnuCash. Most values are defined in src/engine/gnc-engine.h. 88 : : * @return m_type_name. 89 : : */ 90 : 200 : const char* type () const noexcept { return m_type_name.c_str(); } 91 : : /** 92 : : * Compare a version with the compiled version (m_version). 93 : : * @return true if they match. 94 : : */ 95 : : const bool is_version (int version) const noexcept { 96 : : return version == m_version; 97 : : } 98 : : /** 99 : : * Check the presence of an object in the backend's database. 100 : : * 101 : : * @param sql_be Backend owning the database 102 : : * @param inst QofInstance to be checked. 103 : : */ 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; /// The front-end QofIdType 110 : : const EntryVec& m_col_table; /// The ORM table definition. 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 : : 118 : : /** 119 : : * Data-passing struct for callbacks to qof_object_foreach() used in 120 : : * GncSqlObjectBackend::write(). Once QofCollection is rewritten to use C++ 121 : : * containers we'll use std::foreach() and lambdas instead of callbacks and this 122 : : * can go away. 123 : : */ 124 : : struct write_objects_t 125 : : { 126 : 0 : write_objects_t() = default; 127 : 55 : write_objects_t (GncSqlBackend* sql_be, bool o, GncSqlObjectBackend* e) : 128 : 55 : be{sql_be}, is_ok{o}, obe{e} {} 129 : 17 : void commit (QofInstance* inst) { 130 : 17 : if (is_ok) is_ok = obe->commit (be, inst); 131 : 17 : } 132 : : GncSqlBackend* be = nullptr; 133 : : bool is_ok = false; 134 : : GncSqlObjectBackend* obe = nullptr; 135 : : }; 136 : : 137 : : 138 : : #endif //__GNC_SQL_OBJECT_BACKEND_HPP__