GnuCash c935c2f+
Loading...
Searching...
No Matches
Public Member Functions
GncSqlInvoiceBackend Class Reference
Inheritance diagram for GncSqlInvoiceBackend:
GncSqlObjectBackend

Public Member Functions

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.
 
bool commit (GncSqlBackend *sql_be, QofInstance *inst) override
 UPDATE/INSERT a single instance of m_type_name into the database.
 
bool write (GncSqlBackend *) override
 Write all objects of m_type_name to the database.
 
- Public Member Functions inherited from GncSqlObjectBackend
 GncSqlObjectBackend (int version, const std::string &type, const std::string &table, const EntryVec &vec)
 
const char * type () const noexcept
 Return the m_type_name for the class.
 
const bool is_version (int version) const noexcept
 Compare a version with the compiled version (m_version).
 
bool instance_in_db (const GncSqlBackend *sql_be, QofInstance *inst) const noexcept
 Check the presence of an object in the backend's database.
 

Additional Inherited Members

- Protected Attributes inherited from GncSqlObjectBackend
const std::string m_table_name
 
const int m_version
 
const std::string m_type_name
 
const EntryVec & m_col_table
 The front-end QofIdType.
 

Detailed Description

Definition at line 35 of file gnc-invoice-sql.h.

Constructor & Destructor Documentation

◆ GncSqlInvoiceBackend()

GncSqlInvoiceBackend::GncSqlInvoiceBackend ( )

Definition at line 100 of file gnc-invoice-sql.cpp.

100 :
101 GncSqlObjectBackend(TABLE_VERSION, GNC_ID_INVOICE,
102 TABLE_NAME, col_table) {}
Encapsulates per-class table schema with functions to load, create a table, commit a changed front-en...

Member Function Documentation

◆ commit()

bool GncSqlInvoiceBackend::commit ( GncSqlBackend sql_be,
QofInstance inst 
)
overridevirtual

UPDATE/INSERT a single instance of m_type_name into the database.

Parameters
sql_beThe GncSqlBackend containing the database.
instThe QofInstance to be written out.

Reimplemented from GncSqlObjectBackend.

Definition at line 180 of file gnc-invoice-sql.cpp.

181{
182 const GncGUID* guid;
183 GncInvoice* invoice;
184 E_DB_OPERATION op;
185 gboolean is_infant;
186 gboolean is_ok = TRUE;
187
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);
191
192 invoice = GNC_INVOICE (inst);
193
194 is_infant = qof_instance_get_infant (inst);
196 {
197 op = OP_DB_DELETE;
198 }
199 else if (sql_be->pristine() || is_infant)
200 {
201 op = OP_DB_INSERT;
202 }
203 else
204 {
205 op = OP_DB_UPDATE;
206 }
207 if (op != OP_DB_DELETE)
208 {
209 // Ensure the commodity is in the db
210 is_ok = sql_be->save_commodity(gncInvoiceGetCurrency(invoice));
211 }
212
213 if (is_ok)
214 {
215 is_ok = sql_be->do_db_operation(op, TABLE_NAME, GNC_ID_INVOICE, inst,
216 col_table);
217 }
218
219 if (is_ok)
220 {
221 // Now, commit or delete any slots
222 guid = qof_instance_get_guid (inst);
223 if (!qof_instance_get_destroying (inst))
224 {
225 is_ok = gnc_sql_slots_save (sql_be, guid, is_infant, inst);
226 }
227 else
228 {
229 is_ok = gnc_sql_slots_delete (sql_be, guid);
230 }
231 }
232
233 return is_ok;
234}
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.
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.
The type used to store guids in C.
Definition guid.h:75

◆ create_tables()

void GncSqlInvoiceBackend::create_tables ( GncSqlBackend sql_be)
overridevirtual

Conditionally create or update a database table from m_col_table.

The condition is the version returned by querying the database's version table: If it's 0 then the table wasn't found and will be created; All tables areat least version 1. If the database's version is less than the compiled version then the table schema is upgraded but the data isn't, that's the engine's responsibility when the object is loaded. If the version is greater than the compiled version then nothing is touched.

Parameters
sql_beThe GncSqlBackend containing the database connection.

Reimplemented from GncSqlObjectBackend.

Definition at line 152 of file gnc-invoice-sql.cpp.

153{
154 gint version;
155
156 g_return_if_fail (sql_be != NULL);
157
158 version = sql_be->get_table_version( TABLE_NAME);
159 if (version == 0)
160 {
161 sql_be->create_table(TABLE_NAME, TABLE_VERSION, col_table);
162 }
163 else if (version < TABLE_VERSION)
164 {
165 /* Upgrade:
166 1->2: 64 bit int handling
167 2->3: invoice open date can be NULL
168 3->4: Use DATETIME instead of TIMESTAMP in MySQL
169 */
170 sql_be->upgrade_table(TABLE_NAME, col_table);
171 sql_be->set_table_version (TABLE_NAME, TABLE_VERSION);
172
173 PINFO ("Invoices table upgraded from version %d to version %d\n", version,
174 TABLE_VERSION);
175 }
176}
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.
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.
#define PINFO(format, args...)
Print an informational note.
Definition qoflog.h:256

◆ load_all()

void GncSqlInvoiceBackend::load_all ( GncSqlBackend sql_be)
overridevirtual

Load all objects of m_type in the database into memory.

Parameters
sql_beThe GncSqlBackend containing the database connection.

Implements GncSqlObjectBackend.

Definition at line 132 of file gnc-invoice-sql.cpp.

133{
134 g_return_if_fail (sql_be != NULL);
135
136 std::string sql("SELECT * FROM " TABLE_NAME);
137 auto stmt = sql_be->create_statement_from_sql(sql);
138 auto result = sql_be->execute_select_statement(stmt);
139
140 for (auto row : *result)
141 load_single_invoice (sql_be, row);
142
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);
148}
GncSqlResultPtr execute_select_statement(const GncSqlStatementPtr &stmt) const noexcept
Executes an SQL SELECT statement and returns the result rows.

◆ write()

bool GncSqlInvoiceBackend::write ( GncSqlBackend sql_be)
overridevirtual

Write all objects of m_type_name to the database.

Parameters
sql_beThe GncSqlBackend containing the database.
Returns
true if the objects were successfully written, false otherwise.

Reimplemented from GncSqlObjectBackend.

Definition at line 270 of file gnc-invoice-sql.cpp.

271{
272 g_return_val_if_fail (sql_be != NULL, FALSE);
273 write_objects_t data{sql_be, true, this};
274
275 qof_object_foreach (GNC_ID_INVOICE, sql_be->book(), write_single_invoice, &data);
276
277 return data.is_ok;
278}
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.
Data-passing struct for callbacks to qof_object_foreach() used in GncSqlObjectBackend::write().

The documentation for this class was generated from the following files: