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 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 62 static 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,
100 GncSqlInvoiceBackend::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));
125 static inline GncInvoice*
126 gnc_invoice_lookup (
const GncGUID *guid,
const QofBook *book)
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;
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)
238 invoice_should_be_saved (GncInvoice* invoice)
242 g_return_val_if_fail (invoice != NULL, FALSE);
245 id = gncInvoiceGetID (invoice);
246 if (
id == NULL || *
id ==
'\0')
255 write_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);
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 create_table(const std::string &table_name, const EntryVec &col_table) const noexcept
Creates a table in the database.
bool set_table_version(const std::string &table_name, uint_t version) noexcept
Registers the version for a table.
GncSqlResultPtr execute_select_statement(const GncSqlStatementPtr &stmt) const noexcept
Executes an SQL SELECT statement and returns the result rows.
const GncGUID * qof_instance_get_guid(gconstpointer inst)
Return the GncGUID of this instance.
void gnc_sql_slots_load_for_sql_subquery(GncSqlBackend *sql_be, const std::string subquery, BookLookupFn lookup_fn)
gnc_sql_slots_load_for_sql_subquery - Loads slots for all objects whose guid is supplied by a subquer...
#define G_LOG_DOMAIN
Functions providing the SX List as a plugin page.
#define PINFO(format, args...)
Print an informational note.
load and save data to SQL
const gchar * QofIdTypeConst
QofIdTypeConst declaration.
load and save accounts data to SQL
gboolean qof_instance_get_destroying(gconstpointer ptr)
Retrieve the flag that indicates whether or not this object is about to be destroyed.
bool write(GncSqlBackend *) override
Write all objects of m_type_name to the database.
gboolean gnc_sql_slots_save(GncSqlBackend *sql_be, const GncGUID *guid, gboolean is_infant, QofInstance *inst)
gnc_sql_slots_save - Saves slots for an object to the db.
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 load_all(GncSqlBackend *) override
Load all objects of m_type in the database into memory.
#define QOF_BOOK_RETURN_ENTITY(book, guid, e_type, c_type)
Encapsulates all the information about a dataset manipulated by QOF.
load and save accounts data to SQL
void(* QofSetterFunc)(gpointer, gpointer)
The QofSetterFunc defines an function pointer for parameter setters.
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 save_commodity(gnc_commodity *comm) noexcept
Ensure that a commodity referenced in another object is in fact saved in the database.
Row of SQL Query results.
void upgrade_table(const std::string &table_name, const EntryVec &col_table) noexcept
Upgrades a table to a new structure.
void create_tables(GncSqlBackend *) override
Conditionally create or update a database table from m_col_table.
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.
gpointer(* QofAccessFunc)(gpointer object, const QofParam *param)
The QofAccessFunc defines an arbitrary function pointer for access functions.
Encapsulates per-class table schema with functions to load, create a table, commit a changed front-en...
bool commit(GncSqlBackend *sql_be, QofInstance *inst) override
UPDATE/INSERT a single instance of m_type_name into the database.
Data-passing struct for callbacks to qof_object_foreach() used in GncSqlObjectBackend::write().
gboolean gnc_sql_slots_delete(GncSqlBackend *sql_be, const GncGUID *guid)
gnc_sql_slots_delete - Deletes slots for an object from the db.
void add_to_table(ColVec &vec) const noexcept override
Add a GncSqlColumnInfo structure for the column type to a ColVec.
load and save invoice data to SQL
The type used to store guids in C.
uint_t get_table_version(const std::string &table_name) const noexcept
Returns the version number for a DB table.
Commodity handling public routines.
Main SQL backend structure.