GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gnc-entry-sql.c
Go to the documentation of this file.
1 /********************************************************************\
2  * gnc-entry-sql.c -- entry 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 "gncEntryP.h"
41 #include "gncOrderP.h"
42 #include "gncInvoiceP.h"
43 #include "gncTaxTableP.h"
44 #include "gnc-bill-term-sql.h"
45 #include "gnc-entry-sql.h"
46 #include "gnc-invoice-sql.h"
47 #include "gnc-order-sql.h"
48 #include "gnc-owner-sql.h"
49 #include "gnc-tax-table-sql.h"
50 
51 #define _GNC_MOD_NAME GNC_ID_ENTRY
52 
53 static QofLogModule log_module = G_LOG_DOMAIN;
54 
55 #define TABLE_NAME "entries"
56 #define TABLE_VERSION 3
57 #define MAX_DESCRIPTION_LEN 2048
58 #define MAX_ACTION_LEN 2048
59 #define MAX_NOTES_LEN 2048
60 #define MAX_DISCTYPE_LEN 2048
61 #define MAX_DISCHOW_LEN 2048
62 
63 static void entry_set_invoice( gpointer pObject, gpointer val );
64 static void entry_set_bill( gpointer pObject, gpointer val );
65 
66 static GncSqlColumnTableEntry col_table[] =
67 {
68  { "guid", CT_GUID, 0, COL_NNUL | COL_PKEY, "guid" },
69  { "date", CT_TIMESPEC, 0, COL_NNUL, NULL, ENTRY_DATE },
70  { "date_entered", CT_TIMESPEC, 0, 0, NULL, ENTRY_DATE_ENTERED },
71  { "description", CT_STRING, MAX_DESCRIPTION_LEN, 0, "description" },
72  { "action", CT_STRING, MAX_ACTION_LEN, 0, NULL, ENTRY_ACTION },
73  { "notes", CT_STRING, MAX_NOTES_LEN, 0, NULL, ENTRY_NOTES },
74  { "quantity", CT_NUMERIC, 0, 0, NULL, ENTRY_QTY },
75  { "i_acct", CT_ACCOUNTREF, 0, 0, NULL, ENTRY_IACCT },
76  { "i_price", CT_NUMERIC, 0, 0, NULL, ENTRY_IPRICE },
77  {
78  "i_discount", CT_NUMERIC, 0, 0, NULL, NULL,
79  (QofAccessFunc)gncEntryGetInvDiscount, (QofSetterFunc)gncEntrySetInvDiscount
80  },
81  {
82  "invoice", CT_INVOICEREF, 0, 0, NULL, NULL,
83  (QofAccessFunc)gncEntryGetInvoice, (QofSetterFunc)entry_set_invoice
84  },
85  { "i_disc_type", CT_STRING, MAX_DISCTYPE_LEN, 0, NULL, ENTRY_INV_DISC_TYPE },
86  { "i_disc_how", CT_STRING, MAX_DISCHOW_LEN, 0, NULL, ENTRY_INV_DISC_HOW },
87  { "i_taxable", CT_BOOLEAN, 0, 0, NULL, ENTRY_INV_TAXABLE },
88  { "i_taxincluded", CT_BOOLEAN, 0, 0, NULL, ENTRY_INV_TAX_INC },
89  {
90  "i_taxtable", CT_TAXTABLEREF, 0, 0, NULL, NULL,
91  (QofAccessFunc)gncEntryGetInvTaxTable, (QofSetterFunc)gncEntrySetInvTaxTable
92  },
93  { "b_acct", CT_ACCOUNTREF, 0, 0, NULL, ENTRY_BACCT },
94  { "b_price", CT_NUMERIC, 0, 0, NULL, ENTRY_BPRICE },
95  {
96  "bill", CT_INVOICEREF, 0, 0, NULL, NULL,
97  (QofAccessFunc)gncEntryGetBill, (QofSetterFunc)entry_set_bill
98  },
99  { "b_taxable", CT_BOOLEAN, 0, 0, NULL, ENTRY_BILL_TAXABLE },
100  { "b_taxincluded", CT_BOOLEAN, 0, 0, NULL, ENTRY_BILL_TAX_INC },
101  {
102  "b_taxtable", CT_TAXTABLEREF, 0, 0, NULL, NULL,
103  (QofAccessFunc)gncEntryGetBillTaxTable, (QofSetterFunc)gncEntrySetBillTaxTable
104  },
105  {
106  "b_paytype", CT_INT, 0, 0, NULL, NULL,
107  (QofAccessFunc)gncEntryGetBillPayment, (QofSetterFunc)gncEntrySetBillPayment
108  },
109  { "billable", CT_BOOLEAN, 0, 0, NULL, ENTRY_BILLABLE },
110  { "billto", CT_OWNERREF, 0, 0, NULL, ENTRY_BILLTO },
111  {
112  "order_guid", CT_ORDERREF, 0, 0, NULL, NULL,
113  (QofAccessFunc)gncEntryGetOrder, (QofSetterFunc)gncEntrySetOrder
114  },
115  { NULL }
116 };
117 
118 static void
119 entry_set_invoice( gpointer pObject, gpointer val )
120 {
121  GncEntry* entry;
122  GncInvoice* invoice;
123 
124  g_return_if_fail( pObject != NULL );
125  g_return_if_fail( GNC_IS_ENTRY(pObject) );
126  g_return_if_fail( val != NULL );
127  g_return_if_fail( GNC_IS_INVOICE(val) );
128 
129  entry = GNC_ENTRY(pObject);
130  invoice = GNC_INVOICE(val);
131 
132  gncInvoiceAddEntry( invoice, entry );
133 }
134 
135 static void
136 entry_set_bill( gpointer pObject, gpointer val )
137 {
138  GncEntry* entry;
139  GncInvoice* bill;
140 
141  g_return_if_fail( pObject != NULL );
142  g_return_if_fail( GNC_IS_ENTRY(pObject) );
143  g_return_if_fail( val != NULL );
144  g_return_if_fail( GNC_IS_INVOICE(val) );
145 
146  entry = GNC_ENTRY(pObject);
147  bill = GNC_INVOICE(val);
148 
149  gncBillAddEntry( bill, entry );
150 }
151 
152 static GncEntry*
153 load_single_entry( GncSqlBackend* be, GncSqlRow* row )
154 {
155  const GncGUID* guid;
156  GncEntry* pEntry;
157 
158  g_return_val_if_fail( be != NULL, NULL );
159  g_return_val_if_fail( row != NULL, NULL );
160 
161  guid = gnc_sql_load_guid( be, row );
162  pEntry = gncEntryLookup( be->book, guid );
163  if ( pEntry == NULL )
164  {
165  pEntry = gncEntryCreate( be->book );
166  }
167  gnc_sql_load_object( be, row, GNC_ID_ENTRY, pEntry, col_table );
168  qof_instance_mark_clean( QOF_INSTANCE(pEntry) );
169 
170  return pEntry;
171 }
172 
173 static void
174 load_all_entries( GncSqlBackend* be )
175 {
176  GncSqlStatement* stmt;
177  GncSqlResult* result;
178 
179  g_return_if_fail( be != NULL );
180 
181  stmt = gnc_sql_create_select_statement( be, TABLE_NAME );
182  result = gnc_sql_execute_select_statement( be, stmt );
183  gnc_sql_statement_dispose( stmt );
184  if ( result != NULL )
185  {
186  GncSqlRow* row;
187  GList* list = NULL;
188 
189  row = gnc_sql_result_get_first_row( result );
190  while ( row != NULL )
191  {
192  GncEntry* pEntry = load_single_entry( be, row );
193  if ( pEntry != NULL )
194  {
195  list = g_list_append( list, pEntry );
196  }
197  row = gnc_sql_result_get_next_row( result );
198  }
199  gnc_sql_result_dispose( result );
200 
201  if ( list != NULL )
202  {
203  gnc_sql_slots_load_for_list( be, list );
204  g_list_free( list );
205  }
206  }
207 }
208 
209 /* ================================================================= */
210 static void
211 create_entry_tables( GncSqlBackend* be )
212 {
213  gint version;
214 
215  g_return_if_fail( be != NULL );
216 
217  version = gnc_sql_get_table_version( be, TABLE_NAME );
218  if ( version == 0 )
219  {
220  gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table );
221  }
222  else if ( version < TABLE_VERSION )
223  {
224  /* Upgrade:
225  1->2: 64 bit int handling
226  2->3: "entered" -> "date_entered", and it can be NULL
227  */
228  gnc_sql_upgrade_table( be, TABLE_NAME, col_table );
229  gnc_sql_set_table_version( be, TABLE_NAME, TABLE_VERSION );
230 
231  PINFO("Entries table upgraded from version %d to version %d\n", version, TABLE_VERSION);
232  }
233 }
234 
235 /* ================================================================= */
236 static gboolean
237 save_entry( GncSqlBackend* be, QofInstance* inst )
238 {
239  g_return_val_if_fail( inst != NULL, FALSE );
240  g_return_val_if_fail( GNC_IS_ENTRY(inst), FALSE );
241  g_return_val_if_fail( be != NULL, FALSE );
242 
243  return gnc_sql_commit_standard_item( be, inst, TABLE_NAME, GNC_ID_ENTRY, col_table );
244 }
245 
246 /* ================================================================= */
247 static void
248 write_single_entry( QofInstance *term_p, gpointer data_p )
249 {
250  write_objects_t* s = (write_objects_t*)data_p;
251  GncEntry* entry = GNC_ENTRY(term_p);
252 
253  g_return_if_fail( term_p != NULL );
254  g_return_if_fail( GNC_IS_ENTRY(term_p) );
255  g_return_if_fail( data_p != NULL );
256 
257  /* Only save if attached */
258  if ( s->is_ok && (gncEntryGetOrder( entry ) != NULL || gncEntryGetInvoice( entry ) != NULL ||
259  gncEntryGetBill( entry ) != NULL) )
260  {
261  s->is_ok = save_entry( s->be, term_p );
262  }
263 }
264 
265 static gboolean
266 write_entries( GncSqlBackend* be )
267 {
268  write_objects_t data;
269 
270  g_return_val_if_fail( be != NULL, FALSE );
271 
272  data.be = be;
273  data.is_ok = TRUE;
274  qof_object_foreach( GNC_ID_ENTRY, be->book, write_single_entry, &data );
275 
276  return data.is_ok;
277 }
278 
279 /* ================================================================= */
280 void
281 gnc_entry_sql_initialize( void )
282 {
283  static GncSqlObjectBackend be_data =
284  {
285  GNC_SQL_BACKEND_VERSION,
286  GNC_ID_ENTRY,
287  save_entry, /* commit */
288  load_all_entries, /* initial_load */
289  create_entry_tables, /* create_tables */
290  NULL, NULL, NULL,
291  write_entries /* write */
292  };
293 
294  qof_object_register_backend( GNC_ID_ENTRY, GNC_SQL_BACKEND, &be_data );
295 }
296 /* ========================== 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
load and save accounts data to SQL
GncSqlStatement * gnc_sql_create_select_statement(GncSqlBackend *be, const gchar *table_name)
#define COL_NNUL
gboolean gnc_sql_create_table(GncSqlBackend *be, const gchar *table_name, gint table_version, const GncSqlColumnTableEntry *col_table)
load and save data to SQL
gpointer(* QofAccessFunc)(gpointer object, const QofParam *param)
Definition: qofclass.h:177
load and save accounts 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 order data to SQL
gboolean gnc_sql_set_table_version(GncSqlBackend *be, const gchar *table_name, gint version)
load and save owner data to SQL
void gnc_sql_slots_load_for_list(GncSqlBackend *be, GList *list)
void gncBillAddEntry(GncInvoice *bill, GncEntry *entry)
Definition: gncInvoice.c:686
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)
load and save entry data to SQL
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
load and save invoice data to SQL
const gchar * QofLogModule
Definition: qofid.h:89
load and save tax table data to SQL