Re-indentation of source code (sql parts).

This also strips trailing whitespaces from lines where they existed.
This re-indentation was done using astyle-1.24 using the following options:

astyle --indent=spaces=4 --brackets=break --pad-oper --pad-header --suffix

git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@18971 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Christian Stimming 2010-03-27 21:02:18 +00:00
parent 88d3b83579
commit 50163126ea
17 changed files with 4169 additions and 3357 deletions

View File

@ -323,7 +323,8 @@ mysql_error_fn( dbi_conn conn, void* user_data )
{ {
PERR( "DBI error: %s - Failed to reconnect after %d attempts.\n", msg, DBI_MAX_CONN_ATTEMPTS ); PERR( "DBI error: %s - Failed to reconnect after %d attempts.\n", msg, DBI_MAX_CONN_ATTEMPTS );
gnc_dbi_set_error( dbi_conn, ERR_BACKEND_CANT_CONNECT, 0, FALSE ); gnc_dbi_set_error( dbi_conn, ERR_BACKEND_CANT_CONNECT, 0, FALSE );
} else }
else
{ {
PINFO( "DBI error: %s - Reconnecting...\n", msg ); PINFO( "DBI error: %s - Reconnecting...\n", msg );
gnc_dbi_set_error( dbi_conn, ERR_BACKEND_CONN_LOST, 1, TRUE ); gnc_dbi_set_error( dbi_conn, ERR_BACKEND_CONN_LOST, 1, TRUE );
@ -811,10 +812,12 @@ gnc_dbi_sync_all( QofBackend* qbe, /*@ dependent @*/ QofBook *book )
const gchar* table_name = (const gchar*)node->data; const gchar* table_name = (const gchar*)node->data;
dbi_result result; dbi_result result;
do { do
{
gnc_dbi_init_error( ((GncDbiSqlConnection*)(be->sql_be.conn)) ); gnc_dbi_init_error( ((GncDbiSqlConnection*)(be->sql_be.conn)) );
result = dbi_conn_queryf( be->conn, "DROP TABLE %s", table_name ); result = dbi_conn_queryf( be->conn, "DROP TABLE %s", table_name );
} while ( ((GncDbiSqlConnection*)(be->sql_be.conn))->retry ); }
while ( ((GncDbiSqlConnection*)(be->sql_be.conn))->retry );
if ( result != NULL ) if ( result != NULL )
{ {
status = dbi_result_free( result ); status = dbi_result_free( result );
@ -1445,10 +1448,12 @@ conn_execute_select_statement( GncSqlConnection* conn, GncSqlStatement* stmt )
dbi_result result; dbi_result result;
DEBUG( "SQL: %s\n", dbi_stmt->sql->str ); DEBUG( "SQL: %s\n", dbi_stmt->sql->str );
do { do
{
gnc_dbi_init_error( dbi_conn ); gnc_dbi_init_error( dbi_conn );
result = dbi_conn_query( dbi_conn->conn, dbi_stmt->sql->str ); result = dbi_conn_query( dbi_conn->conn, dbi_stmt->sql->str );
} while ( dbi_conn->retry ); }
while ( dbi_conn->retry );
if ( result == NULL ) if ( result == NULL )
{ {
PERR( "Error executing SQL %s\n", dbi_stmt->sql->str ); PERR( "Error executing SQL %s\n", dbi_stmt->sql->str );
@ -1467,10 +1472,12 @@ conn_execute_nonselect_statement( GncSqlConnection* conn, GncSqlStatement* stmt
gint status; gint status;
DEBUG( "SQL: %s\n", dbi_stmt->sql->str ); DEBUG( "SQL: %s\n", dbi_stmt->sql->str );
do { do
{
gnc_dbi_init_error( dbi_conn ); gnc_dbi_init_error( dbi_conn );
result = dbi_conn_query( dbi_conn->conn, dbi_stmt->sql->str ); result = dbi_conn_query( dbi_conn->conn, dbi_stmt->sql->str );
} while( dbi_conn->retry ); }
while ( dbi_conn->retry );
if ( result == NULL ) if ( result == NULL )
{ {
PERR( "Error executing SQL %s\n", dbi_stmt->sql->str ); PERR( "Error executing SQL %s\n", dbi_stmt->sql->str );
@ -1549,10 +1556,12 @@ conn_begin_transaction( /*@ unused @*/ GncSqlConnection* conn )
DEBUG( "BEGIN\n" ); DEBUG( "BEGIN\n" );
do { do
{
gnc_dbi_init_error( dbi_conn ); gnc_dbi_init_error( dbi_conn );
result = dbi_conn_queryf( dbi_conn->conn, "BEGIN" ); result = dbi_conn_queryf( dbi_conn->conn, "BEGIN" );
} while( dbi_conn->retry ); }
while ( dbi_conn->retry );
status = dbi_result_free( result ); status = dbi_result_free( result );
if ( status < 0 ) if ( status < 0 )

View File

@ -39,7 +39,8 @@ static QofLogModule log_module = GNC_MOD_BACKEND;
/* ================================================ */ /* ================================================ */
struct _escape { struct _escape
{
/* pointer to memory used for escaping arguments */ /* pointer to memory used for escaping arguments */
char * escape; char * escape;
size_t esc_buflen; size_t esc_buflen;
@ -62,7 +63,8 @@ sqlEscapeString (sqlEscape *b, const char *str)
if (!b || !str) { LEAVE("(null) args"); return NULL; } if (!b || !str) { LEAVE("(null) args"); return NULL; }
/* if a string is escaped twice, just return the first */ /* if a string is escaped twice, just return the first */
if (b->escape == str) { if (b->escape == str)
{
LEAVE("%s: already escaped", str); LEAVE("%s: already escaped", str);
return str; return str;
} }
@ -70,7 +72,8 @@ sqlEscapeString (sqlEscape *b, const char *str)
/* if nothing to escape, just return */ /* if nothing to escape, just return */
len = strlen (str); len = strlen (str);
slen = strcspn (str, "\\\'"); slen = strcspn (str, "\\\'");
if (len == slen) { if (len == slen)
{
LEAVE("nothing to escape"); LEAVE("nothing to escape");
return str; return str;
} }
@ -142,7 +145,8 @@ sqlEscape_destroy (sqlEscape *b)
{ {
ENTER(" "); ENTER(" ");
if (!b) { LEAVE("b is (null)"); return; } if (!b) { LEAVE("b is (null)"); return; }
g_free (b->escape); b->escape = NULL; g_free (b->escape);
b->escape = NULL;
g_free (b); g_free (b);
LEAVE(" "); LEAVE(" ");
} }

View File

@ -69,8 +69,10 @@ static const GncSqlColumnTableEntry col_table[] =
{ "commodity_guid", CT_COMMODITYREF, 0, 0, "commodity" }, { "commodity_guid", CT_COMMODITYREF, 0, 0, "commodity" },
{ "commodity_scu", CT_INT, 0, COL_NNUL, "commodity-scu" }, { "commodity_scu", CT_INT, 0, COL_NNUL, "commodity-scu" },
{ "non_std_scu", CT_BOOLEAN, 0, COL_NNUL, "non-std-scu" }, { "non_std_scu", CT_BOOLEAN, 0, COL_NNUL, "non-std-scu" },
{ "parent_guid", CT_GUID, 0, 0, NULL, NULL, {
(QofAccessFunc)get_parent, set_parent }, "parent_guid", CT_GUID, 0, 0, NULL, NULL,
(QofAccessFunc)get_parent, set_parent
},
{ "code", CT_STRING, ACCOUNT_MAX_CODE_LEN, 0, "code" }, { "code", CT_STRING, ACCOUNT_MAX_CODE_LEN, 0, "code" },
{ "description", CT_STRING, ACCOUNT_MAX_DESCRIPTION_LEN, 0, "description" }, { "description", CT_STRING, ACCOUNT_MAX_DESCRIPTION_LEN, 0, "description" },
{ "hidden", CT_BOOLEAN, 0, 0, "hidden" }, { "hidden", CT_BOOLEAN, 0, 0, "hidden" },
@ -86,7 +88,8 @@ static GncSqlColumnTableEntry parent_col_table[] =
/*@ +full_init_block @*/ /*@ +full_init_block @*/
}; };
typedef struct { typedef struct
{
/*@ dependent @*/ Account* pAccount; /*@ dependent @*/ Account* pAccount;
GncGUID guid; GncGUID guid;
} account_parent_guid_struct; } account_parent_guid_struct;
@ -105,9 +108,12 @@ get_parent( gpointer pObject )
pAccount = GNC_ACCOUNT(pObject); pAccount = GNC_ACCOUNT(pObject);
pParent = gnc_account_get_parent( pAccount ); pParent = gnc_account_get_parent( pAccount );
if( pParent == NULL ) { if ( pParent == NULL )
{
parent_guid = NULL; parent_guid = NULL;
} else { }
else
{
parent_guid = qof_instance_get_guid( QOF_INSTANCE(pParent) ); parent_guid = qof_instance_get_guid( QOF_INSTANCE(pParent) );
} }
@ -127,9 +133,11 @@ set_parent( gpointer pObject, /*@ null @*/ gpointer pValue )
pAccount = GNC_ACCOUNT(pObject); pAccount = GNC_ACCOUNT(pObject);
pBook = qof_instance_get_book( QOF_INSTANCE(pAccount) ); pBook = qof_instance_get_book( QOF_INSTANCE(pAccount) );
if( guid != NULL ) { if ( guid != NULL )
{
pParent = xaccAccountLookup( guid, pBook ); pParent = xaccAccountLookup( guid, pBook );
if( pParent != NULL ) { if ( pParent != NULL )
{
gnc_account_append_child( pParent, pAccount ); gnc_account_append_child( pParent, pAccount );
} }
} }
@ -159,10 +167,12 @@ load_single_account( GncSqlBackend* be, GncSqlRow* row,
g_return_val_if_fail( l_accounts_needing_parents != NULL, NULL ); g_return_val_if_fail( l_accounts_needing_parents != NULL, NULL );
guid = gnc_sql_load_guid( be, row ); guid = gnc_sql_load_guid( be, row );
if( guid != NULL ) { if ( guid != NULL )
{
pAccount = xaccAccountLookup( guid, be->primary_book ); pAccount = xaccAccountLookup( guid, be->primary_book );
} }
if( pAccount == NULL ) { if ( pAccount == NULL )
{
pAccount = xaccMallocAccount( be->primary_book ); pAccount = xaccMallocAccount( be->primary_book );
} }
xaccAccountBeginEdit( pAccount ); xaccAccountBeginEdit( pAccount );
@ -172,7 +182,8 @@ load_single_account( GncSqlBackend* be, GncSqlRow* row,
/* If we don't have a parent and this isn't the root account, it might be because the parent /* If we don't have a parent and this isn't the root account, it might be because the parent
account hasn't been loaded yet. Remember the account and its parent guid for later. */ account hasn't been loaded yet. Remember the account and its parent guid for later. */
if ( gnc_account_get_parent( pAccount ) == NULL if ( gnc_account_get_parent( pAccount ) == NULL
&& pAccount != gnc_book_get_root_account( be->primary_book ) ) { && pAccount != gnc_book_get_root_account( be->primary_book ) )
{
account_parent_guid_struct* s = g_malloc( (gsize)sizeof(account_parent_guid_struct) ); account_parent_guid_struct* s = g_malloc( (gsize)sizeof(account_parent_guid_struct) );
g_assert( s != NULL ); g_assert( s != NULL );
@ -203,18 +214,21 @@ load_all_accounts( GncSqlBackend* be )
pTable = gnc_commodity_table_get_table( pBook ); pTable = gnc_commodity_table_get_table( pBook );
stmt = gnc_sql_create_select_statement( be, TABLE_NAME ); stmt = gnc_sql_create_select_statement( be, TABLE_NAME );
if( stmt == NULL ) { if ( stmt == NULL )
{
LEAVE( "stmt == NULL" ); LEAVE( "stmt == NULL" );
return; return;
} }
result = gnc_sql_execute_select_statement( be, stmt ); result = gnc_sql_execute_select_statement( be, stmt );
gnc_sql_statement_dispose( stmt ); gnc_sql_statement_dispose( stmt );
if( result != NULL ) { if ( result != NULL )
{
GncSqlRow* row = gnc_sql_result_get_first_row( result ); GncSqlRow* row = gnc_sql_result_get_first_row( result );
Account* acc; Account* acc;
gchar* sql; gchar* sql;
while( row != NULL ) { while ( row != NULL )
{
acc = load_single_account( be, row, &l_accounts_needing_parents ); acc = load_single_account( be, row, &l_accounts_needing_parents );
row = gnc_sql_result_get_next_row( result ); row = gnc_sql_result_get_next_row( result );
} }
@ -229,18 +243,22 @@ load_all_accounts( GncSqlBackend* be )
items are removed from the front and added to the back if the items are removed from the front and added to the back if the
parent is still not available, then eventually, the list will parent is still not available, then eventually, the list will
shrink to size 0. */ shrink to size 0. */
if( l_accounts_needing_parents != NULL ) { if ( l_accounts_needing_parents != NULL )
{
gboolean progress_made = TRUE; gboolean progress_made = TRUE;
Account* root; Account* root;
Account* pParent; Account* pParent;
GList* elem; GList* elem;
while( progress_made ) { while ( progress_made )
{
progress_made = FALSE; progress_made = FALSE;
for( elem = l_accounts_needing_parents; elem != NULL; elem = g_list_next( elem ) ) { for ( elem = l_accounts_needing_parents; elem != NULL; elem = g_list_next( elem ) )
{
account_parent_guid_struct* s = (account_parent_guid_struct*)elem->data; account_parent_guid_struct* s = (account_parent_guid_struct*)elem->data;
pParent = xaccAccountLookup( &s->guid, be->primary_book ); pParent = xaccAccountLookup( &s->guid, be->primary_book );
if( pParent != NULL ) { if ( pParent != NULL )
{
gnc_account_append_child( pParent, s->pAccount ); gnc_account_append_child( pParent, s->pAccount );
l_accounts_needing_parents = g_list_delete_link( l_accounts_needing_parents, elem ); l_accounts_needing_parents = g_list_delete_link( l_accounts_needing_parents, elem );
progress_made = TRUE; progress_made = TRUE;
@ -250,9 +268,11 @@ load_all_accounts( GncSqlBackend* be )
/* Any non-ROOT accounts left over must be parented by the root account */ /* Any non-ROOT accounts left over must be parented by the root account */
root = gnc_book_get_root_account( pBook ); root = gnc_book_get_root_account( pBook );
for( elem = l_accounts_needing_parents; elem != NULL; elem = g_list_next( elem ) ) { for ( elem = l_accounts_needing_parents; elem != NULL; elem = g_list_next( elem ) )
{
account_parent_guid_struct* s = (account_parent_guid_struct*)elem->data; account_parent_guid_struct* s = (account_parent_guid_struct*)elem->data;
if( xaccAccountGetType( s->pAccount ) != ACCT_TYPE_ROOT ) { if ( xaccAccountGetType( s->pAccount ) != ACCT_TYPE_ROOT )
{
gnc_account_append_child( root, s->pAccount ); gnc_account_append_child( root, s->pAccount );
} }
} }
@ -260,7 +280,8 @@ load_all_accounts( GncSqlBackend* be )
/* Load starting balances */ /* Load starting balances */
bal_slist = gnc_sql_get_account_balances_slist( be ); bal_slist = gnc_sql_get_account_balances_slist( be );
for( bal = bal_slist; bal != NULL; bal = bal->next ) { for ( bal = bal_slist; bal != NULL; bal = bal->next )
{
acct_balances_t* balances = (acct_balances_t*)bal->data; acct_balances_t* balances = (acct_balances_t*)bal->data;
g_object_set( balances->acct, g_object_set( balances->acct,
@ -270,7 +291,8 @@ load_all_accounts( GncSqlBackend* be )
NULL); NULL);
} }
if( bal_slist != NULL ) { if ( bal_slist != NULL )
{
g_slist_free( bal_slist ); g_slist_free( bal_slist );
} }
} }
@ -287,7 +309,8 @@ create_account_tables( GncSqlBackend* be )
g_return_if_fail( be != NULL ); g_return_if_fail( be != NULL );
version = gnc_sql_get_table_version( be, TABLE_NAME ); version = gnc_sql_get_table_version( be, TABLE_NAME );
if( version == 0 ) { if ( version == 0 )
{
(void)gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table ); (void)gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table );
} }
} }
@ -318,29 +341,40 @@ gnc_sql_save_account( GncSqlBackend* be, QofInstance* inst )
commodity = xaccAccountGetCommodity( pAcc ); commodity = xaccAccountGetCommodity( pAcc );
is_ok = TRUE; is_ok = TRUE;
if( qof_instance_get_destroying( inst ) ) { if ( qof_instance_get_destroying( inst ) )
{
op = OP_DB_DELETE; op = OP_DB_DELETE;
} else if( be->is_pristine_db || is_infant ) { }
else if ( be->is_pristine_db || is_infant )
{
op = OP_DB_INSERT; op = OP_DB_INSERT;
} else { }
else
{
op = OP_DB_UPDATE; op = OP_DB_UPDATE;
} }
// If not deleting the account, ensure the commodity is in the db // If not deleting the account, ensure the commodity is in the db
if( op != OP_DB_DELETE && commodity != NULL ) { if ( op != OP_DB_DELETE && commodity != NULL )
{
is_ok = gnc_sql_save_commodity( be, commodity ); is_ok = gnc_sql_save_commodity( be, commodity );
} }
if( is_ok ) { if ( is_ok )
{
is_ok = gnc_sql_do_db_operation( be, op, TABLE_NAME, GNC_ID_ACCOUNT, pAcc, col_table ); is_ok = gnc_sql_do_db_operation( be, op, TABLE_NAME, GNC_ID_ACCOUNT, pAcc, col_table );
} }
if( is_ok ) { if ( is_ok )
{
// Now, commit or delete any slots // Now, commit or delete any slots
guid = qof_instance_get_guid( inst ); guid = qof_instance_get_guid( inst );
if( !qof_instance_get_destroying(inst) ) { if ( !qof_instance_get_destroying(inst) )
{
is_ok = gnc_sql_slots_save( be, guid, is_infant, qof_instance_get_slots( inst ) ); is_ok = gnc_sql_slots_save( be, guid, is_infant, qof_instance_get_slots( inst ) );
} else { }
else
{
is_ok = gnc_sql_slots_delete( be, guid ); is_ok = gnc_sql_slots_delete( be, guid );
} }
} }
@ -366,17 +400,24 @@ load_account_guid( const GncSqlBackend* be, GncSqlRow* row,
g_return_if_fail( table_row != NULL ); g_return_if_fail( table_row != NULL );
val = gnc_sql_row_get_value_at_col_name( row, table_row->col_name ); val = gnc_sql_row_get_value_at_col_name( row, table_row->col_name );
if( val != NULL && G_VALUE_HOLDS_STRING( val ) && g_value_get_string( val ) != NULL ) { if ( val != NULL && G_VALUE_HOLDS_STRING( val ) && g_value_get_string( val ) != NULL )
{
(void)string_to_guid( g_value_get_string( val ), &guid ); (void)string_to_guid( g_value_get_string( val ), &guid );
account = xaccAccountLookup( &guid, be->primary_book ); account = xaccAccountLookup( &guid, be->primary_book );
if( account != NULL ) { if ( account != NULL )
if( table_row->gobj_param_name != NULL ) { {
if ( table_row->gobj_param_name != NULL )
{
g_object_set( pObject, table_row->gobj_param_name, account, NULL ); g_object_set( pObject, table_row->gobj_param_name, account, NULL );
} else { }
else
{
g_return_if_fail( setter != NULL ); g_return_if_fail( setter != NULL );
(*setter)( pObject, (const gpointer)account ); (*setter)( pObject, (const gpointer)account );
} }
} else { }
else
{
PWARN( "Account ref '%s' not found", g_value_get_string( val ) ); PWARN( "Account ref '%s' not found", g_value_get_string( val ) );
} }
} }
@ -386,7 +427,8 @@ static GncSqlColumnTypeHandler account_guid_handler
= { load_account_guid, = { load_account_guid,
gnc_sql_add_objectref_guid_col_info_to_list, gnc_sql_add_objectref_guid_col_info_to_list,
gnc_sql_add_colname_to_list, gnc_sql_add_colname_to_list,
gnc_sql_add_gvalue_objectref_guid_to_slist }; gnc_sql_add_gvalue_objectref_guid_to_slist
};
/* ================================================================= */ /* ================================================================= */
void void
gnc_sql_init_account_handler( void ) gnc_sql_init_account_handler( void )

File diff suppressed because it is too large Load Diff

View File

@ -59,10 +59,14 @@ static const GncSqlColumnTableEntry col_table[] =
{ {
/*@ -full_init_block @*/ /*@ -full_init_block @*/
{ "guid", CT_GUID, 0, COL_NNUL | COL_PKEY, "guid" }, { "guid", CT_GUID, 0, COL_NNUL | COL_PKEY, "guid" },
{ "root_account_guid", CT_GUID, 0, COL_NNUL, NULL, NULL, {
(QofAccessFunc)get_root_account_guid, set_root_account_guid }, "root_account_guid", CT_GUID, 0, COL_NNUL, NULL, NULL,
{ "root_template_guid", CT_GUID, 0, COL_NNUL, NULL, NULL, (QofAccessFunc)get_root_account_guid, set_root_account_guid
(QofAccessFunc)get_root_template_guid, set_root_template_guid }, },
{
"root_template_guid", CT_GUID, 0, COL_NNUL, NULL, NULL,
(QofAccessFunc)get_root_template_guid, set_root_template_guid
},
{ NULL } { NULL }
/*@ +full_init_block @*/ /*@ +full_init_block @*/
}; };
@ -121,7 +125,8 @@ set_root_template_guid( gpointer pObject, /*@ null @*/ gpointer pValue )
g_return_if_fail( pValue != NULL ); g_return_if_fail( pValue != NULL );
root = gnc_book_get_template_root( book ); root = gnc_book_get_template_root( book );
if( root == NULL ) { if ( root == NULL )
{
root = xaccMallocAccount( book ); root = xaccMallocAccount( book );
xaccAccountBeginEdit( root ); xaccAccountBeginEdit( root );
xaccAccountSetType( root, ACCT_TYPE_ROOT ); xaccAccountSetType( root, ACCT_TYPE_ROOT );
@ -144,7 +149,8 @@ load_single_book( GncSqlBackend* be, GncSqlRow* row )
guid = gnc_sql_load_guid( be, row ); guid = gnc_sql_load_guid( be, row );
pBook = be->primary_book; pBook = be->primary_book;
if( pBook == NULL ) { if ( pBook == NULL )
{
pBook = qof_book_new(); pBook = qof_book_new();
} }
@ -165,16 +171,21 @@ load_all_books( GncSqlBackend* be )
g_return_if_fail( be != NULL ); g_return_if_fail( be != NULL );
stmt = gnc_sql_create_select_statement( be, BOOK_TABLE ); stmt = gnc_sql_create_select_statement( be, BOOK_TABLE );
if( stmt != NULL ) { if ( stmt != NULL )
{
result = gnc_sql_execute_select_statement( be, stmt ); result = gnc_sql_execute_select_statement( be, stmt );
gnc_sql_statement_dispose( stmt ); gnc_sql_statement_dispose( stmt );
if( result != NULL ) { if ( result != NULL )
{
GncSqlRow* row = gnc_sql_result_get_first_row( result ); GncSqlRow* row = gnc_sql_result_get_first_row( result );
// If there are no rows, try committing the book // If there are no rows, try committing the book
if( row == NULL ) { if ( row == NULL )
{
(void)gnc_sql_save_book( be, QOF_INSTANCE(be->primary_book) ); (void)gnc_sql_save_book( be, QOF_INSTANCE(be->primary_book) );
} else { }
else
{
// Otherwise, load the 1st book. // Otherwise, load the 1st book.
load_single_book( be, row ); load_single_book( be, row );
} }
@ -193,7 +204,8 @@ create_book_tables( GncSqlBackend* be )
g_return_if_fail( be != NULL ); g_return_if_fail( be != NULL );
version = gnc_sql_get_table_version( be, BOOK_TABLE ); version = gnc_sql_get_table_version( be, BOOK_TABLE );
if( version == 0 ) { if ( version == 0 )
{
(void)gnc_sql_create_table( be, BOOK_TABLE, TABLE_VERSION, col_table ); (void)gnc_sql_create_table( be, BOOK_TABLE, TABLE_VERSION, col_table );
} }
} }

View File

@ -76,7 +76,8 @@ static void set_period_num( gpointer pObj, gpointer val );
static gnc_numeric get_amount( gpointer pObj ); static gnc_numeric get_amount( gpointer pObj );
static void set_amount( gpointer pObj, gnc_numeric value ); static void set_amount( gpointer pObj, gnc_numeric value );
typedef struct { typedef struct
{
GncBudget* budget; GncBudget* budget;
Account* account; Account* account;
guint period_num; guint period_num;
@ -86,14 +87,22 @@ static const GncSqlColumnTableEntry budget_amounts_col_table[] =
{ {
/*@ -full_init_block @*/ /*@ -full_init_block @*/
{ "id", CT_INT, 0, COL_NNUL | COL_PKEY | COL_AUTOINC }, { "id", CT_INT, 0, COL_NNUL | COL_PKEY | COL_AUTOINC },
{ "budget_guid", CT_BUDGETREF, 0, COL_NNUL, NULL, NULL, {
(QofAccessFunc)get_budget, (QofSetterFunc)set_budget }, "budget_guid", CT_BUDGETREF, 0, COL_NNUL, NULL, NULL,
{ "account_guid", CT_ACCOUNTREF, 0, COL_NNUL, NULL, NULL, (QofAccessFunc)get_budget, (QofSetterFunc)set_budget
(QofAccessFunc)get_account, (QofSetterFunc)set_account }, },
{ "period_num", CT_INT, 0, COL_NNUL, NULL, NULL, {
(QofAccessFunc)get_period_num, (QofSetterFunc)set_period_num }, "account_guid", CT_ACCOUNTREF, 0, COL_NNUL, NULL, NULL,
{ "amount", CT_NUMERIC, 0, COL_NNUL, NULL, NULL, (QofAccessFunc)get_account, (QofSetterFunc)set_account
(QofAccessFunc)get_amount, (QofSetterFunc)set_amount }, },
{
"period_num", CT_INT, 0, COL_NNUL, NULL, NULL,
(QofAccessFunc)get_period_num, (QofSetterFunc)set_period_num
},
{
"amount", CT_NUMERIC, 0, COL_NNUL, NULL, NULL,
(QofAccessFunc)get_amount, (QofSetterFunc)set_amount
},
{ NULL } { NULL }
/*@ +full_init_block @*/ /*@ +full_init_block @*/
}; };
@ -198,16 +207,19 @@ load_budget_amounts( GncSqlBackend* be, GncBudget* budget )
sql = g_strdup_printf( "SELECT * FROM %s WHERE budget_guid='%s'", AMOUNTS_TABLE, guid_buf ); sql = g_strdup_printf( "SELECT * FROM %s WHERE budget_guid='%s'", AMOUNTS_TABLE, guid_buf );
stmt = gnc_sql_create_statement_from_sql( be, sql ); stmt = gnc_sql_create_statement_from_sql( be, sql );
g_free( sql ); g_free( sql );
if( stmt != NULL ) { if ( stmt != NULL )
{
result = gnc_sql_execute_select_statement( be, stmt ); result = gnc_sql_execute_select_statement( be, stmt );
gnc_sql_statement_dispose( stmt ); gnc_sql_statement_dispose( stmt );
if( result != NULL ) { if ( result != NULL )
{
GncSqlRow* row = gnc_sql_result_get_first_row( result ); GncSqlRow* row = gnc_sql_result_get_first_row( result );
budget_amount_info_t info; budget_amount_info_t info;
info.budget = budget; info.budget = budget;
while( row != NULL ) { while ( row != NULL )
{
gnc_sql_load_object( be, row, NULL, &info, budget_amounts_col_table ); gnc_sql_load_object( be, row, NULL, &info, budget_amounts_col_table );
row = gnc_sql_result_get_next_row( result ); row = gnc_sql_result_get_next_row( result );
} }
@ -249,7 +261,8 @@ static gboolean
save_budget_amounts( GncSqlBackend* be, GncBudget* budget ) save_budget_amounts( GncSqlBackend* be, GncBudget* budget )
{ {
GList* descendants; GList* descendants;
/*@ dependent @*/ GList* node; /*@ dependent @*/
GList* node;
budget_amount_info_t info; budget_amount_info_t info;
guint num_periods; guint num_periods;
gboolean is_ok = TRUE;; gboolean is_ok = TRUE;;
@ -263,12 +276,15 @@ save_budget_amounts( GncSqlBackend* be, GncBudget* budget )
info.budget = budget; info.budget = budget;
num_periods = gnc_budget_get_num_periods( budget ); num_periods = gnc_budget_get_num_periods( budget );
descendants = gnc_account_get_descendants( gnc_book_get_root_account( be->primary_book ) ); descendants = gnc_account_get_descendants( gnc_book_get_root_account( be->primary_book ) );
for( node = descendants; node != NULL && is_ok; node = g_list_next(node) ) { for ( node = descendants; node != NULL && is_ok; node = g_list_next(node) )
{
guint i; guint i;
info.account = GNC_ACCOUNT(node->data); info.account = GNC_ACCOUNT(node->data);
for( i = 0; i < num_periods && is_ok; i++ ) { for ( i = 0; i < num_periods && is_ok; i++ )
if( gnc_budget_is_account_period_value_set( budget, info.account, i ) ) { {
if ( gnc_budget_is_account_period_value_set( budget, info.account, i ) )
{
info.period_num = i; info.period_num = i;
is_ok = gnc_sql_do_db_operation( be, OP_DB_INSERT, AMOUNTS_TABLE, "", &info, is_ok = gnc_sql_do_db_operation( be, OP_DB_INSERT, AMOUNTS_TABLE, "", &info,
budget_amounts_col_table ); budget_amounts_col_table );
@ -291,10 +307,12 @@ load_single_budget( GncSqlBackend* be, GncSqlRow* row )
g_return_val_if_fail( row != NULL, NULL ); g_return_val_if_fail( row != NULL, NULL );
guid = gnc_sql_load_guid( be, row ); guid = gnc_sql_load_guid( be, row );
if( guid != NULL ) { if ( guid != NULL )
{
pBudget = gnc_budget_lookup( guid, be->primary_book ); pBudget = gnc_budget_lookup( guid, be->primary_book );
} }
if( pBudget == NULL ) { if ( pBudget == NULL )
{
pBudget = gnc_budget_new( be->primary_book ); pBudget = gnc_budget_new( be->primary_book );
} }
@ -302,7 +320,8 @@ load_single_budget( GncSqlBackend* be, GncSqlRow* row )
gnc_sql_load_object( be, row, GNC_ID_BUDGET, pBudget, col_table ); gnc_sql_load_object( be, row, GNC_ID_BUDGET, pBudget, col_table );
load_budget_amounts( be, pBudget ); load_budget_amounts( be, pBudget );
r = gnc_sql_recurrence_load( be, gnc_budget_get_guid( pBudget ) ); r = gnc_sql_recurrence_load( be, gnc_budget_get_guid( pBudget ) );
if( r != NULL ) { if ( r != NULL )
{
gnc_budget_set_recurrence( pBudget, r ); gnc_budget_set_recurrence( pBudget, r );
g_free( r ); g_free( r );
} }
@ -321,23 +340,28 @@ load_all_budgets( GncSqlBackend* be )
g_return_if_fail( be != NULL ); g_return_if_fail( be != NULL );
stmt = gnc_sql_create_select_statement( be, BUDGET_TABLE ); stmt = gnc_sql_create_select_statement( be, BUDGET_TABLE );
if( stmt != NULL ) { if ( stmt != NULL )
{
result = gnc_sql_execute_select_statement( be, stmt ); result = gnc_sql_execute_select_statement( be, stmt );
gnc_sql_statement_dispose( stmt ); gnc_sql_statement_dispose( stmt );
if( result != NULL ) { if ( result != NULL )
{
GncSqlRow* row = gnc_sql_result_get_first_row( result ); GncSqlRow* row = gnc_sql_result_get_first_row( result );
GncBudget* b; GncBudget* b;
while( row != NULL ) { while ( row != NULL )
{
b = load_single_budget( be, row ); b = load_single_budget( be, row );
if( b != NULL ) { if ( b != NULL )
{
list = g_list_prepend( list, b ); list = g_list_prepend( list, b );
} }
row = gnc_sql_result_get_next_row( result ); row = gnc_sql_result_get_next_row( result );
} }
gnc_sql_result_dispose( result ); gnc_sql_result_dispose( result );
if( list != NULL ) { if ( list != NULL )
{
gnc_sql_slots_load_for_list( be, list ); gnc_sql_slots_load_for_list( be, list );
g_list_free( list ); g_list_free( list );
} }
@ -354,12 +378,14 @@ create_budget_tables( GncSqlBackend* be )
g_return_if_fail( be != NULL ); g_return_if_fail( be != NULL );
version = gnc_sql_get_table_version( be, BUDGET_TABLE ); version = gnc_sql_get_table_version( be, BUDGET_TABLE );
if( version == 0 ) { if ( version == 0 )
{
(void)gnc_sql_create_table( be, BUDGET_TABLE, TABLE_VERSION, col_table ); (void)gnc_sql_create_table( be, BUDGET_TABLE, TABLE_VERSION, col_table );
} }
version = gnc_sql_get_table_version( be, AMOUNTS_TABLE ); version = gnc_sql_get_table_version( be, AMOUNTS_TABLE );
if( version == 0 ) { if ( version == 0 )
{
(void)gnc_sql_create_table( be, AMOUNTS_TABLE, AMOUNTS_TABLE_VERSION, budget_amounts_col_table ); (void)gnc_sql_create_table( be, AMOUNTS_TABLE, AMOUNTS_TABLE_VERSION, budget_amounts_col_table );
} }
} }
@ -379,32 +405,45 @@ save_budget( GncSqlBackend* be, QofInstance* inst )
g_return_val_if_fail( GNC_IS_BUDGET(inst), FALSE ); g_return_val_if_fail( GNC_IS_BUDGET(inst), FALSE );
is_infant = qof_instance_get_infant( inst ); is_infant = qof_instance_get_infant( inst );
if( qof_instance_get_destroying( inst ) ) { if ( qof_instance_get_destroying( inst ) )
{
op = OP_DB_DELETE; op = OP_DB_DELETE;
} else if( be->is_pristine_db || is_infant ) { }
else if ( be->is_pristine_db || is_infant )
{
op = OP_DB_INSERT; op = OP_DB_INSERT;
} else { }
else
{
op = OP_DB_UPDATE; op = OP_DB_UPDATE;
} }
is_ok = gnc_sql_do_db_operation( be, op, BUDGET_TABLE, GNC_ID_BUDGET, pBudget, col_table ); is_ok = gnc_sql_do_db_operation( be, op, BUDGET_TABLE, GNC_ID_BUDGET, pBudget, col_table );
// Now, commit any slots and recurrence // Now, commit any slots and recurrence
if( is_ok ) { if ( is_ok )
{
guid = qof_instance_get_guid( inst ); guid = qof_instance_get_guid( inst );
if( !qof_instance_get_destroying(inst) ) { if ( !qof_instance_get_destroying(inst) )
{
is_ok = save_budget_amounts( be, pBudget ); is_ok = save_budget_amounts( be, pBudget );
if( is_ok ) { if ( is_ok )
{
is_ok = gnc_sql_recurrence_save( be, guid, gnc_budget_get_recurrence( pBudget ) ); is_ok = gnc_sql_recurrence_save( be, guid, gnc_budget_get_recurrence( pBudget ) );
} }
if( is_ok ) { if ( is_ok )
{
is_ok = gnc_sql_slots_save( be, guid, is_infant, qof_instance_get_slots( inst ) ); is_ok = gnc_sql_slots_save( be, guid, is_infant, qof_instance_get_slots( inst ) );
} }
} else { }
else
{
is_ok = delete_budget_amounts( be, pBudget ); is_ok = delete_budget_amounts( be, pBudget );
if( is_ok ) { if ( is_ok )
{
is_ok = gnc_sql_recurrence_delete( be, guid ); is_ok = gnc_sql_recurrence_delete( be, guid );
} }
if( is_ok ) { if ( is_ok )
{
(void)gnc_sql_slots_delete( be, guid ); (void)gnc_sql_slots_delete( be, guid );
} }
} }
@ -418,7 +457,8 @@ do_save_budget( QofInstance* inst, gpointer data )
{ {
write_objects_t* s = (write_objects_t*)data; write_objects_t* s = (write_objects_t*)data;
if( s->is_ok ) { if ( s->is_ok )
{
s->is_ok = save_budget( s->be, inst ); s->is_ok = save_budget( s->be, inst );
} }
} }
@ -454,17 +494,24 @@ load_budget_guid( const GncSqlBackend* be, GncSqlRow* row,
g_return_if_fail( table_row != NULL ); g_return_if_fail( table_row != NULL );
val = gnc_sql_row_get_value_at_col_name( row, table_row->col_name ); val = gnc_sql_row_get_value_at_col_name( row, table_row->col_name );
if( val != NULL && G_VALUE_HOLDS_STRING( val ) && g_value_get_string( val ) != NULL ) { if ( val != NULL && G_VALUE_HOLDS_STRING( val ) && g_value_get_string( val ) != NULL )
{
(void)string_to_guid( g_value_get_string( val ), &guid ); (void)string_to_guid( g_value_get_string( val ), &guid );
budget = gnc_budget_lookup( &guid, be->primary_book ); budget = gnc_budget_lookup( &guid, be->primary_book );
if( budget != NULL ) { if ( budget != NULL )
if( table_row->gobj_param_name != NULL ) { {
if ( table_row->gobj_param_name != NULL )
{
g_object_set( pObject, table_row->gobj_param_name, budget, NULL ); g_object_set( pObject, table_row->gobj_param_name, budget, NULL );
} else { }
else
{
g_return_if_fail( setter != NULL ); g_return_if_fail( setter != NULL );
(*setter)( pObject, (const gpointer)budget ); (*setter)( pObject, (const gpointer)budget );
} }
} else { }
else
{
PWARN( "Budget ref '%s' not found", g_value_get_string( val ) ); PWARN( "Budget ref '%s' not found", g_value_get_string( val ) );
} }
} }
@ -474,7 +521,8 @@ static GncSqlColumnTypeHandler budget_guid_handler
= { load_budget_guid, = { load_budget_guid,
gnc_sql_add_objectref_guid_col_info_to_list, gnc_sql_add_objectref_guid_col_info_to_list,
gnc_sql_add_colname_to_list, gnc_sql_add_colname_to_list,
gnc_sql_add_gvalue_objectref_guid_to_slist }; gnc_sql_add_gvalue_objectref_guid_to_slist
};
/* ================================================================= */ /* ================================================================= */
void void
gnc_sql_init_budget_handler( void ) gnc_sql_init_budget_handler( void )

View File

@ -57,19 +57,24 @@ static void set_quote_source_name( gpointer pObject, /*@ null @*/ gpointer pValu
#define COMMODITY_MAX_QUOTESOURCE_LEN 2048 #define COMMODITY_MAX_QUOTESOURCE_LEN 2048
#define COMMODITY_MAX_QUOTE_TZ_LEN 2048 #define COMMODITY_MAX_QUOTE_TZ_LEN 2048
static const GncSqlColumnTableEntry col_table[] = { static const GncSqlColumnTableEntry col_table[] =
{
/*@ -full_init_block @*/ /*@ -full_init_block @*/
{ "guid", CT_GUID, 0, COL_NNUL | COL_PKEY, "guid" }, { "guid", CT_GUID, 0, COL_NNUL | COL_PKEY, "guid" },
{ "namespace", CT_STRING, COMMODITY_MAX_NAMESPACE_LEN, COL_NNUL, NULL, NULL, {
"namespace", CT_STRING, COMMODITY_MAX_NAMESPACE_LEN, COL_NNUL, NULL, NULL,
(QofAccessFunc)gnc_commodity_get_namespace, (QofAccessFunc)gnc_commodity_get_namespace,
(QofSetterFunc)gnc_commodity_set_namespace }, (QofSetterFunc)gnc_commodity_set_namespace
},
{ "mnemonic", CT_STRING, COMMODITY_MAX_MNEMONIC_LEN, COL_NNUL, "mnemonic" }, { "mnemonic", CT_STRING, COMMODITY_MAX_MNEMONIC_LEN, COL_NNUL, "mnemonic" },
{ "fullname", CT_STRING, COMMODITY_MAX_FULLNAME_LEN, 0, "fullname" }, { "fullname", CT_STRING, COMMODITY_MAX_FULLNAME_LEN, 0, "fullname" },
{ "cusip", CT_STRING, COMMODITY_MAX_CUSIP_LEN, 0, "cusip" }, { "cusip", CT_STRING, COMMODITY_MAX_CUSIP_LEN, 0, "cusip" },
{ "fraction", CT_INT, 0, COL_NNUL, "fraction" }, { "fraction", CT_INT, 0, COL_NNUL, "fraction" },
{ "quote_flag", CT_BOOLEAN, 0, COL_NNUL, "quote_flag" }, { "quote_flag", CT_BOOLEAN, 0, COL_NNUL, "quote_flag" },
{ "quote_source", CT_STRING, COMMODITY_MAX_QUOTESOURCE_LEN, 0, NULL, NULL, {
(QofAccessFunc)get_quote_source_name, set_quote_source_name }, "quote_source", CT_STRING, COMMODITY_MAX_QUOTESOURCE_LEN, 0, NULL, NULL,
(QofAccessFunc)get_quote_source_name, set_quote_source_name
},
{ "quote_tz", CT_STRING, COMMODITY_MAX_QUOTE_TZ_LEN, 0, "quote-tz" }, { "quote_tz", CT_STRING, COMMODITY_MAX_QUOTE_TZ_LEN, 0, "quote-tz" },
{ NULL } { NULL }
/*@ +full_init_block @*/ /*@ +full_init_block @*/
@ -133,15 +138,18 @@ load_all_commodities( GncSqlBackend* be )
if ( stmt == NULL ) return; if ( stmt == NULL ) return;
result = gnc_sql_execute_select_statement( be, stmt ); result = gnc_sql_execute_select_statement( be, stmt );
gnc_sql_statement_dispose( stmt ); gnc_sql_statement_dispose( stmt );
if( result != NULL ) { if ( result != NULL )
{
gnc_commodity* pCommodity; gnc_commodity* pCommodity;
GncSqlRow* row = gnc_sql_result_get_first_row( result ); GncSqlRow* row = gnc_sql_result_get_first_row( result );
gchar* sql; gchar* sql;
while( row != NULL ) { while ( row != NULL )
{
pCommodity = load_single_commodity( be, row ); pCommodity = load_single_commodity( be, row );
if( pCommodity != NULL ) { if ( pCommodity != NULL )
{
GncGUID guid; GncGUID guid;
guid = *qof_instance_get_guid( QOF_INSTANCE(pCommodity) ); guid = *qof_instance_get_guid( QOF_INSTANCE(pCommodity) );
@ -167,7 +175,8 @@ create_commodities_tables( GncSqlBackend* be )
g_return_if_fail( be != NULL ); g_return_if_fail( be != NULL );
version = gnc_sql_get_table_version( be, COMMODITIES_TABLE ); version = gnc_sql_get_table_version( be, COMMODITIES_TABLE );
if( version == 0 ) { if ( version == 0 )
{
(void)gnc_sql_create_table( be, COMMODITIES_TABLE, TABLE_VERSION, col_table ); (void)gnc_sql_create_table( be, COMMODITIES_TABLE, TABLE_VERSION, col_table );
} }
} }
@ -182,21 +191,30 @@ do_commit_commodity( GncSqlBackend* be, QofInstance* inst, gboolean force_insert
gboolean is_ok; gboolean is_ok;
is_infant = qof_instance_get_infant( inst ); is_infant = qof_instance_get_infant( inst );
if( qof_instance_get_destroying( inst ) ) { if ( qof_instance_get_destroying( inst ) )
{
op = OP_DB_DELETE; op = OP_DB_DELETE;
} else if( be->is_pristine_db || is_infant || force_insert ) { }
else if ( be->is_pristine_db || is_infant || force_insert )
{
op = OP_DB_INSERT; op = OP_DB_INSERT;
} else { }
else
{
op = OP_DB_UPDATE; op = OP_DB_UPDATE;
} }
is_ok = gnc_sql_do_db_operation( be, op, COMMODITIES_TABLE, GNC_ID_COMMODITY, inst, col_table ); is_ok = gnc_sql_do_db_operation( be, op, COMMODITIES_TABLE, GNC_ID_COMMODITY, inst, col_table );
if( is_ok ) { if ( is_ok )
{
// Now, commit any slots // Now, commit any slots
guid = qof_instance_get_guid( inst ); guid = qof_instance_get_guid( inst );
if( !qof_instance_get_destroying(inst) ) { if ( !qof_instance_get_destroying(inst) )
{
is_ok = gnc_sql_slots_save( be, guid, is_infant, qof_instance_get_slots( inst ) ); is_ok = gnc_sql_slots_save( be, guid, is_infant, qof_instance_get_slots( inst ) );
} else { }
else
{
is_ok = gnc_sql_slots_delete( be, guid ); is_ok = gnc_sql_slots_delete( be, guid );
} }
} }
@ -232,7 +250,8 @@ gnc_sql_save_commodity( GncSqlBackend* be, gnc_commodity* pCommodity )
g_return_val_if_fail( be != NULL, FALSE ); g_return_val_if_fail( be != NULL, FALSE );
g_return_val_if_fail( pCommodity != NULL, FALSE ); g_return_val_if_fail( pCommodity != NULL, FALSE );
if( !is_commodity_in_db( be, pCommodity ) ) { if ( !is_commodity_in_db( be, pCommodity ) )
{
is_ok = do_commit_commodity( be, QOF_INSTANCE(pCommodity), TRUE ); is_ok = do_commit_commodity( be, QOF_INSTANCE(pCommodity), TRUE );
} }
@ -256,16 +275,23 @@ load_commodity_guid( const GncSqlBackend* be, GncSqlRow* row,
g_return_if_fail( table_row != NULL ); g_return_if_fail( table_row != NULL );
val = gnc_sql_row_get_value_at_col_name( row, table_row->col_name ); val = gnc_sql_row_get_value_at_col_name( row, table_row->col_name );
if( val != NULL && G_VALUE_HOLDS_STRING( val ) && g_value_get_string( val ) != NULL ) { if ( val != NULL && G_VALUE_HOLDS_STRING( val ) && g_value_get_string( val ) != NULL )
{
(void)string_to_guid( g_value_get_string( val ), &guid ); (void)string_to_guid( g_value_get_string( val ), &guid );
commodity = gnc_commodity_find_commodity_by_guid( &guid, be->primary_book ); commodity = gnc_commodity_find_commodity_by_guid( &guid, be->primary_book );
if( commodity != NULL ) { if ( commodity != NULL )
if( table_row->gobj_param_name != NULL ) { {
if ( table_row->gobj_param_name != NULL )
{
g_object_set( pObject, table_row->gobj_param_name, commodity, NULL ); g_object_set( pObject, table_row->gobj_param_name, commodity, NULL );
} else if( setter != NULL ) { }
else if ( setter != NULL )
{
(*setter)( pObject, (const gpointer)commodity ); (*setter)( pObject, (const gpointer)commodity );
} }
} else { }
else
{
PWARN( "Commodity ref '%s' not found", g_value_get_string( val ) ); PWARN( "Commodity ref '%s' not found", g_value_get_string( val ) );
} }
} }
@ -275,7 +301,8 @@ static GncSqlColumnTypeHandler commodity_guid_handler
= { load_commodity_guid, = { load_commodity_guid,
gnc_sql_add_objectref_guid_col_info_to_list, gnc_sql_add_objectref_guid_col_info_to_list,
gnc_sql_add_colname_to_list, gnc_sql_add_colname_to_list,
gnc_sql_add_gvalue_objectref_guid_to_slist }; gnc_sql_add_gvalue_objectref_guid_to_slist
};
/* ================================================================= */ /* ================================================================= */
void void
gnc_sql_init_commodity_handler( void ) gnc_sql_init_commodity_handler( void )

View File

@ -55,8 +55,10 @@ static const GncSqlColumnTableEntry col_table[] =
{ {
/*@ -full_init_block @*/ /*@ -full_init_block @*/
{ "guid", CT_GUID, 0, COL_NNUL | COL_PKEY, "guid" }, { "guid", CT_GUID, 0, COL_NNUL | COL_PKEY, "guid" },
{ "account_guid", CT_ACCOUNTREF, 0, 0, NULL, NULL, {
(QofAccessFunc)get_lot_account, set_lot_account }, "account_guid", CT_ACCOUNTREF, 0, 0, NULL, NULL,
(QofAccessFunc)get_lot_account, set_lot_account
},
{ "is_closed", CT_BOOLEAN, 0, COL_NNUL, "is-closed" }, { "is_closed", CT_BOOLEAN, 0, COL_NNUL, "is-closed" },
{ NULL } { NULL }
/*@ +full_init_block @*/ /*@ +full_init_block @*/
@ -88,7 +90,8 @@ set_lot_account( gpointer pObject, /*@ null @*/ gpointer pValue )
lot = GNC_LOT(pObject); lot = GNC_LOT(pObject);
pAccount = GNC_ACCOUNT(pValue); pAccount = GNC_ACCOUNT(pValue);
if( pAccount != NULL ) { if ( pAccount != NULL )
{
xaccAccountInsertLot( pAccount, lot ); xaccAccountInsertLot( pAccount, lot );
} }
} }
@ -119,15 +122,18 @@ load_all_lots( GncSqlBackend* be )
g_return_if_fail( be != NULL ); g_return_if_fail( be != NULL );
stmt = gnc_sql_create_select_statement( be, TABLE_NAME ); stmt = gnc_sql_create_select_statement( be, TABLE_NAME );
if( stmt != NULL ) { if ( stmt != NULL )
{
result = gnc_sql_execute_select_statement( be, stmt ); result = gnc_sql_execute_select_statement( be, stmt );
gnc_sql_statement_dispose( stmt ); gnc_sql_statement_dispose( stmt );
if( result != NULL ) { if ( result != NULL )
{
GncSqlRow* row = gnc_sql_result_get_first_row( result ); GncSqlRow* row = gnc_sql_result_get_first_row( result );
GNCLot* lot; GNCLot* lot;
gchar* sql; gchar* sql;
while( row != NULL ) { while ( row != NULL )
{
lot = load_single_lot( be, row ); lot = load_single_lot( be, row );
row = gnc_sql_result_get_next_row( result ); row = gnc_sql_result_get_next_row( result );
} }
@ -149,10 +155,13 @@ create_lots_tables( GncSqlBackend* be )
g_return_if_fail( be != NULL ); g_return_if_fail( be != NULL );
version = gnc_sql_get_table_version( be, TABLE_NAME ); version = gnc_sql_get_table_version( be, TABLE_NAME );
if( version == 0 ) { if ( version == 0 )
{
/* The table doesn't exist, so create it */ /* The table doesn't exist, so create it */
(void)gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table ); (void)gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table );
} else if( version == 1 ) { }
else if ( version == 1 )
{
/* Version 1 -> 2 removes the 'NOT NULL' constraint on the account_guid /* Version 1 -> 2 removes the 'NOT NULL' constraint on the account_guid
field. field.
@ -181,7 +190,8 @@ do_save_lot( QofInstance* inst, gpointer data )
{ {
write_objects_t* s = (write_objects_t*)data; write_objects_t* s = (write_objects_t*)data;
if( s->is_ok ) { if ( s->is_ok )
{
s->is_ok = commit_lot( s->be, inst ); s->is_ok = commit_lot( s->be, inst );
} }
} }
@ -216,17 +226,24 @@ load_lot_guid( const GncSqlBackend* be, GncSqlRow* row,
g_return_if_fail( table_row != NULL ); g_return_if_fail( table_row != NULL );
val = gnc_sql_row_get_value_at_col_name( row, table_row->col_name ); val = gnc_sql_row_get_value_at_col_name( row, table_row->col_name );
if( val != NULL && G_VALUE_HOLDS_STRING( val ) && g_value_get_string( val ) != NULL ) { if ( val != NULL && G_VALUE_HOLDS_STRING( val ) && g_value_get_string( val ) != NULL )
{
(void)string_to_guid( g_value_get_string( val ), &guid ); (void)string_to_guid( g_value_get_string( val ), &guid );
lot = gnc_lot_lookup( &guid, be->primary_book ); lot = gnc_lot_lookup( &guid, be->primary_book );
if( lot != NULL ) { if ( lot != NULL )
if( table_row->gobj_param_name != NULL ) { {
if ( table_row->gobj_param_name != NULL )
{
g_object_set( pObject, table_row->gobj_param_name, lot, NULL ); g_object_set( pObject, table_row->gobj_param_name, lot, NULL );
} else { }
else
{
g_return_if_fail( setter != NULL ); g_return_if_fail( setter != NULL );
(*setter)( pObject, (const gpointer)lot ); (*setter)( pObject, (const gpointer)lot );
} }
} else { }
else
{
PWARN( "Lot ref '%s' not found", g_value_get_string( val ) ); PWARN( "Lot ref '%s' not found", g_value_get_string( val ) );
} }
} }
@ -236,7 +253,8 @@ static GncSqlColumnTypeHandler lot_guid_handler
= { load_lot_guid, = { load_lot_guid,
gnc_sql_add_objectref_guid_col_info_to_list, gnc_sql_add_objectref_guid_col_info_to_list,
gnc_sql_add_colname_to_list, gnc_sql_add_colname_to_list,
gnc_sql_add_gvalue_objectref_guid_to_slist }; gnc_sql_add_gvalue_objectref_guid_to_slist
};
/* ================================================================= */ /* ================================================================= */
void void
gnc_sql_init_lot_handler( void ) gnc_sql_init_lot_handler( void )

View File

@ -97,19 +97,23 @@ load_all_prices( GncSqlBackend* be )
pBook = be->primary_book; pBook = be->primary_book;
pPriceDB = gnc_book_get_pricedb( pBook ); pPriceDB = gnc_book_get_pricedb( pBook );
stmt = gnc_sql_create_select_statement( be, TABLE_NAME ); stmt = gnc_sql_create_select_statement( be, TABLE_NAME );
if( stmt != NULL ) { if ( stmt != NULL )
{
result = gnc_sql_execute_select_statement( be, stmt ); result = gnc_sql_execute_select_statement( be, stmt );
gnc_sql_statement_dispose( stmt ); gnc_sql_statement_dispose( stmt );
if( result != NULL ) { if ( result != NULL )
{
GNCPrice* pPrice; GNCPrice* pPrice;
GncSqlRow* row = gnc_sql_result_get_first_row( result ); GncSqlRow* row = gnc_sql_result_get_first_row( result );
gchar* sql; gchar* sql;
gnc_pricedb_set_bulk_update( pPriceDB, TRUE ); gnc_pricedb_set_bulk_update( pPriceDB, TRUE );
while( row != NULL ) { while ( row != NULL )
{
pPrice = load_single_price( be, row ); pPrice = load_single_price( be, row );
if( pPrice != NULL ) { if ( pPrice != NULL )
{
(void)gnc_pricedb_add_price( pPriceDB, pPrice ); (void)gnc_pricedb_add_price( pPriceDB, pPrice );
gnc_price_unref( pPrice ); gnc_price_unref( pPrice );
} }
@ -134,9 +138,12 @@ create_prices_tables( GncSqlBackend* be )
g_return_if_fail( be != NULL ); g_return_if_fail( be != NULL );
version = gnc_sql_get_table_version( be, TABLE_NAME ); version = gnc_sql_get_table_version( be, TABLE_NAME );
if( version == 0 ) { if ( version == 0 )
{
(void)gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table ); (void)gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table );
} else if( version == 1 ) { }
else if ( version == 1 )
{
/* Upgrade 64 bit int handling */ /* Upgrade 64 bit int handling */
gnc_sql_upgrade_table( be, TABLE_NAME, col_table ); gnc_sql_upgrade_table( be, TABLE_NAME, col_table );
(void)gnc_sql_set_table_version( be, TABLE_NAME, TABLE_VERSION ); (void)gnc_sql_set_table_version( be, TABLE_NAME, TABLE_VERSION );
@ -158,21 +165,28 @@ save_price( GncSqlBackend* be, QofInstance* inst )
g_return_val_if_fail( GNC_IS_PRICE(inst), FALSE ); g_return_val_if_fail( GNC_IS_PRICE(inst), FALSE );
is_infant = qof_instance_get_infant( inst ); is_infant = qof_instance_get_infant( inst );
if( qof_instance_get_destroying( inst ) ) { if ( qof_instance_get_destroying( inst ) )
{
op = OP_DB_DELETE; op = OP_DB_DELETE;
} else if( be->is_pristine_db || is_infant ) { }
else if ( be->is_pristine_db || is_infant )
{
op = OP_DB_INSERT; op = OP_DB_INSERT;
} else { }
else
{
op = OP_DB_UPDATE; op = OP_DB_UPDATE;
} }
if( op != OP_DB_DELETE ) { if ( op != OP_DB_DELETE )
{
/* Ensure commodity and currency are in the db */ /* Ensure commodity and currency are in the db */
(void)gnc_sql_save_commodity( be, gnc_price_get_commodity( pPrice ) ); (void)gnc_sql_save_commodity( be, gnc_price_get_commodity( pPrice ) );
is_ok = gnc_sql_save_commodity( be, gnc_price_get_currency( pPrice ) ); is_ok = gnc_sql_save_commodity( be, gnc_price_get_currency( pPrice ) );
} }
if( is_ok ) { if ( is_ok )
{
is_ok = gnc_sql_do_db_operation( be, op, TABLE_NAME, GNC_ID_PRICE, pPrice, col_table ); is_ok = gnc_sql_do_db_operation( be, op, TABLE_NAME, GNC_ID_PRICE, pPrice, col_table );
} }
@ -187,7 +201,8 @@ write_price( GNCPrice* p, gpointer data )
g_return_val_if_fail( p != NULL, FALSE ); g_return_val_if_fail( p != NULL, FALSE );
g_return_val_if_fail( data != NULL, FALSE ); g_return_val_if_fail( data != NULL, FALSE );
if( s->is_ok ) { if ( s->is_ok )
{
s->is_ok = save_price( s->be, QOF_INSTANCE(p) ); s->is_ok = save_price( s->be, QOF_INSTANCE(p) );
} }

View File

@ -49,10 +49,13 @@
#define BUDGET_MAX_RECURRENCE_PERIOD_TYPE_LEN 2048 #define BUDGET_MAX_RECURRENCE_PERIOD_TYPE_LEN 2048
typedef struct { typedef struct
{
/*@ dependent @*/ GncSqlBackend* be; /*@ dependent @*/ GncSqlBackend* be;
/*@ dependent @*/ const GncGUID* guid; /*@ dependent @*/
/*@ dependent @*/ Recurrence* pRecurrence; const GncGUID* guid;
/*@ dependent @*/
Recurrence* pRecurrence;
} recurrence_info_t; } recurrence_info_t;
static /*@ null @*/ gpointer get_obj_guid( gpointer pObject ); static /*@ null @*/ gpointer get_obj_guid( gpointer pObject );
@ -68,14 +71,22 @@ static const GncSqlColumnTableEntry col_table[] =
{ {
/*@ -full_init_block @*/ /*@ -full_init_block @*/
{ "id", CT_INT, 0, COL_PKEY | COL_NNUL | COL_AUTOINC }, { "id", CT_INT, 0, COL_PKEY | COL_NNUL | COL_AUTOINC },
{ "obj_guid", CT_GUID, 0, COL_NNUL, NULL, NULL, {
(QofAccessFunc)get_obj_guid, (QofSetterFunc)set_obj_guid }, "obj_guid", CT_GUID, 0, COL_NNUL, NULL, NULL,
{ "recurrence_mult", CT_INT, 0, COL_NNUL, NULL, NULL, (QofAccessFunc)get_obj_guid, (QofSetterFunc)set_obj_guid
(QofAccessFunc)get_recurrence_mult, (QofSetterFunc)set_recurrence_mult }, },
{ "recurrence_period_type", CT_STRING, BUDGET_MAX_RECURRENCE_PERIOD_TYPE_LEN, COL_NNUL, NULL, NULL, {
(QofAccessFunc)get_recurrence_period_type, set_recurrence_period_type }, "recurrence_mult", CT_INT, 0, COL_NNUL, NULL, NULL,
{ "recurrence_period_start", CT_GDATE, 0, COL_NNUL, NULL, NULL, (QofAccessFunc)get_recurrence_mult, (QofSetterFunc)set_recurrence_mult
(QofAccessFunc)get_recurrence_period_start, set_recurrence_period_start }, },
{
"recurrence_period_type", CT_STRING, BUDGET_MAX_RECURRENCE_PERIOD_TYPE_LEN, COL_NNUL, NULL, NULL,
(QofAccessFunc)get_recurrence_period_type, set_recurrence_period_type
},
{
"recurrence_period_start", CT_GDATE, 0, COL_NNUL, NULL, NULL,
(QofAccessFunc)get_recurrence_period_start, set_recurrence_period_start
},
{ NULL } { NULL }
/*@ +full_init_block @*/ /*@ +full_init_block @*/
}; };
@ -85,8 +96,10 @@ a column other than the primary key */
static const GncSqlColumnTableEntry guid_col_table[] = static const GncSqlColumnTableEntry guid_col_table[] =
{ {
/*@ -full_init_block @*/ /*@ -full_init_block @*/
{ "obj_guid", CT_GUID, 0, 0, NULL, NULL, {
(QofAccessFunc)get_obj_guid, (QofSetterFunc)set_obj_guid }, "obj_guid", CT_GUID, 0, 0, NULL, NULL,
(QofAccessFunc)get_obj_guid, (QofSetterFunc)set_obj_guid
},
{ NULL } { NULL }
/*@ +full_init_block @*/ /*@ +full_init_block @*/
}; };
@ -214,7 +227,8 @@ gnc_sql_recurrence_save_list( GncSqlBackend* be, const GncGUID* guid, GList* sch
recurrence_info.be = be; recurrence_info.be = be;
recurrence_info.guid = guid; recurrence_info.guid = guid;
for( l = schedule; l != NULL; l = g_list_next( l ) ) { for ( l = schedule; l != NULL; l = g_list_next( l ) )
{
recurrence_info.pRecurrence = (Recurrence*)l->data; recurrence_info.pRecurrence = (Recurrence*)l->data;
(void)gnc_sql_do_db_operation( be, OP_DB_INSERT, TABLE_NAME, (void)gnc_sql_do_db_operation( be, OP_DB_INSERT, TABLE_NAME,
TABLE_NAME, &recurrence_info, col_table ); TABLE_NAME, &recurrence_info, col_table );
@ -280,17 +294,22 @@ gnc_sql_recurrence_load( GncSqlBackend* be, const GncGUID* guid )
g_return_val_if_fail( guid != NULL, NULL ); g_return_val_if_fail( guid != NULL, NULL );
result = gnc_sql_set_recurrences_from_db( be, guid ); result = gnc_sql_set_recurrences_from_db( be, guid );
if( result != NULL ) { if ( result != NULL )
{
guint numRows = gnc_sql_result_get_num_rows( result ); guint numRows = gnc_sql_result_get_num_rows( result );
if( numRows > 0 ) { if ( numRows > 0 )
if( numRows > 1 ) { {
if ( numRows > 1 )
{
g_warning( "More than 1 recurrence found: first one used" ); g_warning( "More than 1 recurrence found: first one used" );
} }
r = g_new0( Recurrence, 1 ); r = g_new0( Recurrence, 1 );
g_assert( r != NULL ); g_assert( r != NULL );
load_recurrence( be, gnc_sql_result_get_first_row( result ), r ); load_recurrence( be, gnc_sql_result_get_first_row( result ), r );
} else { }
else
{
g_warning( "No recurrences found" ); g_warning( "No recurrences found" );
} }
gnc_sql_result_dispose( result ); gnc_sql_result_dispose( result );
@ -309,10 +328,12 @@ gnc_sql_recurrence_load_list( GncSqlBackend* be, const GncGUID* guid )
g_return_val_if_fail( guid != NULL, NULL ); g_return_val_if_fail( guid != NULL, NULL );
result = gnc_sql_set_recurrences_from_db( be, guid ); result = gnc_sql_set_recurrences_from_db( be, guid );
if( result != NULL ) { if ( result != NULL )
{
GncSqlRow* row = gnc_sql_result_get_first_row( result ); GncSqlRow* row = gnc_sql_result_get_first_row( result );
while( row != NULL ) { while ( row != NULL )
{
Recurrence* pRecurrence = g_new0( Recurrence, 1 ); Recurrence* pRecurrence = g_new0( Recurrence, 1 );
g_assert( pRecurrence != NULL ); g_assert( pRecurrence != NULL );
load_recurrence( be, row, pRecurrence ); load_recurrence( be, row, pRecurrence );
@ -334,7 +355,8 @@ create_recurrence_tables( GncSqlBackend* be )
g_return_if_fail( be != NULL ); g_return_if_fail( be != NULL );
version = gnc_sql_get_table_version( be, TABLE_NAME ); version = gnc_sql_get_table_version( be, TABLE_NAME );
if( version == 0 ) { if ( version == 0 )
{
(void)gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table ); (void)gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table );
} }
} }

View File

@ -116,18 +116,21 @@ load_all_sxes( GncSqlBackend* be )
if ( stmt == NULL ) return; if ( stmt == NULL ) return;
result = gnc_sql_execute_select_statement( be, stmt ); result = gnc_sql_execute_select_statement( be, stmt );
gnc_sql_statement_dispose( stmt ); gnc_sql_statement_dispose( stmt );
if( result != NULL ) { if ( result != NULL )
{
GncSqlRow* row; GncSqlRow* row;
SchedXactions *sxes; SchedXactions *sxes;
GList* list = NULL; GList* list = NULL;
sxes = gnc_book_get_schedxactions( be->primary_book ); sxes = gnc_book_get_schedxactions( be->primary_book );
row = gnc_sql_result_get_first_row( result ); row = gnc_sql_result_get_first_row( result );
while( row != NULL ) { while ( row != NULL )
{
SchedXaction* sx; SchedXaction* sx;
sx = load_single_sx( be, row ); sx = load_single_sx( be, row );
if( sx != NULL ) { if ( sx != NULL )
{
gnc_sxes_add_sx( sxes, sx ); gnc_sxes_add_sx( sxes, sx );
list = g_list_prepend( list, sx ); list = g_list_prepend( list, sx );
} }
@ -135,7 +138,8 @@ load_all_sxes( GncSqlBackend* be )
} }
gnc_sql_result_dispose( result ); gnc_sql_result_dispose( result );
if( list != NULL ) { if ( list != NULL )
{
gnc_sql_slots_load_for_list( be, list ); gnc_sql_slots_load_for_list( be, list );
g_list_free( list ); g_list_free( list );
} }
@ -151,7 +155,8 @@ create_sx_tables( GncSqlBackend* be )
g_return_if_fail( be != NULL ); g_return_if_fail( be != NULL );
version = gnc_sql_get_table_version( be, SCHEDXACTION_TABLE ); version = gnc_sql_get_table_version( be, SCHEDXACTION_TABLE );
if( version == 0 ) { if ( version == 0 )
{
(void)gnc_sql_create_table( be, SCHEDXACTION_TABLE, TABLE_VERSION, col_table ); (void)gnc_sql_create_table( be, SCHEDXACTION_TABLE, TABLE_VERSION, col_table );
} }
} }
@ -173,26 +178,38 @@ gnc_sql_save_schedxaction( GncSqlBackend* be, QofInstance* inst )
pSx = GNC_SX(inst); pSx = GNC_SX(inst);
is_infant = qof_instance_get_infant( inst ); is_infant = qof_instance_get_infant( inst );
if( qof_instance_get_destroying( inst ) ) { if ( qof_instance_get_destroying( inst ) )
{
op = OP_DB_DELETE; op = OP_DB_DELETE;
} else if( be->is_pristine_db || is_infant ) { }
else if ( be->is_pristine_db || is_infant )
{
op = OP_DB_INSERT; op = OP_DB_INSERT;
} else { }
else
{
op = OP_DB_UPDATE; op = OP_DB_UPDATE;
} }
is_ok = gnc_sql_do_db_operation( be, op, SCHEDXACTION_TABLE, GNC_SX_ID, pSx, col_table ); is_ok = gnc_sql_do_db_operation( be, op, SCHEDXACTION_TABLE, GNC_SX_ID, pSx, col_table );
guid = qof_instance_get_guid( inst ); guid = qof_instance_get_guid( inst );
if( op == OP_DB_INSERT || op == OP_DB_UPDATE ) { if ( op == OP_DB_INSERT || op == OP_DB_UPDATE )
{
gnc_sql_recurrence_save_list( be, guid, gnc_sx_get_schedule( pSx ) ); gnc_sql_recurrence_save_list( be, guid, gnc_sx_get_schedule( pSx ) );
} else { }
else
{
gnc_sql_recurrence_delete( be, guid ); gnc_sql_recurrence_delete( be, guid );
} }
if( is_ok ) { if ( is_ok )
{
// Now, commit any slots // Now, commit any slots
if( op == OP_DB_INSERT || op == OP_DB_UPDATE ) { if ( op == OP_DB_INSERT || op == OP_DB_UPDATE )
{
is_ok = gnc_sql_slots_save( be, guid, is_infant, qof_instance_get_slots( inst ) ); is_ok = gnc_sql_slots_save( be, guid, is_infant, qof_instance_get_slots( inst ) );
} else { }
else
{
is_ok = gnc_sql_slots_delete( be, guid ); is_ok = gnc_sql_slots_delete( be, guid );
} }
} }

View File

@ -46,13 +46,17 @@
#define TABLE_NAME "slots" #define TABLE_NAME "slots"
#define TABLE_VERSION 2 #define TABLE_VERSION 2
typedef struct { typedef struct
{
/*@ dependent @*/ GncSqlBackend* be; /*@ dependent @*/ GncSqlBackend* be;
/*@ dependent @*/ const GncGUID* guid; /*@ dependent @*/
const GncGUID* guid;
gboolean is_ok; gboolean is_ok;
/*@ dependent @*/ KvpFrame* pKvpFrame; /*@ dependent @*/
KvpFrame* pKvpFrame;
KvpValueType value_type; KvpValueType value_type;
/*@ dependent @*/ KvpValue* pKvpValue; /*@ dependent @*/
KvpValue* pKvpValue;
GString* path; GString* path;
} slot_info_t; } slot_info_t;
@ -82,24 +86,42 @@ static const GncSqlColumnTableEntry col_table[] =
{ {
/*@ -full_init_block @*/ /*@ -full_init_block @*/
{ "id", CT_INT, 0, COL_PKEY | COL_NNUL | COL_AUTOINC }, { "id", CT_INT, 0, COL_PKEY | COL_NNUL | COL_AUTOINC },
{ "obj_guid", CT_GUID, 0, COL_NNUL, NULL, NULL, {
(QofAccessFunc)get_obj_guid, (QofSetterFunc)set_obj_guid }, "obj_guid", CT_GUID, 0, COL_NNUL, NULL, NULL,
{ "name", CT_STRING, SLOT_MAX_PATHNAME_LEN, COL_NNUL, NULL, NULL, (QofAccessFunc)get_obj_guid, (QofSetterFunc)set_obj_guid
(QofAccessFunc)get_path, set_path }, },
{ "slot_type", CT_INT, 0, COL_NNUL, NULL, NULL, {
(QofAccessFunc)get_slot_type, set_slot_type, }, "name", CT_STRING, SLOT_MAX_PATHNAME_LEN, COL_NNUL, NULL, NULL,
{ "int64_val", CT_INT64, 0, 0, NULL, NULL, (QofAccessFunc)get_path, set_path
(QofAccessFunc)get_int64_val, (QofSetterFunc)set_int64_val }, },
{ "string_val", CT_STRING, SLOT_MAX_PATHNAME_LEN, 0, NULL, NULL, {
(QofAccessFunc)get_string_val, set_string_val }, "slot_type", CT_INT, 0, COL_NNUL, NULL, NULL,
{ "double_val", CT_DOUBLE, 0, 0, NULL, NULL, (QofAccessFunc)get_slot_type, set_slot_type,
(QofAccessFunc)get_double_val, set_double_val }, },
{ "timespec_val", CT_TIMESPEC, 0, 0, NULL, NULL, {
(QofAccessFunc)get_timespec_val, (QofSetterFunc)set_timespec_val }, "int64_val", CT_INT64, 0, 0, NULL, NULL,
{ "guid_val", CT_GUID, 0, 0, NULL, NULL, (QofAccessFunc)get_int64_val, (QofSetterFunc)set_int64_val
(QofAccessFunc)get_guid_val, set_guid_val }, },
{ "numeric_val", CT_NUMERIC, 0, 0, NULL, NULL, {
(QofAccessFunc)get_numeric_val, (QofSetterFunc)set_numeric_val }, "string_val", CT_STRING, SLOT_MAX_PATHNAME_LEN, 0, NULL, NULL,
(QofAccessFunc)get_string_val, set_string_val
},
{
"double_val", CT_DOUBLE, 0, 0, NULL, NULL,
(QofAccessFunc)get_double_val, set_double_val
},
{
"timespec_val", CT_TIMESPEC, 0, 0, NULL, NULL,
(QofAccessFunc)get_timespec_val, (QofSetterFunc)set_timespec_val
},
{
"guid_val", CT_GUID, 0, 0, NULL, NULL,
(QofAccessFunc)get_guid_val, set_guid_val
},
{
"numeric_val", CT_NUMERIC, 0, 0, NULL, NULL,
(QofAccessFunc)get_numeric_val, (QofSetterFunc)set_numeric_val
},
{ NULL } { NULL }
/*@ +full_init_block @*/ /*@ +full_init_block @*/
}; };
@ -150,7 +172,8 @@ set_path( gpointer pObject, /*@ null @*/ gpointer pValue )
g_return_if_fail( pObject != NULL ); g_return_if_fail( pObject != NULL );
g_return_if_fail( pValue != NULL ); g_return_if_fail( pValue != NULL );
if( pInfo->path != NULL ) { if ( pInfo->path != NULL )
{
(void)g_string_free( pInfo->path, TRUE ); (void)g_string_free( pInfo->path, TRUE );
} }
pInfo->path = g_string_new( (gchar*)pValue ); pInfo->path = g_string_new( (gchar*)pValue );
@ -184,9 +207,12 @@ get_int64_val( gpointer pObject )
g_return_val_if_fail( pObject != NULL, 0 ); g_return_val_if_fail( pObject != NULL, 0 );
if( kvp_value_get_type( pInfo->pKvpValue ) == KVP_TYPE_GINT64 ) { if ( kvp_value_get_type( pInfo->pKvpValue ) == KVP_TYPE_GINT64 )
{
return kvp_value_get_gint64( pInfo->pKvpValue ); return kvp_value_get_gint64( pInfo->pKvpValue );
} else { }
else
{
return 0; return 0;
} }
} }
@ -198,7 +224,8 @@ set_int64_val( gpointer pObject, gint64 value )
g_return_if_fail( pObject != NULL ); g_return_if_fail( pObject != NULL );
if( pInfo->value_type == KVP_TYPE_GINT64 ) { if ( pInfo->value_type == KVP_TYPE_GINT64 )
{
kvp_frame_set_gint64( pInfo->pKvpFrame, pInfo->path->str, value ); kvp_frame_set_gint64( pInfo->pKvpFrame, pInfo->path->str, value );
} }
} }
@ -210,9 +237,12 @@ get_string_val( gpointer pObject )
g_return_val_if_fail( pObject != NULL, NULL ); g_return_val_if_fail( pObject != NULL, NULL );
if( kvp_value_get_type( pInfo->pKvpValue ) == KVP_TYPE_STRING ) { if ( kvp_value_get_type( pInfo->pKvpValue ) == KVP_TYPE_STRING )
{
return (gpointer)kvp_value_get_string( pInfo->pKvpValue ); return (gpointer)kvp_value_get_string( pInfo->pKvpValue );
} else { }
else
{
return NULL; return NULL;
} }
} }
@ -224,7 +254,8 @@ set_string_val( gpointer pObject, /*@ null @*/ gpointer pValue )
g_return_if_fail( pObject != NULL ); g_return_if_fail( pObject != NULL );
if( pInfo->value_type == KVP_TYPE_STRING && pValue != NULL ) { if ( pInfo->value_type == KVP_TYPE_STRING && pValue != NULL )
{
kvp_frame_set_string( pInfo->pKvpFrame, pInfo->path->str, (const gchar*)pValue ); kvp_frame_set_string( pInfo->pKvpFrame, pInfo->path->str, (const gchar*)pValue );
} }
} }
@ -237,10 +268,13 @@ get_double_val( gpointer pObject )
g_return_val_if_fail( pObject != NULL, NULL ); g_return_val_if_fail( pObject != NULL, NULL );
if( kvp_value_get_type( pInfo->pKvpValue ) == KVP_TYPE_DOUBLE ) { if ( kvp_value_get_type( pInfo->pKvpValue ) == KVP_TYPE_DOUBLE )
{
d_val = kvp_value_get_double( pInfo->pKvpValue ); d_val = kvp_value_get_double( pInfo->pKvpValue );
return (gpointer)&d_val; return (gpointer)&d_val;
} else { }
else
{
return NULL; return NULL;
} }
} }
@ -252,7 +286,8 @@ set_double_val( gpointer pObject, /*@ null @*/ gpointer pValue )
g_return_if_fail( pObject != NULL ); g_return_if_fail( pObject != NULL );
if( pInfo->value_type == KVP_TYPE_DOUBLE && pValue != NULL ) { if ( pInfo->value_type == KVP_TYPE_DOUBLE && pValue != NULL )
{
kvp_frame_set_double( pInfo->pKvpFrame, pInfo->path->str, *(double*)pValue ); kvp_frame_set_double( pInfo->pKvpFrame, pInfo->path->str, *(double*)pValue );
} }
} }
@ -275,7 +310,8 @@ set_timespec_val( gpointer pObject, Timespec ts )
g_return_if_fail( pObject != NULL ); g_return_if_fail( pObject != NULL );
if( pInfo->value_type == KVP_TYPE_TIMESPEC ) { if ( pInfo->value_type == KVP_TYPE_TIMESPEC )
{
kvp_frame_set_timespec( pInfo->pKvpFrame, pInfo->path->str, ts ); kvp_frame_set_timespec( pInfo->pKvpFrame, pInfo->path->str, ts );
} }
} }
@ -287,9 +323,12 @@ get_guid_val( gpointer pObject )
g_return_val_if_fail( pObject != NULL, NULL ); g_return_val_if_fail( pObject != NULL, NULL );
if( kvp_value_get_type( pInfo->pKvpValue ) == KVP_TYPE_GUID ) { if ( kvp_value_get_type( pInfo->pKvpValue ) == KVP_TYPE_GUID )
{
return (gpointer)kvp_value_get_guid( pInfo->pKvpValue ); return (gpointer)kvp_value_get_guid( pInfo->pKvpValue );
} else { }
else
{
return NULL; return NULL;
} }
} }
@ -301,7 +340,8 @@ set_guid_val( gpointer pObject, /*@ null @*/ gpointer pValue )
g_return_if_fail( pObject != NULL ); g_return_if_fail( pObject != NULL );
if( pInfo->value_type == KVP_TYPE_GUID && pValue != NULL ) { if ( pInfo->value_type == KVP_TYPE_GUID && pValue != NULL )
{
kvp_frame_set_guid( pInfo->pKvpFrame, pInfo->path->str, (GncGUID*)pValue ); kvp_frame_set_guid( pInfo->pKvpFrame, pInfo->path->str, (GncGUID*)pValue );
} }
} }
@ -313,9 +353,12 @@ get_numeric_val( gpointer pObject )
g_return_val_if_fail( pObject != NULL, gnc_numeric_zero() ); g_return_val_if_fail( pObject != NULL, gnc_numeric_zero() );
if( kvp_value_get_type( pInfo->pKvpValue ) == KVP_TYPE_NUMERIC ) { if ( kvp_value_get_type( pInfo->pKvpValue ) == KVP_TYPE_NUMERIC )
{
return kvp_value_get_numeric( pInfo->pKvpValue ); return kvp_value_get_numeric( pInfo->pKvpValue );
} else { }
else
{
return gnc_numeric_zero(); return gnc_numeric_zero();
} }
} }
@ -327,7 +370,8 @@ set_numeric_val( gpointer pObject, gnc_numeric value )
g_return_if_fail( pObject != NULL ); g_return_if_fail( pObject != NULL );
if( pInfo->value_type == KVP_TYPE_NUMERIC ) { if ( pInfo->value_type == KVP_TYPE_NUMERIC )
{
kvp_frame_set_numeric( pInfo->pKvpFrame, pInfo->path->str, value ); kvp_frame_set_numeric( pInfo->pKvpFrame, pInfo->path->str, value );
} }
} }
@ -343,21 +387,26 @@ save_slot( const gchar* key, KvpValue* value, gpointer data )
g_return_if_fail( data != NULL ); g_return_if_fail( data != NULL );
// Ignore if we've already run into a failure // Ignore if we've already run into a failure
if( !pSlot_info->is_ok ) { if ( !pSlot_info->is_ok )
{
return; return;
} }
curlen = pSlot_info->path->len; curlen = pSlot_info->path->len;
pSlot_info->pKvpValue = value; pSlot_info->pKvpValue = value;
if( curlen != 0 ) { if ( curlen != 0 )
{
(void)g_string_append( pSlot_info->path, "/" ); (void)g_string_append( pSlot_info->path, "/" );
} }
(void)g_string_append( pSlot_info->path, key ); (void)g_string_append( pSlot_info->path, key );
if( kvp_value_get_type( value ) == KVP_TYPE_FRAME ) { if ( kvp_value_get_type( value ) == KVP_TYPE_FRAME )
{
KvpFrame* pKvpFrame = kvp_value_get_frame( value ); KvpFrame* pKvpFrame = kvp_value_get_frame( value );
kvp_frame_for_each_slot( pKvpFrame, save_slot, pSlot_info ); kvp_frame_for_each_slot( pKvpFrame, save_slot, pSlot_info );
} else { }
else
{
pSlot_info->is_ok = gnc_sql_do_db_operation( pSlot_info->be, pSlot_info->is_ok = gnc_sql_do_db_operation( pSlot_info->be,
OP_DB_INSERT, TABLE_NAME, OP_DB_INSERT, TABLE_NAME,
TABLE_NAME, pSlot_info, TABLE_NAME, pSlot_info,
@ -377,7 +426,8 @@ gnc_sql_slots_save( GncSqlBackend* be, const GncGUID* guid, gboolean is_infant,
g_return_val_if_fail( pFrame != NULL, FALSE ); g_return_val_if_fail( pFrame != NULL, FALSE );
// If this is not saving into a new db, clear out the old saved slots first // If this is not saving into a new db, clear out the old saved slots first
if( !be->is_pristine_db && !is_infant ) { if ( !be->is_pristine_db && !is_infant )
{
(void)gnc_sql_slots_delete( be, guid ); (void)gnc_sql_slots_delete( be, guid );
} }
@ -423,7 +473,8 @@ load_slot( GncSqlBackend* be, GncSqlRow* row, KvpFrame* pFrame )
gnc_sql_load_object( be, row, TABLE_NAME, &slot_info, col_table ); gnc_sql_load_object( be, row, TABLE_NAME, &slot_info, col_table );
if( slot_info.path != NULL ) { if ( slot_info.path != NULL )
{
(void)g_string_free( slot_info.path, TRUE ); (void)g_string_free( slot_info.path, TRUE );
} }
} }
@ -448,13 +499,16 @@ gnc_sql_slots_load( GncSqlBackend* be, QofInstance* inst )
buf = g_strdup_printf( "SELECT * FROM %s WHERE obj_guid='%s'", TABLE_NAME, guid_buf ); buf = g_strdup_printf( "SELECT * FROM %s WHERE obj_guid='%s'", TABLE_NAME, guid_buf );
stmt = gnc_sql_create_statement_from_sql( be, buf ); stmt = gnc_sql_create_statement_from_sql( be, buf );
g_free( buf ); g_free( buf );
if( stmt != NULL ) { if ( stmt != NULL )
{
result = gnc_sql_execute_select_statement( be, stmt ); result = gnc_sql_execute_select_statement( be, stmt );
gnc_sql_statement_dispose( stmt ); gnc_sql_statement_dispose( stmt );
if( result != NULL ) { if ( result != NULL )
{
GncSqlRow* row = gnc_sql_result_get_first_row( result ); GncSqlRow* row = gnc_sql_result_get_first_row( result );
while( row != NULL ) { while ( row != NULL )
{
load_slot( be, row, pFrame ); load_slot( be, row, pFrame );
row = gnc_sql_result_get_next_row( result ); row = gnc_sql_result_get_next_row( result );
} }
@ -497,7 +551,8 @@ load_slot_for_list_item( GncSqlBackend* be, GncSqlRow* row, QofCollection* coll
gnc_sql_load_object( be, row, TABLE_NAME, &slot_info, col_table ); gnc_sql_load_object( be, row, TABLE_NAME, &slot_info, col_table );
if( slot_info.path != NULL ) { if ( slot_info.path != NULL )
{
(void)g_string_free( slot_info.path, TRUE ); (void)g_string_free( slot_info.path, TRUE );
} }
} }
@ -521,21 +576,26 @@ gnc_sql_slots_load_for_list( GncSqlBackend* be, GList* list )
// Create the query for all slots for all items on the list // Create the query for all slots for all items on the list
sql = g_string_sized_new( 40 + (GUID_ENCODING_LENGTH + 3) * g_list_length( list ) ); sql = g_string_sized_new( 40 + (GUID_ENCODING_LENGTH + 3) * g_list_length( list ) );
g_string_append_printf( sql, "SELECT * FROM %s WHERE %s ", TABLE_NAME, obj_guid_col_table[0].col_name ); g_string_append_printf( sql, "SELECT * FROM %s WHERE %s ", TABLE_NAME, obj_guid_col_table[0].col_name );
if( g_list_length( list ) != 1 ) { if ( g_list_length( list ) != 1 )
{
(void)g_string_append( sql, "IN (" ); (void)g_string_append( sql, "IN (" );
single_item = FALSE; single_item = FALSE;
} else { }
else
{
(void)g_string_append( sql, "= " ); (void)g_string_append( sql, "= " );
single_item = TRUE; single_item = TRUE;
} }
(void)gnc_sql_append_guid_list_to_sql( sql, list, G_MAXUINT ); (void)gnc_sql_append_guid_list_to_sql( sql, list, G_MAXUINT );
if( !single_item ) { if ( !single_item )
{
(void)g_string_append( sql, ")" ); (void)g_string_append( sql, ")" );
} }
// Execute the query and load the slots // Execute the query and load the slots
stmt = gnc_sql_create_statement_from_sql( be, sql->str ); stmt = gnc_sql_create_statement_from_sql( be, sql->str );
if( stmt == NULL ) { if ( stmt == NULL )
{
PERR( "stmt == NULL, SQL = '%s'\n", sql->str ); PERR( "stmt == NULL, SQL = '%s'\n", sql->str );
(void)g_string_free( sql, TRUE ); (void)g_string_free( sql, TRUE );
return; return;
@ -543,10 +603,12 @@ gnc_sql_slots_load_for_list( GncSqlBackend* be, GList* list )
(void)g_string_free( sql, TRUE ); (void)g_string_free( sql, TRUE );
result = gnc_sql_execute_select_statement( be, stmt ); result = gnc_sql_execute_select_statement( be, stmt );
gnc_sql_statement_dispose( stmt ); gnc_sql_statement_dispose( stmt );
if( result != NULL ) { if ( result != NULL )
{
GncSqlRow* row = gnc_sql_result_get_first_row( result ); GncSqlRow* row = gnc_sql_result_get_first_row( result );
while( row != NULL ) { while ( row != NULL )
{
load_slot_for_list_item( be, row, coll ); load_slot_for_list_item( be, row, coll );
row = gnc_sql_result_get_next_row( result ); row = gnc_sql_result_get_next_row( result );
} }
@ -576,7 +638,8 @@ load_slot_for_book_object( GncSqlBackend* be, GncSqlRow* row, BookLookupFn looku
gnc_sql_load_object( be, row, TABLE_NAME, &slot_info, col_table ); gnc_sql_load_object( be, row, TABLE_NAME, &slot_info, col_table );
if( slot_info.path != NULL ) { if ( slot_info.path != NULL )
{
(void)g_string_free( slot_info.path, TRUE ); (void)g_string_free( slot_info.path, TRUE );
} }
} }
@ -608,7 +671,8 @@ void gnc_sql_slots_load_for_sql_subquery( GncSqlBackend* be, const gchar* subque
// Execute the query and load the slots // Execute the query and load the slots
stmt = gnc_sql_create_statement_from_sql( be, sql ); stmt = gnc_sql_create_statement_from_sql( be, sql );
if( stmt == NULL ) { if ( stmt == NULL )
{
PERR( "stmt == NULL, SQL = '%s'\n", sql ); PERR( "stmt == NULL, SQL = '%s'\n", sql );
g_free( sql ); g_free( sql );
return; return;
@ -616,10 +680,12 @@ void gnc_sql_slots_load_for_sql_subquery( GncSqlBackend* be, const gchar* subque
g_free( sql ); g_free( sql );
result = gnc_sql_execute_select_statement( be, stmt ); result = gnc_sql_execute_select_statement( be, stmt );
gnc_sql_statement_dispose( stmt ); gnc_sql_statement_dispose( stmt );
if( result != NULL ) { if ( result != NULL )
{
GncSqlRow* row = gnc_sql_result_get_first_row( result ); GncSqlRow* row = gnc_sql_result_get_first_row( result );
while( row != NULL ) { while ( row != NULL )
{
load_slot_for_book_object( be, row, lookup_fn ); load_slot_for_book_object( be, row, lookup_fn );
row = gnc_sql_result_get_next_row( result ); row = gnc_sql_result_get_next_row( result );
} }
@ -637,18 +703,23 @@ create_slots_tables( GncSqlBackend* be )
g_return_if_fail( be != NULL ); g_return_if_fail( be != NULL );
version = gnc_sql_get_table_version( be, TABLE_NAME ); version = gnc_sql_get_table_version( be, TABLE_NAME );
if( version == 0 ) { if ( version == 0 )
{
(void)gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table ); (void)gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table );
ok = gnc_sql_create_index( be, "slots_guid_index", TABLE_NAME, obj_guid_col_table ); ok = gnc_sql_create_index( be, "slots_guid_index", TABLE_NAME, obj_guid_col_table );
if( !ok ) { if ( !ok )
{
PERR( "Unable to create index\n" ); PERR( "Unable to create index\n" );
} }
} else if( version == 1 ) { }
else if ( version == 1 )
{
/* Upgrade 64-bit int values to proper definition */ /* Upgrade 64-bit int values to proper definition */
gnc_sql_upgrade_table( be, TABLE_NAME, col_table ); gnc_sql_upgrade_table( be, TABLE_NAME, col_table );
ok = gnc_sql_create_index( be, "slots_guid_index", TABLE_NAME, obj_guid_col_table ); ok = gnc_sql_create_index( be, "slots_guid_index", TABLE_NAME, obj_guid_col_table );
if( !ok ) { if ( !ok )
{
PERR( "Unable to create index\n" ); PERR( "Unable to create index\n" );
} }
(void)gnc_sql_set_table_version( be, TABLE_NAME, TABLE_VERSION ); (void)gnc_sql_set_table_version( be, TABLE_NAME, TABLE_VERSION );

View File

@ -63,9 +63,11 @@
#define SPLIT_TABLE "splits" #define SPLIT_TABLE "splits"
#define SPLIT_TABLE_VERSION 4 #define SPLIT_TABLE_VERSION 4
typedef struct { typedef struct
{
/*@ dependent @*/ GncSqlBackend* be; /*@ dependent @*/ GncSqlBackend* be;
/*@ dependent @*/ const GncGUID* guid; /*@ dependent @*/
const GncGUID* guid;
gboolean is_ok; gboolean is_ok;
} split_info_t; } split_info_t;
@ -101,13 +103,17 @@ static const GncSqlColumnTableEntry split_col_table[] =
{ "account_guid", CT_ACCOUNTREF, 0, COL_NNUL, "account" }, { "account_guid", CT_ACCOUNTREF, 0, COL_NNUL, "account" },
{ "memo", CT_STRING, SPLIT_MAX_MEMO_LEN, COL_NNUL, "memo" }, { "memo", CT_STRING, SPLIT_MAX_MEMO_LEN, COL_NNUL, "memo" },
{ "action", CT_STRING, SPLIT_MAX_ACTION_LEN, COL_NNUL, "action" }, { "action", CT_STRING, SPLIT_MAX_ACTION_LEN, COL_NNUL, "action" },
{ "reconcile_state", CT_STRING, 1, COL_NNUL, NULL, NULL, {
(QofAccessFunc)get_split_reconcile_state, set_split_reconcile_state }, "reconcile_state", CT_STRING, 1, COL_NNUL, NULL, NULL,
(QofAccessFunc)get_split_reconcile_state, set_split_reconcile_state
},
{ "reconcile_date", CT_TIMESPEC, 0, 0, "reconcile-date" }, { "reconcile_date", CT_TIMESPEC, 0, 0, "reconcile-date" },
{ "value", CT_NUMERIC, 0, COL_NNUL, "value" }, { "value", CT_NUMERIC, 0, COL_NNUL, "value" },
{ "quantity", CT_NUMERIC, 0, COL_NNUL, "amount" }, { "quantity", CT_NUMERIC, 0, COL_NNUL, "amount" },
{ "lot_guid", CT_LOTREF, 0, 0, NULL, NULL, {
(QofAccessFunc)xaccSplitGetLot, set_split_lot }, "lot_guid", CT_LOTREF, 0, 0, NULL, NULL,
(QofAccessFunc)xaccSplitGetLot, set_split_lot
},
{ NULL } { NULL }
/*@ +full_init_block @*/ /*@ +full_init_block @*/
}; };
@ -205,12 +211,14 @@ load_single_split( GncSqlBackend* be, GncSqlRow* row )
split_guid = *guid; split_guid = *guid;
pSplit = xaccSplitLookup( &split_guid, be->primary_book ); pSplit = xaccSplitLookup( &split_guid, be->primary_book );
if( pSplit == NULL ) { if ( pSplit == NULL )
{
pSplit = xaccMallocSplit( be->primary_book ); pSplit = xaccMallocSplit( be->primary_book );
} }
/* If the split is dirty, don't overwrite it */ /* If the split is dirty, don't overwrite it */
if( !qof_instance_is_dirty( QOF_INSTANCE(pSplit) ) ) { if ( !qof_instance_is_dirty( QOF_INSTANCE(pSplit) ) )
{
gnc_sql_load_object( be, row, GNC_ID_SPLIT, pSplit, split_col_table ); gnc_sql_load_object( be, row, GNC_ID_SPLIT, pSplit, split_col_table );
} }
@ -236,21 +244,25 @@ load_splits_for_tx_list( GncSqlBackend* be, GList* list )
// Execute the query and load the splits // Execute the query and load the splits
result = gnc_sql_execute_select_sql( be, sql->str ); result = gnc_sql_execute_select_sql( be, sql->str );
if( result != NULL ) { if ( result != NULL )
{
GList* split_list = NULL; GList* split_list = NULL;
GncSqlRow* row; GncSqlRow* row;
row = gnc_sql_result_get_first_row( result ); row = gnc_sql_result_get_first_row( result );
while( row != NULL ) { while ( row != NULL )
{
Split* s; Split* s;
s = load_single_split( be, row ); s = load_single_split( be, row );
if( s != NULL ) { if ( s != NULL )
{
split_list = g_list_prepend( split_list, s ); split_list = g_list_prepend( split_list, s );
} }
row = gnc_sql_result_get_next_row( result ); row = gnc_sql_result_get_next_row( result );
} }
if( split_list != NULL ) { if ( split_list != NULL )
{
gnc_sql_slots_load_for_list( be, split_list ); gnc_sql_slots_load_for_list( be, split_list );
g_list_free( split_list ); g_list_free( split_list );
} }
@ -276,7 +288,8 @@ load_single_tx( GncSqlBackend* be, GncSqlRow* row )
// Don't overwrite the transaction if it's already been loaded (and possibly modified). // Don't overwrite the transaction if it's already been loaded (and possibly modified).
pTx = xaccTransLookup( &tx_guid, be->primary_book ); pTx = xaccTransLookup( &tx_guid, be->primary_book );
if( pTx != NULL ) { if ( pTx != NULL )
{
return NULL; return NULL;
} }
@ -295,7 +308,8 @@ load_single_tx( GncSqlBackend* be, GncSqlRow* row )
* so that the end balances (which are calculated and correct on initial load) * so that the end balances (which are calculated and correct on initial load)
* are unchanged. * are unchanged.
*/ */
typedef struct { typedef struct
{
/*@ dependent @*/ Account* acc; /*@ dependent @*/ Account* acc;
gnc_numeric start_bal; gnc_numeric start_bal;
gnc_numeric end_bal; gnc_numeric end_bal;
@ -367,7 +381,8 @@ query_transactions( GncSqlBackend* be, GncSqlStatement* stmt )
g_return_if_fail( stmt != NULL ); g_return_if_fail( stmt != NULL );
result = gnc_sql_execute_select_statement( be, stmt ); result = gnc_sql_execute_select_statement( be, stmt );
if( result != NULL ) { if ( result != NULL )
{
GList* tx_list = NULL; GList* tx_list = NULL;
GList* node; GList* node;
GncSqlRow* row; GncSqlRow* row;
@ -389,9 +404,11 @@ query_transactions( GncSqlBackend* be, GncSqlStatement* stmt )
// Load the transactions // Load the transactions
row = gnc_sql_result_get_first_row( result ); row = gnc_sql_result_get_first_row( result );
while( row != NULL ) { while ( row != NULL )
{
tx = load_single_tx( be, row ); tx = load_single_tx( be, row );
if( tx != NULL ) { if ( tx != NULL )
{
tx_list = g_list_prepend( tx_list, tx ); tx_list = g_list_prepend( tx_list, tx );
} }
row = gnc_sql_result_get_next_row( result ); row = gnc_sql_result_get_next_row( result );
@ -399,13 +416,15 @@ query_transactions( GncSqlBackend* be, GncSqlStatement* stmt )
gnc_sql_result_dispose( result ); gnc_sql_result_dispose( result );
// Load all splits and slots for the transactions // Load all splits and slots for the transactions
if( tx_list != NULL ) { if ( tx_list != NULL )
{
gnc_sql_slots_load_for_list( be, tx_list ); gnc_sql_slots_load_for_list( be, tx_list );
load_splits_for_tx_list( be, tx_list ); load_splits_for_tx_list( be, tx_list );
} }
// Commit all of the transactions // Commit all of the transactions
for( node = tx_list; node != NULL; node = node->next ) { for ( node = tx_list; node != NULL; node = node->next )
{
Transaction* pTx = GNC_TRANSACTION(node->data); Transaction* pTx = GNC_TRANSACTION(node->data);
xaccTransCommitEdit( pTx ); xaccTransCommitEdit( pTx );
} }
@ -416,7 +435,8 @@ query_transactions( GncSqlBackend* be, GncSqlStatement* stmt )
// balance has changed, update the start balance so that 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. // balance is the same as it was before the splits were loaded.
// Repeat for cleared and reconciled balances. // Repeat for cleared and reconciled balances.
for( nextbal = bal_list; nextbal != NULL; nextbal = nextbal->next ) { for ( nextbal = bal_list; nextbal != NULL; nextbal = nextbal->next )
{
full_acct_balances_t* balns = (full_acct_balances_t*)nextbal->data; full_acct_balances_t* balns = (full_acct_balances_t*)nextbal->data;
gnc_numeric* pnew_end_bal; gnc_numeric* pnew_end_bal;
gnc_numeric* pnew_end_c_bal; gnc_numeric* pnew_end_c_bal;
@ -429,21 +449,24 @@ query_transactions( GncSqlBackend* be, GncSqlStatement* stmt )
"end-reconciled-balance", &pnew_end_r_bal, "end-reconciled-balance", &pnew_end_r_bal,
NULL ); NULL );
if( !gnc_numeric_eq( *pnew_end_bal, balns->end_bal ) ) { if ( !gnc_numeric_eq( *pnew_end_bal, balns->end_bal ) )
{
adj = gnc_numeric_sub( balns->end_bal, *pnew_end_bal, adj = gnc_numeric_sub( balns->end_bal, *pnew_end_bal,
GNC_DENOM_AUTO, GNC_HOW_DENOM_LCD ); GNC_DENOM_AUTO, GNC_HOW_DENOM_LCD );
balns->start_bal = gnc_numeric_add( balns->start_bal, adj, balns->start_bal = gnc_numeric_add( balns->start_bal, adj,
GNC_DENOM_AUTO, GNC_HOW_DENOM_LCD ); GNC_DENOM_AUTO, GNC_HOW_DENOM_LCD );
g_object_set( balns->acc, "start-balance", &balns->start_bal, NULL ); g_object_set( balns->acc, "start-balance", &balns->start_bal, NULL );
} }
if( !gnc_numeric_eq( *pnew_end_c_bal, balns->end_cleared_bal ) ) { if ( !gnc_numeric_eq( *pnew_end_c_bal, balns->end_cleared_bal ) )
{
adj = gnc_numeric_sub( balns->end_cleared_bal, *pnew_end_c_bal, adj = gnc_numeric_sub( balns->end_cleared_bal, *pnew_end_c_bal,
GNC_DENOM_AUTO, GNC_HOW_DENOM_LCD ); GNC_DENOM_AUTO, GNC_HOW_DENOM_LCD );
balns->start_cleared_bal = gnc_numeric_add( balns->start_cleared_bal, adj, balns->start_cleared_bal = gnc_numeric_add( balns->start_cleared_bal, adj,
GNC_DENOM_AUTO, GNC_HOW_DENOM_LCD ); GNC_DENOM_AUTO, GNC_HOW_DENOM_LCD );
g_object_set( balns->acc, "start-cleared-balance", &balns->start_cleared_bal, NULL ); 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 ) ) { if ( !gnc_numeric_eq( *pnew_end_r_bal, balns->end_reconciled_bal ) )
{
adj = gnc_numeric_sub( balns->end_reconciled_bal, *pnew_end_r_bal, adj = gnc_numeric_sub( balns->end_reconciled_bal, *pnew_end_r_bal,
GNC_DENOM_AUTO, GNC_HOW_DENOM_LCD ); GNC_DENOM_AUTO, GNC_HOW_DENOM_LCD );
balns->start_reconciled_bal = gnc_numeric_add( balns->start_reconciled_bal, adj, balns->start_reconciled_bal = gnc_numeric_add( balns->start_reconciled_bal, adj,
@ -456,7 +479,8 @@ query_transactions( GncSqlBackend* be, GncSqlStatement* stmt )
g_free( pnew_end_r_bal ); g_free( pnew_end_r_bal );
g_free( balns ); g_free( balns );
} }
if( bal_list != NULL ) { if ( bal_list != NULL )
{
g_slist_free( bal_list ); g_slist_free( bal_list );
} }
@ -481,13 +505,17 @@ create_transaction_tables( GncSqlBackend* be )
g_return_if_fail( be != NULL ); g_return_if_fail( be != NULL );
version = gnc_sql_get_table_version( be, TRANSACTION_TABLE ); version = gnc_sql_get_table_version( be, TRANSACTION_TABLE );
if( version == 0 ) { if ( version == 0 )
{
(void)gnc_sql_create_table( be, TRANSACTION_TABLE, TX_TABLE_VERSION, tx_col_table ); (void)gnc_sql_create_table( be, TRANSACTION_TABLE, TX_TABLE_VERSION, tx_col_table );
ok = gnc_sql_create_index( be, "tx_post_date_index", TRANSACTION_TABLE, post_date_col_table ); ok = gnc_sql_create_index( be, "tx_post_date_index", TRANSACTION_TABLE, post_date_col_table );
if( !ok ) { if ( !ok )
{
PERR( "Unable to create index\n" ); PERR( "Unable to create index\n" );
} }
} else if( version < TX_TABLE_VERSION ) { }
else if ( version < TX_TABLE_VERSION )
{
/* Upgrade: /* Upgrade:
1->2: 64 bit int handling 1->2: 64 bit int handling
2->3: allow dates to be NULL 2->3: allow dates to be NULL
@ -497,28 +525,35 @@ create_transaction_tables( GncSqlBackend* be )
} }
version = gnc_sql_get_table_version( be, SPLIT_TABLE ); version = gnc_sql_get_table_version( be, SPLIT_TABLE );
if( version == 0 ) { if ( version == 0 )
{
(void)gnc_sql_create_table( be, SPLIT_TABLE, SPLIT_TABLE_VERSION, split_col_table ); (void)gnc_sql_create_table( be, SPLIT_TABLE, SPLIT_TABLE_VERSION, split_col_table );
ok = gnc_sql_create_index( be, "splits_tx_guid_index", SPLIT_TABLE, tx_guid_col_table ); ok = gnc_sql_create_index( be, "splits_tx_guid_index", SPLIT_TABLE, tx_guid_col_table );
if( !ok ) { if ( !ok )
{
PERR( "Unable to create index\n" ); PERR( "Unable to create index\n" );
} }
ok = gnc_sql_create_index( be, "splits_account_guid_index", SPLIT_TABLE, account_guid_col_table ); ok = gnc_sql_create_index( be, "splits_account_guid_index", SPLIT_TABLE, account_guid_col_table );
if( !ok ) { if ( !ok )
{
PERR( "Unable to create index\n" ); PERR( "Unable to create index\n" );
} }
} else if( version < SPLIT_TABLE_VERSION ) { }
else if ( version < SPLIT_TABLE_VERSION )
{
/* Upgrade: /* Upgrade:
1->2: 64 bit int handling 1->2: 64 bit int handling
3->4: Split reconcile date can be NULL */ 3->4: Split reconcile date can be NULL */
gnc_sql_upgrade_table( be, SPLIT_TABLE, split_col_table ); gnc_sql_upgrade_table( be, SPLIT_TABLE, split_col_table );
ok = gnc_sql_create_index( be, "splits_tx_guid_index", SPLIT_TABLE, tx_guid_col_table ); ok = gnc_sql_create_index( be, "splits_tx_guid_index", SPLIT_TABLE, tx_guid_col_table );
if( !ok ) { if ( !ok )
{
PERR( "Unable to create index\n" ); PERR( "Unable to create index\n" );
} }
ok = gnc_sql_create_index( be, "splits_account_guid_index", SPLIT_TABLE, account_guid_col_table ); ok = gnc_sql_create_index( be, "splits_account_guid_index", SPLIT_TABLE, account_guid_col_table );
if( !ok ) { if ( !ok )
{
PERR( "Unable to create index\n" ); PERR( "Unable to create index\n" );
} }
(void)gnc_sql_set_table_version( be, SPLIT_TABLE, SPLIT_TABLE_VERSION ); (void)gnc_sql_set_table_version( be, SPLIT_TABLE, SPLIT_TABLE_VERSION );
@ -541,7 +576,8 @@ delete_split_slots_cb( gpointer data, gpointer user_data )
g_return_if_fail( GNC_IS_SPLIT(data) ); g_return_if_fail( GNC_IS_SPLIT(data) );
g_return_if_fail( user_data != NULL ); g_return_if_fail( user_data != NULL );
if( split_info->is_ok ) { if ( split_info->is_ok )
{
split_info->is_ok = gnc_sql_slots_delete( split_info->be, split_info->is_ok = gnc_sql_slots_delete( split_info->be,
qof_instance_get_guid( QOF_INSTANCE(pSplit) ) ); qof_instance_get_guid( QOF_INSTANCE(pSplit) ) );
} }
@ -563,7 +599,8 @@ delete_splits( GncSqlBackend* be, Transaction* pTx )
g_return_val_if_fail( pTx != NULL, FALSE ); g_return_val_if_fail( pTx != NULL, FALSE );
if ( !gnc_sql_do_db_operation( be, OP_DB_DELETE, SPLIT_TABLE, if ( !gnc_sql_do_db_operation( be, OP_DB_DELETE, SPLIT_TABLE,
SPLIT_TABLE, pTx, tx_guid_col_table ) ) { SPLIT_TABLE, pTx, tx_guid_col_table ) )
{
return FALSE; return FALSE;
} }
split_info.be = be; split_info.be = be;
@ -592,15 +629,21 @@ commit_split( GncSqlBackend* be, QofInstance* inst )
g_return_val_if_fail( be != NULL, FALSE ); g_return_val_if_fail( be != NULL, FALSE );
is_infant = qof_instance_get_infant( inst ); is_infant = qof_instance_get_infant( inst );
if( qof_instance_get_destroying( inst ) ) { if ( qof_instance_get_destroying( inst ) )
{
op = OP_DB_DELETE; op = OP_DB_DELETE;
} else if( be->is_pristine_db || is_infant ) { }
else if ( be->is_pristine_db || is_infant )
{
op = OP_DB_INSERT; op = OP_DB_INSERT;
} else { }
else
{
op = OP_DB_UPDATE; op = OP_DB_UPDATE;
} }
is_ok = gnc_sql_do_db_operation( be, op, SPLIT_TABLE, GNC_ID_SPLIT, inst, split_col_table ); is_ok = gnc_sql_do_db_operation( be, op, SPLIT_TABLE, GNC_ID_SPLIT, inst, split_col_table );
if( is_ok ) { if ( is_ok )
{
is_ok = gnc_sql_slots_save( be, is_ok = gnc_sql_slots_save( be,
qof_instance_get_guid( inst ), qof_instance_get_guid( inst ),
is_infant, is_infant,
@ -620,7 +663,8 @@ save_split_cb( gpointer data, gpointer user_data )
g_return_if_fail( GNC_IS_SPLIT(data) ); g_return_if_fail( GNC_IS_SPLIT(data) );
g_return_if_fail( user_data != NULL ); g_return_if_fail( user_data != NULL );
if( split_info->is_ok ) { if ( split_info->is_ok )
{
split_info->is_ok = commit_split( split_info->be, QOF_INSTANCE(pSplit) ); split_info->is_ok = commit_split( split_info->be, QOF_INSTANCE(pSplit) );
} }
} }
@ -656,34 +700,47 @@ save_transaction( GncSqlBackend* be, Transaction* pTx, gboolean do_save_splits )
inst = QOF_INSTANCE(pTx); inst = QOF_INSTANCE(pTx);
is_infant = qof_instance_get_infant( inst ); is_infant = qof_instance_get_infant( inst );
if( qof_instance_get_destroying( inst ) ) { if ( qof_instance_get_destroying( inst ) )
{
op = OP_DB_DELETE; op = OP_DB_DELETE;
} else if( be->is_pristine_db || is_infant ) { }
else if ( be->is_pristine_db || is_infant )
{
op = OP_DB_INSERT; op = OP_DB_INSERT;
} else { }
else
{
op = OP_DB_UPDATE; op = OP_DB_UPDATE;
} }
if( op != OP_DB_DELETE ) { if ( op != OP_DB_DELETE )
{
// Ensure the commodity is in the db // Ensure the commodity is in the db
is_ok = gnc_sql_save_commodity( be, xaccTransGetCurrency( pTx ) ); is_ok = gnc_sql_save_commodity( be, xaccTransGetCurrency( pTx ) );
} }
if( is_ok ) { if ( is_ok )
{
is_ok = gnc_sql_do_db_operation( be, op, TRANSACTION_TABLE, GNC_ID_TRANS, pTx, tx_col_table ); is_ok = gnc_sql_do_db_operation( be, op, TRANSACTION_TABLE, GNC_ID_TRANS, pTx, tx_col_table );
} }
if( is_ok ) { if ( is_ok )
{
// Commit slots and splits // Commit slots and splits
guid = qof_instance_get_guid( inst ); guid = qof_instance_get_guid( inst );
if( !qof_instance_get_destroying(inst) ) { if ( !qof_instance_get_destroying(inst) )
{
is_ok = gnc_sql_slots_save( be, guid, is_infant, qof_instance_get_slots( inst ) ); is_ok = gnc_sql_slots_save( be, guid, is_infant, qof_instance_get_slots( inst ) );
if( is_ok && do_save_splits ) { if ( is_ok && do_save_splits )
{
is_ok = save_splits( be, guid, xaccTransGetSplitList( pTx ) ); is_ok = save_splits( be, guid, xaccTransGetSplitList( pTx ) );
} }
} else { }
else
{
is_ok = gnc_sql_slots_delete( be, guid ); is_ok = gnc_sql_slots_delete( be, guid );
if( is_ok ) { if ( is_ok )
{
is_ok = delete_splits( be, pTx ); is_ok = delete_splits( be, pTx );
} }
} }
@ -733,10 +790,13 @@ get_guid_from_query( QofQuery* pQuery )
pPredData = qof_query_term_get_pred_data( pTerm ); pPredData = qof_query_term_get_pred_data( pTerm );
pParamPath = qof_query_term_get_param_path( pTerm ); pParamPath = qof_query_term_get_param_path( pTerm );
if( strcmp( pPredData->type_name, "guid" ) == 0 ) { if ( strcmp( pPredData->type_name, "guid" ) == 0 )
{
query_guid_t pData = (query_guid_t)pPredData; query_guid_t pData = (query_guid_t)pPredData;
return pData->guids->data; return pData->guids->data;
} else { }
else
{
return NULL; return NULL;
} }
} }
@ -764,7 +824,8 @@ void gnc_sql_transaction_load_tx_for_account( GncSqlBackend* be, Account* accoun
TRANSACTION_TABLE, SPLIT_TABLE, guid_buf ); TRANSACTION_TABLE, SPLIT_TABLE, guid_buf );
stmt = gnc_sql_create_statement_from_sql( be, query_sql ); stmt = gnc_sql_create_statement_from_sql( be, query_sql );
g_free( query_sql ); g_free( query_sql );
if( stmt != NULL ) { if ( stmt != NULL )
{
query_transactions( be, stmt ); query_transactions( be, stmt );
gnc_sql_statement_dispose( stmt ); gnc_sql_statement_dispose( stmt );
} }
@ -786,7 +847,8 @@ void gnc_sql_transaction_load_all_tx( GncSqlBackend* be )
query_sql = g_strdup_printf( "SELECT * FROM %s", TRANSACTION_TABLE ); query_sql = g_strdup_printf( "SELECT * FROM %s", TRANSACTION_TABLE );
stmt = gnc_sql_create_statement_from_sql( be, query_sql ); stmt = gnc_sql_create_statement_from_sql( be, query_sql );
g_free( query_sql ); g_free( query_sql );
if( stmt != NULL ) { if ( stmt != NULL )
{
query_transactions( be, stmt ); query_transactions( be, stmt );
gnc_sql_statement_dispose( stmt ); gnc_sql_statement_dispose( stmt );
} }
@ -796,24 +858,37 @@ static void
convert_query_comparison_to_sql( QofQueryPredData* pPredData, gboolean isInverted, GString* sql ) convert_query_comparison_to_sql( QofQueryPredData* pPredData, gboolean isInverted, GString* sql )
{ {
if ( pPredData->how == QOF_COMPARE_LT if ( pPredData->how == QOF_COMPARE_LT
|| ( isInverted && pPredData->how == QOF_COMPARE_GTE ) ) { || ( isInverted && pPredData->how == QOF_COMPARE_GTE ) )
{
g_string_append( sql, "<" ); g_string_append( sql, "<" );
} else if( pPredData->how == QOF_COMPARE_LTE }
|| ( isInverted && pPredData->how == QOF_COMPARE_GT ) ) { else if ( pPredData->how == QOF_COMPARE_LTE
|| ( isInverted && pPredData->how == QOF_COMPARE_GT ) )
{
g_string_append( sql, "<=" ); g_string_append( sql, "<=" );
} else if( pPredData->how == QOF_COMPARE_EQUAL }
|| ( isInverted && pPredData->how == QOF_COMPARE_NEQ ) ) { else if ( pPredData->how == QOF_COMPARE_EQUAL
|| ( isInverted && pPredData->how == QOF_COMPARE_NEQ ) )
{
g_string_append( sql, "=" ); g_string_append( sql, "=" );
} else if( pPredData->how == QOF_COMPARE_GT }
|| ( isInverted && pPredData->how == QOF_COMPARE_LTE ) ) { else if ( pPredData->how == QOF_COMPARE_GT
|| ( isInverted && pPredData->how == QOF_COMPARE_LTE ) )
{
g_string_append( sql, ">" ); g_string_append( sql, ">" );
} else if( pPredData->how == QOF_COMPARE_GTE }
|| ( isInverted && pPredData->how == QOF_COMPARE_LT ) ) { else if ( pPredData->how == QOF_COMPARE_GTE
|| ( isInverted && pPredData->how == QOF_COMPARE_LT ) )
{
g_string_append( sql, ">=" ); g_string_append( sql, ">=" );
} else if( pPredData->how == QOF_COMPARE_NEQ }
|| ( isInverted && pPredData->how == QOF_COMPARE_EQUAL ) ) { else if ( pPredData->how == QOF_COMPARE_NEQ
|| ( isInverted && pPredData->how == QOF_COMPARE_EQUAL ) )
{
g_string_append( sql, "~=" ); g_string_append( sql, "~=" );
} else { }
else
{
PERR( "Unknown comparison type\n" ); PERR( "Unknown comparison type\n" );
g_string_append( sql, "??" ); g_string_append( sql, "??" );
} }
@ -834,14 +909,16 @@ convert_query_term_to_sql( const GncSqlBackend* be, const gchar* fieldName, QofQ
pPredData = qof_query_term_get_pred_data( pTerm ); pPredData = qof_query_term_get_pred_data( pTerm );
isInverted = qof_query_term_is_inverted( pTerm ); isInverted = qof_query_term_is_inverted( pTerm );
if( safe_strcmp( pPredData->type_name, QOF_TYPE_GUID ) == 0 ) { if ( safe_strcmp( pPredData->type_name, QOF_TYPE_GUID ) == 0 )
{
query_guid_t guid_data = (query_guid_t)pPredData; query_guid_t guid_data = (query_guid_t)pPredData;
GList* guid_entry; GList* guid_entry;
g_string_append( sql, "(" ); g_string_append( sql, "(" );
g_string_append( sql, fieldName ); g_string_append( sql, fieldName );
switch( guid_data->options ) { switch ( guid_data->options )
{
case QOF_GUID_MATCH_ANY: case QOF_GUID_MATCH_ANY:
if ( isInverted ) g_string_append( sql, " NOT IN (" ); if ( isInverted ) g_string_append( sql, " NOT IN (" );
else g_string_append( sql, " IN (" ); else g_string_append( sql, " IN (" );
@ -856,7 +933,8 @@ convert_query_term_to_sql( const GncSqlBackend* be, const gchar* fieldName, QofQ
PERR( "Unexpected GncGUID match type: %d\n", guid_data->options ); PERR( "Unexpected GncGUID match type: %d\n", guid_data->options );
} }
for( guid_entry = guid_data->guids; guid_entry != NULL; guid_entry = guid_entry->next ) { for ( guid_entry = guid_data->guids; guid_entry != NULL; guid_entry = guid_entry->next )
{
gchar guid_buf[GUID_ENCODING_LENGTH+1]; gchar guid_buf[GUID_ENCODING_LENGTH+1];
if ( guid_entry != guid_data->guids ) g_string_append( sql, "," ); if ( guid_entry != guid_data->guids ) g_string_append( sql, "," );
@ -865,19 +943,25 @@ convert_query_term_to_sql( const GncSqlBackend* be, const gchar* fieldName, QofQ
} }
g_string_append( sql, "))" ); g_string_append( sql, "))" );
} else if( safe_strcmp( pPredData->type_name, QOF_TYPE_CHAR ) == 0 ) { }
else if ( safe_strcmp( pPredData->type_name, QOF_TYPE_CHAR ) == 0 )
{
query_char_t char_data = (query_char_t)pPredData; query_char_t char_data = (query_char_t)pPredData;
int i; int i;
if( isInverted ) { if ( isInverted )
{
g_string_append( sql, "NOT(" ); g_string_append( sql, "NOT(" );
} }
if( char_data->options == QOF_CHAR_MATCH_NONE ) { if ( char_data->options == QOF_CHAR_MATCH_NONE )
{
g_string_append( sql, "NOT " ); g_string_append( sql, "NOT " );
} }
g_string_append( sql, "(" ); g_string_append( sql, "(" );
for( i = 0; char_data->char_list[i] != '\0'; i++ ) { for ( i = 0; char_data->char_list[i] != '\0'; i++ )
if( i != 0 ) { {
if ( i != 0 )
{
g_string_append( sql, " OR " ); g_string_append( sql, " OR " );
} }
g_string_append( sql, fieldName ); g_string_append( sql, fieldName );
@ -886,22 +970,28 @@ convert_query_term_to_sql( const GncSqlBackend* be, const gchar* fieldName, QofQ
g_string_append( sql, "'" ); g_string_append( sql, "'" );
} }
g_string_append( sql, ") " ); g_string_append( sql, ") " );
if( isInverted ) { if ( isInverted )
{
g_string_append( sql, ") " ); g_string_append( sql, ") " );
} }
} else if( safe_strcmp( pPredData->type_name, QOF_TYPE_STRING ) == 0 ) { }
else if ( safe_strcmp( pPredData->type_name, QOF_TYPE_STRING ) == 0 )
{
query_string_t string_data = (query_string_t)pPredData; query_string_t string_data = (query_string_t)pPredData;
sqlEscape* escape = sqlEscape_new(); sqlEscape* escape = sqlEscape_new();
if( isInverted ) { if ( isInverted )
{
g_string_append( sql, "NOT(" ); g_string_append( sql, "NOT(" );
} }
if( pPredData->how == QOF_COMPARE_NEQ ) { if ( pPredData->how == QOF_COMPARE_NEQ )
{
g_string_append( sql, "NOT(" ); g_string_append( sql, "NOT(" );
} }
g_string_append( sql, fieldName ); g_string_append( sql, fieldName );
if( string_data->is_regex || string_data->options == QOF_STRING_MATCH_CASEINSENSITIVE ) { if ( string_data->is_regex || string_data->options == QOF_STRING_MATCH_CASEINSENSITIVE )
{
PWARN( "String is_regex || option = QOF_STRING_MATCH_INSENSITIVE\n" ); PWARN( "String is_regex || option = QOF_STRING_MATCH_INSENSITIVE\n" );
} }
// g_string_append( sql, " ~" ); // g_string_append( sql, " ~" );
@ -914,53 +1004,70 @@ convert_query_term_to_sql( const GncSqlBackend* be, const gchar* fieldName, QofQ
g_string_append( sql, "'" ); g_string_append( sql, "'" );
g_string_append( sql, sqlEscapeString( escape, string_data->matchstring ) ); g_string_append( sql, sqlEscapeString( escape, string_data->matchstring ) );
g_string_append( sql, "'" ); g_string_append( sql, "'" );
if( pPredData->how == QOF_COMPARE_NEQ ) { if ( pPredData->how == QOF_COMPARE_NEQ )
{
g_string_append( sql, ")" ); g_string_append( sql, ")" );
} }
if( isInverted ) { if ( isInverted )
{
g_string_append( sql, ")" ); g_string_append( sql, ")" );
} }
sqlEscape_destroy( escape ); sqlEscape_destroy( escape );
} else { }
else
{
g_string_append( sql, "(" ); g_string_append( sql, "(" );
g_string_append( sql, fieldName ); g_string_append( sql, fieldName );
convert_query_comparison_to_sql( pPredData, isInverted, sql ); convert_query_comparison_to_sql( pPredData, isInverted, sql );
if( strcmp( pPredData->type_name, QOF_TYPE_NUMERIC ) == 0 ) { if ( strcmp( pPredData->type_name, QOF_TYPE_NUMERIC ) == 0 )
{
query_numeric_t pData = (query_numeric_t)pPredData; query_numeric_t pData = (query_numeric_t)pPredData;
double d = gnc_numeric_to_double( pData->amount ); double d = gnc_numeric_to_double( pData->amount );
g_string_append_printf( sql, "%f", d ); g_string_append_printf( sql, "%f", d );
} else if( safe_strcmp( pPredData->type_name, QOF_TYPE_DATE ) == 0 ) { }
else if ( safe_strcmp( pPredData->type_name, QOF_TYPE_DATE ) == 0 )
{
query_date_t date_data = (query_date_t)pPredData; query_date_t date_data = (query_date_t)pPredData;
gchar* datebuf; gchar* datebuf;
datebuf = gnc_sql_convert_timespec_to_string( be, date_data->date ); datebuf = gnc_sql_convert_timespec_to_string( be, date_data->date );
g_string_append_printf( sql, "'%s'", datebuf ); g_string_append_printf( sql, "'%s'", datebuf );
} else if( strcmp( pPredData->type_name, QOF_TYPE_INT32 ) == 0 ) { }
else if ( strcmp( pPredData->type_name, QOF_TYPE_INT32 ) == 0 )
{
query_int32_t pData = (query_int32_t)pPredData; query_int32_t pData = (query_int32_t)pPredData;
g_string_append_printf( sql, "%d", pData->val ); g_string_append_printf( sql, "%d", pData->val );
} else if( strcmp( pPredData->type_name, QOF_TYPE_INT64 ) == 0 ) { }
else if ( strcmp( pPredData->type_name, QOF_TYPE_INT64 ) == 0 )
{
query_int64_t pData = (query_int64_t)pPredData; query_int64_t pData = (query_int64_t)pPredData;
g_string_append_printf( sql, "%" G_GINT64_FORMAT, pData->val ); g_string_append_printf( sql, "%" G_GINT64_FORMAT, pData->val );
} else if( strcmp( pPredData->type_name, QOF_TYPE_DOUBLE ) == 0 ) { }
else if ( strcmp( pPredData->type_name, QOF_TYPE_DOUBLE ) == 0 )
{
query_double_t pData = (query_double_t)pPredData; query_double_t pData = (query_double_t)pPredData;
g_string_append_printf( sql, "%f", pData->val ); g_string_append_printf( sql, "%f", pData->val );
} else if( strcmp( pPredData->type_name, QOF_TYPE_BOOLEAN ) == 0 ) { }
else if ( strcmp( pPredData->type_name, QOF_TYPE_BOOLEAN ) == 0 )
{
query_boolean_t pData = (query_boolean_t)pPredData; query_boolean_t pData = (query_boolean_t)pPredData;
g_string_append_printf( sql, "%d", pData->val ); g_string_append_printf( sql, "%d", pData->val );
} else { }
else
{
PERR( "Unknown query predicate type: %s\n", pPredData->type_name ); PERR( "Unknown query predicate type: %s\n", pPredData->type_name );
} }
@ -968,7 +1075,8 @@ convert_query_term_to_sql( const GncSqlBackend* be, const gchar* fieldName, QofQ
} }
} }
typedef struct { typedef struct
{
GncSqlStatement* stmt; GncSqlStatement* stmt;
gboolean has_been_run; gboolean has_been_run;
} split_query_info_t; } split_query_info_t;
@ -988,23 +1096,27 @@ compile_split_query( GncSqlBackend* be, QofQuery* query )
g_assert( query_info != NULL ); g_assert( query_info != NULL );
query_info->has_been_run = FALSE; query_info->has_been_run = FALSE;
if( qof_query_has_terms( query ) ) { if ( qof_query_has_terms( query ) )
{
GList* orterms = qof_query_get_terms( query ); GList* orterms = qof_query_get_terms( query );
GList* orTerm; GList* orTerm;
GString* sql = g_string_new( "" ); GString* sql = g_string_new( "" );
gboolean need_OR = FALSE; gboolean need_OR = FALSE;
for( orTerm = orterms; orTerm != NULL; orTerm = orTerm->next ) { for ( orTerm = orterms; orTerm != NULL; orTerm = orTerm->next )
{
GList* andterms = (GList*)orTerm->data; GList* andterms = (GList*)orTerm->data;
GList* andTerm; GList* andTerm;
gboolean need_AND = FALSE; gboolean need_AND = FALSE;
gboolean has_tx_guid_check = FALSE; gboolean has_tx_guid_check = FALSE;
if( need_OR ) { if ( need_OR )
{
g_string_append( sql, " OR " ); g_string_append( sql, " OR " );
} }
g_string_append( sql, "(" ); g_string_append( sql, "(" );
for( andTerm = andterms; andTerm != NULL; andTerm = andTerm->next ) { for ( andTerm = andterms; andTerm != NULL; andTerm = andTerm->next )
{
QofQueryTerm* term; QofQueryTerm* term;
GSList* paramPath; GSList* paramPath;
gboolean unknownPath = FALSE; gboolean unknownPath = FALSE;
@ -1022,40 +1134,57 @@ compile_split_query( GncSqlBackend* be, QofQuery* query )
if ( need_AND ) g_string_append( sql, " AND " ); if ( need_AND ) g_string_append( sql, " AND " );
if ( strcmp( paramPath->data, SPLIT_ACCOUNT ) == 0 if ( strcmp( paramPath->data, SPLIT_ACCOUNT ) == 0
&& strcmp( paramPath->next->data, QOF_PARAM_GUID ) == 0 ) { && strcmp( paramPath->next->data, QOF_PARAM_GUID ) == 0 )
{
convert_query_term_to_sql( be, "s.account_guid", term, sql ); convert_query_term_to_sql( be, "s.account_guid", term, sql );
#if SIMPLE_QUERY_COMPILATION #if SIMPLE_QUERY_COMPILATION
goto done_compiling_query; goto done_compiling_query;
#endif #endif
} else if( strcmp( paramPath->data, SPLIT_RECONCILE ) == 0 ) { }
else if ( strcmp( paramPath->data, SPLIT_RECONCILE ) == 0 )
{
convert_query_term_to_sql( be, "s.reconcile_state", term, sql ); convert_query_term_to_sql( be, "s.reconcile_state", term, sql );
} else if( strcmp( paramPath->data, SPLIT_TRANS ) == 0 ) { }
else if ( strcmp( paramPath->data, SPLIT_TRANS ) == 0 )
{
#if 0 #if 0
if( !has_tx_guid_check ) { if ( !has_tx_guid_check )
{
g_string_append( sql, "(splits.tx_guid = transactions.guid) AND " ); g_string_append( sql, "(splits.tx_guid = transactions.guid) AND " );
has_tx_guid_check = TRUE; has_tx_guid_check = TRUE;
} }
#endif #endif
if( strcmp( paramPath->next->data, TRANS_DATE_POSTED ) == 0 ) { if ( strcmp( paramPath->next->data, TRANS_DATE_POSTED ) == 0 )
{
convert_query_term_to_sql( be, "t.post_date", term, sql ); convert_query_term_to_sql( be, "t.post_date", term, sql );
} else if( strcmp( paramPath->next->data, TRANS_DESCRIPTION ) == 0 ) { }
else if ( strcmp( paramPath->next->data, TRANS_DESCRIPTION ) == 0 )
{
convert_query_term_to_sql( be, "t.description", term, sql ); convert_query_term_to_sql( be, "t.description", term, sql );
} else { }
else
{
unknownPath = TRUE; unknownPath = TRUE;
} }
} else if( strcmp( paramPath->data, SPLIT_VALUE ) == 0 ) { }
else if ( strcmp( paramPath->data, SPLIT_VALUE ) == 0 )
{
convert_query_term_to_sql( be, "s.value_num/s.value_denom", term, sql ); convert_query_term_to_sql( be, "s.value_num/s.value_denom", term, sql );
} else { }
else
{
unknownPath = TRUE; unknownPath = TRUE;
} }
if( unknownPath ) { if ( unknownPath )
{
GString* name = g_string_new( (gchar*)paramPath->data ); GString* name = g_string_new( (gchar*)paramPath->data );
while( paramPath->next != NULL ) { while ( paramPath->next != NULL )
{
g_string_append( name, "." ); g_string_append( name, "." );
g_string_append( name, paramPath->next->data ); g_string_append( name, paramPath->next->data );
paramPath = paramPath->next; paramPath = paramPath->next;
@ -1068,10 +1197,13 @@ compile_split_query( GncSqlBackend* be, QofQuery* query )
/* If the last char in the string is a '(', then for some reason, there were /* If the last char in the string is a '(', then for some reason, there were
no terms added to the SQL. If so, remove it and ignore the OR term. */ no terms added to the SQL. If so, remove it and ignore the OR term. */
if( sql->str[sql->len-1] == '(' ) { if ( sql->str[sql->len-1] == '(' )
{
g_string_truncate( sql, sql->len - 1 ); g_string_truncate( sql, sql->len - 1 );
need_OR = FALSE; need_OR = FALSE;
} else { }
else
{
g_string_append( sql, ")" ); g_string_append( sql, ")" );
need_OR = TRUE; need_OR = TRUE;
} }
@ -1080,14 +1212,17 @@ compile_split_query( GncSqlBackend* be, QofQuery* query )
#if SIMPLE_QUERY_COMPILATION #if SIMPLE_QUERY_COMPILATION
done_compiling_query: done_compiling_query:
#endif #endif
if( sql->len != 0 ) { if ( sql->len != 0 )
{
#if SIMPLE_QUERY_COMPILATION #if SIMPLE_QUERY_COMPILATION
g_string_append( sql, ")" ); g_string_append( sql, ")" );
#endif #endif
query_sql = g_strdup_printf( query_sql = g_strdup_printf(
"SELECT DISTINCT t.* FROM %s AS t, %s AS s WHERE s.tx_guid=t.guid AND %s", "SELECT DISTINCT t.* FROM %s AS t, %s AS s WHERE s.tx_guid=t.guid AND %s",
TRANSACTION_TABLE, SPLIT_TABLE, sql->str ); TRANSACTION_TABLE, SPLIT_TABLE, sql->str );
} else { }
else
{
query_sql = g_strdup_printf( "SELECT * FROM %s", TRANSACTION_TABLE ); query_sql = g_strdup_printf( "SELECT * FROM %s", TRANSACTION_TABLE );
} }
query_info->stmt = gnc_sql_create_statement_from_sql( be, query_sql ); query_info->stmt = gnc_sql_create_statement_from_sql( be, query_sql );
@ -1095,7 +1230,9 @@ done_compiling_query:
g_string_free( sql, TRUE ); g_string_free( sql, TRUE );
g_free( query_sql ); g_free( query_sql );
} else { }
else
{
query_sql = g_strdup_printf( "SELECT * FROM %s", TRANSACTION_TABLE ); query_sql = g_strdup_printf( "SELECT * FROM %s", TRANSACTION_TABLE );
query_info->stmt = gnc_sql_create_statement_from_sql( be, query_sql ); query_info->stmt = gnc_sql_create_statement_from_sql( be, query_sql );
g_free( query_sql ); g_free( query_sql );
@ -1112,7 +1249,8 @@ run_split_query( GncSqlBackend* be, gpointer pQuery )
g_return_if_fail( be != NULL ); g_return_if_fail( be != NULL );
g_return_if_fail( pQuery != NULL ); g_return_if_fail( pQuery != NULL );
if( !query_info->has_been_run ) { if ( !query_info->has_been_run )
{
query_transactions( be, query_info->stmt ); query_transactions( be, query_info->stmt );
query_info->has_been_run = TRUE; query_info->has_been_run = TRUE;
gnc_sql_statement_dispose( query_info->stmt ); gnc_sql_statement_dispose( query_info->stmt );
@ -1130,9 +1268,11 @@ free_split_query( GncSqlBackend* be, gpointer pQuery )
} }
/* ----------------------------------------------------------------- */ /* ----------------------------------------------------------------- */
typedef struct { typedef struct
{
/*@ dependent @*/ const GncSqlBackend* be; /*@ dependent @*/ const GncSqlBackend* be;
/*@ dependent @*/ Account* acct; /*@ dependent @*/
Account* acct;
char reconcile_state; char reconcile_state;
gnc_numeric balance; gnc_numeric balance;
} single_acct_balance_t; } single_acct_balance_t;
@ -1216,18 +1356,22 @@ gnc_sql_get_account_balances_slist( GncSqlBackend* be )
g_free( buf ); g_free( buf );
result = gnc_sql_execute_select_statement( be, stmt ); result = gnc_sql_execute_select_statement( be, stmt );
gnc_sql_statement_dispose( stmt ); gnc_sql_statement_dispose( stmt );
if( result != NULL ) { if ( result != NULL )
{
GncSqlRow* row; GncSqlRow* row;
acct_balances_t* bal = NULL; acct_balances_t* bal = NULL;
row = gnc_sql_result_get_first_row( result ); row = gnc_sql_result_get_first_row( result );
while( row != NULL ) { while ( row != NULL )
{
single_acct_balance_t* single_bal; single_acct_balance_t* single_bal;
// Get the next reconcile state balance and merge with other balances // Get the next reconcile state balance and merge with other balances
single_bal = load_single_acct_balances( be, row ); single_bal = load_single_acct_balances( be, row );
if( single_bal != NULL ) { if ( single_bal != NULL )
if( bal != NULL && bal->acct != single_bal->acct ) { {
if ( bal != NULL && bal->acct != single_bal->acct )
{
bal->cleared_balance = gnc_numeric_add( bal->cleared_balance, bal->reconciled_balance, bal->cleared_balance = gnc_numeric_add( bal->cleared_balance, bal->reconciled_balance,
GNC_DENOM_AUTO, GNC_HOW_DENOM_LCD ); GNC_DENOM_AUTO, GNC_HOW_DENOM_LCD );
bal->balance = gnc_numeric_add( bal->balance, bal->cleared_balance, bal->balance = gnc_numeric_add( bal->balance, bal->cleared_balance,
@ -1235,7 +1379,8 @@ gnc_sql_get_account_balances_slist( GncSqlBackend* be )
bal_slist = g_slist_append( bal_slist, bal ); bal_slist = g_slist_append( bal_slist, bal );
bal = NULL; bal = NULL;
} }
if( bal == NULL ) { if ( bal == NULL )
{
bal = g_malloc( (gsize)sizeof(acct_balances_t) ); bal = g_malloc( (gsize)sizeof(acct_balances_t) );
g_assert( bal != NULL ); g_assert( bal != NULL );
@ -1244,13 +1389,18 @@ gnc_sql_get_account_balances_slist( GncSqlBackend* be )
bal->cleared_balance = gnc_numeric_zero(); bal->cleared_balance = gnc_numeric_zero();
bal->reconciled_balance = gnc_numeric_zero(); bal->reconciled_balance = gnc_numeric_zero();
} }
if( single_bal->reconcile_state == 'n' ) { if ( single_bal->reconcile_state == 'n' )
{
bal->balance = gnc_numeric_add( bal->balance, single_bal->balance, bal->balance = gnc_numeric_add( bal->balance, single_bal->balance,
GNC_DENOM_AUTO, GNC_HOW_DENOM_LCD ); GNC_DENOM_AUTO, GNC_HOW_DENOM_LCD );
} else if( single_bal->reconcile_state == 'c' ) { }
else if ( single_bal->reconcile_state == 'c' )
{
bal->cleared_balance = gnc_numeric_add( bal->cleared_balance, single_bal->balance, bal->cleared_balance = gnc_numeric_add( bal->cleared_balance, single_bal->balance,
GNC_DENOM_AUTO, GNC_HOW_DENOM_LCD ); GNC_DENOM_AUTO, GNC_HOW_DENOM_LCD );
} else if( single_bal->reconcile_state == 'y' ) { }
else if ( single_bal->reconcile_state == 'y' )
{
bal->reconciled_balance = gnc_numeric_add( bal->reconciled_balance, single_bal->balance, bal->reconciled_balance = gnc_numeric_add( bal->reconciled_balance, single_bal->balance,
GNC_DENOM_AUTO, GNC_HOW_DENOM_LCD ); GNC_DENOM_AUTO, GNC_HOW_DENOM_LCD );
} }
@ -1260,7 +1410,8 @@ gnc_sql_get_account_balances_slist( GncSqlBackend* be )
} }
// Add the final balance // Add the final balance
if( bal != NULL ) { if ( bal != NULL )
{
bal->cleared_balance = gnc_numeric_add( bal->cleared_balance, bal->reconciled_balance, bal->cleared_balance = gnc_numeric_add( bal->cleared_balance, bal->reconciled_balance,
GNC_DENOM_AUTO, GNC_HOW_DENOM_LCD ); GNC_DENOM_AUTO, GNC_HOW_DENOM_LCD );
bal->balance = gnc_numeric_add( bal->balance, bal->cleared_balance, bal->balance = gnc_numeric_add( bal->balance, bal->cleared_balance,
@ -1295,12 +1446,14 @@ load_tx_guid( const GncSqlBackend* be, GncSqlRow* row,
val = gnc_sql_row_get_value_at_col_name( row, table_row->col_name ); val = gnc_sql_row_get_value_at_col_name( row, table_row->col_name );
g_assert( val != NULL ); g_assert( val != NULL );
guid_str = g_value_get_string(val); guid_str = g_value_get_string(val);
if( guid_str != NULL ) { if ( guid_str != NULL )
{
(void)string_to_guid( guid_str, &guid ); (void)string_to_guid( guid_str, &guid );
tx = xaccTransLookup( &guid, be->primary_book ); tx = xaccTransLookup( &guid, be->primary_book );
// If the transaction is not found, try loading it // If the transaction is not found, try loading it
if( tx == NULL ) { if ( tx == NULL )
{
gchar* buf; gchar* buf;
GncSqlStatement* stmt; GncSqlStatement* stmt;
@ -1312,10 +1465,14 @@ load_tx_guid( const GncSqlBackend* be, GncSqlRow* row,
tx = xaccTransLookup( &guid, be->primary_book ); tx = xaccTransLookup( &guid, be->primary_book );
} }
if( tx != NULL ) { if ( tx != NULL )
if( table_row->gobj_param_name != NULL ) { {
if ( table_row->gobj_param_name != NULL )
{
g_object_set( pObject, table_row->gobj_param_name, tx, NULL ); g_object_set( pObject, table_row->gobj_param_name, tx, NULL );
} else { }
else
{
g_return_if_fail( setter != NULL ); g_return_if_fail( setter != NULL );
(*setter)( pObject, (const gpointer)tx ); (*setter)( pObject, (const gpointer)tx );
} }
@ -1327,7 +1484,8 @@ static GncSqlColumnTypeHandler tx_guid_handler
= { load_tx_guid, = { load_tx_guid,
gnc_sql_add_objectref_guid_col_info_to_list, gnc_sql_add_objectref_guid_col_info_to_list,
gnc_sql_add_colname_to_list, gnc_sql_add_colname_to_list,
gnc_sql_add_gvalue_objectref_guid_to_slist }; gnc_sql_add_gvalue_objectref_guid_to_slist
};
/* ================================================================= */ /* ================================================================= */
void void
gnc_sql_init_transaction_handler( void ) gnc_sql_init_transaction_handler( void )

View File

@ -95,7 +95,8 @@ static GncSqlColumnTableEntry billterm_parent_col_table[] =
{ NULL } { NULL }
}; };
typedef struct { typedef struct
{
/*@ dependent @*/ GncBillTerm* billterm; /*@ dependent @*/ GncBillTerm* billterm;
GncGUID guid; GncGUID guid;
gboolean have_guid; gboolean have_guid;
@ -126,9 +127,12 @@ bt_get_parent( gpointer pObject )
billterm = GNC_BILLTERM(pObject); billterm = GNC_BILLTERM(pObject);
pParent = gncBillTermGetParent( billterm ); pParent = gncBillTermGetParent( billterm );
if( pParent == NULL ) { if ( pParent == NULL )
{
parent_guid = NULL; parent_guid = NULL;
} else { }
else
{
parent_guid = qof_instance_get_guid( QOF_INSTANCE(pParent) ); parent_guid = qof_instance_get_guid( QOF_INSTANCE(pParent) );
} }
@ -151,7 +155,8 @@ bt_set_parent( gpointer data, gpointer value )
if ( guid != NULL ) if ( guid != NULL )
{ {
parent = gncBillTermLookup( pBook, guid ); parent = gncBillTermLookup( pBook, guid );
if( parent != NULL ) { if ( parent != NULL )
{
gncBillTermSetParent( billterm, parent ); gncBillTermSetParent( billterm, parent );
gncBillTermSetChild( parent, billterm ); gncBillTermSetChild( parent, billterm );
} }
@ -257,15 +262,18 @@ load_all_billterms( GncSqlBackend* be )
items are removed from the front and added to the back if the items are removed from the front and added to the back if the
parent is still not available, then eventually, the list will parent is still not available, then eventually, the list will
shrink to size 0. */ shrink to size 0. */
if( l_billterms_needing_parents != NULL ) { if ( l_billterms_needing_parents != NULL )
{
gboolean progress_made = TRUE; gboolean progress_made = TRUE;
GncTaxTable* root; GncTaxTable* root;
Account* pParent; Account* pParent;
GList* elem; GList* elem;
while( progress_made ) { while ( progress_made )
{
progress_made = FALSE; progress_made = FALSE;
for( elem = l_billterms_needing_parents; elem != NULL; elem = g_list_next( elem ) ) { for ( elem = l_billterms_needing_parents; elem != NULL; elem = g_list_next( elem ) )
{
billterm_parent_guid_struct* s = (billterm_parent_guid_struct*)elem->data; billterm_parent_guid_struct* s = (billterm_parent_guid_struct*)elem->data;
bt_set_parent( s->billterm, &s->guid ); bt_set_parent( s->billterm, &s->guid );
l_billterms_needing_parents = g_list_delete_link( l_billterms_needing_parents, elem ); l_billterms_needing_parents = g_list_delete_link( l_billterms_needing_parents, elem );

View File

@ -121,7 +121,8 @@ static GncSqlColumnTableEntry guid_col_table[] =
{ NULL } { NULL }
}; };
typedef struct { typedef struct
{
/*@ dependent @*/ GncTaxTable* tt; /*@ dependent @*/ GncTaxTable* tt;
GncGUID guid; GncGUID guid;
gboolean have_guid; gboolean have_guid;
@ -180,9 +181,12 @@ bt_get_parent( gpointer pObject )
tt = GNC_TAXTABLE(pObject); tt = GNC_TAXTABLE(pObject);
pParent = gncTaxTableGetParent( tt ); pParent = gncTaxTableGetParent( tt );
if( pParent == NULL ) { if ( pParent == NULL )
{
parent_guid = NULL; parent_guid = NULL;
} else { }
else
{
parent_guid = qof_instance_get_guid( QOF_INSTANCE(pParent) ); parent_guid = qof_instance_get_guid( QOF_INSTANCE(pParent) );
} }
@ -205,7 +209,8 @@ tt_set_parent( gpointer data, gpointer value )
if ( guid != NULL ) if ( guid != NULL )
{ {
parent = gncTaxTableLookup( pBook, guid ); parent = gncTaxTableLookup( pBook, guid );
if( parent != NULL ) { if ( parent != NULL )
{
gncTaxTableSetParent( tt, parent ); gncTaxTableSetParent( tt, parent );
gncTaxTableSetChild( parent, tt ); gncTaxTableSetChild( parent, tt );
} }
@ -297,16 +302,20 @@ load_single_taxtable( GncSqlBackend* be, GncSqlRow* row,
/* If the tax table doesn't have a parent, it might be because it hasn't been loaded yet. /* If the tax table doesn't have a parent, it might be because it hasn't been loaded yet.
If so, add this tax table to the list of tax tables with no parent, along with the parent If so, add this tax table to the list of tax tables with no parent, along with the parent
GncGUID so that after they are all loaded, the parents can be fixed up. */ GncGUID so that after they are all loaded, the parents can be fixed up. */
if( gncTaxTableGetParent( tt ) == NULL ) { if ( gncTaxTableGetParent( tt ) == NULL )
{
taxtable_parent_guid_struct* s = g_malloc( (gsize)sizeof(taxtable_parent_guid_struct) ); taxtable_parent_guid_struct* s = g_malloc( (gsize)sizeof(taxtable_parent_guid_struct) );
g_assert( s != NULL ); g_assert( s != NULL );
s->tt = tt; s->tt = tt;
s->have_guid = FALSE; s->have_guid = FALSE;
gnc_sql_load_object( be, row, GNC_ID_TAXTABLE, s, tt_parent_col_table ); gnc_sql_load_object( be, row, GNC_ID_TAXTABLE, s, tt_parent_col_table );
if( s->have_guid ) { if ( s->have_guid )
{
*l_tt_needing_parents = g_list_prepend( *l_tt_needing_parents, s ); *l_tt_needing_parents = g_list_prepend( *l_tt_needing_parents, s );
} else { }
else
{
g_free( s ); g_free( s );
} }
} }
@ -344,15 +353,18 @@ load_all_taxtables( GncSqlBackend* be )
items are removed from the front and added to the back if the items are removed from the front and added to the back if the
parent is still not available, then eventually, the list will parent is still not available, then eventually, the list will
shrink to size 0. */ shrink to size 0. */
if( tt_needing_parents != NULL ) { if ( tt_needing_parents != NULL )
{
gboolean progress_made = TRUE; gboolean progress_made = TRUE;
GncTaxTable* root; GncTaxTable* root;
Account* pParent; Account* pParent;
GList* elem; GList* elem;
while( progress_made ) { while ( progress_made )
{
progress_made = FALSE; progress_made = FALSE;
for( elem = tt_needing_parents; elem != NULL; elem = g_list_next( elem ) ) { for ( elem = tt_needing_parents; elem != NULL; elem = g_list_next( elem ) )
{
taxtable_parent_guid_struct* s = (taxtable_parent_guid_struct*)elem->data; taxtable_parent_guid_struct* s = (taxtable_parent_guid_struct*)elem->data;
tt_set_parent( s->tt, &s->guid ); tt_set_parent( s->tt, &s->guid );
tt_needing_parents = g_list_delete_link( tt_needing_parents, elem ); tt_needing_parents = g_list_delete_link( tt_needing_parents, elem );