GnuCash c935c2f+
Loading...
Searching...
No Matches
gnc-job-sql.cpp
1/********************************************************************\
2 * gnc-job-sql.c -- job 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
30#include <glib.h>
31
32#include <config.h>
33
34#include <stdlib.h>
35#include <string.h>
36
37#include "gncJobP.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-job-sql.h"
45
46#define _GNC_MOD_NAME GNC_ID_JOB
47
48G_GNUC_UNUSED static QofLogModule log_module = G_LOG_DOMAIN;
49
50#define TABLE_NAME "jobs"
51#define TABLE_VERSION 1
52
53#define MAX_ID_LEN 2048
54#define MAX_NAME_LEN 2048
55#define MAX_REFERENCE_LEN 2048
56
57static 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,
61 JOB_ID, true),
62 gnc_sql_make_table_entry<CT_STRING>("name", MAX_NAME_LEN, COL_NNUL, "name"),
63 gnc_sql_make_table_entry<CT_STRING>("reference", MAX_REFERENCE_LEN,
64 COL_NNUL, JOB_REFERENCE, true),
65 gnc_sql_make_table_entry<CT_BOOLEAN>("active", 0, COL_NNUL,
66 (QofAccessFunc)gncJobGetActive,
67 (QofSetterFunc)gncJobSetActive),
68 gnc_sql_make_table_entry<CT_OWNERREF>("owner", 0, 0,
69 (QofAccessFunc)gncJobGetOwner,
70 (QofSetterFunc)gncJobSetOwner),
71});
72
73GncSqlJobBackend::GncSqlJobBackend() :
74 GncSqlObjectBackend(TABLE_VERSION, GNC_ID_JOB,
75 TABLE_NAME, col_table) {}
76
77static GncJob*
78load_single_job (GncSqlBackend* sql_be, GncSqlRow& row)
79{
80 const GncGUID* guid;
81 GncJob* pJob;
82
83 g_return_val_if_fail (sql_be != NULL, NULL);
84
85 guid = gnc_sql_load_guid (sql_be, row);
86 pJob = gncJobLookup (sql_be->book(), guid);
87 if (pJob == NULL)
88 {
89 pJob = gncJobCreate (sql_be->book());
90 }
91 gnc_sql_load_object (sql_be, row, GNC_ID_JOB, pJob, col_table);
92 qof_instance_mark_clean (QOF_INSTANCE (pJob));
93
94 return pJob;
95}
96
97/* Because gncJobLookup has the arguments backwards: */
98static inline GncJob*
99gnc_job_lookup (const GncGUID *guid, const QofBook *book)
100{
101 QOF_BOOK_RETURN_ENTITY(book, guid, GNC_ID_JOB, GncJob);
102}
103
104void
106{
107 g_return_if_fail (sql_be != NULL);
108
109 std::string sql("SELECT * FROM " TABLE_NAME);
110 auto stmt = sql_be->create_statement_from_sql(sql);
111 auto result = sql_be->execute_select_statement(stmt);
112
113 for (auto row : *result)
114 load_single_job (sql_be, row);
115
116 std::string pkey(col_table[0]->name());
117 sql = "SELECT DISTINCT ";
118 sql += pkey + " FROM " TABLE_NAME;
119 gnc_sql_slots_load_for_sql_subquery (sql_be, sql,
120 (BookLookupFn)gnc_job_lookup);
121}
122
123/* ================================================================= */
124static gboolean
125job_should_be_saved (GncJob* job)
126{
127 const char* id;
128
129 g_return_val_if_fail (job != NULL, FALSE);
130
131 /* make sure this is a valid job before we save it -- should have an ID */
132 id = gncJobGetID (job);
133 if (id == NULL || *id == '\0')
134 {
135 return FALSE;
136 }
137
138 return TRUE;
139}
140
141static void
142write_single_job (QofInstance* term_p, gpointer data_p)
143{
144 auto s = reinterpret_cast<write_objects_t*>(data_p);
145
146 g_return_if_fail (term_p != NULL);
147 g_return_if_fail (GNC_IS_JOB (term_p));
148 g_return_if_fail (data_p != NULL);
149
150 if (s->is_ok && job_should_be_saved (GNC_JOB (term_p)))
151 {
152 s->commit (term_p);
153 }
154}
155
156bool
158{
159 g_return_val_if_fail (sql_be != NULL, FALSE);
160 write_objects_t data{sql_be, true, this};
161
162 qof_object_foreach (GNC_ID_JOB, sql_be->book(), write_single_job, &data);
163
164 return data.is_ok;
165}
166
167/* ========================== END OF FILE ===================== */
Main SQL backend structure.
GncSqlResultPtr execute_select_statement(const GncSqlStatementPtr &stmt) const noexcept
Executes an SQL SELECT statement and returns the result rows.
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.
Encapsulates per-class table schema with functions to load, create a table, commit a changed front-en...
Row of SQL Query results.
load and save job data to SQL
load and save accounts data to SQL
#define QOF_BOOK_RETURN_ENTITY(book, guid, e_type, c_type)
Encapsulates all the information about a dataset manipulated by QOF.
Definition qofbook.h:101
gpointer(* QofAccessFunc)(gpointer object, const QofParam *param)
The QofAccessFunc defines an arbitrary function pointer for access functions.
Definition qofclass.h:123
void(* QofSetterFunc)(gpointer, gpointer)
The QofSetterFunc defines an function pointer for parameter setters.
Definition qofclass.h:130
void qof_object_foreach(QofIdTypeConst type_name, QofBook *book, QofInstanceForeachCB cb, gpointer user_data)
Invoke the callback 'cb' on every instance ov a particular object type.
The type used to store guids in C.
Definition guid.h:75
QofBook reference.
Definition qofbook-p.hpp:47
Data-passing struct for callbacks to qof_object_foreach() used in GncSqlObjectBackend::write().