GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gnc-order-sql.c
Go to the documentation of this file.
1 /********************************************************************\
2  * gnc-order-sql.c -- order sql backend *
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  * *
21 \********************************************************************/
22 
31 #include "config.h"
32 
33 #include <glib.h>
34 #include <stdlib.h>
35 #include <string.h>
36 
37 #include "gnc-backend-sql.h"
38 #include "gnc-slots-sql.h"
39 
40 #include "gncOrderP.h"
41 
42 #include "gnc-order-sql.h"
43 #include "gnc-owner-sql.h"
44 
45 #define _GNC_MOD_NAME GNC_ID_ORDER
46 
47 static QofLogModule log_module = G_LOG_DOMAIN;
48 
49 #define TABLE_NAME "orders"
50 #define TABLE_VERSION 1
51 
52 #define MAX_ID_LEN 2048
53 #define MAX_NOTES_LEN 2048
54 #define MAX_REFERENCE_LEN 2048
55 
56 static GncSqlColumnTableEntry col_table[] =
57 {
58  { "guid", CT_GUID, 0, COL_NNUL | COL_PKEY, "guid" },
59  { "id", CT_STRING, MAX_ID_LEN, COL_NNUL, "id" },
60  { "notes", CT_STRING, MAX_NOTES_LEN, COL_NNUL, "notes" },
61  { "reference", CT_STRING, MAX_REFERENCE_LEN, COL_NNUL, "reference" },
62  { "active", CT_BOOLEAN, 0, COL_NNUL, "order" },
63  { "date_opened", CT_TIMESPEC, 0, COL_NNUL, "date-opened" },
64  { "date_closed", CT_TIMESPEC, 0, COL_NNUL, "date-closed" },
65  { "owner", CT_OWNERREF, 0, COL_NNUL, NULL, ORDER_OWNER },
66  { NULL },
67 };
68 
69 static GncOrder*
70 load_single_order( GncSqlBackend* be, GncSqlRow* row )
71 {
72  const GncGUID* guid;
73  GncOrder* pOrder;
74 
75  g_return_val_if_fail( be != NULL, NULL );
76  g_return_val_if_fail( row != NULL, NULL );
77 
78  guid = gnc_sql_load_guid( be, row );
79  pOrder = gncOrderLookup( be->book, guid );
80  if ( pOrder == NULL )
81  {
82  pOrder = gncOrderCreate( be->book );
83  }
84  gnc_sql_load_object( be, row, GNC_ID_ORDER, pOrder, col_table );
85  qof_instance_mark_clean( QOF_INSTANCE(pOrder) );
86 
87  return pOrder;
88 }
89 
90 static void
91 load_all_orders( GncSqlBackend* be )
92 {
93  GncSqlStatement* stmt;
94  GncSqlResult* result;
95 
96  g_return_if_fail( be != NULL );
97 
98  stmt = gnc_sql_create_select_statement( be, TABLE_NAME );
99  result = gnc_sql_execute_select_statement( be, stmt );
100  gnc_sql_statement_dispose( stmt );
101  if ( result != NULL )
102  {
103  GncSqlRow* row;
104  GList* list = NULL;
105 
106  row = gnc_sql_result_get_first_row( result );
107  while ( row != NULL )
108  {
109  GncOrder* pOrder = load_single_order( be, row );
110  if ( pOrder != NULL )
111  {
112  list = g_list_append( list, pOrder );
113  }
114  row = gnc_sql_result_get_next_row( result );
115  }
116  gnc_sql_result_dispose( result );
117 
118  if ( list != NULL )
119  {
120  gnc_sql_slots_load_for_list( be, list );
121  g_list_free( list );
122  }
123  }
124 }
125 
126 /* ================================================================= */
127 static void
128 create_order_tables( GncSqlBackend* be )
129 {
130  gint version;
131 
132  g_return_if_fail( be != NULL );
133 
134  version = gnc_sql_get_table_version( be, TABLE_NAME );
135  if ( version == 0 )
136  {
137  gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table );
138  }
139 }
140 
141 /* ================================================================= */
142 static gboolean
143 save_order( GncSqlBackend* be, QofInstance* inst )
144 {
145  g_return_val_if_fail( inst != NULL, FALSE );
146  g_return_val_if_fail( GNC_IS_ORDER(inst), FALSE );
147  g_return_val_if_fail( be != NULL, FALSE );
148 
149  return gnc_sql_commit_standard_item( be, inst, TABLE_NAME, GNC_ID_ORDER, col_table );
150 }
151 
152 /* ================================================================= */
153 static gboolean
154 order_should_be_saved( GncOrder *order )
155 {
156  const char *id;
157 
158  g_return_val_if_fail( order != NULL, FALSE );
159 
160  /* make sure this is a valid order before we save it -- should have an ID */
161  id = gncOrderGetID( order );
162  if ( id == NULL || *id == '\0' )
163  {
164  return FALSE;
165  }
166 
167  return TRUE;
168 }
169 
170 static void
171 write_single_order( QofInstance *term_p, gpointer data_p )
172 {
173  write_objects_t* s = (write_objects_t*)data_p;
174 
175  g_return_if_fail( term_p != NULL );
176  g_return_if_fail( GNC_IS_ORDER(term_p) );
177  g_return_if_fail( data_p != NULL );
178 
179  if ( s->is_ok && order_should_be_saved( GNC_ORDER(term_p) ) )
180  {
181  s->is_ok = save_order( s->be, term_p );
182  }
183 }
184 
185 static gboolean
186 write_orders( GncSqlBackend* be )
187 {
188  write_objects_t data;
189 
190  g_return_val_if_fail( be != NULL, FALSE );
191 
192  data.be = be;
193  data.is_ok = TRUE;
194  qof_object_foreach( GNC_ID_ORDER, be->book, write_single_order, &data );
195 
196  return data.is_ok;
197 }
198 
199 /* ================================================================= */
200 static void
201 load_order_guid( const GncSqlBackend* be, GncSqlRow* row,
202  QofSetterFunc setter, gpointer pObject,
203  const GncSqlColumnTableEntry* table_row )
204 {
205  const GValue* val;
206  GncGUID guid;
207  GncOrder* order = NULL;
208 
209  g_return_if_fail( be != NULL );
210  g_return_if_fail( row != NULL );
211  g_return_if_fail( pObject != NULL );
212  g_return_if_fail( table_row != NULL );
213 
214  val = gnc_sql_row_get_value_at_col_name( row, table_row->col_name );
215  if ( val != NULL && G_VALUE_HOLDS_STRING( val ) && g_value_get_string( val ) != NULL )
216  {
217  string_to_guid( g_value_get_string( val ), &guid );
218  order = gncOrderLookup( be->book, &guid );
219  if ( order != NULL )
220  {
221  if ( table_row->gobj_param_name != NULL )
222  {
223  qof_instance_increase_editlevel (pObject);
224  g_object_set( pObject, table_row->gobj_param_name, order, NULL );
225  qof_instance_decrease_editlevel (pObject);
226  }
227  else
228  {
229  (*setter)( pObject, (const gpointer)order );
230  }
231  }
232  else
233  {
234  PWARN( "Order ref '%s' not found", g_value_get_string( val ) );
235  }
236  }
237 }
238 
239 static GncSqlColumnTypeHandler order_guid_handler
240 = { load_order_guid,
244  };
245 /* ================================================================= */
246 void
247 gnc_order_sql_initialize( void )
248 {
249  static GncSqlObjectBackend be_data =
250  {
251  GNC_SQL_BACKEND_VERSION,
252  GNC_ID_ORDER,
253  save_order, /* commit */
254  load_all_orders, /* initial_load */
255  create_order_tables, /* create_tables */
256  NULL, NULL, NULL,
257  write_orders /* write */
258  };
259 
260  qof_object_register_backend( GNC_ID_ORDER, GNC_SQL_BACKEND, &be_data );
261 
262  gnc_sql_register_col_type_handler( CT_ORDERREF, &order_guid_handler );
263 }
264 /* ========================== 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)
#define G_LOG_DOMAIN
Functions providing the SX List as a plugin page.
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 gnc_sql_add_objectref_guid_col_info_to_list(const GncSqlBackend *be, const GncSqlColumnTableEntry *table_row, GList **pList)
QofBook * book
Definition: guid.h:65
#define COL_PKEY
#define PWARN(format, args...)
Definition: qoflog.h:243
const GncGUID * gnc_sql_load_guid(const GncSqlBackend *be, GncSqlRow *row)
load and save order data to SQL
load and save owner data to SQL
void gnc_sql_add_gvalue_objectref_guid_to_slist(const GncSqlBackend *be, QofIdTypeConst obj_name, const gpointer pObject, const GncSqlColumnTableEntry *table_row, GSList **pList)
void gnc_sql_slots_load_for_list(GncSqlBackend *be, GList *list)
void qof_object_foreach(QofIdTypeConst type_name, QofBook *book, QofInstanceForeachCB cb, gpointer user_data)
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
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
const gchar * QofLogModule
Definition: qofid.h:89