GnuCash 2.4.99
gnc-vendor-sql.c
Go to the documentation of this file.
00001 /********************************************************************\
00002  * gnc-vendor-sql.c -- vendor 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-commodity.h"
00038 
00039 #include "gnc-backend-sql.h"
00040 #include "gnc-commodity-sql.h"
00041 #include "gnc-slots-sql.h"
00042 
00043 #include "gnc-commodity.h"
00044 #include "gncBillTermP.h"
00045 #include "gncVendorP.h"
00046 #include "gncTaxTableP.h"
00047 #include "gnc-vendor-sql.h"
00048 #include "gnc-address-sql.h"
00049 #include "gnc-bill-term-sql.h"
00050 #include "gnc-tax-table-sql.h"
00051 
00052 #define _GNC_MOD_NAME   GNC_ID_VENDOR
00053 
00054 static QofLogModule log_module = G_LOG_DOMAIN;
00055 
00056 #define MAX_NAME_LEN 2048
00057 #define MAX_ID_LEN 2048
00058 #define MAX_NOTES_LEN 2048
00059 #define MAX_TAX_INC_LEN 2048
00060 
00061 #define TABLE_NAME "vendors"
00062 #define TABLE_VERSION 1
00063 
00064 static GncSqlColumnTableEntry col_table[] =
00065 {
00066     { "guid",         CT_GUID,          0,               COL_NNUL | COL_PKEY, "guid" },
00067     { "name",         CT_STRING,        MAX_NAME_LEN,    COL_NNUL,            "name" },
00068     { "id",           CT_STRING,        MAX_ID_LEN,      COL_NNUL,            "id" },
00069     { "notes",        CT_STRING,        MAX_NOTES_LEN,   COL_NNUL,            "notes" },
00070     { "currency",     CT_COMMODITYREF,  0,               COL_NNUL,            "currency" },
00071     { "active",       CT_BOOLEAN,       0,               COL_NNUL,            "active" },
00072     { "tax_override", CT_BOOLEAN,       0,               COL_NNUL,            "tax-table-override" },
00073     { "addr",         CT_ADDRESS,       0,               0,                   "address" },
00074     { "terms",        CT_BILLTERMREF,   0,               0,                   "terms" },
00075     { "tax_inc",      CT_STRING,        MAX_TAX_INC_LEN, 0,                   "tax-included-string" },
00076     { "tax_table",    CT_TAXTABLEREF,   0,               0,                   "tax-table" },
00077     { NULL }
00078 };
00079 
00080 static GncVendor*
00081 load_single_vendor( GncSqlBackend* be, GncSqlRow* row )
00082 {
00083     const GncGUID* guid;
00084     GncVendor* pVendor;
00085 
00086     g_return_val_if_fail( be != NULL, NULL );
00087     g_return_val_if_fail( row != NULL, NULL );
00088 
00089     guid = gnc_sql_load_guid( be, row );
00090     pVendor = gncVendorLookup( be->book, guid );
00091     if ( pVendor == NULL )
00092     {
00093         pVendor = gncVendorCreate( be->book );
00094     }
00095     gnc_sql_load_object( be, row, GNC_ID_VENDOR, pVendor, col_table );
00096     qof_instance_mark_clean( QOF_INSTANCE(pVendor) );
00097 
00098     return pVendor;
00099 }
00100 
00101 static void
00102 load_all_vendors( GncSqlBackend* be )
00103 {
00104     GncSqlStatement* stmt;
00105     GncSqlResult* result;
00106     QofBook* pBook;
00107 
00108     g_return_if_fail( be != NULL );
00109 
00110     pBook = be->book;
00111 
00112     stmt = gnc_sql_create_select_statement( be, TABLE_NAME );
00113     result = gnc_sql_execute_select_statement( be, stmt );
00114     gnc_sql_statement_dispose( stmt );
00115     if ( result != NULL )
00116     {
00117         GncSqlRow* row;
00118         GList* list = NULL;
00119 
00120         row = gnc_sql_result_get_first_row( result );
00121         while ( row != NULL )
00122         {
00123             GncVendor* pVendor = load_single_vendor( be, row );
00124             if ( pVendor != NULL )
00125             {
00126                 list = g_list_append( list, pVendor );
00127             }
00128             row = gnc_sql_result_get_next_row( result );
00129         }
00130         gnc_sql_result_dispose( result );
00131 
00132         if ( list != NULL )
00133         {
00134             gnc_sql_slots_load_for_list( be, list );
00135             g_list_free( list );
00136         }
00137     }
00138 }
00139 
00140 /* ================================================================= */
00141 static void
00142 create_vendor_tables( GncSqlBackend* be )
00143 {
00144     gint version;
00145 
00146     g_return_if_fail( be != NULL );
00147 
00148     version = gnc_sql_get_table_version( be, TABLE_NAME );
00149     if ( version == 0 )
00150     {
00151         gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table );
00152     }
00153 }
00154 
00155 /* ================================================================= */
00156 static gboolean
00157 save_vendor( GncSqlBackend* be, QofInstance* inst )
00158 {
00159     GncVendor* v;
00160     const GncGUID* guid;
00161     gint op;
00162     gboolean is_infant;
00163     gboolean is_ok = TRUE;
00164 
00165     g_return_val_if_fail( inst != NULL, FALSE );
00166     g_return_val_if_fail( GNC_IS_VENDOR(inst), FALSE );
00167     g_return_val_if_fail( be != NULL, FALSE );
00168 
00169     v = GNC_VENDOR(inst);
00170 
00171     is_infant = qof_instance_get_infant( inst );
00172     if ( qof_instance_get_destroying( inst ) )
00173     {
00174         op = OP_DB_DELETE;
00175     }
00176     else if ( be->is_pristine_db || is_infant )
00177     {
00178         op = OP_DB_INSERT;
00179     }
00180     else
00181     {
00182         op = OP_DB_UPDATE;
00183     }
00184     if ( op != OP_DB_DELETE )
00185     {
00186         // Ensure the commodity is in the db
00187         is_ok = gnc_sql_save_commodity( be, gncVendorGetCurrency( v ) );
00188     }
00189     if ( is_ok )
00190     {
00191         is_ok = gnc_sql_do_db_operation( be, op, TABLE_NAME, GNC_ID_VENDOR, v, col_table );
00192     }
00193 
00194     if ( is_ok )
00195     {
00196         // Now, commit or delete any slots
00197         guid = qof_instance_get_guid( inst );
00198         if ( !qof_instance_get_destroying(inst) )
00199         {
00200             is_ok = gnc_sql_slots_save( be, guid, is_infant, qof_instance_get_slots( inst ) );
00201         }
00202         else
00203         {
00204             is_ok = gnc_sql_slots_delete( be, guid );
00205         }
00206     }
00207 
00208     return is_ok;
00209 }
00210 
00211 /* ================================================================= */
00212 static gboolean
00213 vendor_should_be_saved( GncVendor *vendor )
00214 {
00215     const char *id;
00216 
00217     g_return_val_if_fail( vendor != NULL, FALSE );
00218 
00219     /* make sure this is a valid vendor before we save it -- should have an ID */
00220     id = gncVendorGetID( vendor );
00221     if ( id == NULL || *id == '\0' )
00222     {
00223         return FALSE;
00224     }
00225 
00226     return TRUE;
00227 }
00228 
00229 static void
00230 write_single_vendor( QofInstance *term_p, gpointer data_p )
00231 {
00232     write_objects_t* s = (write_objects_t*)data_p;
00233 
00234     g_return_if_fail( term_p != NULL );
00235     g_return_if_fail( GNC_IS_VENDOR(term_p) );
00236     g_return_if_fail( data_p != NULL );
00237 
00238     if ( s->is_ok && vendor_should_be_saved( GNC_VENDOR(term_p) ) )
00239     {
00240         s->is_ok = save_vendor( s->be, term_p );
00241     }
00242 }
00243 
00244 static gboolean
00245 write_vendors( GncSqlBackend* be )
00246 {
00247     write_objects_t data;
00248 
00249     g_return_val_if_fail( be != NULL, FALSE );
00250 
00251     data.be = be;
00252     data.is_ok = TRUE;
00253     qof_object_foreach( GNC_ID_VENDOR, be->book, write_single_vendor, &data );
00254 
00255     return data.is_ok;
00256 }
00257 
00258 /* ================================================================= */
00259 void
00260 gnc_vendor_sql_initialize( void )
00261 {
00262     static GncSqlObjectBackend be_data =
00263     {
00264         GNC_SQL_BACKEND_VERSION,
00265         GNC_ID_VENDOR,
00266         save_vendor,                                            /* commit */
00267         load_all_vendors,                                       /* initial_load */
00268         create_vendor_tables,                           /* create_tables */
00269         NULL, NULL, NULL,
00270         write_vendors                                           /* write */
00271     };
00272 
00273     qof_object_register_backend( GNC_ID_VENDOR, GNC_SQL_BACKEND, &be_data );
00274 }
00275 /* ========================== END OF FILE ===================== */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines