GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gnc-vendor-sql.c
Go to the documentation of this file.
1 /********************************************************************\
2  * gnc-vendor-sql.c -- vendor 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-commodity.h"
38 
39 #include "gnc-backend-sql.h"
40 #include "gnc-commodity-sql.h"
41 #include "gnc-slots-sql.h"
42 
43 #include "gnc-commodity.h"
44 #include "gncBillTermP.h"
45 #include "gncVendorP.h"
46 #include "gncTaxTableP.h"
47 #include "gnc-vendor-sql.h"
48 #include "gnc-address-sql.h"
49 #include "gnc-bill-term-sql.h"
50 #include "gnc-tax-table-sql.h"
51 
52 #define _GNC_MOD_NAME GNC_ID_VENDOR
53 
54 G_GNUC_UNUSED static QofLogModule log_module = G_LOG_DOMAIN;
55 
56 #define MAX_NAME_LEN 2048
57 #define MAX_ID_LEN 2048
58 #define MAX_NOTES_LEN 2048
59 #define MAX_TAX_INC_LEN 2048
60 
61 #define TABLE_NAME "vendors"
62 #define TABLE_VERSION 1
63 
64 static GncSqlColumnTableEntry col_table[] =
65 {
66  { "guid", CT_GUID, 0, COL_NNUL | COL_PKEY, "guid" },
67  { "name", CT_STRING, MAX_NAME_LEN, COL_NNUL, "name" },
68  { "id", CT_STRING, MAX_ID_LEN, COL_NNUL, "id" },
69  { "notes", CT_STRING, MAX_NOTES_LEN, COL_NNUL, "notes" },
70  { "currency", CT_COMMODITYREF, 0, COL_NNUL, "currency" },
71  { "active", CT_BOOLEAN, 0, COL_NNUL, "active" },
72  { "tax_override", CT_BOOLEAN, 0, COL_NNUL, "tax-table-override" },
73  { "addr", CT_ADDRESS, 0, 0, "address" },
74  { "terms", CT_BILLTERMREF, 0, 0, "terms" },
75  { "tax_inc", CT_STRING, MAX_TAX_INC_LEN, 0, "tax-included-string" },
76  { "tax_table", CT_TAXTABLEREF, 0, 0, "tax-table" },
77  { NULL }
78 };
79 
80 static GncVendor*
81 load_single_vendor( GncSqlBackend* be, GncSqlRow* row )
82 {
83  const GncGUID* guid;
84  GncVendor* pVendor;
85 
86  g_return_val_if_fail( be != NULL, NULL );
87  g_return_val_if_fail( row != NULL, NULL );
88 
89  guid = gnc_sql_load_guid( be, row );
90  pVendor = gncVendorLookup( be->book, guid );
91  if ( pVendor == NULL )
92  {
93  pVendor = gncVendorCreate( be->book );
94  }
95  gnc_sql_load_object( be, row, GNC_ID_VENDOR, pVendor, col_table );
96  qof_instance_mark_clean( QOF_INSTANCE(pVendor) );
97 
98  return pVendor;
99 }
100 
101 static void
102 load_all_vendors( GncSqlBackend* be )
103 {
104  GncSqlStatement* stmt;
105  GncSqlResult* result;
106 
107  g_return_if_fail( be != NULL );
108 
109  stmt = gnc_sql_create_select_statement( be, TABLE_NAME );
110  result = gnc_sql_execute_select_statement( be, stmt );
111  gnc_sql_statement_dispose( stmt );
112  if ( result != NULL )
113  {
114  GncSqlRow* row;
115  GList* list = NULL;
116 
117  row = gnc_sql_result_get_first_row( result );
118  while ( row != NULL )
119  {
120  GncVendor* pVendor = load_single_vendor( be, row );
121  if ( pVendor != NULL )
122  {
123  list = g_list_append( list, pVendor );
124  }
125  row = gnc_sql_result_get_next_row( result );
126  }
127  gnc_sql_result_dispose( result );
128 
129  if ( list != NULL )
130  {
131  gnc_sql_slots_load_for_list( be, list );
132  g_list_free( list );
133  }
134  }
135 }
136 
137 /* ================================================================= */
138 static void
139 create_vendor_tables( GncSqlBackend* be )
140 {
141  gint version;
142 
143  g_return_if_fail( be != NULL );
144 
145  version = gnc_sql_get_table_version( be, TABLE_NAME );
146  if ( version == 0 )
147  {
148  gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table );
149  }
150 }
151 
152 /* ================================================================= */
153 static gboolean
154 save_vendor( GncSqlBackend* be, QofInstance* inst )
155 {
156  GncVendor* v;
157  const GncGUID* guid;
158  gint op;
159  gboolean is_infant;
160  gboolean is_ok = TRUE;
161 
162  g_return_val_if_fail( inst != NULL, FALSE );
163  g_return_val_if_fail( GNC_IS_VENDOR(inst), FALSE );
164  g_return_val_if_fail( be != NULL, FALSE );
165 
166  v = GNC_VENDOR(inst);
167 
168  is_infant = qof_instance_get_infant( inst );
169  if ( qof_instance_get_destroying( inst ) )
170  {
171  op = OP_DB_DELETE;
172  }
173  else if ( be->is_pristine_db || is_infant )
174  {
175  op = OP_DB_INSERT;
176  }
177  else
178  {
179  op = OP_DB_UPDATE;
180  }
181  if ( op != OP_DB_DELETE )
182  {
183  // Ensure the commodity is in the db
184  is_ok = gnc_sql_save_commodity( be, gncVendorGetCurrency( v ) );
185  }
186  if ( is_ok )
187  {
188  is_ok = gnc_sql_do_db_operation( be, op, TABLE_NAME, GNC_ID_VENDOR, v, col_table );
189  }
190 
191  if ( is_ok )
192  {
193  // Now, commit or delete any slots
194  guid = qof_instance_get_guid( inst );
195  if ( !qof_instance_get_destroying(inst) )
196  {
197  is_ok = gnc_sql_slots_save( be, guid, is_infant, qof_instance_get_slots( inst ) );
198  }
199  else
200  {
201  is_ok = gnc_sql_slots_delete( be, guid );
202  }
203  }
204 
205  return is_ok;
206 }
207 
208 /* ================================================================= */
209 static gboolean
210 vendor_should_be_saved( GncVendor *vendor )
211 {
212  const char *id;
213 
214  g_return_val_if_fail( vendor != NULL, FALSE );
215 
216  /* make sure this is a valid vendor before we save it -- should have an ID */
217  id = gncVendorGetID( vendor );
218  if ( id == NULL || *id == '\0' )
219  {
220  return FALSE;
221  }
222 
223  return TRUE;
224 }
225 
226 static void
227 write_single_vendor( QofInstance *term_p, gpointer data_p )
228 {
229  write_objects_t* s = (write_objects_t*)data_p;
230 
231  g_return_if_fail( term_p != NULL );
232  g_return_if_fail( GNC_IS_VENDOR(term_p) );
233  g_return_if_fail( data_p != NULL );
234 
235  if ( s->is_ok && vendor_should_be_saved( GNC_VENDOR(term_p) ) )
236  {
237  s->is_ok = save_vendor( s->be, term_p );
238  }
239 }
240 
241 static gboolean
242 write_vendors( GncSqlBackend* be )
243 {
244  write_objects_t data;
245 
246  g_return_val_if_fail( be != NULL, FALSE );
247 
248  data.be = be;
249  data.is_ok = TRUE;
250  qof_object_foreach( GNC_ID_VENDOR, be->book, write_single_vendor, &data );
251 
252  return data.is_ok;
253 }
254 
255 /* ================================================================= */
256 void
257 gnc_vendor_sql_initialize( void )
258 {
259  static GncSqlObjectBackend be_data =
260  {
261  GNC_SQL_BACKEND_VERSION,
262  GNC_ID_VENDOR,
263  save_vendor, /* commit */
264  load_all_vendors, /* initial_load */
265  create_vendor_tables, /* create_tables */
266  NULL, NULL, NULL,
267  write_vendors /* write */
268  };
269 
270  qof_object_register_backend( GNC_ID_VENDOR, GNC_SQL_BACKEND, &be_data );
271 }
272 /* ========================== END OF FILE ===================== */
gboolean qof_object_register_backend(QofIdTypeConst type_name, const char *backend_name, gpointer be_data)
load and save vendor data to SQL
const GncGUID * qof_instance_get_guid(gconstpointer)
gint gnc_sql_get_table_version(const GncSqlBackend *be, const gchar *table_name)
#define G_LOG_DOMAIN
Functions providing the SX List as a plugin page.
load and save data to SQL
load and save accounts data to SQL
GncSqlStatement * gnc_sql_create_select_statement(GncSqlBackend *be, const gchar *table_name)
#define COL_NNUL
gboolean qof_instance_get_destroying(gconstpointer ptr)
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 accounts data to SQL
QofBook * book
Definition: guid.h:65
#define COL_PKEY
const GncGUID * gnc_sql_load_guid(const GncSqlBackend *be, GncSqlRow *row)
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)
gboolean gnc_sql_slots_delete(GncSqlBackend *be, const GncGUID *guid)
load and save address data to SQL
gboolean is_pristine_db
gboolean gnc_sql_do_db_operation(GncSqlBackend *be, E_DB_OPERATION op, const gchar *table_name, QofIdTypeConst obj_name, gpointer pObject, const GncSqlColumnTableEntry *table)
Commodity handling public routines.
gboolean gnc_sql_slots_save(GncSqlBackend *be, const GncGUID *guid, gboolean is_infant, KvpFrame *pFrame)
const gchar * QofLogModule
Definition: qofid.h:89
load and save tax table data to SQL