GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Typedefs | Functions
gnc-slots-sql.h File Reference

load and save accounts data to SQL More...

#include <glib.h>
#include "guid.h"
#include "kvp_frame.h"
#include "qof.h"
#include "gnc-backend-sql.h"

Go to the source code of this file.

Typedefs

typedef QofInstance *(* BookLookupFn )(const GncGUID *guid, const QofBook *book)
 

Functions

gboolean gnc_sql_slots_save (GncSqlBackend *be, const GncGUID *guid, gboolean is_infant, KvpFrame *pFrame)
 
gboolean gnc_sql_slots_delete (GncSqlBackend *be, const GncGUID *guid)
 
void gnc_sql_slots_load (GncSqlBackend *be, QofInstance *inst)
 
void gnc_sql_slots_load_for_list (GncSqlBackend *be, GList *list)
 
void gnc_sql_slots_load_for_sql_subquery (GncSqlBackend *be, const gchar *subquery, BookLookupFn lookup_fn)
 
void gnc_sql_init_slots_handler (void)
 

Detailed Description

load and save accounts data to SQL

Author
Copyright (c) 2006-2008 Phil Longstaff plong.nosp@m.staf.nosp@m.f@rog.nosp@m.ers..nosp@m.com

This file implements the top-level QofBackend API for saving/ restoring data to/from an SQL database

Definition in file gnc-slots-sql.h.

Function Documentation

gboolean gnc_sql_slots_delete ( GncSqlBackend be,
const GncGUID guid 
)

gnc_sql_slots_delete - Deletes slots for an object from the db.

Parameters
beSQL backend
guidObject guid
Returns
TRUE if successful, FALSE if error

Definition at line 733 of file gnc-slots-sql.c.

734 {
735  gchar* buf;
736  GncSqlResult* result;
737  gchar guid_buf[GUID_ENCODING_LENGTH + 1];
738  GncSqlStatement* stmt;
739  slot_info_t slot_info = { NULL, NULL, TRUE, NULL, 0, NULL, FRAME, NULL, g_string_new(NULL) };
740 
741  g_return_val_if_fail( be != NULL, FALSE );
742  g_return_val_if_fail( guid != NULL, FALSE );
743 
744  (void)guid_to_string_buff( guid, guid_buf );
745 
746  buf = g_strdup_printf( "SELECT * FROM %s WHERE obj_guid='%s' and slot_type in ('%d', '%d') and not guid_val is null",
747  TABLE_NAME, guid_buf, KVP_TYPE_FRAME, KVP_TYPE_GLIST );
748  stmt = gnc_sql_create_statement_from_sql( be, buf );
749  g_free( buf );
750  if ( stmt != NULL )
751  {
752  result = gnc_sql_execute_select_statement( be, stmt );
753  gnc_sql_statement_dispose( stmt );
754  if ( result != NULL )
755  {
756  GncSqlRow* row = gnc_sql_result_get_first_row( result );
757 
758  while ( row != NULL )
759  {
760  GncSqlColumnTableEntry table_row = col_table[guid_val_col];
761  GncGUID child_guid;
762  const GValue* val =
763  gnc_sql_row_get_value_at_col_name( row, table_row.col_name);
764  if ( val == NULL )
765  continue;
766 
767  (void)string_to_guid( g_value_get_string( val ), &child_guid );
768  gnc_sql_slots_delete( be, &child_guid );
769  row = gnc_sql_result_get_next_row( result );
770  }
771  gnc_sql_result_dispose( result );
772  }
773  }
774 
775  slot_info.be = be;
776  slot_info.guid = guid;
777  slot_info.is_ok = TRUE;
778  slot_info.is_ok = gnc_sql_do_db_operation( be, OP_DB_DELETE, TABLE_NAME,
779  TABLE_NAME, &slot_info, obj_guid_col_table );
780 
781  return slot_info.is_ok;
782 }
gboolean string_to_guid(const gchar *string, GncGUID *guid)
gchar * guid_to_string_buff(const GncGUID *guid, gchar *buff)
Definition: guid.h:65
#define GUID_ENCODING_LENGTH
Definition: guid.h:74
GncSqlResult * gnc_sql_execute_select_statement(GncSqlBackend *be, GncSqlStatement *stmt)
gboolean gnc_sql_slots_delete(GncSqlBackend *be, const GncGUID *guid)
gboolean gnc_sql_do_db_operation(GncSqlBackend *be, E_DB_OPERATION op, const gchar *table_name, QofIdTypeConst obj_name, gpointer pObject, const GncSqlColumnTableEntry *table)
GncSqlStatement * gnc_sql_create_statement_from_sql(GncSqlBackend *be, const gchar *sql)
void gnc_sql_slots_load ( GncSqlBackend be,
QofInstance inst 
)

Loads slots for an object from the db.

Parameters
beSQL backend

Definition at line 819 of file gnc-slots-sql.c.

820 {
821  slot_info_t info = { NULL, NULL, TRUE, NULL, 0, NULL, FRAME, NULL, g_string_new(NULL) };
822  g_return_if_fail( be != NULL );
823  g_return_if_fail( inst != NULL );
824 
825  info.be = be;
826  info.guid = qof_instance_get_guid( inst );
827  info.pKvpFrame = qof_instance_get_slots( inst );
828  info.context = NONE;
829 
830  slots_load_info( &info );
831 }
const GncGUID * qof_instance_get_guid(gconstpointer)
void gnc_sql_slots_load_for_list ( GncSqlBackend be,
GList *  list 
)

gnc_sql_slots_load_for_list - Loads slots for a list of objects from the db. Loading slots for a list of objects can be faster than loading for one object at a time because fewer SQL queries are used.

Parameters
beSQL backend
listList of objects

Definition at line 911 of file gnc-slots-sql.c.

912 {
913  QofCollection* coll;
914  GncSqlStatement* stmt;
915  GString* sql;
916  GncSqlResult* result;
917  gboolean single_item;
918 
919  g_return_if_fail( be != NULL );
920 
921  // Ignore empty list
922  if ( list == NULL ) return;
923 
924  coll = qof_instance_get_collection( QOF_INSTANCE(list->data) );
925 
926  // Create the query for all slots for all items on the list
927  sql = g_string_sized_new( 40 + (GUID_ENCODING_LENGTH + 3) * g_list_length( list ) );
928  g_string_append_printf( sql, "SELECT * FROM %s WHERE %s ", TABLE_NAME, obj_guid_col_table[0].col_name );
929  if ( g_list_length( list ) != 1 )
930  {
931  (void)g_string_append( sql, "IN (" );
932  single_item = FALSE;
933  }
934  else
935  {
936  (void)g_string_append( sql, "= " );
937  single_item = TRUE;
938  }
939  (void)gnc_sql_append_guid_list_to_sql( sql, list, G_MAXUINT );
940  if ( !single_item )
941  {
942  (void)g_string_append( sql, ")" );
943  }
944 
945  // Execute the query and load the slots
946  stmt = gnc_sql_create_statement_from_sql( be, sql->str );
947  if ( stmt == NULL )
948  {
949  PERR( "stmt == NULL, SQL = '%s'\n", sql->str );
950  (void)g_string_free( sql, TRUE );
951  return;
952  }
953  (void)g_string_free( sql, TRUE );
954  result = gnc_sql_execute_select_statement( be, stmt );
955  gnc_sql_statement_dispose( stmt );
956  if ( result != NULL )
957  {
958  GncSqlRow* row = gnc_sql_result_get_first_row( result );
959 
960  while ( row != NULL )
961  {
962  load_slot_for_list_item( be, row, coll );
963  row = gnc_sql_result_get_next_row( result );
964  }
965  gnc_sql_result_dispose( result );
966  }
967 }
QofCollection * qof_instance_get_collection(gconstpointer inst)
#define PERR(format, args...)
Definition: qoflog.h:237
guint gnc_sql_append_guid_list_to_sql(GString *sql, GList *list, guint maxCount)
#define GUID_ENCODING_LENGTH
Definition: guid.h:74
GncSqlResult * gnc_sql_execute_select_statement(GncSqlBackend *be, GncSqlStatement *stmt)
GncSqlStatement * gnc_sql_create_statement_from_sql(GncSqlBackend *be, const gchar *sql)
void gnc_sql_slots_load_for_sql_subquery ( GncSqlBackend be,
const gchar *  subquery,
BookLookupFn  lookup_fn 
)

