GnuCash  4.12-404-geb24099a91
Data Structures | Typedefs | Functions
gnc-slots-sql.h File Reference

load and save accounts data to SQL More...

#include <glib.h>
#include "guid.h"
#include "qof.h"
#include "gnc-sql-object-backend.hpp"

Go to the source code of this file.

Data Structures

class  GncSqlSlotsBackend
 Slots are neither loadable nor committable. More...
 

Typedefs

typedef QofInstance *(* BookLookupFn) (const GncGUID *guid, const QofBook *book)
 

Functions

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. More...
 
gboolean gnc_sql_slots_delete (GncSqlBackend *sql_be, const GncGUID *guid)
 gnc_sql_slots_delete - Deletes slots for an object from the db. More...
 
void gnc_sql_slots_load (GncSqlBackend *sql_be, QofInstance *inst)
 Loads slots for an object from the db. More...
 
void gnc_sql_slots_load_for_sql_subquery (GncSqlBackend *sql_be, const std::string subquery, BookLookupFn lookup_fn)
 gnc_sql_slots_load_for_sql_subquery - Loads slots for all objects whose guid is supplied by a subquery. More...
 
void gnc_sql_init_slots_handler (void)
 

Detailed Description

load and save accounts data to SQL

Author
Copyright (c) 2006-2008 Phil Longstaff plong.nosp@m.staf.nosp@m.f@rog.nosp@m.ers..nosp@m.com

This file implements the top-level QofBackend API for saving/ restoring data to/from an SQL database

Definition in file gnc-slots-sql.h.

Function Documentation

◆ gnc_sql_slots_delete()

gboolean gnc_sql_slots_delete ( GncSqlBackend sql_be,
const GncGUID guid 
)

gnc_sql_slots_delete - Deletes slots for an object from the db.

Parameters
sql_beSQL backend
guidObject guid
Returns
TRUE if successful, FALSE if error

Definition at line 675 of file gnc-slots-sql.cpp.

676 {
677  gchar* buf;
678  gchar guid_buf[GUID_ENCODING_LENGTH + 1];
679  slot_info_t slot_info = { NULL, NULL, TRUE, NULL, KvpValue::Type::INVALID,
680  NULL, FRAME, NULL, "" };
681 
682  g_return_val_if_fail (sql_be != NULL, FALSE);
683  g_return_val_if_fail (guid != NULL, FALSE);
684 
685  (void)guid_to_string_buff (guid, guid_buf);
686 
687  buf = g_strdup_printf ("SELECT * FROM %s WHERE obj_guid='%s' and slot_type in ('%d', '%d') and not guid_val is null",
688  TABLE_NAME, guid_buf, KvpValue::Type::FRAME, KvpValue::Type::GLIST);
689  auto stmt = sql_be->create_statement_from_sql(buf);
690  g_free (buf);
691  if (stmt != nullptr)
692  {
693  auto result = sql_be->execute_select_statement(stmt);
694  for (auto row : *result)
695  {
696  try
697  {
698  const GncSqlColumnTableEntryPtr table_row =
699  col_table[guid_val_col];
700  GncGUID child_guid;
701  auto val = row.get_string_at_col (table_row->name());
702  if (string_to_guid (val.c_str(), &child_guid))
703  gnc_sql_slots_delete (sql_be, &child_guid);
704  }
705  catch (std::invalid_argument&)
706  {
707  continue;
708  }
709  }
710  }
711 
712  slot_info.be = sql_be;
713  slot_info.guid = guid;
714  slot_info.is_ok = TRUE;
715  slot_info.is_ok = sql_be->do_db_operation(OP_DB_DELETE, TABLE_NAME,
716  TABLE_NAME, &slot_info,
717  obj_guid_col_table);
718 
719  return slot_info.is_ok;
720 }
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.
GncSqlResultPtr execute_select_statement(const GncSqlStatementPtr &stmt) const noexcept
Executes an SQL SELECT statement and returns the result rows.
gboolean string_to_guid(const gchar *string, GncGUID *guid)
Given a string, replace the given guid with the parsed one unless the given value is null...
gchar * guid_to_string_buff(const GncGUID *guid, gchar *str)
The guid_to_string_buff() routine puts a null-terminated string encoding of the id into the memory po...
Definition: guid.cpp:174
#define GUID_ENCODING_LENGTH
Number of characters needed to encode a guid as a string not including the null terminator.
Definition: guid.h:84
gboolean gnc_sql_slots_delete(GncSqlBackend *sql_be, const GncGUID *guid)
gnc_sql_slots_delete - Deletes slots for an object from the db.
The type used to store guids in C.
Definition: guid.h:75

