adjust global DB path for owner user
[platform/core/account/account-manager.git] / server / src / account-server-db.c
1 /*
2  *
3  * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <sys/stat.h>
25 #include <pthread.h>
26 #include <glib.h>
27 #include <db-util.h>
28 #include <pthread.h>
29 #include <vconf.h>
30
31 #include <pkgmgr-info.h>
32 //#include <tzplatform_config.h>
33
34 #include <dbg.h>
35 #include <account_ipc_marshal.h>
36 #include <account_free.h>
37 #include <account-private.h>
38 #include <account_db_helper.h>
39 #include <account_crypto_service.h>
40 #include <account_err.h>
41 #include "account_type.h"
42 #include "account-server-db.h"
43
44 //typedef sqlite3_stmt* account_stmt;
45
46 #define EMAIL_SERVICE_CMDLINE "/usr/bin/email-service"
47
48 #define EMAIL_APPID "email-setting-efl"
49
50 #define ACCOUNT_DB_OPEN_READONLY 0
51 #define ACCOUNT_DB_OPEN_READWRITE 1
52
53 #define MAX_TEXT 4096
54
55 #define _TIZEN_PUBLIC_
56 #ifndef _TIZEN_PUBLIC_
57
58 #endif
59
60 static sqlite3* g_hAccountDB = NULL;
61 static sqlite3* g_hAccountDB2 = NULL;
62 static sqlite3* g_hAccountGlobalDB = NULL;
63 static sqlite3* g_hAccountGlobalDB2 = NULL;
64 pthread_mutex_t account_mutex = PTHREAD_MUTEX_INITIALIZER;
65 pthread_mutex_t account_global_mutex = PTHREAD_MUTEX_INITIALIZER;
66
67 //static char *_account_dup_text(const char *text_data);
68 static int _account_insert_custom(account_s *account, int account_id);
69 static int _account_update_custom(account_s *account, int account_id);
70 static int _account_type_update_provider_feature(sqlite3 * account_db_handle, account_type_s *account_type, const char* app_id);
71
72 static void _account_insert_delete_update_notification_send(char *noti_name)
73 {
74         if (!noti_name) {
75                 _ERR("Noti Name is NULL!!!!!!\n");
76                 return;
77         }
78
79         _INFO("noti_type = %s", noti_name);
80
81         if (vconf_set_str(VCONFKEY_ACCOUNT_MSG_STR, noti_name) != 0) {
82                 _ERR("Vconf MSG Str set FAILED !!!!!!\n");;
83         }
84 }
85
86 int _account_get_current_appid_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
87 {
88         char* appid = NULL;
89         char* item = NULL;
90         GSList** appid_list = (GSList**)user_data;
91         int pkgmgr_ret = -1;
92
93         pkgmgr_ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
94
95         if( pkgmgr_ret != PMINFO_R_OK ){
96                 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_appid(%d)", pkgmgr_ret);
97         }
98
99         item = _account_dup_text(appid);
100         *appid_list = g_slist_append(*appid_list, item);
101
102         return 0;
103 }
104
105 static inline int __read_proc(const char *path, char *buf, int size)
106 {
107         int fd = 0, ret = 0;
108
109         if (buf == NULL || path == NULL) {
110                 ACCOUNT_ERROR("path and buffer is mandatory\n");
111                 return -1;
112         }
113
114         fd = open(path, O_RDONLY);
115         if (fd < 0) {
116                 ACCOUNT_ERROR("fd open error(%d)\n", fd);
117                 return -1;
118         }
119
120         ret = read(fd, buf, size - 1);
121         if (ret <= 0) {
122                 ACCOUNT_ERROR("fd read error(%d)\n", fd);
123                 close(fd);
124                 return -1;
125         } else
126                 buf[ret] = 0;
127
128         close(fd);
129
130         return ret;
131 }
132
133 char *_account_get_proc_cmdline_bypid(int pid)
134 {
135         char buf[128];
136         int ret = 0;
137
138         ACCOUNT_SNPRINTF(buf, sizeof(buf), "/proc/%d/cmdline", pid);
139         ret = __read_proc(buf, buf, sizeof(buf));
140         if (ret <= 0) {
141                 ACCOUNT_DEBUG("No proc directory (%d)\n", pid);
142                 return NULL;
143         }
144
145         return strdup(buf);
146 }
147
148
149 /*
150 static int _account_execute_query_from_global_db(const char *query)
151 {
152         int rc = -1;
153         char* pszErrorMsg = NULL;
154
155         if(!query){
156                 ACCOUNT_ERROR("NULL query\n");
157                 return _ACCOUNT_ERROR_QUERY_SYNTAX_ERROR;
158         }
159
160         if(!g_hAccountGlobalDB){
161                 ACCOUNT_ERROR("Global DB is not opened\n");
162                 return _ACCOUNT_ERROR_DB_NOT_OPENED;
163         }
164
165         rc = sqlite3_exec(g_hAccountGlobalDB, query, NULL, NULL, &pszErrorMsg);
166         if (SQLITE_OK != rc) {
167                 ACCOUNT_ERROR("sqlite3_exec rc(%d) query(%s) failed(%s).", rc, query, pszErrorMsg);
168                 sqlite3_free(pszErrorMsg);
169         }
170
171         return rc;
172 }
173 */
174 /*
175 static int _account_begin_transaction_from_global_db(void)
176 {
177         ACCOUNT_DEBUG("_account_begin_transaction start");
178         int ret = -1;
179
180         ret = _account_execute_query_from_global_db("BEGIN IMMEDIATE TRANSACTION");
181
182         if (ret == SQLITE_BUSY){
183                 ACCOUNT_ERROR(" sqlite3 busy = %d", ret);
184                 return _ACCOUNT_ERROR_DATABASE_BUSY;
185         } else if(ret != SQLITE_OK) {
186                 ACCOUNT_ERROR("_account_svc_begin_transaction_in_global_db fail :: %d", ret);
187                 return _ACCOUNT_ERROR_DB_FAILED;
188         }
189
190         ACCOUNT_DEBUG("_account_begin_transaction_in_global_db end");
191         return _ACCOUNT_ERROR_NONE;
192 }
193
194 static int _account_end_transaction_from_global_db(bool is_success)
195 {
196         ACCOUNT_DEBUG("_account_end_transaction_in_global_db start");
197
198         int ret = -1;
199
200         if (is_success == true) {
201                 ret = _account_execute_query_from_global_db("COMMIT TRANSACTION");
202                 ACCOUNT_DEBUG("_account_end_transaction_in_global_db COMMIT");
203         } else {
204                 ret = _account_execute_query_from_global_db("ROLLBACK TRANSACTION");
205                 ACCOUNT_DEBUG("_account_end_transaction ROLLBACK");
206         }
207
208         if(ret == SQLITE_PERM) {
209                 ACCOUNT_ERROR("Account permission denied :: %d", ret);
210                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
211         }
212
213         if (ret == SQLITE_BUSY){
214                 ACCOUNT_DEBUG(" sqlite3 busy = %d", ret);
215                 return _ACCOUNT_ERROR_DATABASE_BUSY;
216         }
217
218         if (ret != SQLITE_OK) {
219                 ACCOUNT_ERROR("_account_svc_end_transaction_in_global_db fail :: %d", ret);
220                 return _ACCOUNT_ERROR_DB_FAILED;
221         }
222
223         ACCOUNT_DEBUG("_account_end_transaction_in_global_db end");
224         return _ACCOUNT_ERROR_NONE;
225 }
226 */
227
228 int _account_global_db_open(void)
229 {
230         int rc = 0;
231         int ret = -1;
232         char account_db_path[256] = {0, };
233
234         _INFO("start _account_global_db_open()");
235
236         ACCOUNT_MEMSET(account_db_path, 0x00, sizeof(account_db_path));
237         ACCOUNT_GET_GLOBAL_DB_PATH(account_db_path, sizeof(account_db_path));
238
239         if (g_hAccountGlobalDB) {
240                 _ERR("Account database is using in another app. %x", g_hAccountDB);
241                 return _ACCOUNT_ERROR_DATABASE_BUSY;
242         }
243
244         ret = _account_db_handle_close(g_hAccountGlobalDB2);
245         if (ret != _ACCOUNT_ERROR_NONE)
246                 ACCOUNT_DEBUG("db_util_close(g_hAccountGlobalDB2) fail ret = %d", ret);
247
248         ACCOUNT_DEBUG("before _account_global_db_open()");
249 //      if(mode == ACCOUNT_DB_OPEN_READWRITE)
250 //              rc = db_util_open(account_db_path, &g_hAccountDB, DB_UTIL_REGISTER_HOOK_METHOD);
251 //      else if(mode == ACCOUNT_DB_OPEN_READONLY)
252         rc = db_util_open_with_options(account_db_path, &g_hAccountGlobalDB, SQLITE_OPEN_READONLY, NULL);
253 //      else
254 //              return _ACCOUNT_ERROR_DB_NOT_OPENED;
255         ACCOUNT_DEBUG("after _account_global_db_open() sqlite_rc = %d", rc);
256
257         if (rc == SQLITE_PERM || _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
258                 ACCOUNT_ERROR("Account permission denied");
259                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
260         }
261
262         if (rc == SQLITE_BUSY) {
263                 ACCOUNT_ERROR("busy handler fail.");
264                 return _ACCOUNT_ERROR_DATABASE_BUSY;
265         }
266
267         if (rc != SQLITE_OK) {
268                 ACCOUNT_ERROR("The database isn't connected.");
269                 return _ACCOUNT_ERROR_DB_NOT_OPENED;
270         }
271
272         _INFO("end _account_global_db_open()");
273         return _ACCOUNT_ERROR_NONE;
274 }
275
276 int _account_global_db_close(void)
277 {
278         ACCOUNT_DEBUG("start account_global_db_close()");
279         int ret = -1;
280 /*
281         ret = _account_db_handle_close(g_hAccountGlobalDB2);
282         if( ret != _ACCOUNT_ERROR_NONE )
283                 ACCOUNT_DEBUG( "db_util_close(g_hAccountGlobalDB2) fail ret = %d", ret);
284 */
285         ret = _account_db_handle_close(g_hAccountGlobalDB);
286         if (ret != _ACCOUNT_ERROR_NONE) {
287                 ACCOUNT_ERROR("db_util_close(g_hAccountGlobalDB) fail ret = %d", ret);
288                 g_hAccountGlobalDB2 = g_hAccountGlobalDB;
289         }
290         g_hAccountGlobalDB = NULL;
291
292         return ret;
293 }
294
295 static bool _account_check_add_more_account(const char* app_id)
296 {
297         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
298         int                     rc = 0;
299
300         ACCOUNT_RETURN_VAL((app_id != 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
301         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
302
303         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
304
305         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s' and MultipleAccountSupport = 1", ACCOUNT_TYPE_TABLE, app_id);
306         rc = _account_get_record_count(g_hAccountDB, query);
307
308         /* multiple account support case (User DB & global DB) */
309         if(rc > 0 || _account_get_record_count(g_hAccountGlobalDB, query) > 0) {
310                 ACCOUNT_SLOGD("app id (%s) supports multiple account. rc(%d)\n", app_id, rc);
311                 return TRUE;
312         }
313
314         /* multiple account not support case */
315         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
316         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE package_name = '%s'", ACCOUNT_TABLE, app_id);
317         rc = _account_get_record_count(g_hAccountDB, query);
318
319         if(rc <= 0) {
320                 ACCOUNT_SLOGD("app id (%s) supports single account. and there is no account of the app id\n", app_id);
321                 return TRUE;
322         }
323
324         return FALSE;
325 }
326
327
328 int _account_db_open(int mode, int pid, uid_t uid)
329 {
330         int rc = 0;
331         int ret = -1;
332         char account_db_dir[256] = {0, };
333         char account_db_path[256] = {0, };
334
335         _INFO("start _account_db_open() pid [%d]. uid [%d]", pid, uid);
336
337         ACCOUNT_MEMSET(account_db_dir, 0x00, sizeof(account_db_dir));
338         ACCOUNT_MEMSET(account_db_path, 0x00, sizeof(account_db_path));
339
340         ACCOUNT_GET_USER_DB_PATH(account_db_path, sizeof(account_db_path), uid);
341
342         if (g_hAccountDB) {
343                 _ERR("Account database is using in another app. %x", g_hAccountDB);
344                 return _ACCOUNT_ERROR_DATABASE_BUSY;
345         }
346
347         ret = _account_db_handle_close(g_hAccountDB2);
348         if (ret != _ACCOUNT_ERROR_NONE)
349                 ACCOUNT_DEBUG("db_util_close(g_hAccountDB2) fail ret = %d", ret);
350
351         ACCOUNT_GET_USER_DB_DIR(account_db_dir, sizeof(account_db_dir), uid);
352
353         if (mkdir(account_db_dir, 0777) != 0)
354                 ACCOUNT_DEBUG("\"%s\" is already exist directory", account_db_dir);
355
356         ACCOUNT_DEBUG("before db_util_open()");
357 //      if(mode == ACCOUNT_DB_OPEN_READWRITE)
358                 rc = db_util_open(account_db_path, &g_hAccountDB, DB_UTIL_REGISTER_HOOK_METHOD);
359 //      else if(mode == ACCOUNT_DB_OPEN_READONLY)
360 //              rc = db_util_open_with_options(account_db_path, &g_hAccountDB, SQLITE_OPEN_READONLY, NULL);
361 //      else
362 //              return _ACCOUNT_ERROR_DB_NOT_OPENED;
363         ACCOUNT_DEBUG("after db_util_open() sqlite_rc = %d", rc);
364
365         if (rc == SQLITE_PERM || _account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
366                 ACCOUNT_ERROR("Account permission denied");
367                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
368         }
369
370         if (rc == SQLITE_BUSY) {
371                 ACCOUNT_ERROR("busy handler fail.");
372                 return _ACCOUNT_ERROR_DATABASE_BUSY;
373         }
374
375         if (rc != SQLITE_OK) {
376                 ACCOUNT_ERROR("The database isn't connected.");
377                 return _ACCOUNT_ERROR_DB_NOT_OPENED;
378         }
379
380         rc = _account_check_is_all_table_exists(g_hAccountDB);
381
382         if (rc < 0) {
383                 _ERR("_account_check_is_all_table_exists rc=[%d]", rc);
384                 return rc;
385         } else if (rc == ACCOUNT_TABLE_TOTAL_COUNT) {
386                 _INFO("Tables OK");
387         } else {
388                 int ret = _account_create_all_tables(g_hAccountDB);
389                 if (ret != _ACCOUNT_ERROR_NONE) {
390                         _ERR("_account_create_all_tables fail ret=[%d]", ret);
391                         return ret;
392                 }
393         }
394
395         _INFO("end _account_db_open()");
396         return _ACCOUNT_ERROR_NONE;
397 }
398
399 int _account_db_close(void)
400 {
401         ACCOUNT_DEBUG("start db_util_close()");
402         int ret = -1;
403 /*
404         ret = _account_db_handle_close(g_hAccountDB2);
405         if( ret != _ACCOUNT_ERROR_NONE )
406                 ACCOUNT_DEBUG( "db_util_close(g_hAccountDB2) fail ret = %d", ret);
407 */
408         ret = _account_db_handle_close(g_hAccountDB);
409         if( ret != _ACCOUNT_ERROR_NONE )
410         {
411                 ACCOUNT_ERROR("db_util_close(g_hAccountDB) fail ret = %d", ret);
412                 g_hAccountDB2 = g_hAccountDB;
413         }
414         g_hAccountDB = NULL;
415
416         return ret;
417 }
418
419 static int _account_execute_insert_query(account_s *account)
420 {
421         _INFO("_account_execute_insert_query start");
422
423         int                             rc = 0;
424         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
425         int                             error_code = _ACCOUNT_ERROR_NONE;
426         account_stmt    hstmt = NULL;
427
428         /* check whether app id exist in account type db */
429
430         if (!account->user_name && !account->display_name && !account->email_address) {
431                 _INFO("");
432                 ACCOUNT_ERROR("Mandetory fields is NULL. At least one field is required among username, display name, email address\n");
433                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
434         }
435
436         _INFO("");
437         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
438         ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (user_name, email_address , display_name , icon_path , source , package_name , "
439                         "access_token , domain_name , auth_type , secret , sync_support , txt_custom0, txt_custom1, txt_custom2, txt_custom3, txt_custom4, "
440                         "int_custom0, int_custom1, int_custom2, int_custom3, int_custom4, txt_custom0 ) values " // to do urusa
441                         "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",   ACCOUNT_TABLE);
442
443         hstmt = _account_prepare_query(g_hAccountDB, query);
444         ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
445
446         _INFO("");
447         _account_convert_account_to_sql(account, hstmt, query);
448
449         _INFO("");
450         rc = _account_query_step(hstmt);
451         if (rc != SQLITE_DONE) {
452                 _INFO("");
453                 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
454
455                 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM )
456                         error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
457                 else
458                         error_code = _ACCOUNT_ERROR_DB_FAILED;
459         }
460
461         _INFO("");
462         rc = _account_query_finalize(hstmt);
463         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
464         hstmt = NULL;
465
466         _INFO("_account_execute_insert_query end");
467         return error_code;
468 }
469
470 static int _account_insert_capability(account_s *account, int account_id)
471 {
472         _INFO("_account_insert_capability start");
473         int                     rc, count = 1;
474         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
475         account_stmt    hstmt = NULL;
476
477         ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
478
479         if (g_slist_length( account->capablity_list)==0) {
480                 ACCOUNT_DEBUG( "_account_insert_capability, no capability\n");
481                 return _ACCOUNT_ERROR_NONE;
482         }
483
484         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
485
486         _INFO("_account_insert_capability _account_get_record_count [%s]", query);
487         rc = _account_get_record_count(g_hAccountDB, query);
488
489         if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
490                 _ERR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
491                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
492         }
493         if (rc <= 0) {
494                 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
495         }
496
497         /* insert query*/
498
499         GSList *iter;
500
501         for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
502                 int ret;
503                 count = 1;
504
505                 account_capability_s* cap_data = NULL;
506                 cap_data = (account_capability_s*)iter->data;
507
508                 _INFO("cap_data->type = %s, cap_data->value = %d \n", cap_data->type, cap_data->value);
509
510                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
511                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
512                                 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
513                 hstmt = _account_prepare_query(g_hAccountDB, query);
514
515                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
516
517                 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
518                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
519                 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
520                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
521                 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
522                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
523                 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
524                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
525                 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
526                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
527
528                 rc = _account_query_step(hstmt);
529                 _INFO("_account_insert_capability _account_query_step[%d]", rc);
530
531                 if (rc != SQLITE_DONE) {
532                         _ERR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
533                         break;
534                 }
535
536                 rc = _account_query_finalize(hstmt);
537                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
538                 hstmt = NULL;
539
540         }
541
542         _INFO("_account_insert_capability end");
543         return _ACCOUNT_ERROR_NONE;
544 }
545
546 static int _account_update_capability(account_s *account, int account_id)
547 {
548         int                     rc, count = 1;
549         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
550         account_stmt    hstmt = NULL;
551
552         ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
553
554         if (g_slist_length( account->capablity_list)==0) {
555                 ACCOUNT_ERROR( "_account_update_capability, no capability\n");
556                 return _ACCOUNT_ERROR_NONE;
557         }
558
559         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
560
561         rc = _account_get_record_count(g_hAccountDB, query);
562
563         if (rc <= 0) {
564                 ACCOUNT_SLOGI( "_account_update_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
565                 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
566         }
567
568         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
569
570         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id=? ", CAPABILITY_TABLE);
571         hstmt = _account_prepare_query(g_hAccountDB, query);
572         count = 1;
573         _account_query_bind_int(hstmt, count++, (int)account_id);
574         rc = _account_query_step(hstmt);
575
576         if (rc != SQLITE_DONE) {
577                 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
578                 return _ACCOUNT_ERROR_DB_FAILED;
579         }
580         rc = _account_query_finalize(hstmt);
581         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
582         hstmt = NULL;
583
584         GSList *iter;
585
586         for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
587                 int ret;
588                 count = 1;
589                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
590                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
591                                 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
592
593                 hstmt = _account_prepare_query(g_hAccountDB, query);
594
595                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
596
597                 account_capability_s* cap_data = NULL;
598                 cap_data = (account_capability_s*)iter->data;
599
600                 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
601                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
602                 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
603                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
604                 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
605                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
606                 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
607                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
608                 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
609                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
610
611                 rc = _account_query_step(hstmt);
612
613                 if (rc != SQLITE_DONE) {
614                         ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
615                         break;
616                 }
617
618                 rc = _account_query_finalize(hstmt);
619                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
620                 hstmt = NULL;
621
622         }
623
624         return _ACCOUNT_ERROR_NONE;
625 }
626
627 static int _account_update_capability_by_user_name(account_s *account, const char *user_name, const char *package_name )
628 {
629         int                     rc, count = 1;
630         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
631         account_stmt    hstmt = NULL;
632
633         ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
634
635         if (g_slist_length( account->capablity_list)==0) {
636                 ACCOUNT_ERROR( "_account_update_capability_by_user_name, no capability\n");
637                 return _ACCOUNT_ERROR_NONE;
638         }
639
640         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where package_name= '%s' and user_name='%s'", ACCOUNT_TABLE, package_name, user_name);
641
642         rc = _account_get_record_count(g_hAccountDB, query);
643
644         if (rc <= 0) {
645                 ACCOUNT_SLOGI( "_account_update_capability_by_user_name : related account item is not existed rc=%d , %s ", rc, _account_db_err_msg(g_hAccountDB));
646                 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
647         }
648
649         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
650
651         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name=? and user_name=? ", CAPABILITY_TABLE);
652         hstmt = _account_prepare_query(g_hAccountDB, query);
653         count = 1;
654         _account_query_bind_text(hstmt, count++, (char*)account->package_name);
655         _account_query_bind_text(hstmt, count++, (char*)account->user_name);
656         rc = _account_query_step(hstmt);
657         if (rc != SQLITE_DONE) {
658                 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
659                 return _ACCOUNT_ERROR_DB_FAILED;
660         }
661
662         rc = _account_query_finalize(hstmt);
663         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
664         hstmt = NULL;
665
666         GSList* iter;
667
668         for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
669                 int ret;
670                 count = 1;
671                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
672                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
673                                 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
674
675                 hstmt = _account_prepare_query(g_hAccountDB, query);
676
677                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
678
679                 account_capability_s* cap_data = NULL;
680                 cap_data = (account_capability_s*)iter->data;
681
682                 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
683                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
684                 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
685                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
686                 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
687                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
688                 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
689                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
690                 ret = _account_query_bind_int(hstmt, count++, (int)account->id);
691                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
692
693                 rc = _account_query_step(hstmt);
694
695                 if (rc != SQLITE_DONE) {
696                         ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
697                         break;
698                 }
699
700                 rc = _account_query_finalize(hstmt);
701                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
702                 hstmt = NULL;
703
704         }
705
706         return _ACCOUNT_ERROR_NONE;
707 }
708
709
710 bool _account_add_capability_to_account_cb(const char* capability_type, int capability_value, account_s *account)
711 {
712         account_capability_s *cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
713
714         if (cap_data == NULL)
715                 return FALSE;
716         ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
717
718         cap_data->type = _account_dup_text(capability_type);
719         cap_data->value = capability_value;
720         _INFO("cap_data->type = %s, cap_data->value = %d", cap_data->type, cap_data->value);
721
722         account->capablity_list = g_slist_append(account->capablity_list, (gpointer)cap_data);
723
724         return TRUE;
725 }
726
727
728 bool _account_add_custom_to_account_cb(const char* key, const char* value, account_s *account)
729 {
730         account_custom_s *custom_data = (account_custom_s*)malloc(sizeof(account_custom_s));
731
732         if (custom_data == NULL) {
733                 ACCOUNT_DEBUG("_account_add_custom_to_account_cb :: malloc fail\n");
734                 return FALSE;
735         }
736         ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s));
737
738         custom_data->account_id = account->id;
739         custom_data->app_id = _account_dup_text(account->package_name);
740         custom_data->key = _account_dup_text(key);
741         custom_data->value = _account_dup_text(value);
742         _INFO("custom_data->key = %s, custom_data->value = %s", custom_data->key, custom_data->value);
743
744         account->custom_list = g_slist_append(account->custom_list, (gpointer)custom_data);
745
746         return TRUE;
747 }
748
749
750 static int _account_compare_old_record_by_user_name(account_s *new_account, const char* user_name, const char* package_name)
751 {
752         int                             error_code = _ACCOUNT_ERROR_NONE;
753         account_stmt    hstmt = NULL;
754         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
755         int                             rc = 0;
756         account_s *old_account = NULL;
757
758         ACCOUNT_RETURN_VAL((new_account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
759         ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
760         ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
761         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
762
763         old_account = (account_s*)calloc(1, sizeof(account_s));
764         if(!old_account) {
765                 ACCOUNT_FATAL("Memory alloc fail\n");
766                 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
767         }
768
769         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
770
771         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = '%s' and package_name='%s'", ACCOUNT_TABLE, user_name, package_name);
772         hstmt = _account_prepare_query(g_hAccountDB, query);
773
774         rc = _account_query_step(hstmt);
775         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
776
777         while (rc == SQLITE_ROW) {
778                 _account_convert_column_to_account(hstmt, old_account);
779                 rc = _account_query_step(hstmt);
780         }
781
782         rc = _account_query_finalize(hstmt);
783         ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
784         hstmt = NULL;
785
786         // get capability
787         error_code = _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, old_account->id, (void*)old_account);
788         ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
789
790         // get custom text
791         error_code = _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, old_account->id, (void*)old_account);
792         ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
793
794         // compare
795         new_account->id = old_account->id;
796
797         //user name
798         if(!new_account->user_name) {
799                 if(old_account->user_name)
800                         new_account->user_name = _account_dup_text(old_account->user_name);
801         }
802
803         // display name
804         if(!new_account->display_name) {
805                 if(old_account->display_name)
806                         new_account->display_name = _account_dup_text(old_account->display_name);
807         }
808
809         // email address
810         if(!new_account->email_address) {
811                 if(old_account->email_address)
812                         new_account->email_address = _account_dup_text(old_account->email_address);
813         }
814
815         // domain name
816         if(!new_account->domain_name) {
817                 if(old_account->domain_name)
818                         new_account->domain_name = _account_dup_text(old_account->domain_name);
819         }
820
821         // icon path
822         if(!new_account->icon_path) {
823                 if(old_account->icon_path)
824                         new_account->icon_path = _account_dup_text(old_account->icon_path);
825         }
826
827         // source
828         if(!new_account->source) {
829                 if(old_account->source)
830                         new_account->source = _account_dup_text(old_account->source);
831         }
832
833         _ACCOUNT_FREE(new_account->package_name);
834         new_account->package_name = _account_dup_text(old_account->package_name);
835
836         // access token
837         if(!new_account->access_token) {
838                 if(old_account->access_token)
839                         new_account->access_token = _account_dup_text(old_account->access_token);
840         }
841
842         // auth type
843         if(new_account->auth_type == _ACCOUNT_AUTH_TYPE_INVALID) {
844                 new_account->auth_type = old_account->auth_type;
845         }
846
847         //secret
848         if(new_account->secret== _ACCOUNT_SECRECY_INVALID) {
849                 new_account->secret = old_account->secret;
850         }
851
852         // sync support
853         if(new_account->sync_support == _ACCOUNT_SYNC_INVALID) {
854                 new_account->sync_support = old_account->sync_support;
855         }
856
857         // TODO user text
858         int i;
859         for(i=0;i<USER_TXT_CNT;i++) {
860                 if(!new_account->user_data_txt[i]) {
861                         if(old_account->user_data_txt[i])
862                                 new_account->user_data_txt[i] = _account_dup_text(old_account->user_data_txt[i]);
863                 }
864         }
865
866         // TODO user int
867         for(i=0;i<USER_INT_CNT;i++) {
868                 if(new_account->user_data_int[i] == 0) {
869                                 new_account->user_data_int[i] = old_account->user_data_int[i];
870                 }
871         }
872
873         // capability
874
875         // user custom table
876
877 CATCH:
878         if (old_account) {
879                 _account_free_account_with_items(old_account);
880         }
881
882         if (hstmt != NULL) {
883                 rc = _account_query_finalize(hstmt);
884                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
885                 hstmt = NULL;
886         }
887
888         return _ACCOUNT_ERROR_NONE;
889 }
890
891
892
893 static int _account_update_account_by_user_name(int pid, uid_t uid, account_s *account, const char *user_name, const char *package_name)
894 {
895         int                             rc = 0, binding_count = 0, count = 0;
896         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
897         int                             error_code = _ACCOUNT_ERROR_NONE;
898         account_stmt    hstmt = NULL;
899
900         ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is NULL.\n"));
901         ACCOUNT_RETURN_VAL((package_name!= NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is NULL.\n"));
902
903         char* current_appid = NULL;
904         char* verified_appid = NULL;
905
906         current_appid = _account_get_current_appid(pid, uid);
907         error_code = _account_get_represented_appid_from_db(g_hAccountDB, g_hAccountGlobalDB, current_appid, uid, &verified_appid);
908
909         _ACCOUNT_FREE(current_appid);
910         _ACCOUNT_FREE(verified_appid);
911
912         if(error_code != _ACCOUNT_ERROR_NONE){
913                 ACCOUNT_ERROR("No permission to update\n");
914                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
915         }
916
917         error_code = encrypt_access_token(account);
918         if (error_code != _ACCOUNT_ERROR_NONE)
919         {
920                 _ERR("_encrypt_access_token error");
921                 return error_code;
922         }
923
924         _account_compare_old_record_by_user_name(account, user_name, package_name);
925
926         if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
927                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
928                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
929         }
930
931         if (!account->package_name) {
932                 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
933                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
934         }
935
936         if (!account->user_name && !account->display_name && !account->email_address) {
937                 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
938                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
939         }
940
941         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE user_name='%s' and package_name='%s'"
942                         , ACCOUNT_TABLE, user_name, package_name);
943
944         count = _account_get_record_count(g_hAccountDB, query);
945
946         if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
947                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
948                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
949         }
950
951         if (count <= 0) {
952                 ACCOUNT_SLOGI("_account_update_account_by_user_name : The account not exist!, count = %d, user_name=%s, package_name=%s\n",
953                         count, user_name, package_name);
954                 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
955         }
956
957         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
958
959         //TODO: Is it required to update id ? As of now I can only think of falied rollback cases (between account and gSSO DB)
960         ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
961                         "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
962                         "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
963                         "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE user_name=? and package_name=? ", ACCOUNT_TABLE);
964
965         hstmt = _account_prepare_query(g_hAccountDB, query);
966         if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
967                 _account_end_transaction(g_hAccountDB, FALSE);
968                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
969                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
970         }
971         ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
972
973         binding_count = _account_convert_account_to_sql(account, hstmt, query);
974
975         _account_query_bind_text(hstmt, binding_count++, user_name);
976         _account_query_bind_text(hstmt, binding_count++, package_name);
977         rc = _account_query_step(hstmt);
978         if (rc != SQLITE_DONE) {
979                 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
980         }
981         rc = _account_query_finalize(hstmt);
982         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
983         hstmt = NULL;
984
985         /*update capability*/
986         error_code = _account_update_capability_by_user_name(account, user_name, package_name);
987
988         /* update custom */
989         error_code = _account_update_custom(account, account->id);
990
991         return error_code;
992 }
993
994 int _account_insert_to_db(account_s* account, int pid, uid_t uid, int *account_id)
995 {
996         _INFO("");
997         int             error_code = _ACCOUNT_ERROR_NONE;
998         int     ret_transaction = 0;
999
1000         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1001         ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1002         ACCOUNT_RETURN_VAL((account_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID POINTER IS NULL"));
1003
1004         if (!account->user_name && !account->display_name && !account->email_address) {
1005                 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1006                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1007         }
1008
1009         account_s *data = (account_s*)account;
1010         ACCOUNT_SLOGD("(%s)-(%d) account_insert_to_db: begin_transaction.\n", __FUNCTION__, __LINE__);
1011
1012         pthread_mutex_lock(&account_mutex);
1013
1014         /* transaction control required*/
1015         ret_transaction = _account_begin_transaction(g_hAccountDB);
1016
1017         if(_account_db_err_code(g_hAccountDB) == SQLITE_PERM){
1018                 pthread_mutex_unlock(&account_mutex);
1019                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1020                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1021         }
1022
1023         if (ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY) {
1024                 ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction);
1025                 pthread_mutex_unlock(&account_mutex);
1026                 return _ACCOUNT_ERROR_DATABASE_BUSY;
1027         }else if (ret_transaction != _ACCOUNT_ERROR_NONE) {
1028                 ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction);
1029                 pthread_mutex_unlock(&account_mutex);
1030                 return ret_transaction;
1031         }
1032
1033         *account_id = _account_get_next_sequence(g_hAccountDB, ACCOUNT_TABLE);
1034         data->id = *account_id;
1035
1036         char* appid = NULL;
1037         appid = _account_get_current_appid(pid, uid);
1038
1039         if(!appid)
1040         {
1041                 _INFO("");
1042                 // API caller cannot be recognized
1043                 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1044                 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1045                 pthread_mutex_unlock(&account_mutex);
1046                 return _ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
1047         }
1048
1049         _INFO("");
1050         char* verified_appid = NULL;
1051         error_code  = _account_get_represented_appid_from_db(g_hAccountDB, g_hAccountGlobalDB, appid, uid, &verified_appid);//FIX
1052         _ACCOUNT_FREE(appid);
1053         if(error_code != _ACCOUNT_ERROR_NONE)
1054         {
1055                 _ERR("error_code = %d", error_code);
1056                 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1057                 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1058                 _ACCOUNT_FREE(verified_appid);
1059                 pthread_mutex_unlock(&account_mutex);
1060                 return error_code;
1061         }
1062
1063         if(verified_appid)
1064         {
1065                 _INFO("");
1066                 error_code = _account_check_duplicated(g_hAccountDB, data, verified_appid, uid);
1067                 if (error_code != _ACCOUNT_ERROR_NONE) {
1068                         _INFO("");
1069                         ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1070                         ACCOUNT_DEBUG("_account_check_duplicated(), rollback insert query(%x)!!!!\n", ret_transaction);
1071                         *account_id = -1;
1072                         pthread_mutex_unlock(&account_mutex);
1073                         return error_code;
1074                 }
1075                 if(!_account_check_add_more_account(verified_appid)) {
1076                         ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1077                         ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
1078                         pthread_mutex_unlock(&account_mutex);
1079                         _ACCOUNT_FREE(verified_appid);
1080                         return _ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
1081                 }
1082
1083                 _ACCOUNT_FREE(data->package_name);
1084                 data->package_name = _account_dup_text(verified_appid);
1085                 _ACCOUNT_FREE(verified_appid);
1086         }
1087
1088         if(!_account_check_add_more_account(data->package_name))
1089         {
1090                 _INFO("");
1091                 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1092                 ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
1093                 pthread_mutex_unlock(&account_mutex);
1094                 return _ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
1095         }
1096
1097         error_code = encrypt_access_token(data);
1098         if (error_code != _ACCOUNT_ERROR_NONE)
1099         {
1100                 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1101                 ACCOUNT_ERROR("encrypt_access_token fail, rollback insert query(%x)!!!!\n", ret_transaction);
1102                 *account_id = -1;
1103                 pthread_mutex_unlock(&account_mutex);
1104                 return error_code;
1105         }
1106
1107         error_code = _account_execute_insert_query(data);
1108
1109         if (error_code != _ACCOUNT_ERROR_NONE)
1110         {
1111                 _INFO("");
1112                 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1113                 ACCOUNT_ERROR("INSERT account fail, rollback insert query(%x)!!!!\n", ret_transaction);
1114                 *account_id = -1;
1115                 pthread_mutex_unlock(&account_mutex);
1116                 return error_code;
1117         }
1118
1119         _INFO("");
1120         error_code = _account_insert_capability(data, *account_id);
1121         if (error_code != _ACCOUNT_ERROR_NONE)
1122         {
1123                 _INFO("");
1124                 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1125                 ACCOUNT_ERROR("INSERT capability fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1126                 *account_id = -1;
1127                 pthread_mutex_unlock(&account_mutex);
1128                 return error_code;
1129         }
1130
1131         _INFO("");
1132         error_code = _account_insert_custom(data, *account_id);
1133         if (error_code != _ACCOUNT_ERROR_NONE)
1134         {
1135                 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1136                 ACCOUNT_ERROR("INSERT custom fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1137                 *account_id = -1;
1138                 pthread_mutex_unlock(&account_mutex);
1139                 return error_code;
1140         }
1141
1142         _INFO("");
1143
1144         pthread_mutex_unlock(&account_mutex);
1145         _account_end_transaction(g_hAccountDB, TRUE);
1146         ACCOUNT_SLOGD("(%s)-(%d) account _end_transaction.\n", __FUNCTION__, __LINE__);
1147
1148         char buf[64]={0,};
1149         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_INSERT, *account_id);
1150         _account_insert_delete_update_notification_send(buf);
1151         _INFO("account _notification_send end.");
1152
1153         return _ACCOUNT_ERROR_NONE;
1154
1155 }
1156
1157 GSList* _account_get_capability_list_by_account_id(int account_id, int *error_code)
1158 {
1159         *error_code = _ACCOUNT_ERROR_NONE;
1160         account_stmt    hstmt = NULL;
1161         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1162         int                     rc = 0;
1163         GSList* capability_list = NULL;
1164
1165         ACCOUNT_RETURN_VAL((account_id > 0), {*error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("ACCOUNT INDEX IS LESS THAN 0"));
1166         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
1167
1168         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1169
1170         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
1171         hstmt = _account_prepare_query(g_hAccountDB, query);
1172
1173         if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
1174                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1175                 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
1176                 return NULL;
1177         }
1178
1179         rc = _account_query_step(hstmt);
1180         ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1181
1182         account_capability_s* capability_record = NULL;
1183
1184         while (rc == SQLITE_ROW) {
1185                 capability_record = (account_capability_s*) malloc(sizeof(account_capability_s));
1186
1187                 if (capability_record == NULL) {
1188                         ACCOUNT_FATAL("malloc Failed");
1189                         break;
1190                 }
1191
1192                 ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s));
1193
1194                 _account_convert_column_to_capability(hstmt, capability_record);
1195
1196                 //cb_ret = callback(capability_record->type, capability_record->value, user_data);
1197
1198                 //_account_free_capability_items(capability_record);
1199                 //_ACCOUNT_FREE(capability_record);
1200
1201                 //ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
1202
1203                 capability_list = g_slist_append(capability_list, capability_record);
1204                 rc = _account_query_step(hstmt);
1205         }
1206
1207         rc = _account_query_finalize(hstmt);
1208         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
1209         hstmt = NULL;
1210
1211         *error_code = _ACCOUNT_ERROR_NONE;
1212
1213 CATCH:
1214         if (hstmt != NULL)
1215         {
1216                 rc = _account_query_finalize(hstmt);
1217                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
1218                 hstmt = NULL;
1219         }
1220
1221         pthread_mutex_unlock(&account_mutex);
1222         return capability_list;
1223 }
1224
1225 static int _account_compare_old_record(account_s *new_account, int account_id)
1226 {
1227         int                             error_code = _ACCOUNT_ERROR_NONE;
1228         account_stmt    hstmt = NULL;
1229         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1230         int                             rc = 0;
1231         account_s *old_account = NULL;
1232
1233         ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1234         ACCOUNT_RETURN_VAL((new_account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
1235         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1236
1237         old_account = (account_s*)calloc(1, sizeof(account_s));
1238         if (old_account == NULL) {
1239                 _ERR("Out of Memory");
1240                 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
1241         }
1242
1243         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1244
1245         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
1246         hstmt = _account_prepare_query(g_hAccountDB, query);
1247
1248         rc = _account_query_step(hstmt);
1249         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1250
1251         while (rc == SQLITE_ROW) {
1252                 _account_convert_column_to_account(hstmt, old_account);
1253                 rc = _account_query_step(hstmt);
1254         }
1255
1256         rc = _account_query_finalize(hstmt);
1257         ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1258         hstmt = NULL;
1259
1260         // get capability
1261         error_code = _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, old_account->id, (void*)old_account);
1262         ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
1263
1264         // get custom text
1265         error_code = _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, old_account->id, (void*)old_account);
1266         ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
1267
1268         // compare
1269
1270         new_account->id = old_account->id;
1271
1272         //user name
1273         if(!new_account->user_name) {
1274                 if(old_account->user_name)
1275                         new_account->user_name = _account_dup_text(old_account->user_name);
1276         }
1277
1278         // display name
1279         if(!new_account->display_name) {
1280                 if(old_account->display_name)
1281                         new_account->display_name = _account_dup_text(old_account->display_name);
1282         }
1283
1284         // email address
1285         if(!new_account->email_address) {
1286                 if(old_account->email_address)
1287                         new_account->email_address = _account_dup_text(old_account->email_address);
1288         }
1289
1290         // domain name
1291         if(!new_account->domain_name) {
1292                 if(old_account->domain_name)
1293                         new_account->domain_name = _account_dup_text(old_account->domain_name);
1294         }
1295
1296         // icon path
1297         if(!new_account->icon_path) {
1298                 if(old_account->icon_path)
1299                         new_account->icon_path = _account_dup_text(old_account->icon_path);
1300         }
1301
1302         // source
1303         if(!new_account->source) {
1304                 if(old_account->source)
1305                         new_account->source = _account_dup_text(old_account->source);
1306         }
1307
1308         _ACCOUNT_FREE(new_account->package_name);
1309         new_account->package_name = _account_dup_text(old_account->package_name);
1310
1311         // access token
1312         if(!new_account->access_token) {
1313                 if(old_account->access_token)
1314                         new_account->access_token = _account_dup_text(old_account->access_token);
1315         }
1316
1317         // user text
1318         int i;
1319         for(i=0;i<USER_TXT_CNT;i++) {
1320                 if(!new_account->user_data_txt[i]) {
1321                         if(old_account->user_data_txt[i])
1322                                 new_account->user_data_txt[i] = _account_dup_text(old_account->user_data_txt[i]);
1323                 }
1324         }
1325
1326         // auth type
1327         if(new_account->auth_type == _ACCOUNT_AUTH_TYPE_INVALID) {
1328                 new_account->auth_type = old_account->auth_type;
1329         }
1330
1331         //secret
1332         if(new_account->secret== _ACCOUNT_SECRECY_INVALID) {
1333                 new_account->secret = old_account->secret;
1334         }
1335
1336         // sync support
1337         if(new_account->sync_support == _ACCOUNT_SYNC_INVALID) {
1338                 new_account->sync_support = old_account->sync_support;
1339         }
1340
1341         // user int
1342         for(i=0;i<USER_INT_CNT;i++) {
1343                 if(new_account->user_data_int[i] == 0) {
1344                                 new_account->user_data_int[i] = old_account->user_data_int[i];
1345                 }
1346         }
1347
1348         // capability
1349
1350         // user custom table
1351
1352 CATCH:
1353                 if (old_account)
1354                         _account_free_account_with_items(old_account);
1355
1356                 if (hstmt != NULL) {
1357                         rc = _account_query_finalize(hstmt);
1358                         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1359                         hstmt = NULL;
1360                 }
1361
1362         return _ACCOUNT_ERROR_NONE;
1363 }
1364
1365 static int _account_get_package_name_from_account_id(int account_id, char **package_name)
1366 {
1367         int                             error_code = _ACCOUNT_ERROR_NONE;
1368         account_stmt    hstmt = NULL;
1369         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1370         int                             rc = 0;
1371         account_s *old_account = NULL;
1372
1373         ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1374         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1375
1376         old_account = (account_s*)calloc(1, sizeof(account_s));
1377         if (old_account == NULL) {
1378                 _ERR("Out Of memory");
1379                 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
1380         }
1381
1382         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1383
1384         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
1385         hstmt = _account_prepare_query(g_hAccountDB, query);
1386
1387         rc = _account_query_step(hstmt);
1388         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1389
1390         while (rc == SQLITE_ROW) {
1391                 _account_convert_column_to_account(hstmt, old_account);
1392                 rc = _account_query_step(hstmt);
1393         }
1394
1395         rc = _account_query_finalize(hstmt);
1396         ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1397         hstmt = NULL;
1398
1399         // get package name.
1400         *package_name = _account_dup_text(old_account->package_name);
1401
1402
1403         CATCH:
1404                 if (old_account) {
1405                         _account_free_account_with_items(old_account);
1406                 }
1407
1408                 if (hstmt != NULL) {
1409                         rc = _account_query_finalize(hstmt);
1410                         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1411                         hstmt = NULL;
1412                 }
1413
1414         return error_code;
1415
1416 }
1417
1418 static int _account_update_account(int pid, uid_t uid, account_s *account, int account_id)
1419 {
1420         int                             rc = 0, binding_count =0;
1421         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1422         int                             error_code = _ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0;
1423         account_stmt    hstmt = NULL;
1424
1425         if (!account->package_name) {
1426                 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
1427                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1428         }
1429
1430         /* Check permission of requested appid */
1431         char* current_appid = NULL;
1432         char *package_name = NULL;
1433
1434         current_appid = _account_get_current_appid(pid, uid);
1435         error_code = _account_get_package_name_from_account_id(account_id, &package_name);
1436
1437         if(error_code != _ACCOUNT_ERROR_NONE || package_name == NULL){
1438                 ACCOUNT_ERROR("No package name with account_id\n");
1439                 _ACCOUNT_FREE(current_appid);
1440                 _ACCOUNT_FREE(package_name);
1441                 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1442         }
1443
1444         error_code = _account_check_appid_group_with_package_name(current_appid, package_name, uid);
1445         ACCOUNT_DEBUG( "UPDATE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name);     // TODO: remove the log later.
1446
1447         _ACCOUNT_FREE(current_appid);
1448         _ACCOUNT_FREE(package_name);
1449
1450         if(error_code != _ACCOUNT_ERROR_NONE){
1451                 ACCOUNT_ERROR("No permission to update\n");
1452                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1453         }
1454
1455         error_code = encrypt_access_token(account);
1456         if (error_code != _ACCOUNT_ERROR_NONE)
1457         {
1458                 _ERR("_encrypt_access_token error");
1459                 return error_code;
1460         }
1461
1462         error_code = _account_compare_old_record(account, account_id);
1463         if (error_code != _ACCOUNT_ERROR_NONE) {
1464                 ACCOUNT_ERROR("_account_compare_old_record fail\n");
1465                 return error_code;
1466         }
1467
1468         if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
1469                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1470                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1471         } else if( _account_db_err_code(g_hAccountDB) == SQLITE_BUSY ){
1472                 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg(g_hAccountDB));
1473                 return _ACCOUNT_ERROR_DATABASE_BUSY;
1474         }
1475
1476         if (!account->user_name && !account->display_name && !account->email_address) {
1477                 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1478                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1479         }
1480
1481         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1482
1483         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
1484
1485         count = _account_get_record_count(g_hAccountDB, query);
1486         if (count <= 0) {
1487                 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
1488                 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1489         }
1490
1491         /* transaction control required*/
1492         ret_transaction = _account_begin_transaction(g_hAccountDB);
1493         if( ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY ){
1494                 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg(g_hAccountDB));
1495                 pthread_mutex_unlock(&account_mutex);
1496                 return _ACCOUNT_ERROR_DATABASE_BUSY;
1497         }
1498
1499         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1500         ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
1501                         "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
1502                         "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
1503                         "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
1504
1505         hstmt = _account_prepare_query(g_hAccountDB, query);
1506
1507         if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
1508                 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1509                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1510                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1511         }
1512
1513         ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s)(%x).\n", _account_db_err_msg(g_hAccountDB), _account_end_transaction(g_hAccountDB, FALSE)));
1514
1515         binding_count = _account_convert_account_to_sql(account, hstmt, query);
1516         _account_query_bind_int(hstmt, binding_count++, account_id);
1517
1518         rc = _account_query_step(hstmt);
1519         if (rc != SQLITE_DONE) {
1520                 ACCOUNT_SLOGE( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
1521         }
1522
1523         rc = _account_query_finalize(hstmt);
1524         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1525         hstmt = NULL;
1526
1527         _INFO("update query=%s", query);
1528
1529         /*update capability*/
1530         error_code = _account_update_capability(account, account_id);
1531         if(error_code != _ACCOUNT_ERROR_NONE && error_code!= _ACCOUNT_ERROR_RECORD_NOT_FOUND){
1532                 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1533                 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1534                 return error_code;
1535         }
1536
1537         /* update custom */
1538         error_code = _account_update_custom(account, account_id);
1539         if(error_code != _ACCOUNT_ERROR_NONE && error_code!= _ACCOUNT_ERROR_RECORD_NOT_FOUND){
1540                 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1541                 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1542                 return error_code;
1543         }
1544
1545         ret_transaction = _account_end_transaction(g_hAccountDB, TRUE);
1546
1547         _INFO("update end");
1548         return error_code;
1549 }
1550
1551
1552 static int _account_update_account_ex(account_s *account, int account_id)
1553 {
1554         int                             rc = 0, binding_count =0;
1555         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1556         int                             error_code = _ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0;
1557         account_stmt    hstmt = NULL;
1558
1559         if (!account->package_name) {
1560                 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
1561                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1562         }
1563
1564         error_code = encrypt_access_token(account);
1565         if (error_code != _ACCOUNT_ERROR_NONE)
1566         {
1567                 _ERR("_encrypt_access_token error");
1568                 return error_code;
1569         }
1570
1571         error_code = _account_compare_old_record(account, account_id);
1572         if (error_code != _ACCOUNT_ERROR_NONE) {
1573                 ACCOUNT_ERROR("_account_compare_old_record fail\n");
1574                 return error_code;
1575         }
1576
1577         if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
1578                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1579                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1580         }
1581
1582         if (!account->user_name && !account->display_name && !account->email_address) {
1583                 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1584                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1585         }
1586
1587         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1588
1589         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
1590
1591         count = _account_get_record_count(g_hAccountDB, query);
1592         if (count <= 0) {
1593                 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
1594                 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1595         }
1596
1597         /* transaction control required*/
1598         ret_transaction = _account_begin_transaction(g_hAccountDB);
1599         if( ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY ){
1600                 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg(g_hAccountDB));
1601                 pthread_mutex_unlock(&account_mutex);
1602                 return _ACCOUNT_ERROR_DATABASE_BUSY;
1603         }
1604
1605         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1606         ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
1607                         "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
1608                         "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
1609                         "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
1610
1611         hstmt = _account_prepare_query(g_hAccountDB, query);
1612
1613         if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
1614                 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1615                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1616                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1617         }
1618
1619         ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s)(%x).\n", _account_db_err_msg(g_hAccountDB), _account_end_transaction(g_hAccountDB, FALSE)));
1620
1621         _INFO("account_update_to_db_by_id_ex_p : before convert() : account_id[%d], user_name=%s", account->id, account->user_name);
1622         binding_count = _account_convert_account_to_sql(account, hstmt, query);
1623         _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], user_name=%s", account->id, account->user_name);
1624         _INFO("account_update_to_db_by_id_ex_p : before bind()");
1625         rc = _account_query_bind_int(hstmt, binding_count++, account_id);
1626         _INFO("account_update_to_db_by_id_ex_p : after bind() : ret = %d", rc);
1627
1628         rc = _account_query_step(hstmt);
1629         if (rc != SQLITE_DONE) {
1630                 ACCOUNT_SLOGE( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
1631         }
1632         _INFO("account_update_to_db_by_id_ex_p : after query_step() : ret = %d", rc);
1633
1634         rc = _account_query_finalize(hstmt);
1635         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1636         hstmt = NULL;
1637         _INFO("account_update_to_db_by_id_ex_p : after query_filnalize() : ret = %d", rc);
1638
1639         _INFO("account_update_to_db_by_id_ex_p : before update_capability()");
1640         /*update capability*/
1641         error_code = _account_update_capability(account, account_id);
1642         if(error_code != _ACCOUNT_ERROR_NONE && error_code!= _ACCOUNT_ERROR_RECORD_NOT_FOUND){
1643                 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1644                 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1645                 return error_code;
1646         }
1647         _INFO("account_update_to_db_by_id_ex_p : after update_capability()");
1648
1649         _INFO("account_update_to_db_by_id_ex_p : before update_custom()");
1650         /* update custom */
1651         error_code = _account_update_custom(account, account_id);
1652         if(error_code != _ACCOUNT_ERROR_NONE && error_code!= _ACCOUNT_ERROR_RECORD_NOT_FOUND){
1653                 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1654                 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1655                 return error_code;
1656         }
1657         _INFO("account_update_to_db_by_id_ex_p : after update_custom()");
1658
1659         ret_transaction = _account_end_transaction(g_hAccountDB, TRUE);
1660
1661         return error_code;
1662 }
1663
1664
1665 int _account_update_to_db_by_id(int pid, uid_t uid, account_s* account, int account_id)
1666 {
1667         ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
1668         ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
1669         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1670         int     error_code = _ACCOUNT_ERROR_NONE;
1671         account_s* data = (account_s*)account;
1672
1673         pthread_mutex_lock(&account_mutex);
1674
1675         error_code = _account_update_account(pid, uid, data, account_id);
1676
1677         if(error_code != _ACCOUNT_ERROR_NONE) {
1678                 pthread_mutex_unlock(&account_mutex);
1679                 return error_code;
1680         }
1681
1682         pthread_mutex_unlock(&account_mutex);
1683
1684         char buf[64]={0,};
1685         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_UPDATE, account_id);
1686         _account_insert_delete_update_notification_send(buf);
1687
1688         return _ACCOUNT_ERROR_NONE;
1689 }
1690
1691 int _account_update_to_db_by_id_ex(account_s* account, int account_id)
1692 {
1693         ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
1694         ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
1695         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1696         int     error_code = _ACCOUNT_ERROR_NONE;
1697         account_s* data = account;
1698
1699         pthread_mutex_lock(&account_mutex);
1700
1701         _INFO("before update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name);
1702         error_code = _account_update_account_ex(data, account_id);
1703         _INFO("after update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name);
1704
1705         if(error_code != _ACCOUNT_ERROR_NONE) {
1706                 pthread_mutex_unlock(&account_mutex);
1707                 return error_code;
1708         }
1709
1710         pthread_mutex_unlock(&account_mutex);
1711
1712         char buf[64]={0,};
1713         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_UPDATE, account_id);
1714         _account_insert_delete_update_notification_send(buf);
1715
1716         return _ACCOUNT_ERROR_NONE;
1717 }
1718
1719
1720 int _account_update_to_db_by_user_name(int pid, uid_t uid, account_s* account, const char *user_name, const char *package_name)
1721 {
1722         ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
1723         ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
1724         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1725
1726         int     error_code = _ACCOUNT_ERROR_NONE;
1727         account_s *data = (account_s*)account;
1728
1729         pthread_mutex_lock(&account_mutex);
1730
1731         error_code = _account_update_account_by_user_name(pid, uid, data, user_name, package_name);
1732
1733         pthread_mutex_unlock(&account_mutex);
1734
1735         char buf[64]={0,};
1736         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_UPDATE, data->id);
1737         _account_insert_delete_update_notification_send(buf);
1738
1739         return error_code;
1740 }
1741
1742 GSList* _account_db_query_all(int pid, uid_t uid)
1743 {
1744         //int                   error_code = _ACCOUNT_ERROR_NONE;
1745         account_stmt    hstmt = NULL;
1746         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1747         int                     rc = 0;
1748         GSList                  *account_list = NULL;
1749
1750         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected."));
1751
1752         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1753
1754         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TABLE);
1755         hstmt = _account_prepare_query(g_hAccountDB, query);
1756
1757         if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
1758                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1759                 return NULL;
1760         }
1761
1762         rc = _account_query_step(hstmt);
1763
1764         account_s *account_record = NULL;
1765
1766         if (rc != SQLITE_ROW)
1767         {
1768                 _ERR("The record isn't found");
1769                 goto CATCH;
1770         }
1771
1772         while(rc == SQLITE_ROW) {
1773                 account_record = (account_s*) malloc(sizeof(account_s));
1774
1775                 if (account_record == NULL) {
1776                         ACCOUNT_FATAL("malloc Failed");
1777                         break;
1778                 }
1779
1780                 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
1781                 _account_convert_column_to_account(hstmt, account_record);
1782                 account_list = g_slist_append(account_list, account_record);
1783                 rc = _account_query_step(hstmt);
1784         }
1785
1786         rc = _account_query_finalize(hstmt);
1787         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, NULL, ("finalize error"));
1788         hstmt = NULL;
1789
1790         GSList* iter;
1791
1792         for (iter = account_list; iter != NULL; iter = g_slist_next(iter)) {
1793                 account_s *account = NULL;
1794                 account = (account_s*)iter->data;
1795                 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, account->id, (void*)account);
1796                 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, account->id, (void*)account);
1797         }
1798
1799 CATCH:
1800         if (hstmt != NULL) {
1801                 rc = _account_query_finalize(hstmt);
1802                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_gslist_account_free(account_list);}, NULL, ("finalize error"));
1803                 hstmt = NULL;
1804         }
1805         if (account_list)
1806         {
1807                 _remove_sensitive_info_from_non_owning_account_slist(account_list, pid, uid);
1808         }
1809         return account_list;
1810 }
1811
1812 int _account_update_sync_status_by_id(uid_t uid, int account_db_id, const int sync_status)
1813 {
1814         int                             error_code = _ACCOUNT_ERROR_NONE;
1815         account_stmt    hstmt = NULL;
1816         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1817         int                             rc = 0;
1818         int count =1;
1819
1820         ACCOUNT_RETURN_VAL((account_db_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1821         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1822         if ( (sync_status < 0) || (sync_status >= _ACCOUNT_SYNC_MAX)) {
1823                 ACCOUNT_SLOGE("(%s)-(%d) sync_status is less than 0 or more than enum max.\n", __FUNCTION__, __LINE__);
1824                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1825         }
1826
1827         pthread_mutex_lock(&account_mutex);
1828
1829         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1830
1831         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_db_id);
1832
1833         rc = _account_get_record_count(g_hAccountDB, query);
1834
1835         if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
1836                 pthread_mutex_unlock(&account_mutex);
1837                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1838                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1839         }
1840
1841         if (rc <= 0) {
1842                 ACCOUNT_SLOGE( "account_update_sync_status_by_id : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
1843                 pthread_mutex_unlock(&account_mutex);
1844                 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1845         }
1846
1847         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1848
1849         ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET sync_support=? WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
1850         hstmt = _account_prepare_query(g_hAccountDB, query);
1851
1852         _account_query_bind_int(hstmt, count, sync_status);
1853
1854         rc = _account_query_step(hstmt);
1855
1856         if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
1857                 pthread_mutex_unlock(&account_mutex);
1858                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1859                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1860         }
1861
1862         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_DB_FAILED,
1863                                 ("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB)));
1864
1865         rc = _account_query_finalize(hstmt);
1866         if (rc != _ACCOUNT_ERROR_NONE) {
1867                 ACCOUNT_ERROR("_account_query_finalize error");
1868                 pthread_mutex_unlock(&account_mutex);
1869                 return rc;
1870         }
1871         char buf[64]={0,};
1872         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_SYNC_UPDATE, account_db_id);
1873         _account_insert_delete_update_notification_send(buf);
1874
1875         hstmt = NULL;
1876         error_code = _ACCOUNT_ERROR_NONE;
1877
1878 CATCH:
1879         if (hstmt != NULL) {
1880                 rc = _account_query_finalize(hstmt);
1881                 pthread_mutex_unlock(&account_mutex);
1882                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1883                 hstmt = NULL;
1884         }
1885
1886         pthread_mutex_unlock(&account_mutex);
1887         return error_code;
1888 }
1889
1890 int _account_query_account_by_account_id(int pid, uid_t uid, int account_db_id, account_s *account_record)
1891 {
1892         _INFO("_account_query_account_by_account_id() start, account_db_id=[%d]", account_db_id);
1893
1894         int                             error_code = _ACCOUNT_ERROR_NONE;
1895         account_stmt    hstmt = NULL;
1896         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1897         int                             rc = 0;
1898
1899         ACCOUNT_RETURN_VAL((account_db_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1900         ACCOUNT_RETURN_VAL(account_record != NULL, {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
1901         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1902
1903         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1904
1905         ACCOUNT_DEBUG("starting db operations");
1906
1907         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
1908         hstmt = _account_prepare_query(g_hAccountDB, query);
1909         rc = _account_db_err_code(g_hAccountDB);
1910         _INFO("after _account_prepare_query, rc=[%d]", rc);
1911
1912         if( rc == SQLITE_PERM ){
1913                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1914                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1915         }
1916
1917         ACCOUNT_DEBUG("before _account_query_step");
1918         rc = _account_query_step(hstmt);
1919         ACCOUNT_DEBUG("after _account_query_step returned [%d]", rc);
1920         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1921
1922         while (rc == SQLITE_ROW) {
1923                 ACCOUNT_DEBUG("before _account_convert_column_to_account");
1924                 _account_convert_column_to_account(hstmt, account_record);
1925                 ACCOUNT_DEBUG("after _account_convert_column_to_account");
1926                 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]);
1927                 rc = _account_query_step(hstmt);
1928         }
1929
1930         ACCOUNT_DEBUG("account_record->id=[%d]", account_record->id);
1931
1932         rc = _account_query_finalize(hstmt);
1933         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1934
1935         ACCOUNT_DEBUG("before _account_query_capability_by_account_id");
1936         _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, account_record->id, (void*)account_record);
1937         ACCOUNT_DEBUG("after _account_query_capability_by_account_id");
1938
1939         ACCOUNT_DEBUG("before _account_query_custom_by_account_id");
1940         _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, account_record->id, (void*)account_record);
1941         ACCOUNT_DEBUG("after _account_query_custom_by_account_id");
1942
1943         hstmt = NULL;
1944         error_code = _ACCOUNT_ERROR_NONE;
1945
1946 CATCH:
1947         if (hstmt != NULL) {
1948                 rc = _account_query_finalize(hstmt);
1949                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1950                 hstmt = NULL;
1951         }
1952
1953         if (account_record)
1954         {
1955                 _remove_sensitive_info_from_non_owning_account(account_record, pid, uid);
1956         }
1957         pthread_mutex_unlock(&account_mutex);
1958         ACCOUNT_DEBUG("_account_query_account_by_account_id end [%d]", error_code);
1959         return error_code;
1960 }
1961
1962 GList* _account_query_account_by_user_name(int pid, uid_t uid, const char *user_name, int *error_code)
1963 {
1964         *error_code = _ACCOUNT_ERROR_NONE;
1965         account_stmt    hstmt = NULL;
1966         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1967         int                             rc = 0;
1968         account_s *account_head = NULL;
1969
1970         if (user_name == NULL)
1971         {
1972                 _ERR("USER NAME IS NULL");
1973                 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
1974                 goto CATCH;
1975         }
1976
1977         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1978
1979         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ?", ACCOUNT_TABLE);
1980
1981         hstmt = _account_prepare_query(g_hAccountDB, query);
1982
1983         if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM)
1984         {
1985                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1986                 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
1987                 goto CATCH;
1988         }
1989
1990         int binding_count = 1;
1991         _account_query_bind_text(hstmt, binding_count++, user_name);
1992
1993         rc = _account_query_step(hstmt);
1994
1995         if (rc != SQLITE_ROW)
1996         {
1997                 _ERR("The record isn't found");
1998                 *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1999                 goto CATCH;
2000         }
2001
2002         int tmp = 0;
2003
2004         account_head = (account_s*) malloc(sizeof(account_s));
2005         if (account_head == NULL) {
2006                 ACCOUNT_FATAL("malloc Failed");
2007                 if (hstmt != NULL) {
2008                         rc = _account_query_finalize(hstmt);
2009
2010                         if (rc != _ACCOUNT_ERROR_NONE)
2011                         {
2012                                 _ERR("finalize error");
2013                                 *error_code = rc;
2014                                 goto CATCH;
2015                         }
2016                         hstmt = NULL;
2017                 }
2018                 *error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
2019                 goto CATCH;
2020         }
2021         ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2022
2023         while (rc == SQLITE_ROW) {
2024                 account_s* account_record = NULL;
2025
2026                 account_record = (account_s*) malloc(sizeof(account_s));
2027
2028                 if (account_record == NULL) {
2029                         ACCOUNT_FATAL("malloc Failed");
2030                         break;
2031                 }
2032                 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2033
2034                 _account_convert_column_to_account(hstmt, account_record);
2035
2036                 account_head->account_list = g_list_append(account_head->account_list, account_record);
2037
2038                 rc = _account_query_step(hstmt);
2039                 tmp++;
2040         }
2041
2042         rc = _account_query_finalize(hstmt);
2043
2044         if (rc != _ACCOUNT_ERROR_NONE)
2045         {
2046                 _ERR("finalize error");
2047                 *error_code = rc;
2048                 goto CATCH;
2049         }
2050
2051         hstmt = NULL;
2052
2053         GList *iter;
2054
2055
2056         tmp = g_list_length(account_head->account_list);
2057
2058         for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2059 //              account_h account;
2060 //              account = (account_h)iter->data;
2061
2062                 account_s *testaccount = (account_s*)iter->data;
2063
2064                 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, testaccount->id, (void*)testaccount);
2065                 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, testaccount->id, (void*)testaccount);
2066
2067         }
2068
2069         *error_code = _ACCOUNT_ERROR_NONE;
2070
2071 CATCH:
2072         if (hstmt != NULL) {
2073                 rc = _account_query_finalize(hstmt);
2074                 if (rc != _ACCOUNT_ERROR_NONE)
2075                 {
2076                         _ERR("finalize error");
2077                         *error_code = rc;
2078                 }
2079                 hstmt = NULL;
2080         }
2081
2082         pthread_mutex_unlock(&account_mutex);
2083         if (account_head)
2084         {
2085                 _remove_sensitive_info_from_non_owning_account_list(account_head->account_list, pid, uid);
2086                 GList* result = account_head->account_list;
2087                 _ACCOUNT_FREE(account_head);
2088                 return result;
2089         }
2090         return NULL;
2091 }
2092
2093 GList*
2094 _account_query_account_by_capability(int pid, uid_t uid, const char* capability_type, const int capability_value, int *error_code)
2095 {
2096         *error_code = _ACCOUNT_ERROR_NONE;
2097         account_stmt    hstmt = NULL;
2098         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2099         int                     rc = 0;
2100
2101         ACCOUNT_RETURN_VAL((capability_type != NULL), {*error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("capability_type IS NULL"));
2102
2103         if ((capability_value  < 0) || (capability_value >= _ACCOUNT_CAPABILITY_STATE_MAX)) {
2104                 ACCOUNT_SLOGE("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
2105                 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
2106                 return NULL;
2107         }
2108
2109         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
2110
2111         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2112
2113         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=? AND value=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
2114
2115         hstmt = _account_prepare_query(g_hAccountDB, query);
2116
2117         if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
2118                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2119                 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
2120                 return NULL;
2121         }
2122
2123         int binding_count = 1;
2124         _account_query_bind_text(hstmt, binding_count++, capability_type);
2125         _account_query_bind_int(hstmt, binding_count++, capability_value);
2126
2127         rc = _account_query_step(hstmt);
2128
2129         account_s* account_head = NULL;
2130
2131         ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2132
2133         int tmp = 0;
2134
2135         account_head = (account_s*) malloc(sizeof(account_s));
2136         if (account_head == NULL) {
2137                 ACCOUNT_FATAL("malloc Failed");
2138                 if (hstmt != NULL) {
2139                         rc = _account_query_finalize(hstmt);
2140                         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
2141                         hstmt = NULL;
2142                 }
2143                 *error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
2144                 return NULL;
2145         }
2146         ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2147
2148         while (rc == SQLITE_ROW) {
2149                 account_s* account_record = NULL;
2150
2151                 account_record = (account_s*) malloc(sizeof(account_s));
2152
2153                 if (account_record == NULL) {
2154                         ACCOUNT_FATAL("malloc Failed");
2155                         break;
2156                 }
2157                 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2158
2159                 _account_convert_column_to_account(hstmt, account_record);
2160
2161                 account_head->account_list = g_list_append(account_head->account_list, account_record);
2162
2163                 rc = _account_query_step(hstmt);
2164                 tmp++;
2165         }
2166
2167         rc = _account_query_finalize(hstmt);
2168         ACCOUNT_CATCH_ERROR_P((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2169         hstmt = NULL;
2170
2171         GList *iter;
2172
2173
2174         tmp = g_list_length(account_head->account_list);
2175
2176         for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2177 //              account_h account = NULL;
2178 //              account = (account_h)iter->data;
2179                 account_s* testaccount = (account_s*)iter->data;
2180
2181                 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, testaccount->id, (void*)testaccount);
2182                 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, testaccount->id, (void*)testaccount);
2183
2184         }
2185
2186
2187         *error_code = _ACCOUNT_ERROR_NONE;
2188
2189 CATCH:
2190         if (hstmt != NULL) {
2191                 rc = _account_query_finalize(hstmt);
2192                 if ( rc != _ACCOUNT_ERROR_NONE ) {
2193                         *error_code = rc;
2194                         _ERR("finalize error");
2195                 }
2196                 hstmt = NULL;
2197         }
2198
2199         if( *error_code != _ACCOUNT_ERROR_NONE && account_head ) {
2200                 _account_glist_account_free(account_head->account_list);
2201                 _ACCOUNT_FREE(account_head);
2202                 account_head = NULL;
2203         }
2204
2205         pthread_mutex_unlock(&account_mutex);
2206
2207         if (account_head)
2208         {
2209                 _remove_sensitive_info_from_non_owning_account_list(account_head->account_list, pid, uid);
2210                 GList* result = account_head->account_list;
2211                 _ACCOUNT_FREE(account_head);
2212                 return result;
2213         }
2214         return NULL;
2215 }
2216
2217 GList* _account_query_account_by_capability_type(int pid, uid_t uid, const char* capability_type, int *error_code)
2218 {
2219         *error_code = _ACCOUNT_ERROR_NONE;
2220         account_stmt    hstmt = NULL;
2221         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2222         int                     rc = 0;
2223
2224         ACCOUNT_RETURN_VAL((capability_type != NULL), {*error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("capability_type IS NULL"));
2225         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;},
2226                                            NULL, ("The database isn't connected."));
2227
2228         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2229
2230         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
2231
2232         hstmt = _account_prepare_query(g_hAccountDB, query);
2233
2234         if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
2235                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2236                 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
2237                 return NULL;
2238         }
2239
2240         int binding_count = 1;
2241         _account_query_bind_text(hstmt, binding_count++, capability_type);
2242
2243         rc = _account_query_step(hstmt);
2244
2245         account_s* account_head = NULL;
2246
2247         ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2248
2249         int tmp = 0;
2250
2251         account_head = (account_s*) malloc(sizeof(account_s));
2252         if (account_head == NULL) {
2253                 ACCOUNT_FATAL("malloc Failed");
2254                 if (hstmt != NULL) {
2255                         rc = _account_query_finalize(hstmt);
2256                         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
2257                         hstmt = NULL;
2258                 }
2259                 *error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
2260                 return NULL;
2261         }
2262         ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2263
2264         while (rc == SQLITE_ROW) {
2265                 account_s* account_record = NULL;
2266
2267                 account_record = (account_s*) malloc(sizeof(account_s));
2268
2269                 if (account_record == NULL) {
2270                         ACCOUNT_FATAL("malloc Failed");
2271                         break;
2272                 }
2273                 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2274
2275                 _account_convert_column_to_account(hstmt, account_record);
2276
2277                 account_head->account_list = g_list_append(account_head->account_list, account_record);
2278
2279                 rc = _account_query_step(hstmt);
2280                 tmp++;
2281         }
2282
2283         rc = _account_query_finalize(hstmt);
2284         ACCOUNT_CATCH_ERROR_P((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2285         hstmt = NULL;
2286
2287         GList *iter;
2288
2289
2290         tmp = g_list_length(account_head->account_list);
2291
2292         for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2293                 account_s* testaccount = (account_s*)iter->data;
2294
2295                 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, testaccount->id, (void*)testaccount);
2296                 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, testaccount->id, (void*)testaccount);
2297
2298         }
2299
2300         *error_code = _ACCOUNT_ERROR_NONE;
2301
2302 CATCH:
2303         if (hstmt != NULL)
2304         {
2305                 rc = _account_query_finalize(hstmt);
2306                 if (rc != _ACCOUNT_ERROR_NONE) {
2307                         *error_code = rc;
2308                         _ERR("finalize error");
2309                 }
2310                 hstmt = NULL;
2311         }
2312
2313         if( (*error_code != _ACCOUNT_ERROR_NONE) && account_head ) {
2314                 _account_glist_account_free(account_head->account_list);
2315                 _ACCOUNT_FREE(account_head);
2316                 account_head = NULL;
2317         }
2318
2319         pthread_mutex_unlock(&account_mutex);
2320
2321         if (account_head)
2322         {
2323                 _remove_sensitive_info_from_non_owning_account_list(account_head->account_list, pid, uid);
2324                 GList* result = account_head->account_list;
2325                 _ACCOUNT_FREE(account_head);
2326                 return result;
2327         }
2328         return NULL;
2329 }
2330
2331 GList* account_server_query_account_by_package_name(const char* package_name, int *error_code, int pid, uid_t uid)
2332 {
2333         _INFO("account_server_query_account_by_package_name start");
2334
2335         GList * account_list = NULL;
2336         *error_code = _ACCOUNT_ERROR_NONE;
2337
2338         ACCOUNT_RETURN_VAL((package_name != NULL), {*error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("PACKAGE NAME IS NULL"));
2339         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
2340
2341         account_list = _account_query_account_by_package_name(g_hAccountDB, package_name, error_code, pid, uid);
2342
2343         _INFO("account_server_query_account_by_package_name end");
2344         return account_list;
2345 }
2346
2347 int account_server_delete_account_by_package_name(const char* package_name, bool permission, int pid, uid_t uid)
2348 {
2349         _INFO("account_db_delete_account_by_package_name");
2350
2351         int error_code = _ACCOUNT_ERROR_NONE;
2352
2353         ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
2354         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2355
2356         error_code = _account_delete_account_by_package_name(g_hAccountDB, package_name, permission, pid, uid);
2357
2358         _INFO("account_server_delete_account_by_package_name end");
2359         return error_code;
2360 }
2361
2362 int _account_delete(int pid, uid_t uid, int account_id)
2363 {
2364         int                             error_code = _ACCOUNT_ERROR_NONE;
2365         account_stmt    hstmt = NULL;
2366         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2367         int                             rc = 0;
2368         int                             ret_transaction = 0;
2369         bool                    is_success = FALSE;
2370
2371         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2372
2373         int count = -1;
2374         /* Check requested ID to delete */
2375         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id=%d", ACCOUNT_TABLE, account_id);
2376
2377         count = _account_get_record_count(g_hAccountDB, query);
2378
2379         if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
2380                 pthread_mutex_unlock(&account_mutex);
2381                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2382                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2383         }
2384
2385         if (count <= 0) {
2386                 ACCOUNT_ERROR("account id(%d) is not exist. count(%d)\n", account_id, count);
2387                 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
2388         }
2389
2390         /* Check permission of requested appid */
2391         char* current_appid = NULL;
2392         char *package_name = NULL;
2393
2394         current_appid = _account_get_current_appid(pid, uid);
2395
2396         error_code = _account_get_package_name_from_account_id(account_id, &package_name);
2397
2398         if(error_code != _ACCOUNT_ERROR_NONE){
2399                 ACCOUNT_ERROR("No package name with account_id\n");
2400                 _ACCOUNT_FREE(current_appid);
2401                 _ACCOUNT_FREE(package_name);
2402                 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
2403         }
2404         ACCOUNT_DEBUG( "DELETE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name);
2405
2406         error_code = _account_check_appid_group_with_package_name(current_appid, package_name, uid);
2407
2408         _ACCOUNT_FREE(current_appid);
2409         _ACCOUNT_FREE(package_name);
2410
2411         if(error_code != _ACCOUNT_ERROR_NONE){
2412                 ACCOUNT_ERROR("No permission to delete\n");
2413                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2414         }
2415
2416         /* transaction control required*/
2417         ret_transaction = _account_begin_transaction(g_hAccountDB);
2418
2419         if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
2420                 pthread_mutex_unlock(&account_mutex);
2421                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2422                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2423         }
2424
2425         if( ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY ){
2426                 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg(g_hAccountDB));
2427                 pthread_mutex_unlock(&account_mutex);
2428                 return _ACCOUNT_ERROR_DATABASE_BUSY;
2429         }
2430
2431         if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2432                 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
2433                 pthread_mutex_unlock(&account_mutex);
2434                 return ret_transaction;
2435         }
2436
2437         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2438         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
2439
2440         hstmt = _account_prepare_query(g_hAccountDB, query);
2441
2442         if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
2443                 pthread_mutex_unlock(&account_mutex);
2444                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2445                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2446         }
2447
2448         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2449                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2450
2451         rc = _account_query_step(hstmt);
2452         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2453
2454         rc = _account_query_finalize(hstmt);
2455
2456         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2457         hstmt = NULL;
2458
2459         ACCOUNT_MEMSET(query, 0, sizeof(query));
2460
2461         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
2462
2463         hstmt = _account_prepare_query(g_hAccountDB, query);
2464         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2465                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2466
2467         rc = _account_query_step(hstmt);
2468         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
2469
2470         rc = _account_query_finalize(hstmt);
2471         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2472         hstmt = NULL;
2473
2474         /* delete custom data */
2475         ACCOUNT_MEMSET(query, 0, sizeof(query));
2476
2477         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
2478
2479         hstmt = _account_prepare_query(g_hAccountDB, query);
2480
2481         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2482                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2483
2484         rc = _account_query_step(hstmt);
2485         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
2486
2487         rc = _account_query_finalize(hstmt);
2488         ACCOUNT_CATCH_ERROR(rc == _ACCOUNT_ERROR_NONE, {}, rc, ("finalize error", account_id, rc));
2489         hstmt = NULL;
2490
2491         is_success = TRUE;
2492
2493 CATCH:
2494         if (hstmt != NULL) {
2495                 rc = _account_query_finalize(hstmt);
2496                 if(rc != _ACCOUNT_ERROR_NONE ){
2497                         ACCOUNT_ERROR("rc (%d)", rc);
2498                         is_success = FALSE;
2499                 }
2500
2501                 hstmt = NULL;
2502         }
2503
2504         ret_transaction = _account_end_transaction(g_hAccountDB, is_success);
2505
2506         if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2507                 ACCOUNT_ERROR("account_delete:_account_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
2508         } else {
2509                 if (is_success == true) {
2510                         char buf[64]={0,};
2511                         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_DELETE, account_id);
2512                         _account_insert_delete_update_notification_send(buf);
2513                 }
2514         }
2515
2516         pthread_mutex_unlock(&account_mutex);
2517
2518         return error_code;
2519
2520 }
2521
2522 static int _account_query_account_by_username_and_package(const char* username, const char* package_name, account_s *account)
2523 {
2524         //FIXME
2525         //return -1;
2526         int                             error_code = _ACCOUNT_ERROR_NONE;
2527         account_stmt    hstmt = NULL;
2528         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2529         int                             rc = 0;
2530         int                             binding_count = 1;
2531
2532         ACCOUNT_RETURN_VAL((username != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("username IS NULL"));
2533         ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name IS NULL"));
2534         ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2535         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2536
2537         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2538
2539         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
2540         hstmt = _account_prepare_query(g_hAccountDB, query);
2541
2542         if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
2543                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2544                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2545         }
2546
2547         _account_query_bind_text(hstmt, binding_count++, username);
2548         _account_query_bind_text(hstmt, binding_count++, package_name);
2549
2550         rc = _account_query_step(hstmt);
2551         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2552
2553         account_s *account_record = account;
2554
2555         while (rc == SQLITE_ROW) {
2556                 _account_convert_column_to_account(hstmt, account_record);
2557                 rc = _account_query_step(hstmt);
2558         }
2559
2560         rc = _account_query_finalize(hstmt);
2561         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2562         _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, account_record->id, (void*)account_record);
2563         _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, account_record->id, (void*)account_record);
2564
2565         hstmt = NULL;
2566         error_code = _ACCOUNT_ERROR_NONE;
2567
2568 CATCH:
2569         if (hstmt != NULL) {
2570                 rc = _account_query_finalize(hstmt);
2571                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2572                 hstmt = NULL;
2573         }
2574
2575         pthread_mutex_unlock(&account_mutex);
2576         return error_code;
2577 }
2578
2579 int _account_create(account_s **account)
2580 {
2581         if (!account) {
2582                 ACCOUNT_SLOGE("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__);
2583                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2584         }
2585
2586         account_s *data = (account_s*)malloc(sizeof(account_s));
2587
2588         if (data == NULL) {
2589                 ACCOUNT_FATAL("Memory Allocation Failed");
2590                 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
2591         }
2592         ACCOUNT_MEMSET(data, 0, sizeof(account_s));
2593
2594         /*Setting account as visible by default*/
2595 //      data->secret = _ACCOUNT_SECRECY_VISIBLE;
2596
2597         /*Setting account as not supporting sync by default*/
2598 //      data->sync_support = _ACCOUNT_SYNC_NOT_SUPPORT;
2599
2600         *account = data;
2601
2602         return _ACCOUNT_ERROR_NONE;
2603 }
2604
2605 int _account_destroy(account_s *account)
2606 {
2607         account_s *data = account;
2608
2609         ACCOUNT_RETURN_VAL((data != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Account handle is null!"));
2610
2611         _account_free_account_with_items(data);
2612
2613         return _ACCOUNT_ERROR_NONE;
2614 }
2615
2616 int _account_get_account_id(account_s* account, int *account_id)
2617 {
2618         if (!account) {
2619                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2620         }
2621         if (!account_id) {
2622                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2623         }
2624
2625         *account_id = account->id;
2626
2627         return _ACCOUNT_ERROR_NONE;
2628 }
2629
2630 int _account_delete_from_db_by_user_name(int pid, uid_t uid, const char *user_name, const char *package_name)
2631 {
2632         _INFO("[%s][%s]", user_name, package_name);
2633
2634         int                     error_code = _ACCOUNT_ERROR_NONE;
2635         account_stmt    hstmt = NULL;
2636         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2637         int                     rc = 0;
2638         int                     ret_transaction = 0;
2639         bool                    is_success = FALSE;
2640         account_s               *account = NULL;
2641         int                     binding_count = 1;
2642         int                             account_id = -1;
2643
2644         ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is null!"));
2645         ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
2646         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2647
2648         /* Check permission of requested appid */
2649         char* current_appid = NULL;
2650         char* package_name_temp = NULL;
2651
2652         current_appid = _account_get_current_appid(pid, uid);
2653
2654         package_name_temp = _account_dup_text(package_name);
2655
2656         ACCOUNT_DEBUG( "DELETE:user_name[%s],current_appid[%s], package_name[%s]", user_name, current_appid, package_name_temp);
2657
2658         error_code = _account_check_appid_group_with_package_name(current_appid, package_name_temp, uid);
2659
2660         _ACCOUNT_FREE(current_appid);
2661         _ACCOUNT_FREE(package_name_temp);
2662
2663         if(error_code != _ACCOUNT_ERROR_NONE){
2664                 ACCOUNT_ERROR("No permission to delete\n");
2665                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2666         }
2667
2668         rc = _account_create(&account);
2669         rc = _account_query_account_by_username_and_package(user_name, package_name, account);
2670
2671         _INFO("");
2672
2673         if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM )
2674         {
2675                 _account_destroy(account);
2676                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2677                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2678         }
2679
2680         _INFO("");
2681         account_s* account_data = (account_s*)account;
2682
2683         rc = _account_get_account_id(account_data, &account_id);
2684
2685         rc = _account_destroy(account);
2686
2687         /* transaction control required*/
2688         ret_transaction = _account_begin_transaction(g_hAccountDB);
2689
2690         if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM )
2691         {
2692                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2693                 pthread_mutex_unlock(&account_mutex);
2694                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2695         }
2696
2697         _INFO("");
2698         if( ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY )
2699         {
2700                 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg(g_hAccountDB));
2701                 pthread_mutex_unlock(&account_mutex);
2702                 return _ACCOUNT_ERROR_DATABASE_BUSY;
2703         }
2704         else if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2705                 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
2706                 pthread_mutex_unlock(&account_mutex);
2707                 return ret_transaction;
2708         }
2709
2710         /* delete custom data */
2711         ACCOUNT_MEMSET(query, 0, sizeof(query));
2712         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = ?", ACCOUNT_CUSTOM_TABLE);
2713
2714         hstmt = _account_prepare_query(g_hAccountDB, query);
2715
2716         if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
2717                 _account_end_transaction(g_hAccountDB, FALSE);
2718                 pthread_mutex_unlock(&account_mutex);
2719                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2720                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2721         }
2722
2723         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2724                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2725
2726         _account_query_bind_int(hstmt, binding_count++, account_id);
2727
2728         rc = _account_query_step(hstmt);
2729         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2730
2731         rc = _account_query_finalize(hstmt);
2732         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2733         hstmt = NULL;
2734
2735         /* delete capability */
2736         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", CAPABILITY_TABLE);
2737
2738         hstmt = _account_prepare_query(g_hAccountDB, query);
2739
2740         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2741                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2742
2743         binding_count = 1;
2744         _account_query_bind_text(hstmt, binding_count++, user_name);
2745         _account_query_bind_text(hstmt, binding_count++, package_name);
2746
2747         rc = _account_query_step(hstmt);
2748         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2749
2750         rc = _account_query_finalize(hstmt);
2751         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2752         hstmt = NULL;
2753
2754         ACCOUNT_MEMSET(query, 0, sizeof(query));
2755
2756         _INFO("");
2757         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
2758
2759         hstmt = _account_prepare_query(g_hAccountDB, query);
2760         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2761                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2762
2763         _INFO("");
2764         binding_count = 1;
2765         _account_query_bind_text(hstmt, binding_count++, user_name);
2766         _account_query_bind_text(hstmt, binding_count++, package_name);
2767
2768         rc = _account_query_step(hstmt);
2769         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));
2770
2771         rc = _account_query_finalize(hstmt);
2772         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2773         is_success = TRUE;
2774
2775         hstmt = NULL;
2776
2777 CATCH:
2778         if (hstmt != NULL) {
2779                 rc = _account_query_finalize(hstmt);
2780                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2781                 hstmt = NULL;
2782         }
2783
2784         ret_transaction = _account_end_transaction(g_hAccountDB, is_success);
2785
2786         if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2787                 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
2788         } else {
2789                 if (is_success == true) {
2790                         char buf[64]={0,};
2791                         ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_DELETE, account_id);
2792                         _account_insert_delete_update_notification_send(buf);
2793                 }
2794         }
2795
2796         pthread_mutex_unlock(&account_mutex);
2797
2798         return error_code;
2799 }
2800
2801
2802 int _account_get_total_count_from_db(gboolean include_hidden, int *count)
2803 {
2804         if (!count) {
2805                 ACCOUNT_SLOGE("(%s)-(%d) count is NULL.\n", __FUNCTION__, __LINE__);
2806                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2807         }
2808
2809         if(!g_hAccountDB){
2810                 ACCOUNT_ERROR("DB is not opened\n");
2811                 return _ACCOUNT_ERROR_DB_NOT_OPENED;
2812         }
2813
2814         char query[1024] = {0, };
2815         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2816
2817         if (include_hidden)
2818         {
2819                 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s", ACCOUNT_TABLE);
2820         }
2821         else
2822         {
2823                 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s where secret = %d", ACCOUNT_TABLE, _ACCOUNT_SECRECY_VISIBLE);
2824         }
2825
2826         *count = _account_get_record_count(g_hAccountDB, query);
2827
2828         if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
2829                 pthread_mutex_unlock(&account_mutex);
2830                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2831                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2832         }
2833
2834         int rc = -1;
2835         int ncount = 0;
2836         account_stmt pStmt = NULL;
2837
2838         rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
2839         if (SQLITE_OK != rc) {
2840                 ACCOUNT_SLOGE("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg(g_hAccountDB));
2841                 sqlite3_finalize(pStmt);
2842                 return _ACCOUNT_ERROR_DB_FAILED;
2843         }
2844
2845         rc = sqlite3_step(pStmt);
2846         if (SQLITE_ROW != rc) {
2847                 ACCOUNT_ERROR("[ERROR] sqlite3_step() failed\n");
2848                 sqlite3_finalize(pStmt);
2849                 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
2850         }
2851
2852         ncount = sqlite3_column_int(pStmt, 0);
2853
2854         *count = ncount;
2855
2856         sqlite3_finalize(pStmt);
2857
2858         if (ncount < 0) {
2859                 ACCOUNT_ERROR("[ERROR] Number of account : %d, End", ncount);
2860                 return _ACCOUNT_ERROR_DB_FAILED;
2861         }
2862
2863         return _ACCOUNT_ERROR_NONE;
2864 }
2865
2866
2867 int account_server_query_app_id_exist(const char* app_id)
2868 {
2869         _INFO("account_server_query_app_id_exist start app_id=[%s]", app_id);
2870         int ret = _ACCOUNT_ERROR_NONE;
2871
2872         ret = _account_type_query_app_id_exist_from_all_db(g_hAccountDB, g_hAccountGlobalDB, app_id);
2873
2874         _INFO("account_server_query_app_id_exist end error_code=[%d]", ret);
2875         return ret;
2876 }
2877
2878 int account_server_insert_account_type_to_user_db(account_type_s *account_type, int *account_type_id, uid_t uid)
2879 {
2880         ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE HANDLE IS NULL"));
2881         ACCOUNT_RETURN_VAL((account_type->app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID OF ACCOUNT TYPE IS NULL"));
2882         ACCOUNT_RETURN_VAL((account_type_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE ID IS NULL"));
2883
2884         _INFO("account_server_insert_account_type_to_user_db start uid=[%d]", uid);
2885         int ret = _ACCOUNT_ERROR_NONE;
2886
2887         if (_account_type_check_duplicated(g_hAccountDB, account_type->app_id) ||
2888                         _account_type_check_duplicated(g_hAccountGlobalDB, account_type->app_id)) {
2889                 *account_type_id = -1;
2890                 return _ACCOUNT_ERROR_DUPLICATED;
2891         }
2892
2893         ret = _account_type_insert_to_db(g_hAccountDB, account_type, account_type_id);
2894         _INFO("account_server_insert_account_type_to_user_db end error_code=[%d]", ret);
2895         return ret;
2896 }
2897
2898 int account_server_delete_account_type_by_app_id_from_user_db(const char * app_id)
2899 {
2900         ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID OF ACCOUNT TYPE IS NULL"));
2901
2902         _INFO("account_server_delete_account_type_by_app_id_from_user_db start");
2903         int ret = _ACCOUNT_ERROR_NONE;
2904
2905         ret = _account_type_delete_by_app_id(g_hAccountDB, app_id);
2906         _INFO("account_server_delete_account_type_by_app_id_from_user_db end error_code=[%d]", ret);
2907         return ret;
2908 }
2909
2910 GSList* _account_type_query_provider_feature_by_app_id_from_global_db(const char* app_id, int *error_code)
2911 {
2912         _INFO("_account_type_query_provider_feature_by_app_id_in_global_db app_id=%s", app_id);
2913         account_stmt hstmt = NULL;
2914         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2915         int rc = 0, binding_count = 1;
2916         GSList* feature_list = NULL;
2917
2918         ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
2919         ACCOUNT_RETURN_VAL((error_code != NULL), {_ERR("error_code pointer is NULL");}, NULL, (""));
2920         ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {*error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; _ERR("The database isn't connected.");}, NULL, ("The database isn't connected."));
2921
2922         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2923
2924         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
2925         _INFO("account query=[%s]", query);
2926
2927         hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
2928
2929         if( _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM ){
2930                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2931                 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
2932                 return NULL;
2933         }
2934
2935         _INFO("before _account_query_bind_text");
2936         _account_query_bind_text(hstmt, binding_count++, app_id);
2937
2938         rc = _account_query_step(hstmt);
2939
2940         ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {*error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND; _ERR("The record isn't found from global db. rc=[%d]", rc);}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2941
2942         provider_feature_s* feature_record = NULL;
2943
2944         while (rc == SQLITE_ROW) {
2945
2946                 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
2947
2948                 if (feature_record == NULL) {
2949                         ACCOUNT_FATAL("malloc Failed");
2950                         break;
2951                 }
2952
2953                 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
2954
2955                 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
2956
2957                 _INFO("Adding account feature_list");
2958                 feature_list = g_slist_append(feature_list, feature_record);
2959
2960                 rc = _account_query_step(hstmt);
2961         }
2962
2963         *error_code = _ACCOUNT_ERROR_NONE;
2964
2965 CATCH:
2966         if (hstmt != NULL) {
2967                 rc = _account_query_finalize(hstmt);
2968                 if (rc != _ACCOUNT_ERROR_NONE) {
2969                         *error_code = rc;
2970                         _ERR("global db fianlize error");
2971                 }
2972         }
2973
2974         if (*error_code != _ACCOUNT_ERROR_NONE) {
2975                 _account_type_gslist_feature_free(feature_list);
2976         }
2977
2978         _INFO("Returning account feature_list from global db");
2979         return feature_list;
2980 }
2981
2982 GSList* _account_type_query_provider_feature_by_app_id(const char* app_id, int *error_code)
2983 {
2984         _INFO("_account_type_query_provider_feature_by_app_id app_id=%s", app_id);
2985         account_stmt hstmt = NULL;
2986         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2987         int rc = 0, binding_count = 1;
2988         GSList* feature_list = NULL;
2989
2990         ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
2991         ACCOUNT_RETURN_VAL((error_code != NULL), {_ERR("error_code pointer is NULL");}, NULL, (""));
2992         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
2993
2994         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2995
2996         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
2997         _INFO("account query=[%s]", query);
2998
2999         hstmt = _account_prepare_query(g_hAccountDB, query);
3000
3001         if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
3002                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3003                 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3004                 return NULL;
3005         }
3006
3007         _account_query_bind_text(hstmt, binding_count++, app_id);
3008
3009         rc = _account_query_step(hstmt);
3010
3011         ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {*error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND; _ERR("The record isn't found from user db. rc=[%d]", rc);}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3012
3013         provider_feature_s* feature_record = NULL;
3014
3015         while (rc == SQLITE_ROW) {
3016
3017                 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
3018
3019                 if (feature_record == NULL) {
3020                         ACCOUNT_FATAL("malloc Failed");
3021                         break;
3022                 }
3023
3024                 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
3025
3026                 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
3027
3028                 _INFO("Adding account feature_list");
3029                 feature_list = g_slist_append(feature_list, feature_record);
3030
3031                 rc = _account_query_step(hstmt);
3032         }
3033
3034         *error_code = _ACCOUNT_ERROR_NONE;
3035
3036         rc = _account_query_finalize(hstmt);
3037         ACCOUNT_CATCH_ERROR_P((rc == _ACCOUNT_ERROR_NONE), {*error_code = rc;}, rc, ("account finalize error"));
3038         hstmt = NULL;
3039
3040 CATCH:
3041         if (hstmt != NULL) {
3042                 rc = _account_query_finalize(hstmt);
3043                 if (rc != _ACCOUNT_ERROR_NONE) {
3044                         *error_code = rc;
3045                         _ERR("account fianlize error");
3046                 }
3047                 hstmt = NULL;
3048         }
3049         _INFO("*error_code=[%d]", *error_code);
3050
3051         if (*error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
3052                 feature_list = _account_type_query_provider_feature_by_app_id_from_global_db(app_id, error_code);
3053         }
3054
3055         if (*error_code != _ACCOUNT_ERROR_NONE)
3056                 _account_type_gslist_feature_free(feature_list);
3057
3058         _INFO("Returning account feature_list");
3059         return feature_list;
3060 }
3061
3062 int _account_type_query_provider_feature_cb_by_app_id_from_global_db(account_type_provider_feature_cb callback, const char* app_id, void *user_data )
3063 {
3064         int                     error_code = _ACCOUNT_ERROR_NONE;
3065         account_stmt    hstmt = NULL;
3066         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3067         int                     rc = 0, binding_count = 1;
3068
3069         _INFO("_account_type_query_provider_feature_cb_by_app_id_in_global_db start app_id=%s", app_id);
3070         ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3071         ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3072         ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3073
3074         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3075
3076         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
3077         hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3078
3079         if( _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM ){
3080                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3081                 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_PERMISSION_DENIED, ("global db permission denied.\n"));
3082         }
3083
3084         _account_query_bind_text(hstmt, binding_count++, app_id);
3085
3086         rc = _account_query_step(hstmt);
3087         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {_ERR("The record isn't found. rc=[%d]", rc);}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3088
3089         provider_feature_s* feature_record = NULL;
3090
3091         while (rc == SQLITE_ROW) {
3092                 bool cb_ret = FALSE;
3093                 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
3094
3095                 if (feature_record == NULL) {
3096                         ACCOUNT_FATAL("malloc Failed");
3097                         break;
3098                 }
3099
3100                 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
3101
3102                 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
3103
3104                 cb_ret = callback(feature_record->app_id, feature_record->key, user_data);
3105
3106                 _account_type_free_feature_with_items(feature_record);
3107
3108                 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returns FALSE, its iteration is stopped!!!!\n"));
3109
3110                 rc = _account_query_step(hstmt);
3111         }
3112
3113         error_code = _ACCOUNT_ERROR_NONE;
3114
3115 CATCH:
3116         if (hstmt != NULL) {
3117                 rc = _account_query_finalize(hstmt);
3118                 if (rc != _ACCOUNT_ERROR_NONE) {
3119                         error_code = rc;
3120                         _ERR("global db finalize error[%d]", rc);
3121                 }
3122                 hstmt = NULL;
3123         }
3124
3125         _INFO("_account_type_query_provider_feature_cb_by_app_id_in_global_db end. error_code=[%d]", error_code);
3126         return error_code;
3127 }
3128
3129 int _account_type_query_provider_feature_cb_by_app_id(account_type_provider_feature_cb callback, const char* app_id, void *user_data )
3130 {
3131         int                     error_code = _ACCOUNT_ERROR_NONE;
3132         account_stmt    hstmt = NULL;
3133         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3134         int                     rc = 0, binding_count = 1;
3135
3136         _INFO("_account_type_query_provider_feature_cb_by_app_id start app_id=%s", app_id);
3137         ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3138         ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3139         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3140
3141         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3142
3143         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
3144         hstmt = _account_prepare_query(g_hAccountDB, query);
3145
3146         if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
3147                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3148                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3149         }
3150
3151         _account_query_bind_text(hstmt, binding_count++, app_id);
3152
3153         rc = _account_query_step(hstmt);
3154         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found in user db.\n"));
3155
3156         provider_feature_s* feature_record = NULL;
3157
3158         while (rc == SQLITE_ROW) {
3159                 bool cb_ret = FALSE;
3160                 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
3161
3162                 if (feature_record == NULL) {
3163                         ACCOUNT_FATAL("malloc Failed");
3164                         break;
3165                 }
3166
3167                 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
3168
3169                 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
3170
3171                 cb_ret = callback(feature_record->app_id, feature_record->key, user_data);
3172
3173                 _account_type_free_feature_with_items(feature_record);
3174
3175                 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
3176
3177                 rc = _account_query_step(hstmt);
3178         }
3179
3180         rc = _account_query_finalize(hstmt);
3181         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3182         hstmt = NULL;
3183
3184         error_code = _ACCOUNT_ERROR_NONE;
3185
3186 CATCH:
3187         if (hstmt != NULL) {
3188                 rc = _account_query_finalize(hstmt);
3189                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3190                 hstmt = NULL;
3191         }
3192 /*
3193         if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
3194                 error_code = _account_type_query_provider_feature_cb_by_app_id_from_global_db(callback, app_id, user_data);
3195         }
3196 */
3197         _INFO("_account_type_query_provider_feature_cb_by_app_id end");
3198         return error_code;
3199 }
3200
3201 int account_type_query_provider_feature_cb_by_app_id(account_type_provider_feature_cb callback, const char* app_id, void *user_data )
3202 {
3203         int                     error_code = _ACCOUNT_ERROR_NONE;
3204
3205         ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3206         ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3207         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3208
3209         error_code = _account_type_query_provider_feature_cb_by_app_id(callback, app_id, user_data);
3210
3211         if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
3212                 error_code = _account_type_query_provider_feature_cb_by_app_id_from_global_db(callback, app_id, user_data);
3213         }
3214
3215         return error_code;
3216 }
3217
3218 bool _account_type_query_supported_feature_from_global_db(const char* app_id, const char* capability, int *error_code)
3219 {
3220         _INFO("_account_type_query_supported_feature_in_global_db start");
3221         ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3222
3223         *error_code = _ACCOUNT_ERROR_NONE;
3224
3225         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3226         int record_count = 0;
3227
3228         if (app_id == NULL || capability == NULL)
3229         {
3230                 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
3231                 return false;
3232         }
3233
3234         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
3235
3236         record_count = _account_get_record_count(g_hAccountGlobalDB, query);
3237
3238         if( _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM ){
3239                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3240                 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3241         }
3242
3243         if (record_count <= 0)
3244         {
3245                 *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
3246                 return false;
3247         }
3248
3249         _INFO("_account_type_query_supported_feature_in_global_db end");
3250         return true;
3251 }
3252
3253 bool _account_type_query_supported_feature(const char* app_id, const char* capability, int *error_code)
3254 {
3255         _INFO("_account_type_query_supported_feature start");
3256
3257         *error_code = _ACCOUNT_ERROR_NONE;
3258
3259         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3260         int                     record_count = 0;
3261
3262         if (app_id == NULL || capability == NULL)
3263         {
3264                 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
3265                 return false;
3266         }
3267
3268         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
3269
3270         record_count = _account_get_record_count(g_hAccountDB, query);
3271
3272         if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
3273                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3274                 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3275                 return false;
3276         }
3277
3278         if (record_count <= 0)
3279         {
3280                 bool is_exist = false;
3281                 is_exist = _account_type_query_supported_feature_from_global_db(app_id, capability, error_code);
3282                 if (!is_exist)
3283                         return false;
3284         }
3285
3286         _INFO("_account_type_query_supported_feature end");
3287         return true;
3288 }
3289
3290
3291 static int _account_type_update_provider_feature(sqlite3 *account_db_handle, account_type_s *account_type, const char* app_id)
3292 {
3293         int                     rc, count = 1;
3294         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3295         account_stmt    hstmt = NULL;
3296
3297         ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
3298
3299         if (g_slist_length( account_type->provider_feature_list)==0) {
3300                 ACCOUNT_ERROR( "no feature\n");
3301                 return _ACCOUNT_ERROR_NONE;
3302         }
3303
3304         ACCOUNT_DEBUG( "app id", app_id);
3305
3306         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3307
3308         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id=? ", PROVIDER_FEATURE_TABLE);
3309         hstmt = _account_prepare_query(account_db_handle, query);
3310
3311         if( _account_db_err_code(account_db_handle) == SQLITE_PERM ){
3312                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(account_db_handle));
3313                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3314         }
3315
3316         count = 1;
3317         _account_query_bind_text(hstmt, count++, app_id);
3318         rc = _account_query_step(hstmt);
3319
3320         if (rc != SQLITE_DONE) {
3321                 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3322                 return _ACCOUNT_ERROR_DB_FAILED;
3323         }
3324         rc = _account_query_finalize(hstmt);
3325         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3326         hstmt = NULL;
3327
3328         GSList *iter;
3329
3330         for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
3331                 int ret;
3332                 count = 1;
3333                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3334                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
3335                                 "(?, ?) ", PROVIDER_FEATURE_TABLE);
3336
3337                 hstmt = _account_prepare_query(account_db_handle, query);
3338
3339                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg(account_db_handle)));
3340
3341                 provider_feature_s* feature_data = NULL;
3342                 feature_data = (provider_feature_s*)iter->data;
3343
3344                 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
3345                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3346                 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
3347                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3348
3349                 rc = _account_query_step(hstmt);
3350
3351                 if (rc != SQLITE_DONE) {
3352                         ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3353                         break;
3354                 }
3355                 rc = _account_query_finalize(hstmt);
3356                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3357                 hstmt = NULL;
3358         }
3359
3360         return _ACCOUNT_ERROR_NONE;
3361 }
3362
3363 static int _account_type_update_label(sqlite3 *account_db_handle, account_type_s *account_type, const char* app_id)
3364 {
3365         int                     rc, count = 1;
3366         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3367         account_stmt    hstmt = NULL;
3368
3369         ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
3370
3371         if (g_slist_length( account_type->label_list)==0) {
3372                 return _ACCOUNT_ERROR_NONE;
3373         }
3374
3375         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3376
3377         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId=? ", LABEL_TABLE);
3378         hstmt = _account_prepare_query(account_db_handle, query);
3379
3380         if( _account_db_err_code(account_db_handle) == SQLITE_PERM ){
3381                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(account_db_handle));
3382                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3383         }
3384
3385         count = 1;
3386         _account_query_bind_text(hstmt, count++, app_id);
3387         rc = _account_query_step(hstmt);
3388
3389         if (rc != SQLITE_DONE) {
3390                 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3391                 return _ACCOUNT_ERROR_DB_FAILED;
3392         }
3393         rc = _account_query_finalize(hstmt);
3394         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3395         hstmt = NULL;
3396
3397         GSList *iter;
3398
3399         for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
3400                 int ret;
3401                 count = 1;
3402                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3403                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
3404                                 "(?, ?, ?) ", LABEL_TABLE);
3405
3406                 hstmt = _account_prepare_query(account_db_handle, query);
3407
3408                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg(account_db_handle)));
3409
3410                 label_s* label_data = NULL;
3411                 label_data = (label_s*)iter->data;
3412
3413                 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
3414                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3415                 ret = _account_query_bind_text(hstmt, count++, label_data->label);
3416                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3417                 ret = _account_query_bind_text(hstmt, count++, label_data->locale);
3418                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3419
3420                 rc = _account_query_step(hstmt);
3421
3422                 if (rc != SQLITE_DONE) {
3423                         ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3424                         break;
3425                 }
3426                 rc = _account_query_finalize(hstmt);
3427                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3428                 hstmt = NULL;
3429         }
3430
3431         return _ACCOUNT_ERROR_NONE;
3432 }
3433
3434
3435 static int _account_type_update_account(sqlite3 *account_db_handle, account_type_s *account_type, const char* app_id)
3436 {
3437         int                             rc = 0, binding_count =1;
3438         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3439         int                             error_code = _ACCOUNT_ERROR_NONE;
3440         account_stmt    hstmt = NULL;
3441
3442         if (!account_type->app_id) {
3443                 ACCOUNT_ERROR("app id is mandetory field, it can not be NULL!!!!\n");
3444                 return _ACCOUNT_ERROR_INVALID_PARAMETER;
3445         }
3446
3447         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3448         ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET AppId=?, ServiceProviderId=?, IconPath=?, "
3449                         "SmallIconPath=?, MultipleAccountSupport=? WHERE AppId=? ", ACCOUNT_TYPE_TABLE);
3450
3451         hstmt = _account_prepare_query(account_db_handle, query);
3452
3453         if( _account_db_err_code(account_db_handle) == SQLITE_PERM ){
3454                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(account_db_handle));
3455                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3456         } else if (_account_db_err_code(account_db_handle) == SQLITE_BUSY){
3457                 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg(account_db_handle));
3458                 return _ACCOUNT_ERROR_DATABASE_BUSY;
3459         }
3460
3461         ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg(account_db_handle)));
3462
3463         binding_count = _account_type_convert_account_to_sql(account_type, hstmt, query);
3464         _account_query_bind_text(hstmt, binding_count++, app_id);
3465
3466         rc = _account_query_step(hstmt);
3467         if (rc != SQLITE_DONE) {
3468                 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3469         }
3470
3471         rc = _account_query_finalize(hstmt);
3472         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3473         hstmt = NULL;
3474
3475         /*update label*/
3476         error_code = _account_type_update_label(account_db_handle, account_type, app_id);
3477         /* update provider feature */
3478         error_code = _account_type_update_provider_feature(account_db_handle, account_type, app_id);
3479
3480         return error_code;
3481 }
3482
3483 int _account_type_update_to_db_by_app_id(account_type_s* account_type, const char* app_id)
3484 {
3485         int     error_code = _ACCOUNT_ERROR_NONE;
3486
3487         ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
3488         ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3489         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3490
3491         account_type_s* data = account_type;
3492
3493         pthread_mutex_lock(&account_mutex);
3494
3495         error_code = _account_type_update_account(g_hAccountDB, data, app_id);
3496
3497         pthread_mutex_unlock(&account_mutex);
3498
3499         return error_code;
3500 }
3501
3502 GSList* _account_type_get_label_list_by_app_id_from_global_db(const char* app_id, int *error_code )
3503 {
3504         *error_code = _ACCOUNT_ERROR_NONE;
3505         account_stmt    hstmt = NULL;
3506         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3507         int                     rc = 0, binding_count = 1;
3508         GSList* label_list = NULL;
3509
3510         ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
3511         ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3512
3513         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3514
3515         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3516         hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3517
3518         if( _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM ){
3519                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3520                 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3521
3522                 goto CATCH;
3523         }
3524
3525         _account_query_bind_text(hstmt, binding_count++, app_id);
3526
3527         rc = _account_query_step(hstmt);
3528         ACCOUNT_CATCH_ERROR_P((rc == SQLITE_ROW), {_ERR("The record isn't found. rc=[%d] done", rc);}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3529
3530         label_s* label_record = NULL;
3531
3532         while (rc == SQLITE_ROW) {
3533                 label_record = (label_s*) malloc(sizeof(label_s));
3534
3535                 if (label_record == NULL) {
3536                         ACCOUNT_FATAL("malloc Failed");
3537                         break;
3538                 }
3539
3540                 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3541
3542                 _account_type_convert_column_to_label(hstmt, label_record);
3543
3544                 _INFO("Adding account label_list");
3545                 label_list = g_slist_append (label_list, label_record);
3546
3547                 rc = _account_query_step(hstmt);
3548         }
3549
3550         *error_code = _ACCOUNT_ERROR_NONE;
3551
3552 CATCH:
3553         if (hstmt != NULL) {
3554                 rc = _account_query_finalize(hstmt);
3555                 if (rc != _ACCOUNT_ERROR_NONE) {
3556                         _ERR("global db finalize error[%d]", rc);
3557                 }
3558                 hstmt = NULL;
3559         }
3560
3561         _INFO("Returning account global label_list");
3562         return label_list;
3563 }
3564
3565 GSList* _account_type_get_label_list_by_app_id(const char* app_id, int *error_code )
3566 {
3567         *error_code = _ACCOUNT_ERROR_NONE;
3568         account_stmt    hstmt = NULL;
3569         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3570         int                     rc = 0, binding_count = 1;
3571         GSList* label_list = NULL;
3572
3573         ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
3574         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
3575
3576         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3577
3578         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3579         hstmt = _account_prepare_query(g_hAccountDB, query);
3580
3581         if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
3582                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3583                 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3584                 return NULL;
3585         }
3586
3587         _account_query_bind_text(hstmt, binding_count++, app_id);
3588
3589         rc = _account_query_step(hstmt);
3590         ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3591
3592         label_s* label_record = NULL;
3593
3594         while (rc == SQLITE_ROW) {
3595                 label_record = (label_s*) malloc(sizeof(label_s));
3596
3597                 if (label_record == NULL) {
3598                         ACCOUNT_FATAL("malloc Failed");
3599                         break;
3600                 }
3601
3602                 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3603
3604                 _account_type_convert_column_to_label(hstmt, label_record);
3605
3606                 _INFO("Adding account label_list");
3607                 label_list = g_slist_append (label_list, label_record);
3608
3609                 rc = _account_query_step(hstmt);
3610         }
3611
3612         rc = _account_query_finalize(hstmt);
3613         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
3614         hstmt = NULL;
3615
3616         *error_code = _ACCOUNT_ERROR_NONE;
3617
3618 CATCH:
3619         if (hstmt != NULL) {
3620                 rc = _account_query_finalize(hstmt);
3621                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
3622                 hstmt = NULL;
3623         }
3624
3625         if (*error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
3626                 label_list = _account_type_get_label_list_by_app_id_from_global_db(app_id, error_code);
3627         }
3628
3629         _INFO("Returning account label_list");
3630         return label_list;
3631 }
3632
3633 int _account_type_query_label_by_app_id_from_global_db(account_type_label_cb callback, const char* app_id, void *user_data )
3634 {
3635         int                     error_code = _ACCOUNT_ERROR_NONE;
3636         account_stmt    hstmt = NULL;
3637         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3638         int                     rc = 0, binding_count = 1;
3639
3640         _INFO("account_type_query_label_by_app_id_from_global_db start");
3641
3642         ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3643         ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3644         ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3645
3646         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3647
3648         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3649         hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3650
3651         if( _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM ){
3652                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3653                 error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3654                 goto CATCH;
3655         }
3656
3657         _account_query_bind_text(hstmt, binding_count++, app_id);
3658
3659         rc = _account_query_step(hstmt);
3660         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3661
3662         label_s* label_record = NULL;
3663
3664         while (rc == SQLITE_ROW) {
3665                 bool cb_ret = FALSE;
3666                 label_record = (label_s*) malloc(sizeof(label_s));
3667
3668                 if (label_record == NULL) {
3669                         ACCOUNT_FATAL("malloc Failed");
3670                         break;
3671                 }
3672
3673                 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3674
3675                 _account_type_convert_column_to_label(hstmt, label_record);
3676
3677                 cb_ret = callback(label_record->app_id, label_record->label , label_record->locale, user_data);
3678
3679                 _account_type_free_label_with_items(label_record);
3680
3681                 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
3682
3683                 rc = _account_query_step(hstmt);
3684         }
3685
3686         error_code = _ACCOUNT_ERROR_NONE;
3687
3688 CATCH:
3689         if (hstmt != NULL) {
3690                 rc = _account_query_finalize(hstmt);
3691                 if (rc != _ACCOUNT_ERROR_NONE) {
3692                         _ERR("global db finalize error[%d]", rc);
3693                 }
3694                 hstmt = NULL;
3695         }
3696
3697         _INFO("account_type_query_label_by_app_id_from_global_db end [%d]", error_code);
3698         return error_code;
3699 }
3700
3701 int _account_type_query_label_by_app_id(account_type_label_cb callback, const char* app_id, void *user_data )
3702 {
3703         int                     error_code = _ACCOUNT_ERROR_NONE;
3704         account_stmt    hstmt = NULL;
3705         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3706         int                     rc = 0, binding_count = 1;
3707
3708         ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3709         ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3710         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3711
3712         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3713
3714         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3715         hstmt = _account_prepare_query(g_hAccountDB, query);
3716
3717         if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
3718                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3719                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3720         }
3721
3722         _account_query_bind_text(hstmt, binding_count++, app_id);
3723
3724         rc = _account_query_step(hstmt);
3725         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3726
3727         label_s* label_record = NULL;
3728
3729         while (rc == SQLITE_ROW) {
3730                 bool cb_ret = FALSE;
3731                 label_record = (label_s*) malloc(sizeof(label_s));
3732
3733                 if (label_record == NULL) {
3734                         ACCOUNT_FATAL("malloc Failed");
3735                         break;
3736                 }
3737
3738                 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3739
3740                 _account_type_convert_column_to_label(hstmt, label_record);
3741
3742                 cb_ret = callback(label_record->app_id, label_record->label , label_record->locale, user_data);
3743
3744                 _account_type_free_label_with_items(label_record);
3745
3746 //              ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
3747                 if(cb_ret != TRUE) {
3748                         _INFO("Callback func returs FALSE, its iteration is stopped!!!!\n");
3749                         break;
3750                 }
3751
3752                 rc = _account_query_step(hstmt);
3753         }
3754
3755         rc = _account_query_finalize(hstmt);
3756         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3757         hstmt = NULL;
3758
3759         error_code = _ACCOUNT_ERROR_NONE;
3760
3761 CATCH:
3762         if (hstmt != NULL) {
3763                 rc = _account_query_finalize(hstmt);
3764                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3765                 hstmt = NULL;
3766         }
3767 /*
3768         if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
3769                 error_code = account_type_query_label_by_app_id_from_global_db(callback, app_id, user_data);
3770         }
3771 */
3772         return error_code;
3773 }
3774
3775 int account_type_query_label_by_app_id(account_type_label_cb callback, const char* app_id, void *user_data )
3776 {
3777         int                     error_code = _ACCOUNT_ERROR_NONE;
3778
3779         ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3780         ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3781         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3782
3783         error_code = _account_type_query_label_by_app_id(callback, app_id, user_data);
3784
3785         if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
3786                 error_code = _account_type_query_label_by_app_id_from_global_db(callback, app_id, user_data);
3787         }
3788
3789         return error_code;
3790 }
3791
3792 bool _account_get_label_text_cb(char* app_id, char* label, char* locale, void *user_data)
3793 {
3794         account_type_s *data = (account_type_s*)user_data;
3795
3796         label_s *label_data = (label_s*)malloc(sizeof(label_s));
3797
3798         if (label_data == NULL) {
3799                 ACCOUNT_FATAL("_account_get_label_text_cb : MALLOC FAIL\n");
3800                 return FALSE;
3801         }
3802         ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
3803
3804         label_data->app_id = _account_dup_text(app_id);
3805         label_data->label = _account_dup_text(label);
3806         label_data->locale = _account_dup_text(locale);
3807
3808         data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
3809
3810         return TRUE;
3811 }
3812
3813 bool _account_get_provider_feature_cb(char* app_id, char* key, void* user_data)
3814 {
3815         account_type_s *data = (account_type_s*)user_data;
3816
3817         provider_feature_s *feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
3818
3819         if (feature_data == NULL) {
3820                 ACCOUNT_FATAL("_account_get_provider_feature_cb : MALLOC FAIL\n");
3821                 return FALSE;
3822         }
3823         ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
3824
3825         feature_data->app_id = _account_dup_text(app_id);
3826         feature_data->key = _account_dup_text(key);
3827
3828         data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
3829
3830         return TRUE;
3831 }
3832
3833 int _account_type_query_by_app_id_from_global_db(const char* app_id, account_type_s** account_type_record)
3834 {
3835         _INFO("_account_type_query_by_app_id_from_global_db start");
3836
3837         int                     error_code = _ACCOUNT_ERROR_NONE;
3838         account_stmt    hstmt = NULL;
3839         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3840         int                     rc = 0, binding_count = 1;
3841
3842         ACCOUNT_RETURN_VAL((app_id != 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3843         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3844         ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3845
3846         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3847
3848         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
3849         hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3850
3851         if( _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM ){
3852                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3853                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3854         }
3855
3856         _account_query_bind_text(hstmt, binding_count++, app_id);
3857
3858         rc = _account_query_step(hstmt);
3859         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3860
3861         *account_type_record = create_empty_account_type_instance();
3862
3863         while (rc == SQLITE_ROW) {
3864                 _account_type_convert_column_to_account_type(hstmt, *account_type_record);
3865                 rc = _account_query_step(hstmt);
3866         }
3867
3868         rc = _account_query_finalize(hstmt);
3869         ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {_ERR("global db finalize error rc=[%d]", rc);}, rc, ("finalize error"));
3870         _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb, app_id, (void*)(*account_type_record));
3871         _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, app_id,(void*)(*account_type_record));
3872
3873         hstmt = NULL;
3874         error_code = _ACCOUNT_ERROR_NONE;
3875
3876 CATCH:
3877         if (hstmt != NULL) {
3878                 rc = _account_query_finalize(hstmt);
3879                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3880                 hstmt = NULL;
3881         }
3882
3883         _INFO("_account_type_query_by_app_id_from_global_db end [%d]", error_code);
3884         return error_code;
3885 }
3886
3887 int _account_type_query_by_app_id(const char* app_id, account_type_s** account_type_record)
3888 {
3889         _INFO("_account_type_query_by_app_id start");
3890
3891         int                     error_code = _ACCOUNT_ERROR_NONE;
3892         account_stmt    hstmt = NULL;
3893         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3894         int                     rc = 0, binding_count = 1;
3895
3896         ACCOUNT_RETURN_VAL((app_id != 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3897         ACCOUNT_RETURN_VAL((account_type_record != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("account type record(account_type_s**) is NULL"));
3898         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3899
3900         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3901
3902         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
3903         hstmt = _account_prepare_query(g_hAccountDB, query);
3904
3905         if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
3906                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3907                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3908         }
3909
3910         _account_query_bind_text(hstmt, binding_count++, app_id);
3911
3912         rc = _account_query_step(hstmt);
3913         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3914
3915         *account_type_record = create_empty_account_type_instance();
3916         if (*account_type_record == NULL) {
3917                 _ERR("Out of Memory");
3918                 error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
3919                 goto CATCH;
3920         }
3921
3922         while (rc == SQLITE_ROW) {
3923                 _account_type_convert_column_to_account_type(hstmt, *account_type_record);
3924                 rc = _account_query_step(hstmt);
3925         }
3926
3927         rc = _account_query_finalize(hstmt);
3928         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3929         _account_type_query_label_by_app_id(_account_get_label_text_cb, app_id, (void*)(*account_type_record));
3930         _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, app_id,(void*)(*account_type_record));
3931
3932         hstmt = NULL;
3933         error_code = _ACCOUNT_ERROR_NONE;
3934
3935 CATCH:
3936         if (hstmt != NULL) {
3937                 rc = _account_query_finalize(hstmt);
3938                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3939                 hstmt = NULL;
3940         }
3941
3942         if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
3943                 error_code = _account_type_query_by_app_id_from_global_db(app_id, account_type_record);
3944         }
3945
3946         _INFO("_account_type_query_by_app_id end [%d]", error_code);
3947         return error_code;
3948 }
3949
3950 int _account_type_query_by_provider_feature_from_global_db(const char* key, GSList **account_type_list_all)
3951 {
3952         int error_code = _ACCOUNT_ERROR_NONE;
3953         account_stmt    hstmt = NULL;
3954         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3955         int                     rc = 0;
3956         GSList                  *account_type_list = NULL;
3957
3958         _INFO("_account_type_query_by_provider_feature_from_global_db start key=%s", key);
3959         if(key == NULL)
3960         {
3961                 ACCOUNT_ERROR("capability_type IS NULL.");
3962                 error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
3963                 goto CATCH;
3964         }
3965
3966         if(g_hAccountGlobalDB == NULL)
3967         {
3968                 ACCOUNT_ERROR("The database isn't connected.");
3969                 error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;
3970                 goto CATCH;
3971         }
3972
3973         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3974
3975         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
3976
3977         hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3978
3979         if( _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM )
3980         {
3981                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3982                 error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3983                 goto CATCH;
3984         }
3985
3986         int binding_count = 1;
3987         _account_query_bind_text(hstmt, binding_count++, key);
3988
3989         rc = _account_query_step(hstmt);
3990
3991         account_type_s *account_type_record = NULL;
3992
3993         if(rc != SQLITE_ROW)
3994         {
3995                 ACCOUNT_ERROR("The record isn't found. rc=[%d]", rc);
3996                 error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
3997                 goto CATCH;
3998         }
3999
4000         while(rc == SQLITE_ROW) {
4001                 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4002
4003                 if (account_type_record == NULL) {
4004                         ACCOUNT_FATAL("malloc Failed");
4005                         break;
4006                 }
4007
4008                 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4009                 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4010                 account_type_list = g_slist_append(account_type_list, account_type_record);
4011                 rc = _account_query_step(hstmt);
4012         }
4013
4014         rc = _account_query_finalize(hstmt);
4015         if (rc != _ACCOUNT_ERROR_NONE )
4016         {
4017                 _account_type_gslist_account_type_free(account_type_list);
4018                 ACCOUNT_ERROR("finalize error(%s)", rc);
4019                 error_code = rc;
4020                 goto CATCH;
4021         }
4022         hstmt = NULL;
4023
4024         GSList* iter;
4025
4026         for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4027                 account_type_s *account_type = NULL;
4028                 account_type = (account_type_s*)iter->data;
4029                 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
4030                 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
4031                 _INFO("add label & provider_feature");
4032         }
4033
4034         for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4035
4036                 account_type_s *account_type = NULL;
4037                 account_type = (account_type_s*)iter->data;
4038                 *account_type_list_all = g_slist_append(*account_type_list_all, account_type);
4039                 _INFO("add account_type");
4040         }
4041         g_slist_free(account_type_list);
4042
4043         error_code = _ACCOUNT_ERROR_NONE;
4044
4045 CATCH:
4046         if (hstmt != NULL) {
4047                 rc = _account_query_finalize(hstmt);
4048                 if (rc != _ACCOUNT_ERROR_NONE)
4049                 {
4050                         ACCOUNT_ERROR("finalize error(%s)", rc);
4051                         return rc;
4052                 }
4053                 hstmt = NULL;
4054         }
4055
4056         _INFO("_account_type_query_by_provider_feature_from_global_db end. error_code=[%d]", error_code);
4057         return error_code;
4058 }
4059
4060 GSList* _account_type_query_by_provider_feature(const char* key, int *error_code)
4061 {
4062         *error_code = _ACCOUNT_ERROR_NONE;
4063         account_stmt hstmt = NULL;
4064         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4065         int rc = 0;
4066         GSList *account_type_list = NULL;
4067
4068         _INFO("account_type_query_by_provider_feature start key=%s", key);
4069         if(key == NULL)
4070         {
4071                 ACCOUNT_ERROR("capability_type IS NULL.");
4072                 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
4073                 goto CATCH;
4074         }
4075
4076         if(g_hAccountDB == NULL)
4077         {
4078                 ACCOUNT_ERROR("The database isn't connected.");
4079                 *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;
4080                 goto CATCH;
4081         }
4082
4083         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4084
4085         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
4086
4087         hstmt = _account_prepare_query(g_hAccountDB, query);
4088
4089         if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM )
4090         {
4091                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4092                 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
4093                 goto CATCH;
4094         }
4095
4096         int binding_count = 1;
4097         _account_query_bind_text(hstmt, binding_count++, key);
4098
4099         rc = _account_query_step(hstmt);
4100
4101         account_type_s *account_type_record = NULL;
4102
4103         if(rc != SQLITE_ROW)
4104         {
4105                 ACCOUNT_ERROR("The record isn't found. rc=[%d]", rc);
4106                 *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4107                 goto CATCH;
4108         }
4109
4110         while(rc == SQLITE_ROW) {
4111                 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4112
4113                 if (account_type_record == NULL) {
4114                         ACCOUNT_FATAL("malloc Failed");
4115                         break;
4116                 }
4117
4118                 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4119                 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4120                 account_type_list = g_slist_append(account_type_list, account_type_record);
4121                 rc = _account_query_step(hstmt);
4122         }
4123
4124         rc = _account_query_finalize(hstmt);
4125         if (rc != _ACCOUNT_ERROR_NONE )
4126         {
4127                 _account_type_gslist_account_type_free(account_type_list);
4128                 ACCOUNT_ERROR("finalize error(%s)", rc);
4129                 *error_code = rc;
4130                 goto CATCH;
4131         }
4132         hstmt = NULL;
4133
4134         GSList* iter;
4135
4136         for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4137                 account_type_s *account_type = NULL;
4138                 account_type = (account_type_s*)iter->data;
4139                 _account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
4140                 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
4141         }
4142
4143         *error_code = _ACCOUNT_ERROR_NONE;
4144
4145 CATCH:
4146         if (hstmt != NULL) {
4147                 rc = _account_query_finalize(hstmt);
4148                 if (rc != _ACCOUNT_ERROR_NONE)
4149                 {
4150                         *error_code = rc;
4151                         return NULL;
4152                 }
4153                 hstmt = NULL;
4154         }
4155
4156         if (*error_code == _ACCOUNT_ERROR_NONE || *error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4157                 rc = _account_type_query_by_provider_feature_from_global_db(key, &account_type_list);
4158                 if (rc != _ACCOUNT_ERROR_NONE && rc != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4159                         ACCOUNT_ERROR( "_account_type_query_by_provider_feature_from_global_db fail=[%d]", rc);
4160                         _account_type_gslist_account_type_free(account_type_list);
4161                         return NULL;
4162                 }
4163                 if (rc == _ACCOUNT_ERROR_NONE)
4164                         *error_code = rc;
4165         }
4166
4167         _INFO("account_type_query_by_provider_feature end");
4168         return account_type_list;
4169 }
4170
4171 int _account_type_query_all_from_global_db(GSList **account_type_list_all)
4172 {
4173         account_stmt    hstmt = NULL;
4174         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4175         int rc = _ACCOUNT_ERROR_NONE;
4176         int error_code = _ACCOUNT_ERROR_NONE;
4177         GSList *account_type_list = NULL;
4178
4179         _INFO("_account_type_query_all_in_global_db start");
4180         ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, NULL, ("The database isn't connected."));
4181
4182         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4183
4184         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
4185         hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
4186
4187         rc = _account_query_step(hstmt);
4188
4189         if( _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM ){
4190                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
4191                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4192         }
4193
4194         account_type_s *account_type_record = NULL;
4195
4196         if (rc != SQLITE_ROW)
4197         {
4198                 _INFO("[_ACCOUNT_ERROR_RECORD_NOT_FOUND]The record isn't found.");
4199                 error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4200                 goto CATCH;
4201         }
4202
4203         while(rc == SQLITE_ROW) {
4204                 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4205
4206                 if (account_type_record == NULL) {
4207                         ACCOUNT_FATAL("malloc Failed");
4208                         break;
4209                 }
4210
4211                 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4212                 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4213                 account_type_list = g_slist_append(account_type_list, account_type_record);
4214                 rc = _account_query_step(hstmt);
4215         }
4216
4217         rc = _account_query_finalize(hstmt);
4218         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4219         hstmt = NULL;
4220
4221         GSList* iter;
4222
4223         for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4224                 account_type_s *account_type = NULL;
4225                 account_type = (account_type_s*)iter->data;
4226                 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
4227                 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
4228         }
4229
4230         for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4231                 account_type_s *account_type = NULL;
4232                 account_type = (account_type_s*)iter->data;
4233                 *account_type_list_all = g_slist_append(*account_type_list_all, account_type);
4234         }
4235         g_slist_free(account_type_list);
4236
4237         error_code = _ACCOUNT_ERROR_NONE;
4238 CATCH:
4239         if (hstmt != NULL)
4240         {
4241                 rc = _account_query_finalize(hstmt);
4242                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list);}, rc, ("finalize error"));
4243                 hstmt = NULL;
4244         }
4245
4246         _INFO("_account_type_query_all_in_global_db end");
4247         return error_code;
4248 }
4249
4250 GSList* _account_type_query_all(void)
4251 {
4252         account_stmt hstmt = NULL;
4253         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4254         int rc = 0;
4255         int error_code = _ACCOUNT_ERROR_NONE;
4256         GSList *account_type_list = NULL;
4257
4258         _INFO("_account_type_query_all start");
4259         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected."));
4260
4261         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4262
4263         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
4264         hstmt = _account_prepare_query(g_hAccountDB, query);
4265
4266         rc = _account_query_step(hstmt);
4267
4268         if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
4269                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4270                 return NULL;
4271         }
4272
4273         account_type_s *account_type_record = NULL;
4274
4275         if (rc != SQLITE_ROW)
4276         {
4277                 _INFO("[_ACCOUNT_ERROR_RECORD_NOT_FOUND]The record isn't found.");
4278                 error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4279                 goto CATCH;
4280         }
4281
4282         while(rc == SQLITE_ROW) {
4283                 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4284
4285                 if (account_type_record == NULL) {
4286                         ACCOUNT_FATAL("malloc Failed");
4287                         break;
4288                 }
4289
4290                 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4291                 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4292                 account_type_list = g_slist_append(account_type_list, account_type_record);
4293                 rc = _account_query_step(hstmt);
4294         }
4295
4296         rc = _account_query_finalize(hstmt);
4297         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, NULL, ("finalize error"));
4298         hstmt = NULL;
4299
4300         GSList* iter;
4301
4302         for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4303                 account_type_s *account_type = NULL;
4304                 account_type = (account_type_s*)iter->data;
4305                 _account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
4306                 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
4307         }
4308
4309         error_code = _ACCOUNT_ERROR_NONE;
4310 CATCH:
4311         if (hstmt != NULL)
4312         {
4313                 rc = _account_query_finalize(hstmt);
4314                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list);}, NULL, ("finalize error"));
4315                 hstmt = NULL;
4316         }
4317
4318         if (error_code == _ACCOUNT_ERROR_NONE || error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4319                 error_code = _account_type_query_all_from_global_db(&account_type_list);
4320                 if (rc != _ACCOUNT_ERROR_NONE && rc != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4321                         ACCOUNT_ERROR( "_account_type_query_all_from_global_db fail=[%d]", rc);
4322                         _account_type_gslist_account_type_free(account_type_list);
4323                         return NULL;
4324                 }
4325         }
4326
4327         _INFO("_account_type_query_all end");
4328         return account_type_list;
4329 }
4330
4331 // output parameter label must be free
4332 int _account_type_query_label_by_locale_from_global_db(const char* app_id, const char* locale, char **label)
4333 {
4334         int                     error_code = _ACCOUNT_ERROR_NONE;
4335         account_stmt    hstmt = NULL;
4336         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4337         int                     rc = 0, binding_count = 1;
4338         char*                   converted_locale = NULL;
4339
4340         ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
4341         ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4342         ACCOUNT_RETURN_VAL((label != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
4343         ACCOUNT_RETURN_VAL((locale != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("locale char is null"));
4344         //Making label newly created
4345
4346         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4347
4348         converted_locale = _account_dup_text(locale);
4349         gchar** tokens = g_strsplit(converted_locale, "-", 2);
4350
4351         if(tokens != NULL) {
4352                 if((char*)(tokens[1]) != NULL) {
4353                         char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1]));
4354                         if(upper_token != NULL) {
4355                                 _ACCOUNT_FREE(converted_locale);
4356                                 converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token);
4357                         }
4358                         _ACCOUNT_FREE(upper_token);
4359                 }
4360         }
4361         g_strfreev(tokens);
4362
4363         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, converted_locale);
4364         _ACCOUNT_FREE(converted_locale);
4365
4366         hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
4367
4368         if( _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM ){
4369                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
4370                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4371         }
4372
4373         _account_query_bind_text(hstmt, binding_count++, app_id);
4374
4375         rc = _account_query_step(hstmt);
4376         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4377
4378         label_s* label_record = NULL;
4379
4380         while (rc == SQLITE_ROW) {
4381                 label_record = (label_s*) malloc(sizeof(label_s));
4382
4383                 if (label_record == NULL) {
4384                         ACCOUNT_FATAL("malloc Failed");
4385                         break;
4386                 }
4387
4388                 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
4389
4390                 _account_type_convert_column_to_label(hstmt,label_record);
4391
4392                 _ACCOUNT_FREE(*label);
4393                 //Making label newly created
4394                 *label = _account_dup_text(label_record->label);
4395
4396                 _account_type_free_label_with_items(label_record);
4397
4398                 rc = _account_query_step(hstmt);
4399         }
4400
4401         rc = _account_query_finalize(hstmt);
4402         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4403         hstmt = NULL;
4404
4405         error_code = _ACCOUNT_ERROR_NONE;
4406
4407 CATCH:
4408         if (hstmt != NULL) {
4409                 rc = _account_query_finalize(hstmt);
4410                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4411                 hstmt = NULL;
4412         }
4413
4414         _INFO("_account_type_query_label_by_locale_from_global_db() end : error_code = %d", error_code);
4415         return error_code;
4416 }
4417
4418 // output parameter label must be free
4419 int _account_type_query_label_by_locale(const char* app_id, const char* locale, char **label)
4420 {
4421         int                     error_code = _ACCOUNT_ERROR_NONE;
4422         account_stmt    hstmt = NULL;
4423         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4424         int                     rc = 0, binding_count = 1;
4425         char*                   converted_locale = NULL;
4426
4427         ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
4428         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4429         ACCOUNT_RETURN_VAL((label != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
4430         ACCOUNT_RETURN_VAL((locale != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("locale char is null"));
4431         //Making label newly created
4432
4433         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4434
4435         converted_locale = _account_dup_text(locale);
4436         gchar** tokens = g_strsplit(converted_locale, "-", 2);
4437
4438         if(tokens != NULL) {
4439                 if((char*)(tokens[1]) != NULL) {
4440                         char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1]));
4441                         if(upper_token != NULL) {
4442                                 _ACCOUNT_FREE(converted_locale);
4443                                 converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token);
4444                         }
4445                         _ACCOUNT_FREE(upper_token);
4446                 }
4447         }
4448         g_strfreev(tokens);
4449
4450         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, converted_locale);
4451         _ACCOUNT_FREE(converted_locale);
4452
4453         hstmt = _account_prepare_query(g_hAccountDB, query);
4454
4455         if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
4456                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4457                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4458         }
4459
4460         _account_query_bind_text(hstmt, binding_count++, app_id);
4461
4462         rc = _account_query_step(hstmt);
4463         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4464
4465         label_s* label_record = NULL;
4466
4467         while (rc == SQLITE_ROW) {
4468                 label_record = (label_s*) malloc(sizeof(label_s));
4469
4470                 if (label_record == NULL) {
4471                         ACCOUNT_FATAL("malloc Failed");
4472                         break;
4473                 }
4474
4475                 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
4476
4477                 _account_type_convert_column_to_label(hstmt,label_record);
4478
4479                 _ACCOUNT_FREE(*label);
4480                 //Making label newly created
4481                 *label = _account_dup_text(label_record->label);
4482
4483                 _account_type_free_label_with_items(label_record);
4484
4485                 rc = _account_query_step(hstmt);
4486         }
4487
4488         rc = _account_query_finalize(hstmt);
4489         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4490         hstmt = NULL;
4491
4492         error_code = _ACCOUNT_ERROR_NONE;
4493
4494         CATCH:
4495         if (hstmt != NULL) {
4496                 rc = _account_query_finalize(hstmt);
4497                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4498                 hstmt = NULL;
4499         }
4500
4501         if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4502                 error_code = _account_type_query_label_by_locale_from_global_db(app_id, locale, label);
4503         }
4504
4505         _INFO("_account_type_query_label_by_locale() end : error_code = %d", error_code);
4506         return error_code;
4507 }
4508
4509 static int _account_insert_custom(account_s *account, int account_id)
4510 {
4511         _INFO("_account_insert_custom start");
4512
4513         int                     rc, count = 1;
4514         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4515         account_stmt    hstmt = NULL;
4516
4517         ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4518
4519         if (g_slist_length( account->custom_list)==0) {
4520                 ACCOUNT_DEBUG( "_account_insert_custom, no custom data\n");
4521                 return _ACCOUNT_ERROR_NONE;
4522         }
4523
4524         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
4525
4526         rc = _account_get_record_count(g_hAccountDB, query);
4527
4528         if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
4529                 ACCOUNT_ERROR( "Access failed(%d, %s)", _account_db_err_msg(g_hAccountDB));
4530                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4531         }
4532
4533         if (rc <= 0) {
4534                 ACCOUNT_SLOGE( "_account_insert_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
4535                 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4536         }
4537
4538         /* insert query*/
4539
4540         GSList *iter;
4541
4542         for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
4543                 int ret;
4544                 count = 1;
4545                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4546                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (AccountId, AppId, Key, Value) VALUES "
4547                                 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
4548
4549                 hstmt = _account_prepare_query(g_hAccountDB, query);
4550
4551                 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
4552                         ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4553                         return _ACCOUNT_ERROR_PERMISSION_DENIED;
4554                 }
4555
4556                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
4557
4558                 account_custom_s* custom_data = NULL;
4559                 custom_data = (account_custom_s*)iter->data;
4560
4561                 ret = _account_query_bind_int(hstmt, count++, account_id);
4562                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
4563                 ret = _account_query_bind_text(hstmt, count++, account->package_name);
4564                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4565                 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
4566                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4567                 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
4568                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4569
4570                 rc = _account_query_step(hstmt);
4571
4572                 if (rc != SQLITE_DONE) {
4573                         ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4574                         break;
4575                 }
4576
4577                 rc = _account_query_finalize(hstmt);
4578                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4579                 hstmt = NULL;
4580
4581         }
4582
4583         _INFO("_account_insert_custom end");
4584         return _ACCOUNT_ERROR_NONE;
4585 }
4586
4587 static int _account_update_custom(account_s *account, int account_id)
4588 {
4589         int                     rc, count = 1;
4590         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4591         account_stmt    hstmt = NULL;
4592
4593         ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4594
4595         if (g_slist_length( account->custom_list)==0) {
4596                 ACCOUNT_DEBUG( "_account_update_custom, no custom data\n");
4597                 return _ACCOUNT_ERROR_NONE;
4598         }
4599
4600         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
4601
4602         rc = _account_get_record_count(g_hAccountDB, query);
4603
4604         if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
4605                 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4606                 pthread_mutex_unlock(&account_mutex);
4607                 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4608         } else if( _account_db_err_code(g_hAccountDB) == SQLITE_BUSY ){
4609                 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg(g_hAccountDB));
4610                 pthread_mutex_unlock(&account_mutex);
4611                 return _ACCOUNT_ERROR_DATABASE_BUSY;
4612         }
4613
4614         if (rc <= 0) {
4615                 ACCOUNT_SLOGE( "_account_update_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
4616                 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4617         }
4618
4619         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4620
4621         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId=? ", ACCOUNT_CUSTOM_TABLE);
4622         hstmt = _account_prepare_query(g_hAccountDB, query);
4623         count = 1;
4624         _account_query_bind_int(hstmt, count++, (int)account_id);
4625         rc = _account_query_step(hstmt);
4626
4627         if (rc == SQLITE_BUSY) {
4628                 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4629                 return _ACCOUNT_ERROR_DATABASE_BUSY;
4630         } else if (rc != SQLITE_DONE) {
4631                 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4632                 return _ACCOUNT_ERROR_DB_FAILED;
4633         }
4634
4635         rc = _account_query_finalize(hstmt);
4636         ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4637         hstmt = NULL;
4638
4639         GSList *iter;
4640
4641         for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
4642                 int ret;
4643                 count = 1;
4644                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4645                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AccountId, AppId, Key, Value) VALUES "
4646                                 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
4647
4648                 hstmt = _account_prepare_query(g_hAccountDB, query);
4649
4650                 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
4651                         ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4652                         return _ACCOUNT_ERROR_PERMISSION_DENIED;
4653                 }
4654
4655                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
4656
4657                 account_custom_s* custom_data = NULL;
4658                 custom_data = (account_custom_s*)iter->data;
4659
4660                 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
4661                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
4662                 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
4663                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4664                 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
4665                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4666                 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
4667                 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4668
4669                 rc = _account_query_step(hstmt);
4670
4671                 if (rc != SQLITE_DONE) {
4672                         ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4673                         break;
4674                 }
4675
4676                 rc = _account_query_finalize(hstmt);
4677                 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4678                 hstmt = NULL;
4679
4680         }
4681
4682         return _ACCOUNT_ERROR_NONE;
4683 }
4684