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