GnuCash  5.6-150-g038405b370+
gnc-book-sql.cpp
1 /********************************************************************
2  * gnc-book-sql.c: load and save data to SQL *
3  * *
4  * This program is free software; you can redistribute it and/or *
5  * modify it under the terms of the GNU General Public License as *
6  * published by the Free Software Foundation; either version 2 of *
7  * the License, or (at your option) any later version. *
8  * *
9  * This program is distributed in the hope that it will be useful, *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12  * GNU General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU General Public License*
15  * along with this program; if not, contact: *
16  * *
17  * Free Software Foundation Voice: +1-617-542-5942 *
18  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
19  * Boston, MA 02110-1301, USA gnu@gnu.org *
20 \********************************************************************/
28 #include <glib.h>
29 
30 #include <config.h>
31 
32 #include "qof.h"
33 
34 #include "gnc-engine.h"
35 #include "SX-book.h"
36 #include "SX-book-p.h"
37 
38 #if defined( S_SPLINT_S )
39 #include "splint-defs.h"
40 #endif
41 
42 #include "gnc-sql-connection.hpp"
43 #include "gnc-sql-backend.hpp"
44 #include "gnc-sql-object-backend.hpp"
45 #include "gnc-sql-column-table-entry.hpp"
46 #include "gnc-book-sql.h"
47 #include "gnc-slots-sql.h"
48 
49 
50 #define BOOK_TABLE "books"
51 #define TABLE_VERSION 1
52 
53 G_GNUC_UNUSED static QofLogModule log_module = G_LOG_DOMAIN;
54 
55 static gpointer get_root_account_guid (gpointer pObject);
56 static void set_root_account_guid (gpointer pObject, gpointer pValue);
57 static gpointer get_root_template_guid (gpointer pObject);
58 static void set_root_template_guid (gpointer pObject, gpointer pValue);
59 
60 static const EntryVec col_table
61 {
62  gnc_sql_make_table_entry<CT_GUID>(
63  "guid", 0, COL_NNUL | COL_PKEY, "guid"),
64  gnc_sql_make_table_entry<CT_GUID>("root_account_guid", 0, COL_NNUL,
65  (QofAccessFunc)get_root_account_guid,
66  set_root_account_guid),
67  gnc_sql_make_table_entry<CT_GUID>("root_template_guid", 0, COL_NNUL,
68  (QofAccessFunc)get_root_template_guid,
69  set_root_template_guid)
70 };
71 
72 GncSqlBookBackend::GncSqlBookBackend() :
73  GncSqlObjectBackend(TABLE_VERSION, GNC_ID_BOOK,
74  BOOK_TABLE, col_table) {}
75 
76 /* ================================================================= */
77 static gpointer
78 get_root_account_guid (gpointer pObject)
79 {
80  QofBook* book = QOF_BOOK (pObject);
81  const Account* root;
82 
83  g_return_val_if_fail (pObject != NULL, NULL);
84  g_return_val_if_fail (QOF_IS_BOOK (pObject), NULL);
85 
86  root = gnc_book_get_root_account (book);
87  return (gpointer)qof_instance_get_guid (QOF_INSTANCE (root));
88 }
89 
90 static void
91 set_root_account_guid (gpointer pObject, gpointer pValue)
92 {
93  QofBook* book = QOF_BOOK (pObject);
94  const Account* root;
95  GncGUID* guid = (GncGUID*)pValue;
96 
97  g_return_if_fail (pObject != NULL);
98  g_return_if_fail (QOF_IS_BOOK (pObject));
99  g_return_if_fail (pValue != NULL);
100 
101  root = gnc_book_get_root_account (book);
102  qof_instance_set_guid (QOF_INSTANCE (root), guid);
103 }
104 
105 static gpointer
106 get_root_template_guid (gpointer pObject)
107 {
108  const QofBook* book = QOF_BOOK (pObject);
109  const Account* root;
110 
111  g_return_val_if_fail (pObject != NULL, NULL);
112  g_return_val_if_fail (QOF_IS_BOOK (pObject), NULL);
113 
114  root = gnc_book_get_template_root (book);
115  return (gpointer)qof_instance_get_guid (QOF_INSTANCE (root));
116 }
117 
118 static void
119 set_root_template_guid (gpointer pObject, gpointer pValue)
120 {
121  QofBook* book = QOF_BOOK (pObject);
122  GncGUID* guid = (GncGUID*)pValue;
123  Account* root;
124 
125  g_return_if_fail (pObject != NULL);
126  g_return_if_fail (QOF_IS_BOOK (pObject));
127  g_return_if_fail (pValue != NULL);
128 
129  root = gnc_book_get_template_root (book);
130  if (root == NULL)
131  {
132  root = xaccMallocAccount (book);
133  xaccAccountBeginEdit (root);
135  xaccAccountCommitEdit (root);
136  gnc_book_set_template_root (book, root);
137  }
138  qof_instance_set_guid (QOF_INSTANCE (root), guid);
139 }
140 
141 /* ================================================================= */
142 static void
143 load_single_book (GncSqlBackend* sql_be, GncSqlRow& row)
144 {
145  QofBook* pBook;
146 
147  g_return_if_fail (sql_be != NULL);
148 
149  gnc_sql_load_guid (sql_be, row);
150 
151  pBook = sql_be->book();
152  if (pBook == NULL)
153  {
154  pBook = qof_book_new ();
155  }
156 
157  qof_book_begin_edit (pBook);
158  gnc_sql_load_object (sql_be, row, GNC_ID_BOOK, pBook, col_table);
159  gnc_sql_slots_load (sql_be, QOF_INSTANCE (pBook));
160  qof_book_commit_edit (pBook);
161 
162  qof_instance_mark_clean (QOF_INSTANCE (pBook));
163 }
164 
165 void
167 {
168  g_return_if_fail (sql_be != NULL);
169 
170  std::stringstream sql;
171  sql << "SELECT * FROM " << BOOK_TABLE;
172  auto stmt = sql_be->create_statement_from_sql(sql.str());
173  if (stmt != nullptr)
174  {
175  auto result = sql_be->execute_select_statement(stmt);
176  auto row = result->begin();
177 
178  /* If there are no rows, try committing the book; unset
179  * loading so that it will actually get saved.
180  */
181  if (row == result->end())
182  {
183  sql_be->set_loading(false);
184  commit (sql_be, QOF_INSTANCE (sql_be->book()));
185  sql_be->set_loading(true);
186  }
187  else
188  {
189  // Otherwise, load the 1st book.
190  load_single_book (sql_be, *row);
191  }
192  }
193 }
194 
195 /* ========================== END OF FILE ===================== */
void xaccAccountSetType(Account *acc, GNCAccountType tip)
Set the account&#39;s type.
Definition: Account.cpp:2406
GncSqlResultPtr execute_select_statement(const GncSqlStatementPtr &stmt) const noexcept
Executes an SQL SELECT statement and returns the result rows.
const GncGUID * qof_instance_get_guid(gconstpointer inst)
Return the GncGUID of this instance.
void load_all(GncSqlBackend *) override
Load all objects of m_type in the database into memory.
#define G_LOG_DOMAIN
Functions providing the SX List as a plugin page.
load and save accounts data to SQL
STRUCTS.
QofBook * qof_book_new(void)
Allocate, initialise and return a new QofBook.
Definition: qofbook.cpp:290
Account * gnc_book_get_template_root(const QofBook *book)
Returns the template group from the book.
Definition: SX-book.cpp:65
Row of SQL Query results.
Anchor Scheduled Transaction info in a book.
gpointer(* QofAccessFunc)(gpointer object, const QofParam *param)
The QofAccessFunc defines an arbitrary function pointer for access functions.
Definition: qofclass.h:178
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.
All type declarations for the whole Gnucash engine.
load and save data to SQL
void xaccAccountBeginEdit(Account *acc)
The xaccAccountBeginEdit() subroutine is the first phase of a two-phase-commit wrapper for account up...
Definition: Account.cpp:1479
void gnc_sql_slots_load(GncSqlBackend *sql_be, QofInstance *inst)
Loads slots for an object from the db.
Account * xaccMallocAccount(QofBook *book)
Constructor.
Definition: Account.cpp:1275
The type used to store guids in C.
Definition: guid.h:75
void xaccAccountCommitEdit(Account *acc)
ThexaccAccountCommitEdit() subroutine is the second phase of a two-phase-commit wrapper for account u...
Definition: Account.cpp:1520
The hidden root account of an account tree.
Definition: Account.h:153
Main SQL backend structure.