|
GnuCash 2.4.99
|
00001 /********************************************************************\ 00002 * gnc-owner-sql.c -- owner sql implementation * 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 00039 #include "gnc-owner-sql.h" 00040 #include "gncCustomerP.h" 00041 #include "gncJobP.h" 00042 #include "gncEmployeeP.h" 00043 #include "gncVendorP.h" 00044 00045 static QofLogModule log_module = G_LOG_DOMAIN; 00046 00047 typedef void (*OwnerSetterFunc)( gpointer, GncOwner* ); 00048 typedef GncOwner* (*OwnerGetterFunc)( const gpointer ); 00049 00050 static void 00051 load_owner( const GncSqlBackend* be, GncSqlRow* row, 00052 QofSetterFunc setter, gpointer pObject, 00053 const GncSqlColumnTableEntry* table_row ) 00054 { 00055 const GValue* val; 00056 gchar* buf; 00057 GncOwnerType type; 00058 GncGUID guid; 00059 QofBook* book; 00060 GncOwner owner; 00061 GncGUID* pGuid = NULL; 00062 00063 g_return_if_fail( be != NULL ); 00064 g_return_if_fail( row != NULL ); 00065 g_return_if_fail( pObject != NULL ); 00066 g_return_if_fail( table_row != NULL ); 00067 00068 book = be->book; 00069 buf = g_strdup_printf( "%s_type", table_row->col_name ); 00070 val = gnc_sql_row_get_value_at_col_name( row, buf ); 00071 type = (GncOwnerType)gnc_sql_get_integer_value( val ); 00072 g_free( buf ); 00073 buf = g_strdup_printf( "%s_guid", table_row->col_name ); 00074 val = gnc_sql_row_get_value_at_col_name( row, buf ); 00075 g_free( buf ); 00076 00077 if ( val != NULL && G_VALUE_HOLDS_STRING( val ) && g_value_get_string( val ) != NULL ) 00078 { 00079 string_to_guid( g_value_get_string( val ), &guid ); 00080 pGuid = &guid; 00081 } 00082 00083 switch ( type ) 00084 { 00085 case GNC_OWNER_CUSTOMER: 00086 { 00087 GncCustomer *cust = NULL; 00088 00089 if ( pGuid != NULL ) 00090 { 00091 cust = gncCustomerLookup( book, pGuid ); 00092 if ( cust == NULL ) 00093 { 00094 cust = gncCustomerCreate( book ); 00095 gncCustomerSetGUID( cust, &guid ); 00096 } 00097 } 00098 gncOwnerInitCustomer( &owner, cust ); 00099 break; 00100 } 00101 00102 case GNC_OWNER_JOB: 00103 { 00104 GncJob *job = NULL; 00105 00106 if ( pGuid != NULL ) 00107 { 00108 job = gncJobLookup( book, pGuid ); 00109 if ( job == NULL ) 00110 { 00111 job = gncJobCreate( book ); 00112 gncJobSetGUID( job, &guid ); 00113 } 00114 } 00115 gncOwnerInitJob( &owner, job ); 00116 break; 00117 } 00118 00119 case GNC_OWNER_VENDOR: 00120 { 00121 GncVendor *vendor = NULL; 00122 00123 if ( pGuid != NULL ) 00124 { 00125 vendor = gncVendorLookup( book, pGuid ); 00126 if ( vendor == NULL ) 00127 { 00128 vendor = gncVendorCreate( book ); 00129 gncVendorSetGUID( vendor, &guid ); 00130 } 00131 } 00132 gncOwnerInitVendor( &owner, vendor ); 00133 break; 00134 } 00135 00136 case GNC_OWNER_EMPLOYEE: 00137 { 00138 GncEmployee *employee = NULL; 00139 00140 if ( pGuid != NULL ) 00141 { 00142 employee = gncEmployeeLookup( book, pGuid ); 00143 if ( employee == NULL ) 00144 { 00145 employee = gncEmployeeCreate( book ); 00146 gncEmployeeSetGUID( employee, &guid ); 00147 } 00148 } 00149 gncOwnerInitEmployee( &owner, employee ); 00150 break; 00151 } 00152 00153 default: 00154 PWARN("Invalid owner type: %d\n", type ); 00155 } 00156 00157 if ( table_row->gobj_param_name != NULL ) 00158 { 00159 g_object_set( pObject, table_row->gobj_param_name, &owner, NULL ); 00160 } 00161 else 00162 { 00163 (*setter)( pObject, &owner ); 00164 } 00165 } 00166 00167 static void 00168 add_owner_col_info_to_list( const GncSqlBackend* be, const GncSqlColumnTableEntry* table_row, 00169 GList** pList ) 00170 { 00171 GncSqlColumnInfo* info; 00172 gchar* buf; 00173 const gchar* type; 00174 00175 g_return_if_fail( be != NULL ); 00176 g_return_if_fail( table_row != NULL ); 00177 g_return_if_fail( pList != NULL ); 00178 00179 buf = g_strdup_printf( "%s_type", table_row->col_name ); 00180 info = g_new0( GncSqlColumnInfo, 1 ); 00181 info->name = buf; 00182 info->type = BCT_INT; 00183 info->is_primary_key = (table_row->flags & COL_PKEY) ? TRUE : FALSE; 00184 info->null_allowed = (table_row->flags & COL_NNUL) ? FALSE : TRUE; 00185 info->size = table_row->size; 00186 info->is_unicode = FALSE; 00187 *pList = g_list_append( *pList, info ); 00188 00189 buf = g_strdup_printf( "%s_guid", table_row->col_name ); 00190 info = g_new0( GncSqlColumnInfo, 1 ); 00191 info->name = buf; 00192 info->type = BCT_STRING; 00193 info->size = GUID_ENCODING_LENGTH; 00194 info->is_primary_key = (table_row->flags & COL_PKEY) ? TRUE : FALSE; 00195 info->null_allowed = (table_row->flags & COL_NNUL) ? FALSE : TRUE; 00196 info->is_unicode = FALSE; 00197 *pList = g_list_append( *pList, info ); 00198 } 00199 00200 static void 00201 add_colname_to_list( const GncSqlColumnTableEntry* table_row, GList** pList ) 00202 { 00203 gchar* buf; 00204 00205 buf = g_strdup_printf( "%s_type", table_row->col_name ); 00206 (*pList) = g_list_append( (*pList), buf ); 00207 buf = g_strdup_printf( "%s_guid", table_row->col_name ); 00208 (*pList) = g_list_append( (*pList), buf ); 00209 } 00210 00211 static void 00212 add_gvalue_owner_to_slist( const GncSqlBackend* be, QofIdTypeConst obj_name, 00213 const gpointer pObject, const GncSqlColumnTableEntry* table_row, GSList** pList ) 00214 { 00215 GValue* subfield_value; 00216 GncOwner* owner; 00217 gchar* buf; 00218 const GncGUID* guid; 00219 gchar guid_buf[GUID_ENCODING_LENGTH+1]; 00220 GncOwnerType type; 00221 QofInstance* inst = NULL; 00222 OwnerGetterFunc getter; 00223 00224 g_return_if_fail( be != NULL ); 00225 g_return_if_fail( obj_name != NULL ); 00226 g_return_if_fail( pObject != NULL ); 00227 g_return_if_fail( table_row != NULL ); 00228 00229 getter = (OwnerGetterFunc)gnc_sql_get_getter( obj_name, table_row ); 00230 owner = (*getter)( pObject ); 00231 00232 if ( owner != NULL ) 00233 { 00234 buf = g_strdup_printf( "%s_type", table_row->col_name ); 00235 subfield_value = g_new0( GValue, 1 ); 00236 g_value_init( subfield_value, G_TYPE_INT ); 00237 type = gncOwnerGetType( owner ); 00238 g_value_set_int( subfield_value, type ); 00239 (*pList) = g_slist_append( (*pList), subfield_value ); 00240 g_free( buf ); 00241 00242 buf = g_strdup_printf( "%s_guid", table_row->col_name ); 00243 subfield_value = g_new0( GValue, 1 ); 00244 switch ( type ) 00245 { 00246 case GNC_OWNER_CUSTOMER: 00247 inst = QOF_INSTANCE(gncOwnerGetCustomer( owner )); 00248 break; 00249 00250 case GNC_OWNER_JOB: 00251 inst = QOF_INSTANCE(gncOwnerGetJob( owner )); 00252 break; 00253 00254 case GNC_OWNER_VENDOR: 00255 inst = QOF_INSTANCE(gncOwnerGetVendor( owner )); 00256 break; 00257 00258 case GNC_OWNER_EMPLOYEE: 00259 inst = QOF_INSTANCE(gncOwnerGetEmployee( owner )); 00260 break; 00261 00262 default: 00263 PWARN("Invalid owner type: %d\n", type ); 00264 } 00265 g_value_init( subfield_value, G_TYPE_STRING ); 00266 if ( inst != NULL ) 00267 { 00268 guid = qof_instance_get_guid( inst ); 00269 if ( guid != NULL ) 00270 { 00271 (void)guid_to_string_buff( guid, guid_buf ); 00272 g_value_take_string( subfield_value, g_strdup_printf( "%s", guid_buf ) ); 00273 } 00274 } 00275 (*pList) = g_slist_append( (*pList), subfield_value ); 00276 g_free( buf ); 00277 } 00278 else 00279 { 00280 subfield_value = g_new0( GValue, 1 ); 00281 g_value_init( subfield_value, G_TYPE_STRING ); 00282 g_value_set_string( subfield_value, "NULL" ); 00283 (*pList) = g_slist_append( (*pList), subfield_value ); 00284 subfield_value = g_new0( GValue, 1 ); 00285 g_value_init( subfield_value, G_TYPE_STRING ); 00286 g_value_set_string( subfield_value, "NULL" ); 00287 (*pList) = g_slist_append( (*pList), subfield_value ); 00288 } 00289 } 00290 00291 static GncSqlColumnTypeHandler owner_handler 00292 = { load_owner, 00293 add_owner_col_info_to_list, 00294 add_colname_to_list, 00295 add_gvalue_owner_to_slist 00296 }; 00297 00298 /* ================================================================= */ 00299 void 00300 gnc_owner_sql_initialize( void ) 00301 { 00302 gnc_sql_register_col_type_handler( CT_OWNERREF, &owner_handler ); 00303 } 00304 /* ========================== END OF FILE ===================== */
1.7.4