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