r20053 - gnucash/trunk/src - Bug #638543: Split the qof_book_get_counter function.

Christian Stimming cstim at code.gnucash.org
Mon Jan 10 16:38:55 EST 2011


Author: cstim
Date: 2011-01-10 16:38:54 -0500 (Mon, 10 Jan 2011)
New Revision: 20053
Trac: http://svn.gnucash.org/trac/changeset/20053

Modified:
   gnucash/trunk/src/engine/gncCustomer.c
   gnucash/trunk/src/engine/gncEmployee.c
   gnucash/trunk/src/engine/gncInvoice.c
   gnucash/trunk/src/engine/gncJob.c
   gnucash/trunk/src/engine/gncOrder.c
   gnucash/trunk/src/engine/gncVendor.c
   gnucash/trunk/src/libqof/qof/qofbook.c
   gnucash/trunk/src/libqof/qof/qofbook.h
Log:
Bug #638543: Split the qof_book_get_counter function.

Patch by Matthijs Kooijman:

The qof_book_get_counter function now only gets the current counter
value. The new qof_book_get_and_increment_counter function now does the
incrementing.

Modified: gnucash/trunk/src/engine/gncCustomer.c
===================================================================
--- gnucash/trunk/src/engine/gncCustomer.c	2011-01-10 21:38:43 UTC (rev 20052)
+++ gnucash/trunk/src/engine/gncCustomer.c	2011-01-10 21:38:54 UTC (rev 20053)
@@ -951,5 +951,5 @@
 
 gint64 gncCustomerNextID (QofBook *book)
 {
-    return qof_book_get_counter (book, _GNC_MOD_NAME);
+    return qof_book_increment_and_get_counter (book, _GNC_MOD_NAME);
 }

Modified: gnucash/trunk/src/engine/gncEmployee.c
===================================================================
--- gnucash/trunk/src/engine/gncEmployee.c	2011-01-10 21:38:43 UTC (rev 20052)
+++ gnucash/trunk/src/engine/gncEmployee.c	2011-01-10 21:38:54 UTC (rev 20053)
@@ -742,5 +742,5 @@
 
 gint64 gncEmployeeNextID (QofBook *book)
 {
-    return qof_book_get_counter (book, _GNC_MOD_NAME);
+    return qof_book_increment_and_get_counter (book, _GNC_MOD_NAME);
 }

Modified: gnucash/trunk/src/engine/gncInvoice.c
===================================================================
--- gnucash/trunk/src/engine/gncInvoice.c	2011-01-10 21:38:43 UTC (rev 20052)
+++ gnucash/trunk/src/engine/gncInvoice.c	2011-01-10 21:38:54 UTC (rev 20053)
@@ -2131,16 +2131,16 @@
     switch (gncOwnerGetType(gncOwnerGetEndOwner(owner)))
     {
     case GNC_OWNER_CUSTOMER:
-        nextID = qof_book_get_counter (book, "gncInvoice");
+        nextID = qof_book_increment_and_get_counter (book, "gncInvoice");
         break;
     case GNC_OWNER_VENDOR:
-        nextID = qof_book_get_counter (book, "gncBill");
+        nextID = qof_book_increment_and_get_counter (book, "gncBill");
         break;
     case GNC_OWNER_EMPLOYEE:
-        nextID = qof_book_get_counter (book, "gncExpVoucher");
+        nextID = qof_book_increment_and_get_counter (book, "gncExpVoucher");
         break;
     default:
-        nextID = qof_book_get_counter (book, _GNC_MOD_NAME);
+        nextID = qof_book_increment_and_get_counter (book, _GNC_MOD_NAME);
         break;
     }
     return nextID;

Modified: gnucash/trunk/src/engine/gncJob.c
===================================================================
--- gnucash/trunk/src/engine/gncJob.c	2011-01-10 21:38:43 UTC (rev 20052)
+++ gnucash/trunk/src/engine/gncJob.c	2011-01-10 21:38:54 UTC (rev 20053)
@@ -585,5 +585,5 @@
 
 gint64 gncJobNextID (QofBook *book)
 {
-    return qof_book_get_counter (book, _GNC_MOD_NAME);
+    return qof_book_increment_and_get_counter (book, _GNC_MOD_NAME);
 }

Modified: gnucash/trunk/src/engine/gncOrder.c
===================================================================
--- gnucash/trunk/src/engine/gncOrder.c	2011-01-10 21:38:43 UTC (rev 20052)
+++ gnucash/trunk/src/engine/gncOrder.c	2011-01-10 21:38:54 UTC (rev 20053)
@@ -606,5 +606,5 @@
 
 gint64 gncOrderNextID (QofBook *book)
 {
-    return qof_book_get_counter (book, _GNC_MOD_NAME);
+    return qof_book_increment_and_get_counter (book, _GNC_MOD_NAME);
 }

Modified: gnucash/trunk/src/engine/gncVendor.c
===================================================================
--- gnucash/trunk/src/engine/gncVendor.c	2011-01-10 21:38:43 UTC (rev 20052)
+++ gnucash/trunk/src/engine/gncVendor.c	2011-01-10 21:38:54 UTC (rev 20053)
@@ -861,5 +861,5 @@
 
 gint64 gncVendorNextID (QofBook *book)
 {
-    return qof_book_get_counter (book, _GNC_MOD_NAME);
+    return qof_book_increment_and_get_counter (book, _GNC_MOD_NAME);
 }

Modified: gnucash/trunk/src/libqof/qof/qofbook.c
===================================================================
--- gnucash/trunk/src/libqof/qof/qofbook.c	2011-01-10 21:38:43 UTC (rev 20052)
+++ gnucash/trunk/src/libqof/qof/qofbook.c	2011-01-10 21:38:54 UTC (rev 20053)
@@ -392,10 +392,8 @@
 gint64
 qof_book_get_counter (QofBook *book, const char *counter_name)
 {
-    QofBackend *be;
     KvpFrame *kvp;
     KvpValue *value;
-    gint64 counter;
 
     if (!book)
     {
@@ -422,17 +420,54 @@
     if (value)
     {
         /* found it */
-        counter = kvp_value_get_gint64 (value);
+        return kvp_value_get_gint64 (value);
     }
     else
     {
         /* New counter */
-        counter = 0;
+        return 0;
     }
+}
 
-    /* Counter is now valid; increment it */
+gint64
+qof_book_increment_and_get_counter (QofBook *book, const char *counter_name)
+{
+    QofBackend *be;
+    KvpFrame *kvp;
+    KvpValue *value;
+    gint64 counter;
+
+    if (!book)
+    {
+        PWARN ("No book!!!");
+        return -1;
+    }
+
+    if (!counter_name || *counter_name == '\0')
+    {
+        PWARN ("Invalid counter name.");
+        return -1;
+    }
+
+    /* Get the current counter value from the KVP in the book. */
+    counter = qof_book_get_counter(book, counter_name);
+
+    /* Check if an error occured */
+    if (counter < 0)
+	return -1;
+
+    /* Increment the counter */
     counter++;
 
+    /* Get the KVP from the current book */
+    kvp = qof_book_get_slots (book);
+
+    if (!kvp)
+    {
+	PWARN ("Book has no KVP_Frame");
+	return -1;
+    }
+
     /* Save off the new counter */
     qof_book_begin_edit(book);
     value = kvp_value_new_gint64 (counter);
@@ -441,7 +476,6 @@
     qof_book_mark_dirty(book);
     qof_book_commit_edit(book);
 
-    /* and return the value */
     return counter;
 }
 

Modified: gnucash/trunk/src/libqof/qof/qofbook.h
===================================================================
--- gnucash/trunk/src/libqof/qof/qofbook.h	2011-01-10 21:38:43 UTC (rev 20052)
+++ gnucash/trunk/src/libqof/qof/qofbook.h	2011-01-10 21:38:54 UTC (rev 20053)
@@ -278,11 +278,17 @@
  */
 gboolean qof_book_equal (const QofBook *book_1, const QofBook *book_2);
 
-/** This will 'get and increment' the named counter for this book.
- * The return value is -1 on error or the incremented counter.
+/** This will get the named counter for this book. The return value is
+ *    -1 on error or the current value of the counter.
  */
 gint64 qof_book_get_counter (QofBook *book, const char *counter_name);
 
+/** This will increment the named counter for this book and return it.
+ *    The return value is -1 on error or the (new) value of the
+ *    counter.
+ */
+gint64 qof_book_increment_and_get_counter (QofBook *book, const char *counter_name);
+
 const char* qof_book_get_string_option(const QofBook* book, const char* opt_name);
 void qof_book_set_string_option(QofBook* book, const char* opt_name, const char* opt_val);
 



More information about the gnucash-changes mailing list