GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gnc-owner-sql.c
Go to the documentation of this file.
1 /********************************************************************\
2  * gnc-owner-sql.c -- owner sql implementation *
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 
39 #include "gnc-owner-sql.h"
40 #include "gncCustomerP.h"
41 #include "gncJobP.h"
42 #include "gncEmployeeP.h"
43 #include "gncVendorP.h"
44 
45 static QofLogModule log_module = G_LOG_DOMAIN;
46 
47 typedef void (*OwnerSetterFunc)( gpointer, GncOwner* );
48 typedef GncOwner* (*OwnerGetterFunc)( const gpointer );
49 
50 static void
51 load_owner( const GncSqlBackend* be, GncSqlRow* row,
52  QofSetterFunc setter, gpointer pObject,
53  const GncSqlColumnTableEntry* table_row )
54 {
55  const GValue* val;
56  gchar* buf;
57  GncOwnerType type;
58  GncGUID guid;
59  QofBook* book;
60  GncOwner owner;
61  GncGUID* pGuid = NULL;
62 
63  g_return_if_fail( be != NULL );
64  g_return_if_fail( row != NULL );
65  g_return_if_fail( pObject != NULL );
66  g_return_if_fail( table_row != NULL );
67 
68  book = be->book;
69  buf = g_strdup_printf( "%s_type", table_row->col_name );
70  val = gnc_sql_row_get_value_at_col_name( row, buf );
71  type = (GncOwnerType)gnc_sql_get_integer_value( val );
72  g_free( buf );
73  buf = g_strdup_printf( "%s_guid", table_row->col_name );
74  val = gnc_sql_row_get_value_at_col_name( row, buf );
75  g_free( buf );
76 
77  if ( val != NULL && G_VALUE_HOLDS_STRING( val ) && g_value_get_string( val ) != NULL )
78  {
79  string_to_guid( g_value_get_string( val ), &guid );
80  pGuid = &guid;
81  }
82 
83  switch ( type )
84  {
85  case GNC_OWNER_CUSTOMER:
86  {
87  GncCustomer *cust = NULL;
88 
89  if ( pGuid != NULL )
90  {
91  cust = gncCustomerLookup( book, pGuid );
92  if ( cust == NULL )
93  {
94  cust = gncCustomerCreate( book );
95  gncCustomerSetGUID( cust, &guid );
96  }
97  }
98  gncOwnerInitCustomer( &owner, cust );
99  break;
100  }
101 
102  case GNC_OWNER_JOB:
103  {
104  GncJob *job = NULL;
105 
106  if ( pGuid != NULL )
107  {
108  job = gncJobLookup( book, pGuid );
109  if ( job == NULL )
110  {
111  job = gncJobCreate( book );
112  gncJobSetGUID( job, &guid );
113  }
114  }
115  gncOwnerInitJob( &owner, job );
116  break;
117  }
118 
119  case GNC_OWNER_VENDOR:
120  {
121  GncVendor *vendor = NULL;
122 
123  if ( pGuid != NULL )
124  {
125  vendor = gncVendorLookup( book, pGuid );
126  if ( vendor == NULL )
127  {
128  vendor = gncVendorCreate( book );
129  gncVendorSetGUID( vendor, &guid );
130  }
131  }
132  gncOwnerInitVendor( &owner, vendor );
133  break;
134  }
135 
136  case GNC_OWNER_EMPLOYEE:
137  {
138  GncEmployee *employee = NULL;
139 
140  if ( pGuid != NULL )
141  {
142  employee = gncEmployeeLookup( book, pGuid );
143  if ( employee == NULL )
144  {
145  employee = gncEmployeeCreate( book );
146  gncEmployeeSetGUID( employee, &guid );
147  }
148  }
149  gncOwnerInitEmployee( &owner, employee );
150  break;
151  }
152 
153  default:
154  PWARN("Invalid owner type: %d\n", type );
155  }
156 
157  if ( table_row->gobj_param_name != NULL )
158  {
159  qof_instance_increase_editlevel (pObject);
160  g_object_set( pObject, table_row->gobj_param_name, &owner, NULL );
161  qof_instance_decrease_editlevel (pObject);
162  }
163  else
164  {
165  (*setter)( pObject, &owner );
166  }
167 }
168 
169 static void
170 add_owner_col_info_to_list( const GncSqlBackend* be, const GncSqlColumnTableEntry* table_row,
171  GList** pList )
172 {
173  GncSqlColumnInfo* info;
174  gchar* buf;
175 
176  g_return_if_fail( be != NULL );
177  g_return_if_fail( table_row != NULL );
178  g_return_if_fail( pList != NULL );
179 
180  buf = g_strdup_printf( "%s_type", table_row->col_name );
181  info = g_new0( GncSqlColumnInfo, 1 );
182  info->name = buf;
183  info->type = BCT_INT;
184  info->is_primary_key = (table_row->flags & COL_PKEY) ? TRUE : FALSE;
185  info->null_allowed = (table_row->flags & COL_NNUL) ? FALSE : TRUE;
186  info->size = table_row->size;
187  info->is_unicode = FALSE;
188  *pList = g_list_append( *pList, info );
189 
190  buf = g_strdup_printf( "%s_guid", table_row->col_name );
191  info = g_new0( GncSqlColumnInfo, 1 );
192  info->name = buf;
193  info->type = BCT_STRING;
194  info->size = GUID_ENCODING_LENGTH;
195  info->is_primary_key = (table_row->flags & COL_PKEY) ? TRUE : FALSE;
196  info->null_allowed = (table_row->flags & COL_NNUL) ? FALSE : TRUE;
197  info->is_unicode = FALSE;
198  *pList = g_list_append( *pList, info );
199 }
200 
201 static void
202 add_colname_to_list( const GncSqlColumnTableEntry* table_row, GList** pList )
203 {
204  gchar* buf;
205 
206  buf = g_strdup_printf( "%s_type", table_row->col_name );
207  (*pList) = g_list_append( (*pList), buf );
208  buf = g_strdup_printf( "%s_guid", table_row->col_name );
209  (*pList) = g_list_append( (*pList), buf );
210 }
211 
212 static void
213 add_gvalue_owner_to_slist( const GncSqlBackend* be, QofIdTypeConst obj_name,
214  const gpointer pObject, const GncSqlColumnTableEntry* table_row, GSList** pList )
215 {
216  GValue* subfield_value;
217  GncOwner* owner;
218  gchar* buf;
219  const GncGUID* guid;
220  gchar guid_buf[GUID_ENCODING_LENGTH+1];
221  GncOwnerType type;
222  QofInstance* inst = NULL;
223  OwnerGetterFunc getter;
224 
225  g_return_if_fail( be != NULL );
226  g_return_if_fail( obj_name != NULL );
227  g_return_if_fail( pObject != NULL );
228  g_return_if_fail( table_row != NULL );
229 
230  getter = (OwnerGetterFunc)gnc_sql_get_getter( obj_name, table_row );
231  owner = (*getter)( pObject );
232 
233  if ( owner != NULL )
234  {
235  buf = g_strdup_printf( "%s_type", table_row->col_name );
236  subfield_value = g_new0( GValue, 1 );
237  g_value_init( subfield_value, G_TYPE_INT );
238  type = gncOwnerGetType( owner );
239  g_value_set_int( subfield_value, type );
240  (*pList) = g_slist_append( (*pList), subfield_value );
241  g_free( buf );
242 
243  buf = g_strdup_printf( "%s_guid", table_row->col_name );
244  subfield_value = g_new0( GValue, 1 );
245  switch ( type )
246  {
247  case GNC_OWNER_CUSTOMER:
248  inst = QOF_INSTANCE(gncOwnerGetCustomer( owner ));
249  break;
250 
251  case GNC_OWNER_JOB:
252  inst = QOF_INSTANCE(gncOwnerGetJob( owner ));
253  break;
254 
255  case GNC_OWNER_VENDOR:
256  inst = QOF_INSTANCE(gncOwnerGetVendor( owner ));
257  break;
258 
259  case GNC_OWNER_EMPLOYEE:
260  inst = QOF_INSTANCE(gncOwnerGetEmployee( owner ));
261  break;
262 
263  default:
264  PWARN("Invalid owner type: %d\n", type );
265  }
266  g_value_init( subfield_value, G_TYPE_STRING );
267  if ( inst != NULL )
268  {
269  guid = qof_instance_get_guid( inst );
270  if ( guid != NULL )
271  {
272  (void)guid_to_string_buff( guid, guid_buf );
273  g_value_take_string( subfield_value, g_strdup_printf( "%s", guid_buf ) );
274  }
275  }
276  (*pList) = g_slist_append( (*pList), subfield_value );
277  g_free( buf );
278  }
279  else
280  {
281  subfield_value = g_new0( GValue, 1 );
282  g_value_init( subfield_value, G_TYPE_STRING );
283  g_value_set_string( subfield_value, "NULL" );
284  (*pList) = g_slist_append( (*pList), subfield_value );
285  subfield_value = g_new0( GValue, 1 );
286  g_value_init( subfield_value, G_TYPE_STRING );
287  g_value_set_string( subfield_value, "NULL" );
288  (*pList) = g_slist_append( (*pList), subfield_value );
289  }
290 }
291 
292 static GncSqlColumnTypeHandler owner_handler
293 = { load_owner,
294  add_owner_col_info_to_list,
295  add_colname_to_list,
296  add_gvalue_owner_to_slist
297  };
298 
299 /* ================================================================= */
300 void
301 gnc_owner_sql_initialize( void )
302 {
303  gnc_sql_register_col_type_handler( CT_OWNERREF, &owner_handler );
304 }
305 /* ========================== END OF FILE ===================== */
const GncGUID * qof_instance_get_guid(gconstpointer)
QofAccessFunc gnc_sql_get_getter(QofIdTypeConst obj_name, const GncSqlColumnTableEntry *table_row)
#define G_LOG_DOMAIN
Functions providing the SX List as a plugin page.
const gchar * QofIdTypeConst
Definition: qofid.h:87
#define COL_NNUL
gboolean string_to_guid(const gchar *string, GncGUID *guid)
load and save data to SQL
void gnc_sql_register_col_type_handler(const gchar *colType, const GncSqlColumnTypeHandler *handler)
gchar * guid_to_string_buff(const GncGUID *guid, gchar *buff)
gint64 gnc_sql_get_integer_value(const GValue *value)
QofBook * book
Definition: guid.h:65
#define COL_PKEY
#define PWARN(format, args...)
Definition: qoflog.h:243
GncSqlBasicColumnType type
load and save owner data to SQL
Definition: gncJob.c:41
#define GUID_ENCODING_LENGTH
Definition: guid.h:74
const gchar * gobj_param_name
GncOwnerType gncOwnerGetType(const GncOwner *owner)
Definition: gncOwner.c:201
GncJob * gncOwnerGetJob(const GncOwner *owner)
Definition: gncOwner.c:354
void(* QofSetterFunc)(gpointer, gpointer)
Definition: qofclass.h:184
GncVendor * gncOwnerGetVendor(const GncOwner *owner)
Definition: gncOwner.c:361
GncCustomer * gncOwnerGetCustomer(const GncOwner *owner)
Definition: gncOwner.c:347
GncEmployee * gncOwnerGetEmployee(const GncOwner *owner)
Definition: gncOwner.c:368
const gchar * QofLogModule
Definition: qofid.h:89