LCOV - code coverage report
Current view: top level - libgnucash/backend/sql - gnc-commodity-sql.cpp (source / functions) Coverage Total Hit
Test: gnucash.info Lines: 94.7 % 76 72
Test Date: 2026-11-02 18:19:43 Functions: 100.0 % 11 11
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /********************************************************************
       2              :  * gnc-commodity-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-commodity-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 <guid.hpp>
      29              : #include <config.h>
      30              : 
      31              : #include <glib.h>
      32              : 
      33              : #include "qof.h"
      34              : #include "gnc-commodity.h"
      35              : 
      36              : #include "gnc-sql-connection.hpp"
      37              : #include "gnc-sql-backend.hpp"
      38              : #include "gnc-sql-object-backend.hpp"
      39              : #include "gnc-sql-column-table-entry.hpp"
      40              : #include "gnc-commodity-sql.h"
      41              : #include "gnc-slots-sql.h"
      42              : 
      43              : #if defined( S_SPLINT_S )
      44              : #include "splint-defs.h"
      45              : #endif
      46              : 
      47              : [[maybe_unused]] static QofLogModule log_module = G_LOG_DOMAIN;
      48              : 
      49              : static  gpointer get_quote_source_name (gpointer pObject);
      50              : static void set_quote_source_name (gpointer pObject,  gpointer pValue);
      51              : 
      52              : #define COMMODITIES_TABLE "commodities"
      53              : #define TABLE_VERSION 1
      54              : 
      55              : #define COMMODITY_MAX_NAMESPACE_LEN 2048
      56              : #define COMMODITY_MAX_MNEMONIC_LEN 2048
      57              : #define COMMODITY_MAX_FULLNAME_LEN 2048
      58              : #define COMMODITY_MAX_CUSIP_LEN 2048
      59              : #define COMMODITY_MAX_QUOTESOURCE_LEN 2048
      60              : #define COMMODITY_MAX_QUOTE_TZ_LEN 2048
      61              : 
      62              : static const EntryVec col_table
      63              : {
      64              :     gnc_sql_make_table_entry<CT_GUID>(
      65              :         "guid", 0, COL_NNUL | COL_PKEY | COL_UNIQUE, "guid"),
      66              :     gnc_sql_make_table_entry<CT_STRING>("namespace",
      67              :                                         COMMODITY_MAX_NAMESPACE_LEN, COL_NNUL,
      68              :                                      (QofAccessFunc)gnc_commodity_get_namespace,
      69              :                                      (QofSetterFunc)gnc_commodity_set_namespace),
      70              :     gnc_sql_make_table_entry<CT_STRING>(
      71              :         "mnemonic", COMMODITY_MAX_MNEMONIC_LEN, COL_NNUL, "mnemonic"),
      72              :     gnc_sql_make_table_entry<CT_STRING>(
      73              :         "fullname", COMMODITY_MAX_FULLNAME_LEN, 0, "fullname"),
      74              :     gnc_sql_make_table_entry<CT_STRING>(
      75              :         "cusip", COMMODITY_MAX_CUSIP_LEN, 0, "cusip"),
      76              :     gnc_sql_make_table_entry<CT_INT>("fraction", 0, COL_NNUL, "fraction"),
      77              :     gnc_sql_make_table_entry<CT_BOOLEAN>(
      78              :         "quote_flag", 0, COL_NNUL, "quote_flag"),
      79              :     gnc_sql_make_table_entry<CT_STRING>("quote_source",
      80              :                                         COMMODITY_MAX_QUOTESOURCE_LEN, 0,
      81              :                                         (QofAccessFunc)get_quote_source_name,
      82              :                                         set_quote_source_name),
      83              :     gnc_sql_make_table_entry<CT_STRING>(
      84              :         "quote_tz", COMMODITY_MAX_QUOTE_TZ_LEN, 0, "quote-tz"),
      85              : };
      86              : 
      87           15 : GncSqlCommodityBackend::GncSqlCommodityBackend() :
      88           30 :     GncSqlObjectBackend(TABLE_VERSION, GNC_ID_COMMODITY,
      89           75 :                         COMMODITIES_TABLE, col_table) {}
      90              : /* ================================================================= */
      91              : 
      92              : static  gpointer
      93           59 : get_quote_source_name (gpointer pObject)
      94              : {
      95              :     const gnc_commodity* pCommodity;
      96              : 
      97           59 :     g_return_val_if_fail (pObject != NULL, NULL);
      98           59 :     g_return_val_if_fail (GNC_IS_COMMODITY (pObject), NULL);
      99              : 
     100           59 :     pCommodity = GNC_COMMODITY (pObject);
     101          118 :     return (gpointer)gnc_quote_source_get_internal_name (
     102           59 :                gnc_commodity_get_quote_source (pCommodity));
     103              : }
     104              : 
     105              : static void
     106           10 : set_quote_source_name (gpointer pObject, gpointer pValue)
     107              : {
     108              :     gnc_commodity* pCommodity;
     109           10 :     const gchar* quote_source_name = (const gchar*)pValue;
     110              :     gnc_quote_source* quote_source;
     111              : 
     112           10 :     g_return_if_fail (pObject != NULL);
     113           10 :     g_return_if_fail (GNC_IS_COMMODITY (pObject));
     114              : 
     115           10 :     if (pValue == NULL) return;
     116              : 
     117           10 :     pCommodity = GNC_COMMODITY (pObject);
     118           10 :     quote_source = gnc_quote_source_lookup_by_internal (quote_source_name);
     119           10 :     if (!quote_source)
     120            1 :         quote_source = gnc_quote_source_add_new (quote_source_name, FALSE);
     121           10 :     gnc_commodity_set_quote_source (pCommodity, quote_source);
     122              : }
     123              : 
     124              : static  gnc_commodity*
     125           10 : load_single_commodity (GncSqlBackend* sql_be, GncSqlRow& row)
     126              : {
     127           10 :     QofBook* pBook = sql_be->book();
     128              :     gnc_commodity* pCommodity;
     129              : 
     130           10 :     pCommodity = gnc_commodity_new (pBook, NULL, NULL, NULL, NULL, 100);
     131           10 :     gnc_commodity_begin_edit (pCommodity);
     132           10 :     gnc_sql_load_object (sql_be, row, GNC_ID_COMMODITY, pCommodity, col_table);
     133           10 :     gnc_commodity_commit_edit (pCommodity);
     134              : 
     135           10 :     return pCommodity;
     136              : }
     137              : 
     138              : void
     139            7 : GncSqlCommodityBackend::load_all (GncSqlBackend* sql_be)
     140              : {
     141              :     gnc_commodity_table* pTable;
     142              : 
     143            7 :     pTable = gnc_commodity_table_get_table (sql_be->book());
     144            7 :     std::string sql("SELECT * FROM " COMMODITIES_TABLE);
     145            7 :     auto stmt = sql_be->create_statement_from_sql(sql);
     146            7 :     auto result = sql_be->execute_select_statement(stmt);
     147              : 
     148           17 :     for (auto row : *result)
     149              :     {
     150           10 :         auto pCommodity = load_single_commodity (sql_be, row);
     151              : 
     152           10 :         if (pCommodity != NULL)
     153              :         {
     154              :             GncGUID guid;
     155              : 
     156           10 :             guid = *qof_instance_get_guid (QOF_INSTANCE (pCommodity));
     157           10 :             pCommodity = gnc_commodity_table_insert (pTable, pCommodity);
     158           10 :             if (qof_instance_is_dirty (QOF_INSTANCE (pCommodity)))
     159            0 :                 sql_be->commodity_for_postload_processing(pCommodity);
     160           10 :             qof_instance_set_guid (QOF_INSTANCE (pCommodity), &guid);
     161              :         }
     162              : 
     163           17 :     }
     164            7 :     std::string pkey(col_table[0]->name());
     165            7 :     sql = "SELECT DISTINCT ";
     166            7 :     sql += pkey + " FROM " COMMODITIES_TABLE;
     167            7 :     gnc_sql_slots_load_for_sql_subquery (sql_be, sql,
     168              :                                          (BookLookupFn)gnc_commodity_find_commodity_by_guid);
     169            7 : }
     170              : /* ================================================================= */
     171              : static gboolean
     172            9 : do_commit_commodity (GncSqlBackend* sql_be, QofInstance* inst,
     173              :                      gboolean force_insert)
     174              : {
     175              :     const GncGUID* guid;
     176              :     gboolean is_infant;
     177              :     E_DB_OPERATION op;
     178              :     gboolean is_ok;
     179              : 
     180            9 :     is_infant = qof_instance_get_infant (inst);
     181            9 :     if (qof_instance_get_destroying (inst))
     182              :     {
     183            0 :         op = OP_DB_DELETE;
     184              :     }
     185            9 :     else if (sql_be->pristine() || is_infant || force_insert)
     186              :     {
     187            9 :         op = OP_DB_INSERT;
     188              :     }
     189              :     else
     190              :     {
     191            0 :         op = OP_DB_UPDATE;
     192              :     }
     193            9 :     is_ok = sql_be->do_db_operation(op, COMMODITIES_TABLE, GNC_ID_COMMODITY,
     194              :                                     inst, col_table);
     195              : 
     196            9 :     if (is_ok)
     197              :     {
     198              :         // Now, commit any slots
     199            9 :         guid = qof_instance_get_guid (inst);
     200            9 :         if (!qof_instance_get_destroying (inst))
     201              :         {
     202            9 :             is_ok = gnc_sql_slots_save (sql_be, guid, is_infant, inst);
     203              :         }
     204              :         else
     205              :         {
     206            0 :             is_ok = gnc_sql_slots_delete (sql_be, guid);
     207              :         }
     208              :     }
     209              : 
     210            9 :     return is_ok;
     211              : }
     212              : 
     213              : bool
     214            9 : GncSqlCommodityBackend::commit (GncSqlBackend* sql_be, QofInstance* inst)
     215              : {
     216            9 :     g_return_val_if_fail (sql_be != NULL, FALSE);
     217            9 :     g_return_val_if_fail (inst != NULL, FALSE);
     218            9 :     g_return_val_if_fail (GNC_IS_COMMODITY (inst), FALSE);
     219            9 :     auto in_be = instance_in_db(sql_be, inst);
     220            9 :     return do_commit_commodity (sql_be, inst, !in_be);
     221              : }
     222              : 
     223              : /* ----------------------------------------------------------------- */
     224              : template<> void
     225           58 : GncSqlColumnTableEntryImpl<CT_COMMODITYREF>::load (const GncSqlBackend* sql_be,
     226              :                                                  GncSqlRow& row,
     227              :                                                  QofIdTypeConst obj_name,
     228              :                                                  gpointer pObject) const noexcept
     229              : {
     230           58 :     load_from_guid_ref(row, obj_name, pObject,
     231           45 :                        [sql_be](GncGUID* g){
     232           45 :                            return gnc_commodity_find_commodity_by_guid(g, sql_be->book());
     233              :                        });
     234           58 : }
     235              : 
     236              : template<> void
     237           48 : GncSqlColumnTableEntryImpl<CT_COMMODITYREF>::add_to_table(ColVec& vec) const noexcept
     238              : {
     239           48 :     add_objectref_guid_to_table(vec);
     240           48 : }
     241              : 
     242              : template<> void
     243           55 : GncSqlColumnTableEntryImpl<CT_COMMODITYREF>::add_to_query(QofIdTypeConst obj_name,
     244              :                                                     const gpointer pObject,
     245              :                                                     PairVec& vec) const noexcept
     246              : {
     247           55 :     add_objectref_guid_to_query(obj_name, pObject, vec);
     248           55 : }
     249              : 
     250              : /* ========================== END OF FILE ===================== */
        

Generated by: LCOV version 2.0-1