GnuCash  5.6-150-g038405b370+
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 663 of file gnc-dbisqlconnection.cpp.

664 {
665  auto backup_tables = m_provider->get_table_list(m_conn, "%_back");
666  auto all_tables = m_provider->get_table_list(m_conn, "");
667  /* No operations on the lock table */
668  auto new_end = std::remove(all_tables.begin(), all_tables.end(), lock_table);
669  all_tables.erase(new_end, all_tables.end());
670  StrVec data_tables;
671  data_tables.reserve(all_tables.size() - backup_tables.size());
672  std::set_difference(all_tables.begin(), all_tables.end(),
673  backup_tables.begin(), backup_tables.end(),
674  std::back_inserter(data_tables));
675  switch(op)
676  {
677  case backup:
678  if (!backup_tables.empty())
679  {
680  PERR("Unable to backup database, an existing backup is present.");
682  return false;
683  }
684  for (auto table : data_tables)
685  if (!rename_table(table, table +"_back"))
686  return false; /* Error, trigger rollback. */
687  break;
688  case drop_backup:
689  for (auto table : backup_tables)
690  {
691  auto data_table = table.substr(0, table.find("_back"));
692  if (std::find(data_tables.begin(), data_tables.end(),
693  data_table) != data_tables.end())
694  drop_table(table); /* Other table exists, OK. */
695  else /* No data table, restore the backup */
696  rename_table(table, data_table);
697  }
698  break;
699  case rollback:
700  for (auto table : backup_tables)
701  {
702  auto data_table = table.substr(0, table.find("_back"));
703  if (std::find(data_tables.begin(), data_tables.end(),
704  data_table) != data_tables.end())
705  drop_table(data_table); /* Other table exists, OK. */
706  rename_table(table, data_table);
707  }
708  break;
709  case recover:
710  for (auto table : backup_tables)
711  {
712  auto data_table = table.substr(0, table.find("_back"));
713  if (std::find(data_tables.begin(), data_tables.end(),
714  data_table) != data_tables.end())
715  {
716  if (!merge_tables(data_table, table))
717  return false;
718  }
719  else
720  {
721  if (!rename_table(table, data_table))
722  return false;
723  }
724  }
725  break;
726  }
727  return true;
728 }
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 if there is a valid connection in the end or FALSE otherwise

Implements GncSqlConnection.

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

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

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