Line data Source code
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 : \********************************************************************/
21 : /** @file gnc-book-sql.c
22 : * @brief load and save data to SQL
23 : * @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff@rogers.com>
24 : *
25 : * This file implements the top-level QofBackend API for saving/
26 : * restoring data to/from an SQL db
27 : */
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 15 : GncSqlBookBackend::GncSqlBookBackend() :
73 30 : GncSqlObjectBackend(TABLE_VERSION, GNC_ID_BOOK,
74 75 : BOOK_TABLE, col_table) {}
75 :
76 : /* ================================================================= */
77 : static gpointer
78 12 : get_root_account_guid (gpointer pObject)
79 : {
80 12 : QofBook* book = QOF_BOOK (pObject);
81 : const Account* root;
82 :
83 12 : g_return_val_if_fail (pObject != NULL, NULL);
84 12 : g_return_val_if_fail (QOF_IS_BOOK (pObject), NULL);
85 :
86 12 : root = gnc_book_get_root_account (book);
87 12 : return (gpointer)qof_instance_get_guid (QOF_INSTANCE (root));
88 : }
89 :
90 : static void
91 7 : set_root_account_guid (gpointer pObject, gpointer pValue)
92 : {
93 7 : QofBook* book = QOF_BOOK (pObject);
94 : const Account* root;
95 7 : GncGUID* guid = (GncGUID*)pValue;
96 :
97 7 : g_return_if_fail (pObject != NULL);
98 7 : g_return_if_fail (QOF_IS_BOOK (pObject));
99 7 : g_return_if_fail (pValue != NULL);
100 :
101 7 : root = gnc_book_get_root_account (book);
102 7 : qof_instance_set_guid (QOF_INSTANCE (root), guid);
103 : }
104 :
105 : static gpointer
106 12 : get_root_template_guid (gpointer pObject)
107 : {
108 12 : const QofBook* book = QOF_BOOK (pObject);
109 : const Account* root;
110 :
111 12 : g_return_val_if_fail (pObject != NULL, NULL);
112 12 : g_return_val_if_fail (QOF_IS_BOOK (pObject), NULL);
113 :
114 12 : root = gnc_book_get_template_root (book);
115 12 : return (gpointer)qof_instance_get_guid (QOF_INSTANCE (root));
116 : }
117 :
118 : static void
119 7 : set_root_template_guid (gpointer pObject, gpointer pValue)
120 : {
121 7 : QofBook* book = QOF_BOOK (pObject);
122 7 : GncGUID* guid = (GncGUID*)pValue;
123 : Account* root;
124 :
125 7 : g_return_if_fail (pObject != NULL);
126 7 : g_return_if_fail (QOF_IS_BOOK (pObject));
127 7 : g_return_if_fail (pValue != NULL);
128 :
129 7 : root = gnc_book_get_template_root (book);
130 7 : if (root == NULL)
131 : {
132 0 : root = xaccMallocAccount (book);
133 0 : xaccAccountBeginEdit (root);
134 0 : xaccAccountSetType (root, ACCT_TYPE_ROOT);
135 0 : xaccAccountCommitEdit (root);
136 0 : gnc_book_set_template_root (book, root);
137 : }
138 7 : qof_instance_set_guid (QOF_INSTANCE (root), guid);
139 : }
140 :
141 : /* ================================================================= */
142 : static void
143 7 : load_single_book (GncSqlBackend* sql_be, GncSqlRow& row)
144 : {
145 : QofBook* pBook;
146 :
147 7 : g_return_if_fail (sql_be != NULL);
148 :
149 7 : gnc_sql_load_guid (sql_be, row);
150 :
151 7 : pBook = sql_be->book();
152 7 : if (pBook == NULL)
153 : {
154 0 : pBook = qof_book_new ();
155 : }
156 :
157 7 : qof_book_begin_edit (pBook);
158 7 : gnc_sql_load_object (sql_be, row, GNC_ID_BOOK, pBook, col_table);
159 7 : gnc_sql_slots_load (sql_be, QOF_INSTANCE (pBook));
160 7 : qof_book_commit_edit (pBook);
161 :
162 7 : qof_instance_mark_clean (QOF_INSTANCE (pBook));
163 : }
164 :
165 : void
166 7 : GncSqlBookBackend::load_all (GncSqlBackend* sql_be)
167 : {
168 7 : g_return_if_fail (sql_be != NULL);
169 :
170 7 : std::stringstream sql;
171 7 : sql << "SELECT * FROM " << BOOK_TABLE;
172 7 : auto stmt = sql_be->create_statement_from_sql(sql.str());
173 7 : if (stmt != nullptr)
174 : {
175 7 : auto result = sql_be->execute_select_statement(stmt);
176 7 : 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 7 : if (row == result->end())
182 : {
183 0 : sql_be->set_loading(false);
184 0 : commit (sql_be, QOF_INSTANCE (sql_be->book()));
185 0 : sql_be->set_loading(true);
186 : }
187 : else
188 : {
189 : // Otherwise, load the 1st book.
190 7 : load_single_book (sql_be, *row);
191 : }
192 7 : }
193 7 : }
194 :
195 : /* ========================== END OF FILE ===================== */
|