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