2008-08-01 11:02:07 -05:00
|
|
|
/********************************************************************\
|
|
|
|
* gnc-owner-sql.c -- owner sql implementation *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License as *
|
|
|
|
* published by the Free Software Foundation; either version 2 of *
|
|
|
|
* the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License*
|
|
|
|
* along with this program; if not, contact: *
|
|
|
|
* *
|
|
|
|
* Free Software Foundation Voice: +1-617-542-5942 *
|
|
|
|
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
|
|
|
|
* Boston, MA 02110-1301, USA gnu@gnu.org *
|
|
|
|
* *
|
|
|
|
\********************************************************************/
|
|
|
|
|
|
|
|
/** @file gnc-owner-sql.c
|
|
|
|
* @brief load and save address data to SQL
|
|
|
|
* @author Copyright (c) 2007-2008 Phil Longstaff <plongstaff@rogers.com>
|
|
|
|
*
|
|
|
|
* This file implements the top-level QofBackend API for saving/
|
|
|
|
* restoring data to/from an SQL database
|
|
|
|
*/
|
2016-06-07 09:39:46 -05:00
|
|
|
#include <guid.hpp>
|
2016-11-01 13:58:21 -05:00
|
|
|
#include <config.h>
|
|
|
|
#include <qof.h>
|
2008-08-01 11:02:07 -05:00
|
|
|
#include <glib.h>
|
|
|
|
#include "gncCustomerP.h"
|
|
|
|
#include "gncJobP.h"
|
|
|
|
#include "gncEmployeeP.h"
|
|
|
|
#include "gncVendorP.h"
|
2016-11-01 13:58:21 -05:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <sstream>
|
|
|
|
#include "gnc-sql-backend.hpp"
|
|
|
|
#include "gnc-sql-column-table-entry.hpp"
|
2008-08-01 11:02:07 -05:00
|
|
|
|
|
|
|
static QofLogModule log_module = G_LOG_DOMAIN;
|
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
typedef void (*OwnerSetterFunc) (gpointer, GncOwner*);
|
|
|
|
typedef GncOwner* (*OwnerGetterFunc) (const gpointer);
|
2008-08-01 11:02:07 -05:00
|
|
|
|
2016-06-09 16:23:42 -05:00
|
|
|
template<> void
|
2016-10-30 19:56:24 -05:00
|
|
|
GncSqlColumnTableEntryImpl<CT_OWNERREF>::load (const GncSqlBackend* sql_be,
|
2016-06-09 16:23:42 -05:00
|
|
|
GncSqlRow& row,
|
|
|
|
QofIdTypeConst obj_name,
|
|
|
|
gpointer pObject) const noexcept
|
2008-08-01 11:02:07 -05:00
|
|
|
{
|
2010-03-02 15:41:05 -06:00
|
|
|
GncOwnerType type;
|
2010-03-27 16:01:21 -05:00
|
|
|
GncGUID guid;
|
2010-03-02 15:41:05 -06:00
|
|
|
GncOwner owner;
|
2017-10-27 11:39:02 -05:00
|
|
|
GncGUID* pGuid = nullptr;
|
2008-08-01 11:02:07 -05:00
|
|
|
|
2017-10-27 11:39:02 -05:00
|
|
|
g_return_if_fail (sql_be != nullptr);
|
|
|
|
g_return_if_fail (pObject != nullptr);
|
2008-08-01 11:02:07 -05:00
|
|
|
|
2016-10-30 19:56:24 -05:00
|
|
|
auto book = sql_be->book();
|
2016-06-09 16:23:42 -05:00
|
|
|
auto buf = std::string{m_col_name} + "_type";
|
2016-03-28 01:48:02 -05:00
|
|
|
try
|
2010-03-02 15:41:05 -06:00
|
|
|
{
|
2023-08-10 15:36:39 -05:00
|
|
|
type = static_cast<decltype(type)>(row.get_int_at_col(buf.c_str()).value_or(0));
|
2016-06-09 16:23:42 -05:00
|
|
|
buf = std::string{m_col_name} + "_guid";
|
|
|
|
auto val = row.get_string_at_col (buf.c_str());
|
2023-08-10 15:36:39 -05:00
|
|
|
if (val && string_to_guid (val->c_str(), &guid))
|
2017-10-27 11:39:02 -05:00
|
|
|
pGuid = &guid;
|
2010-03-02 15:41:05 -06:00
|
|
|
}
|
2018-04-03 21:02:44 -05:00
|
|
|
catch (std::invalid_argument&)
|
2016-03-28 01:48:02 -05:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2017-10-27 11:39:02 -05:00
|
|
|
if (type == GNC_OWNER_NONE || pGuid == nullptr)
|
|
|
|
return;
|
2023-08-10 15:36:39 -05:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
switch (type)
|
2010-03-02 15:41:05 -06:00
|
|
|
{
|
|
|
|
case GNC_OWNER_CUSTOMER:
|
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
GncCustomer* cust = NULL;
|
2010-03-02 15:41:05 -06:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
if (pGuid != NULL)
|
2010-03-02 15:41:05 -06:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
cust = gncCustomerLookup (book, pGuid);
|
|
|
|
if (cust == NULL)
|
2010-03-02 15:41:05 -06:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
cust = gncCustomerCreate (book);
|
|
|
|
gncCustomerSetGUID (cust, &guid);
|
2010-03-02 15:41:05 -06:00
|
|
|
}
|
|
|
|
}
|
2016-03-12 16:04:40 -06:00
|
|
|
gncOwnerInitCustomer (&owner, cust);
|
2010-03-02 15:41:05 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case GNC_OWNER_JOB:
|
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
GncJob* job = NULL;
|
2010-03-02 15:41:05 -06:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
if (pGuid != NULL)
|
2010-03-02 15:41:05 -06:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
job = gncJobLookup (book, pGuid);
|
|
|
|
if (job == NULL)
|
2010-03-02 15:41:05 -06:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
job = gncJobCreate (book);
|
|
|
|
gncJobSetGUID (job, &guid);
|
2010-03-02 15:41:05 -06:00
|
|
|
}
|
|
|
|
}
|
2016-03-12 16:04:40 -06:00
|
|
|
gncOwnerInitJob (&owner, job);
|
2010-03-02 15:41:05 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case GNC_OWNER_VENDOR:
|
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
GncVendor* vendor = NULL;
|
2010-03-02 15:41:05 -06:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
if (pGuid != NULL)
|
2010-03-02 15:41:05 -06:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
vendor = gncVendorLookup (book, pGuid);
|
|
|
|
if (vendor == NULL)
|
2010-03-02 15:41:05 -06:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
vendor = gncVendorCreate (book);
|
|
|
|
gncVendorSetGUID (vendor, &guid);
|
2010-03-02 15:41:05 -06:00
|
|
|
}
|
|
|
|
}
|
2016-03-12 16:04:40 -06:00
|
|
|
gncOwnerInitVendor (&owner, vendor);
|
2010-03-02 15:41:05 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case GNC_OWNER_EMPLOYEE:
|
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
GncEmployee* employee = NULL;
|
2010-03-02 15:41:05 -06:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
if (pGuid != NULL)
|
2010-03-02 15:41:05 -06:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
employee = gncEmployeeLookup (book, pGuid);
|
|
|
|
if (employee == NULL)
|
2010-03-02 15:41:05 -06:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
employee = gncEmployeeCreate (book);
|
|
|
|
gncEmployeeSetGUID (employee, &guid);
|
2010-03-02 15:41:05 -06:00
|
|
|
}
|
|
|
|
}
|
2016-03-12 16:04:40 -06:00
|
|
|
gncOwnerInitEmployee (&owner, employee);
|
2010-03-02 15:41:05 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2016-03-12 16:04:40 -06:00
|
|
|
PWARN ("Invalid owner type: %d\n", type);
|
2010-03-02 15:41:05 -06:00
|
|
|
}
|
2016-06-09 16:23:42 -05:00
|
|
|
set_parameter (pObject, &owner, get_setter(obj_name), m_gobj_param_name);
|
2008-08-01 11:02:07 -05:00
|
|
|
}
|
|
|
|
|
2016-06-09 16:23:42 -05:00
|
|
|
template<> void
|
2016-11-06 12:06:24 -06:00
|
|
|
GncSqlColumnTableEntryImpl<CT_OWNERREF>::add_to_table(ColVec& vec) const noexcept
|
2008-08-01 11:02:07 -05:00
|
|
|
{
|
2016-06-09 16:23:42 -05:00
|
|
|
auto buf = g_strdup_printf ("%s_type", m_col_name);
|
2016-02-28 16:38:29 -06:00
|
|
|
GncSqlColumnInfo info(buf, BCT_INT, 0, false, false,
|
2016-06-09 16:23:42 -05:00
|
|
|
m_flags & COL_PKEY, m_flags & COL_NNUL);
|
2016-02-28 16:38:29 -06:00
|
|
|
vec.emplace_back(std::move(info));
|
2016-06-09 16:23:42 -05:00
|
|
|
/* Buf isn't leaking, it belongs to ColVec now. */
|
|
|
|
buf = g_strdup_printf ("%s_guid", m_col_name);
|
|
|
|
GncSqlColumnInfo info2(buf, BCT_STRING, GUID_ENCODING_LENGTH, false, false,
|
|
|
|
m_flags & COL_PKEY, m_flags & COL_NNUL);
|
2016-02-28 16:38:29 -06:00
|
|
|
vec.emplace_back(std::move(info2));
|
2008-08-01 11:02:07 -05:00
|
|
|
}
|
|
|
|
|
2016-06-09 16:23:42 -05:00
|
|
|
template<> void
|
2016-11-06 12:06:24 -06:00
|
|
|
GncSqlColumnTableEntryImpl<CT_OWNERREF>::add_to_query(QofIdTypeConst obj_name,
|
2016-06-09 16:23:42 -05:00
|
|
|
const gpointer pObject,
|
|
|
|
PairVec& vec) const noexcept
|
2008-08-01 11:02:07 -05:00
|
|
|
{
|
2016-03-12 13:32:27 -06:00
|
|
|
g_return_if_fail (obj_name != NULL);
|
|
|
|
g_return_if_fail (pObject != NULL);
|
2008-08-01 11:02:07 -05:00
|
|
|
|
2016-06-09 16:23:42 -05:00
|
|
|
auto getter = (OwnerGetterFunc)get_getter (obj_name);
|
2016-03-12 13:32:27 -06:00
|
|
|
auto owner = (*getter) (pObject);
|
2008-08-01 11:02:07 -05:00
|
|
|
|
2016-03-12 13:32:27 -06:00
|
|
|
QofInstance* inst = nullptr;
|
2022-03-03 15:21:51 -06:00
|
|
|
GncOwnerType type = GNC_OWNER_NONE;
|
2008-08-01 11:02:07 -05:00
|
|
|
|
2016-06-09 16:23:42 -05:00
|
|
|
auto type_hdr = std::string{m_col_name} + "_type";
|
|
|
|
auto guid_hdr = std::string{m_col_name} + "_guid";
|
2008-08-01 11:02:07 -05:00
|
|
|
|
2016-03-12 13:32:27 -06:00
|
|
|
if (owner != nullptr)
|
2010-03-02 15:41:05 -06:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
type = gncOwnerGetType (owner);
|
|
|
|
switch (type)
|
2010-03-02 15:41:05 -06:00
|
|
|
{
|
|
|
|
case GNC_OWNER_CUSTOMER:
|
2016-03-12 16:04:40 -06:00
|
|
|
inst = QOF_INSTANCE (gncOwnerGetCustomer (owner));
|
2010-03-02 15:41:05 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GNC_OWNER_JOB:
|
2016-03-12 16:04:40 -06:00
|
|
|
inst = QOF_INSTANCE (gncOwnerGetJob (owner));
|
2010-03-02 15:41:05 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GNC_OWNER_VENDOR:
|
2016-03-12 16:04:40 -06:00
|
|
|
inst = QOF_INSTANCE (gncOwnerGetVendor (owner));
|
2010-03-02 15:41:05 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GNC_OWNER_EMPLOYEE:
|
2016-03-12 16:04:40 -06:00
|
|
|
inst = QOF_INSTANCE (gncOwnerGetEmployee (owner));
|
2010-03-02 15:41:05 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2016-03-12 16:04:40 -06:00
|
|
|
PWARN ("Invalid owner type: %d\n", type);
|
2010-03-02 15:41:05 -06:00
|
|
|
}
|
|
|
|
}
|
2016-03-12 13:32:27 -06:00
|
|
|
|
|
|
|
if (inst == nullptr)
|
2010-03-02 15:41:05 -06:00
|
|
|
{
|
2016-03-12 13:32:27 -06:00
|
|
|
/* Twice, once for type, once for guid. */
|
|
|
|
vec.emplace_back (std::make_pair (type_hdr, std::string{"NULL"}));
|
|
|
|
vec.emplace_back (std::make_pair (guid_hdr, std::string{"NULL"}));
|
|
|
|
|
|
|
|
return;
|
2010-03-02 15:41:05 -06:00
|
|
|
}
|
2016-06-09 16:23:42 -05:00
|
|
|
std::ostringstream buf;
|
|
|
|
|
2016-03-12 13:32:27 -06:00
|
|
|
buf << type;
|
2017-11-24 18:53:58 -06:00
|
|
|
vec.emplace_back(std::make_pair(type_hdr, quote_string(buf.str())));
|
2016-03-12 13:32:27 -06:00
|
|
|
buf.str("");
|
|
|
|
auto guid = qof_instance_get_guid(inst);
|
|
|
|
if (guid != nullptr)
|
2023-09-19 07:25:30 -05:00
|
|
|
{
|
|
|
|
char strbuff[GUID_ENCODING_LENGTH+1];
|
|
|
|
guid_to_string_buff (guid, strbuff);
|
|
|
|
buf << strbuff;
|
|
|
|
}
|
2016-03-12 13:32:27 -06:00
|
|
|
else
|
|
|
|
buf << "NULL";
|
2017-11-24 18:53:58 -06:00
|
|
|
vec.emplace_back(std::make_pair(guid_hdr, quote_string(buf.str())));
|
2008-08-01 11:02:07 -05:00
|
|
|
}
|