GnuCash 2.4.99
gnc-book-sql.c
Go to the documentation of this file.
00001 /********************************************************************
00002  * gnc-book-sql.c: load and save data to SQL                        *
00003  *                                                                  *
00004  * This program is free software; you can redistribute it and/or    *
00005  * modify it under the terms of the GNU General Public License as   *
00006  * published by the Free Software Foundation; either version 2 of   *
00007  * the License, or (at your option) any later version.              *
00008  *                                                                  *
00009  * This program is distributed in the hope that it will be useful,  *
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
00012  * GNU General Public License for more details.                     *
00013  *                                                                  *
00014  * You should have received a copy of the GNU General Public License*
00015  * along with this program; if not, contact:                        *
00016  *                                                                  *
00017  * Free Software Foundation           Voice:  +1-617-542-5942       *
00018  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
00019  * Boston, MA  02110-1301,  USA       gnu@gnu.org                   *
00020 \********************************************************************/
00029 #include "config.h"
00030 
00031 #include <glib.h>
00032 
00033 #include "qof.h"
00034 
00035 #include "gnc-backend-sql.h"
00036 
00037 #include "gnc-book-sql.h"
00038 #include "gnc-slots-sql.h"
00039 
00040 #include "gnc-engine.h"
00041 #include "SX-book.h"
00042 #include "SX-book-p.h"
00043 
00044 #if defined( S_SPLINT_S )
00045 #include "splint-defs.h"
00046 #endif
00047 
00048 #define BOOK_TABLE "books"
00049 #define TABLE_VERSION 1
00050 
00051 /*@ unused @*/ static QofLogModule log_module = G_LOG_DOMAIN;
00052 
00053 static /*@ dependent @*//*@ null @*/ gpointer get_root_account_guid( gpointer pObject );
00054 static void set_root_account_guid( gpointer pObject, /*@ null @*/ gpointer pValue );
00055 static /*@ dependent @*//*@ null @*/ gpointer get_root_template_guid( gpointer pObject );
00056 static void set_root_template_guid( gpointer pObject, /*@ null @*/ gpointer pValue );
00057 
00058 static const GncSqlColumnTableEntry col_table[] =
00059 {
00060     /*@ -full_init_block @*/
00061     { "guid",               CT_GUID, 0, COL_NNUL | COL_PKEY, "guid" },
00062     {
00063         "root_account_guid",  CT_GUID, 0, COL_NNUL,          NULL, NULL,
00064         (QofAccessFunc)get_root_account_guid,  set_root_account_guid
00065     },
00066     {
00067         "root_template_guid", CT_GUID, 0, COL_NNUL,          NULL, NULL,
00068         (QofAccessFunc)get_root_template_guid, set_root_template_guid
00069     },
00070     { NULL }
00071     /*@ +full_init_block @*/
00072 };
00073 
00074 /* ================================================================= */
00075 static /*@ dependent @*//*@ null @*/ gpointer
00076 get_root_account_guid( gpointer pObject )
00077 {
00078     QofBook* book = QOF_BOOK(pObject);
00079     const Account* root;
00080 
00081     g_return_val_if_fail( pObject != NULL, NULL );
00082     g_return_val_if_fail( QOF_IS_BOOK(pObject), NULL );
00083 
00084     root = gnc_book_get_root_account( book );
00085     return (gpointer)qof_instance_get_guid( QOF_INSTANCE(root) );
00086 }
00087 
00088 static void
00089 set_root_account_guid( gpointer pObject, /*@ null @*/ gpointer pValue )
00090 {
00091     QofBook* book = QOF_BOOK(pObject);
00092     const Account* root;
00093     GncGUID* guid = (GncGUID*)pValue;
00094 
00095     g_return_if_fail( pObject != NULL );
00096     g_return_if_fail( QOF_IS_BOOK(pObject) );
00097     g_return_if_fail( pValue != NULL );
00098 
00099     root = gnc_book_get_root_account( book );
00100     qof_instance_set_guid( QOF_INSTANCE(root), guid );
00101 }
00102 
00103 static /*@ dependent @*//*@ null @*/ gpointer
00104 get_root_template_guid( gpointer pObject )
00105 {
00106     const QofBook* book = QOF_BOOK(pObject);
00107     const Account* root;
00108 
00109     g_return_val_if_fail( pObject != NULL, NULL );
00110     g_return_val_if_fail( QOF_IS_BOOK(pObject), NULL );
00111 
00112     root = gnc_book_get_template_root( book );
00113     return (gpointer)qof_instance_get_guid( QOF_INSTANCE(root) );
00114 }
00115 
00116 static void
00117 set_root_template_guid( gpointer pObject, /*@ null @*/ gpointer pValue )
00118 {
00119     QofBook* book = QOF_BOOK(pObject);
00120     GncGUID* guid = (GncGUID*)pValue;
00121     Account* root;
00122 
00123     g_return_if_fail( pObject != NULL );
00124     g_return_if_fail( QOF_IS_BOOK(pObject) );
00125     g_return_if_fail( pValue != NULL );
00126 
00127     root = gnc_book_get_template_root( book );
00128     if ( root == NULL )
00129     {
00130         root = xaccMallocAccount( book );
00131         xaccAccountBeginEdit( root );
00132         xaccAccountSetType( root, ACCT_TYPE_ROOT );
00133         xaccAccountCommitEdit( root );
00134         gnc_book_set_template_root( book, root );
00135     }
00136     qof_instance_set_guid( QOF_INSTANCE(root), guid );
00137 }
00138 
00139 /* ================================================================= */
00140 static void
00141 load_single_book( GncSqlBackend* be, GncSqlRow* row )
00142 {
00143     const GncGUID* guid;
00144     QofBook* pBook;
00145 
00146     g_return_if_fail( be != NULL );
00147     g_return_if_fail( row != NULL );
00148 
00149     guid = gnc_sql_load_guid( be, row );
00150 
00151     pBook = be->book;
00152     if ( pBook == NULL )
00153     {
00154         pBook = qof_book_new();
00155     }
00156 
00157     qof_book_begin_edit( pBook );
00158     gnc_sql_load_object( be, row, GNC_ID_BOOK, pBook, col_table );
00159     gnc_sql_slots_load( be, QOF_INSTANCE(pBook) );
00160     qof_book_commit_edit( pBook );
00161 
00162     qof_instance_mark_clean( QOF_INSTANCE(pBook) );
00163 }
00164 
00165 static void
00166 load_all_books( GncSqlBackend* be )
00167 {
00168     GncSqlStatement* stmt;
00169     GncSqlResult* result;
00170 
00171     g_return_if_fail( be != NULL );
00172 
00173     stmt = gnc_sql_create_select_statement( be, BOOK_TABLE );
00174     if ( stmt != NULL )
00175     {
00176         result = gnc_sql_execute_select_statement( be, stmt );
00177         gnc_sql_statement_dispose( stmt );
00178         if ( result != NULL )
00179         {
00180             GncSqlRow* row = gnc_sql_result_get_first_row( result );
00181 
00182             // If there are no rows, try committing the book
00183             if ( row == NULL )
00184             {
00185                 (void)gnc_sql_save_book( be, QOF_INSTANCE(be->book) );
00186             }
00187             else
00188             {
00189                 // Otherwise, load the 1st book.
00190                 load_single_book( be, row );
00191             }
00192 
00193             gnc_sql_result_dispose( result );
00194         }
00195     }
00196 }
00197 
00198 /* ================================================================= */
00199 static void
00200 create_book_tables( GncSqlBackend* be )
00201 {
00202     gint version;
00203 
00204     g_return_if_fail( be != NULL );
00205 
00206     version = gnc_sql_get_table_version( be, BOOK_TABLE );
00207     if ( version == 0 )
00208     {
00209         (void)gnc_sql_create_table( be, BOOK_TABLE, TABLE_VERSION, col_table );
00210     }
00211 }
00212 
00213 /* ================================================================= */
00214 gboolean
00215 gnc_sql_save_book( GncSqlBackend* be, QofInstance* inst)
00216 {
00217     gboolean status;
00218 
00219     g_return_val_if_fail( be != NULL, FALSE );
00220     g_return_val_if_fail( inst != NULL, FALSE );
00221     g_return_val_if_fail( QOF_IS_BOOK(inst), FALSE );
00222 
00223     status = gnc_sql_commit_standard_item( be, inst, BOOK_TABLE, GNC_ID_BOOK, col_table );
00224 
00225     return status;
00226 }
00227 
00228 /* ================================================================= */
00229 void
00230 gnc_sql_init_book_handler( void )
00231 {
00232     static GncSqlObjectBackend be_data =
00233     {
00234         GNC_SQL_BACKEND_VERSION,
00235         GNC_ID_BOOK,
00236         gnc_sql_save_book,      /* commit */
00237         load_all_books,         /* initial_load */
00238         create_book_tables,             /* create_tables */
00239         NULL,                   /* compile_query */
00240         NULL,                   /* run_query */
00241         NULL,                   /* free_query */
00242         NULL                    /* write */
00243     };
00244 
00245     (void)qof_object_register_backend( GNC_ID_BOOK, GNC_SQL_BACKEND, &be_data );
00246 }
00247 /* ========================== END OF FILE ===================== */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines