GnuCash 2.4.99
gnc-order-sql.c
Go to the documentation of this file.
00001 /********************************************************************\
00002  * gnc-order-sql.c -- order sql backend                             *
00003  *                                                                  *
00004  * This program is free software; you can redistribute it and/or    *
00005  * modify it under the terms of the GNU General Public License as   *
00006  * published by the Free Software Foundation; either version 2 of   *
00007  * the License, or (at your option) any later version.              *
00008  *                                                                  *
00009  * This program is distributed in the hope that it will be useful,  *
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
00012  * GNU General Public License for more details.                     *
00013  *                                                                  *
00014  * You should have received a copy of the GNU General Public License*
00015  * along with this program; if not, contact:                        *
00016  *                                                                  *
00017  * Free Software Foundation           Voice:  +1-617-542-5942       *
00018  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
00019  * Boston, MA  02110-1301,  USA       gnu@gnu.org                   *
00020  *                                                                  *
00021 \********************************************************************/
00022 
00031 #include "config.h"
00032 
00033 #include <glib.h>
00034 #include <stdlib.h>
00035 #include <string.h>
00036 
00037 #include "gnc-backend-sql.h"
00038 #include "gnc-slots-sql.h"
00039 
00040 #include "gncOrderP.h"
00041 
00042 #include "gnc-order-sql.h"
00043 #include "gnc-owner-sql.h"
00044 
00045 #define _GNC_MOD_NAME   GNC_ID_ORDER
00046 
00047 static QofLogModule log_module = G_LOG_DOMAIN;
00048 
00049 #define TABLE_NAME "orders"
00050 #define TABLE_VERSION 1
00051 
00052 #define MAX_ID_LEN 2048
00053 #define MAX_NOTES_LEN 2048
00054 #define MAX_REFERENCE_LEN 2048
00055 
00056 static GncSqlColumnTableEntry col_table[] =
00057 {
00058     { "guid",        CT_GUID,     0,                 COL_NNUL | COL_PKEY, "guid" },
00059     { "id",          CT_STRING,   MAX_ID_LEN,        COL_NNUL,            "id" },
00060     { "notes",       CT_STRING,   MAX_NOTES_LEN,     COL_NNUL,            "notes" },
00061     { "reference",   CT_STRING,   MAX_REFERENCE_LEN, COL_NNUL,            "reference" },
00062     { "active",      CT_BOOLEAN,  0,                 COL_NNUL,            "order" },
00063     { "date_opened", CT_TIMESPEC, 0,                 COL_NNUL,            "date-opened" },
00064     { "date_closed", CT_TIMESPEC, 0,                 COL_NNUL,            "date-closed" },
00065     { "owner",       CT_OWNERREF, 0,                 COL_NNUL,            NULL, ORDER_OWNER },
00066     { NULL },
00067 };
00068 
00069 static GncOrder*
00070 load_single_order( GncSqlBackend* be, GncSqlRow* row )
00071 {
00072     const GncGUID* guid;
00073     GncOrder* pOrder;
00074 
00075     g_return_val_if_fail( be != NULL, NULL );
00076     g_return_val_if_fail( row != NULL, NULL );
00077 
00078     guid = gnc_sql_load_guid( be, row );
00079     pOrder = gncOrderLookup( be->book, guid );
00080     if ( pOrder == NULL )
00081     {
00082         pOrder = gncOrderCreate( be->book );
00083     }
00084     gnc_sql_load_object( be, row, GNC_ID_ORDER, pOrder, col_table );
00085     qof_instance_mark_clean( QOF_INSTANCE(pOrder) );
00086 
00087     return pOrder;
00088 }
00089 
00090 static void
00091 load_all_orders( GncSqlBackend* be )
00092 {
00093     GncSqlStatement* stmt;
00094     GncSqlResult* result;
00095     QofBook* pBook;
00096 
00097     g_return_if_fail( be != NULL );
00098 
00099     pBook = be->book;
00100 
00101     stmt = gnc_sql_create_select_statement( be, TABLE_NAME );
00102     result = gnc_sql_execute_select_statement( be, stmt );
00103     gnc_sql_statement_dispose( stmt );
00104     if ( result != NULL )
00105     {
00106         GncSqlRow* row;
00107         GList* list = NULL;
00108 
00109         row = gnc_sql_result_get_first_row( result );
00110         while ( row != NULL )
00111         {
00112             GncOrder* pOrder = load_single_order( be, row );
00113             if ( pOrder != NULL )
00114             {
00115                 list = g_list_append( list, pOrder );
00116             }
00117             row = gnc_sql_result_get_next_row( result );
00118         }
00119         gnc_sql_result_dispose( result );
00120 
00121         if ( list != NULL )
00122         {
00123             gnc_sql_slots_load_for_list( be, list );
00124             g_list_free( list );
00125         }
00126     }
00127 }
00128 
00129 /* ================================================================= */
00130 static void
00131 create_order_tables( GncSqlBackend* be )
00132 {
00133     gint version;
00134 
00135     g_return_if_fail( be != NULL );
00136 
00137     version = gnc_sql_get_table_version( be, TABLE_NAME );
00138     if ( version == 0 )
00139     {
00140         gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table );
00141     }
00142 }
00143 
00144 /* ================================================================= */
00145 static gboolean
00146 save_order( GncSqlBackend* be, QofInstance* inst )
00147 {
00148     g_return_val_if_fail( inst != NULL, FALSE );
00149     g_return_val_if_fail( GNC_IS_ORDER(inst), FALSE );
00150     g_return_val_if_fail( be != NULL, FALSE );
00151 
00152     return gnc_sql_commit_standard_item( be, inst, TABLE_NAME, GNC_ID_ORDER, col_table );
00153 }
00154 
00155 /* ================================================================= */
00156 static gboolean
00157 order_should_be_saved( GncOrder *order )
00158 {
00159     const char *id;
00160 
00161     g_return_val_if_fail( order != NULL, FALSE );
00162 
00163     /* make sure this is a valid order before we save it -- should have an ID */
00164     id = gncOrderGetID( order );
00165     if ( id == NULL || *id == '\0' )
00166     {
00167         return FALSE;
00168     }
00169 
00170     return TRUE;
00171 }
00172 
00173 static void
00174 write_single_order( QofInstance *term_p, gpointer data_p )
00175 {
00176     write_objects_t* s = (write_objects_t*)data_p;
00177 
00178     g_return_if_fail( term_p != NULL );
00179     g_return_if_fail( GNC_IS_ORDER(term_p) );
00180     g_return_if_fail( data_p != NULL );
00181 
00182     if ( s->is_ok && order_should_be_saved( GNC_ORDER(term_p) ) )
00183     {
00184         s->is_ok = save_order( s->be, term_p );
00185     }
00186 }
00187 
00188 static gboolean
00189 write_orders( GncSqlBackend* be )
00190 {
00191     write_objects_t data;
00192 
00193     g_return_val_if_fail( be != NULL, FALSE );
00194 
00195     data.be = be;
00196     data.is_ok = TRUE;
00197     qof_object_foreach( GNC_ID_ORDER, be->book, write_single_order, &data );
00198 
00199     return data.is_ok;
00200 }
00201 
00202 /* ================================================================= */
00203 static void
00204 load_order_guid( const GncSqlBackend* be, GncSqlRow* row,
00205                  QofSetterFunc setter, gpointer pObject,
00206                  const GncSqlColumnTableEntry* table_row )
00207 {
00208     const GValue* val;
00209     GncGUID guid;
00210     GncOrder* order = NULL;
00211 
00212     g_return_if_fail( be != NULL );
00213     g_return_if_fail( row != NULL );
00214     g_return_if_fail( pObject != NULL );
00215     g_return_if_fail( table_row != NULL );
00216 
00217     val = gnc_sql_row_get_value_at_col_name( row, table_row->col_name );
00218     if ( val != NULL && G_VALUE_HOLDS_STRING( val ) && g_value_get_string( val ) != NULL )
00219     {
00220         string_to_guid( g_value_get_string( val ), &guid );
00221         order = gncOrderLookup( be->book, &guid );
00222         if ( order != NULL )
00223         {
00224             if ( table_row->gobj_param_name != NULL )
00225             {
00226                 g_object_set( pObject, table_row->gobj_param_name, order, NULL );
00227             }
00228             else
00229             {
00230                 (*setter)( pObject, (const gpointer)order );
00231             }
00232         }
00233         else
00234         {
00235             PWARN( "Order ref '%s' not found", g_value_get_string( val ) );
00236         }
00237     }
00238 }
00239 
00240 static GncSqlColumnTypeHandler order_guid_handler
00241 = { load_order_guid,
00242     gnc_sql_add_objectref_guid_col_info_to_list,
00243     gnc_sql_add_colname_to_list,
00244     gnc_sql_add_gvalue_objectref_guid_to_slist
00245   };
00246 /* ================================================================= */
00247 void
00248 gnc_order_sql_initialize( void )
00249 {
00250     static GncSqlObjectBackend be_data =
00251     {
00252         GNC_SQL_BACKEND_VERSION,
00253         GNC_ID_ORDER,
00254         save_order,                                             /* commit */
00255         load_all_orders,                                /* initial_load */
00256         create_order_tables,                    /* create_tables */
00257         NULL, NULL, NULL,
00258         write_orders                                    /* write */
00259     };
00260 
00261     qof_object_register_backend( GNC_ID_ORDER, GNC_SQL_BACKEND, &be_data );
00262 
00263     gnc_sql_register_col_type_handler( CT_ORDERREF, &order_guid_handler );
00264 }
00265 /* ========================== END OF FILE ===================== */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines