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