39#include "gncInvoiceP.h"
40#include "gncTaxTableP.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"
53#define _GNC_MOD_NAME GNC_ID_ENTRY
55static QofLogModule log_module = G_LOG_DOMAIN;
57#define TABLE_NAME "entries"
58#define TABLE_VERSION 4
59#define MAX_DESCRIPTION_LEN 2048
60#define MAX_ACTION_LEN 2048
61#define MAX_NOTES_LEN 2048
62#define MAX_DISCTYPE_LEN 2048
63#define MAX_DISCHOW_LEN 2048
65static void entry_set_invoice (gpointer pObject, gpointer val);
66static void entry_set_bill (gpointer pObject, gpointer val);
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_TIME>(
"date", 0, COL_NNUL, ENTRY_DATE,
73 gnc_sql_make_table_entry<CT_TIME>(
"date_entered", 0, 0,
74 ENTRY_DATE_ENTERED,
true),
75 gnc_sql_make_table_entry<CT_STRING>(
76 "description", MAX_DESCRIPTION_LEN, 0,
"description"),
77 gnc_sql_make_table_entry<CT_STRING>(
"action", MAX_ACTION_LEN, 0,
79 gnc_sql_make_table_entry<CT_STRING>(
"notes", MAX_NOTES_LEN, 0, ENTRY_NOTES,
81 gnc_sql_make_table_entry<CT_NUMERIC>(
"quantity", 0, 0, ENTRY_QTY,
83 gnc_sql_make_table_entry<CT_ACCOUNTREF>(
"i_acct", 0, 0, ENTRY_IACCT,
85 gnc_sql_make_table_entry<CT_NUMERIC>(
"i_price", 0, 0, ENTRY_IPRICE,
87 gnc_sql_make_table_entry<CT_NUMERIC>(
"i_discount", 0, 0,
90 gnc_sql_make_table_entry<CT_INVOICEREF>(
"invoice", 0, 0,
93 gnc_sql_make_table_entry<CT_STRING>(
"i_disc_type", MAX_DISCTYPE_LEN, 0,
94 ENTRY_INV_DISC_TYPE,
true),
95 gnc_sql_make_table_entry<CT_STRING>(
"i_disc_how", MAX_DISCHOW_LEN, 0,
96 ENTRY_INV_DISC_HOW,
true),
97 gnc_sql_make_table_entry<CT_BOOLEAN>(
"i_taxable", 0, 0, ENTRY_INV_TAXABLE,
99 gnc_sql_make_table_entry<CT_BOOLEAN>(
"i_taxincluded", 0, 0,
100 ENTRY_INV_TAX_INC,
true),
101 gnc_sql_make_table_entry<CT_TAXTABLEREF>(
"i_taxtable", 0, 0,
104 gnc_sql_make_table_entry<CT_ACCOUNTREF>(
"b_acct", 0, 0, ENTRY_BACCT,
106 gnc_sql_make_table_entry<CT_NUMERIC>(
"b_price", 0, 0, ENTRY_BPRICE,
108 gnc_sql_make_table_entry<CT_INVOICEREF>(
"bill", 0, 0,
111 gnc_sql_make_table_entry<CT_BOOLEAN>(
"b_taxable", 0, 0, ENTRY_BILL_TAXABLE,
113 gnc_sql_make_table_entry<CT_BOOLEAN>(
"b_taxincluded", 0, 0,
114 ENTRY_BILL_TAX_INC,
true),
115 gnc_sql_make_table_entry<CT_TAXTABLEREF>(
"b_taxtable", 0, 0,
118 gnc_sql_make_table_entry<CT_INT>(
"b_paytype", 0, 0,
121 gnc_sql_make_table_entry<CT_BOOLEAN>(
"billable", 0, 0, ENTRY_BILLABLE,
123 gnc_sql_make_table_entry<CT_OWNERREF>(
"billto", 0, 0, ENTRY_BILLTO,
true),
124 gnc_sql_make_table_entry<CT_ORDERREF>(
"order_guid", 0, 0,
129GncSqlEntryBackend::GncSqlEntryBackend() :
131 TABLE_NAME, col_table) {}
134entry_set_invoice (gpointer pObject, gpointer val)
139 g_return_if_fail (pObject != NULL);
140 g_return_if_fail (GNC_IS_ENTRY (pObject));
141 g_return_if_fail (val != NULL);
142 g_return_if_fail (GNC_IS_INVOICE (val));
144 entry = GNC_ENTRY (pObject);
145 invoice = GNC_INVOICE (val);
147 gncInvoiceAddEntry (invoice, entry);
151entry_set_bill (gpointer pObject, gpointer val)
156 g_return_if_fail (pObject != NULL);
157 g_return_if_fail (GNC_IS_ENTRY (pObject));
158 g_return_if_fail (val != NULL);
159 g_return_if_fail (GNC_IS_INVOICE (val));
161 entry = GNC_ENTRY (pObject);
162 bill = GNC_INVOICE (val);
173 g_return_val_if_fail (sql_be != NULL, NULL);
175 guid = gnc_sql_load_guid (sql_be, row);
176 pEntry = gncEntryLookup (sql_be->book(), guid);
179 pEntry = gncEntryCreate (sql_be->book());
181 gnc_sql_load_object (sql_be, row, GNC_ID_ENTRY, pEntry, col_table);
182 qof_instance_mark_clean (QOF_INSTANCE (pEntry));
188static inline GncEntry*
197 g_return_if_fail (sql_be != NULL);
199 std::string sql(
"SELECT * FROM " TABLE_NAME);
200 auto stmt = sql_be->create_statement_from_sql(sql);
203 for (
auto row : *result)
204 load_single_entry (sql_be, row);
206 std::string pkey(col_table[0]->name());
207 sql =
"SELECT DISTINCT ";
208 sql += pkey +
" FROM " TABLE_NAME;
209 gnc_sql_slots_load_for_sql_subquery (sql_be, sql,
210 (BookLookupFn)gnc_entry_lookup);
219 g_return_if_fail (sql_be != NULL);
224 sql_be->
create_table(TABLE_NAME, TABLE_VERSION, col_table);
226 else if (version < TABLE_VERSION)
236 PINFO (
"Entries table upgraded from version %d to version %d\n", version,
243write_single_entry (
QofInstance* term_p, gpointer data_p)
246 GncEntry* entry = GNC_ENTRY (term_p);
248 g_return_if_fail (term_p != NULL);
249 g_return_if_fail (GNC_IS_ENTRY (term_p));
250 g_return_if_fail (data_p != NULL);
253 if (s->is_ok && (gncEntryGetOrder (entry) != NULL ||
254 gncEntryGetInvoice (entry) != NULL ||
255 gncEntryGetBill (entry) != NULL))
264 g_return_val_if_fail (sql_be != NULL, FALSE);
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.
void load_all(GncSqlBackend *) override
Load all objects of m_type in the database into memory.
bool write(GncSqlBackend *) override
Write all objects of m_type_name to the database.
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 entry data to SQL
load and save invoice data to SQL
load and save order data to SQL
load and save accounts data to SQL
load and save tax table 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.
void gncBillAddEntry(GncInvoice *bill, GncEntry *entry)
Call this function when adding an entry to a bill instead of an invoice.
#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().