2008-08-01 11:02:07 -05:00
|
|
|
/********************************************************************
|
|
|
|
* gnc-price-sql.c: load and save data to SQL *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License as *
|
|
|
|
* published by the Free Software Foundation; either version 2 of *
|
|
|
|
* the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License*
|
|
|
|
* along with this program; if not, contact: *
|
|
|
|
* *
|
|
|
|
* Free Software Foundation Voice: +1-617-542-5942 *
|
|
|
|
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
|
|
|
|
* Boston, MA 02110-1301, USA gnu@gnu.org *
|
|
|
|
\********************************************************************/
|
|
|
|
/** @file gnc-price-sql.c
|
|
|
|
* @brief load and save data to SQL
|
2009-05-20 20:52:47 -05:00
|
|
|
* @author Copyright (c) 2006-2009 Phil Longstaff <plongstaff@rogers.com>
|
2008-08-01 11:02:07 -05:00
|
|
|
*
|
|
|
|
* This file implements the top-level QofBackend API for saving/
|
|
|
|
* restoring data to/from an SQL db
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
#include "qof.h"
|
|
|
|
#include "gnc-pricedb.h"
|
|
|
|
|
|
|
|
#include "gnc-backend-sql.h"
|
|
|
|
|
|
|
|
#include "gnc-commodity-sql.h"
|
|
|
|
#include "gnc-price-sql.h"
|
|
|
|
#include "gnc-slots-sql.h"
|
|
|
|
|
2009-03-09 12:17:57 -05:00
|
|
|
#if defined( S_SPLINT_S )
|
|
|
|
#include "splint-defs.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*@ unused @*/ static QofLogModule log_module = G_LOG_DOMAIN;
|
2008-08-01 11:02:07 -05:00
|
|
|
|
|
|
|
#define TABLE_NAME "prices"
|
2009-03-05 09:33:40 -06:00
|
|
|
#define TABLE_VERSION 2
|
2008-08-01 11:02:07 -05:00
|
|
|
|
|
|
|
#define PRICE_MAX_SOURCE_LEN 2048
|
|
|
|
#define PRICE_MAX_TYPE_LEN 2048
|
|
|
|
|
|
|
|
static const GncSqlColumnTableEntry col_table[] =
|
|
|
|
{
|
2009-03-09 12:17:57 -05:00
|
|
|
/*@ -full_init_block @*/
|
2008-08-01 11:02:07 -05:00
|
|
|
{ "guid", CT_GUID, 0, COL_NNUL|COL_PKEY, "guid" },
|
|
|
|
{ "commodity_guid", CT_COMMODITYREF, 0, COL_NNUL, NULL, PRICE_COMMODITY },
|
|
|
|
{ "currency_guid", CT_COMMODITYREF, 0, COL_NNUL, NULL, PRICE_CURRENCY },
|
|
|
|
{ "date", CT_TIMESPEC, 0, COL_NNUL, NULL, PRICE_DATE },
|
|
|
|
{ "source", CT_STRING, PRICE_MAX_SOURCE_LEN, 0, NULL, PRICE_SOURCE },
|
|
|
|
{ "type", CT_STRING, PRICE_MAX_TYPE_LEN, 0, NULL, PRICE_TYPE },
|
|
|
|
{ "value", CT_NUMERIC, 0, COL_NNUL, NULL, PRICE_VALUE },
|
|
|
|
{ NULL }
|
2009-03-09 12:17:57 -05:00
|
|
|
/*@ +full_init_block @*/
|
2008-08-01 11:02:07 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
/* ================================================================= */
|
|
|
|
|
2009-03-09 12:17:57 -05:00
|
|
|
static /*@ null @*//*@ dependent @*/ GNCPrice*
|
2008-08-01 11:02:07 -05:00
|
|
|
load_single_price( GncSqlBackend* be, GncSqlRow* row )
|
|
|
|
{
|
|
|
|
GNCPrice* pPrice;
|
|
|
|
|
|
|
|
g_return_val_if_fail( be != NULL, NULL );
|
|
|
|
g_return_val_if_fail( row != NULL, NULL );
|
|
|
|
|
|
|
|
pPrice = gnc_price_create( be->primary_book );
|
|
|
|
|
|
|
|
gnc_price_begin_edit( pPrice );
|
|
|
|
gnc_sql_load_object( be, row, GNC_ID_PRICE, pPrice, col_table );
|
|
|
|
gnc_price_commit_edit( pPrice );
|
|
|
|
|
|
|
|
return pPrice;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
load_all_prices( GncSqlBackend* be )
|
|
|
|
{
|
|
|
|
GncSqlStatement* stmt;
|
|
|
|
GncSqlResult* result;
|
|
|
|
QofBook* pBook;
|
|
|
|
GNCPriceDB* pPriceDB;
|
|
|
|
|
|
|
|
g_return_if_fail( be != NULL );
|
|
|
|
|
|
|
|
pBook = be->primary_book;
|
|
|
|
pPriceDB = gnc_book_get_pricedb( pBook );
|
|
|
|
stmt = gnc_sql_create_select_statement( be, TABLE_NAME );
|
2009-03-09 12:17:57 -05:00
|
|
|
if( stmt != NULL ) {
|
|
|
|
result = gnc_sql_execute_select_statement( be, stmt );
|
|
|
|
gnc_sql_statement_dispose( stmt );
|
|
|
|
if( result != NULL ) {
|
|
|
|
GNCPrice* pPrice;
|
|
|
|
GncSqlRow* row = gnc_sql_result_get_first_row( result );
|
2009-06-15 17:19:35 -05:00
|
|
|
gchar* sql;
|
2009-03-09 12:17:57 -05:00
|
|
|
|
2009-05-20 20:52:47 -05:00
|
|
|
gnc_pricedb_set_bulk_update( pPriceDB, TRUE );
|
2009-03-09 12:17:57 -05:00
|
|
|
while( row != NULL ) {
|
|
|
|
pPrice = load_single_price( be, row );
|
|
|
|
|
|
|
|
if( pPrice != NULL ) {
|
|
|
|
(void)gnc_pricedb_add_price( pPriceDB, pPrice );
|
2009-12-13 13:18:35 -06:00
|
|
|
gnc_price_unref( pPrice );
|
2009-03-09 12:17:57 -05:00
|
|
|
}
|
|
|
|
row = gnc_sql_result_get_next_row( result );
|
|
|
|
}
|
|
|
|
gnc_sql_result_dispose( result );
|
2009-05-20 20:52:47 -05:00
|
|
|
gnc_pricedb_set_bulk_update( pPriceDB, FALSE );
|
2009-03-09 12:17:57 -05:00
|
|
|
|
2009-06-15 17:19:35 -05:00
|
|
|
sql = g_strdup_printf( "SELECT DISTINCT guid FROM %s", TABLE_NAME );
|
|
|
|
gnc_sql_slots_load_for_sql_subquery( be, sql, (BookLookupFn)gnc_price_lookup );
|
|
|
|
g_free( sql );
|
2008-08-01 11:02:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ================================================================= */
|
|
|
|
static void
|
|
|
|
create_prices_tables( GncSqlBackend* be )
|
|
|
|
{
|
|
|
|
gint version;
|
|
|
|
|
|
|
|
g_return_if_fail( be != NULL );
|
|
|
|
|
|
|
|
version = gnc_sql_get_table_version( be, TABLE_NAME );
|
|
|
|
if( version == 0 ) {
|
2009-03-09 12:17:57 -05:00
|
|
|
(void)gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table );
|
2009-02-16 10:22:18 -06:00
|
|
|
} else if( version == 1 ) {
|
|
|
|
/* Upgrade 64 bit int handling */
|
|
|
|
gnc_sql_upgrade_table( be, TABLE_NAME, col_table );
|
2009-03-09 12:17:57 -05:00
|
|
|
(void)gnc_sql_set_table_version( be, TABLE_NAME, TABLE_VERSION );
|
2008-08-01 11:02:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ================================================================= */
|
|
|
|
|
2008-09-27 12:31:23 -05:00
|
|
|
static gboolean
|
2008-08-01 11:02:07 -05:00
|
|
|
save_price( GncSqlBackend* be, QofInstance* inst )
|
|
|
|
{
|
|
|
|
GNCPrice* pPrice = GNC_PRICE(inst);
|
|
|
|
gint op;
|
|
|
|
gboolean is_infant;
|
2008-10-25 14:48:40 -05:00
|
|
|
gboolean is_ok = TRUE;
|
2008-08-01 11:02:07 -05:00
|
|
|
|
2008-09-27 12:31:23 -05:00
|
|
|
g_return_val_if_fail( be != NULL, FALSE );
|
|
|
|
g_return_val_if_fail( inst != NULL, FALSE );
|
|
|
|
g_return_val_if_fail( GNC_IS_PRICE(inst), FALSE );
|
2008-08-01 11:02:07 -05:00
|
|
|
|
|
|
|
is_infant = qof_instance_get_infant( inst );
|
|
|
|
if( qof_instance_get_destroying( inst ) ) {
|
|
|
|
op = OP_DB_DELETE;
|
|
|
|
} else if( be->is_pristine_db || is_infant ) {
|
|
|
|
op = OP_DB_INSERT;
|
|
|
|
} else {
|
|
|
|
op = OP_DB_UPDATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( op != OP_DB_DELETE ) {
|
|
|
|
/* Ensure commodity and currency are in the db */
|
2009-03-09 12:17:57 -05:00
|
|
|
(void)gnc_sql_save_commodity( be, gnc_price_get_commodity( pPrice ) );
|
2008-09-27 12:31:23 -05:00
|
|
|
is_ok = gnc_sql_save_commodity( be, gnc_price_get_currency( pPrice ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( is_ok ) {
|
|
|
|
is_ok = gnc_sql_do_db_operation( be, op, TABLE_NAME, GNC_ID_PRICE, pPrice, col_table );
|
2008-08-01 11:02:07 -05:00
|
|
|
}
|
|
|
|
|
2008-09-27 12:31:23 -05:00
|
|
|
return is_ok;
|
2008-08-01 11:02:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
write_price( GNCPrice* p, gpointer data )
|
|
|
|
{
|
2008-09-27 12:31:23 -05:00
|
|
|
write_objects_t* s = (write_objects_t*)data;
|
2008-08-01 11:02:07 -05:00
|
|
|
|
|
|
|
g_return_val_if_fail( p != NULL, FALSE );
|
|
|
|
g_return_val_if_fail( data != NULL, FALSE );
|
|
|
|
|
2008-09-27 12:31:23 -05:00
|
|
|
if( s->is_ok ) {
|
|
|
|
s->is_ok = save_price( s->be, QOF_INSTANCE(p) );
|
|
|
|
}
|
2008-08-01 11:02:07 -05:00
|
|
|
|
2008-09-27 12:31:23 -05:00
|
|
|
return s->is_ok;
|
2008-08-01 11:02:07 -05:00
|
|
|
}
|
|
|
|
|
2008-09-27 12:31:23 -05:00
|
|
|
static gboolean
|
2008-08-01 11:02:07 -05:00
|
|
|
write_prices( GncSqlBackend* be )
|
|
|
|
{
|
|
|
|
GNCPriceDB* priceDB;
|
2008-09-27 12:31:23 -05:00
|
|
|
write_objects_t data;
|
2008-08-01 11:02:07 -05:00
|
|
|
|
2008-09-27 12:31:23 -05:00
|
|
|
g_return_val_if_fail( be != NULL, FALSE );
|
2008-08-01 11:02:07 -05:00
|
|
|
|
|
|
|
priceDB = gnc_book_get_pricedb( be->primary_book );
|
|
|
|
|
2008-09-27 12:31:23 -05:00
|
|
|
data.be = be;
|
|
|
|
data.is_ok = TRUE;
|
|
|
|
return gnc_pricedb_foreach_price( priceDB, write_price, &data, TRUE );
|
2008-08-01 11:02:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ================================================================= */
|
|
|
|
void
|
|
|
|
gnc_sql_init_price_handler( void )
|
|
|
|
{
|
|
|
|
static GncSqlObjectBackend be_data =
|
|
|
|
{
|
|
|
|
GNC_SQL_BACKEND_VERSION,
|
|
|
|
GNC_ID_PRICE,
|
|
|
|
save_price, /* commit */
|
|
|
|
load_all_prices, /* initial_load */
|
|
|
|
create_prices_tables, /* create tables */
|
|
|
|
NULL, NULL, NULL,
|
|
|
|
write_prices /* write */
|
|
|
|
};
|
|
|
|
|
2009-03-09 12:17:57 -05:00
|
|
|
(void)qof_object_register_backend( GNC_ID_PRICE, GNC_SQL_BACKEND, &be_data );
|
2008-08-01 11:02:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ========================== END OF FILE ===================== */
|