GnuCash 2.4.99
gnc-address-sql.c
Go to the documentation of this file.
00001 /********************************************************************\
00002  * gnc-address-sql.c -- address sql backend 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-engine.h"
00038 
00039 #include "gncAddress.h"
00040 
00041 #include "gnc-backend-sql.h"
00042 #include "gnc-address-sql.h"
00043 
00044 static QofLogModule log_module = G_LOG_DOMAIN;
00045 
00046 #define ADDRESS_MAX_NAME_LEN 1024
00047 #define ADDRESS_MAX_ADDRESS_LINE_LEN 1024
00048 #define ADDRESS_MAX_PHONE_LEN 128
00049 #define ADDRESS_MAX_FAX_LEN 128
00050 #define ADDRESS_MAX_EMAIL_LEN 256
00051 
00052 static GncSqlColumnTableEntry col_table[] =
00053 {
00054     { "name",  CT_STRING, ADDRESS_MAX_NAME_LEN,         COL_NNUL, "name" },
00055     { "addr1", CT_STRING, ADDRESS_MAX_ADDRESS_LINE_LEN, COL_NNUL, "addr1" },
00056     { "addr2", CT_STRING, ADDRESS_MAX_ADDRESS_LINE_LEN, COL_NNUL, "addr2" },
00057     { "addr3", CT_STRING, ADDRESS_MAX_ADDRESS_LINE_LEN, COL_NNUL, "addr3" },
00058     { "addr4", CT_STRING, ADDRESS_MAX_ADDRESS_LINE_LEN, COL_NNUL, "addr4" },
00059     { "phone", CT_STRING, ADDRESS_MAX_PHONE_LEN,        COL_NNUL, "phone" },
00060     { "fax",   CT_STRING, ADDRESS_MAX_FAX_LEN,          COL_NNUL, "fax" },
00061     { "email", CT_STRING, ADDRESS_MAX_EMAIL_LEN,        COL_NNUL, "email" },
00062     { NULL }
00063 };
00064 
00065 typedef void (*AddressSetterFunc)( gpointer, GncAddress* );
00066 typedef GncAddress* (*AddressGetterFunc)( const gpointer );
00067 
00068 static void
00069 load_address( const GncSqlBackend* be, GncSqlRow* row,
00070               QofSetterFunc setter, gpointer pObject,
00071               const GncSqlColumnTableEntry* table_row )
00072 {
00073     const GValue* val;
00074     gchar* buf;
00075     GncAddress* addr;
00076     AddressSetterFunc a_setter = (AddressSetterFunc)setter;
00077     const GncSqlColumnTableEntry* subtable;
00078     const gchar* s;
00079 
00080     g_return_if_fail( be != NULL );
00081     g_return_if_fail( row != NULL );
00082     g_return_if_fail( pObject != NULL );
00083     g_return_if_fail( table_row != NULL );
00084 
00085     addr = gncAddressCreate( be->book, NULL );
00086     for ( subtable = col_table; subtable->col_name != NULL; subtable++ )
00087     {
00088         buf = g_strdup_printf( "%s_%s", table_row->col_name, subtable->col_name );
00089         val = gnc_sql_row_get_value_at_col_name( row, buf );
00090         g_free( buf );
00091         if ( val == NULL )
00092         {
00093             s = NULL;
00094         }
00095         else
00096         {
00097             s = g_value_get_string( val );
00098         }
00099         if ( subtable->gobj_param_name != NULL )
00100         {
00101             g_object_set( addr, subtable->gobj_param_name, s, NULL );
00102         }
00103         else
00104         {
00105             if ( subtable->qof_param_name != NULL )
00106             {
00107                 setter = qof_class_get_parameter_setter( GNC_ID_ADDRESS, subtable->qof_param_name );
00108             }
00109             else
00110             {
00111                 setter = subtable->setter;
00112             }
00113             (*setter)( addr, (const gpointer)s );
00114         }
00115     }
00116     if ( table_row->gobj_param_name != NULL )
00117     {
00118         g_object_set( pObject, table_row->gobj_param_name, addr, NULL );
00119     }
00120     else
00121     {
00122         (*a_setter)( pObject, addr );
00123     }
00124 }
00125 
00126 static void
00127 add_address_col_info_to_list( const GncSqlBackend* be, const GncSqlColumnTableEntry* table_row,
00128                               GList** pList )
00129 {
00130     GncSqlColumnInfo* info;
00131     gchar* buf;
00132     const GncSqlColumnTableEntry* subtable_row;
00133     const gchar* type;
00134 
00135     g_return_if_fail( be != NULL );
00136     g_return_if_fail( table_row != NULL );
00137     g_return_if_fail( pList != NULL );
00138 
00139     for ( subtable_row = col_table; subtable_row->col_name != NULL; subtable_row++ )
00140     {
00141         buf = g_strdup_printf( "%s_%s", table_row->col_name, subtable_row->col_name );
00142         info = g_new0( GncSqlColumnInfo, 1 );
00143         info->name = buf;
00144         info->type = BCT_STRING;
00145         info->size = subtable_row->size;
00146         info->is_primary_key = (table_row->flags & COL_PKEY) ? TRUE : FALSE;
00147         info->null_allowed = (table_row->flags & COL_NNUL) ? FALSE : TRUE;
00148         info->is_unicode = TRUE;
00149         *pList = g_list_append( *pList, info );
00150     }
00151 }
00152 
00153 static void
00154 add_address_colname_to_list( const GncSqlColumnTableEntry* table_row, GList** pList )
00155 {
00156     gnc_sql_add_subtable_colnames_to_list( table_row, col_table, pList );
00157 }
00158 
00159 static void
00160 get_gvalue_address( const GncSqlBackend* be, QofIdTypeConst obj_name, const gpointer pObject,
00161                     const GncSqlColumnTableEntry* table_row, GValue* value )
00162 {
00163     AddressGetterFunc getter;
00164     GncAddress* addr;
00165 
00166     g_return_if_fail( be != NULL );
00167     g_return_if_fail( obj_name != NULL );
00168     g_return_if_fail( pObject != NULL );
00169     g_return_if_fail( table_row != NULL );
00170     g_return_if_fail( value != NULL );
00171 
00172     memset( value, 0, sizeof( GValue ) );
00173     if ( table_row->gobj_param_name != NULL )
00174     {
00175         g_object_get( pObject, table_row->gobj_param_name, &addr, NULL );
00176     }
00177     else
00178     {
00179         getter = (AddressGetterFunc)gnc_sql_get_getter( obj_name, table_row );
00180         addr = (*getter)( pObject );
00181     }
00182     g_value_init( value, gnc_address_get_type() );
00183     g_value_set_object( value, addr );
00184 }
00185 
00186 static void
00187 add_gvalue_address_to_slist( const GncSqlBackend* be, QofIdTypeConst obj_name,
00188                              const gpointer pObject, const GncSqlColumnTableEntry* table_row, GSList** pList )
00189 {
00190     GValue value;
00191     GValue* subfield_value;
00192     GncAddress* addr;
00193     gchar* s;
00194     QofAccessFunc getter;
00195     const GncSqlColumnTableEntry* subtable_row;
00196 
00197     g_return_if_fail( be != NULL );
00198     g_return_if_fail( obj_name != NULL );
00199     g_return_if_fail( pObject != NULL );
00200     g_return_if_fail( table_row != NULL );
00201 
00202     memset( &value, 0, sizeof( GValue ) );
00203     get_gvalue_address( be, obj_name, pObject, table_row, &value );
00204 
00205     if ( G_VALUE_TYPE(&value) != 0 )
00206     {
00207         addr = g_value_get_object( &value );
00208         for ( subtable_row = col_table; subtable_row->col_name != NULL; subtable_row++ )
00209         {
00210             subfield_value = g_new0( GValue, 1 );
00211             if ( subtable_row->gobj_param_name != NULL )
00212             {
00213                 g_object_get( addr, subtable_row->gobj_param_name, &s, NULL );
00214             }
00215             else
00216             {
00217                 getter = gnc_sql_get_getter( GNC_ID_ADDRESS, subtable_row );
00218                 s = (gchar*)(*getter)( addr, NULL );
00219             }
00220             g_value_init( subfield_value, G_TYPE_STRING );
00221             if ( s )
00222             {
00223                 g_value_set_string( subfield_value, s );
00224             }
00225             else
00226             {
00227                 g_value_set_string( subfield_value, "NULL" );
00228             }
00229             (*pList) = g_slist_append( (*pList), subfield_value );
00230         }
00231     }
00232 }
00233 
00234 static GncSqlColumnTypeHandler address_handler
00235 = { load_address,
00236     add_address_col_info_to_list,
00237     add_address_colname_to_list,
00238     add_gvalue_address_to_slist
00239   };
00240 
00241 /* ================================================================= */
00242 void
00243 gnc_address_sql_initialize( void )
00244 {
00245     gnc_sql_register_col_type_handler( CT_ADDRESS, &address_handler );
00246 }
00247 /* ========================== END OF FILE ===================== */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines