2008-08-01 16:02:07 +00:00
|
|
|
/********************************************************************
|
|
|
|
|
* gnc-commodity-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-commodity-sql.c
|
2010-03-27 21:02:18 +00:00
|
|
|
* @brief load and save data to SQL
|
2008-08-01 16:02:07 +00:00
|
|
|
* @author Copyright (c) 2006-2008 Phil Longstaff <plongstaff@rogers.com>
|
|
|
|
|
*
|
|
|
|
|
* This file implements the top-level QofBackend API for saving/
|
|
|
|
|
* restoring data to/from an SQL db
|
|
|
|
|
*/
|
2016-06-07 10:39:46 -04:00
|
|
|
#include <guid.hpp>
|
2015-11-28 13:40:55 -08:00
|
|
|
extern "C"
|
|
|
|
|
{
|
2008-08-01 16:02:07 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
|
|
#include "qof.h"
|
|
|
|
|
#include "gnc-commodity.h"
|
2015-11-28 13:40:55 -08:00
|
|
|
}
|
2008-08-01 16:02:07 +00:00
|
|
|
|
2015-11-28 13:40:55 -08:00
|
|
|
#include "gnc-backend-sql.h"
|
2008-08-01 16:02:07 +00:00
|
|
|
#include "gnc-commodity-sql.h"
|
|
|
|
|
#include "gnc-slots-sql.h"
|
|
|
|
|
|
2009-03-09 17:17:57 +00:00
|
|
|
#if defined( S_SPLINT_S )
|
|
|
|
|
#include "splint-defs.h"
|
|
|
|
|
#endif
|
2008-08-01 16:02:07 +00:00
|
|
|
|
2016-03-12 14:04:40 -08:00
|
|
|
static QofLogModule log_module = G_LOG_DOMAIN;
|
2009-03-09 17:17:57 +00:00
|
|
|
|
2016-03-12 14:04:40 -08:00
|
|
|
static gpointer get_quote_source_name (gpointer pObject);
|
|
|
|
|
static void set_quote_source_name (gpointer pObject, gpointer pValue);
|
2008-08-01 16:02:07 +00:00
|
|
|
|
|
|
|
|
#define COMMODITIES_TABLE "commodities"
|
|
|
|
|
#define TABLE_VERSION 1
|
|
|
|
|
|
|
|
|
|
#define COMMODITY_MAX_NAMESPACE_LEN 2048
|
|
|
|
|
#define COMMODITY_MAX_MNEMONIC_LEN 2048
|
|
|
|
|
#define COMMODITY_MAX_FULLNAME_LEN 2048
|
|
|
|
|
#define COMMODITY_MAX_CUSIP_LEN 2048
|
|
|
|
|
#define COMMODITY_MAX_QUOTESOURCE_LEN 2048
|
|
|
|
|
#define COMMODITY_MAX_QUOTE_TZ_LEN 2048
|
|
|
|
|
|
2016-03-12 11:13:05 -08:00
|
|
|
static const EntryVec col_table
|
2010-03-27 21:02:18 +00:00
|
|
|
{
|
2016-06-09 14:23:42 -07:00
|
|
|
gnc_sql_make_table_entry<CT_GUID>(
|
|
|
|
|
"guid", 0, COL_NNUL | COL_PKEY | COL_UNIQUE, "guid"),
|
|
|
|
|
gnc_sql_make_table_entry<CT_STRING>("namespace",
|
|
|
|
|
COMMODITY_MAX_NAMESPACE_LEN, COL_NNUL,
|
|
|
|
|
(QofAccessFunc)gnc_commodity_get_namespace,
|
|
|
|
|
(QofSetterFunc)gnc_commodity_set_namespace),
|
|
|
|
|
gnc_sql_make_table_entry<CT_STRING>(
|
|
|
|
|
"mnemonic", COMMODITY_MAX_MNEMONIC_LEN, COL_NNUL, "mnemonic"),
|
|
|
|
|
gnc_sql_make_table_entry<CT_STRING>(
|
|
|
|
|
"fullname", COMMODITY_MAX_FULLNAME_LEN, 0, "fullname"),
|
|
|
|
|
gnc_sql_make_table_entry<CT_STRING>(
|
|
|
|
|
"cusip", COMMODITY_MAX_CUSIP_LEN, 0, "cusip"),
|
|
|
|
|
gnc_sql_make_table_entry<CT_INT>("fraction", 0, COL_NNUL, "fraction"),
|
|
|
|
|
gnc_sql_make_table_entry<CT_BOOLEAN>(
|
|
|
|
|
"quote_flag", 0, COL_NNUL, "quote_flag"),
|
|
|
|
|
gnc_sql_make_table_entry<CT_STRING>("quote_source",
|
|
|
|
|
COMMODITY_MAX_QUOTESOURCE_LEN, 0,
|
|
|
|
|
(QofAccessFunc)get_quote_source_name,
|
|
|
|
|
set_quote_source_name),
|
|
|
|
|
gnc_sql_make_table_entry<CT_STRING>(
|
|
|
|
|
"quote_tz", COMMODITY_MAX_QUOTE_TZ_LEN, 0, "quote-tz"),
|
2008-08-01 16:02:07 +00:00
|
|
|
};
|
|
|
|
|
|
2016-06-17 10:28:02 -07:00
|
|
|
class GncSqlCommodityBackend : public GncSqlObjectBackend
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
GncSqlCommodityBackend(int version, const std::string& type,
|
|
|
|
|
const std::string& table, const EntryVec& vec) :
|
|
|
|
|
GncSqlObjectBackend(version, type, table, vec) {}
|
|
|
|
|
void load_all(GncSqlBackend*) override;
|
|
|
|
|
bool commit(GncSqlBackend*, QofInstance*) override;
|
|
|
|
|
};
|
|
|
|
|
|
2008-08-01 16:02:07 +00:00
|
|
|
/* ================================================================= */
|
|
|
|
|
|
2016-03-12 13:34:22 -08:00
|
|
|
static gpointer
|
2016-03-12 14:04:40 -08:00
|
|
|
get_quote_source_name (gpointer pObject)
|
2008-08-01 16:02:07 +00:00
|
|
|
{
|
2009-03-09 17:17:57 +00:00
|
|
|
const gnc_commodity* pCommodity;
|
2008-08-01 16:02:07 +00:00
|
|
|
|
2016-03-12 14:04:40 -08:00
|
|
|
g_return_val_if_fail (pObject != NULL, NULL);
|
|
|
|
|
g_return_val_if_fail (GNC_IS_COMMODITY (pObject), NULL);
|
2008-08-01 16:02:07 +00:00
|
|
|
|
2016-03-12 14:04:40 -08:00
|
|
|
pCommodity = GNC_COMMODITY (pObject);
|
|
|
|
|
return (gpointer)gnc_quote_source_get_internal_name (
|
|
|
|
|
gnc_commodity_get_quote_source (pCommodity));
|
2008-08-01 16:02:07 +00:00
|
|
|
}
|
|
|
|
|
|
2010-03-27 21:02:18 +00:00
|
|
|
static void
|
2016-03-12 14:04:40 -08:00
|
|
|
set_quote_source_name (gpointer pObject, gpointer pValue)
|
2008-08-01 16:02:07 +00:00
|
|
|
{
|
2009-03-09 17:17:57 +00:00
|
|
|
gnc_commodity* pCommodity;
|
2008-08-01 16:02:07 +00:00
|
|
|
const gchar* quote_source_name = (const gchar*)pValue;
|
|
|
|
|
gnc_quote_source* quote_source;
|
|
|
|
|
|
2016-03-12 14:04:40 -08:00
|
|
|
g_return_if_fail (pObject != NULL);
|
|
|
|
|
g_return_if_fail (GNC_IS_COMMODITY (pObject));
|
2008-08-01 16:02:07 +00:00
|
|
|
|
2016-03-12 14:04:40 -08:00
|
|
|
if (pValue == NULL) return;
|
2008-08-01 16:02:07 +00:00
|
|
|
|
2016-03-12 14:04:40 -08:00
|
|
|
pCommodity = GNC_COMMODITY (pObject);
|
|
|
|
|
quote_source = gnc_quote_source_lookup_by_internal (quote_source_name);
|
|
|
|
|
gnc_commodity_set_quote_source (pCommodity, quote_source);
|
2008-08-01 16:02:07 +00:00
|
|
|
}
|
|
|
|
|
|
2016-03-12 13:34:22 -08:00
|
|
|
static gnc_commodity*
|
2016-04-09 15:31:36 -07:00
|
|
|
load_single_commodity (GncSqlBackend* be, GncSqlRow& row)
|
2008-08-01 16:02:07 +00:00
|
|
|
{
|
2016-07-21 16:21:59 -07:00
|
|
|
QofBook* pBook = be->book();
|
2008-08-01 16:02:07 +00:00
|
|
|
gnc_commodity* pCommodity;
|
|
|
|
|
|
2016-03-12 14:04:40 -08:00
|
|
|
pCommodity = gnc_commodity_new (pBook, NULL, NULL, NULL, NULL, 100);
|
|
|
|
|
gnc_commodity_begin_edit (pCommodity);
|
|
|
|
|
gnc_sql_load_object (be, row, GNC_ID_COMMODITY, pCommodity, col_table);
|
|
|
|
|
gnc_commodity_commit_edit (pCommodity);
|
2008-08-01 16:02:07 +00:00
|
|
|
|
|
|
|
|
return pCommodity;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-17 10:28:02 -07:00
|
|
|
void
|
|
|
|
|
GncSqlCommodityBackend::load_all (GncSqlBackend* be)
|
2008-08-01 16:02:07 +00:00
|
|
|
{
|
|
|
|
|
gnc_commodity_table* pTable;
|
|
|
|
|
|
2016-07-21 16:21:59 -07:00
|
|
|
pTable = gnc_commodity_table_get_table (be->book());
|
2016-07-24 15:16:15 -07:00
|
|
|
std::stringstream sql;
|
|
|
|
|
sql << "SELECT * FROM " << COMMODITIES_TABLE;
|
|
|
|
|
auto stmt = be->create_statement_from_sql(sql.str());
|
2016-07-24 12:12:27 -07:00
|
|
|
auto result = be->execute_select_statement(stmt);
|
2016-04-09 15:31:36 -07:00
|
|
|
|
|
|
|
|
for (auto row : *result)
|
2010-03-27 21:02:18 +00:00
|
|
|
{
|
2016-04-09 15:31:36 -07:00
|
|
|
auto pCommodity = load_single_commodity (be, row);
|
2008-08-01 16:02:07 +00:00
|
|
|
|
2016-04-09 15:31:36 -07:00
|
|
|
if (pCommodity != NULL)
|
2010-03-27 21:02:18 +00:00
|
|
|
{
|
2016-04-09 15:31:36 -07:00
|
|
|
GncGUID guid;
|
2008-08-01 16:02:07 +00:00
|
|
|
|
2016-04-09 15:31:36 -07:00
|
|
|
guid = *qof_instance_get_guid (QOF_INSTANCE (pCommodity));
|
|
|
|
|
pCommodity = gnc_commodity_table_insert (pTable, pCommodity);
|
|
|
|
|
if (qof_instance_is_dirty (QOF_INSTANCE (pCommodity)))
|
|
|
|
|
gnc_sql_push_commodity_for_postload_processing (be, (gpointer)pCommodity);
|
|
|
|
|
qof_instance_set_guid (QOF_INSTANCE (pCommodity), &guid);
|
2008-08-01 16:02:07 +00:00
|
|
|
}
|
|
|
|
|
|
2016-04-09 15:31:36 -07:00
|
|
|
auto sql = g_strdup_printf ("SELECT DISTINCT guid FROM %s", COMMODITIES_TABLE);
|
2016-03-12 14:04:40 -08:00
|
|
|
gnc_sql_slots_load_for_sql_subquery (be, sql,
|
|
|
|
|
(BookLookupFn)gnc_commodity_find_commodity_by_guid);
|
|
|
|
|
g_free (sql);
|
2008-08-01 16:02:07 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* ================================================================= */
|
2009-08-08 16:19:57 +00:00
|
|
|
static gboolean
|
2016-03-12 14:04:40 -08:00
|
|
|
do_commit_commodity (GncSqlBackend* be, QofInstance* inst,
|
|
|
|
|
gboolean force_insert)
|
2009-08-08 16:19:57 +00:00
|
|
|
{
|
2010-03-27 21:02:18 +00:00
|
|
|
const GncGUID* guid;
|
|
|
|
|
gboolean is_infant;
|
2015-11-28 13:40:55 -08:00
|
|
|
E_DB_OPERATION op;
|
2010-03-27 21:02:18 +00:00
|
|
|
gboolean is_ok;
|
|
|
|
|
|
2016-03-12 14:04:40 -08:00
|
|
|
is_infant = qof_instance_get_infant (inst);
|
|
|
|
|
if (qof_instance_get_destroying (inst))
|
2010-03-27 21:02:18 +00:00
|
|
|
{
|
|
|
|
|
op = OP_DB_DELETE;
|
|
|
|
|
}
|
2016-07-21 16:21:59 -07:00
|
|
|
else if (be->pristine() || is_infant || force_insert)
|
2010-03-27 21:02:18 +00:00
|
|
|
{
|
|
|
|
|
op = OP_DB_INSERT;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
op = OP_DB_UPDATE;
|
|
|
|
|
}
|
2016-03-12 14:04:40 -08:00
|
|
|
is_ok = gnc_sql_do_db_operation (be, op, COMMODITIES_TABLE, GNC_ID_COMMODITY,
|
|
|
|
|
inst, col_table);
|
2009-08-08 16:19:57 +00:00
|
|
|
|
2016-03-12 14:04:40 -08:00
|
|
|
if (is_ok)
|
2010-03-27 21:02:18 +00:00
|
|
|
{
|
|
|
|
|
// Now, commit any slots
|
2016-03-12 14:04:40 -08:00
|
|
|
guid = qof_instance_get_guid (inst);
|
|
|
|
|
if (!qof_instance_get_destroying (inst))
|
2010-03-27 21:02:18 +00:00
|
|
|
{
|
2016-03-12 14:04:40 -08:00
|
|
|
is_ok = gnc_sql_slots_save (be, guid, is_infant, inst);
|
2010-03-27 21:02:18 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-03-12 14:04:40 -08:00
|
|
|
is_ok = gnc_sql_slots_delete (be, guid);
|
2010-03-27 21:02:18 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return is_ok;
|
2009-08-08 16:19:57 +00:00
|
|
|
}
|
|
|
|
|
|
2016-06-17 10:28:02 -07:00
|
|
|
bool
|
|
|
|
|
GncSqlCommodityBackend::commit (GncSqlBackend* be, QofInstance* inst)
|
2008-08-01 16:02:07 +00:00
|
|
|
{
|
2016-03-12 14:04:40 -08:00
|
|
|
g_return_val_if_fail (be != NULL, FALSE);
|
|
|
|
|
g_return_val_if_fail (inst != NULL, FALSE);
|
|
|
|
|
g_return_val_if_fail (GNC_IS_COMMODITY (inst), FALSE);
|
2008-08-01 16:02:07 +00:00
|
|
|
|
2016-03-12 14:04:40 -08:00
|
|
|
return do_commit_commodity (be, inst, FALSE);
|
2008-08-01 16:02:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
2016-03-12 14:04:40 -08:00
|
|
|
is_commodity_in_db (GncSqlBackend* be, gnc_commodity* pCommodity)
|
2008-08-01 16:02:07 +00:00
|
|
|
{
|
2016-03-12 14:04:40 -08:00
|
|
|
g_return_val_if_fail (be != NULL, FALSE);
|
|
|
|
|
g_return_val_if_fail (pCommodity != NULL, FALSE);
|
2008-08-01 16:02:07 +00:00
|
|
|
|
2016-03-12 14:04:40 -08:00
|
|
|
return gnc_sql_object_is_it_in_db (be, COMMODITIES_TABLE, GNC_ID_COMMODITY,
|
|
|
|
|
pCommodity, col_table);
|
2008-08-01 16:02:07 +00:00
|
|
|
}
|
|
|
|
|
|
2008-09-27 17:31:23 +00:00
|
|
|
gboolean
|
2016-03-12 14:04:40 -08:00
|
|
|
gnc_sql_save_commodity (GncSqlBackend* be, gnc_commodity* pCommodity)
|
2008-08-01 16:02:07 +00:00
|
|
|
{
|
2010-03-27 21:02:18 +00:00
|
|
|
gboolean is_ok = TRUE;
|
2008-09-27 17:31:23 +00:00
|
|
|
|
2016-03-12 14:04:40 -08:00
|
|
|
g_return_val_if_fail (be != NULL, FALSE);
|
|
|
|
|
g_return_val_if_fail (pCommodity != NULL, FALSE);
|
2008-08-01 16:02:07 +00:00
|
|
|
|
2016-03-12 14:04:40 -08:00
|
|
|
if (!is_commodity_in_db (be, pCommodity))
|
2010-03-27 21:02:18 +00:00
|
|
|
{
|
2016-03-12 14:04:40 -08:00
|
|
|
is_ok = do_commit_commodity (be, QOF_INSTANCE (pCommodity), TRUE);
|
2008-08-01 16:02:07 +00:00
|
|
|
}
|
2008-09-27 17:31:23 +00:00
|
|
|
|
2010-03-27 21:02:18 +00:00
|
|
|
return is_ok;
|
2008-08-01 16:02:07 +00:00
|
|
|
}
|
|
|
|
|
|
2014-09-04 16:26:32 -07:00
|
|
|
void
|
2016-03-12 14:04:40 -08:00
|
|
|
gnc_sql_commit_commodity (gnc_commodity* pCommodity)
|
2014-09-04 16:26:32 -07:00
|
|
|
{
|
|
|
|
|
g_return_if_fail (pCommodity != NULL);
|
|
|
|
|
g_return_if_fail (GNC_IS_COMMODITY (pCommodity));
|
|
|
|
|
gnc_commodity_begin_edit (pCommodity);
|
|
|
|
|
gnc_commodity_commit_edit (pCommodity);
|
|
|
|
|
}
|
|
|
|
|
|
2008-08-01 16:02:07 +00:00
|
|
|
/* ----------------------------------------------------------------- */
|
2016-06-09 14:23:42 -07:00
|
|
|
template<> void
|
|
|
|
|
GncSqlColumnTableEntryImpl<CT_COMMODITYREF>::load (const GncSqlBackend* be,
|
|
|
|
|
GncSqlRow& row,
|
|
|
|
|
QofIdTypeConst obj_name,
|
|
|
|
|
gpointer pObject) const noexcept
|
2008-08-01 16:02:07 +00:00
|
|
|
{
|
2016-06-09 14:23:42 -07:00
|
|
|
load_from_guid_ref(row, obj_name, pObject,
|
|
|
|
|
[be](GncGUID* g){
|
2016-07-21 16:21:59 -07:00
|
|
|
return gnc_commodity_find_commodity_by_guid(g, be->book());
|
2016-06-09 14:23:42 -07:00
|
|
|
});
|
|
|
|
|
}
|
2010-03-27 21:02:18 +00:00
|
|
|
|
2016-06-09 14:23:42 -07:00
|
|
|
template<> void
|
|
|
|
|
GncSqlColumnTableEntryImpl<CT_COMMODITYREF>::add_to_table(const GncSqlBackend* be,
|
|
|
|
|
ColVec& vec) const noexcept
|
|
|
|
|
{
|
|
|
|
|
add_objectref_guid_to_table(be, vec);
|
2008-08-01 16:02:07 +00:00
|
|
|
}
|
|
|
|
|
|
2016-06-09 14:23:42 -07:00
|
|
|
template<> void
|
|
|
|
|
GncSqlColumnTableEntryImpl<CT_COMMODITYREF>::add_to_query(const GncSqlBackend* be,
|
|
|
|
|
QofIdTypeConst obj_name,
|
|
|
|
|
const gpointer pObject,
|
|
|
|
|
PairVec& vec) const noexcept
|
|
|
|
|
{
|
|
|
|
|
add_objectref_guid_to_query(be, obj_name, pObject, vec);
|
|
|
|
|
}
|
2008-08-01 16:02:07 +00:00
|
|
|
/* ================================================================= */
|
|
|
|
|
void
|
2016-03-12 14:04:40 -08:00
|
|
|
gnc_sql_init_commodity_handler (void)
|
2008-08-01 16:02:07 +00:00
|
|
|
{
|
2016-06-17 10:28:02 -07:00
|
|
|
static GncSqlCommodityBackend be_data =
|
2008-08-01 16:02:07 +00:00
|
|
|
{
|
2016-06-17 10:28:02 -07:00
|
|
|
GNC_SQL_BACKEND_VERSION, GNC_ID_COMMODITY, COMMODITIES_TABLE, col_table};
|
2016-08-04 14:41:40 -07:00
|
|
|
gnc_sql_register_backend(&be_data);
|
2008-08-01 16:02:07 +00:00
|
|
|
}
|
|
|
|
|
/* ========================== END OF FILE ===================== */
|