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

Constructor & Destructor Documentation

◆ GncSqlPriceBackend()

GncSqlPriceBackend::GncSqlPriceBackend ( )

Definition at line 70 of file gnc-price-sql.cpp.

70 :
71 GncSqlObjectBackend(TABLE_VERSION, GNC_ID_PRICE,
72 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 GncSqlPriceBackend::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 161 of file gnc-price-sql.cpp.

162{
163 GNCPrice* pPrice = GNC_PRICE (inst);
164 E_DB_OPERATION op;
165 gboolean is_infant;
166 gboolean is_ok = TRUE;
167
168 g_return_val_if_fail (sql_be != NULL, FALSE);
169 g_return_val_if_fail (inst != NULL, FALSE);
170 g_return_val_if_fail (GNC_IS_PRICE (inst), FALSE);
171
172 is_infant = qof_instance_get_infant (inst);
174 {
175 op = OP_DB_DELETE;
176 }
177 else if (sql_be->pristine() || is_infant)
178 {
179 op = OP_DB_INSERT;
180 }
181 else
182 {
183 op = OP_DB_UPDATE;
184 }
185
186 if (op != OP_DB_DELETE)
187 {
188 /* Ensure commodity and currency are in the db */
189 (void)sql_be->save_commodity(gnc_price_get_commodity(pPrice));
190 is_ok = sql_be->save_commodity(gnc_price_get_currency(pPrice));
191 }
192
193 if (is_ok)
194 {
195 is_ok = sql_be->do_db_operation(op, TABLE_NAME, GNC_ID_PRICE, pPrice,
196 col_table);
197 }
198
199 return is_ok;
200}
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.
gboolean qof_instance_get_destroying(gconstpointer ptr)
Retrieve the flag that indicates whether or not this object is about to be destroyed.

◆ create_tables()

void GncSqlPriceBackend::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 134 of file gnc-price-sql.cpp.

135{
136 gint version;
137
138 g_return_if_fail (sql_be != NULL);
139
140 version = sql_be->get_table_version( TABLE_NAME);
141 if (version == 0)
142 {
143 (void)sql_be->create_table(TABLE_NAME, TABLE_VERSION, col_table);
144 }
145 else if (version < m_version)
146 {
147 /*
148 1->2: Upgrade 64 bit int handling
149 2->3: Use DATETIME instead of TIMESTAMP in MySQL
150 */
151 sql_be->upgrade_table(TABLE_NAME, col_table);
152 sql_be->set_table_version (TABLE_NAME, TABLE_VERSION);
153
154 PINFO ("Prices table upgraded from version 1 to version %d\n", TABLE_VERSION);
155 }
156}
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 GncSqlPriceBackend::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 93 of file gnc-price-sql.cpp.

94{
95 QofBook* pBook;
96 GNCPriceDB* pPriceDB;
97
98 g_return_if_fail (sql_be != NULL);
99
100 pBook = sql_be->book();
101 pPriceDB = gnc_pricedb_get_db (pBook);
102 std::string sql("SELECT * FROM " TABLE_NAME);
103 auto stmt = sql_be->create_statement_from_sql(sql);
104 if (stmt != nullptr)
105 {
106 auto result = sql_be->execute_select_statement(stmt);
107 if (result->begin() == result->end())
108 return;
109
110 GNCPrice* pPrice;
111
112 gnc_pricedb_set_bulk_update (pPriceDB, TRUE);
113 for (auto row : *result)
114 {
115 pPrice = load_single_price (sql_be, row);
116
117 if (pPrice != NULL)
118 {
119 (void)gnc_pricedb_add_price (pPriceDB, pPrice);
120 gnc_price_unref (pPrice);
121 }
122 }
123 gnc_pricedb_set_bulk_update (pPriceDB, FALSE);
124 std::string pkey(col_table[0]->name());
125 sql = "SELECT DISTINCT ";
126 sql += pkey + " FROM " TABLE_NAME;
127 gnc_sql_slots_load_for_sql_subquery (sql_be, sql,
128 (BookLookupFn)gnc_price_lookup);
129 }
130}
GncSqlResultPtr execute_select_statement(const GncSqlStatementPtr &stmt) const noexcept
Executes an SQL SELECT statement and returns the result rows.
void gnc_pricedb_set_bulk_update(GNCPriceDB *db, gboolean bulk_update)
Set flag to indicate whether duplication checks should be performed.
GNCPriceDB * gnc_pricedb_get_db(QofBook *book)
Return the pricedb associated with the book.
gboolean gnc_pricedb_add_price(GNCPriceDB *db, GNCPrice *p)
Add a price to the pricedb.
void gnc_price_unref(GNCPrice *p)
gnc_price_unref - indicate you're finished with a price (i.e.
QofBook reference.
Definition qofbook-p.hpp:47

◆ write()

bool GncSqlPriceBackend::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 219 of file gnc-price-sql.cpp.

220{
221 g_return_val_if_fail (sql_be != NULL, FALSE);
222 write_objects_t data{sql_be, true, this};
223
224 auto priceDB = gnc_pricedb_get_db (sql_be->book());
225 return gnc_pricedb_foreach_price (priceDB, write_price, &data, TRUE);
226}
gboolean gnc_pricedb_foreach_price(GNCPriceDB *db, GncPriceForeachFunc f, gpointer user_data, gboolean stable_order)
Call a GncPriceForeachFunction once for each price in db, until the function returns FALSE.
Data-passing struct for callbacks to qof_object_foreach() used in GncSqlObjectBackend::write().

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