|
GnuCash 2.4.99
|
00001 /********************************************************************\ 00002 * gnc-employee-sql.c -- employee sql implementation * 00003 * * 00004 * This program is free software; you can redistribute it and/or * 00005 * modify it under the terms of the GNU General Public License as * 00006 * published by the Free Software Foundation; either version 2 of * 00007 * the License, or (at your option) any later version. * 00008 * * 00009 * This program is distributed in the hope that it will be useful, * 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00012 * GNU General Public License for more details. * 00013 * * 00014 * You should have received a copy of the GNU General Public License* 00015 * along with this program; if not, contact: * 00016 * * 00017 * Free Software Foundation Voice: +1-617-542-5942 * 00018 * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 * 00019 * Boston, MA 02110-1301, USA gnu@gnu.org * 00020 * * 00021 \********************************************************************/ 00022 00031 #include "config.h" 00032 00033 #include <glib.h> 00034 #include <stdlib.h> 00035 #include <string.h> 00036 00037 #include "gnc-commodity.h" 00038 00039 #include "gnc-backend-sql.h" 00040 #include "gnc-slots-sql.h" 00041 #include "gnc-commodity-sql.h" 00042 00043 #include "gncEmployeeP.h" 00044 #include "gnc-employee-sql.h" 00045 #include "gnc-address-sql.h" 00046 00047 #define _GNC_MOD_NAME GNC_ID_EMPLOYEE 00048 00049 static QofLogModule log_module = G_LOG_DOMAIN; 00050 00051 #define MAX_USERNAME_LEN 2048 00052 #define MAX_ID_LEN 2048 00053 #define MAX_LANGUAGE_LEN 2048 00054 #define MAX_ACL_LEN 2048 00055 00056 #define TABLE_NAME "employees" 00057 #define TABLE_VERSION 2 00058 00059 static GncSqlColumnTableEntry col_table[] = 00060 { 00061 { "guid", CT_GUID, 0, COL_NNUL | COL_PKEY, "guid" }, 00062 { "username", CT_STRING, MAX_USERNAME_LEN, COL_NNUL, "username" }, 00063 { "id", CT_STRING, MAX_ID_LEN, COL_NNUL, "id" }, 00064 { "language", CT_STRING, MAX_LANGUAGE_LEN, COL_NNUL, "language" }, 00065 { "acl", CT_STRING, MAX_ACL_LEN, COL_NNUL, "acl" }, 00066 { "active", CT_BOOLEAN, 0, COL_NNUL, "active" }, 00067 { "currency", CT_COMMODITYREF, 0, COL_NNUL, "currency" }, 00068 { "ccard_guid", CT_ACCOUNTREF, 0, 0, "credit-card-account" }, 00069 { "workday", CT_NUMERIC, 0, COL_NNUL, "workday" }, 00070 { "rate", CT_NUMERIC, 0, COL_NNUL, "rate" }, 00071 { "addr", CT_ADDRESS, 0, 0, "address" }, 00072 { NULL } 00073 }; 00074 00075 static GncEmployee* 00076 load_single_employee( GncSqlBackend* be, GncSqlRow* row ) 00077 { 00078 const GncGUID* guid; 00079 GncEmployee* pEmployee; 00080 00081 g_return_val_if_fail( be != NULL, NULL ); 00082 g_return_val_if_fail( row != NULL, NULL ); 00083 00084 guid = gnc_sql_load_guid( be, row ); 00085 pEmployee = gncEmployeeLookup( be->book, guid ); 00086 if ( pEmployee == NULL ) 00087 { 00088 pEmployee = gncEmployeeCreate( be->book ); 00089 } 00090 gnc_sql_load_object( be, row, GNC_ID_EMPLOYEE, pEmployee, col_table ); 00091 qof_instance_mark_clean( QOF_INSTANCE(pEmployee) ); 00092 00093 return pEmployee; 00094 } 00095 00096 static void 00097 load_all_employees( GncSqlBackend* be ) 00098 { 00099 GncSqlStatement* stmt; 00100 GncSqlResult* result; 00101 QofBook* pBook; 00102 gnc_commodity_table* pTable; 00103 00104 g_return_if_fail( be != NULL ); 00105 00106 pBook = be->book; 00107 pTable = gnc_commodity_table_get_table( pBook ); 00108 00109 stmt = gnc_sql_create_select_statement( be, TABLE_NAME ); 00110 result = gnc_sql_execute_select_statement( be, stmt ); 00111 gnc_sql_statement_dispose( stmt ); 00112 if ( result != NULL ) 00113 { 00114 GncSqlRow* row; 00115 GList* list = NULL; 00116 00117 row = gnc_sql_result_get_first_row( result ); 00118 while ( row != NULL ) 00119 { 00120 GncEmployee* pEmployee = load_single_employee( be, row ); 00121 if ( pEmployee != NULL ) 00122 { 00123 list = g_list_append( list, pEmployee ); 00124 } 00125 row = gnc_sql_result_get_next_row( result ); 00126 } 00127 gnc_sql_result_dispose( result ); 00128 00129 if ( list != NULL ) 00130 { 00131 gnc_sql_slots_load_for_list( be, list ); 00132 g_list_free( list ); 00133 } 00134 } 00135 } 00136 00137 /* ================================================================= */ 00138 static void 00139 create_employee_tables( GncSqlBackend* be ) 00140 { 00141 gint version; 00142 00143 g_return_if_fail( be != NULL ); 00144 00145 version = gnc_sql_get_table_version( be, TABLE_NAME ); 00146 if ( version == 0 ) 00147 { 00148 gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table ); 00149 } 00150 else if ( version == 1 ) 00151 { 00152 /* Upgrade 64 bit int handling */ 00153 gnc_sql_upgrade_table( be, TABLE_NAME, col_table ); 00154 gnc_sql_set_table_version( be, TABLE_NAME, TABLE_VERSION ); 00155 00156 PINFO("Employees table upgraded from version 1 to version %d\n", TABLE_VERSION); 00157 } 00158 } 00159 00160 /* ================================================================= */ 00161 static gboolean 00162 save_employee( GncSqlBackend* be, QofInstance* inst ) 00163 { 00164 GncEmployee* emp; 00165 const GncGUID* guid; 00166 gint op; 00167 gboolean is_infant; 00168 gboolean is_ok = TRUE; 00169 00170 g_return_val_if_fail( inst != NULL, FALSE ); 00171 g_return_val_if_fail( GNC_IS_EMPLOYEE(inst), FALSE ); 00172 g_return_val_if_fail( be != NULL, FALSE ); 00173 00174 emp = GNC_EMPLOYEE(inst); 00175 00176 is_infant = qof_instance_get_infant( inst ); 00177 if ( qof_instance_get_destroying( inst ) ) 00178 { 00179 op = OP_DB_DELETE; 00180 } 00181 else if ( be->is_pristine_db || is_infant ) 00182 { 00183 op = OP_DB_INSERT; 00184 } 00185 else 00186 { 00187 op = OP_DB_UPDATE; 00188 } 00189 if ( op != OP_DB_DELETE ) 00190 { 00191 // Ensure the commodity is in the db 00192 is_ok = gnc_sql_save_commodity( be, gncEmployeeGetCurrency( emp ) ); 00193 } 00194 00195 if ( is_ok ) 00196 { 00197 is_ok = gnc_sql_do_db_operation( be, op, TABLE_NAME, GNC_ID_EMPLOYEE, emp, col_table ); 00198 } 00199 00200 if ( is_ok ) 00201 { 00202 // Now, commit or delete any slots 00203 guid = qof_instance_get_guid( inst ); 00204 if ( !qof_instance_get_destroying(inst) ) 00205 { 00206 is_ok = gnc_sql_slots_save( be, guid, is_infant, qof_instance_get_slots( inst ) ); 00207 } 00208 else 00209 { 00210 is_ok = gnc_sql_slots_delete( be, guid ); 00211 } 00212 } 00213 00214 return is_ok; 00215 } 00216 00217 /* ================================================================= */ 00218 static gboolean 00219 employee_should_be_saved( GncEmployee *employee ) 00220 { 00221 const char *id; 00222 00223 g_return_val_if_fail( employee != NULL, FALSE ); 00224 00225 /* make sure this is a valid employee before we save it -- should have an ID */ 00226 id = gncEmployeeGetID( employee ); 00227 if ( id == NULL || *id == '\0' ) 00228 { 00229 return FALSE; 00230 } 00231 00232 return TRUE; 00233 } 00234 00235 static void 00236 write_single_employee( QofInstance *term_p, gpointer data_p ) 00237 { 00238 write_objects_t* s = (write_objects_t*)data_p; 00239 00240 g_return_if_fail( term_p != NULL ); 00241 g_return_if_fail( GNC_IS_EMPLOYEE(term_p) ); 00242 g_return_if_fail( data_p != NULL ); 00243 00244 if ( s->is_ok && employee_should_be_saved( GNC_EMPLOYEE(term_p) ) ) 00245 { 00246 s->is_ok = save_employee( s->be, term_p ); 00247 } 00248 } 00249 00250 static gboolean 00251 write_employees( GncSqlBackend* be ) 00252 { 00253 write_objects_t data; 00254 00255 g_return_val_if_fail( be != NULL, FALSE ); 00256 00257 data.be = be; 00258 data.is_ok = TRUE; 00259 qof_object_foreach( GNC_ID_EMPLOYEE, be->book, write_single_employee, &data ); 00260 00261 return data.is_ok; 00262 } 00263 00264 /* ================================================================= */ 00265 void 00266 gnc_employee_sql_initialize( void ) 00267 { 00268 static GncSqlObjectBackend be_data = 00269 { 00270 GNC_SQL_BACKEND_VERSION, 00271 GNC_ID_EMPLOYEE, 00272 save_employee, /* commit */ 00273 load_all_employees, /* initial_load */ 00274 create_employee_tables, /* create_tables */ 00275 NULL, NULL, NULL, 00276 write_employees /* write */ 00277 }; 00278 00279 qof_object_register_backend( GNC_ID_EMPLOYEE, GNC_SQL_BACKEND, &be_data ); 00280 } 00281 /* ========================== END OF FILE ===================== */
1.7.4