GnuCash  5.6-150-g038405b370+
Public Member Functions
GncSqlTransBackend Class Reference
Inheritance diagram for GncSqlTransBackend:
GncSqlObjectBackend

Public Member Functions

void load_all (GncSqlBackend *) override
 Loads all transactions. More...
 
void create_tables (GncSqlBackend *) override
 Creates the transaction and split tables. More...
 
bool commit (GncSqlBackend *sql_be, QofInstance *inst) override
 UPDATE/INSERT a single instance of m_type_name into the database. More...
 
- Public Member Functions inherited from GncSqlObjectBackend
 GncSqlObjectBackend (int version, const std::string &type, const std::string &table, const EntryVec &vec)
 
virtual bool write (GncSqlBackend *sql_be)
 Write all objects of m_type_name to the database. More...
 
const char * type () const noexcept
 Return the m_type_name for the class. More...
 
const bool is_version (int version) const noexcept
 Compare a version with the compiled version (m_version). More...
 
bool instance_in_db (const GncSqlBackend *sql_be, QofInstance *inst) const noexcept
 Check the presence of an object in the backend's database. More...
 

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 36 of file gnc-transaction-sql.h.

Member Function Documentation

◆ commit()

bool GncSqlTransBackend::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 582 of file gnc-transaction-sql.cpp.

583 {
584  E_DB_OPERATION op;
585  gboolean is_ok = TRUE;
586  const char* err = NULL;
587 
588  g_return_val_if_fail (sql_be != NULL, FALSE);
589  g_return_val_if_fail (inst != NULL, FALSE);
590 
591  auto pTx = GNC_TRANS(inst);
592  auto is_infant = qof_instance_get_infant (inst);
593  if (qof_instance_get_destroying (inst))
594  {
595  op = OP_DB_DELETE;
596  }
597  else if (sql_be->pristine() || is_infant)
598  {
599  op = OP_DB_INSERT;
600  }
601  else
602  {
603  op = OP_DB_UPDATE;
604  }
605 
606  if (op != OP_DB_DELETE)
607  {
608  gnc_commodity* commodity = xaccTransGetCurrency (pTx);
609  // Ensure the commodity is in the db
610  is_ok = sql_be->save_commodity(commodity);
611  if (! is_ok)
612  {
613  err = "Commodity save failed: Probably an invalid or missing currency";
615  }
616  }
617 
618  if (is_ok)
619  {
620  is_ok = sql_be->do_db_operation(op, TRANSACTION_TABLE, GNC_ID_TRANS,
621  pTx, tx_col_table);
622  if (! is_ok)
623  {
624  err = "Transaction header save failed. Check trace log for SQL errors";
625  }
626  }
627 
628  if (is_ok)
629  {
630  // Commit slots
631  auto guid = qof_instance_get_guid (inst);
632  if (!qof_instance_get_destroying (inst))
633  {
634  is_ok = gnc_sql_slots_save (sql_be, guid, is_infant, inst);
635  if (! is_ok)
636  {
637  err = "Slots save failed. Check trace log for SQL errors";
638  }
639  }
640  else
641  {
642  is_ok = gnc_sql_slots_delete (sql_be, guid);
643  if (! is_ok)
644  {
645  err = "Slots delete failed. Check trace log for SQL errors";
646  }
647  if (is_ok)
648  {
649  is_ok = delete_splits (sql_be, pTx);
650  if (! is_ok)
651  {
652  err = "Split delete failed. Check trace log for SQL errors";
653  }
654  }
655  }
656  }
657  if (! is_ok)
658  {
659  Split* split = xaccTransGetSplit (pTx, 0);
660  Account* acc = xaccSplitGetAccount (split);
661  gchar *datestr = qof_print_date (xaccTransGetDate (pTx));
662  /* FIXME: This needs to be implemented
663  const char *message1 = "Transaction %s dated %s in account %s not saved due to %s.%s";
664  const char *message2 = "\nDatabase may be corrupted, check your data carefully.";
665  qof_error_format_secondary_text( GTK_MESSAGE_DIALOG( msg ),
666  message1,
667  xaccTransGetDescription( pTx ),
668  qof_print_date( xaccTransGetDate( pTx ) ),
669  xaccAccountGetName( acc ),
670  err,
671  message2 );
672  */
673  PERR ("Transaction %s dated %s in account %s not saved due to %s.\n",
674  xaccTransGetDescription (pTx), datestr, xaccAccountGetName (acc), err);
675  g_free (datestr);
676  }
677  return is_ok;
678 }
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.
const GncGUID * qof_instance_get_guid(gconstpointer inst)
Return the GncGUID of this instance.
Split * xaccTransGetSplit(const Transaction *trans, int i)
Return a pointer to the indexed split in this transaction's split list.
time64 xaccTransGetDate(const Transaction *trans)
Retrieve the posted date of the transaction.
STRUCTS.
void qof_backend_set_error(QofBackend *qof_be, QofBackendError err)
Set the error on the specified QofBackend.
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 PERR(format, args...)
Log a serious error.
Definition: qoflog.h:244
char * qof_print_date(time64 secs)
Convenience; calls through to qof_print_date_dmy_buff().
Definition: gnc-date.cpp:608
bool save_commodity(gnc_commodity *comm) noexcept
Ensure that a commodity referenced in another object is in fact saved in the database.
const char * xaccTransGetDescription(const Transaction *trans)
Gets the transaction Description.
data in db is corrupt
Definition: qofbackend.h:70
Account * xaccSplitGetAccount(const Split *split)
Returns the account of this split, which was set through xaccAccountInsertSplit().
Definition: gmock-Split.cpp:53
gnc_commodity * xaccTransGetCurrency(const Transaction *trans)
Returns the valuation commodity of this transaction.
gboolean gnc_sql_slots_delete(GncSqlBackend *sql_be, const GncGUID *guid)
gnc_sql_slots_delete - Deletes slots for an object from the db.
const char * xaccAccountGetName(const Account *acc)
Get the account's name.
Definition: Account.cpp:3250

