GnuCash 2.4.99
gnc-customer-sql.c
Go to the documentation of this file.
00001 /********************************************************************\
00002  * gnc-customer-sql.c -- customer 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 "gncBillTermP.h"
00041 #include "gncCustomerP.h"
00042 #include "gncTaxTableP.h"
00043 #include "gnc-customer-sql.h"
00044 #include "gnc-address-sql.h"
00045 #include "gnc-bill-term-sql.h"
00046 #include "gnc-tax-table-sql.h"
00047 
00048 #define _GNC_MOD_NAME   GNC_ID_CUSTOMER
00049 
00050 static QofLogModule log_module = G_LOG_DOMAIN;
00051 
00052 #define TABLE_NAME "customers"
00053 #define TABLE_VERSION 2
00054 
00055 #define MAX_NAME_LEN 2048
00056 #define MAX_ID_LEN 2048
00057 #define MAX_NOTES_LEN 2048
00058 
00059 static GncSqlColumnTableEntry col_table[] =
00060 {
00061     { "guid",         CT_GUID,          0,             COL_NNUL | COL_PKEY, "guid" },
00062     { "name",         CT_STRING,        MAX_NAME_LEN,  COL_NNUL,          "name" },
00063     { "id",           CT_STRING,        MAX_ID_LEN,    COL_NNUL,          NULL, CUSTOMER_ID },
00064     { "notes",        CT_STRING,        MAX_NOTES_LEN, COL_NNUL,          NULL, CUSTOMER_NOTES },
00065     { "active",       CT_BOOLEAN,       0,             COL_NNUL,          NULL, QOF_PARAM_ACTIVE },
00066     { "discount",     CT_NUMERIC,       0,             COL_NNUL,          NULL, CUSTOMER_DISCOUNT },
00067     { "credit",       CT_NUMERIC,       0,             COL_NNUL,          NULL, CUSTOMER_CREDIT },
00068     {
00069         "currency",     CT_COMMODITYREF,  0,             COL_NNUL,          NULL, NULL,
00070         (QofAccessFunc)gncCustomerGetCurrency, (QofSetterFunc)gncCustomerSetCurrency
00071     },
00072     { "tax_override", CT_BOOLEAN,       0,             COL_NNUL,          NULL, CUSTOMER_TT_OVER },
00073     { "addr",         CT_ADDRESS,       0,             0,                 NULL, CUSTOMER_ADDR },
00074     { "shipaddr",     CT_ADDRESS,       0,             0,                 NULL, CUSTOMER_SHIPADDR },
00075     { "terms",        CT_BILLTERMREF,   0,             0,                 NULL, CUSTOMER_TERMS },
00076     {
00077         "tax_included", CT_INT,           0,             0,                 NULL, NULL,
00078         (QofAccessFunc)gncCustomerGetTaxIncluded, (QofSetterFunc)gncCustomerSetTaxIncluded
00079     },
00080     {
00081         "taxtable",     CT_TAXTABLEREF,   0,             0,                 NULL, NULL,
00082         (QofAccessFunc)gncCustomerGetTaxTable, (QofSetterFunc)gncCustomerSetTaxTable
00083     },
00084     { NULL }
00085 };
00086 
00087 static GncCustomer*
00088 load_single_customer( GncSqlBackend* be, GncSqlRow* row )
00089 {
00090     const GncGUID* guid;
00091     GncCustomer* pCustomer;
00092 
00093     g_return_val_if_fail( be != NULL, NULL );
00094     g_return_val_if_fail( row != NULL, NULL );
00095 
00096     guid = gnc_sql_load_guid( be, row );
00097     pCustomer = gncCustomerLookup( be->book, guid );
00098     if ( pCustomer == NULL )
00099     {
00100         pCustomer = gncCustomerCreate( be->book );
00101     }
00102     gnc_sql_load_object( be, row, GNC_ID_CUSTOMER, pCustomer, col_table );
00103     qof_instance_mark_clean( QOF_INSTANCE(pCustomer) );
00104 
00105     return pCustomer;
00106 }
00107 
00108 static void
00109 load_all_customers( GncSqlBackend* be )
00110 {
00111     GncSqlStatement* stmt;
00112     GncSqlResult* result;
00113     QofBook* pBook;
00114 
00115     g_return_if_fail( be != NULL );
00116 
00117     pBook = be->book;
00118 
00119     stmt = gnc_sql_create_select_statement( be, TABLE_NAME );
00120     result = gnc_sql_execute_select_statement( be, stmt );
00121     gnc_sql_statement_dispose( stmt );
00122     if ( result != NULL )
00123     {
00124         GList* list = NULL;
00125         GncSqlRow* row;
00126 
00127         row = gnc_sql_result_get_first_row( result );
00128         while ( row != NULL )
00129         {
00130             GncCustomer* pCustomer = load_single_customer( be, row );
00131             if ( pCustomer != NULL )
00132             {
00133                 list = g_list_append( list, pCustomer );
00134             }
00135             row = gnc_sql_result_get_next_row( result );
00136         }
00137         gnc_sql_result_dispose( result );
00138 
00139         if ( list != NULL )
00140         {
00141             gnc_sql_slots_load_for_list( be, list );
00142             g_list_free( list );
00143         }
00144     }
00145 }
00146 
00147 /* ================================================================= */
00148 static void
00149 create_customer_tables( GncSqlBackend* be )
00150 {
00151     gint version;
00152 
00153     g_return_if_fail( be != NULL );
00154 
00155     version = gnc_sql_get_table_version( be, TABLE_NAME );
00156     if ( version == 0 )
00157     {
00158         gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table );
00159     }
00160     else if ( version == 1 )
00161     {
00162         /* Upgrade 64 bit int handling */
00163         gnc_sql_upgrade_table( be, TABLE_NAME, col_table );
00164         gnc_sql_set_table_version( be, TABLE_NAME, TABLE_VERSION );
00165 
00166         PINFO("Customers table upgraded from version 1 to version %d\n", TABLE_VERSION);
00167     }
00168 }
00169 
00170 /* ================================================================= */
00171 static gboolean
00172 save_customer( GncSqlBackend* be, QofInstance* inst )
00173 {
00174     g_return_val_if_fail( inst != NULL, FALSE );
00175     g_return_val_if_fail( GNC_CUSTOMER(inst), FALSE );
00176     g_return_val_if_fail( be != NULL, FALSE );
00177 
00178     return gnc_sql_commit_standard_item( be, inst, TABLE_NAME, GNC_ID_CUSTOMER, col_table );
00179 }
00180 
00181 /* ================================================================= */
00182 typedef struct
00183 {
00184     GncSqlBackend* be;
00185     gboolean is_ok;
00186 } write_customers_t;
00187 
00188 static gboolean
00189 customer_should_be_saved( GncCustomer *customer )
00190 {
00191     const char *id;
00192 
00193     g_return_val_if_fail( customer != NULL, FALSE );
00194 
00195     /* Make sure this is a valid customer before we save it -- should have an ID */
00196     id = gncCustomerGetID( customer );
00197     if ( id == NULL || *id == '\0' )
00198     {
00199         return FALSE;
00200     }
00201 
00202     return TRUE;
00203 }
00204 
00205 static void
00206 write_single_customer( QofInstance *term_p, gpointer data_p )
00207 {
00208     write_customers_t* data = (write_customers_t*)data_p;
00209 
00210     g_return_if_fail( term_p != NULL );
00211     g_return_if_fail( GNC_IS_CUSTOMER(term_p) );
00212     g_return_if_fail( data_p != NULL );
00213 
00214     if ( customer_should_be_saved( GNC_CUSTOMER(term_p) ) && data->is_ok )
00215     {
00216         data->is_ok = save_customer( data->be, term_p );
00217     }
00218 }
00219 
00220 static gboolean
00221 write_customers( GncSqlBackend* be )
00222 {
00223     write_customers_t data;
00224 
00225     g_return_val_if_fail( be != NULL, FALSE );
00226 
00227     data.be = be;
00228     data.is_ok = TRUE;
00229     qof_object_foreach( GNC_ID_CUSTOMER, be->book, write_single_customer, (gpointer)&data );
00230     return data.is_ok;
00231 }
00232 
00233 /* ================================================================= */
00234 void
00235 gnc_customer_sql_initialize( void )
00236 {
00237     static GncSqlObjectBackend be_data =
00238     {
00239         GNC_SQL_BACKEND_VERSION,
00240         GNC_ID_CUSTOMER,
00241         save_customer,                                          /* commit */
00242         load_all_customers,                                     /* initial_load */
00243         create_customer_tables,                         /* create_tables */
00244         NULL, NULL, NULL,
00245         write_customers                                         /* write */
00246     };
00247 
00248     qof_object_register_backend( GNC_ID_CUSTOMER, GNC_SQL_BACKEND, &be_data );
00249 }
00250 /* ========================== END OF FILE ===================== */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines