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"
|
|
|
|
{
|
2017-10-26 04:14:21 -05:00
|
|
|
#include <config.h>
|
2008-08-01 11:02:07 -05:00
|
|
|
|
|
|
|
#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
|
|
|
|
2016-11-01 13:58:21 -05:00
|
|
|
#include "gnc-sql-connection.hpp"
|
|
|
|
#include "gnc-sql-backend.hpp"
|
|
|
|
#include "gnc-sql-object-backend.hpp"
|
|
|
|
#include "gnc-sql-column-table-entry.hpp"
|
2008-08-01 11:02:07 -05:00
|
|
|
#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"
|
2017-11-23 13:41:09 -06:00
|
|
|
#define TABLE_VERSION 3
|
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
|
|
|
|
({
|
2016-06-09 16:23:42 -05:00
|
|
|
gnc_sql_make_table_entry<CT_GUID>("guid", 0, COL_NNUL | COL_PKEY, "guid"),
|
|
|
|
gnc_sql_make_table_entry<CT_COMMODITYREF>("commodity_guid", 0, COL_NNUL,
|
|
|
|
"commodity"),
|
|
|
|
gnc_sql_make_table_entry<CT_COMMODITYREF>("currency_guid", 0, COL_NNUL,
|
|
|
|
"currency"),
|
|
|
|
gnc_sql_make_table_entry<CT_TIMESPEC>("date", 0, COL_NNUL, "date"),
|
|
|
|
gnc_sql_make_table_entry<CT_STRING>("source", PRICE_MAX_SOURCE_LEN, 0,
|
|
|
|
"source"),
|
|
|
|
gnc_sql_make_table_entry<CT_STRING>("type", PRICE_MAX_TYPE_LEN, 0, "type"),
|
|
|
|
gnc_sql_make_table_entry<CT_NUMERIC>("value", 0, COL_NNUL, "value")
|
2016-03-12 13:13:05 -06:00
|
|
|
});
|
2008-08-01 11:02:07 -05:00
|
|
|
|
2016-11-03 16:21:11 -05:00
|
|
|
GncSqlPriceBackend::GncSqlPriceBackend() :
|
2017-11-21 20:12:30 -06:00
|
|
|
GncSqlObjectBackend(TABLE_VERSION, GNC_ID_PRICE,
|
2016-11-03 16:21:11 -05:00
|
|
|
TABLE_NAME, col_table) {}
|
2016-06-17 12:28:02 -05:00
|
|
|
|
2008-08-01 11:02:07 -05:00
|
|
|
/* ================================================================= */
|
|
|
|
|
2016-03-12 15:34:22 -06:00
|
|
|
static GNCPrice*
|
2016-10-30 19:56:24 -05:00
|
|
|
load_single_price (GncSqlBackend* sql_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-10-30 19:56:24 -05:00
|
|
|
g_return_val_if_fail (sql_be != NULL, NULL);
|
2008-08-01 11:02:07 -05:00
|
|
|
|
2016-10-30 19:56:24 -05:00
|
|
|
pPrice = gnc_price_create (sql_be->book());
|
2008-08-01 11:02:07 -05:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
gnc_price_begin_edit (pPrice);
|
2016-10-30 19:56:24 -05:00
|
|
|
gnc_sql_load_object (sql_be, row, GNC_ID_PRICE, pPrice, col_table);
|
2016-03-12 16:04:40 -06:00
|
|
|
gnc_price_commit_edit (pPrice);
|
2008-08-01 11:02:07 -05:00
|
|
|
|
|
|
|
return pPrice;
|
|
|
|
}
|
|
|
|
|
2016-06-17 12:28:02 -05:00
|
|
|
void
|
2016-10-30 19:56:24 -05:00
|
|
|
GncSqlPriceBackend::load_all (GncSqlBackend* sql_be)
|
2008-08-01 11:02:07 -05:00
|
|
|
{
|
|
|
|
QofBook* pBook;
|
|
|
|
GNCPriceDB* pPriceDB;
|
|
|
|
|
2016-10-30 19:56:24 -05:00
|
|
|
g_return_if_fail (sql_be != NULL);
|
2008-08-01 11:02:07 -05:00
|
|
|
|
2016-10-30 19:56:24 -05:00
|
|
|
pBook = sql_be->book();
|
2016-03-12 16:04:40 -06:00
|
|
|
pPriceDB = gnc_pricedb_get_db (pBook);
|
2016-07-24 17:16:15 -05:00
|
|
|
std::stringstream sql;
|
|
|
|
sql << "SELECT * FROM " << TABLE_NAME;
|
2016-10-30 19:56:24 -05:00
|
|
|
auto stmt = sql_be->create_statement_from_sql(sql.str());
|
2016-06-18 14:38:11 -05:00
|
|
|
if (stmt != nullptr)
|
2010-03-27 16:02:18 -05:00
|
|
|
{
|
2016-10-30 19:56:24 -05:00
|
|
|
auto result = sql_be->execute_select_statement(stmt);
|
2016-04-09 17:31:36 -05:00
|
|
|
if (result->begin() == result->end())
|
|
|
|
return;
|
2016-06-17 12:28:02 -05:00
|
|
|
|
2016-04-09 17:31:36 -05:00
|
|
|
GNCPrice* pPrice;
|
|
|
|
gchar* sql;
|
|
|
|
|
|
|
|
gnc_pricedb_set_bulk_update (pPriceDB, TRUE);
|
|
|
|
for (auto row : *result)
|
2010-03-27 16:02:18 -05:00
|
|
|
{
|
2016-10-30 19:56:24 -05:00
|
|
|
pPrice = load_single_price (sql_be, row);
|
2010-03-27 16:02:18 -05:00
|
|
|
|
2016-04-09 17:31:36 -05:00
|
|
|
if (pPrice != NULL)
|
2010-03-27 16:02:18 -05:00
|
|
|
{
|
2016-04-09 17:31:36 -05:00
|
|
|
(void)gnc_pricedb_add_price (pPriceDB, pPrice);
|
|
|
|
gnc_price_unref (pPrice);
|
2010-03-27 16:02:18 -05:00
|
|
|
}
|
|
|
|
}
|
2016-04-09 17:31:36 -05:00
|
|
|
gnc_pricedb_set_bulk_update (pPriceDB, FALSE);
|
|
|
|
|
|
|
|
sql = g_strdup_printf ("SELECT DISTINCT guid FROM %s", TABLE_NAME);
|
2016-10-30 19:56:24 -05:00
|
|
|
gnc_sql_slots_load_for_sql_subquery (sql_be, sql, (BookLookupFn)gnc_price_lookup);
|
2016-04-09 17:31:36 -05:00
|
|
|
g_free (sql);
|
2008-08-01 11:02:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ================================================================= */
|
2016-06-17 12:28:02 -05:00
|
|
|
void
|
2016-10-30 19:56:24 -05:00
|
|
|
GncSqlPriceBackend::create_tables (GncSqlBackend* sql_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-10-30 19:56:24 -05:00
|
|
|
g_return_if_fail (sql_be != NULL);
|
2008-08-01 11:02:07 -05:00
|
|
|
|
2016-10-30 19:56:24 -05:00
|
|
|
version = sql_be->get_table_version( TABLE_NAME);
|
2016-03-12 16:04:40 -06:00
|
|
|
if (version == 0)
|
2010-03-27 16:02:18 -05:00
|
|
|
{
|
2016-10-30 19:56:24 -05:00
|
|
|
(void)sql_be->create_table(TABLE_NAME, TABLE_VERSION, col_table);
|
2010-03-27 16:02:18 -05:00
|
|
|
}
|
2017-11-21 20:12:30 -06:00
|
|
|
else if (version < m_version)
|
2010-03-27 16:02:18 -05:00
|
|
|
{
|
2017-11-23 13:41:09 -06:00
|
|
|
/*
|
|
|
|
1->2: Upgrade 64 bit int handling
|
|
|
|
2->3: Use DATETIME instead of TIMESTAMP in MySQL
|
|
|
|
*/
|
2016-10-30 19:56:24 -05:00
|
|
|
sql_be->upgrade_table(TABLE_NAME, col_table);
|
|
|
|
sql_be->set_table_version (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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ================================================================= */
|
|
|
|
|
2016-06-17 12:28:02 -05:00
|
|
|
bool
|
2016-10-30 19:56:24 -05:00
|
|
|
GncSqlPriceBackend::commit (GncSqlBackend* sql_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-10-30 19:56:24 -05:00
|
|
|
g_return_val_if_fail (sql_be != NULL, FALSE);
|
2016-03-12 16:04:40 -06:00
|
|
|
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-10-30 19:56:24 -05:00
|
|
|
else if (sql_be->pristine() || 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-11-13 19:21:06 -06:00
|
|
|
(void)sql_be->save_commodity(gnc_price_get_commodity(pPrice));
|
|
|
|
is_ok = sql_be->save_commodity(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-11-13 14:41:32 -06:00
|
|
|
is_ok = sql_be->do_db_operation(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
|
|
|
}
|
|
|
|
|
2016-06-17 12:28:02 -05:00
|
|
|
gboolean
|
2016-03-12 16:04:40 -06:00
|
|
|
write_price (GNCPrice* p, gpointer data)
|
2008-08-01 11:02:07 -05:00
|
|
|
{
|
2016-06-17 12:28:02 -05:00
|
|
|
auto s = reinterpret_cast<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-06-17 12:28:02 -05:00
|
|
|
s->commit (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
|
|
|
}
|
|
|
|
|
2016-06-17 12:28:02 -05:00
|
|
|
bool
|
2016-10-30 19:56:24 -05:00
|
|
|
GncSqlPriceBackend::write (GncSqlBackend* sql_be)
|
2008-08-01 11:02:07 -05:00
|
|
|
{
|
2016-10-30 19:56:24 -05:00
|
|
|
g_return_val_if_fail (sql_be != NULL, FALSE);
|
|
|
|
write_objects_t data{sql_be, true, this};
|
2008-08-01 11:02:07 -05:00
|
|
|
|
2016-10-30 19:56:24 -05:00
|
|
|
auto priceDB = gnc_pricedb_get_db (sql_be->book());
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/* ========================== END OF FILE ===================== */
|