GnuCash  5.6-150-g038405b370+
gnc-commodity-sql.cpp
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 \********************************************************************/
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,
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 GncSqlCommodityBackend::GncSqlCommodityBackend() :
88  GncSqlObjectBackend(TABLE_VERSION, GNC_ID_COMMODITY,
89  COMMODITIES_TABLE, col_table) {}
90 /* ================================================================= */
91 
92 static gpointer
93 get_quote_source_name (gpointer pObject)
94 {
95  const gnc_commodity* pCommodity;
96 
97  g_return_val_if_fail (pObject != NULL, NULL);
98  g_return_val_if_fail (GNC_IS_COMMODITY (pObject), NULL);
99 
100  pCommodity = GNC_COMMODITY (pObject);
101  return (gpointer)gnc_quote_source_get_internal_name (
102  gnc_commodity_get_quote_source (pCommodity));
103 }
104 
105 static void
106 set_quote_source_name (gpointer pObject, gpointer pValue)
107 {
108  gnc_commodity* pCommodity;
109  const gchar* quote_source_name = (const gchar*)pValue;
110  gnc_quote_source* quote_source;
111 
112  g_return_if_fail (pObject != NULL);
113  g_return_if_fail (GNC_IS_COMMODITY (pObject));
114 
115  if (pValue == NULL) return;
116 
117  pCommodity = GNC_COMMODITY (pObject);
118  quote_source = gnc_quote_source_lookup_by_internal (quote_source_name);
119  if (!quote_source)
120  quote_source = gnc_quote_source_add_new (quote_source_name, FALSE);
121  gnc_commodity_set_quote_source (pCommodity, quote_source);
122 }
123 
124 static gnc_commodity*
125 load_single_commodity (GncSqlBackend* sql_be, GncSqlRow& row)
126 {
127  QofBook* pBook = sql_be->book();
128  gnc_commodity* pCommodity;
129 
130  pCommodity = gnc_commodity_new (pBook, NULL, NULL, NULL, NULL, 100);
131  gnc_commodity_begin_edit (pCommodity);
132  gnc_sql_load_object (sql_be, row, GNC_ID_COMMODITY, pCommodity, col_table);
133  gnc_commodity_commit_edit (pCommodity);
134 
135  return pCommodity;
136 }
137 
138 void
140 {
141  gnc_commodity_table* pTable;
142 
143  pTable = gnc_commodity_table_get_table (sql_be->book());
144  std::string sql("SELECT * FROM " COMMODITIES_TABLE);
145  auto stmt = sql_be->create_statement_from_sql(sql);
146  auto result = sql_be->execute_select_statement(stmt);
147 
148  for (auto row : *result)
149  {
150  auto pCommodity = load_single_commodity (sql_be, row);
151 
152  if (pCommodity != NULL)
153  {
154  GncGUID guid;
155 
156  guid = *qof_instance_get_guid (QOF_INSTANCE (pCommodity));
157  pCommodity = gnc_commodity_table_insert (pTable, pCommodity);
158  if (qof_instance_is_dirty (QOF_INSTANCE (pCommodity)))
159  sql_be->commodity_for_postload_processing(pCommodity);
160  qof_instance_set_guid (QOF_INSTANCE (pCommodity), &guid);
161  }
162 
163  }
164  std::string pkey(col_table[0]->name());
165  sql = "SELECT DISTINCT ";
166  sql += pkey + " FROM " COMMODITIES_TABLE;
168  (BookLookupFn)gnc_commodity_find_commodity_by_guid);
169 }
170 /* ================================================================= */
171 static gboolean
172 do_commit_commodity (GncSqlBackend* sql_be, QofInstance* inst,
173  gboolean force_insert)
174 {
175  const GncGUID* guid;
176  gboolean is_infant;
177  E_DB_OPERATION op;
178  gboolean is_ok;
179 
180  is_infant = qof_instance_get_infant (inst);
181  if (qof_instance_get_destroying (inst))
182  {
183  op = OP_DB_DELETE;
184  }
185  else if (sql_be->pristine() || is_infant || force_insert)
186  {
187  op = OP_DB_INSERT;
188  }
189  else
190  {
191  op = OP_DB_UPDATE;
192  }
193  is_ok = sql_be->do_db_operation(op, COMMODITIES_TABLE, GNC_ID_COMMODITY,
194  inst, col_table);
195 
196  if (is_ok)
197  {
198  // Now, commit any slots
199  guid = qof_instance_get_guid (inst);
200  if (!qof_instance_get_destroying (inst))
201  {
202  is_ok = gnc_sql_slots_save (sql_be, guid, is_infant, inst);
203  }
204  else
205  {
206  is_ok = gnc_sql_slots_delete (sql_be, guid);
207  }
208  }
209 
210  return is_ok;
211 }
212 
213 bool
215 {
216  g_return_val_if_fail (sql_be != NULL, FALSE);
217  g_return_val_if_fail (inst != NULL, FALSE);
218  g_return_val_if_fail (GNC_IS_COMMODITY (inst), FALSE);
219  auto in_be = instance_in_db(sql_be, inst);
220  return do_commit_commodity (sql_be, inst, !in_be);
221 }
222 
223 /* ----------------------------------------------------------------- */
224 template<> void
226  GncSqlRow& row,
227  QofIdTypeConst obj_name,
228  gpointer pObject) const noexcept
229 {
230  load_from_guid_ref(row, obj_name, pObject,
231  [sql_be](GncGUID* g){
232  return gnc_commodity_find_commodity_by_guid(g, sql_be->book());
233  });
234 }
235 
236 template<> void
238 {
239  add_objectref_guid_to_table(vec);
240 }
241 
242 template<> void
244  const gpointer pObject,
245  PairVec& vec) const noexcept
246 {
247  add_objectref_guid_to_query(obj_name, pObject, vec);
248 }
249 
250 /* ========================== END OF FILE ===================== */
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.
gnc_commodity * gnc_commodity_table_insert(gnc_commodity_table *table, gnc_commodity *comm)
Add a new commodity to the commodity table.
gnc_commodity_table * gnc_commodity_table_get_table(QofBook *book)
Returns the commodity table associated with a book.
GncSqlResultPtr execute_select_statement(const GncSqlStatementPtr &stmt) const noexcept
Executes an SQL SELECT statement and returns the result rows.
const GncGUID * qof_instance_get_guid(gconstpointer inst)
Return the GncGUID of this instance.
#define qof_instance_is_dirty
Return value of is_dirty flag.
Definition: qofinstance.h:166
void gnc_sql_slots_load_for_sql_subquery(GncSqlBackend *sql_be, const std::string subquery, BookLookupFn lookup_fn)
gnc_sql_slots_load_for_sql_subquery - Loads slots for all objects whose guid is supplied by a subquer...
gnc_quote_source * gnc_quote_source_add_new(const char *source_name, gboolean supported)
Create a new quote source.
#define G_LOG_DOMAIN
Functions providing the SX List as a plugin page.
load and save data to SQL
const gchar * QofIdTypeConst
QofIdTypeConst declaration.
Definition: qofid.h:82
load and save accounts data to SQL
gboolean qof_instance_get_destroying(gconstpointer ptr)
Retrieve the flag that indicates whether or not this object is about to be destroyed.
gboolean gnc_sql_slots_save(GncSqlBackend *sql_be, const GncGUID *guid, gboolean is_infant, QofInstance *inst)
gnc_sql_slots_save - Saves slots for an object to the db.
void add_to_query(QofIdTypeConst obj_name, void *pObject, PairVec &vec) const noexcept override
Add a pair of the table column heading and object&#39;s value&#39;s string representation to a PairVec; used ...
void commodity_for_postload_processing(gnc_commodity *)
Register a commodity to be committed after loading is complete.
const char * gnc_commodity_get_namespace(const gnc_commodity *cm)
Retrieve the namespace for the specified commodity.
void(* QofSetterFunc)(gpointer, gpointer)
The QofSetterFunc defines an function pointer for parameter setters.
Definition: qofclass.h:131
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.
void gnc_commodity_set_quote_source(gnc_commodity *cm, gnc_quote_source *src)
Set the automatic price quote source for the specified commodity.
Row of SQL Query results.
gnc_commodity * gnc_commodity_new(QofBook *book, const char *fullname, const char *name_space, const char *mnemonic, const char *cusip, int fraction)
Create a new commodity.
gpointer(* QofAccessFunc)(gpointer object, const QofParam *param)
The QofAccessFunc defines an arbitrary function pointer for access functions.
Definition: qofclass.h:124
Encapsulates per-class table schema with functions to load, create a table, commit a changed front-en...
void load_all(GncSqlBackend *) override
Load all objects of m_type in the database into memory.
gnc_quote_source * gnc_quote_source_lookup_by_internal(const char *name)
Given the internal (gnucash or F::Q) name of a quote source, find the data structure identified by th...
QofBook reference.
Definition: qofbook-p.hpp:46
gnc_quote_source * gnc_commodity_get_quote_source(const gnc_commodity *cm)
Retrieve the automatic price quote source for the specified commodity.
bool commit(GncSqlBackend *, QofInstance *) override
UPDATE/INSERT a single instance of m_type_name into the database.
gboolean gnc_sql_slots_delete(GncSqlBackend *sql_be, const GncGUID *guid)
gnc_sql_slots_delete - Deletes slots for an object from the db.
void add_to_table(ColVec &vec) const noexcept override
Add a GncSqlColumnInfo structure for the column type to a ColVec.
void gnc_commodity_set_namespace(gnc_commodity *cm, const char *name_space)
Set the namespace for the specified commodity.
const char * gnc_quote_source_get_internal_name(const gnc_quote_source *source)
Given a gnc_quote_source data structure, return the internal name of this quote source.
The type used to store guids in C.
Definition: guid.h:75
Commodity handling public routines.
Main SQL backend structure.
bool instance_in_db(const GncSqlBackend *sql_be, QofInstance *inst) const noexcept
Check the presence of an object in the backend&#39;s database.