LCOV - code coverage report
Current view: top level - libgnucash/backend/sql - gnc-address-sql.cpp (source / functions) Hit Total Coverage
Test: gnucash.info Lines: 32 33 97.0 %
Date: 2024-10-31 11:06:40 Functions: 3 3 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /********************************************************************\
       2                 :            :  * gnc-address-sql.c -- address sql backend implementation          *
       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-address-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 "gnc-engine.h"
      35                 :            : 
      36                 :            : #include "gncAddress.h"
      37                 :            : #include <cstdlib>
      38                 :            : #include <cstring>
      39                 :            : #include <sstream>
      40                 :            : #include "gnc-sql-backend.hpp"
      41                 :            : #include "gnc-sql-column-table-entry.hpp"
      42                 :            : 
      43                 :            : G_GNUC_UNUSED static QofLogModule log_module = G_LOG_DOMAIN;
      44                 :            : 
      45                 :            : #define ADDRESS_MAX_NAME_LEN 1024
      46                 :            : #define ADDRESS_MAX_ADDRESS_LINE_LEN 1024
      47                 :            : #define ADDRESS_MAX_PHONE_LEN 128
      48                 :            : #define ADDRESS_MAX_FAX_LEN 128
      49                 :            : #define ADDRESS_MAX_EMAIL_LEN 256
      50                 :            : 
      51                 :            : static EntryVec col_table
      52                 :            : ({
      53                 :            :     std::make_shared<GncSqlColumnTableEntryImpl<CT_STRING>>(
      54                 :            :         "name",  CT_STRING, ADDRESS_MAX_NAME_LEN, COL_NNUL, "name"),
      55                 :            :     gnc_sql_make_table_entry<CT_STRING>(
      56                 :            :         "addr1", ADDRESS_MAX_ADDRESS_LINE_LEN, COL_NNUL, "addr1"),
      57                 :            :     gnc_sql_make_table_entry<CT_STRING>(
      58                 :            :         "addr2", ADDRESS_MAX_ADDRESS_LINE_LEN, COL_NNUL, "addr2"),
      59                 :            :     gnc_sql_make_table_entry<CT_STRING>(
      60                 :            :         "addr3", ADDRESS_MAX_ADDRESS_LINE_LEN, COL_NNUL, "addr3"),
      61                 :            :     gnc_sql_make_table_entry<CT_STRING>(
      62                 :            :         "addr4", ADDRESS_MAX_ADDRESS_LINE_LEN, COL_NNUL, "addr4"),
      63                 :            :     gnc_sql_make_table_entry<CT_STRING>(
      64                 :            :         "phone", ADDRESS_MAX_PHONE_LEN, COL_NNUL, "phone"),
      65                 :            :     gnc_sql_make_table_entry<CT_STRING>(
      66                 :            :         "fax", ADDRESS_MAX_FAX_LEN, COL_NNUL, "fax" ),
      67                 :            :     gnc_sql_make_table_entry<CT_STRING>(
      68                 :            :         "email", ADDRESS_MAX_EMAIL_LEN, COL_NNUL, "email"),
      69                 :            : });
      70                 :            : 
      71                 :            : typedef void (*AddressSetterFunc) (gpointer, GncAddress*);
      72                 :            : typedef GncAddress* (*AddressGetterFunc) (const gpointer);
      73                 :            : 
      74                 :            : template<> void
      75                 :          4 : GncSqlColumnTableEntryImpl<CT_ADDRESS>::load (const GncSqlBackend* sql_be,
      76                 :            :                                               GncSqlRow& row,
      77                 :            :                                               QofIdTypeConst obj_name,
      78                 :            :                                               gpointer pObject) const noexcept
      79                 :            : {
      80                 :          4 :     g_return_if_fail (sql_be != NULL);
      81                 :          4 :     g_return_if_fail (pObject != NULL);
      82                 :            : 
      83                 :          4 :     auto addr = gncAddressCreate (sql_be->book(), QOF_INSTANCE(pObject));
      84                 :            : 
      85                 :         36 :     for (auto const& subtable_row : col_table)
      86                 :            :     {
      87                 :         64 :         auto buf = std::string{m_col_name} + "_" + subtable_row->m_col_name;
      88                 :         32 :         auto val = row.get_string_at_col (buf.c_str());
      89                 :         32 :         auto sub_setter = subtable_row->get_setter(GNC_ID_ADDRESS);
      90                 :         32 :         if (val)
      91                 :         32 :             set_parameter (addr, val->c_str(), sub_setter,
      92                 :         32 :                            subtable_row->m_gobj_param_name);
      93                 :         32 :     }
      94                 :            : 
      95                 :          4 :     set_parameter (pObject, addr,
      96                 :          4 :                    reinterpret_cast<AddressSetterFunc>(get_setter(obj_name)),
      97                 :          4 :                    m_gobj_param_name);
      98                 :            : }
      99                 :            : 
     100                 :            : template<> void
     101                 :         20 : GncSqlColumnTableEntryImpl<CT_ADDRESS>::add_to_table(ColVec& vec) const noexcept
     102                 :            : {
     103                 :        180 :     for (auto const& subtable_row : col_table)
     104                 :            :     {
     105                 :        320 :         auto buf = std::string{m_col_name} + "_" + subtable_row->m_col_name;
     106                 :        160 :         GncSqlColumnInfo info(buf.c_str(), BCT_STRING, subtable_row->m_size,
     107                 :        320 :                               true, false, m_flags & COL_PKEY, m_flags & COL_NNUL);
     108                 :        160 :         vec.emplace_back(std::move(info));
     109                 :        160 :     }
     110                 :         20 : }
     111                 :            : 
     112                 :            : /* char is unusual in that we get a pointer but don't deref it to pass
     113                 :            :  * it to operator<<().
     114                 :            :  */
     115                 :            : template<> void
     116                 :          4 : GncSqlColumnTableEntryImpl<CT_ADDRESS>::add_to_query(QofIdTypeConst obj_name,
     117                 :            :                                                     const gpointer pObject,
     118                 :            :                                                     PairVec& vec) const noexcept
     119                 :            : {
     120                 :          4 :     auto addr(get_row_value_from_object<char*>(obj_name, pObject));
     121                 :          4 :     if (addr == nullptr) return;
     122                 :            : 
     123                 :         36 :     for (auto const& subtable_row : col_table)
     124                 :            :     {
     125                 :         32 :         auto s = subtable_row->get_row_value_from_object<char*>(GNC_ID_ADDRESS,
     126                 :            :                                                                 addr);
     127                 :         32 :         if (s == nullptr)
     128                 :          0 :             continue;
     129                 :         64 :         auto buf = std::string{m_col_name} + "_" + subtable_row->m_col_name;
     130                 :         32 :         vec.emplace_back(make_pair(buf, quote_string(s)));
     131                 :         32 :     }
     132                 :            : }
     133                 :            : /* ========================== END OF FILE ===================== */

Generated by: LCOV version 1.14