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