Line data Source code
1 : /********************************************************************
2 : * gnc-schedxaction-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-schedxaction-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 <glib.h>
29 :
30 : #include <config.h>
31 :
32 : #include "qof.h"
33 : #include "SchedXaction.h"
34 : #include "SX-book.h"
35 : #include "Recurrence.h"
36 :
37 : #ifdef S_SPLINT_S
38 : #include "splint-defs.h"
39 : #endif
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-schedxaction-sql.h"
46 : #include "gnc-slots-sql.h"
47 : #include "gnc-recurrence-sql.h"
48 : #include "gnc-transaction-sql.h"
49 :
50 :
51 : #define SCHEDXACTION_TABLE "schedxactions"
52 : #define TABLE_VERSION 1
53 :
54 : G_GNUC_UNUSED static QofLogModule log_module = G_LOG_DOMAIN;
55 :
56 : #define SX_MAX_NAME_LEN 2048
57 :
58 : static const EntryVec col_table
59 : ({
60 : gnc_sql_make_table_entry<CT_GUID>("guid", 0, COL_NNUL | COL_PKEY, "guid"),
61 : gnc_sql_make_table_entry<CT_STRING>("name", SX_MAX_NAME_LEN, 0, "name"),
62 : gnc_sql_make_table_entry<CT_BOOLEAN>("enabled", 0, COL_NNUL, "enabled"),
63 : gnc_sql_make_table_entry<CT_GDATE>("start_date", 0, 0, "start-date"),
64 : gnc_sql_make_table_entry<CT_GDATE>("end_date", 0, 0, "end-date"),
65 : gnc_sql_make_table_entry<CT_GDATE>(
66 : "last_occur", 0, 0, "last-occurance-date"),
67 : gnc_sql_make_table_entry<CT_INT>(
68 : "num_occur", 0, COL_NNUL, "num-occurance"),
69 : gnc_sql_make_table_entry<CT_INT>("rem_occur", 0, COL_NNUL, "rem-occurance"),
70 : gnc_sql_make_table_entry<CT_BOOLEAN>(
71 : "auto_create", 0, COL_NNUL, "auto-create"),
72 : gnc_sql_make_table_entry<CT_BOOLEAN>(
73 : "auto_notify", 0, COL_NNUL, "auto-create-notify"),
74 : gnc_sql_make_table_entry<CT_INT>(
75 : "adv_creation", 0, COL_NNUL, "advance-creation-days"),
76 : gnc_sql_make_table_entry<CT_INT>(
77 : "adv_notify", 0, COL_NNUL, "advance-reminder-days"),
78 : gnc_sql_make_table_entry<CT_INT>(
79 : "instance_count", 0, COL_NNUL, "instance-count"),
80 : gnc_sql_make_table_entry<CT_ACCOUNTREF>(
81 : "template_act_guid", 0, COL_NNUL, "template-account"),
82 : });
83 :
84 15 : GncSqlSchedXactionBackend::GncSqlSchedXactionBackend() :
85 30 : GncSqlObjectBackend(TABLE_VERSION, GNC_ID_SCHEDXACTION,
86 75 : SCHEDXACTION_TABLE, col_table) {}
87 :
88 : /* ================================================================= */
89 : static SchedXaction*
90 3 : load_single_sx (GncSqlBackend* sql_be, GncSqlRow& row)
91 : {
92 : const GncGUID* guid;
93 : SchedXaction* pSx;
94 : GList* schedule;
95 : GDate start_date;
96 :
97 3 : g_return_val_if_fail (sql_be != NULL, NULL);
98 :
99 3 : guid = gnc_sql_load_guid (sql_be, row);
100 3 : g_assert (guid != NULL);
101 3 : pSx = xaccSchedXactionMalloc (sql_be->book());
102 :
103 3 : gnc_sx_begin_edit (pSx);
104 3 : gnc_sql_load_object (sql_be, row, GNC_SX_ID, pSx, col_table);
105 3 : schedule = gnc_sql_recurrence_load_list (sql_be, guid);
106 3 : gnc_sx_set_schedule (pSx, schedule);
107 3 : gnc_sx_commit_edit (pSx);
108 3 : gnc_sql_transaction_load_tx_for_account (sql_be, pSx->template_acct);
109 :
110 3 : g_object_get (pSx, "start-date", &start_date, NULL);
111 :
112 3 : return pSx;
113 : }
114 :
115 : /* Because SchedXaction doesn't define a lookup function: */
116 : static inline SchedXaction*
117 0 : gnc_sx_lookup (const GncGUID *guid, const QofBook *book)
118 : {
119 0 : QOF_BOOK_RETURN_ENTITY(book, guid, GNC_ID_SCHEDXACTION, SchedXaction);
120 : }
121 :
122 : void
123 7 : GncSqlSchedXactionBackend::load_all (GncSqlBackend* sql_be)
124 : {
125 7 : g_return_if_fail (sql_be != NULL);
126 :
127 7 : std::string sql("SELECT * FROM " SCHEDXACTION_TABLE);
128 7 : auto stmt = sql_be->create_statement_from_sql(sql);
129 7 : if (stmt == nullptr) return;
130 7 : auto result = sql_be->execute_select_statement(stmt);
131 : SchedXactions* sxes;
132 7 : sxes = gnc_book_get_schedxactions (sql_be->book());
133 :
134 10 : for (auto row : *result)
135 : {
136 : SchedXaction* sx;
137 :
138 3 : sx = load_single_sx (sql_be, row);
139 3 : if (sx != nullptr)
140 3 : gnc_sxes_add_sx (sxes, sx);
141 10 : }
142 7 : std::string pkey(col_table[0]->name());
143 7 : sql = "SELECT DISTINCT ";
144 7 : sql += pkey + " FROM " SCHEDXACTION_TABLE;
145 7 : gnc_sql_slots_load_for_sql_subquery (sql_be, sql,
146 : (BookLookupFn)gnc_sx_lookup);
147 7 : }
148 :
149 :
150 : /* ================================================================= */
151 : bool
152 3 : GncSqlSchedXactionBackend::commit (GncSqlBackend* sql_be, QofInstance* inst)
153 : {
154 : SchedXaction* pSx;
155 : const GncGUID* guid;
156 : E_DB_OPERATION op;
157 : gboolean is_infant;
158 : gboolean is_ok;
159 :
160 3 : g_return_val_if_fail (sql_be != NULL, FALSE);
161 3 : g_return_val_if_fail (inst != NULL, FALSE);
162 3 : g_return_val_if_fail (GNC_IS_SX (inst), FALSE);
163 :
164 3 : pSx = GNC_SX (inst);
165 :
166 3 : is_infant = qof_instance_get_infant (inst);
167 3 : if (qof_instance_get_destroying (inst))
168 : {
169 0 : op = OP_DB_DELETE;
170 : }
171 3 : else if (sql_be->pristine() || is_infant)
172 : {
173 3 : op = OP_DB_INSERT;
174 : }
175 : else
176 : {
177 0 : op = OP_DB_UPDATE;
178 : }
179 3 : is_ok = sql_be->do_db_operation(op, SCHEDXACTION_TABLE, GNC_SX_ID, pSx,
180 : col_table);
181 3 : guid = qof_instance_get_guid (inst);
182 3 : if (op == OP_DB_INSERT || op == OP_DB_UPDATE)
183 : {
184 3 : gnc_sql_recurrence_save_list (sql_be, guid, gnc_sx_get_schedule (pSx));
185 : }
186 : else
187 : {
188 0 : gnc_sql_recurrence_delete (sql_be, guid);
189 : }
190 :
191 3 : if (is_ok)
192 : {
193 : // Now, commit any slots
194 3 : if (op == OP_DB_INSERT || op == OP_DB_UPDATE)
195 : {
196 3 : is_ok = gnc_sql_slots_save (sql_be, guid, is_infant, inst);
197 : }
198 : else
199 : {
200 0 : is_ok = gnc_sql_slots_delete (sql_be, guid);
201 : }
202 : }
203 :
204 3 : return is_ok;
205 : }
206 :
207 : /* ========================== END OF FILE ===================== */
|