Skip to content

Commit

Permalink
- Add new Backend method, counter(), which increments a named counter…
Browse files Browse the repository at this point in the history
… and

  returns the new value.
- Add gnc_book_get_counter() which implements the API and calls the
  counter() method in the backend if it exists.  If not, it stores the
  counters in the Book KVP Frame.
- Modify all the Business objects to use the new counter API.  Counter is
  gint64 so modify all the users to expect that.


git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@6759 57a11ea4-9604-0410-9ed3-97b8803252fd
  • Loading branch information
derekatkins committed Mar 3, 2002
1 parent 4bacc6a commit e83341b
Show file tree
Hide file tree
Showing 23 changed files with 100 additions and 36 deletions.
2 changes: 2 additions & 0 deletions src/backend/file/gnc-backend-file.c
Expand Up @@ -183,6 +183,8 @@ gnc_backend_new(void)
be->run_query = NULL;
be->price_lookup = NULL;

be->counter = NULL;

/* the file backend will never be multi-user... */
be->events_pending = NULL;
be->process_events = NULL;
Expand Down
6 changes: 2 additions & 4 deletions src/business/business-core/gncCustomer.c
Expand Up @@ -415,9 +415,7 @@ gboolean gncCustomerRegister (void)
return gncObjectRegister (&gncCustomerDesc);
}

static gint lastCustomer = 27;

gint gncCustomerNextID (GNCBook *book)
gint64 gncCustomerNextID (GNCBook *book)
{
return ++lastCustomer; /* XXX: Look into Database! */
return gnc_book_get_counter (book, _GNC_MOD_NAME);
}
2 changes: 1 addition & 1 deletion src/business/business-core/gncCustomerP.h
Expand Up @@ -10,7 +10,7 @@
#include "gncCustomer.h"

gboolean gncCustomerRegister (void);
gint gncCustomerNextID (GNCBook *book);
gint64 gncCustomerNextID (GNCBook *book);
void gncCustomerSetGUID (GncCustomer *customer, const GUID *guid);

#endif /* GNC_CUSTOMERP_H_ */
6 changes: 2 additions & 4 deletions src/business/business-core/gncEmployee.c
Expand Up @@ -339,9 +339,7 @@ gboolean gncEmployeeRegister (void)
return gncObjectRegister (&gncEmployeeDesc);
}

static gint lastEmployee = 2;

gint gncEmployeeNextID (GNCBook *book)
gint64 gncEmployeeNextID (GNCBook *book)
{
return ++lastEmployee; /* XXX: Look into Database! */
return gnc_book_get_counter (book, _GNC_MOD_NAME);
}
2 changes: 1 addition & 1 deletion src/business/business-core/gncEmployeeP.h
Expand Up @@ -10,7 +10,7 @@
#include "gncEmployee.h"

gboolean gncEmployeeRegister (void);
gint gncEmployeeNextID (GNCBook *book);
gint64 gncEmployeeNextID (GNCBook *book);
void gncEmployeeSetGUID (GncEmployee *employee, const GUID *guid);

#endif /* GNC_EMPLOYEEP_H_ */
6 changes: 2 additions & 4 deletions src/business/business-core/gncInvoice.c
Expand Up @@ -763,9 +763,7 @@ gboolean gncInvoiceRegister (void)
return gncObjectRegister (&gncInvoiceDesc);
}

static gint lastId = 187; /* XXX */

gint gncInvoiceNextID (GNCBook *book)
gint64 gncInvoiceNextID (GNCBook *book)
{
return lastId++;
return gnc_book_get_counter (book, _GNC_MOD_NAME);
}
2 changes: 1 addition & 1 deletion src/business/business-core/gncInvoiceP.h
Expand Up @@ -12,7 +12,7 @@
#include "Transaction.h"

gboolean gncInvoiceRegister (void);
gint gncInvoiceNextID (GNCBook *book);
gint64 gncInvoiceNextID (GNCBook *book);
void gncInvoiceSetGUID (GncInvoice *invoice, const GUID *guid);
void gncInvoiceSetDirty (GncInvoice *invoice, gboolean dirty);
void gncInvoiceSetPostedAcc (GncInvoice *invoice, Account *acc);
Expand Down
6 changes: 2 additions & 4 deletions src/business/business-core/gncJob.c
Expand Up @@ -329,9 +329,7 @@ gboolean gncJobRegister (void)
return gncObjectRegister (&gncJobDesc);
}

static gint lastJob = 57;

gint gncJobNextID (GNCBook *book)
gint64 gncJobNextID (GNCBook *book)
{
return ++lastJob; /* XXX: Look into Database! */
return gnc_book_get_counter (book, _GNC_MOD_NAME);
}
2 changes: 1 addition & 1 deletion src/business/business-core/gncJobP.h
Expand Up @@ -10,7 +10,7 @@
#include "gncJob.h"

