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

@ -152,10 +152,10 @@ typedef struct
provider_functions_t* provider; provider_functions_t* provider;
gint last_error; // Code of the last error that occurred. This is set in the error callback function gint last_error; // Code of the last error that occurred. This is set in the error callback function
gint error_repeat; // Used in case of transient errors. After such error, another attempt at the gint error_repeat; // Used in case of transient errors. After such error, another attempt at the
// original call is allowed. error_repeat tracks the number of attempts and can // original call is allowed. error_repeat tracks the number of attempts and can
// be used to prevent infinite loops. // be used to prevent infinite loops.
gboolean retry; // Signals the calling function that it should retry (the error handler detected gboolean retry; // Signals the calling function that it should retry (the error handler detected
// transient error and managed to resolve it, but it can't run the original query) // transient error and managed to resolve it, but it can't run the original query)
} GncDbiSqlConnection; } GncDbiSqlConnection;
#define DBI_MAX_CONN_ATTEMPTS 5 #define DBI_MAX_CONN_ATTEMPTS 5
@ -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

@ -54,7 +54,7 @@ create_session(void)
struct timeval tv; struct timeval tv;
gnc_commodity_table* table; gnc_commodity_table* table;
gnc_commodity* currency; gnc_commodity* currency;
table = gnc_commodity_table_get_table( book ); table = gnc_commodity_table_get_table( book );
currency = gnc_commodity_table_lookup( table, GNC_COMMODITY_NS_CURRENCY, "CAD" ); currency = gnc_commodity_table_lookup( table, GNC_COMMODITY_NS_CURRENCY, "CAD" );
@ -110,7 +110,7 @@ int main (int argc, char ** argv)
filename = tempnam( "/tmp", "test-sqlite3-" ); filename = tempnam( "/tmp", "test-sqlite3-" );
printf( "Using filename: %s\n", filename ); printf( "Using filename: %s\n", filename );
test_dbi_store_and_reload( "sqlite3", session_1, filename ); test_dbi_store_and_reload( "sqlite3", session_1, filename );
#if 0 #if 0
printf( "TEST_MYSQL_URL='%s'\n", TEST_MYSQL_URL ); printf( "TEST_MYSQL_URL='%s'\n", TEST_MYSQL_URL );
if ( strlen( TEST_MYSQL_URL ) > 0 ) if ( strlen( TEST_MYSQL_URL ) > 0 )
{ {
@ -123,7 +123,7 @@ int main (int argc, char ** argv)
session_1 = create_session(); session_1 = create_session();
test_dbi_store_and_reload( "pgsql", session_1, TEST_PGSQL_URL ); test_dbi_store_and_reload( "pgsql", session_1, TEST_PGSQL_URL );
} }
#endif #endif
print_test_results(); print_test_results();
qof_close(); qof_close();
exit(get_rv()); exit(get_rv());

View File

@ -35,90 +35,93 @@
#include "gnc-engine.h" #include "gnc-engine.h"
#include "escape.h" #include "escape.h"
static QofLogModule log_module = GNC_MOD_BACKEND; static QofLogModule log_module = GNC_MOD_BACKEND;
/* ================================================ */ /* ================================================ */
struct _escape { struct _escape
/* pointer to memory used for escaping arguments */ {
char * escape; /* pointer to memory used for escaping arguments */
size_t esc_buflen; char * escape;
size_t esc_buflen;
}; };
/* ================================================ */ /* ================================================ */
/* escape single-quote marks and backslashes so that the /* escape single-quote marks and backslashes so that the
* database SQL parser doesn't puke on the query string * database SQL parser doesn't puke on the query string
*/ */
const char * const char *
sqlEscapeString (sqlEscape *b, const char *str) sqlEscapeString (sqlEscape *b, const char *str)
{ {
const char *p, *src_head; const char *p, *src_head;
char *dst_tail; char *dst_tail;
size_t len, slen; size_t len, slen;
ENTER("str = %s", str); ENTER("str = %s", str);
if (!b || !str) { LEAVE("(null) args"); return NULL; }
/* if a string is escaped twice, just return the first */ if (!b || !str) { LEAVE("(null) args"); return NULL; }
if (b->escape == str) {
LEAVE("%s: already escaped", str);
return str;
}
/* if nothing to escape, just return */ /* if a string is escaped twice, just return the first */
len = strlen (str); if (b->escape == str)
slen = strcspn (str, "\\\'"); {
if (len == slen) { LEAVE("%s: already escaped", str);
LEAVE("nothing to escape"); return str;
return str; }
}
/* count to see how much space we'll need */ /* if nothing to escape, just return */
p = str + slen + 1; len = strlen (str);
while (*p) slen = strcspn (str, "\\\'");
{ if (len == slen)
len ++; {
p += 1 + strcspn (p, "\\\'"); LEAVE("nothing to escape");
} return str;
}
/* get more space, if needed */ /* count to see how much space we'll need */
if (len >= b->esc_buflen) p = str + slen + 1;
{ while (*p)
b->escape = g_realloc(b->escape, len+100); {
b->esc_buflen = len+100; len ++;
} p += 1 + strcspn (p, "\\\'");
}
/* copy and escape */ /* get more space, if needed */
src_head = (char *) str; if (len >= b->esc_buflen)
dst_tail = b->escape; {
p = src_head + strcspn (src_head, "\\\'"); b->escape = g_realloc(b->escape, len + 100);
while (*p) b->esc_buflen = len + 100;
{ }
size_t cp_len = p - src_head;
strncpy (dst_tail, src_head, cp_len); /* copy and escape */
dst_tail += cp_len; src_head = (char *) str;
*dst_tail = '\\'; dst_tail = b->escape;
dst_tail ++; p = src_head + strcspn (src_head, "\\\'");
*dst_tail = *p; while (*p)
dst_tail ++; {
size_t cp_len = p - src_head;
src_head = p+1; strncpy (dst_tail, src_head, cp_len);
p = src_head + strcspn (src_head, "\\\'"); dst_tail += cp_len;
} *dst_tail = '\\';
if (p != src_head) dst_tail ++;
{ *dst_tail = *p;
size_t cp_len = p - src_head; dst_tail ++;
strncpy (dst_tail, src_head, cp_len); src_head = p + 1;
dst_tail += cp_len; p = src_head + strcspn (src_head, "\\\'");
} }
*dst_tail = 0; if (p != src_head)
{
size_t cp_len = p - src_head;
LEAVE("b->escape = %s", b->escape); strncpy (dst_tail, src_head, cp_len);
return b->escape; dst_tail += cp_len;
}
*dst_tail = 0;
LEAVE("b->escape = %s", b->escape);
return b->escape;
} }
/* ================================================ */ /* ================================================ */
@ -128,11 +131,11 @@ sqlEscapeString (sqlEscape *b, const char *str)
sqlEscape * sqlEscape *
sqlEscape_new (void) sqlEscape_new (void)
{ {
sqlEscape *b = g_new (sqlEscape, 1); sqlEscape *b = g_new (sqlEscape, 1);
b->escape = g_malloc (INITIAL_BUFSZ); b->escape = g_malloc (INITIAL_BUFSZ);
b->esc_buflen = INITIAL_BUFSZ; b->esc_buflen = INITIAL_BUFSZ;
return (b); return (b);
} }
/* ================================================ */ /* ================================================ */
@ -141,10 +144,11 @@ void
sqlEscape_destroy (sqlEscape *b) 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);
g_free (b); b->escape = NULL;
LEAVE(" "); g_free (b);
LEAVE(" ");
} }
/* ================ END OF FILE ==================== */ /* ================ END OF FILE ==================== */

View File

@ -19,7 +19,7 @@
* Boston, MA 02110-1301, USA gnu@gnu.org * * Boston, MA 02110-1301, USA gnu@gnu.org *
\********************************************************************/ \********************************************************************/
/** @file gnc-account-sql.c /** @file gnc-account-sql.c
* @brief load and save data to SQL * @brief load and save data to SQL
* @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff@rogers.com> * @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff@rogers.com>
* *
* This file implements the top-level QofBackend API for saving/ * This file implements the top-level QofBackend API for saving/
@ -62,33 +62,36 @@ static void set_parent_guid( gpointer pObject, /*@ null @*/ gpointer pValue );
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" },
{ "name", CT_STRING, ACCOUNT_MAX_NAME_LEN, COL_NNUL, "name" }, { "name", CT_STRING, ACCOUNT_MAX_NAME_LEN, COL_NNUL, "name" },
{ "account_type", CT_STRING, ACCOUNT_MAX_TYPE_LEN, COL_NNUL, NULL, ACCOUNT_TYPE_ }, { "account_type", CT_STRING, ACCOUNT_MAX_TYPE_LEN, COL_NNUL, NULL, ACCOUNT_TYPE_ },
{ "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" },
{ "placeholder", CT_BOOLEAN, 0, 0, "placeholder" }, { "placeholder", CT_BOOLEAN, 0, 0, "placeholder" },
{ NULL } { NULL }
/*@ +full_init_block @*/ /*@ +full_init_block @*/
}; };
static GncSqlColumnTableEntry parent_col_table[] = static GncSqlColumnTableEntry parent_col_table[] =
{ {
/*@ -full_init_block @*/ /*@ -full_init_block @*/
{ "parent_guid", CT_GUID, 0, 0, NULL, NULL, NULL, set_parent_guid }, { "parent_guid", CT_GUID, 0, 0, NULL, NULL, NULL, set_parent_guid },
{ NULL } { NULL }
/*@ +full_init_block @*/ /*@ +full_init_block @*/
}; };
typedef struct { typedef struct
/*@ dependent @*/ Account* pAccount; {
GncGUID guid; /*@ dependent @*/ Account* pAccount;
GncGUID guid;
} account_parent_guid_struct; } account_parent_guid_struct;
/* ================================================================= */ /* ================================================================= */
@ -100,36 +103,41 @@ get_parent( gpointer pObject )
const Account* pParent; const Account* pParent;
const GncGUID* parent_guid; const GncGUID* parent_guid;
g_return_val_if_fail( pObject != NULL, NULL ); g_return_val_if_fail( pObject != NULL, NULL );
g_return_val_if_fail( GNC_IS_ACCOUNT(pObject), NULL ); g_return_val_if_fail( GNC_IS_ACCOUNT(pObject), NULL );
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) );
} }
return (gpointer)parent_guid; return (gpointer)parent_guid;
} }
static void static void
set_parent( gpointer pObject, /*@ null @*/ gpointer pValue ) set_parent( gpointer pObject, /*@ null @*/ gpointer pValue )
{ {
Account* pAccount; Account* pAccount;
QofBook* pBook; QofBook* pBook;
GncGUID* guid = (GncGUID*)pValue; GncGUID* guid = (GncGUID*)pValue;
Account* pParent; Account* pParent;
g_return_if_fail( pObject != NULL ); g_return_if_fail( pObject != NULL );
g_return_if_fail( GNC_IS_ACCOUNT(pObject) ); g_return_if_fail( GNC_IS_ACCOUNT(pObject) );
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 );
} }
} }
@ -138,50 +146,53 @@ set_parent( gpointer pObject, /*@ null @*/ gpointer pValue )
static void static void
set_parent_guid( gpointer pObject, /*@ null @*/ gpointer pValue ) set_parent_guid( gpointer pObject, /*@ null @*/ gpointer pValue )
{ {
account_parent_guid_struct* s = (account_parent_guid_struct*)pObject; account_parent_guid_struct* s = (account_parent_guid_struct*)pObject;
GncGUID* guid = (GncGUID*)pValue; GncGUID* guid = (GncGUID*)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 );
s->guid = *guid; s->guid = *guid;
} }
static /*@ dependent @*//*@ null @*/ Account* static /*@ dependent @*//*@ null @*/ Account*
load_single_account( GncSqlBackend* be, GncSqlRow* row, load_single_account( GncSqlBackend* be, GncSqlRow* row,
GList** l_accounts_needing_parents ) GList** l_accounts_needing_parents )
{ {
const GncGUID* guid; const GncGUID* guid;
Account* pAccount = NULL; Account* pAccount = NULL;
g_return_val_if_fail( be != NULL, NULL ); g_return_val_if_fail( be != NULL, NULL );
g_return_val_if_fail( row != NULL, NULL ); g_return_val_if_fail( row != NULL, NULL );
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 );
gnc_sql_load_object( be, row, GNC_ID_ACCOUNT, pAccount, col_table ); gnc_sql_load_object( be, row, GNC_ID_ACCOUNT, pAccount, col_table );
xaccAccountCommitEdit( pAccount ); xaccAccountCommitEdit( pAccount );
/* 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) ); {
g_assert( s != NULL ); account_parent_guid_struct* s = g_malloc( (gsize)sizeof(account_parent_guid_struct) );
g_assert( s != NULL );
s->pAccount = pAccount; s->pAccount = pAccount;
gnc_sql_load_object( be, row, GNC_ID_ACCOUNT, s, parent_col_table ); gnc_sql_load_object( be, row, GNC_ID_ACCOUNT, s, parent_col_table );
*l_accounts_needing_parents = g_list_prepend( *l_accounts_needing_parents, s ); *l_accounts_needing_parents = g_list_prepend( *l_accounts_needing_parents, s );
} }
return pAccount; return pAccount;
} }
static void static void
@ -191,103 +202,115 @@ load_all_accounts( GncSqlBackend* be )
GncSqlResult* result; GncSqlResult* result;
QofBook* pBook; QofBook* pBook;
gnc_commodity_table* pTable; gnc_commodity_table* pTable;
GList* l_accounts_needing_parents = NULL; GList* l_accounts_needing_parents = NULL;
GSList* bal_slist; GSList* bal_slist;
GSList* bal; GSList* bal;
g_return_if_fail( be != NULL ); g_return_if_fail( be != NULL );
ENTER( "" ); ENTER( "" );
pBook = be->primary_book; pBook = be->primary_book;
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" ); {
return; LEAVE( "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 = gnc_sql_result_get_first_row( result ); {
Account* acc; GncSqlRow* row = gnc_sql_result_get_first_row( result );
gchar* sql; Account* acc;
gchar* sql;
while( row != NULL ) { while ( row != NULL )
acc = load_single_account( be, row, &l_accounts_needing_parents ); {
row = gnc_sql_result_get_next_row( result ); acc = load_single_account( be, row, &l_accounts_needing_parents );
} row = gnc_sql_result_get_next_row( result );
gnc_sql_result_dispose( result ); }
gnc_sql_result_dispose( result );
sql = g_strdup_printf( "SELECT DISTINCT guid FROM %s", TABLE_NAME ); sql = g_strdup_printf( "SELECT DISTINCT guid FROM %s", TABLE_NAME );
gnc_sql_slots_load_for_sql_subquery( be, sql, (BookLookupFn)xaccAccountLookup ); gnc_sql_slots_load_for_sql_subquery( be, sql, (BookLookupFn)xaccAccountLookup );
g_free( sql ); g_free( sql );
/* While there are items on the list of accounts needing parents, /* While there are items on the list of accounts needing parents,
try to see if the parent has now been loaded. Theory says that if try to see if the parent has now been loaded. Theory says that if
items are removed from the front and added to the back if the 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; {
Account* root; gboolean progress_made = TRUE;
Account* pParent; Account* root;
GList* elem; Account* pParent;
GList* elem;
while( progress_made ) {
progress_made = FALSE;
for( elem = l_accounts_needing_parents; elem != NULL; elem = g_list_next( elem ) ) {
account_parent_guid_struct* s = (account_parent_guid_struct*)elem->data;
pParent = xaccAccountLookup( &s->guid, be->primary_book );
if( pParent != NULL ) {
gnc_account_append_child( pParent, s->pAccount );
l_accounts_needing_parents = g_list_delete_link( l_accounts_needing_parents, elem );
progress_made = TRUE;
}
}
}
/* Any non-ROOT accounts left over must be parented by the root account */
root = gnc_book_get_root_account( pBook );
for( elem = l_accounts_needing_parents; elem != NULL; elem = g_list_next( elem ) ) {
account_parent_guid_struct* s = (account_parent_guid_struct*)elem->data;
if( xaccAccountGetType( s->pAccount ) != ACCT_TYPE_ROOT ) {
gnc_account_append_child( root, s->pAccount );
}
}
}
/* Load starting balances */ while ( progress_made )
bal_slist = gnc_sql_get_account_balances_slist( be ); {
for( bal = bal_slist; bal != NULL; bal = bal->next ) { progress_made = FALSE;
acct_balances_t* balances = (acct_balances_t*)bal->data; for ( elem = l_accounts_needing_parents; elem != NULL; elem = g_list_next( elem ) )
{
account_parent_guid_struct* s = (account_parent_guid_struct*)elem->data;
pParent = xaccAccountLookup( &s->guid, be->primary_book );
if ( pParent != NULL )
{
gnc_account_append_child( pParent, s->pAccount );
l_accounts_needing_parents = g_list_delete_link( l_accounts_needing_parents, elem );
progress_made = TRUE;
}
}
}
g_object_set( balances->acct, /* Any non-ROOT accounts left over must be parented by the root account */
"start-balance", &balances->balance, root = gnc_book_get_root_account( pBook );
"start-cleared-balance", &balances->cleared_balance, for ( elem = l_accounts_needing_parents; elem != NULL; elem = g_list_next( elem ) )
"start-reconciled-balance", &balances->reconciled_balance, {
NULL); account_parent_guid_struct* s = (account_parent_guid_struct*)elem->data;
if ( xaccAccountGetType( s->pAccount ) != ACCT_TYPE_ROOT )
{
gnc_account_append_child( root, s->pAccount );
}
}
}
} /* Load starting balances */
if( bal_slist != NULL ) { bal_slist = gnc_sql_get_account_balances_slist( be );
g_slist_free( bal_slist ); for ( bal = bal_slist; bal != NULL; bal = bal->next )
} {
} acct_balances_t* balances = (acct_balances_t*)bal->data;
LEAVE( "" ); g_object_set( balances->acct,
"start-balance", &balances->balance,
"start-cleared-balance", &balances->cleared_balance,
"start-reconciled-balance", &balances->reconciled_balance,
NULL);
}
if ( bal_slist != NULL )
{
g_slist_free( bal_slist );
}
}
LEAVE( "" );
} }
/* ================================================================= */ /* ================================================================= */
static void static void
create_account_tables( GncSqlBackend* be ) create_account_tables( GncSqlBackend* be )
{ {
gint version; gint version;
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 );
} }
} }
@ -298,95 +321,114 @@ gnc_sql_save_account( GncSqlBackend* be, QofInstance* inst )
{ {
Account* pAcc = GNC_ACCOUNT(inst); Account* pAcc = GNC_ACCOUNT(inst);
const GncGUID* guid; const GncGUID* guid;
gboolean is_infant; gboolean is_infant;
gboolean is_ok = FALSE; gboolean is_ok = FALSE;
gnc_commodity* commodity; gnc_commodity* commodity;
gint op; gint op;
g_return_val_if_fail( be != NULL, FALSE ); g_return_val_if_fail( be != NULL, FALSE );
g_return_val_if_fail( inst != NULL, FALSE ); g_return_val_if_fail( inst != NULL, FALSE );
g_return_val_if_fail( GNC_IS_ACCOUNT(inst), FALSE ); g_return_val_if_fail( GNC_IS_ACCOUNT(inst), FALSE );
ENTER( "inst=%p", inst ); ENTER( "inst=%p", inst );
is_infant = qof_instance_get_infant( inst ); is_infant = qof_instance_get_infant( inst );
// If there is no commodity yet, this might be because a new account name // If there is no commodity yet, this might be because a new account name
// has been entered directly into the register and an account window will // has been entered directly into the register and an account window will
// be opened. The account info is not complete yet, but the name has been // be opened. The account info is not complete yet, but the name has been
// set, triggering this commit // set, triggering this commit
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; {
} else if( be->is_pristine_db || is_infant ) { op = OP_DB_DELETE;
op = OP_DB_INSERT; }
} else { else if ( be->is_pristine_db || is_infant )
op = OP_DB_UPDATE; {
} op = OP_DB_INSERT;
}
else
{
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 {
guid = qof_instance_get_guid( inst ); // Now, commit or delete any slots
if( !qof_instance_get_destroying(inst) ) { guid = qof_instance_get_guid( inst );
is_ok = gnc_sql_slots_save( be, guid, is_infant, qof_instance_get_slots( inst ) ); if ( !qof_instance_get_destroying(inst) )
} else { {
is_ok = gnc_sql_slots_delete( be, guid ); is_ok = gnc_sql_slots_save( be, guid, is_infant, qof_instance_get_slots( inst ) );
} }
} else
{
is_ok = gnc_sql_slots_delete( be, guid );
}
}
LEAVE( "is_ok=%d", is_ok ); LEAVE( "is_ok=%d", is_ok );
return is_ok; return is_ok;
} }
/* ================================================================= */ /* ================================================================= */
static void static void
load_account_guid( const GncSqlBackend* be, GncSqlRow* row, load_account_guid( const GncSqlBackend* be, GncSqlRow* row,
/*@ null @*/ QofSetterFunc setter, gpointer pObject, /*@ null @*/ QofSetterFunc setter, gpointer pObject,
const GncSqlColumnTableEntry* table_row ) const GncSqlColumnTableEntry* table_row )
{ {
const GValue* val; const GValue* val;
GncGUID guid; GncGUID guid;
Account* account = NULL; Account* account = NULL;
g_return_if_fail( be != NULL ); g_return_if_fail( be != NULL );
g_return_if_fail( row != NULL ); g_return_if_fail( row != NULL );
g_return_if_fail( pObject != NULL ); g_return_if_fail( pObject != NULL );
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 ) { {
g_object_set( pObject, table_row->gobj_param_name, account, NULL ); if ( table_row->gobj_param_name != NULL )
} else { {
g_return_if_fail( setter != NULL ); g_object_set( pObject, table_row->gobj_param_name, account, NULL );
(*setter)( pObject, (const gpointer)account ); }
} else
} else { {
PWARN( "Account ref '%s' not found", g_value_get_string( val ) ); g_return_if_fail( setter != NULL );
} (*setter)( pObject, (const gpointer)account );
} }
}
else
{
PWARN( "Account ref '%s' not found", g_value_get_string( val ) );
}
}
} }
static GncSqlColumnTypeHandler account_guid_handler 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 )
@ -398,14 +440,14 @@ gnc_sql_init_account_handler( void )
gnc_sql_save_account, /* commit */ gnc_sql_save_account, /* commit */
load_all_accounts, /* initial_load */ load_all_accounts, /* initial_load */
create_account_tables, /* create_tables */ create_account_tables, /* create_tables */
NULL, /* compile_query */ NULL, /* compile_query */
NULL, /* run_query */ NULL, /* run_query */
NULL, /* free_query */ NULL, /* free_query */
NULL /* write */ NULL /* write */
}; };
(void)qof_object_register_backend( GNC_ID_ACCOUNT, GNC_SQL_BACKEND, &be_data ); (void)qof_object_register_backend( GNC_ID_ACCOUNT, GNC_SQL_BACKEND, &be_data );
gnc_sql_register_col_type_handler( CT_ACCOUNTREF, &account_guid_handler ); gnc_sql_register_col_type_handler( CT_ACCOUNTREF, &account_guid_handler );
} }
/* ========================== END OF FILE ===================== */ /* ========================== END OF FILE ===================== */

