38 #include "gncEmployeeP.h" 40 #include "gnc-sql-connection.hpp" 41 #include "gnc-sql-backend.hpp" 42 #include "gnc-sql-object-backend.hpp" 43 #include "gnc-sql-column-table-entry.hpp" 48 #define _GNC_MOD_NAME GNC_ID_EMPLOYEE 52 #define MAX_USERNAME_LEN 2048 53 #define MAX_ID_LEN 2048 54 #define MAX_LANGUAGE_LEN 2048 55 #define MAX_ACL_LEN 2048 57 #define TABLE_NAME "employees" 58 #define TABLE_VERSION 2 60 static EntryVec col_table
62 gnc_sql_make_table_entry<CT_GUID>(
"guid", 0, COL_NNUL | COL_PKEY,
"guid"),
63 gnc_sql_make_table_entry<CT_STRING>(
64 "username", MAX_USERNAME_LEN, COL_NNUL,
"username"),
65 gnc_sql_make_table_entry<CT_STRING>(
"id", MAX_ID_LEN, COL_NNUL,
"id"),
66 gnc_sql_make_table_entry<CT_STRING>(
67 "language", MAX_LANGUAGE_LEN, COL_NNUL,
"language"),
68 gnc_sql_make_table_entry<CT_STRING>(
"acl", MAX_ACL_LEN, COL_NNUL,
"acl"),
69 gnc_sql_make_table_entry<CT_BOOLEAN>(
"active", 0, COL_NNUL,
"active"),
70 gnc_sql_make_table_entry<CT_COMMODITYREF>(
71 "currency", 0, COL_NNUL,
"currency"),
72 gnc_sql_make_table_entry<CT_ACCOUNTREF>(
73 "ccard_guid", 0, 0,
"credit-card-account"),
74 gnc_sql_make_table_entry<CT_NUMERIC>(
"workday", 0, COL_NNUL,
"workday"),
75 gnc_sql_make_table_entry<CT_NUMERIC>(
"rate", 0, COL_NNUL,
"rate"),
76 gnc_sql_make_table_entry<CT_ADDRESS>(
"addr", 0, 0,
"address"),
79 GncSqlEmployeeBackend::GncSqlEmployeeBackend() :
81 TABLE_NAME, col_table) {}
87 GncEmployee* pEmployee;
89 g_return_val_if_fail (sql_be != NULL, NULL);
91 guid = gnc_sql_load_guid (sql_be, row);
92 pEmployee = gncEmployeeLookup (sql_be->book(), guid);
93 if (pEmployee == NULL)
95 pEmployee = gncEmployeeCreate (sql_be->book());
97 gnc_sql_load_object (sql_be, row, GNC_ID_EMPLOYEE, pEmployee, col_table);
98 qof_instance_mark_clean (QOF_INSTANCE (pEmployee));
103 static inline GncEmployee*
104 gnc_employee_lookup (
const GncGUID *guid,
const QofBook *book)
112 g_return_if_fail (sql_be != NULL);
114 std::string sql(
"SELECT * FROM " TABLE_NAME);
115 auto stmt = sql_be->create_statement_from_sql(sql);
118 for (
auto row : *result)
119 load_single_employee (sql_be, row);
121 std::string pkey(col_table[0]->name());
122 sql =
"SELECT DISTINCT ";
123 sql += pkey +
" FROM " TABLE_NAME;
125 (BookLookupFn)gnc_employee_lookup);
135 g_return_if_fail (sql_be != NULL);
140 sql_be->
create_table(TABLE_NAME, TABLE_VERSION, col_table);
142 else if (version < m_version)
148 PINFO (
"Employees table upgraded from version 1 to version %d\n",
161 gboolean is_ok = TRUE;
163 g_return_val_if_fail (inst != NULL, FALSE);
164 g_return_val_if_fail (GNC_IS_EMPLOYEE (inst), FALSE);
165 g_return_val_if_fail (sql_be != NULL, FALSE);
167 emp = GNC_EMPLOYEE (inst);
169 is_infant = qof_instance_get_infant (inst);
174 else if (sql_be->pristine() || is_infant)
182 if (op != OP_DB_DELETE)
213 employee_should_be_saved (GncEmployee* employee)
217 g_return_val_if_fail (employee != NULL, FALSE);
220 id = gncEmployeeGetID (employee);
221 if (
id == NULL || *
id ==
'\0')
230 write_single_employee (
QofInstance* term_p, gpointer data_p)
234 g_return_if_fail (term_p != NULL);
235 g_return_if_fail (GNC_IS_EMPLOYEE (term_p));
236 g_return_if_fail (data_p != NULL);
238 if (s->is_ok && employee_should_be_saved (GNC_EMPLOYEE (term_p)))
240 s->is_ok = s->obe->commit (s->be, term_p);
249 g_return_val_if_fail (sql_be != NULL, FALSE);
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.
void create_tables(GncSqlBackend *) override
Conditionally create or update a database table from m_col_table.
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.
bool write(GncSqlBackend *) override
Write all objects of m_type_name to the database.
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
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.
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.
#define QOF_BOOK_RETURN_ENTITY(book, guid, e_type, c_type)
Encapsulates all the information about a dataset manipulated by QOF.
bool commit(GncSqlBackend *, QofInstance *) override
UPDATE/INSERT a single instance of m_type_name into the database.
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.
load and save employee data to SQL
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.
Encapsulates per-class table schema with functions to load, create a table, commit a changed front-en...
void load_all(GncSqlBackend *) override
Load all objects of m_type in the database into memory.
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.
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.