qofsession.c File Reference


Detailed Description

Encapsulate a connection to a storage backend.

HISTORY: Created by Linas Vepstas December 1998

Author:
Copyright (c) 1998-2004 Linas Vepstas <linas@linas.org>

Copyright (c) 2000 Dave Peticolas

Copyright (c) 2005-2006 Neil Williams <linux@codehelp.co.uk>

Definition in file qofsession.c.

#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <glib.h>
#include <libintl.h>
#include "qof.h"
#include "qoferror-p.h"
#include "qofbackend-p.h"
#include "qofbook-p.h"
#include "qofsession-p.h"
#include "qofobject-p.h"

Go to the source code of this file.

Defines

#define _(String)   dgettext (GETTEXT_PACKAGE, String)

Typedefs

typedef struct qof_entity_copy_data QofEntityCopyData

Functions

void qof_backend_register_provider (QofBackendProvider *prov)
void qof_session_add_close_hook (GFunc fn, gpointer data)
void qof_session_call_close_hooks (QofSession *session)
static void qof_session_init (QofSession *session)
QofSession * qof_session_new (void)
QofBookqof_session_get_book (QofSession *session)
void qof_session_add_book (QofSession *session, QofBook *addbook)
QofBackendqof_session_get_backend (QofSession *session)
const gchar * qof_session_get_file_path (QofSession *session)
const gchar * qof_session_get_url (QofSession *session)
static void qof_book_set_partial (QofBook *book)
void qof_session_update_reference_list (QofSession *session, QofEntityReference *reference)
 Adds a new reference to the partial book data hash.
static void qof_entity_param_cb (QofParam *param, gpointer data)
static void col_ref_cb (QofEntity *ref_ent, gpointer user_data)
static void qof_entity_foreach_copy (gpointer data, gpointer user_data)
static gboolean qof_entity_guid_match (QofSession *new_session, QofEntity *original)
static void qof_entity_list_foreach (gpointer data, gpointer user_data)
static void qof_entity_coll_foreach (QofEntity *original, gpointer user_data)
static void qof_entity_coll_copy (QofEntity *original, gpointer user_data)
gboolean qof_entity_copy_to_session (QofSession *new_session, QofEntity *original)
 Copy a single QofEntity to another session.
gboolean qof_entity_copy_list (QofSession *new_session, GList *entity_list)
 Copy a GList of entities to another session.
gboolean qof_entity_copy_coll (QofSession *new_session, QofCollection *entity_coll)
 Copy a QofCollection of entities.
static void recurse_collection_cb (QofEntity *ent, gpointer user_data)
static void recurse_ent_cb (QofEntity *ent, gpointer user_data)
gboolean qof_entity_copy_coll_r (QofSession *new_session, QofCollection *coll)
 Recursively copy a collection of entities to a session.
gboolean qof_entity_copy_one_r (QofSession *new_session, QofEntity *ent)
 Recursively copy a single entity to a new session.
static void qof_session_load_backend (QofSession *session, gchar *access_method)
static void qof_session_destroy_backend (QofSession *session)
void qof_session_begin (QofSession *session, const gchar *book_id, gboolean ignore_lock, gboolean create_if_nonexistent)
void qof_session_load (QofSession *session, QofPercentageFunc percentage_func)
gboolean qof_session_save_may_clobber_data (QofSession *session)
void qof_session_save (QofSession *session, QofPercentageFunc percentage_func)
void qof_session_end (QofSession *session)
void qof_session_destroy (QofSession *session)
void qof_session_swap_data (QofSession *session_1, QofSession *session_2)
gboolean qof_session_events_pending (QofSession *session)
gboolean qof_session_process_events (QofSession *session)

Variables

static GHookList * session_closed_hooks = NULL
static QofLogModule log_module = "qof-session"
static GSList * provider_list = NULL
struct backend_providers backend_list []


Function Documentation

