GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gnc-customer-sql.c
Go to the documentation of this file.
1 /********************************************************************\
2  * gnc-customer-sql.c -- customer sql backend *
3  * *
4  * This program is free software; you can redistribute it and/or *
5  * modify it under the terms of the GNU General Public License as *
6  * published by the Free Software Foundation; either version 2 of *
7  * the License, or (at your option) any later version. *
8  * *
9  * This program is distributed in the hope that it will be useful, *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12  * GNU General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU General Public License*
15  * along with this program; if not, contact: *
16  * *
17  * Free Software Foundation Voice: +1-617-542-5942 *
18  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
19  * Boston, MA 02110-1301, USA [email protected] *
20  * *
21 \********************************************************************/
22 
31 #include "config.h"
32 
33 #include <glib.h>
34 #include <stdlib.h>
35 #include <string.h>
36 
37 #include "gnc-backend-sql.h"
38 #include "gnc-slots-sql.h"
39 
40 #include "gncBillTermP.h"
41 #include "gncCustomerP.h"
42 #include "gncTaxTableP.h"
43 #include "gnc-customer-sql.h"
44 #include "gnc-address-sql.h"
45 #include "gnc-bill-term-sql.h"
46 #include "gnc-tax-table-sql.h"
47 
48 #define _GNC_MOD_NAME GNC_ID_CUSTOMER
49 
50 static QofLogModule log_module = G_LOG_DOMAIN;
51 
52 #define TABLE_NAME "customers"
53 #define TABLE_VERSION 2
54 
55 #define MAX_NAME_LEN 2048
56 #define MAX_ID_LEN 2048
57 #define MAX_NOTES_LEN 2048
58 
59 static GncSqlColumnTableEntry col_table[] =
60 {
61  { "guid", CT_GUID, 0, COL_NNUL | COL_PKEY, "guid" },
62  { "name", CT_STRING, MAX_NAME_LEN, COL_NNUL, "name" },
63  { "id", CT_STRING, MAX_ID_LEN, COL_NNUL, NULL, CUSTOMER_ID },
64  { "notes", CT_STRING, MAX_NOTES_LEN, COL_NNUL, NULL, CUSTOMER_NOTES },
65  { "active", CT_BOOLEAN, 0, COL_NNUL, NULL, QOF_PARAM_ACTIVE },
66  { "discount", CT_NUMERIC, 0, COL_NNUL, NULL, CUSTOMER_DISCOUNT },
67  { "credit", CT_NUMERIC, 0, COL_NNUL, NULL, CUSTOMER_CREDIT },
68  {
69  "currency", CT_COMMODITYREF, 0, COL_NNUL, NULL, NULL,
70  (QofAccessFunc)gncCustomerGetCurrency, (QofSetterFunc)gncCustomerSetCurrency
71  },
72  { "tax_override", CT_BOOLEAN, 0, COL_NNUL, NULL, CUSTOMER_TT_OVER },
73  { "addr", CT_ADDRESS, 0, 0, NULL, CUSTOMER_ADDR },
74  { "shipaddr", CT_ADDRESS, 0, 0, NULL, CUSTOMER_SHIPADDR },
75  { "terms", CT_BILLTERMREF, 0, 0, NULL, CUSTOMER_TERMS },
76  {
77  "tax_included", CT_INT, 0, 0, NULL, NULL,
78  (QofAccessFunc)gncCustomerGetTaxIncluded, (QofSetterFunc)gncCustomerSetTaxIncluded
79  },
80  {
81  "taxtable", CT_TAXTABLEREF, 0, 0, NULL, NULL,
82  (QofAccessFunc)gncCustomerGetTaxTable, (QofSetterFunc)gncCustomerSetTaxTable
83  },
84  { NULL }
85 };
86 
87 static GncCustomer*
88 load_single_customer( GncSqlBackend* be, GncSqlRow* row )
89 {
90  const GncGUID* guid;
91  GncCustomer* pCustomer;
92 
93  g_return_val_if_fail( be != NULL, NULL );
94  g_return_val_if_fail( row != NULL, NULL );
95 
96  guid = gnc_sql_load_guid( be, row );
97  pCustomer = gncCustomerLookup( be->book, guid );
98  if ( pCustomer == NULL )
99  {
100  pCustomer = gncCustomerCreate( be->book );
101  }
102  gnc_sql_load_object( be, row, GNC_ID_CUSTOMER, pCustomer, col_table );
103  qof_instance_mark_clean( QOF_INSTANCE(pCustomer) );
104 
105  return pCustomer;
106 }
107 
108 static void
109 load_all_customers( GncSqlBackend* be )
110 {
111  GncSqlStatement* stmt;
112  GncSqlResult* result;
113 
114  g_return_if_fail( be != NULL );
115 
116 
117  stmt = gnc_sql_create_select_statement( be, TABLE_NAME );
118  result = gnc_sql_execute_select_statement( be, stmt );
119  gnc_sql_statement_dispose( stmt );
120  if ( result != NULL )
121  {
122  GList* list = NULL;
123  GncSqlRow* row;
124 
125  row = gnc_sql_result_get_first_row( result );
126  while ( row != NULL )
127  {
128  GncCustomer* pCustomer = load_single_customer( be, row );
129  if ( pCustomer != NULL )
130  {
131  list = g_list_append( list, pCustomer );
132  }
133  row = gnc_sql_result_get_next_row( result );
134  }
135  gnc_sql_result_dispose( result );
136 
137  if ( list != NULL )
138  {
139  gnc_sql_slots_load_for_list( be, list );
140  g_list_free( list );
141  }
142  }
143 }
144 
145 /* ================================================================= */
146 static void
147 create_customer_tables( GncSqlBackend* be )
148 {
149  gint version;
150 
151  g_return_if_fail( be != NULL );
152 
153  version = gnc_sql_get_table_version( be, TABLE_NAME );
154  if ( version == 0 )
155  {
156  gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table );
157  }
158  else if ( version == 1 )
159  {
160  /* Upgrade 64 bit int handling */
161  gnc_sql_upgrade_table( be, TABLE_NAME, col_table );
162  gnc_sql_set_table_version( be, TABLE_NAME, TABLE_VERSION );
163 
164  PINFO("Customers table upgraded from version 1 to version %d\n", TABLE_VERSION);
165  }
166 }
167 
168 /* ================================================================= */
169 static gboolean
170 save_customer( GncSqlBackend* be, QofInstance* inst )
171 {
172  g_return_val_if_fail( inst != NULL, FALSE );
173  g_return_val_if_fail( GNC_CUSTOMER(inst), FALSE );
174  g_return_val_if_fail( be != NULL, FALSE );
175 
176  return gnc_sql_commit_standard_item( be, inst, TABLE_NAME, GNC_ID_CUSTOMER, col_table );
177 }
178 
179 /* ================================================================= */
180 typedef struct
181 {
182  GncSqlBackend* be;
183  gboolean is_ok;
185 
186 static gboolean
187 customer_should_be_saved( GncCustomer *customer )
188 {
189  const char *id;
190 
191  g_return_val_if_fail( customer != NULL, FALSE );
192 
193  /* Make sure this is a valid customer before we save it -- should have an ID */
194  id = gncCustomerGetID( customer );
195  if ( id == NULL || *id == '\0' )
196  {
197  return FALSE;
198  }
199 
200  return TRUE;
201 }
202 
203 static void
204 write_single_customer( QofInstance *term_p, gpointer data_p )
205 {
206  write_customers_t* data = (write_customers_t*)data_p;
207 
208  g_return_if_fail( term_p != NULL );
209  g_return_if_fail( GNC_IS_CUSTOMER(term_p) );
210  g_return_if_fail( data_p != NULL );
211 
212  if ( customer_should_be_saved( GNC_CUSTOMER(term_p) ) && data->is_ok )
213  {
214  data->is_ok = save_customer( data->be, term_p );
215  }
216 }
217 
218 static gboolean
219 write_customers( GncSqlBackend* be )
220 {
221  write_customers_t data;
222 
223  g_return_val_if_fail( be != NULL, FALSE );
224 
225  data.be = be;
226  data.is_ok = TRUE;
227  qof_object_foreach( GNC_ID_CUSTOMER, be->book, write_single_customer, (gpointer)&data );
228  return data.is_ok;
229 }
230 
231 /* ================================================================= */
232 void
233 gnc_customer_sql_initialize( void )
234 {
235  static GncSqlObjectBackend be_data =
236  {
237  GNC_SQL_BACKEND_VERSION,
238  GNC_ID_CUSTOMER,
239  save_customer, /* commit */
240  load_all_customers, /* initial_load */
241  create_customer_tables, /* create_tables */
242  NULL, NULL, NULL,
243  write_customers /* write */
244  };
245 
246  qof_object_register_backend( GNC_ID_CUSTOMER, GNC_SQL_BACKEND, &be_data );
247 }
248 /* ========================== END OF FILE ===================== */
gboolean qof_object_register_backend(QofIdTypeConst type_name, const char *backend_name, gpointer be_data)
gint gnc_sql_get_table_version(const GncSqlBackend *be, const gchar *table_name)
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...)
Definition: qoflog.h:249
load and save accounts data to SQL
GncSqlStatement * gnc_sql_create_select_statement(GncSqlBackend *be, const gchar *table_name)
#define COL_NNUL
gboolean gnc_sql_create_table(GncSqlBackend *be, const gchar *table_name, gint table_version, const GncSqlColumnTableEntry *col_table)
load and save data to SQL
load and save customer data to SQL
gpointer(* QofAccessFunc)(gpointer object, const QofParam *param)
Definition: qofclass.h:177
load and save accounts data to SQL
QofBook * book
Definition: guid.h:65
#define COL_PKEY
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_slots_load_for_list(GncSqlBackend *be, GList *list)
void qof_object_foreach(QofIdTypeConst type_name, QofBook *book, QofInstanceForeachCB cb, gpointer user_data)
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)
load and save address data to SQL
gboolean gnc_sql_commit_standard_item(GncSqlBackend *be, QofInstance *inst, const gchar *tableName, QofIdTypeConst obj_name, const GncSqlColumnTableEntry *col_table)
void(* QofSetterFunc)(gpointer, gpointer)
Definition: qofclass.h:184
const gchar * QofLogModule
Definition: qofid.h:89
load and save tax table data to SQL