LCOV - code coverage report
Current view: top level - libgnucash/backend/sql - gnc-employee-sql.cpp (source / functions) Coverage Total Hit
Test: gnucash.info Lines: 88.3 % 77 68
Test Date: 2025-02-07 16:25:45 Functions: 88.9 % 9 8
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /********************************************************************\
       2                 :             :  * gnc-employee-sql.c -- employee sql 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-employee-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 "gnc-commodity.h"
      38                 :             : #include "gncEmployeeP.h"
      39                 :             : 
      40                 :             : #include "gnc-sql-connection.hpp"
      41                 :             : #include "gnc-sql-backend.hpp"
      42                 :             : #include "gnc-sql-object-backend.hpp"
      43                 :             : #include "gnc-sql-column-table-entry.hpp"
      44                 :             : #include "gnc-slots-sql.h"
      45                 :             : #include "gnc-commodity-sql.h"
      46                 :             : #include "gnc-employee-sql.h"
      47                 :             : 
      48                 :             : #define _GNC_MOD_NAME   GNC_ID_EMPLOYEE
      49                 :             : 
      50                 :             : static QofLogModule log_module = G_LOG_DOMAIN;
      51                 :             : 
      52                 :             : #define MAX_USERNAME_LEN 2048
      53                 :             : #define MAX_ID_LEN 2048
      54                 :             : #define MAX_LANGUAGE_LEN 2048
      55                 :             : #define MAX_ACL_LEN 2048
      56                 :             : 
      57                 :             : #define TABLE_NAME "employees"
      58                 :             : #define TABLE_VERSION 2
      59                 :             : 
      60                 :             : static EntryVec col_table
      61                 :             : ({
      62                 :             :     gnc_sql_make_table_entry<CT_GUID>("guid", 0, COL_NNUL | COL_PKEY, "guid"),
      63                 :             :     gnc_sql_make_table_entry<CT_STRING>(
      64                 :             :         "username", MAX_USERNAME_LEN, COL_NNUL, "username"),
      65                 :             :     gnc_sql_make_table_entry<CT_STRING>("id", MAX_ID_LEN, COL_NNUL, "id"),
      66                 :             :     gnc_sql_make_table_entry<CT_STRING>(
      67                 :             :         "language", MAX_LANGUAGE_LEN, COL_NNUL, "language"),
      68                 :             :     gnc_sql_make_table_entry<CT_STRING>("acl", MAX_ACL_LEN, COL_NNUL, "acl"),
      69                 :             :     gnc_sql_make_table_entry<CT_BOOLEAN>("active", 0, COL_NNUL, "active"),
      70                 :             :     gnc_sql_make_table_entry<CT_COMMODITYREF>(
      71                 :             :         "currency", 0, COL_NNUL, "currency"),
      72                 :             :     gnc_sql_make_table_entry<CT_ACCOUNTREF>(
      73                 :             :         "ccard_guid", 0, 0, "credit-card-account"),
      74                 :             :     gnc_sql_make_table_entry<CT_NUMERIC>("workday", 0, COL_NNUL, "workday"),
      75                 :             :     gnc_sql_make_table_entry<CT_NUMERIC>("rate", 0, COL_NNUL, "rate"),
      76                 :             :     gnc_sql_make_table_entry<CT_ADDRESS>("addr", 0, 0, "address"),
      77                 :             : });
      78                 :             : 
      79                 :          10 : GncSqlEmployeeBackend::GncSqlEmployeeBackend() :
      80                 :             :     GncSqlObjectBackend(TABLE_VERSION, GNC_ID_EMPLOYEE,
      81                 :          50 :                         TABLE_NAME, col_table) {}
      82                 :             : 
      83                 :             : static GncEmployee*
      84                 :           1 : load_single_employee (GncSqlBackend* sql_be, GncSqlRow& row)
      85                 :             : {
      86                 :             :     const GncGUID* guid;
      87                 :             :     GncEmployee* pEmployee;
      88                 :             : 
      89                 :           1 :     g_return_val_if_fail (sql_be != NULL, NULL);
      90                 :             : 
      91                 :           1 :     guid = gnc_sql_load_guid (sql_be, row);
      92                 :           1 :     pEmployee = gncEmployeeLookup (sql_be->book(), guid);
      93                 :           1 :     if (pEmployee == NULL)
      94                 :             :     {
      95                 :           1 :         pEmployee = gncEmployeeCreate (sql_be->book());
      96                 :             :     }
      97                 :           1 :     gnc_sql_load_object (sql_be, row, GNC_ID_EMPLOYEE, pEmployee, col_table);
      98                 :           1 :     qof_instance_mark_clean (QOF_INSTANCE (pEmployee));
      99                 :             : 
     100                 :           1 :     return pEmployee;
     101                 :             : }
     102                 :             : /* Because gncCustomerLookup has the arguments backwards: */
     103                 :             : static inline GncEmployee*
     104                 :           0 : gnc_employee_lookup (const GncGUID *guid, const QofBook *book)
     105                 :             : {
     106                 :           0 :     QOF_BOOK_RETURN_ENTITY(book, guid, GNC_ID_EMPLOYEE, GncEmployee);
     107                 :             : }
     108                 :             : 
     109                 :             : void
     110                 :           5 : GncSqlEmployeeBackend::load_all (GncSqlBackend* sql_be)
     111                 :             : {
     112                 :           5 :     g_return_if_fail (sql_be != NULL);
     113                 :             : 
     114                 :           5 :     std::string sql("SELECT * FROM " TABLE_NAME);
     115                 :           5 :     auto stmt = sql_be->create_statement_from_sql(sql);
     116                 :           5 :     auto result = sql_be->execute_select_statement(stmt);
     117                 :             : 
     118                 :           6 :     for (auto row : *result)
     119                 :           6 :         load_single_employee (sql_be, row);
     120                 :             : 
     121                 :           5 :     std::string pkey(col_table[0]->name());
     122                 :           5 :     sql = "SELECT DISTINCT ";
     123                 :           5 :     sql += pkey + " FROM " TABLE_NAME;
     124                 :           5 :     gnc_sql_slots_load_for_sql_subquery (sql_be, sql,
     125                 :             :                                          (BookLookupFn)gnc_employee_lookup);
     126                 :           5 : }
     127                 :             : 
     128                 :             : 
     129                 :             : /* ================================================================= */
     130                 :             : void
     131                 :          10 : GncSqlEmployeeBackend::create_tables (GncSqlBackend* sql_be)
     132                 :             : {
     133                 :             :     gint version;
     134                 :             : 
     135                 :          10 :     g_return_if_fail (sql_be != NULL);
     136                 :             : 
     137                 :          20 :     version = sql_be->get_table_version( TABLE_NAME);
     138                 :          10 :     if (version == 0)
     139                 :             :     {
     140                 :          15 :         sql_be->create_table(TABLE_NAME, TABLE_VERSION, col_table);
     141                 :             :     }
     142                 :           5 :     else if (version < m_version)
     143                 :             :     {
     144                 :             :         /* Upgrade 64 bit int handling */
     145                 :           0 :         sql_be->upgrade_table(TABLE_NAME, col_table);
     146                 :           0 :         sql_be->set_table_version (TABLE_NAME, TABLE_VERSION);
     147                 :             : 
     148                 :           0 :         PINFO ("Employees table upgraded from version 1 to version %d\n",
     149                 :             :                TABLE_VERSION);
     150                 :             :     }
     151                 :             : }
     152                 :             : 
     153                 :             : /* ================================================================= */
     154                 :             : bool
     155                 :           1 : GncSqlEmployeeBackend::commit (GncSqlBackend* sql_be, QofInstance* inst)
     156                 :             : {
     157                 :             :     GncEmployee* emp;
     158                 :             :     const GncGUID* guid;
     159                 :             :     E_DB_OPERATION op;
     160                 :             :     gboolean is_infant;
     161                 :           1 :     gboolean is_ok = TRUE;
     162                 :             : 
     163                 :           1 :     g_return_val_if_fail (inst != NULL, FALSE);
     164                 :           1 :     g_return_val_if_fail (GNC_IS_EMPLOYEE (inst), FALSE);
     165                 :           1 :     g_return_val_if_fail (sql_be != NULL, FALSE);
     166                 :             : 
     167                 :           1 :     emp = GNC_EMPLOYEE (inst);
     168                 :             : 
     169                 :           1 :     is_infant = qof_instance_get_infant (inst);
     170                 :           1 :     if (qof_instance_get_destroying (inst))
     171                 :             :     {
     172                 :           0 :         op = OP_DB_DELETE;
     173                 :             :     }
     174                 :           1 :     else if (sql_be->pristine() || is_infant)
     175                 :             :     {
     176                 :           1 :         op = OP_DB_INSERT;
     177                 :             :     }
     178                 :             :     else
     179                 :             :     {
     180                 :           0 :         op = OP_DB_UPDATE;
     181                 :             :     }
     182                 :           1 :     if (op != OP_DB_DELETE)
     183                 :             :     {
     184                 :             :         // Ensure the commodity is in the db
     185                 :           1 :         is_ok = sql_be->save_commodity(gncEmployeeGetCurrency (emp));
     186                 :             :     }
     187                 :             : 
     188                 :           1 :     if (is_ok)
     189                 :             :     {
     190                 :           1 :         is_ok = sql_be->do_db_operation(op, TABLE_NAME, GNC_ID_EMPLOYEE, emp,
     191                 :             :                                         col_table);
     192                 :             :     }
     193                 :             : 
     194                 :           1 :     if (is_ok)
     195                 :             :     {
     196                 :             :         // Now, commit or delete any slots
     197                 :           1 :         guid = qof_instance_get_guid (inst);
     198                 :           1 :         if (!qof_instance_get_destroying (inst))
     199                 :             :         {
     200                 :           1 :             is_ok = gnc_sql_slots_save (sql_be, guid, is_infant, inst);
     201                 :             :         }
     202                 :             :         else
     203                 :             :         {
     204                 :           0 :             is_ok = gnc_sql_slots_delete (sql_be, guid);
     205                 :             :         }
     206                 :             :     }
     207                 :             : 
     208                 :           1 :     return is_ok;
     209                 :             : }
     210                 :             : 
     211                 :             : /* ================================================================= */
     212                 :             : static gboolean
     213                 :           1 : employee_should_be_saved (GncEmployee* employee)
     214                 :             : {
     215                 :             :     const char* id;
     216                 :             : 
     217                 :           1 :     g_return_val_if_fail (employee != NULL, FALSE);
     218                 :             : 
     219                 :             :     /* make sure this is a valid employee before we save it -- should have an ID */
     220                 :           1 :     id = gncEmployeeGetID (employee);
     221                 :           1 :     if (id == NULL || *id == '\0')
     222                 :             :     {
     223                 :           0 :         return FALSE;
     224                 :             :     }
     225                 :             : 
     226                 :           1 :     return TRUE;
     227                 :             : }
     228                 :             : 
     229                 :             : static void
     230                 :           1 : write_single_employee (QofInstance* term_p, gpointer data_p)
     231                 :             : {
     232                 :           1 :     write_objects_t* s = (write_objects_t*)data_p;
     233                 :             : 
     234                 :           1 :     g_return_if_fail (term_p != NULL);
     235                 :           1 :     g_return_if_fail (GNC_IS_EMPLOYEE (term_p));
     236                 :           1 :     g_return_if_fail (data_p != NULL);
     237                 :             : 
     238                 :           1 :     if (s->is_ok && employee_should_be_saved (GNC_EMPLOYEE (term_p)))
     239                 :             :     {
     240                 :           1 :         s->is_ok = s->obe->commit (s->be, term_p);
     241                 :             :     }
     242                 :             : }
     243                 :             : 
     244                 :             : bool
     245                 :           5 : GncSqlEmployeeBackend::write (GncSqlBackend* sql_be)
     246                 :             : {
     247                 :           5 :     write_objects_t data;
     248                 :             : 
     249                 :           5 :     g_return_val_if_fail (sql_be != NULL, FALSE);
     250                 :             : 
     251                 :           5 :     data.be = sql_be;
     252                 :           5 :     data.is_ok = TRUE;
     253                 :           5 :     data.obe = this;
     254                 :           5 :     qof_object_foreach (GNC_ID_EMPLOYEE, sql_be->book(), write_single_employee, &data);
     255                 :             : 
     256                 :           5 :     return data.is_ok;
     257                 :             : }
     258                 :             : 
     259                 :             : 
     260                 :             : /* ========================== END OF FILE ===================== */
        

Generated by: LCOV version 2.0-1