static void qof_entity_foreach_copy ( gpointer  data,
gpointer  user_data 
) [static]

< secondary collections are used for one-to-many references between entities and are implemented using QofCollection. These are NOT the same as the main collections in the QofBook.

  1. Each QofCollection contains one or many entities - *all* of a single type.
  2. The entity type within the collection can be determined at run time.
  3. Easy conversions to GList or whatever in the param_setfcn handler.
  4. Each parameter can have its own collection.
  5. Each entity can have a different *type* of collection to its siblings, provided that it is acceptable to the set function.
  6. Each object decides which types are acceptable for which parameter in the set functions. This is then part of the API for that object.

QOF_TYPE_COLLECT has two functions, both related to one-to-many links:

If the set function can handle it, it could also be used for true one-to-many links: one object linked to many entities of many types.

n.b. Always subject to each collection holding only one type at runtime. (otherwise use books).

Definition at line 299 of file qofsession.c.

00300 {
00301     QofEntity *importEnt, *targetEnt /*, *referenceEnt */ ;
00302     QofEntityCopyData *context;
00303     QofEntityReference *reference;
00304     gboolean registered_type;
00305     /* cm_ prefix used for variables that hold the data to commit */
00306     QofParam *cm_param;
00307     gchar *cm_string, *cm_char;
00308     const GUID *cm_guid;
00309     KvpFrame *cm_kvp;
00310     QofCollection *cm_col;
00311     /* function pointers and variables for parameter getters that don't use pointers normally */
00312     QofNumeric cm_numeric, (*numeric_getter) (QofEntity *, QofParam *);
00313     gdouble cm_double, (*double_getter) (QofEntity *, QofParam *);
00314     gboolean cm_boolean, (*boolean_getter) (QofEntity *, QofParam *);
00315     gint32 cm_i32, (*int32_getter) (QofEntity *, QofParam *);
00316     gint64 cm_i64, (*int64_getter) (QofEntity *, QofParam *);
00317     /* function pointers to the parameter setters */
00318     void (*string_setter) (QofEntity *, const gchar *);
00319     void (*numeric_setter) (QofEntity *, QofNumeric);
00320     void (*guid_setter) (QofEntity *, const GUID *);
00321     void (*double_setter) (QofEntity *, gdouble);
00322     void (*boolean_setter) (QofEntity *, gboolean);
00323     void (*i32_setter) (QofEntity *, gint32);
00324     void (*i64_setter) (QofEntity *, gint64);
00325     void (*char_setter) (QofEntity *, gchar *);
00326     void (*kvp_frame_setter) (QofEntity *, KvpFrame *);
00327 
00328     g_return_if_fail (user_data != NULL);
00329     context = (QofEntityCopyData *) user_data;
00330     importEnt = context->from;
00331     targetEnt = context->to;
00332     registered_type = FALSE;
00333     cm_param = (QofParam *) data;
00334     g_return_if_fail (cm_param != NULL);
00335     context->param = cm_param;
00336     if (safe_strcmp (cm_param->param_type, QOF_TYPE_STRING) == 0)
00337     {
00338         cm_string = (gchar *) cm_param->param_getfcn (importEnt, cm_param);
00339         if (cm_string)
00340         {
00341             string_setter =
00342                 (void (*)(QofEntity *,
00343                     const char *)) cm_param->param_setfcn;
00344             if (string_setter != NULL)
00345             {
00346                 qof_util_param_edit ((QofInstance *) targetEnt, cm_param);
00347                 string_setter (targetEnt, cm_string);
00348                 qof_util_param_commit ((QofInstance *) targetEnt, cm_param);
00349             }
00350         }
00351         registered_type = TRUE;
00352     }
00353     if (safe_strcmp (cm_param->param_type, QOF_TYPE_TIME) == 0)
00354     {
00355         QofTime *qt;
00356         void (*time_setter) (QofEntity *, QofTime *);
00357 
00358         qt = cm_param->param_getfcn (importEnt, cm_param);
00359         time_setter = 
00360             (void (*)(QofEntity *, QofTime*))cm_param->param_setfcn;
00361         if (time_setter != NULL)
00362         {
00363             qof_util_param_edit ((QofInstance *) targetEnt, cm_param);
00364             time_setter (targetEnt, qt);
00365             qof_util_param_commit ((QofInstance *) targetEnt, cm_param);
00366         }
00367         registered_type = TRUE;
00368     }
00369 #ifndef QOF_DISABLE_DEPRECATED
00370     if (safe_strcmp (cm_param->param_type, QOF_TYPE_DATE) == 0)
00371     {
00372         Timespec cm_date, (*date_getter) (QofEntity *, QofParam *);
00373         void (*date_setter) (QofEntity *, Timespec);
00374 
00375         cm_date.tv_nsec = 0;
00376         cm_date.tv_sec = 0;
00377         date_getter =
00378             (Timespec (*)(QofEntity *, QofParam *)) cm_param->param_getfcn;
00379         cm_date = date_getter (importEnt, cm_param);
00380         date_setter =
00381             (void (*)(QofEntity *, Timespec)) cm_param->param_setfcn;
00382         if (date_setter != NULL)
00383         {
00384             qof_util_param_edit ((QofInstance *) targetEnt, cm_param);
00385             date_setter (targetEnt, cm_date);
00386             qof_util_param_commit ((QofInstance *) targetEnt, cm_param);
00387         }
00388         registered_type = TRUE;
00389     }
00390 #endif
00391     if ((safe_strcmp (cm_param->param_type, QOF_TYPE_NUMERIC) == 0) ||
00392         (safe_strcmp (cm_param->param_type, QOF_TYPE_DEBCRED) == 0))
00393     {
00394         numeric_getter =
00395             (QofNumeric (*)(QofEntity *,
00396                 QofParam *)) cm_param->param_getfcn;
00397         cm_numeric = numeric_getter (importEnt, cm_param);
00398         numeric_setter =
00399             (void (*)(QofEntity *, QofNumeric)) cm_param->param_setfcn;
00400         if (numeric_setter != NULL)
00401         {
00402             qof_util_param_edit ((QofInstance *) targetEnt, cm_param);
00403             numeric_setter (targetEnt, cm_numeric);
00404             qof_util_param_commit ((QofInstance *) targetEnt, cm_param);
00405         }
00406         registered_type = TRUE;
00407     }
00408     if (safe_strcmp (cm_param->param_type, QOF_TYPE_GUID) == 0)
00409     {
00410         cm_guid =
00411             (const GUID *) cm_param->param_getfcn (importEnt, cm_param);
00412         guid_setter =
00413             (void (*)(QofEntity *, const GUID *)) cm_param->param_setfcn;
00414         if (guid_setter != NULL)
00415         {
00416             qof_util_param_edit ((QofInstance *) targetEnt, cm_param);
00417             guid_setter (targetEnt, cm_guid);
00418             qof_util_param_commit ((QofInstance *) targetEnt, cm_param);
00419         }
00420         registered_type = TRUE;
00421     }
00422     if (safe_strcmp (cm_param->param_type, QOF_TYPE_INT32) == 0)
00423     {
00424         int32_getter =
00425             (gint32 (*)(QofEntity *, QofParam *)) cm_param->param_getfcn;
00426         cm_i32 = int32_getter (importEnt, cm_param);
00427         i32_setter =
00428             (void (*)(QofEntity *, gint32)) cm_param->param_setfcn;
00429         if (i32_setter != NULL)
00430         {
00431             qof_util_param_edit ((QofInstance *) targetEnt, cm_param);
00432             i32_setter (targetEnt, cm_i32);
00433             qof_util_param_commit ((QofInstance *) targetEnt, cm_param);
00434         }
00435         registered_type = TRUE;
00436     }
00437     if (safe_strcmp (cm_param->param_type, QOF_TYPE_INT64) == 0)
00438     {
00439         int64_getter =
00440             (gint64 (*)(QofEntity *, QofParam *)) cm_param->param_getfcn;
00441         cm_i64 = int64_getter (importEnt, cm_param);
00442         i64_setter =
00443             (void (*)(QofEntity *, gint64)) cm_param->param_setfcn;
00444         if (i64_setter != NULL)
00445         {
00446             qof_util_param_edit ((QofInstance *) targetEnt, cm_param);
00447             i64_setter (targetEnt, cm_i64);
00448             qof_util_param_commit ((QofInstance *) targetEnt, cm_param);
00449         }
00450         registered_type = TRUE;
00451     }
00452     if (safe_strcmp (cm_param->param_type, QOF_TYPE_DOUBLE) == 0)
00453     {
00454         double_getter =
00455             (gdouble (*)(QofEntity *, QofParam *)) cm_param->param_getfcn;
00456         cm_double = double_getter (importEnt, cm_param);
00457         double_setter =
00458             (void (*)(QofEntity *, gdouble)) cm_param->param_setfcn;
00459         if (double_setter != NULL)
00460         {
00461             qof_util_param_edit ((QofInstance *) targetEnt, cm_param);
00462             double_setter (targetEnt, cm_double);
00463             qof_util_param_commit ((QofInstance *) targetEnt, cm_param);
00464         }
00465         registered_type = TRUE;
00466     }
00467     if (safe_strcmp (cm_param->param_type, QOF_TYPE_BOOLEAN) == 0)
00468     {
00469         boolean_getter =
00470             (gboolean (*)(QofEntity *, QofParam *)) cm_param->param_getfcn;
00471         cm_boolean = boolean_getter (importEnt, cm_param);
00472         boolean_setter =
00473             (void (*)(QofEntity *, gboolean)) cm_param->param_setfcn;
00474         if (boolean_setter != NULL)
00475         {
00476             qof_util_param_edit ((QofInstance *) targetEnt, cm_param);
00477             boolean_setter (targetEnt, cm_boolean);
00478             qof_util_param_commit ((QofInstance *) targetEnt, cm_param);
00479         }
00480         registered_type = TRUE;
00481     }
00482     if (safe_strcmp (cm_param->param_type, QOF_TYPE_KVP) == 0)
00483     {
00484         cm_kvp = (KvpFrame *) cm_param->param_getfcn (importEnt, cm_param);
00485         kvp_frame_setter =
00486             (void (*)(QofEntity *, KvpFrame *)) cm_param->param_setfcn;
00487         if (kvp_frame_setter != NULL)
00488         {
00489             qof_util_param_edit ((QofInstance *) targetEnt, cm_param);
00490             kvp_frame_setter (targetEnt, cm_kvp);
00491             qof_util_param_commit ((QofInstance *) targetEnt, cm_param);
00492         }
00493         else
00494         {
00495             QofInstance *target_inst;
00496 
00497             target_inst = (QofInstance *) targetEnt;
00498             kvp_frame_delete (target_inst->kvp_data);
00499             target_inst->kvp_data = kvp_frame_copy (cm_kvp);
00500         }
00501         registered_type = TRUE;
00502     }
00503     if (safe_strcmp (cm_param->param_type, QOF_TYPE_CHAR) == 0)
00504     {
00505         cm_char = (gchar *) cm_param->param_getfcn (importEnt, cm_param);
00506         char_setter =
00507             (void (*)(QofEntity *, char *)) cm_param->param_setfcn;
00508         if (char_setter != NULL)
00509         {
00510             qof_util_param_edit ((QofInstance *) targetEnt, cm_param);
00511             char_setter (targetEnt, cm_char);
00512             qof_util_param_commit ((QofInstance *) targetEnt, cm_param);
00513         }
00514         registered_type = TRUE;
00515     }
00516     if (safe_strcmp (cm_param->param_type, QOF_TYPE_COLLECT) == 0)
00517     {
00518         cm_col =
00519             (QofCollection *) cm_param->param_getfcn (importEnt, cm_param);
00520         if (cm_col)
00521         {
00522             /* create one reference for each member of the collection. */
00523             qof_collection_foreach (cm_col, col_ref_cb, context);
00524         }
00525         registered_type = TRUE;
00526     }
00527     if (registered_type == FALSE)
00528     {
00529 /*      referenceEnt = (QofEntity*)cm_param->param_getfcn(importEnt, cm_param);
00530         if(!referenceEnt) { return; }
00531         if(!referenceEnt->e_type) { return; }*/
00532         reference = qof_entity_get_reference_from (importEnt, cm_param);
00533         if (reference)
00534         {
00535             qof_session_update_reference_list (context->new_session,
00536                 reference);
00537         }
00538     }
00539 }
00540 
00541 static gboolean
00542 qof_entity_guid_match (QofSession * new_session, QofEntity * original)
00543 {
00544     QofEntity *copy;
00545     const GUID *g;
00546     QofIdTypeConst type;
00547     QofBook *targetBook;
00548     QofCollection *coll;
00549 
00550     copy = NULL;
00551     g_return_val_if_fail (original != NULL, FALSE);
00552     targetBook = qof_session_get_book (new_session);
00553     g_return_val_if_fail (targetBook != NULL, FALSE);
00554     g = qof_entity_get_guid (original);
00555     type = g_strdup (original->e_type);
00556     coll = qof_book_get_collection (targetBook, type);
00557     copy = qof_collection_lookup_entity (coll, g);
00558     if (copy)
00559     {
00560         return TRUE;
00561     }
00562     return FALSE;
00563 }
00564 
00565 static void
00566 qof_entity_list_foreach (gpointer data, gpointer user_data)

