diff --git a/src/engine/gncCustomer.c b/src/engine/gncCustomer.c index 358deea926..01ab6ea7a6 100644 --- a/src/engine/gncCustomer.c +++ b/src/engine/gncCustomer.c @@ -951,5 +951,5 @@ gboolean gncCustomerRegister (void) gint64 gncCustomerNextID (QofBook *book) { - return qof_book_get_counter (book, _GNC_MOD_NAME); + return qof_book_increment_and_get_counter (book, _GNC_MOD_NAME); } diff --git a/src/engine/gncEmployee.c b/src/engine/gncEmployee.c index 9b81d3fe92..0fce09cefb 100644 --- a/src/engine/gncEmployee.c +++ b/src/engine/gncEmployee.c @@ -742,5 +742,5 @@ gboolean gncEmployeeRegister (void) gint64 gncEmployeeNextID (QofBook *book) { - return qof_book_get_counter (book, _GNC_MOD_NAME); + return qof_book_increment_and_get_counter (book, _GNC_MOD_NAME); } diff --git a/src/engine/gncInvoice.c b/src/engine/gncInvoice.c index 5fb08afd59..ba68a1edf5 100644 --- a/src/engine/gncInvoice.c +++ b/src/engine/gncInvoice.c @@ -2131,16 +2131,16 @@ gint64 gncInvoiceNextID (QofBook *book, GncOwner *owner) 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; diff --git a/src/engine/gncJob.c b/src/engine/gncJob.c index bb69f89f74..c704d474fb 100644 --- a/src/engine/gncJob.c +++ b/src/engine/gncJob.c @@ -585,5 +585,5 @@ gboolean gncJobRegister (void) gint64 gncJobNextID (QofBook *book) { - return qof_book_get_counter (book, _GNC_MOD_NAME); + return qof_book_increment_and_get_counter (book, _GNC_MOD_NAME); } diff --git a/src/engine/gncOrder.c b/src/engine/gncOrder.c index b4c147103f..4c34821c4a 100644 --- a/src/engine/gncOrder.c +++ b/src/engine/gncOrder.c @@ -606,5 +606,5 @@ gboolean gncOrderRegister (void) gint64 gncOrderNextID (QofBook *book) { - return qof_book_get_counter (book, _GNC_MOD_NAME); + return qof_book_increment_and_get_counter (book, _GNC_MOD_NAME); } diff --git a/src/engine/gncVendor.c b/src/engine/gncVendor.c index 35e7ceb6da..9cbe36a371 100644 --- a/src/engine/gncVendor.c +++ b/src/engine/gncVendor.c @@ -861,5 +861,5 @@ gboolean gncVendorRegister (void) gint64 gncVendorNextID (QofBook *book) { - return qof_book_get_counter (book, _GNC_MOD_NAME); + return qof_book_increment_and_get_counter (book, _GNC_MOD_NAME); } diff --git a/src/libqof/qof/qofbook.c b/src/libqof/qof/qofbook.c index 69889254c6..c87b873718 100644 --- a/src/libqof/qof/qofbook.c +++ b/src/libqof/qof/qofbook.c @@ -392,10 +392,8 @@ void qof_book_set_version (QofBook *book, gint32 version) 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 @@ qof_book_get_counter (QofBook *book, const char *counter_name) if (value) { /* found it */ - counter = kvp_value_get_gint64 (value); + return kvp_value_get_gint64 (value); } else { /* New counter */ - counter = 0; + return 0; + } +} + +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; } - /* Counter is now valid; increment it */ + 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_get_counter (QofBook *book, const char *counter_name) qof_book_mark_dirty(book); qof_book_commit_edit(book); - /* and return the value */ return counter; } diff --git a/src/libqof/qof/qofbook.h b/src/libqof/qof/qofbook.h index cb0c30b734..d74686cb83 100644 --- a/src/libqof/qof/qofbook.h +++ b/src/libqof/qof/qofbook.h @@ -278,11 +278,17 @@ void qof_book_kvp_changed (QofBook *book); */ 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);