GnuCash c935c2f+
Loading...
Searching...
No Matches
gnc-owner-sql.cpp
1/********************************************************************\
2 * gnc-owner-sql.c -- owner 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
30#include <guid.hpp>
31#include <config.h>
32#include <qof.h>
33#include <glib.h>
34#include "gncCustomerP.h"
35#include "gncJobP.h"
36#include "gncEmployeeP.h"
37#include "gncVendorP.h"
38#include <cstdlib>
39#include <cstring>
40#include <sstream>
41#include "gnc-sql-backend.hpp"
42#include "gnc-sql-column-table-entry.hpp"
43
44static QofLogModule log_module = G_LOG_DOMAIN;
45
46typedef void (*OwnerSetterFunc) (gpointer, GncOwner*);
47typedef GncOwner* (*OwnerGetterFunc) (const gpointer);
48
49template<> void
51 GncSqlRow& row,
52 QofIdTypeConst obj_name,
53 gpointer pObject) const noexcept
54{
55 GncOwnerType type;
56 GncGUID guid;
57 GncOwner owner;
58 GncGUID* pGuid = nullptr;
59
60 g_return_if_fail (sql_be != nullptr);
61 g_return_if_fail (pObject != nullptr);
62
63 auto book = sql_be->book();
64 auto buf = std::string{m_col_name} + "_type";
65 try
66 {
67 type = static_cast<decltype(type)>(row.get_int_at_col(buf.c_str()).value_or(0));
68 buf = std::string{m_col_name} + "_guid";
69 auto val = row.get_string_at_col (buf.c_str());
70 if (val && string_to_guid (val->c_str(), &guid))
71 pGuid = &guid;
72 }
73 catch (std::invalid_argument&)
74 {
75 return;
76 }
77 if (type == GNC_OWNER_NONE || pGuid == nullptr)
78 return;
79
80 switch (type)
81 {
82 case GNC_OWNER_CUSTOMER:
83 {
84 GncCustomer* cust = NULL;
85
86 if (pGuid != NULL)
87 {
88 cust = gncCustomerLookup (book, pGuid);
89 if (cust == NULL)
90 {
91 cust = gncCustomerCreate (book);
92 gncCustomerSetGUID (cust, &guid);
93 }
94 }
95 gncOwnerInitCustomer (&owner, cust);
96 break;
97 }
98
99 case GNC_OWNER_JOB:
100 {
101 GncJob* job = NULL;
102
103 if (pGuid != NULL)
104 {
105 job = gncJobLookup (book, pGuid);
106 if (job == NULL)
107 {
108 job = gncJobCreate (book);
109 gncJobSetGUID (job, &guid);
110 }
111 }
112 gncOwnerInitJob (&owner, job);
113 break;
114 }
115
116 case GNC_OWNER_VENDOR:
117 {
118 GncVendor* vendor = NULL;
119
120 if (pGuid != NULL)
121 {
122 vendor = gncVendorLookup (book, pGuid);
123 if (vendor == NULL)
124 {
125 vendor = gncVendorCreate (book);
126 gncVendorSetGUID (vendor, &guid);
127 }
128 }
129 gncOwnerInitVendor (&owner, vendor);
130 break;
131 }
132
133 case GNC_OWNER_EMPLOYEE:
134 {
135 GncEmployee* employee = NULL;
136
137 if (pGuid != NULL)
138 {
139 employee = gncEmployeeLookup (book, pGuid);
140 if (employee == NULL)
141 {
142 employee = gncEmployeeCreate (book);
143 gncEmployeeSetGUID (employee, &guid);
144 }
145 }
146 gncOwnerInitEmployee (&owner, employee);
147 break;
148 }
149
150 default:
151 PWARN ("Invalid owner type: %d\n", type);
152 }
153 set_parameter (pObject, &owner, get_setter(obj_name), m_gobj_param_name);
154}
155
156template<> void
158{
159 auto buf = g_strdup_printf ("%s_type", m_col_name);
160 GncSqlColumnInfo info(buf, BCT_INT, 0, false, false,
161 m_flags & COL_PKEY, m_flags & COL_NNUL);
162 vec.emplace_back(std::move(info));
163/* Buf isn't leaking, it belongs to ColVec now. */
164 buf = g_strdup_printf ("%s_guid", m_col_name);
165 GncSqlColumnInfo info2(buf, BCT_STRING, GUID_ENCODING_LENGTH, false, false,
166 m_flags & COL_PKEY, m_flags & COL_NNUL);
167 vec.emplace_back(std::move(info2));
168}
169
170template<> void
172 const gpointer pObject,
173 PairVec& vec) const noexcept
174{
175 g_return_if_fail (obj_name != NULL);
176 g_return_if_fail (pObject != NULL);
177
178 auto getter = (OwnerGetterFunc)get_getter (obj_name);
179 auto owner = (*getter) (pObject);
180
181 QofInstance* inst = nullptr;
182 GncOwnerType type = GNC_OWNER_NONE;
183
184 auto type_hdr = std::string{m_col_name} + "_type";
185 auto guid_hdr = std::string{m_col_name} + "_guid";
186
187 if (owner != nullptr)
188 {
189 type = gncOwnerGetType (owner);
190 switch (type)
191 {
192 case GNC_OWNER_CUSTOMER:
193 inst = QOF_INSTANCE (gncOwnerGetCustomer (owner));
194 break;
195
196 case GNC_OWNER_JOB:
197 inst = QOF_INSTANCE (gncOwnerGetJob (owner));
198 break;
199
200 case GNC_OWNER_VENDOR:
201 inst = QOF_INSTANCE (gncOwnerGetVendor (owner));
202 break;
203
204 case GNC_OWNER_EMPLOYEE:
205 inst = QOF_INSTANCE (gncOwnerGetEmployee (owner));
206 break;
207
208 default:
209 PWARN ("Invalid owner type: %d\n", type);
210 }
211 }
212
213 if (inst == nullptr)
214 {
215 /* Twice, once for type, once for guid. */
216 vec.emplace_back (std::make_pair (type_hdr, std::string{"NULL"}));
217 vec.emplace_back (std::make_pair (guid_hdr, std::string{"NULL"}));
218
219 return;
220 }
221 std::ostringstream buf;
222
223 buf << type;
224 vec.emplace_back(std::make_pair(type_hdr, quote_string(buf.str())));
225 buf.str("");
226 auto guid = qof_instance_get_guid(inst);
227 if (guid != nullptr)
228 {
229 char strbuff[GUID_ENCODING_LENGTH+1];
230 guid_to_string_buff (guid, strbuff);
231 buf << strbuff;
232 }
233 else
234 buf << "NULL";
235 vec.emplace_back(std::make_pair(guid_hdr, quote_string(buf.str())));
236}
Main SQL backend structure.
void add_to_query(QofIdTypeConst obj_name, void *pObject, PairVec &vec) const noexcept override
Add a pair of the table column heading and object's value's string representation to a PairVec; used ...
void add_to_table(ColVec &vec) const noexcept override
Add a GncSqlColumnInfo structure for the column type to a ColVec.
void load(const GncSqlBackend *sql_be, GncSqlRow &row, QofIdTypeConst obj_name, void *pObject) const noexcept override
Load a value into an object from the database row.
Row of SQL Query results.
const gchar * QofIdTypeConst
QofIdTypeConst declaration.
Definition qofid.h:82
#define GUID_ENCODING_LENGTH
Number of characters needed to encode a guid as a string not including the null terminator.
Definition guid.h:84
gchar * guid_to_string_buff(const GncGUID *guid, gchar *str)
The guid_to_string_buff() routine puts a null-terminated string encoding of the id into the memory po...
Definition guid.cpp:208
const GncGUID * qof_instance_get_guid(gconstpointer inst)
Return the GncGUID of this instance.
#define PWARN(format, args...)
Log a warning.
Definition qoflog.h:250
GncCustomer * gncOwnerGetCustomer(const GncOwner *owner)
If the given owner is of type GNC_OWNER_CUSTOMER, returns the pointer to the customer object.
Definition gncOwner.c:369
GncEmployee * gncOwnerGetEmployee(const GncOwner *owner)
If the given owner is of type GNC_OWNER_EMPLOYEE, returns the pointer to the employee object.
Definition gncOwner.c:390
GncJob * gncOwnerGetJob(const GncOwner *owner)
If the given owner is of type GNC_OWNER_JOB, returns the pointer to the job object.
Definition gncOwner.c:376
GncVendor * gncOwnerGetVendor(const GncOwner *owner)
If the given owner is of type GNC_OWNER_VENDOR, returns the pointer to the vendor object.
Definition gncOwner.c:383
GncOwnerType gncOwnerGetType(const GncOwner *owner)
Returns the GncOwnerType of this owner.
Definition gncOwner.c:200
credit, discount and shipaddr are unique to GncCustomer id, name, notes, terms, addr,...
The type used to store guids in C.
Definition guid.h:75
information required to create a column in a table.