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 664 of file gnc-dbisqlconnection.cpp.

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

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

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