GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gnc-book-sql.c
Go to the documentation of this file.
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 [email protected] *
20 \********************************************************************/
29 #include "config.h"
30 
31 #include <glib.h>
32 
33 #include "qof.h"
34 
35 #include "gnc-backend-sql.h"
36 
37 #include "gnc-book-sql.h"
38 #include "gnc-slots-sql.h"
39 
40 #include "gnc-engine.h"
41 #include "SX-book.h"
42 #include "SX-book-p.h"
43 
44 #if defined( S_SPLINT_S )
45 #include "splint-defs.h"
46 #endif
47 
48 #define BOOK_TABLE "books"
49 #define TABLE_VERSION 1
50 
51 G_GNUC_UNUSED static QofLogModule log_module = G_LOG_DOMAIN;
52 
53 static /*@ dependent @*//*@ null @*/ gpointer get_root_account_guid( gpointer pObject );
54 static void set_root_account_guid( gpointer pObject, /*@ null @*/ gpointer pValue );
55 static /*@ dependent @*//*@ null @*/ gpointer get_root_template_guid( gpointer pObject );
56 static void set_root_template_guid( gpointer pObject, /*@ null @*/ gpointer pValue );
57 
58 static const GncSqlColumnTableEntry col_table[] =
59 {
60  /*@ -full_init_block @*/
61  { "guid", CT_GUID, 0, COL_NNUL | COL_PKEY, "guid" },
62  {
63  "root_account_guid", CT_GUID, 0, COL_NNUL, NULL, NULL,
64  (QofAccessFunc)get_root_account_guid, set_root_account_guid
65  },
66  {
67  "root_template_guid", CT_GUID, 0, COL_NNUL, NULL, NULL,
68  (QofAccessFunc)get_root_template_guid, set_root_template_guid
69  },
70  { NULL }
71  /*@ +full_init_block @*/
72 };
73 
74 /* ================================================================= */
75 static /*@ dependent @*//*@ null @*/ gpointer
76 get_root_account_guid( gpointer pObject )
77 {
78  QofBook* book = QOF_BOOK(pObject);
79  const Account* root;
80 
81  g_return_val_if_fail( pObject != NULL, NULL );
82  g_return_val_if_fail( QOF_IS_BOOK(pObject), NULL );
83 
84  root = gnc_book_get_root_account( book );
85  return (gpointer)qof_instance_get_guid( QOF_INSTANCE(root) );
86 }
87 
88 static void
89 set_root_account_guid( gpointer pObject, /*@ null @*/ gpointer pValue )
90 {
91  QofBook* book = QOF_BOOK(pObject);
92  const Account* root;
93  GncGUID* guid = (GncGUID*)pValue;
94 
95  g_return_if_fail( pObject != NULL );
96  g_return_if_fail( QOF_IS_BOOK(pObject) );
97  g_return_if_fail( pValue != NULL );
98 
99  root = gnc_book_get_root_account( book );
100  qof_instance_set_guid( QOF_INSTANCE(root), guid );
101 }
102 
103 static /*@ dependent @*//*@ null @*/ gpointer
104 get_root_template_guid( gpointer pObject )
105 {
106  const QofBook* book = QOF_BOOK(pObject);
107  const Account* root;
108 
109  g_return_val_if_fail( pObject != NULL, NULL );
110  g_return_val_if_fail( QOF_IS_BOOK(pObject), NULL );
111 
112  root = gnc_book_get_template_root( book );
113  return (gpointer)qof_instance_get_guid( QOF_INSTANCE(root) );
114 }
115 
116 static void
117 set_root_template_guid( gpointer pObject, /*@ null @*/ gpointer pValue )
118 {
119  QofBook* book = QOF_BOOK(pObject);
120  GncGUID* guid = (GncGUID*)pValue;
121  Account* root;
122 
123  g_return_if_fail( pObject != NULL );
124  g_return_if_fail( QOF_IS_BOOK(pObject) );
125  g_return_if_fail( pValue != NULL );
126 
127  root = gnc_book_get_template_root( book );
128  if ( root == NULL )
129  {
130  root = xaccMallocAccount( book );
131  xaccAccountBeginEdit( root );
133  xaccAccountCommitEdit( root );
134  gnc_book_set_template_root( book, root );
135  }
136  qof_instance_set_guid( QOF_INSTANCE(root), guid );
137 }
138 
139 /* ================================================================= */
140 static void
141 load_single_book( GncSqlBackend* be, GncSqlRow* row )
142 {
143  QofBook* pBook;
144 
145  g_return_if_fail( be != NULL );
146  g_return_if_fail( row != NULL );
147 
148  gnc_sql_load_guid( be, row );
149 
150  pBook = be->book;
151  if ( pBook == NULL )
152  {
153  pBook = qof_book_new();
154  }
155 
156  qof_book_begin_edit( pBook );
157  gnc_sql_load_object( be, row, GNC_ID_BOOK, pBook, col_table );
158  gnc_sql_slots_load( be, QOF_INSTANCE(pBook) );
159  qof_book_commit_edit( pBook );
160 
161  qof_instance_mark_clean( QOF_INSTANCE(pBook) );
162 }
163 
164 static void
165 load_all_books( GncSqlBackend* be )
166 {
167  GncSqlStatement* stmt;
168  GncSqlResult* result;
169 
170  g_return_if_fail( be != NULL );
171 
172  stmt = gnc_sql_create_select_statement( be, BOOK_TABLE );
173  if ( stmt != NULL )
174  {
175  result = gnc_sql_execute_select_statement( be, stmt );
176  gnc_sql_statement_dispose( stmt );
177  if ( result != NULL )
178  {
179  GncSqlRow* row = gnc_sql_result_get_first_row( result );
180 
181  /* If there are no rows, try committing the book; unset
182  * loading so that it will actually get saved.
183  */
184  if ( row == NULL )
185  {
186  be->loading = FALSE;
187  (void)gnc_sql_save_book( be, QOF_INSTANCE(be->book) );
188  be->loading = TRUE;
189  }
190  else
191  {
192  // Otherwise, load the 1st book.
193  load_single_book( be, row );
194  }
195 
196  gnc_sql_result_dispose( result );
197  }
198  }
199 }
200 
201 /* ================================================================= */
202 static void
203 create_book_tables( GncSqlBackend* be )
204 {
205  gint version;
206 
207  g_return_if_fail( be != NULL );
208 
209  version = gnc_sql_get_table_version( be, BOOK_TABLE );
210  if ( version == 0 )
211  {
212  (void)gnc_sql_create_table( be, BOOK_TABLE, TABLE_VERSION, col_table );
213  }
214 }
215 
216 /* ================================================================= */
217 gboolean
218 gnc_sql_save_book( GncSqlBackend* be, QofInstance* inst)
219 {
220  gboolean status;
221 
222  g_return_val_if_fail( be != NULL, FALSE );
223  g_return_val_if_fail( inst != NULL, FALSE );
224  g_return_val_if_fail( QOF_IS_BOOK(inst), FALSE );
225 
226  status = gnc_sql_commit_standard_item( be, inst, BOOK_TABLE, GNC_ID_BOOK, col_table );
227 
228  return status;
229 }
230 
231 /* ================================================================= */
232 void
233 gnc_sql_init_book_handler( void )
234 {
235  static GncSqlObjectBackend be_data =
236  {
237  GNC_SQL_BACKEND_VERSION,
238  GNC_ID_BOOK,
239  gnc_sql_save_book, /* commit */
240  load_all_books, /* initial_load */
241  create_book_tables, /* create_tables */
242  NULL, /* compile_query */
243  NULL, /* run_query */
244  NULL, /* free_query */
245  NULL /* write */
246  };
247 
248  (void)qof_object_register_backend( GNC_ID_BOOK, GNC_SQL_BACKEND, &be_data );
249 }
250 /* ========================== END OF FILE ===================== */
void xaccAccountSetType(Account *acc, GNCAccountType tip)
Definition: Account.c:2208
gboolean qof_object_register_backend(QofIdTypeConst type_name, const char *backend_name, gpointer be_data)
const GncGUID * qof_instance_get_guid(gconstpointer)
gint gnc_sql_get_table_version(const GncSqlBackend *be, const gchar *table_name)
gboolean loading
#define G_LOG_DOMAIN
Functions providing the SX List as a plugin page.
load and save accounts data to SQL
GncSqlStatement * gnc_sql_create_select_statement(GncSqlBackend *be, const gchar *table_name)
#define COL_NNUL
QofBook * qof_book_new(void)
gboolean gnc_sql_create_table(GncSqlBackend *be, const gchar *table_name, gint table_version, const GncSqlColumnTableEntry *col_table)
load and save data to SQL
Account * gnc_book_get_template_root(const QofBook *book)
Definition: SX-book.c:65
gpointer(* QofAccessFunc)(gpointer object, const QofParam *param)
Definition: qofclass.h:177
QofBook * book
Definition: guid.h:65
#define COL_PKEY
const GncGUID * gnc_sql_load_guid(const GncSqlBackend *be, GncSqlRow *row)
Anchor Scheduled Transaction info in a book. See src/doc/books.txt for design overview.
void gnc_sql_slots_load(GncSqlBackend *be, QofInstance *inst)
GncSqlResult * gnc_sql_execute_select_statement(GncSqlBackend *be, GncSqlStatement *stmt)
All type declarations for the whole Gnucash engine.
void gnc_sql_load_object(const GncSqlBackend *be, GncSqlRow *row, QofIdTypeConst obj_name, gpointer pObject, const GncSqlColumnTableEntry *table)
load and save data to SQL
void xaccAccountBeginEdit(Account *acc)
Definition: Account.c:1280
gboolean gnc_sql_commit_standard_item(GncSqlBackend *be, QofInstance *inst, const gchar *tableName, QofIdTypeConst obj_name, const GncSqlColumnTableEntry *col_table)
Account * xaccMallocAccount(QofBook *book)
Definition: Account.c:1083
void xaccAccountCommitEdit(Account *acc)
Definition: Account.c:1321
const gchar * QofLogModule
Definition: qofid.h:89