2008-08-01 11:02:07 -05:00
|
|
|
/********************************************************************
|
|
|
|
* gnc-schedxaction-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-schedxaction-sql.c
|
2010-03-27 16:02:18 -05:00
|
|
|
* @brief load and save data to SQL
|
2008-08-01 11:02:07 -05: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
|
|
|
|
*/
|
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 "SchedXaction.h"
|
|
|
|
#include "SX-book.h"
|
|
|
|
#include "Recurrence.h"
|
|
|
|
|
2009-03-09 12:17:57 -05:00
|
|
|
#ifdef S_SPLINT_S
|
|
|
|
#include "splint-defs.h"
|
|
|
|
#endif
|
2015-11-28 15:40:55 -06: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"
|
2015-11-28 15:40:55 -06:00
|
|
|
#include "gnc-schedxaction-sql.h"
|
|
|
|
#include "gnc-slots-sql.h"
|
|
|
|
#include "gnc-recurrence-sql.h"
|
|
|
|
#include "gnc-transaction-sql.h"
|
|
|
|
|
2009-03-09 12:17:57 -05:00
|
|
|
|
2008-08-01 11:02:07 -05:00
|
|
|
#define SCHEDXACTION_TABLE "schedxactions"
|
|
|
|
#define TABLE_VERSION 1
|
|
|
|
|
2012-05-26 18:47:34 -05:00
|
|
|
G_GNUC_UNUSED static QofLogModule log_module = G_LOG_DOMAIN;
|
2008-08-01 11:02:07 -05:00
|
|
|
|
|
|
|
#define SX_MAX_NAME_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_STRING>("name", SX_MAX_NAME_LEN, 0, "name"),
|
|
|
|
gnc_sql_make_table_entry<CT_BOOLEAN>("enabled", 0, COL_NNUL, "enabled"),
|
|
|
|
gnc_sql_make_table_entry<CT_GDATE>("start_date", 0, 0, "start-date"),
|
|
|
|
gnc_sql_make_table_entry<CT_GDATE>("end_date", 0, 0, "end-date"),
|
|
|
|
gnc_sql_make_table_entry<CT_GDATE>(
|
|
|
|
"last_occur", 0, 0, "last-occurance-date"),
|
|
|
|
gnc_sql_make_table_entry<CT_INT>(
|
|
|
|
"num_occur", 0, COL_NNUL, "num-occurance"),
|
|
|
|
gnc_sql_make_table_entry<CT_INT>("rem_occur", 0, COL_NNUL, "rem-occurance"),
|
|
|
|
gnc_sql_make_table_entry<CT_BOOLEAN>(
|
|
|
|
"auto_create", 0, COL_NNUL, "auto-create"),
|
|
|
|
gnc_sql_make_table_entry<CT_BOOLEAN>(
|
|
|
|
"auto_notify", 0, COL_NNUL, "auto-create-notify"),
|
|
|
|
gnc_sql_make_table_entry<CT_INT>(
|
|
|
|
"adv_creation", 0, COL_NNUL, "advance-creation-days"),
|
|
|
|
gnc_sql_make_table_entry<CT_INT>(
|
|
|
|
"adv_notify", 0, COL_NNUL, "advance-reminder-days"),
|
|
|
|
gnc_sql_make_table_entry<CT_INT>(
|
|
|
|
"instance_count", 0, COL_NNUL, "instance-count"),
|
|
|
|
gnc_sql_make_table_entry<CT_ACCOUNTREF>(
|
|
|
|
"template_act_guid", 0, COL_NNUL, "template-account"),
|
2016-03-12 13:13:05 -06:00
|
|
|
});
|
2008-08-01 11:02:07 -05:00
|
|
|
|
2016-11-03 16:21:11 -05:00
|
|
|
GncSqlSchedXactionBackend::GncSqlSchedXactionBackend() :
|
2017-11-21 20:12:30 -06:00
|
|
|
GncSqlObjectBackend(TABLE_VERSION, GNC_ID_SCHEDXACTION,
|
2016-11-03 16:21:11 -05:00
|
|
|
SCHEDXACTION_TABLE, 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 SchedXaction*
|
2016-10-30 19:56:24 -05:00
|
|
|
load_single_sx (GncSqlBackend* sql_be, GncSqlRow& row)
|
2008-08-01 11:02:07 -05:00
|
|
|
{
|
2010-03-27 16:01:21 -05:00
|
|
|
const GncGUID* guid;
|
2010-03-27 16:02:18 -05:00
|
|
|
SchedXaction* pSx;
|
|
|
|
GList* schedule;
|
|
|
|
GDate start_date;
|
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
|
|
|
guid = gnc_sql_load_guid (sql_be, row);
|
2016-03-12 16:04:40 -06:00
|
|
|
g_assert (guid != NULL);
|
2016-10-30 19:56:24 -05:00
|
|
|
pSx = xaccSchedXactionMalloc (sql_be->book());
|
2008-08-01 11:02:07 -05:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
gnc_sx_begin_edit (pSx);
|
2016-10-30 19:56:24 -05:00
|
|
|
gnc_sql_load_object (sql_be, row, GNC_SX_ID, pSx, col_table);
|
|
|
|
schedule = gnc_sql_recurrence_load_list (sql_be, guid);
|
2016-03-12 16:04:40 -06:00
|
|
|
gnc_sx_set_schedule (pSx, schedule);
|
|
|
|
gnc_sx_commit_edit (pSx);
|
2016-10-30 19:56:24 -05:00
|
|
|
gnc_sql_transaction_load_tx_for_account (sql_be, pSx->template_acct);
|
2008-08-01 11:02:07 -05:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
g_object_get (pSx, "start-date", &start_date, NULL);
|
2010-03-01 13:08:00 -06:00
|
|
|
|
2008-08-01 11:02:07 -05:00
|
|
|
return pSx;
|
|
|
|
}
|
|
|
|
|
2018-03-24 19:55:03 -05:00
|
|
|
/* Because SchedXaction doesn't define a lookup function: */
|
|
|
|
static inline SchedXaction*
|
|
|
|
gnc_sx_lookup (const GncGUID *guid, const QofBook *book)
|
|
|
|
{
|
|
|
|
QOF_BOOK_RETURN_ENTITY(book, guid, GNC_ID_SCHEDXACTION, SchedXaction);
|
|
|
|
}
|
|
|
|
|
2016-06-17 12:28:02 -05:00
|
|
|
void
|
2016-10-30 19:56:24 -05:00
|
|
|
GncSqlSchedXactionBackend::load_all (GncSqlBackend* sql_be)
|
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
|
|
|
|
2018-03-24 19:55:03 -05:00
|
|
|
std::string sql("SELECT * FROM " SCHEDXACTION_TABLE);
|
|
|
|
auto stmt = sql_be->create_statement_from_sql(sql);
|
|
|
|
if (stmt == nullptr) return;
|
2016-10-30 19:56:24 -05:00
|
|
|
auto result = sql_be->execute_select_statement(stmt);
|
2016-04-09 17:31:36 -05:00
|
|
|
SchedXactions* sxes;
|
2016-10-30 19:56:24 -05:00
|
|
|
sxes = gnc_book_get_schedxactions (sql_be->book());
|
2016-04-09 17:31:36 -05:00
|
|
|
|
|
|
|
for (auto row : *result)
|
2010-03-27 16:02:18 -05:00
|
|
|
{
|
2016-04-09 17:31:36 -05:00
|
|
|
SchedXaction* sx;
|
2010-03-27 16:02:18 -05:00
|
|
|
|
2016-10-30 19:56:24 -05:00
|
|
|
sx = load_single_sx (sql_be, row);
|
2016-07-24 12:39:14 -05:00
|
|
|
if (sx != nullptr)
|
2016-04-09 17:31:36 -05:00
|
|
|
gnc_sxes_add_sx (sxes, sx);
|
|
|
|
}
|
2018-03-24 19:55:03 -05:00
|
|
|
std::string pkey(col_table[0]->name());
|
|
|
|
sql = "SELECT DISTINCT ";
|
|
|
|
sql += pkey + " FROM " SCHEDXACTION_TABLE;
|
|
|
|
gnc_sql_slots_load_for_sql_subquery (sql_be, sql,
|
|
|
|
(BookLookupFn)gnc_sx_lookup);
|
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
|
|
|
GncSqlSchedXactionBackend::commit (GncSqlBackend* sql_be, QofInstance* inst)
|
2008-08-01 11:02:07 -05:00
|
|
|
{
|
|
|
|
SchedXaction* pSx;
|
2010-03-27 16:01:21 -05:00
|
|
|
const GncGUID* guid;
|
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;
|
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);
|
2016-03-12 16:04:40 -06:00
|
|
|
g_return_val_if_fail (inst != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (GNC_IS_SX (inst), FALSE);
|
2008-08-01 11:02:07 -05:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
pSx = GNC_SX (inst);
|
2008-08-01 11:02:07 -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-11-13 14:41:32 -06:00
|
|
|
is_ok = sql_be->do_db_operation(op, SCHEDXACTION_TABLE, GNC_SX_ID, pSx,
|
|
|
|
col_table);
|
2016-03-12 16:04:40 -06:00
|
|
|
guid = qof_instance_get_guid (inst);
|
|
|
|
if (op == OP_DB_INSERT || op == OP_DB_UPDATE)
|
2010-03-27 16:02:18 -05:00
|
|
|
{
|
2016-10-30 19:56:24 -05:00
|
|
|
gnc_sql_recurrence_save_list (sql_be, guid, gnc_sx_get_schedule (pSx));
|
2010-03-27 16:02:18 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-30 19:56:24 -05:00
|
|
|
gnc_sql_recurrence_delete (sql_be, guid);
|
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
|
|
|
{
|
|
|
|
// Now, commit any slots
|
2016-03-12 16:04:40 -06:00
|
|
|
if (op == OP_DB_INSERT || op == OP_DB_UPDATE)
|
2010-03-27 16:02:18 -05:00
|
|
|
{
|
2016-10-30 19:56:24 -05:00
|
|
|
is_ok = gnc_sql_slots_save (sql_be, guid, is_infant, inst);
|
2010-03-27 16:02:18 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-30 19:56:24 -05:00
|
|
|
is_ok = gnc_sql_slots_delete (sql_be, guid);
|
2010-03-27 16:02:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return is_ok;
|
2008-08-01 11:02:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ========================== END OF FILE ===================== */
|