LCOV - code coverage report
Current view: top level - libgnucash/backend/sql - gnc-customer-sql.cpp (source / functions) Coverage Total Hit
Test: gnucash.info Lines: 89.3 % 56 50
Test Date: 2026-11-02 18:19:43 Functions: 87.5 % 8 7
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /********************************************************************\
       2              :  * gnc-customer-sql.c -- customer sql backend                       *
       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              : \********************************************************************/
      22              : 
      23              : /** @file gnc-customer-sql.c
      24              :  *  @brief load and save address data to SQL
      25              :  *  @author Copyright (c) 2007-2008 Phil Longstaff <plongstaff@rogers.com>
      26              :  *
      27              :  * This file implements the top-level QofBackend API for saving/
      28              :  * restoring data to/from an SQL database
      29              :  */
      30              : #include <glib.h>
      31              : 
      32              : #include <config.h>
      33              : 
      34              : #include <stdlib.h>
      35              : #include <string.h>
      36              : 
      37              : #include "gncBillTermP.h"
      38              : #include "gncCustomerP.h"
      39              : #include "gncTaxTableP.h"
      40              : 
      41              : #include "gnc-sql-connection.hpp"
      42              : #include "gnc-sql-backend.hpp"
      43              : #include "gnc-sql-object-backend.hpp"
      44              : #include "gnc-sql-column-table-entry.hpp"
      45              : #include "gnc-slots-sql.h"
      46              : #include "gnc-customer-sql.h"
      47              : #include "gnc-bill-term-sql.h"
      48              : #include "gnc-tax-table-sql.h"
      49              : 
      50              : #define _GNC_MOD_NAME   GNC_ID_CUSTOMER
      51              : 
      52              : static QofLogModule log_module = G_LOG_DOMAIN;
      53              : 
      54              : #define TABLE_NAME "customers"
      55              : #define TABLE_VERSION 2
      56              : 
      57              : #define MAX_NAME_LEN 2048
      58              : #define MAX_ID_LEN 2048
      59              : #define MAX_NOTES_LEN 2048
      60              : 
      61              : static EntryVec col_table
      62              : ({
      63              :     gnc_sql_make_table_entry<CT_GUID>("guid", 0, COL_NNUL | COL_PKEY, "guid" ),
      64              :     gnc_sql_make_table_entry<CT_STRING>("name", MAX_NAME_LEN, COL_NNUL, "name"),
      65              :     gnc_sql_make_table_entry<CT_STRING>("id", MAX_ID_LEN, COL_NNUL,
      66              :                                         CUSTOMER_ID, true),
      67              :     gnc_sql_make_table_entry<CT_STRING>("notes", MAX_NOTES_LEN, COL_NNUL,
      68              :                                         CUSTOMER_NOTES, true),
      69              :     gnc_sql_make_table_entry<CT_BOOLEAN>("active", 0, COL_NNUL,
      70              :                                          QOF_PARAM_ACTIVE, true),
      71              :     gnc_sql_make_table_entry<CT_NUMERIC>("discount", 0, COL_NNUL,
      72              :                                          CUSTOMER_DISCOUNT, true),
      73              :     gnc_sql_make_table_entry<CT_NUMERIC>("credit", 0, COL_NNUL,
      74              :                                          CUSTOMER_CREDIT, true),
      75              :     gnc_sql_make_table_entry<CT_COMMODITYREF>("currency", 0, COL_NNUL,
      76              :                                          (QofAccessFunc)gncCustomerGetCurrency,
      77              :                                          (QofSetterFunc)gncCustomerSetCurrency),
      78              :     gnc_sql_make_table_entry<CT_BOOLEAN>("tax_override", 0, COL_NNUL,
      79              :                                          CUSTOMER_TT_OVER, true),
      80              :     gnc_sql_make_table_entry<CT_ADDRESS>("addr", 0, 0, CUSTOMER_ADDR,
      81              :                                          true),
      82              :     gnc_sql_make_table_entry<CT_ADDRESS>("shipaddr", 0, 0, CUSTOMER_SHIPADDR,
      83              :                                          true),
      84              :     gnc_sql_make_table_entry<CT_BILLTERMREF>("terms", 0, 0, CUSTOMER_TERMS,
      85              :                                              true),
      86              :     gnc_sql_make_table_entry<CT_INT>("tax_included", 0, 0,
      87              :                                      (QofAccessFunc)gncCustomerGetTaxIncluded,
      88              :                                      (QofSetterFunc)gncCustomerSetTaxIncluded),
      89              :     gnc_sql_make_table_entry<CT_TAXTABLEREF>("taxtable", 0, 0,
      90              :                                          (QofAccessFunc)gncCustomerGetTaxTable,
      91              :                                          (QofSetterFunc)gncCustomerSetTaxTable),
      92              : });
      93              : 
      94           15 : GncSqlCustomerBackend::GncSqlCustomerBackend() :
      95           30 :     GncSqlObjectBackend(TABLE_VERSION, GNC_ID_CUSTOMER,
      96           75 :                         TABLE_NAME, col_table) {}
      97              : 
      98              : static GncCustomer*
      99            1 : load_single_customer (GncSqlBackend* sql_be, GncSqlRow& row)
     100              : {
     101              :     const GncGUID* guid;
     102              :     GncCustomer* pCustomer;
     103              : 
     104            1 :     g_return_val_if_fail (sql_be != NULL, NULL);
     105              : 
     106            1 :     guid = gnc_sql_load_guid (sql_be, row);
     107            1 :     pCustomer = gncCustomerLookup (sql_be->book(), guid);
     108            1 :     if (pCustomer == NULL)
     109              :     {
     110            1 :         pCustomer = gncCustomerCreate (sql_be->book());
     111              :     }
     112            1 :     gnc_sql_load_object (sql_be, row, GNC_ID_CUSTOMER, pCustomer, col_table);
     113            1 :     qof_instance_mark_clean (QOF_INSTANCE (pCustomer));
     114              : 
     115            1 :     return pCustomer;
     116              : }
     117              : 
     118              : /* Because gncCustomerLookup has the arguments backwards: */
     119              : static inline GncCustomer*
     120            0 : gnc_customer_lookup (const GncGUID *guid, const QofBook *book)
     121              : {
     122            0 :      QOF_BOOK_RETURN_ENTITY(book, guid, GNC_ID_CUSTOMER, GncCustomer);
     123              : }
     124              : 
     125              : void
     126            7 : GncSqlCustomerBackend::load_all (GncSqlBackend* sql_be)
     127              : {
     128            7 :     g_return_if_fail (sql_be != NULL);
     129              : 
     130            7 :     std::string sql("SELECT * FROM " TABLE_NAME);
     131            7 :     auto stmt = sql_be->create_statement_from_sql(sql);
     132            7 :     auto result = sql_be->execute_select_statement(stmt);
     133              : 
     134            8 :     for (auto row : *result)
     135            8 :         load_single_customer (sql_be, row);
     136              : 
     137            7 :     std::string pkey(col_table[0]->name());
     138            7 :     sql = "SELECT DISTINCT ";
     139            7 :     sql += pkey + " FROM " TABLE_NAME;
     140            7 :     gnc_sql_slots_load_for_sql_subquery (sql_be, sql,
     141              :                                          (BookLookupFn)gnc_customer_lookup);
     142            7 : }
     143              : 
     144              : /* ================================================================= */
     145              : void
     146           13 : GncSqlCustomerBackend::create_tables (GncSqlBackend* sql_be)
     147              : {
     148              :     gint version;
     149              : 
     150           13 :     g_return_if_fail (sql_be != NULL);
     151              : 
     152           26 :     version = sql_be->get_table_version( TABLE_NAME);
     153           13 :     if (version == 0)
     154              :     {
     155           18 :         sql_be->create_table(TABLE_NAME, TABLE_VERSION, col_table);
     156              :     }
     157            7 :     else if (version < m_version)
     158              :     {
     159              :         /* Upgrade 64 bit int handling */
     160            0 :         sql_be->upgrade_table(TABLE_NAME, col_table);
     161            0 :         sql_be->set_table_version (TABLE_NAME, TABLE_VERSION);
     162              : 
     163            0 :         PINFO ("Customers table upgraded from version 1 to version %d\n",
     164              :                TABLE_VERSION);
     165              :     }
     166              : }
     167              : 
     168              : /* ================================================================= */
     169              : static gboolean
     170            1 : customer_should_be_saved (GncCustomer* customer)
     171              : {
     172              :     const char* id;
     173              : 
     174            1 :     g_return_val_if_fail (customer != NULL, FALSE);
     175              : 
     176              :     /* Make sure this is a valid customer before we save it -- should have an ID */
     177            1 :     id = gncCustomerGetID (customer);
     178            1 :     if (id == NULL || *id == '\0')
     179              :     {
     180            0 :         return FALSE;
     181              :     }
     182              : 
     183            1 :     return TRUE;
     184              : }
     185              : 
     186              : static void
     187            1 : write_single_customer (QofInstance* term_p, gpointer data_p)
     188              : {
     189            1 :     auto data = reinterpret_cast<write_objects_t*>(data_p);
     190              : 
     191            1 :     g_return_if_fail (term_p != NULL);
     192            1 :     g_return_if_fail (GNC_IS_CUSTOMER (term_p));
     193            1 :     g_return_if_fail (data_p != NULL);
     194              : 
     195            1 :     if (customer_should_be_saved (GNC_CUSTOMER (term_p)))
     196              :     {
     197            1 :         data->commit (term_p);
     198              :     }
     199              : }
     200              : 
     201              : bool
     202            6 : GncSqlCustomerBackend::write (GncSqlBackend* sql_be)
     203              : {
     204            6 :     write_objects_t data;
     205              : 
     206            6 :     g_return_val_if_fail (sql_be != NULL, FALSE);
     207              : 
     208            6 :     data.be = sql_be;
     209            6 :     data.is_ok = TRUE;
     210            6 :     data.obe = this;
     211            6 :     qof_object_foreach (GNC_ID_CUSTOMER, sql_be->book(), write_single_customer,
     212              :                         (gpointer)&data);
     213            6 :     return data.is_ok;
     214              : }
     215              : 
     216              : /* ========================== END OF FILE ===================== */
        

Generated by: LCOV version 2.0-1