static void load_all_taxtables( GncSqlBackend* be ) { GncSqlStatement* stmt; GncSqlResult* result; g_return_if_fail( be != NULL ); /* First time, create the query */ stmt = gnc_sql_create_select_statement( be, TT_TABLE_NAME ); result = gnc_sql_execute_select_statement( be, stmt ); gnc_sql_statement_dispose( stmt ); if ( result != NULL ) { GncSqlRow* row; GList* tt_needing_parents = NULL; row = gnc_sql_result_get_first_row( result ); while ( row != NULL ) { load_single_taxtable( be, row, &tt_needing_parents ); row = gnc_sql_result_get_next_row( result ); } gnc_sql_result_dispose( result ); /* While there are items on the list of taxtables needing parents, try to see if the parent has now been loaded. Theory says that if items are removed from the front and added to the back if the parent is still not available, then eventually, the list will shrink to size 0. */ if ( tt_needing_parents != NULL ) { gboolean progress_made = TRUE; GncTaxTable* root; Account* pParent; GList* elem; while ( progress_made ) { progress_made = FALSE; for ( elem = tt_needing_parents; elem != NULL; elem = g_list_next( elem ) ) { taxtable_parent_guid_struct* s = (taxtable_parent_guid_struct*)elem->data; tt_set_parent( s->tt, &s->guid ); tt_needing_parents = g_list_delete_link( tt_needing_parents, elem ); progress_made = TRUE; } } } } }
void GncSqlTaxTableBackend::load_all (GncSqlBackend* sql_be) { g_return_if_fail (sql_be != NULL); /* First time, create the query */ std::stringstream sql; sql << "SELECT * FROM " << TT_TABLE_NAME; auto stmt = sql_be->create_statement_from_sql(sql.str()); auto result = sql_be->execute_select_statement(stmt); TaxTblParentGuidVec tt_needing_parents; for (auto row : *result) load_single_taxtable (sql_be, row, tt_needing_parents); /* While there are items on the list of taxtables needing parents, try to see if the parent has now been loaded. Theory says that if items are removed from the front and added to the back if the parent is still not available, then eventually, the list will shrink to size 0. */ if (!tt_needing_parents.empty()) { bool progress_made = true; std::reverse(tt_needing_parents.begin(), tt_needing_parents.end()); auto end = tt_needing_parents.end(); while (progress_made) { progress_made = false; end = std::remove_if(tt_needing_parents.begin(), end, [&](TaxTblParentGuidPtr s) { auto pBook = qof_instance_get_book (QOF_INSTANCE (s->tt)); auto parent = gncTaxTableLookup (pBook, &s->guid); if (parent != nullptr) { tt_set_parent (s->tt, &s->guid); progress_made = true; delete s; return true; } return false; }); } } }