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
|
2010-03-27 16:02:18 -05:00
|
|
|
* @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
|
|
|
|
*/
|
2015-11-28 15:40:55 -06:00
|
|
|
extern "C"
|
|
|
|
{
|
2008-08-01 11:02:07 -05:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
#include "qof.h"
|
|
|
|
#include "gnc-pricedb.h"
|
|
|
|
|
2015-11-28 15:40:55 -06:00
|
|
|
#if defined( S_SPLINT_S )
|
|
|
|
#include "splint-defs.h"
|
|
|
|
#endif
|
|
|
|
}
|
2008-08-01 11:02:07 -05:00
|
|
|
#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
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
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
|
|
|
|
|
2016-03-12 13:13:05 -06:00
|
|
|
static const EntryVec col_table
|
|
|
|
({
|
2010-03-27 16:02:18 -05:00
|
|
|
{ "guid", CT_GUID, 0, COL_NNUL | COL_PKEY, "guid" },
|
2010-03-01 13:08:00 -06:00
|
|
|
{ "commodity_guid", CT_COMMODITYREF, 0, COL_NNUL, "commodity" },
|
|
|
|
{ "currency_guid", CT_COMMODITYREF, 0, COL_NNUL, "currency" },
|
|
|
|
{ "date", CT_TIMESPEC, 0, COL_NNUL, "date" },
|
2010-02-28 11:36:49 -06:00
|
|
|
{ "source", CT_STRING, PRICE_MAX_SOURCE_LEN, 0, "source" },
|
|
|
|
{ "type", CT_STRING, PRICE_MAX_TYPE_LEN, 0, "type" },
|
2016-03-12 13:13:05 -06:00
|
|
|
{ "value", CT_NUMERIC, 0, COL_NNUL, "value" }
|
|
|
|
});
|
2008-08-01 11:02:07 -05:00
|
|
|
|
|
|
|
/* ================================================================= */
|
|
|
|
|
2016-03-12 15:34:22 -06:00
|
|
|
static GNCPrice*
|
2016-03-12 16:04:40 -06:00
|
|
|
load_single_price (GncSqlBackend* be, GncSqlRow* row)
|
2008-08-01 11:02:07 -05:00
|
|
|
{
|
2010-03-27 16:02:18 -05:00
|
|
|
GNCPrice* pPrice;
|
2008-08-01 11:02:07 -05:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
g_return_val_if_fail (be != NULL, NULL);
|
|
|
|
g_return_val_if_fail (row != NULL, NULL);
|
2008-08-01 11:02:07 -05:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
pPrice = gnc_price_create (be->book);
|
2008-08-01 11:02:07 -05:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
gnc_price_begin_edit (pPrice);
|
|
|
|
gnc_sql_load_object (be, row, GNC_ID_PRICE, pPrice, col_table);
|
|
|
|
gnc_price_commit_edit (pPrice);
|
2008-08-01 11:02:07 -05:00
|
|
|
|
|
|
|
return pPrice;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-03-12 16:04:40 -06:00
|
|
|
load_all_prices (GncSqlBackend* be)
|
2008-08-01 11:02:07 -05:00
|
|
|
{
|
|
|
|
GncSqlStatement* stmt;
|
|
|
|
GncSqlResult* result;
|
|
|
|
QofBook* pBook;
|
|
|
|
GNCPriceDB* pPriceDB;
|
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
g_return_if_fail (be != NULL);
|
2008-08-01 11:02:07 -05:00
|
|
|
|
2011-11-13 15:33:29 -06:00
|
|
|
pBook = be->book;
|
2016-03-12 16:04:40 -06:00
|
|
|
pPriceDB = gnc_pricedb_get_db (pBook);
|
|
|
|
stmt = gnc_sql_create_select_statement (be, TABLE_NAME);
|
|
|
|
if (stmt != NULL)
|
2010-03-27 16:02:18 -05:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
result = gnc_sql_execute_select_statement (be, stmt);
|
|
|
|
gnc_sql_statement_dispose (stmt);
|
|
|
|
if (result != NULL)
|
2010-03-27 16:02:18 -05:00
|
|
|
{
|
|
|
|
GNCPrice* pPrice;
|
2016-03-12 16:04:40 -06:00
|
|
|
GncSqlRow* row = gnc_sql_result_get_first_row (result);
|
2010-03-27 16:02:18 -05:00
|
|
|
gchar* sql;
|
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
gnc_pricedb_set_bulk_update (pPriceDB, TRUE);
|
|
|
|
while (row != NULL)
|
2010-03-27 16:02:18 -05:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
pPrice = load_single_price (be, row);
|
2010-03-27 16:02:18 -05:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
if (pPrice != NULL)
|
2010-03-27 16:02:18 -05:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
(void)gnc_pricedb_add_price (pPriceDB, pPrice);
|
|
|
|
gnc_price_unref (pPrice);
|
2010-03-27 16:02:18 -05:00
|
|
|
}
|
2016-03-12 16:04:40 -06:00
|
|
|
row = gnc_sql_result_get_next_row (result);
|
2010-03-27 16:02:18 -05:00
|
|
|
}
|
2016-03-12 16:04:40 -06:00
|
|
|
gnc_sql_result_dispose (result);
|
|
|
|
gnc_pricedb_set_bulk_update (pPriceDB, FALSE);
|
2010-03-27 16:02:18 -05:00
|
|
|
|
2016-03-12 16:04:40 -06: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);
|
2010-03-27 16:02:18 -05:00
|
|
|
}
|
2008-08-01 11:02:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ================================================================= */
|
|
|
|
static void
|
2016-03-12 16:04:40 -06:00
|
|
|
create_prices_tables (GncSqlBackend* be)
|
2008-08-01 11:02:07 -05:00
|
|
|
{
|
2010-03-27 16:02:18 -05:00
|
|
|
gint version;
|
2008-08-01 11:02:07 -05:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
g_return_if_fail (be != NULL);
|
2008-08-01 11:02:07 -05:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
version = gnc_sql_get_table_version (be, TABLE_NAME);
|
|
|
|
if (version == 0)
|
2010-03-27 16:02:18 -05:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
(void)gnc_sql_create_table (be, TABLE_NAME, TABLE_VERSION, col_table);
|
2010-03-27 16:02:18 -05:00
|
|
|
}
|
2016-03-12 16:04:40 -06:00
|
|
|
else if (version == 1)
|
2010-03-27 16:02:18 -05:00
|
|
|
{
|
|
|
|
/* Upgrade 64 bit int handling */
|
2016-03-12 16:04:40 -06:00
|
|
|
gnc_sql_upgrade_table (be, TABLE_NAME, col_table);
|
|
|
|
(void)gnc_sql_set_table_version (be, TABLE_NAME, TABLE_VERSION);
|
2010-10-24 10:29:08 -05:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
PINFO ("Prices table upgraded from version 1 to version %d\n", TABLE_VERSION);
|
2008-08-01 11:02:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ================================================================= */
|
|
|
|
|
2008-09-27 12:31:23 -05:00
|
|
|
static gboolean
|
2016-03-12 16:04:40 -06:00
|
|
|
save_price (GncSqlBackend* be, QofInstance* inst)
|
2008-08-01 11:02:07 -05:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
GNCPrice* pPrice = GNC_PRICE (inst);
|
2015-11-28 15:40:55 -06:00
|
|
|
E_DB_OPERATION op;
|
2010-03-27 16:02:18 -05:00
|
|
|
gboolean is_infant;
|
|
|
|
gboolean is_ok = TRUE;
|
|
|
|
|
2016-03-12 16:04:40 -06: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);
|
2010-03-27 16:02:18 -05:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
is_infant = qof_instance_get_infant (inst);
|
|
|
|
if (qof_instance_get_destroying (inst))
|
2010-03-27 16:02:18 -05:00
|
|
|
{
|
|
|
|
op = OP_DB_DELETE;
|
|
|
|
}
|
2016-03-12 16:04:40 -06:00
|
|
|
else if (be->is_pristine_db || is_infant)
|
2010-03-27 16:02:18 -05:00
|
|
|
{
|
|
|
|
op = OP_DB_INSERT;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
op = OP_DB_UPDATE;
|
|
|
|
}
|
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
if (op != OP_DB_DELETE)
|
2010-03-27 16:02:18 -05:00
|
|
|
{
|
|
|
|
/* Ensure commodity and currency are in the db */
|
2016-03-12 16:04:40 -06:00
|
|
|
(void)gnc_sql_save_commodity (be, gnc_price_get_commodity (pPrice));
|
|
|
|
is_ok = gnc_sql_save_commodity (be, gnc_price_get_currency (pPrice));
|
2010-03-27 16:02:18 -05:00
|
|
|
}
|
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
if (is_ok)
|
2010-03-27 16:02:18 -05:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
is_ok = gnc_sql_do_db_operation (be, op, TABLE_NAME, GNC_ID_PRICE, pPrice,
|
|
|
|
col_table);
|
2010-03-27 16:02:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return is_ok;
|
2008-08-01 11:02:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2016-03-12 16:04:40 -06:00
|
|
|
write_price (GNCPrice* p, gpointer data)
|
2008-08-01 11:02:07 -05:00
|
|
|
{
|
2008-09-27 12:31:23 -05:00
|
|
|
write_objects_t* s = (write_objects_t*)data;
|
2008-08-01 11:02:07 -05:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
g_return_val_if_fail (p != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (data != NULL, FALSE);
|
2008-08-01 11:02:07 -05:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
if (s->is_ok && gnc_price_get_source (p) != PRICE_SOURCE_TEMP)
|
2010-03-27 16:02:18 -05:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
s->is_ok = save_price (s->be, QOF_INSTANCE (p));
|
2010-03-27 16:02:18 -05:00
|
|
|
}
|
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
|
2016-03-12 16:04:40 -06:00
|
|
|
write_prices (GncSqlBackend* be)
|
2008-08-01 11:02:07 -05:00
|
|
|
{
|
|
|
|
GNCPriceDB* priceDB;
|
2010-03-27 16:02:18 -05:00
|
|
|
write_objects_t data;
|
2008-08-01 11:02:07 -05:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
g_return_val_if_fail (be != NULL, FALSE);
|
2008-08-01 11:02:07 -05:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
priceDB = gnc_pricedb_get_db (be->book);
|
2008-08-01 11:02:07 -05:00
|
|
|
|
2010-03-27 16:02:18 -05:00
|
|
|
data.be = be;
|
|
|
|
data.is_ok = TRUE;
|
2016-03-12 16:04:40 -06:00
|
|
|
return gnc_pricedb_foreach_price (priceDB, write_price, &data, TRUE);
|
2008-08-01 11:02:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ================================================================= */
|
|
|
|
void
|
2016-03-12 16:04:40 -06:00
|
|
|
gnc_sql_init_price_handler (void)
|
2008-08-01 11:02:07 -05:00
|
|
|
{
|
|
|
|
static GncSqlObjectBackend be_data =
|
|
|
|
{
|
|
|
|
GNC_SQL_BACKEND_VERSION,
|
|
|
|
GNC_ID_PRICE,
|
2016-03-12 16:04:40 -06:00
|
|
|
save_price, /* commit */
|
2008-08-01 11:02:07 -05:00
|
|
|
load_all_prices, /* initial_load */
|
2016-03-12 16:04:40 -06:00
|
|
|
create_prices_tables, /* create tables */
|
2010-03-27 16:02:18 -05:00
|
|
|
NULL, NULL, NULL,
|
2016-03-12 16:04:40 -06:00
|
|
|
write_prices /* write */
|
2008-08-01 11:02:07 -05:00
|
|
|
};
|
|
|
|
|
2016-08-04 16:41:40 -05:00
|
|
|
gnc_sql_register_backend(&be_data);
|
2008-08-01 11:02:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ========================== END OF FILE ===================== */
|