37#include "gncBillTermP.h"
45#include "gnc-sql-connection.hpp"
46#include "gnc-sql-backend.hpp"
47#include "gnc-sql-object-backend.hpp"
48#include "gnc-sql-column-table-entry.hpp"
52#define _GNC_MOD_NAME GNC_ID_BILLTERM
54static QofLogModule log_module = G_LOG_DOMAIN;
56#define MAX_NAME_LEN 2048
57#define MAX_DESCRIPTION_LEN 2048
58#define MAX_TYPE_LEN 2048
60static void set_invisible (gpointer data, gboolean value);
61static gpointer bt_get_parent (gpointer data);
62static void bt_set_parent (gpointer data, gpointer value);
63static void bt_set_parent_guid (gpointer data, gpointer value);
65#define TABLE_NAME "billterms"
66#define TABLE_VERSION 2
68static EntryVec col_table
70 gnc_sql_make_table_entry<CT_GUID>(
"guid", 0, COL_NNUL | COL_PKEY,
"guid"),
71 gnc_sql_make_table_entry<CT_STRING>(
"name", MAX_NAME_LEN, COL_NNUL,
"name"),
72 gnc_sql_make_table_entry<CT_STRING>(
"description", MAX_DESCRIPTION_LEN,
73 COL_NNUL, GNC_BILLTERM_DESC,
75 gnc_sql_make_table_entry<CT_INT>(
"refcount", 0, COL_NNUL,
78 gnc_sql_make_table_entry<CT_BOOLEAN>(
"invisible", 0, COL_NNUL,
81 gnc_sql_make_table_entry<CT_GUID>(
"parent", 0, 0,
84 gnc_sql_make_table_entry<CT_STRING>(
"type", MAX_TYPE_LEN, COL_NNUL,
85 GNC_BILLTERM_TYPE,
true),
86 gnc_sql_make_table_entry<CT_INT>(
"duedays", 0, 0, GNC_BILLTERM_DUEDAYS,
88 gnc_sql_make_table_entry<CT_INT>(
"discountdays", 0, 0,
89 GNC_BILLTERM_DISCDAYS,
true),
90 gnc_sql_make_table_entry<CT_NUMERIC>(
"discount", 0, 0,
91 GNC_BILLTERM_DISCOUNT,
true),
92 gnc_sql_make_table_entry<CT_INT>(
"cutoff", 0, 0, GNC_BILLTERM_CUTOFF,
96static EntryVec billterm_parent_col_table
98 gnc_sql_make_table_entry<CT_GUID>(
"parent", 0, 0,
nullptr,
102GncSqlBillTermBackend::GncSqlBillTermBackend() :
104 TABLE_NAME, col_table) {}
108 GncBillTerm* billterm;
114using BillTermParentGuidVec = std::vector<BillTermParentGuidPtr>;
117set_invisible (gpointer data, gboolean value)
119 GncBillTerm* term = GNC_BILLTERM (data);
121 g_return_if_fail (term != NULL);
125 gncBillTermMakeInvisible (term);
130bt_get_parent (gpointer pObject)
132 const GncBillTerm* billterm;
133 const GncBillTerm* pParent;
136 g_return_val_if_fail (pObject != NULL, NULL);
137 g_return_val_if_fail (GNC_IS_BILLTERM (pObject), NULL);
139 billterm = GNC_BILLTERM (pObject);
140 pParent = gncBillTermGetParent (billterm);
150 return (gpointer)parent_guid;
154bt_set_parent (gpointer data, gpointer value)
156 GncBillTerm* billterm;
161 g_return_if_fail (data != NULL);
162 g_return_if_fail (GNC_IS_BILLTERM (data));
164 billterm = GNC_BILLTERM (data);
168 parent = gncBillTermLookup (pBook, guid);
171 gncBillTermSetParent (billterm, parent);
172 gncBillTermSetChild (parent, billterm);
178bt_set_parent_guid (gpointer pObject, gpointer pValue)
180 g_return_if_fail (pObject != NULL);
181 g_return_if_fail (pValue != NULL);
184 s->guid = *
static_cast<GncGUID*
>(pValue);
190 BillTermParentGuidVec& l_billterms_needing_parents)
192 g_return_val_if_fail (sql_be != NULL, NULL);
194 auto guid = gnc_sql_load_guid (sql_be, row);
195 auto pBillTerm = gncBillTermLookup (sql_be->book(), guid);
196 if (pBillTerm ==
nullptr)
198 pBillTerm = gncBillTermCreate (sql_be->book());
200 gnc_sql_load_object (sql_be, row, GNC_ID_BILLTERM, pBillTerm, col_table);
206 if (gncBillTermGetParent (pBillTerm) == NULL)
210 s.billterm = pBillTerm;
212 gnc_sql_load_object (sql_be, row, GNC_ID_BILLTERM, &s,
213 billterm_parent_col_table);
219 qof_instance_mark_clean (QOF_INSTANCE (pBillTerm));
225static inline GncBillTerm*
235 g_return_if_fail (sql_be != NULL);
237 std::string sql(
"SELECT * FROM " TABLE_NAME);
238 auto stmt = sql_be->create_statement_from_sql(sql);
240 BillTermParentGuidVec l_billterms_needing_parents;
242 for (
auto row : *result)
244 load_single_billterm (sql_be, row, l_billterms_needing_parents);
247 std::string pkey(col_table[0]->name());
248 sql =
"SELECT DISTINCT ";
249 sql += pkey +
" FROM " TABLE_NAME;
250 gnc_sql_slots_load_for_sql_subquery (sql_be, sql,
251 (BookLookupFn)gnc_billterm_lookup);
258 if (!l_billterms_needing_parents.empty())
260 bool progress_made =
true;
261 std::reverse(l_billterms_needing_parents.begin(),
262 l_billterms_needing_parents.end());
263 auto end = l_billterms_needing_parents.end();
264 while (progress_made)
266 progress_made =
false;
267 end = std::remove_if(l_billterms_needing_parents.begin(), end,
270 auto pBook = qof_instance_get_book (QOF_INSTANCE (s->billterm));
271 auto parent = gncBillTermLookup (pBook,
273 if (parent != nullptr)
275 gncBillTermSetParent (s->billterm, parent);
276 gncBillTermSetChild (parent, s->billterm);
277 progress_made = true;
299 g_return_val_if_fail (sql_be != NULL, FALSE);
312 g_return_if_fail (sql_be != NULL);
317 sql_be->
create_table(TABLE_NAME, TABLE_VERSION, col_table);
319 else if (version < m_version)
325 PINFO (
"Billterms table upgraded from version 1 to version %d\n",
336 gpointer pObject)
const noexcept
338 load_from_guid_ref(row, obj_name, pObject,
340 return gncBillTermLookup(sql_be->book(), g);
347 add_objectref_guid_to_table(vec);
352 const gpointer pObject,
353 PairVec& vec)
const noexcept
355 add_objectref_guid_to_query(obj_name, pObject, vec);
Main SQL backend structure.
uint_t get_table_version(const std::string &table_name) const noexcept
Returns the version number for a DB table.
bool create_table(const std::string &table_name, const EntryVec &col_table) const noexcept
Creates a table in the database.
GncSqlResultPtr execute_select_statement(const GncSqlStatementPtr &stmt) const noexcept
Executes an SQL SELECT statement and returns the result rows.
void upgrade_table(const std::string &table_name, const EntryVec &col_table) noexcept
Upgrades a table to a new structure.
bool set_table_version(const std::string &table_name, uint_t version) noexcept
Registers the version for a table.
void create_tables(GncSqlBackend *) override
Conditionally create or update a database table from m_col_table.
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.
void add_to_query(QofIdTypeConst obj_name, void *pObject, PairVec &vec) const noexcept override
Add a pair of the table column heading and object's value's string representation to a PairVec; used ...
void add_to_table(ColVec &vec) const noexcept override
Add a GncSqlColumnInfo structure for the column type to a ColVec.
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.
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 accounts data to SQL
load and save accounts data to SQL
Business Invoice Interface.
#define QOF_BOOK_RETURN_ENTITY(book, guid, e_type, c_type)
Encapsulates all the information about a dataset manipulated by QOF.
gpointer(* QofAccessFunc)(gpointer object, const QofParam *param)
The QofAccessFunc defines an arbitrary function pointer for access functions.
void(* QofSetterFunc)(gpointer, gpointer)
The QofSetterFunc defines an function pointer for parameter setters.
const gchar * QofIdTypeConst
QofIdTypeConst declaration.
QofBook * qof_instance_get_book(gconstpointer inst)
Return the book pointer.
const GncGUID * qof_instance_get_guid(gconstpointer inst)
Return the GncGUID of this instance.
#define PINFO(format, args...)
Print an informational note.
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.
Data-passing struct for callbacks to qof_object_foreach() used in GncSqlObjectBackend::write().