apply secure token.
[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 <aul.h>
33 #include <tzplatform_config.h>
34
35 #include <dbg.h>
36 #include <account_ipc_marshal.h>
37 #include <account_free.h>
38 #include <account-private.h>
39 #include <account.h>
40 #include <account-error.h>
41 #include "account-server-db.h"
42 #include "account-key-handler.h"
43 #include "account-crypto-service.h"
44
45 typedef sqlite3_stmt* account_stmt;
46
47 #define TEST_APP_ID "org.tizen.MyAccountCoreTest"
48 #define EAS_CMDLINE "/usr/bin/eas-engine"
49 #define EMAIL_SERVICE_CMDLINE "/usr/bin/email-service"
50 #define IMS_ENGINE_CMDLINE "/usr/bin/ims-srv"
51 #define IMS_AGENT_CMDLINE "/usr/bin/ims-agent"
52 #define MDM_SERVER_CMDLINE "/usr/bin/mdm-server"
53
54 #define RCS_APPID "com.samsung.rcs-im"
55 #define IMS_SERVICE_APPID "ims-service"
56 #define ACTIVESYNC_APPID "eas-ui"
57 #define EMAIL_APPID "email-setting-efl"
58 #define SYNCHRONISE_APPID "setting-synchronise-efl"
59 #define DS_AGENT_CMDLINE "/usr/bin/oma-ds-agent"
60
61 #define FACEBOOK_SDK_APPID "com.samsung.facebook-service"
62 #define FACEBOOK_APPID "com.samsung.facebook"
63
64 #define EASYSIGNUP_CMDLINE      "/usr/bin/esu-agent"
65 #define EASYSIGNUP_APPID        "com.samsung.esu-agent"
66
67 #define ACCESS_TOKEN_ALIAS      "access_token"
68
69 #define ACCOUNT_DB_OPEN_READONLY 0
70 #define ACCOUNT_DB_OPEN_READWRITE 1
71
72 #define MAX_TEXT 4096
73
74 #define _TIZEN_PUBLIC_
75 #ifndef _TIZEN_PUBLIC_
76
77 #endif
78
79 static sqlite3* g_hAccountDB = NULL;
80 static sqlite3* g_hAccountDB2 = NULL;
81 static sqlite3* g_hAccountGlobalDB = NULL;
82 static sqlite3* g_hAccountGlobalDB2 = NULL;
83 pthread_mutex_t account_mutex = PTHREAD_MUTEX_INITIALIZER;
84 pthread_mutex_t account_global_mutex = PTHREAD_MUTEX_INITIALIZER;
85
86 int _account_db_handle_close(sqlite3* hDB);
87 static char *_account_get_text(const char *text_data);
88 static const char *_account_query_table_column_text(account_stmt pStmt, int pos);
89 static int _account_insert_custom(account_s *account, int account_id);
90 static int _account_update_custom(account_s *account, int account_id);
91 static int _account_query_custom_by_account_id(account_custom_cb callback, int account_id, void *user_data );
92 static int _account_type_update_provider_feature(account_type_s *account_type, const char* app_id);
93
94 int _account_query_capability_by_account_id(capability_cb callback, int account_id, void *user_data );
95
96 static void _account_insert_delete_update_notification_send(char *noti_name)
97 {
98         if (!noti_name) {
99                 _ERR("Noti Name is NULL!!!!!!\n");
100                 return;
101         }
102
103         _INFO("noti_type = %s", noti_name);
104
105         if (vconf_set_str(VCONFKEY_ACCOUNT_MSG_STR, noti_name) != 0) {
106                 _ERR("Vconf MSG Str set FAILED !!!!!!\n");;
107         }
108 }
109
110 int _account_get_current_appid_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
111 {
112         char* appid = NULL;
113         char* item = NULL;
114         GSList** appid_list = (GSList**)user_data;
115         int pkgmgr_ret = -1;
116
117         pkgmgr_ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
118
119         if( pkgmgr_ret != PMINFO_R_OK ){
120                 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_appid(%d)", pkgmgr_ret);
121         }
122
123         item = _account_get_text(appid);
124         *appid_list = g_slist_append(*appid_list, item);
125
126         return 0;
127 }
128
129 static inline int __read_proc(const char *path, char *buf, int size)
130 {
131         int fd = 0, ret = 0;
132
133         if (buf == NULL || path == NULL) {
134                 ACCOUNT_ERROR("path and buffer is mandatory\n");
135                 return -1;
136         }
137
138         fd = open(path, O_RDONLY);
139         if (fd < 0) {
140                 ACCOUNT_ERROR("fd open error(%d)\n", fd);
141                 return -1;
142         }
143
144         ret = read(fd, buf, size - 1);
145         if (ret <= 0) {
146                 ACCOUNT_ERROR("fd read error(%d)\n", fd);
147                 close(fd);
148                 return -1;
149         } else
150                 buf[ret] = 0;
151
152         close(fd);
153
154         return ret;
155 }
156
157 char *_account_get_proc_cmdline_bypid(int pid)
158 {
159         char buf[128];
160         int ret = 0;
161
162         ACCOUNT_SNPRINTF(buf, sizeof(buf), "/proc/%d/cmdline", pid);
163         ret = __read_proc(buf, buf, sizeof(buf));
164         if (ret <= 0) {
165                 ACCOUNT_DEBUG("No proc directory (%d)\n", pid);
166                 return NULL;
167         }
168
169         return strdup(buf);
170 }
171
172
173 static char* _account_get_current_appid(int pid)
174 {
175         _INFO("getting caller appid with pid=[%d]", pid);
176
177         int ret=0;
178         char appid[128]={0,};
179         char* appid_ret = NULL;
180
181         ret = aul_app_get_appid_bypid(pid, appid, sizeof(appid));
182
183         if(ret < 0){
184                 ACCOUNT_ERROR("fail to get current appid ret=[%d], appid=%s\n", ret, appid);
185         }
186
187         _INFO("");
188
189         /* SLP platform core exception */
190         if(strlen(appid) == 0){
191                 _INFO("");
192                 char* cmdline = NULL;
193                 cmdline = _account_get_proc_cmdline_bypid(pid);
194                 ACCOUNT_SLOGD("cmdline (%s)!!!!!!\n", cmdline);
195                 if(!g_strcmp0(cmdline, EAS_CMDLINE)) {
196                         appid_ret = _account_get_text(ACTIVESYNC_APPID);
197                         _ACCOUNT_FREE(cmdline);
198                         return appid_ret;
199                 } else if (!g_strcmp0(cmdline, EMAIL_SERVICE_CMDLINE) || !g_strcmp0(cmdline, MDM_SERVER_CMDLINE)) {
200                         appid_ret = _account_get_text(EMAIL_APPID);
201                         _ACCOUNT_FREE(cmdline);
202                         return appid_ret;
203                 } else if (!g_strcmp0(cmdline, IMS_ENGINE_CMDLINE) || !g_strcmp0(cmdline, IMS_AGENT_CMDLINE)) {
204                         if(_account_type_query_app_id_exist_from_all_db(RCS_APPID) == ACCOUNT_ERROR_NONE) {
205                                 appid_ret = _account_get_text(RCS_APPID);
206                         } else if(_account_type_query_app_id_exist_from_all_db(IMS_SERVICE_APPID) == ACCOUNT_ERROR_NONE) {
207                                 appid_ret = _account_get_text(IMS_SERVICE_APPID);
208                         } else {
209                                 appid_ret = _account_get_text(RCS_APPID);
210                         }
211                         _ACCOUNT_FREE(cmdline);
212                         return appid_ret;
213                 } else if (!g_strcmp0(cmdline, DS_AGENT_CMDLINE)) {
214                         appid_ret = _account_get_text(SYNCHRONISE_APPID);
215                         _ACCOUNT_FREE(cmdline);
216                         return appid_ret;
217                 } else if (!g_strcmp0(cmdline, EASYSIGNUP_CMDLINE)) {
218                         appid_ret = _account_get_text(EASYSIGNUP_APPID);
219                         _ACCOUNT_FREE(cmdline);
220                         return appid_ret;
221                 } else {
222                         ACCOUNT_DEBUG("No app id\n");
223                         _ACCOUNT_FREE(cmdline);
224                         return NULL;
225                 }
226         }
227
228         _INFO("");
229         /* temporary exception */
230         if(!g_strcmp0(appid, "com.samsung.gallery")){
231                 appid_ret = _account_get_text("com.samsung.facebook");
232         } else if(!g_strcmp0(appid, FACEBOOK_SDK_APPID)){
233                 appid_ret = _account_get_text(FACEBOOK_APPID);
234         } else {
235                 appid_ret = _account_get_text(appid);
236         }
237
238         return appid_ret;
239 }
240
241 static const char *_account_db_err_msg_from_global_db()
242 {
243         return sqlite3_errmsg(g_hAccountGlobalDB);
244 }
245
246 static int _account_db_err_code_from_global_db()
247 {
248         return sqlite3_errcode(g_hAccountGlobalDB);
249 }
250
251 static int _account_get_record_count_from_global_db(const char *query)
252 {
253         _INFO("_account_get_record_count_in_global_db");
254
255         int rc = -1;
256         int ncount = 0;
257         account_stmt pStmt = NULL;
258
259         if(!query){
260                 _ERR("NULL query\n");
261                 return ACCOUNT_ERROR_QUERY_SYNTAX_ERROR;
262         }
263
264         if(!g_hAccountGlobalDB) {
265                 _ERR("DB is not opened\n");
266                 return ACCOUNT_ERROR_DB_NOT_OPENED;
267         }
268
269         rc = sqlite3_prepare_v2(g_hAccountGlobalDB, query, strlen(query), &pStmt, NULL);
270
271         if (SQLITE_BUSY == rc){
272                 _ERR("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg_from_global_db());
273                 sqlite3_finalize(pStmt);
274                 return ACCOUNT_ERROR_DATABASE_BUSY;
275         } else if (SQLITE_OK != rc) {
276                 _ERR("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg_from_global_db());
277                 sqlite3_finalize(pStmt);
278                 return ACCOUNT_ERROR_DB_FAILED;
279         }
280
281         rc = sqlite3_step(pStmt);
282         if (SQLITE_BUSY == rc) {
283                 _ERR("sqlite3_step() failed(%d, %s).", rc, _account_db_err_msg_from_global_db());
284                 sqlite3_finalize(pStmt);
285                 return ACCOUNT_ERROR_DATABASE_BUSY;
286         } else if (SQLITE_ROW != rc) {
287                 _ERR("sqlite3_step() failed(%d, %s).", rc, _account_db_err_msg_from_global_db());
288                 sqlite3_finalize(pStmt);
289                 return ACCOUNT_ERROR_DB_FAILED;
290         }
291
292         ncount = sqlite3_column_int(pStmt, 0);
293
294         _INFO("account record count [%d]", ncount);
295         sqlite3_finalize(pStmt);
296
297         return ncount;
298 }
299 /*
300 static int _account_execute_query_from_global_db(const char *query)
301 {
302         int rc = -1;
303         char* pszErrorMsg = NULL;
304
305         if(!query){
306                 ACCOUNT_ERROR("NULL query\n");
307                 return ACCOUNT_ERROR_QUERY_SYNTAX_ERROR;
308         }
309
310         if(!g_hAccountGlobalDB){
311                 ACCOUNT_ERROR("Global DB is not opened\n");
312                 return ACCOUNT_ERROR_DB_NOT_OPENED;
313         }
314
315         rc = sqlite3_exec(g_hAccountGlobalDB, query, NULL, NULL, &pszErrorMsg);
316         if (SQLITE_OK != rc) {
317                 ACCOUNT_ERROR("sqlite3_exec rc(%d) query(%s) failed(%s).", rc, query, pszErrorMsg);
318                 sqlite3_free(pszErrorMsg);
319         }
320
321         return rc;
322 }
323 */
324 /*
325 static int _account_begin_transaction_from_global_db(void)
326 {
327         ACCOUNT_DEBUG("_account_begin_transaction start");
328         int ret = -1;
329
330         ret = _account_execute_query_from_global_db("BEGIN IMMEDIATE TRANSACTION");
331
332         if (ret == SQLITE_BUSY){
333                 ACCOUNT_ERROR(" sqlite3 busy = %d", ret);
334                 return ACCOUNT_ERROR_DATABASE_BUSY;
335         } else if(ret != SQLITE_OK) {
336                 ACCOUNT_ERROR("_account_svc_begin_transaction_in_global_db fail :: %d", ret);
337                 return ACCOUNT_ERROR_DB_FAILED;
338         }
339
340         ACCOUNT_DEBUG("_account_begin_transaction_in_global_db end");
341         return ACCOUNT_ERROR_NONE;
342 }
343
344 static int _account_end_transaction_from_global_db(bool is_success)
345 {
346         ACCOUNT_DEBUG("_account_end_transaction_in_global_db start");
347
348         int ret = -1;
349
350         if (is_success == true) {
351                 ret = _account_execute_query_from_global_db("COMMIT TRANSACTION");
352                 ACCOUNT_DEBUG("_account_end_transaction_in_global_db COMMIT");
353         } else {
354                 ret = _account_execute_query_from_global_db("ROLLBACK TRANSACTION");
355                 ACCOUNT_DEBUG("_account_end_transaction ROLLBACK");
356         }
357
358         if(ret == SQLITE_PERM) {
359                 ACCOUNT_ERROR("Account permission denied :: %d", ret);
360                 return ACCOUNT_ERROR_PERMISSION_DENIED;
361         }
362
363         if (ret == SQLITE_BUSY){
364                 ACCOUNT_DEBUG(" sqlite3 busy = %d", ret);
365                 return ACCOUNT_ERROR_DATABASE_BUSY;
366         }
367
368         if (ret != SQLITE_OK) {
369                 ACCOUNT_ERROR("_account_svc_end_transaction_in_global_db fail :: %d", ret);
370                 return ACCOUNT_ERROR_DB_FAILED;
371         }
372
373         ACCOUNT_DEBUG("_account_end_transaction_in_global_db end");
374         return ACCOUNT_ERROR_NONE;
375 }
376 */
377 int _account_type_query_app_id_exist_from_global_db(const char *app_id)
378 {
379         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
380         int rc = 0;
381
382         ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
383         ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
384
385         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
386
387         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
388 /*
389         pthread_mutex_lock(&account_global_mutex);
390
391         ret_transaction = _account_begin_transaction_from_global_db();
392
393         if(_account_db_err_code() == SQLITE_PERM){
394                 pthread_mutex_unlock(&account_global_mutex);
395                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg_global());
396                 return ACCOUNT_ERROR_PERMISSION_DENIED;
397         }
398
399         if (ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY) {
400                 ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction);
401                 pthread_mutex_unlock(&account_global_mutex);
402                 return ACCOUNT_ERROR_DATABASE_BUSY;
403         }else if (ret_transaction != ACCOUNT_ERROR_NONE) {
404                 ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction);
405                 pthread_mutex_unlock(&account_global_mutex);
406                 return ret_transaction;
407         }
408 */
409         rc = _account_get_record_count_from_global_db(query);
410
411         if( _account_db_err_code_from_global_db() == SQLITE_PERM ){
412                 _ERR( "Global DB access failed(%s)", _account_db_err_msg_from_global_db());
413
414                 return ACCOUNT_ERROR_PERMISSION_DENIED;
415         }
416
417         if (rc <= 0) {
418                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
419         }
420
421         return ACCOUNT_ERROR_NONE;
422 }
423
424 int _account_global_db_open(void)
425 {
426         int  rc = 0;
427         int ret = -1;
428         char account_db_path[256] = {0, };
429
430         _INFO( "start _account_global_db_open()");
431
432         ACCOUNT_MEMSET(account_db_path, 0x00, sizeof(account_db_path));
433         ACCOUNT_GET_GLOBAL_DB_PATH(account_db_path, sizeof(account_db_path));
434
435         if( g_hAccountGlobalDB ) {
436                 _ERR( "Account database is using in another app. %x", g_hAccountDB );
437                 return ACCOUNT_ERROR_DATABASE_BUSY;
438         }
439
440         ret = _account_db_handle_close(g_hAccountGlobalDB2);
441         if( ret != ACCOUNT_ERROR_NONE )
442                 ACCOUNT_DEBUG( "db_util_close(g_hAccountGlobalDB2) fail ret = %d", ret);
443
444         ACCOUNT_DEBUG( "before db_util_open()");
445 //      if(mode == ACCOUNT_DB_OPEN_READWRITE)
446 //              rc = db_util_open(account_db_path, &g_hAccountDB, DB_UTIL_REGISTER_HOOK_METHOD);
447 //      else if(mode == ACCOUNT_DB_OPEN_READONLY)
448         rc = db_util_open_with_options(account_db_path, &g_hAccountGlobalDB, SQLITE_OPEN_READONLY, NULL);
449 //      else
450 //              return ACCOUNT_ERROR_DB_NOT_OPENED;
451         ACCOUNT_DEBUG( "after db_util_open() sqlite_rc = %d", rc);
452
453         if( rc == SQLITE_PERM || _account_db_err_code_from_global_db() == SQLITE_PERM ) {
454                 ACCOUNT_ERROR( "Account permission denied");
455                 return ACCOUNT_ERROR_PERMISSION_DENIED;
456         }
457
458         if( rc == SQLITE_BUSY ) {
459                 ACCOUNT_ERROR( "busy handler fail.");
460                 return ACCOUNT_ERROR_DATABASE_BUSY;
461         }
462
463         if( rc != SQLITE_OK ) {
464                 ACCOUNT_ERROR( "The database isn't connected." );
465                 return ACCOUNT_ERROR_DB_NOT_OPENED;
466         }
467
468         _INFO( "end _account_global_db_open()");
469         return ACCOUNT_ERROR_NONE;
470 }
471
472 int _account_global_db_close(void)
473 {
474         ACCOUNT_DEBUG( "start account_global_db_close()");
475         int ret = -1;
476 /*
477         ret = _account_db_handle_close(g_hAccountGlobalDB2);
478         if( ret != ACCOUNT_ERROR_NONE )
479                 ACCOUNT_DEBUG( "db_util_close(g_hAccountGlobalDB2) fail ret = %d", ret);
480 */
481         ret = _account_db_handle_close(g_hAccountGlobalDB);
482         if( ret != ACCOUNT_ERROR_NONE )
483         {
484                 ACCOUNT_ERROR( "db_util_close(g_hAccountGlobalDB) fail ret = %d", ret);
485                 g_hAccountGlobalDB2 = g_hAccountGlobalDB;
486         }
487         g_hAccountGlobalDB = NULL;
488
489         return ret;
490 }
491
492 static int _account_check_account_type_with_appid_group(int uid, const char* appid, char** verified_appid)
493 {
494         int error_code = ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
495         pkgmgrinfo_appinfo_h ahandle=NULL;
496         pkgmgrinfo_pkginfo_h phandle=NULL;
497         char* package_id = NULL;
498         GSList* appid_list = NULL;
499         GSList* iter = NULL;
500
501         if(!appid){
502                 ACCOUNT_ERROR("input param is null\n");
503                 return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
504         }
505
506         if(!verified_appid){
507                 ACCOUNT_ERROR("output param is null\n");
508                 return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
509         }
510
511         if(!strcmp(appid, "com.samsung.setting")){
512                 ACCOUNT_DEBUG("Setting exception\n");
513                 *verified_appid = _account_get_text("com.samsung.setting");
514                 return ACCOUNT_ERROR_NONE;
515         }
516
517         if(!strcmp(appid, "com.samsung.samsung-account-front")){
518                 ACCOUNT_DEBUG("Setting exception\n");
519                 *verified_appid = _account_get_text("com.samsung.samsung-account-front");
520                 return ACCOUNT_ERROR_NONE;
521         }
522
523         if(!strcmp(appid, IMS_SERVICE_APPID) || !strcmp(appid, RCS_APPID)){
524                 ACCOUNT_DEBUG("ims service exception\n");
525                 *verified_appid = _account_get_text(appid);
526                 return ACCOUNT_ERROR_NONE;
527         }
528
529         if(!strcmp(appid, EASYSIGNUP_APPID)){
530                 ACCOUNT_DEBUG("easysignup exception\n");
531                 *verified_appid = _account_get_text(appid);
532                 return ACCOUNT_ERROR_NONE;
533         }
534         /* Get app id family which is stored in account database */
535         int pkgmgr_ret = -1;
536
537         if (uid == OWNER_ROOT || uid == GLOBAL_USER) {
538                 pkgmgr_ret = pkgmgrinfo_appinfo_get_appinfo(appid, &ahandle);
539         } else {
540                 pkgmgr_ret = pkgmgrinfo_appinfo_get_usr_appinfo(appid, uid, &ahandle);
541         }
542         if( pkgmgr_ret != PMINFO_R_OK ){
543                 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_appinfo(%d)", pkgmgr_ret);
544         }
545
546         pkgmgr_ret = pkgmgrinfo_appinfo_get_pkgid(ahandle, &package_id);
547         if( pkgmgr_ret != PMINFO_R_OK ){
548                 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_pkgid(%d)", pkgmgr_ret);
549         }
550
551         if (uid == OWNER_ROOT || uid == GLOBAL_USER) {
552                 pkgmgr_ret = pkgmgrinfo_pkginfo_get_pkginfo(package_id, &phandle);
553         } else {
554                 pkgmgr_ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(package_id, uid, &phandle);
555         }
556         if( pkgmgr_ret != PMINFO_R_OK ){
557                 ACCOUNT_DEBUG("pkgmgrinfo_pkginfo_get_pkginfo(%d)", pkgmgr_ret);
558         }
559
560         if (uid == OWNER_ROOT || uid == GLOBAL_USER) {
561                 pkgmgr_ret = pkgmgrinfo_appinfo_get_list(phandle, PMINFO_ALL_APP, _account_get_current_appid_cb, (void *)&appid_list);
562         } else {
563                 pkgmgr_ret = pkgmgrinfo_appinfo_get_usr_list(phandle, PMINFO_ALL_APP, _account_get_current_appid_cb, (void *)&appid_list, uid);
564         }
565         if( pkgmgr_ret != PMINFO_R_OK ){
566                 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_list(%d)", pkgmgr_ret);
567         }
568
569         /* Compare current app id with the stored app id family */
570         for(iter=appid_list;iter!=NULL;iter=g_slist_next(iter)){
571                 char* tmp = (char*)iter->data;
572                 if(tmp) {
573                         if(_account_type_query_app_id_exist_from_all_db(tmp) == ACCOUNT_ERROR_NONE) {
574                                 *verified_appid = _account_get_text(tmp);
575                                 error_code = ACCOUNT_ERROR_NONE;
576                                 _ACCOUNT_FREE(tmp);
577                                 break;
578                         } else {
579                                 ACCOUNT_SLOGD("not matched owner group app id(%s), current appid(%s)\n", tmp, appid);
580                         }
581                 }
582                 _ACCOUNT_FREE(tmp);
583         }
584
585         g_slist_free(appid_list);
586         pkgmgr_ret = pkgmgrinfo_pkginfo_destroy_pkginfo(phandle);
587         if( pkgmgr_ret != PMINFO_R_OK ){
588                 ACCOUNT_DEBUG("pkgmgrinfo_pkginfo_destroy_pkginfo(%d)", pkgmgr_ret);
589         }
590
591         pkgmgr_ret = pkgmgrinfo_appinfo_destroy_appinfo(ahandle);
592         if( pkgmgr_ret != PMINFO_R_OK ){
593                 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_destroy_appinfo(%d)", pkgmgr_ret);
594         }
595
596         return error_code;
597 }
598
599 static int _account_check_appid_group_with_package_name(int uid, const char* appid, char* package_name)
600 {
601         int error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
602         pkgmgrinfo_appinfo_h ahandle=NULL;
603         pkgmgrinfo_pkginfo_h phandle=NULL;
604         char* package_id = NULL;
605         GSList* appid_list = NULL;
606         GSList* iter = NULL;
607
608         if(!appid){
609                 ACCOUNT_ERROR("input param -appid is null\n");
610                 return ACCOUNT_ERROR_INVALID_PARAMETER;
611         }
612
613         if(!package_name){
614                 ACCOUNT_ERROR("input param - package name is null\n");
615                 return ACCOUNT_ERROR_INVALID_PARAMETER;
616         }
617
618         /* ims-service Exception */
619         if ( strcmp(appid, IMS_SERVICE_APPID) == 0 &&   strcmp(package_name, IMS_SERVICE_APPID) == 0 ) {
620                 ACCOUNT_DEBUG("ims exception.");                                // TODO: NEED TO REMOVE, debug log.
621                 return ACCOUNT_ERROR_NONE;
622         }
623
624         /* easysignup Exception */
625         if ( strcmp(appid, EASYSIGNUP_APPID) == 0 &&    strcmp(package_name, EASYSIGNUP_APPID) == 0 ) {
626                 ACCOUNT_DEBUG("easysignup exception.");                         // TODO: NEED TO REMOVE, debug log.
627                 return ACCOUNT_ERROR_NONE;
628         }
629         /* Get app id family which is stored in account database */
630         int pkgmgr_ret = -1;
631         if (uid == OWNER_ROOT || uid == GLOBAL_USER) {
632                 pkgmgr_ret = pkgmgrinfo_appinfo_get_appinfo(appid, &ahandle);
633         } else {
634                 pkgmgr_ret = pkgmgrinfo_appinfo_get_usr_appinfo(appid, uid, &ahandle);
635         }
636         if( pkgmgr_ret != PMINFO_R_OK ){
637                 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_appinfo(%d)", pkgmgr_ret);
638         }
639
640         pkgmgr_ret = pkgmgrinfo_appinfo_get_pkgid(ahandle, &package_id);
641         if( pkgmgr_ret != PMINFO_R_OK ){
642                 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_pkgid(%d)", pkgmgr_ret);
643         }
644
645         if (uid == OWNER_ROOT || uid == GLOBAL_USER) {
646                 pkgmgr_ret = pkgmgrinfo_pkginfo_get_pkginfo(package_id, &phandle);
647         } else {
648                 pkgmgr_ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(package_id, uid, &phandle);
649         }
650         if( pkgmgr_ret != PMINFO_R_OK ){
651                 ACCOUNT_DEBUG("pkgmgrinfo_pkginfo_get_pkginfo(%d)", pkgmgr_ret);
652         }
653
654         if (uid == OWNER_ROOT || uid == GLOBAL_USER) {
655                 pkgmgr_ret = pkgmgrinfo_appinfo_get_list(phandle, PMINFO_ALL_APP, _account_get_current_appid_cb, (void *)&appid_list);
656         } else {
657                 pkgmgr_ret = pkgmgrinfo_appinfo_get_usr_list(phandle, PMINFO_ALL_APP, _account_get_current_appid_cb, (void *)&appid_list, uid);
658         }
659         if( pkgmgr_ret != PMINFO_R_OK ){
660                 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_list(%d)", pkgmgr_ret);
661         }
662
663         /* Compare current app id with the stored app id family */
664         for(iter=appid_list;iter!=NULL;iter=g_slist_next(iter)){
665                 char* tmp = (char*)iter->data;
666                 if(tmp) {
667                         //ACCOUNT_ERROR("tmp(%s)package_name(%s)\n\n", tmp, package_name);      // TODO: NEED TO REMOVE, debug log.
668                         if( strcmp(tmp, package_name) == 0) {
669                                 error_code = ACCOUNT_ERROR_NONE;
670                                 _ACCOUNT_FREE(tmp);
671                                 break;
672                         } else if ( strcmp(tmp, "com.samsung.samsung-account-front") == 0 &&
673                                                 strcmp(package_name, "com.samsung.samsungaccount") == 0 ) {
674                                 /* Samung Account Exception */
675                                 error_code = ACCOUNT_ERROR_NONE;
676                                 _ACCOUNT_FREE(tmp);
677                                 break;
678                         } else {
679                                 ACCOUNT_SLOGD("not matched owner group app id(%s), current appid(%s)\n", tmp, appid);
680                         }
681                 }
682                 _ACCOUNT_FREE(tmp);
683         }
684
685         g_slist_free(appid_list);
686         pkgmgr_ret = pkgmgrinfo_pkginfo_destroy_pkginfo(phandle);
687         if( pkgmgr_ret != PMINFO_R_OK ){
688                 ACCOUNT_DEBUG("pkgmgrinfo_pkginfo_destroy_pkginfo(%d)", pkgmgr_ret);
689         }
690
691         pkgmgr_ret = pkgmgrinfo_appinfo_destroy_appinfo(ahandle);
692         if( pkgmgr_ret != PMINFO_R_OK ){
693                 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_destroy_appinfo(%d)", pkgmgr_ret);
694         }
695
696         return error_code;
697 }
698
699 static int __encrypt_data(unsigned char *data, const int data_len, char **pp_encrypted_data)
700 {
701         int ret;
702         int enc_data_len = 0;
703         int key_len = 0;
704         const char *alias = ACCESS_TOKEN_ALIAS;
705         unsigned char *key = NULL;
706
707         _INFO("before account_key_handler_get_account_dek");
708
709         ret = account_key_handler_get_account_dek(alias, &key, &key_len);
710         if (ret != ACCOUNT_ERROR_NONE) {
711                 //To Do : fail
712                 _ERR("account_key_handler_get_account_dek failed");
713         }
714         _INFO("after account_key_handler_get_account_dek");
715
716         _INFO("before _encrypt_aes_cbc");
717
718         ret = encrypt_aes_cbc(key, key_len, data, data_len, pp_encrypted_data, &enc_data_len);
719         if (ret != ACCOUNT_ERROR_NONE) {
720                 //To Do : fail
721                 _ERR("encrypt_aes_cbc failed");
722         }
723
724         _INFO("after encrypt_aes_cbc");
725
726         return ACCOUNT_ERROR_NONE;
727 }
728
729
730 static int _encrypt_access_token(account_s *account)
731 {
732         int ret = -1;
733         char *encrypted_token = NULL;
734
735         if (account->access_token) {
736                 _INFO("before __encrypt_data");
737                 ret = __encrypt_data((unsigned char *)(account->access_token), strlen(account->access_token), &encrypted_token);
738                 _INFO("after _encrypt_data, ret=[%d]", ret);
739                 free(account->access_token);
740                 account->access_token = NULL;
741                 if( ret == ACCOUNT_ERROR_NONE) {
742                         account->access_token = encrypted_token;
743                         _INFO("after access_token, encrypted_token size=[%d]", strlen(account->access_token));
744                 }
745                 else {
746                         //To Do : fail
747                         _ERR("_encrypt_data fail");
748                         return ret;
749                 }
750         }
751
752         return ACCOUNT_ERROR_NONE;
753 }
754
755
756 static int __decrypt_data(unsigned char *data, const int data_len, char **pp_decrypted_data)
757 {
758         int ret;
759         int dec_data_len = 0;
760         int key_len = 0;
761         const char *alias = ACCESS_TOKEN_ALIAS;
762         unsigned char *key = NULL;
763
764         _INFO("before account_key_handler_get_account_dek");
765         ret = account_key_handler_get_account_dek(alias, &key, &key_len);
766         if (ret != ACCOUNT_ERROR_NONE) {
767                 //To Do : fail
768                 _ERR("account_key_handler_get_account_dek failed");
769         }
770
771         _INFO("before _decrypt_aes_cbc");
772         decrypt_aes_cbc(key, key_len, data, data_len, pp_decrypted_data, &dec_data_len);
773         if (ret != ACCOUNT_ERROR_NONE) {
774                 //To Do : fail
775                 _ERR("decrypt_aes_cbc failed");
776         }
777         _INFO("after decrypt_aes_cbc, dec_data = %s", *pp_decrypted_data);
778
779         return ACCOUNT_ERROR_NONE;
780 }
781
782 static int _decrypt_access_token(account_s *account)
783 {
784         int ret = -1;
785         char *decrypted_token = NULL;
786
787         if (account->access_token) {
788                 ret = __decrypt_data((unsigned char *)account->access_token, strlen(account->access_token), &decrypted_token);
789                 free(account->access_token);
790                 account->access_token = NULL;
791                 if( ret == ACCOUNT_ERROR_NONE)
792                         account->access_token = decrypted_token;
793                 else {
794                         //To Do : fail
795                         _ERR("_decrypt_access_token fail");
796                         return ret;
797                 }
798         }
799         _INFO("_decrypt_access_token end");
800
801         return ACCOUNT_ERROR_NONE;
802 }
803
804 static int _remove_sensitive_info_from_non_owning_account(int caller_pid, account_s *account)
805 {
806         if (account == NULL)
807         {
808                 _ERR("Null input");
809                 return ACCOUNT_ERROR_INVALID_PARAMETER;
810         }
811
812         if (account->package_name)
813         {
814                 char *caller_package_name = _account_get_current_appid(caller_pid);
815                 if (caller_package_name == NULL)
816                 {
817                         _ERR("Could not get caller app id, so removing sensitive info from account id [%d]", account->id);
818                         return ACCOUNT_ERROR_INVALID_PARAMETER;
819                 }
820
821                 if (g_strcmp0(caller_package_name, account->package_name) != 0)
822                 {
823                         // packages dont match, so remove sensitive info
824                         _INFO("Removing sensitive info from account id [%d]", account->id);
825                         free (account->access_token);
826                         account->access_token = NULL;
827
828                 } else {
829                         int ret = _decrypt_access_token(account);
830                         if (ret != ACCOUNT_ERROR_NONE)
831                         {
832                                 _ERR("_decrypt_access_token error");
833                                 return ret;
834                         }
835                 }
836                 _ACCOUNT_FREE(caller_package_name);
837                 return ACCOUNT_ERROR_NONE;
838         }
839         return ACCOUNT_ERROR_INVALID_PARAMETER;
840 }
841
842 static int _remove_sensitive_info_from_non_owning_account_list(int caller_pid, GList *account_list)
843 {
844         int return_code = ACCOUNT_ERROR_NONE;
845
846         if (account_list == NULL)
847         {
848                 _ERR("Null input");
849                 return ACCOUNT_ERROR_INVALID_PARAMETER;
850         }
851
852         GList *list_iter = NULL;
853         for (list_iter = account_list; list_iter != NULL; list_iter = g_list_next(list_iter))
854         {
855                 account_s *account = (account_s *) list_iter->data;
856                 int ret = _remove_sensitive_info_from_non_owning_account(caller_pid, account);
857                 if( ret != ACCOUNT_ERROR_NONE)
858                         return_code = ret;
859         }
860         return return_code;
861 }
862
863 static int _remove_sensitive_info_from_non_owning_account_slist(int caller_pid, GSList *account_list)
864 {
865         int return_code = ACCOUNT_ERROR_NONE;
866
867         if (account_list == NULL)
868         {
869                 _ERR("Null input");
870                 return ACCOUNT_ERROR_INVALID_PARAMETER;
871         }
872
873         GSList *list_iter = NULL;
874         for (list_iter = account_list; list_iter != NULL; list_iter = g_slist_next(list_iter))
875         {
876                 account_s *account = (account_s *) list_iter->data;
877                 int ret = _remove_sensitive_info_from_non_owning_account(caller_pid, account);
878                 if( ret != ACCOUNT_ERROR_NONE)
879                         return_code = ret;
880         }
881         return return_code;
882 }
883
884 static const char *_account_db_err_msg()
885 {
886         return sqlite3_errmsg(g_hAccountDB);
887 }
888
889 static int _account_db_err_code()
890 {
891         return sqlite3_errcode(g_hAccountDB);
892 }
893
894 static int _account_get_record_count(char* query)
895 {
896         _INFO("_account_get_record_count");
897
898         int rc = -1;
899         int ncount = 0;
900         account_stmt pStmt = NULL;
901
902         if(!query){
903                 _ERR("NULL query\n");
904                 return ACCOUNT_ERROR_QUERY_SYNTAX_ERROR;
905         }
906
907         if(!g_hAccountDB){
908                 _ERR("DB is not opened\n");
909                 return ACCOUNT_ERROR_DB_NOT_OPENED;
910         }
911
912         rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
913
914         if (SQLITE_BUSY == rc){
915                 _ERR("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg());
916                 sqlite3_finalize(pStmt);
917                 return ACCOUNT_ERROR_DATABASE_BUSY;
918         } else if (SQLITE_OK != rc) {
919                 _ERR("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg());
920                 sqlite3_finalize(pStmt);
921                 return ACCOUNT_ERROR_DB_FAILED;
922         }
923
924         rc = sqlite3_step(pStmt);
925         if (SQLITE_BUSY == rc) {
926                 _ERR("sqlite3_step() failed(%d, %s).", rc, _account_db_err_msg());
927                 sqlite3_finalize(pStmt);
928                 return ACCOUNT_ERROR_DATABASE_BUSY;
929         } else if (SQLITE_ROW != rc) {
930                 _ERR("sqlite3_step() failed(%d, %s).", rc, _account_db_err_msg());
931                 sqlite3_finalize(pStmt);
932                 return ACCOUNT_ERROR_DB_FAILED;
933         }
934
935         ncount = sqlite3_column_int(pStmt, 0);
936
937         _INFO("account record count [%d]", ncount);
938         sqlite3_finalize(pStmt);
939
940         return ncount;
941 }
942
943 static int _account_execute_query(const char *query)
944 {
945         int rc = -1;
946         char* pszErrorMsg = NULL;
947
948         if(!query){
949                 ACCOUNT_ERROR("NULL query\n");
950                 return ACCOUNT_ERROR_QUERY_SYNTAX_ERROR;
951         }
952
953         if(!g_hAccountDB){
954                 ACCOUNT_ERROR("DB is not opened\n");
955                 return ACCOUNT_ERROR_DB_NOT_OPENED;
956         }
957
958         rc = sqlite3_exec(g_hAccountDB, query, NULL, NULL, &pszErrorMsg);
959         if (SQLITE_OK != rc) {
960                 ACCOUNT_ERROR("sqlite3_exec rc(%d) query(%s) failed(%s).", rc, query, pszErrorMsg);
961                 sqlite3_free(pszErrorMsg);
962         }
963
964         return rc;
965 }
966
967 static int _account_begin_transaction(void)
968 {
969         ACCOUNT_DEBUG("_account_begin_transaction start");
970         int ret = -1;
971
972         ret = _account_execute_query("BEGIN IMMEDIATE TRANSACTION");
973
974         if (ret == SQLITE_BUSY){
975                 ACCOUNT_ERROR(" sqlite3 busy = %d", ret);
976                 return ACCOUNT_ERROR_DATABASE_BUSY;
977         } else if(ret != SQLITE_OK) {
978                 ACCOUNT_ERROR("_account_svc_begin_transaction fail :: %d", ret);
979                 return ACCOUNT_ERROR_DB_FAILED;
980         }
981
982         ACCOUNT_DEBUG("_account_begin_transaction end");
983         return ACCOUNT_ERROR_NONE;
984 }
985
986 static int _account_end_transaction(bool is_success)
987 {
988         ACCOUNT_DEBUG("_account_end_transaction start");
989
990         int ret = -1;
991
992         if (is_success == true) {
993                 ret = _account_execute_query("COMMIT TRANSACTION");
994                 ACCOUNT_DEBUG("_account_end_transaction COMMIT");
995         } else {
996                 ret = _account_execute_query("ROLLBACK TRANSACTION");
997                 ACCOUNT_DEBUG("_account_end_transaction ROLLBACK");
998         }
999
1000         if(ret == SQLITE_PERM){
1001                 ACCOUNT_ERROR("Account permission denied :: %d", ret);
1002                 return ACCOUNT_ERROR_PERMISSION_DENIED;
1003         }
1004
1005         if (ret == SQLITE_BUSY){
1006                 ACCOUNT_DEBUG(" sqlite3 busy = %d", ret);
1007                 return ACCOUNT_ERROR_DATABASE_BUSY;
1008         }
1009
1010         if (ret != SQLITE_OK) {
1011                 ACCOUNT_ERROR("_account_svc_end_transaction fail :: %d", ret);
1012                 return ACCOUNT_ERROR_DB_FAILED;
1013         }
1014
1015         ACCOUNT_DEBUG("_account_end_transaction end");
1016         return ACCOUNT_ERROR_NONE;
1017 }
1018
1019 static bool _account_check_add_more_account(const char* app_id)
1020 {
1021         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1022         int                     rc = 0;
1023
1024         ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
1025         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1026
1027         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1028
1029         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s' and MultipleAccountSupport = 1", ACCOUNT_TYPE_TABLE, app_id);
1030         rc = _account_get_record_count(query);
1031
1032         /* multiple account support case */
1033         if(rc > 0) {
1034                 ACCOUNT_SLOGD("app id (%s) supports multiple account. rc(%d)\n", app_id, rc);
1035                 return TRUE;
1036         }
1037
1038         /* multiple account not support case */
1039         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1040         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE package_name = '%s'", ACCOUNT_TABLE, app_id);
1041         rc = _account_get_record_count(query);
1042
1043         if(rc <= 0) {
1044                 ACCOUNT_SLOGD("app id (%s) supports single account. and there is no account of the app id\n", app_id);
1045                 return TRUE;
1046         }
1047
1048         return FALSE;
1049 }
1050
1051 //TODO: Need to enable creating db on the first connect for
1052 //a) multi-user cases
1053 //b) to ensure db exist in every connect call
1054
1055 static int _account_create_all_tables(void)
1056 {
1057         int rc = -1;
1058         int error_code = ACCOUNT_ERROR_NONE;
1059         char    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1060
1061         _INFO("create all table - BEGIN");
1062         ACCOUNT_MEMSET(query, 0, sizeof(query));
1063
1064         /*Create the account table*/
1065         ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_TABLE);
1066         rc = _account_get_record_count(query);
1067         if (rc <= 0) {
1068                 rc = _account_execute_query(ACCOUNT_SCHEMA);
1069                 if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY;
1070                 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", ACCOUNT_SCHEMA, rc, _account_db_err_msg()));
1071
1072         }
1073
1074         /*Create capability table*/
1075         ACCOUNT_MEMSET(query, 0, sizeof(query));
1076         ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", CAPABILITY_TABLE);
1077         rc = _account_get_record_count(query);
1078         if (rc <= 0) {
1079                 rc = _account_execute_query(CAPABILITY_SCHEMA);
1080                 if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY;
1081                 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", CAPABILITY_SCHEMA, rc, _account_db_err_msg()));
1082         }
1083
1084         /* Create account custom table */
1085         ACCOUNT_MEMSET(query, 0, sizeof(query));
1086         ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_CUSTOM_TABLE);
1087         rc = _account_get_record_count(query);
1088         if (rc <= 0) {
1089                 rc = _account_execute_query(ACCOUNT_CUSTOM_SCHEMA);
1090                 if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY;
1091                 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
1092         }
1093
1094         /* Create account type table */
1095         ACCOUNT_MEMSET(query, 0, sizeof(query));
1096         ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_TYPE_TABLE);
1097         rc = _account_get_record_count(query);
1098         if (rc <= 0) {
1099                 rc = _account_execute_query(ACCOUNT_TYPE_SCHEMA);
1100                 if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY;
1101                 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", ACCOUNT_TYPE_SCHEMA, rc, _account_db_err_msg()));
1102         }
1103
1104         /* Create label table */
1105         ACCOUNT_MEMSET(query, 0, sizeof(query));
1106         ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", LABEL_TABLE);
1107         rc = _account_get_record_count(query);
1108         if (rc <= 0) {
1109                 rc = _account_execute_query(LABEL_SCHEMA);
1110                 if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY;
1111                 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", LABEL_SCHEMA, rc, _account_db_err_msg()));
1112         }
1113
1114         /* Create account feature table */
1115         ACCOUNT_MEMSET(query, 0, sizeof(query));
1116         ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", PROVIDER_FEATURE_TABLE);
1117         rc = _account_get_record_count(query);
1118         if (rc <= 0) {
1119                 rc = _account_execute_query(PROVIDER_FEATURE_SCHEMA);
1120                 if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY;
1121                 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", PROVIDER_FEATURE_SCHEMA, rc, _account_db_err_msg()));
1122         }
1123
1124         _INFO("create all table - END");
1125         return error_code;
1126 }
1127
1128 static int _account_check_is_all_table_exists()
1129 {
1130         int     rc = 0;
1131         char    query[ACCOUNT_SQL_LEN_MAX] = {0,};
1132         ACCOUNT_MEMSET(query, 0, sizeof(query));
1133
1134         ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s', '%s', '%s', '%s', '%s', '%s')",
1135                         ACCOUNT_TABLE, CAPABILITY_TABLE, ACCOUNT_CUSTOM_TABLE, ACCOUNT_TYPE_TABLE, LABEL_TABLE, PROVIDER_FEATURE_TABLE);
1136         rc = _account_get_record_count(query);
1137
1138         if (rc != ACCOUNT_TABLE_TOTAL_COUNT) {
1139                 ACCOUNT_ERROR("Table count is not matched rc=%d\n", rc);
1140         }
1141
1142         return rc;
1143 }
1144
1145 int _account_db_handle_close(sqlite3* hDB)
1146 {
1147         int rc = 0;
1148         int ret = ACCOUNT_ERROR_NONE;
1149         if(hDB)
1150         {
1151                 rc = db_util_close(hDB);
1152                 if(  rc == SQLITE_OK )
1153                         ret = ACCOUNT_ERROR_NONE;
1154                 else if(  rc == SQLITE_PERM )
1155                         ret = ACCOUNT_ERROR_PERMISSION_DENIED;
1156                 else if ( rc == SQLITE_BUSY )
1157                         ret = ACCOUNT_ERROR_DATABASE_BUSY;
1158                 else
1159                         ret = ACCOUNT_ERROR_DB_FAILED;
1160         }
1161         return ret;
1162 }
1163
1164 int _account_db_open(int mode, int pid, int uid)
1165 {
1166         int  rc = 0;
1167         int ret = -1;
1168         char account_db_dir[256] = {0, };
1169         char account_db_path[256] = {0, };
1170
1171         _INFO( "start _account_db_open()");
1172
1173         ACCOUNT_MEMSET(account_db_dir, 0x00, sizeof(account_db_dir));
1174         ACCOUNT_MEMSET(account_db_path, 0x00, sizeof(account_db_path));
1175
1176         ACCOUNT_GET_USER_DB_PATH(account_db_path, sizeof(account_db_path), uid);
1177
1178         if( g_hAccountDB ) {
1179                 _ERR( "Account database is using in another app. %x", g_hAccountDB );
1180                 return ACCOUNT_ERROR_DATABASE_BUSY;
1181         }
1182
1183         ret = _account_db_handle_close(g_hAccountDB2);
1184         if( ret != ACCOUNT_ERROR_NONE )
1185                 ACCOUNT_DEBUG( "db_util_close(g_hAccountDB2) fail ret = %d", ret);
1186
1187         ACCOUNT_GET_USER_DB_DIR(account_db_dir, sizeof(account_db_dir), uid);
1188         if (-1 == access (account_db_dir, F_OK)) {
1189                 mkdir(account_db_dir, 644);
1190         }
1191
1192         ACCOUNT_DEBUG( "before db_util_open()");
1193 //      if(mode == ACCOUNT_DB_OPEN_READWRITE)
1194                 rc = db_util_open(account_db_path, &g_hAccountDB, DB_UTIL_REGISTER_HOOK_METHOD);
1195 //      else if(mode == ACCOUNT_DB_OPEN_READONLY)
1196 //              rc = db_util_open_with_options(account_db_path, &g_hAccountDB, SQLITE_OPEN_READONLY, NULL);
1197 //      else
1198 //              return ACCOUNT_ERROR_DB_NOT_OPENED;
1199         ACCOUNT_DEBUG( "after db_util_open() sqlite_rc = %d", rc);
1200
1201         if( rc == SQLITE_PERM || _account_db_err_code() == SQLITE_PERM ) {
1202                 ACCOUNT_ERROR( "Account permission denied");
1203                 return ACCOUNT_ERROR_PERMISSION_DENIED;
1204         }
1205
1206         if( rc == SQLITE_BUSY ) {
1207                 ACCOUNT_ERROR( "busy handler fail.");
1208                 return ACCOUNT_ERROR_DATABASE_BUSY;
1209         }
1210
1211         if( rc != SQLITE_OK ) {
1212                 ACCOUNT_ERROR( "The database isn't connected." );
1213                 return ACCOUNT_ERROR_DB_NOT_OPENED;
1214         }
1215
1216         rc = _account_check_is_all_table_exists();
1217
1218         if (rc < 0) {
1219                 _ERR("_account_check_is_all_table_exists rc=[%d]", rc);
1220                 return rc;
1221         } else if (rc == ACCOUNT_TABLE_TOTAL_COUNT) {
1222                 _INFO("Tables OK");
1223         } else {
1224                 int ret = _account_create_all_tables();
1225                 if (ret != ACCOUNT_ERROR_NONE) {
1226                         _ERR("_account_create_all_tables fail ret=[%d]", ret);
1227                         return ret;
1228                 }
1229         }
1230
1231         _INFO( "end _account_db_open()");
1232         return ACCOUNT_ERROR_NONE;
1233 }
1234
1235 int _account_db_close(void)
1236 {
1237         ACCOUNT_DEBUG( "start db_util_close()");
1238         int ret = -1;
1239 /*
1240         ret = _account_db_handle_close(g_hAccountDB2);
1241         if( ret != ACCOUNT_ERROR_NONE )
1242                 ACCOUNT_DEBUG( "db_util_close(g_hAccountDB2) fail ret = %d", ret);
1243 */
1244         ret = _account_db_handle_close(g_hAccountDB);
1245         if( ret != ACCOUNT_ERROR_NONE )
1246         {
1247                 ACCOUNT_ERROR( "db_util_close(g_hAccountDB) fail ret = %d", ret);
1248                 g_hAccountDB2 = g_hAccountDB;
1249         }
1250         g_hAccountDB = NULL;
1251
1252         return ret;
1253 }
1254
1255 static int _account_check_duplicated(account_s *data, const char* verified_appid)
1256 {
1257         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1258         int count = 0;
1259         int ret = -1;
1260
1261         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1262
1263         ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s where package_name='%s' and (user_name='%s' or display_name='%s' or email_address='%s')"
1264                         , ACCOUNT_TABLE, verified_appid, data->user_name, data->display_name, data->email_address);
1265
1266         count = _account_get_record_count(query);
1267
1268         if (count<=0) {
1269                 return ACCOUNT_ERROR_NONE;
1270         }
1271
1272         //check whether duplicated account or not.
1273         //1. check user_name
1274         //2. check display_name
1275         //3. check email_address
1276         GList* account_list_temp = _account_query_account_by_package_name(getpid(), verified_appid, &ret);
1277         if (account_list_temp == NULL)
1278         {
1279                 _ERR("_account_query_account_by_package_name returned NULL");
1280                 return ACCOUNT_ERROR_DB_FAILED;
1281         }
1282
1283         if( _account_db_err_code() == SQLITE_PERM ){
1284                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
1285                 return ACCOUNT_ERROR_PERMISSION_DENIED;
1286         }
1287
1288         if(ret != ACCOUNT_ERROR_NONE){
1289                 return ret;
1290         }
1291
1292         account_list_temp = g_list_first(account_list_temp);
1293         _INFO("account_list_temp length=[%d]",g_list_length(account_list_temp));
1294
1295         GList* iter = NULL;
1296         for (iter = account_list_temp; iter != NULL; iter = g_list_next(iter))
1297         {
1298                 _INFO("iterating account_list_temp");
1299                 account_s *account = NULL;
1300                 _INFO("Before iter->data");
1301                 account = (account_s*)iter->data;
1302                 _INFO("After iter->data");
1303                 if (account != NULL)
1304                 {
1305                         if(account->user_name!=NULL && data->user_name!=NULL && strcmp(account->user_name, data->user_name)==0)
1306                         {
1307                                 _INFO("duplicated account(s) exist!, same user_name=%s", data->user_name);
1308                                 return ACCOUNT_ERROR_DUPLICATED;
1309                         }
1310                         //when user_name is not NULL and display_name is same.
1311                         if(account->user_name==NULL && data->user_name==NULL && account->display_name!=NULL && data->display_name!=NULL && strcmp(account->display_name, data->display_name)==0)
1312                         {
1313                                 _INFO("duplicated account(s) exist!, same display_name=%s", data->display_name);
1314                                 return ACCOUNT_ERROR_DUPLICATED;
1315                         }
1316                         //when user_name and display_name are not NULL and email_address is same.
1317                         if(account->user_name==NULL && data->user_name==NULL && account->display_name==NULL && data->display_name==NULL && account->email_address!=NULL && data->email_address!=NULL && strcmp(account->email_address, data->email_address)==0)
1318                         {
1319                                 _INFO("duplicated account(s) exist!, same email_address=%s", data->email_address);
1320                                 return ACCOUNT_ERROR_DUPLICATED;
1321                         }
1322                 }
1323         }
1324
1325         return ACCOUNT_ERROR_NONE;
1326 }
1327
1328 static int _account_get_next_sequence(const char *pszName)
1329 {
1330         int                     rc = 0;
1331         account_stmt    pStmt = NULL;
1332         int                     max_seq = 0;
1333         char                    szQuery[ACCOUNT_SQL_LEN_MAX] = {0,};
1334
1335         ACCOUNT_MEMSET(szQuery, 0x00, sizeof(szQuery));
1336         ACCOUNT_SNPRINTF(szQuery, sizeof(szQuery),  "SELECT max(seq) FROM %s where name = '%s' ", ACCOUNT_SQLITE_SEQ, pszName);
1337         rc = sqlite3_prepare_v2(g_hAccountDB, szQuery, strlen(szQuery), &pStmt, NULL);
1338         if (SQLITE_OK != rc) {
1339                 ACCOUNT_SLOGE("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg());
1340                 sqlite3_finalize(pStmt);
1341                 return ACCOUNT_ERROR_DB_FAILED;
1342         }
1343
1344         rc = sqlite3_step(pStmt);
1345         max_seq = sqlite3_column_int(pStmt, 0);
1346         max_seq++;
1347
1348         /*Finalize Statement*/
1349         rc = sqlite3_finalize(pStmt);
1350         pStmt = NULL;
1351
1352         return max_seq;
1353 }
1354
1355 static account_stmt _account_prepare_query(char *query)
1356 {
1357         int                     rc = -1;
1358         account_stmt    pStmt = NULL;
1359
1360         ACCOUNT_RETURN_VAL((query != NULL), {}, NULL, ("query is NULL"));
1361
1362         rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
1363
1364         ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, NULL, ("sqlite3_prepare_v2(%s) failed(%s).", query, _account_db_err_msg()));
1365
1366         return pStmt;
1367 }
1368
1369 static account_stmt _account_prepare_query_from_global_db(char *query)
1370 {
1371         int                     rc = -1;
1372         account_stmt    pStmt = NULL;
1373
1374         ACCOUNT_RETURN_VAL((query != NULL), {}, NULL, ("query is NULL"));
1375
1376         rc = sqlite3_prepare_v2(g_hAccountGlobalDB, query, strlen(query), &pStmt, NULL);
1377
1378         ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, NULL, ("sqlite3_prepare_v2(%s) failed(%s).", query, _account_db_err_msg_from_global_db()));
1379
1380         return pStmt;
1381 }
1382
1383 static int _account_query_bind_int(account_stmt pStmt, int pos, int num)
1384 {
1385         if(!pStmt){
1386                 ACCOUNT_ERROR("statement is null");
1387                 return -1;
1388         }
1389
1390         if(pos < 0){
1391                 ACCOUNT_ERROR("invalid pos");
1392                 return -1;
1393         }
1394
1395         return sqlite3_bind_int(pStmt, pos, num);
1396 }
1397
1398 static int _account_query_bind_text(account_stmt pStmt, int pos, const char *str)
1399 {
1400         _INFO("_account_query_bind_text");
1401
1402         if(!pStmt)
1403         {
1404                 _ERR("statement is null");
1405                 return -1;
1406         }
1407
1408         if(str)
1409         {
1410                 _INFO("sqlite3_bind_text");
1411                 return sqlite3_bind_text(pStmt, pos, (const char*)str, strlen(str), SQLITE_STATIC);
1412         }
1413         else
1414         {
1415                 _INFO("sqlite3_bind_null");
1416                 return sqlite3_bind_null(pStmt, pos);
1417         }
1418 }
1419
1420 static int _account_convert_account_to_sql(account_s *account, account_stmt hstmt, char *sql_value)
1421 {
1422         _INFO("start");
1423
1424         int count = 1;
1425
1426         /*Caution : Keep insert query orders.*/
1427
1428         /* 1. user name*/
1429         _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1430         _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], user_name=%s", account->id, account->user_name);
1431
1432         /* 2. email address*/
1433         _account_query_bind_text(hstmt, count++, (char*)account->email_address);
1434         _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], email_address=%s", account->id, account->email_address);
1435
1436         /* 3. display name*/
1437         _account_query_bind_text(hstmt, count++, (char*)account->display_name);
1438         _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], display_name=%s", account->id, account->display_name);
1439
1440         /* 4. icon path*/
1441         _account_query_bind_text(hstmt, count++, (char*)account->icon_path);
1442         _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], icon_path=%s", account->id, account->icon_path);
1443
1444         /* 5. source*/
1445         _account_query_bind_text(hstmt, count++, (char*)account->source);
1446         _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], source=%s", account->id, account->source);
1447
1448         /* 6. package name*/
1449         _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1450         _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], package_name=%s", account->id, account->package_name);
1451
1452         /* 7. access token*/
1453         _account_query_bind_text(hstmt, count++, (char*)account->access_token);
1454         _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], access_token=%s", account->id, account->access_token);
1455
1456         /* 8. domain name*/
1457         _account_query_bind_text(hstmt, count++, (char*)account->domain_name);
1458         _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], domain_name=%s", account->id, account->domain_name);
1459
1460         /* 9. auth type*/
1461         _account_query_bind_int(hstmt, count++, account->auth_type);
1462         _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], auth_type=%d", account->id, account->auth_type);
1463
1464         /* 10. secret */
1465         _account_query_bind_int(hstmt, count++, account->secret);
1466         _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], secret=%d", account->id, account->secret);
1467
1468         /* 11. sync_support */
1469         _account_query_bind_int(hstmt, count++, account->sync_support);
1470         _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], sync_support=%d", account->id, account->sync_support);
1471
1472         int i;
1473
1474         /* 12. user text*/
1475         for(i=0; i< USER_TXT_CNT; i++)
1476                 _account_query_bind_text(hstmt, count++, (char*)account->user_data_txt[i]);
1477
1478         /* 13. user integer     */
1479         for(i=0; i< USER_INT_CNT; i++)
1480         {
1481                 _account_query_bind_int(hstmt, count++, account->user_data_int[i]);
1482         _INFO("convert user_data_int : marshal_user_int data_int[%d]=%d", i, account->user_data_int[i]);
1483         }
1484
1485         _INFO("end");
1486
1487         return count;
1488 }
1489
1490 static int _account_query_finalize(account_stmt pStmt)
1491 {
1492         int rc = -1;
1493
1494         if (!pStmt) {
1495                 ACCOUNT_ERROR( "pStmt is NULL");
1496                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1497         }
1498
1499         rc = sqlite3_finalize(pStmt);
1500         if (rc == SQLITE_BUSY){
1501                 ACCOUNT_ERROR(" sqlite3 busy = %d", rc);
1502                 return ACCOUNT_ERROR_DATABASE_BUSY;
1503         } else if (rc != SQLITE_OK) {
1504                 ACCOUNT_ERROR( "sqlite3_finalize fail, rc : %d, db_error : %s\n", rc, _account_db_err_msg());
1505                 return ACCOUNT_ERROR_DB_FAILED;
1506         }
1507
1508         return ACCOUNT_ERROR_NONE;
1509 }
1510
1511 static int _account_query_finalize_from_global_db(account_stmt pStmt)
1512 {
1513         int rc = -1;
1514
1515         if (!pStmt) {
1516                 ACCOUNT_ERROR( "pStmt is NULL");
1517                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1518         }
1519
1520         rc = sqlite3_finalize(pStmt);
1521         if (rc == SQLITE_BUSY){
1522                 ACCOUNT_ERROR(" sqlite3 busy = %d", rc);
1523                 return ACCOUNT_ERROR_DATABASE_BUSY;
1524         } else if (rc != SQLITE_OK) {
1525                 ACCOUNT_ERROR( "sqlite3_finalize fail, rc : %d, db_error : %s\n", rc, _account_db_err_msg_from_global_db());
1526                 return ACCOUNT_ERROR_DB_FAILED;
1527         }
1528
1529         return ACCOUNT_ERROR_NONE;
1530 }
1531
1532 static int _account_query_step(account_stmt pStmt)
1533 {
1534         if(!pStmt){
1535                 ACCOUNT_ERROR( "pStmt is NULL");
1536                 return -1;
1537         }
1538
1539         return sqlite3_step(pStmt);
1540 }
1541
1542 static int _account_execute_insert_query(account_s *account)
1543 {
1544         _INFO("_account_execute_insert_query start");
1545
1546         int                             rc = 0;
1547         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1548         int                             error_code = ACCOUNT_ERROR_NONE;
1549         account_stmt    hstmt = NULL;
1550
1551         /* check whether app id exist in account type db */
1552
1553         if (!account->user_name && !account->display_name && !account->email_address) {
1554                 _INFO("");
1555                 ACCOUNT_ERROR("Mandetory fields is NULL. At least one field is required among username, display name, email address\n");
1556                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1557         }
1558
1559         _INFO("");
1560         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1561         ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (user_name, email_address , display_name , icon_path , source , package_name , "
1562                         "access_token , domain_name , auth_type , secret , sync_support , txt_custom0, txt_custom1, txt_custom2, txt_custom3, txt_custom4, "
1563                         "int_custom0, int_custom1, int_custom2, int_custom3, int_custom4, txt_custom0 ) values " // to do urusa
1564                         "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",   ACCOUNT_TABLE);
1565
1566         hstmt = _account_prepare_query(query);
1567         ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
1568
1569         _INFO("");
1570         _account_convert_account_to_sql(account, hstmt, query);
1571
1572         _INFO("");
1573         rc = _account_query_step(hstmt);
1574         if (rc != SQLITE_DONE) {
1575                 _INFO("");
1576                 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1577
1578                 if( _account_db_err_code() == SQLITE_PERM )
1579                         error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
1580                 else
1581                         error_code = ACCOUNT_ERROR_DB_FAILED;
1582         }
1583
1584         _INFO("");
1585         rc = _account_query_finalize(hstmt);
1586         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1587         hstmt = NULL;
1588
1589         _INFO("_account_execute_insert_query end");
1590         return error_code;
1591 }
1592
1593 static int _account_insert_capability(account_s *account, int account_id)
1594 {
1595         _INFO("_account_insert_capability start");
1596         int                     rc, count = 1;
1597         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1598         account_stmt    hstmt = NULL;
1599
1600         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1601
1602         if (g_slist_length( account->capablity_list)==0) {
1603                 ACCOUNT_DEBUG( "_account_insert_capability, no capability\n");
1604                 return ACCOUNT_ERROR_NONE;
1605         }
1606
1607         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
1608
1609         _INFO("_account_insert_capability _account_get_record_count [%s]", query);
1610         rc = _account_get_record_count(query);
1611
1612         if( _account_db_err_code() == SQLITE_PERM ){
1613                 _ERR( "Access failed(%s)", _account_db_err_msg());
1614                 return ACCOUNT_ERROR_PERMISSION_DENIED;
1615         }
1616         if (rc <= 0) {
1617                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1618         }
1619
1620         /* insert query*/
1621
1622         GSList *iter;
1623
1624         for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1625                 int ret;
1626                 count = 1;
1627
1628                 account_capability_s* cap_data = NULL;
1629                 cap_data = (account_capability_s*)iter->data;
1630
1631                 _INFO("cap_data->type = %s, cap_data->value = %d \n", cap_data->type, cap_data->value);
1632
1633                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1634                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
1635                                 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
1636                 hstmt = _account_prepare_query(query);
1637
1638                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
1639
1640                 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
1641                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1642                 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
1643                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1644                 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1645                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1646                 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1647                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1648                 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
1649                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1650
1651                 rc = _account_query_step(hstmt);
1652                 _INFO("_account_insert_capability _account_query_step[%d]", rc);
1653
1654                 if (rc != SQLITE_DONE) {
1655                         _ERR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1656                         break;
1657                 }
1658
1659                 rc = _account_query_finalize(hstmt);
1660                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1661                 hstmt = NULL;
1662
1663         }
1664
1665         _INFO("_account_insert_capability end");
1666         return ACCOUNT_ERROR_NONE;
1667 }
1668
1669 static int _account_update_capability(account_s *account, int account_id)
1670 {
1671         int                     rc, count = 1;
1672         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1673         account_stmt    hstmt = NULL;
1674
1675         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1676
1677         if (g_slist_length( account->capablity_list)==0) {
1678                 ACCOUNT_ERROR( "_account_update_capability, no capability\n");
1679                 return ACCOUNT_ERROR_NONE;
1680         }
1681
1682         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
1683
1684         rc = _account_get_record_count(query);
1685
1686         if (rc <= 0) {
1687                 ACCOUNT_SLOGI( "_account_update_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
1688                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1689         }
1690
1691         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1692
1693         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id=? ", CAPABILITY_TABLE);
1694         hstmt = _account_prepare_query(query);
1695         count = 1;
1696         _account_query_bind_int(hstmt, count++, (int)account_id);
1697         rc = _account_query_step(hstmt);
1698
1699         if (rc != SQLITE_DONE) {
1700                 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1701                 return ACCOUNT_ERROR_DB_FAILED;
1702         }
1703         rc = _account_query_finalize(hstmt);
1704         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1705         hstmt = NULL;
1706
1707         GSList *iter;
1708
1709         for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1710                 int ret;
1711                 count = 1;
1712                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1713                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
1714                                 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
1715
1716                 hstmt = _account_prepare_query(query);
1717
1718                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
1719
1720                 account_capability_s* cap_data = NULL;
1721                 cap_data = (account_capability_s*)iter->data;
1722
1723                 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
1724                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1725                 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
1726                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1727                 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1728                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1729                 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1730                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1731                 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
1732                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1733
1734                 rc = _account_query_step(hstmt);
1735
1736                 if (rc != SQLITE_DONE) {
1737                         ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1738                         break;
1739                 }
1740
1741                 rc = _account_query_finalize(hstmt);
1742                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1743                 hstmt = NULL;
1744
1745         }
1746
1747         return ACCOUNT_ERROR_NONE;
1748 }
1749
1750 static int _account_update_capability_by_user_name(account_s *account, const char *user_name, const char *package_name )
1751 {
1752         int                     rc, count = 1;
1753         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1754         account_stmt    hstmt = NULL;
1755
1756         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1757
1758         if (g_slist_length( account->capablity_list)==0) {
1759                 ACCOUNT_ERROR( "_account_update_capability_by_user_name, no capability\n");
1760                 return ACCOUNT_ERROR_NONE;
1761         }
1762
1763         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where package_name= '%s' and user_name='%s'", ACCOUNT_TABLE, package_name, user_name);
1764
1765         rc = _account_get_record_count(query);
1766
1767         if (rc <= 0) {
1768                 ACCOUNT_SLOGI( "_account_update_capability_by_user_name : related account item is not existed rc=%d , %s ", rc, _account_db_err_msg());
1769                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1770         }
1771
1772         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1773
1774         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name=? and user_name=? ", CAPABILITY_TABLE);
1775         hstmt = _account_prepare_query(query);
1776         count = 1;
1777         _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1778         _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1779         rc = _account_query_step(hstmt);
1780         if (rc != SQLITE_DONE) {
1781                 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1782                 return ACCOUNT_ERROR_DB_FAILED;
1783         }
1784
1785         rc = _account_query_finalize(hstmt);
1786         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1787         hstmt = NULL;
1788
1789         GSList* iter;
1790
1791         for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1792                 int ret;
1793                 count = 1;
1794                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1795                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
1796                                 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
1797
1798                 hstmt = _account_prepare_query(query);
1799
1800                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
1801
1802                 account_capability_s* cap_data = NULL;
1803                 cap_data = (account_capability_s*)iter->data;
1804
1805                 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
1806                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1807                 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
1808                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1809                 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1810                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1811                 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1812                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1813                 ret = _account_query_bind_int(hstmt, count++, (int)account->id);
1814                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1815
1816                 rc = _account_query_step(hstmt);
1817
1818                 if (rc != SQLITE_DONE) {
1819                         ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1820                         break;
1821                 }
1822
1823                 rc = _account_query_finalize(hstmt);
1824                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1825                 hstmt = NULL;
1826
1827         }
1828
1829         return ACCOUNT_ERROR_NONE;
1830 }
1831
1832 static int _account_query_table_column_int(account_stmt pStmt, int pos)
1833 {
1834         if(!pStmt){
1835                 ACCOUNT_ERROR("statement is null");
1836                 return -1;
1837         }
1838
1839         if(pos < 0){
1840                 ACCOUNT_ERROR("invalid pos");
1841                 return -1;
1842         }
1843
1844         return sqlite3_column_int(pStmt, pos);
1845 }
1846
1847 static const char *_account_query_table_column_text(account_stmt pStmt, int pos)
1848 {
1849         if(!pStmt){
1850                 ACCOUNT_ERROR("statement is null");
1851                 return NULL;
1852         }
1853
1854         if(pos < 0){
1855                 ACCOUNT_ERROR("invalid pos");
1856                 return NULL;
1857         }
1858
1859         return (const char*)sqlite3_column_text(pStmt, pos);
1860 }
1861
1862 static void _account_db_data_to_text(const char *textbuf, char **output)
1863 {
1864         if (textbuf && strlen(textbuf)>0) {
1865                 if (*output) {
1866                         free(*output);
1867                         *output = NULL;
1868                 }
1869                 *output = strdup(textbuf);
1870         }
1871 }
1872
1873 static void _account_convert_column_to_account(account_stmt hstmt, account_s *account_record)
1874 {
1875         const char *textbuf = NULL;
1876
1877         account_record->id = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_ID);
1878         ACCOUNT_DEBUG("account_record->id =[%d]", account_record->id);
1879
1880         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_NAME);
1881         _account_db_data_to_text(textbuf, &(account_record->user_name));
1882
1883         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_EMAIL_ADDRESS);
1884         _account_db_data_to_text(textbuf, &(account_record->email_address));
1885
1886         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DISPLAY_NAME);
1887         _account_db_data_to_text(textbuf, &(account_record->display_name));
1888
1889         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ICON_PATH);
1890         _account_db_data_to_text(textbuf, &(account_record->icon_path));
1891
1892         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_SOURCE);
1893         _account_db_data_to_text(textbuf, &(account_record->source));
1894
1895         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_PACKAGE_NAME);
1896         _account_db_data_to_text(textbuf, &(account_record->package_name));
1897
1898         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ACCESS_TOKEN);
1899         _account_db_data_to_text(textbuf, &(account_record->access_token));
1900
1901         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DOMAIN_NAME);
1902         _account_db_data_to_text(textbuf, &(account_record->domain_name));
1903
1904         account_record->auth_type = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_AUTH_TYPE);
1905
1906         account_record->secret = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SECRET);
1907
1908         account_record->sync_support = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SYNC_SUPPORT);
1909
1910         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_0);
1911         _account_db_data_to_text(textbuf, &(account_record->user_data_txt[0]));
1912
1913         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_1);
1914         _account_db_data_to_text(textbuf, &(account_record->user_data_txt[1]));
1915
1916         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_2);
1917         _account_db_data_to_text(textbuf, &(account_record->user_data_txt[2]));
1918
1919         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_3);
1920         _account_db_data_to_text(textbuf, &(account_record->user_data_txt[3]));
1921
1922         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_4);
1923         _account_db_data_to_text(textbuf, &(account_record->user_data_txt[4]));
1924
1925         account_record->user_data_int[0] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_0);
1926         account_record->user_data_int[1] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_1);
1927         account_record->user_data_int[2] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_2);
1928         account_record->user_data_int[3] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_3);
1929         account_record->user_data_int[4] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_4);
1930 }
1931
1932 static void _account_convert_column_to_capability(account_stmt hstmt, account_capability_s *capability_record)
1933 {
1934         const char *textbuf = NULL;
1935
1936         _INFO("start _account_convert_column_to_capability()");
1937         capability_record->id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ID);
1938
1939         textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_KEY);
1940         _account_db_data_to_text(textbuf, &(capability_record->type));
1941
1942         capability_record->value = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_VALUE);
1943
1944         textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_PACKAGE_NAME);
1945         _account_db_data_to_text(textbuf, &(capability_record->package_name));
1946
1947         textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_USER_NAME);
1948         _account_db_data_to_text(textbuf, &(capability_record->user_name));
1949
1950         capability_record->account_id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ACCOUNT_ID);
1951         _INFO("type = %s, value = %d", capability_record->type, capability_record->value);
1952         _INFO("end _account_convert_column_to_capability()");
1953 }
1954
1955 static void _account_convert_column_to_custom(account_stmt hstmt, account_custom_s *custom_record)
1956 {
1957         _INFO("start _account_convert_column_to_custom()");
1958         const char *textbuf = NULL;
1959
1960         custom_record->account_id = _account_query_table_column_int(hstmt, ACCOUNT_CUSTOM_FIELD_ACCOUNT_ID);
1961
1962         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_APP_ID);
1963         _account_db_data_to_text(textbuf, &(custom_record->app_id));
1964
1965         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_KEY);
1966         _account_db_data_to_text(textbuf, &(custom_record->key));
1967
1968         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_VALUE);
1969         _account_db_data_to_text(textbuf, &(custom_record->value));
1970         _INFO("key = %s, value = %s", custom_record->key, custom_record->value);
1971         _INFO("end _account_convert_column_to_custom()");
1972 }
1973
1974 bool _account_get_capability_text_cb(const char* capability_type, account_capability_state_e capability_value, void *user_data)
1975 {
1976         account_s *data = (account_s*)user_data;
1977
1978         account_capability_s *cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
1979
1980         if (cap_data == NULL)
1981                 return FALSE;
1982         ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
1983
1984         cap_data->type = _account_get_text(capability_type);
1985         cap_data->value = capability_value;
1986         _INFO("cap_data->type = %s, cap_data->value = %d", cap_data->type, cap_data->value);
1987
1988         data->capablity_list = g_slist_append(data->capablity_list, (gpointer)cap_data);
1989
1990         return TRUE;
1991 }
1992
1993
1994 bool _account_get_custom_text_cb(char* key, char* value, void *user_data)
1995 {
1996         account_s *data = (account_s*)user_data;
1997
1998         account_custom_s *custom_data = (account_custom_s*)malloc(sizeof(account_custom_s));
1999
2000         if (custom_data == NULL) {
2001                 ACCOUNT_DEBUG("_account_get_custom_text_cb :: malloc fail\n");
2002                 return FALSE;
2003         }
2004         ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s));
2005
2006         custom_data->account_id = data->id;
2007         custom_data->app_id = _account_get_text(data->package_name);
2008         custom_data->key = _account_get_text(key);
2009         custom_data->value = _account_get_text(value);
2010         _INFO("custom_data->key = %s, custom_data->value = %s", custom_data->key, custom_data->value);
2011
2012         data->custom_list = g_slist_append(data->custom_list, (gpointer)custom_data);
2013
2014         return TRUE;
2015 }
2016
2017
2018 static char *_account_get_text(const char *text_data)
2019 {
2020         char *text_value = NULL;
2021
2022         if (text_data != NULL) {
2023                 text_value = strdup(text_data);
2024         }
2025         return text_value;
2026 }
2027
2028 static int _account_compare_old_record_by_user_name(account_s *new_account, const char* user_name, const char* package_name)
2029 {
2030         int                             error_code = ACCOUNT_ERROR_NONE;
2031         account_stmt    hstmt = NULL;
2032         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2033         int                             rc = 0;
2034         account_s *old_account = NULL;
2035
2036         ACCOUNT_RETURN_VAL((new_account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2037         ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
2038         ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
2039         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2040
2041         old_account = (account_s*)calloc(1, sizeof(account_s));
2042         if(!old_account) {
2043                 ACCOUNT_FATAL("Memory alloc fail\n");
2044                 return ACCOUNT_ERROR_OUT_OF_MEMORY;
2045         }
2046
2047         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2048
2049         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = '%s' and package_name='%s'", ACCOUNT_TABLE, user_name, package_name);
2050         hstmt = _account_prepare_query(query);
2051
2052         rc = _account_query_step(hstmt);
2053         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2054
2055         while (rc == SQLITE_ROW) {
2056                 _account_convert_column_to_account(hstmt, old_account);
2057                 rc = _account_query_step(hstmt);
2058         }
2059
2060         rc = _account_query_finalize(hstmt);
2061         ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2062         hstmt = NULL;
2063
2064         // get capability
2065         error_code = _account_query_capability_by_account_id(_account_get_capability_text_cb, old_account->id, (void*)old_account);
2066         ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
2067
2068         // get custom text
2069         error_code = _account_query_custom_by_account_id(_account_get_custom_text_cb, old_account->id, (void*)old_account);
2070         ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
2071
2072         // compare
2073         new_account->id = old_account->id;
2074
2075         //user name
2076         if(!new_account->user_name) {
2077                 if(old_account->user_name)
2078                         new_account->user_name = _account_get_text(old_account->user_name);
2079         }
2080
2081         // display name
2082         if(!new_account->display_name) {
2083                 if(old_account->display_name)
2084                         new_account->display_name = _account_get_text(old_account->display_name);
2085         }
2086
2087         // email address
2088         if(!new_account->email_address) {
2089                 if(old_account->email_address)
2090                         new_account->email_address = _account_get_text(old_account->email_address);
2091         }
2092
2093         // domain name
2094         if(!new_account->domain_name) {
2095                 if(old_account->domain_name)
2096                         new_account->domain_name = _account_get_text(old_account->domain_name);
2097         }
2098
2099         // icon path
2100         if(!new_account->icon_path) {
2101                 if(old_account->icon_path)
2102                         new_account->icon_path = _account_get_text(old_account->icon_path);
2103         }
2104
2105         // source
2106         if(!new_account->source) {
2107                 if(old_account->source)
2108                         new_account->source = _account_get_text(old_account->source);
2109         }
2110
2111         _ACCOUNT_FREE(new_account->package_name);
2112         new_account->package_name = _account_get_text(old_account->package_name);
2113
2114         // access token
2115         if(!new_account->access_token) {
2116                 if(old_account->access_token)
2117                         new_account->access_token = _account_get_text(old_account->access_token);
2118         }
2119
2120         // auth type
2121         if(new_account->auth_type == ACCOUNT_AUTH_TYPE_INVALID) {
2122                 new_account->auth_type = old_account->auth_type;
2123         }
2124
2125         //secret
2126         if(new_account->secret== ACCOUNT_SECRECY_INVALID) {
2127                 new_account->secret = old_account->secret;
2128         }
2129
2130         // sync support
2131         if(new_account->sync_support == ACCOUNT_SYNC_INVALID) {
2132                 new_account->sync_support = old_account->sync_support;
2133         }
2134
2135         // TODO user text
2136         int i;
2137         for(i=0;i<USER_TXT_CNT;i++) {
2138                 if(!new_account->user_data_txt[i]) {
2139                         if(old_account->user_data_txt[i])
2140                                 new_account->user_data_txt[i] = _account_get_text(old_account->user_data_txt[i]);
2141                 }
2142         }
2143
2144         // TODO user int
2145         for(i=0;i<USER_INT_CNT;i++) {
2146                 if(new_account->user_data_int[i] == 0) {
2147                                 new_account->user_data_int[i] = old_account->user_data_int[i];
2148                 }
2149         }
2150
2151         // capability
2152
2153         // user custom table
2154
2155 CATCH:
2156         if (old_account) {
2157                 _account_free_account_with_items(old_account);
2158         }
2159
2160         if (hstmt != NULL) {
2161                 rc = _account_query_finalize(hstmt);
2162                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2163                 hstmt = NULL;
2164         }
2165
2166         return ACCOUNT_ERROR_NONE;
2167 }
2168
2169
2170
2171 static int _account_update_account_by_user_name(int pid, int uid, account_s *account, const char *user_name, const char *package_name)
2172 {
2173         int                             rc = 0, binding_count = 0, count = 0;
2174         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2175         int                             error_code = ACCOUNT_ERROR_NONE;
2176         account_stmt    hstmt = NULL;
2177
2178         ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is NULL.\n"));
2179         ACCOUNT_RETURN_VAL((package_name!= NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is NULL.\n"));
2180
2181         char* current_appid = NULL;
2182         char* verified_appid = NULL;
2183
2184         current_appid = _account_get_current_appid(pid);
2185         error_code = _account_check_account_type_with_appid_group(uid, current_appid, &verified_appid);
2186
2187         _ACCOUNT_FREE(current_appid);
2188         _ACCOUNT_FREE(verified_appid);
2189
2190         if(error_code != ACCOUNT_ERROR_NONE){
2191                 ACCOUNT_ERROR("No permission to update\n");
2192                 return ACCOUNT_ERROR_PERMISSION_DENIED;
2193         }
2194
2195         error_code = _encrypt_access_token(account);
2196         if (error_code != ACCOUNT_ERROR_NONE)
2197         {
2198                 _ERR("_encrypt_access_token error");
2199                 return error_code;
2200         }
2201
2202         _account_compare_old_record_by_user_name(account, user_name, package_name);
2203
2204         if( _account_db_err_code() == SQLITE_PERM ){
2205                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2206                 return ACCOUNT_ERROR_PERMISSION_DENIED;
2207         }
2208
2209         if (!account->package_name) {
2210                 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
2211                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2212         }
2213
2214         if (!account->user_name && !account->display_name && !account->email_address) {
2215                 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
2216                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2217         }
2218
2219         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE user_name='%s' and package_name='%s'"
2220                         , ACCOUNT_TABLE, user_name, package_name);
2221
2222         count = _account_get_record_count(query);
2223
2224         if( _account_db_err_code() == SQLITE_PERM ){
2225                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2226                 return ACCOUNT_ERROR_PERMISSION_DENIED;
2227         }
2228
2229         if (count <= 0) {
2230                 ACCOUNT_SLOGI("_account_update_account_by_user_name : The account not exist!, count = %d, user_name=%s, package_name=%s\n",
2231                         count, user_name, package_name);
2232                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2233         }
2234
2235         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2236
2237         //TODO: Is it required to update id ? As of now I can only think of falied rollback cases (between account and gSSO DB)
2238         ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
2239                         "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
2240                         "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
2241                         "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE user_name=? and package_name=? ", ACCOUNT_TABLE);
2242
2243         hstmt = _account_prepare_query(query);
2244         if( _account_db_err_code() == SQLITE_PERM ){
2245                 _account_end_transaction(FALSE);
2246                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2247                 return ACCOUNT_ERROR_PERMISSION_DENIED;
2248         }
2249         ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg()));
2250
2251         binding_count = _account_convert_account_to_sql(account, hstmt, query);
2252
2253         _account_query_bind_text(hstmt, binding_count++, user_name);
2254         _account_query_bind_text(hstmt, binding_count++, package_name);
2255         rc = _account_query_step(hstmt);
2256         if (rc != SQLITE_DONE) {
2257                 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
2258         }
2259         rc = _account_query_finalize(hstmt);
2260         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2261         hstmt = NULL;
2262
2263         /*update capability*/
2264         error_code = _account_update_capability_by_user_name(account, user_name, package_name);
2265
2266         /* update custom */
2267         error_code = _account_update_custom(account, account->id);
2268
2269         return error_code;
2270 }
2271
2272 int _account_insert_to_db(account_s* account, int pid, int uid, int *account_id)
2273 {
2274         _INFO("");
2275         int             error_code = ACCOUNT_ERROR_NONE;
2276         int     ret_transaction = 0;
2277
2278         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2279         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
2280         ACCOUNT_RETURN_VAL((account_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID POINTER IS NULL"));
2281
2282         if (!account->user_name && !account->display_name && !account->email_address) {
2283                 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
2284                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2285         }
2286
2287         account_s *data = (account_s*)account;
2288         ACCOUNT_SLOGD("(%s)-(%d) account_insert_to_db: begin_transaction.\n", __FUNCTION__, __LINE__);
2289
2290         pthread_mutex_lock(&account_mutex);
2291
2292         /* transaction control required*/
2293         ret_transaction = _account_begin_transaction();
2294
2295         if(_account_db_err_code() == SQLITE_PERM){
2296                 pthread_mutex_unlock(&account_mutex);
2297                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2298                 return ACCOUNT_ERROR_PERMISSION_DENIED;
2299         }
2300
2301         if (ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY) {
2302                 ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction);
2303                 pthread_mutex_unlock(&account_mutex);
2304                 return ACCOUNT_ERROR_DATABASE_BUSY;
2305         }else if (ret_transaction != ACCOUNT_ERROR_NONE) {
2306                 ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction);
2307                 pthread_mutex_unlock(&account_mutex);
2308                 return ret_transaction;
2309         }
2310
2311         *account_id = _account_get_next_sequence(ACCOUNT_TABLE);
2312         data->id = *account_id;
2313
2314         char* appid = NULL;
2315         appid = _account_get_current_appid(pid);
2316
2317         if(!appid)
2318         {
2319                 _INFO("");
2320                 // API caller cannot be recognized
2321                 ret_transaction = _account_end_transaction(FALSE);
2322                 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
2323                 pthread_mutex_unlock(&account_mutex);
2324                 return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
2325         }
2326
2327         _INFO("");
2328         char* verified_appid = NULL;
2329         error_code  = _account_check_account_type_with_appid_group(uid, appid, &verified_appid);//FIX
2330         _ACCOUNT_FREE(appid);
2331         if(error_code != ACCOUNT_ERROR_NONE)
2332         {
2333                 _ERR("error_code = %d", error_code);
2334                 ret_transaction = _account_end_transaction(FALSE);
2335                 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
2336                 _ACCOUNT_FREE(verified_appid);
2337                 pthread_mutex_unlock(&account_mutex);
2338                 return error_code;
2339         }
2340
2341         if(verified_appid)
2342         {
2343                 _INFO("");
2344                 error_code = _account_check_duplicated(data, verified_appid);
2345                 if (error_code != ACCOUNT_ERROR_NONE) {
2346                         _INFO("");
2347                         ret_transaction = _account_end_transaction(FALSE);
2348                         ACCOUNT_DEBUG("_account_check_duplicated(), rollback insert query(%x)!!!!\n", ret_transaction);
2349                         *account_id = -1;
2350                         pthread_mutex_unlock(&account_mutex);
2351                         return error_code;
2352                 }
2353                 if(!_account_check_add_more_account(verified_appid)) {
2354                         ret_transaction = _account_end_transaction(FALSE);
2355                         ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
2356                         pthread_mutex_unlock(&account_mutex);
2357                         _ACCOUNT_FREE(verified_appid);
2358                         return ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
2359                 }
2360
2361                 _ACCOUNT_FREE(data->package_name);
2362                 data->package_name = _account_get_text(verified_appid);
2363                 _ACCOUNT_FREE(verified_appid);
2364         }
2365
2366         if(!_account_check_add_more_account(data->package_name))
2367         {
2368                 _INFO("");
2369                 ret_transaction = _account_end_transaction(FALSE);
2370                 ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
2371                 pthread_mutex_unlock(&account_mutex);
2372                 return ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
2373         }
2374
2375         error_code = _encrypt_access_token(data);
2376         if (error_code != ACCOUNT_ERROR_NONE)
2377         {
2378                 _ERR("_encrypt_access_token error");
2379                 return error_code;
2380         }
2381
2382         error_code = _account_execute_insert_query(data);
2383
2384         if (error_code != ACCOUNT_ERROR_NONE)
2385         {
2386                 _INFO("");
2387                 ret_transaction = _account_end_transaction(FALSE);
2388                 ACCOUNT_ERROR("INSERT account fail, rollback insert query(%x)!!!!\n", ret_transaction);
2389                 *account_id = -1;
2390                 pthread_mutex_unlock(&account_mutex);
2391                 return error_code;
2392         }
2393
2394         _INFO("");
2395         error_code = _account_insert_capability(data, *account_id);
2396         if (error_code != ACCOUNT_ERROR_NONE)
2397         {
2398                 _INFO("");
2399                 ret_transaction = _account_end_transaction(FALSE);
2400                 ACCOUNT_ERROR("INSERT capability fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
2401                 *account_id = -1;
2402                 pthread_mutex_unlock(&account_mutex);
2403                 return error_code;
2404         }
2405
2406         _INFO("");
2407         error_code = _account_insert_custom(data, *account_id);
2408         if (error_code != ACCOUNT_ERROR_NONE)
2409         {
2410                 ret_transaction = _account_end_transaction(FALSE);
2411                 ACCOUNT_ERROR("INSERT custom fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
2412                 *account_id = -1;
2413                 pthread_mutex_unlock(&account_mutex);
2414                 return error_code;
2415         }
2416
2417         _INFO("");
2418
2419         pthread_mutex_unlock(&account_mutex);
2420         _account_end_transaction(TRUE);
2421         ACCOUNT_SLOGD("(%s)-(%d) account _end_transaction.\n", __FUNCTION__, __LINE__);
2422
2423         char buf[64]={0,};
2424         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_INSERT, *account_id);
2425         _account_insert_delete_update_notification_send(buf);
2426         _INFO("account _notification_send end.");
2427
2428         return ACCOUNT_ERROR_NONE;
2429
2430 }
2431
2432 int _account_query_capability_by_account_id(capability_cb callback, int account_id, void *user_data )
2433 {
2434         int                     error_code = ACCOUNT_ERROR_NONE;
2435         account_stmt    hstmt = NULL;
2436         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2437         int                     rc = 0;
2438
2439         ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2440         ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
2441         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2442
2443         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2444
2445         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
2446         hstmt = _account_prepare_query(query);
2447
2448         if( _account_db_err_code() == SQLITE_PERM ){
2449                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2450                 return ACCOUNT_ERROR_PERMISSION_DENIED;
2451         }
2452
2453         rc = _account_query_step(hstmt);
2454         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2455
2456         account_capability_s* capability_record = NULL;
2457
2458         while (rc == SQLITE_ROW) {
2459                 bool cb_ret = FALSE;
2460                 capability_record = (account_capability_s*) malloc(sizeof(account_capability_s));
2461
2462                 if (capability_record == NULL) {
2463                         ACCOUNT_FATAL("malloc Failed");
2464                         break;
2465                 }
2466
2467                 ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s));
2468
2469                 _account_convert_column_to_capability(hstmt, capability_record);
2470
2471                 cb_ret = callback(capability_record->type, capability_record->value, user_data);
2472
2473                 _account_free_capability_with_items(capability_record);
2474
2475                 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
2476
2477                 rc = _account_query_step(hstmt);
2478         }
2479
2480         rc = _account_query_finalize(hstmt);
2481         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2482         hstmt = NULL;
2483
2484         error_code = ACCOUNT_ERROR_NONE;
2485
2486 CATCH:
2487         if (hstmt != NULL) {
2488                 rc = _account_query_finalize(hstmt);
2489                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2490                 hstmt = NULL;
2491         }
2492
2493         pthread_mutex_unlock(&account_mutex);
2494         return error_code;
2495 }
2496
2497 GSList* _account_get_capability_list_by_account_id(int account_id, int *error_code)
2498 {
2499         *error_code = ACCOUNT_ERROR_NONE;
2500         account_stmt    hstmt = NULL;
2501         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2502         int                     rc = 0;
2503         GSList* capability_list = NULL;
2504
2505         ACCOUNT_RETURN_VAL((account_id > 0), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("ACCOUNT INDEX IS LESS THAN 0"));
2506         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
2507
2508         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2509
2510         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
2511         hstmt = _account_prepare_query(query);
2512
2513         if( _account_db_err_code() == SQLITE_PERM ){
2514                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2515                 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
2516                 return NULL;
2517         }
2518
2519         rc = _account_query_step(hstmt);
2520         ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2521
2522         account_capability_s* capability_record = NULL;
2523
2524         while (rc == SQLITE_ROW) {
2525                 capability_record = (account_capability_s*) malloc(sizeof(account_capability_s));
2526
2527                 if (capability_record == NULL) {
2528                         ACCOUNT_FATAL("malloc Failed");
2529                         break;
2530                 }
2531
2532                 ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s));
2533
2534                 _account_convert_column_to_capability(hstmt, capability_record);
2535
2536                 //cb_ret = callback(capability_record->type, capability_record->value, user_data);
2537
2538                 //_account_free_capability_items(capability_record);
2539                 //_ACCOUNT_FREE(capability_record);
2540
2541                 //ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
2542
2543                 capability_list = g_slist_append(capability_list, capability_record);
2544                 rc = _account_query_step(hstmt);
2545         }
2546
2547         rc = _account_query_finalize(hstmt);
2548         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
2549         hstmt = NULL;
2550
2551         *error_code = ACCOUNT_ERROR_NONE;
2552
2553 CATCH:
2554         if (hstmt != NULL)
2555         {
2556                 rc = _account_query_finalize(hstmt);
2557                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
2558                 hstmt = NULL;
2559         }
2560
2561         pthread_mutex_unlock(&account_mutex);
2562         return capability_list;
2563 }
2564
2565 static int _account_compare_old_record(account_s *new_account, int account_id)
2566 {
2567         int                             error_code = ACCOUNT_ERROR_NONE;
2568         account_stmt    hstmt = NULL;
2569         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2570         int                             rc = 0;
2571         account_s *old_account = NULL;
2572
2573         ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2574         ACCOUNT_RETURN_VAL((new_account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2575         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2576
2577         old_account = (account_s*)calloc(1, sizeof(account_s));
2578         if (old_account == NULL) {
2579                 _ERR("Out of Memory");
2580                 return ACCOUNT_ERROR_OUT_OF_MEMORY;
2581         }
2582
2583         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2584
2585         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
2586         hstmt = _account_prepare_query(query);
2587
2588         rc = _account_query_step(hstmt);
2589         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2590
2591         while (rc == SQLITE_ROW) {
2592                 _account_convert_column_to_account(hstmt, old_account);
2593                 rc = _account_query_step(hstmt);
2594         }
2595
2596         rc = _account_query_finalize(hstmt);
2597         ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2598         hstmt = NULL;
2599
2600         // get capability
2601         error_code = _account_query_capability_by_account_id(_account_get_capability_text_cb, old_account->id, (void*)old_account);
2602         ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
2603
2604         // get custom text
2605         error_code = _account_query_custom_by_account_id(_account_get_custom_text_cb, old_account->id, (void*)old_account);
2606         ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
2607
2608         // compare
2609
2610         new_account->id = old_account->id;
2611
2612         //user name
2613         if(!new_account->user_name) {
2614                 if(old_account->user_name)
2615                         new_account->user_name = _account_get_text(old_account->user_name);
2616         }
2617
2618         // display name
2619         if(!new_account->display_name) {
2620                 if(old_account->display_name)
2621                         new_account->display_name = _account_get_text(old_account->display_name);
2622         }
2623
2624         // email address
2625         if(!new_account->email_address) {
2626                 if(old_account->email_address)
2627                         new_account->email_address = _account_get_text(old_account->email_address);
2628         }
2629
2630         // domain name
2631         if(!new_account->domain_name) {
2632                 if(old_account->domain_name)
2633                         new_account->domain_name = _account_get_text(old_account->domain_name);
2634         }
2635
2636         // icon path
2637         if(!new_account->icon_path) {
2638                 if(old_account->icon_path)
2639                         new_account->icon_path = _account_get_text(old_account->icon_path);
2640         }
2641
2642         // source
2643         if(!new_account->source) {
2644                 if(old_account->source)
2645                         new_account->source = _account_get_text(old_account->source);
2646         }
2647
2648         _ACCOUNT_FREE(new_account->package_name);
2649         new_account->package_name = _account_get_text(old_account->package_name);
2650
2651         // access token
2652         if(!new_account->access_token) {
2653                 if(old_account->access_token)
2654                         new_account->access_token = _account_get_text(old_account->access_token);
2655         }
2656
2657         // user text
2658         int i;
2659         for(i=0;i<USER_TXT_CNT;i++) {
2660                 if(!new_account->user_data_txt[i]) {
2661                         if(old_account->user_data_txt[i])
2662                                 new_account->user_data_txt[i] = _account_get_text(old_account->user_data_txt[i]);
2663                 }
2664         }
2665
2666         // auth type
2667         if(new_account->auth_type == ACCOUNT_AUTH_TYPE_INVALID) {
2668                 new_account->auth_type = old_account->auth_type;
2669         }
2670
2671         //secret
2672         if(new_account->secret== ACCOUNT_SECRECY_INVALID) {
2673                 new_account->secret = old_account->secret;
2674         }
2675
2676         // sync support
2677         if(new_account->sync_support == ACCOUNT_SYNC_INVALID) {
2678                 new_account->sync_support = old_account->sync_support;
2679         }
2680
2681         // user int
2682         for(i=0;i<USER_INT_CNT;i++) {
2683                 if(new_account->user_data_int[i] == 0) {
2684                                 new_account->user_data_int[i] = old_account->user_data_int[i];
2685                 }
2686         }
2687
2688         // capability
2689
2690         // user custom table
2691
2692 CATCH:
2693                 if (old_account)
2694                         _account_free_account_with_items(old_account);
2695
2696                 if (hstmt != NULL) {
2697                         rc = _account_query_finalize(hstmt);
2698                         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2699                         hstmt = NULL;
2700                 }
2701
2702         return ACCOUNT_ERROR_NONE;
2703 }
2704
2705 static int _account_get_package_name_from_account_id(int account_id, char **package_name)
2706 {
2707         int                             error_code = ACCOUNT_ERROR_NONE;
2708         account_stmt    hstmt = NULL;
2709         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2710         int                             rc = 0;
2711         account_s *old_account = NULL;
2712
2713         ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2714         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2715
2716         old_account = (account_s*)calloc(1, sizeof(account_s));
2717         if (old_account == NULL) {
2718                 _ERR("Out Of memory");
2719                 return ACCOUNT_ERROR_OUT_OF_MEMORY;
2720         }
2721
2722         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2723
2724         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
2725         hstmt = _account_prepare_query(query);
2726
2727         rc = _account_query_step(hstmt);
2728         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2729
2730         while (rc == SQLITE_ROW) {
2731                 _account_convert_column_to_account(hstmt, old_account);
2732                 rc = _account_query_step(hstmt);
2733         }
2734
2735         rc = _account_query_finalize(hstmt);
2736         ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2737         hstmt = NULL;
2738
2739         // get package name.
2740         *package_name = _account_get_text(old_account->package_name);
2741
2742
2743         CATCH:
2744                 if (old_account) {
2745                         _account_free_account_with_items(old_account);
2746                 }
2747
2748                 if (hstmt != NULL) {
2749                         rc = _account_query_finalize(hstmt);
2750                         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2751                         hstmt = NULL;
2752                 }
2753
2754         return error_code;
2755
2756 }
2757
2758 static int _account_update_account(int pid, int uid, account_s *account, int account_id)
2759 {
2760         int                             rc = 0, binding_count =0;
2761         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2762         int                             error_code = ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0;
2763         account_stmt    hstmt = NULL;
2764
2765         if (!account->package_name) {
2766                 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
2767                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2768         }
2769
2770         /* Check permission of requested appid */
2771         char* current_appid = NULL;
2772         char *package_name = NULL;
2773
2774         current_appid = _account_get_current_appid(pid);
2775         error_code = _account_get_package_name_from_account_id(account_id, &package_name);
2776
2777         if(error_code != ACCOUNT_ERROR_NONE || package_name == NULL){
2778                 ACCOUNT_ERROR("No package name with account_id\n");
2779                 _ACCOUNT_FREE(current_appid);
2780                 _ACCOUNT_FREE(package_name);
2781                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2782         }
2783
2784         error_code = _account_check_appid_group_with_package_name(uid, current_appid, package_name);
2785         ACCOUNT_DEBUG( "UPDATE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name);     // TODO: remove the log later.
2786
2787         _ACCOUNT_FREE(current_appid);
2788         _ACCOUNT_FREE(package_name);
2789
2790         if(error_code != ACCOUNT_ERROR_NONE){
2791                 ACCOUNT_ERROR("No permission to update\n");
2792                 return ACCOUNT_ERROR_PERMISSION_DENIED;
2793         }
2794
2795         error_code = _encrypt_access_token(account);
2796         if (error_code != ACCOUNT_ERROR_NONE)
2797         {
2798                 _ERR("_encrypt_access_token error");
2799                 return error_code;
2800         }
2801
2802         error_code = _account_compare_old_record(account, account_id);
2803         if (error_code != ACCOUNT_ERROR_NONE) {
2804                 ACCOUNT_ERROR("_account_compare_old_record fail\n");
2805                 return error_code;
2806         }
2807
2808         if( _account_db_err_code() == SQLITE_PERM ){
2809                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2810                 return ACCOUNT_ERROR_PERMISSION_DENIED;
2811         } else if( _account_db_err_code() == SQLITE_BUSY ){
2812                 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
2813                 return ACCOUNT_ERROR_DATABASE_BUSY;
2814         }
2815
2816         if (!account->user_name && !account->display_name && !account->email_address) {
2817                 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
2818                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2819         }
2820
2821         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2822
2823         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
2824
2825         count = _account_get_record_count(query);
2826         if (count <= 0) {
2827                 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
2828                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2829         }
2830
2831         /* transaction control required*/
2832         ret_transaction = _account_begin_transaction();
2833         if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
2834                 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
2835                 pthread_mutex_unlock(&account_mutex);
2836                 return ACCOUNT_ERROR_DATABASE_BUSY;
2837         }
2838
2839         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2840         ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
2841                         "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
2842                         "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
2843                         "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
2844
2845         hstmt = _account_prepare_query(query);
2846
2847         if( _account_db_err_code() == SQLITE_PERM ){
2848                 ret_transaction = _account_end_transaction(FALSE);
2849                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2850                 return ACCOUNT_ERROR_PERMISSION_DENIED;
2851         }
2852
2853         ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s)(%x).\n", _account_db_err_msg(), _account_end_transaction(FALSE)));
2854
2855         binding_count = _account_convert_account_to_sql(account, hstmt, query);
2856         _account_query_bind_int(hstmt, binding_count++, account_id);
2857
2858         rc = _account_query_step(hstmt);
2859         if (rc != SQLITE_DONE) {
2860                 ACCOUNT_SLOGE( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
2861         }
2862
2863         rc = _account_query_finalize(hstmt);
2864         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2865         hstmt = NULL;
2866
2867         _INFO("update query=%s", query);
2868
2869         /*update capability*/
2870         error_code = _account_update_capability(account, account_id);
2871         if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2872                 ret_transaction = _account_end_transaction(FALSE);
2873                 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2874                 return error_code;
2875         }
2876
2877         /* update custom */
2878         error_code = _account_update_custom(account, account_id);
2879         if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2880                 ret_transaction = _account_end_transaction(FALSE);
2881                 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2882                 return error_code;
2883         }
2884
2885         ret_transaction = _account_end_transaction(TRUE);
2886
2887         _INFO("update end");
2888         return error_code;
2889 }
2890
2891
2892 static int _account_update_account_ex(account_s *account, int account_id)
2893 {
2894         int                             rc = 0, binding_count =0;
2895         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2896         int                             error_code = ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0;
2897         account_stmt    hstmt = NULL;
2898
2899         if (!account->package_name) {
2900                 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
2901                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2902         }
2903
2904         error_code = _encrypt_access_token(account);
2905         if (error_code != ACCOUNT_ERROR_NONE)
2906         {
2907                 _ERR("_encrypt_access_token error");
2908                 return error_code;
2909         }
2910
2911         error_code = _account_compare_old_record(account, account_id);
2912         if (error_code != ACCOUNT_ERROR_NONE) {
2913                 ACCOUNT_ERROR("_account_compare_old_record fail\n");
2914                 return error_code;
2915         }
2916
2917         if( _account_db_err_code() == SQLITE_PERM ){
2918                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2919                 return ACCOUNT_ERROR_PERMISSION_DENIED;
2920         }
2921
2922         if (!account->user_name && !account->display_name && !account->email_address) {
2923                 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
2924                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2925         }
2926
2927         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2928
2929         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
2930
2931         count = _account_get_record_count(query);
2932         if (count <= 0) {
2933                 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
2934                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2935         }
2936
2937         /* transaction control required*/
2938         ret_transaction = _account_begin_transaction();
2939         if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
2940                 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
2941                 pthread_mutex_unlock(&account_mutex);
2942                 return ACCOUNT_ERROR_DATABASE_BUSY;
2943         }
2944
2945         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2946         ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
2947                         "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
2948                         "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
2949                         "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
2950
2951         hstmt = _account_prepare_query(query);
2952
2953         if( _account_db_err_code() == SQLITE_PERM ){
2954                 ret_transaction = _account_end_transaction(FALSE);
2955                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2956                 return ACCOUNT_ERROR_PERMISSION_DENIED;
2957         }
2958
2959         ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s)(%x).\n", _account_db_err_msg(), _account_end_transaction(FALSE)));
2960
2961         _INFO("account_update_to_db_by_id_ex_p : before convert() : account_id[%d], user_name=%s", account->id, account->user_name);
2962         binding_count = _account_convert_account_to_sql(account, hstmt, query);
2963         _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], user_name=%s", account->id, account->user_name);
2964         _INFO("account_update_to_db_by_id_ex_p : before bind()");
2965         rc = _account_query_bind_int(hstmt, binding_count++, account_id);
2966         _INFO("account_update_to_db_by_id_ex_p : after bind() : ret = %d", rc);
2967
2968         rc = _account_query_step(hstmt);
2969         if (rc != SQLITE_DONE) {
2970                 ACCOUNT_SLOGE( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
2971         }
2972         _INFO("account_update_to_db_by_id_ex_p : after query_step() : ret = %d", rc);
2973
2974         rc = _account_query_finalize(hstmt);
2975         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2976         hstmt = NULL;
2977         _INFO("account_update_to_db_by_id_ex_p : after query_filnalize() : ret = %d", rc);
2978
2979         _INFO("account_update_to_db_by_id_ex_p : before update_capability()");
2980         /*update capability*/
2981         error_code = _account_update_capability(account, account_id);
2982         if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2983                 ret_transaction = _account_end_transaction(FALSE);
2984                 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2985                 return error_code;
2986         }
2987         _INFO("account_update_to_db_by_id_ex_p : after update_capability()");
2988
2989         _INFO("account_update_to_db_by_id_ex_p : before update_custom()");
2990         /* update custom */
2991         error_code = _account_update_custom(account, account_id);
2992         if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2993                 ret_transaction = _account_end_transaction(FALSE);
2994                 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2995                 return error_code;
2996         }
2997         _INFO("account_update_to_db_by_id_ex_p : after update_custom()");
2998
2999         ret_transaction = _account_end_transaction(TRUE);
3000
3001         return error_code;
3002 }
3003
3004
3005 int _account_update_to_db_by_id(int pid, int uid, account_s* account, int account_id)
3006 {
3007         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
3008         ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
3009         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3010         int     error_code = ACCOUNT_ERROR_NONE;
3011         account_s* data = (account_s*)account;
3012
3013         pthread_mutex_lock(&account_mutex);
3014
3015         error_code = _account_update_account(pid, uid, data, account_id);
3016
3017         if(error_code != ACCOUNT_ERROR_NONE) {
3018                 pthread_mutex_unlock(&account_mutex);
3019                 return error_code;
3020         }
3021
3022         pthread_mutex_unlock(&account_mutex);
3023
3024         char buf[64]={0,};
3025         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, account_id);
3026         _account_insert_delete_update_notification_send(buf);
3027
3028         return ACCOUNT_ERROR_NONE;
3029 }
3030
3031 int _account_update_to_db_by_id_ex(account_s* account, int account_id)
3032 {
3033         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
3034         ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
3035         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3036         int     error_code = ACCOUNT_ERROR_NONE;
3037         account_s* data = account;
3038
3039         pthread_mutex_lock(&account_mutex);
3040
3041         _INFO("before update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name);
3042         error_code = _account_update_account_ex(data, account_id);
3043         _INFO("after update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name);
3044
3045         if(error_code != ACCOUNT_ERROR_NONE) {
3046                 pthread_mutex_unlock(&account_mutex);
3047                 return error_code;
3048         }
3049
3050         pthread_mutex_unlock(&account_mutex);
3051
3052         char buf[64]={0,};
3053         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, account_id);
3054         _account_insert_delete_update_notification_send(buf);
3055
3056         return ACCOUNT_ERROR_NONE;
3057 }
3058
3059
3060 int _account_update_to_db_by_user_name(int pid, int uid, account_s* account, const char *user_name, const char *package_name)
3061 {
3062         ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
3063         ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
3064         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3065
3066         int     error_code = ACCOUNT_ERROR_NONE;
3067         account_s *data = (account_s*)account;
3068
3069         pthread_mutex_lock(&account_mutex);
3070
3071         error_code = _account_update_account_by_user_name(pid, uid, data, user_name, package_name);
3072
3073         pthread_mutex_unlock(&account_mutex);
3074
3075         char buf[64]={0,};
3076         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, data->id);
3077         _account_insert_delete_update_notification_send(buf);
3078
3079         return error_code;
3080 }
3081
3082 GSList* _account_db_query_all(int pid)
3083 {
3084         //int                   error_code = ACCOUNT_ERROR_NONE;
3085         account_stmt    hstmt = NULL;
3086         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3087         int                     rc = 0;
3088         GSList                  *account_list = NULL;
3089
3090         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected."));
3091
3092         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3093
3094         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TABLE);
3095         hstmt = _account_prepare_query(query);
3096
3097         if( _account_db_err_code() == SQLITE_PERM ){
3098                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3099                 return NULL;
3100         }
3101
3102         rc = _account_query_step(hstmt);
3103
3104         account_s *account_record = NULL;
3105
3106         if (rc != SQLITE_ROW)
3107         {
3108                 _ERR("The record isn't found");
3109                 goto CATCH;
3110         }
3111
3112         while(rc == SQLITE_ROW) {
3113                 account_record = (account_s*) malloc(sizeof(account_s));
3114
3115                 if (account_record == NULL) {
3116                         ACCOUNT_FATAL("malloc Failed");
3117                         break;
3118                 }
3119
3120                 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3121                 _account_convert_column_to_account(hstmt, account_record);
3122                 account_list = g_slist_append(account_list, account_record);
3123                 rc = _account_query_step(hstmt);
3124         }
3125
3126         rc = _account_query_finalize(hstmt);
3127         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, NULL, ("finalize error"));
3128         hstmt = NULL;
3129
3130         GSList* iter;
3131
3132         for (iter = account_list; iter != NULL; iter = g_slist_next(iter)) {
3133                 account_s *account = NULL;
3134                 account = (account_s*)iter->data;
3135                 _account_query_capability_by_account_id(_account_get_capability_text_cb, account->id, (void*)account);
3136                 _account_query_custom_by_account_id(_account_get_custom_text_cb, account->id, (void*)account);
3137         }
3138
3139 CATCH:
3140         if (hstmt != NULL) {
3141                 rc = _account_query_finalize(hstmt);
3142                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {_account_gslist_account_free(account_list);}, NULL, ("finalize error"));
3143                 hstmt = NULL;
3144         }
3145         if (account_list)
3146         {
3147                 _remove_sensitive_info_from_non_owning_account_slist(pid, account_list);
3148         }
3149         return account_list;
3150 }
3151
3152 int _account_update_sync_status_by_id(int uid, int account_db_id, const int sync_status)
3153 {
3154         int                             error_code = ACCOUNT_ERROR_NONE;
3155         account_stmt    hstmt = NULL;
3156         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3157         int                             rc = 0;
3158         int count =1;
3159
3160         ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
3161         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3162         if ( (sync_status < 0) || (sync_status > ACCOUNT_SYNC_STATUS_RUNNING)) {
3163                 ACCOUNT_SLOGE("(%s)-(%d) sync_status is less than 0 or more than enum max.\n", __FUNCTION__, __LINE__);
3164                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3165         }
3166
3167         pthread_mutex_lock(&account_mutex);
3168
3169         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3170
3171         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_db_id);
3172
3173         rc = _account_get_record_count(query);
3174
3175         if( _account_db_err_code() == SQLITE_PERM ){
3176                 pthread_mutex_unlock(&account_mutex);
3177                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3178                 return ACCOUNT_ERROR_PERMISSION_DENIED;
3179         }
3180
3181         if (rc <= 0) {
3182                 ACCOUNT_SLOGE( "account_update_sync_status_by_id : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
3183                 pthread_mutex_unlock(&account_mutex);
3184                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3185         }
3186
3187         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3188
3189         ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET sync_support=? WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
3190         hstmt = _account_prepare_query(query);
3191
3192         _account_query_bind_int(hstmt, count, sync_status);
3193
3194         rc = _account_query_step(hstmt);
3195
3196         if( _account_db_err_code() == SQLITE_PERM ){
3197                 pthread_mutex_unlock(&account_mutex);
3198                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3199                 return ACCOUNT_ERROR_PERMISSION_DENIED;
3200         }
3201
3202         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_DB_FAILED,
3203                                 ("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg()));
3204
3205         rc = _account_query_finalize(hstmt);
3206         if (rc != ACCOUNT_ERROR_NONE) {
3207                 ACCOUNT_ERROR("_account_query_finalize error");
3208                 pthread_mutex_unlock(&account_mutex);
3209                 return rc;
3210         }
3211         char buf[64]={0,};
3212         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_SYNC_UPDATE, account_db_id);
3213         _account_insert_delete_update_notification_send(buf);
3214
3215         hstmt = NULL;
3216         error_code = ACCOUNT_ERROR_NONE;
3217
3218 CATCH:
3219         if (hstmt != NULL) {
3220                 rc = _account_query_finalize(hstmt);
3221                 pthread_mutex_unlock(&account_mutex);
3222                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3223                 hstmt = NULL;
3224         }
3225
3226         pthread_mutex_unlock(&account_mutex);
3227         return error_code;
3228 }
3229
3230 int _account_query_account_by_account_id(int pid, int account_db_id, account_s *account_record)
3231 {
3232         _INFO("_account_query_account_by_account_id() start, account_db_id=[%d]", account_db_id);
3233
3234         int                             error_code = ACCOUNT_ERROR_NONE;
3235         account_stmt    hstmt = NULL;
3236         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3237         int                             rc = 0;
3238
3239         ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
3240         ACCOUNT_RETURN_VAL(account_record != NULL, {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
3241         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3242
3243         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3244
3245         ACCOUNT_DEBUG("starting db operations");
3246
3247         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
3248         hstmt = _account_prepare_query(query);
3249         rc = _account_db_err_code();
3250         _INFO("after _account_prepare_query, rc=[%d]", rc);
3251
3252         if( rc == SQLITE_PERM ){
3253                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3254                 return ACCOUNT_ERROR_PERMISSION_DENIED;
3255         }
3256
3257         ACCOUNT_DEBUG("before _account_query_step");
3258         rc = _account_query_step(hstmt);
3259         ACCOUNT_DEBUG("after _account_query_step returned [%d]", rc);
3260         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3261
3262         while (rc == SQLITE_ROW) {
3263                 ACCOUNT_DEBUG("before _account_convert_column_to_account");
3264                 _account_convert_column_to_account(hstmt, account_record);
3265                 ACCOUNT_DEBUG("after _account_convert_column_to_account");
3266                 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]);
3267                 rc = _account_query_step(hstmt);
3268         }
3269
3270         ACCOUNT_DEBUG("account_record->id=[%d]", account_record->id);
3271
3272         rc = _account_query_finalize(hstmt);
3273         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3274
3275         ACCOUNT_DEBUG("before _account_query_capability_by_account_id");
3276         _account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record);
3277         ACCOUNT_DEBUG("after _account_query_capability_by_account_id");
3278
3279         ACCOUNT_DEBUG("before _account_query_custom_by_account_id");
3280         _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record);
3281         ACCOUNT_DEBUG("after _account_query_custom_by_account_id");
3282
3283         hstmt = NULL;
3284         error_code = ACCOUNT_ERROR_NONE;
3285
3286 CATCH:
3287         if (hstmt != NULL) {
3288                 rc = _account_query_finalize(hstmt);
3289                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3290                 hstmt = NULL;
3291         }
3292
3293         if (account_record)
3294         {
3295                 _remove_sensitive_info_from_non_owning_account(pid, account_record);
3296         }
3297         pthread_mutex_unlock(&account_mutex);
3298         ACCOUNT_DEBUG("_account_query_account_by_account_id end [%d]", error_code);
3299         return error_code;
3300 }
3301
3302 GList* _account_query_account_by_user_name(int pid, const char *user_name, int *error_code)
3303 {
3304         *error_code = ACCOUNT_ERROR_NONE;
3305         account_stmt    hstmt = NULL;
3306         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3307         int                             rc = 0;
3308         account_s *account_head = NULL;
3309
3310         if (user_name == NULL)
3311         {
3312                 _ERR("USER NAME IS NULL");
3313                 *error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
3314                 goto CATCH;
3315         }
3316
3317         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3318
3319         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ?", ACCOUNT_TABLE);
3320
3321         hstmt = _account_prepare_query(query);
3322
3323         if (_account_db_err_code() == SQLITE_PERM)
3324         {
3325                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3326                 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
3327                 goto CATCH;
3328         }
3329
3330         int binding_count = 1;
3331         _account_query_bind_text(hstmt, binding_count++, user_name);
3332
3333         rc = _account_query_step(hstmt);
3334
3335         if (rc != SQLITE_ROW)
3336         {
3337                 _ERR("The record isn't found");
3338                 *error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
3339                 goto CATCH;
3340         }
3341
3342         int tmp = 0;
3343
3344         account_head = (account_s*) malloc(sizeof(account_s));
3345         if (account_head == NULL) {
3346                 ACCOUNT_FATAL("malloc Failed");
3347                 if (hstmt != NULL) {
3348                         rc = _account_query_finalize(hstmt);
3349
3350                         if (rc != ACCOUNT_ERROR_NONE)
3351                         {
3352                                 _ERR("finalize error");
3353                                 *error_code = rc;
3354                                 goto CATCH;
3355                         }
3356                         hstmt = NULL;
3357                 }
3358                 *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
3359                 goto CATCH;
3360         }
3361         ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3362
3363         while (rc == SQLITE_ROW) {
3364                 account_s* account_record = NULL;
3365
3366                 account_record = (account_s*) malloc(sizeof(account_s));
3367
3368                 if (account_record == NULL) {
3369                         ACCOUNT_FATAL("malloc Failed");
3370                         break;
3371                 }
3372                 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3373
3374                 _account_convert_column_to_account(hstmt, account_record);
3375
3376                 account_head->account_list = g_list_append(account_head->account_list, account_record);
3377
3378                 rc = _account_query_step(hstmt);
3379                 tmp++;
3380         }
3381
3382         rc = _account_query_finalize(hstmt);
3383
3384         if (rc != ACCOUNT_ERROR_NONE)
3385         {
3386                 _ERR("finalize error");
3387                 *error_code = rc;
3388                 goto CATCH;
3389         }
3390
3391         hstmt = NULL;
3392
3393         GList *iter;
3394
3395
3396         tmp = g_list_length(account_head->account_list);
3397
3398         for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3399                 account_h account;
3400                 account = (account_h)iter->data;
3401
3402                 account_s *testaccount = (account_s*)account;
3403
3404                 _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3405                 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3406
3407         }
3408
3409         *error_code = ACCOUNT_ERROR_NONE;
3410
3411 CATCH:
3412         if (hstmt != NULL) {
3413                 rc = _account_query_finalize(hstmt);
3414                 if (rc != ACCOUNT_ERROR_NONE)
3415                 {
3416                         _ERR("finalize error");
3417                         *error_code = rc;
3418                 }
3419                 hstmt = NULL;
3420         }
3421
3422         pthread_mutex_unlock(&account_mutex);
3423         if (account_head)
3424         {
3425                 _remove_sensitive_info_from_non_owning_account_list(pid, account_head->account_list);
3426                 GList* result = account_head->account_list;
3427                 _ACCOUNT_FREE(account_head);
3428                 return result;
3429         }
3430         return NULL;
3431 }
3432
3433 GList*
3434 _account_query_account_by_capability(int pid, const char* capability_type, const int capability_value, int *error_code)
3435 {
3436         *error_code = ACCOUNT_ERROR_NONE;
3437         account_stmt    hstmt = NULL;
3438         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3439         int                     rc = 0;
3440
3441         ACCOUNT_RETURN_VAL((capability_type != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("capability_type IS NULL"));
3442
3443         if ((capability_value  < 0) || (capability_value > ACCOUNT_CAPABILITY_ENABLED)) {
3444                 ACCOUNT_SLOGE("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
3445                 *error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
3446                 return NULL;
3447         }
3448
3449         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
3450
3451         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3452
3453         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=? AND value=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
3454
3455         hstmt = _account_prepare_query(query);
3456
3457         if( _account_db_err_code() == SQLITE_PERM ){
3458                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3459                 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
3460                 return NULL;
3461         }
3462
3463         int binding_count = 1;
3464         _account_query_bind_text(hstmt, binding_count++, capability_type);
3465         _account_query_bind_int(hstmt, binding_count++, capability_value);
3466
3467         rc = _account_query_step(hstmt);
3468
3469         account_s* account_head = NULL;
3470
3471         ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3472
3473         int tmp = 0;
3474
3475         account_head = (account_s*) malloc(sizeof(account_s));
3476         if (account_head == NULL) {
3477                 ACCOUNT_FATAL("malloc Failed");
3478                 if (hstmt != NULL) {
3479                         rc = _account_query_finalize(hstmt);
3480                         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
3481                         hstmt = NULL;
3482                 }
3483                 *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
3484                 return NULL;
3485         }
3486         ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3487
3488         while (rc == SQLITE_ROW) {
3489                 account_s* account_record = NULL;
3490
3491                 account_record = (account_s*) malloc(sizeof(account_s));
3492
3493                 if (account_record == NULL) {
3494                         ACCOUNT_FATAL("malloc Failed");
3495                         break;
3496                 }
3497                 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3498
3499                 _account_convert_column_to_account(hstmt, account_record);
3500
3501                 account_head->account_list = g_list_append(account_head->account_list, account_record);
3502
3503                 rc = _account_query_step(hstmt);
3504                 tmp++;
3505         }
3506
3507         rc = _account_query_finalize(hstmt);
3508         ACCOUNT_CATCH_ERROR_P((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3509         hstmt = NULL;
3510
3511         GList *iter;
3512
3513
3514         tmp = g_list_length(account_head->account_list);
3515
3516         for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3517                 account_h account = NULL;
3518                 account = (account_h)iter->data;
3519                 account_s* testaccount = (account_s*)account;
3520
3521                 _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3522                 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3523
3524         }
3525
3526
3527         *error_code = ACCOUNT_ERROR_NONE;
3528
3529 CATCH:
3530         if (hstmt != NULL) {
3531                 rc = _account_query_finalize(hstmt);
3532                 if ( rc != ACCOUNT_ERROR_NONE ) {
3533                         *error_code = rc;
3534                         _ERR("finalize error");
3535                 }
3536                 hstmt = NULL;
3537         }
3538
3539         if( *error_code != ACCOUNT_ERROR_NONE && account_head ) {
3540                 _account_glist_account_free(account_head->account_list);
3541                 _ACCOUNT_FREE(account_head);
3542                 account_head = NULL;
3543         }
3544
3545         pthread_mutex_unlock(&account_mutex);
3546
3547         if (account_head)
3548         {
3549                 _remove_sensitive_info_from_non_owning_account_list(pid, account_head->account_list);
3550                 GList* result = account_head->account_list;
3551                 _ACCOUNT_FREE(account_head);
3552                 return result;
3553         }
3554         return NULL;
3555 }
3556
3557 GList* _account_query_account_by_capability_type(int pid, const char* capability_type, int *error_code)
3558 {
3559         *error_code = ACCOUNT_ERROR_NONE;
3560         account_stmt    hstmt = NULL;
3561         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3562         int                     rc = 0;
3563
3564         ACCOUNT_RETURN_VAL((capability_type != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("capability_type IS NULL"));
3565         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;},
3566                                            NULL, ("The database isn't connected."));
3567
3568         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3569
3570         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
3571
3572         hstmt = _account_prepare_query(query);
3573
3574         if( _account_db_err_code() == SQLITE_PERM ){
3575                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3576                 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
3577                 return NULL;
3578         }
3579
3580         int binding_count = 1;
3581         _account_query_bind_text(hstmt, binding_count++, capability_type);
3582
3583         rc = _account_query_step(hstmt);
3584
3585         account_s* account_head = NULL;
3586
3587         ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3588
3589         int tmp = 0;
3590
3591         account_head = (account_s*) malloc(sizeof(account_s));
3592         if (account_head == NULL) {
3593                 ACCOUNT_FATAL("malloc Failed");
3594                 if (hstmt != NULL) {
3595                         rc = _account_query_finalize(hstmt);
3596                         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
3597                         hstmt = NULL;
3598                 }
3599                 *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
3600                 return NULL;
3601         }
3602         ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3603
3604         while (rc == SQLITE_ROW) {
3605                 account_s* account_record = NULL;
3606
3607                 account_record = (account_s*) malloc(sizeof(account_s));
3608
3609                 if (account_record == NULL) {
3610                         ACCOUNT_FATAL("malloc Failed");
3611                         break;
3612                 }
3613                 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3614
3615                 _account_convert_column_to_account(hstmt, account_record);
3616
3617                 account_head->account_list = g_list_append(account_head->account_list, account_record);
3618
3619                 rc = _account_query_step(hstmt);
3620                 tmp++;
3621         }
3622
3623         rc = _account_query_finalize(hstmt);
3624         ACCOUNT_CATCH_ERROR_P((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3625         hstmt = NULL;
3626
3627         GList *iter;
3628
3629
3630         tmp = g_list_length(account_head->account_list);
3631
3632         for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3633                 account_s* testaccount = (account_s*)iter->data;
3634
3635                 _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3636                 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3637
3638         }
3639
3640         *error_code = ACCOUNT_ERROR_NONE;
3641
3642 CATCH:
3643         if (hstmt != NULL)
3644         {
3645                 rc = _account_query_finalize(hstmt);
3646                 if (rc != ACCOUNT_ERROR_NONE) {
3647                         *error_code = rc;
3648                         _ERR("finalize error");
3649                 }
3650                 hstmt = NULL;
3651         }
3652
3653         if( (*error_code != ACCOUNT_ERROR_NONE) && account_head ) {
3654                 _account_glist_account_free(account_head->account_list);
3655                 _ACCOUNT_FREE(account_head);
3656                 account_head = NULL;
3657         }
3658
3659         pthread_mutex_unlock(&account_mutex);
3660
3661         if (account_head)
3662         {
3663                 _remove_sensitive_info_from_non_owning_account_list(pid, account_head->account_list);
3664                 GList* result = account_head->account_list;
3665                 _ACCOUNT_FREE(account_head);
3666                 return result;
3667         }
3668         return NULL;
3669 }
3670
3671 GList* _account_query_account_by_package_name(int pid,const char* package_name, int *error_code)
3672 {
3673         _INFO("_account_query_account_by_package_name");
3674
3675         *error_code = ACCOUNT_ERROR_NONE;
3676         account_stmt    hstmt = NULL;
3677         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3678         int                     rc = 0;
3679
3680         ACCOUNT_RETURN_VAL((package_name != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("PACKAGE NAME IS NULL"));
3681         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
3682
3683         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3684
3685         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE package_name=?", ACCOUNT_TABLE);
3686
3687         hstmt = _account_prepare_query(query);
3688
3689         if( _account_db_err_code() == SQLITE_PERM ){
3690                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3691                 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
3692                 return NULL;
3693         }
3694
3695         int binding_count = 1;
3696         _account_query_bind_text(hstmt, binding_count++, package_name);
3697
3698         rc = _account_query_step(hstmt);
3699
3700         account_s* account_head = NULL;
3701
3702         ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.(%s)\n", package_name));
3703
3704         int tmp = 0;
3705
3706         account_head = (account_s*) malloc(sizeof(account_s));
3707         if (account_head == NULL) {
3708                 ACCOUNT_FATAL("malloc Failed");
3709                 if (hstmt != NULL) {
3710                         rc = _account_query_finalize(hstmt);
3711                         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
3712                         hstmt = NULL;
3713                 }
3714                 *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
3715                 return NULL;
3716         }
3717         ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3718
3719         while (rc == SQLITE_ROW) {
3720                 account_s* account_record = NULL;
3721
3722                 account_record = (account_s*) malloc(sizeof(account_s));
3723
3724                 if (account_record == NULL) {
3725                         ACCOUNT_FATAL("malloc Failed");
3726                         break;
3727                 }
3728                 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3729
3730                 _account_convert_column_to_account(hstmt, account_record);
3731
3732                 _INFO("Adding account_list");
3733                 account_head->account_list = g_list_append(account_head->account_list, account_record);
3734
3735                 rc = _account_query_step(hstmt);
3736                 tmp++;
3737         }
3738
3739         rc = _account_query_finalize(hstmt);
3740         ACCOUNT_CATCH_ERROR_P((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3741         hstmt = NULL;
3742
3743         GList *iter;
3744
3745         tmp = g_list_length(account_head->account_list);
3746
3747         for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3748                 account_s* testaccount = (account_s*)iter->data;
3749
3750                 _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3751                 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3752         }
3753
3754         *error_code = ACCOUNT_ERROR_NONE;
3755
3756 CATCH:
3757         if (hstmt != NULL)
3758         {
3759                 rc = _account_query_finalize(hstmt);
3760                 if (rc != ACCOUNT_ERROR_NONE) {
3761                         *error_code = rc;
3762                         _ERR("finalize error");
3763                 }
3764                 hstmt = NULL;
3765         }
3766
3767         pthread_mutex_unlock(&account_mutex);
3768
3769         if( (*error_code != ACCOUNT_ERROR_NONE) && account_head ) {
3770                 _account_glist_account_free(account_head->account_list);
3771                 _ACCOUNT_FREE(account_head);
3772                 account_head = NULL;
3773         }
3774
3775         if ((*error_code == ACCOUNT_ERROR_NONE) && account_head != NULL)
3776         {
3777                 _INFO("Returning account_list");
3778                 _remove_sensitive_info_from_non_owning_account_list(pid,account_head->account_list);
3779                 GList* result = account_head->account_list;
3780                 _ACCOUNT_FREE(account_head);
3781                 return result;
3782         }
3783         return NULL;
3784 }
3785
3786 int _account_delete(int pid, int uid, int account_id)
3787 {
3788         int                             error_code = ACCOUNT_ERROR_NONE;
3789         account_stmt    hstmt = NULL;
3790         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3791         int                             rc = 0;
3792         int                             ret_transaction = 0;
3793         bool                    is_success = FALSE;
3794
3795         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3796
3797         int count = -1;
3798         /* Check requested ID to delete */
3799         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id=%d", ACCOUNT_TABLE, account_id);
3800
3801         count = _account_get_record_count(query);
3802
3803         if( _account_db_err_code() == SQLITE_PERM ){
3804                 pthread_mutex_unlock(&account_mutex);
3805                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3806                 return ACCOUNT_ERROR_PERMISSION_DENIED;
3807         }
3808
3809         if (count <= 0) {
3810                 ACCOUNT_ERROR("account id(%d) is not exist. count(%d)\n", account_id, count);
3811                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3812         }
3813
3814         /* Check permission of requested appid */
3815         char* current_appid = NULL;
3816         char *package_name = NULL;
3817
3818         current_appid = _account_get_current_appid(pid);
3819
3820         error_code = _account_get_package_name_from_account_id(account_id, &package_name);
3821
3822         if(error_code != ACCOUNT_ERROR_NONE){
3823                 ACCOUNT_ERROR("No package name with account_id\n");
3824                 _ACCOUNT_FREE(current_appid);
3825                 _ACCOUNT_FREE(package_name);
3826                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3827         }
3828         ACCOUNT_DEBUG( "DELETE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name);
3829
3830         error_code = _account_check_appid_group_with_package_name(uid, current_appid, package_name);
3831
3832         _ACCOUNT_FREE(current_appid);
3833         _ACCOUNT_FREE(package_name);
3834
3835         if(error_code != ACCOUNT_ERROR_NONE){
3836                 ACCOUNT_ERROR("No permission to delete\n");
3837                 return ACCOUNT_ERROR_PERMISSION_DENIED;
3838         }
3839
3840         /* transaction control required*/
3841         ret_transaction = _account_begin_transaction();
3842
3843         if( _account_db_err_code() == SQLITE_PERM ){
3844                 pthread_mutex_unlock(&account_mutex);
3845                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3846                 return ACCOUNT_ERROR_PERMISSION_DENIED;
3847         }
3848
3849         if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
3850                 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
3851                 pthread_mutex_unlock(&account_mutex);
3852                 return ACCOUNT_ERROR_DATABASE_BUSY;
3853         }
3854
3855         if (ret_transaction != ACCOUNT_ERROR_NONE) {
3856                 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3857                 pthread_mutex_unlock(&account_mutex);
3858                 return ret_transaction;
3859         }
3860
3861         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3862         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
3863
3864         hstmt = _account_prepare_query(query);
3865
3866         if( _account_db_err_code() == SQLITE_PERM ){
3867                 pthread_mutex_unlock(&account_mutex);
3868                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3869                 return ACCOUNT_ERROR_PERMISSION_DENIED;
3870         }
3871
3872         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3873                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3874
3875         rc = _account_query_step(hstmt);
3876         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3877
3878         rc = _account_query_finalize(hstmt);
3879
3880         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3881         hstmt = NULL;
3882
3883         ACCOUNT_MEMSET(query, 0, sizeof(query));
3884
3885         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
3886
3887         hstmt = _account_prepare_query(query);
3888         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3889                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3890
3891         rc = _account_query_step(hstmt);
3892         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
3893
3894         rc = _account_query_finalize(hstmt);
3895         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3896         hstmt = NULL;
3897
3898         /* delete custom data */
3899         ACCOUNT_MEMSET(query, 0, sizeof(query));
3900
3901         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
3902
3903         hstmt = _account_prepare_query(query);
3904
3905         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3906                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3907
3908         rc = _account_query_step(hstmt);
3909         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
3910
3911         rc = _account_query_finalize(hstmt);
3912         ACCOUNT_CATCH_ERROR(rc == ACCOUNT_ERROR_NONE, {}, rc, ("finalize error", account_id, rc));
3913         hstmt = NULL;
3914
3915         is_success = TRUE;
3916
3917 CATCH:
3918         if (hstmt != NULL) {
3919                 rc = _account_query_finalize(hstmt);
3920                 if(rc != ACCOUNT_ERROR_NONE ){
3921                         ACCOUNT_ERROR("rc (%d)", rc);
3922                         is_success = FALSE;
3923                 }
3924
3925                 hstmt = NULL;
3926         }
3927
3928         ret_transaction = _account_end_transaction(is_success);
3929
3930         if (ret_transaction != ACCOUNT_ERROR_NONE) {
3931                 ACCOUNT_ERROR("account_delete:_account_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3932         } else {
3933                 if (is_success == true) {
3934                         char buf[64]={0,};
3935                         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, account_id);
3936                         _account_insert_delete_update_notification_send(buf);
3937                 }
3938         }
3939
3940         pthread_mutex_unlock(&account_mutex);
3941
3942         return error_code;
3943
3944 }
3945
3946 static int _account_query_account_by_username_and_package(const char* username, const char* package_name, account_h *account)
3947 {
3948         //FIXME
3949         //return -1;
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         int                             binding_count = 1;
3955
3956         ACCOUNT_RETURN_VAL((username != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("username IS NULL"));
3957         ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name IS NULL"));
3958         ACCOUNT_RETURN_VAL((*account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
3959         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3960
3961         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3962
3963         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
3964         hstmt = _account_prepare_query(query);
3965
3966         if( _account_db_err_code() == SQLITE_PERM ){
3967                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3968                 return ACCOUNT_ERROR_PERMISSION_DENIED;
3969         }
3970
3971         _account_query_bind_text(hstmt, binding_count++, username);
3972         _account_query_bind_text(hstmt, binding_count++, package_name);
3973
3974         rc = _account_query_step(hstmt);
3975         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3976
3977         account_s *account_record = (account_s *)(*account);
3978
3979         while (rc == SQLITE_ROW) {
3980                 _account_convert_column_to_account(hstmt, account_record);
3981                 rc = _account_query_step(hstmt);
3982         }
3983
3984         rc = _account_query_finalize(hstmt);
3985         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3986         _account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record);
3987         _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record);
3988
3989         hstmt = NULL;
3990         error_code = ACCOUNT_ERROR_NONE;
3991
3992 CATCH:
3993         if (hstmt != NULL) {
3994                 rc = _account_query_finalize(hstmt);
3995                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3996                 hstmt = NULL;
3997         }
3998
3999         pthread_mutex_unlock(&account_mutex);
4000         return error_code;
4001 }
4002
4003 int _account_create(account_h *account)
4004 {
4005         if (!account) {
4006                 ACCOUNT_SLOGE("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__);
4007                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4008         }
4009
4010         account_s *data = (account_s*)malloc(sizeof(account_s));
4011
4012         if (data == NULL) {
4013                 ACCOUNT_FATAL("Memory Allocation Failed");
4014                 return ACCOUNT_ERROR_OUT_OF_MEMORY;
4015         }
4016         ACCOUNT_MEMSET(data, 0, sizeof(account_s));
4017
4018         /*Setting account as visible by default*/
4019         data->secret = ACCOUNT_SECRECY_VISIBLE;
4020
4021         /*Setting account as not supporting sync by default*/
4022         data->sync_support = ACCOUNT_SYNC_NOT_SUPPORT;
4023
4024         *account = (account_h)data;
4025
4026         return ACCOUNT_ERROR_NONE;
4027 }
4028
4029 int _account_destroy(account_h account)
4030 {
4031         account_s *data = (account_s*)account;
4032
4033         ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account handle is null!"));
4034
4035         _account_free_account_with_items(data);
4036
4037         return ACCOUNT_ERROR_NONE;
4038 }
4039
4040 int _account_get_account_id(account_s* account, int *account_id)
4041 {
4042         if (!account) {
4043                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4044         }
4045         if (!account_id) {
4046                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4047         }
4048
4049         *account_id = account->id;
4050
4051         return ACCOUNT_ERROR_NONE;
4052 }
4053
4054 int _account_delete_from_db_by_user_name(int pid, int uid, const char *user_name, const char *package_name)
4055 {
4056         _INFO("[%s][%s]", user_name, package_name);
4057
4058         int                     error_code = ACCOUNT_ERROR_NONE;
4059         account_stmt    hstmt = NULL;
4060         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4061         int                     rc = 0;
4062         int                     ret_transaction = 0;
4063         bool                    is_success = FALSE;
4064         account_h               account = NULL;
4065         int                     binding_count = 1;
4066         int                             account_id = -1;
4067
4068         ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is null!"));
4069         ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
4070         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4071
4072         /* Check permission of requested appid */
4073         char* current_appid = NULL;
4074         char* package_name_temp = NULL;
4075
4076         current_appid = _account_get_current_appid(pid);
4077
4078         package_name_temp = _account_get_text(package_name);
4079
4080         ACCOUNT_DEBUG( "DELETE:user_name[%s],current_appid[%s], package_name[%s]", user_name, current_appid, package_name_temp);
4081
4082         error_code = _account_check_appid_group_with_package_name(uid, current_appid, package_name_temp);
4083
4084         _ACCOUNT_FREE(current_appid);
4085         _ACCOUNT_FREE(package_name_temp);
4086
4087         if(error_code != ACCOUNT_ERROR_NONE){
4088                 ACCOUNT_ERROR("No permission to delete\n");
4089                 return ACCOUNT_ERROR_PERMISSION_DENIED;
4090         }
4091
4092         rc = _account_create(&account);
4093         rc = _account_query_account_by_username_and_package(user_name, package_name, &account);
4094
4095         _INFO("");
4096
4097         if( _account_db_err_code() == SQLITE_PERM )
4098         {
4099                 _account_destroy(account);
4100                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4101                 return ACCOUNT_ERROR_PERMISSION_DENIED;
4102         }
4103
4104         _INFO("");
4105         account_s* account_data = (account_s*)account;
4106
4107         rc = _account_get_account_id(account_data, &account_id);
4108
4109         rc = _account_destroy(account);
4110
4111         /* transaction control required*/
4112         ret_transaction = _account_begin_transaction();
4113
4114         if( _account_db_err_code() == SQLITE_PERM )
4115         {
4116                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4117                 pthread_mutex_unlock(&account_mutex);
4118                 return ACCOUNT_ERROR_PERMISSION_DENIED;
4119         }
4120
4121         _INFO("");
4122         if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY )
4123         {
4124                 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
4125                 pthread_mutex_unlock(&account_mutex);
4126                 return ACCOUNT_ERROR_DATABASE_BUSY;
4127         }
4128         else if (ret_transaction != ACCOUNT_ERROR_NONE) {
4129                 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
4130                 pthread_mutex_unlock(&account_mutex);
4131                 return ret_transaction;
4132         }
4133
4134         /* delete custom data */
4135         ACCOUNT_MEMSET(query, 0, sizeof(query));
4136         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = ?", ACCOUNT_CUSTOM_TABLE);
4137
4138         hstmt = _account_prepare_query(query);
4139
4140         if( _account_db_err_code() == SQLITE_PERM ){
4141                 _account_end_transaction(FALSE);
4142                 pthread_mutex_unlock(&account_mutex);
4143                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4144                 return ACCOUNT_ERROR_PERMISSION_DENIED;
4145         }
4146
4147         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4148                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4149
4150         _account_query_bind_int(hstmt, binding_count++, account_id);
4151
4152         rc = _account_query_step(hstmt);
4153         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4154
4155         rc = _account_query_finalize(hstmt);
4156         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4157         hstmt = NULL;
4158
4159         /* delete capability */
4160         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", CAPABILITY_TABLE);
4161
4162         hstmt = _account_prepare_query(query);
4163
4164         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4165                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4166
4167         binding_count = 1;
4168         _account_query_bind_text(hstmt, binding_count++, user_name);
4169         _account_query_bind_text(hstmt, binding_count++, package_name);
4170
4171         rc = _account_query_step(hstmt);
4172         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4173
4174         rc = _account_query_finalize(hstmt);
4175         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4176         hstmt = NULL;
4177
4178         ACCOUNT_MEMSET(query, 0, sizeof(query));
4179
4180         _INFO("");
4181         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
4182
4183         hstmt = _account_prepare_query(query);
4184         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4185                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4186
4187         _INFO("");
4188         binding_count = 1;
4189         _account_query_bind_text(hstmt, binding_count++, user_name);
4190         _account_query_bind_text(hstmt, binding_count++, package_name);
4191
4192         rc = _account_query_step(hstmt);
4193         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));
4194
4195         rc = _account_query_finalize(hstmt);
4196         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4197         is_success = TRUE;
4198
4199         hstmt = NULL;
4200
4201 CATCH:
4202         if (hstmt != NULL) {
4203                 rc = _account_query_finalize(hstmt);
4204                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4205                 hstmt = NULL;
4206         }
4207
4208         ret_transaction = _account_end_transaction(is_success);
4209
4210         if (ret_transaction != ACCOUNT_ERROR_NONE) {
4211                 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
4212         } else {
4213                 if (is_success == true) {
4214                         char buf[64]={0,};
4215                         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, account_id);
4216                         _account_insert_delete_update_notification_send(buf);
4217                 }
4218         }
4219
4220         pthread_mutex_unlock(&account_mutex);
4221
4222         return error_code;
4223 }
4224
4225 int _account_delete_from_db_by_package_name(int pid, int uid, const char *package_name, gboolean permission)
4226 {
4227         _INFO("_account_delete_from_db_by_package_name");
4228         int                     error_code = ACCOUNT_ERROR_NONE;
4229         account_stmt    hstmt = NULL;
4230         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4231         int                     rc = 0;
4232         int                     ret_transaction = 0;
4233         bool                    is_success = FALSE;
4234         int                     binding_count = 1;
4235         GSList                  *account_id_list = NULL;
4236         int                             ret = -1;
4237
4238         ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
4239         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4240
4241         // It only needs list of ids, does not need to query sensitive info. So sending 0
4242         GList* account_list_temp = _account_query_account_by_package_name(getpid(), package_name, &ret);
4243         if( _account_db_err_code() == SQLITE_PERM ){
4244                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4245                 return ACCOUNT_ERROR_PERMISSION_DENIED;
4246         }
4247
4248         if(ret != ACCOUNT_ERROR_NONE){
4249                 _ERR("_account_query_account_by_package_name failed ret=[%d]", ret);
4250                 return ret;
4251         }
4252
4253         /* Check permission of requested appid */
4254         if(permission){
4255                 char* current_appid = NULL;
4256                 char* package_name_temp = NULL;
4257
4258                 current_appid = _account_get_current_appid(pid);
4259
4260                 package_name_temp = _account_get_text(package_name);
4261
4262                 ACCOUNT_DEBUG( "DELETE: current_appid[%s], package_name[%s]", current_appid, package_name_temp);
4263
4264                 error_code = _account_check_appid_group_with_package_name(uid, current_appid, package_name_temp);
4265
4266                 _ACCOUNT_FREE(current_appid);
4267                 _ACCOUNT_FREE(package_name_temp);
4268
4269                 if(error_code != ACCOUNT_ERROR_NONE){
4270                         ACCOUNT_ERROR("No permission to delete\n");
4271                         _account_glist_account_free(account_list_temp);
4272                         return ACCOUNT_ERROR_PERMISSION_DENIED;
4273                 }
4274         }
4275
4276         GList *account_list = g_list_first(account_list_temp);
4277         _INFO("account_list_temp length=[%d]",g_list_length(account_list));
4278
4279         GList* iter = NULL;
4280         for (iter = account_list; iter != NULL; iter = g_list_next(iter))
4281         {
4282                 _INFO("iterating account_list");
4283                 account_s *account = NULL;
4284                 _INFO("Before iter->data");
4285                 account = (account_s*)iter->data;
4286                 _INFO("After iter->data");
4287                 if (account != NULL)
4288                 {
4289                         char id[256] = {0, };
4290
4291                         ACCOUNT_MEMSET(id, 0, 256);
4292
4293                         ACCOUNT_SNPRINTF(id, 256, "%d", account->id);
4294
4295                         _INFO("Adding account id [%s]", id);
4296                         account_id_list = g_slist_append(account_id_list, g_strdup(id));
4297                 }
4298         }
4299
4300         _account_glist_account_free(account_list_temp);
4301
4302         /* transaction control required*/
4303         ret_transaction = _account_begin_transaction();
4304
4305         if( _account_db_err_code() == SQLITE_PERM ){
4306                 pthread_mutex_unlock(&account_mutex);
4307                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4308                 return ACCOUNT_ERROR_PERMISSION_DENIED;
4309         }
4310
4311         if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
4312                 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
4313                 pthread_mutex_unlock(&account_mutex);
4314                 return ACCOUNT_ERROR_DATABASE_BUSY;
4315         }else if (ret_transaction != ACCOUNT_ERROR_NONE) {
4316                 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
4317                 pthread_mutex_unlock(&account_mutex);
4318                 return ret_transaction;
4319         }
4320
4321         /* delete custom table  */
4322         ACCOUNT_MEMSET(query, 0, sizeof(query));
4323         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", ACCOUNT_CUSTOM_TABLE);
4324
4325         hstmt = _account_prepare_query(query);
4326
4327         if( _account_db_err_code() == SQLITE_PERM ){
4328                 _account_end_transaction(FALSE);
4329                 pthread_mutex_unlock(&account_mutex);
4330                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4331                 return ACCOUNT_ERROR_PERMISSION_DENIED;
4332         }
4333
4334         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4335                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4336
4337         binding_count = 1;
4338         _account_query_bind_text(hstmt, binding_count++, package_name);
4339
4340         rc = _account_query_step(hstmt);
4341         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4342
4343         rc = _account_query_finalize(hstmt);
4344         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4345         hstmt = NULL;
4346
4347         /* delete capability table */
4348         ACCOUNT_MEMSET(query, 0, sizeof(query));
4349         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", CAPABILITY_TABLE);
4350
4351         hstmt = _account_prepare_query(query);
4352
4353         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4354                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4355
4356         binding_count = 1;
4357         _account_query_bind_text(hstmt, binding_count++, package_name);
4358
4359         rc = _account_query_step(hstmt);
4360         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4361
4362         rc = _account_query_finalize(hstmt);
4363         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4364         hstmt = NULL;
4365
4366         /* delete account table */
4367         ACCOUNT_MEMSET(query, 0, sizeof(query));
4368
4369         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", ACCOUNT_TABLE);
4370
4371         hstmt = _account_prepare_query(query);
4372         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4373                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4374
4375         binding_count = 1;
4376         _account_query_bind_text(hstmt, binding_count++, package_name);
4377
4378         rc = _account_query_step(hstmt);
4379         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. package_name=%s, rc=%d\n", package_name, rc));
4380
4381         rc = _account_query_finalize(hstmt);
4382         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4383         is_success = TRUE;
4384
4385         hstmt = NULL;
4386
4387 CATCH:
4388         if (hstmt != NULL) {
4389                 rc = _account_query_finalize(hstmt);
4390                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4391                 hstmt = NULL;
4392         }
4393
4394         ret_transaction = _account_end_transaction(is_success);
4395
4396         if (ret_transaction != ACCOUNT_ERROR_NONE) {
4397                 ACCOUNT_ERROR("account_delete:_account_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
4398         } else {
4399                 if (is_success == true) {
4400                         GSList* gs_iter = NULL;
4401                         for (gs_iter = account_id_list; gs_iter != NULL; gs_iter = g_slist_next(gs_iter)) {
4402                                 char* p_tmpid = NULL;
4403                                 p_tmpid = (char*)gs_iter->data;
4404                                 char buf[64]={0,};
4405                                 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%s", ACCOUNT_NOTI_NAME_DELETE, p_tmpid);
4406                                 ACCOUNT_SLOGD("%s", buf);
4407                                 _account_insert_delete_update_notification_send(buf);
4408                                 _ACCOUNT_FREE(p_tmpid);
4409                         }
4410                         g_slist_free(account_id_list);
4411                 }
4412         }
4413
4414         pthread_mutex_unlock(&account_mutex);
4415
4416         _INFO("_account_delete_from_db_by_package_name end");
4417         return error_code;
4418 }
4419
4420 int _account_get_total_count_from_db(gboolean include_hidden, int *count)
4421 {
4422         if (!count) {
4423                 ACCOUNT_SLOGE("(%s)-(%d) count is NULL.\n", __FUNCTION__, __LINE__);
4424                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4425         }
4426
4427         if(!g_hAccountDB){
4428                 ACCOUNT_ERROR("DB is not opened\n");
4429                 return ACCOUNT_ERROR_DB_NOT_OPENED;
4430         }
4431
4432         char query[1024] = {0, };
4433         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4434
4435         if (include_hidden)
4436         {
4437                 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s", ACCOUNT_TABLE);
4438         }
4439         else
4440         {
4441                 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s where secret = %d", ACCOUNT_TABLE, ACCOUNT_SECRECY_VISIBLE);
4442         }
4443
4444         *count = _account_get_record_count(query);
4445
4446         if( _account_db_err_code() == SQLITE_PERM ){
4447                 pthread_mutex_unlock(&account_mutex);
4448                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4449                 return ACCOUNT_ERROR_PERMISSION_DENIED;
4450         }
4451
4452         int rc = -1;
4453         int ncount = 0;
4454         account_stmt pStmt = NULL;
4455
4456         rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
4457         if (SQLITE_OK != rc) {
4458                 ACCOUNT_SLOGE("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg());
4459                 sqlite3_finalize(pStmt);
4460                 return ACCOUNT_ERROR_DB_FAILED;
4461         }
4462
4463         rc = sqlite3_step(pStmt);
4464         if (SQLITE_ROW != rc) {
4465                 ACCOUNT_ERROR("[ERROR] sqlite3_step() failed\n");
4466                 sqlite3_finalize(pStmt);
4467                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4468         }
4469
4470         ncount = sqlite3_column_int(pStmt, 0);
4471
4472         *count = ncount;
4473
4474         sqlite3_finalize(pStmt);
4475
4476         if (ncount < 0) {
4477                 ACCOUNT_ERROR("[ERROR] Number of account : %d, End", ncount);
4478                 return ACCOUNT_ERROR_DB_FAILED;
4479         }
4480
4481         return ACCOUNT_ERROR_NONE;
4482 }
4483
4484 int account_type_create(account_type_h *account_type)
4485 {
4486         if (!account_type) {
4487                 ACCOUNT_SLOGE("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
4488                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4489         }
4490
4491         account_type_s *data = (account_type_s*)malloc(sizeof(account_type_s));
4492
4493         if (data == NULL) {
4494                 ACCOUNT_ERROR("Memory Allocation Failed");
4495                 return ACCOUNT_ERROR_OUT_OF_MEMORY;
4496         }
4497
4498         ACCOUNT_MEMSET(data, 0, sizeof(account_type_s));
4499
4500         *account_type = (account_type_h)data;
4501
4502         return ACCOUNT_ERROR_NONE;
4503 }
4504
4505 int account_type_destroy(account_type_h account_type)
4506 {
4507         account_type_s *data = (account_type_s*)account_type;
4508
4509         ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account type handle is null!"));
4510
4511         _account_type_free_account_type_with_items(data);
4512
4513         return ACCOUNT_ERROR_NONE;
4514 }
4515
4516 //app_id mandatory field
4517 int account_type_set_app_id(account_type_h account_type, const char *app_id)
4518 {
4519         if (!account_type) {
4520                 ACCOUNT_SLOGE("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
4521                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4522         }
4523
4524         if (!app_id) {
4525                 ACCOUNT_SLOGE("(%s)-(%d) app_id is NULL.\n", __FUNCTION__, __LINE__);
4526                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4527         }
4528
4529         account_type_s *data = (account_type_s*)account_type;
4530
4531         _ACCOUNT_FREE(data->app_id);
4532         data->app_id = _account_get_text(app_id);
4533
4534         return ACCOUNT_ERROR_NONE;
4535 }
4536
4537 //service_provider_id mandatory field
4538 int account_type_set_service_provider_id(account_type_h account_type, const char *service_provider_id)
4539 {
4540         if (!account_type) {
4541                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4542         }
4543
4544         if (!service_provider_id) {
4545                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4546         }
4547
4548         account_type_s *data = (account_type_s*)account_type;
4549
4550         _ACCOUNT_FREE(data->service_provider_id);
4551         data->service_provider_id = _account_get_text(service_provider_id);
4552
4553         return ACCOUNT_ERROR_NONE;
4554 }
4555
4556 int account_type_set_icon_path(account_type_h account_type, const char *icon_path)
4557 {
4558         if (!account_type) {
4559                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4560         }
4561
4562         if (!icon_path) {
4563                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4564         }
4565
4566         account_type_s *data = (account_type_s*)account_type;
4567
4568         _ACCOUNT_FREE(data->icon_path);
4569         data->icon_path = _account_get_text(icon_path);
4570
4571         return ACCOUNT_ERROR_NONE;
4572 }
4573
4574 int account_type_set_small_icon_path(account_type_h account_type, const char *small_icon_path)
4575 {
4576         if (!account_type) {
4577                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4578         }
4579
4580         if (!small_icon_path) {
4581                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4582         }
4583
4584         account_type_s *data = (account_type_s*)account_type;
4585
4586         _ACCOUNT_FREE(data->small_icon_path);
4587         data->small_icon_path = _account_get_text(small_icon_path);
4588
4589         return ACCOUNT_ERROR_NONE;
4590 }
4591
4592 int account_type_set_multiple_account_support(account_type_h account_type, const bool multiple_account_support)
4593 {
4594         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n",  __FUNCTION__, __LINE__));
4595
4596         account_type_s *data = (account_type_s*)account_type;
4597
4598         data->multiple_account_support = multiple_account_support;
4599
4600         return ACCOUNT_ERROR_NONE;
4601 }
4602
4603 // unset?
4604 int account_type_set_label(account_type_h account_type, const char* label, const char* locale)
4605 {
4606         if (!account_type) {
4607                 ACCOUNT_SLOGE("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
4608                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4609         }
4610
4611         if(!label || !locale) {
4612                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4613         }
4614
4615         account_type_s *data = (account_type_s*)account_type;
4616         label_s *label_data = (label_s*)malloc(sizeof(label_s));
4617
4618         if (label_data == NULL) {
4619                 return ACCOUNT_ERROR_OUT_OF_MEMORY;
4620         }
4621         ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
4622
4623         label_data->label = _account_get_text(label);
4624         label_data->locale = _account_get_text(locale);
4625
4626         data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
4627
4628         return ACCOUNT_ERROR_NONE;
4629 }
4630
4631 int account_type_get_app_id(account_type_h account_type, char **app_id)
4632 {
4633         if (!account_type) {
4634                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4635         }
4636
4637         if (!app_id) {
4638                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4639         }
4640
4641         account_type_s *data = (account_type_s*)account_type;
4642
4643         (*app_id) = NULL;
4644         *app_id = _account_get_text(data->app_id);
4645
4646         return ACCOUNT_ERROR_NONE;
4647 }
4648
4649 int account_type_get_service_provider_id(account_type_h account_type, char **service_provider_id)
4650 {
4651         if (!account_type) {
4652                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4653         }
4654
4655         if (!service_provider_id) {
4656                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4657         }
4658
4659         account_type_s *data = (account_type_s*)account_type;
4660
4661         (*service_provider_id) = NULL;
4662         *service_provider_id = _account_get_text(data->service_provider_id);
4663
4664         return ACCOUNT_ERROR_NONE;
4665 }
4666
4667 int account_type_get_icon_path(account_type_h account_type, char **icon_path)
4668 {
4669         if (!account_type) {
4670                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4671         }
4672
4673         if (!icon_path) {
4674                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4675         }
4676
4677         account_type_s *data = (account_type_s*)account_type;
4678
4679         (*icon_path) = NULL;
4680         *icon_path = _account_get_text(data->icon_path);
4681
4682         return ACCOUNT_ERROR_NONE;
4683 }
4684
4685 int account_type_get_small_icon_path(account_type_h account_type, char **small_icon_path)
4686 {
4687         if (!account_type) {
4688                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4689         }
4690
4691         if (!small_icon_path) {
4692                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4693         }
4694
4695         account_type_s *data = (account_type_s*)account_type;
4696
4697         (*small_icon_path) = NULL;
4698         *small_icon_path = _account_get_text(data->small_icon_path);
4699
4700         return ACCOUNT_ERROR_NONE;
4701 }
4702
4703 int account_type_get_multiple_account_support(account_type_h account_type, int *multiple_account_support)
4704 {
4705         if (!account_type) {
4706                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4707         }
4708         if (!multiple_account_support) {
4709                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4710         }
4711
4712         account_type_s *data = (account_type_s*)account_type;
4713
4714         *multiple_account_support = data->multiple_account_support;
4715
4716         return ACCOUNT_ERROR_NONE;
4717 }
4718
4719 int account_type_get_label_by_locale(account_type_h account_type, const char* locale, char** label)
4720 {
4721         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4722         ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID PARAMETER"));
4723
4724         GSList *iter;
4725         account_type_s *data = (account_type_s*)account_type;
4726
4727         for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) {
4728                 label_s *label_data = NULL;
4729
4730                 label_data = (label_s*)iter->data;
4731
4732                 *label = NULL;
4733
4734                 if(!strcmp(locale, label_data->locale)) {
4735                         *label = _account_get_text(label_data->label);
4736                         return ACCOUNT_ERROR_NONE;
4737                 }
4738         }
4739
4740         return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4741 }
4742
4743 int account_type_get_label(account_type_h account_type, account_label_cb callback, void *user_data)
4744 {
4745         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4746         ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4747
4748         GSList *iter;
4749         account_type_s *data = (account_type_s*)account_type;
4750
4751         for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) {
4752                 label_s *label_data = NULL;
4753
4754                 label_data = (label_s*)iter->data;
4755
4756                 if(callback(label_data->app_id, label_data->label, label_data->locale, user_data)!=TRUE) {
4757                         ACCOUNT_DEBUG("Callback func returs FALSE, its iteration is stopped!!!!\n");
4758                         return ACCOUNT_ERROR_NONE;
4759                 }
4760         }
4761
4762         return ACCOUNT_ERROR_NONE;
4763 }
4764
4765 static gboolean _account_type_check_duplicated(account_type_s *data)
4766 {
4767         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4768         int count = 0;
4769
4770         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4771
4772         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId='%s'"
4773                         , ACCOUNT_TYPE_TABLE, data->app_id);
4774
4775         count = _account_get_record_count(query);
4776         if (count > 0) {
4777                 return TRUE;
4778         }
4779
4780         return FALSE;
4781 }
4782
4783 static int _account_type_convert_account_to_sql(account_type_s *account_type, account_stmt hstmt, char *sql_value)
4784 {
4785         _INFO("");
4786
4787         int count = 1;
4788
4789         /*Caution : Keep insert query orders.*/
4790
4791         /* 1. app id*/
4792         _account_query_bind_text(hstmt, count++, (char*)account_type->app_id);
4793
4794         /* 2. service provider id*/
4795         _account_query_bind_text(hstmt, count++, (char*)account_type->service_provider_id);
4796
4797         /* 3. icon path*/
4798         _account_query_bind_text(hstmt, count++, (char*)account_type->icon_path);
4799
4800         /* 4. small icon path*/
4801         _account_query_bind_text(hstmt, count++, (char*)account_type->small_icon_path);
4802
4803         /* 5. multiple accont support*/
4804         _account_query_bind_int(hstmt, count++, account_type->multiple_account_support);
4805
4806         _INFO("");
4807
4808         return count;
4809 }
4810
4811
4812 static int _account_type_execute_insert_query(account_type_s *account_type)
4813 {
4814         _INFO("");
4815
4816         int                             rc = 0;
4817         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4818         int                             error_code = ACCOUNT_ERROR_NONE;
4819         account_stmt    hstmt = NULL;
4820
4821         /* check mandatory field */
4822         // app id & service provider id
4823         if (!account_type->app_id) {
4824                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4825         }
4826
4827         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4828         ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s( AppId, ServiceProviderId , IconPath , SmallIconPath , MultipleAccountSupport ) values "
4829                         "(?, ?, ?, ?, ?)",      ACCOUNT_TYPE_TABLE);
4830
4831         _INFO("");
4832         hstmt = _account_prepare_query(query);
4833         _INFO("");
4834
4835         if( _account_db_err_code() == SQLITE_PERM ){
4836                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4837                 return ACCOUNT_ERROR_PERMISSION_DENIED;
4838         } else if( _account_db_err_code() == SQLITE_BUSY ){
4839                 ACCOUNT_ERROR( "Database Busy(%s)", _account_db_err_msg());
4840                 return ACCOUNT_ERROR_DATABASE_BUSY;
4841         }
4842
4843         ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4844
4845         _INFO("");
4846         _account_type_convert_account_to_sql(account_type, hstmt, query);
4847         _INFO("");
4848
4849         rc = _account_query_step(hstmt);
4850         if (rc == SQLITE_BUSY) {
4851                 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4852                 error_code = ACCOUNT_ERROR_DATABASE_BUSY;
4853         } else if (rc != SQLITE_DONE) {
4854                 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4855                 error_code = ACCOUNT_ERROR_DB_FAILED;
4856         }
4857
4858         _INFO("");
4859         rc = _account_query_finalize(hstmt);
4860         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4861         hstmt = NULL;
4862
4863         _INFO("");
4864         return error_code;
4865 }
4866
4867 static int _account_type_insert_label(account_type_s *account_type)
4868 {
4869         int                     rc, count = 1;
4870         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4871         account_stmt    hstmt = NULL;
4872
4873         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4874
4875         if (g_slist_length( account_type->label_list)==0) {
4876                 ACCOUNT_ERROR( "_account_type_insert_label, no label\n");
4877                 return ACCOUNT_ERROR_NONE;
4878         }
4879
4880         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId = '%s'", ACCOUNT_TYPE_TABLE, account_type->app_id);
4881
4882         rc = _account_get_record_count(query);
4883
4884         if( _account_db_err_code() == SQLITE_PERM ){
4885                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4886                 return ACCOUNT_ERROR_PERMISSION_DENIED;
4887         }
4888
4889         if (rc <= 0) {
4890                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4891         }
4892
4893         /* insert query*/
4894         GSList *iter;
4895
4896         for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
4897                 int ret;
4898                 count = 1;
4899                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4900                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
4901                                 "(?, ?, ?) ", LABEL_TABLE);
4902
4903                 hstmt = _account_prepare_query(query);
4904
4905                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4906
4907                 label_s* label_data = NULL;
4908                 label_data = (label_s*)iter->data;
4909
4910                 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4911                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4912                 ret = _account_query_bind_text(hstmt, count++, label_data->label);
4913                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4914                 ret = _account_query_bind_text(hstmt, count++, (char*)label_data->locale);
4915                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4916
4917                 rc = _account_query_step(hstmt);
4918
4919                 if (rc != SQLITE_DONE) {
4920                         ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4921                         break;
4922                 }
4923
4924                 rc = _account_query_finalize(hstmt);
4925                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4926                 hstmt = NULL;
4927
4928         }
4929
4930         return ACCOUNT_ERROR_NONE;
4931 }
4932
4933 static void _account_type_convert_column_to_provider_feature(account_stmt hstmt, provider_feature_s *feature_record)
4934 {
4935         const char *textbuf = NULL;
4936
4937         textbuf = _account_query_table_column_text(hstmt, PROVIDER_FEATURE_FIELD_APP_ID);
4938         _account_db_data_to_text(textbuf, &(feature_record->app_id));
4939
4940         textbuf = _account_query_table_column_text(hstmt, PROVIDER_FEATURE_FIELD_KEY);
4941         _account_db_data_to_text(textbuf, &(feature_record->key));
4942
4943 }
4944
4945 GSList* _account_type_query_provider_feature_by_app_id_from_global_db(const char* app_id, int *error_code)
4946 {
4947         _INFO("_account_type_query_provider_feature_by_app_id_in_global_db app_id=%s", app_id);
4948         account_stmt hstmt = NULL;
4949         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4950         int rc = 0, binding_count = 1;
4951         GSList* feature_list = NULL;
4952
4953         ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
4954         ACCOUNT_RETURN_VAL((error_code != NULL), {_ERR("error_code pointer is NULL");}, NULL, (""));
4955         ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER; _ERR("The database isn't connected.");}, NULL, ("The database isn't connected."));
4956
4957         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4958
4959         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
4960         _INFO("account query=[%s]", query);
4961
4962         hstmt = _account_prepare_query_from_global_db(query);
4963
4964         if( _account_db_err_code_from_global_db() == SQLITE_PERM ){
4965                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4966                 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
4967                 return NULL;
4968         }
4969
4970         _INFO("before _account_query_bind_text");
4971         _account_query_bind_text(hstmt, binding_count++, app_id);
4972
4973         rc = _account_query_step(hstmt);
4974
4975         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"));
4976
4977         provider_feature_s* feature_record = NULL;
4978
4979         while (rc == SQLITE_ROW) {
4980
4981                 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
4982
4983                 if (feature_record == NULL) {
4984                         ACCOUNT_FATAL("malloc Failed");
4985                         break;
4986                 }
4987
4988                 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
4989
4990                 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
4991
4992                 _INFO("Adding account feature_list");
4993                 feature_list = g_slist_append(feature_list, feature_record);
4994
4995                 rc = _account_query_step(hstmt);
4996         }
4997
4998         *error_code = ACCOUNT_ERROR_NONE;
4999
5000 CATCH:
5001         if (hstmt != NULL) {
5002                 rc = _account_query_finalize_from_global_db(hstmt);
5003                 if (rc != ACCOUNT_ERROR_NONE) {
5004                         *error_code = rc;
5005                         _ERR("global db fianlize error");
5006                 }
5007         }
5008
5009         if (*error_code != ACCOUNT_ERROR_NONE) {
5010                 _account_type_gslist_feature_free(feature_list);
5011         }
5012
5013         _INFO("Returning account feature_list from global db");
5014         return feature_list;
5015 }
5016
5017 GSList* _account_type_query_provider_feature_by_app_id(const char* app_id, int *error_code)
5018 {
5019         _INFO("_account_type_query_provider_feature_by_app_id app_id=%s", app_id);
5020         account_stmt hstmt = NULL;
5021         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5022         int rc = 0, binding_count = 1;
5023         GSList* feature_list = NULL;
5024
5025         ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
5026         ACCOUNT_RETURN_VAL((error_code != NULL), {_ERR("error_code pointer is NULL");}, NULL, (""));
5027         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
5028
5029         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5030
5031         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
5032         _INFO("account query=[%s]", query);
5033
5034         hstmt = _account_prepare_query(query);
5035
5036         if( _account_db_err_code() == SQLITE_PERM ){
5037                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5038                 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
5039                 return NULL;
5040         }
5041
5042         _account_query_bind_text(hstmt, binding_count++, app_id);
5043
5044         rc = _account_query_step(hstmt);
5045
5046         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"));
5047
5048         provider_feature_s* feature_record = NULL;
5049
5050         while (rc == SQLITE_ROW) {
5051
5052                 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
5053
5054                 if (feature_record == NULL) {
5055                         ACCOUNT_FATAL("malloc Failed");
5056                         break;
5057                 }
5058
5059                 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
5060
5061                 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
5062
5063                 _INFO("Adding account feature_list");
5064                 feature_list = g_slist_append(feature_list, feature_record);
5065
5066                 rc = _account_query_step(hstmt);
5067         }
5068
5069         *error_code = ACCOUNT_ERROR_NONE;
5070
5071         rc = _account_query_finalize(hstmt);
5072         ACCOUNT_CATCH_ERROR_P((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, rc, ("account finalize error"));
5073         hstmt = NULL;
5074
5075 CATCH:
5076         if (hstmt != NULL) {
5077                 rc = _account_query_finalize(hstmt);
5078                 if (rc != ACCOUNT_ERROR_NONE) {
5079                         *error_code = rc;
5080                         _ERR("account fianlize error");
5081                 }
5082                 hstmt = NULL;
5083         }
5084         _INFO("*error_code=[%d]", *error_code);
5085
5086         if (*error_code == ACCOUNT_ERROR_RECORD_NOT_FOUND) {
5087                 feature_list = _account_type_query_provider_feature_by_app_id_from_global_db(app_id, error_code);
5088         }
5089
5090         if (*error_code != ACCOUNT_ERROR_NONE)
5091                 _account_type_gslist_feature_free(feature_list);
5092
5093         _INFO("Returning account feature_list");
5094         return feature_list;
5095 }
5096
5097 int _account_type_query_provider_feature_cb_by_app_id_from_global_db(provider_feature_cb callback, const char* app_id, void *user_data )
5098 {
5099         int                     error_code = ACCOUNT_ERROR_NONE;
5100         account_stmt    hstmt = NULL;
5101         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
5102         int                     rc = 0, binding_count = 1;
5103
5104         _INFO("_account_type_query_provider_feature_cb_by_app_id_in_global_db start app_id=%s", app_id);
5105         ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5106         ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
5107         ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5108
5109         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5110
5111         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
5112         hstmt = _account_prepare_query_from_global_db(query);
5113
5114         if( _account_db_err_code_from_global_db() == SQLITE_PERM ){
5115                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg_from_global_db());
5116                 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_PERMISSION_DENIED, ("global db permission denied.\n"));
5117         }
5118
5119         _account_query_bind_text(hstmt, binding_count++, app_id);
5120
5121         rc = _account_query_step(hstmt);
5122         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"));
5123
5124         provider_feature_s* feature_record = NULL;
5125
5126         while (rc == SQLITE_ROW) {
5127                 bool cb_ret = FALSE;
5128                 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
5129
5130                 if (feature_record == NULL) {
5131                         ACCOUNT_FATAL("malloc Failed");
5132                         break;
5133                 }
5134
5135                 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
5136
5137                 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
5138
5139                 cb_ret = callback(feature_record->app_id, feature_record->key, user_data);
5140
5141                 _account_type_free_feature_with_items(feature_record);
5142
5143                 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returns FALSE, its iteration is stopped!!!!\n"));
5144
5145                 rc = _account_query_step(hstmt);
5146         }
5147
5148         error_code = ACCOUNT_ERROR_NONE;
5149
5150 CATCH:
5151         if (hstmt != NULL) {
5152                 rc = _account_query_finalize_from_global_db(hstmt);
5153                 if (rc != ACCOUNT_ERROR_NONE) {
5154                         error_code = rc;
5155                         _ERR("global db finalize error[%d]", rc);
5156                 }
5157                 hstmt = NULL;
5158         }
5159
5160         _INFO("_account_type_query_provider_feature_cb_by_app_id_in_global_db end. error_code=[%d]", error_code);
5161         return error_code;
5162 }
5163
5164 int _account_type_query_provider_feature_cb_by_app_id(provider_feature_cb callback, const char* app_id, void *user_data )
5165 {
5166         int                     error_code = ACCOUNT_ERROR_NONE;
5167         account_stmt    hstmt = NULL;
5168         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
5169         int                     rc = 0, binding_count = 1;
5170
5171         _INFO("_account_type_query_provider_feature_cb_by_app_id start app_id=%s", app_id);
5172         ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5173         ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
5174         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5175
5176         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5177
5178         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
5179         hstmt = _account_prepare_query(query);
5180
5181         if( _account_db_err_code() == SQLITE_PERM ){
5182                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5183                 return ACCOUNT_ERROR_PERMISSION_DENIED;
5184         }
5185
5186         _account_query_bind_text(hstmt, binding_count++, app_id);
5187
5188         rc = _account_query_step(hstmt);
5189         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found in user db.\n"));
5190
5191         provider_feature_s* feature_record = NULL;
5192
5193         while (rc == SQLITE_ROW) {
5194                 bool cb_ret = FALSE;
5195                 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
5196
5197                 if (feature_record == NULL) {
5198                         ACCOUNT_FATAL("malloc Failed");
5199                         break;
5200                 }
5201
5202                 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
5203
5204                 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
5205
5206                 cb_ret = callback(feature_record->app_id, feature_record->key, user_data);
5207
5208                 _account_type_free_feature_with_items(feature_record);
5209
5210                 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
5211
5212                 rc = _account_query_step(hstmt);
5213         }
5214
5215         rc = _account_query_finalize(hstmt);
5216         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5217         hstmt = NULL;
5218
5219         error_code = ACCOUNT_ERROR_NONE;
5220
5221 CATCH:
5222         if (hstmt != NULL) {
5223                 rc = _account_query_finalize(hstmt);
5224                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5225                 hstmt = NULL;
5226         }
5227 /*
5228         if (error_code == ACCOUNT_ERROR_RECORD_NOT_FOUND) {
5229                 error_code = _account_type_query_provider_feature_cb_by_app_id_from_global_db(callback, app_id, user_data);
5230         }
5231 */
5232         _INFO("_account_type_query_provider_feature_cb_by_app_id end");
5233         return error_code;
5234 }
5235
5236 int account_type_query_provider_feature_cb_by_app_id(provider_feature_cb callback, const char* app_id, void *user_data )
5237 {
5238         int                     error_code = ACCOUNT_ERROR_NONE;
5239
5240         ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5241         ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
5242         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5243
5244         error_code = _account_type_query_provider_feature_cb_by_app_id(callback, app_id, user_data);
5245
5246         if (error_code == ACCOUNT_ERROR_RECORD_NOT_FOUND) {
5247                 error_code = _account_type_query_provider_feature_cb_by_app_id_from_global_db(callback, app_id, user_data);
5248         }
5249
5250         return error_code;
5251 }
5252
5253 bool _account_type_query_supported_feature_from_global_db(const char* app_id, const char* capability, int *error_code)
5254 {
5255         _INFO("_account_type_query_supported_feature_in_global_db start");
5256         ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5257
5258         *error_code = ACCOUNT_ERROR_NONE;
5259
5260         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5261         int record_count = 0;
5262
5263         if (app_id == NULL || capability == NULL)
5264         {
5265                 *error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
5266                 return false;
5267         }
5268
5269         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
5270
5271         record_count = _account_get_record_count_from_global_db(query);
5272
5273         if( _account_db_err_code_from_global_db() == SQLITE_PERM ){
5274                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg_from_global_db());
5275                 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
5276         }
5277
5278         if (record_count <= 0)
5279         {
5280                 *error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
5281                 return false;
5282         }
5283
5284         _INFO("_account_type_query_supported_feature_in_global_db end");
5285         return true;
5286 }
5287
5288 bool _account_type_query_supported_feature(const char* app_id, const char* capability, int *error_code)
5289 {
5290         _INFO("_account_type_query_supported_feature start");
5291
5292         *error_code = ACCOUNT_ERROR_NONE;
5293
5294         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
5295         int                     record_count = 0;
5296
5297         if (app_id == NULL || capability == NULL)
5298         {
5299                 *error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
5300                 return false;
5301         }
5302
5303         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
5304
5305         record_count = _account_get_record_count(query);
5306
5307         if( _account_db_err_code() == SQLITE_PERM ){
5308                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5309                 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
5310                 return false;
5311         }
5312
5313         if (record_count <= 0)
5314         {
5315                 bool is_exist = false;
5316                 is_exist = _account_type_query_supported_feature_from_global_db(app_id, capability, error_code);
5317                 if (!is_exist)
5318                         return false;
5319         }
5320
5321         _INFO("_account_type_query_supported_feature end");
5322         return true;
5323 }
5324
5325 static int _account_type_insert_provider_feature(account_type_s *account_type, const char* app_id)
5326 {
5327         int                     rc, count = 1;
5328         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
5329         account_stmt    hstmt = NULL;
5330
5331         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
5332         ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5333
5334         if (g_slist_length( account_type->provider_feature_list)==0) {
5335                 ACCOUNT_ERROR( "no capability\n");
5336                 return ACCOUNT_ERROR_NONE;
5337         }
5338
5339         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId='%s'", ACCOUNT_TYPE_TABLE, app_id);
5340
5341         rc = _account_get_record_count(query);
5342
5343         if( _account_db_err_code() == SQLITE_PERM ){
5344                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5345                 return ACCOUNT_ERROR_PERMISSION_DENIED;
5346         }
5347
5348         if (rc <= 0) {
5349                 ACCOUNT_SLOGI( "related account type item is not existed rc=%d , %s", rc, _account_db_err_msg());
5350                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5351         }
5352
5353         /* insert query*/
5354
5355         GSList *iter;
5356
5357         for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
5358                 int ret;
5359                 count = 1;
5360                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5361                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
5362                                 "(?, ?) ", PROVIDER_FEATURE_TABLE);
5363
5364                 hstmt = _account_prepare_query(query);
5365
5366                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
5367
5368                 provider_feature_s* feature_data = NULL;
5369                 feature_data = (provider_feature_s*)iter->data;
5370
5371                 ret = _account_query_bind_text(hstmt, count++, app_id);
5372                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5373                 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
5374                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
5375
5376                 rc = _account_query_step(hstmt);
5377
5378                 if (rc != SQLITE_DONE) {
5379                         ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5380                         break;
5381                 }
5382
5383                 rc = _account_query_finalize(hstmt);
5384                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5385                 hstmt = NULL;
5386
5387         }
5388
5389         return ACCOUNT_ERROR_NONE;
5390 }
5391
5392 int _account_type_insert_to_db(account_type_s* account_type, int* account_type_id)
5393 {
5394         _INFO("");
5395
5396         int             error_code = ACCOUNT_ERROR_NONE, ret_transaction = 0;
5397
5398         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5399         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE HANDLE IS NULL"));
5400         ACCOUNT_RETURN_VAL((account_type_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE ID POINTER IS NULL"));
5401
5402         account_type_s *data = (account_type_s*)account_type;
5403
5404         pthread_mutex_lock(&account_mutex);
5405
5406
5407         /* transaction control required*/
5408         ret_transaction = _account_begin_transaction();
5409
5410         _INFO("");
5411
5412         if( _account_db_err_code() == SQLITE_PERM ){
5413                 pthread_mutex_unlock(&account_mutex);
5414                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5415                 return ACCOUNT_ERROR_PERMISSION_DENIED;
5416         }
5417
5418         _INFO("");
5419         if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
5420                 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
5421                 pthread_mutex_unlock(&account_mutex);
5422                 return ACCOUNT_ERROR_DATABASE_BUSY;
5423         } else if (ret_transaction != ACCOUNT_ERROR_NONE) {
5424                 ACCOUNT_ERROR("_account_begin_transaction fail %d\n", ret_transaction);
5425                 pthread_mutex_unlock(&account_mutex);
5426                 return ret_transaction;
5427         }
5428
5429         _INFO("");
5430         if (_account_type_check_duplicated(data)) {
5431                 _INFO("");
5432                 ret_transaction = _account_end_transaction(FALSE);
5433                 ACCOUNT_ERROR("Duplicated, rollback insert query(%x)!!!!\n", ret_transaction);
5434                 *account_type_id = -1;
5435                 pthread_mutex_unlock(&account_mutex);
5436                 return ACCOUNT_ERROR_DUPLICATED;
5437         } else {
5438                 _INFO("");
5439                 *account_type_id = _account_get_next_sequence(ACCOUNT_TYPE_TABLE);
5440
5441                 error_code = _account_type_execute_insert_query(data);
5442
5443                 if (error_code != ACCOUNT_ERROR_NONE){
5444                         error_code = ACCOUNT_ERROR_DUPLICATED;
5445                         ret_transaction = _account_end_transaction(FALSE);
5446                         ACCOUNT_ERROR("Insert fail, rollback insert query(%x)!!!!\n", ret_transaction);
5447                         *account_type_id = -1;
5448                         pthread_mutex_unlock(&account_mutex);
5449                         return error_code;
5450                 }
5451         }
5452
5453         _INFO("");
5454         error_code = _account_type_insert_provider_feature(data, data->app_id);
5455         if(error_code != ACCOUNT_ERROR_NONE) {
5456                 _INFO("");
5457                 ret_transaction = _account_end_transaction(FALSE);
5458                 ACCOUNT_ERROR("Insert provider feature fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction);
5459                 pthread_mutex_unlock(&account_mutex);
5460                 return error_code;
5461         }
5462         _INFO("");
5463         error_code = _account_type_insert_label(data);
5464         if(error_code != ACCOUNT_ERROR_NONE) {
5465                 _INFO("");
5466                 ret_transaction = _account_end_transaction(FALSE);
5467                 ACCOUNT_ERROR("Insert label fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction);
5468                 pthread_mutex_unlock(&account_mutex);
5469                 return error_code;
5470         }
5471
5472         ret_transaction = _account_end_transaction(TRUE);
5473         _INFO("");
5474         pthread_mutex_unlock(&account_mutex);
5475
5476         _INFO("");
5477         return ACCOUNT_ERROR_NONE;
5478 }
5479
5480 static int _account_type_update_provider_feature(account_type_s *account_type, const char* app_id)
5481 {
5482         int                     rc, count = 1;
5483         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
5484         account_stmt    hstmt = NULL;
5485
5486         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
5487
5488         if (g_slist_length( account_type->provider_feature_list)==0) {
5489                 ACCOUNT_ERROR( "no feature\n");
5490                 return ACCOUNT_ERROR_NONE;
5491         }
5492
5493         ACCOUNT_DEBUG( "app id", app_id);
5494
5495         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5496
5497         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id=? ", PROVIDER_FEATURE_TABLE);
5498         hstmt = _account_prepare_query(query);
5499
5500         if( _account_db_err_code() == SQLITE_PERM ){
5501                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5502                 return ACCOUNT_ERROR_PERMISSION_DENIED;
5503         }
5504
5505         count = 1;
5506         _account_query_bind_text(hstmt, count++, app_id);
5507         rc = _account_query_step(hstmt);
5508
5509         if (rc != SQLITE_DONE) {
5510                 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5511                 return ACCOUNT_ERROR_DB_FAILED;
5512         }
5513         rc = _account_query_finalize(hstmt);
5514         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5515         hstmt = NULL;
5516
5517         GSList *iter;
5518
5519         for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
5520                 int ret;
5521                 count = 1;
5522                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5523                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
5524                                 "(?, ?) ", PROVIDER_FEATURE_TABLE);
5525
5526                 hstmt = _account_prepare_query(query);
5527
5528                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
5529
5530                 provider_feature_s* feature_data = NULL;
5531                 feature_data = (provider_feature_s*)iter->data;
5532
5533                 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
5534                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5535                 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
5536                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5537
5538                 rc = _account_query_step(hstmt);
5539
5540                 if (rc != SQLITE_DONE) {
5541                         ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5542                         break;
5543                 }
5544                 rc = _account_query_finalize(hstmt);
5545                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5546                 hstmt = NULL;
5547         }
5548
5549         return ACCOUNT_ERROR_NONE;
5550 }
5551
5552 static int _account_type_update_label(account_type_s *account_type, const char* app_id)
5553 {
5554         int                     rc, count = 1;
5555         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
5556         account_stmt    hstmt = NULL;
5557
5558         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
5559
5560         if (g_slist_length( account_type->label_list)==0) {
5561                 return ACCOUNT_ERROR_NONE;
5562         }
5563
5564         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5565
5566         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId=? ", LABEL_TABLE);
5567         hstmt = _account_prepare_query(query);
5568
5569         if( _account_db_err_code() == SQLITE_PERM ){
5570                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5571                 return ACCOUNT_ERROR_PERMISSION_DENIED;
5572         }
5573
5574         count = 1;
5575         _account_query_bind_text(hstmt, count++, app_id);
5576         rc = _account_query_step(hstmt);
5577
5578         if (rc != SQLITE_DONE) {
5579                 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5580                 return ACCOUNT_ERROR_DB_FAILED;
5581         }
5582         rc = _account_query_finalize(hstmt);
5583         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5584         hstmt = NULL;
5585
5586         GSList *iter;
5587
5588         for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
5589                 int ret;
5590                 count = 1;
5591                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5592                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
5593                                 "(?, ?, ?) ", LABEL_TABLE);
5594
5595                 hstmt = _account_prepare_query(query);
5596
5597                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
5598
5599                 label_s* label_data = NULL;
5600                 label_data = (label_s*)iter->data;
5601
5602                 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
5603                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5604                 ret = _account_query_bind_text(hstmt, count++, label_data->label);
5605                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5606                 ret = _account_query_bind_text(hstmt, count++, label_data->locale);
5607                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5608
5609                 rc = _account_query_step(hstmt);
5610
5611                 if (rc != SQLITE_DONE) {
5612                         ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5613                         break;
5614                 }
5615                 rc = _account_query_finalize(hstmt);
5616                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5617                 hstmt = NULL;
5618         }
5619
5620         return ACCOUNT_ERROR_NONE;
5621 }
5622
5623
5624 static int _account_type_update_account(account_type_s *account_type, const char* app_id)
5625 {
5626         int                             rc = 0, binding_count =1;
5627         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
5628         int                             error_code = ACCOUNT_ERROR_NONE;
5629         account_stmt    hstmt = NULL;
5630
5631         if (!account_type->app_id) {
5632                 ACCOUNT_ERROR("app id is mandetory field, it can not be NULL!!!!\n");
5633                 return ACCOUNT_ERROR_INVALID_PARAMETER;
5634         }
5635
5636         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5637         ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET AppId=?, ServiceProviderId=?, IconPath=?, "
5638                         "SmallIconPath=?, MultipleAccountSupport=? WHERE AppId=? ", ACCOUNT_TYPE_TABLE);
5639
5640         hstmt = _account_prepare_query(query);
5641
5642         if( _account_db_err_code() == SQLITE_PERM ){
5643                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5644                 return ACCOUNT_ERROR_PERMISSION_DENIED;
5645         } else if (_account_db_err_code() == SQLITE_BUSY){
5646                 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
5647                 return ACCOUNT_ERROR_DATABASE_BUSY;
5648         }
5649
5650         ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg()));
5651
5652         binding_count = _account_type_convert_account_to_sql(account_type, hstmt, query);
5653         _account_query_bind_text(hstmt, binding_count++, app_id);
5654
5655         rc = _account_query_step(hstmt);
5656         if (rc != SQLITE_DONE) {
5657                 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5658         }
5659
5660         rc = _account_query_finalize(hstmt);
5661         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5662         hstmt = NULL;
5663
5664         /*update label*/
5665         error_code = _account_type_update_label(account_type, app_id);
5666         /* update provider feature */
5667         error_code = _account_type_update_provider_feature(account_type, app_id);
5668
5669         return error_code;
5670 }
5671
5672 int _account_type_update_to_db_by_app_id(account_type_s* account_type, const char* app_id)
5673 {
5674         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
5675         ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5676         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5677
5678         int     error_code = ACCOUNT_ERROR_NONE;
5679         account_type_s* data = account_type;
5680
5681         pthread_mutex_lock(&account_mutex);
5682
5683         error_code = _account_type_update_account(data, app_id);
5684
5685         pthread_mutex_unlock(&account_mutex);
5686
5687         return error_code;
5688 }
5689
5690 int _account_type_delete_by_app_id(const char* app_id)
5691 {
5692         int                     error_code = ACCOUNT_ERROR_NONE;
5693         account_stmt    hstmt = NULL;
5694         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
5695         int                     rc = 0, count = -1;
5696         int                     ret_transaction = 0;
5697         int                             binding_count = 1;
5698         bool                    is_success = FALSE;
5699
5700         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5701         ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("The database isn't connected."));
5702
5703         /* Check requested ID to delete */
5704         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
5705
5706         count = _account_get_record_count(query);
5707
5708         if( _account_db_err_code() == SQLITE_PERM ){
5709                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5710                 return ACCOUNT_ERROR_PERMISSION_DENIED;
5711         }
5712
5713         if (count <= 0) {
5714                 ACCOUNT_SLOGE("app id(%s) is not exist. count(%d)\n", app_id, count);
5715                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5716         }
5717
5718         /* transaction control required*/
5719         ret_transaction = _account_begin_transaction();
5720
5721         if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
5722                 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
5723                 pthread_mutex_unlock(&account_mutex);
5724                 return ACCOUNT_ERROR_DATABASE_BUSY;
5725         }else if (ret_transaction != ACCOUNT_ERROR_NONE) {
5726                 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
5727                 pthread_mutex_unlock(&account_mutex);
5728                 return ret_transaction;
5729         }
5730
5731         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", LABEL_TABLE);
5732
5733         hstmt = _account_prepare_query(query);
5734
5735         if( _account_db_err_code() == SQLITE_PERM ){
5736                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5737                 pthread_mutex_unlock(&account_mutex);
5738                 return ACCOUNT_ERROR_PERMISSION_DENIED;
5739         }
5740
5741         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
5742                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
5743
5744         _account_query_bind_text(hstmt, binding_count++, app_id);
5745
5746         rc = _account_query_step(hstmt);
5747         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5748
5749         rc = _account_query_finalize(hstmt);
5750         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5751         hstmt = NULL;
5752
5753         binding_count = 1;
5754         ACCOUNT_MEMSET(query, 0, sizeof(query));
5755
5756         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id = ? ", PROVIDER_FEATURE_TABLE);
5757
5758         hstmt = _account_prepare_query(query);
5759         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
5760                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
5761
5762         _account_query_bind_text(hstmt, binding_count++, app_id);
5763
5764         rc = _account_query_step(hstmt);
5765         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
5766
5767         rc = _account_query_finalize(hstmt);
5768         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5769         is_success = TRUE;
5770
5771         hstmt = NULL;
5772
5773         binding_count = 1;
5774         ACCOUNT_MEMSET(query, 0, sizeof(query));
5775
5776         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ? ", ACCOUNT_TYPE_TABLE);
5777
5778         hstmt = _account_prepare_query(query);
5779         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
5780                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
5781
5782         _account_query_bind_text(hstmt, binding_count++, app_id);
5783
5784         rc = _account_query_step(hstmt);
5785         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
5786
5787         rc = _account_query_finalize(hstmt);
5788         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5789         is_success = TRUE;
5790
5791         hstmt = NULL;
5792
5793         CATCH:
5794         if (hstmt != NULL) {
5795                 rc = _account_query_finalize(hstmt);
5796                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5797                 hstmt = NULL;
5798         }
5799
5800         ret_transaction = _account_end_transaction(is_success);
5801
5802         if (ret_transaction != ACCOUNT_ERROR_NONE) {
5803                 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
5804         }
5805
5806         pthread_mutex_unlock(&account_mutex);
5807
5808         return error_code;
5809 }
5810
5811 static void _account_type_convert_column_to_account_type(account_stmt hstmt, account_type_s *account_type_record)
5812 {
5813         const char *textbuf = NULL;
5814
5815         account_type_record->id = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_ID);
5816
5817         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_APP_ID);
5818         _account_db_data_to_text(textbuf, &(account_type_record->app_id));
5819
5820         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SERVICE_PROVIDER_ID);
5821         _account_db_data_to_text(textbuf, &(account_type_record->service_provider_id));
5822
5823         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_ICON_PATH);
5824         _account_db_data_to_text(textbuf, &(account_type_record->icon_path));
5825
5826         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SMALL_ICON_PATH);
5827         _account_db_data_to_text(textbuf, &(account_type_record->small_icon_path));
5828
5829         account_type_record->multiple_account_support = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_MULTIPLE_ACCOUNT_SUPPORT);
5830
5831 }
5832
5833 static void _account_type_convert_column_to_label(account_stmt hstmt, label_s *label_record)
5834 {
5835         const char *textbuf = NULL;
5836
5837         textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_APP_ID);
5838         _account_db_data_to_text(textbuf, &(label_record->app_id));
5839
5840         textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LABEL);
5841         _account_db_data_to_text(textbuf, &(label_record->label));
5842
5843         textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LOCALE);
5844         _account_db_data_to_text(textbuf, &(label_record->locale));
5845
5846 }
5847
5848 GSList* _account_type_get_label_list_by_app_id_from_global_db(const char* app_id, int *error_code )
5849 {
5850         *error_code = ACCOUNT_ERROR_NONE;
5851         account_stmt    hstmt = NULL;
5852         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
5853         int                     rc = 0, binding_count = 1;
5854         GSList* label_list = NULL;
5855
5856         ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
5857         ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5858
5859         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5860
5861         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
5862         hstmt = _account_prepare_query_from_global_db(query);
5863
5864         if( _account_db_err_code_from_global_db() == SQLITE_PERM ){
5865                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg_from_global_db());
5866                 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
5867
5868                 goto CATCH;
5869         }
5870
5871         _account_query_bind_text(hstmt, binding_count++, app_id);
5872
5873         rc = _account_query_step(hstmt);
5874         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"));
5875
5876         label_s* label_record = NULL;
5877
5878         while (rc == SQLITE_ROW) {
5879                 label_record = (label_s*) malloc(sizeof(label_s));
5880
5881                 if (label_record == NULL) {
5882                         ACCOUNT_FATAL("malloc Failed");
5883                         break;
5884                 }
5885
5886                 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
5887
5888                 _account_type_convert_column_to_label(hstmt, label_record);
5889
5890                 _INFO("Adding account label_list");
5891                 label_list = g_slist_append (label_list, label_record);
5892
5893                 rc = _account_query_step(hstmt);
5894         }
5895
5896         *error_code = ACCOUNT_ERROR_NONE;
5897
5898 CATCH:
5899         if (hstmt != NULL) {
5900                 rc = _account_query_finalize_from_global_db(hstmt);
5901                 if (rc != ACCOUNT_ERROR_NONE) {
5902                         _ERR("global db finalize error[%d]", rc);
5903                 }
5904                 hstmt = NULL;
5905         }
5906
5907         _INFO("Returning account global label_list");
5908         return label_list;
5909 }
5910
5911 GSList* _account_type_get_label_list_by_app_id(const char* app_id, int *error_code )
5912 {
5913         *error_code = ACCOUNT_ERROR_NONE;
5914         account_stmt    hstmt = NULL;
5915         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
5916         int                     rc = 0, binding_count = 1;
5917         GSList* label_list = NULL;
5918
5919         ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
5920         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
5921
5922         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5923
5924         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
5925         hstmt = _account_prepare_query(query);
5926
5927         if( _account_db_err_code() == SQLITE_PERM ){
5928                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5929                 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
5930                 return NULL;
5931         }
5932
5933         _account_query_bind_text(hstmt, binding_count++, app_id);
5934
5935         rc = _account_query_step(hstmt);
5936         ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5937
5938         label_s* label_record = NULL;
5939
5940         while (rc == SQLITE_ROW) {
5941                 label_record = (label_s*) malloc(sizeof(label_s));
5942
5943                 if (label_record == NULL) {
5944                         ACCOUNT_FATAL("malloc Failed");
5945                         break;
5946                 }
5947
5948                 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
5949
5950                 _account_type_convert_column_to_label(hstmt, label_record);
5951
5952                 _INFO("Adding account label_list");
5953                 label_list = g_slist_append (label_list, label_record);
5954
5955                 rc = _account_query_step(hstmt);
5956         }
5957
5958         rc = _account_query_finalize(hstmt);
5959         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
5960         hstmt = NULL;
5961
5962         *error_code = ACCOUNT_ERROR_NONE;
5963
5964 CATCH:
5965         if (hstmt != NULL) {
5966                 rc = _account_query_finalize(hstmt);
5967                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
5968                 hstmt = NULL;
5969         }
5970
5971         if (*error_code == ACCOUNT_ERROR_RECORD_NOT_FOUND) {
5972                 label_list = _account_type_get_label_list_by_app_id_from_global_db(app_id, error_code);
5973         }
5974
5975         _INFO("Returning account label_list");
5976         return label_list;
5977 }
5978
5979 int _account_type_query_label_by_app_id_from_global_db(account_label_cb callback, const char* app_id, void *user_data )
5980 {
5981         int                     error_code = ACCOUNT_ERROR_NONE;
5982         account_stmt    hstmt = NULL;
5983         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
5984         int                     rc = 0, binding_count = 1;
5985
5986         _INFO("account_type_query_label_by_app_id_from_global_db start");
5987
5988         ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5989         ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
5990         ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5991
5992         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5993
5994         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
5995         hstmt = _account_prepare_query_from_global_db(query);
5996
5997         if( _account_db_err_code_from_global_db() == SQLITE_PERM ){
5998                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg_from_global_db());
5999                 error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
6000                 goto CATCH;
6001         }
6002
6003         _account_query_bind_text(hstmt, binding_count++, app_id);
6004
6005         rc = _account_query_step(hstmt);
6006         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
6007
6008         label_s* label_record = NULL;
6009
6010         while (rc == SQLITE_ROW) {
6011                 bool cb_ret = FALSE;
6012                 label_record = (label_s*) malloc(sizeof(label_s));
6013
6014                 if (label_record == NULL) {
6015                         ACCOUNT_FATAL("malloc Failed");
6016                         break;
6017                 }
6018
6019                 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
6020
6021                 _account_type_convert_column_to_label(hstmt, label_record);
6022
6023                 cb_ret = callback(label_record->app_id, label_record->label , label_record->locale, user_data);
6024
6025                 _account_type_free_label_with_items(label_record);
6026
6027                 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
6028
6029                 rc = _account_query_step(hstmt);
6030         }
6031
6032         error_code = ACCOUNT_ERROR_NONE;
6033
6034 CATCH:
6035         if (hstmt != NULL) {
6036                 rc = _account_query_finalize_from_global_db(hstmt);
6037                 if (rc != ACCOUNT_ERROR_NONE) {
6038                         _ERR("global db finalize error[%d]", rc);
6039                 }
6040                 hstmt = NULL;
6041         }
6042
6043         _INFO("account_type_query_label_by_app_id_from_global_db end [%d]", error_code);
6044         return error_code;
6045 }
6046
6047 int _account_type_query_label_by_app_id(account_label_cb callback, const char* app_id, void *user_data )
6048 {
6049         int                     error_code = ACCOUNT_ERROR_NONE;
6050         account_stmt    hstmt = NULL;
6051         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
6052         int                     rc = 0, binding_count = 1;
6053
6054         ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
6055         ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
6056         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
6057
6058         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
6059
6060         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
6061         hstmt = _account_prepare_query(query);
6062
6063         if( _account_db_err_code() == SQLITE_PERM ){
6064                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
6065                 return ACCOUNT_ERROR_PERMISSION_DENIED;
6066         }
6067
6068         _account_query_bind_text(hstmt, binding_count++, app_id);
6069
6070         rc = _account_query_step(hstmt);
6071         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
6072
6073         label_s* label_record = NULL;
6074
6075         while (rc == SQLITE_ROW) {
6076                 bool cb_ret = FALSE;
6077                 label_record = (label_s*) malloc(sizeof(label_s));
6078
6079                 if (label_record == NULL) {
6080                         ACCOUNT_FATAL("malloc Failed");
6081                         break;
6082                 }
6083
6084                 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
6085
6086                 _account_type_convert_column_to_label(hstmt, label_record);
6087
6088                 cb_ret = callback(label_record->app_id, label_record->label , label_record->locale, user_data);
6089
6090                 _account_type_free_label_with_items(label_record);
6091
6092 //              ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
6093                 if(cb_ret == TRUE) {
6094                         _INFO("Callback func returs FALSE, its iteration is stopped!!!!\n");
6095                         break;
6096                 }
6097
6098                 rc = _account_query_step(hstmt);
6099         }
6100
6101         rc = _account_query_finalize(hstmt);
6102         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
6103         hstmt = NULL;
6104
6105         error_code = ACCOUNT_ERROR_NONE;
6106
6107 CATCH:
6108         if (hstmt != NULL) {
6109                 rc = _account_query_finalize(hstmt);
6110                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
6111                 hstmt = NULL;
6112         }
6113 /*
6114         if (error_code == ACCOUNT_ERROR_RECORD_NOT_FOUND) {
6115                 error_code = account_type_query_label_by_app_id_from_global_db(callback, app_id, user_data);
6116         }
6117 */
6118         return error_code;
6119 }
6120
6121 int account_type_query_label_by_app_id(account_label_cb callback, const char* app_id, void *user_data )
6122 {
6123         int                     error_code = ACCOUNT_ERROR_NONE;
6124
6125         ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
6126         ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
6127         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
6128
6129         error_code = _account_type_query_label_by_app_id(callback, app_id, user_data);
6130
6131         if (error_code == ACCOUNT_ERROR_RECORD_NOT_FOUND) {
6132                 error_code = _account_type_query_label_by_app_id_from_global_db(callback, app_id, user_data);
6133         }
6134
6135         return error_code;
6136 }
6137
6138 int _account_type_label_get_app_id(label_h label, char **app_id)
6139 {
6140         if (!label) {
6141                 return ACCOUNT_ERROR_INVALID_PARAMETER;
6142         }
6143
6144         if (!app_id) {
6145                 return ACCOUNT_ERROR_INVALID_PARAMETER;
6146         }
6147
6148         label_s *data = (label_s*)label;
6149
6150         (*app_id) = NULL;
6151
6152         *app_id = _account_get_text(data->app_id);
6153
6154         return ACCOUNT_ERROR_NONE;
6155 }
6156
6157 bool _account_get_label_text_cb(char* app_id, char* label, char* locale, void *user_data)
6158 {
6159         account_type_s *data = (account_type_s*)user_data;
6160
6161         label_s *label_data = (label_s*)malloc(sizeof(label_s));
6162
6163         if (label_data == NULL) {
6164                 ACCOUNT_FATAL("_account_get_label_text_cb : MALLOC FAIL\n");
6165                 return FALSE;
6166         }
6167         ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
6168
6169         label_data->app_id = _account_get_text(app_id);
6170         label_data->label = _account_get_text(label);
6171         label_data->locale = _account_get_text(locale);
6172
6173         data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
6174
6175         return TRUE;
6176 }
6177
6178 bool _account_get_provider_feature_cb(char* app_id, char* key, void* user_data)
6179 {
6180         account_type_s *data = (account_type_s*)user_data;
6181
6182         provider_feature_s *feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
6183
6184         if (feature_data == NULL) {
6185                 ACCOUNT_FATAL("_account_get_provider_feature_cb : MALLOC FAIL\n");
6186                 return FALSE;
6187         }
6188         ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
6189
6190         feature_data->app_id = _account_get_text(app_id);
6191         feature_data->key = _account_get_text(key);
6192
6193         data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
6194
6195         return TRUE;
6196 }
6197
6198 int _account_type_query_by_app_id_from_global_db(const char* app_id, account_type_s** account_type_record)
6199 {
6200         _INFO("_account_type_query_by_app_id_from_global_db start");
6201
6202         int                     error_code = ACCOUNT_ERROR_NONE;
6203         account_stmt    hstmt = NULL;
6204         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
6205         int                     rc = 0, binding_count = 1;
6206
6207         ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
6208         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
6209         ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
6210
6211         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
6212
6213         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
6214         hstmt = _account_prepare_query_from_global_db(query);
6215
6216         if( _account_db_err_code_from_global_db() == SQLITE_PERM ){
6217                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg_from_global_db());
6218                 return ACCOUNT_ERROR_PERMISSION_DENIED;
6219         }
6220
6221         _account_query_bind_text(hstmt, binding_count++, app_id);
6222
6223         rc = _account_query_step(hstmt);
6224         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
6225
6226         *account_type_record = create_empty_account_type_instance();
6227
6228         while (rc == SQLITE_ROW) {
6229                 _account_type_convert_column_to_account_type(hstmt, *account_type_record);
6230                 rc = _account_query_step(hstmt);
6231         }
6232
6233         rc = _account_query_finalize_from_global_db(hstmt);
6234         ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {_ERR("global db finalize error rc=[%d]", rc);}, rc, ("finalize error"));
6235         _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb, app_id, (void*)(*account_type_record));
6236         _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, app_id,(void*)(*account_type_record));
6237
6238         hstmt = NULL;
6239         error_code = ACCOUNT_ERROR_NONE;
6240
6241 CATCH:
6242         if (hstmt != NULL) {
6243                 rc = _account_query_finalize(hstmt);
6244                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
6245                 hstmt = NULL;
6246         }
6247
6248         _INFO("_account_type_query_by_app_id_from_global_db end [%d]", error_code);
6249         return error_code;
6250 }
6251
6252 int _account_type_query_by_app_id(const char* app_id, account_type_s** account_type_record)
6253 {
6254         _INFO("_account_type_query_by_app_id start");
6255
6256         int                     error_code = ACCOUNT_ERROR_NONE;
6257         account_stmt    hstmt = NULL;
6258         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
6259         int                     rc = 0, binding_count = 1;
6260
6261         ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
6262         ACCOUNT_RETURN_VAL((account_type_record != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("account type record(account_type_s**) is NULL"));
6263         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
6264
6265         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
6266
6267         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
6268         hstmt = _account_prepare_query(query);
6269
6270         if( _account_db_err_code() == SQLITE_PERM ){
6271                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
6272                 return ACCOUNT_ERROR_PERMISSION_DENIED;
6273         }
6274
6275         _account_query_bind_text(hstmt, binding_count++, app_id);
6276
6277         rc = _account_query_step(hstmt);
6278         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
6279
6280         *account_type_record = create_empty_account_type_instance();
6281         if (*account_type_record == NULL) {
6282                 _ERR("Out of Memory");
6283                 error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
6284                 goto CATCH;
6285         }
6286
6287         while (rc == SQLITE_ROW) {
6288                 _account_type_convert_column_to_account_type(hstmt, *account_type_record);
6289                 rc = _account_query_step(hstmt);
6290         }
6291
6292         rc = _account_query_finalize(hstmt);
6293         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
6294         _account_type_query_label_by_app_id(_account_get_label_text_cb, app_id, (void*)(*account_type_record));
6295         _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, app_id,(void*)(*account_type_record));
6296
6297         hstmt = NULL;
6298         error_code = ACCOUNT_ERROR_NONE;
6299
6300 CATCH:
6301         if (hstmt != NULL) {
6302                 rc = _account_query_finalize(hstmt);
6303                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
6304                 hstmt = NULL;
6305         }
6306
6307         if (error_code == ACCOUNT_ERROR_RECORD_NOT_FOUND) {
6308                 error_code = _account_type_query_by_app_id_from_global_db(app_id, account_type_record);
6309         }
6310
6311         _INFO("_account_type_query_by_app_id end [%d]", error_code);
6312         return error_code;
6313 }
6314
6315 int _account_type_query_app_id_exist(const char* app_id)
6316 {
6317         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
6318         int                     rc = 0;
6319
6320         ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
6321         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
6322
6323         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
6324
6325         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
6326         rc = _account_get_record_count(query);
6327
6328         if( _account_db_err_code() == SQLITE_PERM ){
6329                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
6330                 return ACCOUNT_ERROR_PERMISSION_DENIED;
6331         }
6332
6333         if (rc <= 0) {
6334                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
6335         }
6336
6337         return ACCOUNT_ERROR_NONE;
6338 }
6339
6340 int _account_type_query_app_id_exist_from_all_db(const char *app_id)
6341 {
6342         _INFO("_account_type_query_app_id_exist_from_all_db start app_id=%s", app_id);
6343         int return_code = ACCOUNT_ERROR_NONE;
6344
6345         return_code = _account_type_query_app_id_exist(app_id);
6346
6347         if (return_code == ACCOUNT_ERROR_RECORD_NOT_FOUND) {
6348                 return_code = _account_type_query_app_id_exist_from_global_db(app_id);
6349         } else {
6350                 return return_code;
6351         }
6352         _INFO("_account_type_query_app_id_exist_from_all_db end");
6353         return return_code;
6354 }
6355
6356 int _account_type_query_by_provider_feature_from_global_db(const char* key, GSList **account_type_list_all)
6357 {
6358         int error_code = ACCOUNT_ERROR_NONE;
6359         account_stmt    hstmt = NULL;
6360         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
6361         int                     rc = 0;
6362         GSList                  *account_type_list = NULL;
6363
6364         _INFO("_account_type_query_by_provider_feature_from_global_db start key=%s", key);
6365         if(key == NULL)
6366         {
6367                 ACCOUNT_ERROR("capability_type IS NULL.");
6368                 error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
6369                 goto CATCH;
6370         }
6371
6372         if(g_hAccountGlobalDB == NULL)
6373         {
6374                 ACCOUNT_ERROR("The database isn't connected.");
6375                 error_code = ACCOUNT_ERROR_DB_NOT_OPENED;
6376                 goto CATCH;
6377         }
6378
6379         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
6380
6381         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
6382
6383         hstmt = _account_prepare_query_from_global_db(query);
6384
6385         if( _account_db_err_code_from_global_db() == SQLITE_PERM )
6386         {
6387                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg_from_global_db());
6388                 error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
6389                 goto CATCH;
6390         }
6391
6392         int binding_count = 1;
6393         _account_query_bind_text(hstmt, binding_count++, key);
6394
6395         rc = _account_query_step(hstmt);
6396
6397         account_type_s *account_type_record = NULL;
6398
6399         if(rc != SQLITE_ROW)
6400         {
6401                 ACCOUNT_ERROR("The record isn't found. rc=[%d]", rc);
6402                 error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
6403                 goto CATCH;
6404         }
6405
6406         while(rc == SQLITE_ROW) {
6407                 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
6408
6409                 if (account_type_record == NULL) {
6410                         ACCOUNT_FATAL("malloc Failed");
6411                         break;
6412                 }
6413
6414                 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
6415                 _account_type_convert_column_to_account_type(hstmt, account_type_record);
6416                 account_type_list = g_slist_append(account_type_list, account_type_record);
6417                 rc = _account_query_step(hstmt);
6418         }
6419
6420         rc = _account_query_finalize_from_global_db(hstmt);
6421         if (rc != ACCOUNT_ERROR_NONE )
6422         {
6423                 _account_type_gslist_account_type_free(account_type_list);
6424                 ACCOUNT_ERROR("finalize error(%s)", rc);
6425                 error_code = rc;
6426                 goto CATCH;
6427         }
6428         hstmt = NULL;
6429
6430         GSList* iter;
6431
6432         for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
6433                 account_type_s *account_type = NULL;
6434                 account_type = (account_type_s*)iter->data;
6435                 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
6436                 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
6437                 _INFO("add label & provider_feature");
6438         }
6439
6440         for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
6441
6442                 account_type_s *account_type = NULL;
6443                 account_type = (account_type_s*)iter->data;
6444                 *account_type_list_all = g_slist_append(*account_type_list_all, account_type);
6445                 _INFO("add account_type");
6446         }
6447
6448         error_code = ACCOUNT_ERROR_NONE;
6449
6450 CATCH:
6451         if (hstmt != NULL) {
6452                 rc = _account_query_finalize_from_global_db(hstmt);
6453                 if (rc != ACCOUNT_ERROR_NONE)
6454                 {
6455                         ACCOUNT_ERROR("finalize error(%s)", rc);
6456                         return rc;
6457                 }
6458                 hstmt = NULL;
6459         }
6460
6461         _INFO("_account_type_query_by_provider_feature_from_global_db end. error_code=[%d]", error_code);
6462         return error_code;
6463 }
6464
6465 GSList* _account_type_query_by_provider_feature(const char* key, int *error_code)
6466 {
6467         *error_code = ACCOUNT_ERROR_NONE;
6468         account_stmt hstmt = NULL;
6469         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
6470         int rc = 0;
6471         GSList *account_type_list = NULL;
6472
6473         _INFO("account_type_query_by_provider_feature start key=%s", key);
6474         if(key == NULL)
6475         {
6476                 ACCOUNT_ERROR("capability_type IS NULL.");
6477                 *error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
6478                 goto CATCH;
6479         }
6480
6481         if(g_hAccountDB == NULL)
6482         {
6483                 ACCOUNT_ERROR("The database isn't connected.");
6484                 *error_code = ACCOUNT_ERROR_DB_NOT_OPENED;
6485                 goto CATCH;
6486         }
6487
6488         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
6489
6490         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
6491
6492         hstmt = _account_prepare_query(query);
6493
6494         if( _account_db_err_code() == SQLITE_PERM )
6495         {
6496                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
6497                 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
6498                 goto CATCH;
6499         }
6500
6501         int binding_count = 1;
6502         _account_query_bind_text(hstmt, binding_count++, key);
6503
6504         rc = _account_query_step(hstmt);
6505
6506         account_type_s *account_type_record = NULL;
6507
6508         if(rc != SQLITE_ROW)
6509         {
6510                 ACCOUNT_ERROR("The record isn't found. rc=[%d]", rc);
6511                 *error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
6512                 goto CATCH;
6513         }
6514
6515         while(rc == SQLITE_ROW) {
6516                 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
6517
6518                 if (account_type_record == NULL) {
6519                         ACCOUNT_FATAL("malloc Failed");
6520                         break;
6521                 }
6522
6523                 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
6524                 _account_type_convert_column_to_account_type(hstmt, account_type_record);
6525                 account_type_list = g_slist_append(account_type_list, account_type_record);
6526                 rc = _account_query_step(hstmt);
6527         }
6528
6529         rc = _account_query_finalize(hstmt);
6530         if (rc != ACCOUNT_ERROR_NONE )
6531         {
6532                 _account_type_gslist_account_type_free(account_type_list);
6533                 ACCOUNT_ERROR("finalize error(%s)", rc);
6534                 *error_code = rc;
6535                 goto CATCH;
6536         }
6537         hstmt = NULL;
6538
6539         GSList* iter;
6540
6541         for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
6542                 account_type_s *account_type = NULL;
6543                 account_type = (account_type_s*)iter->data;
6544                 _account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
6545                 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
6546         }
6547
6548         *error_code = ACCOUNT_ERROR_NONE;
6549
6550 CATCH:
6551         if (hstmt != NULL) {
6552                 rc = _account_query_finalize(hstmt);
6553                 if (rc != ACCOUNT_ERROR_NONE)
6554                 {
6555                         *error_code = rc;
6556                         return NULL;
6557                 }
6558                 hstmt = NULL;
6559         }
6560
6561         if (*error_code == ACCOUNT_ERROR_NONE || *error_code == ACCOUNT_ERROR_RECORD_NOT_FOUND) {
6562                 rc = _account_type_query_by_provider_feature_from_global_db(key, &account_type_list);
6563                 if (rc != ACCOUNT_ERROR_NONE && rc != ACCOUNT_ERROR_RECORD_NOT_FOUND) {
6564                         ACCOUNT_ERROR( "_account_type_query_by_provider_feature_from_global_db fail=[%d]", rc);
6565                         _account_type_gslist_account_type_free(account_type_list);
6566                         return NULL;
6567                 }
6568                 if (rc == ACCOUNT_ERROR_NONE)
6569                         *error_code = rc;
6570         }
6571
6572         _INFO("account_type_query_by_provider_feature end");
6573         return account_type_list;
6574 }
6575
6576 int _account_type_query_all_from_global_db(GSList **account_type_list_all)
6577 {
6578         account_stmt    hstmt = NULL;
6579         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
6580         int rc = ACCOUNT_ERROR_NONE;
6581         int error_code = ACCOUNT_ERROR_NONE;
6582         GSList *account_type_list = NULL;
6583
6584         _INFO("_account_type_query_all_in_global_db start");
6585         ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, NULL, ("The database isn't connected."));
6586
6587         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
6588
6589         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
6590         hstmt = _account_prepare_query_from_global_db(query);
6591
6592         rc = _account_query_step(hstmt);
6593
6594         if( _account_db_err_code_from_global_db() == SQLITE_PERM ){
6595                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg_from_global_db());
6596                 return ACCOUNT_ERROR_PERMISSION_DENIED;
6597         }
6598
6599         account_type_s *account_type_record = NULL;
6600
6601         if (rc != SQLITE_ROW)
6602         {
6603                 _INFO("[ACCOUNT_ERROR_RECORD_NOT_FOUND]The record isn't found.");
6604                 error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
6605                 goto CATCH;
6606         }
6607
6608         while(rc == SQLITE_ROW) {
6609                 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
6610
6611                 if (account_type_record == NULL) {
6612                         ACCOUNT_FATAL("malloc Failed");
6613                         break;
6614                 }
6615
6616                 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
6617                 _account_type_convert_column_to_account_type(hstmt, account_type_record);
6618                 account_type_list = g_slist_append(account_type_list, account_type_record);
6619                 rc = _account_query_step(hstmt);
6620         }
6621
6622         rc = _account_query_finalize_from_global_db(hstmt);
6623         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
6624         hstmt = NULL;
6625
6626         GSList* iter;
6627
6628         for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
6629                 account_type_s *account_type = NULL;
6630                 account_type = (account_type_s*)iter->data;
6631                 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
6632                 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
6633         }
6634
6635         for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
6636                 account_type_s *account_type = NULL;
6637                 account_type = (account_type_s*)iter->data;
6638                 *account_type_list_all = g_slist_append(*account_type_list_all, account_type);
6639         }
6640
6641         error_code = ACCOUNT_ERROR_NONE;
6642 CATCH:
6643         if (hstmt != NULL)
6644         {
6645                 rc = _account_query_finalize_from_global_db(hstmt);
6646                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list);}, rc, ("finalize error"));
6647                 hstmt = NULL;
6648         }
6649
6650         _INFO("_account_type_query_all_in_global_db end");
6651         return error_code;
6652 }
6653
6654 GSList* _account_type_query_all(void)
6655 {
6656         account_stmt hstmt = NULL;
6657         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
6658         int rc = 0;
6659         int error_code = ACCOUNT_ERROR_NONE;
6660         GSList *account_type_list = NULL;
6661
6662         _INFO("_account_type_query_all start");
6663         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected."));
6664
6665         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
6666
6667         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
6668         hstmt = _account_prepare_query(query);
6669
6670         rc = _account_query_step(hstmt);
6671
6672         if( _account_db_err_code() == SQLITE_PERM ){
6673                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
6674                 return NULL;
6675         }
6676
6677         account_type_s *account_type_record = NULL;
6678
6679         if (rc != SQLITE_ROW)
6680         {
6681                 _INFO("[ACCOUNT_ERROR_RECORD_NOT_FOUND]The record isn't found.");
6682                 error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
6683                 goto CATCH;
6684         }
6685
6686         while(rc == SQLITE_ROW) {
6687                 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
6688
6689                 if (account_type_record == NULL) {
6690                         ACCOUNT_FATAL("malloc Failed");
6691                         break;
6692                 }
6693
6694                 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
6695                 _account_type_convert_column_to_account_type(hstmt, account_type_record);
6696                 account_type_list = g_slist_append(account_type_list, account_type_record);
6697                 rc = _account_query_step(hstmt);
6698         }
6699
6700         rc = _account_query_finalize(hstmt);
6701         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, NULL, ("finalize error"));
6702         hstmt = NULL;
6703
6704         GSList* iter;
6705
6706         for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
6707                 account_type_s *account_type = NULL;
6708                 account_type = (account_type_s*)iter->data;
6709                 _account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
6710                 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
6711         }
6712
6713         error_code = ACCOUNT_ERROR_NONE;
6714 CATCH:
6715         if (hstmt != NULL)
6716         {
6717                 rc = _account_query_finalize(hstmt);
6718                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list);}, NULL, ("finalize error"));
6719                 hstmt = NULL;
6720         }
6721
6722         if (error_code == ACCOUNT_ERROR_NONE || error_code == ACCOUNT_ERROR_RECORD_NOT_FOUND) {
6723                 error_code = _account_type_query_all_from_global_db(&account_type_list);
6724                 if (rc != ACCOUNT_ERROR_NONE && rc != ACCOUNT_ERROR_RECORD_NOT_FOUND) {
6725                         ACCOUNT_ERROR( "_account_type_query_all_from_global_db fail=[%d]", rc);
6726                         _account_type_gslist_account_type_free(account_type_list);
6727                         return NULL;
6728                 }
6729         }
6730
6731         _INFO("_account_type_query_all end");
6732         return account_type_list;
6733 }
6734
6735 // output parameter label must be free
6736 int _account_type_query_label_by_locale_from_global_db(const char* app_id, const char* locale, char **label)
6737 {
6738         int                     error_code = ACCOUNT_ERROR_NONE;
6739         account_stmt    hstmt = NULL;
6740         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
6741         int                     rc = 0, binding_count = 1;
6742         char*                   converted_locale = NULL;
6743
6744         ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
6745         ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
6746         ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
6747         ACCOUNT_RETURN_VAL((locale != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("locale char is null"));
6748         //Making label newly created
6749
6750         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
6751
6752         converted_locale = _account_get_text(locale);
6753         gchar** tokens = g_strsplit(converted_locale, "-", 2);
6754
6755         if(tokens != NULL) {
6756                 if((char*)(tokens[1]) != NULL) {
6757                         char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1]));
6758                         if(upper_token != NULL) {
6759                                 _ACCOUNT_FREE(converted_locale);
6760                                 converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token);
6761                         }
6762                         _ACCOUNT_FREE(upper_token);
6763                 }
6764         }
6765         g_strfreev(tokens);
6766
6767         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, converted_locale);
6768         _ACCOUNT_FREE(converted_locale);
6769
6770         hstmt = _account_prepare_query_from_global_db(query);
6771
6772         if( _account_db_err_code_from_global_db() == SQLITE_PERM ){
6773                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg_from_global_db());
6774                 return ACCOUNT_ERROR_PERMISSION_DENIED;
6775         }
6776
6777         _account_query_bind_text(hstmt, binding_count++, app_id);
6778
6779         rc = _account_query_step(hstmt);
6780         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
6781
6782         label_s* label_record = NULL;
6783
6784         while (rc == SQLITE_ROW) {
6785                 label_record = (label_s*) malloc(sizeof(label_s));
6786
6787                 if (label_record == NULL) {
6788                         ACCOUNT_FATAL("malloc Failed");
6789                         break;
6790                 }
6791
6792                 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
6793
6794                 _account_type_convert_column_to_label(hstmt,label_record);
6795
6796                 _ACCOUNT_FREE(*label);
6797                 //Making label newly created
6798                 *label = _account_get_text(label_record->label);
6799
6800                 _account_type_free_label_with_items(label_record);
6801
6802                 rc = _account_query_step(hstmt);
6803         }
6804
6805         rc = _account_query_finalize_from_global_db(hstmt);
6806         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
6807         hstmt = NULL;
6808
6809         error_code = ACCOUNT_ERROR_NONE;
6810
6811 CATCH:
6812         if (hstmt != NULL) {
6813                 rc = _account_query_finalize_from_global_db(hstmt);
6814                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
6815                 hstmt = NULL;
6816         }
6817
6818         _INFO("_account_type_query_label_by_locale_from_global_db() end : error_code = %d", error_code);
6819         return error_code;
6820 }
6821
6822 // output parameter label must be free
6823 int _account_type_query_label_by_locale(const char* app_id, const char* locale, char **label)
6824 {
6825         int                     error_code = ACCOUNT_ERROR_NONE;
6826         account_stmt    hstmt = NULL;
6827         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
6828         int                     rc = 0, binding_count = 1;
6829         char*                   converted_locale = NULL;
6830
6831         ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
6832         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
6833         ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
6834         ACCOUNT_RETURN_VAL((locale != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("locale char is null"));
6835         //Making label newly created
6836
6837         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
6838
6839         converted_locale = _account_get_text(locale);
6840         gchar** tokens = g_strsplit(converted_locale, "-", 2);
6841
6842         if(tokens != NULL) {
6843                 if((char*)(tokens[1]) != NULL) {
6844                         char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1]));
6845                         if(upper_token != NULL) {
6846                                 _ACCOUNT_FREE(converted_locale);
6847                                 converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token);
6848                         }
6849                         _ACCOUNT_FREE(upper_token);
6850                 }
6851         }
6852         g_strfreev(tokens);
6853
6854         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, converted_locale);
6855         _ACCOUNT_FREE(converted_locale);
6856
6857         hstmt = _account_prepare_query(query);
6858
6859         if( _account_db_err_code() == SQLITE_PERM ){
6860                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
6861                 return ACCOUNT_ERROR_PERMISSION_DENIED;
6862         }
6863
6864         _account_query_bind_text(hstmt, binding_count++, app_id);
6865
6866         rc = _account_query_step(hstmt);
6867         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
6868
6869         label_s* label_record = NULL;
6870
6871         while (rc == SQLITE_ROW) {
6872                 label_record = (label_s*) malloc(sizeof(label_s));
6873
6874                 if (label_record == NULL) {
6875                         ACCOUNT_FATAL("malloc Failed");
6876                         break;
6877                 }
6878
6879                 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
6880
6881                 _account_type_convert_column_to_label(hstmt,label_record);
6882
6883                 _ACCOUNT_FREE(*label);
6884                 //Making label newly created
6885                 *label = _account_get_text(label_record->label);
6886
6887                 _account_type_free_label_with_items(label_record);
6888
6889                 rc = _account_query_step(hstmt);
6890         }
6891
6892         rc = _account_query_finalize(hstmt);
6893         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
6894         hstmt = NULL;
6895
6896         error_code = ACCOUNT_ERROR_NONE;
6897
6898         CATCH:
6899         if (hstmt != NULL) {
6900                 rc = _account_query_finalize(hstmt);
6901                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
6902                 hstmt = NULL;
6903         }
6904
6905         if (error_code == ACCOUNT_ERROR_RECORD_NOT_FOUND) {
6906                 error_code = _account_type_query_label_by_locale_from_global_db(app_id, locale, label);
6907         }
6908
6909         _INFO("_account_type_query_label_by_locale() end : error_code = %d", error_code);
6910         return error_code;
6911 }
6912
6913 static int _account_insert_custom(account_s *account, int account_id)
6914 {
6915         _INFO("_account_insert_custom start");
6916
6917         int                     rc, count = 1;
6918         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
6919         account_stmt    hstmt = NULL;
6920
6921         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
6922
6923         if (g_slist_length( account->custom_list)==0) {
6924                 ACCOUNT_DEBUG( "_account_insert_custom, no custom data\n");
6925                 return ACCOUNT_ERROR_NONE;
6926         }
6927
6928         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
6929
6930         rc = _account_get_record_count(query);
6931
6932         if( _account_db_err_code() == SQLITE_PERM ){
6933                 ACCOUNT_ERROR( "Access failed(%d, %s)", _account_db_err_msg());
6934                 return ACCOUNT_ERROR_PERMISSION_DENIED;
6935         }
6936
6937         if (rc <= 0) {
6938                 ACCOUNT_SLOGE( "_account_insert_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
6939                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
6940         }
6941
6942         /* insert query*/
6943
6944         GSList *iter;
6945
6946         for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
6947                 int ret;
6948                 count = 1;
6949                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
6950                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (AccountId, AppId, Key, Value) VALUES "
6951                                 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
6952
6953                 hstmt = _account_prepare_query(query);
6954
6955                 if( _account_db_err_code() == SQLITE_PERM ){
6956                         ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
6957                         return ACCOUNT_ERROR_PERMISSION_DENIED;
6958                 }
6959
6960                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
6961
6962                 account_custom_s* custom_data = NULL;
6963                 custom_data = (account_custom_s*)iter->data;
6964
6965                 ret = _account_query_bind_int(hstmt, count++, account_id);
6966                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
6967                 ret = _account_query_bind_text(hstmt, count++, account->package_name);
6968                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
6969                 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
6970                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
6971                 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
6972                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
6973
6974                 rc = _account_query_step(hstmt);
6975
6976                 if (rc != SQLITE_DONE) {
6977                         ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
6978                         break;
6979                 }
6980
6981                 rc = _account_query_finalize(hstmt);
6982                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
6983                 hstmt = NULL;
6984
6985         }
6986
6987         _INFO("_account_insert_custom end");
6988         return ACCOUNT_ERROR_NONE;
6989 }
6990
6991 static int _account_update_custom(account_s *account, int account_id)
6992 {
6993         int                     rc, count = 1;
6994         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
6995         account_stmt    hstmt = NULL;
6996
6997         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
6998
6999         if (g_slist_length( account->custom_list)==0) {
7000                 ACCOUNT_DEBUG( "_account_update_custom, no custom data\n");
7001                 return ACCOUNT_ERROR_NONE;
7002         }
7003
7004         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
7005
7006         rc = _account_get_record_count(query);
7007
7008         if( _account_db_err_code() == SQLITE_PERM ){
7009                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
7010                 pthread_mutex_unlock(&account_mutex);
7011                 return ACCOUNT_ERROR_PERMISSION_DENIED;
7012         } else if( _account_db_err_code() == SQLITE_BUSY ){
7013                 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
7014                 pthread_mutex_unlock(&account_mutex);
7015                 return ACCOUNT_ERROR_DATABASE_BUSY;
7016         }
7017
7018         if (rc <= 0) {
7019                 ACCOUNT_SLOGE( "_account_update_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
7020                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
7021         }
7022
7023         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
7024
7025         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId=? ", ACCOUNT_CUSTOM_TABLE);
7026         hstmt = _account_prepare_query(query);
7027         count = 1;
7028         _account_query_bind_int(hstmt, count++, (int)account_id);
7029         rc = _account_query_step(hstmt);
7030
7031         if (rc == SQLITE_BUSY) {
7032                 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
7033                 return ACCOUNT_ERROR_DATABASE_BUSY;
7034         } else if (rc != SQLITE_DONE) {
7035                 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
7036                 return ACCOUNT_ERROR_DB_FAILED;
7037         }
7038
7039         rc = _account_query_finalize(hstmt);
7040         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
7041         hstmt = NULL;
7042
7043         GSList *iter;
7044
7045         for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
7046                 int ret;
7047                 count = 1;
7048                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
7049                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AccountId, AppId, Key, Value) VALUES "
7050                                 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
7051
7052                 hstmt = _account_prepare_query(query);
7053
7054                 if( _account_db_err_code() == SQLITE_PERM ){
7055                         ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
7056                         return ACCOUNT_ERROR_PERMISSION_DENIED;
7057                 }
7058
7059                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
7060
7061                 account_custom_s* custom_data = NULL;
7062                 custom_data = (account_custom_s*)iter->data;
7063
7064                 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
7065                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
7066                 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
7067                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
7068                 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
7069                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
7070                 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
7071                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
7072
7073                 rc = _account_query_step(hstmt);
7074
7075                 if (rc != SQLITE_DONE) {
7076                         ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
7077                         break;
7078                 }
7079
7080                 rc = _account_query_finalize(hstmt);
7081                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
7082                 hstmt = NULL;
7083
7084         }
7085
7086         return ACCOUNT_ERROR_NONE;
7087 }
7088
7089 static int _account_query_custom_by_account_id(account_custom_cb callback, int account_id, void *user_data )
7090 {
7091         int                     error_code = ACCOUNT_ERROR_NONE;
7092         account_stmt    hstmt = NULL;
7093         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
7094         int                     rc = 0;
7095
7096         ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
7097         ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
7098         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
7099
7100         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
7101
7102         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
7103         hstmt = _account_prepare_query(query);
7104
7105         if( _account_db_err_code() == SQLITE_PERM ){
7106                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
7107                 return ACCOUNT_ERROR_PERMISSION_DENIED;
7108         }
7109
7110         rc = _account_query_step(hstmt);
7111
7112         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
7113
7114         account_custom_s* custom_record = NULL;
7115
7116         while (rc == SQLITE_ROW) {
7117                 bool cb_ret = FALSE;
7118                 custom_record = (account_custom_s*) malloc(sizeof(account_custom_s));
7119
7120                 if (custom_record == NULL) {
7121                         ACCOUNT_FATAL("malloc Failed");
7122                         break;
7123                 }
7124
7125                 ACCOUNT_MEMSET(custom_record, 0x00, sizeof(account_custom_s));
7126
7127                 _account_convert_column_to_custom(hstmt, custom_record);
7128
7129                 cb_ret = callback(custom_record->key, custom_record->value, user_data);
7130
7131                 _account_free_custom_with_items(custom_record);
7132
7133                 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
7134
7135                 rc = _account_query_step(hstmt);
7136         }
7137
7138         rc = _account_query_finalize(hstmt);
7139         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
7140         hstmt = NULL;
7141
7142         error_code = ACCOUNT_ERROR_NONE;
7143
7144 CATCH:
7145         if (hstmt != NULL) {
7146                 rc = _account_query_finalize(hstmt);
7147                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
7148                 hstmt = NULL;
7149         }
7150
7151         pthread_mutex_unlock(&account_mutex);
7152         return error_code;
7153 }