static void recurse_ent_cb ( QofEntity ent,
gpointer  user_data 
) [static]

< secondary collections are used for one-to-many references between entities and are implemented using QofCollection. These are NOT the same as the main collections in the QofBook.

  1. Each QofCollection contains one or many entities - *all* of a single type.
  2. The entity type within the collection can be determined at run time.
  3. Easy conversions to GList or whatever in the param_setfcn handler.
  4. Each parameter can have its own collection.
  5. Each entity can have a different *type* of collection to its siblings, provided that it is acceptable to the set function.
  6. Each object decides which types are acceptable for which parameter in the set functions. This is then part of the API for that object.

QOF_TYPE_COLLECT has two functions, both related to one-to-many links:

If the set function can handle it, it could also be used for true one-to-many links: one object linked to many entities of many types.

n.b. Always subject to each collection holding only one type at runtime. (otherwise use books).

Definition at line 793 of file qofsession.c.

00794     {
00795         if (i->data == NULL)
00796         {
00797             continue;
00798         }
00799         ref_param = (QofParam *) i->data;
00800         if (ref_param->param_name == NULL)
00801         {
00802             continue;
00803         }
00804         if (0 == safe_strcmp (ref_param->param_type, QOF_TYPE_COLLECT))
00805         {
00806             QofCollection *col;
00807 
00808             col = ref_param->param_getfcn (ent, ref_param);
00809             if (col)
00810             {
00811                 qof_collection_foreach (col, recurse_collection_cb, store);
00812             }
00813             continue;
00814         }
00815         ref_ent = (QofEntity *) ref_param->param_getfcn (ent, ref_param);
00816         if ((ref_ent) && (ref_ent->e_type))
00817         {
00818             store->success = qof_entity_copy_to_session (session, ref_ent);
00819             if (store->success)
00820             {
00821                 ent_list = g_list_append (ent_list, ref_ent);
00822             }
00823         }
00824     }
00825     for (i = ent_list; i != NULL; i = i->next)
00826     {
00827         if (i->data == NULL)
00828         {
00829             continue;
00830         }
00831         child_ent = (QofEntity *) i->data;
00832         if (child_ent == NULL)
00833         {
00834             continue;
00835         }
00836         ref_list = qof_class_get_referenceList (child_ent->e_type);
00837         for (j = ref_list; j != NULL; j = j->next)
00838         {
00839             if (j->data == NULL)
00840             {
00841                 continue;
00842             }
00843             ref_param = (QofParam *) j->data;
00844             ref_ent = ref_param->param_getfcn (child_ent, ref_param);
00845             if (ref_ent != NULL)
00846             {
00847                 success = qof_entity_copy_to_session (session, ref_ent);
00848                 if (success)
00849                 {
00850                     child_list = g_list_append (child_list, ref_ent);
00851                 }
00852             }
00853         }
00854     }
00855     for (i = child_list; i != NULL; i = i->next)
00856     {
00857         if (i->data == NULL)
00858         {
00859             continue;
00860         }
00861         ref_ent = (QofEntity *) i->data;
00862         if (ref_ent == NULL)
00863         {
00864             continue;
00865         }
00866         ref_list = qof_class_get_referenceList (ref_ent->e_type);
00867         for (j = ref_list; j != NULL; j = j->next)
00868         {
00869             if (j->data == NULL)
00870             {
00871                 continue;
00872             }
00873             ref_param = (QofParam *) j->data;
00874             child_ent = ref_param->param_getfcn (ref_ent, ref_param);
00875             if (child_ent != NULL)
00876             {
00877                 qof_entity_copy_to_session (session, child_ent);
00878             }
00879         }
00880     }
00881 }
00882 
00883 gboolean
00884 qof_entity_copy_coll_r (QofSession * new_session, QofCollection * coll)
00885 {
00886     struct recurse_s store;
00887     gboolean success;
00888 
00889     if ((!new_session) || (!coll))
00890     {
00891         return FALSE;
00892     }
00893     store.session = new_session;
00894     success = TRUE;
00895     store.success = success;
00896     store.ent_list = NULL;
00897     store.ref_list =
00898         qof_class_get_referenceList (qof_collection_get_type (coll));
00899     success = qof_entity_copy_coll (new_session, coll);
00900     if (success)
00901     {
00902         qof_collection_foreach (coll, recurse_ent_cb, &store);
00903     }
00904     return success;
00905 }
00906 
00907 gboolean
00908 qof_entity_copy_one_r (QofSession * new_session, QofEntity * ent)
00909 {
00910     struct recurse_s store;
00911     QofCollection *coll;
00912     gboolean success;
00913 
00914     if ((!new_session) || (!ent))
00915     {
00916         return FALSE;
00917     }
00918     store.session = new_session;
00919     success = TRUE;
00920     store.success = success;
00921     store.ref_list = qof_class_get_referenceList (ent->e_type);
00922     success = qof_entity_copy_to_session (new_session, ent);
00923     if (success == TRUE)
00924     {
00925         coll =
00926             qof_book_get_collection (qof_session_get_book (new_session),
00927             ent->e_type);
00928         if (coll)
00929         {
00930             qof_collection_foreach (coll, recurse_ent_cb, &store);
00931         }
00932     }
00933     return success;
00934 }
00935 


Variable Documentation

struct backend_providers backend_list[]

Initial value:

 {
    { "/opt/holly/qof/lib" ,  "libqof-backend-qsf" ,  "qsf_provider_init" },
    { "/opt/holly/qof/lib" , "libqof-backend-sqlite", "qof_sqlite_provider_init"},






    {NULL, NULL, NULL}
}

Definition at line 1007 of file qofsession.c.


Generated on Thu Jan 31 22:50:26 2008 for QOF by  doxygen 1.5.4