gnc_sql_slots_load_for_sql_subquery - Loads slots for all objects whose guid is supplied by a subquery. The subquery should be of the form "SELECT DISTINCT guid FROM ...". This is faster than loading for one object at a time because fewer SQL queries * are used.

Parameters
beSQL backend
subquerySubquery SQL string
lookup_fnLookup function to get the right object from the book

gnc_sql_slots_load_for_sql_subquery - Loads slots for all objects whose guid is supplied by a subquery. The subquery should be of the form "SELECT DISTINCT guid FROM ...". This is faster than loading for one object at a time because fewer SQL queries * are used.

Parameters
beSQL backend
subquerySubquery SQL string
lookup_fnLookup function

Definition at line 1006 of file gnc-slots-sql.c.

1008 {
1009  gchar* sql;
1010  GncSqlStatement* stmt;
1011  GncSqlResult* result;
1012 
1013  g_return_if_fail( be != NULL );
1014 
1015  // Ignore empty subquery
1016  if ( subquery == NULL ) return;
1017 
1018  sql = g_strdup_printf( "SELECT * FROM %s WHERE %s IN (%s)",
1019  TABLE_NAME, obj_guid_col_table[0].col_name,
1020  subquery );
1021 
1022  // Execute the query and load the slots
1023  stmt = gnc_sql_create_statement_from_sql( be, sql );
1024  if ( stmt == NULL )
1025  {
1026  PERR( "stmt == NULL, SQL = '%s'\n", sql );
1027  g_free( sql );
1028  return;
1029  }
1030  g_free( sql );
1031  result = gnc_sql_execute_select_statement( be, stmt );
1032  gnc_sql_statement_dispose( stmt );
1033  if ( result != NULL )
1034  {
1035  GncSqlRow* row = gnc_sql_result_get_first_row( result );
1036 
1037  while ( row != NULL )
1038  {
1039  load_slot_for_book_object( be, row, lookup_fn );
1040  row = gnc_sql_result_get_next_row( result );
1041  }
1042  gnc_sql_result_dispose( result );
1043  }
1044 }
#define PERR(format, args...)
Definition: qoflog.h:237
GncSqlResult * gnc_sql_execute_select_statement(GncSqlBackend *be, GncSqlStatement *stmt)
GncSqlStatement * gnc_sql_create_statement_from_sql(GncSqlBackend *be, const gchar *sql)
gboolean gnc_sql_slots_save ( GncSqlBackend be,
const GncGUID guid,
gboolean  is_infant,
KvpFrame pFrame 
)

gnc_sql_slots_save - Saves slots for an object to the db.

Parameters
beSQL backend
guidObject guid
is_infantIs this an infant object?
pFrameTop-level KVP frame
Returns
TRUE if successful, FALSE if error

Definition at line 710 of file gnc-slots-sql.c.

711 {
712  slot_info_t slot_info = { NULL, NULL, TRUE, NULL, 0, NULL, FRAME, NULL, g_string_new(NULL) };
713 
714  g_return_val_if_fail( be != NULL, FALSE );
715  g_return_val_if_fail( guid != NULL, FALSE );
716  g_return_val_if_fail( pFrame != NULL, FALSE );
717 
718  // If this is not saving into a new db, clear out the old saved slots first
719  if ( !be->is_pristine_db && !is_infant )
720  {
721  (void)gnc_sql_slots_delete( be, guid );
722  }
723 
724  slot_info.be = be;
725  slot_info.guid = guid;
726  kvp_frame_for_each_slot( pFrame, save_slot, &slot_info );
727  (void)g_string_free( slot_info.path, TRUE );
728 
729  return slot_info.is_ok;
730 }
void kvp_frame_for_each_slot(KvpFrame *f, void(*proc)(const gchar *key, KvpValue *value, gpointer data), gpointer data)
gboolean gnc_sql_slots_delete(GncSqlBackend *be, const GncGUID *guid)
gboolean is_pristine_db