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>
|
2015-11-28 15:40:55 -06:00
|
|
|
extern "C"
|
|
|
|
{
|
2008-08-01 11:02:07 -05:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "gncCustomerP.h"
|
|
|
|
#include "gncJobP.h"
|
|
|
|
#include "gncEmployeeP.h"
|
|
|
|
#include "gncVendorP.h"
|
2015-11-28 15:40:55 -06:00
|
|
|
}
|
|
|
|
#include "gnc-backend-sql.h"
|
|
|
|
#include "gnc-owner-sql.h"
|
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
|
|
|
|
|
|
|
static void
|
2016-03-12 16:04:40 -06:00
|
|
|
load_owner (const GncSqlBackend* be, GncSqlRow* row,
|
2008-08-01 11:02:07 -05:00
|
|
|
QofSetterFunc setter, gpointer pObject,
|
2016-03-12 16:04:40 -06:00
|
|
|
const GncSqlColumnTableEntry* table_row)
|
2008-08-01 11:02:07 -05:00
|
|
|
{
|
|
|
|
const GValue* val;
|
|
|
|
gchar* buf;
|
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
|
|
|
QofBook* book;
|
|
|
|
GncOwner owner;
|
2010-03-27 16:01:21 -05:00
|
|
|
GncGUID* pGuid = NULL;
|
2008-08-01 11:02:07 -05:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
g_return_if_fail (be != NULL);
|
|
|
|
g_return_if_fail (row != NULL);
|
|
|
|
g_return_if_fail (pObject != NULL);
|
|
|
|
g_return_if_fail (table_row != NULL);
|
2008-08-01 11:02:07 -05:00
|
|
|
|
2011-11-13 15:33:29 -06:00
|
|
|
book = be->book;
|
2016-03-12 16:04:40 -06:00
|
|
|
buf = g_strdup_printf ("%s_type", table_row->col_name);
|
|
|
|
val = gnc_sql_row_get_value_at_col_name (row, buf);
|
|
|
|
type = (GncOwnerType)gnc_sql_get_integer_value (val);
|
|
|
|
g_free (buf);
|
|
|
|
buf = g_strdup_printf ("%s_guid", table_row->col_name);
|
|
|
|
val = gnc_sql_row_get_value_at_col_name (row, buf);
|
|
|
|
g_free (buf);
|
|
|
|
|
|
|
|
if (val != NULL && G_VALUE_HOLDS_STRING (val) &&
|
|
|
|
g_value_get_string (val) != NULL)
|
2010-03-02 15:41:05 -06:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
string_to_guid (g_value_get_string (val), &guid);
|
2010-03-02 15:41:05 -06:00
|
|
|
pGuid = &guid;
|
|
|
|
}
|
|
|
|
|
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-03-12 16:04:40 -06:00
|
|
|
if (table_row->gobj_param_name != NULL)
|
2010-03-02 15:41:05 -06:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
qof_instance_increase_editlevel (pObject);
|
|
|
|
g_object_set (pObject, table_row->gobj_param_name, &owner, NULL);
|
|
|
|
qof_instance_decrease_editlevel (pObject);
|
2010-03-02 15:41:05 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
(*setter) (pObject, &owner);
|
2010-03-02 15:41:05 -06:00
|
|
|
}
|
2008-08-01 11:02:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-03-12 16:04:40 -06:00
|
|
|
add_owner_col_info_to_list (const GncSqlBackend* be,
|
|
|
|
const GncSqlColumnTableEntry* table_row,
|
|
|
|
GList** pList)
|
2008-08-01 11:02:07 -05:00
|
|
|
{
|
|
|
|
gchar* buf;
|
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
g_return_if_fail (be != NULL);
|
|
|
|
g_return_if_fail (table_row != NULL);
|
|
|
|
g_return_if_fail (pList != NULL);
|
2008-08-01 11:02:07 -05:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
buf = g_strdup_printf ("%s_type", table_row->col_name);
|
2016-02-28 14:18:49 -06:00
|
|
|
auto info = new GncSqlColumnInfo(buf, BCT_INT, 0, false, false,
|
|
|
|
table_row->flags & COL_PKEY,
|
2016-02-28 14:28:18 -06:00
|
|
|
table_row->flags & COL_NNUL);
|
2016-02-28 14:18:49 -06:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
*pList = g_list_append (*pList, info);
|
2010-03-02 15:41:05 -06:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
buf = g_strdup_printf ("%s_guid", table_row->col_name);
|
2016-02-28 14:18:49 -06:00
|
|
|
info = new GncSqlColumnInfo(buf, BCT_STRING, GUID_ENCODING_LENGTH,
|
|
|
|
false, false,
|
|
|
|
table_row->flags & COL_PKEY,
|
2016-02-28 14:28:18 -06:00
|
|
|
table_row->flags & COL_NNUL);
|
2016-02-28 14:18:49 -06:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
*pList = g_list_append (*pList, info);
|
2008-08-01 11:02:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-03-12 16:04:40 -06:00
|
|
|
add_colname_to_list (const GncSqlColumnTableEntry* table_row, GList** pList)
|
2008-08-01 11:02:07 -05:00
|
|
|
{
|
|
|
|
gchar* buf;
|
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
buf = g_strdup_printf ("%s_type", table_row->col_name);
|
|
|
|
(*pList) = g_list_append ((*pList), buf);
|
|
|
|
buf = g_strdup_printf ("%s_guid", table_row->col_name);
|
|
|
|
(*pList) = g_list_append ((*pList), buf);
|
2008-08-01 11:02:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-03-12 16:04:40 -06:00
|
|
|
add_gvalue_owner_to_slist (const GncSqlBackend* be, QofIdTypeConst obj_name,
|
|
|
|
const gpointer pObject, const GncSqlColumnTableEntry* table_row,
|
|
|
|
GSList** pList)
|
2008-08-01 11:02:07 -05:00
|
|
|
{
|
|
|
|
GValue* subfield_value;
|
|
|
|
GncOwner* owner;
|
2010-03-02 15:41:05 -06:00
|
|
|
gchar* buf;
|
2010-03-27 16:01:21 -05:00
|
|
|
const GncGUID* guid;
|
2016-03-12 16:04:40 -06:00
|
|
|
gchar guid_buf[GUID_ENCODING_LENGTH + 1];
|
2010-03-02 15:41:05 -06:00
|
|
|
GncOwnerType type;
|
|
|
|
QofInstance* inst = NULL;
|
2008-08-01 11:02:07 -05:00
|
|
|
OwnerGetterFunc getter;
|
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
g_return_if_fail (be != NULL);
|
|
|
|
g_return_if_fail (obj_name != NULL);
|
|
|
|
g_return_if_fail (pObject != NULL);
|
|
|
|
g_return_if_fail (table_row != NULL);
|
2008-08-01 11:02:07 -05:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
getter = (OwnerGetterFunc)gnc_sql_get_getter (obj_name, table_row);
|
|
|
|
owner = (*getter) (pObject);
|
2008-08-01 11:02:07 -05:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
if (owner != NULL)
|
2010-03-02 15:41:05 -06:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
buf = g_strdup_printf ("%s_type", table_row->col_name);
|
|
|
|
subfield_value = g_new0 (GValue, 1);
|
|
|
|
g_value_init (subfield_value, G_TYPE_INT);
|
|
|
|
type = gncOwnerGetType (owner);
|
|
|
|
g_value_set_int (subfield_value, type);
|
|
|
|
(*pList) = g_slist_append ((*pList), subfield_value);
|
|
|
|
g_free (buf);
|
|
|
|
|
|
|
|
buf = g_strdup_printf ("%s_guid", table_row->col_name);
|
|
|
|
subfield_value = g_new0 (GValue, 1);
|
|
|
|
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 16:04:40 -06:00
|
|
|
g_value_init (subfield_value, G_TYPE_STRING);
|
|
|
|
if (inst != NULL)
|
2010-03-02 15:41:05 -06:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
guid = qof_instance_get_guid (inst);
|
|
|
|
if (guid != NULL)
|
2010-03-02 15:41:05 -06:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
(void)guid_to_string_buff (guid, guid_buf);
|
|
|
|
g_value_take_string (subfield_value, g_strdup_printf ("%s", guid_buf));
|
2010-03-02 15:41:05 -06:00
|
|
|
}
|
|
|
|
}
|
2016-03-12 16:04:40 -06:00
|
|
|
(*pList) = g_slist_append ((*pList), subfield_value);
|
|
|
|
g_free (buf);
|
2010-03-02 15:41:05 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
subfield_value = g_new0 (GValue, 1);
|
|
|
|
g_value_init (subfield_value, G_TYPE_STRING);
|
|
|
|
g_value_set_string (subfield_value, "NULL");
|
|
|
|
(*pList) = g_slist_append ((*pList), subfield_value);
|
|
|
|
subfield_value = g_new0 (GValue, 1);
|
|
|
|
g_value_init (subfield_value, G_TYPE_STRING);
|
|
|
|
g_value_set_string (subfield_value, "NULL");
|
|
|
|
(*pList) = g_slist_append ((*pList), subfield_value);
|
2010-03-02 15:41:05 -06:00
|
|
|
}
|
2008-08-01 11:02:07 -05:00
|
|
|
}
|
|
|
|
|
2008-08-02 13:57:13 -05:00
|
|
|
static GncSqlColumnTypeHandler owner_handler
|
2010-03-02 15:41:05 -06:00
|
|
|
= { load_owner,
|
|
|
|
add_owner_col_info_to_list,
|
|
|
|
add_colname_to_list,
|
|
|
|
add_gvalue_owner_to_slist
|
|
|
|
};
|
2008-08-01 11:02:07 -05:00
|
|
|
|
|
|
|
/* ================================================================= */
|
|
|
|
void
|
2016-03-12 16:04:40 -06:00
|
|
|
gnc_owner_sql_initialize (void)
|
2008-08-01 11:02:07 -05:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
gnc_sql_register_col_type_handler (CT_OWNERREF, &owner_handler);
|
2008-08-01 11:02:07 -05:00
|
|
|
}
|
|
|
|
/* ========================== END OF FILE ===================== */
|