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 659 of file gnc-slots-sql.cpp.

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

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

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

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