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...
 
QofBackend * qbe () 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 678 of file gnc-dbisqlconnection.cpp.

679 {
680  auto backup_tables = m_provider->get_table_list(m_conn, "%_back");
681  auto all_tables = m_provider->get_table_list(m_conn, "");
682  /* No operations on the lock table */
683  auto new_end = std::remove(all_tables.begin(), all_tables.end(), lock_table);
684  all_tables.erase(new_end, all_tables.end());
685  StrVec data_tables;
686  data_tables.reserve(all_tables.size() - backup_tables.size());
687  std::set_difference(all_tables.begin(), all_tables.end(),
688  backup_tables.begin(), backup_tables.end(),
689  std::back_inserter(data_tables));
690  switch(op)
691  {
692  case backup:
693  if (!backup_tables.empty())
694  {
695  PERR("Unable to backup database, an existing backup is present.");
697  return false;
698  }
699  for (auto table : data_tables)
700  if (!rename_table(table, table +"_back"))
701  return false; /* Error, trigger rollback. */
702  break;
703  case drop_backup:
704  for (auto table : backup_tables)
705  {
706  auto data_table = table.substr(0, table.find("_back"));
707  if (std::find(data_tables.begin(), data_tables.end(),
708  data_table) != data_tables.end())
709  drop_table(table); /* Other table exists, OK. */
710  else /* No data table, restore the backup */
711  rename_table(table, data_table);
712  }
713  break;
714  case rollback:
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  drop_table(data_table); /* Other table exists, OK. */
721  rename_table(table, data_table);
722  }
723  break;
724  case recover:
725  for (auto table : backup_tables)
726  {
727  auto data_table = table.substr(0, table.find("_back"));
728  if (std::find(data_tables.begin(), data_tables.end(),
729  data_table) != data_tables.end())
730  {
731  if (!merge_tables(data_table, table))
732  return false;
733  }
734  else
735  {
736  if (!rename_table(table, data_table))
737  return false;
738  }
739  }
740  break;
741  }
742  return true;
743 }
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 565 of file gnc-dbisqlconnection.cpp.

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

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