More a large number of the QofInstance properties from the public data

structure to a private data structure, with access to them as properties
of the object.


git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@16037 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
David Hampton 2007-05-02 01:57:49 +00:00
parent 06c2311047
commit 7d3c6b9a38
7 changed files with 547 additions and 233 deletions

View File

@ -30,6 +30,7 @@
* Created by Linas Vepstas December 1998
* Copyright (c) 1998-2001,2003 Linas Vepstas <linas@linas.org>
* Copyright (c) 2000 Dave Peticolas
* Copyright (c) 2007 David Hampton <hampton@employees.org>
*/
#include "config.h"
@ -168,7 +169,7 @@ qof_book_not_saved (const QofBook *book)
{
if (!book) return FALSE;
return(book->inst.dirty || qof_object_is_dirty (book));
return(qof_instance_get_dirty_flag(book) || qof_object_is_dirty(book));
}
void
@ -178,8 +179,8 @@ qof_book_mark_saved (QofBook *book)
if (!book) return;
was_dirty = book->inst.dirty;
book->inst.dirty = FALSE;
was_dirty = qof_instance_get_dirty_flag(book);
qof_instance_set_dirty_flag(book, FALSE);
book->dirty_time = 0;
qof_object_mark_clean (book);
if (was_dirty) {
@ -194,8 +195,8 @@ void qof_book_mark_dirty (QofBook *book)
if (!book) return;
was_dirty = book->inst.dirty;
book->inst.dirty = TRUE;
was_dirty = qof_instance_get_dirty_flag(book);
qof_instance_set_dirty_flag(book, TRUE);
if (!was_dirty) {
book->dirty_time = time(NULL);
if (book->dirty_cb)
@ -206,7 +207,7 @@ void qof_book_mark_dirty (QofBook *book)
void
qof_book_print_dirty (const QofBook *book)
{
if (book->inst.dirty)
if (qof_instance_get_dirty_flag(book))
printf("book is dirty.\n");
qof_book_foreach_collection
(book, (QofCollectionForeachCB)qof_collection_print_dirty, NULL);

View File

@ -131,12 +131,12 @@ qof_collection_remove_entity (QofInstance *ent)
{
QofCollection *col;
if (!ent) return;
col = ent->collection;
col = qof_instance_get_collection(ent);
if (!col) return;
g_hash_table_remove (col->hash_of_entities, &ent->guid);
if (!qof_alt_dirty_mode)
qof_collection_mark_dirty(col);
ent->collection = NULL;
qof_instance_set_collection(ent, NULL);
}
void
@ -149,7 +149,7 @@ qof_collection_insert_entity (QofCollection *col, QofInstance *ent)
g_hash_table_insert (col->hash_of_entities, &ent->guid, ent);
if (!qof_alt_dirty_mode)
qof_collection_mark_dirty(col);
ent->collection = col;
qof_instance_set_collection(ent, col);
}
gboolean

View File

@ -24,6 +24,7 @@
* gnucash objects use.
*
* Copyright (C) 2003 Linas Vepstas <linas@linas.org>
* Copyright (c) 2007 David Hampton <hampton@employees.org>
*/
#ifndef QOF_INSTANCE_P_H
@ -31,6 +32,11 @@
#include "qofinstance.h"
/** Set the collection this instance belongs to. This function should never
* be called by user code. Instead call the qof_collection_insert_entity()
* function. */
void qof_instance_set_collection (gconstpointer ptr, QofCollection *col);
void qof_instance_set_slots (QofInstance *, KvpFrame *);
/* Set the last_update time. Reserved for use by the SQL backend;
@ -39,4 +45,8 @@ void qof_instance_set_slots (QofInstance *, KvpFrame *);
*/
void qof_instance_set_last_update (QofInstance *inst, Timespec ts);
/** Set the dirty flag of just the instance. Don't modify the
* collection flag at all. */
void qof_instance_set_dirty_flag (gconstpointer inst, gboolean flag);
#endif /* QOF_INSTANCE_P_H */

View File

@ -25,6 +25,7 @@
* gnucash objects use.
*
* Copyright (C) 2003 Linas Vepstas <linas@linas.org>
* Copyright (c) 2007 David Hampton <hampton@employees.org>
*/
#include "config.h"
@ -39,37 +40,195 @@ static QofLogModule log_module = QOF_MOD_ENGINE;
/* ========================================================== */
enum {
LAST_SIGNAL
};
enum {
PROP_0,
PROP_TYPE,
PROP_GUID,
PROP_COLLECTION,
PROP_BOOK,
PROP_KVP_DATA,
PROP_LAST_UPDATE,
PROP_EDITLEVEL,
PROP_DESTROYING,
PROP_DIRTY,
PROP_INFANT,
};
typedef struct QofInstancePrivate
{
// QofIdType e_type; /**< Entity type */
// GUID guid; /**< GUID for the entity */
QofCollection *collection; /**< Entity collection */
/* The entity_table in which this instance is stored */
// QofBook * book;
/* kvp_data is a key-value pair database for storing arbirtary
* information associated with this instance.
* See src/engine/kvp_doc.txt for a list and description of the
* important keys. */
// KvpFrame *kvp_data;
/* Timestamp used to track the last modification to this
* instance. Typically used to compare two versions of the
* same object, to see which is newer. When used with the
* SQL backend, this field is reserved for SQL use, to compare
* the version in local memory to the remote, server version.
*/
Timespec last_update;
/* Keep track of nesting level of begin/end edit calls */
int editlevel;
/* In process of being destroyed */
gboolean do_free;
/* dirty/clean flag. If dirty, then this instance has been modified,
* but has not yet been written out to storage (file/database)
*/
gboolean dirty;
/* True iff this instance has never been committed. */
gboolean infant;
} QofInstancePrivate;
#define GET_PRIVATE(o) \
(G_TYPE_INSTANCE_GET_PRIVATE ((o), QOF_TYPE_INSTANCE, QofInstancePrivate))
QOF_GOBJECT_GET_TYPE(QofInstance, qof_instance, G_TYPE_OBJECT, {});
QOF_GOBJECT_FINALIZE(qof_instance);
static void qof_instance_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec);
static void qof_instance_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec);
static void qof_instance_dispose(GObject*);
static void qof_instance_class_init(QofInstanceClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS(klass);
object_class->finalize = qof_instance_finalize;
object_class->dispose = qof_instance_dispose;
object_class->set_property = qof_instance_set_property;
object_class->get_property = qof_instance_get_property;
g_type_class_add_private(klass, sizeof(QofInstancePrivate));
g_object_class_install_property
(object_class,
PROP_COLLECTION,
g_param_spec_pointer ("collection",
"Object Collection",
"A collection of like objects of which this "
"particular object is amember. E.g.. A "
"collection of accounts, or a collection of "
"splits.",
G_PARAM_READWRITE));
g_object_class_install_property
(object_class,
PROP_BOOK,
g_param_spec_object ("book",
"Object Book",
"The book that contains this object.",
QOF_TYPE_BOOK,
G_PARAM_READWRITE));
g_object_class_install_property
(object_class,
PROP_KVP_DATA,
g_param_spec_pointer ("kvp-data",
"Object KVP Data",
"A pointer to the key-value data associated "
"with this object.",
G_PARAM_READWRITE));
g_object_class_install_property
(object_class,
PROP_LAST_UPDATE,
g_param_spec_pointer ("last-update",
"Object Last Update",
"A pointer to the last time this object was "
"updated. This value is present for use by "
"backends and shouldnot be written by other "
"code.",
G_PARAM_READWRITE));
g_object_class_install_property
(object_class,
PROP_EDITLEVEL,
g_param_spec_int ("editlevel",
"Object Edit Level",
"The object edit level.",
0, G_MAXINT, 0,
G_PARAM_READABLE));
g_object_class_install_property
(object_class,
PROP_DESTROYING,
g_param_spec_boolean ("destroying",
"Object Destroying",
"This flag is set to TRUE if the object is "
"about to be destroyed.",
FALSE,
G_PARAM_READWRITE));
g_object_class_install_property
(object_class,
PROP_DIRTY,
g_param_spec_boolean ("dirty",
"Object Dirty",
"This flag is set to TRUE if the object has "
"unsaved changes.",
FALSE,
G_PARAM_READWRITE));
g_object_class_install_property
(object_class,
PROP_INFANT,
g_param_spec_boolean ("infant",
"Object Infant",
"This flag is set to TRUE if the object has "
"never been added to a book. This implies "
"that its destruction does not affect the "
"state of the book, and therefore the saved "
"state of the data file.",
FALSE,
G_PARAM_READABLE));
}
static void
qof_instance_init (QofInstance *inst)
{
QofInstancePrivate *priv;
priv = GET_PRIVATE(inst);
inst->book = NULL;
inst->kvp_data = kvp_frame_new();
inst->last_update.tv_sec = 0;
inst->last_update.tv_nsec = -1;
inst->editlevel = 0;
inst->do_free = FALSE;
inst->dirty = FALSE;
inst->infant = TRUE;
priv->last_update.tv_sec = 0;
priv->last_update.tv_nsec = -1;
priv->editlevel = 0;
priv->do_free = FALSE;
priv->dirty = FALSE;
priv->infant = TRUE;
}
void
qof_instance_init_data (QofInstance *inst, QofIdType type, QofBook *book)
{
QofInstancePrivate *priv;
QofCollection *col;
QofIdType col_type;
g_return_if_fail(QOF_IS_INSTANCE(inst));
priv = GET_PRIVATE(inst);
g_return_if_fail(!inst->book);
inst->book = book;
@ -95,7 +254,7 @@ qof_instance_init_data (QofInstance *inst, QofIdType type, QofBook *book)
PWARN("duplicate id created, trying again");
} while(1);
inst->collection = col;
priv->collection = col;
qof_collection_insert_entity (col, inst);
}
@ -103,52 +262,167 @@ qof_instance_init_data (QofInstance *inst, QofIdType type, QofBook *book)
static void
qof_instance_dispose (GObject *instp)
{
QofInstancePrivate *priv;
QofInstance* inst = QOF_INSTANCE(instp);
if (!inst->collection)
priv = GET_PRIVATE(instp);
if (!priv->collection)
return;
qof_collection_remove_entity(inst);
CACHE_REMOVE(inst->e_type);
inst->e_type = NULL;
G_OBJECT_CLASS(qof_instance_parent_class)->dispose(instp);
}
static void
qof_instance_finalize_real (GObject *instp)
{
QofInstancePrivate *priv;
QofInstance* inst = QOF_INSTANCE(instp);
kvp_frame_delete (inst->kvp_data);
inst->kvp_data = NULL;
inst->editlevel = 0;
inst->do_free = FALSE;
inst->dirty = FALSE;
priv = GET_PRIVATE(inst);
priv->editlevel = 0;
priv->do_free = FALSE;
priv->dirty = FALSE;
}
static void
qof_instance_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
QofInstance *inst;
QofInstancePrivate *priv;
g_return_if_fail(QOF_IS_INSTANCE(object));
inst = QOF_INSTANCE(object);
priv = GET_PRIVATE(inst);
switch (prop_id) {
case PROP_GUID:
g_value_set_boxed(value, &inst->guid);
break;
case PROP_COLLECTION:
g_value_set_pointer(value, priv->collection);
break;
case PROP_BOOK:
g_value_set_object(value, inst->book);
break;
case PROP_KVP_DATA:
g_value_set_pointer(value, inst->kvp_data);
break;
case PROP_LAST_UPDATE:
g_value_set_pointer(value, &priv->last_update);
break;
case PROP_EDITLEVEL:
g_value_set_int(value, priv->editlevel);
break;
case PROP_DESTROYING:
g_value_set_boolean(value, priv->do_free);
break;
case PROP_DIRTY:
g_value_set_boolean(value, qof_instance_get_dirty(inst));
break;
case PROP_INFANT:
g_value_set_boolean(value, priv->infant);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void
qof_instance_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
QofInstance *inst;
QofInstancePrivate *priv;
Timespec *ts;
g_return_if_fail(QOF_IS_INSTANCE(object));
inst = QOF_INSTANCE(object);
priv = GET_PRIVATE(inst);
switch (prop_id) {
case PROP_GUID:
qof_instance_set_guid(inst, g_value_get_boxed(value));
break;
case PROP_COLLECTION:
qof_instance_set_collection(inst, g_value_get_pointer(value));
break;
case PROP_BOOK:
qof_instance_set_book(inst, g_value_get_object(value));
break;
case PROP_KVP_DATA:
qof_instance_set_slots(inst, g_value_get_pointer(value));
break;
case PROP_LAST_UPDATE:
ts = g_value_get_pointer(value);
qof_instance_set_last_update(inst, *ts);
break;
case PROP_DESTROYING:
qof_instance_set_destroying(inst, g_value_get_boolean(value));
break;
case PROP_DIRTY:
qof_instance_set_dirty(inst);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
const GUID *
qof_instance_get_guid (const QofInstance *inst)
{
QofInstancePrivate *priv;
if (!inst) return guid_null();
return &(inst->guid);
g_return_val_if_fail(QOF_IS_INSTANCE(inst), guid_null());
priv = GET_PRIVATE(inst);
return &(inst->guid);
}
void
qof_instance_set_guid (QofInstance *ent, const GUID *guid)
{
QofCollection *col;
if (guid_equal (guid, &ent->guid)) return;
QofInstancePrivate *priv;
QofCollection *col;
col = ent->collection;
qof_collection_remove_entity(ent);
ent->guid = *guid;
qof_collection_insert_entity(col, ent);
priv = GET_PRIVATE(ent);
if (guid_equal (guid, &ent->guid))
return;
col = priv->collection;
qof_collection_remove_entity (ent);
ent->guid = *guid;
qof_collection_insert_entity (col, ent);
}
const QofCollection *
QofCollection *
qof_instance_get_collection (gconstpointer ptr)
{
g_return_val_if_fail(QOF_IS_INSTANCE(ptr), NULL);
return QOF_INSTANCE(ptr)->collection;
return GET_PRIVATE(ptr)->collection;
}
void
qof_instance_set_collection (gconstpointer ptr, QofCollection *col)
{
g_return_if_fail(QOF_IS_INSTANCE(ptr));
GET_PRIVATE(ptr)->collection = col;
}
QofBook *
@ -158,66 +432,100 @@ qof_instance_get_book (const QofInstance *inst)
return inst->book;
}
void
qof_instance_set_book (gconstpointer inst, QofBook *book)
{
g_return_if_fail(QOF_IS_INSTANCE(inst));
QOF_INSTANCE(inst)->book = book;
}
KvpFrame*
qof_instance_get_slots (const QofInstance *inst)
{
if (!inst) return NULL;
return inst->kvp_data;
if (!inst) return NULL;
return inst->kvp_data;
}
void
qof_instance_set_slots (QofInstance *inst, KvpFrame *frm)
{
QofInstancePrivate *priv;
if (!inst) return;
priv = GET_PRIVATE(inst);
if (inst->kvp_data && (inst->kvp_data != frm)) {
kvp_frame_delete(inst->kvp_data);
}
priv->dirty = TRUE;
inst->kvp_data = frm;
}
Timespec
qof_instance_get_last_update (const QofInstance *inst)
{
if (!inst)
{
Timespec ts = {0,-1};
return ts;
}
return inst->last_update;
if (!inst) {
Timespec ts = {0,-1};
return ts;
}
return GET_PRIVATE(inst)->last_update;
}
void
qof_instance_set_last_update (QofInstance *inst, Timespec ts)
{
if (!inst) return;
GET_PRIVATE(inst)->last_update = ts;
}
gint
qof_instance_get_editlevel (gconstpointer ptr)
{
g_return_val_if_fail(QOF_IS_INSTANCE(ptr), 0);
return QOF_INSTANCE(ptr)->editlevel;
return GET_PRIVATE(ptr)->editlevel;
}
void qof_instance_increase_editlevel (gpointer ptr)
{
g_return_if_fail(QOF_IS_INSTANCE(ptr));
QOF_INSTANCE(ptr)->editlevel++;
GET_PRIVATE(ptr)->editlevel++;
}
void qof_instance_decrease_editlevel (gpointer ptr)
{
g_return_if_fail(QOF_IS_INSTANCE(ptr));
QOF_INSTANCE(ptr)->editlevel--;
GET_PRIVATE(ptr)->editlevel--;
}
void qof_instance_reset_editlevel (gpointer ptr)
{
g_return_if_fail(QOF_IS_INSTANCE(ptr));
QOF_INSTANCE(ptr)->editlevel = 0;
GET_PRIVATE(ptr)->editlevel = 0;
}
gboolean
qof_instance_check_edit(const QofInstance *inst)
{
g_return_val_if_fail(QOF_IS_INSTANCE(inst), FALSE);
return (inst->editlevel > 0);
return (GET_PRIVATE(inst)->editlevel > 0);
}
int
qof_instance_version_cmp (const QofInstance *left, const QofInstance *right)
{
QofInstancePrivate *lpriv, *rpriv;
if (!left && !right) return 0;
if (!left) return -1;
if (!right) return +1;
if (left->last_update.tv_sec < right->last_update.tv_sec) return -1;
if (left->last_update.tv_sec > right->last_update.tv_sec) return +1;
if (left->last_update.tv_nsec < right->last_update.tv_nsec) return -1;
if (left->last_update.tv_nsec > right->last_update.tv_nsec) return +1;
lpriv = GET_PRIVATE(left);
rpriv = GET_PRIVATE(right);
if (lpriv->last_update.tv_sec < rpriv->last_update.tv_sec) return -1;
if (lpriv->last_update.tv_sec > rpriv->last_update.tv_sec) return +1;
if (lpriv->last_update.tv_nsec < rpriv->last_update.tv_nsec) return -1;
if (lpriv->last_update.tv_nsec > rpriv->last_update.tv_nsec) return +1;
return 0;
}
@ -225,99 +533,85 @@ gboolean
qof_instance_get_destroying (gconstpointer ptr)
{
g_return_val_if_fail(QOF_IS_INSTANCE(ptr), FALSE);
return QOF_INSTANCE(ptr)->do_free;
return GET_PRIVATE(ptr)->do_free;
}
void
qof_instance_set_destroying (gpointer ptr, gboolean value)
{
g_return_if_fail(QOF_IS_INSTANCE(ptr));
QOF_INSTANCE(ptr)->do_free = value;
GET_PRIVATE(ptr)->do_free = value;
}
gboolean
qof_instance_get_dirty_flag (gconstpointer ptr)
{
g_return_val_if_fail(QOF_IS_INSTANCE(ptr), FALSE);
return QOF_INSTANCE(ptr)->dirty;
return GET_PRIVATE(ptr)->dirty;
}
void
qof_instance_print_dirty (const QofInstance *entity, gpointer dummy)
qof_instance_set_dirty_flag (gconstpointer inst, gboolean flag)
{
QofInstance *inst = QOF_INSTANCE(entity);
g_return_if_fail(QOF_IS_INSTANCE(inst));
GET_PRIVATE(inst)->dirty = flag;
}
if (inst->dirty)
printf("%s instance %s is dirty.\n", inst->e_type,
guid_to_string(&inst->guid));
void
qof_instance_mark_clean (QofInstance *inst)
{
if(!inst) return;
GET_PRIVATE(inst)->dirty = FALSE;
}
void
qof_instance_print_dirty (const QofInstance *inst, gpointer dummy)
{
QofInstancePrivate *priv;
priv = GET_PRIVATE(inst);
if (priv->dirty) {
printf("%s instance %s is dirty.\n", inst->e_type,
guid_to_string(&inst->guid));
}
}
gboolean
qof_instance_is_dirty (QofInstance *inst)
qof_instance_get_dirty (QofInstance *inst)
{
QofInstancePrivate *priv;
QofCollection *coll;
if (!inst) { return FALSE; }
priv = GET_PRIVATE(inst);
if (qof_get_alt_dirty_mode())
return inst->dirty;
coll = inst->collection;
if(qof_collection_is_dirty(coll)) { return inst->dirty; }
inst->dirty = FALSE;
return priv->dirty;
coll = priv->collection;
if(qof_collection_is_dirty(coll)) { return priv->dirty; }
priv->dirty = FALSE;
return FALSE;
}
void
qof_instance_set_dirty(QofInstance* inst)
{
QofInstancePrivate *priv;
QofCollection *coll;
inst->dirty = TRUE;
priv = GET_PRIVATE(inst);
priv->dirty = TRUE;
if (!qof_get_alt_dirty_mode()) {
coll = inst->collection;
coll = priv->collection;
qof_collection_mark_dirty(coll);
}
}
gboolean
qof_instance_do_free(const QofInstance *inst)
qof_instance_get_infant(const QofInstance *inst)
{
return inst->do_free;
}
void
qof_instance_mark_free(QofInstance *inst)
{
inst->do_free = TRUE;
}
/* ========================================================== */
/* setters */
void
qof_instance_mark_clean (QofInstance *inst)
{
if(!inst) return;
inst->dirty = FALSE;
}
void
qof_instance_set_slots (QofInstance *inst, KvpFrame *frm)
{
if (!inst) return;
if (inst->kvp_data && (inst->kvp_data != frm))
{
kvp_frame_delete(inst->kvp_data);
}
inst->dirty = TRUE;
inst->kvp_data = frm;
}
void
qof_instance_set_last_update (QofInstance *inst, Timespec ts)
{
if (!inst) return;
inst->last_update = ts;
g_return_val_if_fail(QOF_IS_INSTANCE(inst), FALSE);
return GET_PRIVATE(inst)->infant;
}
/* ========================================================== */
@ -325,10 +619,20 @@ qof_instance_set_last_update (QofInstance *inst, Timespec ts)
void
qof_instance_gemini (QofInstance *to, const QofInstance *from)
{
QofInstancePrivate *from_priv, *to_priv, *fb_priv, *tb_priv;
time_t now;
g_return_if_fail(QOF_IS_INSTANCE(to));
g_return_if_fail(QOF_IS_INSTANCE(from));
from_priv = GET_PRIVATE(from);
to_priv = GET_PRIVATE(to);
fb_priv = GET_PRIVATE(QOF_INSTANCE(from)->book);
tb_priv = GET_PRIVATE(QOF_INSTANCE(to)->book);
/* Books must differ for a gemini to be meaningful */
if (!from || !to || (from->book == to->book)) return;
if (from->book == to->book)
return;
now = time(0);
@ -342,7 +646,7 @@ qof_instance_gemini (QofInstance *to, const QofInstance *from)
"book_guid", &to->book->inst.guid,
NULL);
to->dirty = TRUE;
to_priv->dirty = TRUE;
}
QofInstance *
@ -368,4 +672,118 @@ qof_instance_lookup_twin (const QofInstance *src, QofBook *target_book)
return twin;
}
/* =================================================================== */
/* Entity edit and commit utilities */
/* =================================================================== */
gboolean
qof_begin_edit (QofInstance *inst)
{
QofInstancePrivate *priv;
QofBackend * be;
if (!inst) return FALSE;
priv = GET_PRIVATE(inst);
priv->editlevel++;
if (1 < priv->editlevel) return FALSE;
if (0 >= priv->editlevel)
priv->editlevel = 1;
be = qof_book_get_backend(inst->book);
if (be && qof_backend_begin_exists(be))
qof_backend_run_begin(be, inst);
else
priv->dirty = TRUE;
return TRUE;
}
gboolean qof_commit_edit (QofInstance *inst)
{
QofInstancePrivate *priv;
QofBackend * be;
if (!inst) return FALSE;
priv = GET_PRIVATE(inst);
priv->editlevel--;
if (0 < priv->editlevel) return FALSE;
if ((0 == priv->editlevel) && priv->dirty) {
be = qof_book_get_backend(inst->book);
if (be && qof_backend_commit_exists(be)) {
qof_backend_run_commit(be, inst);
}
}
if (0 > priv->editlevel) {
PERR ("unbalanced call - resetting (was %d)", priv->editlevel);
priv->editlevel = 0;
}
return TRUE;
}
gboolean
qof_commit_edit_part2(QofInstance *inst,
void (*on_error)(QofInstance *, QofBackendError),
void (*on_done)(QofInstance *),
void (*on_free)(QofInstance *))
{
QofInstancePrivate *priv;
QofBackend * be;
gboolean dirty;
priv = GET_PRIVATE(inst);
dirty = priv->dirty;
/* See if there's a backend. If there is, invoke it. */
be = qof_book_get_backend(inst->book);
if (be && qof_backend_commit_exists(be)) {
QofBackendError errcode;
/* clear errors */
do {
errcode = qof_backend_get_error(be);
} while (ERR_BACKEND_NO_ERR != errcode);
qof_backend_run_commit(be, inst);
errcode = qof_backend_get_error(be);
if (ERR_BACKEND_NO_ERR != errcode) {
/* XXX Should perform a rollback here */
priv->do_free = FALSE;
/* Push error back onto the stack */
qof_backend_set_error (be, errcode);
if (on_error)
on_error(inst, errcode);
return FALSE;
}
/* XXX the backend commit code should clear dirty!! */
priv->dirty = FALSE;
}
if (dirty && qof_get_alt_dirty_mode() &&
!(priv->infant && priv->do_free)) {
qof_collection_mark_dirty(priv->collection);
qof_book_mark_dirty(inst->book);
}
priv->infant = FALSE;
if (priv->do_free) {
if (on_free)
on_free(inst);
return TRUE;
}
if (on_done)
on_done(inst);
return TRUE;
}
/* ========================== END OF FILE ======================= */
// Local Variables:
// mode: c
// indent-tabs-mode: nil
// c-block-comment-prefix: "* "
// eval: (c-add-style "gnc" '("k&r" (c-basic-offset . 4) (c-offsets-alist (case-label . +))) t)
// End:

