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