merge from tizen 2.4
[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         _INFO("");
1836         *account_id = _account_get_next_sequence(ACCOUNT_TABLE);
1837         data->id = *account_id;
1838
1839         char* appid = NULL;
1840         appid = _account_get_current_appid(pid);
1841         _INFO("");
1842
1843         if(!appid)
1844         {
1845                 _INFO("");
1846                 // API caller cannot be recognized
1847                 ret_transaction = _account_end_transaction(FALSE);
1848                 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1849                 pthread_mutex_unlock(&account_mutex);
1850                 return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
1851         }
1852
1853         _INFO("");
1854         char* verified_appid = NULL;
1855         error_code  = _account_check_account_type_with_appid_group(appid, &verified_appid);//FIX
1856         _ACCOUNT_FREE(appid);
1857         if(error_code != ACCOUNT_ERROR_NONE)
1858         {
1859                 _ERR("error_code = %d", error_code);
1860                 ret_transaction = _account_end_transaction(FALSE);
1861                 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1862                 _ACCOUNT_FREE(verified_appid);
1863                 pthread_mutex_unlock(&account_mutex);
1864                 return error_code;
1865         }
1866
1867         if(verified_appid)
1868         {
1869                 _INFO("");
1870                 error_code = _account_check_duplicated(data, verified_appid);
1871                 if (error_code != ACCOUNT_ERROR_NONE) {
1872                         _INFO("");
1873                         ret_transaction = _account_end_transaction(FALSE);
1874                         ACCOUNT_DEBUG("_account_check_duplicated(), rollback insert query(%x)!!!!\n", ret_transaction);
1875                         *account_id = -1;
1876                         pthread_mutex_unlock(&account_mutex);
1877                         return error_code;
1878                 }
1879                 if(!_account_check_add_more_account(verified_appid)) {
1880                         ret_transaction = _account_end_transaction(FALSE);
1881                         ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
1882                         pthread_mutex_unlock(&account_mutex);
1883                         _ACCOUNT_FREE(verified_appid);
1884                         return ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
1885                 }
1886
1887                 _ACCOUNT_FREE(data->package_name);
1888                 data->package_name = _account_get_text(verified_appid);
1889                 _ACCOUNT_FREE(verified_appid);
1890         }
1891
1892         if(!_account_check_add_more_account(data->package_name))
1893         {
1894                 _INFO("");
1895                 ret_transaction = _account_end_transaction(FALSE);
1896                 ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
1897                 pthread_mutex_unlock(&account_mutex);
1898                 return ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
1899         }
1900
1901         error_code = _account_execute_insert_query(data);
1902
1903         if (error_code != ACCOUNT_ERROR_NONE)
1904         {
1905                 _INFO("");
1906                 ret_transaction = _account_end_transaction(FALSE);
1907                 ACCOUNT_ERROR("INSERT account fail, rollback insert query(%x)!!!!\n", ret_transaction);
1908                 *account_id = -1;
1909                 pthread_mutex_unlock(&account_mutex);
1910                 return error_code;
1911         }
1912
1913         _INFO("");
1914         error_code = _account_insert_capability(data, *account_id);
1915         if (error_code != ACCOUNT_ERROR_NONE)
1916         {
1917                 _INFO("");
1918                 ret_transaction = _account_end_transaction(FALSE);
1919                 ACCOUNT_ERROR("INSERT capability fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1920                 *account_id = -1;
1921                 pthread_mutex_unlock(&account_mutex);
1922                 return error_code;
1923         }
1924
1925         _INFO("");
1926         error_code = _account_insert_custom(data, *account_id);
1927         if (error_code != ACCOUNT_ERROR_NONE)
1928         {
1929                 ret_transaction = _account_end_transaction(FALSE);
1930                 ACCOUNT_ERROR("INSERT custom fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1931                 *account_id = -1;
1932                 pthread_mutex_unlock(&account_mutex);
1933                 return error_code;
1934         }
1935
1936         _INFO("");
1937
1938         pthread_mutex_unlock(&account_mutex);
1939         _account_end_transaction(TRUE);
1940         ACCOUNT_SLOGD("(%s)-(%d) account _end_transaction.\n", __FUNCTION__, __LINE__);
1941
1942         char buf[64]={0,};
1943         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_INSERT, *account_id);
1944         _account_insert_delete_update_notification_send(buf);
1945         _INFO("account _notification_send end.");
1946
1947         return ACCOUNT_ERROR_NONE;
1948
1949 }
1950
1951 int _account_query_capability_by_account_id(capability_cb callback, int account_id, void *user_data )
1952 {
1953         int                     error_code = ACCOUNT_ERROR_NONE;
1954         account_stmt    hstmt = NULL;
1955         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1956         int                     rc = 0;
1957
1958         ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1959         ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
1960         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1961
1962         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1963
1964         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
1965         hstmt = _account_prepare_query(query);
1966
1967         if( _account_db_err_code() == SQLITE_PERM ){
1968                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
1969                 return ACCOUNT_ERROR_PERMISSION_DENIED;
1970         }
1971
1972         rc = _account_query_step(hstmt);
1973         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1974
1975         account_capability_s* capability_record = NULL;
1976
1977         while (rc == SQLITE_ROW) {
1978                 bool cb_ret = FALSE;
1979                 capability_record = (account_capability_s*) malloc(sizeof(account_capability_s));
1980
1981                 if (capability_record == NULL) {
1982                         ACCOUNT_FATAL("malloc Failed");
1983                         break;
1984                 }
1985
1986                 ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s));
1987
1988                 _account_convert_column_to_capability(hstmt, capability_record);
1989
1990                 cb_ret = callback(capability_record->type, capability_record->value, user_data);
1991
1992                 _account_free_capability_items(capability_record);
1993                 _ACCOUNT_FREE(capability_record);
1994
1995                 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
1996
1997                 rc = _account_query_step(hstmt);
1998         }
1999
2000         rc = _account_query_finalize(hstmt);
2001         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2002         hstmt = NULL;
2003
2004         error_code = ACCOUNT_ERROR_NONE;
2005
2006 CATCH:
2007         if (hstmt != NULL) {
2008                 rc = _account_query_finalize(hstmt);
2009                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2010                 hstmt = NULL;
2011         }
2012
2013         pthread_mutex_unlock(&account_mutex);
2014         return error_code;
2015 }
2016
2017 GSList* _account_get_capability_list_by_account_id(int account_id, int *error_code)
2018 {
2019         *error_code = ACCOUNT_ERROR_NONE;
2020         account_stmt    hstmt = NULL;
2021         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2022         int                     rc = 0;
2023         GSList* capability_list = NULL;
2024
2025         ACCOUNT_RETURN_VAL((account_id > 0), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("ACCOUNT INDEX IS LESS THAN 0"));
2026         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
2027
2028         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2029
2030         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
2031         hstmt = _account_prepare_query(query);
2032
2033         if( _account_db_err_code() == SQLITE_PERM ){
2034                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2035                 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
2036                 return NULL;
2037         }
2038
2039         rc = _account_query_step(hstmt);
2040         ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2041
2042         account_capability_s* capability_record = NULL;
2043
2044         while (rc == SQLITE_ROW) {
2045                 capability_record = (account_capability_s*) malloc(sizeof(account_capability_s));
2046
2047                 if (capability_record == NULL) {
2048                         ACCOUNT_FATAL("malloc Failed");
2049                         break;
2050                 }
2051
2052                 ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s));
2053
2054                 _account_convert_column_to_capability(hstmt, capability_record);
2055
2056                 //cb_ret = callback(capability_record->type, capability_record->value, user_data);
2057
2058                 //_account_free_capability_items(capability_record);
2059                 //_ACCOUNT_FREE(capability_record);
2060
2061                 //ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
2062
2063                 capability_list = g_slist_append(capability_list, capability_record);
2064                 rc = _account_query_step(hstmt);
2065         }
2066
2067         rc = _account_query_finalize(hstmt);
2068         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
2069         hstmt = NULL;
2070
2071         *error_code = ACCOUNT_ERROR_NONE;
2072
2073 CATCH:
2074         if (hstmt != NULL)
2075         {
2076                 rc = _account_query_finalize(hstmt);
2077                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
2078                 hstmt = NULL;
2079         }
2080
2081         pthread_mutex_unlock(&account_mutex);
2082         return capability_list;
2083 }
2084
2085 static int _account_compare_old_record(account_s *new_account, int account_id)
2086 {
2087         int                             error_code = ACCOUNT_ERROR_NONE;
2088         account_stmt    hstmt = NULL;
2089         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2090         int                             rc = 0;
2091         account_s *old_account = NULL;
2092
2093         ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2094         ACCOUNT_RETURN_VAL((new_account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2095         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2096
2097         old_account = (account_s*)calloc(1, sizeof(account_s));
2098
2099         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2100
2101         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
2102         hstmt = _account_prepare_query(query);
2103
2104         rc = _account_query_step(hstmt);
2105         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2106
2107         while (rc == SQLITE_ROW) {
2108                 _account_convert_column_to_account(hstmt, old_account);
2109                 rc = _account_query_step(hstmt);
2110         }
2111
2112         rc = _account_query_finalize(hstmt);
2113         ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2114         hstmt = NULL;
2115
2116         // get capability
2117         error_code = _account_query_capability_by_account_id(_account_get_capability_text_cb, old_account->id, (void*)old_account);
2118         ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
2119
2120         // get custom text
2121         error_code = _account_query_custom_by_account_id(_account_get_custom_text_cb, old_account->id, (void*)old_account);
2122         ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
2123
2124         // compare
2125
2126         new_account->id = old_account->id;
2127
2128         //user name
2129         if(!new_account->user_name) {
2130                 if(old_account->user_name)
2131                         new_account->user_name = _account_get_text(old_account->user_name);
2132         }
2133
2134         // display name
2135         if(!new_account->display_name) {
2136                 if(old_account->display_name)
2137                         new_account->display_name = _account_get_text(old_account->display_name);
2138         }
2139
2140         // email address
2141         if(!new_account->email_address) {
2142                 if(old_account->email_address)
2143                         new_account->email_address = _account_get_text(old_account->email_address);
2144         }
2145
2146         // domain name
2147         if(!new_account->domain_name) {
2148                 if(old_account->domain_name)
2149                         new_account->domain_name = _account_get_text(old_account->domain_name);
2150         }
2151
2152         // icon path
2153         if(!new_account->icon_path) {
2154                 if(old_account->icon_path)
2155                         new_account->icon_path = _account_get_text(old_account->icon_path);
2156         }
2157
2158         // source
2159         if(!new_account->source) {
2160                 if(old_account->source)
2161                         new_account->source = _account_get_text(old_account->source);
2162         }
2163
2164         _ACCOUNT_FREE(new_account->package_name);
2165         new_account->package_name = _account_get_text(old_account->package_name);
2166
2167         // access token
2168         if(!new_account->access_token) {
2169                 if(old_account->access_token)
2170                         new_account->access_token = _account_get_text(old_account->access_token);
2171         }
2172
2173         // user text
2174         int i;
2175         for(i=0;i<USER_TXT_CNT;i++) {
2176                 if(!new_account->user_data_txt[i]) {
2177                         if(old_account->user_data_txt[i])
2178                                 new_account->user_data_txt[i] = _account_get_text(old_account->user_data_txt[i]);
2179                 }
2180         }
2181
2182         // auth type
2183         if(new_account->auth_type == ACCOUNT_AUTH_TYPE_INVALID) {
2184                 new_account->auth_type = old_account->auth_type;
2185         }
2186
2187         //secret
2188         if(new_account->secret== ACCOUNT_SECRECY_INVALID) {
2189                 new_account->secret = old_account->secret;
2190         }
2191
2192         // sync support
2193         if(new_account->sync_support == ACCOUNT_SYNC_INVALID) {
2194                 new_account->sync_support = old_account->sync_support;
2195         }
2196
2197         // user int
2198         for(i=0;i<USER_INT_CNT;i++) {
2199                 if(new_account->user_data_int[i] == 0) {
2200                                 new_account->user_data_int[i] = old_account->user_data_int[i];
2201                 }
2202         }
2203
2204         // capability
2205
2206         // user custom table
2207
2208 CATCH:
2209                 if (old_account) {
2210                         _account_free_account_items(old_account);
2211                         _ACCOUNT_FREE(old_account);
2212                 }
2213
2214                 if (hstmt != NULL) {
2215                         rc = _account_query_finalize(hstmt);
2216                         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2217                         hstmt = NULL;
2218                 }
2219
2220         return ACCOUNT_ERROR_NONE;
2221 }
2222
2223 static int _account_get_package_name_from_account_id(int account_id, char **package_name)
2224 {
2225         int                             error_code = ACCOUNT_ERROR_NONE;
2226         account_stmt    hstmt = NULL;
2227         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2228         int                             rc = 0;
2229         account_s *old_account = NULL;
2230
2231         ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2232         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2233
2234         old_account = (account_s*)calloc(1, sizeof(account_s));
2235
2236         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2237
2238         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
2239         hstmt = _account_prepare_query(query);
2240
2241         rc = _account_query_step(hstmt);
2242         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2243
2244         while (rc == SQLITE_ROW) {
2245                 _account_convert_column_to_account(hstmt, old_account);
2246                 rc = _account_query_step(hstmt);
2247         }
2248
2249         rc = _account_query_finalize(hstmt);
2250         ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2251         hstmt = NULL;
2252
2253         // get package name.
2254         *package_name = _account_get_text(old_account->package_name);
2255
2256
2257         CATCH:
2258                 if (old_account) {
2259                         _account_free_account_items(old_account);
2260                         _ACCOUNT_FREE(old_account);
2261                 }
2262
2263                 if (hstmt != NULL) {
2264                         rc = _account_query_finalize(hstmt);
2265                         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2266                         hstmt = NULL;
2267                 }
2268
2269         return error_code;
2270
2271 }
2272
2273 static int _account_update_account(int pid, account_s *account, int account_id)
2274 {
2275         int                             rc = 0, binding_count =0;
2276         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2277         int                             error_code = ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0;
2278         account_stmt    hstmt = NULL;
2279
2280         if (!account->package_name) {
2281                 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
2282                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2283         }
2284
2285         /* Check permission of requested appid */
2286         char* current_appid = NULL;
2287         char *package_name = NULL;
2288
2289         current_appid = _account_get_current_appid(pid);
2290         error_code = _account_get_package_name_from_account_id(account_id, &package_name);
2291
2292         if(error_code != ACCOUNT_ERROR_NONE || package_name == NULL){
2293                 ACCOUNT_ERROR("No package name with account_id\n");
2294                 _ACCOUNT_FREE(current_appid);
2295                 _ACCOUNT_FREE(package_name);
2296                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2297         }
2298
2299         error_code = _account_check_appid_group_with_package_name(current_appid, package_name);
2300         ACCOUNT_DEBUG( "UPDATE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name);     // TODO: remove the log later.
2301
2302         _ACCOUNT_FREE(current_appid);
2303         _ACCOUNT_FREE(package_name);
2304
2305         if(error_code != ACCOUNT_ERROR_NONE){
2306                 ACCOUNT_ERROR("No permission to update\n");
2307                 return ACCOUNT_ERROR_PERMISSION_DENIED;
2308         }
2309
2310         _account_compare_old_record(account, account_id);
2311
2312         if( _account_db_err_code() == SQLITE_PERM ){
2313                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2314                 return ACCOUNT_ERROR_PERMISSION_DENIED;
2315         } else if( _account_db_err_code() == SQLITE_BUSY ){
2316                 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
2317                 return ACCOUNT_ERROR_DATABASE_BUSY;
2318         }
2319
2320         if (!account->user_name && !account->display_name && !account->email_address) {
2321                 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
2322                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2323         }
2324
2325         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2326
2327         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
2328
2329         count = _account_get_record_count(query);
2330         if (count <= 0) {
2331                 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
2332                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2333         }
2334
2335         /* transaction control required*/
2336         ret_transaction = _account_begin_transaction();
2337         if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
2338                 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
2339                 pthread_mutex_unlock(&account_mutex);
2340                 return ACCOUNT_ERROR_DATABASE_BUSY;
2341         }
2342
2343         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2344         ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
2345                         "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
2346                         "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
2347                         "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
2348
2349         hstmt = _account_prepare_query(query);
2350
2351         if( _account_db_err_code() == SQLITE_PERM ){
2352                 ret_transaction = _account_end_transaction(FALSE);
2353                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2354                 return ACCOUNT_ERROR_PERMISSION_DENIED;
2355         }
2356
2357         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)));
2358
2359         binding_count = _account_convert_account_to_sql(account, hstmt, query);
2360         _account_query_bind_int(hstmt, binding_count++, account_id);
2361
2362         rc = _account_query_step(hstmt);
2363         if (rc != SQLITE_DONE) {
2364                 ACCOUNT_SLOGE( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
2365         }
2366
2367         rc = _account_query_finalize(hstmt);
2368         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2369         hstmt = NULL;
2370
2371         _INFO("update query=%s", query);
2372
2373         /*update capability*/
2374         error_code = _account_update_capability(account, account_id);
2375         if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2376                 ret_transaction = _account_end_transaction(FALSE);
2377                 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2378                 return error_code;
2379         }
2380
2381         /* update custom */
2382         error_code = _account_update_custom(account, account_id);
2383         if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2384                 ret_transaction = _account_end_transaction(FALSE);
2385                 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2386                 return error_code;
2387         }
2388
2389         ret_transaction = _account_end_transaction(TRUE);
2390
2391         _INFO("update end");
2392         return error_code;
2393 }
2394
2395
2396 static int _account_update_account_ex(account_s *account, int account_id)
2397 {
2398         int                             rc = 0, binding_count =0;
2399         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2400         int                             error_code = ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0;
2401         account_stmt    hstmt = NULL;
2402
2403         if (!account->package_name) {
2404                 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
2405                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2406         }
2407
2408         _account_compare_old_record(account, account_id);
2409
2410         if( _account_db_err_code() == SQLITE_PERM ){
2411                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2412                 return ACCOUNT_ERROR_PERMISSION_DENIED;
2413         }
2414
2415         if (!account->user_name && !account->display_name && !account->email_address) {
2416                 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
2417                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2418         }
2419
2420         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2421
2422         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
2423
2424         count = _account_get_record_count(query);
2425         if (count <= 0) {
2426                 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
2427                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2428         }
2429
2430         /* transaction control required*/
2431         ret_transaction = _account_begin_transaction();
2432         if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
2433                 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
2434                 pthread_mutex_unlock(&account_mutex);
2435                 return ACCOUNT_ERROR_DATABASE_BUSY;
2436         }
2437
2438         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2439         ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
2440                         "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
2441                         "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
2442                         "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
2443
2444         hstmt = _account_prepare_query(query);
2445
2446         if( _account_db_err_code() == SQLITE_PERM ){
2447                 ret_transaction = _account_end_transaction(FALSE);
2448                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2449                 return ACCOUNT_ERROR_PERMISSION_DENIED;
2450         }
2451
2452         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)));
2453
2454         _INFO("account_update_to_db_by_id_ex_p : before convert() : account_id[%d], user_name=%s", account->id, account->user_name);
2455         binding_count = _account_convert_account_to_sql(account, hstmt, query);
2456         _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], user_name=%s", account->id, account->user_name);
2457         _INFO("account_update_to_db_by_id_ex_p : before bind()");
2458         rc = _account_query_bind_int(hstmt, binding_count++, account_id);
2459         _INFO("account_update_to_db_by_id_ex_p : after bind() : ret = %d", rc);
2460
2461         rc = _account_query_step(hstmt);
2462         if (rc != SQLITE_DONE) {
2463                 ACCOUNT_SLOGE( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
2464         }
2465         _INFO("account_update_to_db_by_id_ex_p : after query_step() : ret = %d", rc);
2466
2467         rc = _account_query_finalize(hstmt);
2468         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2469         hstmt = NULL;
2470         _INFO("account_update_to_db_by_id_ex_p : after query_filnalize() : ret = %d", rc);
2471
2472         _INFO("account_update_to_db_by_id_ex_p : before update_capability()");
2473         /*update capability*/
2474         error_code = _account_update_capability(account, account_id);
2475         if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2476                 ret_transaction = _account_end_transaction(FALSE);
2477                 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2478                 return error_code;
2479         }
2480         _INFO("account_update_to_db_by_id_ex_p : after update_capability()");
2481
2482         _INFO("account_update_to_db_by_id_ex_p : before update_custom()");
2483         /* update custom */
2484         error_code = _account_update_custom(account, account_id);
2485         if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2486                 ret_transaction = _account_end_transaction(FALSE);
2487                 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2488                 return error_code;
2489         }
2490         _INFO("account_update_to_db_by_id_ex_p : after update_custom()");
2491
2492         ret_transaction = _account_end_transaction(TRUE);
2493
2494         return error_code;
2495 }
2496
2497
2498 int _account_update_to_db_by_id(int pid, account_s* account, int account_id)
2499 {
2500         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
2501         ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
2502         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2503         int     error_code = ACCOUNT_ERROR_NONE;
2504         account_s* data = (account_s*)account;
2505
2506         pthread_mutex_lock(&account_mutex);
2507
2508         error_code = _account_update_account(pid, data, account_id);
2509
2510         if(error_code != ACCOUNT_ERROR_NONE) {
2511                 pthread_mutex_unlock(&account_mutex);
2512                 return error_code;
2513         }
2514
2515         pthread_mutex_unlock(&account_mutex);
2516
2517         char buf[64]={0,};
2518         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, account_id);
2519         _account_insert_delete_update_notification_send(buf);
2520
2521         return ACCOUNT_ERROR_NONE;
2522 }
2523
2524 int _account_update_to_db_by_id_ex(account_s* account, int account_id)
2525 {
2526         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
2527         ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
2528         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2529         int     error_code = ACCOUNT_ERROR_NONE;
2530         account_s* data = account;
2531
2532         pthread_mutex_lock(&account_mutex);
2533
2534         _INFO("before update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name);
2535         error_code = _account_update_account_ex(data, account_id);
2536         _INFO("after update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name);
2537
2538         if(error_code != ACCOUNT_ERROR_NONE) {
2539                 pthread_mutex_unlock(&account_mutex);
2540                 return error_code;
2541         }
2542
2543         pthread_mutex_unlock(&account_mutex);
2544
2545         char buf[64]={0,};
2546         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, account_id);
2547         _account_insert_delete_update_notification_send(buf);
2548
2549         return ACCOUNT_ERROR_NONE;
2550 }
2551
2552
2553 int _account_update_to_db_by_user_name(int pid, account_s* account, const char *user_name, const char *package_name)
2554 {
2555         ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
2556         ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
2557         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2558
2559         int     error_code = ACCOUNT_ERROR_NONE;
2560         account_s *data = (account_s*)account;
2561
2562         pthread_mutex_lock(&account_mutex);
2563
2564         error_code = _account_update_account_by_user_name(pid, data, user_name, package_name);
2565
2566         pthread_mutex_unlock(&account_mutex);
2567
2568         char buf[64]={0,};
2569         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, data->id);
2570         _account_insert_delete_update_notification_send(buf);
2571
2572         return error_code;
2573 }
2574
2575 GSList* _account_db_query_all(int pid)
2576 {
2577         //int                   error_code = ACCOUNT_ERROR_NONE;
2578         account_stmt    hstmt = NULL;
2579         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2580         int                     rc = 0;
2581         GSList                  *account_list = NULL;
2582
2583         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected."));
2584
2585         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2586
2587         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TABLE);
2588         hstmt = _account_prepare_query(query);
2589
2590         if( _account_db_err_code() == SQLITE_PERM ){
2591                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2592                 return NULL;
2593         }
2594
2595         rc = _account_query_step(hstmt);
2596
2597         account_s *account_record = NULL;
2598
2599         if (rc != SQLITE_ROW)
2600         {
2601                 _ERR("The record isn't found");
2602                 goto CATCH;
2603         }
2604
2605         while(rc == SQLITE_ROW) {
2606                 account_record = (account_s*) malloc(sizeof(account_s));
2607
2608                 if (account_record == NULL) {
2609                         ACCOUNT_FATAL("malloc Failed");
2610                         break;
2611                 }
2612
2613                 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2614                 _account_convert_column_to_account(hstmt, account_record);
2615                 account_list = g_slist_append(account_list, account_record);
2616                 rc = _account_query_step(hstmt);
2617         }
2618
2619         rc = _account_query_finalize(hstmt);
2620         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, NULL, ("finalize error"));
2621         hstmt = NULL;
2622
2623         GSList* iter;
2624
2625         for (iter = account_list; iter != NULL; iter = g_slist_next(iter)) {
2626                 account_s *account = NULL;
2627                 account = (account_s*)iter->data;
2628                 _account_query_capability_by_account_id(_account_get_capability_text_cb, account->id, (void*)account);
2629                 _account_query_custom_by_account_id(_account_get_custom_text_cb, account->id, (void*)account);
2630         }
2631
2632 CATCH:
2633         if (hstmt != NULL) {
2634                 rc = _account_query_finalize(hstmt);
2635                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {_account_gslist_free(account_list);}, NULL, ("finalize error"));
2636                 hstmt = NULL;
2637         }
2638         if (account_list)
2639         {
2640                 _remove_sensitive_info_from_non_owning_account_slist(pid, account_list);
2641         }
2642         return account_list;
2643 }
2644
2645 int _account_update_sync_status_by_id(int account_db_id, const int sync_status)
2646 {
2647         int                             error_code = ACCOUNT_ERROR_NONE;
2648         account_stmt    hstmt = NULL;
2649         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2650         int                             rc = 0;
2651         int count =1;
2652
2653         ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2654         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2655         if ( (sync_status < 0) || (sync_status > ACCOUNT_SYNC_STATUS_RUNNING)) {
2656                 ACCOUNT_SLOGE("(%s)-(%d) sync_status is less than 0 or more than enum max.\n", __FUNCTION__, __LINE__);
2657                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2658         }
2659
2660         pthread_mutex_lock(&account_mutex);
2661
2662         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2663
2664         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_db_id);
2665
2666         rc = _account_get_record_count(query);
2667
2668         if( _account_db_err_code() == SQLITE_PERM ){
2669                 pthread_mutex_unlock(&account_mutex);
2670                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2671                 return ACCOUNT_ERROR_PERMISSION_DENIED;
2672         }
2673
2674         if (rc <= 0) {
2675                 ACCOUNT_SLOGE( "account_update_sync_status_by_id : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
2676                 pthread_mutex_unlock(&account_mutex);
2677                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2678         }
2679
2680         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2681
2682         ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET sync_support=? WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
2683         hstmt = _account_prepare_query(query);
2684
2685         _account_query_bind_int(hstmt, count, sync_status);
2686
2687         rc = _account_query_step(hstmt);
2688
2689         if( _account_db_err_code() == SQLITE_PERM ){
2690                 pthread_mutex_unlock(&account_mutex);
2691                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2692                 return ACCOUNT_ERROR_PERMISSION_DENIED;
2693         }
2694
2695         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_DB_FAILED,
2696                                 ("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg()));
2697
2698         rc = _account_query_finalize(hstmt);
2699         if (rc != ACCOUNT_ERROR_NONE) {
2700                 ACCOUNT_ERROR("_account_query_finalize error");
2701                 pthread_mutex_unlock(&account_mutex);
2702                 return rc;
2703         }
2704         char buf[64]={0,};
2705         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_SYNC_UPDATE, account_db_id);
2706         _account_insert_delete_update_notification_send(buf);
2707
2708         hstmt = NULL;
2709         error_code = ACCOUNT_ERROR_NONE;
2710
2711 CATCH:
2712         if (hstmt != NULL) {
2713                 rc = _account_query_finalize(hstmt);
2714                 pthread_mutex_unlock(&account_mutex);
2715                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2716                 hstmt = NULL;
2717         }
2718
2719         pthread_mutex_unlock(&account_mutex);
2720         return error_code;
2721 }
2722
2723 int _account_query_account_by_account_id(int pid, int account_db_id, account_s *account_record)
2724 {
2725         _INFO("_account_query_account_by_account_id() start, account_db_id=[%d]", account_db_id);
2726
2727         int                             error_code = ACCOUNT_ERROR_NONE;
2728         account_stmt    hstmt = NULL;
2729         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2730         int                             rc = 0;
2731
2732         ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2733         ACCOUNT_RETURN_VAL(account_record != NULL, {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2734         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2735
2736         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2737
2738         ACCOUNT_DEBUG("starting db operations");
2739
2740         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
2741         hstmt = _account_prepare_query(query);
2742         rc = _account_db_err_code();
2743         _INFO("after _account_prepare_query, rc=[%d]", rc);
2744
2745         if( rc == SQLITE_PERM ){
2746                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2747                 return ACCOUNT_ERROR_PERMISSION_DENIED;
2748         }
2749
2750         ACCOUNT_DEBUG("before _account_query_step");
2751         rc = _account_query_step(hstmt);
2752         ACCOUNT_DEBUG("after _account_query_step returned [%d]", rc);
2753         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2754
2755         while (rc == SQLITE_ROW) {
2756                 ACCOUNT_DEBUG("before _account_convert_column_to_account");
2757                 _account_convert_column_to_account(hstmt, account_record);
2758                 ACCOUNT_DEBUG("after _account_convert_column_to_account");
2759                 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]);
2760                 rc = _account_query_step(hstmt);
2761         }
2762
2763         ACCOUNT_DEBUG("account_record->id=[%d]", account_record->id);
2764
2765         rc = _account_query_finalize(hstmt);
2766         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2767
2768         ACCOUNT_DEBUG("before _account_query_capability_by_account_id");
2769         _account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record);
2770         ACCOUNT_DEBUG("after _account_query_capability_by_account_id");
2771
2772         ACCOUNT_DEBUG("before _account_query_custom_by_account_id");
2773         _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record);
2774         ACCOUNT_DEBUG("after _account_query_custom_by_account_id");
2775
2776         hstmt = NULL;
2777         error_code = ACCOUNT_ERROR_NONE;
2778
2779 CATCH:
2780         if (hstmt != NULL) {
2781                 rc = _account_query_finalize(hstmt);
2782                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2783                 hstmt = NULL;
2784         }
2785
2786         if (account_record)
2787         {
2788                 _remove_sensitive_info_from_non_owning_account(pid, account_record);
2789         }
2790         pthread_mutex_unlock(&account_mutex);
2791         ACCOUNT_DEBUG("_account_query_account_by_account_id end [%d]", error_code);
2792         return error_code;
2793 }
2794
2795 GList* _account_query_account_by_user_name(int pid, const char *user_name, int *error_code)
2796 {
2797         *error_code = ACCOUNT_ERROR_NONE;
2798         account_stmt    hstmt = NULL;
2799         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2800         int                             rc = 0;
2801         account_s *account_head = NULL;
2802
2803         if (user_name == NULL)
2804         {
2805                 _ERR("USER NAME IS NULL");
2806                 *error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
2807                 goto CATCH;
2808         }
2809
2810         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2811
2812         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ?", ACCOUNT_TABLE);
2813
2814         hstmt = _account_prepare_query(query);
2815
2816         if (_account_db_err_code() == SQLITE_PERM)
2817         {
2818                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2819                 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
2820                 goto CATCH;
2821         }
2822
2823         int binding_count = 1;
2824         _account_query_bind_text(hstmt, binding_count++, user_name);
2825
2826         rc = _account_query_step(hstmt);
2827
2828         if (rc != SQLITE_ROW)
2829         {
2830                 _ERR("The record isn't found");
2831                 *error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
2832                 goto CATCH;
2833         }
2834
2835         int tmp = 0;
2836
2837         account_head = (account_s*) malloc(sizeof(account_s));
2838         if (account_head == NULL) {
2839                 ACCOUNT_FATAL("malloc Failed");
2840                 if (hstmt != NULL) {
2841                         rc = _account_query_finalize(hstmt);
2842
2843                         if (rc != ACCOUNT_ERROR_NONE)
2844                         {
2845                                 _ERR("finalize error");
2846                                 *error_code = rc;
2847                                 goto CATCH;
2848                         }
2849                         hstmt = NULL;
2850                 }
2851                 *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
2852                 goto CATCH;
2853         }
2854         ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2855
2856         while (rc == SQLITE_ROW) {
2857                 account_s* account_record = NULL;
2858
2859                 account_record = (account_s*) malloc(sizeof(account_s));
2860
2861                 if (account_record == NULL) {
2862                         ACCOUNT_FATAL("malloc Failed");
2863                         break;
2864                 }
2865                 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2866
2867                 _account_convert_column_to_account(hstmt, account_record);
2868
2869                 account_head->account_list = g_list_append(account_head->account_list, account_record);
2870
2871                 rc = _account_query_step(hstmt);
2872                 tmp++;
2873         }
2874
2875         rc = _account_query_finalize(hstmt);
2876
2877         if (rc != ACCOUNT_ERROR_NONE)
2878         {
2879                 _ERR("finalize error");
2880                 *error_code = rc;
2881                 goto CATCH;
2882         }
2883
2884         hstmt = NULL;
2885
2886         GList *iter;
2887
2888
2889         tmp = g_list_length(account_head->account_list);
2890
2891         for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2892                 account_h account;
2893                 account = (account_h)iter->data;
2894
2895                 account_s *testaccount = (account_s*)account;
2896
2897                 _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
2898                 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
2899
2900         }
2901
2902         *error_code = ACCOUNT_ERROR_NONE;
2903
2904 CATCH:
2905         if (hstmt != NULL) {
2906                 rc = _account_query_finalize(hstmt);
2907                 if (rc != ACCOUNT_ERROR_NONE)
2908                 {
2909                         _ERR("finalize error");
2910                         *error_code = rc;
2911                 }
2912                 hstmt = NULL;
2913         }
2914
2915         pthread_mutex_unlock(&account_mutex);
2916         if (account_head)
2917         {
2918                 _remove_sensitive_info_from_non_owning_account_list(pid, account_head->account_list);
2919                 GList* result = account_head->account_list;
2920                 _ACCOUNT_FREE(account_head);
2921                 return result;
2922         }
2923         return NULL;
2924 }
2925
2926 GList*
2927 _account_query_account_by_capability(int pid, const char* capability_type, const int capability_value, int *error_code)
2928 {
2929         *error_code = ACCOUNT_ERROR_NONE;
2930         account_stmt    hstmt = NULL;
2931         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2932         int                     rc = 0;
2933
2934         ACCOUNT_RETURN_VAL((capability_type != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("capability_type IS NULL"));
2935
2936         if ((capability_value  < 0) || (capability_value > ACCOUNT_CAPABILITY_ENABLED)) {
2937                 ACCOUNT_SLOGE("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
2938                 *error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
2939                 return NULL;
2940         }
2941
2942         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
2943
2944         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2945
2946         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=? AND value=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
2947
2948         hstmt = _account_prepare_query(query);
2949
2950         if( _account_db_err_code() == SQLITE_PERM ){
2951                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2952                 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
2953                 return NULL;
2954         }
2955
2956         int binding_count = 1;
2957         _account_query_bind_text(hstmt, binding_count++, capability_type);
2958         _account_query_bind_int(hstmt, binding_count++, capability_value);
2959
2960         rc = _account_query_step(hstmt);
2961
2962         account_s* account_head = NULL;
2963
2964         ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2965
2966         int tmp = 0;
2967
2968         account_head = (account_s*) malloc(sizeof(account_s));
2969         if (account_head == NULL) {
2970                 ACCOUNT_FATAL("malloc Failed");
2971                 if (hstmt != NULL) {
2972                         rc = _account_query_finalize(hstmt);
2973                         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
2974                         hstmt = NULL;
2975                 }
2976                 *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
2977                 return NULL;
2978         }
2979         ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2980
2981         while (rc == SQLITE_ROW) {
2982                 account_s* account_record = NULL;
2983
2984                 account_record = (account_s*) malloc(sizeof(account_s));
2985
2986                 if (account_record == NULL) {
2987                         ACCOUNT_FATAL("malloc Failed");
2988                         break;
2989                 }
2990                 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2991
2992                 _account_convert_column_to_account(hstmt, account_record);
2993
2994                 account_head->account_list = g_list_append(account_head->account_list, account_record);
2995
2996                 rc = _account_query_step(hstmt);
2997                 tmp++;
2998         }
2999
3000         rc = _account_query_finalize(hstmt);
3001         ACCOUNT_CATCH_ERROR_P((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3002         hstmt = NULL;
3003
3004         GList *iter;
3005
3006
3007         tmp = g_list_length(account_head->account_list);
3008
3009         for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3010                 account_h account = NULL;
3011                 account = (account_h)iter->data;
3012                 account_s* testaccount = (account_s*)account;
3013
3014                 _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3015                 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3016
3017         }
3018
3019
3020         *error_code = ACCOUNT_ERROR_NONE;
3021
3022 CATCH:
3023         if (hstmt != NULL) {
3024                 rc = _account_query_finalize(hstmt);
3025                 if ( rc != ACCOUNT_ERROR_NONE ) {
3026                         *error_code = rc;
3027                         _ERR("finalize error");
3028                 }
3029                 hstmt = NULL;
3030         }
3031
3032         if( *error_code != ACCOUNT_ERROR_NONE && account_head ) {
3033                 _account_glist_free(account_head->account_list);
3034                 _ACCOUNT_FREE(account_head);
3035                 account_head = NULL;
3036         }
3037
3038         pthread_mutex_unlock(&account_mutex);
3039
3040         if (account_head)
3041         {
3042                 _remove_sensitive_info_from_non_owning_account_list(pid, account_head->account_list);
3043                 GList* result = account_head->account_list;
3044                 _ACCOUNT_FREE(account_head);
3045                 return result;
3046         }
3047         return NULL;
3048 }
3049
3050 GList* _account_query_account_by_capability_type(int pid, const char* capability_type, int *error_code)
3051 {
3052         *error_code = ACCOUNT_ERROR_NONE;
3053         account_stmt    hstmt = NULL;
3054         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3055         int                     rc = 0;
3056
3057         ACCOUNT_RETURN_VAL((capability_type != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("capability_type IS NULL"));
3058         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;},
3059                                            NULL, ("The database isn't connected."));
3060
3061         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3062
3063         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
3064
3065         hstmt = _account_prepare_query(query);
3066
3067         if( _account_db_err_code() == SQLITE_PERM ){
3068                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3069                 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
3070                 return NULL;
3071         }
3072
3073         int binding_count = 1;
3074         _account_query_bind_text(hstmt, binding_count++, capability_type);
3075
3076         rc = _account_query_step(hstmt);
3077
3078         account_s* account_head = NULL;
3079
3080         ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3081
3082         int tmp = 0;
3083
3084         account_head = (account_s*) malloc(sizeof(account_s));
3085         if (account_head == NULL) {
3086                 ACCOUNT_FATAL("malloc Failed");
3087                 if (hstmt != NULL) {
3088                         rc = _account_query_finalize(hstmt);
3089                         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
3090                         hstmt = NULL;
3091                 }
3092                 *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
3093                 return NULL;
3094         }
3095         ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3096
3097         while (rc == SQLITE_ROW) {
3098                 account_s* account_record = NULL;
3099
3100                 account_record = (account_s*) malloc(sizeof(account_s));
3101
3102                 if (account_record == NULL) {
3103                         ACCOUNT_FATAL("malloc Failed");
3104                         break;
3105                 }
3106                 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3107
3108                 _account_convert_column_to_account(hstmt, account_record);
3109
3110                 account_head->account_list = g_list_append(account_head->account_list, account_record);
3111
3112                 rc = _account_query_step(hstmt);
3113                 tmp++;
3114         }
3115
3116         rc = _account_query_finalize(hstmt);
3117         ACCOUNT_CATCH_ERROR_P((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3118         hstmt = NULL;
3119
3120         GList *iter;
3121
3122
3123         tmp = g_list_length(account_head->account_list);
3124
3125         for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3126                 account_s* testaccount = (account_s*)iter->data;
3127
3128                 _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3129                 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3130
3131         }
3132
3133         *error_code = ACCOUNT_ERROR_NONE;
3134
3135 CATCH:
3136         if (hstmt != NULL)
3137         {
3138                 rc = _account_query_finalize(hstmt);
3139                 if (rc != ACCOUNT_ERROR_NONE) {
3140                         *error_code = rc;
3141                         _ERR("finalize error");
3142                 }
3143                 hstmt = NULL;
3144         }
3145
3146         if( (*error_code != ACCOUNT_ERROR_NONE) && account_head ) {
3147                 _account_glist_free(account_head->account_list);
3148                 _ACCOUNT_FREE(account_head);
3149                 account_head = NULL;
3150         }
3151
3152         pthread_mutex_unlock(&account_mutex);
3153
3154         if (account_head)
3155         {
3156                 _remove_sensitive_info_from_non_owning_account_list(pid, account_head->account_list);
3157                 GList* result = account_head->account_list;
3158                 _ACCOUNT_FREE(account_head);
3159                 return result;
3160         }
3161         return NULL;
3162 }
3163
3164 GList* _account_query_account_by_package_name(int pid,const char* package_name, int *error_code)
3165 {
3166         _INFO("_account_query_account_by_package_name");
3167
3168         *error_code = ACCOUNT_ERROR_NONE;
3169         account_stmt    hstmt = NULL;
3170         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3171         int                     rc = 0;
3172
3173         ACCOUNT_RETURN_VAL((package_name != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("PACKAGE NAME IS NULL"));
3174         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
3175
3176         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3177
3178         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE package_name=?", ACCOUNT_TABLE);
3179
3180         hstmt = _account_prepare_query(query);
3181
3182         if( _account_db_err_code() == SQLITE_PERM ){
3183                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3184                 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
3185                 return NULL;
3186         }
3187
3188         int binding_count = 1;
3189         _account_query_bind_text(hstmt, binding_count++, package_name);
3190
3191         rc = _account_query_step(hstmt);
3192
3193         account_s* account_head = NULL;
3194
3195         ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.(%s)\n", package_name));
3196
3197         int tmp = 0;
3198
3199         account_head = (account_s*) malloc(sizeof(account_s));
3200         if (account_head == NULL) {
3201                 ACCOUNT_FATAL("malloc Failed");
3202                 if (hstmt != NULL) {
3203                         rc = _account_query_finalize(hstmt);
3204                         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
3205                         hstmt = NULL;
3206                 }
3207                 *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
3208                 return NULL;
3209         }
3210         ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3211
3212         while (rc == SQLITE_ROW) {
3213                 account_s* account_record = NULL;
3214
3215                 account_record = (account_s*) malloc(sizeof(account_s));
3216
3217                 if (account_record == NULL) {
3218                         ACCOUNT_FATAL("malloc Failed");
3219                         break;
3220                 }
3221                 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3222
3223                 _account_convert_column_to_account(hstmt, account_record);
3224
3225                 _INFO("Adding account_list");
3226                 account_head->account_list = g_list_append(account_head->account_list, account_record);
3227
3228                 rc = _account_query_step(hstmt);
3229                 tmp++;
3230         }
3231
3232         rc = _account_query_finalize(hstmt);
3233         ACCOUNT_CATCH_ERROR_P((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3234         hstmt = NULL;
3235
3236         GList *iter;
3237
3238         tmp = g_list_length(account_head->account_list);
3239
3240         for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3241                 account_s* testaccount = (account_s*)iter->data;
3242
3243                 _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3244                 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3245         }
3246
3247         *error_code = ACCOUNT_ERROR_NONE;
3248
3249 CATCH:
3250         if (hstmt != NULL)
3251         {
3252                 rc = _account_query_finalize(hstmt);
3253                 if (rc != ACCOUNT_ERROR_NONE) {
3254                         *error_code = rc;
3255                         _ERR("finalize error");
3256                 }
3257                 hstmt = NULL;
3258         }
3259
3260         pthread_mutex_unlock(&account_mutex);
3261
3262         if( (*error_code != ACCOUNT_ERROR_NONE) && account_head ) {
3263                 _account_glist_free(account_head->account_list);
3264                 _ACCOUNT_FREE(account_head);
3265                 account_head = NULL;
3266         }
3267
3268         if ((*error_code == ACCOUNT_ERROR_NONE) && account_head != NULL)
3269         {
3270                 _INFO("Returning account_list");
3271                 _remove_sensitive_info_from_non_owning_account_list(pid,account_head->account_list);
3272                 GList* result = account_head->account_list;
3273                 _ACCOUNT_FREE(account_head);
3274                 return result;
3275         }
3276         return NULL;
3277 }
3278
3279 int _account_delete(int pid, int account_id)
3280 {
3281         int                             error_code = ACCOUNT_ERROR_NONE;
3282         account_stmt    hstmt = NULL;
3283         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3284         int                             rc = 0;
3285         int                             ret_transaction = 0;
3286         bool                    is_success = FALSE;
3287
3288         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3289
3290         int count = -1;
3291         /* Check requested ID to delete */
3292         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id=%d", ACCOUNT_TABLE, account_id);
3293
3294         count = _account_get_record_count(query);
3295
3296         if( _account_db_err_code() == SQLITE_PERM ){
3297                 pthread_mutex_unlock(&account_mutex);
3298                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3299                 return ACCOUNT_ERROR_PERMISSION_DENIED;
3300         }
3301
3302         if (count <= 0) {
3303                 ACCOUNT_ERROR("account id(%d) is not exist. count(%d)\n", account_id, count);
3304                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3305         }
3306
3307         /* Check permission of requested appid */
3308         char* current_appid = NULL;
3309         char *package_name = NULL;
3310
3311         current_appid = _account_get_current_appid(pid);
3312
3313         error_code = _account_get_package_name_from_account_id(account_id, &package_name);
3314
3315         if(error_code != ACCOUNT_ERROR_NONE){
3316                 ACCOUNT_ERROR("No package name with account_id\n");
3317                 _ACCOUNT_FREE(current_appid);
3318                 _ACCOUNT_FREE(package_name);
3319                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3320         }
3321         ACCOUNT_DEBUG( "DELETE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name);
3322
3323         error_code = _account_check_appid_group_with_package_name(current_appid, package_name);
3324
3325         _ACCOUNT_FREE(current_appid);
3326         _ACCOUNT_FREE(package_name);
3327
3328         if(error_code != ACCOUNT_ERROR_NONE){
3329                 ACCOUNT_ERROR("No permission to delete\n");
3330                 return ACCOUNT_ERROR_PERMISSION_DENIED;
3331         }
3332
3333         /* transaction control required*/
3334         ret_transaction = _account_begin_transaction();
3335
3336         if( _account_db_err_code() == SQLITE_PERM ){
3337                 pthread_mutex_unlock(&account_mutex);
3338                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3339                 return ACCOUNT_ERROR_PERMISSION_DENIED;
3340         }
3341
3342         if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
3343                 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
3344                 pthread_mutex_unlock(&account_mutex);
3345                 return ACCOUNT_ERROR_DATABASE_BUSY;
3346         }
3347
3348         if (ret_transaction != ACCOUNT_ERROR_NONE) {
3349                 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3350                 pthread_mutex_unlock(&account_mutex);
3351                 return ret_transaction;
3352         }
3353
3354         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3355         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
3356
3357         hstmt = _account_prepare_query(query);
3358
3359         if( _account_db_err_code() == SQLITE_PERM ){
3360                 pthread_mutex_unlock(&account_mutex);
3361                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3362                 return ACCOUNT_ERROR_PERMISSION_DENIED;
3363         }
3364
3365         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3366                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3367
3368         rc = _account_query_step(hstmt);
3369         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3370
3371         rc = _account_query_finalize(hstmt);
3372
3373         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3374         hstmt = NULL;
3375
3376         ACCOUNT_MEMSET(query, 0, sizeof(query));
3377
3378         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
3379
3380         hstmt = _account_prepare_query(query);
3381         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3382                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3383
3384         rc = _account_query_step(hstmt);
3385         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
3386
3387         rc = _account_query_finalize(hstmt);
3388         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3389         hstmt = NULL;
3390
3391         /* delete custom data */
3392         ACCOUNT_MEMSET(query, 0, sizeof(query));
3393
3394         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
3395
3396         hstmt = _account_prepare_query(query);
3397
3398         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3399                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3400
3401         rc = _account_query_step(hstmt);
3402         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
3403
3404         rc = _account_query_finalize(hstmt);
3405         ACCOUNT_CATCH_ERROR(rc == ACCOUNT_ERROR_NONE, {}, rc, ("finalize error", account_id, rc));
3406         hstmt = NULL;
3407
3408         is_success = TRUE;
3409
3410 CATCH:
3411         if (hstmt != NULL) {
3412                 rc = _account_query_finalize(hstmt);
3413                 if(rc != ACCOUNT_ERROR_NONE ){
3414                         ACCOUNT_ERROR("rc (%d)", rc);
3415                         is_success = FALSE;
3416                 }
3417
3418                 hstmt = NULL;
3419         }
3420
3421         ret_transaction = _account_end_transaction(is_success);
3422
3423         if (ret_transaction != ACCOUNT_ERROR_NONE) {
3424                 ACCOUNT_ERROR("account_delete:_account_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3425         } else {
3426                 if (is_success == true) {
3427                         char buf[64]={0,};
3428                         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, account_id);
3429                         _account_insert_delete_update_notification_send(buf);
3430                 }
3431         }
3432
3433         pthread_mutex_unlock(&account_mutex);
3434
3435         return error_code;
3436
3437 }
3438
3439 static int _account_query_account_by_username_and_package(const char* username, const char* package_name, account_h *account)
3440 {
3441         //FIXME
3442         //return -1;
3443         int                             error_code = ACCOUNT_ERROR_NONE;
3444         account_stmt    hstmt = NULL;
3445         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3446         int                             rc = 0;
3447         int                             binding_count = 1;
3448
3449         ACCOUNT_RETURN_VAL((username != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("username IS NULL"));
3450         ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name IS NULL"));
3451         ACCOUNT_RETURN_VAL((*account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
3452         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3453
3454         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3455
3456         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
3457         hstmt = _account_prepare_query(query);
3458
3459         if( _account_db_err_code() == SQLITE_PERM ){
3460                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3461                 return ACCOUNT_ERROR_PERMISSION_DENIED;
3462         }
3463
3464         _account_query_bind_text(hstmt, binding_count++, username);
3465         _account_query_bind_text(hstmt, binding_count++, package_name);
3466
3467         rc = _account_query_step(hstmt);
3468         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3469
3470         account_s *account_record = (account_s *)(*account);
3471
3472         while (rc == SQLITE_ROW) {
3473                 _account_convert_column_to_account(hstmt, account_record);
3474                 rc = _account_query_step(hstmt);
3475         }
3476
3477         rc = _account_query_finalize(hstmt);
3478         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3479         _account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record);
3480         _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record);
3481
3482         hstmt = NULL;
3483         error_code = ACCOUNT_ERROR_NONE;
3484
3485 CATCH:
3486         if (hstmt != NULL) {
3487                 rc = _account_query_finalize(hstmt);
3488                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3489                 hstmt = NULL;
3490         }
3491
3492         pthread_mutex_unlock(&account_mutex);
3493         return error_code;
3494 }
3495
3496 int _account_create(account_h *account)
3497 {
3498         if (!account) {
3499                 ACCOUNT_SLOGE("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__);
3500                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3501         }
3502
3503         account_s *data = (account_s*)malloc(sizeof(account_s));
3504
3505         if (data == NULL) {
3506                 ACCOUNT_FATAL("Memory Allocation Failed");
3507                 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3508         }
3509         ACCOUNT_MEMSET(data, 0, sizeof(account_s));
3510
3511         /*Setting account as visible by default*/
3512         data->secret = ACCOUNT_SECRECY_VISIBLE;
3513
3514         /*Setting account as not supporting sync by default*/
3515         data->sync_support = ACCOUNT_SYNC_NOT_SUPPORT;
3516
3517         *account = (account_h)data;
3518
3519         return ACCOUNT_ERROR_NONE;
3520 }
3521
3522 int _account_destroy(account_h account)
3523 {
3524         account_s *data = (account_s*)account;
3525
3526         ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account handle is null!"));
3527
3528         _account_free_account_items(data);
3529         _ACCOUNT_FREE(data);
3530
3531         return ACCOUNT_ERROR_NONE;
3532 }
3533
3534 int _account_get_account_id(account_s* account, int *account_id)
3535 {
3536         if (!account) {
3537                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3538         }
3539         if (!account_id) {
3540                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3541         }
3542
3543         *account_id = account->id;
3544
3545         return ACCOUNT_ERROR_NONE;
3546 }
3547
3548 int _account_delete_from_db_by_user_name(int pid, const char *user_name, const char *package_name)
3549 {
3550         _INFO("[%s][%s]", user_name, package_name);
3551
3552         int                     error_code = ACCOUNT_ERROR_NONE;
3553         account_stmt    hstmt = NULL;
3554         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3555         int                     rc = 0;
3556         int                     ret_transaction = 0;
3557         bool                    is_success = FALSE;
3558         account_h               account = NULL;
3559         int                     binding_count = 1;
3560         int                             account_id = -1;
3561
3562         ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is null!"));
3563         ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
3564         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3565
3566         /* Check permission of requested appid */
3567         char* current_appid = NULL;
3568         char* package_name_temp = NULL;
3569
3570         current_appid = _account_get_current_appid(pid);
3571
3572         package_name_temp = _account_get_text(package_name);
3573
3574         ACCOUNT_DEBUG( "DELETE:user_name[%s],current_appid[%s], package_name[%s]", user_name, current_appid, package_name_temp);
3575
3576         error_code = _account_check_appid_group_with_package_name(current_appid, package_name_temp);
3577
3578         _ACCOUNT_FREE(current_appid);
3579         _ACCOUNT_FREE(package_name_temp);
3580
3581         if(error_code != ACCOUNT_ERROR_NONE){
3582                 ACCOUNT_ERROR("No permission to delete\n");
3583                 return ACCOUNT_ERROR_PERMISSION_DENIED;
3584         }
3585
3586         rc = _account_create(&account);
3587         rc = _account_query_account_by_username_and_package(user_name, package_name, &account);
3588
3589         _INFO("");
3590
3591         if( _account_db_err_code() == SQLITE_PERM )
3592         {
3593                 _account_destroy(account);
3594                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3595                 return ACCOUNT_ERROR_PERMISSION_DENIED;
3596         }
3597
3598         _INFO("");
3599         account_s* account_data = (account_s*)account;
3600
3601         rc = _account_get_account_id(account_data, &account_id);
3602
3603         rc = _account_destroy(account);
3604
3605         /* transaction control required*/
3606         ret_transaction = _account_begin_transaction();
3607
3608         if( _account_db_err_code() == SQLITE_PERM )
3609         {
3610                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3611                 pthread_mutex_unlock(&account_mutex);
3612                 return ACCOUNT_ERROR_PERMISSION_DENIED;
3613         }
3614
3615         _INFO("");
3616         if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY )
3617         {
3618                 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
3619                 pthread_mutex_unlock(&account_mutex);
3620                 return ACCOUNT_ERROR_DATABASE_BUSY;
3621         }
3622         else if (ret_transaction != ACCOUNT_ERROR_NONE) {
3623                 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3624                 pthread_mutex_unlock(&account_mutex);
3625                 return ret_transaction;
3626         }
3627
3628         /* delete custom data */
3629         ACCOUNT_MEMSET(query, 0, sizeof(query));
3630         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = ?", ACCOUNT_CUSTOM_TABLE);
3631
3632         hstmt = _account_prepare_query(query);
3633
3634         if( _account_db_err_code() == SQLITE_PERM ){
3635                 _account_end_transaction(FALSE);
3636                 pthread_mutex_unlock(&account_mutex);
3637                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3638                 return ACCOUNT_ERROR_PERMISSION_DENIED;
3639         }
3640
3641         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3642                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3643
3644         _account_query_bind_int(hstmt, binding_count++, account_id);
3645
3646         rc = _account_query_step(hstmt);
3647         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3648
3649         rc = _account_query_finalize(hstmt);
3650         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3651         hstmt = NULL;
3652
3653         /* delete capability */
3654         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", CAPABILITY_TABLE);
3655
3656         hstmt = _account_prepare_query(query);
3657
3658         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3659                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3660
3661         binding_count = 1;
3662         _account_query_bind_text(hstmt, binding_count++, user_name);
3663         _account_query_bind_text(hstmt, binding_count++, package_name);
3664
3665         rc = _account_query_step(hstmt);
3666         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3667
3668         rc = _account_query_finalize(hstmt);
3669         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3670         hstmt = NULL;
3671
3672         ACCOUNT_MEMSET(query, 0, sizeof(query));
3673
3674         _INFO("");
3675         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
3676
3677         hstmt = _account_prepare_query(query);
3678         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3679                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3680
3681         _INFO("");
3682         binding_count = 1;
3683         _account_query_bind_text(hstmt, binding_count++, user_name);
3684         _account_query_bind_text(hstmt, binding_count++, package_name);
3685
3686         rc = _account_query_step(hstmt);
3687         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));
3688
3689         rc = _account_query_finalize(hstmt);
3690         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3691         is_success = TRUE;
3692
3693         hstmt = NULL;
3694
3695 CATCH:
3696         if (hstmt != NULL) {
3697                 rc = _account_query_finalize(hstmt);
3698                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3699                 hstmt = NULL;
3700         }
3701
3702         ret_transaction = _account_end_transaction(is_success);
3703
3704         if (ret_transaction != ACCOUNT_ERROR_NONE) {
3705                 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3706         } else {
3707                 if (is_success == true) {
3708                         char buf[64]={0,};
3709                         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, account_id);
3710                         _account_insert_delete_update_notification_send(buf);
3711                 }
3712         }
3713
3714         pthread_mutex_unlock(&account_mutex);
3715
3716         return error_code;
3717 }
3718
3719 int _account_delete_from_db_by_package_name(int pid, const char *package_name, gboolean permission)
3720 {
3721         _INFO("_account_delete_from_db_by_package_name");
3722         int                     error_code = ACCOUNT_ERROR_NONE;
3723         account_stmt    hstmt = NULL;
3724         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3725         int                     rc = 0;
3726         int                     ret_transaction = 0;
3727         bool                    is_success = FALSE;
3728         int                     binding_count = 1;
3729         GSList                  *account_id_list = NULL;
3730         int                             ret = -1;
3731
3732         ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
3733         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3734
3735         /* Check permission of requested appid */
3736         if(permission){
3737                 char* current_appid = NULL;
3738                 char* package_name_temp = NULL;
3739
3740                 current_appid = _account_get_current_appid(pid);
3741
3742                 package_name_temp = _account_get_text(package_name);
3743
3744                 ACCOUNT_DEBUG( "DELETE: current_appid[%s], package_name[%s]", current_appid, package_name_temp);
3745
3746                 error_code = _account_check_appid_group_with_package_name(current_appid, package_name_temp);
3747
3748                 _ACCOUNT_FREE(current_appid);
3749                 _ACCOUNT_FREE(package_name_temp);               
3750
3751                 if(error_code != ACCOUNT_ERROR_NONE){
3752                         ACCOUNT_ERROR("No permission to delete\n");
3753                         return ACCOUNT_ERROR_PERMISSION_DENIED;
3754                 }       
3755         }
3756
3757         // It only needs list of ids, does not need to query sensitive info. So sending 0
3758         GList* account_list_temp = _account_query_account_by_package_name(getpid(), package_name, &ret);
3759         if (account_list_temp == NULL)
3760         {
3761                 _ERR("_account_query_account_by_package_name returned NULL");
3762                 return ACCOUNT_ERROR_DB_FAILED;
3763         }
3764
3765         if( _account_db_err_code() == SQLITE_PERM ){
3766                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3767                 return ACCOUNT_ERROR_PERMISSION_DENIED;
3768         }
3769
3770         if(ret != ACCOUNT_ERROR_NONE){
3771                 return ret;
3772         }
3773
3774         account_list_temp = g_list_first(account_list_temp);
3775         _INFO("account_list_temp length=[%d]",g_list_length(account_list_temp));
3776
3777         GList* iter = NULL;
3778         for (iter = account_list_temp; iter != NULL; iter = g_list_next(iter))
3779         {
3780                 _INFO("iterating account_list_temp");
3781                 account_s *account = NULL;
3782                 _INFO("Before iter->data");
3783                 account = (account_s*)iter->data;
3784                 _INFO("After iter->data");
3785                 if (account != NULL)
3786                 {
3787                         char id[256] = {0, };
3788
3789                         ACCOUNT_MEMSET(id, 0, 256);
3790
3791                         ACCOUNT_SNPRINTF(id, 256, "%d", account->id);
3792
3793                         _INFO("Adding account id [%s]", id);
3794                         account_id_list = g_slist_append(account_id_list, g_strdup(id));
3795                 }
3796         }
3797
3798         /* transaction control required*/
3799         ret_transaction = _account_begin_transaction();
3800
3801         if( _account_db_err_code() == SQLITE_PERM ){
3802                 pthread_mutex_unlock(&account_mutex);
3803                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3804                 return ACCOUNT_ERROR_PERMISSION_DENIED;
3805         }
3806
3807         if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
3808                 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
3809                 pthread_mutex_unlock(&account_mutex);
3810                 return ACCOUNT_ERROR_DATABASE_BUSY;
3811         }else if (ret_transaction != ACCOUNT_ERROR_NONE) {
3812                 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3813                 pthread_mutex_unlock(&account_mutex);
3814                 return ret_transaction;
3815         }
3816
3817         /* delete custom table  */
3818         ACCOUNT_MEMSET(query, 0, sizeof(query));
3819         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", ACCOUNT_CUSTOM_TABLE);
3820
3821         hstmt = _account_prepare_query(query);
3822
3823         if( _account_db_err_code() == SQLITE_PERM ){
3824                 _account_end_transaction(FALSE);
3825                 pthread_mutex_unlock(&account_mutex);
3826                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3827                 return ACCOUNT_ERROR_PERMISSION_DENIED;
3828         }
3829
3830         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3831                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3832
3833         binding_count = 1;
3834         _account_query_bind_text(hstmt, binding_count++, package_name);
3835
3836         rc = _account_query_step(hstmt);
3837         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3838
3839         rc = _account_query_finalize(hstmt);
3840         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3841         hstmt = NULL;
3842
3843         /* delete capability table */
3844         ACCOUNT_MEMSET(query, 0, sizeof(query));
3845         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", CAPABILITY_TABLE);
3846
3847         hstmt = _account_prepare_query(query);
3848
3849         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3850                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3851
3852         binding_count = 1;
3853         _account_query_bind_text(hstmt, binding_count++, package_name);
3854
3855         rc = _account_query_step(hstmt);
3856         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3857
3858         rc = _account_query_finalize(hstmt);
3859         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3860         hstmt = NULL;
3861
3862         /* delete account table */
3863         ACCOUNT_MEMSET(query, 0, sizeof(query));
3864
3865         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", ACCOUNT_TABLE);
3866
3867         hstmt = _account_prepare_query(query);
3868         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3869                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3870
3871         binding_count = 1;
3872         _account_query_bind_text(hstmt, binding_count++, package_name);
3873
3874         rc = _account_query_step(hstmt);
3875         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));
3876
3877         rc = _account_query_finalize(hstmt);
3878         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3879         is_success = TRUE;
3880
3881         hstmt = NULL;
3882
3883 CATCH:
3884         if (hstmt != NULL) {
3885                 rc = _account_query_finalize(hstmt);
3886                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3887                 hstmt = NULL;
3888         }
3889
3890         ret_transaction = _account_end_transaction(is_success);
3891
3892         if (ret_transaction != ACCOUNT_ERROR_NONE) {
3893                 ACCOUNT_ERROR("account_delete:_account_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3894         } else {
3895                 if (is_success == true) {
3896                         GSList* gs_iter = NULL;
3897                         for (gs_iter = account_id_list; gs_iter != NULL; gs_iter = g_slist_next(gs_iter)) {
3898                                 char* p_tmpid = NULL;
3899                                 p_tmpid = (char*)gs_iter->data;
3900                                 char buf[64]={0,};
3901                                 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%s", ACCOUNT_NOTI_NAME_DELETE, p_tmpid);
3902                                 ACCOUNT_SLOGD("%s", buf);
3903                                 _account_insert_delete_update_notification_send(buf);
3904                                 _ACCOUNT_FREE(p_tmpid);
3905                         }
3906                         g_slist_free(account_id_list);
3907                 }
3908         }
3909
3910         pthread_mutex_unlock(&account_mutex);
3911
3912         _INFO("_account_delete_from_db_by_package_name end");
3913         return error_code;
3914 }
3915
3916 int _account_get_total_count_from_db(gboolean include_hidden, int *count)
3917 {
3918         if (!count) {
3919                 ACCOUNT_SLOGE("(%s)-(%d) count is NULL.\n", __FUNCTION__, __LINE__);
3920                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3921         }
3922
3923         if(!g_hAccountDB){
3924                 ACCOUNT_ERROR("DB is not opened\n");
3925                 return ACCOUNT_ERROR_DB_NOT_OPENED;
3926         }
3927
3928         char query[1024] = {0, };
3929         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3930
3931         if (include_hidden)
3932         {
3933                 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s", ACCOUNT_TABLE);
3934         }
3935         else
3936         {
3937                 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s where secret = %d", ACCOUNT_TABLE, ACCOUNT_SECRECY_VISIBLE);
3938         }
3939
3940         *count = _account_get_record_count(query);
3941
3942         if( _account_db_err_code() == SQLITE_PERM ){
3943                 pthread_mutex_unlock(&account_mutex);
3944                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3945                 return ACCOUNT_ERROR_PERMISSION_DENIED;
3946         }
3947
3948         int rc = -1;
3949         int ncount = 0;
3950         account_stmt pStmt = NULL;
3951
3952         rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
3953         if (SQLITE_OK != rc) {
3954                 ACCOUNT_SLOGE("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg());
3955                 sqlite3_finalize(pStmt);
3956                 return ACCOUNT_ERROR_DB_FAILED;
3957         }
3958
3959         rc = sqlite3_step(pStmt);
3960         if (SQLITE_ROW != rc) {
3961                 ACCOUNT_ERROR("[ERROR] sqlite3_step() failed\n");
3962                 sqlite3_finalize(pStmt);
3963                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3964         }
3965
3966         ncount = sqlite3_column_int(pStmt, 0);
3967
3968         *count = ncount;
3969
3970         sqlite3_finalize(pStmt);
3971
3972         if (ncount < 0) {
3973                 ACCOUNT_ERROR("[ERROR] Number of account : %d, End", ncount);
3974                 return ACCOUNT_ERROR_DB_FAILED;
3975         }
3976
3977         return ACCOUNT_ERROR_NONE;
3978 }
3979
3980 int account_type_create(account_type_h *account_type)
3981 {
3982         if (!account_type) {
3983                 ACCOUNT_SLOGE("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
3984                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3985         }
3986
3987         account_type_s *data = (account_type_s*)malloc(sizeof(account_type_s));
3988
3989         if (data == NULL) {
3990                 ACCOUNT_ERROR("Memory Allocation Failed");
3991                 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3992         }
3993
3994         ACCOUNT_MEMSET(data, 0, sizeof(account_type_s));
3995
3996         *account_type = (account_type_h)data;
3997
3998         return ACCOUNT_ERROR_NONE;
3999 }
4000
4001 int account_type_destroy(account_type_h account_type)
4002 {
4003         account_type_s *data = (account_type_s*)account_type;
4004
4005         ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account type handle is null!"));
4006
4007         _account_type_free_account_type_items(data);
4008         _ACCOUNT_FREE(data);
4009
4010         return ACCOUNT_ERROR_NONE;
4011 }
4012
4013 //app_id mandatory field
4014 int account_type_set_app_id(account_type_h account_type, const char *app_id)
4015 {
4016         if (!account_type) {
4017                 ACCOUNT_SLOGE("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
4018                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4019         }
4020
4021         if (!app_id) {
4022                 ACCOUNT_SLOGE("(%s)-(%d) app_id is NULL.\n", __FUNCTION__, __LINE__);
4023                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4024         }
4025
4026         account_type_s *data = (account_type_s*)account_type;
4027
4028         _ACCOUNT_FREE(data->app_id);
4029         data->app_id = _account_get_text(app_id);
4030
4031         return ACCOUNT_ERROR_NONE;
4032 }
4033
4034 //service_provider_id mandatory field
4035 int account_type_set_service_provider_id(account_type_h account_type, const char *service_provider_id)
4036 {
4037         if (!account_type) {
4038                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4039         }
4040
4041         if (!service_provider_id) {
4042                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4043         }
4044
4045         account_type_s *data = (account_type_s*)account_type;
4046
4047         _ACCOUNT_FREE(data->service_provider_id);
4048         data->service_provider_id = _account_get_text(service_provider_id);
4049
4050         return ACCOUNT_ERROR_NONE;
4051 }
4052
4053 int account_type_set_icon_path(account_type_h account_type, const char *icon_path)
4054 {
4055         if (!account_type) {
4056                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4057         }
4058
4059         if (!icon_path) {
4060                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4061         }
4062
4063         account_type_s *data = (account_type_s*)account_type;
4064
4065         _ACCOUNT_FREE(data->icon_path);
4066         data->icon_path = _account_get_text(icon_path);
4067
4068         return ACCOUNT_ERROR_NONE;
4069 }
4070
4071 int account_type_set_small_icon_path(account_type_h account_type, const char *small_icon_path)
4072 {
4073         if (!account_type) {
4074                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4075         }
4076
4077         if (!small_icon_path) {
4078                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4079         }
4080
4081         account_type_s *data = (account_type_s*)account_type;
4082
4083         _ACCOUNT_FREE(data->small_icon_path);
4084         data->small_icon_path = _account_get_text(small_icon_path);
4085
4086         return ACCOUNT_ERROR_NONE;
4087 }
4088
4089 int account_type_set_multiple_account_support(account_type_h account_type, const bool multiple_account_support)
4090 {
4091         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n",  __FUNCTION__, __LINE__));
4092
4093         account_type_s *data = (account_type_s*)account_type;
4094
4095         data->multiple_account_support = multiple_account_support;
4096
4097         return ACCOUNT_ERROR_NONE;
4098 }
4099
4100 // unset?
4101 int account_type_set_label(account_type_h account_type, const char* label, const char* locale)
4102 {
4103         if (!account_type) {
4104                 ACCOUNT_SLOGE("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
4105                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4106         }
4107
4108         if(!label || !locale) {
4109                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4110         }
4111
4112         account_type_s *data = (account_type_s*)account_type;
4113         label_s *label_data = (label_s*)malloc(sizeof(label_s));
4114
4115         if (label_data == NULL) {
4116                 return ACCOUNT_ERROR_OUT_OF_MEMORY;
4117         }
4118         ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
4119
4120         label_data->label = _account_get_text(label);
4121         label_data->locale = _account_get_text(locale);
4122
4123         data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
4124
4125         return ACCOUNT_ERROR_NONE;
4126 }
4127
4128 int account_type_get_app_id(account_type_h account_type, char **app_id)
4129 {
4130         if (!account_type) {
4131                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4132         }
4133
4134         if (!app_id) {
4135                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4136         }
4137
4138         account_type_s *data = (account_type_s*)account_type;
4139
4140         (*app_id) = NULL;
4141         *app_id = _account_get_text(data->app_id);
4142
4143         return ACCOUNT_ERROR_NONE;
4144 }
4145
4146 int account_type_get_service_provider_id(account_type_h account_type, char **service_provider_id)
4147 {
4148         if (!account_type) {
4149                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4150         }
4151
4152         if (!service_provider_id) {
4153                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4154         }
4155
4156         account_type_s *data = (account_type_s*)account_type;
4157
4158         (*service_provider_id) = NULL;
4159         *service_provider_id = _account_get_text(data->service_provider_id);
4160
4161         return ACCOUNT_ERROR_NONE;
4162 }
4163
4164 int account_type_get_icon_path(account_type_h account_type, char **icon_path)
4165 {
4166         if (!account_type) {
4167                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4168         }
4169
4170         if (!icon_path) {
4171                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4172         }
4173
4174         account_type_s *data = (account_type_s*)account_type;
4175
4176         (*icon_path) = NULL;
4177         *icon_path = _account_get_text(data->icon_path);
4178
4179         return ACCOUNT_ERROR_NONE;
4180 }
4181
4182 int account_type_get_small_icon_path(account_type_h account_type, char **small_icon_path)
4183 {
4184         if (!account_type) {
4185                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4186         }
4187
4188         if (!small_icon_path) {
4189                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4190         }
4191
4192         account_type_s *data = (account_type_s*)account_type;
4193
4194         (*small_icon_path) = NULL;
4195         *small_icon_path = _account_get_text(data->small_icon_path);
4196
4197         return ACCOUNT_ERROR_NONE;
4198 }
4199
4200 int account_type_get_multiple_account_support(account_type_h account_type, int *multiple_account_support)
4201 {
4202         if (!account_type) {
4203                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4204         }
4205         if (!multiple_account_support) {
4206                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4207         }
4208
4209         account_type_s *data = (account_type_s*)account_type;
4210
4211         *multiple_account_support = data->multiple_account_support;
4212
4213         return ACCOUNT_ERROR_NONE;
4214 }
4215
4216 int account_type_get_label_by_locale(account_type_h account_type, const char* locale, char** label)
4217 {
4218         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4219         ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID PARAMETER"));
4220
4221         GSList *iter;
4222         account_type_s *data = (account_type_s*)account_type;
4223
4224         for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) {
4225                 label_s *label_data = NULL;
4226
4227                 label_data = (label_s*)iter->data;
4228
4229                 *label = NULL;
4230
4231                 if(!strcmp(locale, label_data->locale)) {
4232                         *label = _account_get_text(label_data->label);
4233                         return ACCOUNT_ERROR_NONE;
4234                 }
4235         }
4236
4237         return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4238 }
4239
4240 int account_type_get_label(account_type_h account_type, account_label_cb callback, void *user_data)
4241 {
4242         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4243         ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4244
4245         GSList *iter;
4246         account_type_s *data = (account_type_s*)account_type;
4247
4248         for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) {
4249                 label_s *label_data = NULL;
4250
4251                 label_data = (label_s*)iter->data;
4252
4253                 if(callback(label_data->app_id, label_data->label, label_data->locale, user_data)!=TRUE) {
4254                         ACCOUNT_DEBUG("Callback func returs FALSE, its iteration is stopped!!!!\n");
4255                         return ACCOUNT_ERROR_NONE;
4256                 }
4257         }
4258
4259         return ACCOUNT_ERROR_NONE;
4260 }
4261
4262 static gboolean _account_type_check_duplicated(account_type_s *data)
4263 {
4264         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4265         int count = 0;
4266
4267         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4268
4269         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId='%s'"
4270                         , ACCOUNT_TYPE_TABLE, data->app_id);
4271
4272         count = _account_get_record_count(query);
4273         if (count > 0) {
4274                 return TRUE;
4275         }
4276
4277         return FALSE;
4278 }
4279
4280 static int _account_type_convert_account_to_sql(account_type_s *account_type, account_stmt hstmt, char *sql_value)
4281 {
4282         _INFO("");
4283
4284         int count = 1;
4285
4286         /*Caution : Keep insert query orders.*/
4287
4288         /* 1. app id*/
4289         _account_query_bind_text(hstmt, count++, (char*)account_type->app_id);
4290
4291         /* 2. service provider id*/
4292         _account_query_bind_text(hstmt, count++, (char*)account_type->service_provider_id);
4293
4294         /* 3. icon path*/
4295         _account_query_bind_text(hstmt, count++, (char*)account_type->icon_path);
4296
4297         /* 4. small icon path*/
4298         _account_query_bind_text(hstmt, count++, (char*)account_type->small_icon_path);
4299
4300         /* 5. multiple accont support*/
4301         _account_query_bind_int(hstmt, count++, account_type->multiple_account_support);
4302
4303         _INFO("");
4304
4305         return count;
4306 }
4307
4308
4309 static int _account_type_execute_insert_query(account_type_s *account_type)
4310 {
4311         _INFO("");
4312
4313         int                             rc = 0;
4314         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4315         int                             error_code = ACCOUNT_ERROR_NONE;
4316         account_stmt    hstmt = NULL;
4317
4318         /* check mandatory field */
4319         // app id & service provider id
4320         if (!account_type->app_id) {
4321                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4322         }
4323
4324         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4325         ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s( AppId, ServiceProviderId , IconPath , SmallIconPath , MultipleAccountSupport ) values "
4326                         "(?, ?, ?, ?, ?)",      ACCOUNT_TYPE_TABLE);
4327
4328         _INFO("");
4329         hstmt = _account_prepare_query(query);
4330         _INFO("");
4331
4332         if( _account_db_err_code() == SQLITE_PERM ){
4333                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4334                 return ACCOUNT_ERROR_PERMISSION_DENIED;
4335         } else if( _account_db_err_code() == SQLITE_BUSY ){
4336                 ACCOUNT_ERROR( "Database Busy(%s)", _account_db_err_msg());
4337                 return ACCOUNT_ERROR_DATABASE_BUSY;
4338         }
4339
4340         ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4341
4342         _INFO("");
4343         _account_type_convert_account_to_sql(account_type, hstmt, query);
4344         _INFO("");
4345
4346         rc = _account_query_step(hstmt);
4347         if (rc == SQLITE_BUSY) {
4348                 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4349                 error_code = ACCOUNT_ERROR_DATABASE_BUSY;
4350         } else if (rc != SQLITE_DONE) {
4351                 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4352                 error_code = ACCOUNT_ERROR_DB_FAILED;
4353         }
4354
4355         _INFO("");
4356         rc = _account_query_finalize(hstmt);
4357         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4358         hstmt = NULL;
4359
4360         _INFO("");
4361         return error_code;
4362 }
4363
4364 static int _account_type_insert_label(account_type_s *account_type)
4365 {
4366         int                     rc, count = 1;
4367         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4368         account_stmt    hstmt = NULL;
4369
4370         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4371
4372         if (g_slist_length( account_type->label_list)==0) {
4373                 ACCOUNT_ERROR( "_account_type_insert_label, no label\n");
4374                 return ACCOUNT_ERROR_NONE;
4375         }
4376
4377         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId = '%s'", ACCOUNT_TYPE_TABLE, account_type->app_id);
4378
4379         rc = _account_get_record_count(query);
4380
4381         if( _account_db_err_code() == SQLITE_PERM ){
4382                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4383                 return ACCOUNT_ERROR_PERMISSION_DENIED;
4384         }
4385
4386         if (rc <= 0) {
4387                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4388         }
4389
4390         /* insert query*/
4391         GSList *iter;
4392
4393         for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
4394                 int ret;
4395                 count = 1;
4396                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4397                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
4398                                 "(?, ?, ?) ", LABEL_TABLE);
4399
4400                 hstmt = _account_prepare_query(query);
4401
4402                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4403
4404                 label_s* label_data = NULL;
4405                 label_data = (label_s*)iter->data;
4406
4407                 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4408                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4409                 ret = _account_query_bind_text(hstmt, count++, label_data->label);
4410                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4411                 ret = _account_query_bind_text(hstmt, count++, (char*)label_data->locale);
4412                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4413
4414                 rc = _account_query_step(hstmt);
4415
4416                 if (rc != SQLITE_DONE) {
4417                         ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4418                         break;
4419                 }
4420
4421                 rc = _account_query_finalize(hstmt);
4422                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4423                 hstmt = NULL;
4424
4425         }
4426
4427         return ACCOUNT_ERROR_NONE;
4428 }
4429
4430 static void _account_type_convert_column_to_provider_feature(account_stmt hstmt, provider_feature_s *feature_record)
4431 {
4432         const char *textbuf = NULL;
4433
4434         textbuf = _account_query_table_column_text(hstmt, PROVIDER_FEATURE_FIELD_APP_ID);
4435         _account_db_data_to_text(textbuf, &(feature_record->app_id));
4436
4437         textbuf = _account_query_table_column_text(hstmt, PROVIDER_FEATURE_FIELD_KEY);
4438         _account_db_data_to_text(textbuf, &(feature_record->key));
4439
4440 }
4441
4442 GSList* _account_type_query_provider_feature_by_app_id(const char* app_id, int *error_code)
4443 {
4444         _INFO("_account_type_query_provider_feature_by_app_id");
4445         *error_code = ACCOUNT_ERROR_NONE;
4446         account_stmt    hstmt = NULL;
4447         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4448         int                     rc = 0, binding_count = 1;
4449         GSList* feature_list = NULL;
4450
4451         ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
4452         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
4453
4454         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4455
4456         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
4457         _INFO("account query=[%s]", query);
4458
4459         hstmt = _account_prepare_query(query);
4460
4461         if( _account_db_err_code() == SQLITE_PERM ){
4462                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4463                 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
4464                 return NULL;
4465         }
4466
4467         _account_query_bind_text(hstmt, binding_count++, app_id);
4468
4469         rc = _account_query_step(hstmt);
4470         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {*error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;}, NULL, ("The record isn't found.\n"));
4471
4472         provider_feature_s* feature_record = NULL;
4473
4474         while (rc == SQLITE_ROW) {
4475
4476                 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
4477
4478                 if (feature_record == NULL) {
4479                         ACCOUNT_FATAL("malloc Failed");
4480                         break;
4481                 }
4482
4483                 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
4484
4485                 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
4486
4487                 _INFO("Adding account feature_list");
4488                 feature_list = g_slist_append(feature_list, feature_record);
4489
4490                 rc = _account_query_step(hstmt);
4491         }
4492
4493         rc = _account_query_finalize(hstmt);
4494         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("account finalize error"));
4495         hstmt = NULL;
4496
4497         *error_code = ACCOUNT_ERROR_NONE;
4498
4499 CATCH:
4500         if (hstmt != NULL) {
4501                 rc = _account_query_finalize(hstmt);
4502                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("account finalize error"));
4503                 hstmt = NULL;
4504         }
4505
4506         pthread_mutex_unlock(&account_mutex);
4507
4508         _INFO("Returning account feature_list");
4509         return feature_list;
4510 }
4511
4512 int account_type_query_provider_feature_by_app_id(provider_feature_cb callback, const char* app_id, void *user_data )
4513 {
4514         int                     error_code = ACCOUNT_ERROR_NONE;
4515         account_stmt    hstmt = NULL;
4516         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4517         int                     rc = 0, binding_count = 1;
4518
4519         ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4520         ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4521         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4522
4523         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4524
4525         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
4526         hstmt = _account_prepare_query(query);
4527
4528         if( _account_db_err_code() == SQLITE_PERM ){
4529                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4530                 return ACCOUNT_ERROR_PERMISSION_DENIED;
4531         }
4532
4533         _account_query_bind_text(hstmt, binding_count++, app_id);
4534
4535         rc = _account_query_step(hstmt);
4536         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4537
4538         provider_feature_s* feature_record = NULL;
4539
4540         while (rc == SQLITE_ROW) {
4541                 bool cb_ret = FALSE;
4542                 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
4543
4544                 if (feature_record == NULL) {
4545                         ACCOUNT_FATAL("malloc Failed");
4546                         break;
4547                 }
4548
4549                 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
4550
4551                 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
4552
4553                 cb_ret = callback(feature_record->app_id, feature_record->key, user_data);
4554
4555                 _account_type_free_feature_items(feature_record);
4556                 _ACCOUNT_FREE(feature_record);
4557
4558                 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
4559
4560                 rc = _account_query_step(hstmt);
4561         }
4562
4563         rc = _account_query_finalize(hstmt);
4564         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4565         hstmt = NULL;
4566
4567         error_code = ACCOUNT_ERROR_NONE;
4568
4569 CATCH:
4570         if (hstmt != NULL) {
4571                 rc = _account_query_finalize(hstmt);
4572                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4573                 hstmt = NULL;
4574         }
4575
4576         pthread_mutex_unlock(&account_mutex);
4577         return error_code;
4578 }
4579
4580 bool _account_type_query_supported_feature(const char* app_id, const char* capability, int *error_code)
4581 {
4582         _INFO("_account_type_query_supported_feature start");
4583
4584         *error_code = ACCOUNT_ERROR_NONE;
4585
4586         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4587         int                     record_count = 0;
4588
4589         if (app_id == NULL || capability == NULL)
4590         {
4591                 *error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
4592                 return false;
4593         }
4594
4595         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
4596
4597         record_count = _account_get_record_count(query);
4598
4599         if( _account_db_err_code() == SQLITE_PERM ){
4600                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4601                 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
4602                 return false;
4603         }
4604
4605         if (record_count <= 0)
4606         {
4607                 *error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
4608                 return false;
4609         }
4610
4611         _INFO("_account_type_query_supported_feature end");
4612         return true;
4613
4614 }
4615
4616
4617 int account_type_get_provider_feature_all(account_type_h account_type, provider_feature_cb callback, void* user_data)
4618 {
4619         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4620         ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4621
4622         GSList *iter;
4623         account_type_s *data = (account_type_s*)account_type;
4624
4625         for (iter = data->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4626                 provider_feature_s *feature_data = NULL;
4627
4628                 feature_data = (provider_feature_s*)iter->data;
4629
4630                 if(callback(feature_data->app_id, feature_data->key, user_data)!=TRUE) {
4631                         ACCOUNT_DEBUG("Callback func returs FALSE, its iteration is stopped!!!!\n");
4632                         return ACCOUNT_ERROR_NONE;
4633                 }
4634         }
4635
4636         return ACCOUNT_ERROR_NONE;
4637 }
4638
4639 int account_type_set_provider_feature(account_type_h account_type, const char* provider_feature)
4640 {
4641         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("account type handle is null"));
4642         ACCOUNT_RETURN_VAL((provider_feature != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("provider_feature is null"));
4643
4644         account_type_s *data = (account_type_s*)account_type;
4645
4646         GSList *iter = NULL;
4647         bool b_is_new = TRUE;
4648
4649         for(iter = data->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4650                 provider_feature_s *feature_data = NULL;
4651                 feature_data = (provider_feature_s*)iter->data;
4652
4653                 if(!strcmp(feature_data->key, provider_feature)) {
4654                         b_is_new = FALSE;
4655                         break;
4656                 }
4657         }
4658
4659         if(b_is_new) {
4660                 provider_feature_s* feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
4661
4662                 if (feature_data == NULL)
4663                         return ACCOUNT_ERROR_OUT_OF_MEMORY;
4664                 ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
4665
4666                 feature_data->key = _account_get_text(provider_feature);
4667                 data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
4668         }
4669
4670         return ACCOUNT_ERROR_NONE;
4671 }
4672
4673 static int _account_type_insert_provider_feature(account_type_s *account_type, const char* app_id)
4674 {
4675         int                     rc, count = 1;
4676         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4677         account_stmt    hstmt = NULL;
4678
4679         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4680         ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4681
4682         if (g_slist_length( account_type->provider_feature_list)==0) {
4683                 ACCOUNT_ERROR( "no capability\n");
4684                 return ACCOUNT_ERROR_NONE;
4685         }
4686
4687         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId='%s'", ACCOUNT_TYPE_TABLE, app_id);
4688
4689         rc = _account_get_record_count(query);
4690
4691         if( _account_db_err_code() == SQLITE_PERM ){
4692                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4693                 return ACCOUNT_ERROR_PERMISSION_DENIED;
4694         }
4695
4696         if (rc <= 0) {
4697                 ACCOUNT_SLOGI( "related account type item is not existed rc=%d , %s", rc, _account_db_err_msg());
4698                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4699         }
4700
4701         /* insert query*/
4702
4703         GSList *iter;
4704
4705         for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4706                 int ret;
4707                 count = 1;
4708                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4709                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
4710                                 "(?, ?) ", PROVIDER_FEATURE_TABLE);
4711
4712                 hstmt = _account_prepare_query(query);
4713
4714                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4715
4716                 provider_feature_s* feature_data = NULL;
4717                 feature_data = (provider_feature_s*)iter->data;
4718
4719                 ret = _account_query_bind_text(hstmt, count++, app_id);
4720                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4721                 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
4722                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
4723
4724                 rc = _account_query_step(hstmt);
4725
4726                 if (rc != SQLITE_DONE) {
4727                         ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4728                         break;
4729                 }
4730
4731                 rc = _account_query_finalize(hstmt);
4732                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4733                 hstmt = NULL;
4734
4735         }
4736
4737         return ACCOUNT_ERROR_NONE;
4738 }
4739
4740 int _account_type_insert_to_db(account_type_s* account_type, int* account_type_id)
4741 {
4742         _INFO("");
4743
4744         int             error_code = ACCOUNT_ERROR_NONE, ret_transaction = 0;
4745
4746         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4747         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE HANDLE IS NULL"));
4748         ACCOUNT_RETURN_VAL((account_type_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE ID POINTER IS NULL"));
4749
4750         account_type_s *data = (account_type_s*)account_type;
4751
4752         pthread_mutex_lock(&account_mutex);
4753
4754
4755         /* transaction control required*/
4756         ret_transaction = _account_begin_transaction();
4757
4758         _INFO("");
4759
4760         if( _account_db_err_code() == SQLITE_PERM ){
4761                 pthread_mutex_unlock(&account_mutex);
4762                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4763                 return ACCOUNT_ERROR_PERMISSION_DENIED;
4764         }
4765
4766         _INFO("");
4767         if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
4768                 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
4769                 pthread_mutex_unlock(&account_mutex);
4770                 return ACCOUNT_ERROR_DATABASE_BUSY;
4771         } else if (ret_transaction != ACCOUNT_ERROR_NONE) {
4772                 ACCOUNT_ERROR("_account_begin_transaction fail %d\n", ret_transaction);
4773                 pthread_mutex_unlock(&account_mutex);
4774                 return ret_transaction;
4775         }
4776
4777         _INFO("");
4778         if (_account_type_check_duplicated(data)) {
4779                 _INFO("");
4780                 ret_transaction = _account_end_transaction(FALSE);
4781                 ACCOUNT_ERROR("Duplicated, rollback insert query(%x)!!!!\n", ret_transaction);
4782                 *account_type_id = -1;
4783                 pthread_mutex_unlock(&account_mutex);
4784                 return ACCOUNT_ERROR_DUPLICATED;
4785         } else {
4786                 _INFO("");
4787                 *account_type_id = _account_get_next_sequence(ACCOUNT_TYPE_TABLE);
4788
4789                 error_code = _account_type_execute_insert_query(data);
4790
4791                 if (error_code != ACCOUNT_ERROR_NONE){
4792                         error_code = ACCOUNT_ERROR_DUPLICATED;
4793                         ret_transaction = _account_end_transaction(FALSE);
4794                         ACCOUNT_ERROR("Insert fail, rollback insert query(%x)!!!!\n", ret_transaction);
4795                         *account_type_id = -1;
4796                         pthread_mutex_unlock(&account_mutex);
4797                         return error_code;
4798                 }
4799         }
4800
4801         _INFO("");
4802         error_code = _account_type_insert_provider_feature(data, data->app_id);
4803         if(error_code != ACCOUNT_ERROR_NONE) {
4804                 _INFO("");
4805                 ret_transaction = _account_end_transaction(FALSE);
4806                 ACCOUNT_ERROR("Insert provider feature fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction);
4807                 pthread_mutex_unlock(&account_mutex);
4808                 return error_code;
4809         }
4810         _INFO("");
4811         error_code = _account_type_insert_label(data);
4812         if(error_code != ACCOUNT_ERROR_NONE) {
4813                 _INFO("");
4814                 ret_transaction = _account_end_transaction(FALSE);
4815                 ACCOUNT_ERROR("Insert label fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction);
4816                 pthread_mutex_unlock(&account_mutex);
4817                 return error_code;
4818         }
4819
4820         ret_transaction = _account_end_transaction(TRUE);
4821         _INFO("");
4822         pthread_mutex_unlock(&account_mutex);
4823
4824         _INFO("");
4825         return ACCOUNT_ERROR_NONE;
4826 }
4827
4828 static int _account_type_update_provider_feature(account_type_s *account_type, const char* app_id)
4829 {
4830         int                     rc, count = 1;
4831         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4832         account_stmt    hstmt = NULL;
4833
4834         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4835
4836         if (g_slist_length( account_type->provider_feature_list)==0) {
4837                 ACCOUNT_ERROR( "no feature\n");
4838                 return ACCOUNT_ERROR_NONE;
4839         }
4840
4841         ACCOUNT_DEBUG( "app id", app_id);
4842
4843         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4844
4845         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id=? ", PROVIDER_FEATURE_TABLE);
4846         hstmt = _account_prepare_query(query);
4847
4848         if( _account_db_err_code() == SQLITE_PERM ){
4849                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4850                 return ACCOUNT_ERROR_PERMISSION_DENIED;
4851         }
4852
4853         count = 1;
4854         _account_query_bind_text(hstmt, count++, app_id);
4855         rc = _account_query_step(hstmt);
4856
4857         if (rc != SQLITE_DONE) {
4858                 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4859                 return ACCOUNT_ERROR_DB_FAILED;
4860         }
4861         rc = _account_query_finalize(hstmt);
4862         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4863         hstmt = NULL;
4864
4865         GSList *iter;
4866
4867         for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4868                 int ret;
4869                 count = 1;
4870                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4871                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
4872                                 "(?, ?) ", PROVIDER_FEATURE_TABLE);
4873
4874                 hstmt = _account_prepare_query(query);
4875
4876                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4877
4878                 provider_feature_s* feature_data = NULL;
4879                 feature_data = (provider_feature_s*)iter->data;
4880
4881                 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4882                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4883                 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
4884                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4885
4886                 rc = _account_query_step(hstmt);
4887
4888                 if (rc != SQLITE_DONE) {
4889                         ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4890                         break;
4891                 }
4892                 rc = _account_query_finalize(hstmt);
4893                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4894                 hstmt = NULL;
4895         }
4896
4897         return ACCOUNT_ERROR_NONE;
4898 }
4899
4900 static int _account_type_update_label(account_type_s *account_type, const char* app_id)
4901 {
4902         int                     rc, count = 1;
4903         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4904         account_stmt    hstmt = NULL;
4905
4906         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4907
4908         if (g_slist_length( account_type->label_list)==0) {
4909                 return ACCOUNT_ERROR_NONE;
4910         }
4911
4912         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4913
4914         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId=? ", LABEL_TABLE);
4915         hstmt = _account_prepare_query(query);
4916
4917         if( _account_db_err_code() == SQLITE_PERM ){
4918                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4919                 return ACCOUNT_ERROR_PERMISSION_DENIED;
4920         }
4921
4922         count = 1;
4923         _account_query_bind_text(hstmt, count++, app_id);
4924         rc = _account_query_step(hstmt);
4925
4926         if (rc != SQLITE_DONE) {
4927                 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4928                 return ACCOUNT_ERROR_DB_FAILED;
4929         }
4930         rc = _account_query_finalize(hstmt);
4931         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4932         hstmt = NULL;
4933
4934         GSList *iter;
4935
4936         for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
4937                 int ret;
4938                 count = 1;
4939                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4940                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
4941                                 "(?, ?, ?) ", LABEL_TABLE);
4942
4943                 hstmt = _account_prepare_query(query);
4944
4945                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4946
4947                 label_s* label_data = NULL;
4948                 label_data = (label_s*)iter->data;
4949
4950                 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4951                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4952                 ret = _account_query_bind_text(hstmt, count++, label_data->label);
4953                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4954                 ret = _account_query_bind_text(hstmt, count++, label_data->locale);
4955                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4956
4957                 rc = _account_query_step(hstmt);
4958
4959                 if (rc != SQLITE_DONE) {
4960                         ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4961                         break;
4962                 }
4963                 rc = _account_query_finalize(hstmt);
4964                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4965                 hstmt = NULL;
4966         }
4967
4968         return ACCOUNT_ERROR_NONE;
4969 }
4970
4971
4972 static int _account_type_update_account(account_type_s *account_type, const char* app_id)
4973 {
4974         int                             rc = 0, binding_count =1;
4975         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4976         int                             error_code = ACCOUNT_ERROR_NONE;
4977         account_stmt    hstmt = NULL;
4978
4979         if (!account_type->app_id) {
4980                 ACCOUNT_ERROR("app id is mandetory field, it can not be NULL!!!!\n");
4981                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4982         }
4983
4984         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4985         ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET AppId=?, ServiceProviderId=?, IconPath=?, "
4986                         "SmallIconPath=?, MultipleAccountSupport=? WHERE AppId=? ", ACCOUNT_TYPE_TABLE);
4987
4988         hstmt = _account_prepare_query(query);
4989
4990         if( _account_db_err_code() == SQLITE_PERM ){
4991                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4992                 return ACCOUNT_ERROR_PERMISSION_DENIED;
4993         } else if (_account_db_err_code() == SQLITE_BUSY){
4994                 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
4995                 return ACCOUNT_ERROR_DATABASE_BUSY;
4996         }
4997
4998         ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg()));
4999
5000         binding_count = _account_type_convert_account_to_sql(account_type, hstmt, query);
5001         _account_query_bind_text(hstmt, binding_count++, app_id);
5002
5003         rc = _account_query_step(hstmt);
5004         if (rc != SQLITE_DONE) {
5005                 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5006         }
5007
5008         rc = _account_query_finalize(hstmt);
5009         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5010         hstmt = NULL;
5011
5012         /*update label*/
5013         error_code = _account_type_update_label(account_type, app_id);
5014         /* update provider feature */
5015         error_code = _account_type_update_provider_feature(account_type, app_id);
5016
5017         return error_code;
5018 }
5019
5020 int _account_type_update_to_db_by_app_id(account_type_s* account_type, const char* app_id)
5021 {
5022         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
5023         ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5024         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5025
5026         int     error_code = ACCOUNT_ERROR_NONE;
5027         account_type_s* data = account_type;
5028
5029         pthread_mutex_lock(&account_mutex);
5030
5031         error_code = _account_type_update_account(data, app_id);
5032
5033         pthread_mutex_unlock(&account_mutex);
5034
5035         return error_code;
5036 }
5037
5038 int _account_type_delete_by_app_id(const char* app_id)
5039 {
5040         int                     error_code = ACCOUNT_ERROR_NONE;
5041         account_stmt    hstmt = NULL;
5042         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
5043         int                     rc = 0, count = -1;
5044         int                     ret_transaction = 0;
5045         int                             binding_count = 1;
5046         bool                    is_success = FALSE;
5047
5048         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5049         ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("The database isn't connected."));
5050
5051         /* Check requested ID to delete */
5052         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
5053
5054         count = _account_get_record_count(query);
5055
5056         if( _account_db_err_code() == SQLITE_PERM ){
5057                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5058                 return ACCOUNT_ERROR_PERMISSION_DENIED;
5059         }
5060
5061         if (count <= 0) {
5062                 ACCOUNT_SLOGE("app id(%s) is not exist. count(%d)\n", app_id, count);
5063                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5064         }
5065
5066         /* transaction control required*/
5067         ret_transaction = _account_begin_transaction();
5068
5069         if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
5070                 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
5071                 pthread_mutex_unlock(&account_mutex);
5072                 return ACCOUNT_ERROR_DATABASE_BUSY;
5073         }else if (ret_transaction != ACCOUNT_ERROR_NONE) {
5074                 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
5075                 pthread_mutex_unlock(&account_mutex);
5076                 return ret_transaction;
5077         }
5078
5079         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", LABEL_TABLE);
5080
5081         hstmt = _account_prepare_query(query);
5082
5083         if( _account_db_err_code() == SQLITE_PERM ){
5084                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5085                 pthread_mutex_unlock(&account_mutex);
5086                 return ACCOUNT_ERROR_PERMISSION_DENIED;
5087         }
5088
5089         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
5090                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
5091
5092         _account_query_bind_text(hstmt, binding_count++, app_id);
5093
5094         rc = _account_query_step(hstmt);
5095         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5096
5097         rc = _account_query_finalize(hstmt);
5098         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5099         hstmt = NULL;
5100
5101         binding_count = 1;
5102         ACCOUNT_MEMSET(query, 0, sizeof(query));
5103
5104         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id = ? ", PROVIDER_FEATURE_TABLE);
5105
5106         hstmt = _account_prepare_query(query);
5107         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
5108                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
5109
5110         _account_query_bind_text(hstmt, binding_count++, app_id);
5111
5112         rc = _account_query_step(hstmt);
5113         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
5114
5115         rc = _account_query_finalize(hstmt);
5116         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5117         is_success = TRUE;
5118
5119         hstmt = NULL;
5120
5121         binding_count = 1;
5122         ACCOUNT_MEMSET(query, 0, sizeof(query));
5123
5124         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ? ", ACCOUNT_TYPE_TABLE);
5125
5126         hstmt = _account_prepare_query(query);
5127         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
5128                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
5129
5130         _account_query_bind_text(hstmt, binding_count++, app_id);
5131
5132         rc = _account_query_step(hstmt);
5133         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
5134
5135         rc = _account_query_finalize(hstmt);
5136         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5137         is_success = TRUE;
5138
5139         hstmt = NULL;
5140
5141         CATCH:
5142         if (hstmt != NULL) {
5143                 rc = _account_query_finalize(hstmt);
5144                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5145                 hstmt = NULL;
5146         }
5147
5148         ret_transaction = _account_end_transaction(is_success);
5149
5150         if (ret_transaction != ACCOUNT_ERROR_NONE) {
5151                 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
5152         }
5153
5154         pthread_mutex_unlock(&account_mutex);
5155
5156         return error_code;
5157 }
5158
5159 static void _account_type_convert_column_to_account_type(account_stmt hstmt, account_type_s *account_type_record)
5160 {
5161         const char *textbuf = NULL;
5162
5163         account_type_record->id = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_ID);
5164
5165         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_APP_ID);
5166         _account_db_data_to_text(textbuf, &(account_type_record->app_id));
5167
5168         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SERVICE_PROVIDER_ID);
5169         _account_db_data_to_text(textbuf, &(account_type_record->service_provider_id));
5170
5171         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_ICON_PATH);
5172         _account_db_data_to_text(textbuf, &(account_type_record->icon_path));
5173
5174         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SMALL_ICON_PATH);
5175         _account_db_data_to_text(textbuf, &(account_type_record->small_icon_path));
5176
5177         account_type_record->multiple_account_support = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_MULTIPLE_ACCOUNT_SUPPORT);
5178
5179 }
5180
5181 static void _account_type_convert_column_to_label(account_stmt hstmt, label_s *label_record)
5182 {
5183         const char *textbuf = NULL;
5184
5185         textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_APP_ID);
5186         _account_db_data_to_text(textbuf, &(label_record->app_id));
5187
5188         textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LABEL);
5189         _account_db_data_to_text(textbuf, &(label_record->label));
5190
5191         textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LOCALE);
5192         _account_db_data_to_text(textbuf, &(label_record->locale));
5193
5194 }
5195
5196 GSList* _account_type_get_label_list_by_app_id(const char* app_id, int *error_code )
5197 {
5198         *error_code = ACCOUNT_ERROR_NONE;
5199         account_stmt    hstmt = NULL;
5200         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
5201         int                     rc = 0, binding_count = 1;
5202         GSList* label_list = NULL;
5203
5204         ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
5205         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
5206
5207         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5208
5209         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
5210         hstmt = _account_prepare_query(query);
5211
5212         if( _account_db_err_code() == SQLITE_PERM ){
5213                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5214                 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
5215                 return NULL;
5216         }
5217
5218         _account_query_bind_text(hstmt, binding_count++, app_id);
5219
5220         rc = _account_query_step(hstmt);
5221         ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5222
5223         label_s* label_record = NULL;
5224
5225         while (rc == SQLITE_ROW) {
5226                 label_record = (label_s*) malloc(sizeof(label_s));
5227
5228                 if (label_record == NULL) {
5229                         ACCOUNT_FATAL("malloc Failed");
5230                         break;
5231                 }
5232
5233                 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
5234
5235                 _account_type_convert_column_to_label(hstmt, label_record);
5236
5237                 _INFO("Adding account label_list");
5238                 label_list = g_slist_append (label_list, label_record);
5239
5240                 rc = _account_query_step(hstmt);
5241         }
5242
5243         rc = _account_query_finalize(hstmt);
5244         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
5245         hstmt = NULL;
5246
5247         *error_code = ACCOUNT_ERROR_NONE;
5248
5249 CATCH:
5250         if (hstmt != NULL) {
5251                 rc = _account_query_finalize(hstmt);
5252                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
5253                 hstmt = NULL;
5254         }
5255
5256         pthread_mutex_unlock(&account_mutex);
5257         _INFO("Returning account label_list");
5258         return label_list;
5259 }
5260
5261 int account_type_query_label_by_app_id(account_label_cb callback, const char* app_id, void *user_data )
5262 {
5263         int                     error_code = ACCOUNT_ERROR_NONE;
5264         account_stmt    hstmt = NULL;
5265         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
5266         int                     rc = 0, binding_count = 1;
5267
5268         ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5269         ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
5270         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5271
5272         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5273
5274         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
5275         hstmt = _account_prepare_query(query);
5276
5277         if( _account_db_err_code() == SQLITE_PERM ){
5278                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5279                 return ACCOUNT_ERROR_PERMISSION_DENIED;
5280         }
5281
5282         _account_query_bind_text(hstmt, binding_count++, app_id);
5283
5284         rc = _account_query_step(hstmt);
5285         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5286
5287         label_s* label_record = NULL;
5288
5289         while (rc == SQLITE_ROW) {
5290                 bool cb_ret = FALSE;
5291                 label_record = (label_s*) malloc(sizeof(label_s));
5292
5293                 if (label_record == NULL) {
5294                         ACCOUNT_FATAL("malloc Failed");
5295                         break;
5296                 }
5297
5298                 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
5299
5300                 _account_type_convert_column_to_label(hstmt, label_record);
5301
5302                 cb_ret = callback(label_record->app_id, label_record->label , label_record->locale, user_data);
5303
5304                 _account_type_free_label_items(label_record);
5305                 _ACCOUNT_FREE(label_record);
5306
5307                 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
5308
5309                 rc = _account_query_step(hstmt);
5310         }
5311
5312         rc = _account_query_finalize(hstmt);
5313         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5314         hstmt = NULL;
5315
5316         error_code = ACCOUNT_ERROR_NONE;
5317
5318 CATCH:
5319         if (hstmt != NULL) {
5320                 rc = _account_query_finalize(hstmt);
5321                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5322                 hstmt = NULL;
5323         }
5324
5325         pthread_mutex_unlock(&account_mutex);
5326         return error_code;
5327 }
5328
5329 int _account_type_label_get_app_id(label_h label, char **app_id)
5330 {
5331         if (!label) {
5332                 return ACCOUNT_ERROR_INVALID_PARAMETER;
5333         }
5334
5335         if (!app_id) {
5336                 return ACCOUNT_ERROR_INVALID_PARAMETER;
5337         }
5338
5339         label_s *data = (label_s*)label;
5340
5341         (*app_id) = NULL;
5342
5343         *app_id = _account_get_text(data->app_id);
5344
5345         return ACCOUNT_ERROR_NONE;
5346 }
5347
5348 bool _account_get_label_text_cb(char* app_id, char* label, char* locale, void *user_data)
5349 {
5350         account_type_s *data = (account_type_s*)user_data;
5351
5352         label_s *label_data = (label_s*)malloc(sizeof(label_s));
5353
5354         if (label_data == NULL) {
5355                 ACCOUNT_FATAL("_account_get_label_text_cb : MALLOC FAIL\n");
5356                 return FALSE;
5357         }
5358         ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
5359
5360         label_data->app_id = _account_get_text(app_id);
5361         label_data->label = _account_get_text(label);
5362         label_data->locale = _account_get_text(locale);
5363
5364         data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
5365
5366         return TRUE;
5367 }
5368
5369 bool _account_get_provider_feature_cb(char* app_id, char* key, void* user_data)
5370 {
5371         account_type_s *data = (account_type_s*)user_data;
5372
5373         provider_feature_s *feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
5374
5375         if (feature_data == NULL) {
5376                 ACCOUNT_FATAL("_account_get_provider_feature_cb : MALLOC FAIL\n");
5377                 return FALSE;
5378         }
5379         ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
5380
5381         feature_data->app_id = _account_get_text(app_id);
5382         feature_data->key = _account_get_text(key);
5383
5384         data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
5385
5386         return TRUE;
5387 }
5388
5389 int _account_type_query_by_app_id(const char* app_id, account_type_s** account_type_record)
5390 {
5391         _INFO("_account_type_query_by_app_id start");
5392
5393         int                     error_code = ACCOUNT_ERROR_NONE;
5394         account_stmt    hstmt = NULL;
5395         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
5396         int                     rc = 0, binding_count = 1;
5397
5398         ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5399         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5400
5401         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5402
5403         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
5404         hstmt = _account_prepare_query(query);
5405
5406         if( _account_db_err_code() == SQLITE_PERM ){
5407                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5408                 return ACCOUNT_ERROR_PERMISSION_DENIED;
5409         }
5410
5411         _account_query_bind_text(hstmt, binding_count++, app_id);
5412
5413         rc = _account_query_step(hstmt);
5414         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5415
5416         *account_type_record = create_empty_account_type_instance();
5417
5418         while (rc == SQLITE_ROW) {
5419                 _account_type_convert_column_to_account_type(hstmt, *account_type_record);
5420                 rc = _account_query_step(hstmt);
5421         }
5422
5423         rc = _account_query_finalize(hstmt);
5424         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5425         account_type_query_label_by_app_id(_account_get_label_text_cb, app_id, (void*)(*account_type_record));
5426         account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, app_id,(void*)(*account_type_record));
5427
5428         hstmt = NULL;
5429         error_code = ACCOUNT_ERROR_NONE;
5430
5431 CATCH:
5432         if (hstmt != NULL) {
5433                 rc = _account_query_finalize(hstmt);
5434                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5435                 hstmt = NULL;
5436         }
5437
5438         pthread_mutex_unlock(&account_mutex);
5439         _INFO("_account_type_query_by_app_id end [%d]", error_code);
5440         return error_code;
5441 }
5442
5443 int _account_type_query_app_id_exist(const char* app_id)
5444 {
5445         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
5446         int                     rc = 0;
5447
5448         ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5449         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5450
5451         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5452
5453         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
5454         rc = _account_get_record_count(query);
5455
5456         if( _account_db_err_code() == SQLITE_PERM ){
5457                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5458                 return ACCOUNT_ERROR_PERMISSION_DENIED;
5459         }
5460
5461         if (rc <= 0) {
5462                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5463         }
5464
5465         return ACCOUNT_ERROR_NONE;
5466 }
5467
5468 GSList* _account_type_query_by_provider_feature(const char* key, int *error_code)
5469 {
5470         *error_code = ACCOUNT_ERROR_NONE;
5471         account_stmt    hstmt = NULL;
5472         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
5473         int                     rc = 0;
5474         GSList                  *account_type_list = NULL;
5475
5476         if(key == NULL)
5477         {
5478                 ACCOUNT_ERROR("capability_type IS NULL.");
5479                 *error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
5480                 goto CATCH;
5481         }
5482
5483         if(g_hAccountDB == NULL)
5484         {
5485                 ACCOUNT_ERROR("The database isn't connected.");
5486                 *error_code = ACCOUNT_ERROR_DB_NOT_OPENED;
5487                 goto CATCH;
5488         }
5489
5490         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5491
5492         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
5493
5494         hstmt = _account_prepare_query(query);
5495
5496         if( _account_db_err_code() == SQLITE_PERM )
5497         {
5498                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5499                 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
5500                 goto CATCH;
5501         }
5502
5503         int binding_count = 1;
5504         _account_query_bind_text(hstmt, binding_count++, key);
5505
5506         rc = _account_query_step(hstmt);
5507
5508         account_type_s *account_type_record = NULL;
5509
5510         if(rc != SQLITE_ROW)
5511         {
5512                 ACCOUNT_ERROR("The record isn't found.");
5513                 *error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
5514                 goto CATCH;
5515         }
5516
5517         while(rc == SQLITE_ROW) {
5518                 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
5519
5520                 if (account_type_record == NULL) {
5521                         ACCOUNT_FATAL("malloc Failed");
5522                         break;
5523                 }
5524
5525                 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
5526                 _account_type_convert_column_to_account_type(hstmt, account_type_record);
5527                 account_type_list = g_slist_append(account_type_list, account_type_record);
5528                 rc = _account_query_step(hstmt);
5529         }
5530
5531         rc = _account_query_finalize(hstmt);
5532         if (rc != ACCOUNT_ERROR_NONE )
5533         {
5534                 _account_type_gslist_free(account_type_list);
5535                 ACCOUNT_ERROR("finalize error(%s)", rc);
5536                 *error_code = rc;
5537                 goto CATCH;
5538         }
5539         hstmt = NULL;
5540
5541         GSList* iter;
5542
5543         for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
5544                 account_type_s *account_type = NULL;
5545                 account_type = (account_type_s*)iter->data;
5546                 account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
5547                 account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
5548         }
5549
5550         *error_code = ACCOUNT_ERROR_NONE;
5551
5552 CATCH:
5553         if (hstmt != NULL) {
5554                 rc = _account_query_finalize(hstmt);
5555                 if (rc != ACCOUNT_ERROR_NONE)
5556                 {
5557                         *error_code = rc;
5558                         return NULL;
5559                 }
5560                 hstmt = NULL;
5561         }
5562
5563         return account_type_list;
5564 }
5565
5566
5567 GSList* _account_type_query_all(void)
5568 {
5569         account_stmt    hstmt = NULL;
5570         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
5571         int                     rc = 0;
5572         GSList                  *account_type_list = NULL;
5573
5574         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected."));
5575
5576         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5577
5578         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
5579         hstmt = _account_prepare_query(query);
5580
5581         rc = _account_query_step(hstmt);
5582
5583         if( _account_db_err_code() == SQLITE_PERM ){
5584                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5585                 return NULL;
5586         }
5587
5588         account_type_s *account_type_record = NULL;
5589
5590         if (rc != SQLITE_ROW)
5591         {
5592                 _INFO("[ACCOUNT_ERROR_RECORD_NOT_FOUND]The record isn't found.");
5593                 goto CATCH;
5594         }
5595
5596         while(rc == SQLITE_ROW) {
5597                 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
5598
5599                 if (account_type_record == NULL) {
5600                         ACCOUNT_FATAL("malloc Failed");
5601                         break;
5602                 }
5603
5604                 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
5605                 _account_type_convert_column_to_account_type(hstmt, account_type_record);
5606                 account_type_list = g_slist_append(account_type_list, account_type_record);
5607                 rc = _account_query_step(hstmt);
5608         }
5609
5610         rc = _account_query_finalize(hstmt);
5611         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, NULL, ("finalize error"));
5612         hstmt = NULL;
5613
5614         GSList* iter;
5615
5616         for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
5617                 account_type_s *account_type = NULL;
5618                 account_type = (account_type_s*)iter->data;
5619                 account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
5620                 account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
5621         }
5622
5623 CATCH:
5624         if (hstmt != NULL)
5625         {
5626                 rc = _account_query_finalize(hstmt);
5627                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {_account_type_gslist_free(account_type_list);}, NULL, ("finalize error"));
5628                 hstmt = NULL;
5629         }
5630
5631         return account_type_list;
5632 }
5633
5634 // output parameter label must be free
5635 int _account_type_query_label_by_locale(const char* app_id, const char* locale, char **label)
5636 {
5637         int                     error_code = ACCOUNT_ERROR_NONE;
5638         account_stmt    hstmt = NULL;
5639         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
5640         int                     rc = 0, binding_count = 1;
5641         char*                   converted_locale = NULL;
5642
5643         ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
5644         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5645         ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
5646         ACCOUNT_RETURN_VAL((locale != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("locale char is null"));
5647         //Making label newly created
5648
5649         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5650
5651         converted_locale = _account_get_text(locale);
5652         gchar** tokens = g_strsplit(converted_locale, "-", 2);
5653
5654         if(tokens != NULL) {
5655                 if((char*)(tokens[1]) != NULL) {
5656                         char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1]));
5657                         if(upper_token != NULL) {
5658                                 _ACCOUNT_FREE(converted_locale);
5659                                 converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token);
5660                         }
5661                         _ACCOUNT_FREE(upper_token);
5662                 }
5663         }
5664         g_strfreev(tokens);
5665
5666         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, converted_locale);
5667         _ACCOUNT_FREE(converted_locale);
5668
5669         hstmt = _account_prepare_query(query);
5670
5671         if( _account_db_err_code() == SQLITE_PERM ){
5672                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5673                 return ACCOUNT_ERROR_PERMISSION_DENIED;
5674         }
5675
5676         _account_query_bind_text(hstmt, binding_count++, app_id);
5677
5678         rc = _account_query_step(hstmt);
5679         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5680
5681         label_s* label_record = NULL;
5682
5683         while (rc == SQLITE_ROW) {
5684                 label_record = (label_s*) malloc(sizeof(label_s));
5685
5686                 if (label_record == NULL) {
5687                         ACCOUNT_FATAL("malloc Failed");
5688                         break;
5689                 }
5690
5691                 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
5692
5693                 _account_type_convert_column_to_label(hstmt,label_record);
5694
5695                 _ACCOUNT_FREE(*label);
5696                 //Making label newly created
5697                 *label = _account_get_text(label_record->label);
5698
5699                 _account_type_free_label_items(label_record);
5700                 _ACCOUNT_FREE(label_record);
5701
5702                 rc = _account_query_step(hstmt);
5703         }
5704
5705         rc = _account_query_finalize(hstmt);
5706         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5707         hstmt = NULL;
5708
5709         error_code = ACCOUNT_ERROR_NONE;
5710
5711         CATCH:
5712         if (hstmt != NULL) {
5713                 rc = _account_query_finalize(hstmt);
5714                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5715                 hstmt = NULL;
5716         }
5717
5718         pthread_mutex_unlock(&account_mutex);
5719         _INFO("_account_type_query_label_by_locale() end : error_code = %d", error_code);
5720         return error_code;
5721 }
5722
5723 static int _account_insert_custom(account_s *account, int account_id)
5724 {
5725         _INFO("_account_insert_custom start");
5726
5727         int                     rc, count = 1;
5728         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
5729         account_stmt    hstmt = NULL;
5730
5731         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
5732
5733         if (g_slist_length( account->custom_list)==0) {
5734                 ACCOUNT_DEBUG( "_account_insert_custom, no custom data\n");
5735                 return ACCOUNT_ERROR_NONE;
5736         }
5737
5738         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
5739
5740         rc = _account_get_record_count(query);
5741
5742         if( _account_db_err_code() == SQLITE_PERM ){
5743                 ACCOUNT_ERROR( "Access failed(%d, %s)", _account_db_err_msg());
5744                 return ACCOUNT_ERROR_PERMISSION_DENIED;
5745         }
5746
5747         if (rc <= 0) {
5748                 ACCOUNT_SLOGE( "_account_insert_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
5749                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5750         }
5751
5752         /* insert query*/
5753
5754         GSList *iter;
5755
5756         for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
5757                 int ret;
5758                 count = 1;
5759                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5760                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (AccountId, AppId, Key, Value) VALUES "
5761                                 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
5762
5763                 hstmt = _account_prepare_query(query);
5764
5765                 if( _account_db_err_code() == SQLITE_PERM ){
5766                         ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5767                         return ACCOUNT_ERROR_PERMISSION_DENIED;
5768                 }
5769
5770                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
5771
5772                 account_custom_s* custom_data = NULL;
5773                 custom_data = (account_custom_s*)iter->data;
5774
5775                 ret = _account_query_bind_int(hstmt, count++, account_id);
5776                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
5777                 ret = _account_query_bind_text(hstmt, count++, account->package_name);
5778                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5779                 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
5780                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5781                 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
5782                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5783
5784                 rc = _account_query_step(hstmt);
5785
5786                 if (rc != SQLITE_DONE) {
5787                         ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5788                         break;
5789                 }
5790
5791                 rc = _account_query_finalize(hstmt);
5792                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5793                 hstmt = NULL;
5794
5795         }
5796
5797         _INFO("_account_insert_custom end");
5798         return ACCOUNT_ERROR_NONE;
5799 }
5800
5801 static int _account_update_custom(account_s *account, int account_id)
5802 {
5803         int                     rc, count = 1;
5804         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
5805         account_stmt    hstmt = NULL;
5806
5807         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
5808
5809         if (g_slist_length( account->custom_list)==0) {
5810                 ACCOUNT_DEBUG( "_account_update_custom, no custom data\n");
5811                 return ACCOUNT_ERROR_NONE;
5812         }
5813
5814         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
5815
5816         rc = _account_get_record_count(query);
5817
5818         if( _account_db_err_code() == SQLITE_PERM ){
5819                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5820                 pthread_mutex_unlock(&account_mutex);
5821                 return ACCOUNT_ERROR_PERMISSION_DENIED;
5822         } else if( _account_db_err_code() == SQLITE_BUSY ){
5823                 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
5824                 pthread_mutex_unlock(&account_mutex);
5825                 return ACCOUNT_ERROR_DATABASE_BUSY;
5826         }
5827
5828         if (rc <= 0) {
5829                 ACCOUNT_SLOGE( "_account_update_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
5830                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5831         }
5832
5833         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5834
5835         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId=? ", ACCOUNT_CUSTOM_TABLE);
5836         hstmt = _account_prepare_query(query);
5837         count = 1;
5838         _account_query_bind_int(hstmt, count++, (int)account_id);
5839         rc = _account_query_step(hstmt);
5840
5841         if (rc == SQLITE_BUSY) {
5842                 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5843                 return ACCOUNT_ERROR_DATABASE_BUSY;
5844         } else if (rc != SQLITE_DONE) {
5845                 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5846                 return ACCOUNT_ERROR_DB_FAILED;
5847         }
5848
5849         rc = _account_query_finalize(hstmt);
5850         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5851         hstmt = NULL;
5852
5853         GSList *iter;
5854
5855         for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
5856                 int ret;
5857                 count = 1;
5858                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5859                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AccountId, AppId, Key, Value) VALUES "
5860                                 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
5861
5862                 hstmt = _account_prepare_query(query);
5863
5864                 if( _account_db_err_code() == SQLITE_PERM ){
5865                         ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5866                         return ACCOUNT_ERROR_PERMISSION_DENIED;
5867                 }
5868
5869                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
5870
5871                 account_custom_s* custom_data = NULL;
5872                 custom_data = (account_custom_s*)iter->data;
5873
5874                 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
5875                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
5876                 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
5877                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5878                 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
5879                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5880                 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
5881                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5882
5883                 rc = _account_query_step(hstmt);
5884
5885                 if (rc != SQLITE_DONE) {
5886                         ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5887                         break;
5888                 }
5889
5890                 rc = _account_query_finalize(hstmt);
5891                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5892                 hstmt = NULL;
5893
5894         }
5895
5896         return ACCOUNT_ERROR_NONE;
5897 }
5898
5899 static int _account_query_custom_by_account_id(account_custom_cb callback, int account_id, void *user_data )
5900 {
5901         int                     error_code = ACCOUNT_ERROR_NONE;
5902         account_stmt    hstmt = NULL;
5903         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
5904         int                     rc = 0;
5905
5906         ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
5907         ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
5908         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5909
5910         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5911
5912         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
5913         hstmt = _account_prepare_query(query);
5914
5915         if( _account_db_err_code() == SQLITE_PERM ){
5916                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5917                 return ACCOUNT_ERROR_PERMISSION_DENIED;
5918         }
5919
5920         rc = _account_query_step(hstmt);
5921
5922         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5923
5924         account_custom_s* custom_record = NULL;
5925
5926         while (rc == SQLITE_ROW) {
5927                 bool cb_ret = FALSE;
5928                 custom_record = (account_custom_s*) malloc(sizeof(account_custom_s));
5929
5930                 if (custom_record == NULL) {
5931                         ACCOUNT_FATAL("malloc Failed");
5932                         break;
5933                 }
5934
5935                 ACCOUNT_MEMSET(custom_record, 0x00, sizeof(account_custom_s));
5936
5937                 _account_convert_column_to_custom(hstmt, custom_record);
5938
5939                 cb_ret = callback(custom_record->key, custom_record->value, user_data);
5940
5941                 _account_custom_item_free(custom_record);
5942                 _ACCOUNT_FREE(custom_record);
5943
5944                 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
5945
5946                 rc = _account_query_step(hstmt);
5947         }
5948
5949         rc = _account_query_finalize(hstmt);
5950         ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5951         hstmt = NULL;
5952
5953         error_code = ACCOUNT_ERROR_NONE;
5954
5955 CATCH:
5956         if (hstmt != NULL) {
5957                 rc = _account_query_finalize(hstmt);
5958                 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5959                 hstmt = NULL;
5960         }
5961
5962         pthread_mutex_unlock(&account_mutex);
5963         return error_code;
5964 }