Line data Source code
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 :
23 : /** @file gnc-order-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 :
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 15 : GncSqlOrderBackend::GncSqlOrderBackend() :
75 30 : GncSqlObjectBackend(TABLE_VERSION, GNC_ID_ORDER,
76 75 : TABLE_NAME, col_table) {}
77 :
78 : static GncOrder*
79 0 : load_single_order (GncSqlBackend* sql_be, GncSqlRow& row)
80 : {
81 : const GncGUID* guid;
82 : GncOrder* pOrder;
83 :
84 0 : g_return_val_if_fail (sql_be != NULL, NULL);
85 :
86 0 : guid = gnc_sql_load_guid (sql_be, row);
87 0 : pOrder = gncOrderLookup (sql_be->book(), guid);
88 0 : if (pOrder == NULL)
89 : {
90 0 : pOrder = gncOrderCreate (sql_be->book());
91 : }
92 0 : gnc_sql_load_object (sql_be, row, GNC_ID_ORDER, pOrder, col_table);
93 0 : qof_instance_mark_clean (QOF_INSTANCE (pOrder));
94 :
95 0 : return pOrder;
96 : }
97 :
98 : /* Because gncOrderLookup has the arguments backwards: */
99 : static inline GncOrder*
100 0 : gnc_order_lookup (const GncGUID *guid, const QofBook *book)
101 : {
102 0 : QOF_BOOK_RETURN_ENTITY(book, guid, GNC_ID_ORDER, GncOrder);
103 : }
104 :
105 : void
106 7 : GncSqlOrderBackend::load_all (GncSqlBackend* sql_be)
107 : {
108 7 : g_return_if_fail (sql_be != NULL);
109 :
110 7 : std::string sql("SELECT * FROM " TABLE_NAME);
111 7 : auto stmt = sql_be->create_statement_from_sql(sql);
112 7 : auto result = sql_be->execute_select_statement(stmt);
113 :
114 7 : for (auto row : *result)
115 7 : load_single_order (sql_be, row);
116 :
117 7 : std::string pkey(col_table[0]->name());
118 7 : sql = "SELECT DISTINCT ";
119 7 : sql += pkey + " FROM " TABLE_NAME;
120 7 : gnc_sql_slots_load_for_sql_subquery (sql_be, sql,
121 : (BookLookupFn)gnc_order_lookup);
122 7 : }
123 :
124 : /* ================================================================= */
125 : static gboolean
126 0 : order_should_be_saved (GncOrder* order)
127 : {
128 : const char* id;
129 :
130 0 : 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 0 : id = gncOrderGetID (order);
134 0 : if (id == NULL || *id == '\0')
135 : {
136 0 : return FALSE;
137 : }
138 :
139 0 : return TRUE;
140 : }
141 :
142 : static void
143 0 : write_single_order (QofInstance* term_p, gpointer data_p)
144 : {
145 0 : auto s = reinterpret_cast<write_objects_t*>(data_p);
146 :
147 0 : g_return_if_fail (term_p != NULL);
148 0 : g_return_if_fail (GNC_IS_ORDER (term_p));
149 0 : g_return_if_fail (data_p != NULL);
150 :
151 0 : if (s->is_ok && order_should_be_saved (GNC_ORDER (term_p)))
152 : {
153 0 : s->commit (term_p);
154 : }
155 : }
156 :
157 : bool
158 6 : GncSqlOrderBackend::write (GncSqlBackend* sql_be)
159 : {
160 6 : g_return_val_if_fail (sql_be != NULL, FALSE);
161 6 : write_objects_t data{sql_be, true, this};
162 :
163 6 : qof_object_foreach (GNC_ID_ORDER, sql_be->book(), write_single_order, &data);
164 :
165 6 : return data.is_ok;
166 : }
167 :
168 : /* ================================================================= */
169 : template<> void
170 1 : GncSqlColumnTableEntryImpl<CT_ORDERREF>::load (const GncSqlBackend* sql_be,
171 : GncSqlRow& row,
172 : QofIdTypeConst obj_name,
173 : gpointer pObject) const noexcept
174 : {
175 1 : load_from_guid_ref(row, obj_name, pObject,
176 0 : [sql_be](GncGUID* g){
177 0 : return gncOrderLookup(sql_be->book(), g);
178 : });
179 1 : }
180 :
181 : template<> void
182 6 : GncSqlColumnTableEntryImpl<CT_ORDERREF>::add_to_table(ColVec& vec) const noexcept
183 : {
184 6 : add_objectref_guid_to_table(vec);
185 6 : }
186 :
187 : template<> void
188 1 : GncSqlColumnTableEntryImpl<CT_ORDERREF>::add_to_query(QofIdTypeConst obj_name,
189 : const gpointer pObject,
190 : PairVec& vec) const noexcept
191 : {
192 1 : add_objectref_guid_to_query(obj_name, pObject, vec);
193 1 : }
194 :
195 : /* ========================== END OF FILE ===================== */
|