File diff suppressed because it is too large Load Diff

View File

@ -19,7 +19,7 @@
* Boston, MA 02110-1301, USA gnu@gnu.org * * Boston, MA 02110-1301, USA gnu@gnu.org *
\********************************************************************/ \********************************************************************/
/** @file gnc-book-sql.c /** @file gnc-book-sql.c
* @brief load and save data to SQL * @brief load and save data to SQL
* @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff@rogers.com> * @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff@rogers.com>
* *
* This file implements the top-level QofBackend API for saving/ * This file implements the top-level QofBackend API for saving/
@ -57,14 +57,18 @@ static void set_root_template_guid( gpointer pObject, /*@ null @*/ gpointer pVal
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" },
{ "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 @*/
}; };
/* ================================================================= */ /* ================================================================= */
@ -74,23 +78,23 @@ get_root_account_guid( gpointer pObject )
QofBook* book = QOF_BOOK(pObject); QofBook* book = QOF_BOOK(pObject);
const Account* root; const Account* root;
g_return_val_if_fail( pObject != NULL, NULL ); g_return_val_if_fail( pObject != NULL, NULL );
g_return_val_if_fail( QOF_IS_BOOK(pObject), NULL ); g_return_val_if_fail( QOF_IS_BOOK(pObject), NULL );
root = gnc_book_get_root_account( book ); root = gnc_book_get_root_account( book );
return (gpointer)qof_instance_get_guid( QOF_INSTANCE(root) ); return (gpointer)qof_instance_get_guid( QOF_INSTANCE(root) );
} }
static void static void
set_root_account_guid( gpointer pObject, /*@ null @*/ gpointer pValue ) set_root_account_guid( gpointer pObject, /*@ null @*/ gpointer pValue )
{ {
QofBook* book = QOF_BOOK(pObject); QofBook* book = QOF_BOOK(pObject);
const Account* root; const Account* root;
GncGUID* guid = (GncGUID*)pValue; GncGUID* guid = (GncGUID*)pValue;
g_return_if_fail( pObject != NULL ); g_return_if_fail( pObject != NULL );
g_return_if_fail( QOF_IS_BOOK(pObject) ); g_return_if_fail( QOF_IS_BOOK(pObject) );
g_return_if_fail( pValue != NULL ); g_return_if_fail( pValue != NULL );
root = gnc_book_get_root_account( book ); root = gnc_book_get_root_account( book );
qof_instance_set_guid( QOF_INSTANCE(root), guid ); qof_instance_set_guid( QOF_INSTANCE(root), guid );
@ -102,26 +106,27 @@ get_root_template_guid( gpointer pObject )
const QofBook* book = QOF_BOOK(pObject); const QofBook* book = QOF_BOOK(pObject);
const Account* root; const Account* root;
g_return_val_if_fail( pObject != NULL, NULL ); g_return_val_if_fail( pObject != NULL, NULL );
g_return_val_if_fail( QOF_IS_BOOK(pObject), NULL ); g_return_val_if_fail( QOF_IS_BOOK(pObject), NULL );
root = gnc_book_get_template_root( book ); root = gnc_book_get_template_root( book );
return (gpointer)qof_instance_get_guid( QOF_INSTANCE(root) ); return (gpointer)qof_instance_get_guid( QOF_INSTANCE(root) );
} }
static void static void
set_root_template_guid( gpointer pObject, /*@ null @*/ gpointer pValue ) set_root_template_guid( gpointer pObject, /*@ null @*/ gpointer pValue )
{ {
QofBook* book = QOF_BOOK(pObject); QofBook* book = QOF_BOOK(pObject);
GncGUID* guid = (GncGUID*)pValue; GncGUID* guid = (GncGUID*)pValue;
Account* root; Account* root;
g_return_if_fail( pObject != NULL ); g_return_if_fail( pObject != NULL );
g_return_if_fail( QOF_IS_BOOK(pObject) ); g_return_if_fail( QOF_IS_BOOK(pObject) );
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 );
@ -136,22 +141,23 @@ static void
load_single_book( GncSqlBackend* be, GncSqlRow* row ) load_single_book( GncSqlBackend* be, GncSqlRow* row )
{ {
const GncGUID* guid; const GncGUID* guid;
QofBook* pBook; QofBook* pBook;
g_return_if_fail( be != NULL ); g_return_if_fail( be != NULL );
g_return_if_fail( row != NULL ); g_return_if_fail( row != NULL );
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();
}
qof_book_begin_edit( pBook ); qof_book_begin_edit( pBook );
gnc_sql_load_object( be, row, GNC_ID_BOOK, pBook, col_table ); gnc_sql_load_object( be, row, GNC_ID_BOOK, pBook, col_table );
gnc_sql_slots_load( be, QOF_INSTANCE(pBook) ); gnc_sql_slots_load( be, QOF_INSTANCE(pBook) );
qof_book_commit_edit( pBook ); qof_book_commit_edit( pBook );
qof_instance_mark_clean( QOF_INSTANCE(pBook) ); qof_instance_mark_clean( QOF_INSTANCE(pBook) );
} }
@ -162,25 +168,30 @@ load_all_books( GncSqlBackend* be )
GncSqlStatement* stmt; GncSqlStatement* stmt;
GncSqlResult* result; GncSqlResult* result;
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 ); {
gnc_sql_statement_dispose( stmt ); result = gnc_sql_execute_select_statement( be, stmt );
if( result != NULL ) { gnc_sql_statement_dispose( stmt );
GncSqlRow* row = gnc_sql_result_get_first_row( result ); if ( result != NULL )
{
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) ); {
} else { (void)gnc_sql_save_book( be, QOF_INSTANCE(be->primary_book) );
// Otherwise, load the 1st book. }
load_single_book( be, row ); else
} {
// Otherwise, load the 1st book.
load_single_book( be, row );
}
gnc_sql_result_dispose( result ); gnc_sql_result_dispose( result );
} }
} }
} }
@ -188,12 +199,13 @@ load_all_books( GncSqlBackend* be )
static void static void
create_book_tables( GncSqlBackend* be ) create_book_tables( GncSqlBackend* be )
{ {
gint version; gint version;
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 );
} }
} }
@ -202,17 +214,17 @@ create_book_tables( GncSqlBackend* be )
gboolean gboolean
gnc_sql_save_book( GncSqlBackend* be, QofInstance* inst) gnc_sql_save_book( GncSqlBackend* be, QofInstance* inst)
{ {
gboolean status; gboolean status;
g_return_val_if_fail( be != NULL, FALSE ); g_return_val_if_fail( be != NULL, FALSE );
g_return_val_if_fail( inst != NULL, FALSE ); g_return_val_if_fail( inst != NULL, FALSE );
g_return_val_if_fail( QOF_IS_BOOK(inst), FALSE ); g_return_val_if_fail( QOF_IS_BOOK(inst), FALSE );
status = gnc_sql_commit_standard_item( be, inst, BOOK_TABLE, GNC_ID_BOOK, col_table ); status = gnc_sql_commit_standard_item( be, inst, BOOK_TABLE, GNC_ID_BOOK, col_table );
qof_book_mark_saved( QOF_BOOK(inst) ); qof_book_mark_saved( QOF_BOOK(inst) );
return status; return status;
} }
/* ================================================================= */ /* ================================================================= */
@ -226,10 +238,10 @@ gnc_sql_init_book_handler( void )
gnc_sql_save_book, /* commit */ gnc_sql_save_book, /* commit */
load_all_books, /* initial_load */ load_all_books, /* initial_load */
create_book_tables, /* create_tables */ create_book_tables, /* create_tables */
NULL, /* compile_query */ NULL, /* compile_query */
NULL, /* run_query */ NULL, /* run_query */
NULL, /* free_query */ NULL, /* free_query */
NULL /* write */ NULL /* write */
}; };
(void)qof_object_register_backend( GNC_ID_BOOK, GNC_SQL_BACKEND, &be_data ); (void)qof_object_register_backend( GNC_ID_BOOK, GNC_SQL_BACKEND, &be_data );

View File

