GnuCash  5.6-150-g038405b370+
gnc-order-sql.cpp
1 /********************************************************************\
2  * gnc-order-sql.c -- order 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 
31 #include <guid.hpp>
32 #include <config.h>
33 
34 #include <glib.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include "gncOrderP.h"
38 
39 #include "gnc-sql-connection.hpp"
40 #include "gnc-sql-backend.hpp"
41 #include "gnc-sql-object-backend.hpp"
42 #include "gnc-sql-column-table-entry.hpp"
43 #include "gnc-slots-sql.h"
44 #include "gnc-order-sql.h"
45 
46 #define _GNC_MOD_NAME GNC_ID_ORDER
47 
48 [[maybe_unused]] static QofLogModule log_module = G_LOG_DOMAIN;
49 
50 #define TABLE_NAME "orders"
51 #define TABLE_VERSION 1
52 
53 #define MAX_ID_LEN 2048
54 #define MAX_NOTES_LEN 2048
55 #define MAX_REFERENCE_LEN 2048
56 
57 static EntryVec col_table
58 ({
59  gnc_sql_make_table_entry<CT_GUID>("guid", 0, COL_NNUL | COL_PKEY, "guid"),
60  gnc_sql_make_table_entry<CT_STRING>("id", MAX_ID_LEN, COL_NNUL, "id"),
61  gnc_sql_make_table_entry<CT_STRING>("notes", MAX_NOTES_LEN, COL_NNUL,
62  "notes"),
63  gnc_sql_make_table_entry<CT_STRING>(
64  "reference", MAX_REFERENCE_LEN, COL_NNUL, "reference"),
65  gnc_sql_make_table_entry<CT_BOOLEAN>("active", 0, COL_NNUL, "order"),
66  gnc_sql_make_table_entry<CT_TIME>("date_opened", 0, COL_NNUL,
67  "date-opened"),
68  gnc_sql_make_table_entry<CT_TIME>("date_closed", 0, COL_NNUL,
69  "date-closed"),
70  gnc_sql_make_table_entry<CT_OWNERREF>("owner", 0, COL_NNUL,
71  ORDER_OWNER, true),
72 });
73 
74 GncSqlOrderBackend::GncSqlOrderBackend() :
75  GncSqlObjectBackend(TABLE_VERSION, GNC_ID_ORDER,
76  TABLE_NAME, col_table) {}
77 
78 static GncOrder*
79 load_single_order (GncSqlBackend* sql_be, GncSqlRow& row)
80 {
81  const GncGUID* guid;
82  GncOrder* pOrder;
83 
84  g_return_val_if_fail (sql_be != NULL, NULL);
85 
86  guid = gnc_sql_load_guid (sql_be, row);
87  pOrder = gncOrderLookup (sql_be->book(), guid);
88  if (pOrder == NULL)
89  {
90  pOrder = gncOrderCreate (sql_be->book());
91  }
92  gnc_sql_load_object (sql_be, row, GNC_ID_ORDER, pOrder, col_table);
93  qof_instance_mark_clean (QOF_INSTANCE (pOrder));
94 
95  return pOrder;
96 }
97 
98 /* Because gncOrderLookup has the arguments backwards: */
99 static inline GncOrder*
100 gnc_order_lookup (const GncGUID *guid, const QofBook *book)
101 {
102  QOF_BOOK_RETURN_ENTITY(book, guid, GNC_ID_ORDER, GncOrder);
103 }
104 
105 void
107 {
108  g_return_if_fail (sql_be != NULL);
109 
110  std::string sql("SELECT * FROM " TABLE_NAME);
111  auto stmt = sql_be->create_statement_from_sql(sql);
112  auto result = sql_be->execute_select_statement(stmt);
113 
114  for (auto row : *result)
115  load_single_order (sql_be, row);
116 
117  std::string pkey(col_table[0]->name());
118  sql = "SELECT DISTINCT ";
119  sql += pkey + " FROM " TABLE_NAME;
121  (BookLookupFn)gnc_order_lookup);
122 }
123 
124 /* ================================================================= */
125 static gboolean
126 order_should_be_saved (GncOrder* order)
127 {
128  const char* id;
129 
130  g_return_val_if_fail (order != NULL, FALSE);
131 
132  /* make sure this is a valid order before we save it -- should have an ID */
133  id = gncOrderGetID (order);
134  if (id == NULL || *id == '\0')
135  {
136  return FALSE;
137  }
138 
139  return TRUE;
140 }
141 
142 static void
143 write_single_order (QofInstance* term_p, gpointer data_p)
144 {
145  auto s = reinterpret_cast<write_objects_t*>(data_p);
146 
147  g_return_if_fail (term_p != NULL);
148  g_return_if_fail (GNC_IS_ORDER (term_p));
149  g_return_if_fail (data_p != NULL);
150 
151  if (s->is_ok && order_should_be_saved (GNC_ORDER (term_p)))
152  {
153  s->commit (term_p);
154  }
155 }
156 
157 bool
159 {
160  g_return_val_if_fail (sql_be != NULL, FALSE);
161  write_objects_t data{sql_be, true, this};
162 
163  qof_object_foreach (GNC_ID_ORDER, sql_be->book(), write_single_order, &data);
164 
165  return data.is_ok;
166 }
167 
168 /* ================================================================= */
169 template<> void
171  GncSqlRow& row,
172  QofIdTypeConst obj_name,
173  gpointer pObject) const noexcept
174 {
175  load_from_guid_ref(row, obj_name, pObject,
176  [sql_be](GncGUID* g){
177  return gncOrderLookup(sql_be->book(), g);
178  });
179 }
180 
181 template<> void
183 {
184  add_objectref_guid_to_table(vec);
185 }
186 
187 template<> void
189  const gpointer pObject,
190  PairVec& vec) const noexcept
191 {
192  add_objectref_guid_to_query(obj_name, pObject, vec);
193 }
194 
195 /* ========================== END OF FILE ===================== */
bool write(GncSqlBackend *) override
Write all objects of m_type_name to the database.
void load_all(GncSqlBackend *) override
Load all objects of m_type in the database into memory.
GncSqlResultPtr execute_select_statement(const GncSqlStatementPtr &stmt) const noexcept
Executes an SQL SELECT statement and returns the result rows.
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...
#define G_LOG_DOMAIN
Functions providing the SX List as a plugin page.
const gchar * QofIdTypeConst
QofIdTypeConst declaration.
Definition: qofid.h:82
load and save accounts data to SQL
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 ...
#define QOF_BOOK_RETURN_ENTITY(book, guid, e_type, c_type)
Encapsulates all the information about a dataset manipulated by QOF.
Definition: qofbook.h:186
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.
load and save order data to SQL
Row of SQL Query results.
void qof_object_foreach(QofIdTypeConst type_name, QofBook *book, QofInstanceForeachCB cb, gpointer user_data)
Invoke the callback &#39;cb&#39; on every instance ov a particular object type.
Definition: qofobject.cpp:185
Encapsulates per-class table schema with functions to load, create a table, commit a changed front-en...
Data-passing struct for callbacks to qof_object_foreach() used in GncSqlObjectBackend::write().
void add_to_table(ColVec &vec) const noexcept override
Add a GncSqlColumnInfo structure for the column type to a ColVec.
The type used to store guids in C.
Definition: guid.h:75
Main SQL backend structure.