LCOV - code coverage report
Current view: top level - libgnucash/backend/sql - gnc-book-sql.cpp (source / functions) Coverage Total Hit
Test: gnucash.info Lines: 85.5 % 62 53
Test Date: 2025-02-07 16:25:45 Functions: 100.0 % 7 7
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     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                 :          10 : GncSqlBookBackend::GncSqlBookBackend() :
      73                 :             :     GncSqlObjectBackend(TABLE_VERSION, GNC_ID_BOOK,
      74                 :          50 :                         BOOK_TABLE, col_table) {}
      75                 :             : 
      76                 :             : /* ================================================================= */
      77                 :             : static  gpointer
      78                 :          10 : get_root_account_guid (gpointer pObject)
      79                 :             : {
      80                 :          10 :     QofBook* book = QOF_BOOK (pObject);
      81                 :             :     const Account* root;
      82                 :             : 
      83                 :          10 :     g_return_val_if_fail (pObject != NULL, NULL);
      84                 :          10 :     g_return_val_if_fail (QOF_IS_BOOK (pObject), NULL);
      85                 :             : 
      86                 :          10 :     root = gnc_book_get_root_account (book);
      87                 :          10 :     return (gpointer)qof_instance_get_guid (QOF_INSTANCE (root));
      88                 :             : }
      89                 :             : 
      90                 :             : static void
      91                 :           5 : set_root_account_guid (gpointer pObject,  gpointer pValue)
      92                 :             : {
      93                 :           5 :     QofBook* book = QOF_BOOK (pObject);
      94                 :             :     const Account* root;
      95                 :           5 :     GncGUID* guid = (GncGUID*)pValue;
      96                 :             : 
      97                 :           5 :     g_return_if_fail (pObject != NULL);
      98                 :           5 :     g_return_if_fail (QOF_IS_BOOK (pObject));
      99                 :           5 :     g_return_if_fail (pValue != NULL);
     100                 :             : 
     101                 :           5 :     root = gnc_book_get_root_account (book);
     102                 :           5 :     qof_instance_set_guid (QOF_INSTANCE (root), guid);
     103                 :             : }
     104                 :             : 
     105                 :             : static  gpointer
     106                 :          10 : get_root_template_guid (gpointer pObject)
     107                 :             : {
     108                 :          10 :     const QofBook* book = QOF_BOOK (pObject);
     109                 :             :     const Account* root;
     110                 :             : 
     111                 :          10 :     g_return_val_if_fail (pObject != NULL, NULL);
     112                 :          10 :     g_return_val_if_fail (QOF_IS_BOOK (pObject), NULL);
     113                 :             : 
     114                 :          10 :     root = gnc_book_get_template_root (book);
     115                 :          10 :     return (gpointer)qof_instance_get_guid (QOF_INSTANCE (root));
     116                 :             : }
     117                 :             : 
     118                 :             : static void
     119                 :           5 : set_root_template_guid (gpointer pObject,  gpointer pValue)
     120                 :             : {
     121                 :           5 :     QofBook* book = QOF_BOOK (pObject);
     122                 :           5 :     GncGUID* guid = (GncGUID*)pValue;
     123                 :             :     Account* root;
     124                 :             : 
     125                 :           5 :     g_return_if_fail (pObject != NULL);
     126                 :           5 :     g_return_if_fail (QOF_IS_BOOK (pObject));
     127                 :           5 :     g_return_if_fail (pValue != NULL);
     128                 :             : 
     129                 :           5 :     root = gnc_book_get_template_root (book);
     130                 :           5 :     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                 :           5 :     qof_instance_set_guid (QOF_INSTANCE (root), guid);
     139                 :             : }
     140                 :             : 
     141                 :             : /* ================================================================= */
     142                 :             : static void
     143                 :           5 : load_single_book (GncSqlBackend* sql_be, GncSqlRow& row)
     144                 :             : {
     145                 :             :     QofBook* pBook;
     146                 :             : 
     147                 :           5 :     g_return_if_fail (sql_be != NULL);
     148                 :             : 
     149                 :           5 :     gnc_sql_load_guid (sql_be, row);
     150                 :             : 
     151                 :           5 :     pBook = sql_be->book();
     152                 :           5 :     if (pBook == NULL)
     153                 :             :     {
     154                 :           0 :         pBook = qof_book_new ();
     155                 :             :     }
     156                 :             : 
     157                 :           5 :     qof_book_begin_edit (pBook);
     158                 :           5 :     gnc_sql_load_object (sql_be, row, GNC_ID_BOOK, pBook, col_table);
     159                 :           5 :     gnc_sql_slots_load (sql_be, QOF_INSTANCE (pBook));
     160                 :           5 :     qof_book_commit_edit (pBook);
     161                 :             : 
     162                 :           5 :     qof_instance_mark_clean (QOF_INSTANCE (pBook));
     163                 :             : }
     164                 :             : 
     165                 :             : void
     166                 :           5 : GncSqlBookBackend::load_all (GncSqlBackend* sql_be)
     167                 :             : {
     168                 :           5 :     g_return_if_fail (sql_be != NULL);
     169                 :             : 
     170                 :           5 :     std::stringstream sql;
     171                 :           5 :     sql << "SELECT * FROM " << BOOK_TABLE;
     172                 :           5 :     auto stmt = sql_be->create_statement_from_sql(sql.str());
     173                 :           5 :     if (stmt != nullptr)
     174                 :             :     {
     175                 :           5 :         auto result = sql_be->execute_select_statement(stmt);
     176                 :           5 :         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                 :           5 :         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                 :           5 :             load_single_book (sql_be, *row);
     191                 :             :         }
     192                 :           5 :     }
     193                 :           5 : }
     194                 :             : 
     195                 :             : /* ========================== END OF FILE ===================== */
        

Generated by: LCOV version 2.0-1