◆ gnc_sql_slots_load()

void gnc_sql_slots_load ( GncSqlBackend sql_be,
QofInstance inst 
)

Loads slots for an object from the db.

Parameters
sql_beSQL backend

Definition at line 750 of file gnc-slots-sql.cpp.

751 {
752  slot_info_t info = { NULL, NULL, TRUE, NULL, KvpValue::Type::INVALID,
753  NULL, FRAME, NULL, "" };
754  g_return_if_fail (sql_be != NULL);
755  g_return_if_fail (inst != NULL);
756 
757  info.be = sql_be;
758  info.guid = qof_instance_get_guid (inst);
759  info.pKvpFrame = qof_instance_get_slots (inst);
760  info.context = NONE;
761 
762  slots_load_info (&info);
763 }
const GncGUID * qof_instance_get_guid(gconstpointer inst)
Return the GncGUID of this instance.

◆ gnc_sql_slots_load_for_sql_subquery()

void gnc_sql_slots_load_for_sql_subquery ( GncSqlBackend sql_be,
const std::string  subquery,
BookLookupFn  lookup_fn 
)

gnc_sql_slots_load_for_sql_subquery - Loads slots for all objects whose guid is supplied by a subquery.

The subquery should be of the form "SELECT DISTINCT guid FROM ...". This is faster than loading for one object at a time because fewer SQL queries * are used.

Parameters
sql_beSQL backend
subquerySubquery SQL string
lookup_fnLookup function to get the right object from the book

The subquery should be of the form "SELECT DISTINCT guid FROM ...". This is faster than loading for one object at a time because fewer SQL queries * are used.

Parameters
sql_beSQL backend
subquerySubquery SQL string
lookup_fnLookup function

Definition at line 856 of file gnc-slots-sql.cpp.

859 {
860  g_return_if_fail (sql_be != NULL);
861 
862  // Ignore empty subquery
863  if (subquery.empty()) return;
864 
865  std::string pkey(obj_guid_col_table[0]->name());
866  std::string sql("SELECT * FROM " TABLE_NAME " WHERE ");
867  sql += pkey + " IN (" + subquery + ")";
868 
869  // Execute the query and load the slots
870  auto stmt = sql_be->create_statement_from_sql(sql);
871  if (stmt == nullptr)
872  {
873  PERR ("stmt == NULL, SQL = '%s'\n", sql.c_str());
874  return;
875  }
876  auto result = sql_be->execute_select_statement(stmt);
877  for (auto row : *result)
878  load_slot_for_book_object (sql_be, row, lookup_fn);
879  delete result;
880 }
GncSqlResultPtr execute_select_statement(const GncSqlStatementPtr &stmt) const noexcept
Executes an SQL SELECT statement and returns the result rows.
#define PERR(format, args...)
Log a serious error.
Definition: qoflog.h:244

◆ gnc_sql_slots_save()

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.

Parameters
sql_beSQL backend
guidObject guid
is_infantIs this an infant object?
instThe QodInstance owning the slots.
Returns
TRUE if successful, FALSE if error

Definition at line 650 of file gnc-slots-sql.cpp.

652 {
653  slot_info_t slot_info = { NULL, NULL, TRUE, NULL, KvpValue::Type::INVALID,
654  NULL, FRAME, NULL, "" };
655  KvpFrame* pFrame = qof_instance_get_slots (inst);
656 
657  g_return_val_if_fail (sql_be != NULL, FALSE);
658  g_return_val_if_fail (guid != NULL, FALSE);
659  g_return_val_if_fail (pFrame != NULL, FALSE);
660 
661  // If this is not saving into a new db, clear out the old saved slots first
662  if (!sql_be->pristine() && !is_infant)
663  {
664  (void)gnc_sql_slots_delete (sql_be, guid);
665  }
666 
667  slot_info.be = sql_be;
668  slot_info.guid = guid;
669  pFrame->for_each_slot_temp (save_slot, slot_info);
670 
671  return slot_info.is_ok;
672 }
gboolean gnc_sql_slots_delete(GncSqlBackend *sql_be, const GncGUID *guid)
gnc_sql_slots_delete - Deletes slots for an object from the db.