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