free prepared statements
[platform/core/account/account-manager.git] / server / src / account-server-db.c
1 /*
2  *
3  * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <sys/stat.h>
25 #include <pthread.h>
26 #include <glib.h>
27 #include <db-util.h>
28 #include <pthread.h>
29 #include <vconf.h>
30
31 #include <pkgmgr-info.h>
32 //#include <tzplatform_config.h>
33
34 #include <dbg.h>
35 #include <account_ipc_marshal.h>
36 #include <account_free.h>
37 #include <account-private.h>
38 #include <account_db_helper.h>
39 #include <account_crypto_service.h>
40 #include <account_err.h>
41 #include "account_type.h"
42 #include "account-server-db.h"
43
44 #define EMAIL_SERVICE_CMDLINE "/usr/bin/email-service"
45
46 #define EMAIL_APPID "email-setting-efl"
47
48 #define ACCOUNT_DB_OPEN_READONLY 0
49 #define ACCOUNT_DB_OPEN_READWRITE 1
50
51 #define MAX_TEXT 4096
52
53 #define _TIZEN_PUBLIC_
54 #ifndef _TIZEN_PUBLIC_
55
56 #endif
57
58 static sqlite3* g_hAccountDB = NULL;
59 static sqlite3* g_hAccountDB2 = NULL;
60 static sqlite3* g_hAccountGlobalDB = NULL;
61 static sqlite3* g_hAccountGlobalDB2 = NULL;
62 static pthread_mutex_t account_mutex = PTHREAD_MUTEX_INITIALIZER;
63
64 //static char *_account_dup_text(const char *text_data);
65 static int _account_insert_custom(account_s *account, int account_id);
66 static int _account_update_custom(account_s *account, int account_id);
67 static int _account_type_update_provider_feature(sqlite3 * account_db_handle, account_type_s *account_type, const char* app_id);
68
69 static void _account_insert_delete_update_notification_send(char *noti_name)
70 {
71         if (!noti_name) {
72                 _ERR("Noti Name is NULL!!!!!!\n");
73                 return;
74         }
75
76         _INFO("noti_type = %s", noti_name);
77
78         if (vconf_set_str(VCONFKEY_ACCOUNT_MSG_STR, noti_name) != 0) {
79                 _ERR("Vconf MSG Str set FAILED !!!!!!\n");;
80         }
81 }
82
83 int _account_get_current_appid_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
84 {
85         char* appid = NULL;
86         char* item = NULL;
87         GSList** appid_list = (GSList**)user_data;
88         int pkgmgr_ret = -1;
89
90         pkgmgr_ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
91
92         if (pkgmgr_ret != PMINFO_R_OK)
93                 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_appid(%d)", pkgmgr_ret);
94
95         item = _account_dup_text(appid);
96         *appid_list = g_slist_append(*appid_list, item);
97
98         return 0;
99 }
100
101 static inline int __read_proc(const char *path, char *buf, int size)
102 {
103         int fd = 0, ret = 0;
104
105         if (buf == NULL || path == NULL) {
106                 ACCOUNT_ERROR("path and buffer is mandatory\n");
107                 return -1;
108         }
109
110         fd = open(path, O_RDONLY);
111         if (fd < 0) {
112                 ACCOUNT_ERROR("fd open error(%d)\n", fd);
113                 return -1;
114         }
115
116         ret = read(fd, buf, size - 1);
117         if (ret <= 0) {
118                 ACCOUNT_ERROR("fd read error(%d)\n", fd);
119                 close(fd);
120                 return -1;
121         } else {
122                 buf[ret] = 0;
123         }
124
125         close(fd);
126
127         return ret;
128 }
129
130 char *_account_get_proc_cmdline_bypid(int pid)
131 {
132         char buf[128];
133         int ret = 0;
134
135         ACCOUNT_SNPRINTF(buf, sizeof(buf), "/proc/%d/cmdline", pid);
136         ret = __read_proc(buf, buf, sizeof(buf));
137         if (ret <= 0) {
138                 ACCOUNT_DEBUG("No proc directory (%d)\n", pid);
139                 return NULL;
140         }
141
142         return strdup(buf);
143 }
144
145 /*
146 static int _account_execute_query_from_global_db(const char *query)
147 {
148         int rc = -1;
149         char* pszErrorMsg = NULL;
150
151         if (!query) {
152                 ACCOUNT_ERROR("NULL query\n");
153                 return _ACCOUNT_ERROR_QUERY_SYNTAX_ERROR;
154         }
155
156         if (!g_hAccountGlobalDB) {
157                 ACCOUNT_ERROR("Global DB is not opened\n");
158                 return _ACCOUNT_ERROR_DB_NOT_OPENED;
159         }
160
161         rc = sqlite3_exec(g_hAccountGlobalDB, query, NULL, NULL, &pszErrorMsg);
162         if (SQLITE_OK != rc) {
163                 ACCOUNT_ERROR("sqlite3_exec rc(%d) query(%s) failed(%s).", rc, query, pszErrorMsg);
164                 sqlite3_free(pszErrorMsg);
165         }
166
167         return rc;
168 }
169 */
170
171 /*
172 static int _account_begin_transaction_from_global_db(void)
173 {
174         ACCOUNT_DEBUG("_account_begin_transaction start");
175         int ret = -1;
176
177         ret = _account_execute_query_from_global_db("BEGIN IMMEDIATE TRANSACTION");
178
179         if (ret == SQLITE_BUSY) {
180                 ACCOUNT_ERROR(" sqlite3 busy = %d", ret);
181                 return _ACCOUNT_ERROR_DATABASE_BUSY;
182         } else if (ret != SQLITE_OK) {
183                 ACCOUNT_ERROR("_account_svc_begin_transaction_in_global_db fail :: %d", ret);
184                 return _ACCOUNT_ERROR_DB_FAILED;
185         }
186
187         ACCOUNT_DEBUG("_account_begin_transaction_in_global_db end");
188         return _ACCOUNT_ERROR_NONE;
189 }
190
191 static int _account_end_transaction_from_global_db(bool is_success)
192 {
193         ACCOUNT_DEBUG("_account_end_transaction_in_global_db start");
194
195         int ret = -1;
196
197         if (is_success == true) {
198                 ret = _account_execute_query_from_global_db("COMMIT TRANSACTION");
199                 ACCOUNT_DEBUG("_account_end_transaction_in_global_db COMMIT");
200         } else {
201                 ret = _account_execute_query_from_global_db("ROLLBACK TRANSACTION");
202                 ACCOUNT_DEBUG("_account_end_transaction ROLLBACK");
203         }
204
205         if (ret == SQLITE_PERM) {
206                 ACCOUNT_ERROR("Account permission denied :: %d", ret);
207                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
208         }
209
210         if (ret == SQLITE_BUSY) {
211                 ACCOUNT_DEBUG(" sqlite3 busy = %d", ret);
212                 return _ACCOUNT_ERROR_DATABASE_BUSY;
213         }
214
215         if (ret != SQLITE_OK) {
216                 ACCOUNT_ERROR("_account_svc_end_transaction_in_global_db fail :: %d", ret);
217                 return _ACCOUNT_ERROR_DB_FAILED;
218         }
219
220         ACCOUNT_DEBUG("_account_end_transaction_in_global_db end");
221         return _ACCOUNT_ERROR_NONE;
222 }
223 */
224
225 int _account_global_db_open(void)
226 {
227         int rc = 0;
228         int ret = -1;
229         char account_db_path[256] = {0, };
230
231         _INFO("start _account_global_db_open()");
232
233         ACCOUNT_MEMSET(account_db_path, 0x00, sizeof(account_db_path));
234         ACCOUNT_GET_GLOBAL_DB_PATH(account_db_path, sizeof(account_db_path));
235
236         if (g_hAccountGlobalDB) {
237                 _ERR("Account database is using in another app. %x", g_hAccountDB);
238                 return _ACCOUNT_ERROR_DATABASE_BUSY;
239         }
240
241         ret = _account_db_handle_close(g_hAccountGlobalDB2);
242         if (ret != _ACCOUNT_ERROR_NONE)
243                 ACCOUNT_DEBUG("db_util_close(g_hAccountGlobalDB2) fail ret = %d", ret);
244
245         ACCOUNT_DEBUG("before _account_global_db_open()");
246 //      if (mode == ACCOUNT_DB_OPEN_READWRITE)
247 //              rc = db_util_open(account_db_path, &g_hAccountDB, DB_UTIL_REGISTER_HOOK_METHOD);
248 //      else if (mode == ACCOUNT_DB_OPEN_READONLY)
249         rc = db_util_open_with_options(account_db_path, &g_hAccountGlobalDB, SQLITE_OPEN_READONLY, NULL);
250 //      else
251 //              return _ACCOUNT_ERROR_DB_NOT_OPENED;
252         ACCOUNT_DEBUG("after _account_global_db_open() sqlite_rc = %d", rc);
253
254         if (rc == SQLITE_PERM || _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
255                 ACCOUNT_ERROR("Account permission denied");
256                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
257         }
258
259         if (rc == SQLITE_BUSY) {
260                 ACCOUNT_ERROR("busy handler fail.");
261                 return _ACCOUNT_ERROR_DATABASE_BUSY;
262         }
263
264         if (rc != SQLITE_OK) {
265                 ACCOUNT_ERROR("The database isn't connected.");
266                 return _ACCOUNT_ERROR_DB_NOT_OPENED;
267         }
268
269         _INFO("end _account_global_db_open()");
270         return _ACCOUNT_ERROR_NONE;
271 }
272
273 int _account_global_db_close(void)
274 {
275         ACCOUNT_DEBUG("start account_global_db_close()");
276         int ret = -1;
277 /*
278         ret = _account_db_handle_close(g_hAccountGlobalDB2);
279         if (ret != _ACCOUNT_ERROR_NONE)
280                 ACCOUNT_DEBUG("db_util_close(g_hAccountGlobalDB2) fail ret = %d", ret);
281 */
282         ret = _account_db_handle_close(g_hAccountGlobalDB);
283         if (ret != _ACCOUNT_ERROR_NONE) {
284                 ACCOUNT_ERROR("db_util_close(g_hAccountGlobalDB) fail ret = %d", ret);
285                 g_hAccountGlobalDB2 = g_hAccountGlobalDB;
286         }
287         g_hAccountGlobalDB = NULL;
288
289         return ret;
290 }
291
292 static bool _account_check_add_more_account(const char* app_id)
293 {
294         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
295         int                             rc = 0;
296
297         ACCOUNT_RETURN_VAL((app_id != 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
298         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
299
300         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
301
302         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s' and MultipleAccountSupport = 1", ACCOUNT_TYPE_TABLE, app_id);
303         rc = _account_get_record_count(g_hAccountDB, query);
304
305         /* multiple account support case (User DB & global DB) */
306         if (rc > 0 || _account_get_record_count(g_hAccountGlobalDB, query) > 0) {
307                 ACCOUNT_SLOGD("app id (%s) supports multiple account. rc(%d)\n", app_id, rc);
308                 return TRUE;
309         }
310
311         /* multiple account not support case */
312         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
313         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE package_name = '%s'", ACCOUNT_TABLE, app_id);
314         rc = _account_get_record_count(g_hAccountDB, query);
315
316         if (rc <= 0) {
317                 ACCOUNT_SLOGD("app id (%s) supports single account. and there is no account of the app id\n", app_id);
318                 return TRUE;
319         }
320
321         return FALSE;
322 }
323
324
325 int _account_db_open(int mode, int pid, uid_t uid)
326 {
327         int rc = 0;
328         int ret = -1;
329         char account_db_dir[256] = {0, };
330         char account_db_path[256] = {0, };
331
332         _INFO("start _account_db_open() pid [%d]. uid [%d]", pid, uid);
333
334         ACCOUNT_MEMSET(account_db_dir, 0x00, sizeof(account_db_dir));
335         ACCOUNT_MEMSET(account_db_path, 0x00, sizeof(account_db_path));
336
337         ACCOUNT_GET_USER_DB_PATH(account_db_path, sizeof(account_db_path), uid);
338
339         if (g_hAccountDB) {
340                 _ERR("Account database is using in another app. %x", g_hAccountDB);
341                 return _ACCOUNT_ERROR_DATABASE_BUSY;
342         }
343
344         ret = _account_db_handle_close(g_hAccountDB2);
345         if (ret != _ACCOUNT_ERROR_NONE)
346                 ACCOUNT_DEBUG("db_util_close(g_hAccountDB2) fail ret = %d", ret);
347
348         ACCOUNT_GET_USER_DB_DIR(account_db_dir, sizeof(account_db_dir), uid);
349
350         if (mkdir(account_db_dir, 0777) != 0)
351                 ACCOUNT_DEBUG("\"%s\" is already exist directory", account_db_dir);
352
353         ACCOUNT_DEBUG("before db_util_open()");
354 //      if (mode == ACCOUNT_DB_OPEN_READWRITE)
355                 rc = db_util_open(account_db_path, &g_hAccountDB, DB_UTIL_REGISTER_HOOK_METHOD);
356 //      else if (mode == ACCOUNT_DB_OPEN_READONLY)
357 //              rc = db_util_open_with_options(account_db_path, &g_hAccountDB, SQLITE_OPEN_READONLY, NULL);
358 //      else
359 //              return _ACCOUNT_ERROR_DB_NOT_OPENED;
360         ACCOUNT_DEBUG("after db_util_open() sqlite_rc = %d", rc);
361
362         if (rc == SQLITE_PERM || _account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
363                 ACCOUNT_ERROR("Account permission denied");
364                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
365         }
366
367         if (rc == SQLITE_BUSY) {
368                 ACCOUNT_ERROR("busy handler fail.");
369                 return _ACCOUNT_ERROR_DATABASE_BUSY;
370         }
371
372         if (rc != SQLITE_OK) {
373                 ACCOUNT_ERROR("The database isn't connected.");
374                 return _ACCOUNT_ERROR_DB_NOT_OPENED;
375         }
376
377         rc = _account_check_is_all_table_exists(g_hAccountDB);
378
379         if (rc < 0) {
380                 _ERR("_account_check_is_all_table_exists rc=[%d]", rc);
381                 return rc;
382         } else if (rc == ACCOUNT_TABLE_TOTAL_COUNT) {
383                 _INFO("Tables OK");
384         } else {
385                 int ret = _account_create_all_tables(g_hAccountDB);
386                 if (ret != _ACCOUNT_ERROR_NONE) {
387                         _ERR("_account_create_all_tables fail ret=[%d]", ret);
388                         return ret;
389                 }
390         }
391
392         _INFO("end _account_db_open()");
393         return _ACCOUNT_ERROR_NONE;
394 }
395
396 int _account_db_close(void)
397 {
398         ACCOUNT_DEBUG("start db_util_close()");
399         int ret = -1;
400 /*
401         ret = _account_db_handle_close(g_hAccountDB2);
402         if (ret != _ACCOUNT_ERROR_NONE)
403                 ACCOUNT_DEBUG("db_util_close(g_hAccountDB2) fail ret = %d", ret);
404 */
405         ret = _account_db_handle_close(g_hAccountDB);
406         if (ret != _ACCOUNT_ERROR_NONE) {
407                 ACCOUNT_ERROR("db_util_close(g_hAccountDB) fail ret = %d", ret);
408                 g_hAccountDB2 = g_hAccountDB;
409         }
410         g_hAccountDB = NULL;
411
412         return ret;
413 }
414
415 static int _account_execute_insert_query(account_s *account)
416 {
417         _INFO("_account_execute_insert_query start");
418
419         int                             rc = 0;
420         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
421         int                             error_code = _ACCOUNT_ERROR_NONE;
422         account_stmt    hstmt = NULL;
423
424         /* check whether app id exist in account type db */
425
426         if (!account->user_name && !account->display_name && !account->email_address) {
427                 _INFO("");
428                 ACCOUNT_ERROR("Mandetory fields is NULL. At least one field is required among username, display name, email address\n");
429                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
430         }
431
432         _INFO("");
433         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
434         ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (user_name, email_address , display_name , icon_path , source , package_name , "
435                         "access_token , domain_name , auth_type , secret , sync_support , txt_custom0, txt_custom1, txt_custom2, txt_custom3, txt_custom4, "
436                         "int_custom0, int_custom1, int_custom2, int_custom3, int_custom4, txt_custom0 ) values " // to do urusa
437                         "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",   ACCOUNT_TABLE);
438
439         hstmt = _account_prepare_query(g_hAccountDB, query);
440         ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
441
442         _INFO("");
443         _account_convert_account_to_sql(account, hstmt, query);
444
445         _INFO("");
446         rc = _account_query_step(hstmt);
447         if (rc != SQLITE_DONE) {
448                 _INFO("");
449                 ACCOUNT_ERROR("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
450
451                 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM)
452                         error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
453                 else
454                         error_code = _ACCOUNT_ERROR_DB_FAILED;
455         }
456
457         _INFO("");
458         rc = _account_query_finalize(hstmt);
459         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
460         hstmt = NULL;
461
462         _INFO("_account_execute_insert_query end");
463         return error_code;
464 }
465
466 static int _account_insert_capability(account_s *account, int account_id)
467 {
468         _INFO("_account_insert_capability start");
469         int                             rc, count = 1;
470         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
471         account_stmt    hstmt = NULL;
472
473         ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
474
475         if (g_slist_length(account->capablity_list) == 0) {
476                 ACCOUNT_DEBUG("_account_insert_capability, no capability\n");
477                 return _ACCOUNT_ERROR_NONE;
478         }
479
480         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
481
482         _INFO("_account_insert_capability _account_get_record_count [%s]", query);
483         rc = _account_get_record_count(g_hAccountDB, query);
484
485         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
486                 _ERR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
487                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
488         }
489
490         if (rc <= 0)
491                 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
492
493         /* insert query*/
494
495         GSList *iter;
496
497         for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
498                 int ret;
499                 count = 1;
500
501                 account_capability_s* cap_data = NULL;
502                 cap_data = (account_capability_s*)iter->data;
503
504                 _INFO("cap_data->type = %s, cap_data->value = %d \n", cap_data->type, cap_data->value);
505
506                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
507                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
508                                 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
509                 hstmt = _account_prepare_query(g_hAccountDB, query);
510
511                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
512
513                 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
514                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
515                 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
516                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
517                 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
518                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
519                 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
520                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
521                 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
522                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
523
524                 rc = _account_query_step(hstmt);
525                 _INFO("_account_insert_capability _account_query_step[%d]", rc);
526
527                 if (rc != SQLITE_DONE) {
528                         _ERR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
529                         break;
530                 }
531
532                 rc = _account_query_finalize(hstmt);
533                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
534                 hstmt = NULL;
535
536         }
537
538         _INFO("_account_insert_capability end");
539         return _ACCOUNT_ERROR_NONE;
540 }
541
542 static int _account_update_capability(account_s *account, int account_id)
543 {
544         int                             rc, count = 1;
545         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
546         account_stmt    hstmt = NULL;
547
548         ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
549
550         if (g_slist_length(account->capablity_list) == 0) {
551                 ACCOUNT_ERROR("_account_update_capability, no capability\n");
552                 return _ACCOUNT_ERROR_NONE;
553         }
554
555         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
556
557         rc = _account_get_record_count(g_hAccountDB, query);
558
559         if (rc <= 0) {
560                 ACCOUNT_SLOGI("_account_update_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
561                 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
562         }
563
564         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
565
566         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id=? ", CAPABILITY_TABLE);
567         hstmt = _account_prepare_query(g_hAccountDB, query);
568         count = 1;
569         _account_query_bind_int(hstmt, count++, (int)account_id);
570         rc = _account_query_step(hstmt);
571
572         if (rc != SQLITE_DONE) {
573                 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
574                 if (_account_query_finalize(hstmt) != _ACCOUNT_ERROR_NONE)
575                         _ERR("finalize error - account_query_step() failed");
576                 return _ACCOUNT_ERROR_DB_FAILED;
577         }
578         rc = _account_query_finalize(hstmt);
579         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
580         hstmt = NULL;
581
582         GSList *iter;
583
584         for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
585                 int ret;
586                 count = 1;
587                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
588                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
589                                 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
590
591                 hstmt = _account_prepare_query(g_hAccountDB, query);
592
593                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
594
595                 account_capability_s* cap_data = NULL;
596                 cap_data = (account_capability_s*)iter->data;
597
598                 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
599                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
600                 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
601                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
602                 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
603                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
604                 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
605                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
606                 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
607                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
608
609                 rc = _account_query_step(hstmt);
610
611                 if (rc != SQLITE_DONE) {
612                         ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
613                         break;
614                 }
615
616                 rc = _account_query_finalize(hstmt);
617                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
618                 hstmt = NULL;
619
620         }
621
622         return _ACCOUNT_ERROR_NONE;
623 }
624
625 static int _account_update_capability_by_user_name(account_s *account, const char *user_name, const char *package_name)
626 {
627         int                             rc, count = 1;
628         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
629         account_stmt    hstmt = NULL;
630
631         ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
632
633         if (g_slist_length(account->capablity_list) == 0) {
634                 ACCOUNT_ERROR("_account_update_capability_by_user_name, no capability\n");
635                 return _ACCOUNT_ERROR_NONE;
636         }
637
638         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where package_name= '%s' and user_name='%s'", ACCOUNT_TABLE, package_name, user_name);
639
640         rc = _account_get_record_count(g_hAccountDB, query);
641
642         if (rc <= 0) {
643                 ACCOUNT_SLOGI("_account_update_capability_by_user_name : related account item is not existed rc=%d , %s ", rc, _account_db_err_msg(g_hAccountDB));
644                 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
645         }
646
647         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
648
649         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name=? and user_name=? ", CAPABILITY_TABLE);
650         hstmt = _account_prepare_query(g_hAccountDB, query);
651         count = 1;
652         _account_query_bind_text(hstmt, count++, (char*)account->package_name);
653         _account_query_bind_text(hstmt, count++, (char*)account->user_name);
654         rc = _account_query_step(hstmt);
655         if (rc != SQLITE_DONE) {
656                 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
657                 if (_account_query_finalize(hstmt) != _ACCOUNT_ERROR_NONE)
658                         _ERR("finalize error - account_query_step() failed");
659                 return _ACCOUNT_ERROR_DB_FAILED;
660         }
661
662         rc = _account_query_finalize(hstmt);
663         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
664         hstmt = NULL;
665
666         GSList* iter;
667
668         for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
669                 int ret;
670                 count = 1;
671                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
672                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
673                                 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
674
675                 hstmt = _account_prepare_query(g_hAccountDB, query);
676
677                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
678
679                 account_capability_s* cap_data = NULL;
680                 cap_data = (account_capability_s*)iter->data;
681
682                 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
683                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
684                 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
685                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
686                 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
687                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
688                 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
689                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
690                 ret = _account_query_bind_int(hstmt, count++, (int)account->id);
691                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
692
693                 rc = _account_query_step(hstmt);
694
695                 if (rc != SQLITE_DONE) {
696                         ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
697                         break;
698                 }
699
700                 rc = _account_query_finalize(hstmt);
701                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
702                 hstmt = NULL;
703
704         }
705
706         return _ACCOUNT_ERROR_NONE;
707 }
708
709
710 bool _account_add_capability_to_account_cb(const char* capability_type, int capability_value, account_s *account)
711 {
712         account_capability_s *cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
713
714         if (cap_data == NULL)
715                 return FALSE;
716         ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
717
718         cap_data->type = _account_dup_text(capability_type);
719         cap_data->value = capability_value;
720         _INFO("cap_data->type = %s, cap_data->value = %d", cap_data->type, cap_data->value);
721
722         account->capablity_list = g_slist_append(account->capablity_list, (gpointer)cap_data);
723
724         return TRUE;
725 }
726
727
728 bool _account_add_custom_to_account_cb(const char* key, const char* value, account_s *account)
729 {
730         account_custom_s *custom_data = (account_custom_s*)malloc(sizeof(account_custom_s));
731
732         if (custom_data == NULL) {
733                 ACCOUNT_DEBUG("_account_add_custom_to_account_cb :: malloc fail\n");
734                 return FALSE;
735         }
736         ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s));
737
738         custom_data->account_id = account->id;
739         custom_data->app_id = _account_dup_text(account->package_name);
740         custom_data->key = _account_dup_text(key);
741         custom_data->value = _account_dup_text(value);
742         _INFO("custom_data->key = %s, custom_data->value = %s", custom_data->key, custom_data->value);
743
744         account->custom_list = g_slist_append(account->custom_list, (gpointer)custom_data);
745
746         return TRUE;
747 }
748
749 static void _account_compare_new_and_old_account_properties(account_s** new_account, account_s* old_account)
750 {
751         if ((*new_account) == NULL || old_account == NULL) {
752                 _ERR("invalid input params");
753                 return;
754         }
755
756         // compare
757         (*new_account)->id = old_account->id;
758
759         //user name
760         if (!(*new_account)->user_name) {
761                 if (old_account->user_name)
762                         (*new_account)->user_name = _account_dup_text(old_account->user_name);
763         }
764
765         // display name
766         if (!(*new_account)->display_name) {
767                 if (old_account->display_name)
768                         (*new_account)->display_name = _account_dup_text(old_account->display_name);
769         }
770
771         // email address
772         if (!(*new_account)->email_address) {
773                 if (old_account->email_address)
774                         (*new_account)->email_address = _account_dup_text(old_account->email_address);
775         }
776
777         // domain name
778         if (!(*new_account)->domain_name) {
779                 if (old_account->domain_name)
780                         (*new_account)->domain_name = _account_dup_text(old_account->domain_name);
781         }
782
783         // icon path
784         if (!(*new_account)->icon_path) {
785                 if (old_account->icon_path)
786                         (*new_account)->icon_path = _account_dup_text(old_account->icon_path);
787         }
788
789         // source
790         if (!(*new_account)->source) {
791                 if (old_account->source)
792                         (*new_account)->source = _account_dup_text(old_account->source);
793         }
794
795         _ACCOUNT_FREE((*new_account)->package_name);
796         (*new_account)->package_name = _account_dup_text(old_account->package_name);
797
798         // access token
799         if (!(*new_account)->access_token) {
800                 if (old_account->access_token)
801                         (*new_account)->access_token = _account_dup_text(old_account->access_token);
802         }
803
804         // auth type
805         if ((*new_account)->auth_type == _ACCOUNT_AUTH_TYPE_INVALID)
806                 (*new_account)->auth_type = old_account->auth_type;
807
808         //secret
809         if ((*new_account)->secret == _ACCOUNT_SECRECY_INVALID)
810                 (*new_account)->secret = old_account->secret;
811
812         // sync support
813         if ((*new_account)->sync_support == _ACCOUNT_SYNC_INVALID)
814                 (*new_account)->sync_support = old_account->sync_support;
815
816         // TODO user text
817         int i;
818         for (i = 0; i < USER_TXT_CNT; i++) {
819                 if (!(*new_account)->user_data_txt[i]) {
820                         if (old_account->user_data_txt[i])
821                                 (*new_account)->user_data_txt[i] = _account_dup_text(old_account->user_data_txt[i]);
822                 }
823         }
824
825         // TODO user int
826         for (i = 0; i < USER_INT_CNT; i++) {
827                 if ((*new_account)->user_data_int[i] == 0)
828                                 (*new_account)->user_data_int[i] = old_account->user_data_int[i];
829         }
830
831         return;
832 }
833
834
835 static int _account_compare_old_record_by_user_name(account_s *new_account, const char* user_name, const char* package_name)
836 {
837         int                             error_code = _ACCOUNT_ERROR_NONE;
838         account_stmt    hstmt = NULL;
839         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
840         int                             rc = 0;
841         account_s *old_account = NULL;
842
843         ACCOUNT_RETURN_VAL((new_account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
844         ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
845         ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
846         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
847
848         old_account = (account_s*)calloc(1, sizeof(account_s));
849         if (!old_account) {
850                 ACCOUNT_FATAL("Memory alloc fail\n");
851                 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
852         }
853
854         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
855
856         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = '%s' and package_name='%s'", ACCOUNT_TABLE, user_name, package_name);
857         hstmt = _account_prepare_query(g_hAccountDB, query);
858
859         rc = _account_query_step(hstmt);
860         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
861
862         while (rc == SQLITE_ROW) {
863                 _account_convert_column_to_account(hstmt, old_account);
864                 rc = _account_query_step(hstmt);
865         }
866
867         rc = _account_query_finalize(hstmt);
868         ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
869         hstmt = NULL;
870
871         // get capability
872         error_code = _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, old_account->id, (void*)old_account, false);
873         ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
874
875         // get custom text
876         error_code = _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, old_account->id, (void*)old_account, false);
877         ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
878
879         _account_compare_new_and_old_account_properties(&new_account, old_account);
880
881 CATCH:
882         if (old_account)
883                 _account_free_account_with_items(old_account);
884
885         if (hstmt != NULL) {
886                 rc = _account_query_finalize(hstmt);
887                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
888                 hstmt = NULL;
889         }
890
891         return _ACCOUNT_ERROR_NONE;
892 }
893
894 static int _account_update_account_by_user_name(int pid, uid_t uid, account_s *account, const char *user_name, const char *package_name)
895 {
896         int                             rc = 0, binding_count = 0, count = 0;
897         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
898         int                             error_code = _ACCOUNT_ERROR_NONE;
899         account_stmt    hstmt = NULL;
900
901         ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is NULL.\n"));
902         ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is NULL.\n"));
903
904         char* current_appid = NULL;
905         char* verified_appid = NULL;
906
907         current_appid = _account_get_current_appid(pid, uid);
908         error_code = _account_get_represented_appid_from_db(g_hAccountDB, g_hAccountGlobalDB, current_appid, uid, &verified_appid);
909
910         _ACCOUNT_FREE(current_appid);
911         _ACCOUNT_FREE(verified_appid);
912
913         if (error_code != _ACCOUNT_ERROR_NONE) {
914                 ACCOUNT_ERROR("No permission to update\n");
915                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
916         }
917
918         error_code = encrypt_access_token(account);
919         if (error_code != _ACCOUNT_ERROR_NONE) {
920                 _ERR("_encrypt_access_token error");
921                 return error_code;
922         }
923
924         _account_compare_old_record_by_user_name(account, user_name, package_name);
925
926         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
927                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
928                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
929         }
930
931         if (!account->package_name) {
932                 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
933                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
934         }
935
936         if (!account->user_name && !account->display_name && !account->email_address) {
937                 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
938                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
939         }
940
941         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE user_name='%s' and package_name='%s'"
942                         , ACCOUNT_TABLE, user_name, package_name);
943
944         count = _account_get_record_count(g_hAccountDB, query);
945
946         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
947                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
948                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
949         }
950
951         if (count <= 0) {
952                 ACCOUNT_SLOGI("_account_update_account_by_user_name : The account not exist!, count = %d, user_name=%s, package_name=%s\n",
953                         count, user_name, package_name);
954                 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
955         }
956
957         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
958
959         //TODO: Is it required to update id ? As of now I can only think of falied rollback cases (between account and gSSO DB)
960         ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
961                         "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
962                         "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
963                         "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE user_name=? and package_name=? ", ACCOUNT_TABLE);
964
965         hstmt = _account_prepare_query(g_hAccountDB, query);
966         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
967                 _account_end_transaction(g_hAccountDB, FALSE);
968                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
969                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
970         }
971         ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
972
973         binding_count = _account_convert_account_to_sql(account, hstmt, query);
974
975         _account_query_bind_text(hstmt, binding_count++, user_name);
976         _account_query_bind_text(hstmt, binding_count++, package_name);
977
978         rc = _account_query_step(hstmt);
979         if (rc != SQLITE_DONE)
980                 ACCOUNT_ERROR("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
981
982         rc = _account_query_finalize(hstmt);
983         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
984         hstmt = NULL;
985
986         /*update capability*/
987         error_code = _account_update_capability_by_user_name(account, user_name, package_name);
988
989         /* update custom */
990         error_code = _account_update_custom(account, account->id);
991
992         return error_code;
993 }
994
995 int _account_insert_to_db(account_s* account, int pid, uid_t uid, int *account_id)
996 {
997         _INFO("");
998         int             error_code = _ACCOUNT_ERROR_NONE;
999         int             ret_transaction = 0;
1000
1001         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1002         ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1003         ACCOUNT_RETURN_VAL((account_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID POINTER IS NULL"));
1004
1005         if (!account->user_name && !account->display_name && !account->email_address) {
1006                 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1007                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1008         }
1009
1010         account_s *data = (account_s*)account;
1011         ACCOUNT_SLOGD("(%s)-(%d) account_insert_to_db: begin_transaction.\n", __FUNCTION__, __LINE__);
1012
1013         pthread_mutex_lock(&account_mutex);
1014
1015         /* transaction control required*/
1016         ret_transaction = _account_begin_transaction(g_hAccountDB);
1017
1018         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1019                 pthread_mutex_unlock(&account_mutex);
1020                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1021                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1022         }
1023
1024         if (ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY) {
1025                 ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction);
1026                 pthread_mutex_unlock(&account_mutex);
1027                 return _ACCOUNT_ERROR_DATABASE_BUSY;
1028         } else if (ret_transaction != _ACCOUNT_ERROR_NONE) {
1029                 ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction);
1030                 pthread_mutex_unlock(&account_mutex);
1031                 return ret_transaction;
1032         }
1033
1034         *account_id = _account_get_next_sequence(g_hAccountDB, ACCOUNT_TABLE);
1035         data->id = *account_id;
1036
1037         char* appid = NULL;
1038         appid = _account_get_current_appid(pid, uid);
1039
1040         if (!appid) {
1041                 _INFO("");
1042                 // API caller cannot be recognized
1043                 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1044                 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1045                 pthread_mutex_unlock(&account_mutex);
1046                 return _ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
1047         }
1048
1049         _INFO("");
1050         char* verified_appid = NULL;
1051         error_code  = _account_get_represented_appid_from_db(g_hAccountDB, g_hAccountGlobalDB, appid, uid, &verified_appid);//FIX
1052         _ACCOUNT_FREE(appid);
1053         if (error_code != _ACCOUNT_ERROR_NONE) {
1054                 _ERR("error_code = %d", error_code);
1055                 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1056                 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1057                 _ACCOUNT_FREE(verified_appid);
1058                 pthread_mutex_unlock(&account_mutex);
1059                 return error_code;
1060         }
1061
1062         if (verified_appid) {
1063                 _INFO("");
1064                 error_code = _account_check_duplicated(g_hAccountDB, data, verified_appid, uid);
1065                 if (error_code != _ACCOUNT_ERROR_NONE) {
1066                         _INFO("");
1067                         ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1068                         ACCOUNT_DEBUG("_account_check_duplicated(), rollback insert query(%x)!!!!\n", ret_transaction);
1069                         *account_id = -1;
1070                         pthread_mutex_unlock(&account_mutex);
1071                         return error_code;
1072                 }
1073                 if (!_account_check_add_more_account(verified_appid)) {
1074                         ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1075                         ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
1076                         pthread_mutex_unlock(&account_mutex);
1077                         _ACCOUNT_FREE(verified_appid);
1078                         return _ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
1079                 }
1080
1081                 _ACCOUNT_FREE(data->package_name);
1082                 data->package_name = _account_dup_text(verified_appid);
1083                 _ACCOUNT_FREE(verified_appid);
1084         }
1085
1086         if (!_account_check_add_more_account(data->package_name)) {
1087                 _INFO("");
1088                 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1089                 ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
1090                 pthread_mutex_unlock(&account_mutex);
1091                 return _ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
1092         }
1093
1094         error_code = encrypt_access_token(data);
1095         if (error_code != _ACCOUNT_ERROR_NONE) {
1096                 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1097                 ACCOUNT_ERROR("encrypt_access_token fail, rollback insert query(%x)!!!!\n", ret_transaction);
1098                 *account_id = -1;
1099                 pthread_mutex_unlock(&account_mutex);
1100                 return error_code;
1101         }
1102
1103         error_code = _account_execute_insert_query(data);
1104
1105         if (error_code != _ACCOUNT_ERROR_NONE) {
1106                 _INFO("");
1107                 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1108                 ACCOUNT_ERROR("INSERT account fail, rollback insert query(%x)!!!!\n", ret_transaction);
1109                 *account_id = -1;
1110                 pthread_mutex_unlock(&account_mutex);
1111                 return error_code;
1112         }
1113
1114         _INFO("");
1115         error_code = _account_insert_capability(data, *account_id);
1116         if (error_code != _ACCOUNT_ERROR_NONE) {
1117                 _INFO("");
1118                 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1119                 ACCOUNT_ERROR("INSERT capability fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1120                 *account_id = -1;
1121                 pthread_mutex_unlock(&account_mutex);
1122                 return error_code;
1123         }
1124
1125         _INFO("");
1126         error_code = _account_insert_custom(data, *account_id);
1127         if (error_code != _ACCOUNT_ERROR_NONE) {
1128                 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1129                 ACCOUNT_ERROR("INSERT custom fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1130                 *account_id = -1;
1131                 pthread_mutex_unlock(&account_mutex);
1132                 return error_code;
1133         }
1134
1135         _INFO("");
1136
1137         pthread_mutex_unlock(&account_mutex);
1138         _account_end_transaction(g_hAccountDB, TRUE);
1139         ACCOUNT_SLOGD("(%s)-(%d) account _end_transaction.\n", __FUNCTION__, __LINE__);
1140
1141         char buf[64] = {0,};
1142         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_INSERT, *account_id);
1143         _account_insert_delete_update_notification_send(buf);
1144         _INFO("account _notification_send end.");
1145
1146         return _ACCOUNT_ERROR_NONE;
1147 }
1148
1149 GSList* _account_get_capability_list_by_account_id(int account_id, int *error_code)
1150 {
1151         *error_code = _ACCOUNT_ERROR_NONE;
1152         account_stmt    hstmt = NULL;
1153         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1154         int                             rc = 0;
1155         GSList* capability_list = NULL;
1156
1157         ACCOUNT_RETURN_VAL((account_id > 0), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("ACCOUNT INDEX IS LESS THAN 0"));
1158         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), { *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED; }, NULL, ("The database isn't connected."));
1159
1160         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1161
1162         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
1163         hstmt = _account_prepare_query(g_hAccountDB, query);
1164
1165         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1166                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1167                 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
1168                 return NULL;
1169         }
1170
1171         rc = _account_query_step(hstmt);
1172         ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1173
1174         account_capability_s* capability_record = NULL;
1175
1176         while (rc == SQLITE_ROW) {
1177                 capability_record = (account_capability_s*) malloc(sizeof(account_capability_s));
1178
1179                 if (capability_record == NULL) {
1180                         ACCOUNT_FATAL("malloc Failed");
1181                         break;
1182                 }
1183
1184                 ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s));
1185
1186                 _account_convert_column_to_capability(hstmt, capability_record);
1187
1188                 //cb_ret = callback(capability_record->type, capability_record->value, user_data);
1189
1190                 //_account_free_capability_items(capability_record);
1191                 //_ACCOUNT_FREE(capability_record);
1192
1193                 //ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
1194
1195                 capability_list = g_slist_append(capability_list, capability_record);
1196                 rc = _account_query_step(hstmt);
1197         }
1198
1199         rc = _account_query_finalize(hstmt);
1200         if (rc != _ACCOUNT_ERROR_NONE) {
1201                 _ERR("finalize error");
1202                 _account_gslist_capability_free(capability_list);
1203                 *error_code = rc;
1204                 return NULL;
1205         }
1206
1207         hstmt = NULL;
1208
1209         *error_code = _ACCOUNT_ERROR_NONE;
1210
1211 CATCH:
1212         if (hstmt != NULL) {
1213                 rc = _account_query_finalize(hstmt);
1214                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), { *error_code = rc; }, NULL, ("finalize error"));
1215                 hstmt = NULL;
1216         }
1217
1218         pthread_mutex_unlock(&account_mutex);
1219
1220         return capability_list;
1221 }
1222
1223 static int _account_compare_old_record(account_s *new_account, int account_id)
1224 {
1225         int                             error_code = _ACCOUNT_ERROR_NONE;
1226         account_stmt    hstmt = NULL;
1227         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1228         int                             rc = 0;
1229         account_s *old_account = NULL;
1230
1231         ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1232         ACCOUNT_RETURN_VAL((new_account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
1233         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1234
1235         old_account = (account_s*)calloc(1, sizeof(account_s));
1236         if (old_account == NULL) {
1237                 _ERR("Out of Memory");
1238                 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
1239         }
1240
1241         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1242
1243         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
1244         hstmt = _account_prepare_query(g_hAccountDB, query);
1245
1246         rc = _account_query_step(hstmt);
1247         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1248
1249         while (rc == SQLITE_ROW) {
1250                 _account_convert_column_to_account(hstmt, old_account);
1251                 rc = _account_query_step(hstmt);
1252         }
1253
1254         rc = _account_query_finalize(hstmt);
1255         ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1256         hstmt = NULL;
1257
1258         // get capability
1259         error_code = _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, old_account->id, (void*)old_account, false);
1260         ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
1261
1262         // get custom text
1263         error_code = _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, old_account->id, (void*)old_account, false);
1264         ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
1265
1266         _account_compare_new_and_old_account_properties(&new_account, old_account);
1267
1268 CATCH:
1269         if (old_account)
1270                 _account_free_account_with_items(old_account);
1271
1272         if (hstmt != NULL) {
1273                 rc = _account_query_finalize(hstmt);
1274                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1275                 hstmt = NULL;
1276         }
1277
1278         return _ACCOUNT_ERROR_NONE;
1279 }
1280
1281 static int _account_get_package_name_from_account_id(int account_id, char **package_name)
1282 {
1283         int                             error_code = _ACCOUNT_ERROR_NONE;
1284         account_stmt    hstmt = NULL;
1285         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1286         int                             rc = 0;
1287         account_s *old_account = NULL;
1288
1289         ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1290         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1291
1292         old_account = (account_s*)calloc(1, sizeof(account_s));
1293         if (old_account == NULL) {
1294                 _ERR("Out Of memory");
1295                 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
1296         }
1297
1298         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1299
1300         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
1301         hstmt = _account_prepare_query(g_hAccountDB, query);
1302
1303         rc = _account_query_step(hstmt);
1304         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1305
1306         while (rc == SQLITE_ROW) {
1307                 _account_convert_column_to_account(hstmt, old_account);
1308                 rc = _account_query_step(hstmt);
1309         }
1310
1311         rc = _account_query_finalize(hstmt);
1312         ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1313         hstmt = NULL;
1314
1315         // get package name.
1316         *package_name = _account_dup_text(old_account->package_name);
1317
1318 CATCH:
1319         if (old_account)
1320                 _account_free_account_with_items(old_account);
1321
1322         if (hstmt != NULL) {
1323                 rc = _account_query_finalize(hstmt);
1324                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1325                 hstmt = NULL;
1326         }
1327
1328         return error_code;
1329
1330 }
1331
1332 static int _account_update_account(int pid, uid_t uid, account_s *account, int account_id)
1333 {
1334         int                             rc = 0, binding_count = 0;
1335         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1336         int                             error_code = _ACCOUNT_ERROR_NONE, count = 0, ret_transaction = 0;
1337         account_stmt    hstmt = NULL;
1338
1339         if (!account->package_name) {
1340                 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
1341                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1342         }
1343
1344         /* Check permission of requested appid */
1345         char* current_appid = NULL;
1346         char *package_name = NULL;
1347
1348         current_appid = _account_get_current_appid(pid, uid);
1349         error_code = _account_get_package_name_from_account_id(account_id, &package_name);
1350
1351         if (error_code != _ACCOUNT_ERROR_NONE || package_name == NULL) {
1352                 ACCOUNT_ERROR("No package name with account_id\n");
1353                 _ACCOUNT_FREE(current_appid);
1354                 _ACCOUNT_FREE(package_name);
1355                 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1356         }
1357
1358         error_code = _account_check_appid_group_with_package_name(current_appid, package_name, uid);
1359         ACCOUNT_DEBUG("UPDATE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name);      // TODO: remove the log later.
1360
1361         _ACCOUNT_FREE(current_appid);
1362         _ACCOUNT_FREE(package_name);
1363
1364         if (error_code != _ACCOUNT_ERROR_NONE) {
1365                 ACCOUNT_ERROR("No permission to update\n");
1366                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1367         }
1368
1369         error_code = encrypt_access_token(account);
1370         if (error_code != _ACCOUNT_ERROR_NONE) {
1371                 _ERR("_encrypt_access_token error");
1372                 return error_code;
1373         }
1374
1375         error_code = _account_compare_old_record(account, account_id);
1376         if (error_code != _ACCOUNT_ERROR_NONE) {
1377                 ACCOUNT_ERROR("_account_compare_old_record fail\n");
1378                 return error_code;
1379         }
1380
1381         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1382                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1383                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1384         } else if (_account_db_err_code(g_hAccountDB) == SQLITE_BUSY) {
1385                 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
1386                 return _ACCOUNT_ERROR_DATABASE_BUSY;
1387         }
1388
1389         if (!account->user_name && !account->display_name && !account->email_address) {
1390                 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1391                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1392         }
1393
1394         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1395
1396         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
1397
1398         count = _account_get_record_count(g_hAccountDB, query);
1399         if (count <= 0) {
1400                 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
1401                 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1402         }
1403
1404         /* transaction control required*/
1405         ret_transaction = _account_begin_transaction(g_hAccountDB);
1406         if (ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY) {
1407                 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
1408                 pthread_mutex_unlock(&account_mutex);
1409                 return _ACCOUNT_ERROR_DATABASE_BUSY;
1410         }
1411
1412         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1413         ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
1414                         "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
1415                         "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
1416                         "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
1417
1418         hstmt = _account_prepare_query(g_hAccountDB, query);
1419
1420         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1421                 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1422                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1423                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1424         }
1425
1426         ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s)(%x).\n", _account_db_err_msg(g_hAccountDB), _account_end_transaction(g_hAccountDB, FALSE)));
1427
1428         binding_count = _account_convert_account_to_sql(account, hstmt, query);
1429         _account_query_bind_int(hstmt, binding_count++, account_id);
1430
1431         rc = _account_query_step(hstmt);
1432         if (rc != SQLITE_DONE)
1433                 ACCOUNT_SLOGE("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
1434
1435         rc = _account_query_finalize(hstmt);
1436         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1437         hstmt = NULL;
1438
1439         _INFO("update query=%s", query);
1440
1441         /*update capability*/
1442         error_code = _account_update_capability(account, account_id);
1443         if (error_code != _ACCOUNT_ERROR_NONE && error_code != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
1444                 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1445                 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1446                 return error_code;
1447         }
1448
1449         /* update custom */
1450         error_code = _account_update_custom(account, account_id);
1451         if (error_code != _ACCOUNT_ERROR_NONE && error_code != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
1452                 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1453                 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1454                 return error_code;
1455         }
1456
1457         ret_transaction = _account_end_transaction(g_hAccountDB, TRUE);
1458
1459         _INFO("update end");
1460
1461         return error_code;
1462 }
1463
1464
1465 static int _account_update_account_ex(account_s *account, int account_id)
1466 {
1467         int                             rc = 0, binding_count = 0;
1468         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1469         int                             error_code = _ACCOUNT_ERROR_NONE, count = 0, ret_transaction = 0;
1470         account_stmt    hstmt = NULL;
1471
1472         if (!account->package_name) {
1473                 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
1474                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1475         }
1476
1477         error_code = encrypt_access_token(account);
1478         if (error_code != _ACCOUNT_ERROR_NONE) {
1479                 _ERR("_encrypt_access_token error");
1480                 return error_code;
1481         }
1482
1483         error_code = _account_compare_old_record(account, account_id);
1484         if (error_code != _ACCOUNT_ERROR_NONE) {
1485                 ACCOUNT_ERROR("_account_compare_old_record fail\n");
1486                 return error_code;
1487         }
1488
1489         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1490                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1491                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1492         }
1493
1494         if (!account->user_name && !account->display_name && !account->email_address) {
1495                 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1496                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1497         }
1498
1499         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1500
1501         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
1502
1503         count = _account_get_record_count(g_hAccountDB, query);
1504         if (count <= 0) {
1505                 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
1506                 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1507         }
1508
1509         /* transaction control required*/
1510         ret_transaction = _account_begin_transaction(g_hAccountDB);
1511         if (ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY) {
1512                 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
1513                 pthread_mutex_unlock(&account_mutex);
1514                 return _ACCOUNT_ERROR_DATABASE_BUSY;
1515         }
1516
1517         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1518         ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
1519                         "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
1520                         "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
1521                         "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
1522
1523         hstmt = _account_prepare_query(g_hAccountDB, query);
1524
1525         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1526                 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1527                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1528                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1529         }
1530
1531         ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s)(%x).\n", _account_db_err_msg(g_hAccountDB), _account_end_transaction(g_hAccountDB, FALSE)));
1532
1533         _INFO("account_update_to_db_by_id_ex_p : before convert() : account_id[%d], user_name=%s", account->id, account->user_name);
1534         binding_count = _account_convert_account_to_sql(account, hstmt, query);
1535         _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], user_name=%s", account->id, account->user_name);
1536         _INFO("account_update_to_db_by_id_ex_p : before bind()");
1537         rc = _account_query_bind_int(hstmt, binding_count++, account_id);
1538         _INFO("account_update_to_db_by_id_ex_p : after bind() : ret = %d", rc);
1539
1540         rc = _account_query_step(hstmt);
1541         if (rc != SQLITE_DONE)
1542                 ACCOUNT_SLOGE("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
1543         _INFO("account_update_to_db_by_id_ex_p : after query_step() : ret = %d", rc);
1544
1545         rc = _account_query_finalize(hstmt);
1546         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1547         hstmt = NULL;
1548         _INFO("account_update_to_db_by_id_ex_p : after query_filnalize() : ret = %d", rc);
1549
1550         _INFO("account_update_to_db_by_id_ex_p : before update_capability()");
1551         /*update capability*/
1552         error_code = _account_update_capability(account, account_id);
1553         if (error_code != _ACCOUNT_ERROR_NONE && error_code != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
1554                 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1555                 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1556                 return error_code;
1557         }
1558         _INFO("account_update_to_db_by_id_ex_p : after update_capability()");
1559
1560         _INFO("account_update_to_db_by_id_ex_p : before update_custom()");
1561         /* update custom */
1562         error_code = _account_update_custom(account, account_id);
1563         if (error_code != _ACCOUNT_ERROR_NONE && error_code != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
1564                 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1565                 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1566                 return error_code;
1567         }
1568         _INFO("account_update_to_db_by_id_ex_p : after update_custom()");
1569
1570         ret_transaction = _account_end_transaction(g_hAccountDB, TRUE);
1571
1572         return error_code;
1573 }
1574
1575
1576 int _account_update_to_db_by_id(int pid, uid_t uid, account_s* account, int account_id)
1577 {
1578         ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
1579         ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
1580         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1581         int     error_code = _ACCOUNT_ERROR_NONE;
1582         account_s* data = (account_s*)account;
1583
1584         pthread_mutex_lock(&account_mutex);
1585
1586         error_code = _account_update_account(pid, uid, data, account_id);
1587
1588         if (error_code != _ACCOUNT_ERROR_NONE) {
1589                 pthread_mutex_unlock(&account_mutex);
1590                 return error_code;
1591         }
1592
1593         pthread_mutex_unlock(&account_mutex);
1594
1595         char buf[64] = {0,};
1596         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_UPDATE, account_id);
1597         _account_insert_delete_update_notification_send(buf);
1598
1599         return _ACCOUNT_ERROR_NONE;
1600 }
1601
1602 int _account_update_to_db_by_id_ex(account_s* account, int account_id)
1603 {
1604         ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
1605         ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
1606         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1607         int     error_code = _ACCOUNT_ERROR_NONE;
1608         account_s* data = account;
1609
1610         pthread_mutex_lock(&account_mutex);
1611
1612         _INFO("before update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name);
1613         error_code = _account_update_account_ex(data, account_id);
1614         _INFO("after update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name);
1615
1616         if (error_code != _ACCOUNT_ERROR_NONE) {
1617                 pthread_mutex_unlock(&account_mutex);
1618                 return error_code;
1619         }
1620
1621         pthread_mutex_unlock(&account_mutex);
1622
1623         char buf[64] = {0,};
1624         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_UPDATE, account_id);
1625         _account_insert_delete_update_notification_send(buf);
1626
1627         return _ACCOUNT_ERROR_NONE;
1628 }
1629
1630 int _account_update_to_db_by_user_name(int pid, uid_t uid, account_s* account, const char *user_name, const char *package_name)
1631 {
1632         ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
1633         ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
1634         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1635
1636         int     error_code = _ACCOUNT_ERROR_NONE;
1637         account_s *data = (account_s*)account;
1638
1639         pthread_mutex_lock(&account_mutex);
1640
1641         error_code = _account_update_account_by_user_name(pid, uid, data, user_name, package_name);
1642
1643         pthread_mutex_unlock(&account_mutex);
1644
1645         char buf[64] = {0,};
1646         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_UPDATE, data->id);
1647         _account_insert_delete_update_notification_send(buf);
1648
1649         return error_code;
1650 }
1651
1652 GSList* _account_db_query_all(int pid, uid_t uid)
1653 {
1654         //int                   error_code = _ACCOUNT_ERROR_NONE;
1655         account_stmt    hstmt = NULL;
1656         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1657         int                             rc = 0;
1658         GSList                  *account_list = NULL;
1659
1660         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected."));
1661
1662         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1663
1664         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TABLE);
1665         hstmt = _account_prepare_query(g_hAccountDB, query);
1666
1667         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1668                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1669                 return NULL;
1670         }
1671
1672         rc = _account_query_step(hstmt);
1673
1674         account_s *account_record = NULL;
1675
1676         if (rc != SQLITE_ROW) {
1677                 _ERR("The record isn't found");
1678                 goto CATCH;
1679         }
1680
1681         while (rc == SQLITE_ROW) {
1682                 account_record = (account_s*) malloc(sizeof(account_s));
1683
1684                 if (account_record == NULL) {
1685                         ACCOUNT_FATAL("malloc Failed");
1686                         break;
1687                 }
1688
1689                 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
1690                 _account_convert_column_to_account(hstmt, account_record);
1691                 account_list = g_slist_append(account_list, account_record);
1692                 rc = _account_query_step(hstmt);
1693         }
1694
1695         rc = _account_query_finalize(hstmt);
1696         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_gslist_account_free(account_list); }, NULL, ("finalize error"));
1697         hstmt = NULL;
1698
1699         GSList* iter;
1700
1701         for (iter = account_list; iter != NULL; iter = g_slist_next(iter)) {
1702                 account_s *account = NULL;
1703                 account = (account_s*)iter->data;
1704                 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, account->id, (void*)account, false);
1705                 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, account->id, (void*)account, false);
1706         }
1707
1708 CATCH:
1709         if (hstmt != NULL) {
1710                 rc = _account_query_finalize(hstmt);
1711                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_gslist_account_free(account_list); }, NULL, ("finalize error"));
1712                 hstmt = NULL;
1713         }
1714
1715         if (account_list)
1716                 _remove_sensitive_info_from_non_owning_account_slist(account_list, pid, uid);
1717
1718         return account_list;
1719 }
1720
1721 int _account_update_sync_status_by_id(uid_t uid, int account_db_id, const int sync_status)
1722 {
1723         int                             error_code = _ACCOUNT_ERROR_NONE;
1724         account_stmt    hstmt = NULL;
1725         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1726         int                             rc = 0;
1727         int count = 1;
1728
1729         ACCOUNT_RETURN_VAL((account_db_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1730         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1731         if ((sync_status < 0) || (sync_status >= _ACCOUNT_SYNC_MAX)) {
1732                 ACCOUNT_SLOGE("(%s)-(%d) sync_status is less than 0 or more than enum max.\n", __FUNCTION__, __LINE__);
1733                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1734         }
1735
1736         pthread_mutex_lock(&account_mutex);
1737
1738         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1739
1740         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_db_id);
1741
1742         rc = _account_get_record_count(g_hAccountDB, query);
1743
1744         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1745                 pthread_mutex_unlock(&account_mutex);
1746                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1747                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1748         }
1749
1750         if (rc <= 0) {
1751                 ACCOUNT_SLOGE("account_update_sync_status_by_id : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
1752                 pthread_mutex_unlock(&account_mutex);
1753                 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1754         }
1755
1756         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1757
1758         ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET sync_support=? WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
1759         hstmt = _account_prepare_query(g_hAccountDB, query);
1760
1761         _account_query_bind_int(hstmt, count, sync_status);
1762
1763         rc = _account_query_step(hstmt);
1764
1765         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1766                 pthread_mutex_unlock(&account_mutex);
1767                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1768                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1769         }
1770
1771         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_DB_FAILED,
1772                                 ("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB)));
1773
1774         rc = _account_query_finalize(hstmt);
1775         if (rc != _ACCOUNT_ERROR_NONE) {
1776                 ACCOUNT_ERROR("_account_query_finalize error");
1777                 pthread_mutex_unlock(&account_mutex);
1778                 return rc;
1779         }
1780
1781         char buf[64] = {0,};
1782         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_SYNC_UPDATE, account_db_id);
1783         _account_insert_delete_update_notification_send(buf);
1784
1785         hstmt = NULL;
1786         error_code = _ACCOUNT_ERROR_NONE;
1787
1788 CATCH:
1789         if (hstmt != NULL) {
1790                 rc = _account_query_finalize(hstmt);
1791                 pthread_mutex_unlock(&account_mutex);
1792                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1793                 hstmt = NULL;
1794         }
1795
1796         pthread_mutex_unlock(&account_mutex);
1797
1798         return error_code;
1799 }
1800
1801 int _account_query_account_by_account_id(int pid, uid_t uid, int account_db_id, account_s *account_record, bool query_del_acc)
1802 {
1803         _INFO("_account_query_account_by_account_id() start, account_db_id=[%d], query_del_acc=[%d]", account_db_id, query_del_acc);
1804
1805         int error_code = _ACCOUNT_ERROR_NONE;
1806         account_stmt hstmt = NULL;
1807         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1808         int rc = 0;
1809
1810         ACCOUNT_RETURN_VAL((account_db_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1811         ACCOUNT_RETURN_VAL(account_record != NULL, {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
1812         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1813
1814         ACCOUNT_DEBUG("starting db operations");
1815
1816         /* prepare query for account-table */
1817         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1818         if (!query_del_acc)
1819                 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
1820         else
1821                 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", DELETED_ACCOUNT_TABLE, account_db_id);
1822
1823         _INFO("after _account_prepare_query, rc=[%d]", rc);
1824
1825         hstmt = _account_prepare_query(g_hAccountDB, query);
1826
1827         rc = _account_db_err_code(g_hAccountDB);
1828         if (rc == SQLITE_PERM) {
1829                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1830                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1831         }
1832
1833         ACCOUNT_DEBUG("before _account_query_step");
1834
1835         rc = _account_query_step(hstmt);
1836
1837         ACCOUNT_DEBUG("after _account_query_step returned [%d]", rc);
1838
1839         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1840
1841         while (rc == SQLITE_ROW) {
1842                 ACCOUNT_DEBUG("before _account_convert_column_to_account");
1843                 _account_convert_column_to_account(hstmt, account_record);
1844                 ACCOUNT_DEBUG("after _account_convert_column_to_account");
1845                 ACCOUNT_DEBUG("user_name = %s, user_txt[0] = %s, user_int[1] = %d", account_record->user_name, account_record->user_data_txt[0], account_record->user_data_int[1]);
1846                 rc = _account_query_step(hstmt);
1847         }
1848
1849         ACCOUNT_DEBUG("account_record->id=[%d]", account_record->id);
1850
1851         rc = _account_query_finalize(hstmt);
1852         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1853
1854         ACCOUNT_DEBUG("before _account_query_capability_by_account_id");
1855         _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, account_record->id, (void*)account_record, query_del_acc);
1856         ACCOUNT_DEBUG("after _account_query_capability_by_account_id");
1857
1858         ACCOUNT_DEBUG("before _account_query_custom_by_account_id");
1859         _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, account_record->id, (void*)account_record, query_del_acc);
1860         ACCOUNT_DEBUG("after _account_query_custom_by_account_id");
1861
1862         hstmt = NULL;
1863         error_code = _ACCOUNT_ERROR_NONE;
1864
1865 CATCH:
1866         if (hstmt != NULL) {
1867                 rc = _account_query_finalize(hstmt);
1868                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1869                 hstmt = NULL;
1870         }
1871
1872         if (account_record)
1873                 _remove_sensitive_info_from_non_owning_account(account_record, pid, uid);
1874
1875         pthread_mutex_unlock(&account_mutex);
1876         ACCOUNT_DEBUG("_account_query_account_by_account_id end [%d]", error_code);
1877
1878         return error_code;
1879 }
1880
1881 GList* _account_query_account_by_user_name(int pid, uid_t uid, const char *user_name, int *error_code)
1882 {
1883         *error_code = _ACCOUNT_ERROR_NONE;
1884         account_stmt    hstmt = NULL;
1885         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1886         int                             rc = 0;
1887         account_s *account_head = NULL;
1888
1889         if (user_name == NULL) {
1890                 _ERR("USER NAME IS NULL");
1891                 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
1892                 goto CATCH;
1893         }
1894
1895         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1896
1897         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ?", ACCOUNT_TABLE);
1898
1899         hstmt = _account_prepare_query(g_hAccountDB, query);
1900
1901         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1902                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1903                 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
1904                 goto CATCH;
1905         }
1906
1907         int binding_count = 1;
1908         _account_query_bind_text(hstmt, binding_count++, user_name);
1909
1910         rc = _account_query_step(hstmt);
1911
1912         if (rc != SQLITE_ROW) {
1913                 _ERR("The record isn't found");
1914                 *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1915                 goto CATCH;
1916         }
1917
1918         int tmp = 0;
1919
1920         account_head = (account_s*) malloc(sizeof(account_s));
1921         if (account_head == NULL) {
1922                 ACCOUNT_FATAL("malloc Failed");
1923                 if (hstmt != NULL) {
1924                         rc = _account_query_finalize(hstmt);
1925                         if (rc != _ACCOUNT_ERROR_NONE) {
1926                                 _ERR("finalize error");
1927                                 *error_code = rc;
1928                                 goto CATCH;
1929                         }
1930                         hstmt = NULL;
1931                 }
1932                 *error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
1933                 goto CATCH;
1934         }
1935         ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
1936
1937         while (rc == SQLITE_ROW) {
1938                 account_s* account_record = NULL;
1939
1940                 account_record = (account_s*) malloc(sizeof(account_s));
1941
1942                 if (account_record == NULL) {
1943                         ACCOUNT_FATAL("malloc Failed");
1944                         break;
1945                 }
1946                 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
1947
1948                 _account_convert_column_to_account(hstmt, account_record);
1949
1950                 account_head->account_list = g_list_append(account_head->account_list, account_record);
1951
1952                 rc = _account_query_step(hstmt);
1953                 tmp++;
1954         }
1955
1956         rc = _account_query_finalize(hstmt);
1957
1958         if (rc != _ACCOUNT_ERROR_NONE) {
1959                 _ERR("finalize error");
1960                 *error_code = rc;
1961                 goto CATCH;
1962         }
1963
1964         hstmt = NULL;
1965
1966         GList *iter;
1967
1968         tmp = g_list_length(account_head->account_list);
1969
1970         for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
1971                 //account_h account;
1972                 //account = (account_h)iter->data;
1973
1974                 account_s *testaccount = (account_s*)iter->data;
1975
1976                 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, testaccount->id, (void*)testaccount, false);
1977                 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, testaccount->id, (void*)testaccount, false);
1978         }
1979
1980         *error_code = _ACCOUNT_ERROR_NONE;
1981
1982 CATCH:
1983         if (hstmt != NULL) {
1984                 rc = _account_query_finalize(hstmt);
1985                 if (rc != _ACCOUNT_ERROR_NONE) {
1986                         _ERR("finalize error");
1987                         *error_code = rc;
1988                 }
1989                 hstmt = NULL;
1990         }
1991
1992         pthread_mutex_unlock(&account_mutex);
1993
1994         if (account_head) {
1995                 _remove_sensitive_info_from_non_owning_account_list(account_head->account_list, pid, uid);
1996                 GList* result = account_head->account_list;
1997                 _ACCOUNT_FREE(account_head);
1998                 return result;
1999         }
2000
2001         return NULL;
2002 }
2003
2004 GList*
2005 _account_query_account_by_capability(int pid, uid_t uid, const char* capability_type, const int capability_value, int *error_code)
2006 {
2007         *error_code = _ACCOUNT_ERROR_NONE;
2008         account_stmt    hstmt = NULL;
2009         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2010         int                             rc = 0;
2011
2012         ACCOUNT_RETURN_VAL((capability_type != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("capability_type IS NULL"));
2013
2014         if ((capability_value  < 0) || (capability_value >= _ACCOUNT_CAPABILITY_STATE_MAX)) {
2015                 ACCOUNT_SLOGE("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
2016                 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
2017                 return NULL;
2018         }
2019
2020         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), { *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED; }, NULL, ("The database isn't connected."));
2021
2022         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2023
2024         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=? AND value=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
2025
2026         hstmt = _account_prepare_query(g_hAccountDB, query);
2027
2028         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2029                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2030                 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
2031                 return NULL;
2032         }
2033
2034         int binding_count = 1;
2035         _account_query_bind_text(hstmt, binding_count++, capability_type);
2036         _account_query_bind_int(hstmt, binding_count++, capability_value);
2037
2038         rc = _account_query_step(hstmt);
2039
2040         account_s* account_head = NULL;
2041
2042         ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2043
2044         int tmp = 0;
2045
2046         account_head = (account_s*) malloc(sizeof(account_s));
2047         if (account_head == NULL) {
2048                 ACCOUNT_FATAL("malloc Failed");
2049                 if (hstmt != NULL) {
2050                         rc = _account_query_finalize(hstmt);
2051                         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), { *error_code = rc; }, NULL, ("finalize error"));
2052                         hstmt = NULL;
2053                 }
2054                 *error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
2055                 return NULL;
2056         }
2057         ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2058
2059         while (rc == SQLITE_ROW) {
2060                 account_s* account_record = NULL;
2061
2062                 account_record = (account_s*) malloc(sizeof(account_s));
2063
2064                 if (account_record == NULL) {
2065                         ACCOUNT_FATAL("malloc Failed");
2066                         break;
2067                 }
2068                 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2069
2070                 _account_convert_column_to_account(hstmt, account_record);
2071
2072                 account_head->account_list = g_list_append(account_head->account_list, account_record);
2073
2074                 rc = _account_query_step(hstmt);
2075                 tmp++;
2076         }
2077
2078         rc = _account_query_finalize(hstmt);
2079         ACCOUNT_CATCH_ERROR_P((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2080         hstmt = NULL;
2081
2082         GList *iter;
2083
2084
2085         tmp = g_list_length(account_head->account_list);
2086
2087         for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2088                 //account_h account = NULL;
2089                 //account = (account_h)iter->data;
2090                 account_s* testaccount = (account_s*)iter->data;
2091
2092                 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, testaccount->id, (void*)testaccount, false);
2093                 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, testaccount->id, (void*)testaccount, false);
2094
2095         }
2096
2097         *error_code = _ACCOUNT_ERROR_NONE;
2098
2099 CATCH:
2100         if (hstmt != NULL) {
2101                 rc = _account_query_finalize(hstmt);
2102                 if (rc != _ACCOUNT_ERROR_NONE) {
2103                         *error_code = rc;
2104                         _ERR("finalize error");
2105                 }
2106                 hstmt = NULL;
2107         }
2108
2109         if (*error_code != _ACCOUNT_ERROR_NONE && account_head) {
2110                 _account_glist_account_free(account_head->account_list);
2111                 _ACCOUNT_FREE(account_head);
2112                 account_head = NULL;
2113         }
2114
2115         pthread_mutex_unlock(&account_mutex);
2116
2117         if (account_head) {
2118                 _remove_sensitive_info_from_non_owning_account_list(account_head->account_list, pid, uid);
2119                 GList* result = account_head->account_list;
2120                 _ACCOUNT_FREE(account_head);
2121                 return result;
2122         }
2123
2124         return NULL;
2125 }
2126
2127 GList* _account_query_account_by_capability_type(int pid, uid_t uid, const char* capability_type, int *error_code)
2128 {
2129         *error_code = _ACCOUNT_ERROR_NONE;
2130         account_stmt    hstmt = NULL;
2131         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2132         int                             rc = 0;
2133
2134         ACCOUNT_RETURN_VAL((capability_type != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("capability_type IS NULL"));
2135         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), { *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED; },
2136                                            NULL, ("The database isn't connected."));
2137
2138         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2139
2140         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
2141
2142         hstmt = _account_prepare_query(g_hAccountDB, query);
2143
2144         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2145                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2146                 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
2147                 return NULL;
2148         }
2149
2150         int binding_count = 1;
2151         _account_query_bind_text(hstmt, binding_count++, capability_type);
2152
2153         rc = _account_query_step(hstmt);
2154
2155         account_s* account_head = NULL;
2156
2157         ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2158
2159         int tmp = 0;
2160
2161         account_head = (account_s*) malloc(sizeof(account_s));
2162         if (account_head == NULL) {
2163                 ACCOUNT_FATAL("malloc Failed");
2164                 if (hstmt != NULL) {
2165                         rc = _account_query_finalize(hstmt);
2166                         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), { *error_code = rc; }, NULL, ("finalize error"));
2167                         hstmt = NULL;
2168                 }
2169                 *error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
2170                 return NULL;
2171         }
2172         ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2173
2174         while (rc == SQLITE_ROW) {
2175                 account_s* account_record = NULL;
2176
2177                 account_record = (account_s*) malloc(sizeof(account_s));
2178
2179                 if (account_record == NULL) {
2180                         ACCOUNT_FATAL("malloc Failed");
2181                         break;
2182                 }
2183                 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2184
2185                 _account_convert_column_to_account(hstmt, account_record);
2186
2187                 account_head->account_list = g_list_append(account_head->account_list, account_record);
2188
2189                 rc = _account_query_step(hstmt);
2190                 tmp++;
2191         }
2192
2193         rc = _account_query_finalize(hstmt);
2194         ACCOUNT_CATCH_ERROR_P((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2195         hstmt = NULL;
2196
2197         GList *iter;
2198
2199
2200         tmp = g_list_length(account_head->account_list);
2201
2202         for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2203                 account_s* testaccount = (account_s*)iter->data;
2204
2205                 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, testaccount->id, (void*)testaccount, false);
2206                 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, testaccount->id, (void*)testaccount, false);
2207
2208         }
2209
2210         *error_code = _ACCOUNT_ERROR_NONE;
2211
2212 CATCH:
2213         if (hstmt != NULL) {
2214                 rc = _account_query_finalize(hstmt);
2215                 if (rc != _ACCOUNT_ERROR_NONE) {
2216                         *error_code = rc;
2217                         _ERR("finalize error");
2218                 }
2219                 hstmt = NULL;
2220         }
2221
2222         if ((*error_code != _ACCOUNT_ERROR_NONE) && account_head) {
2223                 _account_glist_account_free(account_head->account_list);
2224                 _ACCOUNT_FREE(account_head);
2225                 account_head = NULL;
2226         }
2227
2228         pthread_mutex_unlock(&account_mutex);
2229
2230         if (account_head) {
2231                 _remove_sensitive_info_from_non_owning_account_list(account_head->account_list, pid, uid);
2232                 GList* result = account_head->account_list;
2233                 _ACCOUNT_FREE(account_head);
2234                 return result;
2235         }
2236
2237         return NULL;
2238 }
2239
2240 GList* account_server_query_account_by_package_name(const char* package_name, int *error_code, int pid, uid_t uid)
2241 {
2242         _INFO("account_server_query_account_by_package_name start");
2243
2244         GList * account_list = NULL;
2245         *error_code = _ACCOUNT_ERROR_NONE;
2246
2247         ACCOUNT_RETURN_VAL((package_name != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("PACKAGE NAME IS NULL"));
2248         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), { *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED; }, NULL, ("The database isn't connected."));
2249
2250         account_list = _account_query_account_by_package_name(g_hAccountDB, package_name, error_code, pid, uid);
2251
2252         _INFO("account_server_query_account_by_package_name end");
2253
2254         return account_list;
2255 }
2256
2257 int account_server_delete_account_by_package_name(const char* package_name, bool permission, int pid, uid_t uid)
2258 {
2259         _INFO("account_db_delete_account_by_package_name");
2260
2261         int error_code = _ACCOUNT_ERROR_NONE;
2262
2263         ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
2264         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2265
2266         error_code = _account_delete_account_by_package_name(g_hAccountDB, package_name, permission, pid, uid);
2267
2268         _INFO("account_server_delete_account_by_package_name end");
2269
2270         return error_code;
2271 }
2272
2273 int _account_delete(int pid, uid_t uid, int account_id)
2274 {
2275         int error_code = _ACCOUNT_ERROR_NONE;
2276         account_stmt    hstmt = NULL;
2277         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2278         int                             rc = 0;
2279         int                             ret_transaction = 0;
2280         bool                    is_success = FALSE;
2281
2282         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2283
2284         int count = -1;
2285         /* Check requested ID to delete */
2286         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id=%d", ACCOUNT_TABLE, account_id);
2287
2288         count = _account_get_record_count(g_hAccountDB, query);
2289
2290         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2291                 pthread_mutex_unlock(&account_mutex);
2292                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2293                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2294         }
2295
2296         if (count <= 0) {
2297                 ACCOUNT_ERROR("account id(%d) is not exist. count(%d)\n", account_id, count);
2298                 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
2299         }
2300
2301         /* Check permission of requested appid */
2302         char* current_appid = NULL;
2303         char *package_name = NULL;
2304
2305         current_appid = _account_get_current_appid(pid, uid);
2306
2307         error_code = _account_get_package_name_from_account_id(account_id, &package_name);
2308
2309         if (error_code != _ACCOUNT_ERROR_NONE) {
2310                 ACCOUNT_ERROR("No package name with account_id\n");
2311                 _ACCOUNT_FREE(current_appid);
2312                 _ACCOUNT_FREE(package_name);
2313                 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
2314         }
2315         ACCOUNT_DEBUG("DELETE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name);
2316
2317         error_code = _account_check_appid_group_with_package_name(current_appid, package_name, uid);
2318
2319         _ACCOUNT_FREE(current_appid);
2320         _ACCOUNT_FREE(package_name);
2321
2322         if (error_code != _ACCOUNT_ERROR_NONE) {
2323                 ACCOUNT_ERROR("No permission to delete\n");
2324                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2325         }
2326
2327         /* transaction control required*/
2328         ret_transaction = _account_begin_transaction(g_hAccountDB);
2329
2330         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2331                 pthread_mutex_unlock(&account_mutex);
2332                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2333                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2334         }
2335
2336         if (ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY) {
2337                 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
2338                 pthread_mutex_unlock(&account_mutex);
2339                 return _ACCOUNT_ERROR_DATABASE_BUSY;
2340         }
2341
2342         if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2343                 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
2344                 pthread_mutex_unlock(&account_mutex);
2345                 return ret_transaction;
2346         }
2347
2348         /* capability table */
2349         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2350         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
2351
2352         hstmt = _account_prepare_query(g_hAccountDB, query);
2353
2354         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2355                 pthread_mutex_unlock(&account_mutex);
2356                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2357                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2358         }
2359
2360         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2361                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2362
2363         rc = _account_query_step(hstmt);
2364         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2365
2366         rc = _account_query_finalize(hstmt);
2367
2368         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2369         hstmt = NULL;
2370
2371         /* account table */
2372         ACCOUNT_MEMSET(query, 0, sizeof(query));
2373         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
2374
2375         hstmt = _account_prepare_query(g_hAccountDB, query);
2376         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2377                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2378
2379         rc = _account_query_step(hstmt);
2380         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
2381
2382         rc = _account_query_finalize(hstmt);
2383         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2384         hstmt = NULL;
2385
2386         /* delete custom data */
2387         ACCOUNT_MEMSET(query, 0, sizeof(query));
2388         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
2389
2390         hstmt = _account_prepare_query(g_hAccountDB, query);
2391
2392         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2393                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2394
2395         rc = _account_query_step(hstmt);
2396         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
2397
2398         rc = _account_query_finalize(hstmt);
2399         ACCOUNT_CATCH_ERROR(rc == _ACCOUNT_ERROR_NONE, {}, rc, ("finalize error", account_id, rc));
2400         hstmt = NULL;
2401
2402         is_success = TRUE;
2403
2404 CATCH:
2405         if (hstmt != NULL) {
2406                 rc = _account_query_finalize(hstmt);
2407                 if (rc != _ACCOUNT_ERROR_NONE) {
2408                         ACCOUNT_ERROR("rc (%d)", rc);
2409                         is_success = FALSE;
2410                 }
2411
2412                 hstmt = NULL;
2413         }
2414
2415         ret_transaction = _account_end_transaction(g_hAccountDB, is_success);
2416
2417         if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2418                 ACCOUNT_ERROR("account_delete:_account_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
2419         } else {
2420                 if (is_success == true) {
2421                         char buf[64] = {0,};
2422                         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_DELETE, account_id);
2423                         _account_insert_delete_update_notification_send(buf);
2424                 }
2425         }
2426
2427         pthread_mutex_unlock(&account_mutex);
2428
2429         return error_code;
2430 }
2431
2432 static int _account_query_account_by_username_and_package(const char* username, const char* package_name, account_s *account)
2433 {
2434         //FIXME
2435         //return -1;
2436         int                             error_code = _ACCOUNT_ERROR_NONE;
2437         account_stmt    hstmt = NULL;
2438         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2439         int                             rc = 0;
2440         int                             binding_count = 1;
2441
2442         ACCOUNT_RETURN_VAL((username != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("username IS NULL"));
2443         ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name IS NULL"));
2444         ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2445         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2446
2447         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2448
2449         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
2450         hstmt = _account_prepare_query(g_hAccountDB, query);
2451
2452         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2453                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2454                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2455         }
2456
2457         _account_query_bind_text(hstmt, binding_count++, username);
2458         _account_query_bind_text(hstmt, binding_count++, package_name);
2459
2460         rc = _account_query_step(hstmt);
2461         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2462
2463         account_s *account_record = account;
2464
2465         while (rc == SQLITE_ROW) {
2466                 _account_convert_column_to_account(hstmt, account_record);
2467                 rc = _account_query_step(hstmt);
2468         }
2469
2470         rc = _account_query_finalize(hstmt);
2471         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2472         _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, account_record->id, (void*)account_record, false);
2473         _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, account_record->id, (void*)account_record, false);
2474
2475         hstmt = NULL;
2476         error_code = _ACCOUNT_ERROR_NONE;
2477
2478 CATCH:
2479         if (hstmt != NULL) {
2480                 rc = _account_query_finalize(hstmt);
2481                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2482                 hstmt = NULL;
2483         }
2484
2485         pthread_mutex_unlock(&account_mutex);
2486
2487         return error_code;
2488 }
2489
2490 int _account_create(account_s **account)
2491 {
2492         if (!account) {
2493                 ACCOUNT_SLOGE("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__);
2494                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2495         }
2496
2497         account_s *data = (account_s*)malloc(sizeof(account_s));
2498
2499         if (data == NULL) {
2500                 ACCOUNT_FATAL("Memory Allocation Failed");
2501                 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
2502         }
2503         ACCOUNT_MEMSET(data, 0, sizeof(account_s));
2504
2505         /*Setting account as visible by default*/
2506         //data->secret = _ACCOUNT_SECRECY_VISIBLE;
2507
2508         /*Setting account as not supporting sync by default*/
2509         //data->sync_support = _ACCOUNT_SYNC_NOT_SUPPORT;
2510
2511         *account = data;
2512
2513         return _ACCOUNT_ERROR_NONE;
2514 }
2515
2516 int _account_destroy(account_s *account)
2517 {
2518         account_s *data = account;
2519
2520         ACCOUNT_RETURN_VAL((data != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Account handle is null!"));
2521
2522         _account_free_account_with_items(data);
2523
2524         return _ACCOUNT_ERROR_NONE;
2525 }
2526
2527 int _account_get_account_id(account_s* account, int *account_id)
2528 {
2529         if (!account)
2530                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2531
2532         if (!account_id)
2533                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2534
2535         *account_id = account->id;
2536
2537         return _ACCOUNT_ERROR_NONE;
2538 }
2539
2540 int _account_delete_from_db_by_user_name(int pid, uid_t uid, const char *user_name, const char *package_name)
2541 {
2542         _INFO("[%s][%s]", user_name, package_name);
2543
2544         int                             error_code = _ACCOUNT_ERROR_NONE;
2545         account_stmt    hstmt = NULL;
2546         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2547         int                             rc = 0;
2548         int                             ret_transaction = 0;
2549         bool                    is_success = FALSE;
2550         account_s               *account = NULL;
2551         int                             binding_count = 1;
2552         int                             account_id = -1;
2553
2554         ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is null!"));
2555         ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
2556         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2557
2558         /* Check permission of requested appid */
2559         char* current_appid = NULL;
2560         char* package_name_temp = NULL;
2561
2562         current_appid = _account_get_current_appid(pid, uid);
2563
2564         package_name_temp = _account_dup_text(package_name);
2565
2566         ACCOUNT_DEBUG("DELETE:user_name[%s],current_appid[%s], package_name[%s]", user_name, current_appid, package_name_temp);
2567
2568         error_code = _account_check_appid_group_with_package_name(current_appid, package_name_temp, uid);
2569
2570         _ACCOUNT_FREE(current_appid);
2571         _ACCOUNT_FREE(package_name_temp);
2572
2573         if (error_code != _ACCOUNT_ERROR_NONE) {
2574                 ACCOUNT_ERROR("No permission to delete\n");
2575                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2576         }
2577
2578         rc = _account_create(&account);
2579         rc = _account_query_account_by_username_and_package(user_name, package_name, account);
2580
2581         _INFO("");
2582
2583         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2584                 _account_destroy(account);
2585                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2586                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2587         }
2588
2589         _INFO("");
2590         account_s* account_data = (account_s*)account;
2591
2592         rc = _account_get_account_id(account_data, &account_id);
2593
2594         rc = _account_destroy(account);
2595
2596         /* transaction control required*/
2597         ret_transaction = _account_begin_transaction(g_hAccountDB);
2598
2599         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2600                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2601                 pthread_mutex_unlock(&account_mutex);
2602                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2603         }
2604
2605         _INFO("");
2606         if (ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY) {
2607                 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
2608                 pthread_mutex_unlock(&account_mutex);
2609                 return _ACCOUNT_ERROR_DATABASE_BUSY;
2610         } else if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2611                 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
2612                 pthread_mutex_unlock(&account_mutex);
2613                 return ret_transaction;
2614         }
2615
2616         /* delete custom data */
2617         ACCOUNT_MEMSET(query, 0, sizeof(query));
2618         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = ?", ACCOUNT_CUSTOM_TABLE);
2619
2620         hstmt = _account_prepare_query(g_hAccountDB, query);
2621
2622         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2623                 _account_end_transaction(g_hAccountDB, FALSE);
2624                 pthread_mutex_unlock(&account_mutex);
2625                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2626                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2627         }
2628
2629         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2630                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2631
2632         _account_query_bind_int(hstmt, binding_count++, account_id);
2633
2634         rc = _account_query_step(hstmt);
2635         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2636
2637         rc = _account_query_finalize(hstmt);
2638         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2639         hstmt = NULL;
2640
2641         /* delete capability */
2642         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", CAPABILITY_TABLE);
2643
2644         hstmt = _account_prepare_query(g_hAccountDB, query);
2645
2646         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2647                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2648
2649         binding_count = 1;
2650         _account_query_bind_text(hstmt, binding_count++, user_name);
2651         _account_query_bind_text(hstmt, binding_count++, package_name);
2652
2653         rc = _account_query_step(hstmt);
2654         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2655
2656         rc = _account_query_finalize(hstmt);
2657         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2658         hstmt = NULL;
2659
2660         ACCOUNT_MEMSET(query, 0, sizeof(query));
2661
2662         _INFO("");
2663         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
2664
2665         hstmt = _account_prepare_query(g_hAccountDB, query);
2666         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2667                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2668
2669         _INFO("");
2670         binding_count = 1;
2671         _account_query_bind_text(hstmt, binding_count++, user_name);
2672         _account_query_bind_text(hstmt, binding_count++, package_name);
2673
2674         rc = _account_query_step(hstmt);
2675         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. user_name=%s, package_name=%s, rc=%d\n", user_name, package_name, rc));
2676
2677         rc = _account_query_finalize(hstmt);
2678         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2679         is_success = TRUE;
2680
2681         hstmt = NULL;
2682
2683 CATCH:
2684         if (hstmt != NULL) {
2685                 rc = _account_query_finalize(hstmt);
2686                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2687                 hstmt = NULL;
2688         }
2689
2690         ret_transaction = _account_end_transaction(g_hAccountDB, is_success);
2691
2692         if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2693                 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
2694         } else {
2695                 if (is_success == true) {
2696                         char buf[64] = {0,};
2697                         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_DELETE, account_id);
2698                         _account_insert_delete_update_notification_send(buf);
2699                 }
2700         }
2701
2702         pthread_mutex_unlock(&account_mutex);
2703
2704         return error_code;
2705 }
2706
2707
2708 int _account_get_total_count_from_db(gboolean include_hidden, int *count)
2709 {
2710         if (!count) {
2711                 ACCOUNT_SLOGE("(%s)-(%d) count is NULL.\n", __FUNCTION__, __LINE__);
2712                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2713         }
2714
2715         if (!g_hAccountDB) {
2716                 ACCOUNT_ERROR("DB is not opened\n");
2717                 return _ACCOUNT_ERROR_DB_NOT_OPENED;
2718         }
2719
2720         char query[1024] = {0, };
2721         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2722
2723         if (include_hidden)
2724                 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s", ACCOUNT_TABLE);
2725         else
2726                 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s where secret = %d", ACCOUNT_TABLE, _ACCOUNT_SECRECY_VISIBLE);
2727
2728         *count = _account_get_record_count(g_hAccountDB, query);
2729
2730         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2731                 pthread_mutex_unlock(&account_mutex);
2732                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2733                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2734         }
2735
2736         int rc = -1;
2737         int ncount = 0;
2738         account_stmt pStmt = NULL;
2739
2740         rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
2741         if (SQLITE_OK != rc) {
2742                 ACCOUNT_SLOGE("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg(g_hAccountDB));
2743                 sqlite3_finalize(pStmt);
2744                 return _ACCOUNT_ERROR_DB_FAILED;
2745         }
2746
2747         rc = sqlite3_step(pStmt);
2748         if (SQLITE_ROW != rc) {
2749                 ACCOUNT_ERROR("[ERROR] sqlite3_step() failed\n");
2750                 sqlite3_finalize(pStmt);
2751                 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
2752         }
2753
2754         ncount = sqlite3_column_int(pStmt, 0);
2755
2756         *count = ncount;
2757
2758         sqlite3_finalize(pStmt);
2759
2760         if (ncount < 0) {
2761                 ACCOUNT_ERROR("[ERROR] Number of account : %d, End", ncount);
2762                 return _ACCOUNT_ERROR_DB_FAILED;
2763         }
2764
2765         return _ACCOUNT_ERROR_NONE;
2766 }
2767
2768
2769 int account_server_query_app_id_exist(const char* app_id)
2770 {
2771         _INFO("account_server_query_app_id_exist start app_id=[%s]", app_id);
2772         int ret = _ACCOUNT_ERROR_NONE;
2773
2774         ret = _account_type_query_app_id_exist_from_all_db(g_hAccountDB, g_hAccountGlobalDB, app_id);
2775
2776         _INFO("account_server_query_app_id_exist end error_code=[%d]", ret);
2777
2778         return ret;
2779 }
2780
2781 int account_server_insert_account_type_to_user_db(account_type_s *account_type, int *account_type_id, uid_t uid)
2782 {
2783         ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE HANDLE IS NULL"));
2784         ACCOUNT_RETURN_VAL((account_type->app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID OF ACCOUNT TYPE IS NULL"));
2785         ACCOUNT_RETURN_VAL((account_type_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE ID IS NULL"));
2786
2787         _INFO("account_server_insert_account_type_to_user_db start uid=[%d]", uid);
2788         int ret = _ACCOUNT_ERROR_NONE;
2789
2790         if (_account_type_check_duplicated(g_hAccountDB, account_type->app_id) ||
2791                         _account_type_check_duplicated(g_hAccountGlobalDB, account_type->app_id)) {
2792                 *account_type_id = -1;
2793                 return _ACCOUNT_ERROR_DUPLICATED;
2794         }
2795
2796         ret = _account_type_insert_to_db(g_hAccountDB, account_type, account_type_id);
2797         _INFO("account_server_insert_account_type_to_user_db end error_code=[%d]", ret);
2798
2799         return ret;
2800 }
2801
2802 int account_server_delete_account_type_by_app_id_from_user_db(const char * app_id)
2803 {
2804         ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID OF ACCOUNT TYPE IS NULL"));
2805
2806         _INFO("account_server_delete_account_type_by_app_id_from_user_db start");
2807         int ret = _ACCOUNT_ERROR_NONE;
2808
2809         ret = _account_type_delete_by_app_id(g_hAccountDB, app_id);
2810         _INFO("account_server_delete_account_type_by_app_id_from_user_db end error_code=[%d]", ret);
2811
2812         return ret;
2813 }
2814
2815 GSList* _account_type_query_provider_feature_by_app_id_from_global_db(const char* app_id, int *error_code)
2816 {
2817         _INFO("_account_type_query_provider_feature_by_app_id_in_global_db app_id=%s", app_id);
2818         account_stmt hstmt = NULL;
2819         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2820         int rc = 0, binding_count = 1;
2821         GSList* feature_list = NULL;
2822
2823         ACCOUNT_RETURN_VAL((app_id != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("APP ID IS NULL"));
2824         ACCOUNT_RETURN_VAL((error_code != NULL), {_ERR("error_code pointer is NULL"); }, NULL, (""));
2825         ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; _ERR("The database isn't connected."); }, NULL, ("The database isn't connected."));
2826
2827         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2828
2829         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
2830         _INFO("account query=[%s]", query);
2831
2832         hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
2833
2834         if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
2835                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2836                 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
2837                 return NULL;
2838         }
2839
2840         _INFO("before _account_query_bind_text");
2841         _account_query_bind_text(hstmt, binding_count++, app_id);
2842
2843         rc = _account_query_step(hstmt);
2844
2845         ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, { *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND; _ERR("The record isn't found from global db. rc=[%d]", rc); }, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2846
2847         provider_feature_s* feature_record = NULL;
2848
2849         while (rc == SQLITE_ROW) {
2850
2851                 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
2852
2853                 if (feature_record == NULL) {
2854                         ACCOUNT_FATAL("malloc Failed");
2855                         break;
2856                 }
2857
2858                 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
2859
2860                 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
2861
2862                 _INFO("Adding account feature_list");
2863                 feature_list = g_slist_append(feature_list, feature_record);
2864
2865                 rc = _account_query_step(hstmt);
2866         }
2867
2868         *error_code = _ACCOUNT_ERROR_NONE;
2869
2870 CATCH:
2871         if (hstmt != NULL) {
2872                 rc = _account_query_finalize(hstmt);
2873                 if (rc != _ACCOUNT_ERROR_NONE) {
2874                         *error_code = rc;
2875                         _ERR("global db fianlize error");
2876                 }
2877         }
2878
2879         if (*error_code != _ACCOUNT_ERROR_NONE)
2880                 _account_type_gslist_feature_free(feature_list);
2881
2882         _INFO("Returning account feature_list from global db");
2883
2884         return feature_list;
2885 }
2886
2887 GSList* _account_type_query_provider_feature_by_app_id(const char* app_id, int *error_code)
2888 {
2889         _INFO("_account_type_query_provider_feature_by_app_id app_id=%s", app_id);
2890         account_stmt hstmt = NULL;
2891         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2892         int rc = 0, binding_count = 1;
2893         GSList* feature_list = NULL;
2894
2895         ACCOUNT_RETURN_VAL((app_id != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("APP ID IS NULL"));
2896         ACCOUNT_RETURN_VAL((error_code != NULL), {_ERR("error_code pointer is NULL"); }, NULL, (""));
2897         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), { *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED; }, NULL, ("The database isn't connected."));
2898
2899         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2900
2901         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
2902         _INFO("account query=[%s]", query);
2903
2904         hstmt = _account_prepare_query(g_hAccountDB, query);
2905
2906         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2907                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2908                 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
2909                 return NULL;
2910         }
2911
2912         _account_query_bind_text(hstmt, binding_count++, app_id);
2913
2914         rc = _account_query_step(hstmt);
2915
2916         ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, { *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
2917                         _ERR("The record isn't found from user db. rc=[%d]", rc); },
2918                                 _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2919
2920         provider_feature_s* feature_record = NULL;
2921
2922         while (rc == SQLITE_ROW) {
2923
2924                 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
2925
2926                 if (feature_record == NULL) {
2927                         ACCOUNT_FATAL("malloc Failed");
2928                         break;
2929                 }
2930
2931                 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
2932
2933                 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
2934
2935                 _INFO("Adding account feature_list");
2936                 feature_list = g_slist_append(feature_list, feature_record);
2937
2938                 rc = _account_query_step(hstmt);
2939         }
2940
2941         *error_code = _ACCOUNT_ERROR_NONE;
2942
2943         rc = _account_query_finalize(hstmt);
2944         ACCOUNT_CATCH_ERROR_P((rc == _ACCOUNT_ERROR_NONE), { *error_code = rc; }, rc, ("account finalize error"));
2945         hstmt = NULL;
2946
2947 CATCH:
2948         if (hstmt != NULL) {
2949                 rc = _account_query_finalize(hstmt);
2950                 if (rc != _ACCOUNT_ERROR_NONE) {
2951                         *error_code = rc;
2952                         _ERR("account fianlize error");
2953                 }
2954                 hstmt = NULL;
2955         }
2956         _INFO("*error_code=[%d]", *error_code);
2957
2958         if (*error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
2959                 feature_list = _account_type_query_provider_feature_by_app_id_from_global_db(app_id, error_code);
2960
2961         if (*error_code != _ACCOUNT_ERROR_NONE) {
2962                 _account_type_gslist_feature_free(feature_list);
2963                 return NULL;
2964         }
2965
2966         _INFO("Returning account feature_list");
2967
2968         return feature_list;
2969 }
2970
2971 int _account_type_query_provider_feature_cb_by_app_id_from_global_db(account_type_provider_feature_cb callback, const char* app_id, void *user_data)
2972 {
2973         int                             error_code = _ACCOUNT_ERROR_NONE;
2974         account_stmt    hstmt = NULL;
2975         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2976         int                             rc = 0, binding_count = 1;
2977
2978         _INFO("_account_type_query_provider_feature_cb_by_app_id_in_global_db start app_id=%s", app_id);
2979         ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
2980         ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
2981         ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2982
2983         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2984
2985         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
2986         hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
2987
2988         if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
2989                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
2990                 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_PERMISSION_DENIED, ("global db permission denied.\n"));
2991         }
2992
2993         _account_query_bind_text(hstmt, binding_count++, app_id);
2994
2995         rc = _account_query_step(hstmt);
2996         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {_ERR("The record isn't found. rc=[%d]", rc); }, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2997
2998         provider_feature_s* feature_record = NULL;
2999
3000         while (rc == SQLITE_ROW) {
3001                 bool cb_ret = FALSE;
3002                 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
3003
3004                 if (feature_record == NULL) {
3005                         ACCOUNT_FATAL("malloc Failed");
3006                         break;
3007                 }
3008
3009                 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
3010
3011                 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
3012
3013                 cb_ret = callback(feature_record->app_id, feature_record->key, user_data);
3014
3015                 _account_type_free_feature_with_items(feature_record);
3016
3017                 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returns FALSE, its iteration is stopped!!!!\n"));
3018
3019                 rc = _account_query_step(hstmt);
3020         }
3021
3022         error_code = _ACCOUNT_ERROR_NONE;
3023
3024 CATCH:
3025         if (hstmt != NULL) {
3026                 rc = _account_query_finalize(hstmt);
3027                 if (rc != _ACCOUNT_ERROR_NONE) {
3028                         error_code = rc;
3029                         _ERR("global db finalize error[%d]", rc);
3030                 }
3031                 hstmt = NULL;
3032         }
3033
3034         _INFO("_account_type_query_provider_feature_cb_by_app_id_in_global_db end. error_code=[%d]", error_code);
3035
3036         return error_code;
3037 }
3038
3039 int _account_type_query_provider_feature_cb_by_app_id(account_type_provider_feature_cb callback, const char* app_id, void *user_data)
3040 {
3041         int                             error_code = _ACCOUNT_ERROR_NONE;
3042         account_stmt    hstmt = NULL;
3043         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3044         int                             rc = 0, binding_count = 1;
3045
3046         _INFO("_account_type_query_provider_feature_cb_by_app_id start app_id=%s", app_id);
3047         ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3048         ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3049         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3050
3051         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3052
3053         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
3054         hstmt = _account_prepare_query(g_hAccountDB, query);
3055
3056         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
3057                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3058                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3059         }
3060
3061         _account_query_bind_text(hstmt, binding_count++, app_id);
3062
3063         rc = _account_query_step(hstmt);
3064         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found in user db.\n"));
3065
3066         provider_feature_s* feature_record = NULL;
3067
3068         while (rc == SQLITE_ROW) {
3069                 bool cb_ret = FALSE;
3070                 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
3071
3072                 if (feature_record == NULL) {
3073                         ACCOUNT_FATAL("malloc Failed");
3074                         break;
3075                 }
3076
3077                 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
3078
3079                 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
3080
3081                 cb_ret = callback(feature_record->app_id, feature_record->key, user_data);
3082
3083                 _account_type_free_feature_with_items(feature_record);
3084
3085                 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
3086
3087                 rc = _account_query_step(hstmt);
3088         }
3089
3090         rc = _account_query_finalize(hstmt);
3091         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3092         hstmt = NULL;
3093
3094         error_code = _ACCOUNT_ERROR_NONE;
3095
3096 CATCH:
3097         if (hstmt != NULL) {
3098                 rc = _account_query_finalize(hstmt);
3099                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3100                 hstmt = NULL;
3101         }
3102 /*
3103         if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3104                 error_code = _account_type_query_provider_feature_cb_by_app_id_from_global_db(callback, app_id, user_data);
3105 */
3106         _INFO("_account_type_query_provider_feature_cb_by_app_id end");
3107
3108         return error_code;
3109 }
3110
3111 int account_type_query_provider_feature_cb_by_app_id(account_type_provider_feature_cb callback, const char* app_id, void *user_data)
3112 {
3113         int                             error_code = _ACCOUNT_ERROR_NONE;
3114
3115         ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3116         ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3117         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3118
3119         error_code = _account_type_query_provider_feature_cb_by_app_id(callback, app_id, user_data);
3120
3121         if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3122                 error_code = _account_type_query_provider_feature_cb_by_app_id_from_global_db(callback, app_id, user_data);
3123
3124         return error_code;
3125 }
3126
3127 bool _account_type_query_supported_feature_from_global_db(const char* app_id, const char* capability, int *error_code)
3128 {
3129         _INFO("_account_type_query_supported_feature_in_global_db start");
3130         ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3131
3132         *error_code = _ACCOUNT_ERROR_NONE;
3133
3134         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3135         int record_count = 0;
3136
3137         if (app_id == NULL || capability == NULL) {
3138                 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
3139                 return false;
3140         }
3141
3142         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
3143
3144         record_count = _account_get_record_count(g_hAccountGlobalDB, query);
3145
3146         if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3147                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3148                 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3149         }
3150
3151         if (record_count <= 0) {
3152                 *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
3153                 return false;
3154         }
3155
3156         _INFO("_account_type_query_supported_feature_in_global_db end");
3157
3158         return true;
3159 }
3160
3161 bool _account_type_query_supported_feature(const char* app_id, const char* capability, int *error_code)
3162 {
3163         _INFO("_account_type_query_supported_feature start");
3164
3165         *error_code = _ACCOUNT_ERROR_NONE;
3166
3167         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3168         int                             record_count = 0;
3169
3170         if (app_id == NULL || capability == NULL) {
3171                 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
3172                 return false;
3173         }
3174
3175         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
3176
3177         record_count = _account_get_record_count(g_hAccountDB, query);
3178
3179         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
3180                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3181                 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3182                 return false;
3183         }
3184
3185         if (record_count <= 0) {
3186                 bool is_exist = false;
3187                 is_exist = _account_type_query_supported_feature_from_global_db(app_id, capability, error_code);
3188                 if (!is_exist)
3189                         return false;
3190         }
3191
3192         _INFO("_account_type_query_supported_feature end");
3193
3194         return true;
3195 }
3196
3197
3198 static int _account_type_update_provider_feature(sqlite3 *account_db_handle, account_type_s *account_type, const char* app_id)
3199 {
3200         int                             rc, count = 1;
3201         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3202         account_stmt    hstmt = NULL;
3203
3204         ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
3205
3206         if (g_slist_length(account_type->provider_feature_list) == 0) {
3207                 ACCOUNT_ERROR("no feature\n");
3208                 return _ACCOUNT_ERROR_NONE;
3209         }
3210
3211         ACCOUNT_DEBUG("app id", app_id);
3212
3213         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3214
3215         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id=? ", PROVIDER_FEATURE_TABLE);
3216         hstmt = _account_prepare_query(account_db_handle, query);
3217
3218         if (_account_db_err_code(account_db_handle) == SQLITE_PERM) {
3219                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(account_db_handle));
3220                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3221         }
3222
3223         count = 1;
3224         _account_query_bind_text(hstmt, count++, app_id);
3225         rc = _account_query_step(hstmt);
3226
3227         if (rc != SQLITE_DONE) {
3228                 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3229                 if (_account_query_finalize(hstmt) != _ACCOUNT_ERROR_NONE)
3230                         _ERR("finalize error - account_query_step() failed");
3231                 return _ACCOUNT_ERROR_DB_FAILED;
3232         }
3233         rc = _account_query_finalize(hstmt);
3234         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3235         hstmt = NULL;
3236
3237         GSList *iter;
3238
3239         for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
3240                 int ret;
3241                 count = 1;
3242                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3243                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
3244                                 "(?, ?) ", PROVIDER_FEATURE_TABLE);
3245
3246                 hstmt = _account_prepare_query(account_db_handle, query);
3247
3248                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg(account_db_handle)));
3249
3250                 provider_feature_s* feature_data = NULL;
3251                 feature_data = (provider_feature_s*)iter->data;
3252
3253                 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
3254                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3255                 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
3256                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3257
3258                 rc = _account_query_step(hstmt);
3259
3260                 if (rc != SQLITE_DONE) {
3261                         ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3262                         break;
3263                 }
3264                 rc = _account_query_finalize(hstmt);
3265                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3266                 hstmt = NULL;
3267         }
3268
3269         return _ACCOUNT_ERROR_NONE;
3270 }
3271
3272 static int _account_type_update_label(sqlite3 *account_db_handle, account_type_s *account_type, const char* app_id)
3273 {
3274         int                             rc, count = 1;
3275         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3276         account_stmt    hstmt = NULL;
3277
3278         ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
3279
3280         if (g_slist_length(account_type->label_list) == 0)
3281                 return _ACCOUNT_ERROR_NONE;
3282
3283         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3284
3285         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId=? ", LABEL_TABLE);
3286         hstmt = _account_prepare_query(account_db_handle, query);
3287
3288         if (_account_db_err_code(account_db_handle) == SQLITE_PERM) {
3289                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(account_db_handle));
3290                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3291         }
3292
3293         count = 1;
3294         _account_query_bind_text(hstmt, count++, app_id);
3295         rc = _account_query_step(hstmt);
3296
3297         if (rc != SQLITE_DONE) {
3298                 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3299                 if (_account_query_finalize(hstmt) != _ACCOUNT_ERROR_NONE)
3300                         _ERR("finalize error - account_query_step() failed");
3301                 return _ACCOUNT_ERROR_DB_FAILED;
3302         }
3303         rc = _account_query_finalize(hstmt);
3304         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3305         hstmt = NULL;
3306
3307         GSList *iter;
3308
3309         for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
3310                 int ret;
3311                 count = 1;
3312                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3313                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
3314                                 "(?, ?, ?) ", LABEL_TABLE);
3315
3316                 hstmt = _account_prepare_query(account_db_handle, query);
3317
3318                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg(account_db_handle)));
3319
3320                 label_s* label_data = NULL;
3321                 label_data = (label_s*)iter->data;
3322
3323                 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
3324                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3325                 ret = _account_query_bind_text(hstmt, count++, label_data->label);
3326                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3327                 ret = _account_query_bind_text(hstmt, count++, label_data->locale);
3328                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3329
3330                 rc = _account_query_step(hstmt);
3331
3332                 if (rc != SQLITE_DONE) {
3333                         ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3334                         break;
3335                 }
3336                 rc = _account_query_finalize(hstmt);
3337                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3338                 hstmt = NULL;
3339         }
3340
3341         return _ACCOUNT_ERROR_NONE;
3342 }
3343
3344
3345 static int _account_type_update_account(sqlite3 *account_db_handle, account_type_s *account_type, const char* app_id)
3346 {
3347         int                             rc = 0, binding_count = 1;
3348         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3349         int                             error_code = _ACCOUNT_ERROR_NONE;
3350         account_stmt    hstmt = NULL;
3351
3352         if (!account_type->app_id) {
3353                 ACCOUNT_ERROR("app id is mandetory field, it can not be NULL!!!!\n");
3354                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
3355         }
3356
3357         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3358         ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET AppId=?, ServiceProviderId=?, IconPath=?, "
3359                         "SmallIconPath=?, MultipleAccountSupport=? WHERE AppId=? ", ACCOUNT_TYPE_TABLE);
3360
3361         hstmt = _account_prepare_query(account_db_handle, query);
3362
3363         if (_account_db_err_code(account_db_handle) == SQLITE_PERM) {
3364                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(account_db_handle));
3365                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3366         } else if (_account_db_err_code(account_db_handle) == SQLITE_BUSY) {
3367                 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(account_db_handle));
3368                 return _ACCOUNT_ERROR_DATABASE_BUSY;
3369         }
3370
3371         ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg(account_db_handle)));
3372
3373         binding_count = _account_type_convert_account_to_sql(account_type, hstmt, query);
3374         _account_query_bind_text(hstmt, binding_count++, app_id);
3375
3376         rc = _account_query_step(hstmt);
3377         if (rc != SQLITE_DONE)
3378                 ACCOUNT_ERROR("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3379
3380         rc = _account_query_finalize(hstmt);
3381         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3382         hstmt = NULL;
3383
3384         /*update label*/
3385         error_code = _account_type_update_label(account_db_handle, account_type, app_id);
3386         /* update provider feature */
3387         error_code = _account_type_update_provider_feature(account_db_handle, account_type, app_id);
3388
3389         return error_code;
3390 }
3391
3392 int _account_type_update_to_db_by_app_id(account_type_s* account_type, const char* app_id)
3393 {
3394         int     error_code = _ACCOUNT_ERROR_NONE;
3395
3396         ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
3397         ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3398         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3399
3400         account_type_s* data = account_type;
3401
3402         pthread_mutex_lock(&account_mutex);
3403
3404         error_code = _account_type_update_account(g_hAccountDB, data, app_id);
3405
3406         pthread_mutex_unlock(&account_mutex);
3407
3408         return error_code;
3409 }
3410
3411 GSList* _account_type_get_label_list_by_app_id_from_global_db(const char* app_id, int *error_code)
3412 {
3413         *error_code = _ACCOUNT_ERROR_NONE;
3414         account_stmt    hstmt = NULL;
3415         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3416         int                             rc = 0, binding_count = 1;
3417         GSList* label_list = NULL;
3418
3419         ACCOUNT_RETURN_VAL((app_id != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("APP ID IS NULL"));
3420         ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3421
3422         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3423
3424         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3425         hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3426
3427         if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3428                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3429                 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3430
3431                 goto CATCH;
3432         }
3433
3434         _account_query_bind_text(hstmt, binding_count++, app_id);
3435
3436         rc = _account_query_step(hstmt);
3437         ACCOUNT_CATCH_ERROR_P((rc == SQLITE_ROW), {_ERR("The record isn't found. rc=[%d] done", rc); }, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3438
3439         label_s* label_record = NULL;
3440
3441         while (rc == SQLITE_ROW) {
3442                 label_record = (label_s*) malloc(sizeof(label_s));
3443
3444                 if (label_record == NULL) {
3445                         ACCOUNT_FATAL("malloc Failed");
3446                         break;
3447                 }
3448
3449                 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3450
3451                 _account_type_convert_column_to_label(hstmt, label_record);
3452
3453                 _INFO("Adding account label_list");
3454                 label_list = g_slist_append(label_list, label_record);
3455
3456                 rc = _account_query_step(hstmt);
3457         }
3458
3459         *error_code = _ACCOUNT_ERROR_NONE;
3460
3461 CATCH:
3462         if (hstmt != NULL) {
3463                 rc = _account_query_finalize(hstmt);
3464                 if (rc != _ACCOUNT_ERROR_NONE)
3465                         _ERR("global db finalize error[%d]", rc);
3466                 hstmt = NULL;
3467         }
3468
3469         _INFO("Returning account global label_list");
3470
3471         return label_list;
3472 }
3473
3474 GSList* _account_type_get_label_list_by_app_id(const char* app_id, int *error_code)
3475 {
3476         *error_code = _ACCOUNT_ERROR_NONE;
3477         account_stmt    hstmt = NULL;
3478         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3479         int                             rc = 0, binding_count = 1;
3480         GSList* label_list = NULL;
3481
3482         ACCOUNT_RETURN_VAL((app_id != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("APP ID IS NULL"));
3483         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), { *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED; }, NULL, ("The database isn't connected."));
3484
3485         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3486
3487         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3488         hstmt = _account_prepare_query(g_hAccountDB, query);
3489
3490         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
3491                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3492                 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3493                 return NULL;
3494         }
3495
3496         _account_query_bind_text(hstmt, binding_count++, app_id);
3497
3498         rc = _account_query_step(hstmt);
3499         ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3500
3501         label_s* label_record = NULL;
3502
3503         while (rc == SQLITE_ROW) {
3504                 label_record = (label_s*) malloc(sizeof(label_s));
3505
3506                 if (label_record == NULL) {
3507                         ACCOUNT_FATAL("malloc Failed");
3508                         break;
3509                 }
3510
3511                 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3512
3513                 _account_type_convert_column_to_label(hstmt, label_record);
3514
3515                 _INFO("Adding account label_list");
3516                 label_list = g_slist_append(label_list, label_record);
3517
3518                 rc = _account_query_step(hstmt);
3519         }
3520
3521         rc = _account_query_finalize(hstmt);
3522         if (rc != _ACCOUNT_ERROR_NONE) {
3523                         _account_type_gslist_label_free(label_list);
3524                         *error_code = rc;
3525                         _ERR("finalize error");
3526                         return NULL;
3527                 }
3528         hstmt = NULL;
3529
3530         *error_code = _ACCOUNT_ERROR_NONE;
3531
3532 CATCH:
3533         if (hstmt != NULL) {
3534                 rc = _account_query_finalize(hstmt);
3535                 if (rc != _ACCOUNT_ERROR_NONE) {
3536                         _account_type_gslist_label_free(label_list);
3537                         *error_code = rc;
3538                         _ERR("finalize error");
3539                         return NULL;
3540                 }
3541                 hstmt = NULL;
3542         }
3543
3544         if (*error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3545                 label_list = _account_type_get_label_list_by_app_id_from_global_db(app_id, error_code);
3546
3547         _INFO("Returning account label_list");
3548
3549         return label_list;
3550 }
3551
3552 int _account_type_query_label_by_app_id_from_global_db(account_type_label_cb callback, const char* app_id, void *user_data)
3553 {
3554         int                             error_code = _ACCOUNT_ERROR_NONE;
3555         account_stmt    hstmt = NULL;
3556         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3557         int                             rc = 0, binding_count = 1;
3558
3559         _INFO("account_type_query_label_by_app_id_from_global_db start");
3560
3561         ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3562         ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3563         ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3564
3565         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3566
3567         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3568         hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3569
3570         if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3571                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3572                 error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3573                 goto CATCH;
3574         }
3575
3576         _account_query_bind_text(hstmt, binding_count++, app_id);
3577
3578         rc = _account_query_step(hstmt);
3579         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3580
3581         label_s* label_record = NULL;
3582
3583         while (rc == SQLITE_ROW) {
3584                 bool cb_ret = FALSE;
3585                 label_record = (label_s*) malloc(sizeof(label_s));
3586
3587                 if (label_record == NULL) {
3588                         ACCOUNT_FATAL("malloc Failed");
3589                         break;
3590                 }
3591
3592                 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3593
3594                 _account_type_convert_column_to_label(hstmt, label_record);
3595
3596                 cb_ret = callback(label_record->app_id, label_record->label , label_record->locale, user_data);
3597
3598                 _account_type_free_label_with_items(label_record);
3599
3600                 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
3601
3602                 rc = _account_query_step(hstmt);
3603         }
3604
3605         error_code = _ACCOUNT_ERROR_NONE;
3606
3607 CATCH:
3608         if (hstmt != NULL) {
3609                 rc = _account_query_finalize(hstmt);
3610                 if (rc != _ACCOUNT_ERROR_NONE)
3611                         _ERR("global db finalize error[%d]", rc);
3612                 hstmt = NULL;
3613         }
3614
3615         _INFO("account_type_query_label_by_app_id_from_global_db end [%d]", error_code);
3616
3617         return error_code;
3618 }
3619
3620 int _account_type_query_label_by_app_id(account_type_label_cb callback, const char* app_id, void *user_data)
3621 {
3622         int                             error_code = _ACCOUNT_ERROR_NONE;
3623         account_stmt    hstmt = NULL;
3624         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3625         int                             rc = 0, binding_count = 1;
3626
3627         ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3628         ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3629         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3630
3631         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3632
3633         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3634         hstmt = _account_prepare_query(g_hAccountDB, query);
3635
3636         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
3637                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3638                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3639         }
3640
3641         _account_query_bind_text(hstmt, binding_count++, app_id);
3642
3643         rc = _account_query_step(hstmt);
3644         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3645
3646         label_s* label_record = NULL;
3647
3648         while (rc == SQLITE_ROW) {
3649                 bool cb_ret = FALSE;
3650                 label_record = (label_s*) malloc(sizeof(label_s));
3651
3652                 if (label_record == NULL) {
3653                         ACCOUNT_FATAL("malloc Failed");
3654                         break;
3655                 }
3656
3657                 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3658
3659                 _account_type_convert_column_to_label(hstmt, label_record);
3660
3661                 cb_ret = callback(label_record->app_id, label_record->label , label_record->locale, user_data);
3662
3663                 _account_type_free_label_with_items(label_record);
3664
3665                 //ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
3666                 if (cb_ret != TRUE) {
3667                         _INFO("Callback func returs FALSE, its iteration is stopped!!!!\n");
3668                         break;
3669                 }
3670
3671                 rc = _account_query_step(hstmt);
3672         }
3673
3674         rc = _account_query_finalize(hstmt);
3675         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3676         hstmt = NULL;
3677
3678         error_code = _ACCOUNT_ERROR_NONE;
3679
3680 CATCH:
3681         if (hstmt != NULL) {
3682                 rc = _account_query_finalize(hstmt);
3683                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3684                 hstmt = NULL;
3685         }
3686 /*
3687         if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
3688                 error_code = account_type_query_label_by_app_id_from_global_db(callback, app_id, user_data);
3689         }
3690 */
3691         return error_code;
3692 }
3693
3694 int account_type_query_label_by_app_id(account_type_label_cb callback, const char* app_id, void *user_data)
3695 {
3696         int error_code = _ACCOUNT_ERROR_NONE;
3697
3698         ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3699         ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3700         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3701
3702         error_code = _account_type_query_label_by_app_id(callback, app_id, user_data);
3703
3704         if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3705                 error_code = _account_type_query_label_by_app_id_from_global_db(callback, app_id, user_data);
3706
3707         return error_code;
3708 }
3709
3710 bool _account_get_label_text_cb(char* app_id, char* label, char* locale, void *user_data)
3711 {
3712         account_type_s *data = (account_type_s*)user_data;
3713
3714         label_s *label_data = (label_s*)malloc(sizeof(label_s));
3715
3716         if (label_data == NULL) {
3717                 ACCOUNT_FATAL("_account_get_label_text_cb : MALLOC FAIL\n");
3718                 return FALSE;
3719         }
3720         ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
3721
3722         label_data->app_id = _account_dup_text(app_id);
3723         label_data->label = _account_dup_text(label);
3724         label_data->locale = _account_dup_text(locale);
3725
3726         data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
3727
3728         return TRUE;
3729 }
3730
3731 bool _account_get_provider_feature_cb(char* app_id, char* key, void* user_data)
3732 {
3733         account_type_s *data = (account_type_s*)user_data;
3734
3735         provider_feature_s *feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
3736
3737         if (feature_data == NULL) {
3738                 ACCOUNT_FATAL("_account_get_provider_feature_cb : MALLOC FAIL\n");
3739                 return FALSE;
3740         }
3741         ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
3742
3743         feature_data->app_id = _account_dup_text(app_id);
3744         feature_data->key = _account_dup_text(key);
3745
3746         data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
3747
3748         return TRUE;
3749 }
3750
3751 int _account_type_query_by_app_id_from_global_db(const char* app_id, account_type_s** account_type_record)
3752 {
3753         _INFO("_account_type_query_by_app_id_from_global_db start");
3754
3755         int                             error_code = _ACCOUNT_ERROR_NONE;
3756         account_stmt    hstmt = NULL;
3757         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3758         int                             rc = 0, binding_count = 1;
3759
3760         ACCOUNT_RETURN_VAL((app_id != 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3761         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3762         ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3763
3764         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3765
3766         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
3767         hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3768
3769         if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3770                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3771                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3772         }
3773
3774         _account_query_bind_text(hstmt, binding_count++, app_id);
3775
3776         rc = _account_query_step(hstmt);
3777         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3778
3779         *account_type_record = create_empty_account_type_instance();
3780
3781         while (rc == SQLITE_ROW) {
3782                 _account_type_convert_column_to_account_type(hstmt, *account_type_record);
3783                 rc = _account_query_step(hstmt);
3784         }
3785
3786         rc = _account_query_finalize(hstmt);
3787         ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {_ERR("global db finalize error rc=[%d]", rc); }, rc, ("finalize error"));
3788         _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb, app_id, (void*)(*account_type_record));
3789         _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, app_id, (void*)(*account_type_record));
3790
3791         hstmt = NULL;
3792         error_code = _ACCOUNT_ERROR_NONE;
3793
3794 CATCH:
3795         if (hstmt != NULL) {
3796                 rc = _account_query_finalize(hstmt);
3797                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3798                 hstmt = NULL;
3799         }
3800
3801         _INFO("_account_type_query_by_app_id_from_global_db end [%d]", error_code);
3802
3803         return error_code;
3804 }
3805
3806 int _account_type_query_by_app_id(const char* app_id, account_type_s** account_type_record)
3807 {
3808         _INFO("_account_type_query_by_app_id start");
3809
3810         int                             error_code = _ACCOUNT_ERROR_NONE;
3811         account_stmt    hstmt = NULL;
3812         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3813         int                             rc = 0, binding_count = 1;
3814
3815         ACCOUNT_RETURN_VAL((app_id != 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3816         ACCOUNT_RETURN_VAL((account_type_record != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("account type record(account_type_s**) is NULL"));
3817         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3818
3819         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3820
3821         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
3822         hstmt = _account_prepare_query(g_hAccountDB, query);
3823
3824         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
3825                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3826                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3827         }
3828
3829         _account_query_bind_text(hstmt, binding_count++, app_id);
3830
3831         rc = _account_query_step(hstmt);
3832         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3833
3834         *account_type_record = create_empty_account_type_instance();
3835         if (*account_type_record == NULL) {
3836                 _ERR("Out of Memory");
3837                 error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
3838                 goto CATCH;
3839         }
3840
3841         while (rc == SQLITE_ROW) {
3842                 _account_type_convert_column_to_account_type(hstmt, *account_type_record);
3843                 rc = _account_query_step(hstmt);
3844         }
3845
3846         rc = _account_query_finalize(hstmt);
3847         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3848         _account_type_query_label_by_app_id(_account_get_label_text_cb, app_id, (void*)(*account_type_record));
3849         _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, app_id, (void*)(*account_type_record));
3850
3851         hstmt = NULL;
3852         error_code = _ACCOUNT_ERROR_NONE;
3853
3854 CATCH:
3855         if (hstmt != NULL) {
3856                 rc = _account_query_finalize(hstmt);
3857                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3858                 hstmt = NULL;
3859         }
3860
3861         if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3862                 error_code = _account_type_query_by_app_id_from_global_db(app_id, account_type_record);
3863
3864         _INFO("_account_type_query_by_app_id end [%d]", error_code);
3865
3866         return error_code;
3867 }
3868
3869 int _account_type_query_by_provider_feature_from_global_db(const char* key, GSList **account_type_list_all)
3870 {
3871         int error_code = _ACCOUNT_ERROR_NONE;
3872         account_stmt    hstmt = NULL;
3873         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3874         int                             rc = 0;
3875         GSList                  *account_type_list = NULL;
3876
3877         _INFO("_account_type_query_by_provider_feature_from_global_db start key=%s", key);
3878         if (key == NULL) {
3879                 ACCOUNT_ERROR("capability_type IS NULL.");
3880                 error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
3881                 goto CATCH;
3882         }
3883
3884         if (g_hAccountGlobalDB == NULL) {
3885                 ACCOUNT_ERROR("The database isn't connected.");
3886                 error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;
3887                 goto CATCH;
3888         }
3889
3890         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3891
3892         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
3893
3894         hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3895
3896         if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3897                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3898                 error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3899                 goto CATCH;
3900         }
3901
3902         int binding_count = 1;
3903         _account_query_bind_text(hstmt, binding_count++, key);
3904
3905         rc = _account_query_step(hstmt);
3906
3907         account_type_s *account_type_record = NULL;
3908
3909         if (rc != SQLITE_ROW) {
3910                 ACCOUNT_ERROR("The record isn't found. rc=[%d]", rc);
3911                 error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
3912                 goto CATCH;
3913         }
3914
3915         while (rc == SQLITE_ROW) {
3916                 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
3917
3918                 if (account_type_record == NULL) {
3919                         ACCOUNT_FATAL("malloc Failed");
3920                         break;
3921                 }
3922
3923                 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
3924                 _account_type_convert_column_to_account_type(hstmt, account_type_record);
3925                 account_type_list = g_slist_append(account_type_list, account_type_record);
3926                 rc = _account_query_step(hstmt);
3927         }
3928
3929         rc = _account_query_finalize(hstmt);
3930         if (rc != _ACCOUNT_ERROR_NONE) {
3931                 _account_type_gslist_account_type_free(account_type_list);
3932                 ACCOUNT_ERROR("finalize error(%s)", rc);
3933                 error_code = rc;
3934                 goto CATCH;
3935         }
3936         hstmt = NULL;
3937
3938         GSList* iter;
3939
3940         for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
3941                 account_type_s *account_type = NULL;
3942                 account_type = (account_type_s*)iter->data;
3943                 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb, account_type->app_id, (void*)account_type);
3944                 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, account_type->app_id, (void*)account_type);
3945                 _INFO("add label & provider_feature");
3946         }
3947
3948         for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
3949
3950                 account_type_s *account_type = NULL;
3951                 account_type = (account_type_s*)iter->data;
3952                 *account_type_list_all = g_slist_append(*account_type_list_all, account_type);
3953                 _INFO("add account_type");
3954         }
3955         g_slist_free(account_type_list);
3956
3957         error_code = _ACCOUNT_ERROR_NONE;
3958
3959 CATCH:
3960         if (hstmt != NULL) {
3961                 rc = _account_query_finalize(hstmt);
3962                 if (rc != _ACCOUNT_ERROR_NONE) {
3963                         ACCOUNT_ERROR("finalize error(%s)", rc);
3964                         return rc;
3965                 }
3966                 hstmt = NULL;
3967         }
3968
3969         _INFO("_account_type_query_by_provider_feature_from_global_db end. error_code=[%d]", error_code);
3970
3971         return error_code;
3972 }
3973
3974 GSList* _account_type_query_by_provider_feature(const char* key, int *error_code)
3975 {
3976         *error_code = _ACCOUNT_ERROR_NONE;
3977         account_stmt hstmt = NULL;
3978         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3979         int rc = 0;
3980         GSList *account_type_list = NULL;
3981
3982         _INFO("account_type_query_by_provider_feature start key=%s", key);
3983         if (key == NULL) {
3984                 ACCOUNT_ERROR("capability_type IS NULL.");
3985                 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
3986                 goto CATCH;
3987         }
3988
3989         if (g_hAccountDB == NULL) {
3990                 ACCOUNT_ERROR("The database isn't connected.");
3991                 *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;
3992                 goto CATCH;
3993         }
3994
3995         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3996
3997         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
3998
3999         hstmt = _account_prepare_query(g_hAccountDB, query);
4000
4001         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4002                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4003                 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
4004                 goto CATCH;
4005         }
4006
4007         int binding_count = 1;
4008         _account_query_bind_text(hstmt, binding_count++, key);
4009
4010         rc = _account_query_step(hstmt);
4011
4012         account_type_s *account_type_record = NULL;
4013
4014         if (rc != SQLITE_ROW) {
4015                 ACCOUNT_ERROR("The record isn't found. rc=[%d]", rc);
4016                 *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4017                 goto CATCH;
4018         }
4019
4020         while (rc == SQLITE_ROW) {
4021                 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4022
4023                 if (account_type_record == NULL) {
4024                         ACCOUNT_FATAL("malloc Failed");
4025                         break;
4026                 }
4027
4028                 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4029                 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4030                 account_type_list = g_slist_append(account_type_list, account_type_record);
4031                 rc = _account_query_step(hstmt);
4032         }
4033
4034         rc = _account_query_finalize(hstmt);
4035         if (rc != _ACCOUNT_ERROR_NONE) {
4036                 _account_type_gslist_account_type_free(account_type_list);
4037                 ACCOUNT_ERROR("finalize error(%s)", rc);
4038                 *error_code = rc;
4039                 goto CATCH;
4040         }
4041         hstmt = NULL;
4042
4043         GSList* iter;
4044
4045         for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4046                 account_type_s *account_type = NULL;
4047                 account_type = (account_type_s*)iter->data;
4048                 _account_type_query_label_by_app_id(_account_get_label_text_cb, account_type->app_id, (void*)account_type);
4049                 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, account_type->app_id, (void*)account_type);
4050         }
4051
4052         *error_code = _ACCOUNT_ERROR_NONE;
4053
4054 CATCH:
4055         if (hstmt != NULL) {
4056                 rc = _account_query_finalize(hstmt);
4057                 if (rc != _ACCOUNT_ERROR_NONE) {
4058                         *error_code = rc;
4059                         return NULL;
4060                 }
4061                 hstmt = NULL;
4062         }
4063
4064         if (*error_code == _ACCOUNT_ERROR_NONE || *error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4065                 rc = _account_type_query_by_provider_feature_from_global_db(key, &account_type_list);
4066                 if (rc != _ACCOUNT_ERROR_NONE && rc != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4067                         ACCOUNT_ERROR("_account_type_query_by_provider_feature_from_global_db fail=[%d]", rc);
4068                         _account_type_gslist_account_type_free(account_type_list);
4069                         return NULL;
4070                 }
4071                 if (rc == _ACCOUNT_ERROR_NONE)
4072                         *error_code = rc;
4073         }
4074
4075         _INFO("account_type_query_by_provider_feature end");
4076
4077         return account_type_list;
4078 }
4079
4080 int _account_type_query_all_from_global_db(GSList **account_type_list_all)
4081 {
4082         account_stmt    hstmt = NULL;
4083         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4084         int rc = _ACCOUNT_ERROR_NONE;
4085         int error_code = _ACCOUNT_ERROR_NONE;
4086         GSList *account_type_list = NULL;
4087
4088         _INFO("_account_type_query_all_in_global_db start");
4089         ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, NULL, ("The database isn't connected."));
4090
4091         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4092
4093         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
4094         hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
4095
4096         rc = _account_query_step(hstmt);
4097
4098         if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
4099                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
4100                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4101         }
4102
4103         account_type_s *account_type_record = NULL;
4104
4105         if (rc != SQLITE_ROW) {
4106                 _INFO("[_ACCOUNT_ERROR_RECORD_NOT_FOUND]The record isn't found.");
4107                 error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4108                 goto CATCH;
4109         }
4110
4111         while (rc == SQLITE_ROW) {
4112                 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4113
4114                 if (account_type_record == NULL) {
4115                         ACCOUNT_FATAL("malloc Failed");
4116                         break;
4117                 }
4118
4119                 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4120                 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4121                 account_type_list = g_slist_append(account_type_list, account_type_record);
4122                 rc = _account_query_step(hstmt);
4123         }
4124
4125         rc = _account_query_finalize(hstmt);
4126         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list); }, rc, ("finalize error"));
4127         hstmt = NULL;
4128
4129         GSList* iter;
4130
4131         for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4132                 account_type_s *account_type = NULL;
4133                 account_type = (account_type_s*)iter->data;
4134                 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb, account_type->app_id, (void*)account_type);
4135                 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, account_type->app_id, (void*)account_type);
4136         }
4137
4138         for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4139                 account_type_s *account_type = NULL;
4140                 account_type = (account_type_s*)iter->data;
4141                 *account_type_list_all = g_slist_append(*account_type_list_all, account_type);
4142         }
4143         g_slist_free(account_type_list);
4144
4145         error_code = _ACCOUNT_ERROR_NONE;
4146
4147 CATCH:
4148         if (hstmt != NULL) {
4149                 rc = _account_query_finalize(hstmt);
4150                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list); }, rc, ("finalize error"));
4151                 hstmt = NULL;
4152         }
4153
4154         _INFO("_account_type_query_all_in_global_db end");
4155
4156         return error_code;
4157 }
4158
4159 GSList* _account_type_query_all(void)
4160 {
4161         account_stmt hstmt = NULL;
4162         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4163         int rc = 0;
4164         int error_code = _ACCOUNT_ERROR_NONE;
4165         GSList *account_type_list = NULL;
4166
4167         _INFO("_account_type_query_all start");
4168         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected."));
4169
4170         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4171
4172         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
4173         hstmt = _account_prepare_query(g_hAccountDB, query);
4174
4175         rc = _account_query_step(hstmt);
4176
4177         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4178                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4179                 return NULL;
4180         }
4181
4182         account_type_s *account_type_record = NULL;
4183
4184         if (rc != SQLITE_ROW) {
4185                 _INFO("[_ACCOUNT_ERROR_RECORD_NOT_FOUND]The record isn't found.");
4186                 error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4187                 goto CATCH;
4188         }
4189
4190         while (rc == SQLITE_ROW) {
4191                 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4192
4193                 if (account_type_record == NULL) {
4194                         ACCOUNT_FATAL("malloc Failed");
4195                         break;
4196                 }
4197
4198                 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4199                 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4200                 account_type_list = g_slist_append(account_type_list, account_type_record);
4201                 rc = _account_query_step(hstmt);
4202         }
4203
4204         rc = _account_query_finalize(hstmt);
4205         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list); }, NULL, ("finalize error"));
4206         hstmt = NULL;
4207
4208         GSList* iter;
4209
4210         for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4211                 account_type_s *account_type = NULL;
4212                 account_type = (account_type_s*)iter->data;
4213                 _account_type_query_label_by_app_id(_account_get_label_text_cb, account_type->app_id, (void*)account_type);
4214                 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, account_type->app_id, (void*)account_type);
4215         }
4216
4217         error_code = _ACCOUNT_ERROR_NONE;
4218
4219 CATCH:
4220         if (hstmt != NULL) {
4221                 rc = _account_query_finalize(hstmt);
4222                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list); }, NULL, ("finalize error"));
4223                 hstmt = NULL;
4224         }
4225
4226         if (error_code == _ACCOUNT_ERROR_NONE || error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4227                 error_code = _account_type_query_all_from_global_db(&account_type_list);
4228                 if (rc != _ACCOUNT_ERROR_NONE && rc != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4229                         ACCOUNT_ERROR("_account_type_query_all_from_global_db fail=[%d]", rc);
4230                         _account_type_gslist_account_type_free(account_type_list);
4231                         return NULL;
4232                 }
4233         }
4234
4235         _INFO("_account_type_query_all end");
4236
4237         return account_type_list;
4238 }
4239
4240 // output parameter label must be free
4241 int _account_type_query_label_by_locale_from_global_db(const char* app_id, const char* locale, char **label)
4242 {
4243         int                             error_code = _ACCOUNT_ERROR_NONE;
4244         account_stmt    hstmt = NULL;
4245         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4246         int                             rc = 0, binding_count = 1;
4247         char*                   converted_locale = NULL;
4248
4249         ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
4250         ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4251         ACCOUNT_RETURN_VAL((label != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
4252         ACCOUNT_RETURN_VAL((locale != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("locale char is null"));
4253         //Making label newly created
4254
4255         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4256
4257         converted_locale = _account_dup_text(locale);
4258         gchar** tokens = g_strsplit(converted_locale, "-", 2);
4259
4260         if (tokens != NULL) {
4261                 if ((char*)(tokens[1]) != NULL) {
4262                         char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1]));
4263                         if (upper_token != NULL) {
4264                                 _ACCOUNT_FREE(converted_locale);
4265                                 converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token);
4266                         }
4267                         _ACCOUNT_FREE(upper_token);
4268                 }
4269         }
4270         g_strfreev(tokens);
4271
4272         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, converted_locale);
4273         _ACCOUNT_FREE(converted_locale);
4274
4275         hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
4276
4277         if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
4278                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
4279                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4280         }
4281
4282         _account_query_bind_text(hstmt, binding_count++, app_id);
4283
4284         rc = _account_query_step(hstmt);
4285         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4286
4287         label_s* label_record = NULL;
4288
4289         while (rc == SQLITE_ROW) {
4290                 label_record = (label_s*) malloc(sizeof(label_s));
4291
4292                 if (label_record == NULL) {
4293                         ACCOUNT_FATAL("malloc Failed");
4294                         break;
4295                 }
4296
4297                 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
4298
4299                 _account_type_convert_column_to_label(hstmt, label_record);
4300
4301                 _ACCOUNT_FREE(*label);
4302                 //Making label newly created
4303                 *label = _account_dup_text(label_record->label);
4304
4305                 _account_type_free_label_with_items(label_record);
4306
4307                 rc = _account_query_step(hstmt);
4308         }
4309
4310         rc = _account_query_finalize(hstmt);
4311         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4312         hstmt = NULL;
4313
4314         error_code = _ACCOUNT_ERROR_NONE;
4315
4316 CATCH:
4317         if (hstmt != NULL) {
4318                 rc = _account_query_finalize(hstmt);
4319                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4320                 hstmt = NULL;
4321         }
4322
4323         _INFO("_account_type_query_label_by_locale_from_global_db() end : error_code = %d", error_code);
4324
4325         return error_code;
4326 }
4327
4328 // output parameter label must be free
4329 int _account_type_query_label_by_locale(const char* app_id, const char* locale, char **label)
4330 {
4331         int                             error_code = _ACCOUNT_ERROR_NONE;
4332         account_stmt    hstmt = NULL;
4333         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4334         int                             rc = 0, binding_count = 1;
4335         char*                   converted_locale = NULL;
4336
4337         ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
4338         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4339         ACCOUNT_RETURN_VAL((label != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
4340         ACCOUNT_RETURN_VAL((locale != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("locale char is null"));
4341         //Making label newly created
4342
4343         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4344
4345         converted_locale = _account_dup_text(locale);
4346         gchar** tokens = g_strsplit(converted_locale, "-", 2);
4347
4348         if (tokens != NULL) {
4349                 if ((char*)(tokens[1]) != NULL) {
4350                         char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1]));
4351                         if (upper_token != NULL) {
4352                                 _ACCOUNT_FREE(converted_locale);
4353                                 converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token);
4354                         }
4355                         _ACCOUNT_FREE(upper_token);
4356                 }
4357         }
4358         g_strfreev(tokens);
4359
4360         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, converted_locale);
4361         _ACCOUNT_FREE(converted_locale);
4362
4363         hstmt = _account_prepare_query(g_hAccountDB, query);
4364
4365         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4366                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4367                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4368         }
4369
4370         _account_query_bind_text(hstmt, binding_count++, app_id);
4371
4372         rc = _account_query_step(hstmt);
4373         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4374
4375         label_s* label_record = NULL;
4376
4377         while (rc == SQLITE_ROW) {
4378                 label_record = (label_s*) malloc(sizeof(label_s));
4379
4380                 if (label_record == NULL) {
4381                         ACCOUNT_FATAL("malloc Failed");
4382                         break;
4383                 }
4384
4385                 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
4386
4387                 _account_type_convert_column_to_label(hstmt, label_record);
4388
4389                 _ACCOUNT_FREE(*label);
4390                 //Making label newly created
4391                 *label = _account_dup_text(label_record->label);
4392
4393                 _account_type_free_label_with_items(label_record);
4394
4395                 rc = _account_query_step(hstmt);
4396         }
4397
4398         rc = _account_query_finalize(hstmt);
4399         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4400         hstmt = NULL;
4401
4402         error_code = _ACCOUNT_ERROR_NONE;
4403
4404 CATCH:
4405         if (hstmt != NULL) {
4406                 rc = _account_query_finalize(hstmt);
4407                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4408                 hstmt = NULL;
4409         }
4410
4411         if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
4412                 error_code = _account_type_query_label_by_locale_from_global_db(app_id, locale, label);
4413
4414         _INFO("_account_type_query_label_by_locale() end : error_code = %d", error_code);
4415
4416         return error_code;
4417 }
4418
4419 static int _account_insert_custom(account_s *account, int account_id)
4420 {
4421         _INFO("_account_insert_custom start");
4422
4423         int                             rc, count = 1;
4424         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4425         account_stmt    hstmt = NULL;
4426
4427         ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4428
4429         if (g_slist_length(account->custom_list) == 0) {
4430                 ACCOUNT_DEBUG("_account_insert_custom, no custom data\n");
4431                 return _ACCOUNT_ERROR_NONE;
4432         }
4433
4434         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
4435
4436         rc = _account_get_record_count(g_hAccountDB, query);
4437
4438         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4439                 ACCOUNT_ERROR("Access failed(%d, %s)", _account_db_err_msg(g_hAccountDB));
4440                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4441         }
4442
4443         if (rc <= 0) {
4444                 ACCOUNT_SLOGE("_account_insert_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
4445                 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4446         }
4447
4448         /* insert query*/
4449
4450         GSList *iter;
4451
4452         for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
4453                 int ret;
4454                 count = 1;
4455                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4456                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (AccountId, AppId, Key, Value) VALUES "
4457                                 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
4458
4459                 hstmt = _account_prepare_query(g_hAccountDB, query);
4460
4461                 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4462                         ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4463                         return _ACCOUNT_ERROR_PERMISSION_DENIED;
4464                 }
4465
4466                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
4467
4468                 account_custom_s* custom_data = NULL;
4469                 custom_data = (account_custom_s*)iter->data;
4470
4471                 ret = _account_query_bind_int(hstmt, count++, account_id);
4472                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
4473                 ret = _account_query_bind_text(hstmt, count++, account->package_name);
4474                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4475                 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
4476                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4477                 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
4478                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4479
4480                 rc = _account_query_step(hstmt);
4481
4482                 if (rc != SQLITE_DONE) {
4483                         ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4484                         break;
4485                 }
4486
4487                 rc = _account_query_finalize(hstmt);
4488                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4489                 hstmt = NULL;
4490
4491         }
4492
4493         _INFO("_account_insert_custom end");
4494
4495         return _ACCOUNT_ERROR_NONE;
4496 }
4497
4498 static int _account_update_custom(account_s *account, int account_id)
4499 {
4500         int                             rc, count = 1;
4501         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4502         account_stmt    hstmt = NULL;
4503
4504         ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4505
4506         if (g_slist_length(account->custom_list) == 0) {
4507                 ACCOUNT_DEBUG("_account_update_custom, no custom data\n");
4508                 return _ACCOUNT_ERROR_NONE;
4509         }
4510
4511         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
4512
4513         rc = _account_get_record_count(g_hAccountDB, query);
4514
4515         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4516                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4517                 pthread_mutex_unlock(&account_mutex);
4518                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4519         } else if (_account_db_err_code(g_hAccountDB) == SQLITE_BUSY) {
4520                 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
4521                 pthread_mutex_unlock(&account_mutex);
4522                 return _ACCOUNT_ERROR_DATABASE_BUSY;
4523         }
4524
4525         if (rc <= 0) {
4526                 ACCOUNT_SLOGE("_account_update_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
4527                 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4528         }
4529
4530         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4531
4532         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId=? ", ACCOUNT_CUSTOM_TABLE);
4533         hstmt = _account_prepare_query(g_hAccountDB, query);
4534         count = 1;
4535         _account_query_bind_int(hstmt, count++, (int)account_id);
4536         rc = _account_query_step(hstmt);
4537
4538         if (rc == SQLITE_BUSY) {
4539                 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4540                 if (_account_query_finalize(hstmt) != _ACCOUNT_ERROR_NONE)
4541                         _ERR("finalize error - account_query_step() failed");
4542                 return _ACCOUNT_ERROR_DATABASE_BUSY;
4543         } else if (rc != SQLITE_DONE) {
4544                 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4545                 if (_account_query_finalize(hstmt) != _ACCOUNT_ERROR_NONE)
4546                         _ERR("finalize error - account_query_step() failed");
4547                 return _ACCOUNT_ERROR_DB_FAILED;
4548         }
4549
4550         rc = _account_query_finalize(hstmt);
4551         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4552         hstmt = NULL;
4553
4554         GSList *iter;
4555
4556         for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
4557                 int ret;
4558                 count = 1;
4559                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4560                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AccountId, AppId, Key, Value) VALUES "
4561                                 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
4562
4563                 hstmt = _account_prepare_query(g_hAccountDB, query);
4564
4565                 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4566                         ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4567                         return _ACCOUNT_ERROR_PERMISSION_DENIED;
4568                 }
4569
4570                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
4571
4572                 account_custom_s* custom_data = NULL;
4573                 custom_data = (account_custom_s*)iter->data;
4574
4575                 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
4576                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
4577                 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
4578                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4579                 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
4580                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4581                 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
4582                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4583
4584                 rc = _account_query_step(hstmt);
4585
4586                 if (rc != SQLITE_DONE) {
4587                         ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4588                         break;
4589                 }
4590
4591                 rc = _account_query_finalize(hstmt);
4592                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4593                 hstmt = NULL;
4594
4595         }
4596
4597         return _ACCOUNT_ERROR_NONE;
4598 }
4599