4.0 tool chain build error fix
[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         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), { *error_code = rc; }, NULL, ("finalize error"));
1188         hstmt = NULL;
1189
1190         *error_code = _ACCOUNT_ERROR_NONE;
1191
1192 CATCH:
1193         if (hstmt != NULL) {
1194                 rc = _account_query_finalize(hstmt);
1195                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), { *error_code = rc; }, NULL, ("finalize error"));
1196                 hstmt = NULL;
1197         }
1198
1199         pthread_mutex_unlock(&account_mutex);
1200
1201         return capability_list;
1202 }
1203
1204 static int _account_compare_old_record(account_s *new_account, int account_id)
1205 {
1206         int                             error_code = _ACCOUNT_ERROR_NONE;
1207         account_stmt    hstmt = NULL;
1208         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1209         int                             rc = 0;
1210         account_s *old_account = NULL;
1211
1212         ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1213         ACCOUNT_RETURN_VAL((new_account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
1214         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1215
1216         old_account = (account_s*)calloc(1, sizeof(account_s));
1217         if (old_account == NULL) {
1218                 _ERR("Out of Memory");
1219                 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
1220         }
1221
1222         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1223
1224         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
1225         hstmt = _account_prepare_query(g_hAccountDB, query);
1226
1227         rc = _account_query_step(hstmt);
1228         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1229
1230         while (rc == SQLITE_ROW) {
1231                 _account_convert_column_to_account(hstmt, old_account);
1232                 rc = _account_query_step(hstmt);
1233         }
1234
1235         rc = _account_query_finalize(hstmt);
1236         ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1237         hstmt = NULL;
1238
1239         // get capability
1240         error_code = _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, old_account->id, (void*)old_account);
1241         ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
1242
1243         // get custom text
1244         error_code = _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, old_account->id, (void*)old_account);
1245         ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
1246
1247         // compare
1248
1249         new_account->id = old_account->id;
1250
1251         //user name
1252         if (!new_account->user_name) {
1253                 if (old_account->user_name)
1254                         new_account->user_name = _account_dup_text(old_account->user_name);
1255         }
1256
1257         // display name
1258         if (!new_account->display_name) {
1259                 if (old_account->display_name)
1260                         new_account->display_name = _account_dup_text(old_account->display_name);
1261         }
1262
1263         // email address
1264         if (!new_account->email_address) {
1265                 if (old_account->email_address)
1266                         new_account->email_address = _account_dup_text(old_account->email_address);
1267         }
1268
1269         // domain name
1270         if (!new_account->domain_name) {
1271                 if (old_account->domain_name)
1272                         new_account->domain_name = _account_dup_text(old_account->domain_name);
1273         }
1274
1275         // icon path
1276         if (!new_account->icon_path) {
1277                 if (old_account->icon_path)
1278                         new_account->icon_path = _account_dup_text(old_account->icon_path);
1279         }
1280
1281         // source
1282         if (!new_account->source) {
1283                 if (old_account->source)
1284                         new_account->source = _account_dup_text(old_account->source);
1285         }
1286
1287         _ACCOUNT_FREE(new_account->package_name);
1288         new_account->package_name = _account_dup_text(old_account->package_name);
1289
1290         // access token
1291         if (!new_account->access_token) {
1292                 if (old_account->access_token)
1293                         new_account->access_token = _account_dup_text(old_account->access_token);
1294         }
1295
1296         // user text
1297         int i;
1298         for (i = 0; i < USER_TXT_CNT; i++) {
1299                 if (!new_account->user_data_txt[i]) {
1300                         if (old_account->user_data_txt[i])
1301                                 new_account->user_data_txt[i] = _account_dup_text(old_account->user_data_txt[i]);
1302                 }
1303         }
1304
1305         // auth type
1306         if (new_account->auth_type == _ACCOUNT_AUTH_TYPE_INVALID)
1307                 new_account->auth_type = old_account->auth_type;
1308
1309         // secret
1310         if (new_account->secret == _ACCOUNT_SECRECY_INVALID)
1311                 new_account->secret = old_account->secret;
1312
1313         // sync support
1314         if (new_account->sync_support == _ACCOUNT_SYNC_INVALID)
1315                 new_account->sync_support = old_account->sync_support;
1316
1317         // user int
1318         for (i = 0; i < USER_INT_CNT; i++) {
1319                 if (new_account->user_data_int[i] == 0)
1320                                 new_account->user_data_int[i] = old_account->user_data_int[i];
1321         }
1322
1323 CATCH:
1324         if (old_account)
1325                 _account_free_account_with_items(old_account);
1326
1327         if (hstmt != NULL) {
1328                 rc = _account_query_finalize(hstmt);
1329                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1330                 hstmt = NULL;
1331         }
1332
1333         return _ACCOUNT_ERROR_NONE;
1334 }
1335
1336 static int _account_get_package_name_from_account_id(int account_id, char **package_name)
1337 {
1338         int                             error_code = _ACCOUNT_ERROR_NONE;
1339         account_stmt    hstmt = NULL;
1340         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1341         int                             rc = 0;
1342         account_s *old_account = NULL;
1343
1344         ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1345         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1346
1347         old_account = (account_s*)calloc(1, sizeof(account_s));
1348         if (old_account == NULL) {
1349                 _ERR("Out Of memory");
1350                 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
1351         }
1352
1353         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1354
1355         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
1356         hstmt = _account_prepare_query(g_hAccountDB, query);
1357
1358         rc = _account_query_step(hstmt);
1359         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1360
1361         while (rc == SQLITE_ROW) {
1362                 _account_convert_column_to_account(hstmt, old_account);
1363                 rc = _account_query_step(hstmt);
1364         }
1365
1366         rc = _account_query_finalize(hstmt);
1367         ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1368         hstmt = NULL;
1369
1370         // get package name.
1371         *package_name = _account_dup_text(old_account->package_name);
1372
1373 CATCH:
1374         if (old_account)
1375                 _account_free_account_with_items(old_account);
1376
1377         if (hstmt != NULL) {
1378                 rc = _account_query_finalize(hstmt);
1379                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1380                 hstmt = NULL;
1381         }
1382
1383         return error_code;
1384
1385 }
1386
1387 static int _account_update_account(int pid, uid_t uid, account_s *account, int account_id)
1388 {
1389         int                             rc = 0, binding_count = 0;
1390         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1391         int                             error_code = _ACCOUNT_ERROR_NONE, count = 0, ret_transaction = 0;
1392         account_stmt    hstmt = NULL;
1393
1394         if (!account->package_name) {
1395                 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
1396                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1397         }
1398
1399         /* Check permission of requested appid */
1400         char* current_appid = NULL;
1401         char *package_name = NULL;
1402
1403         current_appid = _account_get_current_appid(pid, uid);
1404         error_code = _account_get_package_name_from_account_id(account_id, &package_name);
1405
1406         if (error_code != _ACCOUNT_ERROR_NONE || package_name == NULL) {
1407                 ACCOUNT_ERROR("No package name with account_id\n");
1408                 _ACCOUNT_FREE(current_appid);
1409                 _ACCOUNT_FREE(package_name);
1410                 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1411         }
1412
1413         error_code = _account_check_appid_group_with_package_name(current_appid, package_name, uid);
1414         ACCOUNT_DEBUG("UPDATE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name);      // TODO: remove the log later.
1415
1416         _ACCOUNT_FREE(current_appid);
1417         _ACCOUNT_FREE(package_name);
1418
1419         if (error_code != _ACCOUNT_ERROR_NONE) {
1420                 ACCOUNT_ERROR("No permission to update\n");
1421                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1422         }
1423
1424         error_code = encrypt_access_token(account);
1425         if (error_code != _ACCOUNT_ERROR_NONE) {
1426                 _ERR("_encrypt_access_token error");
1427                 return error_code;
1428         }
1429
1430         error_code = _account_compare_old_record(account, account_id);
1431         if (error_code != _ACCOUNT_ERROR_NONE) {
1432                 ACCOUNT_ERROR("_account_compare_old_record fail\n");
1433                 return error_code;
1434         }
1435
1436         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1437                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1438                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1439         } else if (_account_db_err_code(g_hAccountDB) == SQLITE_BUSY) {
1440                 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
1441                 return _ACCOUNT_ERROR_DATABASE_BUSY;
1442         }
1443
1444         if (!account->user_name && !account->display_name && !account->email_address) {
1445                 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1446                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1447         }
1448
1449         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1450
1451         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
1452
1453         count = _account_get_record_count(g_hAccountDB, query);
1454         if (count <= 0) {
1455                 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
1456                 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1457         }
1458
1459         /* transaction control required*/
1460         ret_transaction = _account_begin_transaction(g_hAccountDB);
1461         if (ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY) {
1462                 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
1463                 pthread_mutex_unlock(&account_mutex);
1464                 return _ACCOUNT_ERROR_DATABASE_BUSY;
1465         }
1466
1467         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1468         ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
1469                         "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
1470                         "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
1471                         "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
1472
1473         hstmt = _account_prepare_query(g_hAccountDB, query);
1474
1475         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1476                 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1477                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1478                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1479         }
1480
1481         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)));
1482
1483         binding_count = _account_convert_account_to_sql(account, hstmt, query);
1484         _account_query_bind_int(hstmt, binding_count++, account_id);
1485
1486         rc = _account_query_step(hstmt);
1487         if (rc != SQLITE_DONE)
1488                 ACCOUNT_SLOGE("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
1489
1490         rc = _account_query_finalize(hstmt);
1491         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1492         hstmt = NULL;
1493
1494         _INFO("update query=%s", query);
1495
1496         /*update capability*/
1497         error_code = _account_update_capability(account, account_id);
1498         if (error_code != _ACCOUNT_ERROR_NONE && error_code != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
1499                 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1500                 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1501                 return error_code;
1502         }
1503
1504         /* update custom */
1505         error_code = _account_update_custom(account, account_id);
1506         if (error_code != _ACCOUNT_ERROR_NONE && error_code != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
1507                 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1508                 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1509                 return error_code;
1510         }
1511
1512         ret_transaction = _account_end_transaction(g_hAccountDB, TRUE);
1513
1514         _INFO("update end");
1515
1516         return error_code;
1517 }
1518
1519
1520 static int _account_update_account_ex(account_s *account, int account_id)
1521 {
1522         int                             rc = 0, binding_count = 0;
1523         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1524         int                             error_code = _ACCOUNT_ERROR_NONE, count = 0, ret_transaction = 0;
1525         account_stmt    hstmt = NULL;
1526
1527         if (!account->package_name) {
1528                 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
1529                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1530         }
1531
1532         error_code = encrypt_access_token(account);
1533         if (error_code != _ACCOUNT_ERROR_NONE) {
1534                 _ERR("_encrypt_access_token error");
1535                 return error_code;
1536         }
1537
1538         error_code = _account_compare_old_record(account, account_id);
1539         if (error_code != _ACCOUNT_ERROR_NONE) {
1540                 ACCOUNT_ERROR("_account_compare_old_record fail\n");
1541                 return error_code;
1542         }
1543
1544         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1545                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1546                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1547         }
1548
1549         if (!account->user_name && !account->display_name && !account->email_address) {
1550                 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1551                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1552         }
1553
1554         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1555
1556         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
1557
1558         count = _account_get_record_count(g_hAccountDB, query);
1559         if (count <= 0) {
1560                 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
1561                 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1562         }
1563
1564         /* transaction control required*/
1565         ret_transaction = _account_begin_transaction(g_hAccountDB);
1566         if (ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY) {
1567                 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
1568                 pthread_mutex_unlock(&account_mutex);
1569                 return _ACCOUNT_ERROR_DATABASE_BUSY;
1570         }
1571
1572         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1573         ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
1574                         "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
1575                         "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
1576                         "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
1577
1578         hstmt = _account_prepare_query(g_hAccountDB, query);
1579
1580         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1581                 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1582                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1583                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1584         }
1585
1586         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)));
1587
1588         _INFO("account_update_to_db_by_id_ex_p : before convert() : account_id[%d], user_name=%s", account->id, account->user_name);
1589         binding_count = _account_convert_account_to_sql(account, hstmt, query);
1590         _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], user_name=%s", account->id, account->user_name);
1591         _INFO("account_update_to_db_by_id_ex_p : before bind()");
1592         rc = _account_query_bind_int(hstmt, binding_count++, account_id);
1593         _INFO("account_update_to_db_by_id_ex_p : after bind() : ret = %d", rc);
1594
1595         rc = _account_query_step(hstmt);
1596         if (rc != SQLITE_DONE)
1597                 ACCOUNT_SLOGE("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
1598         _INFO("account_update_to_db_by_id_ex_p : after query_step() : ret = %d", rc);
1599
1600         rc = _account_query_finalize(hstmt);
1601         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1602         hstmt = NULL;
1603         _INFO("account_update_to_db_by_id_ex_p : after query_filnalize() : ret = %d", rc);
1604
1605         _INFO("account_update_to_db_by_id_ex_p : before update_capability()");
1606         /*update capability*/
1607         error_code = _account_update_capability(account, account_id);
1608         if (error_code != _ACCOUNT_ERROR_NONE && error_code != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
1609                 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1610                 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1611                 return error_code;
1612         }
1613         _INFO("account_update_to_db_by_id_ex_p : after update_capability()");
1614
1615         _INFO("account_update_to_db_by_id_ex_p : before update_custom()");
1616         /* update custom */
1617         error_code = _account_update_custom(account, account_id);
1618         if (error_code != _ACCOUNT_ERROR_NONE && error_code != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
1619                 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1620                 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1621                 return error_code;
1622         }
1623         _INFO("account_update_to_db_by_id_ex_p : after update_custom()");
1624
1625         ret_transaction = _account_end_transaction(g_hAccountDB, TRUE);
1626
1627         return error_code;
1628 }
1629
1630
1631 int _account_update_to_db_by_id(int pid, uid_t uid, account_s* account, int account_id)
1632 {
1633         ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
1634         ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
1635         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1636         int     error_code = _ACCOUNT_ERROR_NONE;
1637         account_s* data = (account_s*)account;
1638
1639         pthread_mutex_lock(&account_mutex);
1640
1641         error_code = _account_update_account(pid, uid, data, account_id);
1642
1643         if (error_code != _ACCOUNT_ERROR_NONE) {
1644                 pthread_mutex_unlock(&account_mutex);
1645                 return error_code;
1646         }
1647
1648         pthread_mutex_unlock(&account_mutex);
1649
1650         char buf[64] = {0,};
1651         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_UPDATE, account_id);
1652         _account_insert_delete_update_notification_send(buf);
1653
1654         return _ACCOUNT_ERROR_NONE;
1655 }
1656
1657 int _account_update_to_db_by_id_ex(account_s* account, int account_id)
1658 {
1659         ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
1660         ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
1661         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1662         int     error_code = _ACCOUNT_ERROR_NONE;
1663         account_s* data = account;
1664
1665         pthread_mutex_lock(&account_mutex);
1666
1667         _INFO("before update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name);
1668         error_code = _account_update_account_ex(data, account_id);
1669         _INFO("after update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name);
1670
1671         if (error_code != _ACCOUNT_ERROR_NONE) {
1672                 pthread_mutex_unlock(&account_mutex);
1673                 return error_code;
1674         }
1675
1676         pthread_mutex_unlock(&account_mutex);
1677
1678         char buf[64] = {0,};
1679         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_UPDATE, account_id);
1680         _account_insert_delete_update_notification_send(buf);
1681
1682         return _ACCOUNT_ERROR_NONE;
1683 }
1684
1685 int _account_update_to_db_by_user_name(int pid, uid_t uid, account_s* account, const char *user_name, const char *package_name)
1686 {
1687         ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
1688         ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
1689         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1690
1691         int     error_code = _ACCOUNT_ERROR_NONE;
1692         account_s *data = (account_s*)account;
1693
1694         pthread_mutex_lock(&account_mutex);
1695
1696         error_code = _account_update_account_by_user_name(pid, uid, data, user_name, package_name);
1697
1698         pthread_mutex_unlock(&account_mutex);
1699
1700         char buf[64] = {0,};
1701         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_UPDATE, data->id);
1702         _account_insert_delete_update_notification_send(buf);
1703
1704         return error_code;
1705 }
1706
1707 GSList* _account_db_query_all(int pid, uid_t uid)
1708 {
1709         //int                   error_code = _ACCOUNT_ERROR_NONE;
1710         account_stmt    hstmt = NULL;
1711         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1712         int                             rc = 0;
1713         GSList                  *account_list = NULL;
1714
1715         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected."));
1716
1717         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1718
1719         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TABLE);
1720         hstmt = _account_prepare_query(g_hAccountDB, query);
1721
1722         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1723                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1724                 return NULL;
1725         }
1726
1727         rc = _account_query_step(hstmt);
1728
1729         account_s *account_record = NULL;
1730
1731         if (rc != SQLITE_ROW) {
1732                 _ERR("The record isn't found");
1733                 goto CATCH;
1734         }
1735
1736         while (rc == SQLITE_ROW) {
1737                 account_record = (account_s*) malloc(sizeof(account_s));
1738
1739                 if (account_record == NULL) {
1740                         ACCOUNT_FATAL("malloc Failed");
1741                         break;
1742                 }
1743
1744                 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
1745                 _account_convert_column_to_account(hstmt, account_record);
1746                 account_list = g_slist_append(account_list, account_record);
1747                 rc = _account_query_step(hstmt);
1748         }
1749
1750         rc = _account_query_finalize(hstmt);
1751         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, NULL, ("finalize error"));
1752         hstmt = NULL;
1753
1754         GSList* iter;
1755
1756         for (iter = account_list; iter != NULL; iter = g_slist_next(iter)) {
1757                 account_s *account = NULL;
1758                 account = (account_s*)iter->data;
1759                 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, account->id, (void*)account);
1760                 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, account->id, (void*)account);
1761         }
1762
1763 CATCH:
1764         if (hstmt != NULL) {
1765                 rc = _account_query_finalize(hstmt);
1766                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_gslist_account_free(account_list); }, NULL, ("finalize error"));
1767                 hstmt = NULL;
1768         }
1769
1770         if (account_list)
1771                 _remove_sensitive_info_from_non_owning_account_slist(account_list, pid, uid);
1772
1773         return account_list;
1774 }
1775
1776 int _account_update_sync_status_by_id(uid_t uid, int account_db_id, const int sync_status)
1777 {
1778         int                             error_code = _ACCOUNT_ERROR_NONE;
1779         account_stmt    hstmt = NULL;
1780         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1781         int                             rc = 0;
1782         int count = 1;
1783
1784         ACCOUNT_RETURN_VAL((account_db_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1785         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1786         if ((sync_status < 0) || (sync_status >= _ACCOUNT_SYNC_MAX)) {
1787                 ACCOUNT_SLOGE("(%s)-(%d) sync_status is less than 0 or more than enum max.\n", __FUNCTION__, __LINE__);
1788                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1789         }
1790
1791         pthread_mutex_lock(&account_mutex);
1792
1793         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1794
1795         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_db_id);
1796
1797         rc = _account_get_record_count(g_hAccountDB, query);
1798
1799         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1800                 pthread_mutex_unlock(&account_mutex);
1801                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1802                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1803         }
1804
1805         if (rc <= 0) {
1806                 ACCOUNT_SLOGE("account_update_sync_status_by_id : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
1807                 pthread_mutex_unlock(&account_mutex);
1808                 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1809         }
1810
1811         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1812
1813         ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET sync_support=? WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
1814         hstmt = _account_prepare_query(g_hAccountDB, query);
1815
1816         _account_query_bind_int(hstmt, count, sync_status);
1817
1818         rc = _account_query_step(hstmt);
1819
1820         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1821                 pthread_mutex_unlock(&account_mutex);
1822                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1823                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1824         }
1825
1826         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_DB_FAILED,
1827                                 ("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB)));
1828
1829         rc = _account_query_finalize(hstmt);
1830         if (rc != _ACCOUNT_ERROR_NONE) {
1831                 ACCOUNT_ERROR("_account_query_finalize error");
1832                 pthread_mutex_unlock(&account_mutex);
1833                 return rc;
1834         }
1835
1836         char buf[64] = {0,};
1837         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_SYNC_UPDATE, account_db_id);
1838         _account_insert_delete_update_notification_send(buf);
1839
1840         hstmt = NULL;
1841         error_code = _ACCOUNT_ERROR_NONE;
1842
1843 CATCH:
1844         if (hstmt != NULL) {
1845                 rc = _account_query_finalize(hstmt);
1846                 pthread_mutex_unlock(&account_mutex);
1847                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1848                 hstmt = NULL;
1849         }
1850
1851         pthread_mutex_unlock(&account_mutex);
1852
1853         return error_code;
1854 }
1855
1856 int _account_query_account_by_account_id(int pid, uid_t uid, int account_db_id, account_s *account_record)
1857 {
1858         _INFO("_account_query_account_by_account_id() start, account_db_id=[%d]", account_db_id);
1859
1860         int                             error_code = _ACCOUNT_ERROR_NONE;
1861         account_stmt    hstmt = NULL;
1862         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1863         int                             rc = 0;
1864
1865         ACCOUNT_RETURN_VAL((account_db_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1866         ACCOUNT_RETURN_VAL(account_record != NULL, {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
1867         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1868
1869         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1870
1871         ACCOUNT_DEBUG("starting db operations");
1872
1873         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
1874         hstmt = _account_prepare_query(g_hAccountDB, query);
1875         rc = _account_db_err_code(g_hAccountDB);
1876         _INFO("after _account_prepare_query, rc=[%d]", rc);
1877
1878         if (rc == SQLITE_PERM) {
1879                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1880                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1881         }
1882
1883         ACCOUNT_DEBUG("before _account_query_step");
1884         rc = _account_query_step(hstmt);
1885         ACCOUNT_DEBUG("after _account_query_step returned [%d]", rc);
1886         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1887
1888         while (rc == SQLITE_ROW) {
1889                 ACCOUNT_DEBUG("before _account_convert_column_to_account");
1890                 _account_convert_column_to_account(hstmt, account_record);
1891                 ACCOUNT_DEBUG("after _account_convert_column_to_account");
1892                 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]);
1893                 rc = _account_query_step(hstmt);
1894         }
1895
1896         ACCOUNT_DEBUG("account_record->id=[%d]", account_record->id);
1897
1898         rc = _account_query_finalize(hstmt);
1899         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1900
1901         ACCOUNT_DEBUG("before _account_query_capability_by_account_id");
1902         _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, account_record->id, (void*)account_record);
1903         ACCOUNT_DEBUG("after _account_query_capability_by_account_id");
1904
1905         ACCOUNT_DEBUG("before _account_query_custom_by_account_id");
1906         _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, account_record->id, (void*)account_record);
1907         ACCOUNT_DEBUG("after _account_query_custom_by_account_id");
1908
1909         hstmt = NULL;
1910         error_code = _ACCOUNT_ERROR_NONE;
1911
1912 CATCH:
1913         if (hstmt != NULL) {
1914                 rc = _account_query_finalize(hstmt);
1915                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1916                 hstmt = NULL;
1917         }
1918
1919         if (account_record)
1920                 _remove_sensitive_info_from_non_owning_account(account_record, pid, uid);
1921
1922         pthread_mutex_unlock(&account_mutex);
1923         ACCOUNT_DEBUG("_account_query_account_by_account_id end [%d]", error_code);
1924
1925         return error_code;
1926 }
1927
1928 GList* _account_query_account_by_user_name(int pid, uid_t uid, const char *user_name, int *error_code)
1929 {
1930         *error_code = _ACCOUNT_ERROR_NONE;
1931         account_stmt    hstmt = NULL;
1932         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1933         int                             rc = 0;
1934         account_s *account_head = NULL;
1935
1936         if (user_name == NULL) {
1937                 _ERR("USER NAME IS NULL");
1938                 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
1939                 goto CATCH;
1940         }
1941
1942         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1943
1944         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ?", ACCOUNT_TABLE);
1945
1946         hstmt = _account_prepare_query(g_hAccountDB, query);
1947
1948         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1949                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1950                 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
1951                 goto CATCH;
1952         }
1953
1954         int binding_count = 1;
1955         _account_query_bind_text(hstmt, binding_count++, user_name);
1956
1957         rc = _account_query_step(hstmt);
1958
1959         if (rc != SQLITE_ROW) {
1960                 _ERR("The record isn't found");
1961                 *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1962                 goto CATCH;
1963         }
1964
1965         int tmp = 0;
1966
1967         account_head = (account_s*) malloc(sizeof(account_s));
1968         if (account_head == NULL) {
1969                 ACCOUNT_FATAL("malloc Failed");
1970                 if (hstmt != NULL) {
1971                         rc = _account_query_finalize(hstmt);
1972                         if (rc != _ACCOUNT_ERROR_NONE) {
1973                                 _ERR("finalize error");
1974                                 *error_code = rc;
1975                                 goto CATCH;
1976                         }
1977                         hstmt = NULL;
1978                 }
1979                 *error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
1980                 goto CATCH;
1981         }
1982         ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
1983
1984         while (rc == SQLITE_ROW) {
1985                 account_s* account_record = NULL;
1986
1987                 account_record = (account_s*) malloc(sizeof(account_s));
1988
1989                 if (account_record == NULL) {
1990                         ACCOUNT_FATAL("malloc Failed");
1991                         break;
1992                 }
1993                 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
1994
1995                 _account_convert_column_to_account(hstmt, account_record);
1996
1997                 account_head->account_list = g_list_append(account_head->account_list, account_record);
1998
1999                 rc = _account_query_step(hstmt);
2000                 tmp++;
2001         }
2002
2003         rc = _account_query_finalize(hstmt);
2004
2005         if (rc != _ACCOUNT_ERROR_NONE) {
2006                 _ERR("finalize error");
2007                 *error_code = rc;
2008                 goto CATCH;
2009         }
2010
2011         hstmt = NULL;
2012
2013         GList *iter;
2014
2015         tmp = g_list_length(account_head->account_list);
2016
2017         for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2018                 //account_h account;
2019                 //account = (account_h)iter->data;
2020
2021                 account_s *testaccount = (account_s*)iter->data;
2022
2023                 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, testaccount->id, (void*)testaccount);
2024                 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, testaccount->id, (void*)testaccount);
2025         }
2026
2027         *error_code = _ACCOUNT_ERROR_NONE;
2028
2029 CATCH:
2030         if (hstmt != NULL) {
2031                 rc = _account_query_finalize(hstmt);
2032                 if (rc != _ACCOUNT_ERROR_NONE) {
2033                         _ERR("finalize error");
2034                         *error_code = rc;
2035                 }
2036                 hstmt = NULL;
2037         }
2038
2039         pthread_mutex_unlock(&account_mutex);
2040
2041         if (account_head) {
2042                 _remove_sensitive_info_from_non_owning_account_list(account_head->account_list, pid, uid);
2043                 GList* result = account_head->account_list;
2044                 _ACCOUNT_FREE(account_head);
2045                 return result;
2046         }
2047
2048         return NULL;
2049 }
2050
2051 GList*
2052 _account_query_account_by_capability(int pid, uid_t uid, const char* capability_type, const int capability_value, int *error_code)
2053 {
2054         *error_code = _ACCOUNT_ERROR_NONE;
2055         account_stmt    hstmt = NULL;
2056         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2057         int                             rc = 0;
2058
2059         ACCOUNT_RETURN_VAL((capability_type != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("capability_type IS NULL"));
2060
2061         if ((capability_value  < 0) || (capability_value >= _ACCOUNT_CAPABILITY_STATE_MAX)) {
2062                 ACCOUNT_SLOGE("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
2063                 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
2064                 return NULL;
2065         }
2066
2067         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), { *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED; }, NULL, ("The database isn't connected."));
2068
2069         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2070
2071         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=? AND value=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
2072
2073         hstmt = _account_prepare_query(g_hAccountDB, query);
2074
2075         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2076                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2077                 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
2078                 return NULL;
2079         }
2080
2081         int binding_count = 1;
2082         _account_query_bind_text(hstmt, binding_count++, capability_type);
2083         _account_query_bind_int(hstmt, binding_count++, capability_value);
2084
2085         rc = _account_query_step(hstmt);
2086
2087         account_s* account_head = NULL;
2088
2089         ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2090
2091         int tmp = 0;
2092
2093         account_head = (account_s*) malloc(sizeof(account_s));
2094         if (account_head == NULL) {
2095                 ACCOUNT_FATAL("malloc Failed");
2096                 if (hstmt != NULL) {
2097                         rc = _account_query_finalize(hstmt);
2098                         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), { *error_code = rc; }, NULL, ("finalize error"));
2099                         hstmt = NULL;
2100                 }
2101                 *error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
2102                 return NULL;
2103         }
2104         ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2105
2106         while (rc == SQLITE_ROW) {
2107                 account_s* account_record = NULL;
2108
2109                 account_record = (account_s*) malloc(sizeof(account_s));
2110
2111                 if (account_record == NULL) {
2112                         ACCOUNT_FATAL("malloc Failed");
2113                         break;
2114                 }
2115                 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2116
2117                 _account_convert_column_to_account(hstmt, account_record);
2118
2119                 account_head->account_list = g_list_append(account_head->account_list, account_record);
2120
2121                 rc = _account_query_step(hstmt);
2122                 tmp++;
2123         }
2124
2125         rc = _account_query_finalize(hstmt);
2126         ACCOUNT_CATCH_ERROR_P((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2127         hstmt = NULL;
2128
2129         GList *iter;
2130
2131
2132         tmp = g_list_length(account_head->account_list);
2133
2134         for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2135                 //account_h account = NULL;
2136                 //account = (account_h)iter->data;
2137                 account_s* testaccount = (account_s*)iter->data;
2138
2139                 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, testaccount->id, (void*)testaccount);
2140                 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, testaccount->id, (void*)testaccount);
2141
2142         }
2143
2144         *error_code = _ACCOUNT_ERROR_NONE;
2145
2146 CATCH:
2147         if (hstmt != NULL) {
2148                 rc = _account_query_finalize(hstmt);
2149                 if (rc != _ACCOUNT_ERROR_NONE) {
2150                         *error_code = rc;
2151                         _ERR("finalize error");
2152                 }
2153                 hstmt = NULL;
2154         }
2155
2156         if (*error_code != _ACCOUNT_ERROR_NONE && account_head) {
2157                 _account_glist_account_free(account_head->account_list);
2158                 _ACCOUNT_FREE(account_head);
2159                 account_head = NULL;
2160         }
2161
2162         pthread_mutex_unlock(&account_mutex);
2163
2164         if (account_head) {
2165                 _remove_sensitive_info_from_non_owning_account_list(account_head->account_list, pid, uid);
2166                 GList* result = account_head->account_list;
2167                 _ACCOUNT_FREE(account_head);
2168                 return result;
2169         }
2170
2171         return NULL;
2172 }
2173
2174 GList* _account_query_account_by_capability_type(int pid, uid_t uid, const char* capability_type, int *error_code)
2175 {
2176         *error_code = _ACCOUNT_ERROR_NONE;
2177         account_stmt    hstmt = NULL;
2178         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2179         int                             rc = 0;
2180
2181         ACCOUNT_RETURN_VAL((capability_type != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("capability_type IS NULL"));
2182         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), { *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED; },
2183                                            NULL, ("The database isn't connected."));
2184
2185         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2186
2187         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
2188
2189         hstmt = _account_prepare_query(g_hAccountDB, query);
2190
2191         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2192                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2193                 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
2194                 return NULL;
2195         }
2196
2197         int binding_count = 1;
2198         _account_query_bind_text(hstmt, binding_count++, capability_type);
2199
2200         rc = _account_query_step(hstmt);
2201
2202         account_s* account_head = NULL;
2203
2204         ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2205
2206         int tmp = 0;
2207
2208         account_head = (account_s*) malloc(sizeof(account_s));
2209         if (account_head == NULL) {
2210                 ACCOUNT_FATAL("malloc Failed");
2211                 if (hstmt != NULL) {
2212                         rc = _account_query_finalize(hstmt);
2213                         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), { *error_code = rc; }, NULL, ("finalize error"));
2214                         hstmt = NULL;
2215                 }
2216                 *error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
2217                 return NULL;
2218         }
2219         ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2220
2221         while (rc == SQLITE_ROW) {
2222                 account_s* account_record = NULL;
2223
2224                 account_record = (account_s*) malloc(sizeof(account_s));
2225
2226                 if (account_record == NULL) {
2227                         ACCOUNT_FATAL("malloc Failed");
2228                         break;
2229                 }
2230                 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2231
2232                 _account_convert_column_to_account(hstmt, account_record);
2233
2234                 account_head->account_list = g_list_append(account_head->account_list, account_record);
2235
2236                 rc = _account_query_step(hstmt);
2237                 tmp++;
2238         }
2239
2240         rc = _account_query_finalize(hstmt);
2241         ACCOUNT_CATCH_ERROR_P((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2242         hstmt = NULL;
2243
2244         GList *iter;
2245
2246
2247         tmp = g_list_length(account_head->account_list);
2248
2249         for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2250                 account_s* testaccount = (account_s*)iter->data;
2251
2252                 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, testaccount->id, (void*)testaccount);
2253                 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, testaccount->id, (void*)testaccount);
2254
2255         }
2256
2257         *error_code = _ACCOUNT_ERROR_NONE;
2258
2259 CATCH:
2260         if (hstmt != NULL) {
2261                 rc = _account_query_finalize(hstmt);
2262                 if (rc != _ACCOUNT_ERROR_NONE) {
2263                         *error_code = rc;
2264                         _ERR("finalize error");
2265                 }
2266                 hstmt = NULL;
2267         }
2268
2269         if ((*error_code != _ACCOUNT_ERROR_NONE) && account_head) {
2270                 _account_glist_account_free(account_head->account_list);
2271                 _ACCOUNT_FREE(account_head);
2272                 account_head = NULL;
2273         }
2274
2275         pthread_mutex_unlock(&account_mutex);
2276
2277         if (account_head) {
2278                 _remove_sensitive_info_from_non_owning_account_list(account_head->account_list, pid, uid);
2279                 GList* result = account_head->account_list;
2280                 _ACCOUNT_FREE(account_head);
2281                 return result;
2282         }
2283
2284         return NULL;
2285 }
2286
2287 GList* account_server_query_account_by_package_name(const char* package_name, int *error_code, int pid, uid_t uid)
2288 {
2289         _INFO("account_server_query_account_by_package_name start");
2290
2291         GList * account_list = NULL;
2292         *error_code = _ACCOUNT_ERROR_NONE;
2293
2294         ACCOUNT_RETURN_VAL((package_name != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("PACKAGE NAME IS NULL"));
2295         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), { *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED; }, NULL, ("The database isn't connected."));
2296
2297         account_list = _account_query_account_by_package_name(g_hAccountDB, package_name, error_code, pid, uid);
2298
2299         _INFO("account_server_query_account_by_package_name end");
2300
2301         return account_list;
2302 }
2303
2304 int account_server_delete_account_by_package_name(const char* package_name, bool permission, int pid, uid_t uid)
2305 {
2306         _INFO("account_db_delete_account_by_package_name");
2307
2308         int error_code = _ACCOUNT_ERROR_NONE;
2309
2310         ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
2311         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2312
2313         error_code = _account_delete_account_by_package_name(g_hAccountDB, package_name, permission, pid, uid);
2314
2315         _INFO("account_server_delete_account_by_package_name end");
2316
2317         return error_code;
2318 }
2319
2320 int _account_delete(int pid, uid_t uid, int account_id)
2321 {
2322         int                             error_code = _ACCOUNT_ERROR_NONE;
2323         account_stmt    hstmt = NULL;
2324         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2325         int                             rc = 0;
2326         int                             ret_transaction = 0;
2327         bool                    is_success = FALSE;
2328
2329         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2330
2331         int count = -1;
2332         /* Check requested ID to delete */
2333         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id=%d", ACCOUNT_TABLE, account_id);
2334
2335         count = _account_get_record_count(g_hAccountDB, query);
2336
2337         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2338                 pthread_mutex_unlock(&account_mutex);
2339                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2340                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2341         }
2342
2343         if (count <= 0) {
2344                 ACCOUNT_ERROR("account id(%d) is not exist. count(%d)\n", account_id, count);
2345                 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
2346         }
2347
2348         /* Check permission of requested appid */
2349         char* current_appid = NULL;
2350         char *package_name = NULL;
2351
2352         current_appid = _account_get_current_appid(pid, uid);
2353
2354         error_code = _account_get_package_name_from_account_id(account_id, &package_name);
2355
2356         if (error_code != _ACCOUNT_ERROR_NONE) {
2357                 ACCOUNT_ERROR("No package name with account_id\n");
2358                 _ACCOUNT_FREE(current_appid);
2359                 _ACCOUNT_FREE(package_name);
2360                 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
2361         }
2362         ACCOUNT_DEBUG("DELETE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name);
2363
2364         error_code = _account_check_appid_group_with_package_name(current_appid, package_name, uid);
2365
2366         _ACCOUNT_FREE(current_appid);
2367         _ACCOUNT_FREE(package_name);
2368
2369         if (error_code != _ACCOUNT_ERROR_NONE) {
2370                 ACCOUNT_ERROR("No permission to delete\n");
2371                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2372         }
2373
2374         /* transaction control required*/
2375         ret_transaction = _account_begin_transaction(g_hAccountDB);
2376
2377         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2378                 pthread_mutex_unlock(&account_mutex);
2379                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2380                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2381         }
2382
2383         if (ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY) {
2384                 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
2385                 pthread_mutex_unlock(&account_mutex);
2386                 return _ACCOUNT_ERROR_DATABASE_BUSY;
2387         }
2388
2389         if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2390                 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
2391                 pthread_mutex_unlock(&account_mutex);
2392                 return ret_transaction;
2393         }
2394
2395         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2396         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
2397
2398         hstmt = _account_prepare_query(g_hAccountDB, query);
2399
2400         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2401                 pthread_mutex_unlock(&account_mutex);
2402                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2403                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2404         }
2405
2406         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2407                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2408
2409         rc = _account_query_step(hstmt);
2410         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2411
2412         rc = _account_query_finalize(hstmt);
2413
2414         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2415         hstmt = NULL;
2416
2417         ACCOUNT_MEMSET(query, 0, sizeof(query));
2418
2419         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
2420
2421         hstmt = _account_prepare_query(g_hAccountDB, query);
2422         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2423                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2424
2425         rc = _account_query_step(hstmt);
2426         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
2427
2428         rc = _account_query_finalize(hstmt);
2429         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2430         hstmt = NULL;
2431
2432         /* delete custom data */
2433         ACCOUNT_MEMSET(query, 0, sizeof(query));
2434
2435         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
2436
2437         hstmt = _account_prepare_query(g_hAccountDB, query);
2438
2439         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2440                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2441
2442         rc = _account_query_step(hstmt);
2443         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
2444
2445         rc = _account_query_finalize(hstmt);
2446         ACCOUNT_CATCH_ERROR(rc == _ACCOUNT_ERROR_NONE, {}, rc, ("finalize error", account_id, rc));
2447         hstmt = NULL;
2448
2449         is_success = TRUE;
2450
2451 CATCH:
2452         if (hstmt != NULL) {
2453                 rc = _account_query_finalize(hstmt);
2454                 if (rc != _ACCOUNT_ERROR_NONE) {
2455                         ACCOUNT_ERROR("rc (%d)", rc);
2456                         is_success = FALSE;
2457                 }
2458
2459                 hstmt = NULL;
2460         }
2461
2462         ret_transaction = _account_end_transaction(g_hAccountDB, is_success);
2463
2464         if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2465                 ACCOUNT_ERROR("account_delete:_account_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
2466         } else {
2467                 if (is_success == true) {
2468                         char buf[64] = {0,};
2469                         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_DELETE, account_id);
2470                         _account_insert_delete_update_notification_send(buf);
2471                 }
2472         }
2473
2474         pthread_mutex_unlock(&account_mutex);
2475
2476         return error_code;
2477 }
2478
2479 static int _account_query_account_by_username_and_package(const char* username, const char* package_name, account_s *account)
2480 {
2481         //FIXME
2482         //return -1;
2483         int                             error_code = _ACCOUNT_ERROR_NONE;
2484         account_stmt    hstmt = NULL;
2485         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2486         int                             rc = 0;
2487         int                             binding_count = 1;
2488
2489         ACCOUNT_RETURN_VAL((username != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("username IS NULL"));
2490         ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name IS NULL"));
2491         ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2492         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2493
2494         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2495
2496         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
2497         hstmt = _account_prepare_query(g_hAccountDB, query);
2498
2499         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2500                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2501                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2502         }
2503
2504         _account_query_bind_text(hstmt, binding_count++, username);
2505         _account_query_bind_text(hstmt, binding_count++, package_name);
2506
2507         rc = _account_query_step(hstmt);
2508         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2509
2510         account_s *account_record = account;
2511
2512         while (rc == SQLITE_ROW) {
2513                 _account_convert_column_to_account(hstmt, account_record);
2514                 rc = _account_query_step(hstmt);
2515         }
2516
2517         rc = _account_query_finalize(hstmt);
2518         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2519         _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, account_record->id, (void*)account_record);
2520         _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, account_record->id, (void*)account_record);
2521
2522         hstmt = NULL;
2523         error_code = _ACCOUNT_ERROR_NONE;
2524
2525 CATCH:
2526         if (hstmt != NULL) {
2527                 rc = _account_query_finalize(hstmt);
2528                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2529                 hstmt = NULL;
2530         }
2531
2532         pthread_mutex_unlock(&account_mutex);
2533
2534         return error_code;
2535 }
2536
2537 int _account_create(account_s **account)
2538 {
2539         if (!account) {
2540                 ACCOUNT_SLOGE("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__);
2541                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2542         }
2543
2544         account_s *data = (account_s*)malloc(sizeof(account_s));
2545
2546         if (data == NULL) {
2547                 ACCOUNT_FATAL("Memory Allocation Failed");
2548                 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
2549         }
2550         ACCOUNT_MEMSET(data, 0, sizeof(account_s));
2551
2552         /*Setting account as visible by default*/
2553         //data->secret = _ACCOUNT_SECRECY_VISIBLE;
2554
2555         /*Setting account as not supporting sync by default*/
2556         //data->sync_support = _ACCOUNT_SYNC_NOT_SUPPORT;
2557
2558         *account = data;
2559
2560         return _ACCOUNT_ERROR_NONE;
2561 }
2562
2563 int _account_destroy(account_s *account)
2564 {
2565         account_s *data = account;
2566
2567         ACCOUNT_RETURN_VAL((data != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Account handle is null!"));
2568
2569         _account_free_account_with_items(data);
2570
2571         return _ACCOUNT_ERROR_NONE;
2572 }
2573
2574 int _account_get_account_id(account_s* account, int *account_id)
2575 {
2576         if (!account)
2577                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2578
2579         if (!account_id)
2580                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2581
2582         *account_id = account->id;
2583
2584         return _ACCOUNT_ERROR_NONE;
2585 }
2586
2587 int _account_delete_from_db_by_user_name(int pid, uid_t uid, const char *user_name, const char *package_name)
2588 {
2589         _INFO("[%s][%s]", user_name, package_name);
2590
2591         int                             error_code = _ACCOUNT_ERROR_NONE;
2592         account_stmt    hstmt = NULL;
2593         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2594         int                             rc = 0;
2595         int                             ret_transaction = 0;
2596         bool                    is_success = FALSE;
2597         account_s               *account = NULL;
2598         int                             binding_count = 1;
2599         int                             account_id = -1;
2600
2601         ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is null!"));
2602         ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
2603         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2604
2605         /* Check permission of requested appid */
2606         char* current_appid = NULL;
2607         char* package_name_temp = NULL;
2608
2609         current_appid = _account_get_current_appid(pid, uid);
2610
2611         package_name_temp = _account_dup_text(package_name);
2612
2613         ACCOUNT_DEBUG("DELETE:user_name[%s],current_appid[%s], package_name[%s]", user_name, current_appid, package_name_temp);
2614
2615         error_code = _account_check_appid_group_with_package_name(current_appid, package_name_temp, uid);
2616
2617         _ACCOUNT_FREE(current_appid);
2618         _ACCOUNT_FREE(package_name_temp);
2619
2620         if (error_code != _ACCOUNT_ERROR_NONE) {
2621                 ACCOUNT_ERROR("No permission to delete\n");
2622                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2623         }
2624
2625         rc = _account_create(&account);
2626         rc = _account_query_account_by_username_and_package(user_name, package_name, account);
2627
2628         _INFO("");
2629
2630         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2631                 _account_destroy(account);
2632                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2633                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2634         }
2635
2636         _INFO("");
2637         account_s* account_data = (account_s*)account;
2638
2639         rc = _account_get_account_id(account_data, &account_id);
2640
2641         rc = _account_destroy(account);
2642
2643         /* transaction control required*/
2644         ret_transaction = _account_begin_transaction(g_hAccountDB);
2645
2646         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2647                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2648                 pthread_mutex_unlock(&account_mutex);
2649                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2650         }
2651
2652         _INFO("");
2653         if (ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY) {
2654                 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
2655                 pthread_mutex_unlock(&account_mutex);
2656                 return _ACCOUNT_ERROR_DATABASE_BUSY;
2657         } else if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2658                 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
2659                 pthread_mutex_unlock(&account_mutex);
2660                 return ret_transaction;
2661         }
2662
2663         /* delete custom data */
2664         ACCOUNT_MEMSET(query, 0, sizeof(query));
2665         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = ?", ACCOUNT_CUSTOM_TABLE);
2666
2667         hstmt = _account_prepare_query(g_hAccountDB, query);
2668
2669         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2670                 _account_end_transaction(g_hAccountDB, FALSE);
2671                 pthread_mutex_unlock(&account_mutex);
2672                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2673                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2674         }
2675
2676         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2677                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2678
2679         _account_query_bind_int(hstmt, binding_count++, account_id);
2680
2681         rc = _account_query_step(hstmt);
2682         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2683
2684         rc = _account_query_finalize(hstmt);
2685         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2686         hstmt = NULL;
2687
2688         /* delete capability */
2689         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", CAPABILITY_TABLE);
2690
2691         hstmt = _account_prepare_query(g_hAccountDB, query);
2692
2693         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2694                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2695
2696         binding_count = 1;
2697         _account_query_bind_text(hstmt, binding_count++, user_name);
2698         _account_query_bind_text(hstmt, binding_count++, package_name);
2699
2700         rc = _account_query_step(hstmt);
2701         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2702
2703         rc = _account_query_finalize(hstmt);
2704         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2705         hstmt = NULL;
2706
2707         ACCOUNT_MEMSET(query, 0, sizeof(query));
2708
2709         _INFO("");
2710         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
2711
2712         hstmt = _account_prepare_query(g_hAccountDB, query);
2713         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2714                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2715
2716         _INFO("");
2717         binding_count = 1;
2718         _account_query_bind_text(hstmt, binding_count++, user_name);
2719         _account_query_bind_text(hstmt, binding_count++, package_name);
2720
2721         rc = _account_query_step(hstmt);
2722         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));
2723
2724         rc = _account_query_finalize(hstmt);
2725         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2726         is_success = TRUE;
2727
2728         hstmt = NULL;
2729
2730 CATCH:
2731         if (hstmt != NULL) {
2732                 rc = _account_query_finalize(hstmt);
2733                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2734                 hstmt = NULL;
2735         }
2736
2737         ret_transaction = _account_end_transaction(g_hAccountDB, is_success);
2738
2739         if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2740                 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
2741         } else {
2742                 if (is_success == true) {
2743                         char buf[64] = {0,};
2744                         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_DELETE, account_id);
2745                         _account_insert_delete_update_notification_send(buf);
2746                 }
2747         }
2748
2749         pthread_mutex_unlock(&account_mutex);
2750
2751         return error_code;
2752 }
2753
2754
2755 int _account_get_total_count_from_db(gboolean include_hidden, int *count)
2756 {
2757         if (!count) {
2758                 ACCOUNT_SLOGE("(%s)-(%d) count is NULL.\n", __FUNCTION__, __LINE__);
2759                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2760         }
2761
2762         if (!g_hAccountDB) {
2763                 ACCOUNT_ERROR("DB is not opened\n");
2764                 return _ACCOUNT_ERROR_DB_NOT_OPENED;
2765         }
2766
2767         char query[1024] = {0, };
2768         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2769
2770         if (include_hidden)
2771                 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s", ACCOUNT_TABLE);
2772         else
2773                 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s where secret = %d", ACCOUNT_TABLE, _ACCOUNT_SECRECY_VISIBLE);
2774
2775         *count = _account_get_record_count(g_hAccountDB, query);
2776
2777         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2778                 pthread_mutex_unlock(&account_mutex);
2779                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2780                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2781         }
2782
2783         int rc = -1;
2784         int ncount = 0;
2785         account_stmt pStmt = NULL;
2786
2787         rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
2788         if (SQLITE_OK != rc) {
2789                 ACCOUNT_SLOGE("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg(g_hAccountDB));
2790                 sqlite3_finalize(pStmt);
2791                 return _ACCOUNT_ERROR_DB_FAILED;
2792         }
2793
2794         rc = sqlite3_step(pStmt);
2795         if (SQLITE_ROW != rc) {
2796                 ACCOUNT_ERROR("[ERROR] sqlite3_step() failed\n");
2797                 sqlite3_finalize(pStmt);
2798                 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
2799         }
2800
2801         ncount = sqlite3_column_int(pStmt, 0);
2802
2803         *count = ncount;
2804
2805         sqlite3_finalize(pStmt);
2806
2807         if (ncount < 0) {
2808                 ACCOUNT_ERROR("[ERROR] Number of account : %d, End", ncount);
2809                 return _ACCOUNT_ERROR_DB_FAILED;
2810         }
2811
2812         return _ACCOUNT_ERROR_NONE;
2813 }
2814
2815
2816 int account_server_query_app_id_exist(const char* app_id)
2817 {
2818         _INFO("account_server_query_app_id_exist start app_id=[%s]", app_id);
2819         int ret = _ACCOUNT_ERROR_NONE;
2820
2821         ret = _account_type_query_app_id_exist_from_all_db(g_hAccountDB, g_hAccountGlobalDB, app_id);
2822
2823         _INFO("account_server_query_app_id_exist end error_code=[%d]", ret);
2824
2825         return ret;
2826 }
2827
2828 int account_server_insert_account_type_to_user_db(account_type_s *account_type, int *account_type_id, uid_t uid)
2829 {
2830         ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE HANDLE IS NULL"));
2831         ACCOUNT_RETURN_VAL((account_type->app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID OF ACCOUNT TYPE IS NULL"));
2832         ACCOUNT_RETURN_VAL((account_type_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE ID IS NULL"));
2833
2834         _INFO("account_server_insert_account_type_to_user_db start uid=[%d]", uid);
2835         int ret = _ACCOUNT_ERROR_NONE;
2836
2837         if (_account_type_check_duplicated(g_hAccountDB, account_type->app_id) ||
2838                         _account_type_check_duplicated(g_hAccountGlobalDB, account_type->app_id)) {
2839                 *account_type_id = -1;
2840                 return _ACCOUNT_ERROR_DUPLICATED;
2841         }
2842
2843         ret = _account_type_insert_to_db(g_hAccountDB, account_type, account_type_id);
2844         _INFO("account_server_insert_account_type_to_user_db end error_code=[%d]", ret);
2845
2846         return ret;
2847 }
2848
2849 int account_server_delete_account_type_by_app_id_from_user_db(const char * app_id)
2850 {
2851         ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID OF ACCOUNT TYPE IS NULL"));
2852
2853         _INFO("account_server_delete_account_type_by_app_id_from_user_db start");
2854         int ret = _ACCOUNT_ERROR_NONE;
2855
2856         ret = _account_type_delete_by_app_id(g_hAccountDB, app_id);
2857         _INFO("account_server_delete_account_type_by_app_id_from_user_db end error_code=[%d]", ret);
2858
2859         return ret;
2860 }
2861
2862 GSList* _account_type_query_provider_feature_by_app_id_from_global_db(const char* app_id, int *error_code)
2863 {
2864         _INFO("_account_type_query_provider_feature_by_app_id_in_global_db app_id=%s", app_id);
2865         account_stmt hstmt = NULL;
2866         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2867         int rc = 0, binding_count = 1;
2868         GSList* feature_list = NULL;
2869
2870         ACCOUNT_RETURN_VAL((app_id != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("APP ID IS NULL"));
2871         ACCOUNT_RETURN_VAL((error_code != NULL), {_ERR("error_code pointer is NULL"); }, NULL, (""));
2872         ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; _ERR("The database isn't connected."); }, NULL, ("The database isn't connected."));
2873
2874         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2875
2876         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
2877         _INFO("account query=[%s]", query);
2878
2879         hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
2880
2881         if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
2882                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2883                 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
2884                 return NULL;
2885         }
2886
2887         _INFO("before _account_query_bind_text");
2888         _account_query_bind_text(hstmt, binding_count++, app_id);
2889
2890         rc = _account_query_step(hstmt);
2891
2892         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"));
2893
2894         provider_feature_s* feature_record = NULL;
2895
2896         while (rc == SQLITE_ROW) {
2897
2898                 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
2899
2900                 if (feature_record == NULL) {
2901                         ACCOUNT_FATAL("malloc Failed");
2902                         break;
2903                 }
2904
2905                 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
2906
2907                 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
2908
2909                 _INFO("Adding account feature_list");
2910                 feature_list = g_slist_append(feature_list, feature_record);
2911
2912                 rc = _account_query_step(hstmt);
2913         }
2914
2915         *error_code = _ACCOUNT_ERROR_NONE;
2916
2917 CATCH:
2918         if (hstmt != NULL) {
2919                 rc = _account_query_finalize(hstmt);
2920                 if (rc != _ACCOUNT_ERROR_NONE) {
2921                         *error_code = rc;
2922                         _ERR("global db fianlize error");
2923                 }
2924         }
2925
2926         if (*error_code != _ACCOUNT_ERROR_NONE)
2927                 _account_type_gslist_feature_free(feature_list);
2928
2929         _INFO("Returning account feature_list from global db");
2930
2931         return feature_list;
2932 }
2933
2934 GSList* _account_type_query_provider_feature_by_app_id(const char* app_id, int *error_code)
2935 {
2936         _INFO("_account_type_query_provider_feature_by_app_id app_id=%s", app_id);
2937         account_stmt hstmt = NULL;
2938         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2939         int rc = 0, binding_count = 1;
2940         GSList* feature_list = NULL;
2941
2942         ACCOUNT_RETURN_VAL((app_id != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("APP ID IS NULL"));
2943         ACCOUNT_RETURN_VAL((error_code != NULL), {_ERR("error_code pointer is NULL"); }, NULL, (""));
2944         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), { *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED; }, NULL, ("The database isn't connected."));
2945
2946         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2947
2948         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
2949         _INFO("account query=[%s]", query);
2950
2951         hstmt = _account_prepare_query(g_hAccountDB, query);
2952
2953         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2954                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2955                 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
2956                 return NULL;
2957         }
2958
2959         _account_query_bind_text(hstmt, binding_count++, app_id);
2960
2961         rc = _account_query_step(hstmt);
2962
2963         ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, { *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND; _ERR("The record isn't found from user db. rc=[%d]", rc); }, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2964
2965         provider_feature_s* feature_record = NULL;
2966
2967         while (rc == SQLITE_ROW) {
2968
2969                 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
2970
2971                 if (feature_record == NULL) {
2972                         ACCOUNT_FATAL("malloc Failed");
2973                         break;
2974                 }
2975
2976                 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
2977
2978                 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
2979
2980                 _INFO("Adding account feature_list");
2981                 feature_list = g_slist_append(feature_list, feature_record);
2982
2983                 rc = _account_query_step(hstmt);
2984         }
2985
2986         *error_code = _ACCOUNT_ERROR_NONE;
2987
2988         rc = _account_query_finalize(hstmt);
2989         ACCOUNT_CATCH_ERROR_P((rc == _ACCOUNT_ERROR_NONE), { *error_code = rc; }, rc, ("account finalize error"));
2990         hstmt = NULL;
2991
2992 CATCH:
2993         if (hstmt != NULL) {
2994                 rc = _account_query_finalize(hstmt);
2995                 if (rc != _ACCOUNT_ERROR_NONE) {
2996                         *error_code = rc;
2997                         _ERR("account fianlize error");
2998                 }
2999                 hstmt = NULL;
3000         }
3001         _INFO("*error_code=[%d]", *error_code);
3002
3003         if (*error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3004                 feature_list = _account_type_query_provider_feature_by_app_id_from_global_db(app_id, error_code);
3005
3006         if (*error_code != _ACCOUNT_ERROR_NONE)
3007                 _account_type_gslist_feature_free(feature_list);
3008
3009         _INFO("Returning account feature_list");
3010
3011         return feature_list;
3012 }
3013
3014 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)
3015 {
3016         int                             error_code = _ACCOUNT_ERROR_NONE;
3017         account_stmt    hstmt = NULL;
3018         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3019         int                             rc = 0, binding_count = 1;
3020
3021         _INFO("_account_type_query_provider_feature_cb_by_app_id_in_global_db start app_id=%s", app_id);
3022         ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3023         ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3024         ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3025
3026         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3027
3028         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
3029         hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3030
3031         if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3032                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3033                 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_PERMISSION_DENIED, ("global db permission denied.\n"));
3034         }
3035
3036         _account_query_bind_text(hstmt, binding_count++, app_id);
3037
3038         rc = _account_query_step(hstmt);
3039         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"));
3040
3041         provider_feature_s* feature_record = NULL;
3042
3043         while (rc == SQLITE_ROW) {
3044                 bool cb_ret = FALSE;
3045                 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
3046
3047                 if (feature_record == NULL) {
3048                         ACCOUNT_FATAL("malloc Failed");
3049                         break;
3050                 }
3051
3052                 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
3053
3054                 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
3055
3056                 cb_ret = callback(feature_record->app_id, feature_record->key, user_data);
3057
3058                 _account_type_free_feature_with_items(feature_record);
3059
3060                 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returns FALSE, its iteration is stopped!!!!\n"));
3061
3062                 rc = _account_query_step(hstmt);
3063         }
3064
3065         error_code = _ACCOUNT_ERROR_NONE;
3066
3067 CATCH:
3068         if (hstmt != NULL) {
3069                 rc = _account_query_finalize(hstmt);
3070                 if (rc != _ACCOUNT_ERROR_NONE) {
3071                         error_code = rc;
3072                         _ERR("global db finalize error[%d]", rc);
3073                 }
3074                 hstmt = NULL;
3075         }
3076
3077         _INFO("_account_type_query_provider_feature_cb_by_app_id_in_global_db end. error_code=[%d]", error_code);
3078
3079         return error_code;
3080 }
3081
3082 int _account_type_query_provider_feature_cb_by_app_id(account_type_provider_feature_cb callback, const char* app_id, void *user_data)
3083 {
3084         int                             error_code = _ACCOUNT_ERROR_NONE;
3085         account_stmt    hstmt = NULL;
3086         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3087         int                             rc = 0, binding_count = 1;
3088
3089         _INFO("_account_type_query_provider_feature_cb_by_app_id start app_id=%s", app_id);
3090         ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3091         ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3092         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3093
3094         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3095
3096         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
3097         hstmt = _account_prepare_query(g_hAccountDB, query);
3098
3099         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
3100                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3101                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3102         }
3103
3104         _account_query_bind_text(hstmt, binding_count++, app_id);
3105
3106         rc = _account_query_step(hstmt);
3107         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found in user db.\n"));
3108
3109         provider_feature_s* feature_record = NULL;
3110
3111         while (rc == SQLITE_ROW) {
3112                 bool cb_ret = FALSE;
3113                 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
3114
3115                 if (feature_record == NULL) {
3116                         ACCOUNT_FATAL("malloc Failed");
3117                         break;
3118                 }
3119
3120                 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
3121
3122                 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
3123
3124                 cb_ret = callback(feature_record->app_id, feature_record->key, user_data);
3125
3126                 _account_type_free_feature_with_items(feature_record);
3127
3128                 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
3129
3130                 rc = _account_query_step(hstmt);
3131         }
3132
3133         rc = _account_query_finalize(hstmt);
3134         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3135         hstmt = NULL;
3136
3137         error_code = _ACCOUNT_ERROR_NONE;
3138
3139 CATCH:
3140         if (hstmt != NULL) {
3141                 rc = _account_query_finalize(hstmt);
3142                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3143                 hstmt = NULL;
3144         }
3145 /*
3146         if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3147                 error_code = _account_type_query_provider_feature_cb_by_app_id_from_global_db(callback, app_id, user_data);
3148 */
3149         _INFO("_account_type_query_provider_feature_cb_by_app_id end");
3150
3151         return error_code;
3152 }
3153
3154 int account_type_query_provider_feature_cb_by_app_id(account_type_provider_feature_cb callback, const char* app_id, void *user_data)
3155 {
3156         int                             error_code = _ACCOUNT_ERROR_NONE;
3157
3158         ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3159         ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3160         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3161
3162         error_code = _account_type_query_provider_feature_cb_by_app_id(callback, app_id, user_data);
3163
3164         if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3165                 error_code = _account_type_query_provider_feature_cb_by_app_id_from_global_db(callback, app_id, user_data);
3166
3167         return error_code;
3168 }
3169
3170 bool _account_type_query_supported_feature_from_global_db(const char* app_id, const char* capability, int *error_code)
3171 {
3172         _INFO("_account_type_query_supported_feature_in_global_db start");
3173         ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3174
3175         *error_code = _ACCOUNT_ERROR_NONE;
3176
3177         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3178         int record_count = 0;
3179
3180         if (app_id == NULL || capability == NULL) {
3181                 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
3182                 return false;
3183         }
3184
3185         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
3186
3187         record_count = _account_get_record_count(g_hAccountGlobalDB, query);
3188
3189         if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3190                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3191                 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3192         }
3193
3194         if (record_count <= 0) {
3195                 *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
3196                 return false;
3197         }
3198
3199         _INFO("_account_type_query_supported_feature_in_global_db end");
3200
3201         return true;
3202 }
3203
3204 bool _account_type_query_supported_feature(const char* app_id, const char* capability, int *error_code)
3205 {
3206         _INFO("_account_type_query_supported_feature start");
3207
3208         *error_code = _ACCOUNT_ERROR_NONE;
3209
3210         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3211         int                             record_count = 0;
3212
3213         if (app_id == NULL || capability == NULL) {
3214                 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
3215                 return false;
3216         }
3217
3218         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
3219
3220         record_count = _account_get_record_count(g_hAccountDB, query);
3221
3222         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
3223                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3224                 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3225                 return false;
3226         }
3227
3228         if (record_count <= 0) {
3229                 bool is_exist = false;
3230                 is_exist = _account_type_query_supported_feature_from_global_db(app_id, capability, error_code);
3231                 if (!is_exist)
3232                         return false;
3233         }
3234
3235         _INFO("_account_type_query_supported_feature end");
3236
3237         return true;
3238 }
3239
3240
3241 static int _account_type_update_provider_feature(sqlite3 *account_db_handle, account_type_s *account_type, const char* app_id)
3242 {
3243         int                             rc, count = 1;
3244         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3245         account_stmt    hstmt = NULL;
3246
3247         ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
3248
3249         if (g_slist_length(account_type->provider_feature_list) == 0) {
3250                 ACCOUNT_ERROR("no feature\n");
3251                 return _ACCOUNT_ERROR_NONE;
3252         }
3253
3254         ACCOUNT_DEBUG("app id", app_id);
3255
3256         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3257
3258         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id=? ", PROVIDER_FEATURE_TABLE);
3259         hstmt = _account_prepare_query(account_db_handle, query);
3260
3261         if (_account_db_err_code(account_db_handle) == SQLITE_PERM) {
3262                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(account_db_handle));
3263                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3264         }
3265
3266         count = 1;
3267         _account_query_bind_text(hstmt, count++, app_id);
3268         rc = _account_query_step(hstmt);
3269
3270         if (rc != SQLITE_DONE) {
3271                 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3272                 return _ACCOUNT_ERROR_DB_FAILED;
3273         }
3274         rc = _account_query_finalize(hstmt);
3275         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3276         hstmt = NULL;
3277
3278         GSList *iter;
3279
3280         for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
3281                 int ret;
3282                 count = 1;
3283                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3284                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
3285                                 "(?, ?) ", PROVIDER_FEATURE_TABLE);
3286
3287                 hstmt = _account_prepare_query(account_db_handle, query);
3288
3289                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg(account_db_handle)));
3290
3291                 provider_feature_s* feature_data = NULL;
3292                 feature_data = (provider_feature_s*)iter->data;
3293
3294                 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
3295                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3296                 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
3297                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3298
3299                 rc = _account_query_step(hstmt);
3300
3301                 if (rc != SQLITE_DONE) {
3302                         ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3303                         break;
3304                 }
3305                 rc = _account_query_finalize(hstmt);
3306                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3307                 hstmt = NULL;
3308         }
3309
3310         return _ACCOUNT_ERROR_NONE;
3311 }
3312
3313 static int _account_type_update_label(sqlite3 *account_db_handle, account_type_s *account_type, const char* app_id)
3314 {
3315         int                             rc, count = 1;
3316         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3317         account_stmt    hstmt = NULL;
3318
3319         ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
3320
3321         if (g_slist_length(account_type->label_list) == 0)
3322                 return _ACCOUNT_ERROR_NONE;
3323
3324         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3325
3326         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId=? ", LABEL_TABLE);
3327         hstmt = _account_prepare_query(account_db_handle, query);
3328
3329         if (_account_db_err_code(account_db_handle) == SQLITE_PERM) {
3330                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(account_db_handle));
3331                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3332         }
3333
3334         count = 1;
3335         _account_query_bind_text(hstmt, count++, app_id);
3336         rc = _account_query_step(hstmt);
3337
3338         if (rc != SQLITE_DONE) {
3339                 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3340                 return _ACCOUNT_ERROR_DB_FAILED;
3341         }
3342         rc = _account_query_finalize(hstmt);
3343         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3344         hstmt = NULL;
3345
3346         GSList *iter;
3347
3348         for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
3349                 int ret;
3350                 count = 1;
3351                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3352                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
3353                                 "(?, ?, ?) ", LABEL_TABLE);
3354
3355                 hstmt = _account_prepare_query(account_db_handle, query);
3356
3357                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg(account_db_handle)));
3358
3359                 label_s* label_data = NULL;
3360                 label_data = (label_s*)iter->data;
3361
3362                 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
3363                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3364                 ret = _account_query_bind_text(hstmt, count++, label_data->label);
3365                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3366                 ret = _account_query_bind_text(hstmt, count++, label_data->locale);
3367                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3368
3369                 rc = _account_query_step(hstmt);
3370
3371                 if (rc != SQLITE_DONE) {
3372                         ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3373                         break;
3374                 }
3375                 rc = _account_query_finalize(hstmt);
3376                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3377                 hstmt = NULL;
3378         }
3379
3380         return _ACCOUNT_ERROR_NONE;
3381 }
3382
3383
3384 static int _account_type_update_account(sqlite3 *account_db_handle, account_type_s *account_type, const char* app_id)
3385 {
3386         int                             rc = 0, binding_count = 1;
3387         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3388         int                             error_code = _ACCOUNT_ERROR_NONE;
3389         account_stmt    hstmt = NULL;
3390
3391         if (!account_type->app_id) {
3392                 ACCOUNT_ERROR("app id is mandetory field, it can not be NULL!!!!\n");
3393                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
3394         }
3395
3396         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3397         ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET AppId=?, ServiceProviderId=?, IconPath=?, "
3398                         "SmallIconPath=?, MultipleAccountSupport=? WHERE AppId=? ", ACCOUNT_TYPE_TABLE);
3399
3400         hstmt = _account_prepare_query(account_db_handle, query);
3401
3402         if (_account_db_err_code(account_db_handle) == SQLITE_PERM) {
3403                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(account_db_handle));
3404                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3405         } else if (_account_db_err_code(account_db_handle) == SQLITE_BUSY) {
3406                 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(account_db_handle));
3407                 return _ACCOUNT_ERROR_DATABASE_BUSY;
3408         }
3409
3410         ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg(account_db_handle)));
3411
3412         binding_count = _account_type_convert_account_to_sql(account_type, hstmt, query);
3413         _account_query_bind_text(hstmt, binding_count++, app_id);
3414
3415         rc = _account_query_step(hstmt);
3416         if (rc != SQLITE_DONE)
3417                 ACCOUNT_ERROR("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3418
3419         rc = _account_query_finalize(hstmt);
3420         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3421         hstmt = NULL;
3422
3423         /*update label*/
3424         error_code = _account_type_update_label(account_db_handle, account_type, app_id);
3425         /* update provider feature */
3426         error_code = _account_type_update_provider_feature(account_db_handle, account_type, app_id);
3427
3428         return error_code;
3429 }
3430
3431 int _account_type_update_to_db_by_app_id(account_type_s* account_type, const char* app_id)
3432 {
3433         int     error_code = _ACCOUNT_ERROR_NONE;
3434
3435         ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
3436         ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3437         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3438
3439         account_type_s* data = account_type;
3440
3441         pthread_mutex_lock(&account_mutex);
3442
3443         error_code = _account_type_update_account(g_hAccountDB, data, app_id);
3444
3445         pthread_mutex_unlock(&account_mutex);
3446
3447         return error_code;
3448 }
3449
3450 GSList* _account_type_get_label_list_by_app_id_from_global_db(const char* app_id, int *error_code)
3451 {
3452         *error_code = _ACCOUNT_ERROR_NONE;
3453         account_stmt    hstmt = NULL;
3454         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3455         int                             rc = 0, binding_count = 1;
3456         GSList* label_list = NULL;
3457
3458         ACCOUNT_RETURN_VAL((app_id != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("APP ID IS NULL"));
3459         ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), { *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED; }, NULL, ("The database isn't connected."));
3460
3461         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3462
3463         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3464         hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3465
3466         if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3467                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3468                 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3469
3470                 goto CATCH;
3471         }
3472
3473         _account_query_bind_text(hstmt, binding_count++, app_id);
3474
3475         rc = _account_query_step(hstmt);
3476         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"));
3477
3478         label_s* label_record = NULL;
3479
3480         while (rc == SQLITE_ROW) {
3481                 label_record = (label_s*) malloc(sizeof(label_s));
3482
3483                 if (label_record == NULL) {
3484                         ACCOUNT_FATAL("malloc Failed");
3485                         break;
3486                 }
3487
3488                 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3489
3490                 _account_type_convert_column_to_label(hstmt, label_record);
3491
3492                 _INFO("Adding account label_list");
3493                 label_list = g_slist_append(label_list, label_record);
3494
3495                 rc = _account_query_step(hstmt);
3496         }
3497
3498         *error_code = _ACCOUNT_ERROR_NONE;
3499
3500 CATCH:
3501         if (hstmt != NULL) {
3502                 rc = _account_query_finalize(hstmt);
3503                 if (rc != _ACCOUNT_ERROR_NONE)
3504                         _ERR("global db finalize error[%d]", rc);
3505                 hstmt = NULL;
3506         }
3507
3508         _INFO("Returning account global label_list");
3509
3510         return label_list;
3511 }
3512
3513 GSList* _account_type_get_label_list_by_app_id(const char* app_id, int *error_code)
3514 {
3515         *error_code = _ACCOUNT_ERROR_NONE;
3516         account_stmt    hstmt = NULL;
3517         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3518         int                             rc = 0, binding_count = 1;
3519         GSList* label_list = NULL;
3520
3521         ACCOUNT_RETURN_VAL((app_id != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("APP ID IS NULL"));
3522         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), { *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED; }, NULL, ("The database isn't connected."));
3523
3524         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3525
3526         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3527         hstmt = _account_prepare_query(g_hAccountDB, query);
3528
3529         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
3530                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3531                 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3532                 return NULL;
3533         }
3534
3535         _account_query_bind_text(hstmt, binding_count++, app_id);
3536
3537         rc = _account_query_step(hstmt);
3538         ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3539
3540         label_s* label_record = NULL;
3541
3542         while (rc == SQLITE_ROW) {
3543                 label_record = (label_s*) malloc(sizeof(label_s));
3544
3545                 if (label_record == NULL) {
3546                         ACCOUNT_FATAL("malloc Failed");
3547                         break;
3548                 }
3549
3550                 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3551
3552                 _account_type_convert_column_to_label(hstmt, label_record);
3553
3554                 _INFO("Adding account label_list");
3555                 label_list = g_slist_append(label_list, label_record);
3556
3557                 rc = _account_query_step(hstmt);
3558         }
3559
3560         rc = _account_query_finalize(hstmt);
3561         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), { *error_code = rc; }, NULL, ("finalize error"));
3562         hstmt = NULL;
3563
3564         *error_code = _ACCOUNT_ERROR_NONE;
3565
3566 CATCH:
3567         if (hstmt != NULL) {
3568                 rc = _account_query_finalize(hstmt);
3569                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), { *error_code = rc; }, NULL, ("finalize error"));
3570                 hstmt = NULL;
3571         }
3572
3573         if (*error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3574                 label_list = _account_type_get_label_list_by_app_id_from_global_db(app_id, error_code);
3575
3576         _INFO("Returning account label_list");
3577
3578         return label_list;
3579 }
3580
3581 int _account_type_query_label_by_app_id_from_global_db(account_type_label_cb callback, const char* app_id, void *user_data)
3582 {
3583         int                             error_code = _ACCOUNT_ERROR_NONE;
3584         account_stmt    hstmt = NULL;
3585         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3586         int                             rc = 0, binding_count = 1;
3587
3588         _INFO("account_type_query_label_by_app_id_from_global_db start");
3589
3590         ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3591         ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3592         ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3593
3594         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3595
3596         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3597         hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3598
3599         if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3600                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3601                 error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3602                 goto CATCH;
3603         }
3604
3605         _account_query_bind_text(hstmt, binding_count++, app_id);
3606
3607         rc = _account_query_step(hstmt);
3608         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3609
3610         label_s* label_record = NULL;
3611
3612         while (rc == SQLITE_ROW) {
3613                 bool cb_ret = FALSE;
3614                 label_record = (label_s*) malloc(sizeof(label_s));
3615
3616                 if (label_record == NULL) {
3617                         ACCOUNT_FATAL("malloc Failed");
3618                         break;
3619                 }
3620
3621                 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3622
3623                 _account_type_convert_column_to_label(hstmt, label_record);
3624
3625                 cb_ret = callback(label_record->app_id, label_record->label , label_record->locale, user_data);
3626
3627                 _account_type_free_label_with_items(label_record);
3628
3629                 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
3630
3631                 rc = _account_query_step(hstmt);
3632         }
3633
3634         error_code = _ACCOUNT_ERROR_NONE;
3635
3636 CATCH:
3637         if (hstmt != NULL) {
3638                 rc = _account_query_finalize(hstmt);
3639                 if (rc != _ACCOUNT_ERROR_NONE)
3640                         _ERR("global db finalize error[%d]", rc);
3641                 hstmt = NULL;
3642         }
3643
3644         _INFO("account_type_query_label_by_app_id_from_global_db end [%d]", error_code);
3645
3646         return error_code;
3647 }
3648
3649 int _account_type_query_label_by_app_id(account_type_label_cb callback, const char* app_id, void *user_data)
3650 {
3651         int                             error_code = _ACCOUNT_ERROR_NONE;
3652         account_stmt    hstmt = NULL;
3653         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3654         int                             rc = 0, binding_count = 1;
3655
3656         ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3657         ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3658         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3659
3660         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3661
3662         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3663         hstmt = _account_prepare_query(g_hAccountDB, query);
3664
3665         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
3666                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3667                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3668         }
3669
3670         _account_query_bind_text(hstmt, binding_count++, app_id);
3671
3672         rc = _account_query_step(hstmt);
3673         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3674
3675         label_s* label_record = NULL;
3676
3677         while (rc == SQLITE_ROW) {
3678                 bool cb_ret = FALSE;
3679                 label_record = (label_s*) malloc(sizeof(label_s));
3680
3681                 if (label_record == NULL) {
3682                         ACCOUNT_FATAL("malloc Failed");
3683                         break;
3684                 }
3685
3686                 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3687
3688                 _account_type_convert_column_to_label(hstmt, label_record);
3689
3690                 cb_ret = callback(label_record->app_id, label_record->label , label_record->locale, user_data);
3691
3692                 _account_type_free_label_with_items(label_record);
3693
3694                 //ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
3695                 if (cb_ret != TRUE) {
3696                         _INFO("Callback func returs FALSE, its iteration is stopped!!!!\n");
3697                         break;
3698                 }
3699
3700                 rc = _account_query_step(hstmt);
3701         }
3702
3703         rc = _account_query_finalize(hstmt);
3704         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3705         hstmt = NULL;
3706
3707         error_code = _ACCOUNT_ERROR_NONE;
3708
3709 CATCH:
3710         if (hstmt != NULL) {
3711                 rc = _account_query_finalize(hstmt);
3712                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3713                 hstmt = NULL;
3714         }
3715 /*
3716         if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
3717                 error_code = account_type_query_label_by_app_id_from_global_db(callback, app_id, user_data);
3718         }
3719 */
3720         return error_code;
3721 }
3722
3723 int account_type_query_label_by_app_id(account_type_label_cb callback, const char* app_id, void *user_data)
3724 {
3725         int error_code = _ACCOUNT_ERROR_NONE;
3726
3727         ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3728         ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3729         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3730
3731         error_code = _account_type_query_label_by_app_id(callback, app_id, user_data);
3732
3733         if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3734                 error_code = _account_type_query_label_by_app_id_from_global_db(callback, app_id, user_data);
3735
3736         return error_code;
3737 }
3738
3739 bool _account_get_label_text_cb(char* app_id, char* label, char* locale, void *user_data)
3740 {
3741         account_type_s *data = (account_type_s*)user_data;
3742
3743         label_s *label_data = (label_s*)malloc(sizeof(label_s));
3744
3745         if (label_data == NULL) {
3746                 ACCOUNT_FATAL("_account_get_label_text_cb : MALLOC FAIL\n");
3747                 return FALSE;
3748         }
3749         ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
3750
3751         label_data->app_id = _account_dup_text(app_id);
3752         label_data->label = _account_dup_text(label);
3753         label_data->locale = _account_dup_text(locale);
3754
3755         data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
3756
3757         return TRUE;
3758 }
3759
3760 bool _account_get_provider_feature_cb(char* app_id, char* key, void* user_data)
3761 {
3762         account_type_s *data = (account_type_s*)user_data;
3763
3764         provider_feature_s *feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
3765
3766         if (feature_data == NULL) {
3767                 ACCOUNT_FATAL("_account_get_provider_feature_cb : MALLOC FAIL\n");
3768                 return FALSE;
3769         }
3770         ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
3771
3772         feature_data->app_id = _account_dup_text(app_id);
3773         feature_data->key = _account_dup_text(key);
3774
3775         data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
3776
3777         return TRUE;
3778 }
3779
3780 int _account_type_query_by_app_id_from_global_db(const char* app_id, account_type_s** account_type_record)
3781 {
3782         _INFO("_account_type_query_by_app_id_from_global_db start");
3783
3784         int                             error_code = _ACCOUNT_ERROR_NONE;
3785         account_stmt    hstmt = NULL;
3786         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3787         int                             rc = 0, binding_count = 1;
3788
3789         ACCOUNT_RETURN_VAL((app_id != 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3790         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3791         ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3792
3793         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3794
3795         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
3796         hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3797
3798         if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3799                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3800                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3801         }
3802
3803         _account_query_bind_text(hstmt, binding_count++, app_id);
3804
3805         rc = _account_query_step(hstmt);
3806         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3807
3808         *account_type_record = create_empty_account_type_instance();
3809
3810         while (rc == SQLITE_ROW) {
3811                 _account_type_convert_column_to_account_type(hstmt, *account_type_record);
3812                 rc = _account_query_step(hstmt);
3813         }
3814
3815         rc = _account_query_finalize(hstmt);
3816         ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {_ERR("global db finalize error rc=[%d]", rc); }, rc, ("finalize error"));
3817         _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb, app_id, (void*)(*account_type_record));
3818         _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, app_id, (void*)(*account_type_record));
3819
3820         hstmt = NULL;
3821         error_code = _ACCOUNT_ERROR_NONE;
3822
3823 CATCH:
3824         if (hstmt != NULL) {
3825                 rc = _account_query_finalize(hstmt);
3826                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3827                 hstmt = NULL;
3828         }
3829
3830         _INFO("_account_type_query_by_app_id_from_global_db end [%d]", error_code);
3831
3832         return error_code;
3833 }
3834
3835 int _account_type_query_by_app_id(const char* app_id, account_type_s** account_type_record)
3836 {
3837         _INFO("_account_type_query_by_app_id start");
3838
3839         int                             error_code = _ACCOUNT_ERROR_NONE;
3840         account_stmt    hstmt = NULL;
3841         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3842         int                             rc = 0, binding_count = 1;
3843
3844         ACCOUNT_RETURN_VAL((app_id != 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3845         ACCOUNT_RETURN_VAL((account_type_record != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("account type record(account_type_s**) is NULL"));
3846         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3847
3848         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3849
3850         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
3851         hstmt = _account_prepare_query(g_hAccountDB, query);
3852
3853         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
3854                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3855                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3856         }
3857
3858         _account_query_bind_text(hstmt, binding_count++, app_id);
3859
3860         rc = _account_query_step(hstmt);
3861         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3862
3863         *account_type_record = create_empty_account_type_instance();
3864         if (*account_type_record == NULL) {
3865                 _ERR("Out of Memory");
3866                 error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
3867                 goto CATCH;
3868         }
3869
3870         while (rc == SQLITE_ROW) {
3871                 _account_type_convert_column_to_account_type(hstmt, *account_type_record);
3872                 rc = _account_query_step(hstmt);
3873         }
3874
3875         rc = _account_query_finalize(hstmt);
3876         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3877         _account_type_query_label_by_app_id(_account_get_label_text_cb, app_id, (void*)(*account_type_record));
3878         _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, app_id, (void*)(*account_type_record));
3879
3880         hstmt = NULL;
3881         error_code = _ACCOUNT_ERROR_NONE;
3882
3883 CATCH:
3884         if (hstmt != NULL) {
3885                 rc = _account_query_finalize(hstmt);
3886                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3887                 hstmt = NULL;
3888         }
3889
3890         if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3891                 error_code = _account_type_query_by_app_id_from_global_db(app_id, account_type_record);
3892
3893         _INFO("_account_type_query_by_app_id end [%d]", error_code);
3894
3895         return error_code;
3896 }
3897
3898 int _account_type_query_by_provider_feature_from_global_db(const char* key, GSList **account_type_list_all)
3899 {
3900         int error_code = _ACCOUNT_ERROR_NONE;
3901         account_stmt    hstmt = NULL;
3902         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3903         int                             rc = 0;
3904         GSList                  *account_type_list = NULL;
3905
3906         _INFO("_account_type_query_by_provider_feature_from_global_db start key=%s", key);
3907         if (key == NULL) {
3908                 ACCOUNT_ERROR("capability_type IS NULL.");
3909                 error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
3910                 goto CATCH;
3911         }
3912
3913         if (g_hAccountGlobalDB == NULL) {
3914                 ACCOUNT_ERROR("The database isn't connected.");
3915                 error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;
3916                 goto CATCH;
3917         }
3918
3919         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3920
3921         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
3922
3923         hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3924
3925         if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3926                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3927                 error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3928                 goto CATCH;
3929         }
3930
3931         int binding_count = 1;
3932         _account_query_bind_text(hstmt, binding_count++, key);
3933
3934         rc = _account_query_step(hstmt);
3935
3936         account_type_s *account_type_record = NULL;
3937
3938         if (rc != SQLITE_ROW) {
3939                 ACCOUNT_ERROR("The record isn't found. rc=[%d]", rc);
3940                 error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
3941                 goto CATCH;
3942         }
3943
3944         while (rc == SQLITE_ROW) {
3945                 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
3946
3947                 if (account_type_record == NULL) {
3948                         ACCOUNT_FATAL("malloc Failed");
3949                         break;
3950                 }
3951
3952                 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
3953                 _account_type_convert_column_to_account_type(hstmt, account_type_record);
3954                 account_type_list = g_slist_append(account_type_list, account_type_record);
3955                 rc = _account_query_step(hstmt);
3956         }
3957
3958         rc = _account_query_finalize(hstmt);
3959         if (rc != _ACCOUNT_ERROR_NONE) {
3960                 _account_type_gslist_account_type_free(account_type_list);
3961                 ACCOUNT_ERROR("finalize error(%s)", rc);
3962                 error_code = rc;
3963                 goto CATCH;
3964         }
3965         hstmt = NULL;
3966
3967         GSList* iter;
3968
3969         for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
3970                 account_type_s *account_type = NULL;
3971                 account_type = (account_type_s*)iter->data;
3972                 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb, account_type->app_id, (void*)account_type);
3973                 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, account_type->app_id, (void*)account_type);
3974                 _INFO("add label & provider_feature");
3975         }
3976
3977         for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
3978
3979                 account_type_s *account_type = NULL;
3980                 account_type = (account_type_s*)iter->data;
3981                 *account_type_list_all = g_slist_append(*account_type_list_all, account_type);
3982                 _INFO("add account_type");
3983         }
3984         g_slist_free(account_type_list);
3985
3986         error_code = _ACCOUNT_ERROR_NONE;
3987
3988 CATCH:
3989         if (hstmt != NULL) {
3990                 rc = _account_query_finalize(hstmt);
3991                 if (rc != _ACCOUNT_ERROR_NONE) {
3992                         ACCOUNT_ERROR("finalize error(%s)", rc);
3993                         return rc;
3994                 }
3995                 hstmt = NULL;
3996         }
3997
3998         _INFO("_account_type_query_by_provider_feature_from_global_db end. error_code=[%d]", error_code);
3999
4000         return error_code;
4001 }
4002
4003 GSList* _account_type_query_by_provider_feature(const char* key, int *error_code)
4004 {
4005         *error_code = _ACCOUNT_ERROR_NONE;
4006         account_stmt hstmt = NULL;
4007         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4008         int rc = 0;
4009         GSList *account_type_list = NULL;
4010
4011         _INFO("account_type_query_by_provider_feature start key=%s", key);
4012         if (key == NULL) {
4013                 ACCOUNT_ERROR("capability_type IS NULL.");
4014                 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
4015                 goto CATCH;
4016         }
4017
4018         if (g_hAccountDB == NULL) {
4019                 ACCOUNT_ERROR("The database isn't connected.");
4020                 *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;
4021                 goto CATCH;
4022         }
4023
4024         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4025
4026         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
4027
4028         hstmt = _account_prepare_query(g_hAccountDB, query);
4029
4030         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4031                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4032                 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
4033                 goto CATCH;
4034         }
4035
4036         int binding_count = 1;
4037         _account_query_bind_text(hstmt, binding_count++, key);
4038
4039         rc = _account_query_step(hstmt);
4040
4041         account_type_s *account_type_record = NULL;
4042
4043         if (rc != SQLITE_ROW) {
4044                 ACCOUNT_ERROR("The record isn't found. rc=[%d]", rc);
4045                 *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4046                 goto CATCH;
4047         }
4048
4049         while (rc == SQLITE_ROW) {
4050                 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4051
4052                 if (account_type_record == NULL) {
4053                         ACCOUNT_FATAL("malloc Failed");
4054                         break;
4055                 }
4056
4057                 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4058                 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4059                 account_type_list = g_slist_append(account_type_list, account_type_record);
4060                 rc = _account_query_step(hstmt);
4061         }
4062
4063         rc = _account_query_finalize(hstmt);
4064         if (rc != _ACCOUNT_ERROR_NONE) {
4065                 _account_type_gslist_account_type_free(account_type_list);
4066                 ACCOUNT_ERROR("finalize error(%s)", rc);
4067                 *error_code = rc;
4068                 goto CATCH;
4069         }
4070         hstmt = NULL;
4071
4072         GSList* iter;
4073
4074         for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4075                 account_type_s *account_type = NULL;
4076                 account_type = (account_type_s*)iter->data;
4077                 _account_type_query_label_by_app_id(_account_get_label_text_cb, account_type->app_id, (void*)account_type);
4078                 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, account_type->app_id, (void*)account_type);
4079         }
4080
4081         *error_code = _ACCOUNT_ERROR_NONE;
4082
4083 CATCH:
4084         if (hstmt != NULL) {
4085                 rc = _account_query_finalize(hstmt);
4086                 if (rc != _ACCOUNT_ERROR_NONE) {
4087                         *error_code = rc;
4088                         return NULL;
4089                 }
4090                 hstmt = NULL;
4091         }
4092
4093         if (*error_code == _ACCOUNT_ERROR_NONE || *error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4094                 rc = _account_type_query_by_provider_feature_from_global_db(key, &account_type_list);
4095                 if (rc != _ACCOUNT_ERROR_NONE && rc != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4096                         ACCOUNT_ERROR("_account_type_query_by_provider_feature_from_global_db fail=[%d]", rc);
4097                         _account_type_gslist_account_type_free(account_type_list);
4098                         return NULL;
4099                 }
4100                 if (rc == _ACCOUNT_ERROR_NONE)
4101                         *error_code = rc;
4102         }
4103
4104         _INFO("account_type_query_by_provider_feature end");
4105
4106         return account_type_list;
4107 }
4108
4109 int _account_type_query_all_from_global_db(GSList **account_type_list_all)
4110 {
4111         account_stmt    hstmt = NULL;
4112         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4113         int rc = _ACCOUNT_ERROR_NONE;
4114         int error_code = _ACCOUNT_ERROR_NONE;
4115         GSList *account_type_list = NULL;
4116
4117         _INFO("_account_type_query_all_in_global_db start");
4118         ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4119
4120         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4121
4122         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
4123         hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
4124
4125         rc = _account_query_step(hstmt);
4126
4127         if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
4128                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
4129                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4130         }
4131
4132         account_type_s *account_type_record = NULL;
4133
4134         if (rc != SQLITE_ROW) {
4135                 _INFO("[_ACCOUNT_ERROR_RECORD_NOT_FOUND]The record isn't found.");
4136                 error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4137                 goto CATCH;
4138         }
4139
4140         while (rc == SQLITE_ROW) {
4141                 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4142
4143                 if (account_type_record == NULL) {
4144                         ACCOUNT_FATAL("malloc Failed");
4145                         break;
4146                 }
4147
4148                 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4149                 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4150                 account_type_list = g_slist_append(account_type_list, account_type_record);
4151                 rc = _account_query_step(hstmt);
4152         }
4153
4154         rc = _account_query_finalize(hstmt);
4155         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4156         hstmt = NULL;
4157
4158         GSList* iter;
4159
4160         for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4161                 account_type_s *account_type = NULL;
4162                 account_type = (account_type_s*)iter->data;
4163                 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb, account_type->app_id, (void*)account_type);
4164                 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, account_type->app_id, (void*)account_type);
4165         }
4166
4167         for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4168                 account_type_s *account_type = NULL;
4169                 account_type = (account_type_s*)iter->data;
4170                 *account_type_list_all = g_slist_append(*account_type_list_all, account_type);
4171         }
4172         g_slist_free(account_type_list);
4173
4174         error_code = _ACCOUNT_ERROR_NONE;
4175
4176 CATCH:
4177         if (hstmt != NULL) {
4178                 rc = _account_query_finalize(hstmt);
4179                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list); }, rc, ("finalize error"));
4180                 hstmt = NULL;
4181         }
4182
4183         _INFO("_account_type_query_all_in_global_db end");
4184
4185         return error_code;
4186 }
4187
4188 GSList* _account_type_query_all(void)
4189 {
4190         account_stmt hstmt = NULL;
4191         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4192         int rc = 0;
4193         int error_code = _ACCOUNT_ERROR_NONE;
4194         GSList *account_type_list = NULL;
4195
4196         _INFO("_account_type_query_all start");
4197         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected."));
4198
4199         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4200
4201         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
4202         hstmt = _account_prepare_query(g_hAccountDB, query);
4203
4204         rc = _account_query_step(hstmt);
4205
4206         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4207                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4208                 return NULL;
4209         }
4210
4211         account_type_s *account_type_record = NULL;
4212
4213         if (rc != SQLITE_ROW) {
4214                 _INFO("[_ACCOUNT_ERROR_RECORD_NOT_FOUND]The record isn't found.");
4215                 error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4216                 goto CATCH;
4217         }
4218
4219         while (rc == SQLITE_ROW) {
4220                 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4221
4222                 if (account_type_record == NULL) {
4223                         ACCOUNT_FATAL("malloc Failed");
4224                         break;
4225                 }
4226
4227                 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4228                 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4229                 account_type_list = g_slist_append(account_type_list, account_type_record);
4230                 rc = _account_query_step(hstmt);
4231         }
4232
4233         rc = _account_query_finalize(hstmt);
4234         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, NULL, ("finalize error"));
4235         hstmt = NULL;
4236
4237         GSList* iter;
4238
4239         for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4240                 account_type_s *account_type = NULL;
4241                 account_type = (account_type_s*)iter->data;
4242                 _account_type_query_label_by_app_id(_account_get_label_text_cb, account_type->app_id, (void*)account_type);
4243                 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, account_type->app_id, (void*)account_type);
4244         }
4245
4246         error_code = _ACCOUNT_ERROR_NONE;
4247
4248 CATCH:
4249         if (hstmt != NULL) {
4250                 rc = _account_query_finalize(hstmt);
4251                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list); }, NULL, ("finalize error"));
4252                 hstmt = NULL;
4253         }
4254
4255         if (error_code == _ACCOUNT_ERROR_NONE || error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4256                 error_code = _account_type_query_all_from_global_db(&account_type_list);
4257                 if (rc != _ACCOUNT_ERROR_NONE && rc != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4258                         ACCOUNT_ERROR("_account_type_query_all_from_global_db fail=[%d]", rc);
4259                         _account_type_gslist_account_type_free(account_type_list);
4260                         return NULL;
4261                 }
4262         }
4263
4264         _INFO("_account_type_query_all end");
4265
4266         return account_type_list;
4267 }
4268
4269 // output parameter label must be free
4270 int _account_type_query_label_by_locale_from_global_db(const char* app_id, const char* locale, char **label)
4271 {
4272         int                             error_code = _ACCOUNT_ERROR_NONE;
4273         account_stmt    hstmt = NULL;
4274         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4275         int                             rc = 0, binding_count = 1;
4276         char*                   converted_locale = NULL;
4277
4278         ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
4279         ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4280         ACCOUNT_RETURN_VAL((label != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
4281         ACCOUNT_RETURN_VAL((locale != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("locale char is null"));
4282         //Making label newly created
4283
4284         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4285
4286         converted_locale = _account_dup_text(locale);
4287         gchar** tokens = g_strsplit(converted_locale, "-", 2);
4288
4289         if (tokens != NULL) {
4290                 if ((char*)(tokens[1]) != NULL) {
4291                         char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1]));
4292                         if (upper_token != NULL) {
4293                                 _ACCOUNT_FREE(converted_locale);
4294                                 converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token);
4295                         }
4296                         _ACCOUNT_FREE(upper_token);
4297                 }
4298         }
4299         g_strfreev(tokens);
4300
4301         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, converted_locale);
4302         _ACCOUNT_FREE(converted_locale);
4303
4304         hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
4305
4306         if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
4307                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
4308                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4309         }
4310
4311         _account_query_bind_text(hstmt, binding_count++, app_id);
4312
4313         rc = _account_query_step(hstmt);
4314         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4315
4316         label_s* label_record = NULL;
4317
4318         while (rc == SQLITE_ROW) {
4319                 label_record = (label_s*) malloc(sizeof(label_s));
4320
4321                 if (label_record == NULL) {
4322                         ACCOUNT_FATAL("malloc Failed");
4323                         break;
4324                 }
4325
4326                 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
4327
4328                 _account_type_convert_column_to_label(hstmt, label_record);
4329
4330                 _ACCOUNT_FREE(*label);
4331                 //Making label newly created
4332                 *label = _account_dup_text(label_record->label);
4333
4334                 _account_type_free_label_with_items(label_record);
4335
4336                 rc = _account_query_step(hstmt);
4337         }
4338
4339         rc = _account_query_finalize(hstmt);
4340         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4341         hstmt = NULL;
4342
4343         error_code = _ACCOUNT_ERROR_NONE;
4344
4345 CATCH:
4346         if (hstmt != NULL) {
4347                 rc = _account_query_finalize(hstmt);
4348                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4349                 hstmt = NULL;
4350         }
4351
4352         _INFO("_account_type_query_label_by_locale_from_global_db() end : error_code = %d", error_code);
4353
4354         return error_code;
4355 }
4356
4357 // output parameter label must be free
4358 int _account_type_query_label_by_locale(const char* app_id, const char* locale, char **label)
4359 {
4360         int                             error_code = _ACCOUNT_ERROR_NONE;
4361         account_stmt    hstmt = NULL;
4362         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4363         int                             rc = 0, binding_count = 1;
4364         char*                   converted_locale = NULL;
4365
4366         ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
4367         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4368         ACCOUNT_RETURN_VAL((label != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
4369         ACCOUNT_RETURN_VAL((locale != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("locale char is null"));
4370         //Making label newly created
4371
4372         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4373
4374         converted_locale = _account_dup_text(locale);
4375         gchar** tokens = g_strsplit(converted_locale, "-", 2);
4376
4377         if (tokens != NULL) {
4378                 if ((char*)(tokens[1]) != NULL) {
4379                         char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1]));
4380                         if (upper_token != NULL) {
4381                                 _ACCOUNT_FREE(converted_locale);
4382                                 converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token);
4383                         }
4384                         _ACCOUNT_FREE(upper_token);
4385                 }
4386         }
4387         g_strfreev(tokens);
4388
4389         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, converted_locale);
4390         _ACCOUNT_FREE(converted_locale);
4391
4392         hstmt = _account_prepare_query(g_hAccountDB, query);
4393
4394         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4395                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4396                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4397         }
4398
4399         _account_query_bind_text(hstmt, binding_count++, app_id);
4400
4401         rc = _account_query_step(hstmt);
4402         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4403
4404         label_s* label_record = NULL;
4405
4406         while (rc == SQLITE_ROW) {
4407                 label_record = (label_s*) malloc(sizeof(label_s));
4408
4409                 if (label_record == NULL) {
4410                         ACCOUNT_FATAL("malloc Failed");
4411                         break;
4412                 }
4413
4414                 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
4415
4416                 _account_type_convert_column_to_label(hstmt, label_record);
4417
4418                 _ACCOUNT_FREE(*label);
4419                 //Making label newly created
4420                 *label = _account_dup_text(label_record->label);
4421
4422                 _account_type_free_label_with_items(label_record);
4423
4424                 rc = _account_query_step(hstmt);
4425         }
4426
4427         rc = _account_query_finalize(hstmt);
4428         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4429         hstmt = NULL;
4430
4431         error_code = _ACCOUNT_ERROR_NONE;
4432
4433 CATCH:
4434         if (hstmt != NULL) {
4435                 rc = _account_query_finalize(hstmt);
4436                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4437                 hstmt = NULL;
4438         }
4439
4440         if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
4441                 error_code = _account_type_query_label_by_locale_from_global_db(app_id, locale, label);
4442
4443         _INFO("_account_type_query_label_by_locale() end : error_code = %d", error_code);
4444
4445         return error_code;
4446 }
4447
4448 static int _account_insert_custom(account_s *account, int account_id)
4449 {
4450         _INFO("_account_insert_custom start");
4451
4452         int                             rc, count = 1;
4453         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4454         account_stmt    hstmt = NULL;
4455
4456         ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4457
4458         if (g_slist_length(account->custom_list) == 0) {
4459                 ACCOUNT_DEBUG("_account_insert_custom, no custom data\n");
4460                 return _ACCOUNT_ERROR_NONE;
4461         }
4462
4463         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
4464
4465         rc = _account_get_record_count(g_hAccountDB, query);
4466
4467         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4468                 ACCOUNT_ERROR("Access failed(%d, %s)", _account_db_err_msg(g_hAccountDB));
4469                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4470         }
4471
4472         if (rc <= 0) {
4473                 ACCOUNT_SLOGE("_account_insert_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
4474                 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4475         }
4476
4477         /* insert query*/
4478
4479         GSList *iter;
4480
4481         for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
4482                 int ret;
4483                 count = 1;
4484                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4485                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (AccountId, AppId, Key, Value) VALUES "
4486                                 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
4487
4488                 hstmt = _account_prepare_query(g_hAccountDB, query);
4489
4490                 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4491                         ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4492                         return _ACCOUNT_ERROR_PERMISSION_DENIED;
4493                 }
4494
4495                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
4496
4497                 account_custom_s* custom_data = NULL;
4498                 custom_data = (account_custom_s*)iter->data;
4499
4500                 ret = _account_query_bind_int(hstmt, count++, account_id);
4501                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
4502                 ret = _account_query_bind_text(hstmt, count++, account->package_name);
4503                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4504                 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
4505                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4506                 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
4507                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4508
4509                 rc = _account_query_step(hstmt);
4510
4511                 if (rc != SQLITE_DONE) {
4512                         ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4513                         break;
4514                 }
4515
4516                 rc = _account_query_finalize(hstmt);
4517                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4518                 hstmt = NULL;
4519
4520         }
4521
4522         _INFO("_account_insert_custom end");
4523
4524         return _ACCOUNT_ERROR_NONE;
4525 }
4526
4527 static int _account_update_custom(account_s *account, int account_id)
4528 {
4529         int                             rc, count = 1;
4530         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4531         account_stmt    hstmt = NULL;
4532
4533         ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4534
4535         if (g_slist_length(account->custom_list) == 0) {
4536                 ACCOUNT_DEBUG("_account_update_custom, no custom data\n");
4537                 return _ACCOUNT_ERROR_NONE;
4538         }
4539
4540         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
4541
4542         rc = _account_get_record_count(g_hAccountDB, query);
4543
4544         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4545                 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4546                 pthread_mutex_unlock(&account_mutex);
4547                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4548         } else if (_account_db_err_code(g_hAccountDB) == SQLITE_BUSY) {
4549                 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
4550                 pthread_mutex_unlock(&account_mutex);
4551                 return _ACCOUNT_ERROR_DATABASE_BUSY;
4552         }
4553
4554         if (rc <= 0) {
4555                 ACCOUNT_SLOGE("_account_update_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
4556                 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4557         }
4558
4559         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4560
4561         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId=? ", ACCOUNT_CUSTOM_TABLE);
4562         hstmt = _account_prepare_query(g_hAccountDB, query);
4563         count = 1;
4564         _account_query_bind_int(hstmt, count++, (int)account_id);
4565         rc = _account_query_step(hstmt);
4566
4567         if (rc == SQLITE_BUSY) {
4568                 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4569                 return _ACCOUNT_ERROR_DATABASE_BUSY;
4570         } else if (rc != SQLITE_DONE) {
4571                 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4572                 return _ACCOUNT_ERROR_DB_FAILED;
4573         }
4574
4575         rc = _account_query_finalize(hstmt);
4576         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4577         hstmt = NULL;
4578
4579         GSList *iter;
4580
4581         for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
4582                 int ret;
4583                 count = 1;
4584                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4585                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AccountId, AppId, Key, Value) VALUES "
4586                                 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
4587
4588                 hstmt = _account_prepare_query(g_hAccountDB, query);
4589
4590                 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4591                         ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4592                         return _ACCOUNT_ERROR_PERMISSION_DENIED;
4593                 }
4594
4595                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
4596
4597                 account_custom_s* custom_data = NULL;
4598                 custom_data = (account_custom_s*)iter->data;
4599
4600                 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
4601                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
4602                 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
4603                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4604                 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
4605                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4606                 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
4607                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4608
4609                 rc = _account_query_step(hstmt);
4610
4611                 if (rc != SQLITE_DONE) {
4612                         ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4613                         break;
4614                 }
4615
4616                 rc = _account_query_finalize(hstmt);
4617                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4618                 hstmt = NULL;
4619
4620         }
4621
4622         return _ACCOUNT_ERROR_NONE;
4623 }
4624