GnuCash 2.4.99
gnc-backend-sql.h
Go to the documentation of this file.
00001 /********************************************************************
00002  * gnc-backend-sql.h: load and save data to SQL                     *
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 
00039 #ifndef GNC_BACKEND_SQL_H
00040 #define GNC_BACKEND_SQL_H
00041 
00042 #include "qof.h"
00043 #include "qofbackend-p.h"
00044 #include <gmodule.h>
00045 
00046 typedef struct GncSqlConnection GncSqlConnection;
00047 
00053 struct GncSqlBackend
00054 {
00055     QofBackend be;                              
00056     GncSqlConnection* conn;             
00057     /*@ dependent @*/
00058     QofBook *book;      
00059     gboolean loading;                           
00060     gboolean in_query;                  
00061     gboolean is_pristine_db;            
00062     gint obj_total;                             
00063     gint operations_done;                       
00064     GHashTable* versions;                       
00065     const gchar* timespec_format;       
00066 };
00067 typedef struct GncSqlBackend GncSqlBackend;
00068 
00074 void gnc_sql_init( GncSqlBackend* be );
00075 
00082 void gnc_sql_load( GncSqlBackend* be, /*@ dependent @*/ QofBook *book, QofBackendLoadType loadType );
00083 
00090 void gnc_sql_sync_all( GncSqlBackend* be, /*@ dependent @*/ QofBook *book );
00091 
00098 void gnc_sql_begin_edit( GncSqlBackend* be, QofInstance *inst );
00099 
00106 void gnc_sql_rollback_edit( GncSqlBackend* qbe, QofInstance *inst );
00107 
00114 void gnc_sql_commit_edit( GncSqlBackend* qbe, QofInstance *inst );
00115 
00118 typedef struct GncSqlColumnTableEntry GncSqlColumnTableEntry;
00119 typedef struct GncSqlStatement GncSqlStatement;
00120 typedef struct GncSqlResult GncSqlResult;
00121 typedef struct GncSqlRow GncSqlRow;
00122 
00129 struct GncSqlStatement
00130 {
00131     void (*dispose)( /*@ only @*/ GncSqlStatement* );
00132     /*@ dependent @*/
00133     gchar* (*toSql)( GncSqlStatement* );
00134     void (*addWhereCond)( GncSqlStatement*, QofIdTypeConst, gpointer, const GncSqlColumnTableEntry*, GValue* );
00135 };
00136 #define gnc_sql_statement_dispose(STMT) \
00137                 (STMT)->dispose(STMT)
00138 #define gnc_sql_statement_to_sql(STMT) \
00139                 (STMT)->toSql(STMT)
00140 #define gnc_sql_statement_add_where_cond(STMT,TYPENAME,OBJ,COLDESC,VALUE) \
00141                 (STMT)->addWhereCond(STMT, TYPENAME, OBJ, COLDESC, VALUE)
00142 
00149 struct GncSqlConnection
00150 {
00151     void (*dispose)( /*@ only @*/ GncSqlConnection* );
00152     GncSqlResult* (*executeSelectStatement)( GncSqlConnection*, GncSqlStatement* ); 
00153     gint (*executeNonSelectStatement)( GncSqlConnection*, GncSqlStatement* ); 
00154     GncSqlStatement* (*createStatementFromSql)( /*@ observer @*/ GncSqlConnection*, const gchar* );
00155     gboolean (*doesTableExist)( GncSqlConnection*, const gchar* );  
00156     gboolean (*beginTransaction)( GncSqlConnection* ); 
00157     gboolean (*rollbackTransaction)( GncSqlConnection* ); 
00158     gboolean (*commitTransaction)( GncSqlConnection* ); 
00159     gboolean (*createTable)( GncSqlConnection*, const gchar*, GList* ); 
00160     gboolean (*createIndex)( GncSqlConnection*, const gchar*, const gchar*, const GncSqlColumnTableEntry* ); 
00161     gboolean (*addColumnsToTable)( GncSqlConnection*, const gchar* table, GList* ); 
00162     gchar* (*quoteString)( const GncSqlConnection*, gchar* );
00163 };
00164 #define gnc_sql_connection_dispose(CONN) (CONN)->dispose(CONN)
00165 #define gnc_sql_connection_execute_select_statement(CONN,STMT) \
00166                 (CONN)->executeSelectStatement(CONN,STMT)
00167 #define gnc_sql_connection_execute_nonselect_statement(CONN,STMT) \
00168                 (CONN)->executeNonSelectStatement(CONN,STMT)
00169 #define gnc_sql_connection_create_statement_from_sql(CONN,SQL) \
00170                 (CONN)->createStatementFromSql(CONN,SQL)
00171 #define gnc_sql_connection_does_table_exist(CONN,NAME) \
00172                 (CONN)->doesTableExist(CONN,NAME)
00173 #define gnc_sql_connection_begin_transaction(CONN) \
00174                 (CONN)->beginTransaction(CONN)
00175 #define gnc_sql_connection_rollback_transaction(CONN) \
00176                 (CONN)->rollbackTransaction(CONN)
00177 #define gnc_sql_connection_commit_transaction(CONN) \
00178                 (CONN)->commitTransaction(CONN)
00179 #define gnc_sql_connection_create_table(CONN,NAME,COLLIST) \
00180                 (CONN)->createTable(CONN,NAME,COLLIST)
00181 #define gnc_sql_connection_create_index(CONN,INDEXNAME,TABLENAME,COLTABLE) \
00182                 (CONN)->createIndex(CONN,INDEXNAME,TABLENAME,COLTABLE)
00183 #define gnc_sql_connection_add_columns_to_table(CONN,TABLENAME,COLLIST) \
00184                 (CONN)->addColumnsToTable(CONN,TABLENAME,COLLIST)
00185 #define gnc_sql_connection_quote_string(CONN,STR) \
00186                 (CONN)->quoteString(CONN,STR)
00187 
00194 struct GncSqlRow
00195 {
00196     const GValue* (*getValueAtColName)( GncSqlRow*, const gchar* );
00197     void (*dispose)( /*@ only @*/ GncSqlRow* );
00198 };
00199 #define gnc_sql_row_get_value_at_col_name(ROW,N) \
00200                 (ROW)->getValueAtColName(ROW,N)
00201 #define gnc_sql_row_dispose(ROW) \
00202                 (ROW)->dispose(ROW)
00203 
00210 struct GncSqlResult
00211 {
00212     guint (*getNumRows)( GncSqlResult* );
00213     GncSqlRow* (*getFirstRow)( GncSqlResult* );
00214     GncSqlRow* (*getNextRow)( GncSqlResult* );
00215     void (*dispose)( /*@ only @*/ GncSqlResult* );
00216 };
00217 #define gnc_sql_result_get_num_rows(RESULT) \
00218                 (RESULT)->getNumRows(RESULT)
00219 #define gnc_sql_result_get_first_row(RESULT) \
00220                 (RESULT)->getFirstRow(RESULT)
00221 #define gnc_sql_result_get_next_row(RESULT) \
00222                 (RESULT)->getNextRow(RESULT)
00223 #define gnc_sql_result_dispose(RESULT) \
00224                 (RESULT)->dispose(RESULT)
00225 
00240 typedef struct
00241 {
00242     int         version;                
00243     const gchar *       type_name;      
00247     /*@ null @*/
00248     gboolean    (*commit)( GncSqlBackend* be, QofInstance* inst );
00250     /*@ null @*/
00251     void                (*initial_load)( GncSqlBackend* be );
00253     /*@ null @*/
00254     void                (*create_tables)( GncSqlBackend* be );
00256     /*@ null @*/
00257     gpointer    (*compile_query)( GncSqlBackend* be, QofQuery* pQuery );
00259     /*@ null @*/
00260     void                (*run_query)( GncSqlBackend* be, gpointer pQuery );
00262     /*@ null @*/
00263     void                (*free_query)( GncSqlBackend* be, gpointer pQuery );
00267     /*@ null @*/
00268     gboolean    (*write)( GncSqlBackend* be );
00269 } GncSqlObjectBackend;
00270 #define GNC_SQL_BACKEND             "gnc:sql:1"
00271 #define GNC_SQL_BACKEND_VERSION 1
00272 
00276 typedef enum
00277 {
00278     BCT_STRING,
00279     BCT_INT,
00280     BCT_INT64,
00281     BCT_DATE,
00282     BCT_DOUBLE,
00283     BCT_DATETIME
00284 } GncSqlBasicColumnType;
00285 
00292 typedef struct
00293 {
00294     /*@ only @*/ gchar* name;                           
00295     GncSqlBasicColumnType type;                         
00296     gint size;                                                          
00297     gboolean is_unicode;                                        
00298     gboolean is_autoinc;                                        
00299     gboolean is_primary_key;                            
00300     gboolean null_allowed;                                      
00301 } GncSqlColumnInfo;
00302 
00303 // Type for conversion of db row to object.
00304 #define CT_STRING "ct_string"
00305 #define CT_GUID "ct_guid"
00306 #define CT_INT "ct_int"
00307 #define CT_INT64 "ct_int64"
00308 #define CT_TIMESPEC "ct_timespec"
00309 #define CT_GDATE "ct_gdate"
00310 #define CT_NUMERIC "ct_numeric"
00311 #define CT_DOUBLE "ct_double"
00312 #define CT_BOOLEAN "ct_boolean"
00313 #define CT_ACCOUNTREF "ct_accountref"
00314 #define CT_BUDGETREF "ct_budgetref"
00315 #define CT_COMMODITYREF "ct_commodityref"
00316 #define CT_LOTREF "ct_lotref"
00317 #define CT_TXREF "ct_txref"
00318 
00337 struct GncSqlColumnTableEntry
00338 {
00339     /*@ dependent @*/ const gchar* col_name;    
00340     const gchar* col_type;      
00341     gint size;                          
00342 #define COL_PKEY        0x01    
00343 #define COL_NNUL        0x02    
00344 #define COL_UNIQUE      0x04    
00345 #define COL_AUTOINC     0x08    
00346     gint flags;                         
00347     /*@ null @*/
00348     const gchar* gobj_param_name; 
00349     /*@ null @*/
00350     const gchar* qof_param_name;  
00351     /*@ null @*/
00352     QofAccessFunc getter;       
00353     /*@ null @*/
00354     QofSetterFunc setter;       
00355 };
00356 
00357 typedef enum
00358 {
00359     OP_DB_INSERT,
00360     OP_DB_UPDATE,
00361     OP_DB_DELETE
00362 } E_DB_OPERATION;
00363 
00364 typedef void (*GNC_SQL_LOAD_FN)( const GncSqlBackend* be,
00365                                  GncSqlRow* row,
00366                                  /*@ null @*/ QofSetterFunc setter, gpointer pObject,
00367                                  const GncSqlColumnTableEntry* table );
00368 typedef void (*GNC_SQL_ADD_COL_INFO_TO_LIST_FN)( const GncSqlBackend* be,
00369         const GncSqlColumnTableEntry* table_row,
00370         GList** pList );
00371 typedef void (*GNC_SQL_ADD_COLNAME_TO_LIST_FN)( const GncSqlColumnTableEntry* table_row, GList** pList );
00372 typedef void (*GNC_SQL_ADD_GVALUE_TO_SLIST_FN)( const GncSqlBackend* be,
00373         QofIdTypeConst obj_name, const gpointer pObject,
00374         const GncSqlColumnTableEntry* table_row, GSList** pList );
00375 
00384 typedef struct
00385 {
00389     GNC_SQL_LOAD_FN                 load_fn;
00390 
00395     GNC_SQL_ADD_COL_INFO_TO_LIST_FN add_col_info_to_list_fn;
00396 
00400     GNC_SQL_ADD_COLNAME_TO_LIST_FN  add_colname_to_list_fn;
00401 
00405     GNC_SQL_ADD_GVALUE_TO_SLIST_FN      add_gvalue_to_slist_fn;
00406 } GncSqlColumnTypeHandler;
00407 
00415 /*@ null @*/
00416 QofAccessFunc gnc_sql_get_getter( QofIdTypeConst obj_name, const GncSqlColumnTableEntry* table_row );
00417 
00425 void gnc_sql_add_colname_to_list( const GncSqlColumnTableEntry* table_row, GList** pList );
00426 
00438 gboolean gnc_sql_do_db_operation( GncSqlBackend* be,
00439                                   E_DB_OPERATION op,
00440                                   const gchar* table_name,
00441                                   QofIdTypeConst obj_name,
00442                                   gpointer pObject,
00443                                   const GncSqlColumnTableEntry* table );
00444 
00454 /*@ null @*/
00455 GncSqlResult* gnc_sql_execute_select_statement( GncSqlBackend* be, GncSqlStatement* statement );
00456 
00466 /*@ null @*/
00467 GncSqlResult* gnc_sql_execute_select_sql( GncSqlBackend* be, const gchar* sql );
00468 
00476 gint gnc_sql_execute_nonselect_sql( GncSqlBackend* be, const gchar* sql );
00477 
00485 /*@ null @*/
00486 GncSqlStatement* gnc_sql_create_statement_from_sql( GncSqlBackend* be, const gchar* sql );
00487 
00497 void gnc_sql_load_object( const GncSqlBackend* be, GncSqlRow* row,
00498                           /*@ null @*/ QofIdTypeConst obj_name, gpointer pObject,
00499                           const GncSqlColumnTableEntry* table );
00500 
00511 gboolean gnc_sql_object_is_it_in_db( GncSqlBackend* be,
00512                                      const gchar* table_name,
00513                                      QofIdTypeConst obj_name, const gpointer pObject,
00514                                      const GncSqlColumnTableEntry* table );
00515 
00523 gint gnc_sql_get_table_version( const GncSqlBackend* be, const gchar* table_name );
00524 
00525 gboolean gnc_sql_set_table_version( GncSqlBackend* be,
00526                                     const gchar* table_name,
00527                                     gint version );
00528 
00538 gboolean gnc_sql_create_table( GncSqlBackend* be,
00539                                const gchar* table_name,
00540                                gint table_version,
00541                                const GncSqlColumnTableEntry* col_table );
00542 
00552 gboolean gnc_sql_create_temp_table( const GncSqlBackend* be,
00553                                     const gchar* table_name,
00554                                     const GncSqlColumnTableEntry* col_table );
00555 
00565 gboolean gnc_sql_create_index( const GncSqlBackend* be, const gchar* index_name,
00566                                const gchar* table_name, const GncSqlColumnTableEntry* col_table );
00567 
00576 /*@ dependent @*//*@ null @*/
00577 const GncGUID* gnc_sql_load_guid( const GncSqlBackend* be, GncSqlRow* row );
00578 
00587 /*@ dependent @*//*@ null @*/
00588 const GncGUID* gnc_sql_load_tx_guid( const GncSqlBackend* be, GncSqlRow* row );
00589 
00597 /*@ null @*/
00598 GncSqlStatement* gnc_sql_create_select_statement( GncSqlBackend* be,
00599         const gchar* table_name );
00600 
00607 void gnc_sql_register_col_type_handler( const gchar* colType, const GncSqlColumnTypeHandler* handler );
00608 
00618 void gnc_sql_add_gvalue_objectref_guid_to_slist( const GncSqlBackend* be,
00619         QofIdTypeConst obj_name, const gpointer pObject,
00620         const GncSqlColumnTableEntry* table_row, GSList** pList );
00621 
00630 void gnc_sql_add_objectref_guid_col_info_to_list( const GncSqlBackend* be,
00631         const GncSqlColumnTableEntry* table_row, GList** pList );
00632 
00641 guint gnc_sql_append_guid_list_to_sql( GString* str, GList* list, guint maxCount );
00642 
00650 void gnc_sql_add_subtable_colnames_to_list( const GncSqlColumnTableEntry* table_row,
00651         const GncSqlColumnTableEntry* subtable,
00652         GList** pList );
00653 
00662 gchar* gnc_sql_get_sql_value( const GncSqlConnection* conn, const GValue* value );
00663 
00669 void gnc_sql_init_version_info( GncSqlBackend* be );
00670 
00676 void gnc_sql_finalize_version_info( GncSqlBackend* be );
00677 
00689 gboolean gnc_sql_commit_standard_item( GncSqlBackend* be, QofInstance* inst, const gchar* tableName,
00690                                        QofIdTypeConst obj_name, const GncSqlColumnTableEntry* col_table );
00691 
00698 gint64 gnc_sql_get_integer_value( const GValue* value );
00699 
00707 gchar* gnc_sql_convert_timespec_to_string( const GncSqlBackend* be, Timespec ts );
00708 
00719 void gnc_sql_upgrade_table( GncSqlBackend* be, const gchar* table_name,
00720                             const GncSqlColumnTableEntry* col_table );
00721 
00730 gboolean gnc_sql_add_columns_to_table( GncSqlBackend* be, const gchar* table_name,
00731                                        const GncSqlColumnTableEntry* new_col_table );
00732 
00740 void gnc_sql_set_load_order( const gchar** load_order );
00741 
00742 void _retrieve_guid_( gpointer pObject, /*@ null @*/ gpointer pValue );
00743 
00744 /*@ null @*/
00745 gpointer gnc_sql_compile_query( QofBackend* pBEnd, QofQuery* pQuery );
00746 void gnc_sql_free_query( QofBackend* pBEnd, gpointer pQuery );
00747 void gnc_sql_run_query( QofBackend* pBEnd, gpointer pQuery );
00748 
00749 typedef struct
00750 {
00751     /*@ dependent @*/ GncSqlBackend* be;
00752     gboolean is_ok;
00753 } write_objects_t;
00754 
00755 #endif /* GNC_BACKEND_SQL_H */
00756 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines