GnuCash c935c2f+
Loading...
Searching...
No Matches
Public Member Functions
GncSqlLotsBackend Class Reference
Inheritance diagram for GncSqlLotsBackend:
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 34 of file gnc-lots-sql.h.

Constructor & Destructor Documentation

◆ GncSqlLotsBackend()

GncSqlLotsBackend::GncSqlLotsBackend ( )

Definition at line 67 of file gnc-lots-sql.cpp.

67 :
68 GncSqlObjectBackend(TABLE_VERSION, GNC_ID_LOT,
69 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 GncSqlLotsBackend::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 144 of file gnc-lots-sql.cpp.

145{
146 gint version;
147
148 g_return_if_fail (sql_be != NULL);
149
150 version = sql_be->get_table_version( TABLE_NAME);
151 if (version == 0)
152 {
153 /* The table doesn't exist, so create it */
154 (void)sql_be->create_table(TABLE_NAME, TABLE_VERSION, col_table);
155 }
156 else if (version < m_version)
157 {
158 /* Version 1 -> 2 removes the 'NOT NULL' constraint on the account_guid
159 field.
160
161 Create a temporary table, copy the data from the old table, delete the
162 old table, then rename the new one. */
163
164 sql_be->upgrade_table(TABLE_NAME, col_table);
165 sql_be->set_table_version (TABLE_NAME, TABLE_VERSION);
166
167 PINFO ("Lots table upgraded from version 1 to version %d\n", TABLE_VERSION);
168 }
169}
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 GncSqlLotsBackend::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 120 of file gnc-lots-sql.cpp.

121{
122 g_return_if_fail (sql_be != NULL);
123
124 std::stringstream sql;
125 sql << "SELECT * FROM " << TABLE_NAME;
126 auto stmt = sql_be->create_statement_from_sql(sql.str());
127 if (stmt != nullptr)
128 {
129 auto result = sql_be->execute_select_statement(stmt);
130 if (result->begin () == nullptr)
131 return;
132 for (auto row : *result)
133 load_single_lot (sql_be, row);
134
135 auto sql = g_strdup_printf ("SELECT DISTINCT guid FROM %s",
136 TABLE_NAME);
137 gnc_sql_slots_load_for_sql_subquery (sql_be, sql, (BookLookupFn)gnc_lot_lookup);
138 g_free (sql);
139 }
140}
GncSqlResultPtr execute_select_statement(const GncSqlStatementPtr &stmt) const noexcept
Executes an SQL SELECT statement and returns the result rows.

◆ write()

bool GncSqlLotsBackend::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 183 of file gnc-lots-sql.cpp.

184{
185 g_return_val_if_fail (sql_be != NULL, FALSE);
186 write_objects_t data{sql_be, true, this};
187
188 qof_collection_foreach (qof_book_get_collection (sql_be->book(), GNC_ID_LOT),
189 (QofInstanceForeachCB)do_save_lot, &data);
190 return data.is_ok;
191}
QofCollection * qof_book_get_collection(const QofBook *book, QofIdType entity_type)
Return The table of entities of the given type.
Definition qofbook.cpp:521
void(* QofInstanceForeachCB)(QofInstance *, gpointer user_data)
Callback type for qof_collection_foreach.
Definition qofid.h:146
Data-passing struct for callbacks to qof_object_foreach() used in GncSqlObjectBackend::write().

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