@ -19,7 +19,7 @@
* Boston, MA 02110-1301, USA gnu@gnu.org * * Boston, MA 02110-1301, USA gnu@gnu.org *
\********************************************************************/ \********************************************************************/
/** @file gnc-budget-sql.c /** @file gnc-budget-sql.c
* @brief load and save data to SQL * @brief load and save data to SQL
* @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff@rogers.com> * @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff@rogers.com>
* *
* This file implements the top-level QofBackend API for saving/ * This file implements the top-level QofBackend API for saving/
@ -58,13 +58,13 @@
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" },
{ "name", CT_STRING, BUDGET_MAX_NAME_LEN, COL_NNUL, "name" }, { "name", CT_STRING, BUDGET_MAX_NAME_LEN, COL_NNUL, "name" },
{ "description", CT_STRING, BUDGET_MAX_DESCRIPTION_LEN, 0, "description" }, { "description", CT_STRING, BUDGET_MAX_DESCRIPTION_LEN, 0, "description" },
{ "num_periods", CT_INT, 0, COL_NNUL, "num_periods" }, { "num_periods", CT_INT, 0, COL_NNUL, "num_periods" },
{ NULL } { NULL }
/*@ +full_init_block @*/ /*@ +full_init_block @*/
}; };
static /*@ dependent @*//*@ null @*/ QofInstance* get_budget( gpointer pObj ); static /*@ dependent @*//*@ null @*/ QofInstance* get_budget( gpointer pObj );
@ -76,26 +76,35 @@ 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;
} budget_amount_info_t; } budget_amount_info_t;
static const GncSqlColumnTableEntry budget_amounts_col_table[] = 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 @*/
}; };
/* ================================================================= */ /* ================================================================= */
@ -104,9 +113,9 @@ get_budget( gpointer pObj )
{ {
budget_amount_info_t* info = (budget_amount_info_t*)pObj; budget_amount_info_t* info = (budget_amount_info_t*)pObj;
g_return_val_if_fail( pObj != NULL, NULL ); g_return_val_if_fail( pObj != NULL, NULL );
return QOF_INSTANCE(info->budget); return QOF_INSTANCE(info->budget);
} }
static void static void
@ -119,9 +128,9 @@ get_account( gpointer pObj )
{ {
budget_amount_info_t* info = (budget_amount_info_t*)pObj; budget_amount_info_t* info = (budget_amount_info_t*)pObj;
g_return_val_if_fail( pObj != NULL, NULL ); g_return_val_if_fail( pObj != NULL, NULL );
return QOF_INSTANCE(info->account); return QOF_INSTANCE(info->account);
} }
static void static void
@ -129,11 +138,11 @@ set_account( gpointer pObj, gpointer val )
{ {
budget_amount_info_t* info = (budget_amount_info_t*)pObj; budget_amount_info_t* info = (budget_amount_info_t*)pObj;
g_return_if_fail( pObj != NULL ); g_return_if_fail( pObj != NULL );
g_return_if_fail( val != NULL ); g_return_if_fail( val != NULL );
g_return_if_fail( GNC_IS_ACCOUNT(val) ); g_return_if_fail( GNC_IS_ACCOUNT(val) );
info->account = GNC_ACCOUNT(val); info->account = GNC_ACCOUNT(val);
} }
static gint static gint
@ -141,9 +150,9 @@ get_period_num( gpointer pObj )
{ {
budget_amount_info_t* info = (budget_amount_info_t*)pObj; budget_amount_info_t* info = (budget_amount_info_t*)pObj;
g_return_val_if_fail( pObj != NULL, 0 ); g_return_val_if_fail( pObj != NULL, 0 );
return info->period_num; return info->period_num;
} }
static void static void
@ -151,9 +160,9 @@ set_period_num( gpointer pObj, gpointer val )
{ {
budget_amount_info_t* info = (budget_amount_info_t*)pObj; budget_amount_info_t* info = (budget_amount_info_t*)pObj;
g_return_if_fail( pObj != NULL ); g_return_if_fail( pObj != NULL );
info->period_num = GPOINTER_TO_UINT(val); info->period_num = GPOINTER_TO_UINT(val);
} }
static gnc_numeric static gnc_numeric
@ -161,9 +170,9 @@ get_amount( gpointer pObj )
{ {
budget_amount_info_t* info = (budget_amount_info_t*)pObj; budget_amount_info_t* info = (budget_amount_info_t*)pObj;
g_return_val_if_fail( pObj != NULL, gnc_numeric_zero() ); g_return_val_if_fail( pObj != NULL, gnc_numeric_zero() );
return gnc_budget_get_account_period_value( info->budget, info->account, info->period_num ); return gnc_budget_get_account_period_value( info->budget, info->account, info->period_num );
} }
static void static void
@ -171,9 +180,9 @@ set_amount( gpointer pObj, gnc_numeric value )
{ {
budget_amount_info_t* info = (budget_amount_info_t*)pObj; budget_amount_info_t* info = (budget_amount_info_t*)pObj;
g_return_if_fail( pObj != NULL ); g_return_if_fail( pObj != NULL );
gnc_budget_set_account_period_value( info->budget, info->account, info->period_num, value ); gnc_budget_set_account_period_value( info->budget, info->account, info->period_num, value );
} }
/*----------------------------------------------------------------*/ /*----------------------------------------------------------------*/
@ -187,33 +196,36 @@ static void
load_budget_amounts( GncSqlBackend* be, GncBudget* budget ) load_budget_amounts( GncSqlBackend* be, GncBudget* budget )
{ {
gchar guid_buf[GUID_ENCODING_LENGTH+1]; gchar guid_buf[GUID_ENCODING_LENGTH+1];
gchar* sql; gchar* sql;
GncSqlStatement* stmt; GncSqlStatement* stmt;
GncSqlResult* result; GncSqlResult* result;
g_return_if_fail( be != NULL ); g_return_if_fail( be != NULL );
g_return_if_fail( budget != NULL ); g_return_if_fail( budget != NULL );
(void)guid_to_string_buff( qof_instance_get_guid( QOF_INSTANCE(budget) ), guid_buf ); (void)guid_to_string_buff( qof_instance_get_guid( QOF_INSTANCE(budget) ), guid_buf );
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 ); {
gnc_sql_statement_dispose( stmt ); result = gnc_sql_execute_select_statement( be, stmt );
if( result != NULL ) { gnc_sql_statement_dispose( stmt );
GncSqlRow* row = gnc_sql_result_get_first_row( result ); if ( result != NULL )
budget_amount_info_t info; {
GncSqlRow* row = gnc_sql_result_get_first_row( result );
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 ); {
row = gnc_sql_result_get_next_row( result ); gnc_sql_load_object( be, row, NULL, &info, budget_amounts_col_table );
} row = gnc_sql_result_get_next_row( result );
gnc_sql_result_dispose( result ); }
} gnc_sql_result_dispose( result );
} }
}
} }
/** /**
@ -226,17 +238,17 @@ static gboolean
delete_budget_amounts( GncSqlBackend* be, GncBudget* budget ) delete_budget_amounts( GncSqlBackend* be, GncBudget* budget )
{ {
gchar guid_buf[GUID_ENCODING_LENGTH+1]; gchar guid_buf[GUID_ENCODING_LENGTH+1];
gchar* sql; gchar* sql;
g_return_val_if_fail( be != NULL, FALSE ); g_return_val_if_fail( be != NULL, FALSE );
g_return_val_if_fail( budget != NULL, FALSE ); g_return_val_if_fail( budget != NULL, FALSE );
(void)guid_to_string_buff( qof_instance_get_guid( QOF_INSTANCE(budget) ), guid_buf ); (void)guid_to_string_buff( qof_instance_get_guid( QOF_INSTANCE(budget) ), guid_buf );
sql = g_strdup_printf( "DELETE FROM %s WHERE budget_guid='%s'", AMOUNTS_TABLE, guid_buf ); sql = g_strdup_printf( "DELETE FROM %s WHERE budget_guid='%s'", AMOUNTS_TABLE, guid_buf );
(void)gnc_sql_execute_nonselect_sql( be, sql ); (void)gnc_sql_execute_nonselect_sql( be, sql );
g_free( sql ); g_free( sql );
return TRUE; return TRUE;
} }
/** /**
@ -249,66 +261,73 @@ static gboolean
save_budget_amounts( GncSqlBackend* be, GncBudget* budget ) save_budget_amounts( GncSqlBackend* be, GncBudget* budget )
{ {
GList* descendants; GList* descendants;
/*@ dependent @*/ GList* node; /*@ dependent @*/
budget_amount_info_t info; GList* node;
guint num_periods; budget_amount_info_t info;
gboolean is_ok = TRUE;; guint num_periods;
gboolean is_ok = TRUE;;
g_return_val_if_fail( be != NULL, FALSE ); g_return_val_if_fail( be != NULL, FALSE );
g_return_val_if_fail( budget != NULL, FALSE ); g_return_val_if_fail( budget != NULL, FALSE );
// Delete the amounts, then save // Delete the amounts, then save
delete_budget_amounts( be, budget ); delete_budget_amounts( be, 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 ) ) { {
info.period_num = i; if ( gnc_budget_is_account_period_value_set( budget, info.account, i ) )
is_ok = gnc_sql_do_db_operation( be, OP_DB_INSERT, AMOUNTS_TABLE, "", &info, {
budget_amounts_col_table ); info.period_num = i;
} is_ok = gnc_sql_do_db_operation( be, OP_DB_INSERT, AMOUNTS_TABLE, "", &info,
} budget_amounts_col_table );
}
}
} }
g_list_free( descendants ); g_list_free( descendants );
return is_ok; return is_ok;
} }
/*----------------------------------------------------------------*/ /*----------------------------------------------------------------*/
static /*@ dependent @*//*@ null @*/ GncBudget* static /*@ dependent @*//*@ null @*/ GncBudget*
load_single_budget( GncSqlBackend* be, GncSqlRow* row ) load_single_budget( GncSqlBackend* be, GncSqlRow* row )
{ {
const GncGUID* guid; const GncGUID* guid;
GncBudget* pBudget = NULL; GncBudget* pBudget = NULL;
Recurrence* r; Recurrence* r;
g_return_val_if_fail( be != NULL, NULL ); g_return_val_if_fail( be != NULL, NULL );
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 );
} }
gnc_budget_begin_edit( pBudget ); gnc_budget_begin_edit( pBudget );
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 ); {
g_free( r ); gnc_budget_set_recurrence( pBudget, r );
} g_free( r );
gnc_budget_commit_edit( pBudget ); }
gnc_budget_commit_edit( pBudget );
return pBudget; return pBudget;
} }
static void static void
@ -316,32 +335,37 @@ load_all_budgets( GncSqlBackend* be )
{ {
GncSqlStatement* stmt; GncSqlStatement* stmt;
GncSqlResult* result; GncSqlResult* result;
GList* list = NULL; GList* list = NULL;
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 ); {
gnc_sql_statement_dispose( stmt ); result = gnc_sql_execute_select_statement( be, stmt );
if( result != NULL ) { gnc_sql_statement_dispose( stmt );
GncSqlRow* row = gnc_sql_result_get_first_row( result ); if ( result != NULL )
GncBudget* b; {
GncSqlRow* row = gnc_sql_result_get_first_row( result );
GncBudget* b;
while( row != NULL ) { while ( row != NULL )
b = load_single_budget( be, row ); {
if( b != NULL ) { b = load_single_budget( be, row );
list = g_list_prepend( list, b ); if ( b != NULL )
} {
row = gnc_sql_result_get_next_row( result ); list = g_list_prepend( list, b );
} }
gnc_sql_result_dispose( result ); row = gnc_sql_result_get_next_row( result );
}
gnc_sql_result_dispose( result );
if( list != NULL ) { if ( list != NULL )
gnc_sql_slots_load_for_list( be, list ); {
g_list_free( list ); gnc_sql_slots_load_for_list( be, list );
} g_list_free( list );
} }
}
} }
} }
@ -349,17 +373,19 @@ load_all_budgets( GncSqlBackend* be )
static void static void
create_budget_tables( GncSqlBackend* be ) create_budget_tables( GncSqlBackend* be )
{ {
gint version; gint version;
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 );
} }
} }
@ -370,111 +396,133 @@ save_budget( GncSqlBackend* be, QofInstance* inst )
{ {
GncBudget* pBudget = GNC_BUDGET(inst); GncBudget* pBudget = GNC_BUDGET(inst);
const GncGUID* guid; const GncGUID* guid;
gint op; gint op;
gboolean is_infant; gboolean is_infant;
gboolean is_ok; gboolean is_ok;
g_return_val_if_fail( be != NULL, FALSE ); g_return_val_if_fail( be != NULL, FALSE );
g_return_val_if_fail( inst != NULL, FALSE ); g_return_val_if_fail( inst != NULL, FALSE );
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; {
} else if( be->is_pristine_db || is_infant ) { op = OP_DB_DELETE;
op = OP_DB_INSERT; }
} else { else if ( be->is_pristine_db || is_infant )
op = OP_DB_UPDATE; {
} op = OP_DB_INSERT;
}
else
{
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 ); {
if( !qof_instance_get_destroying(inst) ) { guid = qof_instance_get_guid( inst );
is_ok = save_budget_amounts( be, pBudget ); if ( !qof_instance_get_destroying(inst) )
if( is_ok ) { {
is_ok = gnc_sql_recurrence_save( be, guid, gnc_budget_get_recurrence( pBudget ) ); is_ok = save_budget_amounts( be, 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_recurrence_save( be, guid, gnc_budget_get_recurrence( pBudget ) );
} }
} else { if ( is_ok )
is_ok = delete_budget_amounts( be, pBudget ); {
if( is_ok ) { is_ok = gnc_sql_slots_save( be, guid, is_infant, qof_instance_get_slots( inst ) );
is_ok = gnc_sql_recurrence_delete( be, guid ); }
} }
if( is_ok ) { else
(void)gnc_sql_slots_delete( be, guid ); {
} is_ok = delete_budget_amounts( be, pBudget );
} if ( is_ok )
} {
is_ok = gnc_sql_recurrence_delete( be, guid );
}
if ( is_ok )
{
(void)gnc_sql_slots_delete( be, guid );
}
}
}
return is_ok; return is_ok;
} }
static void static void
do_save_budget( QofInstance* inst, gpointer data ) 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 );
}
} }
static gboolean static gboolean
write_budgets( GncSqlBackend* be ) write_budgets( GncSqlBackend* be )
{ {
write_objects_t data; write_objects_t data;
g_return_val_if_fail( be != NULL, FALSE ); g_return_val_if_fail( be != NULL, FALSE );
data.be = be; data.be = be;
data.is_ok = TRUE; data.is_ok = TRUE;
qof_collection_foreach( qof_book_get_collection( be->primary_book, GNC_ID_BUDGET ), qof_collection_foreach( qof_book_get_collection( be->primary_book, GNC_ID_BUDGET ),
(QofInstanceForeachCB)do_save_budget, &data ); (QofInstanceForeachCB)do_save_budget, &data );
return data.is_ok; return data.is_ok;
} }
/* ================================================================= */ /* ================================================================= */
static void static void
load_budget_guid( const GncSqlBackend* be, GncSqlRow* row, load_budget_guid( const GncSqlBackend* be, GncSqlRow* row,
/*@ null @*/ QofSetterFunc setter, gpointer pObject, /*@ null @*/ QofSetterFunc setter, gpointer pObject,
const GncSqlColumnTableEntry* table_row ) const GncSqlColumnTableEntry* table_row )
{ {
const GValue* val; const GValue* val;
GncGUID guid; GncGUID guid;
GncBudget* budget = NULL; GncBudget* budget = NULL;
g_return_if_fail( be != NULL ); g_return_if_fail( be != NULL );
g_return_if_fail( row != NULL ); g_return_if_fail( row != NULL );
g_return_if_fail( pObject != NULL ); g_return_if_fail( pObject != NULL );
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 ) { {
g_object_set( pObject, table_row->gobj_param_name, budget, NULL ); if ( table_row->gobj_param_name != NULL )
} else { {
g_return_if_fail( setter != NULL ); g_object_set( pObject, table_row->gobj_param_name, budget, NULL );
(*setter)( pObject, (const gpointer)budget ); }
} else
} else { {
PWARN( "Budget ref '%s' not found", g_value_get_string( val ) ); g_return_if_fail( setter != NULL );
} (*setter)( pObject, (const gpointer)budget );
} }
}
else
{
PWARN( "Budget ref '%s' not found", g_value_get_string( val ) );
}
}
} }
static GncSqlColumnTypeHandler budget_guid_handler 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 )
@ -486,14 +534,14 @@ gnc_sql_init_budget_handler( void )
save_budget, /* commit */ save_budget, /* commit */
load_all_budgets, /* initial_load */ load_all_budgets, /* initial_load */
create_budget_tables, /* create_tables */ create_budget_tables, /* create_tables */
NULL, /* compile_query */ NULL, /* compile_query */
NULL, /* run_query */ NULL, /* run_query */
NULL, /* free_query */ NULL, /* free_query */
write_budgets /* write */ write_budgets /* write */
}; };
(void)qof_object_register_backend( GNC_ID_BUDGET, GNC_SQL_BACKEND, &be_data ); (void)qof_object_register_backend( GNC_ID_BUDGET, GNC_SQL_BACKEND, &be_data );
gnc_sql_register_col_type_handler( CT_BUDGETREF, &budget_guid_handler ); gnc_sql_register_col_type_handler( CT_BUDGETREF, &budget_guid_handler );
} }
/* ========================== END OF FILE ===================== */ /* ========================== END OF FILE ===================== */

View File

@ -19,7 +19,7 @@
* Boston, MA 02110-1301, USA gnu@gnu.org * * Boston, MA 02110-1301, USA gnu@gnu.org *
\********************************************************************/ \********************************************************************/
/** @file gnc-commodity-sql.c /** @file gnc-commodity-sql.c
* @brief load and save data to SQL * @brief load and save data to SQL
* @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff@rogers.com> * @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff@rogers.com>
* *
* This file implements the top-level QofBackend API for saving/ * This file implements the top-level QofBackend API for saving/
@ -57,22 +57,27 @@ 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 @*/ {
{ "guid", CT_GUID, 0, COL_NNUL|COL_PKEY, "guid" }, /*@ -full_init_block @*/
{ "namespace", CT_STRING, COMMODITY_MAX_NAMESPACE_LEN, COL_NNUL, NULL, NULL, { "guid", CT_GUID, 0, COL_NNUL | COL_PKEY, "guid" },
(QofAccessFunc)gnc_commodity_get_namespace, {
(QofSetterFunc)gnc_commodity_set_namespace }, "namespace", CT_STRING, COMMODITY_MAX_NAMESPACE_LEN, COL_NNUL, NULL, NULL,
(QofAccessFunc)gnc_commodity_get_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 @*/
}; };
/* ================================================================= */ /* ================================================================= */
@ -82,25 +87,25 @@ get_quote_source_name( gpointer pObject )
{ {
const gnc_commodity* pCommodity; const gnc_commodity* pCommodity;
g_return_val_if_fail( pObject != NULL, NULL ); g_return_val_if_fail( pObject != NULL, NULL );
g_return_val_if_fail( GNC_IS_COMMODITY(pObject), NULL ); g_return_val_if_fail( GNC_IS_COMMODITY(pObject), NULL );
pCommodity = GNC_COMMODITY(pObject); pCommodity = GNC_COMMODITY(pObject);
return (gpointer)gnc_quote_source_get_internal_name( return (gpointer)gnc_quote_source_get_internal_name(
gnc_commodity_get_quote_source(pCommodity)); gnc_commodity_get_quote_source(pCommodity));
} }
static void static void
set_quote_source_name( gpointer pObject, gpointer pValue ) set_quote_source_name( gpointer pObject, gpointer pValue )
{ {
gnc_commodity* pCommodity; gnc_commodity* pCommodity;
const gchar* quote_source_name = (const gchar*)pValue; const gchar* quote_source_name = (const gchar*)pValue;
gnc_quote_source* quote_source; gnc_quote_source* quote_source;
g_return_if_fail( pObject != NULL ); g_return_if_fail( pObject != NULL );
g_return_if_fail( GNC_IS_COMMODITY(pObject) ); g_return_if_fail( GNC_IS_COMMODITY(pObject) );
if( pValue == NULL ) return; if ( pValue == NULL ) return;
pCommodity = GNC_COMMODITY(pObject); pCommodity = GNC_COMMODITY(pObject);
quote_source = gnc_quote_source_lookup_by_internal( quote_source_name ); quote_source = gnc_quote_source_lookup_by_internal( quote_source_name );
@ -114,9 +119,9 @@ load_single_commodity( GncSqlBackend* be, GncSqlRow* row )
gnc_commodity* pCommodity; gnc_commodity* pCommodity;
pCommodity = gnc_commodity_new( pBook, NULL, NULL, NULL, NULL, 100 ); pCommodity = gnc_commodity_new( pBook, NULL, NULL, NULL, NULL, 100 );
gnc_commodity_begin_edit( pCommodity ); gnc_commodity_begin_edit( pCommodity );
gnc_sql_load_object( be, row, GNC_ID_COMMODITY, pCommodity, col_table ); gnc_sql_load_object( be, row, GNC_ID_COMMODITY, pCommodity, col_table );
gnc_commodity_commit_edit( pCommodity ); gnc_commodity_commit_edit( pCommodity );
return pCommodity; return pCommodity;
} }
@ -130,44 +135,48 @@ load_all_commodities( GncSqlBackend* be )
pTable = gnc_commodity_table_get_table( be->primary_book ); pTable = gnc_commodity_table_get_table( be->primary_book );
stmt = gnc_sql_create_select_statement( be, COMMODITIES_TABLE ); stmt = gnc_sql_create_select_statement( be, COMMODITIES_TABLE );
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) );
pCommodity = gnc_commodity_table_insert( pTable, pCommodity ); pCommodity = gnc_commodity_table_insert( pTable, pCommodity );
qof_instance_set_guid( QOF_INSTANCE(pCommodity), &guid ); qof_instance_set_guid( QOF_INSTANCE(pCommodity), &guid );
} }
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 );
sql = g_strdup_printf( "SELECT DISTINCT guid FROM %s", COMMODITIES_TABLE ); sql = g_strdup_printf( "SELECT DISTINCT guid FROM %s", COMMODITIES_TABLE );
gnc_sql_slots_load_for_sql_subquery( be, sql, gnc_sql_slots_load_for_sql_subquery( be, sql,
(BookLookupFn)gnc_commodity_find_commodity_by_guid ); (BookLookupFn)gnc_commodity_find_commodity_by_guid );
g_free( sql ); g_free( sql );
} }
} }
/* ================================================================= */ /* ================================================================= */
static void static void
create_commodities_tables( GncSqlBackend* be ) create_commodities_tables( GncSqlBackend* be )
{ {
gint version; gint version;
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 );
} }
} }
@ -176,40 +185,49 @@ create_commodities_tables( GncSqlBackend* be )
static gboolean static gboolean
do_commit_commodity( GncSqlBackend* be, QofInstance* inst, gboolean force_insert ) do_commit_commodity( GncSqlBackend* be, QofInstance* inst, gboolean force_insert )
{ {
const GncGUID* guid; const GncGUID* guid;
gboolean is_infant; gboolean is_infant;
gint op; gint op;
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; {
} else if( be->is_pristine_db || is_infant || force_insert ) { op = OP_DB_DELETE;
op = OP_DB_INSERT; }
} else { else if ( be->is_pristine_db || is_infant || force_insert )
op = OP_DB_UPDATE; {
} op = OP_DB_INSERT;
}
else
{
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 {
guid = qof_instance_get_guid( inst ); // Now, commit any slots
if( !qof_instance_get_destroying(inst) ) { guid = qof_instance_get_guid( inst );
is_ok = gnc_sql_slots_save( be, guid, is_infant, qof_instance_get_slots( inst ) ); if ( !qof_instance_get_destroying(inst) )
} else { {
is_ok = gnc_sql_slots_delete( be, guid ); is_ok = gnc_sql_slots_save( be, guid, is_infant, qof_instance_get_slots( inst ) );
} }
} else
{
is_ok = gnc_sql_slots_delete( be, guid );
}
}
return is_ok; return is_ok;
} }
static gboolean static gboolean
commit_commodity( GncSqlBackend* be, QofInstance* inst ) commit_commodity( GncSqlBackend* be, QofInstance* inst )
{ {
g_return_val_if_fail( be != NULL, FALSE ); g_return_val_if_fail( be != NULL, FALSE );
g_return_val_if_fail( inst != NULL, FALSE ); g_return_val_if_fail( inst != NULL, FALSE );
g_return_val_if_fail( GNC_IS_COMMODITY(inst), FALSE ); g_return_val_if_fail( GNC_IS_COMMODITY(inst), FALSE );
return do_commit_commodity( be, inst, FALSE ); return do_commit_commodity( be, inst, FALSE );
} }
@ -217,65 +235,74 @@ commit_commodity( GncSqlBackend* be, QofInstance* inst )
static gboolean static gboolean
is_commodity_in_db( GncSqlBackend* be, gnc_commodity* pCommodity ) is_commodity_in_db( 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 );
return gnc_sql_object_is_it_in_db( be, COMMODITIES_TABLE, GNC_ID_COMMODITY, return gnc_sql_object_is_it_in_db( be, COMMODITIES_TABLE, GNC_ID_COMMODITY,
pCommodity, col_table ); pCommodity, col_table );
} }
gboolean gboolean
gnc_sql_save_commodity( GncSqlBackend* be, gnc_commodity* pCommodity ) gnc_sql_save_commodity( GncSqlBackend* be, gnc_commodity* pCommodity )
{ {
gboolean is_ok = TRUE; gboolean is_ok = TRUE;
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 );
} }
return is_ok; return is_ok;
} }
/* ----------------------------------------------------------------- */ /* ----------------------------------------------------------------- */
static void static void
load_commodity_guid( const GncSqlBackend* be, GncSqlRow* row, load_commodity_guid( const GncSqlBackend* be, GncSqlRow* row,
/*@ null @*/ QofSetterFunc setter, gpointer pObject, /*@ null @*/ QofSetterFunc setter, gpointer pObject,
const GncSqlColumnTableEntry* table_row ) const GncSqlColumnTableEntry* table_row )
{ {
const GValue* val; const GValue* val;
GncGUID guid; GncGUID guid;
gnc_commodity* commodity = NULL; gnc_commodity* commodity = NULL;
g_return_if_fail( be != NULL ); g_return_if_fail( be != NULL );
g_return_if_fail( row != NULL ); g_return_if_fail( row != NULL );
g_return_if_fail( pObject != NULL ); g_return_if_fail( pObject != NULL );
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 ); {
commodity = gnc_commodity_find_commodity_by_guid( &guid, be->primary_book ); (void)string_to_guid( g_value_get_string( val ), &guid );
if( commodity != NULL ) { commodity = gnc_commodity_find_commodity_by_guid( &guid, be->primary_book );
if( table_row->gobj_param_name != NULL ) { if ( commodity != NULL )
g_object_set( pObject, table_row->gobj_param_name, commodity, NULL ); {
} else if( setter != NULL ) { if ( table_row->gobj_param_name != NULL )
(*setter)( pObject, (const gpointer)commodity ); {
} g_object_set( pObject, table_row->gobj_param_name, commodity, NULL );
} else { }
PWARN( "Commodity ref '%s' not found", g_value_get_string( val ) ); else if ( setter != NULL )
} {
} (*setter)( pObject, (const gpointer)commodity );
}
}
else
{
PWARN( "Commodity ref '%s' not found", g_value_get_string( val ) );
}
}
} }
static GncSqlColumnTypeHandler commodity_guid_handler 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 )
@ -287,14 +314,14 @@ gnc_sql_init_commodity_handler( void )
commit_commodity, /* commit */ commit_commodity, /* commit */
load_all_commodities, /* initial_load */ load_all_commodities, /* initial_load */
create_commodities_tables, /* create_tables */ create_commodities_tables, /* create_tables */
NULL, /* compile_query */ NULL, /* compile_query */
NULL, /* run_query */ NULL, /* run_query */
NULL, /* free_query */ NULL, /* free_query */
NULL /* write */ NULL /* write */
}; };
(void)qof_object_register_backend( GNC_ID_COMMODITY, GNC_SQL_BACKEND, &be_data ); (void)qof_object_register_backend( GNC_ID_COMMODITY, GNC_SQL_BACKEND, &be_data );
gnc_sql_register_col_type_handler( CT_COMMODITYREF, &commodity_guid_handler ); gnc_sql_register_col_type_handler( CT_COMMODITYREF, &commodity_guid_handler );
} }
/* ========================== END OF FILE ===================== */ /* ========================== END OF FILE ===================== */

View File

@ -19,7 +19,7 @@
* Boston, MA 02110-1301, USA gnu@gnu.org * * Boston, MA 02110-1301, USA gnu@gnu.org *
\********************************************************************/ \********************************************************************/
/** @file gnc-lots-sql.c /** @file gnc-lots-sql.c
* @brief load and save data to SQL * @brief load and save data to SQL
* @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff@rogers.com> * @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff@rogers.com>
* *
* This file implements the top-level QofBackend API for saving/ * This file implements the top-level QofBackend API for saving/
@ -53,13 +53,15 @@ static void set_lot_account( gpointer pObject, /*@ null @*/ gpointer pValue );
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" },
{ "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 @*/
}; };
/* ================================================================= */ /* ================================================================= */
@ -69,45 +71,46 @@ get_lot_account( gpointer pObject )
const GNCLot* lot; const GNCLot* lot;
Account* pAccount; Account* pAccount;
g_return_val_if_fail( pObject != NULL, NULL ); g_return_val_if_fail( pObject != NULL, NULL );
g_return_val_if_fail( GNC_IS_LOT(pObject), NULL ); g_return_val_if_fail( GNC_IS_LOT(pObject), NULL );
lot = GNC_LOT(pObject); lot = GNC_LOT(pObject);
pAccount = gnc_lot_get_account( lot ); pAccount = gnc_lot_get_account( lot );
return pAccount; return pAccount;
} }
static void static void
set_lot_account( gpointer pObject, /*@ null @*/ gpointer pValue ) set_lot_account( gpointer pObject, /*@ null @*/ gpointer pValue )
{ {
GNCLot* lot; GNCLot* lot;
Account* pAccount; Account* pAccount;
g_return_if_fail( pObject != NULL && GNC_IS_LOT(pObject) ); g_return_if_fail( pObject != NULL && GNC_IS_LOT(pObject) );
g_return_if_fail( pValue == NULL || GNC_IS_ACCOUNT(pValue) ); g_return_if_fail( pValue == NULL || GNC_IS_ACCOUNT(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 );
}
} }
static /*@ dependent @*//*@ null @*/ GNCLot* static /*@ dependent @*//*@ null @*/ GNCLot*
load_single_lot( GncSqlBackend* be, GncSqlRow* row ) load_single_lot( GncSqlBackend* be, GncSqlRow* row )
{ {
GNCLot* lot; GNCLot* lot;
g_return_val_if_fail( be != NULL, NULL ); g_return_val_if_fail( be != NULL, NULL );
g_return_val_if_fail( row != NULL, NULL ); g_return_val_if_fail( row != NULL, NULL );
lot = gnc_lot_new( be->primary_book ); lot = gnc_lot_new( be->primary_book );
gnc_lot_begin_edit( lot ); gnc_lot_begin_edit( lot );
gnc_sql_load_object( be, row, GNC_ID_LOT, lot, col_table ); gnc_sql_load_object( be, row, GNC_ID_LOT, lot, col_table );
gnc_lot_commit_edit( lot ); gnc_lot_commit_edit( lot );
return lot; return lot;
} }
static void static void
@ -116,51 +119,57 @@ load_all_lots( GncSqlBackend* be )
GncSqlStatement* stmt; GncSqlStatement* stmt;
GncSqlResult* result; GncSqlResult* result;
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 ); {
gnc_sql_statement_dispose( stmt ); result = gnc_sql_execute_select_statement( be, stmt );
if( result != NULL ) { gnc_sql_statement_dispose( stmt );
GncSqlRow* row = gnc_sql_result_get_first_row( result ); if ( result != NULL )
GNCLot* lot; {
gchar* sql; GncSqlRow* row = gnc_sql_result_get_first_row( result );
GNCLot* lot;
gchar* sql;
while( row != NULL ) { while ( row != NULL )
lot = load_single_lot( be, row ); {
row = gnc_sql_result_get_next_row( result ); lot = load_single_lot( be, row );
} row = gnc_sql_result_get_next_row( result );
gnc_sql_result_dispose( result ); }
gnc_sql_result_dispose( result );
sql = g_strdup_printf( "SELECT DISTINCT guid FROM %s", TABLE_NAME ); sql = g_strdup_printf( "SELECT DISTINCT guid FROM %s", TABLE_NAME );
gnc_sql_slots_load_for_sql_subquery( be, sql, (BookLookupFn)gnc_lot_lookup ); gnc_sql_slots_load_for_sql_subquery( be, sql, (BookLookupFn)gnc_lot_lookup );
g_free( sql ); g_free( sql );
} }
} }
} }
/* ================================================================= */ /* ================================================================= */
static void static void
create_lots_tables( GncSqlBackend* be ) create_lots_tables( GncSqlBackend* be )
{ {
gint version; gint version;
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 ) { }
/* Version 1 -> 2 removes the 'NOT NULL' constraint on the account_guid else if ( version == 1 )
field. {
/* Version 1 -> 2 removes the 'NOT NULL' constraint on the account_guid
field.
Create a temporary table, copy the data from the old table, delete the Create a temporary table, copy the data from the old table, delete the
old table, then rename the new one. */ old table, then rename the new one. */
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 );
} }
} }
@ -169,9 +178,9 @@ create_lots_tables( GncSqlBackend* be )
static gboolean static gboolean
commit_lot( GncSqlBackend* be, QofInstance* inst ) commit_lot( GncSqlBackend* be, QofInstance* inst )
{ {
g_return_val_if_fail( be != NULL, FALSE ); g_return_val_if_fail( be != NULL, FALSE );
g_return_val_if_fail( inst != NULL, FALSE ); g_return_val_if_fail( inst != NULL, FALSE );
g_return_val_if_fail( GNC_IS_LOT(inst), FALSE ); g_return_val_if_fail( GNC_IS_LOT(inst), FALSE );
return gnc_sql_commit_standard_item( be, inst, TABLE_NAME, GNC_ID_LOT, col_table ); return gnc_sql_commit_standard_item( be, inst, TABLE_NAME, GNC_ID_LOT, col_table );
} }
@ -179,64 +188,73 @@ commit_lot( GncSqlBackend* be, QofInstance* inst )
static void static void
do_save_lot( QofInstance* inst, gpointer data ) 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 );
}
} }
static gboolean static gboolean
write_lots( GncSqlBackend* be ) write_lots( GncSqlBackend* be )
{ {
write_objects_t data; write_objects_t data;
g_return_val_if_fail( be != NULL, FALSE ); g_return_val_if_fail( be != NULL, FALSE );
data.be = be; data.be = be;
data.is_ok = TRUE; data.is_ok = TRUE;
qof_collection_foreach( qof_book_get_collection( be->primary_book, GNC_ID_LOT ), qof_collection_foreach( qof_book_get_collection( be->primary_book, GNC_ID_LOT ),
(QofInstanceForeachCB)do_save_lot, &data ); (QofInstanceForeachCB)do_save_lot, &data );
return data.is_ok; return data.is_ok;
} }
/* ================================================================= */ /* ================================================================= */
static void static void
load_lot_guid( const GncSqlBackend* be, GncSqlRow* row, load_lot_guid( const GncSqlBackend* be, GncSqlRow* row,
/*@ null @*/ QofSetterFunc setter, gpointer pObject, /*@ null @*/ QofSetterFunc setter, gpointer pObject,
const GncSqlColumnTableEntry* table_row ) const GncSqlColumnTableEntry* table_row )
{ {
const GValue* val; const GValue* val;
GncGUID guid; GncGUID guid;
GNCLot* lot; GNCLot* lot;
g_return_if_fail( be != NULL ); g_return_if_fail( be != NULL );
g_return_if_fail( row != NULL ); g_return_if_fail( row != NULL );
g_return_if_fail( pObject != NULL ); g_return_if_fail( pObject != NULL );
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 ); {
lot = gnc_lot_lookup( &guid, be->primary_book ); (void)string_to_guid( g_value_get_string( val ), &guid );
if( lot != NULL ) { lot = gnc_lot_lookup( &guid, be->primary_book );
if( table_row->gobj_param_name != NULL ) { if ( lot != NULL )
g_object_set( pObject, table_row->gobj_param_name, lot, NULL ); {
} else { if ( table_row->gobj_param_name != NULL )
g_return_if_fail( setter != NULL ); {
(*setter)( pObject, (const gpointer)lot ); g_object_set( pObject, table_row->gobj_param_name, lot, NULL );
} }
} else { else
PWARN( "Lot ref '%s' not found", g_value_get_string( val ) ); {
} g_return_if_fail( setter != NULL );
} (*setter)( pObject, (const gpointer)lot );
}
}
else
{
PWARN( "Lot ref '%s' not found", g_value_get_string( val ) );
}
}
} }
static GncSqlColumnTypeHandler lot_guid_handler 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 )
@ -248,13 +266,13 @@ gnc_sql_init_lot_handler( void )
commit_lot, /* commit */ commit_lot, /* commit */
load_all_lots, /* initial_load */ load_all_lots, /* initial_load */
create_lots_tables, /* create tables */ create_lots_tables, /* create tables */
NULL, NULL, NULL, NULL, NULL, NULL,
write_lots /* save all */ write_lots /* save all */
}; };
(void)qof_object_register_backend( GNC_ID_LOT, GNC_SQL_BACKEND, &be_data ); (void)qof_object_register_backend( GNC_ID_LOT, GNC_SQL_BACKEND, &be_data );
gnc_sql_register_col_type_handler( CT_LOTREF, &lot_guid_handler ); gnc_sql_register_col_type_handler( CT_LOTREF, &lot_guid_handler );
} }
/* ========================== END OF FILE ===================== */ /* ========================== END OF FILE ===================== */

