41 #include "gncTaxTableP.h"
45 #define _GNC_MOD_NAME GNC_ID_TAXTABLE
55 static gpointer get_obj_guid( gpointer pObject,
const QofParam* param );
56 static void set_obj_guid( gpointer pObject, gpointer pValue );
57 static gpointer bt_get_parent( gpointer pObject );
58 static void tt_set_parent( gpointer pObject, gpointer pValue );
59 static void tt_set_parent_guid( gpointer pObject, gpointer pValue );
61 #define MAX_NAME_LEN 50
63 #define TT_TABLE_NAME "taxtables"
64 #define TT_TABLE_VERSION 2
69 {
"name", CT_STRING, MAX_NAME_LEN,
COL_NNUL,
"name" },
70 {
"refcount", CT_INT64, 0,
COL_NNUL,
"ref-count" },
71 {
"invisible", CT_BOOLEAN, 0,
COL_NNUL,
"invisible" },
75 "parent", CT_GUID, 0, 0, NULL, NULL,
83 {
"parent", CT_GUID, 0, 0, NULL, NULL, NULL, tt_set_parent_guid },
87 #define TTENTRIES_TABLE_NAME "taxtable_entries"
88 #define TTENTRIES_TABLE_VERSION 3
94 "taxtable", CT_TAXTABLEREF, 0,
COL_NNUL, NULL, NULL,
98 "account", CT_ACCOUNTREF, 0,
COL_NNUL, NULL, NULL,
102 "amount", CT_NUMERIC, 0,
COL_NNUL, NULL, NULL,
106 "type", CT_INT, 0,
COL_NNUL, NULL, NULL,
116 {
"taxtable", CT_GUID, 0, 0, NULL, NULL, get_obj_guid, set_obj_guid },
128 get_obj_guid( gpointer pObject,
const QofParam* param )
132 g_return_val_if_fail( pInfo != NULL, NULL );
134 return (gpointer)pInfo->guid;
138 set_obj_guid( gpointer pObject, gpointer pValue )
144 bt_get_parent( gpointer pObject )
150 g_return_val_if_fail( pObject != NULL, NULL );
151 g_return_val_if_fail( GNC_IS_TAXTABLE(pObject), NULL );
153 tt = GNC_TAXTABLE(pObject);
154 pParent = gncTaxTableGetParent( tt );
155 if ( pParent == NULL )
164 return (gpointer)parent_guid;
168 tt_set_parent( gpointer data, gpointer value )
175 g_return_if_fail( data != NULL );
176 g_return_if_fail( GNC_IS_TAXTABLE(data) );
178 tt = GNC_TAXTABLE(data);
182 parent = gncTaxTableLookup( pBook, guid );
183 if ( parent != NULL )
185 gncTaxTableSetParent( tt, parent );
186 gncTaxTableSetChild( parent, tt );
192 tt_set_parent_guid( gpointer pObject, gpointer pValue )
197 g_return_if_fail( pObject != NULL );
198 g_return_if_fail( pValue != NULL );
209 g_return_if_fail( be != NULL );
210 g_return_if_fail( row != NULL );
211 g_return_if_fail( tt != NULL );
214 gncTaxTableAddEntry( tt, e );
226 g_return_if_fail( be != NULL );
227 g_return_if_fail( tt != NULL );
230 memset( &value, 0,
sizeof( GValue ) );
231 g_value_init( &value, G_TYPE_STRING );
232 g_value_set_string( &value, guid_buf );
233 buf = g_strdup_printf(
"SELECT * FROM %s WHERE taxtable='%s'", TTENTRIES_TABLE_NAME, guid_buf );
234 stmt = gnc_sql_connection_create_statement_from_sql( be->
conn, buf );
237 gnc_sql_statement_dispose( stmt );
238 if ( result != NULL )
242 row = gnc_sql_result_get_first_row( result );
243 while ( row != NULL )
245 load_single_ttentry( be, row, tt );
246 row = gnc_sql_result_get_next_row( result );
248 gnc_sql_result_dispose( result );
254 GList** l_tt_needing_parents )
259 g_return_if_fail( be != NULL );
260 g_return_if_fail( row != NULL );
263 tt = gncTaxTableLookup( be->
book, guid );
266 tt = gncTaxTableCreate( be->
book );
270 load_taxtable_entries( be, tt );
275 if ( gncTaxTableGetParent( tt ) == NULL )
278 g_assert( s != NULL );
281 s->have_guid = FALSE;
285 *l_tt_needing_parents = g_list_prepend( *l_tt_needing_parents, s );
293 qof_instance_mark_clean( QOF_INSTANCE(tt) );
302 g_return_if_fail( be != NULL );
307 gnc_sql_statement_dispose( stmt );
308 if ( result != NULL )
311 GList* tt_needing_parents = NULL;
313 row = gnc_sql_result_get_first_row( result );
314 while ( row != NULL )
316 load_single_taxtable( be, row, &tt_needing_parents );
317 row = gnc_sql_result_get_next_row( result );
319 gnc_sql_result_dispose( result );
326 if ( tt_needing_parents != NULL )
328 gboolean progress_made = TRUE;
331 while ( progress_made )
333 progress_made = FALSE;
334 for ( elem = tt_needing_parents; elem != NULL; elem = g_list_next( elem ) )
337 tt_set_parent( s->tt, &s->guid );
338 tt_needing_parents = g_list_delete_link( tt_needing_parents, elem );
339 progress_made = TRUE;
352 g_return_if_fail( be != NULL );
359 else if ( version == 1 )
364 PINFO(
"Taxtables table upgraded from version 1 to version %d\n", TT_TABLE_VERSION);
372 else if ( version == 1 )
377 PINFO(
"Taxtable entries table upgraded from version 1 to version %d\n", TTENTRIES_TABLE_VERSION);
387 g_return_val_if_fail( be != NULL, FALSE );
388 g_return_val_if_fail( guid != NULL, FALSE );
391 guid_info.guid = guid;
393 TTENTRIES_TABLE_NAME, &guid_info, guid_col_table );
402 g_return_val_if_fail( be != NULL, FALSE );
403 g_return_val_if_fail( guid != NULL, FALSE );
406 is_ok = delete_all_tt_entries( be, guid );
408 for ( entry = entries; entry != NULL && is_ok; entry = entry->next )
413 TTENTRIES_TABLE_NAME,
415 ttentries_col_table );
430 g_return_val_if_fail( inst != NULL, FALSE );
431 g_return_val_if_fail( GNC_IS_TAXTABLE(inst), FALSE );
432 g_return_val_if_fail( be != NULL, FALSE );
434 tt = GNC_TAXTABLE(inst);
436 is_infant = qof_instance_get_infant( inst );
460 is_ok = save_tt_entries( be, guid, gncTaxTableGetEntries( tt ) );
468 is_ok = delete_all_tt_entries( be, guid );
478 save_next_taxtable(
QofInstance* inst, gpointer data )
484 s->is_ok = save_taxtable( s->be, inst );
493 g_return_val_if_fail( be != NULL, FALSE );
512 g_return_if_fail( be != NULL );
513 g_return_if_fail( row != NULL );
514 g_return_if_fail( pObject != NULL );
515 g_return_if_fail( table_row != NULL );
517 val = gnc_sql_row_get_value_at_col_name( row, table_row->
col_name );
518 if ( val != NULL && G_VALUE_HOLDS_STRING( val ) && g_value_get_string( val ) != NULL )
521 taxtable = gncTaxTableLookup( be->
book, &guid );
522 if ( taxtable != NULL )
526 qof_instance_increase_editlevel (pObject);
528 qof_instance_decrease_editlevel (pObject);
532 (*setter)( pObject, (
const gpointer)taxtable );
537 PWARN(
"Taxtable ref '%s' not found", g_value_get_string( val ) );
543 = { load_taxtable_guid,
550 gnc_taxtable_sql_initialize(
void )
554 GNC_SQL_BACKEND_VERSION,
558 create_taxtable_tables,
gboolean qof_object_register_backend(QofIdTypeConst type_name, const char *backend_name, gpointer be_data)
const GncGUID * qof_instance_get_guid(gconstpointer)
gint gnc_sql_get_table_version(const GncSqlBackend *be, const gchar *table_name)
QofBook * qof_instance_get_book(gconstpointer)
void gnc_sql_upgrade_table(GncSqlBackend *be, const gchar *table_name, const GncSqlColumnTableEntry *col_table)
#define G_LOG_DOMAIN
Functions providing the SX List as a plugin page.
#define PINFO(format, args...)
void gnc_sql_add_colname_to_list(const GncSqlColumnTableEntry *table_row, GList **pList)
load and save accounts data to SQL
GncSqlStatement * gnc_sql_create_select_statement(GncSqlBackend *be, const gchar *table_name)
gboolean qof_instance_get_destroying(gconstpointer ptr)
gboolean string_to_guid(const gchar *string, GncGUID *guid)
gboolean gnc_sql_create_table(GncSqlBackend *be, const gchar *table_name, gint table_version, const GncSqlColumnTableEntry *col_table)
load and save data to SQL
void gnc_sql_register_col_type_handler(const gchar *colType, const GncSqlColumnTypeHandler *handler)
void gnc_sql_add_objectref_guid_col_info_to_list(const GncSqlBackend *be, const GncSqlColumnTableEntry *table_row, GList **pList)
gchar * guid_to_string_buff(const GncGUID *guid, gchar *buff)
gpointer(* QofAccessFunc)(gpointer object, const QofParam *param)
#define PWARN(format, args...)
const GncGUID * gnc_sql_load_guid(const GncSqlBackend *be, GncSqlRow *row)
gboolean gnc_sql_set_table_version(GncSqlBackend *be, const gchar *table_name, gint version)
void gnc_sql_add_gvalue_objectref_guid_to_slist(const GncSqlBackend *be, QofIdTypeConst obj_name, const gpointer pObject, const GncSqlColumnTableEntry *table_row, GSList **pList)
#define GUID_ENCODING_LENGTH
void qof_object_foreach(QofIdTypeConst type_name, QofBook *book, QofInstanceForeachCB cb, gpointer user_data)
void gnc_sql_slots_load(GncSqlBackend *be, QofInstance *inst)
GncSqlResult * gnc_sql_execute_select_statement(GncSqlBackend *be, GncSqlStatement *stmt)
void gnc_sql_load_object(const GncSqlBackend *be, GncSqlRow *row, QofIdTypeConst obj_name, gpointer pObject, const GncSqlColumnTableEntry *table)
const gchar * gobj_param_name
gboolean gnc_sql_slots_delete(GncSqlBackend *be, const GncGUID *guid)
void(* QofSetterFunc)(gpointer, gpointer)
gboolean gnc_sql_do_db_operation(GncSqlBackend *be, E_DB_OPERATION op, const gchar *table_name, QofIdTypeConst obj_name, gpointer pObject, const GncSqlColumnTableEntry *table)
Business Entry Interface.
gboolean gnc_sql_slots_save(GncSqlBackend *be, const GncGUID *guid, gboolean is_infant, KvpFrame *pFrame)
const gchar * QofLogModule
load and save tax table data to SQL