◆ create_tables()

void GncSqlTransBackend::create_tables ( GncSqlBackend sql_be)
overridevirtual

Creates the transaction and split tables.

Parameters
sql_beSQL backend

Reimplemented from GncSqlObjectBackend.

Definition at line 406 of file gnc-transaction-sql.cpp.

407 {
408  gint version;
409  gboolean ok;
410 
411  g_return_if_fail (sql_be != NULL);
412 
413  version = sql_be->get_table_version( m_table_name.c_str());
414  if (version == 0)
415  {
416  (void)sql_be->create_table(TRANSACTION_TABLE, TX_TABLE_VERSION,
417  tx_col_table);
418  ok = sql_be->create_index ("tx_post_date_index", TRANSACTION_TABLE,
419  post_date_col_table);
420  if (!ok)
421  {
422  PERR ("Unable to create index\n");
423  }
424  }
425  else if (version < m_version)
426  {
427  /* Upgrade:
428  1->2: 64 bit int handling
429  2->3: allow dates to be NULL
430  3->4: Use DATETIME instead of TIMESTAMP in MySQL
431  */
432  sql_be->upgrade_table(m_table_name.c_str(), tx_col_table);
433  sql_be->set_table_version (m_table_name.c_str(), m_version);
434  PINFO ("Transactions table upgraded from version %d to version %d\n",
435  version, m_version);
436  }
437 }
bool create_table(const std::string &table_name, const EntryVec &col_table) const noexcept
Creates a table in the database.
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
#define PERR(format, args...)
Log a serious error.
Definition: qoflog.h:244
void upgrade_table(const std::string &table_name, const EntryVec &col_table) noexcept
Upgrades a table to a new structure.
bool create_index(const std::string &index_name, const std::string &table_name, const EntryVec &col_table) const noexcept
Creates an index in the database.
uint_t get_table_version(const std::string &table_name) const noexcept
Returns the version number for a DB table.

◆ load_all()

void GncSqlTransBackend::load_all ( GncSqlBackend sql_be)
overridevirtual

Loads all transactions.

This might be used during a save-as operation to ensure that all data is in memory and ready to be saved.

Parameters
sql_beSQL backend

Implements GncSqlObjectBackend.

Definition at line 714 of file gnc-transaction-sql.cpp.

715 {
716  g_return_if_fail (sql_be != NULL);
717 
718  auto root = gnc_book_get_root_account (sql_be->book());
720  nullptr);
721  query_transactions (sql_be, "");
723  nullptr);
724 }
void gnc_account_foreach_descendant(const Account *acc, AccountCb thunk, gpointer user_data)
This method will traverse all children of this accounts and their descendants, calling &#39;func&#39; on each...
Definition: Account.cpp:3197
void xaccAccountBeginEdit(Account *acc)
The xaccAccountBeginEdit() subroutine is the first phase of a two-phase-commit wrapper for account up...
Definition: Account.cpp:1479
void xaccAccountCommitEdit(Account *acc)
ThexaccAccountCommitEdit() subroutine is the second phase of a two-phase-commit wrapper for account u...
Definition: Account.cpp:1520

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