On Tuesday August 17 2010 20:54:42 Christian Stimming wrote:
> Am Monday 16 August 2010 schrieb Mike Evans:
> > I've attached a patch to move utils.c utils.h to src/engine as discussed
> > in Bug 625193.
> 
> Committed to SVN, thanks.
> 
> One suggestion for the function naming (in gncIDsearch.h): The functions
> which belong to a hypothetical "core gnucash library" should all begin
> with "gnc_", and the next word may or may not point to the main object on
> which this function operates. In this case, I'd suggest one of the
> following namings: * gnc_search_customer_on_id
> * gnc_book_search_by_id_customer
> * gnc_book_search_customer_by_id
> 
> Regards,
> 
> Christian
Attached is a patch to; add vendor search, to python bindings and refactors  
files gncIDSearch.c/h with vendor search.  Both patch files need to be applied.


-- 
GPG Key: 1024D/050895C2
Keyserver: http://pgp.mit.edu/          
Search String: 0x050895C2
diff --git src/optional/python-bindings/gnucash_core.py src/optional/python-bindings/gnucash_core.py
index bb0ba21..fb91912 100644
--- src/optional/python-bindings/gnucash_core.py
+++ src/optional/python-bindings/gnucash_core.py
@@ -31,8 +31,8 @@ from function_class import \
 from gnucash_core_c import gncInvoiceLookup, gncInvoiceGetInvoiceFromTxn, \
     gncInvoiceGetInvoiceFromLot, gncEntryLookup, gncInvoiceLookup, \
     gncCustomerLookup, gncVendorLookup, gncJobLookup, gncEmployeeLookup, \
-    gncTaxTableLookup, gncTaxTableLookupByName, search_invoice_on_id, \
-    search_customer_on_id, search_bill_on_id
+    gncTaxTableLookup, gncTaxTableLookupByName, gnc_search_invoice_on_id, \
+    gnc_search_customer_on_id, gnc_search_bill_on_id , gnc_search_vendor_on_id
     
 
 class GnuCashCoreClass(ClassFromFunctions):
@@ -192,17 +192,22 @@ class Book(GnuCashCoreClass):
     def BillLoookupByID(self, id):
         from gnucash_business import Bill
         return self.do_lookup_create_oo_instance(
-            search_bill_on_id, Bill, id)
+            gnc_search_bill_on_id, Bill, id)
 
     def InvoiceLookupByID(self, id):
         from gnucash_business import Invoice
         return self.do_lookup_create_oo_instance(
-            search_invoice_on_id, Invoice, id)
+            gnc_search_invoice_on_id, Invoice, id)
 
     def CustomerLookupByID(self, id):
         from gnucash_business import Customer
         return self.do_lookup_create_oo_instance(
-            search_customer_on_id, Customer, id)
+            gnc_search_customer_on_id, Customer, id)
+
+    def VendorLookupByID(self, id):
+        from gnucash_business import Vendor
+        return self.do_lookup_create_oo_instance(
+            gnc_search_vendor_on_id, Vendor, id)
 
 class GncNumeric(GnuCashCoreClass):
     """Object used by GnuCash to store all numbers. Always consists of a
diff --git src/engine/gncIDSearch.c src/engine/gncIDSearch.c
index 3237fee..f398b71 100644
--- src/engine/gncIDSearch.c
+++ src/engine/gncIDSearch.c
@@ -23,128 +23,65 @@
 
 #include "gncIDSearch.h"
 
+GNCIdType type = NULL;
 
 /***********************************************************************
- * Search the book for a Customer with the same ID.  If it exists return a
- * Customer object, if nit then return NULL.
+ * Search the book for a Customer/Invoice/Bill with the same ID.
+ * If it exists return a valid object, if not then returns NULL.
  @param QofBook	The book
  @param gchar ID of the Customer
  @return GncCustomer * Pointer to the customer or NULL of there is no customer
- *
  **********************************************************************/
 GncCustomer *
-search_customer_on_id (QofBook * book, const gchar *id)
+gnc_search_customer_on_id (QofBook * book, const gchar *id)
 {
-	QueryNew *q;
-	GNCIdType type = GNC_CUSTOMER_MODULE_NAME;
-	GList *result; // do not free this!
-	QueryPredData_t string_pred_data;
 	GncCustomer *customer = NULL;
-	gint len;
-
-	g_return_val_if_fail (id, NULL);
-	g_return_val_if_fail (book, NULL);
-
-	// Build the query
-	q = gncQueryCreateFor (type);
-	gncQuerySetBook (q, book);
-
-	// Search only the customer id field
-	string_pred_data = gncQueryStringPredicate (COMPARE_EQUAL, id, STRING_MATCH_NORMAL, FALSE);
-	gncQueryAddTerm (q, gncQueryBuildParamList(CUSTOMER_ID), string_pred_data, QUERY_AND);
-
-	// Run the query
-	result = gncQueryRun (q);
-
-	// now compare _exactly_
-	len = g_list_length (result);
-	if (result && (len>0)) {
-		result = g_list_first (result);
-		while (result) {
-			GncCustomer *c = result->data;
-			if (strcmp(id,gncCustomerGetID(c)) == 0) {
-				// correct id found
-				customer = c;
-				break;
-			}
-			result = g_list_next (result);
-		}
-	}
-
-	gncQueryDestroy (q);
+	type = GNC_CUSTOMER_MODULE_NAME;
+	customer = (GncCustomer*)search(book, id, customer);
 	return customer;
 }
 
-/***********************************************************************
- * Search the book for an Invoice with the same ID.  If it exists return an
- * Invoice object, if not then return NULL.
- @param QofBook	The book
- @param gchar ID of the invoice
- @return GncCustomer * Pointer to the Invoice or NULL of there is no customer
- *
- **********************************************************************/
 GncInvoice *
-search_invoice_on_id(QofBook *book, const gchar *id)
+gnc_search_invoice_on_id (QofBook * book, const gchar *id)
 {
-	QueryNew *q;
-	GNCIdType type = GNC_INVOICE_MODULE_NAME;
-	GList *result; // do not free this!
-	QueryPredData_t string_pred_data;
 	GncInvoice *invoice = NULL;
-	gint len;
-
-	g_return_val_if_fail (id, NULL);
-	g_return_val_if_fail (book, NULL);
-
-	// Build the query
-	q = gncQueryCreateFor (type);
-	gncQuerySetBook (q, book);
-
-	// Search only the invoice id field
-	string_pred_data = gncQueryStringPredicate (COMPARE_EQUAL, id, STRING_MATCH_NORMAL, FALSE);
-	gncQueryAddTerm (q, gncQueryBuildParamList(INVOICE_ID), string_pred_data, QUERY_AND);
-
-	// Run the query
-	result = gncQueryRun (q);
-
-	// now compare _exactly_
-	len = g_list_length (result);
-	if (result && (len>0)) {
-		result = g_list_first (result);
-		while (result) {
-			GncInvoice *c = result->data;
-			if (strcmp(id,gncInvoiceGetID(c)) == 0) {
-				// correct id found
-				invoice = c;
-				break;
-			}
-			result = g_list_next (result);
+	type = GNC_INVOICE_MODULE_NAME;
+	invoice = (GncInvoice*)search(book,id, invoice);
+	return invoice;
 		}
+
+/* Essentially identical to above.*/
+GncInvoice *
+gnc_search_bill_on_id (QofBook * book, const gchar *id)
+{
+	GncInvoice *bill =  NULL;
+	type = GNC_INVOICE_MODULE_NAME;
+	bill = (GncInvoice*)search(book, id, bill);
+	return bill;
 	}
 
-	gncQueryDestroy (q);
-	return invoice;
+GncVendor *
+gnc_search_vendor_on_id (QofBook * book, const gchar *id)
+{
+	GncVendor *vendor =  NULL;
+	type = GNC_VENDOR_MODULE_NAME;
+	vendor = (GncVendor*)search(book, id, vendor);
+	return vendor;
 }
 
 
-/***********************************************************************
- * Search the book for a Bill with the same ID.  If it exists return an
- * Invoice object, if not then return NULL.
- @param QofBook	The book
- @param gchar ID of the invoice
- @return GncCustomer * Pointer to the Invoice or NULL of there is no customer
- *
- **********************************************************************/
-GncInvoice *
-search_bill_on_id(QofBook *book, const gchar *id)
+/******************************************************************
+ * Generic search called after setting up stuff
+ * DO NOT call directly but type tests should fail anyway
+ ****************************************************************/
+void * search(QofBook * book, const gchar *id, void * object)
 {
+	void *c;
+	GList *result;
 	QueryNew *q;
-	GNCIdType type = GNC_INVOICE_MODULE_NAME;
-	GList *result; // do not free this!
-	QueryPredData_t string_pred_data;
-	GncInvoice *bill = NULL;
 	gint len;
-
+	QueryPredData_t string_pred_data;
+	g_return_val_if_fail (type, NULL);
 	g_return_val_if_fail (id, NULL);
 	g_return_val_if_fail (book, NULL);
 
@@ -152,9 +89,24 @@ search_bill_on_id(QofBook *book, const gchar *id)
 	q = gncQueryCreateFor (type);
 	gncQuerySetBook (q, book);
 
-	// Search only the invoice id field
+	// Search only the id field
 	string_pred_data = gncQueryStringPredicate (COMPARE_EQUAL, id, STRING_MATCH_NORMAL, FALSE);
+
+	if (strcmp(type,GNC_CUSTOMER_MODULE_NAME))
+	{
+		GncCustomer *c = NULL;
+		gncQueryAddTerm (q, gncQueryBuildParamList(CUSTOMER_ID), string_pred_data, QUERY_AND);
+	}
+	else if (strcmp(type,GNC_INVOICE_MODULE_NAME))
+	{
+		GncInvoice *c = NULL;
 	gncQueryAddTerm (q, gncQueryBuildParamList(INVOICE_ID), string_pred_data, QUERY_AND);
+	}
+	else if (strcmp(type,GNC_VENDOR_MODULE_NAME))
+	{
+		GncVendor *c = NULL;
+		gncQueryAddTerm (q, gncQueryBuildParamList(VENDOR_ID), string_pred_data, QUERY_AND);
+	}
 
 	// Run the query
 	result = gncQueryRun (q);
@@ -164,16 +116,15 @@ search_bill_on_id(QofBook *book, const gchar *id)
 	if (result && (len>0)) {
 		result = g_list_first (result);
 		while (result) {
-			GncInvoice *c = result->data;
-			if (strcmp(id,gncInvoiceGetID(c)) == 0) {
+			c = result->data;
+			if (strcmp(id,gncCustomerGetID(c)) == 0) {
 				// correct id found
-				bill = c;
+				object = c;
 				break;
 			}
 			result = g_list_next (result);
 		}
 	}
-
 	gncQueryDestroy (q);
-	return bill;
+	return object;
 }
diff --git src/engine/gncIDSearch.h src/engine/gncIDSearch.h
index d1b377b..4e76434 100644
--- src/engine/gncIDSearch.h
+++ src/engine/gncIDSearch.h
@@ -36,12 +36,14 @@
 #include "QueryNew.h"
 #include "GNCId.h"
 
-#ifndef GNC_PLUGIN_invoice_import_invoice_import_H
-#define GNC_PLUGIN_invoice_import_invoice_import_H
 
+#ifndef GNC_invoice_import_invoice_import_H
+#define GNC_invoice_import_invoice_import_H
 
-GncCustomer * search_customer_on_id  (QofBook *book, const gchar *id);
-GncInvoice  * search_invoice_on_id   (QofBook *book, const gchar *id);
-GncInvoice  * search_bill_on_id   (QofBook *book, const gchar *id);
+void * search(QofBook * book, const gchar *id, void * object);
+GncCustomer * gnc_search_customer_on_id  (QofBook *book, const gchar *id);
+GncInvoice  * gnc_search_invoice_on_id   (QofBook *book, const gchar *id);
+GncInvoice  * gnc_search_bill_on_id   (QofBook *book, const gchar *id);
+GncVendor  * gnc_search_vendor_on_id   (QofBook *book, const gchar *id);
 
 #endif
_______________________________________________
gnucash-devel mailing list
gnucash-devel@gnucash.org
https://lists.gnucash.org/mailman/listinfo/gnucash-devel

Reply via email to