r18852 - gnucash/trunk/src - Add a mechanism so that the business sql backend module can provide the main sql backend with the order in which objects should be loaded. This will allow billterms and taxtables to be loaded before objects which contain references to those objects.

Phil Longstaff plongstaff at code.gnucash.org
Sat Mar 6 06:02:09 EST 2010


Author: plongstaff
Date: 2010-03-06 06:02:09 -0500 (Sat, 06 Mar 2010)
New Revision: 18852
Trac: http://svn.gnucash.org/trac/changeset/18852

Modified:
   gnucash/trunk/src/backend/sql/gnc-backend-sql.c
   gnucash/trunk/src/backend/sql/gnc-backend-sql.h
   gnucash/trunk/src/business/business-core/sql/gncmod-business-backend-sql.c
Log:
Add a mechanism so that the business sql backend module can provide the main sql backend with the order in which objects should be loaded.  This will allow billterms and taxtables to be loaded before objects which contain references to those objects.


Modified: gnucash/trunk/src/backend/sql/gnc-backend-sql.c
===================================================================
--- gnucash/trunk/src/backend/sql/gnc-backend-sql.c	2010-03-06 11:01:37 UTC (rev 18851)
+++ gnucash/trunk/src/backend/sql/gnc-backend-sql.c	2010-03-06 11:02:09 UTC (rev 18852)
@@ -137,10 +137,19 @@
 
 /* ================================================================= */
 
+/* Main object load order */
 static const gchar* fixed_load_order[] =
-{ GNC_ID_BOOK, GNC_ID_COMMODITY, GNC_ID_ACCOUNT, GNC_ID_LOT };
-#define NUM_FIXED_LOAD_ORDER (gint)(sizeof(fixed_load_order)/sizeof(fixed_load_order[0]))
+{ GNC_ID_BOOK, GNC_ID_COMMODITY, GNC_ID_ACCOUNT, GNC_ID_LOT, NULL };
 
+/* Load order for objects from other modules */
+static const gchar** other_load_order = NULL;
+
+void
+gnc_sql_set_load_order( const gchar** load_order )
+{
+    other_load_order = load_order;
+}
+
 static void
 initial_load_cb( const gchar* type, gpointer data_p, gpointer be_p )
 {
@@ -152,9 +161,14 @@
     g_return_if_fail( pData->version == GNC_SQL_BACKEND_VERSION );
 
 	// Don't need to load anything if it has already been loaded with the fixed order
-	for( i = 0; i < NUM_FIXED_LOAD_ORDER; i++ ) {
+	for( i = 0; fixed_load_order[i] != NULL; i++ ) {
     	if( g_ascii_strcasecmp( type, fixed_load_order[i] ) == 0 ) return;
 	}
+    if( other_load_order != NULL ) {
+	    for( i = 0; other_load_order[i] != NULL; i++ ) {
+    	    if( g_ascii_strcasecmp( type, other_load_order[i] ) == 0 ) return;
+        }
+	}
 
     if( pData->initial_load != NULL ) {
         (pData->initial_load)( be );
@@ -180,12 +194,20 @@
     	be->primary_book = book;
 
     	/* Load any initial stuff. Some of this needs to happen in a certain order */
-		for( i = 0; i < NUM_FIXED_LOAD_ORDER; i++ ) {
+		for( i = 0; fixed_load_order[i] != NULL; i++ ) {
     		pData = qof_object_lookup_backend( fixed_load_order[i], GNC_SQL_BACKEND );
     		if( pData->initial_load != NULL ) {
         		(pData->initial_load)( be );
 			}
     	}
+        if( other_load_order != NULL ) {
+		    for( i = 0; other_load_order[i] != NULL; i++ ) {
+    		    pData = qof_object_lookup_backend( other_load_order[i], GNC_SQL_BACKEND );
+    		    if( pData->initial_load != NULL ) {
+        		    (pData->initial_load)( be );
+			    }
+            }
+    	}
 
 		root = gnc_book_get_root_account( book );
 		gnc_account_foreach_descendant( root, (AccountCb)xaccAccountBeginEdit, NULL );

Modified: gnucash/trunk/src/backend/sql/gnc-backend-sql.h
===================================================================
--- gnucash/trunk/src/backend/sql/gnc-backend-sql.h	2010-03-06 11:01:37 UTC (rev 18851)
+++ gnucash/trunk/src/backend/sql/gnc-backend-sql.h	2010-03-06 11:02:09 UTC (rev 18852)
@@ -716,6 +716,15 @@
 void gnc_sql_upgrade_table( GncSqlBackend* be, const gchar* table_name,
                             const GncSqlColumnTableEntry* col_table );
 
+/**
+ * Specifies the load order for a set of objects.  When loading from a database, the
+ * objects will be loaded in this order, so that when later objects have references to
+ * objects, those objects will already have been loaded.
+ *
+ * @param load_order NULL-terminated array of object type ID strings
+ */
+void gnc_sql_set_load_order( const gchar** load_order );
+
 void _retrieve_guid_( gpointer pObject, /*@ null @*/ gpointer pValue );
 
 /*@ null @*/

Modified: gnucash/trunk/src/business/business-core/sql/gncmod-business-backend-sql.c
===================================================================
--- gnucash/trunk/src/business/business-core/sql/gncmod-business-backend-sql.c	2010-03-06 11:01:37 UTC (rev 18851)
+++ gnucash/trunk/src/business/business-core/sql/gncmod-business-backend-sql.c	2010-03-06 11:02:09 UTC (rev 18852)
@@ -75,6 +75,10 @@
     return g_strdup( "The SQL backend for GnuCash business objects" );
 }
 
+/* Order in which business objects need to be loaded */
+static const gchar* fixed_load_order[] =
+{ GNC_ID_BILLTERM, GNC_ID_TAXTABLE, NULL };
+
 int
 libgncmod_business_backend_sql_gnc_module_init(int refcount)
 {
@@ -100,6 +104,8 @@
         gnc_owner_sql_initialize();
         gnc_taxtable_sql_initialize();
         gnc_vendor_sql_initialize();
+
+        gnc_sql_set_load_order( fixed_load_order );
     }
 
     return TRUE;



More information about the gnucash-changes mailing list