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