GnuCash c935c2f+
Loading...
Searching...
No Matches
Public Member Functions
GncSqlEntryBackend Class Reference
Inheritance diagram for GncSqlEntryBackend:
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 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)
 
virtual bool commit (GncSqlBackend *sql_be, QofInstance *inst)
 UPDATE/INSERT a single instance of m_type_name into the database.
 
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-entry-sql.h.

Constructor & Destructor Documentation

◆ GncSqlEntryBackend()

GncSqlEntryBackend::GncSqlEntryBackend ( )

Definition at line 129 of file gnc-entry-sql.cpp.

129 :
130 GncSqlObjectBackend(TABLE_VERSION, GNC_ID_ENTRY,
131 TABLE_NAME, col_table) {}
Encapsulates per-class table schema with functions to load, create a table, commit a changed front-en...

Member Function Documentation

◆ create_tables()

void GncSqlEntryBackend::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 215 of file gnc-entry-sql.cpp.

216{
217 gint version;
218
219 g_return_if_fail (sql_be != NULL);
220
221 version = sql_be->get_table_version( TABLE_NAME);
222 if (version == 0)
223 {
224 sql_be->create_table(TABLE_NAME, TABLE_VERSION, col_table);
225 }
226 else if (version < TABLE_VERSION)
227 {
228 /* Upgrade:
229 1->2: 64 bit int handling
230 2->3: "entered" -> "date_entered", and it can be NULL
231 3->4: Use DATETIME instead of TIMESTAMP in MySQL
232 */
233 sql_be->upgrade_table(TABLE_NAME, col_table);
234 sql_be->set_table_version (TABLE_NAME, TABLE_VERSION);
235
236 PINFO ("Entries table upgraded from version %d to version %d\n", version,
237 TABLE_VERSION);
238 }
239}
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 GncSqlEntryBackend::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 195 of file gnc-entry-sql.cpp.

196{
197 g_return_if_fail (sql_be != NULL);
198
199 std::string sql("SELECT * FROM " TABLE_NAME);
200 auto stmt = sql_be->create_statement_from_sql(sql);
201 auto result = sql_be->execute_select_statement(stmt);
202
203 for (auto row : *result)
204 load_single_entry (sql_be, row);
205
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);
211}
GncSqlResultPtr execute_select_statement(const GncSqlStatementPtr &stmt) const noexcept
Executes an SQL SELECT statement and returns the result rows.

◆ write()

bool GncSqlEntryBackend::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 262 of file gnc-entry-sql.cpp.

263{
264 g_return_val_if_fail (sql_be != NULL, FALSE);
265 write_objects_t data{sql_be, true, this};
266
267 qof_object_foreach (GNC_ID_ENTRY, sql_be->book(), write_single_entry, &data);
268
269 return data.is_ok;
270}
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: