GnuCash  4.11-627-g58c1a51897
Public Member Functions
GncDbiSqlConnection Class Reference

Encapsulate a libdbi dbi_conn connection. More...

#include <gnc-dbisqlconnection.hpp>

Inheritance diagram for GncDbiSqlConnection:
GncSqlConnection

Public Member Functions

 GncDbiSqlConnection (DbType type, QofBackend *qbe, dbi_conn conn, SessionOpenMode mode)
 
GncSqlResultPtr execute_select_statement (const GncSqlStatementPtr &) noexcept override
 
int execute_nonselect_statement (const GncSqlStatementPtr &) noexcept override
 Returns false if error.
 
GncSqlStatementPtr create_statement_from_sql (const std::string &) const noexcept override
 
bool does_table_exist (const std::string &) const noexcept override
 Returns true if successful.
 
bool begin_transaction () noexcept override
 Returns TRUE if successful, false if error.
 
bool rollback_transaction () noexcept override
 Returns TRUE if successful, FALSE if error.
 
bool commit_transaction () noexcept override
 Returns TRUE if successful, FALSE if error.
 
bool create_table (const std::string &, const ColVec &) const noexcept override
 Returns TRUE if successful, FALSE if error.
 
bool create_index (const std::string &, const std::string &, const EntryVec &) const noexcept override
 Returns TRUE if successful, FALSE if error.
 
bool add_columns_to_table (const std::string &, const ColVec &) const noexcept override
 Returns TRUE if successful, FALSE if error.
 
std::string quote_string (const std::string &) const noexcept override
 
int dberror () const noexcept override
 Get the connection error value. More...
 
QofBackendqbe () const noexcept
 
dbi_conn conn () const noexcept
 
void set_error (QofBackendError error, unsigned int repeat, bool retry) noexcept override
 
void init_error () noexcept
 
bool verify () noexcept override
 Check if the dbi connection is valid. More...
 
bool retry_connection (const char *msg) noexcept override
 
bool table_operation (TableOpType op) noexcept
 Perform a specified SQL operation on every table in a database. More...
 
std::string add_columns_ddl (const std::string &table_name, const ColVec &info_vec) const noexcept
 
bool drop_indexes () noexcept
 
- Public Member Functions inherited from GncSqlConnection
virtual ~GncSqlConnection ()=default
 Returns NULL if error.
 

Detailed Description

Encapsulate a libdbi dbi_conn connection.

Definition at line 41 of file gnc-dbisqlconnection.hpp.

Member Function Documentation

◆ dberror()

int GncDbiSqlConnection::dberror ( ) const
inlineoverridevirtualnoexcept

Get the connection error value.

If not 0 will normally be meaningless outside of implementation code.

Implements GncSqlConnection.

Definition at line 63 of file gnc-dbisqlconnection.hpp.

63  {
64  return dbi_conn_error(m_conn, nullptr); }

◆ table_operation()

bool GncDbiSqlConnection::table_operation ( TableOpType  op)
noexcept

Perform a specified SQL operation on every table in a database.

Possible operations are:

  • drop: to DROP all tables from the database
  • empty: to DELETE all records from each table in the database.
  • backup: Rename every table from "name" to "name_back"
  • drop_backup: DROP the backup tables.
  • rollback: DROP the new table "name" and rename "name_back" to "name", restoring the database to its previous state.

The intent of the last two is to be able to move an existing table aside, query its contents with a transformation (in 2.4.x this is already done as the contents are loaded completely when a Qof session is started), save them to a new table according to a new database format, and finally drop the backup table; if there's an error during the process, rollback allows returning the table to its original state.

Parameters
sql_connThe sql connection (via dbi) to which the transactions will be sent
table_namessStrVec of tables to operate on.
opThe operation to perform.
Returns
Success (TRUE) or failure.

Definition at line 668 of file gnc-dbisqlconnection.cpp.

669 {
670  auto backup_tables = m_provider->get_table_list(m_conn, "%_back");
671  auto all_tables = m_provider->get_table_list(m_conn, "");
672  /* No operations on the lock table */
673  auto new_end = std::remove(all_tables.begin(), all_tables.end(), lock_table);
674  all_tables.erase(new_end, all_tables.end());
675  StrVec data_tables;
676  data_tables.reserve(all_tables.size() - backup_tables.size());
677  std::set_difference(all_tables.begin(), all_tables.end(),
678  backup_tables.begin(), backup_tables.end(),
679  std::back_inserter(data_tables));
680  switch(op)
681  {
682  case backup:
683  if (!backup_tables.empty())
684  {
685  PERR("Unable to backup database, an existing backup is present.");
687  return false;
688  }
689  for (auto table : data_tables)
690  if (!rename_table(table, table +"_back"))
691  return false; /* Error, trigger rollback. */
692  break;
693  case drop_backup:
694  for (auto table : backup_tables)
695  {
696  auto data_table = table.substr(0, table.find("_back"));
697  if (std::find(data_tables.begin(), data_tables.end(),
698  data_table) != data_tables.end())
699  drop_table(table); /* Other table exists, OK. */
700  else /* No data table, restore the backup */
701  rename_table(table, data_table);
702  }
703  break;
704  case rollback:
705  for (auto table : backup_tables)
706  {
707  auto data_table = table.substr(0, table.find("_back"));
708  if (std::find(data_tables.begin(), data_tables.end(),
709  data_table) != data_tables.end())
710  drop_table(data_table); /* Other table exists, OK. */
711  rename_table(table, data_table);
712  }
713  break;
714  case recover:
715  for (auto table : backup_tables)
716  {
717  auto data_table = table.substr(0, table.find("_back"));
718  if (std::find(data_tables.begin(), data_tables.end(),
719  data_table) != data_tables.end())
720  {
721  if (!merge_tables(data_table, table))
722  return false;
723  }
724  else
725  {
726  if (!rename_table(table, data_table))
727  return false;
728  }
729  }
730  break;
731  }
732  return true;
733 }
void qof_backend_set_error(QofBackend *qof_be, QofBackendError err)
Set the error on the specified QofBackend.
#define PERR(format, args...)
Log a serious error.
Definition: qoflog.h:244
data in db is corrupt
Definition: qofbackend.h:70

◆ verify()

bool GncDbiSqlConnection::verify ( )
overridevirtualnoexcept

Check if the dbi connection is valid.

If not attempt to re-establish it Returns TRUE is there is a valid connection in the end or FALSE otherwise

Implements GncSqlConnection.

Definition at line 562 of file gnc-dbisqlconnection.cpp.

563 {
564  if (m_conn_ok)
565  return true;
566 
567  /* We attempt to connect only once here. The error function will
568  * automatically re-attempt up until DBI_MAX_CONN_ATTEMPTS time to connect
569  * if this call fails. After all these attempts, conn_ok will indicate if
570  * there is a valid connection or not.
571  */
572  init_error ();
573  m_conn_ok = true;
574  (void)dbi_conn_connect (m_conn);
575 
576  return m_conn_ok;
577 }

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