GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gnc-lots-sql.c
Go to the documentation of this file.
1 /********************************************************************
2  * gnc-lots-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 #include "Account.h"
35 #include "gnc-lot.h"
36 
37 #include "gnc-backend-sql.h"
38 #include "gnc-slots-sql.h"
39 
40 #include "gnc-lots-sql.h"
41 
42 #if defined( S_SPLINT_S )
43 #include "splint-defs.h"
44 #endif
45 
46 /*@ unused @*/ static QofLogModule log_module = G_LOG_DOMAIN;
47 
48 #define TABLE_NAME "lots"
49 #define TABLE_VERSION 2
50 
51 static /*@ dependent @*//*@ null @*/ gpointer get_lot_account( gpointer pObject );
52 static void set_lot_account( gpointer pObject, /*@ null @*/ gpointer pValue );
53 
54 static const GncSqlColumnTableEntry col_table[] =
55 {
56  /*@ -full_init_block @*/
57  { "guid", CT_GUID, 0, COL_NNUL | COL_PKEY, "guid" },
58  {
59  "account_guid", CT_ACCOUNTREF, 0, 0, NULL, NULL,
60  (QofAccessFunc)get_lot_account, set_lot_account
61  },
62  { "is_closed", CT_BOOLEAN, 0, COL_NNUL, "is-closed" },
63  { NULL }
64  /*@ +full_init_block @*/
65 };
66 
67 /* ================================================================= */
68 static /*@ dependent @*//*@ null @*/ gpointer
69 get_lot_account( gpointer pObject )
70 {
71  const GNCLot* lot;
72  Account* pAccount;
73 
74  g_return_val_if_fail( pObject != NULL, NULL );
75  g_return_val_if_fail( GNC_IS_LOT(pObject), NULL );
76 
77  lot = GNC_LOT(pObject);
78  pAccount = gnc_lot_get_account( lot );
79  return pAccount;
80 }
81 
82 static void
83 set_lot_account( gpointer pObject, /*@ null @*/ gpointer pValue )
84 {
85  GNCLot* lot;
86  Account* pAccount;
87 
88  g_return_if_fail( pObject != NULL && GNC_IS_LOT(pObject) );
89  g_return_if_fail( pValue == NULL || GNC_IS_ACCOUNT(pValue) );
90 
91  lot = GNC_LOT(pObject);
92  pAccount = GNC_ACCOUNT(pValue);
93  if ( pAccount != NULL )
94  {
95  xaccAccountInsertLot( pAccount, lot );
96  }
97 }
98 
99 static /*@ dependent @*//*@ null @*/ GNCLot*
100 load_single_lot( GncSqlBackend* be, GncSqlRow* row )
101 {
102  GNCLot* lot;
103 
104  g_return_val_if_fail( be != NULL, NULL );
105  g_return_val_if_fail( row != NULL, NULL );
106 
107  lot = gnc_lot_new( be->book );
108 
109  gnc_lot_begin_edit( lot );
110  gnc_sql_load_object( be, row, GNC_ID_LOT, lot, col_table );
111  gnc_lot_commit_edit( lot );
112 
113  return lot;
114 }
115 
116 static void
117 load_all_lots( GncSqlBackend* be )
118 {
119  GncSqlStatement* stmt;
120  GncSqlResult* result;
121 
122  g_return_if_fail( be != NULL );
123 
124  stmt = gnc_sql_create_select_statement( be, TABLE_NAME );
125  if ( stmt != NULL )
126  {
127  result = gnc_sql_execute_select_statement( be, stmt );
128  gnc_sql_statement_dispose( stmt );
129  if ( result != NULL )
130  {
131  GncSqlRow* row = gnc_sql_result_get_first_row( result );
132  gchar* sql;
133 
134  while ( row != NULL )
135  {
136  load_single_lot( be, row );
137  row = gnc_sql_result_get_next_row( result );
138  }
139  gnc_sql_result_dispose( result );
140 
141  sql = g_strdup_printf( "SELECT DISTINCT guid FROM %s", TABLE_NAME );
142  gnc_sql_slots_load_for_sql_subquery( be, sql, (BookLookupFn)gnc_lot_lookup );
143  g_free( sql );
144  }
145  }
146 }
147 
148 /* ================================================================= */
149 static void
150 create_lots_tables( GncSqlBackend* be )
151 {
152  gint version;
153 
154  g_return_if_fail( be != NULL );
155 
156  version = gnc_sql_get_table_version( be, TABLE_NAME );
157  if ( version == 0 )
158  {
159  /* The table doesn't exist, so create it */
160  (void)gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table );
161  }
162  else if ( version == 1 )
163  {
164  /* Version 1 -> 2 removes the 'NOT NULL' constraint on the account_guid
165  field.
166 
167  Create a temporary table, copy the data from the old table, delete the
168  old table, then rename the new one. */
169 
170  gnc_sql_upgrade_table( be, TABLE_NAME, col_table );
171  (void)gnc_sql_set_table_version( be, TABLE_NAME, TABLE_VERSION );
172 
173  PINFO("Lots table upgraded from version 1 to version %d\n", TABLE_VERSION);
174  }
175 }
176 
177 /* ================================================================= */
178 
179 static gboolean
180 commit_lot( GncSqlBackend* be, QofInstance* inst )
181 {
182  g_return_val_if_fail( be != NULL, FALSE );
183  g_return_val_if_fail( inst != NULL, FALSE );
184  g_return_val_if_fail( GNC_IS_LOT(inst), FALSE );
185 
186  return gnc_sql_commit_standard_item( be, inst, TABLE_NAME, GNC_ID_LOT, col_table );
187 }
188 
189 static void
190 do_save_lot( QofInstance* inst, gpointer data )
191 {
192  write_objects_t* s = (write_objects_t*)data;
193 
194  if ( s->is_ok )
195  {
196  s->is_ok = commit_lot( s->be, inst );
197  }
198 }
199 
200 static gboolean
201 write_lots( GncSqlBackend* be )
202 {
203  write_objects_t data;
204 
205  g_return_val_if_fail( be != NULL, FALSE );
206 
207  data.be = be;
208  data.is_ok = TRUE;
210  (QofInstanceForeachCB)do_save_lot, &data );
211  return data.is_ok;
212 }
213 
214 /* ================================================================= */
215 static void
216 load_lot_guid( const GncSqlBackend* be, GncSqlRow* row,
217  /*@ null @*/ QofSetterFunc setter, gpointer pObject,
218  const GncSqlColumnTableEntry* table_row )
219 {
220  const GValue* val;
221  GncGUID guid;
222  GNCLot* lot;
223 
224  g_return_if_fail( be != NULL );
225  g_return_if_fail( row != NULL );
226  g_return_if_fail( pObject != NULL );
227  g_return_if_fail( table_row != NULL );
228 
229  val = gnc_sql_row_get_value_at_col_name( row, table_row->col_name );
230  if ( val != NULL && G_VALUE_HOLDS_STRING( val ) && g_value_get_string( val ) != NULL )
231  {
232  (void)string_to_guid( g_value_get_string( val ), &guid );
233  lot = gnc_lot_lookup( &guid, be->book );
234  if ( lot != NULL )
235  {
236  if ( table_row->gobj_param_name != NULL )
237  {
238  qof_instance_increase_editlevel (pObject);
239  g_object_set( pObject, table_row->gobj_param_name, lot, NULL );
240  qof_instance_decrease_editlevel (pObject);
241  }
242  else
243  {
244  g_return_if_fail( setter != NULL );
245  (*setter)( pObject, (const gpointer)lot );
246  }
247  }
248  else
249  {
250  PWARN( "Lot ref '%s' not found", g_value_get_string( val ) );
251  }
252  }
253 }
254 
255 static GncSqlColumnTypeHandler lot_guid_handler
256 = { load_lot_guid,
260  };
261 /* ================================================================= */
262 void
263 gnc_sql_init_lot_handler( void )
264 {
265  static GncSqlObjectBackend be_data =
266  {
267  GNC_SQL_BACKEND_VERSION,
268  GNC_ID_LOT,
269  commit_lot, /* commit */
270  load_all_lots, /* initial_load */
271  create_lots_tables, /* create tables */
272  NULL, NULL, NULL,
273  write_lots /* save all */
274  };
275 
276  (void)qof_object_register_backend( GNC_ID_LOT, GNC_SQL_BACKEND, &be_data );
277 
278  gnc_sql_register_col_type_handler( CT_LOTREF, &lot_guid_handler );
279 }
280 
281 /* ========================== END OF FILE ===================== */
gboolean qof_object_register_backend(QofIdTypeConst type_name, const char *backend_name, gpointer be_data)
gint gnc_sql_get_table_version(const GncSqlBackend *be, const gchar *table_name)
void gnc_sql_upgrade_table(GncSqlBackend *be, const gchar *table_name, const GncSqlColumnTableEntry *col_table)
#define G_LOG_DOMAIN
Functions providing the SX List as a plugin page.
#define PINFO(format, args...)
Definition: qoflog.h:249
void gnc_sql_add_colname_to_list(const GncSqlColumnTableEntry *table_row, GList **pList)
load and save accounts data to SQL
GncSqlStatement * gnc_sql_create_select_statement(GncSqlBackend *be, const gchar *table_name)
#define COL_NNUL
gboolean string_to_guid(const gchar *string, GncGUID *guid)
gboolean gnc_sql_create_table(GncSqlBackend *be, const gchar *table_name, gint table_version, const GncSqlColumnTableEntry *col_table)
load and save data to SQL
void gnc_sql_register_col_type_handler(const gchar *colType, const GncSqlColumnTypeHandler *handler)
void xaccAccountInsertLot(Account *acc, GNCLot *lot)
Definition: Account.c:1920
void gnc_sql_add_objectref_guid_col_info_to_list(const GncSqlBackend *be, const GncSqlColumnTableEntry *table_row, GList **pList)
gpointer(* QofAccessFunc)(gpointer object, const QofParam *param)
Definition: qofclass.h:177
void qof_collection_foreach(const QofCollection *, QofInstanceForeachCB, gpointer user_data)
QofBook * book
Definition: guid.h:65
#define COL_PKEY
#define PWARN(format, args...)
Definition: qoflog.h:243
void gnc_sql_slots_load_for_sql_subquery(GncSqlBackend *be, const gchar *subquery, BookLookupFn lookup_fn)
Account handling public routines.
gboolean gnc_sql_set_table_version(GncSqlBackend *be, const gchar *table_name, gint version)
void gnc_sql_add_gvalue_objectref_guid_to_slist(const GncSqlBackend *be, QofIdTypeConst obj_name, const gpointer pObject, const GncSqlColumnTableEntry *table_row, GSList **pList)
load and save data to SQL
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)
const gchar * gobj_param_name
void(* QofInstanceForeachCB)(QofInstance *, gpointer user_data)
Definition: qofid.h:186
gboolean gnc_sql_commit_standard_item(GncSqlBackend *be, QofInstance *inst, const gchar *tableName, QofIdTypeConst obj_name, const GncSqlColumnTableEntry *col_table)
void(* QofSetterFunc)(gpointer, gpointer)
Definition: qofclass.h:184
QofCollection * qof_book_get_collection(const QofBook *, QofIdType)
Account * gnc_lot_get_account(const GNCLot *lot)
Definition: gnc-lot.c:386
const gchar * QofLogModule
Definition: qofid.h:89