GnuCash  5.6-150-g038405b370+
qof-backend.hpp
1 /********************************************************************\
2  * qof-backend.hpp Declare QofBackend class *
3  * Copyright 2016 John Ralls <jralls@ceridwen.us> *
4  * *
5  * This program is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU General Public License as *
7  * published by the Free Software Foundation; either version 2 of *
8  * the License, or (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License*
16  * along with this program; if not, contact: *
17  * *
18  * Free Software Foundation Voice: +1-617-542-5942 *
19  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
20  * Boston, MA 02110-1301, USA gnu@gnu.org *
21  * *
22 \********************************************************************/
42 #ifndef __QOF_BACKEND_HPP__
43 #define __QOF_BACKEND_HPP__
44 
45 #include "qofbackend.h"
46 #include "qofbook.h"
47 #include "qofquery.h"
48 #include "qofsession.h"
49 #include <gmodule.h>
50 
51 #include "qofinstance-p.h"
52 #include <string>
53 #include <algorithm>
54 #include <vector>
55 /* NOTE: The following comments were musings by the original developer about how
56  * some additional API might work. The compile/free/run_query functions were
57  * implemented for the DBI backend but never put into use; the rest were never
58  * implemented. They're here as something to consider if we ever decide to
59  * implement them.
60  *
61  * The compile_query() method compiles a QOF query object into
62  * a backend-specific data structure and returns the compiled
63  * query. For an SQL backend, the contents of the query object
64  * need to be turned into a corresponding SQL query statement, and
65  * sent to the database for evaluation.
66  *
67  * The free_query() method frees the data structure returned from
68  * compile_query()
69  *
70  * The run_query() callback takes a compiled query (generated by
71  * compile_query) and runs the query in across the backend,
72  * inserting the responses into the engine. The database will
73  * return a set of splits and transactions and this callback needs
74  * to poke these into the account-group hierarchy held by the query
75  * object.
76  *
77  * For a network-communications backend, essentially the same is
78  * done, except that this routine would convert the query to wire
79  * protocol, get an answer from the remote server, and push that
80  * into the account-group object.
81  *
82  * The returned list of entities can be used to build a local
83  * cache of the matching data. This will allow the QOF client to
84  * continue functioning even when disconnected from the server:
85  * this is because it will have its local cache of data from which to work.
86  *
87  * The events_pending() routines should return true if there are
88  * external events which need to be processed to bring the
89  * engine up to date with the backend.
90  *
91  * The process_events() routine should process any events indicated
92  * by the events_pending() routine. It should return TRUE if
93  * the engine was changed while engine events were suspended.
94  *
95  * For support of book partitioning, use special "Book" begin_edit()
96  * and commit_edit() QOF_ID types.
97  *
98  * Call the book begin() at the beginning of a book partitioning. A
99  * 'partitioning' is the splitting off of a chunk of the current
100  * book into a second book by means of a query. Every transaction
101  * in that query is to be moved ('transferred') to the second book
102  * from the existing book. The argument of this routine is a
103  * pointer to the second book, where the results of the query
104  * should go.
105  *
106  * Call the book commit() to complete the book partitioning.
107  *
108  * After the begin(), there will be a call to run_query(), followed
109  * probably by a string of object calls, and completed by commit().
110  * It should be explicitly understood that the results of that
111  * run_query() precisely constitute the set of objects that are to
112  * be moved between the initial and the new book. This specification
113  * can be used by a clever backend to avoid excess data movement
114  * between the server and the QOF client, as explained below.
115  *
116  * There are several possible ways in which a backend may choose to
117  * implement the book splitting process. A 'file-type' backend may
118  * choose to ignore this call, and the subsequent query, and simply
119  * write out the new book to a file when the commit() call is made.
120  * By that point, the engine will have performed all of the
121  * nitty-gritty of moving transactions from one book to the other.
122  *
123  * A 'database-type' backend has several interesting choices. One
124  * simple choice is to simply perform the run_query() as it
125  * normally would, and likewise treat the object edits as usual.
126  * In this scenario, the commit() is more or less a no-op.
127  * This implementation has a drawback, however: the run_query() may
128  * cause the transfer of a <b>huge</b> amount of data between the backend
129  * and the engine. For a large dataset, this is quite undesirable.
130  * In addition, there are risks associated with the loss of network
131  * connectivity during the transfer; thus a partition might terminate
132  * half-finished, in some indeterminate state, due to network errors.
133  * It might be difficult to recover from such errors: the engine does
134  * not take any special safety measures during the transfer.
135  *
136  * Thus, for a large database, an alternate implementation
137  * might be to use the run_query() call as an opportunity to
138  * transfer entities between the two books in the database,
139  * and not actually return any new data to the engine. In
140  * this scenario, the engine will attempt to transfer those
141  * entities that it does know about. It does not, however,
142  * need to know about all the other entities that also would
143  * be transferred over. In this way, a backend could perform
144  * a mass transfer of entities between books without having
145  * to actually move much (or any) data to the engine.
146  *
147  * To support configuration options from the frontend, the backend
148  * can be passed a KvpFrame - according to the allowed options
149  * for that backend, using load_config(). Configuration can be
150  * updated at any point - it is up to the frontend to load the
151  * data in time for whatever the backend needs to do. e.g. an
152  * option to save a new book in a compressed format need not be
153  * loaded until the backend is about to save. If the configuration
154  * is updated by the user, the frontend should call load_config
155  * again to update the backend.
156  *
157  * Backends are responsible for ensuring that any supported
158  * configuration options are initialised to usable values.
159  * This should be done in the function called from backend_new.
160  */
161 
162 
163 typedef enum
164 {
165  LOAD_TYPE_INITIAL_LOAD,
166  LOAD_TYPE_LOAD_ALL
167 } QofBackendLoadType;
168 
169 using GModuleVec = std::vector<GModule*>;
171 {
172 public:
173  /* For reasons that aren't a bit clear, using the default constructor
174  * sometimes initializes m_last_err incorrectly with Xcode8 and a 32-bit
175  * build unless the initialization is stepped-through in a debugger.
176  */
177  QofBackend() :
178  m_percentage{nullptr}, m_fullpath{}, m_last_err{ERR_BACKEND_NO_ERR},
179  m_error_msg{} {}
180  QofBackend(const QofBackend&) = delete;
181  QofBackend(const QofBackend&&) = delete;
182  virtual ~QofBackend() = default;
189  virtual void session_begin(QofSession *session, const char* new_uri,
190  SessionOpenMode mode) = 0;
191  virtual void session_end() = 0;
212  virtual void load (QofBook*, QofBackendLoadType) = 0;
217  virtual void begin(QofInstance*) {}
221  virtual void commit (QofInstance*);
225  virtual void rollback(QofInstance*) {}
243  virtual void sync(QofBook *) = 0;
246  virtual void safe_sync(QofBook *) = 0;
250  virtual void export_coa(QofBook *) {}
253  void set_error(QofBackendError err);
259  bool check_error();
263  void set_message(std::string&&);
266  const std::string&& get_message();
270  void set_percentage(QofBePercentageFunc pctfn) { m_percentage = pctfn; }
271  QofBePercentageFunc get_percentage() { return m_percentage; }
274  const std::string& get_uri() { return m_fullpath; }
279  static bool register_backend(const char*, const char*);
280  static void release_backends();
281 protected:
282  QofBePercentageFunc m_percentage;
286  std::string m_fullpath;
287 private:
288  static GModuleVec c_be_registry;
289  QofBackendError m_last_err;
290  std::string m_error_msg;
291 };
292 
293 /* @} */
294 /* @} */
295 /* @} */
296 
297 #endif /* __QOF_BACKEND_HPP__ */
void set_percentage(QofBePercentageFunc pctfn)
Store and retrieve a backend-specific function for determining the progress in completing a long oper...
Encapsulates a connection to a backend (persistent store)
API for data storage Backend.
const std::string && get_message()
Retrieve and clear the stored error message.
Definition: qof-backend.cpp:85
void set_message(std::string &&)
Set a descriptive message that can be displayed to the user when there&#39;s an error.
Definition: qof-backend.cpp:79
QofBackendError
The errors that can be reported to the GUI & other front-end users.
Definition: qofbackend.h:57
virtual void commit(QofInstance *)
Commits the changes from the engine to the backend data storage.
Definition: qof-backend.cpp:49
virtual void session_begin(QofSession *session, const char *new_uri, SessionOpenMode mode)=0
Open the file or connect to the server.
find objects that match a certain expression.
virtual void begin(QofInstance *)
Called when the engine is about to make a change to a data structure.
static bool register_backend(const char *, const char *)
Class methods for dynamically loading the several backends and for freeing them at shutdown...
Definition: qof-backend.cpp:91
virtual void export_coa(QofBook *)
Extract the chart of accounts from the current database and create a new database with it...
std::string m_fullpath
Each backend resolves a fully-qualified file path.
virtual void load(QofBook *, QofBackendLoadType)=0
Load the minimal set of application data needed for the application to be operable at initial startup...
Encapsulate all the information about a dataset.
virtual void safe_sync(QofBook *)=0
Perform a sync in a way that prevents data loss on a DBI backend.
SessionOpenMode
Mode for opening sessions.
Definition: qofsession.h:120
virtual void rollback(QofInstance *)
Revert changes in the engine and unlock the backend.
void(* QofBePercentageFunc)(const char *message, double percent)
DOCUMENT ME!
Definition: qofbackend.h:163
virtual void sync(QofBook *)=0
Synchronizes the engine contents to the backend.
bool check_error()
Report if there is an error.
Definition: qof-backend.cpp:73
const std::string & get_uri()
Retrieve the backend&#39;s storage URI.
void set_error(QofBackendError err)
Set the error value only if there isn&#39;t already an error already.
Definition: qof-backend.cpp:56
QofBackendError get_error()
Retrieve the currently-stored error and clear it.
Definition: qof-backend.cpp:64