View File

@ -19,7 +19,7 @@
* Boston, MA 02110-1301, USA gnu@gnu.org * * Boston, MA 02110-1301, USA gnu@gnu.org *
\********************************************************************/ \********************************************************************/
/** @file gnc-price-sql.c /** @file gnc-price-sql.c
* @brief load and save data to SQL * @brief load and save data to SQL
* @author Copyright (c) 2006-2009 Phil Longstaff <plongstaff@rogers.com> * @author Copyright (c) 2006-2009 Phil Longstaff <plongstaff@rogers.com>
* *
* This file implements the top-level QofBackend API for saving/ * This file implements the top-level QofBackend API for saving/
@ -53,8 +53,8 @@
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" },
{ "commodity_guid", CT_COMMODITYREF, 0, COL_NNUL, "commodity" }, { "commodity_guid", CT_COMMODITYREF, 0, COL_NNUL, "commodity" },
{ "currency_guid", CT_COMMODITYREF, 0, COL_NNUL, "currency" }, { "currency_guid", CT_COMMODITYREF, 0, COL_NNUL, "currency" },
{ "date", CT_TIMESPEC, 0, COL_NNUL, "date" }, { "date", CT_TIMESPEC, 0, COL_NNUL, "date" },
@ -62,7 +62,7 @@ static const GncSqlColumnTableEntry col_table[] =
{ "type", CT_STRING, PRICE_MAX_TYPE_LEN, 0, "type" }, { "type", CT_STRING, PRICE_MAX_TYPE_LEN, 0, "type" },
{ "value", CT_NUMERIC, 0, COL_NNUL, "value" }, { "value", CT_NUMERIC, 0, COL_NNUL, "value" },
{ NULL } { NULL }
/*@ +full_init_block @*/ /*@ +full_init_block @*/
}; };
/* ================================================================= */ /* ================================================================= */
@ -70,16 +70,16 @@ static const GncSqlColumnTableEntry col_table[] =
static /*@ null @*//*@ dependent @*/ GNCPrice* static /*@ null @*//*@ dependent @*/ GNCPrice*
load_single_price( GncSqlBackend* be, GncSqlRow* row ) load_single_price( GncSqlBackend* be, GncSqlRow* row )
{ {
GNCPrice* pPrice; GNCPrice* pPrice;
g_return_val_if_fail( be != NULL, NULL ); g_return_val_if_fail( be != NULL, NULL );
g_return_val_if_fail( row != NULL, NULL ); g_return_val_if_fail( row != NULL, NULL );
pPrice = gnc_price_create( be->primary_book ); pPrice = gnc_price_create( be->primary_book );
gnc_price_begin_edit( pPrice ); gnc_price_begin_edit( pPrice );
gnc_sql_load_object( be, row, GNC_ID_PRICE, pPrice, col_table ); gnc_sql_load_object( be, row, GNC_ID_PRICE, pPrice, col_table );
gnc_price_commit_edit( pPrice ); gnc_price_commit_edit( pPrice );
return pPrice; return pPrice;
} }
@ -92,36 +92,40 @@ load_all_prices( GncSqlBackend* be )
QofBook* pBook; QofBook* pBook;
GNCPriceDB* pPriceDB; GNCPriceDB* pPriceDB;
g_return_if_fail( be != NULL ); g_return_if_fail( be != NULL );
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 ); {
gnc_sql_statement_dispose( stmt ); result = gnc_sql_execute_select_statement( be, stmt );
if( result != NULL ) { gnc_sql_statement_dispose( stmt );
GNCPrice* pPrice; if ( result != NULL )
GncSqlRow* row = gnc_sql_result_get_first_row( result ); {
gchar* sql; GNCPrice* pPrice;
GncSqlRow* row = gnc_sql_result_get_first_row( result );
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 ); {
gnc_price_unref( pPrice ); (void)gnc_pricedb_add_price( pPriceDB, pPrice );
} gnc_price_unref( pPrice );
row = gnc_sql_result_get_next_row( result ); }
} row = gnc_sql_result_get_next_row( result );
gnc_sql_result_dispose( result ); }
gnc_pricedb_set_bulk_update( pPriceDB, FALSE ); gnc_sql_result_dispose( result );
gnc_pricedb_set_bulk_update( pPriceDB, FALSE );
sql = g_strdup_printf( "SELECT DISTINCT guid FROM %s", TABLE_NAME ); sql = g_strdup_printf( "SELECT DISTINCT guid FROM %s", TABLE_NAME );
gnc_sql_slots_load_for_sql_subquery( be, sql, (BookLookupFn)gnc_price_lookup ); gnc_sql_slots_load_for_sql_subquery( be, sql, (BookLookupFn)gnc_price_lookup );
g_free( sql ); g_free( sql );
} }
} }
} }
@ -129,17 +133,20 @@ load_all_prices( GncSqlBackend* be )
static void static void
create_prices_tables( GncSqlBackend* be ) create_prices_tables( GncSqlBackend* be )
{ {
gint version; gint version;
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 ) { }
/* Upgrade 64 bit int handling */ else if ( version == 1 )
gnc_sql_upgrade_table( be, TABLE_NAME, col_table ); {
(void)gnc_sql_set_table_version( be, TABLE_NAME, TABLE_VERSION ); /* Upgrade 64 bit int handling */
gnc_sql_upgrade_table( be, TABLE_NAME, col_table );
(void)gnc_sql_set_table_version( be, TABLE_NAME, TABLE_VERSION );
} }
} }
@ -149,34 +156,41 @@ static gboolean
save_price( GncSqlBackend* be, QofInstance* inst ) save_price( GncSqlBackend* be, QofInstance* inst )
{ {
GNCPrice* pPrice = GNC_PRICE(inst); GNCPrice* pPrice = GNC_PRICE(inst);
gint op; gint op;
gboolean is_infant; gboolean is_infant;
gboolean is_ok = TRUE; gboolean is_ok = TRUE;
g_return_val_if_fail( be != NULL, FALSE ); g_return_val_if_fail( be != NULL, FALSE );
g_return_val_if_fail( inst != NULL, FALSE ); g_return_val_if_fail( inst != NULL, FALSE );
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; {
} else if( be->is_pristine_db || is_infant ) { op = OP_DB_DELETE;
op = OP_DB_INSERT; }
} else { else if ( be->is_pristine_db || is_infant )
op = OP_DB_UPDATE; {
} op = OP_DB_INSERT;
}
else
{
op = OP_DB_UPDATE;
}
if( op != OP_DB_DELETE ) { if ( op != OP_DB_DELETE )
/* Ensure commodity and currency are in the db */ {
(void)gnc_sql_save_commodity( be, gnc_price_get_commodity( pPrice ) ); /* Ensure commodity and currency are in the db */
is_ok = gnc_sql_save_commodity( be, gnc_price_get_currency( pPrice ) ); (void)gnc_sql_save_commodity( be, gnc_price_get_commodity( 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 );
}
return is_ok; return is_ok;
} }
static gboolean static gboolean
@ -184,12 +198,13 @@ write_price( GNCPrice* p, gpointer data )
{ {
write_objects_t* s = (write_objects_t*)data; write_objects_t* s = (write_objects_t*)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) );
}
return s->is_ok; return s->is_ok;
} }
@ -198,14 +213,14 @@ static gboolean
write_prices( GncSqlBackend* be ) write_prices( GncSqlBackend* be )
{ {
GNCPriceDB* priceDB; GNCPriceDB* priceDB;
write_objects_t data; write_objects_t data;
g_return_val_if_fail( be != NULL, FALSE ); g_return_val_if_fail( be != NULL, FALSE );
priceDB = gnc_book_get_pricedb( be->primary_book ); priceDB = gnc_book_get_pricedb( be->primary_book );
data.be = be; data.be = be;
data.is_ok = TRUE; data.is_ok = TRUE;
return gnc_pricedb_foreach_price( priceDB, write_price, &data, TRUE ); return gnc_pricedb_foreach_price( priceDB, write_price, &data, TRUE );
} }
@ -220,8 +235,8 @@ gnc_sql_init_price_handler( void )
save_price, /* commit */ save_price, /* commit */
load_all_prices, /* initial_load */ load_all_prices, /* initial_load */
create_prices_tables, /* create tables */ create_prices_tables, /* create tables */
NULL, NULL, NULL, NULL, NULL, NULL,
write_prices /* write */ write_prices /* write */
}; };
(void)qof_object_register_backend( GNC_ID_PRICE, GNC_SQL_BACKEND, &be_data ); (void)qof_object_register_backend( GNC_ID_PRICE, GNC_SQL_BACKEND, &be_data );

View File