gboolean gncJobRegister (void);
gint gncJobNextID (GNCBook *book);
gint64 gncJobNextID (GNCBook *book);
void gncJobSetGUID (GncJob *job, const GUID *guid);

#endif /* GNC_JOBP_H_ */
6 changes: 2 additions & 4 deletions src/business/business-core/gncOrder.c
Expand Up @@ -400,9 +400,7 @@ gboolean gncOrderRegister (void)
return gncObjectRegister (&gncOrderDesc);
}

static gint lastId = 471; /* XXX */

gint gncOrderNextID (GNCBook *book)
gint64 gncOrderNextID (GNCBook *book)
{
return lastId++;
return gnc_book_get_counter (book, _GNC_MOD_NAME);
}
2 changes: 1 addition & 1 deletion src/business/business-core/gncOrderP.h
Expand Up @@ -10,7 +10,7 @@
#include "gncOrder.h"

gboolean gncOrderRegister (void);
gint gncOrderNextID (GNCBook *book);
gint64 gncOrderNextID (GNCBook *book);
void gncOrderSetGUID (GncOrder *order, const GUID *guid);
void gncOrderSetDirty (GncOrder *order, gboolean dirty);

Expand Down
6 changes: 2 additions & 4 deletions src/business/business-core/gncVendor.c
Expand Up @@ -371,9 +371,7 @@ gboolean gncVendorRegister (void)
return gncObjectRegister (&gncVendorDesc);
}

static gint lastVendor = 17;

gint gncVendorNextID (GNCBook *book)
gint64 gncVendorNextID (GNCBook *book)
{
return ++lastVendor; /* XXX: Look into Database! */
return gnc_book_get_counter (book, _GNC_MOD_NAME);
}
2 changes: 1 addition & 1 deletion src/business/business-core/gncVendorP.h
Expand Up @@ -10,7 +10,7 @@
#include "gncVendor.h"

gboolean gncVendorRegister (void);
gint gncVendorNextID (GNCBook *book);
gint64 gncVendorNextID (GNCBook *book);
void gncVendorSetGUID (GncVendor *vendor, const GUID *guid);

#endif /* GNC_VENDORP_H_ */
2 changes: 1 addition & 1 deletion src/business/business-gnome/dialog-customer.c
Expand Up @@ -552,7 +552,7 @@ gnc_customer_new_window (GtkWidget *parent, GNCBook *bookp,

cw->dialog_type = NEW_CUSTOMER;
gtk_entry_set_text (GTK_ENTRY (cw->id_entry),
g_strdup_printf ("%.6d", gncCustomerNextID(bookp)));
g_strdup_printf ("%.6lld", gncCustomerNextID(bookp)));
cw->component_id =
gnc_register_gui_component (DIALOG_NEW_CUSTOMER_CM_CLASS,
gnc_customer_window_refresh_handler,
Expand Down
2 changes: 1 addition & 1 deletion src/business/business-gnome/dialog-employee.c
Expand Up @@ -467,7 +467,7 @@ gnc_employee_new_window (GtkWidget *parent, GNCBook *bookp,

ew->dialog_type = NEW_EMPLOYEE;
gtk_entry_set_text (GTK_ENTRY (ew->id_entry),
g_strdup_printf ("%.6d", gncEmployeeNextID(bookp)));
g_strdup_printf ("%.6lld", gncEmployeeNextID(bookp)));
ew->component_id =
gnc_register_gui_component (DIALOG_NEW_EMPLOYEE_CM_CLASS,
gnc_employee_window_refresh_handler,
Expand Down
2 changes: 1 addition & 1 deletion src/business/business-gnome/dialog-invoice.c
Expand Up @@ -846,7 +846,7 @@ gnc_invoice_window_new_invoice (GtkWidget *parent, GNCBook *bookp,
iw->invoice_guid = *gncInvoiceGetGUID (invoice);

gtk_entry_set_text (GTK_ENTRY (iw->id_entry),
g_strdup_printf ("%.6d", gncInvoiceNextID(bookp)));
g_strdup_printf ("%.6lld", gncInvoiceNextID(bookp)));

iw->component_id =
gnc_register_gui_component (DIALOG_NEW_INVOICE_CM_CLASS,
Expand Down
2 changes: 1 addition & 1 deletion src/business/business-gnome/dialog-job.c
Expand Up @@ -351,7 +351,7 @@ gnc_job_new_window (GtkWidget *parent, GNCBook *bookp, GncOwner *owner,
bookp, owner);

gtk_entry_set_text (GTK_ENTRY (jw->id_entry),
g_strdup_printf ("%.6d", gncJobNextID(bookp)));
g_strdup_printf ("%.6lld", gncJobNextID(bookp)));

jw->component_id = gnc_register_gui_component (DIALOG_NEW_JOB_CM_CLASS,
gnc_job_window_refresh_handler,
Expand Down
2 changes: 1 addition & 1 deletion src/business/business-gnome/dialog-order.c
Expand Up @@ -717,7 +717,7 @@ gnc_order_window_new_order (GtkWidget *parent, GNCBook *bookp, GncOwner *owner)
/* Setup initial values */
ow->order_guid = *gncOrderGetGUID (order);
gtk_entry_set_text (GTK_ENTRY (ow->id_entry),
g_strdup_printf ("%.6d", gncOrderNextID(bookp)));
g_strdup_printf ("%.6lld", gncOrderNextID(bookp)));

ow->component_id =
gnc_register_gui_component (DIALOG_NEW_ORDER_CM_CLASS,
Expand Down
2 changes: 1 addition & 1 deletion src/business/business-gnome/dialog-vendor.c
Expand Up @@ -421,7 +421,7 @@ gnc_vendor_new_window (GtkWidget *parent, GNCBook *bookp,

vw->dialog_type = NEW_VENDOR;
gtk_entry_set_text (GTK_ENTRY (vw->id_entry),
g_strdup_printf ("%.6d", gncVendorNextID(bookp)));
g_strdup_printf ("%.6lld", gncVendorNextID(bookp)));
vw->component_id =
gnc_register_gui_component (DIALOG_NEW_VENDOR_CM_CLASS,
gnc_vendor_window_refresh_handler,
Expand Down
5 changes: 5 additions & 0 deletions src/engine/BackendP.h
Expand Up @@ -143,6 +143,9 @@
* needed to update an existing transaction. It might pull new
* currencies (??))
*
* The counter() routine increments the named counter and returns the
* post-incremented value. Returns -1 if there is a problem.
*
* The events_pending() routines should return true if there are
* external events which need to be processed to bring the
* engine up to date with the backend.
Expand Down Expand Up @@ -242,6 +245,8 @@ struct backend_s
void (*sync_group) (Backend *, GNCBook *);
void (*sync_price) (Backend *, GNCBook *);

gint64 (*counter) (Backend *, const char *counter_name);

gboolean (*events_pending) (Backend *be);
gboolean (*process_events) (Backend *be);

Expand Down
54 changes: 54 additions & 0 deletions src/engine/gnc-book.c
Expand Up @@ -439,4 +439,58 @@ gnc_book_count_transactions(GNCBook *book)
return count;
}

/* ====================================================================== */

gint64
gnc_book_get_counter (GNCBook *book, const char *counter_name)
{
Backend *be;
kvp_frame *kvp;
kvp_value *value;
gint64 counter;

if (!book) {
PWARN ("No book!!!");
return -1;
}

if (!counter_name || *counter_name == '\0') {
PWARN ("Invalid counter name.");
return -1;
}

/* If we've got a backend with a counter method, call it */
be = book->backend;
if (be && be->counter)
return ((be->counter)(be, counter_name));

/* If not, then use the KVP in the book */
kvp = gnc_book_get_slots (book);

if (!kvp) {
PWARN ("Book has no KVP_Frame");
return -1;
}

value = kvp_frame_get_slot_path (kvp, "counters", counter_name, NULL);
if (value) {
/* found it */
counter = kvp_value_get_gint64 (value);
} else {
/* New counter */
counter = 0;
}

/* Counter is now valid; increment it */
counter++;

/* Save off the new counter */
value = kvp_value_new_gint64 (counter);
kvp_frame_set_slot_path (kvp, value, "counters", counter_name, NULL);
kvp_value_delete (value);

/* and return the value */
return counter;
}

/* ========================== END OF FILE =============================== */
5 changes: 5 additions & 0 deletions src/engine/gnc-book.h
Expand Up @@ -91,4 +91,9 @@ gboolean gnc_book_equal (GNCBook *book_1, GNCBook *book_2);
* to be moved to some utility/support file. */
guint gnc_book_count_transactions(GNCBook *book);

/* This will 'get and increment' the named counter for this book.
* The return value is -1 on error or the incremented counter.
*/
gint64 gnc_book_get_counter (GNCBook *book, const char *counter_name);

#endif /* GNC_BOOK_H */
10 changes: 10 additions & 0 deletions src/engine/kvp_doc.txt
Expand Up @@ -107,6 +107,16 @@ Use: The GUID of the book that preceeds this one, chronologically.

-----------------------

Name: /counters/...
Type: int64
Entities: Book
Use: Holders for a bunch of counters for various types. Used specifically
in the business objects for ID counters. The counter name is the path
that follows /counters/, e.g. "/counters/GncCustomer"


-----------------------

Name: from-sched-xaction
Type: GUID
Entities: Transaction
Expand Down

0 comments on commit e83341b

Please sign in to comment.