37#include "gncBillTermP.h"
38#include "gncCustomerP.h"
39#include "gncTaxTableP.h"
41#include "gnc-sql-connection.hpp"
42#include "gnc-sql-backend.hpp"
43#include "gnc-sql-object-backend.hpp"
44#include "gnc-sql-column-table-entry.hpp"
50#define _GNC_MOD_NAME GNC_ID_CUSTOMER
52static QofLogModule log_module = G_LOG_DOMAIN;
54#define TABLE_NAME "customers"
55#define TABLE_VERSION 2
57#define MAX_NAME_LEN 2048
58#define MAX_ID_LEN 2048
59#define MAX_NOTES_LEN 2048
61static EntryVec col_table
63 gnc_sql_make_table_entry<CT_GUID>(
"guid", 0, COL_NNUL | COL_PKEY,
"guid" ),
64 gnc_sql_make_table_entry<CT_STRING>(
"name", MAX_NAME_LEN, COL_NNUL,
"name"),
65 gnc_sql_make_table_entry<CT_STRING>(
"id", MAX_ID_LEN, COL_NNUL,
67 gnc_sql_make_table_entry<CT_STRING>(
"notes", MAX_NOTES_LEN, COL_NNUL,
68 CUSTOMER_NOTES,
true),
69 gnc_sql_make_table_entry<CT_BOOLEAN>(
"active", 0, COL_NNUL,
70 QOF_PARAM_ACTIVE,
true),
71 gnc_sql_make_table_entry<CT_NUMERIC>(
"discount", 0, COL_NNUL,
72 CUSTOMER_DISCOUNT,
true),
73 gnc_sql_make_table_entry<CT_NUMERIC>(
"credit", 0, COL_NNUL,
74 CUSTOMER_CREDIT,
true),
75 gnc_sql_make_table_entry<CT_COMMODITYREF>(
"currency", 0, COL_NNUL,
78 gnc_sql_make_table_entry<CT_BOOLEAN>(
"tax_override", 0, COL_NNUL,
79 CUSTOMER_TT_OVER,
true),
80 gnc_sql_make_table_entry<CT_ADDRESS>(
"addr", 0, 0, CUSTOMER_ADDR,
82 gnc_sql_make_table_entry<CT_ADDRESS>(
"shipaddr", 0, 0, CUSTOMER_SHIPADDR,
84 gnc_sql_make_table_entry<CT_BILLTERMREF>(
"terms", 0, 0, CUSTOMER_TERMS,
86 gnc_sql_make_table_entry<CT_INT>(
"tax_included", 0, 0,
89 gnc_sql_make_table_entry<CT_TAXTABLEREF>(
"taxtable", 0, 0,
94GncSqlCustomerBackend::GncSqlCustomerBackend() :
96 TABLE_NAME, col_table) {}
104 g_return_val_if_fail (sql_be != NULL, NULL);
106 guid = gnc_sql_load_guid (sql_be, row);
107 pCustomer = gncCustomerLookup (sql_be->book(), guid);
108 if (pCustomer == NULL)
110 pCustomer = gncCustomerCreate (sql_be->book());
112 gnc_sql_load_object (sql_be, row, GNC_ID_CUSTOMER, pCustomer, col_table);
113 qof_instance_mark_clean (QOF_INSTANCE (pCustomer));
128 g_return_if_fail (sql_be != NULL);
130 std::string sql(
"SELECT * FROM " TABLE_NAME);
131 auto stmt = sql_be->create_statement_from_sql(sql);
134 for (
auto row : *result)
135 load_single_customer (sql_be, row);
137 std::string pkey(col_table[0]->name());
138 sql =
"SELECT DISTINCT ";
139 sql += pkey +
" FROM " TABLE_NAME;
140 gnc_sql_slots_load_for_sql_subquery (sql_be, sql,
141 (BookLookupFn)gnc_customer_lookup);
150 g_return_if_fail (sql_be != NULL);
155 sql_be->
create_table(TABLE_NAME, TABLE_VERSION, col_table);
157 else if (version < m_version)
163 PINFO (
"Customers table upgraded from version 1 to version %d\n",
174 g_return_val_if_fail (customer != NULL, FALSE);
177 id = gncCustomerGetID (customer);
178 if (
id == NULL || *
id ==
'\0')
187write_single_customer (
QofInstance* term_p, gpointer data_p)
191 g_return_if_fail (term_p != NULL);
192 g_return_if_fail (GNC_IS_CUSTOMER (term_p));
193 g_return_if_fail (data_p != NULL);
195 if (customer_should_be_saved (GNC_CUSTOMER (term_p)))
197 data->commit (term_p);
206 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 customer 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.
#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.
credit, discount and shipaddr are unique to GncCustomer id, name, notes, terms, addr,...
The type used to store guids in C.
Data-passing struct for callbacks to qof_object_foreach() used in GncSqlObjectBackend::write().