@ -19,7 +19,7 @@
* Boston, MA 02110-1301, USA gnu@gnu.org * * Boston, MA 02110-1301, USA gnu@gnu.org *
\********************************************************************/ \********************************************************************/
/** @file gnc-recurrence-sql.c /** @file gnc-recurrence-sql.c
* @brief load and save data to SQL * @brief load and save data to SQL
* @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff@rogers.com> * @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff@rogers.com>
* *
* This file implements the top-level QofBackend API for saving/ * This file implements the top-level QofBackend API for saving/
@ -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 );
@ -66,29 +69,39 @@ static void set_recurrence_period_start( gpointer pObject, /*@ null @*/ gpointer
static const GncSqlColumnTableEntry col_table[] = 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 @*/
}; };
/* Special column table because we need to be able to access the table by /* Special column table because we need to be able to access the table by
a column other than the primary key */ 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 @*/
}; };
/* ================================================================= */ /* ================================================================= */
@ -98,7 +111,7 @@ get_obj_guid( gpointer pObject )
{ {
recurrence_info_t* pInfo = (recurrence_info_t*)pObject; recurrence_info_t* pInfo = (recurrence_info_t*)pObject;
g_return_val_if_fail( pObject != NULL, NULL ); g_return_val_if_fail( pObject != NULL, NULL );
return (gpointer)pInfo->guid; return (gpointer)pInfo->guid;
} }
@ -113,11 +126,11 @@ static gint
get_recurrence_mult( gpointer pObject ) get_recurrence_mult( gpointer pObject )
{ {
recurrence_info_t* pInfo = (recurrence_info_t*)pObject; recurrence_info_t* pInfo = (recurrence_info_t*)pObject;
g_return_val_if_fail( pObject != NULL, 0 );
g_return_val_if_fail( pInfo->pRecurrence != NULL, 0 );
return (gint)pInfo->pRecurrence->mult; g_return_val_if_fail( pObject != NULL, 0 );
g_return_val_if_fail( pInfo->pRecurrence != NULL, 0 );
return (gint)pInfo->pRecurrence->mult;
} }
static void static void
@ -125,8 +138,8 @@ set_recurrence_mult( gpointer pObject, gint value )
{ {
recurrence_info_t* pInfo = (recurrence_info_t*)pObject; recurrence_info_t* pInfo = (recurrence_info_t*)pObject;
g_return_if_fail( pObject != NULL ); g_return_if_fail( pObject != NULL );
g_return_if_fail( pInfo->pRecurrence != NULL ); g_return_if_fail( pInfo->pRecurrence != NULL );
pInfo->pRecurrence->mult = (guint16)value; pInfo->pRecurrence->mult = (guint16)value;
} }
@ -136,11 +149,11 @@ get_recurrence_period_type( gpointer pObject )
{ {
recurrence_info_t* pInfo = (recurrence_info_t*)pObject; recurrence_info_t* pInfo = (recurrence_info_t*)pObject;
g_return_val_if_fail( pObject != NULL, NULL ); g_return_val_if_fail( pObject != NULL, NULL );
g_return_val_if_fail( pInfo->pRecurrence != NULL, NULL ); g_return_val_if_fail( pInfo->pRecurrence != NULL, NULL );
return (gpointer)recurrencePeriodTypeToString( return (gpointer)recurrencePeriodTypeToString(
recurrenceGetPeriodType( pInfo->pRecurrence ) ); recurrenceGetPeriodType( pInfo->pRecurrence ) );
} }
static void static void
@ -148,9 +161,9 @@ set_recurrence_period_type( gpointer pObject, gpointer pValue )
{ {
recurrence_info_t* pInfo = (recurrence_info_t*)pObject; recurrence_info_t* pInfo = (recurrence_info_t*)pObject;
g_return_if_fail( pObject != NULL ); g_return_if_fail( pObject != NULL );
g_return_if_fail( pInfo->pRecurrence != NULL ); g_return_if_fail( pInfo->pRecurrence != NULL );
g_return_if_fail( pValue != NULL ); g_return_if_fail( pValue != NULL );
pInfo->pRecurrence->ptype = recurrencePeriodTypeFromString( (gchar*)pValue ); pInfo->pRecurrence->ptype = recurrencePeriodTypeFromString( (gchar*)pValue );
} }
@ -161,8 +174,8 @@ get_recurrence_period_start( gpointer pObject )
recurrence_info_t* pInfo = (recurrence_info_t*)pObject; recurrence_info_t* pInfo = (recurrence_info_t*)pObject;
static GDate date; static GDate date;
g_return_val_if_fail( pObject != NULL, NULL ); g_return_val_if_fail( pObject != NULL, NULL );
g_return_val_if_fail( pInfo->pRecurrence != NULL, NULL ); g_return_val_if_fail( pInfo->pRecurrence != NULL, NULL );
date = recurrenceGetDate( pInfo->pRecurrence ); date = recurrenceGetDate( pInfo->pRecurrence );
return (gpointer)&date; return (gpointer)&date;
@ -174,9 +187,9 @@ set_recurrence_period_start( gpointer pObject, gpointer pValue )
recurrence_info_t* pInfo = (recurrence_info_t*)pObject; recurrence_info_t* pInfo = (recurrence_info_t*)pObject;
GDate* date = (GDate*)pValue; GDate* date = (GDate*)pValue;
g_return_if_fail( pObject != NULL ); g_return_if_fail( pObject != NULL );
g_return_if_fail( pInfo->pRecurrence != NULL ); g_return_if_fail( pInfo->pRecurrence != NULL );
g_return_if_fail( pValue != NULL ); g_return_if_fail( pValue != NULL );
pInfo->pRecurrence->start = *date; pInfo->pRecurrence->start = *date;
} }
@ -188,37 +201,38 @@ gnc_sql_recurrence_save( GncSqlBackend* be, const GncGUID* guid, const Recurrenc
{ {
recurrence_info_t recurrence_info; recurrence_info_t recurrence_info;
g_return_val_if_fail( be != NULL, FALSE ); g_return_val_if_fail( be != NULL, FALSE );
g_return_val_if_fail( guid != NULL, FALSE ); g_return_val_if_fail( guid != NULL, FALSE );
g_return_val_if_fail( r != NULL, FALSE ); g_return_val_if_fail( r != NULL, FALSE );
(void)gnc_sql_recurrence_delete( be, guid ); (void)gnc_sql_recurrence_delete( be, guid );
recurrence_info.be = be; recurrence_info.be = be;
recurrence_info.guid = guid; recurrence_info.guid = guid;
recurrence_info.pRecurrence = (Recurrence*)r; recurrence_info.pRecurrence = (Recurrence*)r;
return gnc_sql_do_db_operation( be, OP_DB_INSERT, TABLE_NAME, return gnc_sql_do_db_operation( be, OP_DB_INSERT, TABLE_NAME,
TABLE_NAME, &recurrence_info, col_table ); TABLE_NAME, &recurrence_info, col_table );
} }
void void
gnc_sql_recurrence_save_list( GncSqlBackend* be, const GncGUID* guid, GList* schedule ) gnc_sql_recurrence_save_list( GncSqlBackend* be, const GncGUID* guid, GList* schedule )
{ {
recurrence_info_t recurrence_info; recurrence_info_t recurrence_info;
GList* l; GList* l;
g_return_if_fail( be != NULL ); g_return_if_fail( be != NULL );
g_return_if_fail( guid != NULL ); g_return_if_fail( guid != NULL );
(void)gnc_sql_recurrence_delete( be, guid ); (void)gnc_sql_recurrence_delete( be, guid );
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; {
(void)gnc_sql_do_db_operation( be, OP_DB_INSERT, TABLE_NAME, recurrence_info.pRecurrence = (Recurrence*)l->data;
TABLE_NAME, &recurrence_info, col_table ); (void)gnc_sql_do_db_operation( be, OP_DB_INSERT, TABLE_NAME,
} TABLE_NAME, &recurrence_info, col_table );
}
} }
gboolean gboolean
@ -226,13 +240,13 @@ gnc_sql_recurrence_delete( GncSqlBackend* be, const GncGUID* guid )
{ {
recurrence_info_t recurrence_info; recurrence_info_t recurrence_info;
g_return_val_if_fail( be != NULL, FALSE ); g_return_val_if_fail( be != NULL, FALSE );
g_return_val_if_fail( guid != NULL, FALSE ); g_return_val_if_fail( guid != NULL, FALSE );
recurrence_info.be = be; recurrence_info.be = be;
recurrence_info.guid = guid; recurrence_info.guid = guid;
return gnc_sql_do_db_operation( be, OP_DB_DELETE, TABLE_NAME, return gnc_sql_do_db_operation( be, OP_DB_DELETE, TABLE_NAME,
TABLE_NAME, &recurrence_info, guid_col_table ); TABLE_NAME, &recurrence_info, guid_col_table );
} }
static void static void
@ -240,12 +254,12 @@ load_recurrence( GncSqlBackend* be, GncSqlRow* row, /*@ out @*/ Recurrence* r )
{ {
recurrence_info_t recurrence_info; recurrence_info_t recurrence_info;
g_return_if_fail( be != NULL ); g_return_if_fail( be != NULL );
g_return_if_fail( row != NULL ); g_return_if_fail( row != NULL );
g_return_if_fail( r != NULL ); g_return_if_fail( r != NULL );
recurrence_info.be = be; recurrence_info.be = be;
recurrence_info.pRecurrence = r; recurrence_info.pRecurrence = r;
gnc_sql_load_object( be, row, TABLE_NAME, &recurrence_info, col_table ); gnc_sql_load_object( be, row, TABLE_NAME, &recurrence_info, col_table );
} }
@ -256,85 +270,93 @@ gnc_sql_set_recurrences_from_db( GncSqlBackend* be, const GncGUID* guid )
gchar* buf; gchar* buf;
gchar guid_buf[GUID_ENCODING_LENGTH+1]; gchar guid_buf[GUID_ENCODING_LENGTH+1];
GncSqlStatement* stmt; GncSqlStatement* stmt;
GncSqlResult* result; GncSqlResult* result;
g_return_val_if_fail( be != NULL, NULL ); g_return_val_if_fail( be != NULL, NULL );
g_return_val_if_fail( guid != NULL, NULL ); g_return_val_if_fail( guid != NULL, NULL );
(void)guid_to_string_buff( guid, guid_buf ); (void)guid_to_string_buff( guid, guid_buf );
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_connection_create_statement_from_sql( be->conn, buf ); stmt = gnc_sql_connection_create_statement_from_sql( be->conn, buf );
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 );
return result; return result;
} }
/*@ null @*/ Recurrence* /*@ null @*/ Recurrence*
gnc_sql_recurrence_load( GncSqlBackend* be, const GncGUID* guid ) gnc_sql_recurrence_load( GncSqlBackend* be, const GncGUID* guid )
{ {
GncSqlResult* result; GncSqlResult* result;
Recurrence* r = NULL; Recurrence* r = NULL;
g_return_val_if_fail( be != NULL, NULL ); g_return_val_if_fail( be != NULL, NULL );
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 ) { {
g_warning( "More than 1 recurrence found: first one used" ); if ( numRows > 1 )
} {
r = g_new0( Recurrence, 1 ); g_warning( "More than 1 recurrence found: first one used" );
g_assert( r != NULL ); }
load_recurrence( be, gnc_sql_result_get_first_row( result ), r ); r = g_new0( Recurrence, 1 );
} else { g_assert( r != NULL );
g_warning( "No recurrences found" ); load_recurrence( be, gnc_sql_result_get_first_row( result ), r );
} }
gnc_sql_result_dispose( result ); else
{
g_warning( "No recurrences found" );
}
gnc_sql_result_dispose( result );
} }
return r; return r;
} }
/*@ null @*/ GList* /*@ null @*/ GList*
gnc_sql_recurrence_load_list( GncSqlBackend* be, const GncGUID* guid ) gnc_sql_recurrence_load_list( GncSqlBackend* be, const GncGUID* guid )
{ {
GncSqlResult* result; GncSqlResult* result;
GList* list = NULL; GList* list = NULL;
g_return_val_if_fail( be != NULL, NULL ); g_return_val_if_fail( be != NULL, NULL );
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 ); {
g_assert( pRecurrence != NULL ); Recurrence* pRecurrence = g_new0( Recurrence, 1 );
load_recurrence( be, row, pRecurrence ); g_assert( pRecurrence != NULL );
list = g_list_append( list, pRecurrence ); load_recurrence( be, row, pRecurrence );
row = gnc_sql_result_get_next_row( result ); list = g_list_append( list, pRecurrence );
} row = gnc_sql_result_get_next_row( result );
gnc_sql_result_dispose( result ); }
gnc_sql_result_dispose( result );
} }
return list; return list;
} }
/* ================================================================= */ /* ================================================================= */
static void static void
create_recurrence_tables( GncSqlBackend* be ) create_recurrence_tables( GncSqlBackend* be )
{ {
gint version; gint version;
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 );
} }
} }
@ -350,10 +372,10 @@ gnc_sql_init_recurrence_handler( void )
NULL, /* commit - cannot occur */ NULL, /* commit - cannot occur */
NULL, /* initial_load - cannot occur */ NULL, /* initial_load - cannot occur */
create_recurrence_tables, /* create_tables */ create_recurrence_tables, /* create_tables */
NULL, /* compile_query */ NULL, /* compile_query */
NULL, /* run_query */ NULL, /* run_query */
NULL, /* free_query */ NULL, /* free_query */
NULL /* write */ NULL /* write */
}; };
(void)qof_object_register_backend( TABLE_NAME, GNC_SQL_BACKEND, &be_data ); (void)qof_object_register_backend( TABLE_NAME, GNC_SQL_BACKEND, &be_data );

View File

@ -19,7 +19,7 @@
* Boston, MA 02110-1301, USA gnu@gnu.org * * Boston, MA 02110-1301, USA gnu@gnu.org *
\********************************************************************/ \********************************************************************/
/** @file gnc-schedxaction-sql.c /** @file gnc-schedxaction-sql.c
* @brief load and save data to SQL * @brief load and save data to SQL
* @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff@rogers.com> * @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff@rogers.com>
* *
* This file implements the top-level QofBackend API for saving/ * This file implements the top-level QofBackend API for saving/
@ -57,10 +57,10 @@
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" },
{ "name", CT_STRING, SX_MAX_NAME_LEN, 0, "name" }, { "name", CT_STRING, SX_MAX_NAME_LEN, 0, "name" },
{ "enabled", CT_BOOLEAN, 0, COL_NNUL, "enabled" }, { "enabled", CT_BOOLEAN, 0, COL_NNUL, "enabled" },
{ "start_date", CT_GDATE, 0, 0, "start-date" }, { "start_date", CT_GDATE, 0, 0, "start-date" },
{ "end_date", CT_GDATE, 0, 0, "end-date" }, { "end_date", CT_GDATE, 0, 0, "end-date" },
{ "last_occur", CT_GDATE, 0, 0, "last-occurance-date" }, { "last_occur", CT_GDATE, 0, 0, "last-occurance-date" },
@ -70,10 +70,10 @@ static const GncSqlColumnTableEntry col_table[] =
{ "auto_notify", CT_BOOLEAN, 0, COL_NNUL, "auto-create-notify" }, { "auto_notify", CT_BOOLEAN, 0, COL_NNUL, "auto-create-notify" },
{ "adv_creation", CT_INT, 0, COL_NNUL, "advance-creation-days" }, { "adv_creation", CT_INT, 0, COL_NNUL, "advance-creation-days" },
{ "adv_notify", CT_INT, 0, COL_NNUL, "advance-reminder-days" }, { "adv_notify", CT_INT, 0, COL_NNUL, "advance-reminder-days" },
{ "instance_count", CT_INT, 0, COL_NNUL, "instance-count" }, { "instance_count", CT_INT, 0, COL_NNUL, "instance-count" },
{ "template_act_guid", CT_ACCOUNTREF, 0, COL_NNUL, "template-account" }, { "template_act_guid", CT_ACCOUNTREF, 0, COL_NNUL, "template-account" },
{ NULL } { NULL }
/*@ +full_init_block @*/ /*@ +full_init_block @*/
}; };
/* ================================================================= */ /* ================================================================= */
@ -81,23 +81,23 @@ static /*@ null @*/ SchedXaction*
load_single_sx( GncSqlBackend* be, GncSqlRow* row ) load_single_sx( GncSqlBackend* be, GncSqlRow* row )
{ {
const GncGUID* guid; const GncGUID* guid;
SchedXaction* pSx; SchedXaction* pSx;
GList* schedule; GList* schedule;
GDate start_date; GDate start_date;
g_return_val_if_fail( be != NULL, NULL ); g_return_val_if_fail( be != NULL, NULL );
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 );
g_assert( guid != NULL ); g_assert( guid != NULL );
pSx = xaccSchedXactionMalloc( be->primary_book ); pSx = xaccSchedXactionMalloc( be->primary_book );
gnc_sx_begin_edit( pSx ); gnc_sx_begin_edit( pSx );
gnc_sql_load_object( be, row, GNC_SX_ID, pSx, col_table ); gnc_sql_load_object( be, row, GNC_SX_ID, pSx, col_table );
schedule = gnc_sql_recurrence_load_list( be, guid ); schedule = gnc_sql_recurrence_load_list( be, guid );
gnc_sx_set_schedule( pSx, schedule ); gnc_sx_set_schedule( pSx, schedule );
gnc_sx_commit_edit( pSx ); gnc_sx_commit_edit( pSx );
gnc_sql_transaction_load_tx_for_account( be, pSx->template_acct ); gnc_sql_transaction_load_tx_for_account( be, pSx->template_acct );
g_object_get(pSx, "start-date", &start_date, NULL); g_object_get(pSx, "start-date", &start_date, NULL);
@ -110,35 +110,39 @@ load_all_sxes( GncSqlBackend* be )
GncSqlStatement* stmt = NULL; GncSqlStatement* stmt = NULL;
GncSqlResult* result; GncSqlResult* result;
g_return_if_fail( be != NULL ); g_return_if_fail( be != NULL );
stmt = gnc_sql_create_select_statement( be, SCHEDXACTION_TABLE ); stmt = gnc_sql_create_select_statement( be, SCHEDXACTION_TABLE );
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; {
SchedXactions *sxes; GncSqlRow* row;
GList* list = NULL; SchedXactions *sxes;
sxes = gnc_book_get_schedxactions( be->primary_book ); GList* list = NULL;
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 );
if( sx != NULL ) {
gnc_sxes_add_sx( sxes, sx );
list = g_list_prepend( list, sx );
}
row = gnc_sql_result_get_next_row( result );
}
gnc_sql_result_dispose( result );
if( list != NULL ) { sx = load_single_sx( be, row );
gnc_sql_slots_load_for_list( be, list ); if ( sx != NULL )
g_list_free( list ); {
} gnc_sxes_add_sx( sxes, sx );
list = g_list_prepend( list, sx );
}
row = gnc_sql_result_get_next_row( result );
}
gnc_sql_result_dispose( result );
if ( list != NULL )
{
gnc_sql_slots_load_for_list( be, list );
g_list_free( list );
}
} }
} }
@ -146,12 +150,13 @@ load_all_sxes( GncSqlBackend* be )
static void static void
create_sx_tables( GncSqlBackend* be ) create_sx_tables( GncSqlBackend* be )
{ {
gint version; gint version;
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 );
} }
} }
@ -162,42 +167,54 @@ gnc_sql_save_schedxaction( GncSqlBackend* be, QofInstance* inst )
{ {
SchedXaction* pSx; SchedXaction* pSx;
const GncGUID* guid; const GncGUID* guid;
gint op; gint op;
gboolean is_infant; gboolean is_infant;
gboolean is_ok; gboolean is_ok;
g_return_val_if_fail( be != NULL, FALSE ); g_return_val_if_fail( be != NULL, FALSE );
g_return_val_if_fail( inst != NULL, FALSE ); g_return_val_if_fail( inst != NULL, FALSE );
g_return_val_if_fail( GNC_IS_SX(inst), FALSE ); g_return_val_if_fail( GNC_IS_SX(inst), FALSE );
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; {
} else if( be->is_pristine_db || is_infant ) { op = OP_DB_DELETE;
op = OP_DB_INSERT; }
} else { else if ( be->is_pristine_db || is_infant )
op = OP_DB_UPDATE; {
} op = OP_DB_INSERT;
}
else
{
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 ) ); {
} else { gnc_sql_recurrence_save_list( be, guid, gnc_sx_get_schedule( pSx ) );
gnc_sql_recurrence_delete( be, guid ); }
} else
{
gnc_sql_recurrence_delete( be, guid );
}
if( is_ok ) { if ( is_ok )
// Now, commit any slots {
if( op == OP_DB_INSERT || op == OP_DB_UPDATE ) { // Now, commit any slots
is_ok = gnc_sql_slots_save( be, guid, is_infant, qof_instance_get_slots( inst ) ); if ( op == OP_DB_INSERT || op == OP_DB_UPDATE )
} else { {
is_ok = gnc_sql_slots_delete( be, guid ); is_ok = gnc_sql_slots_save( be, guid, is_infant, qof_instance_get_slots( inst ) );
} }
} else
{
is_ok = gnc_sql_slots_delete( be, guid );
}
}
return is_ok; return is_ok;
} }
/* ================================================================= */ /* ================================================================= */
@ -211,10 +228,10 @@ gnc_sql_init_schedxaction_handler( void )
gnc_sql_save_schedxaction, /* commit */ gnc_sql_save_schedxaction, /* commit */
load_all_sxes, /* initial_load */ load_all_sxes, /* initial_load */
create_sx_tables, /* create_tables */ create_sx_tables, /* create_tables */
NULL, /* compile_query */ NULL, /* compile_query */
NULL, /* run_query */ NULL, /* run_query */
NULL, /* free_query */ NULL, /* free_query */
NULL /* write */ NULL /* write */
}; };
(void)qof_object_register_backend( GNC_ID_SCHEDXACTION, GNC_SQL_BACKEND, &be_data ); (void)qof_object_register_backend( GNC_ID_SCHEDXACTION, GNC_SQL_BACKEND, &be_data );