View File

@ -31,6 +31,7 @@
* @brief Object instance holds common fields that most gnucash objects use.
*
* @author Copyright (C) 2003,2004 Linas Vepstas <linas@linas.org>
* @author Copyright (c) 2007 David Hampton <hampton@employees.org>
*/
#ifndef QOF_INSTANCE_H
@ -65,10 +66,8 @@ struct QofInstance_s
{
GObject object;
/* Globally unique id identifying this instance */
QofIdType e_type; /**< Entity type */
GUID guid; /**< GUID for the entity */
QofCollection * collection; /**< Entity collection */
/* The entity_table in which this instance is stored */
QofBook * book;
@ -78,28 +77,6 @@ struct QofInstance_s
* See src/engine/kvp_doc.txt for a list and description of the
* important keys. */
KvpFrame *kvp_data;
/* Timestamp used to track the last modification to this
* instance. Typically used to compare two versions of the
* same object, to see which is newer. When used with the
* SQL backend, this field is reserved for SQL use, to compare
* the version in local memory to the remote, server version.
*/
Timespec last_update;
/* Keep track of nesting level of begin/end edit calls */
int editlevel;
/* In process of being destroyed */
gboolean do_free;
/* dirty/clean flag. If dirty, then this instance has been modified,
* but has not yet been written out to storage (file/database)
*/
gboolean dirty;
/* True iff this instance has never been committed. */
gboolean infant;
};
struct _QofInstanceClass
@ -116,11 +93,14 @@ void qof_instance_init_data (QofInstance *, QofIdType, QofBook *);
/** Return the book pointer */
QofBook * qof_instance_get_book (const QofInstance *);
/** Set the book pointer */
void qof_instance_set_book (gconstpointer inst, QofBook *book);
/** Return the GUID of this instance */
const GUID * qof_instance_get_guid (const QofInstance *);
/** Return the collection this instance belongs to */
const QofCollection* qof_instance_get_collection (gconstpointer inst);
QofCollection* qof_instance_get_collection (gconstpointer inst);
/** Set the GUID of this instance */
void qof_instance_set_guid (QofInstance *ent, const GUID *guid);
@ -183,7 +163,8 @@ gboolean qof_instance_get_dirty_flag (gconstpointer ptr);
void qof_instance_print_dirty (const QofInstance *entity, gpointer dummy);
/** Return value of is_dirty flag */
gboolean qof_instance_is_dirty (QofInstance *);
#define qof_instance_is_dirty qof_instance_get_dirty
gboolean qof_instance_get_dirty (QofInstance *);
/** \brief Set the dirty flag
@ -194,11 +175,13 @@ void qof_instance_set_dirty(QofInstance* inst);
/* reset the dirty flag */
void qof_instance_mark_clean (QofInstance *);
gint qof_instance_get_editlevel(gconstpointer inst);
void qof_instance_set_editlevel(gpointer inst, gint level);
void qof_instance_increase_editlevel(gpointer inst);
void qof_instance_decrease_editlevel(gpointer inst);
gboolean qof_instance_check_edit(const QofInstance *inst);
gboolean qof_instance_do_free(const QofInstance *inst);
void qof_instance_mark_free(QofInstance *inst);
gboolean qof_instance_get_infant(const QofInstance *inst);
/** Pair things up. This routine inserts a kvp value into each instance
* containing the guid of the other. In this way, if one has one of the

View File

@ -223,105 +223,6 @@ qof_util_bool_to_int (const gchar * val)
return atoi (val);
}
/* =================================================================== */
/* Entity edit and commit utilities */
/* =================================================================== */
gboolean
qof_begin_edit(QofInstance *inst)
{
QofBackend * be;
if (!inst) return FALSE;
inst->editlevel++;
if (1 < inst->editlevel) return FALSE;
if (0 >= inst->editlevel)
inst->editlevel = 1;
be = qof_book_get_backend (inst->book);
if (be && qof_backend_begin_exists(be))
qof_backend_run_begin(be, inst);
else
inst->dirty = TRUE;
return TRUE;
}
gboolean qof_commit_edit(QofInstance *inst)
{
QofBackend * be;
if (!inst) return FALSE;
inst->editlevel--;
if (0 < inst->editlevel) return FALSE;
if ((0 == inst->editlevel) && inst->dirty)
{
be = qof_book_get_backend (inst->book);
if (be && qof_backend_commit_exists(be)) {
qof_backend_run_commit(be, inst);
}
}
if (0 > inst->editlevel) {
PERR ("unbalanced call - resetting (was %d)", inst->editlevel);
inst->editlevel = 0;
}
return TRUE;
}
gboolean
qof_commit_edit_part2(QofInstance *inst,
void (*on_error)(QofInstance *, QofBackendError),
void (*on_done)(QofInstance *),
void (*on_free)(QofInstance *))
{
QofBackend * be;
gboolean dirty = inst->dirty;
/* See if there's a backend. If there is, invoke it. */
be = qof_book_get_backend(inst->book);
if (be && qof_backend_commit_exists(be)) {
QofBackendError errcode;
/* clear errors */
do {
errcode = qof_backend_get_error(be);
} while (ERR_BACKEND_NO_ERR != errcode);
qof_backend_run_commit(be, inst);
errcode = qof_backend_get_error(be);
if (ERR_BACKEND_NO_ERR != errcode) {
/* XXX Should perform a rollback here */
inst->do_free = FALSE;
/* Push error back onto the stack */
qof_backend_set_error (be, errcode);
if (on_error)
on_error(inst, errcode);
return FALSE;
}
/* XXX the backend commit code should clear dirty!! */
inst->dirty = FALSE;
}
if (dirty && qof_get_alt_dirty_mode() &&
!(inst->infant && inst->do_free)) {
qof_collection_mark_dirty(inst->collection);
qof_book_mark_dirty(inst->book);
}
inst->infant = FALSE;
if (inst->do_free) {
if (on_free)
on_free(inst);
return TRUE;
}
if (on_done)
on_done(inst);
return TRUE;
}
/* =================================================================== */
/* The QOF string cache */
/* =================================================================== */

View File

@ -34,6 +34,8 @@
#include "test-engine-stuff.h"
#include "qof.h"
#define NENT 500123
static void test_null_guid(void)
{
GUID g;
@ -53,7 +55,7 @@ run_test (void)
int i;
QofSession *sess;
QofBook *book;
QofInstance *eblk;
QofInstance *ent, *eblk[NENT];
QofCollection *col;
QofIdType type;
@ -64,11 +66,10 @@ run_test (void)
col = qof_book_get_collection (book, "asdf");
type = qof_collection_get_type (col);
#define NENT 500123
eblk = g_new0(QofInstance, NENT);
for (i=0; i<NENT; i++)
{
QofInstance *ent = &eblk[i];
ent = g_object_new(QOF_TYPE_INSTANCE, NULL);
eblk[i] = ent;
guid_new(&ent->guid);
do_test ((NULL == qof_collection_lookup_entity (col, &ent->guid)),
"duplicate guid");