GnuCash c935c2f+
Loading...
Searching...
No Matches
gnc-vendor-sql.cpp
1/********************************************************************\
2 * gnc-vendor-sql.c -- vendor sql backend *
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 <glib.h>
31
32#include <config.h>
33#include <stdlib.h>
34#include <string.h>
35
36#include "gnc-commodity.h"
37#include "gncBillTermP.h"
38#include "gncVendorP.h"
39#include "gncTaxTableP.h"
40
41#include "gnc-sql-connection.hpp"
42#include "gnc-sql-backend.hpp"
43#include "gnc-sql-object-backend.hpp"
44#include "gnc-sql-column-table-entry.hpp"
45#include "gnc-vendor-sql.h"
46#include "gnc-bill-term-sql.h"
47#include "gnc-tax-table-sql.h"
48#include "gnc-commodity-sql.h"
49#include "gnc-slots-sql.h"
50
51#define _GNC_MOD_NAME GNC_ID_VENDOR
52
53G_GNUC_UNUSED static QofLogModule log_module = G_LOG_DOMAIN;
54
55#define MAX_NAME_LEN 2048
56#define MAX_ID_LEN 2048
57#define MAX_NOTES_LEN 2048
58#define MAX_TAX_INC_LEN 2048
59
60#define TABLE_NAME "vendors"
61#define TABLE_VERSION 1
62
63static EntryVec col_table
64({
65 gnc_sql_make_table_entry<CT_GUID>("guid", 0, COL_NNUL | COL_PKEY, "guid"),
66 gnc_sql_make_table_entry<CT_STRING>("name", MAX_NAME_LEN, COL_NNUL, "name"),
67 gnc_sql_make_table_entry<CT_STRING>("id", MAX_ID_LEN, COL_NNUL, "id"),
68 gnc_sql_make_table_entry<CT_STRING>("notes", MAX_NOTES_LEN, COL_NNUL,
69 "notes"),
70 gnc_sql_make_table_entry<CT_COMMODITYREF>("currency", 0, COL_NNUL,
71 "currency"),
72 gnc_sql_make_table_entry<CT_BOOLEAN>("active", 0, COL_NNUL, "active"),
73 gnc_sql_make_table_entry<CT_BOOLEAN>("tax_override", 0, COL_NNUL,
74 "tax-table-override"),
75 gnc_sql_make_table_entry<CT_ADDRESS>("addr", 0, 0, "address"),
76 gnc_sql_make_table_entry<CT_BILLTERMREF>("terms", 0, 0, "terms"),
77 gnc_sql_make_table_entry<CT_STRING>("tax_inc", MAX_TAX_INC_LEN, 0,
78 "tax-included-string"),
79 gnc_sql_make_table_entry<CT_TAXTABLEREF>("tax_table", 0, 0, "tax-table"),
80});
81
82GncSqlVendorBackend::GncSqlVendorBackend() :
83 GncSqlObjectBackend(TABLE_VERSION, GNC_ID_VENDOR,
84 TABLE_NAME, col_table) {}
85
86static GncVendor*
87load_single_vendor (GncSqlBackend* sql_be, GncSqlRow& row)
88{
89 const GncGUID* guid;
90 GncVendor* pVendor;
91
92 g_return_val_if_fail (sql_be != NULL, NULL);
93
94 guid = gnc_sql_load_guid (sql_be, row);
95 pVendor = gncVendorLookup (sql_be->book(), guid);
96 if (pVendor == NULL)
97 {
98 pVendor = gncVendorCreate (sql_be->book());
99 }
100 gnc_sql_load_object (sql_be, row, GNC_ID_VENDOR, pVendor, col_table);
101 qof_instance_mark_clean (QOF_INSTANCE (pVendor));
102
103 return pVendor;
104}
105
106/* Because gncVendorLookup has the arguments backwards: */
107static inline GncVendor*
108gnc_vendor_lookup (const GncGUID *guid, const QofBook *book)
109{
110 QOF_BOOK_RETURN_ENTITY(book, guid, GNC_ID_VENDOR, GncVendor);
111}
112
113void
115{
116 g_return_if_fail (sql_be != NULL);
117
118 std::string sql("SELECT * FROM " TABLE_NAME);
119 auto stmt = sql_be->create_statement_from_sql(sql);
120 auto result = sql_be->execute_select_statement(stmt);
121
122 for (auto row : *result)
123 load_single_vendor (sql_be, row);
124
125 std::string pkey(col_table[0]->name());
126 sql = "SELECT DISTINCT ";
127 sql += pkey + " FROM " TABLE_NAME;
128 gnc_sql_slots_load_for_sql_subquery (sql_be, sql,
129 (BookLookupFn)gnc_vendor_lookup);
130}
131
132/* ================================================================= */
133bool
135{
136 GncVendor* v;
137 const GncGUID* guid;
138 E_DB_OPERATION op;
139 gboolean is_infant;
140 gboolean is_ok = TRUE;
141
142 g_return_val_if_fail (inst != NULL, FALSE);
143 g_return_val_if_fail (GNC_IS_VENDOR (inst), FALSE);
144 g_return_val_if_fail (sql_be != NULL, FALSE);
145
146 v = GNC_VENDOR (inst);
147
148 is_infant = qof_instance_get_infant (inst);
150 {
151 op = OP_DB_DELETE;
152 }
153 else if (sql_be->pristine() || is_infant)
154 {
155 op = OP_DB_INSERT;
156 }
157 else
158 {
159 op = OP_DB_UPDATE;
160 }
161 if (op != OP_DB_DELETE)
162 {
163 // Ensure the commodity is in the db
164 is_ok = sql_be->save_commodity (gncVendorGetCurrency(v));
165 }
166 if (is_ok)
167 {
168 is_ok = sql_be->do_db_operation(op, TABLE_NAME, GNC_ID_VENDOR, v,
169 col_table);
170 }
171
172 if (is_ok)
173 {
174 // Now, commit or delete any slots
175 guid = qof_instance_get_guid (inst);
176 if (!qof_instance_get_destroying (inst))
177 {
178 is_ok = gnc_sql_slots_save (sql_be, guid, is_infant, inst);
179 }
180 else
181 {
182 is_ok = gnc_sql_slots_delete (sql_be, guid);
183 }
184 }
185
186 return is_ok;
187}
188
189/* ================================================================= */
190static gboolean
191vendor_should_be_saved (GncVendor* vendor)
192{
193 const char* id;
194
195 g_return_val_if_fail (vendor != NULL, FALSE);
196
197 /* make sure this is a valid vendor before we save it -- should have an ID */
198 id = gncVendorGetID (vendor);
199 if (id == NULL || *id == '\0')
200 {
201 return FALSE;
202 }
203
204 return TRUE;
205}
206
207static void
208write_single_vendor (QofInstance* term_p, gpointer data_p)
209{
210 auto s = reinterpret_cast<write_objects_t*>(data_p);
211
212 g_return_if_fail (term_p != NULL);
213 g_return_if_fail (GNC_IS_VENDOR (term_p));
214 g_return_if_fail (data_p != NULL);
215
216 if (vendor_should_be_saved (GNC_VENDOR (term_p)))
217 {
218 s->commit (term_p);
219 }
220}
221
222bool
224{
225 g_return_val_if_fail (sql_be != NULL, FALSE);
226 write_objects_t data{sql_be, true, this};
227
228 qof_object_foreach (GNC_ID_VENDOR, sql_be->book(), write_single_vendor, &data);
229
230 return data.is_ok;
231}
232
233/* ========================== END OF FILE ===================== */
Main SQL backend structure.
GncSqlResultPtr execute_select_statement(const GncSqlStatementPtr &stmt) const noexcept
Executes an SQL SELECT statement and returns the result rows.
bool do_db_operation(E_DB_OPERATION op, const char *table_name, QofIdTypeConst obj_name, gpointer pObject, const EntryVec &table) const noexcept
Performs an operation on the database.
bool save_commodity(gnc_commodity *comm) noexcept
Ensure that a commodity referenced in another object is in fact saved in the database.
Encapsulates per-class table schema with functions to load, create a table, commit a changed front-en...
Row of SQL Query results.
void load_all(GncSqlBackend *) override
Load all objects of m_type in the database into memory.
bool write(GncSqlBackend *) override
Write all objects of m_type_name to the database.
bool commit(GncSqlBackend *, QofInstance *) override
UPDATE/INSERT a single instance of m_type_name into the database.
load and save accounts data to SQL
load and save data to SQL
Commodity handling public routines.
load and save accounts data to SQL
load and save tax table data to SQL
load and save vendor data to SQL
#define QOF_BOOK_RETURN_ENTITY(book, guid, e_type, c_type)
Encapsulates all the information about a dataset manipulated by QOF.
Definition qofbook.h:101
const GncGUID * qof_instance_get_guid(gconstpointer inst)
Return the GncGUID of this instance.
gboolean qof_instance_get_destroying(gconstpointer ptr)
Retrieve the flag that indicates whether or not this object is about to be destroyed.
void qof_object_foreach(QofIdTypeConst type_name, QofBook *book, QofInstanceForeachCB cb, gpointer user_data)
Invoke the callback 'cb' on every instance ov a particular object type.
The type used to store guids in C.
Definition guid.h:75
QofBook reference.
Definition qofbook-p.hpp:47
Data-passing struct for callbacks to qof_object_foreach() used in GncSqlObjectBackend::write().