View File

@ -19,7 +19,7 @@
* Boston, MA 02110-1301, USA gnu@gnu.org * * Boston, MA 02110-1301, USA gnu@gnu.org *
\********************************************************************/ \********************************************************************/
/** @file gnc-slots-sql.c /** @file gnc-slots-sql.c
* @brief load and save data to SQL * @brief load and save data to SQL
* @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff@rogers.com> * @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff@rogers.com>
* *
* This file implements the top-level QofBackend API for saving/ * This file implements the top-level QofBackend API for saving/
@ -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 @*/
gboolean is_ok; const GncGUID* guid;
/*@ dependent @*/ KvpFrame* pKvpFrame; gboolean is_ok;
/*@ 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;
@ -80,38 +84,56 @@ static void set_numeric_val( gpointer pObject, gnc_numeric value );
static const GncSqlColumnTableEntry col_table[] = 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 @*/
}; };
/* Special column table because we need to be able to access the table by /* Special column table because we need to be able to access the table by
a column other than the primary key */ a column other than the primary key */
static const GncSqlColumnTableEntry obj_guid_col_table[] = static const GncSqlColumnTableEntry obj_guid_col_table[] =
{ {
/*@ -full_init_block @*/ /*@ -full_init_block @*/
{ "obj_guid", CT_GUID, 0, 0, NULL, NULL, (QofAccessFunc)get_obj_guid, _retrieve_guid_ }, { "obj_guid", CT_GUID, 0, 0, NULL, NULL, (QofAccessFunc)get_obj_guid, _retrieve_guid_ },
{ NULL } { NULL }
/*@ +full_init_block @*/ /*@ +full_init_block @*/
}; };
/* ================================================================= */ /* ================================================================= */
@ -121,7 +143,7 @@ get_obj_guid( gpointer pObject )
{ {
slot_info_t* pInfo = (slot_info_t*)pObject; slot_info_t* pInfo = (slot_info_t*)pObject;
g_return_val_if_fail( pObject != NULL, NULL ); g_return_val_if_fail( pObject != NULL, NULL );
return (gpointer)pInfo->guid; return (gpointer)pInfo->guid;
} }
@ -137,7 +159,7 @@ get_path( gpointer pObject )
{ {
slot_info_t* pInfo = (slot_info_t*)pObject; slot_info_t* pInfo = (slot_info_t*)pObject;
g_return_val_if_fail( pObject != NULL, NULL ); g_return_val_if_fail( pObject != NULL, NULL );
return (gpointer)pInfo->path->str; return (gpointer)pInfo->path->str;
} }
@ -147,12 +169,13 @@ set_path( gpointer pObject, /*@ null @*/ gpointer pValue )
{ {
slot_info_t* pInfo = (slot_info_t*)pObject; slot_info_t* pInfo = (slot_info_t*)pObject;
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 );
} }
@ -161,7 +184,7 @@ get_slot_type( gpointer pObject )
{ {
slot_info_t* pInfo = (slot_info_t*)pObject; slot_info_t* pInfo = (slot_info_t*)pObject;
g_return_val_if_fail( pObject != NULL, NULL ); g_return_val_if_fail( pObject != NULL, NULL );
return (gpointer)kvp_value_get_type( pInfo->pKvpValue ); return (gpointer)kvp_value_get_type( pInfo->pKvpValue );
} }
@ -171,8 +194,8 @@ set_slot_type( gpointer pObject, /*@ null @*/ gpointer pValue )
{ {
slot_info_t* pInfo = (slot_info_t*)pObject; slot_info_t* pInfo = (slot_info_t*)pObject;
g_return_if_fail( pObject != NULL ); g_return_if_fail( pObject != NULL );
g_return_if_fail( pValue != NULL ); g_return_if_fail( pValue != NULL );
pInfo->value_type = (KvpValueType)pValue; pInfo->value_type = (KvpValueType)pValue;
} }
@ -182,11 +205,14 @@ get_int64_val( gpointer pObject )
{ {
slot_info_t* pInfo = (slot_info_t*)pObject; slot_info_t* pInfo = (slot_info_t*)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;
} }
} }
@ -196,9 +222,10 @@ set_int64_val( gpointer pObject, gint64 value )
{ {
slot_info_t* pInfo = (slot_info_t*)pObject; slot_info_t* pInfo = (slot_info_t*)pObject;
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 );
} }
} }
@ -208,11 +235,14 @@ get_string_val( gpointer pObject )
{ {
slot_info_t* pInfo = (slot_info_t*)pObject; slot_info_t* pInfo = (slot_info_t*)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;
} }
} }
@ -222,9 +252,10 @@ set_string_val( gpointer pObject, /*@ null @*/ gpointer pValue )
{ {
slot_info_t* pInfo = (slot_info_t*)pObject; slot_info_t* pInfo = (slot_info_t*)pObject;
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 );
} }
} }
@ -235,12 +266,15 @@ get_double_val( gpointer pObject )
slot_info_t* pInfo = (slot_info_t*)pObject; slot_info_t* pInfo = (slot_info_t*)pObject;
static double d_val; static double d_val;
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;
} }
} }
@ -250,9 +284,10 @@ set_double_val( gpointer pObject, /*@ null @*/ gpointer pValue )
{ {
slot_info_t* pInfo = (slot_info_t*)pObject; slot_info_t* pInfo = (slot_info_t*)pObject;
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 );
} }
} }
@ -262,7 +297,7 @@ get_timespec_val( gpointer pObject )
{ {
slot_info_t* pInfo = (slot_info_t*)pObject; slot_info_t* pInfo = (slot_info_t*)pObject;
g_return_val_if_fail( pObject != NULL, gnc_dmy2timespec( 1, 1, 1970 ) ); g_return_val_if_fail( pObject != NULL, gnc_dmy2timespec( 1, 1, 1970 ) );
//if( kvp_value_get_type( pInfo->pKvpValue ) == KVP_TYPE_TIMESPEC ) { //if( kvp_value_get_type( pInfo->pKvpValue ) == KVP_TYPE_TIMESPEC ) {
return kvp_value_get_timespec( pInfo->pKvpValue ); return kvp_value_get_timespec( pInfo->pKvpValue );
@ -273,9 +308,10 @@ set_timespec_val( gpointer pObject, Timespec ts )
{ {
slot_info_t* pInfo = (slot_info_t*)pObject; slot_info_t* pInfo = (slot_info_t*)pObject;
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 );
} }
} }
@ -285,11 +321,14 @@ get_guid_val( gpointer pObject )
{ {
slot_info_t* pInfo = (slot_info_t*)pObject; slot_info_t* pInfo = (slot_info_t*)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;
} }
} }
@ -299,9 +338,10 @@ set_guid_val( gpointer pObject, /*@ null @*/ gpointer pValue )
{ {
slot_info_t* pInfo = (slot_info_t*)pObject; slot_info_t* pInfo = (slot_info_t*)pObject;
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 );
} }
} }
@ -311,11 +351,14 @@ get_numeric_val( gpointer pObject )
{ {
slot_info_t* pInfo = (slot_info_t*)pObject; slot_info_t* pInfo = (slot_info_t*)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();
} }
} }
@ -325,9 +368,10 @@ set_numeric_val( gpointer pObject, gnc_numeric value )
{ {
slot_info_t* pInfo = (slot_info_t*)pObject; slot_info_t* pInfo = (slot_info_t*)pObject;
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 );
} }
} }
@ -338,30 +382,35 @@ save_slot( const gchar* key, KvpValue* value, gpointer data )
slot_info_t* pSlot_info = (slot_info_t*)data; slot_info_t* pSlot_info = (slot_info_t*)data;
gsize curlen; gsize curlen;
g_return_if_fail( key != NULL ); g_return_if_fail( key != NULL );
g_return_if_fail( value != NULL ); g_return_if_fail( value != NULL );
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,
col_table ); col_table );
} }
(void)g_string_truncate( pSlot_info->path, curlen ); (void)g_string_truncate( pSlot_info->path, curlen );
@ -372,23 +421,24 @@ gnc_sql_slots_save( GncSqlBackend* be, const GncGUID* guid, gboolean is_infant,
{ {
slot_info_t slot_info; slot_info_t slot_info;
g_return_val_if_fail( be != NULL, FALSE ); g_return_val_if_fail( be != NULL, FALSE );
g_return_val_if_fail( guid != NULL, FALSE ); g_return_val_if_fail( guid != NULL, FALSE );
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 );
}
slot_info.be = be; slot_info.be = be;
slot_info.guid = guid; slot_info.guid = guid;
slot_info.path = g_string_new( "" ); slot_info.path = g_string_new( "" );
slot_info.is_ok = TRUE; slot_info.is_ok = TRUE;
kvp_frame_for_each_slot( pFrame, save_slot, &slot_info ); kvp_frame_for_each_slot( pFrame, save_slot, &slot_info );
(void)g_string_free( slot_info.path, TRUE ); (void)g_string_free( slot_info.path, TRUE );
return slot_info.is_ok; return slot_info.is_ok;
} }
gboolean gboolean
@ -396,16 +446,16 @@ gnc_sql_slots_delete( GncSqlBackend* be, const GncGUID* guid )
{ {
slot_info_t slot_info; slot_info_t slot_info;
g_return_val_if_fail( be != NULL, FALSE ); g_return_val_if_fail( be != NULL, FALSE );
g_return_val_if_fail( guid != NULL, FALSE ); g_return_val_if_fail( guid != NULL, FALSE );
slot_info.be = be; slot_info.be = be;
slot_info.guid = guid; slot_info.guid = guid;
slot_info.is_ok = TRUE; slot_info.is_ok = TRUE;
slot_info.is_ok = gnc_sql_do_db_operation( be, OP_DB_DELETE, TABLE_NAME, slot_info.is_ok = gnc_sql_do_db_operation( be, OP_DB_DELETE, TABLE_NAME,
TABLE_NAME, &slot_info, obj_guid_col_table ); TABLE_NAME, &slot_info, obj_guid_col_table );
return slot_info.is_ok; return slot_info.is_ok;
} }
static void static void
@ -413,9 +463,9 @@ load_slot( GncSqlBackend* be, GncSqlRow* row, KvpFrame* pFrame )
{ {
slot_info_t slot_info; slot_info_t slot_info;
g_return_if_fail( be != NULL ); g_return_if_fail( be != NULL );
g_return_if_fail( row != NULL ); g_return_if_fail( row != NULL );
g_return_if_fail( pFrame != NULL ); g_return_if_fail( pFrame != NULL );
slot_info.be = be; slot_info.be = be;
slot_info.pKvpFrame = pFrame; slot_info.pKvpFrame = pFrame;
@ -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 );
} }
} }
@ -435,32 +486,35 @@ gnc_sql_slots_load( GncSqlBackend* be, QofInstance* inst )
GncSqlResult* result; GncSqlResult* result;
gchar guid_buf[GUID_ENCODING_LENGTH+1]; gchar guid_buf[GUID_ENCODING_LENGTH+1];
GncSqlStatement* stmt; GncSqlStatement* stmt;
const GncGUID* guid; const GncGUID* guid;
KvpFrame* pFrame; KvpFrame* pFrame;
g_return_if_fail( be != NULL ); g_return_if_fail( be != NULL );
g_return_if_fail( inst != NULL ); g_return_if_fail( inst != NULL );
guid = qof_instance_get_guid( inst ); guid = qof_instance_get_guid( inst );
pFrame = qof_instance_get_slots( inst ); pFrame = qof_instance_get_slots( inst );
(void)guid_to_string_buff( guid, guid_buf ); (void)guid_to_string_buff( guid, guid_buf );
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 ); {
gnc_sql_statement_dispose( stmt ); result = gnc_sql_execute_select_statement( be, stmt );
if( result != NULL ) { gnc_sql_statement_dispose( stmt );
GncSqlRow* row = gnc_sql_result_get_first_row( result ); if ( result != NULL )
{
GncSqlRow* row = gnc_sql_result_get_first_row( result );
while( row != NULL ) { while ( row != NULL )
load_slot( be, row, pFrame ); {
row = gnc_sql_result_get_next_row( result ); load_slot( be, row, pFrame );
} row = gnc_sql_result_get_next_row( result );
gnc_sql_result_dispose( result ); }
} gnc_sql_result_dispose( result );
} }
}
} }
static /*@ dependent @*//*@ null @*/ const GncGUID* static /*@ dependent @*//*@ null @*/ const GncGUID*
@ -468,8 +522,8 @@ load_obj_guid( const GncSqlBackend* be, GncSqlRow* row )
{ {
static GncGUID guid; static GncGUID guid;
g_return_val_if_fail( be != NULL, NULL ); g_return_val_if_fail( be != NULL, NULL );
g_return_val_if_fail( row != NULL, NULL ); g_return_val_if_fail( row != NULL, NULL );
gnc_sql_load_object( be, row, NULL, &guid, obj_guid_col_table ); gnc_sql_load_object( be, row, NULL, &guid, obj_guid_col_table );
@ -480,16 +534,16 @@ static void
load_slot_for_list_item( GncSqlBackend* be, GncSqlRow* row, QofCollection* coll ) load_slot_for_list_item( GncSqlBackend* be, GncSqlRow* row, QofCollection* coll )
{ {
slot_info_t slot_info; slot_info_t slot_info;
const GncGUID* guid; const GncGUID* guid;
QofInstance* inst; QofInstance* inst;
g_return_if_fail( be != NULL ); g_return_if_fail( be != NULL );
g_return_if_fail( row != NULL ); g_return_if_fail( row != NULL );
g_return_if_fail( coll != NULL ); g_return_if_fail( coll != NULL );
guid = load_obj_guid( be, row ); guid = load_obj_guid( be, row );
g_assert( guid != NULL ); g_assert( guid != NULL );
inst = qof_collection_lookup_entity( coll, guid ); inst = qof_collection_lookup_entity( coll, guid );
slot_info.be = be; slot_info.be = be;
slot_info.pKvpFrame = qof_instance_get_slots( inst ); slot_info.pKvpFrame = qof_instance_get_slots( inst );
@ -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 );
} }
} }
@ -505,52 +560,59 @@ load_slot_for_list_item( GncSqlBackend* be, GncSqlRow* row, QofCollection* coll
void void
gnc_sql_slots_load_for_list( GncSqlBackend* be, GList* list ) gnc_sql_slots_load_for_list( GncSqlBackend* be, GList* list )
{ {
QofCollection* coll; QofCollection* coll;
GncSqlStatement* stmt; GncSqlStatement* stmt;
GString* sql; GString* sql;
GncSqlResult* result; GncSqlResult* result;
gboolean single_item; gboolean single_item;
g_return_if_fail( be != NULL ); g_return_if_fail( be != NULL );
// Ignore empty list // Ignore empty list
if( list == NULL ) return; if ( list == NULL ) return;
coll = qof_instance_get_collection( QOF_INSTANCE(list->data) ); coll = qof_instance_get_collection( QOF_INSTANCE(list->data) );
// 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 (" ); {
single_item = FALSE; (void)g_string_append( sql, "IN (" );
} else { single_item = FALSE;
(void)g_string_append( sql, "= " ); }
single_item = TRUE; else
} {
(void)gnc_sql_append_guid_list_to_sql( sql, list, G_MAXUINT ); (void)g_string_append( sql, "= " );
if( !single_item ) { single_item = TRUE;
(void)g_string_append( sql, ")" ); }
} (void)gnc_sql_append_guid_list_to_sql( sql, list, G_MAXUINT );
if ( !single_item )
{
(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 ); {
(void)g_string_free( sql, TRUE ); PERR( "stmt == NULL, SQL = '%s'\n", sql->str );
return; (void)g_string_free( sql, TRUE );
} return;
(void)g_string_free( sql, TRUE ); }
result = gnc_sql_execute_select_statement( be, stmt ); (void)g_string_free( sql, TRUE );
gnc_sql_statement_dispose( stmt ); result = gnc_sql_execute_select_statement( be, stmt );
if( result != NULL ) { gnc_sql_statement_dispose( stmt );
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 );
} }
gnc_sql_result_dispose( result ); gnc_sql_result_dispose( result );
} }
} }
@ -558,17 +620,17 @@ static void
load_slot_for_book_object( GncSqlBackend* be, GncSqlRow* row, BookLookupFn lookup_fn ) load_slot_for_book_object( GncSqlBackend* be, GncSqlRow* row, BookLookupFn lookup_fn )
{ {
slot_info_t slot_info; slot_info_t slot_info;
const GncGUID* guid; const GncGUID* guid;
QofInstance* inst; QofInstance* inst;
g_return_if_fail( be != NULL ); g_return_if_fail( be != NULL );
g_return_if_fail( row != NULL ); g_return_if_fail( row != NULL );
g_return_if_fail( lookup_fn != NULL ); g_return_if_fail( lookup_fn != NULL );
guid = load_obj_guid( be, row ); guid = load_obj_guid( be, row );
g_return_if_fail( guid != NULL ); g_return_if_fail( guid != NULL );
inst = lookup_fn( guid, be->primary_book ); inst = lookup_fn( guid, be->primary_book );
g_return_if_fail( inst != NULL ); g_return_if_fail( inst != NULL );
slot_info.be = be; slot_info.be = be;
slot_info.pKvpFrame = qof_instance_get_slots( inst ); slot_info.pKvpFrame = qof_instance_get_slots( inst );
@ -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 );
} }
} }
@ -591,39 +654,42 @@ load_slot_for_book_object( GncSqlBackend* be, GncSqlRow* row, BookLookupFn looku
* @param lookup_fn Lookup function * @param lookup_fn Lookup function
*/ */
void gnc_sql_slots_load_for_sql_subquery( GncSqlBackend* be, const gchar* subquery, void gnc_sql_slots_load_for_sql_subquery( GncSqlBackend* be, const gchar* subquery,
BookLookupFn lookup_fn ) BookLookupFn lookup_fn )
{ {
gchar* sql; gchar* sql;
GncSqlStatement* stmt; GncSqlStatement* stmt;
GncSqlResult* result; GncSqlResult* result;
g_return_if_fail( be != NULL ); g_return_if_fail( be != NULL );
// Ignore empty subquery // Ignore empty subquery
if( subquery == NULL ) return; if ( subquery == NULL ) return;
sql = g_strdup_printf( "SELECT * FROM %s WHERE %s IN (%s)", sql = g_strdup_printf( "SELECT * FROM %s WHERE %s IN (%s)",
TABLE_NAME, obj_guid_col_table[0].col_name, TABLE_NAME, obj_guid_col_table[0].col_name,
subquery ); subquery );
// 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 ); {
g_free( sql ); PERR( "stmt == NULL, SQL = '%s'\n", sql );
return; g_free( sql );
} return;
g_free( sql ); }
result = gnc_sql_execute_select_statement( be, stmt ); g_free( sql );
gnc_sql_statement_dispose( stmt ); result = gnc_sql_execute_select_statement( be, stmt );
if( result != NULL ) { gnc_sql_statement_dispose( stmt );
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 );
} }
gnc_sql_result_dispose( result ); gnc_sql_result_dispose( result );
} }
} }
@ -631,28 +697,33 @@ void gnc_sql_slots_load_for_sql_subquery( GncSqlBackend* be, const gchar* subque
static void static void
create_slots_tables( GncSqlBackend* be ) create_slots_tables( GncSqlBackend* be )
{ {
gint version; gint version;
gboolean ok; gboolean ok;
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 ) { }
/* Upgrade 64-bit int values to proper definition */ }
gnc_sql_upgrade_table( be, TABLE_NAME, col_table ); else if ( version == 1 )
ok = gnc_sql_create_index( be, "slots_guid_index", TABLE_NAME, obj_guid_col_table ); {
if( !ok ) { /* Upgrade 64-bit int values to proper definition */
PERR( "Unable to create index\n" ); gnc_sql_upgrade_table( be, TABLE_NAME, col_table );
} ok = gnc_sql_create_index( be, "slots_guid_index", TABLE_NAME, obj_guid_col_table );
(void)gnc_sql_set_table_version( be, TABLE_NAME, TABLE_VERSION ); if ( !ok )
} {
PERR( "Unable to create index\n" );
}
(void)gnc_sql_set_table_version( be, TABLE_NAME, TABLE_VERSION );
}
} }
/* ================================================================= */ /* ================================================================= */
@ -666,10 +737,10 @@ gnc_sql_init_slots_handler( void )
NULL, /* commit - cannot occur */ NULL, /* commit - cannot occur */
NULL, /* initial_load - cannot occur */ NULL, /* initial_load - cannot occur */
create_slots_tables, /* create_tables */ create_slots_tables, /* create_tables */
NULL, /* compile_query */ NULL, /* compile_query */
NULL, /* run_query */ NULL, /* run_query */
NULL, /* free_query */ NULL, /* free_query */
NULL /* write */ NULL /* write */
}; };
(void)qof_object_register_backend( TABLE_NAME, GNC_SQL_BACKEND, &be_data ); (void)qof_object_register_backend( TABLE_NAME, GNC_SQL_BACKEND, &be_data );

