Branch data Line data Source code
1 : : /********************************************************************
2 : : * gnc-commodity-sql.c: load and save data to SQL *
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 : : /** @file gnc-commodity-sql.c
22 : : * @brief load and save data to SQL
23 : : * @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff@rogers.com>
24 : : *
25 : : * This file implements the top-level QofBackend API for saving/
26 : : * restoring data to/from an SQL db
27 : : */
28 : : #include <guid.hpp>
29 : : #include <config.h>
30 : :
31 : : #include <glib.h>
32 : :
33 : : #include "qof.h"
34 : : #include "gnc-commodity.h"
35 : :
36 : : #include "gnc-sql-connection.hpp"
37 : : #include "gnc-sql-backend.hpp"
38 : : #include "gnc-sql-object-backend.hpp"
39 : : #include "gnc-sql-column-table-entry.hpp"
40 : : #include "gnc-commodity-sql.h"
41 : : #include "gnc-slots-sql.h"
42 : :
43 : : #if defined( S_SPLINT_S )
44 : : #include "splint-defs.h"
45 : : #endif
46 : :
47 : : [[maybe_unused]] static QofLogModule log_module = G_LOG_DOMAIN;
48 : :
49 : : static gpointer get_quote_source_name (gpointer pObject);
50 : : static void set_quote_source_name (gpointer pObject, gpointer pValue);
51 : :
52 : : #define COMMODITIES_TABLE "commodities"
53 : : #define TABLE_VERSION 1
54 : :
55 : : #define COMMODITY_MAX_NAMESPACE_LEN 2048
56 : : #define COMMODITY_MAX_MNEMONIC_LEN 2048
57 : : #define COMMODITY_MAX_FULLNAME_LEN 2048
58 : : #define COMMODITY_MAX_CUSIP_LEN 2048
59 : : #define COMMODITY_MAX_QUOTESOURCE_LEN 2048
60 : : #define COMMODITY_MAX_QUOTE_TZ_LEN 2048
61 : :
62 : : static const EntryVec col_table
63 : : {
64 : : gnc_sql_make_table_entry<CT_GUID>(
65 : : "guid", 0, COL_NNUL | COL_PKEY | COL_UNIQUE, "guid"),
66 : : gnc_sql_make_table_entry<CT_STRING>("namespace",
67 : : COMMODITY_MAX_NAMESPACE_LEN, COL_NNUL,
68 : : (QofAccessFunc)gnc_commodity_get_namespace,
69 : : (QofSetterFunc)gnc_commodity_set_namespace),
70 : : gnc_sql_make_table_entry<CT_STRING>(
71 : : "mnemonic", COMMODITY_MAX_MNEMONIC_LEN, COL_NNUL, "mnemonic"),
72 : : gnc_sql_make_table_entry<CT_STRING>(
73 : : "fullname", COMMODITY_MAX_FULLNAME_LEN, 0, "fullname"),
74 : : gnc_sql_make_table_entry<CT_STRING>(
75 : : "cusip", COMMODITY_MAX_CUSIP_LEN, 0, "cusip"),
76 : : gnc_sql_make_table_entry<CT_INT>("fraction", 0, COL_NNUL, "fraction"),
77 : : gnc_sql_make_table_entry<CT_BOOLEAN>(
78 : : "quote_flag", 0, COL_NNUL, "quote_flag"),
79 : : gnc_sql_make_table_entry<CT_STRING>("quote_source",
80 : : COMMODITY_MAX_QUOTESOURCE_LEN, 0,
81 : : (QofAccessFunc)get_quote_source_name,
82 : : set_quote_source_name),
83 : : gnc_sql_make_table_entry<CT_STRING>(
84 : : "quote_tz", COMMODITY_MAX_QUOTE_TZ_LEN, 0, "quote-tz"),
85 : : };
86 : :
87 : 10 : GncSqlCommodityBackend::GncSqlCommodityBackend() :
88 : : GncSqlObjectBackend(TABLE_VERSION, GNC_ID_COMMODITY,
89 : 50 : COMMODITIES_TABLE, col_table) {}
90 : : /* ================================================================= */
91 : :
92 : : static gpointer
93 : 53 : get_quote_source_name (gpointer pObject)
94 : : {
95 : : const gnc_commodity* pCommodity;
96 : :
97 : 53 : g_return_val_if_fail (pObject != NULL, NULL);
98 : 53 : g_return_val_if_fail (GNC_IS_COMMODITY (pObject), NULL);
99 : :
100 : 53 : pCommodity = GNC_COMMODITY (pObject);
101 : 106 : return (gpointer)gnc_quote_source_get_internal_name (
102 : 53 : gnc_commodity_get_quote_source (pCommodity));
103 : : }
104 : :
105 : : static void
106 : 8 : set_quote_source_name (gpointer pObject, gpointer pValue)
107 : : {
108 : : gnc_commodity* pCommodity;
109 : 8 : const gchar* quote_source_name = (const gchar*)pValue;
110 : : gnc_quote_source* quote_source;
111 : :
112 : 8 : g_return_if_fail (pObject != NULL);
113 : 8 : g_return_if_fail (GNC_IS_COMMODITY (pObject));
114 : :
115 : 8 : if (pValue == NULL) return;
116 : :
117 : 8 : pCommodity = GNC_COMMODITY (pObject);
118 : 8 : quote_source = gnc_quote_source_lookup_by_internal (quote_source_name);
119 : 8 : gnc_commodity_set_quote_source (pCommodity, quote_source);
120 : : }
121 : :
122 : : static gnc_commodity*
123 : 8 : load_single_commodity (GncSqlBackend* sql_be, GncSqlRow& row)
124 : : {
125 : 8 : QofBook* pBook = sql_be->book();
126 : : gnc_commodity* pCommodity;
127 : :
128 : 8 : pCommodity = gnc_commodity_new (pBook, NULL, NULL, NULL, NULL, 100);
129 : 8 : gnc_commodity_begin_edit (pCommodity);
130 : 8 : gnc_sql_load_object (sql_be, row, GNC_ID_COMMODITY, pCommodity, col_table);
131 : 8 : gnc_commodity_commit_edit (pCommodity);
132 : :
133 : 8 : return pCommodity;
134 : : }
135 : :
136 : : void
137 : 5 : GncSqlCommodityBackend::load_all (GncSqlBackend* sql_be)
138 : : {
139 : : gnc_commodity_table* pTable;
140 : :
141 : 5 : pTable = gnc_commodity_table_get_table (sql_be->book());
142 : 5 : std::string sql("SELECT * FROM " COMMODITIES_TABLE);
143 : 5 : auto stmt = sql_be->create_statement_from_sql(sql);
144 : 5 : auto result = sql_be->execute_select_statement(stmt);
145 : :
146 : 13 : for (auto row : *result)
147 : : {
148 : 8 : auto pCommodity = load_single_commodity (sql_be, row);
149 : :
150 : 8 : if (pCommodity != NULL)
151 : : {
152 : : GncGUID guid;
153 : :
154 : 8 : guid = *qof_instance_get_guid (QOF_INSTANCE (pCommodity));
155 : 8 : pCommodity = gnc_commodity_table_insert (pTable, pCommodity);
156 : 8 : if (qof_instance_is_dirty (QOF_INSTANCE (pCommodity)))
157 : 0 : sql_be->commodity_for_postload_processing(pCommodity);
158 : 8 : qof_instance_set_guid (QOF_INSTANCE (pCommodity), &guid);
159 : : }
160 : :
161 : 13 : }
162 : 5 : std::string pkey(col_table[0]->name());
163 : 5 : sql = "SELECT DISTINCT ";
164 : 5 : sql += pkey + " FROM " COMMODITIES_TABLE;
165 : 5 : gnc_sql_slots_load_for_sql_subquery (sql_be, sql,
166 : : (BookLookupFn)gnc_commodity_find_commodity_by_guid);
167 : 5 : }
168 : : /* ================================================================= */
169 : : static gboolean
170 : 8 : do_commit_commodity (GncSqlBackend* sql_be, QofInstance* inst,
171 : : gboolean force_insert)
172 : : {
173 : : const GncGUID* guid;
174 : : gboolean is_infant;
175 : : E_DB_OPERATION op;
176 : : gboolean is_ok;
177 : :
178 : 8 : is_infant = qof_instance_get_infant (inst);
179 : 8 : if (qof_instance_get_destroying (inst))
180 : : {
181 : 0 : op = OP_DB_DELETE;
182 : : }
183 : 8 : else if (sql_be->pristine() || is_infant || force_insert)
184 : : {
185 : 8 : op = OP_DB_INSERT;
186 : : }
187 : : else
188 : : {
189 : 0 : op = OP_DB_UPDATE;
190 : : }
191 : 8 : is_ok = sql_be->do_db_operation(op, COMMODITIES_TABLE, GNC_ID_COMMODITY,
192 : : inst, col_table);
193 : :
194 : 8 : if (is_ok)
195 : : {
196 : : // Now, commit any slots
197 : 8 : guid = qof_instance_get_guid (inst);
198 : 8 : if (!qof_instance_get_destroying (inst))
199 : : {
200 : 8 : is_ok = gnc_sql_slots_save (sql_be, guid, is_infant, inst);
201 : : }
202 : : else
203 : : {
204 : 0 : is_ok = gnc_sql_slots_delete (sql_be, guid);
205 : : }
206 : : }
207 : :
208 : 8 : return is_ok;
209 : : }
210 : :
211 : : bool
212 : 8 : GncSqlCommodityBackend::commit (GncSqlBackend* sql_be, QofInstance* inst)
213 : : {
214 : 8 : g_return_val_if_fail (sql_be != NULL, FALSE);
215 : 8 : g_return_val_if_fail (inst != NULL, FALSE);
216 : 8 : g_return_val_if_fail (GNC_IS_COMMODITY (inst), FALSE);
217 : 8 : auto in_be = instance_in_db(sql_be, inst);
218 : 8 : return do_commit_commodity (sql_be, inst, !in_be);
219 : : }
220 : :
221 : : /* ----------------------------------------------------------------- */
222 : : template<> void
223 : 47 : GncSqlColumnTableEntryImpl<CT_COMMODITYREF>::load (const GncSqlBackend* sql_be,
224 : : GncSqlRow& row,
225 : : QofIdTypeConst obj_name,
226 : : gpointer pObject) const noexcept
227 : : {
228 : 47 : load_from_guid_ref(row, obj_name, pObject,
229 : 38 : [sql_be](GncGUID* g){
230 : 38 : return gnc_commodity_find_commodity_by_guid(g, sql_be->book());
231 : : });
232 : 47 : }
233 : :
234 : : template<> void
235 : 40 : GncSqlColumnTableEntryImpl<CT_COMMODITYREF>::add_to_table(ColVec& vec) const noexcept
236 : : {
237 : 40 : add_objectref_guid_to_table(vec);
238 : 40 : }
239 : :
240 : : template<> void
241 : 49 : GncSqlColumnTableEntryImpl<CT_COMMODITYREF>::add_to_query(QofIdTypeConst obj_name,
242 : : const gpointer pObject,
243 : : PairVec& vec) const noexcept
244 : : {
245 : 49 : add_objectref_guid_to_query(obj_name, pObject, vec);
246 : 49 : }
247 : :
248 : : /* ========================== END OF FILE ===================== */
|