GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gnc-employee-sql.c
Go to the documentation of this file.
1 /********************************************************************\
2  * gnc-employee-sql.c -- employee sql implementation *
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-commodity.h"
38 
39 #include "gnc-backend-sql.h"
40 #include "gnc-slots-sql.h"
41 #include "gnc-commodity-sql.h"
42 
43 #include "gncEmployeeP.h"
44 #include "gnc-employee-sql.h"
45 #include "gnc-address-sql.h"
46 
47 #define _GNC_MOD_NAME GNC_ID_EMPLOYEE
48 
49 static QofLogModule log_module = G_LOG_DOMAIN;
50 
51 #define MAX_USERNAME_LEN 2048
52 #define MAX_ID_LEN 2048
53 #define MAX_LANGUAGE_LEN 2048
54 #define MAX_ACL_LEN 2048
55 
56 #define TABLE_NAME "employees"
57 #define TABLE_VERSION 2
58 
59 static GncSqlColumnTableEntry col_table[] =
60 {
61  { "guid", CT_GUID, 0, COL_NNUL | COL_PKEY, "guid" },
62  { "username", CT_STRING, MAX_USERNAME_LEN, COL_NNUL, "username" },
63  { "id", CT_STRING, MAX_ID_LEN, COL_NNUL, "id" },
64  { "language", CT_STRING, MAX_LANGUAGE_LEN, COL_NNUL, "language" },
65  { "acl", CT_STRING, MAX_ACL_LEN, COL_NNUL, "acl" },
66  { "active", CT_BOOLEAN, 0, COL_NNUL, "active" },
67  { "currency", CT_COMMODITYREF, 0, COL_NNUL, "currency" },
68  { "ccard_guid", CT_ACCOUNTREF, 0, 0, "credit-card-account" },
69  { "workday", CT_NUMERIC, 0, COL_NNUL, "workday" },
70  { "rate", CT_NUMERIC, 0, COL_NNUL, "rate" },
71  { "addr", CT_ADDRESS, 0, 0, "address" },
72  { NULL }
73 };
74 
75 static GncEmployee*
76 load_single_employee( GncSqlBackend* be, GncSqlRow* row )
77 {
78  const GncGUID* guid;
79  GncEmployee* pEmployee;
80 
81  g_return_val_if_fail( be != NULL, NULL );
82  g_return_val_if_fail( row != NULL, NULL );
83 
84  guid = gnc_sql_load_guid( be, row );
85  pEmployee = gncEmployeeLookup( be->book, guid );
86  if ( pEmployee == NULL )
87  {
88  pEmployee = gncEmployeeCreate( be->book );
89  }
90  gnc_sql_load_object( be, row, GNC_ID_EMPLOYEE, pEmployee, col_table );
91  qof_instance_mark_clean( QOF_INSTANCE(pEmployee) );
92 
93  return pEmployee;
94 }
95 
96 static void
97 load_all_employees( GncSqlBackend* be )
98 {
99  GncSqlStatement* stmt;
100  GncSqlResult* result;
101 
102  g_return_if_fail( be != NULL );
103 
104  stmt = gnc_sql_create_select_statement( be, TABLE_NAME );
105  result = gnc_sql_execute_select_statement( be, stmt );
106  gnc_sql_statement_dispose( stmt );
107  if ( result != NULL )
108  {
109  GncSqlRow* row;
110  GList* list = NULL;
111 
112  row = gnc_sql_result_get_first_row( result );
113  while ( row != NULL )
114  {
115  GncEmployee* pEmployee = load_single_employee( be, row );
116  if ( pEmployee != NULL )
117  {
118  list = g_list_append( list, pEmployee );
119  }
120  row = gnc_sql_result_get_next_row( result );
121  }
122  gnc_sql_result_dispose( result );
123 
124  if ( list != NULL )
125  {
126  gnc_sql_slots_load_for_list( be, list );
127  g_list_free( list );
128  }
129  }
130 }
131 
132 /* ================================================================= */
133 static void
134 create_employee_tables( GncSqlBackend* be )
135 {
136  gint version;
137 
138  g_return_if_fail( be != NULL );
139 
140  version = gnc_sql_get_table_version( be, TABLE_NAME );
141  if ( version == 0 )
142  {
143  gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table );
144  }
145  else if ( version == 1 )
146  {
147  /* Upgrade 64 bit int handling */
148  gnc_sql_upgrade_table( be, TABLE_NAME, col_table );
149  gnc_sql_set_table_version( be, TABLE_NAME, TABLE_VERSION );
150 
151  PINFO("Employees table upgraded from version 1 to version %d\n", TABLE_VERSION);
152  }
153 }
154 
155 /* ================================================================= */
156 static gboolean
157 save_employee( GncSqlBackend* be, QofInstance* inst )
158 {
159  GncEmployee* emp;
160  const GncGUID* guid;
161  gint op;
162  gboolean is_infant;
163  gboolean is_ok = TRUE;
164 
165  g_return_val_if_fail( inst != NULL, FALSE );
166  g_return_val_if_fail( GNC_IS_EMPLOYEE(inst), FALSE );
167  g_return_val_if_fail( be != NULL, FALSE );
168 
169  emp = GNC_EMPLOYEE(inst);
170 
171  is_infant = qof_instance_get_infant( inst );
172  if ( qof_instance_get_destroying( inst ) )
173  {
174  op = OP_DB_DELETE;
175  }
176  else if ( be->is_pristine_db || is_infant )
177  {
178  op = OP_DB_INSERT;
179  }
180  else
181  {
182  op = OP_DB_UPDATE;
183  }
184  if ( op != OP_DB_DELETE )
185  {
186  // Ensure the commodity is in the db
187  is_ok = gnc_sql_save_commodity( be, gncEmployeeGetCurrency( emp ) );
188  }
189 
190  if ( is_ok )
191  {
192  is_ok = gnc_sql_do_db_operation( be, op, TABLE_NAME, GNC_ID_EMPLOYEE, emp, col_table );
193  }
194 
195  if ( is_ok )
196  {
197  // Now, commit or delete any slots
198  guid = qof_instance_get_guid( inst );
199  if ( !qof_instance_get_destroying(inst) )
200  {
201  is_ok = gnc_sql_slots_save( be, guid, is_infant, qof_instance_get_slots( inst ) );
202  }
203  else
204  {
205  is_ok = gnc_sql_slots_delete( be, guid );
206  }
207  }
208 
209  return is_ok;
210 }
211 
212 /* ================================================================= */
213 static gboolean
214 employee_should_be_saved( GncEmployee *employee )
215 {
216  const char *id;
217 
218  g_return_val_if_fail( employee != NULL, FALSE );
219 
220  /* make sure this is a valid employee before we save it -- should have an ID */
221  id = gncEmployeeGetID( employee );
222  if ( id == NULL || *id == '\0' )
223  {
224  return FALSE;
225  }
226 
227  return TRUE;
228 }
229 
230 static void
231 write_single_employee( QofInstance *term_p, gpointer data_p )
232 {
233  write_objects_t* s = (write_objects_t*)data_p;
234 
235  g_return_if_fail( term_p != NULL );
236  g_return_if_fail( GNC_IS_EMPLOYEE(term_p) );
237  g_return_if_fail( data_p != NULL );
238 
239  if ( s->is_ok && employee_should_be_saved( GNC_EMPLOYEE(term_p) ) )
240  {
241  s->is_ok = save_employee( s->be, term_p );
242  }
243 }
244 
245 static gboolean
246 write_employees( GncSqlBackend* be )
247 {
248  write_objects_t data;
249 
250  g_return_val_if_fail( be != NULL, FALSE );
251 
252  data.be = be;
253  data.is_ok = TRUE;
254  qof_object_foreach( GNC_ID_EMPLOYEE, be->book, write_single_employee, &data );
255 
256  return data.is_ok;
257 }
258 
259 /* ================================================================= */
260 void
261 gnc_employee_sql_initialize( void )
262 {
263  static GncSqlObjectBackend be_data =
264  {
265  GNC_SQL_BACKEND_VERSION,
266  GNC_ID_EMPLOYEE,
267  save_employee, /* commit */
268  load_all_employees, /* initial_load */
269  create_employee_tables, /* create_tables */
270  NULL, NULL, NULL,
271  write_employees /* write */
272  };
273 
274  qof_object_register_backend( GNC_ID_EMPLOYEE, GNC_SQL_BACKEND, &be_data );
275 }
276 /* ========================== END OF FILE ===================== */
gboolean qof_object_register_backend(QofIdTypeConst type_name, const char *backend_name, gpointer be_data)
const GncGUID * qof_instance_get_guid(gconstpointer)
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 data to SQL
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)
gboolean gnc_sql_set_table_version(GncSqlBackend *be, const gchar *table_name, gint version)
void gnc_sql_slots_load_for_list(GncSqlBackend *be, GList *list)
load and save employee data to SQL
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)
gboolean gnc_sql_slots_delete(GncSqlBackend *be, const GncGUID *guid)
load and save address data to SQL
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)
Commodity handling public routines.
gboolean gnc_sql_slots_save(GncSqlBackend *be, const GncGUID *guid, gboolean is_infant, KvpFrame *pFrame)
const gchar * QofLogModule
Definition: qofid.h:89