39#include "gncBillTermP.h"
40#include "gncInvoiceP.h"
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"
51#define _GNC_MOD_NAME GNC_ID_INVOICE
53static QofLogModule log_module = G_LOG_DOMAIN;
55#define TABLE_NAME "invoices"
56#define TABLE_VERSION 4
58#define MAX_ID_LEN 2048
59#define MAX_NOTES_LEN 2048
60#define MAX_BILLING_ID_LEN 2048
62static EntryVec col_table
64 gnc_sql_make_table_entry<CT_GUID>(
"guid", 0, COL_NNUL | COL_PKEY,
"guid"),
65 gnc_sql_make_table_entry<CT_STRING>(
"id", MAX_ID_LEN, COL_NNUL, INVOICE_ID,
67 gnc_sql_make_table_entry<CT_TIME>(
"date_opened", 0, 0, INVOICE_OPENED,
69 gnc_sql_make_table_entry<CT_TIME>(
"date_posted", 0, 0, INVOICE_POSTED,
71 gnc_sql_make_table_entry<CT_STRING>(
"notes", MAX_NOTES_LEN, COL_NNUL,
73 gnc_sql_make_table_entry<CT_BOOLEAN>(
"active", 0, COL_NNUL,
74 QOF_PARAM_ACTIVE,
true),
75 gnc_sql_make_table_entry<CT_COMMODITYREF>(
"currency", 0, COL_NNUL,
78 gnc_sql_make_table_entry<CT_OWNERREF>(
"owner", 0, 0,
81 gnc_sql_make_table_entry<CT_BILLTERMREF>(
"terms", 0, 0, INVOICE_TERMS,
83 gnc_sql_make_table_entry<CT_STRING>(
"billing_id", MAX_BILLING_ID_LEN, 0,
84 INVOICE_BILLINGID,
true),
85 gnc_sql_make_table_entry<CT_TXREF>(
"post_txn", 0, 0, INVOICE_POST_TXN,
87 gnc_sql_make_table_entry<CT_LOTREF>(
"post_lot", 0, 0,
90 gnc_sql_make_table_entry<CT_ACCOUNTREF>(
"post_acc", 0, 0, INVOICE_ACC,
92 gnc_sql_make_table_entry<CT_OWNERREF>(
"billto", 0, 0,
95 gnc_sql_make_table_entry<CT_NUMERIC>(
"charge_amt", 0, 0,
100GncSqlInvoiceBackend::GncSqlInvoiceBackend() :
102 TABLE_NAME, col_table) {}
108 GncInvoice* pInvoice;
110 g_return_val_if_fail (sql_be != NULL, NULL);
112 guid = gnc_sql_load_guid (sql_be, row);
113 pInvoice = gncInvoiceLookup (sql_be->book(), guid);
114 if (pInvoice == NULL)
116 pInvoice = gncInvoiceCreate (sql_be->book());
118 gnc_sql_load_object (sql_be, row, GNC_ID_INVOICE, pInvoice, col_table);
119 qof_instance_mark_clean (QOF_INSTANCE (pInvoice));
125static inline GncInvoice*
134 g_return_if_fail (sql_be != NULL);
136 std::string sql(
"SELECT * FROM " TABLE_NAME);
137 auto stmt = sql_be->create_statement_from_sql(sql);
140 for (
auto row : *result)
141 load_single_invoice (sql_be, row);
143 std::string pkey(col_table[0]->name());
144 sql =
"SELECT DISTINCT ";
145 sql += pkey +
" FROM " TABLE_NAME;
146 gnc_sql_slots_load_for_sql_subquery (sql_be, sql,
147 (BookLookupFn)gnc_invoice_lookup);
156 g_return_if_fail (sql_be != NULL);
161 sql_be->
create_table(TABLE_NAME, TABLE_VERSION, col_table);
163 else if (version < TABLE_VERSION)
173 PINFO (
"Invoices table upgraded from version %d to version %d\n", version,
186 gboolean is_ok = TRUE;
188 g_return_val_if_fail (inst != NULL, FALSE);
189 g_return_val_if_fail (GNC_IS_INVOICE (inst), FALSE);
190 g_return_val_if_fail (sql_be != NULL, FALSE);
192 invoice = GNC_INVOICE (inst);
194 is_infant = qof_instance_get_infant (inst);
199 else if (sql_be->pristine() || is_infant)
207 if (op != OP_DB_DELETE)
225 is_ok = gnc_sql_slots_save (sql_be, guid, is_infant, inst);
229 is_ok = gnc_sql_slots_delete (sql_be, guid);
238invoice_should_be_saved (GncInvoice* invoice)
242 g_return_val_if_fail (invoice != NULL, FALSE);
245 id = gncInvoiceGetID (invoice);
246 if (
id == NULL || *
id ==
'\0')
255write_single_invoice (
QofInstance* term_p, gpointer data_p)
259 g_return_if_fail (term_p != NULL);
260 g_return_if_fail (GNC_IS_INVOICE (term_p));
261 g_return_if_fail (data_p != NULL);
263 if (s->is_ok && invoice_should_be_saved (GNC_INVOICE (term_p)))
272 g_return_val_if_fail (sql_be != NULL, FALSE);
285 gpointer pObject)
const noexcept
287 load_from_guid_ref(row, obj_name, pObject,
289 return gncInvoiceLookup (sql_be->book(), g);
296 add_objectref_guid_to_table(vec);
301 const gpointer pObject,
302 PairVec& vec)
const noexcept
304 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 do_db_operation(E_DB_OPERATION op, const char *table_name, QofIdTypeConst obj_name, gpointer pObject, const EntryVec &table) const noexcept
Performs an operation on the database.
bool save_commodity(gnc_commodity *comm) noexcept
Ensure that a commodity referenced in another object is in fact saved in the database.
bool set_table_version(const std::string &table_name, uint_t version) noexcept
Registers the version for a table.
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.
bool write(GncSqlBackend *) override
Write all objects of m_type_name to the database.
bool commit(GncSqlBackend *sql_be, QofInstance *inst) override
UPDATE/INSERT a single instance of m_type_name into the database.
void load_all(GncSqlBackend *) override
Load all objects of m_type in the database into memory.
void create_tables(GncSqlBackend *) override
Conditionally create or update a database table from m_col_table.
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 data to SQL
Commodity handling public routines.
load and save invoice 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.
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.
const GncGUID * qof_instance_get_guid(gconstpointer inst)
Return the GncGUID of this instance.
gboolean qof_instance_get_destroying(gconstpointer ptr)
Retrieve the flag that indicates whether or not this object is about to be destroyed.
#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().