File diff suppressed because it is too large Load Diff

View File

@ -95,9 +95,10 @@ static GncSqlColumnTableEntry billterm_parent_col_table[] =
{ NULL } { NULL }
}; };
typedef struct { typedef struct
/*@ dependent @*/ GncBillTerm* billterm; {
GncGUID guid; /*@ dependent @*/ GncBillTerm* billterm;
GncGUID guid;
gboolean have_guid; gboolean have_guid;
} billterm_parent_guid_struct; } billterm_parent_guid_struct;
@ -121,14 +122,17 @@ bt_get_parent( gpointer pObject )
const GncBillTerm* pParent; const GncBillTerm* pParent;
const GncGUID* parent_guid; const GncGUID* parent_guid;
g_return_val_if_fail( pObject != NULL, NULL ); g_return_val_if_fail( pObject != NULL, NULL );
g_return_val_if_fail( GNC_IS_BILLTERM(pObject), NULL ); g_return_val_if_fail( GNC_IS_BILLTERM(pObject), NULL );
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 );
} }
@ -161,19 +166,19 @@ bt_set_parent( gpointer data, gpointer value )
static void static void
bt_set_parent_guid( gpointer pObject, /*@ null @*/ gpointer pValue ) bt_set_parent_guid( gpointer pObject, /*@ null @*/ gpointer pValue )
{ {
billterm_parent_guid_struct* s = (billterm_parent_guid_struct*)pObject; billterm_parent_guid_struct* s = (billterm_parent_guid_struct*)pObject;
GncGUID* guid = (GncGUID*)pValue; GncGUID* guid = (GncGUID*)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 );
s->guid = *guid; s->guid = *guid;
s->have_guid = TRUE; s->have_guid = TRUE;
} }
static GncBillTerm* static GncBillTerm*
load_single_billterm( GncSqlBackend* be, GncSqlRow* row, load_single_billterm( GncSqlBackend* be, GncSqlRow* row,
GList** l_billterms_needing_parents ) GList** l_billterms_needing_parents )
{ {
const GncGUID* guid; const GncGUID* guid;
GncBillTerm* pBillTerm; GncBillTerm* pBillTerm;
@ -194,15 +199,15 @@ load_single_billterm( GncSqlBackend* be, GncSqlRow* row,
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 ( gncBillTermGetParent( pBillTerm ) == NULL ) if ( gncBillTermGetParent( pBillTerm ) == NULL )
{ {
billterm_parent_guid_struct* s = g_malloc( (gsize)sizeof(billterm_parent_guid_struct) ); billterm_parent_guid_struct* s = g_malloc( (gsize)sizeof(billterm_parent_guid_struct) );
g_assert( s != NULL ); g_assert( s != NULL );
s->billterm = pBillTerm; s->billterm = pBillTerm;
s->have_guid = FALSE; s->have_guid = FALSE;
gnc_sql_load_object( be, row, GNC_ID_TAXTABLE, s, billterm_parent_col_table ); gnc_sql_load_object( be, row, GNC_ID_TAXTABLE, s, billterm_parent_col_table );
if ( s->have_guid ) if ( s->have_guid )
{ {
*l_billterms_needing_parents = g_list_prepend( *l_billterms_needing_parents, s ); *l_billterms_needing_parents = g_list_prepend( *l_billterms_needing_parents, s );
} }
else else
{ {
@ -252,26 +257,29 @@ load_all_billterms( GncSqlBackend* be )
gnc_sql_slots_load_for_list( be, list ); gnc_sql_slots_load_for_list( be, list );
} }
/* While there are items on the list of billterms needing parents, /* While there are items on the list of billterms needing parents,
try to see if the parent has now been loaded. Theory says that if try to see if the parent has now been loaded. Theory says that if
items are removed from the front and added to the back if the 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; {
GncTaxTable* root; gboolean progress_made = TRUE;
Account* pParent; GncTaxTable* root;
GList* elem; Account* pParent;
GList* elem;
while( progress_made ) {
progress_made = FALSE; while ( progress_made )
for( elem = l_billterms_needing_parents; elem != NULL; elem = g_list_next( elem ) ) { {
billterm_parent_guid_struct* s = (billterm_parent_guid_struct*)elem->data; progress_made = FALSE;
for ( elem = l_billterms_needing_parents; elem != NULL; elem = g_list_next( elem ) )
{
billterm_parent_guid_struct* s = (billterm_parent_guid_struct*)elem->data;
bt_set_parent( s->billterm, &s->guid ); 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 );
progress_made = TRUE; progress_made = TRUE;
} }
} }
} }
} }
} }

View File

@ -121,9 +121,10 @@ static GncSqlColumnTableEntry guid_col_table[] =
{ NULL } { NULL }
}; };
typedef struct { typedef struct
/*@ dependent @*/ GncTaxTable* tt; {
GncGUID guid; /*@ dependent @*/ GncTaxTable* tt;
GncGUID guid;
gboolean have_guid; gboolean have_guid;
} taxtable_parent_guid_struct; } taxtable_parent_guid_struct;
@ -175,14 +176,17 @@ bt_get_parent( gpointer pObject )
const GncTaxTable* pParent; const GncTaxTable* pParent;
const GncGUID* parent_guid; const GncGUID* parent_guid;
g_return_val_if_fail( pObject != NULL, NULL ); g_return_val_if_fail( pObject != NULL, NULL );
g_return_val_if_fail( GNC_IS_TAXTABLE(pObject), NULL ); g_return_val_if_fail( GNC_IS_TAXTABLE(pObject), NULL );
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 );
} }
@ -215,13 +220,13 @@ tt_set_parent( gpointer data, gpointer value )
static void static void
tt_set_parent_guid( gpointer pObject, /*@ null @*/ gpointer pValue ) tt_set_parent_guid( gpointer pObject, /*@ null @*/ gpointer pValue )
{ {
taxtable_parent_guid_struct* s = (taxtable_parent_guid_struct*)pObject; taxtable_parent_guid_struct* s = (taxtable_parent_guid_struct*)pObject;
GncGUID* guid = (GncGUID*)pValue; GncGUID* guid = (GncGUID*)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 );
s->guid = *guid; s->guid = *guid;
s->have_guid = TRUE; s->have_guid = TRUE;
} }
@ -276,7 +281,7 @@ load_taxtable_entries( GncSqlBackend* be, GncTaxTable* tt )
static void static void
load_single_taxtable( GncSqlBackend* be, GncSqlRow* row, load_single_taxtable( GncSqlBackend* be, GncSqlRow* row,
GList** l_tt_needing_parents ) GList** l_tt_needing_parents )
{ {
const GncGUID* guid; const GncGUID* guid;
GncTaxTable* tt; GncTaxTable* 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) ); {
g_assert( s != NULL ); taxtable_parent_guid_struct* s = g_malloc( (gsize)sizeof(taxtable_parent_guid_struct) );
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 ); {
} else { *l_tt_needing_parents = g_list_prepend( *l_tt_needing_parents, s );
}
else
{
g_free( s ); g_free( s );
} }
} }
@ -339,26 +348,29 @@ load_all_taxtables( GncSqlBackend* be )
} }
gnc_sql_result_dispose( result ); gnc_sql_result_dispose( result );
/* While there are items on the list of taxtables needing parents, /* While there are items on the list of taxtables needing parents,
try to see if the parent has now been loaded. Theory says that if try to see if the parent has now been loaded. Theory says that if
items are removed from the front and added to the back if the 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; {
GncTaxTable* root; gboolean progress_made = TRUE;
Account* pParent; GncTaxTable* root;
GList* elem; Account* pParent;
GList* elem;
while( progress_made ) {
progress_made = FALSE; while ( progress_made )
for( elem = tt_needing_parents; elem != NULL; elem = g_list_next( elem ) ) { {
taxtable_parent_guid_struct* s = (taxtable_parent_guid_struct*)elem->data; progress_made = FALSE;
for ( elem = tt_needing_parents; elem != NULL; elem = g_list_next( elem ) )
{
taxtable_parent_guid_struct* s = (taxtable_parent_guid_struct*)elem->data;
tt_set_parent( s->tt, &s->guid ); tt_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 );
progress_made = TRUE; progress_made = TRUE;
} }
} }
} }
} }
} }

View File

@ -66,7 +66,7 @@ create_session(void)
gnc_commodity* currency; gnc_commodity* currency;
GncAddress* addr; GncAddress* addr;
GncCustomer* cust; GncCustomer* cust;
table = gnc_commodity_table_get_table( book ); table = gnc_commodity_table_get_table( book );
currency = gnc_commodity_table_lookup( table, GNC_COMMODITY_NS_CURRENCY, "CAD" ); currency = gnc_commodity_table_lookup( table, GNC_COMMODITY_NS_CURRENCY, "CAD" );
@ -106,20 +106,20 @@ static const gchar* fixed_load_order[] =
static void static void
init_business_sql(void) init_business_sql(void)
{ {
/* Initialize our pointers into the backend subsystem */ /* Initialize our pointers into the backend subsystem */
gnc_address_sql_initialize(); gnc_address_sql_initialize();
gnc_billterm_sql_initialize(); gnc_billterm_sql_initialize();
gnc_customer_sql_initialize(); gnc_customer_sql_initialize();
gnc_employee_sql_initialize(); gnc_employee_sql_initialize();
gnc_entry_sql_initialize(); gnc_entry_sql_initialize();
gnc_invoice_sql_initialize(); gnc_invoice_sql_initialize();
gnc_job_sql_initialize(); gnc_job_sql_initialize();
gnc_order_sql_initialize(); gnc_order_sql_initialize();
gnc_owner_sql_initialize(); gnc_owner_sql_initialize();
gnc_taxtable_sql_initialize(); gnc_taxtable_sql_initialize();
gnc_vendor_sql_initialize(); gnc_vendor_sql_initialize();
gnc_sql_set_load_order( fixed_load_order ); gnc_sql_set_load_order( fixed_load_order );
} }
int main (int argc, char ** argv) int main (int argc, char ** argv)
@ -138,7 +138,7 @@ int main (int argc, char ** argv)
filename = tempnam( "/tmp", "test-sqlite3-" ); filename = tempnam( "/tmp", "test-sqlite3-" );
printf( "Using filename: %s\n", filename ); printf( "Using filename: %s\n", filename );
test_dbi_business_store_and_reload( "sqlite3", session_1, filename ); test_dbi_business_store_and_reload( "sqlite3", session_1, filename );
#if 0 #if 0
printf( "TEST_MYSQL_URL='%s'\n", TEST_MYSQL_URL ); printf( "TEST_MYSQL_URL='%s'\n", TEST_MYSQL_URL );
if ( strlen( TEST_MYSQL_URL ) > 0 ) if ( strlen( TEST_MYSQL_URL ) > 0 )
{ {
@ -151,7 +151,7 @@ int main (int argc, char ** argv)
session_1 = create_session(); session_1 = create_session();
test_dbi_store_and_reload( "pgsql", session_1, TEST_PGSQL_URL ); test_dbi_store_and_reload( "pgsql", session_1, TEST_PGSQL_URL );
} }
#endif #endif
print_test_results(); print_test_results();
qof_close(); qof_close();
exit(get_rv()); exit(get_rv());