示例#1
0
static void
load_all_employees (GncSqlBackend* be)
{
    GncSqlStatement* stmt;
    GncSqlResult* result;

    g_return_if_fail (be != NULL);

    stmt = gnc_sql_create_select_statement (be, TABLE_NAME);
    result = gnc_sql_execute_select_statement (be, stmt);
    gnc_sql_statement_dispose (stmt);
    if (result != NULL)
    {
        GncSqlRow* row;
        GList* list = NULL;

        row = gnc_sql_result_get_first_row (result);
        while (row != NULL)
        {
            GncEmployee* pEmployee = load_single_employee (be, row);
            if (pEmployee != NULL)
            {
                list = g_list_append (list, pEmployee);
            }
            row = gnc_sql_result_get_next_row (result);
        }
        gnc_sql_result_dispose (result);

        if (list != NULL)
        {
            gnc_sql_slots_load_for_list (be, list);
            g_list_free (list);
        }
    }
}
示例#2
0
static void
load_splits_for_tx_list (GncSqlBackend* be, GList* list)
{
    GString* sql;
    GncSqlResult* result;

    g_return_if_fail (be != NULL);

    if (list == NULL) return;

    sql = g_string_sized_new (40 + (GUID_ENCODING_LENGTH + 3) * g_list_length (
                                  list));
    g_string_append_printf (sql, "SELECT * FROM %s WHERE %s IN (", SPLIT_TABLE,
                            tx_guid_col_table[0].col_name);
    (void)gnc_sql_append_guid_list_to_sql (sql, list, G_MAXUINT);
    (void)g_string_append (sql, ")");

    // Execute the query and load the splits
    result = gnc_sql_execute_select_sql (be, sql->str);
    if (result != NULL)
    {
        GList* split_list = NULL;
        GncSqlRow* row;

        row = gnc_sql_result_get_first_row (result);
        while (row != NULL)
        {
            Split* s;
            s = load_single_split (be, row);
            if (s != NULL)
            {
                split_list = g_list_prepend (split_list, s);
            }
            row = gnc_sql_result_get_next_row (result);
        }

        if (split_list != NULL)
        {
            gnc_sql_slots_load_for_list (be, split_list);
            g_list_free (split_list);
        }

        gnc_sql_result_dispose (result);
    }
    (void)g_string_free (sql, TRUE);
}
示例#3
0
static void
load_all_billterms (GncSqlBackend* be)
{
    GncSqlStatement* stmt;
    GncSqlResult* result;

    g_return_if_fail (be != NULL);

    stmt = gnc_sql_create_select_statement (be, TABLE_NAME);
    result = gnc_sql_execute_select_statement (be, stmt);
    gnc_sql_statement_dispose (stmt);
    if (result != NULL)
    {
        GncSqlRow* row;
        GList* list = NULL;
        GList* l_billterms_needing_parents = NULL;

        row = gnc_sql_result_get_first_row (result);
        while (row != NULL)
        {
            GncBillTerm* pBillTerm = load_single_billterm (be, row,
                                                           &l_billterms_needing_parents);
            if (pBillTerm != NULL)
            {
                list = g_list_append (list, pBillTerm);
            }
            row = gnc_sql_result_get_next_row (result);
        }
        gnc_sql_result_dispose (result);

        if (list != NULL)
        {
            gnc_sql_slots_load_for_list (be, list);
            g_list_free (list);
        }

        /* While there are items on the list of billterms 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 (l_billterms_needing_parents != NULL)
        {
            gboolean progress_made = TRUE;
            GList* elem;

            while (progress_made)
            {
                progress_made = FALSE;
                for (elem = l_billterms_needing_parents; elem != NULL;
                     elem = g_list_next (elem))
                {
                    billterm_parent_guid_struct* s = (billterm_parent_guid_struct*)elem->data;
                    bt_set_parent (s->billterm, &s->guid);
                    l_billterms_needing_parents = g_list_delete_link (l_billterms_needing_parents,
                                                                      elem);
                    progress_made = TRUE;
                }
            }
        }
    }
}
示例#4
0
/**
 * Executes a transaction query statement and loads the transactions and all
 * of the splits.
 *
 * @param be SQL backend
 * @param stmt SQL statement
 */
static void
query_transactions( GncSqlBackend* be, GncSqlStatement* stmt )
{
    GncSqlResult* result;

    g_return_if_fail( be != NULL );
    g_return_if_fail( stmt != NULL );

    result = gnc_sql_execute_select_statement( be, stmt );
    if ( result != NULL )
    {
        GList* tx_list = NULL;
        GList* node;
        GncSqlRow* row;
        Transaction* tx;
#if LOAD_TRANSACTIONS_AS_NEEDED
        GSList* bal_list = NULL;
        GSList* nextbal;
        Account* root = gnc_book_get_root_account( be->book );

        qof_event_suspend();
        xaccAccountBeginEdit( root );

        // Save the start/ending balances (balance, cleared and reconciled) for
        // every account.
        gnc_account_foreach_descendant( gnc_book_get_root_account( be->primary_book ),
                                        save_account_balances,
                                        &bal_list );
#endif

        // Load the transactions
        row = gnc_sql_result_get_first_row( result );
        while ( row != NULL )
        {
            tx = load_single_tx( be, row );
            if ( tx != NULL )
            {
                tx_list = g_list_prepend( tx_list, tx );
            }
            row = gnc_sql_result_get_next_row( result );
        }
        gnc_sql_result_dispose( result );

        // Load all splits and slots for the transactions
        if ( tx_list != NULL )
        {
            gnc_sql_slots_load_for_list( be, tx_list );
            load_splits_for_tx_list( be, tx_list );
        }

        // Commit all of the transactions
        for ( node = tx_list; node != NULL; node = node->next )
        {
            Transaction* pTx = GNC_TRANSACTION(node->data);
            xaccTransCommitEdit( pTx );
        }
        g_list_free( tx_list );

#if LOAD_TRANSACTIONS_AS_NEEDED
        // Update the account balances based on the loaded splits.  If the end
        // balance has changed, update the start balance so that the end
        // balance is the same as it was before the splits were loaded.
        // Repeat for cleared and reconciled balances.
        for ( nextbal = bal_list; nextbal != NULL; nextbal = nextbal->next )
        {
            full_acct_balances_t* balns = (full_acct_balances_t*)nextbal->data;
            gnc_numeric* pnew_end_bal;
            gnc_numeric* pnew_end_c_bal;
            gnc_numeric* pnew_end_r_bal;
            gnc_numeric adj;

            g_object_get( balns->acc,
                          "end-balance", &pnew_end_bal,
                          "end-cleared-balance", &pnew_end_c_bal,
                          "end-reconciled-balance", &pnew_end_r_bal,
                          NULL );

            if ( !gnc_numeric_eq( *pnew_end_bal, balns->end_bal ) )
            {
                adj = gnc_numeric_sub( balns->end_bal, *pnew_end_bal,
                                       GNC_DENOM_AUTO, GNC_HOW_DENOM_LCD );
                balns->start_bal = gnc_numeric_add( balns->start_bal, adj,
                                                    GNC_DENOM_AUTO, GNC_HOW_DENOM_LCD );
                g_object_set( balns->acc, "start-balance", &balns->start_bal, NULL );
            }
            if ( !gnc_numeric_eq( *pnew_end_c_bal, balns->end_cleared_bal ) )
            {
                adj = gnc_numeric_sub( balns->end_cleared_bal, *pnew_end_c_bal,
                                       GNC_DENOM_AUTO, GNC_HOW_DENOM_LCD );
                balns->start_cleared_bal = gnc_numeric_add( balns->start_cleared_bal, adj,
                                           GNC_DENOM_AUTO, GNC_HOW_DENOM_LCD );
                g_object_set( balns->acc, "start-cleared-balance", &balns->start_cleared_bal, NULL );
            }
            if ( !gnc_numeric_eq( *pnew_end_r_bal, balns->end_reconciled_bal ) )
            {
                adj = gnc_numeric_sub( balns->end_reconciled_bal, *pnew_end_r_bal,
                                       GNC_DENOM_AUTO, GNC_HOW_DENOM_LCD );
                balns->start_reconciled_bal = gnc_numeric_add( balns->start_reconciled_bal, adj,
                                              GNC_DENOM_AUTO, GNC_HOW_DENOM_LCD );
                g_object_set( balns->acc, "start-reconciled-balance", &balns->start_reconciled_bal, NULL );
            }
            xaccAccountRecomputeBalance( balns->acc );
            g_free( pnew_end_bal );
            g_free( pnew_end_c_bal );
            g_free( pnew_end_r_bal );
            g_free( balns );
        }
        if ( bal_list != NULL )
        {
            g_slist_free( bal_list );
        }

        xaccAccountCommitEdit( root );
        qof_event_resume();
#endif
    }
}