From 0c944231f61a62a1b41b8106539e628e551cb62b Mon Sep 17 00:00:00 2001 From: Phil Longstaff Date: Sat, 6 Mar 2010 11:02:09 +0000 Subject: [PATCH] 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. git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@18852 57a11ea4-9604-0410-9ed3-97b8803252fd --- src/backend/sql/gnc-backend-sql.c | 30 ++++++++++++++++--- src/backend/sql/gnc-backend-sql.h | 9 ++++++ .../sql/gncmod-business-backend-sql.c | 6 ++++ 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/backend/sql/gnc-backend-sql.c b/src/backend/sql/gnc-backend-sql.c index 3f0f7559f0..adbd63849f 100644 --- a/src/backend/sql/gnc-backend-sql.c +++ b/src/backend/sql/gnc-backend-sql.c @@ -137,9 +137,18 @@ create_tables_cb( const gchar* type, gpointer data_p, gpointer be_p ) /* ================================================================= */ +/* 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 @@ initial_load_cb( const gchar* type, gpointer data_p, gpointer be_p ) 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 @@ gnc_sql_load( GncSqlBackend* be, /*@ dependent @*/ QofBook *book, QofBackendLoad 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 ); diff --git a/src/backend/sql/gnc-backend-sql.h b/src/backend/sql/gnc-backend-sql.h index bacbd17798..55772b1598 100644 --- a/src/backend/sql/gnc-backend-sql.h +++ b/src/backend/sql/gnc-backend-sql.h @@ -716,6 +716,15 @@ gchar* gnc_sql_convert_timespec_to_string( const GncSqlBackend* be, Timespec ts 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 @*/ diff --git a/src/business/business-core/sql/gncmod-business-backend-sql.c b/src/business/business-core/sql/gncmod-business-backend-sql.c index 425e9bd55b..c240b82c8e 100644 --- a/src/business/business-core/sql/gncmod-business-backend-sql.c +++ b/src/business/business-core/sql/gncmod-business-backend-sql.c @@ -75,6 +75,10 @@ libgncmod_business_backend_sql_gnc_module_description(void) 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 @@ libgncmod_business_backend_sql_gnc_module_init(int refcount) gnc_owner_sql_initialize(); gnc_taxtable_sql_initialize(); gnc_vendor_sql_initialize(); + + gnc_sql_set_load_order( fixed_load_order ); } return TRUE;