GnuCash  5.6-150-g038405b370+
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 658 of file gnc-slots-sql.cpp.

659 {
660  gchar* buf;
661  gchar guid_buf[GUID_ENCODING_LENGTH + 1];
662  slot_info_t slot_info = { NULL, NULL, TRUE, NULL, KvpValue::Type::INVALID,
663  NULL, FRAME, NULL, "" };
664 
665  g_return_val_if_fail (sql_be != NULL, FALSE);
666  g_return_val_if_fail (guid != NULL, FALSE);
667 
668  (void)guid_to_string_buff (guid, guid_buf);
669 
670  buf = g_strdup_printf ("SELECT * FROM %s WHERE obj_guid='%s' and slot_type in ('%d', '%d') and not guid_val is null",
671  TABLE_NAME, guid_buf, KvpValue::Type::FRAME, KvpValue::Type::GLIST);
672  auto stmt = sql_be->create_statement_from_sql(buf);
673  g_free (buf);
674  if (stmt != nullptr)
675  {
676  auto result = sql_be->execute_select_statement(stmt);
677  for (auto row : *result)
678  {
679  const GncSqlColumnTableEntryPtr table_row =
680  col_table[guid_val_col];
681  GncGUID child_guid;
682  auto val = row.get_string_at_col (table_row->name());
683  if (val && string_to_guid (val->c_str(), &child_guid))
684  gnc_sql_slots_delete (sql_be, &child_guid);
685  }
686  }
687 
688  slot_info.be = sql_be;
689  slot_info.guid = guid;
690  slot_info.is_ok = TRUE;
691  slot_info.is_ok = sql_be->do_db_operation(OP_DB_DELETE, TABLE_NAME,
692  TABLE_NAME, &slot_info,
693  obj_guid_col_table);
694 
695  return slot_info.is_ok;
696 }
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:173
#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 726 of file gnc-slots-sql.cpp.

727 {
728  slot_info_t info = { NULL, NULL, TRUE, NULL, KvpValue::Type::INVALID,
729  NULL, FRAME, NULL, "" };
730  g_return_if_fail (sql_be != NULL);
731  g_return_if_fail (inst != NULL);
732 
733  info.be = sql_be;
734  info.guid = qof_instance_get_guid (inst);
735  info.pKvpFrame = qof_instance_get_slots (inst);
736  info.context = NONE;
737 
738  slots_load_info (&info);
739 }
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 807 of file gnc-slots-sql.cpp.

810 {
811  g_return_if_fail (sql_be != NULL);
812 
813  // Ignore empty subquery
814  if (subquery.empty()) return;
815 
816  std::string pkey(obj_guid_col_table[0]->name());
817  std::string sql("SELECT * FROM " TABLE_NAME " WHERE ");
818  sql += pkey + " IN (" + subquery + ")";
819 
820  // Execute the query and load the slots
821  auto stmt = sql_be->create_statement_from_sql(sql);
822  if (stmt == nullptr)
823  {
824  PERR ("stmt == NULL, SQL = '%s'\n", sql.c_str());
825  return;
826  }
827  auto result = sql_be->execute_select_statement(stmt);
828  for (auto row : *result)
829  load_slot_for_book_object (sql_be, row, lookup_fn);
830  delete result;
831 }
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 633 of file gnc-slots-sql.cpp.

635 {
636  slot_info_t slot_info = { NULL, NULL, TRUE, NULL, KvpValue::Type::INVALID,
637  NULL, FRAME, NULL, "" };
638  KvpFrame* pFrame = qof_instance_get_slots (inst);
639 
640  g_return_val_if_fail (sql_be != NULL, FALSE);
641  g_return_val_if_fail (guid != NULL, FALSE);
642  g_return_val_if_fail (pFrame != NULL, FALSE);
643 
644  // If this is not saving into a new db, clear out the old saved slots first
645  if (!sql_be->pristine() && !is_infant)
646  {
647  (void)gnc_sql_slots_delete (sql_be, guid);
648  }
649 
650  slot_info.be = sql_be;
651  slot_info.guid = guid;
652  pFrame->for_each_slot_temp (save_slot, slot_info);
653 
654  return slot_info.is_ok;
655 }
gboolean gnc_sql_slots_delete(GncSqlBackend *sql_be, const GncGUID *guid)
gnc_sql_slots_delete - Deletes slots for an object from the db.