Line data Source code
1 : /********************************************************************
2 : * gnc-lots-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-lots-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 :
29 : #include <guid.hpp>
30 : #include <config.h>
31 :
32 : #include <glib.h>
33 :
34 : #include "qof.h"
35 : #include "Account.h"
36 : #include "gnc-lot.h"
37 :
38 : #if defined( S_SPLINT_S )
39 : #include "splint-defs.h"
40 : #endif
41 :
42 : #include "gnc-sql-connection.hpp"
43 : #include "gnc-sql-backend.hpp"
44 : #include "gnc-sql-object-backend.hpp"
45 : #include "gnc-sql-column-table-entry.hpp"
46 : #include "gnc-slots-sql.h"
47 :
48 : #include "gnc-lots-sql.h"
49 :
50 : static QofLogModule log_module = G_LOG_DOMAIN;
51 :
52 : #define TABLE_NAME "lots"
53 : #define TABLE_VERSION 2
54 :
55 : static gpointer get_lot_account (gpointer pObject);
56 : static void set_lot_account (gpointer pObject, gpointer pValue);
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_ACCOUNTREF>("account_guid", 0, 0,
62 : (QofAccessFunc)get_lot_account,
63 : set_lot_account),
64 : gnc_sql_make_table_entry<CT_BOOLEAN>("is_closed", 0, COL_NNUL, "is-closed")
65 : });
66 :
67 15 : GncSqlLotsBackend::GncSqlLotsBackend() :
68 30 : GncSqlObjectBackend(TABLE_VERSION, GNC_ID_LOT,
69 75 : TABLE_NAME, col_table) {}
70 :
71 : /* ================================================================= */
72 : static gpointer
73 1 : get_lot_account (gpointer pObject)
74 : {
75 : const GNCLot* lot;
76 : Account* pAccount;
77 :
78 1 : g_return_val_if_fail (pObject != NULL, NULL);
79 1 : g_return_val_if_fail (GNC_IS_LOT (pObject), NULL);
80 :
81 1 : lot = GNC_LOT (pObject);
82 1 : pAccount = gnc_lot_get_account (lot);
83 1 : return pAccount;
84 : }
85 :
86 : static void
87 1 : set_lot_account (gpointer pObject, gpointer pValue)
88 : {
89 : GNCLot* lot;
90 : Account* pAccount;
91 :
92 1 : g_return_if_fail (pObject != NULL && GNC_IS_LOT (pObject));
93 1 : g_return_if_fail (pValue == NULL || GNC_IS_ACCOUNT (pValue));
94 :
95 1 : lot = GNC_LOT (pObject);
96 1 : pAccount = GNC_ACCOUNT (pValue);
97 1 : if (pAccount != NULL)
98 : {
99 1 : xaccAccountInsertLot (pAccount, lot);
100 : }
101 : }
102 :
103 : static GNCLot*
104 1 : load_single_lot (GncSqlBackend* sql_be, GncSqlRow& row)
105 : {
106 : GNCLot* lot;
107 :
108 1 : g_return_val_if_fail (sql_be != NULL, NULL);
109 :
110 1 : lot = gnc_lot_new (sql_be->book());
111 :
112 1 : gnc_lot_begin_edit (lot);
113 1 : gnc_sql_load_object (sql_be, row, GNC_ID_LOT, lot, col_table);
114 1 : gnc_lot_commit_edit (lot);
115 :
116 1 : return lot;
117 : }
118 :
119 : void
120 7 : GncSqlLotsBackend::load_all (GncSqlBackend* sql_be)
121 : {
122 13 : g_return_if_fail (sql_be != NULL);
123 :
124 7 : std::stringstream sql;
125 7 : sql << "SELECT * FROM " << TABLE_NAME;
126 7 : auto stmt = sql_be->create_statement_from_sql(sql.str());
127 7 : if (stmt != nullptr)
128 : {
129 7 : auto result = sql_be->execute_select_statement(stmt);
130 7 : if (result->begin () == nullptr)
131 6 : return;
132 2 : for (auto row : *result)
133 2 : load_single_lot (sql_be, row);
134 :
135 1 : auto sql = g_strdup_printf ("SELECT DISTINCT guid FROM %s",
136 : TABLE_NAME);
137 1 : gnc_sql_slots_load_for_sql_subquery (sql_be, sql, (BookLookupFn)gnc_lot_lookup);
138 1 : g_free (sql);
139 : }
140 13 : }
141 :
142 : /* ================================================================= */
143 : void
144 13 : GncSqlLotsBackend::create_tables (GncSqlBackend* sql_be)
145 : {
146 : gint version;
147 :
148 13 : g_return_if_fail (sql_be != NULL);
149 :
150 26 : version = sql_be->get_table_version( TABLE_NAME);
151 13 : if (version == 0)
152 : {
153 : /* The table doesn't exist, so create it */
154 18 : (void)sql_be->create_table(TABLE_NAME, TABLE_VERSION, col_table);
155 : }
156 7 : else if (version < m_version)
157 : {
158 : /* Version 1 -> 2 removes the 'NOT NULL' constraint on the account_guid
159 : field.
160 :
161 : Create a temporary table, copy the data from the old table, delete the
162 : old table, then rename the new one. */
163 :
164 0 : sql_be->upgrade_table(TABLE_NAME, col_table);
165 0 : sql_be->set_table_version (TABLE_NAME, TABLE_VERSION);
166 :
167 0 : PINFO ("Lots table upgraded from version 1 to version %d\n", TABLE_VERSION);
168 : }
169 : }
170 :
171 : static void
172 1 : do_save_lot (QofInstance* inst, gpointer data)
173 : {
174 1 : auto s = reinterpret_cast<write_objects_t*>(data);
175 :
176 1 : if (s->is_ok)
177 : {
178 1 : s->commit (inst);
179 : }
180 1 : }
181 :
182 : bool
183 6 : GncSqlLotsBackend::write (GncSqlBackend* sql_be)
184 : {
185 6 : g_return_val_if_fail (sql_be != NULL, FALSE);
186 6 : write_objects_t data{sql_be, true, this};
187 :
188 6 : qof_collection_foreach (qof_book_get_collection (sql_be->book(), GNC_ID_LOT),
189 : (QofInstanceForeachCB)do_save_lot, &data);
190 6 : return data.is_ok;
191 : }
192 :
193 : /* ================================================================= */
194 : template<> void
195 24 : GncSqlColumnTableEntryImpl<CT_LOTREF>::load (const GncSqlBackend* sql_be,
196 : GncSqlRow& row,
197 : QofIdTypeConst obj_name,
198 : gpointer pObject) const noexcept
199 : {
200 24 : load_from_guid_ref(row, obj_name, pObject,
201 3 : [sql_be](GncGUID* g){
202 3 : return gnc_lot_lookup(g, sql_be->book());
203 : });
204 24 : }
205 :
206 : template<> void
207 12 : GncSqlColumnTableEntryImpl<CT_LOTREF>::add_to_table(ColVec& vec) const noexcept
208 : {
209 12 : add_objectref_guid_to_table(vec);
210 12 : }
211 :
212 : template<> void
213 23 : GncSqlColumnTableEntryImpl<CT_LOTREF>::add_to_query(QofIdTypeConst obj_name,
214 : const gpointer pObject,
215 : PairVec& vec) const noexcept
216 : {
217 23 : add_objectref_guid_to_query(obj_name, pObject, vec);
218 23 : }
219 :
220 : /* ========================== END OF FILE ===================== */
|