Branch data Line data Source code
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 : :
23 : : /** @file gnc-vendor-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 : : #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 : :
53 : : G_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 : :
63 : : static 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 : :
82 : 10 : GncSqlVendorBackend::GncSqlVendorBackend() :
83 : : GncSqlObjectBackend(TABLE_VERSION, GNC_ID_VENDOR,
84 : 50 : TABLE_NAME, col_table) {}
85 : :
86 : : static GncVendor*
87 : 1 : load_single_vendor (GncSqlBackend* sql_be, GncSqlRow& row)
88 : : {
89 : : const GncGUID* guid;
90 : : GncVendor* pVendor;
91 : :
92 : 1 : g_return_val_if_fail (sql_be != NULL, NULL);
93 : :
94 : 1 : guid = gnc_sql_load_guid (sql_be, row);
95 : 1 : pVendor = gncVendorLookup (sql_be->book(), guid);
96 : 1 : if (pVendor == NULL)
97 : : {
98 : 0 : pVendor = gncVendorCreate (sql_be->book());
99 : : }
100 : 1 : gnc_sql_load_object (sql_be, row, GNC_ID_VENDOR, pVendor, col_table);
101 : 1 : qof_instance_mark_clean (QOF_INSTANCE (pVendor));
102 : :
103 : 1 : return pVendor;
104 : : }
105 : :
106 : : /* Because gncVendorLookup has the arguments backwards: */
107 : : static inline GncVendor*
108 : 0 : gnc_vendor_lookup (const GncGUID *guid, const QofBook *book)
109 : : {
110 : 0 : QOF_BOOK_RETURN_ENTITY(book, guid, GNC_ID_VENDOR, GncVendor);
111 : : }
112 : :
113 : : void
114 : 5 : GncSqlVendorBackend::load_all (GncSqlBackend* sql_be)
115 : : {
116 : 5 : g_return_if_fail (sql_be != NULL);
117 : :
118 : 5 : std::string sql("SELECT * FROM " TABLE_NAME);
119 : 5 : auto stmt = sql_be->create_statement_from_sql(sql);
120 : 5 : auto result = sql_be->execute_select_statement(stmt);
121 : :
122 : 6 : for (auto row : *result)
123 : 6 : load_single_vendor (sql_be, row);
124 : :
125 : 5 : std::string pkey(col_table[0]->name());
126 : 5 : sql = "SELECT DISTINCT ";
127 : 5 : sql += pkey + " FROM " TABLE_NAME;
128 : 5 : gnc_sql_slots_load_for_sql_subquery (sql_be, sql,
129 : : (BookLookupFn)gnc_vendor_lookup);
130 : 5 : }
131 : :
132 : : /* ================================================================= */
133 : : bool
134 : 1 : GncSqlVendorBackend::commit (GncSqlBackend* sql_be, QofInstance* inst)
135 : : {
136 : : GncVendor* v;
137 : : const GncGUID* guid;
138 : : E_DB_OPERATION op;
139 : : gboolean is_infant;
140 : 1 : gboolean is_ok = TRUE;
141 : :
142 : 1 : g_return_val_if_fail (inst != NULL, FALSE);
143 : 1 : g_return_val_if_fail (GNC_IS_VENDOR (inst), FALSE);
144 : 1 : g_return_val_if_fail (sql_be != NULL, FALSE);
145 : :
146 : 1 : v = GNC_VENDOR (inst);
147 : :
148 : 1 : is_infant = qof_instance_get_infant (inst);
149 : 1 : if (qof_instance_get_destroying (inst))
150 : : {
151 : 0 : op = OP_DB_DELETE;
152 : : }
153 : 1 : else if (sql_be->pristine() || is_infant)
154 : : {
155 : 1 : op = OP_DB_INSERT;
156 : : }
157 : : else
158 : : {
159 : 0 : op = OP_DB_UPDATE;
160 : : }
161 : 1 : if (op != OP_DB_DELETE)
162 : : {
163 : : // Ensure the commodity is in the db
164 : 1 : is_ok = sql_be->save_commodity (gncVendorGetCurrency(v));
165 : : }
166 : 1 : if (is_ok)
167 : : {
168 : 1 : is_ok = sql_be->do_db_operation(op, TABLE_NAME, GNC_ID_VENDOR, v,
169 : : col_table);
170 : : }
171 : :
172 : 1 : if (is_ok)
173 : : {
174 : : // Now, commit or delete any slots
175 : 1 : guid = qof_instance_get_guid (inst);
176 : 1 : if (!qof_instance_get_destroying (inst))
177 : : {
178 : 1 : is_ok = gnc_sql_slots_save (sql_be, guid, is_infant, inst);
179 : : }
180 : : else
181 : : {
182 : 0 : is_ok = gnc_sql_slots_delete (sql_be, guid);
183 : : }
184 : : }
185 : :
186 : 1 : return is_ok;
187 : : }
188 : :
189 : : /* ================================================================= */
190 : : static gboolean
191 : 1 : vendor_should_be_saved (GncVendor* vendor)
192 : : {
193 : : const char* id;
194 : :
195 : 1 : 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 : 1 : id = gncVendorGetID (vendor);
199 : 1 : if (id == NULL || *id == '\0')
200 : : {
201 : 0 : return FALSE;
202 : : }
203 : :
204 : 1 : return TRUE;
205 : : }
206 : :
207 : : static void
208 : 1 : write_single_vendor (QofInstance* term_p, gpointer data_p)
209 : : {
210 : 1 : auto s = reinterpret_cast<write_objects_t*>(data_p);
211 : :
212 : 1 : g_return_if_fail (term_p != NULL);
213 : 1 : g_return_if_fail (GNC_IS_VENDOR (term_p));
214 : 1 : g_return_if_fail (data_p != NULL);
215 : :
216 : 1 : if (vendor_should_be_saved (GNC_VENDOR (term_p)))
217 : : {
218 : 1 : s->commit (term_p);
219 : : }
220 : : }
221 : :
222 : : bool
223 : 5 : GncSqlVendorBackend::write (GncSqlBackend* sql_be)
224 : : {
225 : 5 : g_return_val_if_fail (sql_be != NULL, FALSE);
226 : 5 : write_objects_t data{sql_be, true, this};
227 : :
228 : 5 : qof_object_foreach (GNC_ID_VENDOR, sql_be->book(), write_single_vendor, &data);
229 : :
230 : 5 : return data.is_ok;
231 : : }
232 : :
233 : : /* ========================== END OF FILE ===================== */
|