GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gnc-schedxaction-sql.c
Go to the documentation of this file.
1 /********************************************************************
2  * gnc-schedxaction-sql.c: load and save data to SQL *
3  * *
4  * This program is free software; you can redistribute it and/or *
5  * modify it under the terms of the GNU General Public License as *
6  * published by the Free Software Foundation; either version 2 of *
7  * the License, or (at your option) any later version. *
8  * *
9  * This program is distributed in the hope that it will be useful, *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12  * GNU General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU General Public License*
15  * along with this program; if not, contact: *
16  * *
17  * Free Software Foundation Voice: +1-617-542-5942 *
18  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
19  * Boston, MA 02110-1301, USA [email protected] *
20 \********************************************************************/
29 #include "config.h"
30 
31 #include <glib.h>
32 
33 #include "qof.h"
34 
35 #include "gnc-backend-sql.h"
36 
37 #include "gnc-schedxaction-sql.h"
38 #include "gnc-slots-sql.h"
39 
40 #include "SchedXaction.h"
41 #include "SX-book.h"
42 #include "Recurrence.h"
43 
44 #include "gnc-recurrence-sql.h"
45 #include "gnc-transaction-sql.h"
46 
47 #ifdef S_SPLINT_S
48 #include "splint-defs.h"
49 #endif
50 
51 #define SCHEDXACTION_TABLE "schedxactions"
52 #define TABLE_VERSION 1
53 
54 G_GNUC_UNUSED static QofLogModule log_module = G_LOG_DOMAIN;
55 
56 #define SX_MAX_NAME_LEN 2048
57 
58 static const GncSqlColumnTableEntry col_table[] =
59 {
60  /*@ -full_init_block @*/
61  { "guid", CT_GUID, 0, COL_NNUL | COL_PKEY, "guid" },
62  { "name", CT_STRING, SX_MAX_NAME_LEN, 0, "name" },
63  { "enabled", CT_BOOLEAN, 0, COL_NNUL, "enabled" },
64  { "start_date", CT_GDATE, 0, 0, "start-date" },
65  { "end_date", CT_GDATE, 0, 0, "end-date" },
66  { "last_occur", CT_GDATE, 0, 0, "last-occurance-date" },
67  { "num_occur", CT_INT, 0, COL_NNUL, "num-occurance" },
68  { "rem_occur", CT_INT, 0, COL_NNUL, "rem-occurance" },
69  { "auto_create", CT_BOOLEAN, 0, COL_NNUL, "auto-create" },
70  { "auto_notify", CT_BOOLEAN, 0, COL_NNUL, "auto-create-notify" },
71  { "adv_creation", CT_INT, 0, COL_NNUL, "advance-creation-days" },
72  { "adv_notify", CT_INT, 0, COL_NNUL, "advance-reminder-days" },
73  { "instance_count", CT_INT, 0, COL_NNUL, "instance-count" },
74  { "template_act_guid", CT_ACCOUNTREF, 0, COL_NNUL, "template-account" },
75  { NULL }
76  /*@ +full_init_block @*/
77 };
78 
79 /* ================================================================= */
80 static /*@ null @*/ SchedXaction*
81 load_single_sx( GncSqlBackend* be, GncSqlRow* row )
82 {
83  const GncGUID* guid;
84  SchedXaction* pSx;
85  GList* schedule;
86  GDate start_date;
87 
88  g_return_val_if_fail( be != NULL, NULL );
89  g_return_val_if_fail( row != NULL, NULL );
90 
91  guid = gnc_sql_load_guid( be, row );
92  g_assert( guid != NULL );
93  pSx = xaccSchedXactionMalloc( be->book );
94 
95  gnc_sx_begin_edit( pSx );
96  gnc_sql_load_object( be, row, GNC_SX_ID, pSx, col_table );
97  schedule = gnc_sql_recurrence_load_list( be, guid );
98  gnc_sx_set_schedule( pSx, schedule );
99  gnc_sx_commit_edit( pSx );
100  gnc_sql_transaction_load_tx_for_account( be, pSx->template_acct );
101 
102  g_object_get(pSx, "start-date", &start_date, NULL);
103 
104  return pSx;
105 }
106 
107 static void
108 load_all_sxes( GncSqlBackend* be )
109 {
110  GncSqlStatement* stmt = NULL;
111  GncSqlResult* result;
112 
113  g_return_if_fail( be != NULL );
114 
115  stmt = gnc_sql_create_select_statement( be, SCHEDXACTION_TABLE );
116  if ( stmt == NULL ) return;
117  result = gnc_sql_execute_select_statement( be, stmt );
118  gnc_sql_statement_dispose( stmt );
119  if ( result != NULL )
120  {
121  GncSqlRow* row;
122  SchedXactions *sxes;
123  GList* list = NULL;
124  sxes = gnc_book_get_schedxactions( be->book );
125 
126  row = gnc_sql_result_get_first_row( result );
127  while ( row != NULL )
128  {
129  SchedXaction* sx;
130 
131  sx = load_single_sx( be, row );
132  if ( sx != NULL )
133  {
134  gnc_sxes_add_sx( sxes, sx );
135  list = g_list_prepend( list, sx );
136  }
137  row = gnc_sql_result_get_next_row( result );
138  }
139  gnc_sql_result_dispose( result );
140 
141  if ( list != NULL )
142  {
143  gnc_sql_slots_load_for_list( be, list );
144  g_list_free( list );
145  }
146  }
147 }
148 
149 /* ================================================================= */
150 static void
151 create_sx_tables( GncSqlBackend* be )
152 {
153  gint version;
154 
155  g_return_if_fail( be != NULL );
156 
157  version = gnc_sql_get_table_version( be, SCHEDXACTION_TABLE );
158  if ( version == 0 )
159  {
160  (void)gnc_sql_create_table( be, SCHEDXACTION_TABLE, TABLE_VERSION, col_table );
161  }
162 }
163 
164 /* ================================================================= */
165 gboolean
166 gnc_sql_save_schedxaction( GncSqlBackend* be, QofInstance* inst )
167 {
168  SchedXaction* pSx;
169  const GncGUID* guid;
170  gint op;
171  gboolean is_infant;
172  gboolean is_ok;
173 
174  g_return_val_if_fail( be != NULL, FALSE );
175  g_return_val_if_fail( inst != NULL, FALSE );
176  g_return_val_if_fail( GNC_IS_SX(inst), FALSE );
177 
178  pSx = GNC_SX(inst);
179 
180  is_infant = qof_instance_get_infant( inst );
181  if ( qof_instance_get_destroying( inst ) )
182  {
183  op = OP_DB_DELETE;
184  }
185  else if ( be->is_pristine_db || is_infant )
186  {
187  op = OP_DB_INSERT;
188  }
189  else
190  {
191  op = OP_DB_UPDATE;
192  }
193  is_ok = gnc_sql_do_db_operation( be, op, SCHEDXACTION_TABLE, GNC_SX_ID, pSx, col_table );
194  guid = qof_instance_get_guid( inst );
195  if ( op == OP_DB_INSERT || op == OP_DB_UPDATE )
196  {
197  gnc_sql_recurrence_save_list( be, guid, gnc_sx_get_schedule( pSx ) );
198  }
199  else
200  {
201  gnc_sql_recurrence_delete( be, guid );
202  }
203 
204  if ( is_ok )
205  {
206  // Now, commit any slots
207  if ( op == OP_DB_INSERT || op == OP_DB_UPDATE )
208  {
209  is_ok = gnc_sql_slots_save( be, guid, is_infant, qof_instance_get_slots( inst ) );
210  }
211  else
212  {
213  is_ok = gnc_sql_slots_delete( be, guid );
214  }
215  }
216 
217  return is_ok;
218 }
219 
220 /* ================================================================= */
221 void
222 gnc_sql_init_schedxaction_handler( void )
223 {
224  static GncSqlObjectBackend be_data =
225  {
226  GNC_SQL_BACKEND_VERSION,
227  GNC_ID_SCHEDXACTION,
228  gnc_sql_save_schedxaction, /* commit */
229  load_all_sxes, /* initial_load */
230  create_sx_tables, /* create_tables */
231  NULL, /* compile_query */
232  NULL, /* run_query */
233  NULL, /* free_query */
234  NULL /* write */
235  };
236 
237  (void)qof_object_register_backend( GNC_ID_SCHEDXACTION, GNC_SQL_BACKEND, &be_data );
238 }
239 /* ========================== END OF FILE ===================== */
gboolean qof_object_register_backend(QofIdTypeConst type_name, const char *backend_name, gpointer be_data)
void gnc_sx_set_schedule(SchedXaction *sx, GList *schedule)
Definition: SchedXaction.c:565
GList * gnc_sx_get_schedule(const SchedXaction *sx)
Definition: SchedXaction.c:559
const GncGUID * qof_instance_get_guid(gconstpointer)
gint gnc_sql_get_table_version(const GncSqlBackend *be, const gchar *table_name)
#define G_LOG_DOMAIN
Functions providing the SX List as a plugin page.
load and save accounts data to SQL
GncSqlStatement * gnc_sql_create_select_statement(GncSqlBackend *be, const gchar *table_name)
#define COL_NNUL
gboolean qof_instance_get_destroying(gconstpointer ptr)
gboolean gnc_sql_create_table(GncSqlBackend *be, const gchar *table_name, gint table_version, const GncSqlColumnTableEntry *col_table)
load and save data to SQL
QofBook * book
Definition: guid.h:65
#define COL_PKEY
const GncGUID * gnc_sql_load_guid(const GncSqlBackend *be, GncSqlRow *row)
load and save accounts data to SQL
void gnc_sql_slots_load_for_list(GncSqlBackend *be, GList *list)
Anchor Scheduled Transaction info in a book. See src/doc/books.txt for design overview.
load and save data to SQL
void gnc_sql_transaction_load_tx_for_account(GncSqlBackend *be, Account *account)
GncSqlResult * gnc_sql_execute_select_statement(GncSqlBackend *be, GncSqlStatement *stmt)
void gnc_sql_load_object(const GncSqlBackend *be, GncSqlRow *row, QofIdTypeConst obj_name, gpointer pObject, const GncSqlColumnTableEntry *table)
gboolean gnc_sql_slots_delete(GncSqlBackend *be, const GncGUID *guid)
gboolean is_pristine_db
gboolean gnc_sql_do_db_operation(GncSqlBackend *be, E_DB_OPERATION op, const gchar *table_name, QofIdTypeConst obj_name, gpointer pObject, const GncSqlColumnTableEntry *table)
SchedXaction * xaccSchedXactionMalloc(QofBook *book)
Definition: SchedXaction.c:404
Scheduled Transactions public handling routines.
gboolean gnc_sql_slots_save(GncSqlBackend *be, const GncGUID *guid, gboolean is_infant, KvpFrame *pFrame)
const gchar * QofLogModule
Definition: qofid.h:89