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 585 of file gnc-transaction-sql.cpp.

586 {
587  E_DB_OPERATION op;
588  gboolean is_ok = TRUE;
589  const char* err = NULL;
590 
591  g_return_val_if_fail (sql_be != NULL, FALSE);
592  g_return_val_if_fail (inst != NULL, FALSE);
593 
594  auto pTx = GNC_TRANS(inst);
595  auto is_infant = qof_instance_get_infant (inst);
596  if (qof_instance_get_destroying (inst))
597  {
598  op = OP_DB_DELETE;
599  }
600  else if (sql_be->pristine() || is_infant)
601  {
602  op = OP_DB_INSERT;
603  }
604  else
605  {
606  op = OP_DB_UPDATE;
607  }
608 
609  if (op != OP_DB_DELETE)
610  {
611  gnc_commodity* commodity = xaccTransGetCurrency (pTx);
612  // Ensure the commodity is in the db
613  is_ok = sql_be->save_commodity(commodity);
614  if (! is_ok)
615  {
616  err = "Commodity save failed: Probably an invalid or missing currency";
618  }
619  }
620 
621  if (is_ok)
622  {
623  is_ok = sql_be->do_db_operation(op, TRANSACTION_TABLE, GNC_ID_TRANS,
624  pTx, tx_col_table);
625  if (! is_ok)
626  {
627  err = "Transaction header save failed. Check trace log for SQL errors";
628  }
629  }
630 
631  if (is_ok)
632  {
633  // Commit slots
634  auto guid = qof_instance_get_guid (inst);
635  if (!qof_instance_get_destroying (inst))
636  {
637  is_ok = gnc_sql_slots_save (sql_be, guid, is_infant, inst);
638  if (! is_ok)
639  {
640  err = "Slots save failed. Check trace log for SQL errors";
641  }
642  }
643  else
644  {
645  is_ok = gnc_sql_slots_delete (sql_be, guid);
646  if (! is_ok)
647  {
648  err = "Slots delete failed. Check trace log for SQL errors";
649  }
650  if (is_ok)
651  {
652  is_ok = delete_splits (sql_be, pTx);
653  if (! is_ok)
654  {
655  err = "Split delete failed. Check trace log for SQL errors";
656  }
657  }
658  }
659  }
660  if (! is_ok)
661  {
662  Split* split = xaccTransGetSplit (pTx, 0);
663  Account* acc = xaccSplitGetAccount (split);
664  gchar *datestr = qof_print_date (xaccTransGetDate (pTx));
665  /* FIXME: This needs to be implemented
666  const char *message1 = "Transaction %s dated %s in account %s not saved due to %s.%s";
667  const char *message2 = "\nDatabase may be corrupted, check your data carefully.";
668  qof_error_format_secondary_text( GTK_MESSAGE_DIALOG( msg ),
669  message1,
670  xaccTransGetDescription( pTx ),
671  qof_print_date( xaccTransGetDate( pTx ) ),
672  xaccAccountGetName( acc ),
673  err,
674  message2 );
675  */
676  PERR ("Transaction %s dated %s in account %s not saved due to %s.\n",
677  xaccTransGetDescription (pTx), datestr, xaccAccountGetName (acc), err);
678  g_free (datestr);
679  }
680  return is_ok;
681 }
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:609
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:3239

◆ 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 409 of file gnc-transaction-sql.cpp.

410 {
411  gint version;
412  gboolean ok;
413 
414  g_return_if_fail (sql_be != NULL);
415 
416  version = sql_be->get_table_version( m_table_name.c_str());
417  if (version == 0)
418  {
419  (void)sql_be->create_table(TRANSACTION_TABLE, TX_TABLE_VERSION,
420  tx_col_table);
421  ok = sql_be->create_index ("tx_post_date_index", TRANSACTION_TABLE,
422  post_date_col_table);
423  if (!ok)
424  {
425  PERR ("Unable to create index\n");
426  }
427  }
428  else if (version < m_version)
429  {
430  /* Upgrade:
431  1->2: 64 bit int handling
432  2->3: allow dates to be NULL
433  3->4: Use DATETIME instead of TIMESTAMP in MySQL
434  */
435  sql_be->upgrade_table(m_table_name.c_str(), tx_col_table);
436  sql_be->set_table_version (m_table_name.c_str(), m_version);
437  PINFO ("Transactions table upgraded from version %d to version %d\n",
438  version, m_version);
439  }
440 }
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 717 of file gnc-transaction-sql.cpp.

718 {
719  g_return_if_fail (sql_be != NULL);
720 
721  auto root = gnc_book_get_root_account (sql_be->book());
722  gnc_account_foreach_descendant(root, (AccountCb)xaccAccountBeginEdit,
723  nullptr);
724  query_transactions (sql_be, "");
725  gnc_account_foreach_descendant(root, (AccountCb)xaccAccountCommitEdit,
726  nullptr);
727 }
void xaccAccountBeginEdit(Account *acc)
The xaccAccountBeginEdit() subroutine is the first phase of a two-phase-commit wrapper for account up...
Definition: Account.cpp:1475
void xaccAccountCommitEdit(Account *acc)
ThexaccAccountCommitEdit() subroutine is the second phase of a two-phase-commit wrapper for account u...
Definition: Account.cpp:1516

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