3 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
31 #include <pkgmgr-info.h>
32 //#include <tzplatform_config.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"
44 //typedef sqlite3_stmt* account_stmt;
46 #define EMAIL_SERVICE_CMDLINE "/usr/bin/email-service"
48 #define EMAIL_APPID "email-setting-efl"
50 #define ACCOUNT_DB_OPEN_READONLY 0
51 #define ACCOUNT_DB_OPEN_READWRITE 1
55 #define _TIZEN_PUBLIC_
56 #ifndef _TIZEN_PUBLIC_
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;
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);
72 static void _account_insert_delete_update_notification_send(char *noti_name)
75 _ERR("Noti Name is NULL!!!!!!\n");
79 _INFO("noti_type = %s", noti_name);
81 if (vconf_set_str(VCONFKEY_ACCOUNT_MSG_STR, noti_name) != 0) {
82 _ERR("Vconf MSG Str set FAILED !!!!!!\n");;
86 int _account_get_current_appid_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
90 GSList** appid_list = (GSList**)user_data;
93 pkgmgr_ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
95 if( pkgmgr_ret != PMINFO_R_OK ){
96 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_appid(%d)", pkgmgr_ret);
99 item = _account_dup_text(appid);
100 *appid_list = g_slist_append(*appid_list, item);
105 static inline int __read_proc(const char *path, char *buf, int size)
109 if (buf == NULL || path == NULL) {
110 ACCOUNT_ERROR("path and buffer is mandatory\n");
114 fd = open(path, O_RDONLY);
116 ACCOUNT_ERROR("fd open error(%d)\n", fd);
120 ret = read(fd, buf, size - 1);
122 ACCOUNT_ERROR("fd read error(%d)\n", fd);
133 char *_account_get_proc_cmdline_bypid(int pid)
138 ACCOUNT_SNPRINTF(buf, sizeof(buf), "/proc/%d/cmdline", pid);
139 ret = __read_proc(buf, buf, sizeof(buf));
141 ACCOUNT_DEBUG("No proc directory (%d)\n", pid);
150 static int _account_execute_query_from_global_db(const char *query)
153 char* pszErrorMsg = NULL;
156 ACCOUNT_ERROR("NULL query\n");
157 return _ACCOUNT_ERROR_QUERY_SYNTAX_ERROR;
160 if(!g_hAccountGlobalDB){
161 ACCOUNT_ERROR("Global DB is not opened\n");
162 return _ACCOUNT_ERROR_DB_NOT_OPENED;
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);
175 static int _account_begin_transaction_from_global_db(void)
177 ACCOUNT_DEBUG("_account_begin_transaction start");
180 ret = _account_execute_query_from_global_db("BEGIN IMMEDIATE TRANSACTION");
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;
190 ACCOUNT_DEBUG("_account_begin_transaction_in_global_db end");
191 return _ACCOUNT_ERROR_NONE;
194 static int _account_end_transaction_from_global_db(bool is_success)
196 ACCOUNT_DEBUG("_account_end_transaction_in_global_db start");
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");
204 ret = _account_execute_query_from_global_db("ROLLBACK TRANSACTION");
205 ACCOUNT_DEBUG("_account_end_transaction ROLLBACK");
208 if(ret == SQLITE_PERM) {
209 ACCOUNT_ERROR("Account permission denied :: %d", ret);
210 return _ACCOUNT_ERROR_PERMISSION_DENIED;
213 if (ret == SQLITE_BUSY){
214 ACCOUNT_DEBUG(" sqlite3 busy = %d", ret);
215 return _ACCOUNT_ERROR_DATABASE_BUSY;
218 if (ret != SQLITE_OK) {
219 ACCOUNT_ERROR("_account_svc_end_transaction_in_global_db fail :: %d", ret);
220 return _ACCOUNT_ERROR_DB_FAILED;
223 ACCOUNT_DEBUG("_account_end_transaction_in_global_db end");
224 return _ACCOUNT_ERROR_NONE;
228 int _account_global_db_open(void)
232 char account_db_path[256] = {0, };
234 _INFO( "start _account_global_db_open()");
236 ACCOUNT_MEMSET(account_db_path, 0x00, sizeof(account_db_path));
237 ACCOUNT_GET_GLOBAL_DB_PATH(account_db_path, sizeof(account_db_path));
239 if( g_hAccountGlobalDB ) {
240 _ERR( "Account database is using in another app. %x", g_hAccountDB );
241 return _ACCOUNT_ERROR_DATABASE_BUSY;
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);
248 ACCOUNT_DEBUG( "before db_util_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);
254 // return _ACCOUNT_ERROR_DB_NOT_OPENED;
255 ACCOUNT_DEBUG( "after db_util_open() sqlite_rc = %d", rc);
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;
262 if( rc == SQLITE_BUSY ) {
263 ACCOUNT_ERROR( "busy handler fail.");
264 return _ACCOUNT_ERROR_DATABASE_BUSY;
267 if( rc != SQLITE_OK ) {
268 ACCOUNT_ERROR( "The database isn't connected." );
269 return _ACCOUNT_ERROR_DB_NOT_OPENED;
272 _INFO( "end _account_global_db_open()");
273 return _ACCOUNT_ERROR_NONE;
276 int _account_global_db_close(void)
278 ACCOUNT_DEBUG( "start account_global_db_close()");
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);
285 ret = _account_db_handle_close(g_hAccountGlobalDB);
286 if( ret != _ACCOUNT_ERROR_NONE )
288 ACCOUNT_ERROR( "db_util_close(g_hAccountGlobalDB) fail ret = %d", ret);
289 g_hAccountGlobalDB2 = g_hAccountGlobalDB;
291 g_hAccountGlobalDB = NULL;
296 static bool _account_check_add_more_account(const char* app_id)
298 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
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."));
304 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
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);
309 /* multiple account support case */
311 ACCOUNT_SLOGD("app id (%s) supports multiple account. rc(%d)\n", app_id, rc);
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);
321 ACCOUNT_SLOGD("app id (%s) supports single account. and there is no account of the app id\n", app_id);
329 int _account_db_open(int mode, int pid, uid_t uid)
333 char account_db_dir[256] = {0, };
334 char account_db_path[256] = {0, };
336 _INFO( "start _account_db_open()");
338 ACCOUNT_MEMSET(account_db_dir, 0x00, sizeof(account_db_dir));
339 ACCOUNT_MEMSET(account_db_path, 0x00, sizeof(account_db_path));
341 ACCOUNT_GET_USER_DB_PATH(account_db_path, sizeof(account_db_path), uid);
344 _ERR( "Account database is using in another app. %x", g_hAccountDB );
345 return _ACCOUNT_ERROR_DATABASE_BUSY;
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);
352 ACCOUNT_GET_USER_DB_DIR(account_db_dir, sizeof(account_db_dir), uid);
353 if (-1 == access (account_db_dir, F_OK)) {
354 mkdir(account_db_dir, 644);
357 ACCOUNT_DEBUG( "before db_util_open()");
358 // if(mode == ACCOUNT_DB_OPEN_READWRITE)
359 rc = db_util_open(account_db_path, &g_hAccountDB, DB_UTIL_REGISTER_HOOK_METHOD);
360 // else if(mode == ACCOUNT_DB_OPEN_READONLY)
361 // rc = db_util_open_with_options(account_db_path, &g_hAccountDB, SQLITE_OPEN_READONLY, NULL);
363 // return _ACCOUNT_ERROR_DB_NOT_OPENED;
364 ACCOUNT_DEBUG( "after db_util_open() sqlite_rc = %d", rc);
366 if( rc == SQLITE_PERM || _account_db_err_code(g_hAccountDB) == SQLITE_PERM ) {
367 ACCOUNT_ERROR( "Account permission denied");
368 return _ACCOUNT_ERROR_PERMISSION_DENIED;
371 if( rc == SQLITE_BUSY ) {
372 ACCOUNT_ERROR( "busy handler fail.");
373 return _ACCOUNT_ERROR_DATABASE_BUSY;
376 if( rc != SQLITE_OK ) {
377 ACCOUNT_ERROR( "The database isn't connected." );
378 return _ACCOUNT_ERROR_DB_NOT_OPENED;
381 rc = _account_check_is_all_table_exists(g_hAccountDB);
384 _ERR("_account_check_is_all_table_exists rc=[%d]", rc);
386 } else if (rc == ACCOUNT_TABLE_TOTAL_COUNT) {
389 int ret = _account_create_all_tables(g_hAccountDB);
390 if (ret != _ACCOUNT_ERROR_NONE) {
391 _ERR("_account_create_all_tables fail ret=[%d]", ret);
396 _INFO( "end _account_db_open()");
397 return _ACCOUNT_ERROR_NONE;
400 int _account_db_close(void)
402 ACCOUNT_DEBUG( "start db_util_close()");
405 ret = _account_db_handle_close(g_hAccountDB2);
406 if( ret != _ACCOUNT_ERROR_NONE )
407 ACCOUNT_DEBUG( "db_util_close(g_hAccountDB2) fail ret = %d", ret);
409 ret = _account_db_handle_close(g_hAccountDB);
410 if( ret != _ACCOUNT_ERROR_NONE )
412 ACCOUNT_ERROR( "db_util_close(g_hAccountDB) fail ret = %d", ret);
413 g_hAccountDB2 = g_hAccountDB;
420 static int _account_execute_insert_query(account_s *account)
422 _INFO("_account_execute_insert_query start");
425 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
426 int error_code = _ACCOUNT_ERROR_NONE;
427 account_stmt hstmt = NULL;
429 /* check whether app id exist in account type db */
431 if (!account->user_name && !account->display_name && !account->email_address) {
433 ACCOUNT_ERROR("Mandetory fields is NULL. At least one field is required among username, display name, email address\n");
434 return _ACCOUNT_ERROR_INVALID_PARAMETER;
438 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
439 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (user_name, email_address , display_name , icon_path , source , package_name , "
440 "access_token , domain_name , auth_type , secret , sync_support , txt_custom0, txt_custom1, txt_custom2, txt_custom3, txt_custom4, "
441 "int_custom0, int_custom1, int_custom2, int_custom3, int_custom4, txt_custom0 ) values " // to do urusa
442 "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", ACCOUNT_TABLE);
444 hstmt = _account_prepare_query(g_hAccountDB, query);
445 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
448 _account_convert_account_to_sql(account, hstmt, query);
451 rc = _account_query_step(hstmt);
452 if (rc != SQLITE_DONE) {
454 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
456 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM )
457 error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
459 error_code = _ACCOUNT_ERROR_DB_FAILED;
463 rc = _account_query_finalize(hstmt);
464 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
467 _INFO("_account_execute_insert_query end");
471 static int _account_insert_capability(account_s *account, int account_id)
473 _INFO("_account_insert_capability start");
475 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
476 account_stmt hstmt = NULL;
478 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
480 if (g_slist_length( account->capablity_list)==0) {
481 ACCOUNT_DEBUG( "_account_insert_capability, no capability\n");
482 return _ACCOUNT_ERROR_NONE;
485 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
487 _INFO("_account_insert_capability _account_get_record_count [%s]", query);
488 rc = _account_get_record_count(g_hAccountDB, query);
490 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
491 _ERR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
492 return _ACCOUNT_ERROR_PERMISSION_DENIED;
495 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
502 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
506 account_capability_s* cap_data = NULL;
507 cap_data = (account_capability_s*)iter->data;
509 _INFO("cap_data->type = %s, cap_data->value = %d \n", cap_data->type, cap_data->value);
511 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
512 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
513 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
514 hstmt = _account_prepare_query(g_hAccountDB, query);
516 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
518 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
519 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
520 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
521 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
522 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
523 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
524 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
525 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
526 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
527 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
529 rc = _account_query_step(hstmt);
530 _INFO("_account_insert_capability _account_query_step[%d]", rc);
532 if (rc != SQLITE_DONE) {
533 _ERR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
537 rc = _account_query_finalize(hstmt);
538 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
543 _INFO("_account_insert_capability end");
544 return _ACCOUNT_ERROR_NONE;
547 static int _account_update_capability(account_s *account, int account_id)
550 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
551 account_stmt hstmt = NULL;
553 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
555 if (g_slist_length( account->capablity_list)==0) {
556 ACCOUNT_ERROR( "_account_update_capability, no capability\n");
557 return _ACCOUNT_ERROR_NONE;
560 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
562 rc = _account_get_record_count(g_hAccountDB, query);
565 ACCOUNT_SLOGI( "_account_update_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
566 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
569 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
571 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id=? ", CAPABILITY_TABLE);
572 hstmt = _account_prepare_query(g_hAccountDB, query);
574 _account_query_bind_int(hstmt, count++, (int)account_id);
575 rc = _account_query_step(hstmt);
577 if (rc != SQLITE_DONE) {
578 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
579 return _ACCOUNT_ERROR_DB_FAILED;
581 rc = _account_query_finalize(hstmt);
582 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
587 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
590 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
591 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
592 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
594 hstmt = _account_prepare_query(g_hAccountDB, query);
596 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
598 account_capability_s* cap_data = NULL;
599 cap_data = (account_capability_s*)iter->data;
601 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
602 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
603 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
604 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
605 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
606 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
607 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
608 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
609 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
610 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
612 rc = _account_query_step(hstmt);
614 if (rc != SQLITE_DONE) {
615 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
619 rc = _account_query_finalize(hstmt);
620 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
625 return _ACCOUNT_ERROR_NONE;
628 static int _account_update_capability_by_user_name(account_s *account, const char *user_name, const char *package_name )
631 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
632 account_stmt hstmt = NULL;
634 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
636 if (g_slist_length( account->capablity_list)==0) {
637 ACCOUNT_ERROR( "_account_update_capability_by_user_name, no capability\n");
638 return _ACCOUNT_ERROR_NONE;
641 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where package_name= '%s' and user_name='%s'", ACCOUNT_TABLE, package_name, user_name);
643 rc = _account_get_record_count(g_hAccountDB, query);
646 ACCOUNT_SLOGI( "_account_update_capability_by_user_name : related account item is not existed rc=%d , %s ", rc, _account_db_err_msg(g_hAccountDB));
647 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
650 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
652 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name=? and user_name=? ", CAPABILITY_TABLE);
653 hstmt = _account_prepare_query(g_hAccountDB, query);
655 _account_query_bind_text(hstmt, count++, (char*)account->package_name);
656 _account_query_bind_text(hstmt, count++, (char*)account->user_name);
657 rc = _account_query_step(hstmt);
658 if (rc != SQLITE_DONE) {
659 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
660 return _ACCOUNT_ERROR_DB_FAILED;
663 rc = _account_query_finalize(hstmt);
664 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
669 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
672 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
673 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
674 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
676 hstmt = _account_prepare_query(g_hAccountDB, query);
678 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
680 account_capability_s* cap_data = NULL;
681 cap_data = (account_capability_s*)iter->data;
683 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
684 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
685 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
686 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
687 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
688 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
689 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
690 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
691 ret = _account_query_bind_int(hstmt, count++, (int)account->id);
692 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
694 rc = _account_query_step(hstmt);
696 if (rc != SQLITE_DONE) {
697 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
701 rc = _account_query_finalize(hstmt);
702 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
707 return _ACCOUNT_ERROR_NONE;
711 bool _account_add_capability_to_account_cb(const char* capability_type, int capability_value, account_s *account)
713 account_capability_s *cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
715 if (cap_data == NULL)
717 ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
719 cap_data->type = _account_dup_text(capability_type);
720 cap_data->value = capability_value;
721 _INFO("cap_data->type = %s, cap_data->value = %d", cap_data->type, cap_data->value);
723 account->capablity_list = g_slist_append(account->capablity_list, (gpointer)cap_data);
729 bool _account_add_custom_to_account_cb(const char* key, const char* value, account_s *account)
731 account_custom_s *custom_data = (account_custom_s*)malloc(sizeof(account_custom_s));
733 if (custom_data == NULL) {
734 ACCOUNT_DEBUG("_account_add_custom_to_account_cb :: malloc fail\n");
737 ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s));
739 custom_data->account_id = account->id;
740 custom_data->app_id = _account_dup_text(account->package_name);
741 custom_data->key = _account_dup_text(key);
742 custom_data->value = _account_dup_text(value);
743 _INFO("custom_data->key = %s, custom_data->value = %s", custom_data->key, custom_data->value);
745 account->custom_list = g_slist_append(account->custom_list, (gpointer)custom_data);
751 static int _account_compare_old_record_by_user_name(account_s *new_account, const char* user_name, const char* package_name)
753 int error_code = _ACCOUNT_ERROR_NONE;
754 account_stmt hstmt = NULL;
755 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
757 account_s *old_account = NULL;
759 ACCOUNT_RETURN_VAL((new_account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
760 ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
761 ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
762 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
764 old_account = (account_s*)calloc(1, sizeof(account_s));
766 ACCOUNT_FATAL("Memory alloc fail\n");
767 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
770 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
772 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = '%s' and package_name='%s'", ACCOUNT_TABLE, user_name, package_name);
773 hstmt = _account_prepare_query(g_hAccountDB, query);
775 rc = _account_query_step(hstmt);
776 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
778 while (rc == SQLITE_ROW) {
779 _account_convert_column_to_account(hstmt, old_account);
780 rc = _account_query_step(hstmt);
783 rc = _account_query_finalize(hstmt);
784 ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
788 error_code = _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, old_account->id, (void*)old_account);
789 ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
792 error_code = _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, old_account->id, (void*)old_account);
793 ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
796 new_account->id = old_account->id;
799 if(!new_account->user_name) {
800 if(old_account->user_name)
801 new_account->user_name = _account_dup_text(old_account->user_name);
805 if(!new_account->display_name) {
806 if(old_account->display_name)
807 new_account->display_name = _account_dup_text(old_account->display_name);
811 if(!new_account->email_address) {
812 if(old_account->email_address)
813 new_account->email_address = _account_dup_text(old_account->email_address);
817 if(!new_account->domain_name) {
818 if(old_account->domain_name)
819 new_account->domain_name = _account_dup_text(old_account->domain_name);
823 if(!new_account->icon_path) {
824 if(old_account->icon_path)
825 new_account->icon_path = _account_dup_text(old_account->icon_path);
829 if(!new_account->source) {
830 if(old_account->source)
831 new_account->source = _account_dup_text(old_account->source);
834 _ACCOUNT_FREE(new_account->package_name);
835 new_account->package_name = _account_dup_text(old_account->package_name);
838 if(!new_account->access_token) {
839 if(old_account->access_token)
840 new_account->access_token = _account_dup_text(old_account->access_token);
844 if(new_account->auth_type == _ACCOUNT_AUTH_TYPE_INVALID) {
845 new_account->auth_type = old_account->auth_type;
849 if(new_account->secret== _ACCOUNT_SECRECY_INVALID) {
850 new_account->secret = old_account->secret;
854 if(new_account->sync_support == _ACCOUNT_SYNC_INVALID) {
855 new_account->sync_support = old_account->sync_support;
860 for(i=0;i<USER_TXT_CNT;i++) {
861 if(!new_account->user_data_txt[i]) {
862 if(old_account->user_data_txt[i])
863 new_account->user_data_txt[i] = _account_dup_text(old_account->user_data_txt[i]);
868 for(i=0;i<USER_INT_CNT;i++) {
869 if(new_account->user_data_int[i] == 0) {
870 new_account->user_data_int[i] = old_account->user_data_int[i];
880 _account_free_account_with_items(old_account);
884 rc = _account_query_finalize(hstmt);
885 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
889 return _ACCOUNT_ERROR_NONE;
894 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 int rc = 0, binding_count = 0, count = 0;
897 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
898 int error_code = _ACCOUNT_ERROR_NONE;
899 account_stmt hstmt = NULL;
901 ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is NULL.\n"));
902 ACCOUNT_RETURN_VAL((package_name!= NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is NULL.\n"));
904 char* current_appid = NULL;
905 char* verified_appid = NULL;
907 current_appid = _account_get_current_appid(pid, uid);
908 error_code = _account_get_represented_appid_from_db(g_hAccountDB, g_hAccountGlobalDB, current_appid, uid, &verified_appid);
910 _ACCOUNT_FREE(current_appid);
911 _ACCOUNT_FREE(verified_appid);
913 if(error_code != _ACCOUNT_ERROR_NONE){
914 ACCOUNT_ERROR("No permission to update\n");
915 return _ACCOUNT_ERROR_PERMISSION_DENIED;
918 error_code = encrypt_access_token(account);
919 if (error_code != _ACCOUNT_ERROR_NONE)
921 _ERR("_encrypt_access_token error");
925 _account_compare_old_record_by_user_name(account, user_name, package_name);
927 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
928 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
929 return _ACCOUNT_ERROR_PERMISSION_DENIED;
932 if (!account->package_name) {
933 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
934 return _ACCOUNT_ERROR_INVALID_PARAMETER;
937 if (!account->user_name && !account->display_name && !account->email_address) {
938 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
939 return _ACCOUNT_ERROR_INVALID_PARAMETER;
942 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE user_name='%s' and package_name='%s'"
943 , ACCOUNT_TABLE, user_name, package_name);
945 count = _account_get_record_count(g_hAccountDB, query);
947 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
948 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
949 return _ACCOUNT_ERROR_PERMISSION_DENIED;
953 ACCOUNT_SLOGI("_account_update_account_by_user_name : The account not exist!, count = %d, user_name=%s, package_name=%s\n",
954 count, user_name, package_name);
955 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
958 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
960 //TODO: Is it required to update id ? As of now I can only think of falied rollback cases (between account and gSSO DB)
961 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
962 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
963 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
964 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE user_name=? and package_name=? ", ACCOUNT_TABLE);
966 hstmt = _account_prepare_query(g_hAccountDB, query);
967 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
968 _account_end_transaction(g_hAccountDB, FALSE);
969 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
970 return _ACCOUNT_ERROR_PERMISSION_DENIED;
972 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
974 binding_count = _account_convert_account_to_sql(account, hstmt, query);
976 _account_query_bind_text(hstmt, binding_count++, user_name);
977 _account_query_bind_text(hstmt, binding_count++, package_name);
978 rc = _account_query_step(hstmt);
979 if (rc != SQLITE_DONE) {
980 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
982 rc = _account_query_finalize(hstmt);
983 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
986 /*update capability*/
987 error_code = _account_update_capability_by_user_name(account, user_name, package_name);
990 error_code = _account_update_custom(account, account->id);
995 int _account_insert_to_db(account_s* account, int pid, uid_t uid, int *account_id)
998 int error_code = _ACCOUNT_ERROR_NONE;
999 int ret_transaction = 0;
1001 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1002 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1003 ACCOUNT_RETURN_VAL((account_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID POINTER IS NULL"));
1005 if (!account->user_name && !account->display_name && !account->email_address) {
1006 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1007 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1010 account_s *data = (account_s*)account;
1011 ACCOUNT_SLOGD("(%s)-(%d) account_insert_to_db: begin_transaction.\n", __FUNCTION__, __LINE__);
1013 pthread_mutex_lock(&account_mutex);
1015 /* transaction control required*/
1016 ret_transaction = _account_begin_transaction(g_hAccountDB);
1018 if(_account_db_err_code(g_hAccountDB) == SQLITE_PERM){
1019 pthread_mutex_unlock(&account_mutex);
1020 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1021 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1024 if (ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY) {
1025 ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction);
1026 pthread_mutex_unlock(&account_mutex);
1027 return _ACCOUNT_ERROR_DATABASE_BUSY;
1028 }else if (ret_transaction != _ACCOUNT_ERROR_NONE) {
1029 ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction);
1030 pthread_mutex_unlock(&account_mutex);
1031 return ret_transaction;
1034 *account_id = _account_get_next_sequence(g_hAccountDB, ACCOUNT_TABLE);
1035 data->id = *account_id;
1038 appid = _account_get_current_appid(pid, uid);
1043 // API caller cannot be recognized
1044 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1045 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1046 pthread_mutex_unlock(&account_mutex);
1047 return _ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
1051 char* verified_appid = NULL;
1052 error_code = _account_get_represented_appid_from_db(g_hAccountDB, g_hAccountGlobalDB, appid, uid, &verified_appid);//FIX
1053 _ACCOUNT_FREE(appid);
1054 if(error_code != _ACCOUNT_ERROR_NONE)
1056 _ERR("error_code = %d", error_code);
1057 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1058 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1059 _ACCOUNT_FREE(verified_appid);
1060 pthread_mutex_unlock(&account_mutex);
1067 error_code = _account_check_duplicated(g_hAccountDB, data, verified_appid, uid);
1068 if (error_code != _ACCOUNT_ERROR_NONE) {
1070 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1071 ACCOUNT_DEBUG("_account_check_duplicated(), rollback insert query(%x)!!!!\n", ret_transaction);
1073 pthread_mutex_unlock(&account_mutex);
1076 if(!_account_check_add_more_account(verified_appid)) {
1077 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1078 ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
1079 pthread_mutex_unlock(&account_mutex);
1080 _ACCOUNT_FREE(verified_appid);
1081 return _ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
1084 _ACCOUNT_FREE(data->package_name);
1085 data->package_name = _account_dup_text(verified_appid);
1086 _ACCOUNT_FREE(verified_appid);
1089 if(!_account_check_add_more_account(data->package_name))
1092 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1093 ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
1094 pthread_mutex_unlock(&account_mutex);
1095 return _ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
1098 error_code = encrypt_access_token(data);
1099 if (error_code != _ACCOUNT_ERROR_NONE)
1101 _ERR("_encrypt_access_token error");
1105 error_code = _account_execute_insert_query(data);
1107 if (error_code != _ACCOUNT_ERROR_NONE)
1110 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1111 ACCOUNT_ERROR("INSERT account fail, rollback insert query(%x)!!!!\n", ret_transaction);
1113 pthread_mutex_unlock(&account_mutex);
1118 error_code = _account_insert_capability(data, *account_id);
1119 if (error_code != _ACCOUNT_ERROR_NONE)
1122 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1123 ACCOUNT_ERROR("INSERT capability fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1125 pthread_mutex_unlock(&account_mutex);
1130 error_code = _account_insert_custom(data, *account_id);
1131 if (error_code != _ACCOUNT_ERROR_NONE)
1133 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1134 ACCOUNT_ERROR("INSERT custom fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1136 pthread_mutex_unlock(&account_mutex);
1142 pthread_mutex_unlock(&account_mutex);
1143 _account_end_transaction(g_hAccountDB, TRUE);
1144 ACCOUNT_SLOGD("(%s)-(%d) account _end_transaction.\n", __FUNCTION__, __LINE__);
1147 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_INSERT, *account_id);
1148 _account_insert_delete_update_notification_send(buf);
1149 _INFO("account _notification_send end.");
1151 return _ACCOUNT_ERROR_NONE;
1155 GSList* _account_get_capability_list_by_account_id(int account_id, int *error_code)
1157 *error_code = _ACCOUNT_ERROR_NONE;
1158 account_stmt hstmt = NULL;
1159 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1161 GSList* capability_list = NULL;
1163 ACCOUNT_RETURN_VAL((account_id > 0), {*error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("ACCOUNT INDEX IS LESS THAN 0"));
1164 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
1166 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1168 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
1169 hstmt = _account_prepare_query(g_hAccountDB, query);
1171 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
1172 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1173 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
1177 rc = _account_query_step(hstmt);
1178 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1180 account_capability_s* capability_record = NULL;
1182 while (rc == SQLITE_ROW) {
1183 capability_record = (account_capability_s*) malloc(sizeof(account_capability_s));
1185 if (capability_record == NULL) {
1186 ACCOUNT_FATAL("malloc Failed");
1190 ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s));
1192 _account_convert_column_to_capability(hstmt, capability_record);
1194 //cb_ret = callback(capability_record->type, capability_record->value, user_data);
1196 //_account_free_capability_items(capability_record);
1197 //_ACCOUNT_FREE(capability_record);
1199 //ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
1201 capability_list = g_slist_append(capability_list, capability_record);
1202 rc = _account_query_step(hstmt);
1205 rc = _account_query_finalize(hstmt);
1206 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
1209 *error_code = _ACCOUNT_ERROR_NONE;
1214 rc = _account_query_finalize(hstmt);
1215 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
1219 pthread_mutex_unlock(&account_mutex);
1220 return capability_list;
1223 static int _account_compare_old_record(account_s *new_account, int account_id)
1225 int error_code = _ACCOUNT_ERROR_NONE;
1226 account_stmt hstmt = NULL;
1227 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1229 account_s *old_account = NULL;
1231 ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1232 ACCOUNT_RETURN_VAL((new_account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
1233 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1235 old_account = (account_s*)calloc(1, sizeof(account_s));
1236 if (old_account == NULL) {
1237 _ERR("Out of Memory");
1238 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
1241 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1243 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
1244 hstmt = _account_prepare_query(g_hAccountDB, query);
1246 rc = _account_query_step(hstmt);
1247 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1249 while (rc == SQLITE_ROW) {
1250 _account_convert_column_to_account(hstmt, old_account);
1251 rc = _account_query_step(hstmt);
1254 rc = _account_query_finalize(hstmt);
1255 ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1259 error_code = _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, old_account->id, (void*)old_account);
1260 ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
1263 error_code = _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, old_account->id, (void*)old_account);
1264 ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
1268 new_account->id = old_account->id;
1271 if(!new_account->user_name) {
1272 if(old_account->user_name)
1273 new_account->user_name = _account_dup_text(old_account->user_name);
1277 if(!new_account->display_name) {
1278 if(old_account->display_name)
1279 new_account->display_name = _account_dup_text(old_account->display_name);
1283 if(!new_account->email_address) {
1284 if(old_account->email_address)
1285 new_account->email_address = _account_dup_text(old_account->email_address);
1289 if(!new_account->domain_name) {
1290 if(old_account->domain_name)
1291 new_account->domain_name = _account_dup_text(old_account->domain_name);
1295 if(!new_account->icon_path) {
1296 if(old_account->icon_path)
1297 new_account->icon_path = _account_dup_text(old_account->icon_path);
1301 if(!new_account->source) {
1302 if(old_account->source)
1303 new_account->source = _account_dup_text(old_account->source);
1306 _ACCOUNT_FREE(new_account->package_name);
1307 new_account->package_name = _account_dup_text(old_account->package_name);
1310 if(!new_account->access_token) {
1311 if(old_account->access_token)
1312 new_account->access_token = _account_dup_text(old_account->access_token);
1317 for(i=0;i<USER_TXT_CNT;i++) {
1318 if(!new_account->user_data_txt[i]) {
1319 if(old_account->user_data_txt[i])
1320 new_account->user_data_txt[i] = _account_dup_text(old_account->user_data_txt[i]);
1325 if(new_account->auth_type == _ACCOUNT_AUTH_TYPE_INVALID) {
1326 new_account->auth_type = old_account->auth_type;
1330 if(new_account->secret== _ACCOUNT_SECRECY_INVALID) {
1331 new_account->secret = old_account->secret;
1335 if(new_account->sync_support == _ACCOUNT_SYNC_INVALID) {
1336 new_account->sync_support = old_account->sync_support;
1340 for(i=0;i<USER_INT_CNT;i++) {
1341 if(new_account->user_data_int[i] == 0) {
1342 new_account->user_data_int[i] = old_account->user_data_int[i];
1348 // user custom table
1352 _account_free_account_with_items(old_account);
1354 if (hstmt != NULL) {
1355 rc = _account_query_finalize(hstmt);
1356 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1360 return _ACCOUNT_ERROR_NONE;
1363 static int _account_get_package_name_from_account_id(int account_id, char **package_name)
1365 int error_code = _ACCOUNT_ERROR_NONE;
1366 account_stmt hstmt = NULL;
1367 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1369 account_s *old_account = NULL;
1371 ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1372 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1374 old_account = (account_s*)calloc(1, sizeof(account_s));
1375 if (old_account == NULL) {
1376 _ERR("Out Of memory");
1377 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
1380 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1382 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
1383 hstmt = _account_prepare_query(g_hAccountDB, query);
1385 rc = _account_query_step(hstmt);
1386 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1388 while (rc == SQLITE_ROW) {
1389 _account_convert_column_to_account(hstmt, old_account);
1390 rc = _account_query_step(hstmt);
1393 rc = _account_query_finalize(hstmt);
1394 ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1397 // get package name.
1398 *package_name = _account_dup_text(old_account->package_name);
1403 _account_free_account_with_items(old_account);
1406 if (hstmt != NULL) {
1407 rc = _account_query_finalize(hstmt);
1408 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1416 static int _account_update_account(int pid, uid_t uid, account_s *account, int account_id)
1418 int rc = 0, binding_count =0;
1419 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1420 int error_code = _ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0;
1421 account_stmt hstmt = NULL;
1423 if (!account->package_name) {
1424 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
1425 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1428 /* Check permission of requested appid */
1429 char* current_appid = NULL;
1430 char *package_name = NULL;
1432 current_appid = _account_get_current_appid(pid, uid);
1433 error_code = _account_get_package_name_from_account_id(account_id, &package_name);
1435 if(error_code != _ACCOUNT_ERROR_NONE || package_name == NULL){
1436 ACCOUNT_ERROR("No package name with account_id\n");
1437 _ACCOUNT_FREE(current_appid);
1438 _ACCOUNT_FREE(package_name);
1439 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1442 error_code = _account_check_appid_group_with_package_name(current_appid, package_name, uid);
1443 ACCOUNT_DEBUG( "UPDATE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name); // TODO: remove the log later.
1445 _ACCOUNT_FREE(current_appid);
1446 _ACCOUNT_FREE(package_name);
1448 if(error_code != _ACCOUNT_ERROR_NONE){
1449 ACCOUNT_ERROR("No permission to update\n");
1450 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1453 error_code = encrypt_access_token(account);
1454 if (error_code != _ACCOUNT_ERROR_NONE)
1456 _ERR("_encrypt_access_token error");
1460 error_code = _account_compare_old_record(account, account_id);
1461 if (error_code != _ACCOUNT_ERROR_NONE) {
1462 ACCOUNT_ERROR("_account_compare_old_record fail\n");
1466 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
1467 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1468 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1469 } else if( _account_db_err_code(g_hAccountDB) == SQLITE_BUSY ){
1470 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg(g_hAccountDB));
1471 return _ACCOUNT_ERROR_DATABASE_BUSY;
1474 if (!account->user_name && !account->display_name && !account->email_address) {
1475 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1476 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1479 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1481 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
1483 count = _account_get_record_count(g_hAccountDB, query);
1485 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
1486 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1489 /* transaction control required*/
1490 ret_transaction = _account_begin_transaction(g_hAccountDB);
1491 if( ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY ){
1492 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg(g_hAccountDB));
1493 pthread_mutex_unlock(&account_mutex);
1494 return _ACCOUNT_ERROR_DATABASE_BUSY;
1497 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1498 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
1499 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
1500 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
1501 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
1503 hstmt = _account_prepare_query(g_hAccountDB, query);
1505 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
1506 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1507 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1508 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1511 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)));
1513 binding_count = _account_convert_account_to_sql(account, hstmt, query);
1514 _account_query_bind_int(hstmt, binding_count++, account_id);
1516 rc = _account_query_step(hstmt);
1517 if (rc != SQLITE_DONE) {
1518 ACCOUNT_SLOGE( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
1521 rc = _account_query_finalize(hstmt);
1522 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1525 _INFO("update query=%s", query);
1527 /*update capability*/
1528 error_code = _account_update_capability(account, account_id);
1529 if(error_code != _ACCOUNT_ERROR_NONE && error_code!= _ACCOUNT_ERROR_RECORD_NOT_FOUND){
1530 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1531 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1536 error_code = _account_update_custom(account, account_id);
1537 if(error_code != _ACCOUNT_ERROR_NONE && error_code!= _ACCOUNT_ERROR_RECORD_NOT_FOUND){
1538 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1539 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1543 ret_transaction = _account_end_transaction(g_hAccountDB, TRUE);
1545 _INFO("update end");
1550 static int _account_update_account_ex(account_s *account, int account_id)
1552 int rc = 0, binding_count =0;
1553 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1554 int error_code = _ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0;
1555 account_stmt hstmt = NULL;
1557 if (!account->package_name) {
1558 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
1559 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1562 error_code = encrypt_access_token(account);
1563 if (error_code != _ACCOUNT_ERROR_NONE)
1565 _ERR("_encrypt_access_token error");
1569 error_code = _account_compare_old_record(account, account_id);
1570 if (error_code != _ACCOUNT_ERROR_NONE) {
1571 ACCOUNT_ERROR("_account_compare_old_record fail\n");
1575 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
1576 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1577 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1580 if (!account->user_name && !account->display_name && !account->email_address) {
1581 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1582 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1585 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1587 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
1589 count = _account_get_record_count(g_hAccountDB, query);
1591 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
1592 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1595 /* transaction control required*/
1596 ret_transaction = _account_begin_transaction(g_hAccountDB);
1597 if( ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY ){
1598 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg(g_hAccountDB));
1599 pthread_mutex_unlock(&account_mutex);
1600 return _ACCOUNT_ERROR_DATABASE_BUSY;
1603 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1604 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
1605 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
1606 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
1607 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
1609 hstmt = _account_prepare_query(g_hAccountDB, query);
1611 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
1612 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1613 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1614 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1617 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)));
1619 _INFO("account_update_to_db_by_id_ex_p : before convert() : account_id[%d], user_name=%s", account->id, account->user_name);
1620 binding_count = _account_convert_account_to_sql(account, hstmt, query);
1621 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], user_name=%s", account->id, account->user_name);
1622 _INFO("account_update_to_db_by_id_ex_p : before bind()");
1623 rc = _account_query_bind_int(hstmt, binding_count++, account_id);
1624 _INFO("account_update_to_db_by_id_ex_p : after bind() : ret = %d", rc);
1626 rc = _account_query_step(hstmt);
1627 if (rc != SQLITE_DONE) {
1628 ACCOUNT_SLOGE( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
1630 _INFO("account_update_to_db_by_id_ex_p : after query_step() : ret = %d", rc);
1632 rc = _account_query_finalize(hstmt);
1633 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1635 _INFO("account_update_to_db_by_id_ex_p : after query_filnalize() : ret = %d", rc);
1637 _INFO("account_update_to_db_by_id_ex_p : before update_capability()");
1638 /*update capability*/
1639 error_code = _account_update_capability(account, account_id);
1640 if(error_code != _ACCOUNT_ERROR_NONE && error_code!= _ACCOUNT_ERROR_RECORD_NOT_FOUND){
1641 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1642 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1645 _INFO("account_update_to_db_by_id_ex_p : after update_capability()");
1647 _INFO("account_update_to_db_by_id_ex_p : before update_custom()");
1649 error_code = _account_update_custom(account, account_id);
1650 if(error_code != _ACCOUNT_ERROR_NONE && error_code!= _ACCOUNT_ERROR_RECORD_NOT_FOUND){
1651 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1652 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1655 _INFO("account_update_to_db_by_id_ex_p : after update_custom()");
1657 ret_transaction = _account_end_transaction(g_hAccountDB, TRUE);
1663 int _account_update_to_db_by_id(int pid, uid_t uid, account_s* account, int account_id)
1665 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
1666 ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
1667 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1668 int error_code = _ACCOUNT_ERROR_NONE;
1669 account_s* data = (account_s*)account;
1671 pthread_mutex_lock(&account_mutex);
1673 error_code = _account_update_account(pid, uid, data, account_id);
1675 if(error_code != _ACCOUNT_ERROR_NONE) {
1676 pthread_mutex_unlock(&account_mutex);
1680 pthread_mutex_unlock(&account_mutex);
1683 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_UPDATE, account_id);
1684 _account_insert_delete_update_notification_send(buf);
1686 return _ACCOUNT_ERROR_NONE;
1689 int _account_update_to_db_by_id_ex(account_s* account, int account_id)
1691 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
1692 ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
1693 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1694 int error_code = _ACCOUNT_ERROR_NONE;
1695 account_s* data = account;
1697 pthread_mutex_lock(&account_mutex);
1699 _INFO("before update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name);
1700 error_code = _account_update_account_ex(data, account_id);
1701 _INFO("after update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name);
1703 if(error_code != _ACCOUNT_ERROR_NONE) {
1704 pthread_mutex_unlock(&account_mutex);
1708 pthread_mutex_unlock(&account_mutex);
1711 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_UPDATE, account_id);
1712 _account_insert_delete_update_notification_send(buf);
1714 return _ACCOUNT_ERROR_NONE;
1718 int _account_update_to_db_by_user_name(int pid, uid_t uid, account_s* account, const char *user_name, const char *package_name)
1720 ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
1721 ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
1722 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1724 int error_code = _ACCOUNT_ERROR_NONE;
1725 account_s *data = (account_s*)account;
1727 pthread_mutex_lock(&account_mutex);
1729 error_code = _account_update_account_by_user_name(pid, uid, data, user_name, package_name);
1731 pthread_mutex_unlock(&account_mutex);
1734 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_UPDATE, data->id);
1735 _account_insert_delete_update_notification_send(buf);
1740 GSList* _account_db_query_all(int pid, uid_t uid)
1742 //int error_code = _ACCOUNT_ERROR_NONE;
1743 account_stmt hstmt = NULL;
1744 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1746 GSList *account_list = NULL;
1748 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected."));
1750 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1752 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TABLE);
1753 hstmt = _account_prepare_query(g_hAccountDB, query);
1755 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
1756 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1760 rc = _account_query_step(hstmt);
1762 account_s *account_record = NULL;
1764 if (rc != SQLITE_ROW)
1766 _ERR("The record isn't found");
1770 while(rc == SQLITE_ROW) {
1771 account_record = (account_s*) malloc(sizeof(account_s));
1773 if (account_record == NULL) {
1774 ACCOUNT_FATAL("malloc Failed");
1778 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
1779 _account_convert_column_to_account(hstmt, account_record);
1780 account_list = g_slist_append(account_list, account_record);
1781 rc = _account_query_step(hstmt);
1784 rc = _account_query_finalize(hstmt);
1785 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, NULL, ("finalize error"));
1790 for (iter = account_list; iter != NULL; iter = g_slist_next(iter)) {
1791 account_s *account = NULL;
1792 account = (account_s*)iter->data;
1793 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, account->id, (void*)account);
1794 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, account->id, (void*)account);
1798 if (hstmt != NULL) {
1799 rc = _account_query_finalize(hstmt);
1800 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_gslist_account_free(account_list);}, NULL, ("finalize error"));
1805 _remove_sensitive_info_from_non_owning_account_slist(account_list, pid, uid);
1807 return account_list;
1810 int _account_update_sync_status_by_id(uid_t uid, int account_db_id, const int sync_status)
1812 int error_code = _ACCOUNT_ERROR_NONE;
1813 account_stmt hstmt = NULL;
1814 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1818 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1819 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1820 if ( (sync_status < 0) || (sync_status >= _ACCOUNT_SYNC_MAX)) {
1821 ACCOUNT_SLOGE("(%s)-(%d) sync_status is less than 0 or more than enum max.\n", __FUNCTION__, __LINE__);
1822 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1825 pthread_mutex_lock(&account_mutex);
1827 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1829 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_db_id);
1831 rc = _account_get_record_count(g_hAccountDB, query);
1833 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
1834 pthread_mutex_unlock(&account_mutex);
1835 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1836 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1840 ACCOUNT_SLOGE( "account_update_sync_status_by_id : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
1841 pthread_mutex_unlock(&account_mutex);
1842 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1845 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1847 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET sync_support=? WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
1848 hstmt = _account_prepare_query(g_hAccountDB, query);
1850 _account_query_bind_int(hstmt, count, sync_status);
1852 rc = _account_query_step(hstmt);
1854 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
1855 pthread_mutex_unlock(&account_mutex);
1856 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1857 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1860 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_DB_FAILED,
1861 ("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB)));
1863 rc = _account_query_finalize(hstmt);
1864 if (rc != _ACCOUNT_ERROR_NONE) {
1865 ACCOUNT_ERROR("_account_query_finalize error");
1866 pthread_mutex_unlock(&account_mutex);
1870 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_SYNC_UPDATE, account_db_id);
1871 _account_insert_delete_update_notification_send(buf);
1874 error_code = _ACCOUNT_ERROR_NONE;
1877 if (hstmt != NULL) {
1878 rc = _account_query_finalize(hstmt);
1879 pthread_mutex_unlock(&account_mutex);
1880 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1884 pthread_mutex_unlock(&account_mutex);
1888 int _account_query_account_by_account_id(int pid, uid_t uid, int account_db_id, account_s *account_record)
1890 _INFO("_account_query_account_by_account_id() start, account_db_id=[%d]", account_db_id);
1892 int error_code = _ACCOUNT_ERROR_NONE;
1893 account_stmt hstmt = NULL;
1894 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1897 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1898 ACCOUNT_RETURN_VAL(account_record != NULL, {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
1899 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1901 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1903 ACCOUNT_DEBUG("starting db operations");
1905 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
1906 hstmt = _account_prepare_query(g_hAccountDB, query);
1907 rc = _account_db_err_code(g_hAccountDB);
1908 _INFO("after _account_prepare_query, rc=[%d]", rc);
1910 if( rc == SQLITE_PERM ){
1911 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1912 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1915 ACCOUNT_DEBUG("before _account_query_step");
1916 rc = _account_query_step(hstmt);
1917 ACCOUNT_DEBUG("after _account_query_step returned [%d]", rc);
1918 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1920 while (rc == SQLITE_ROW) {
1921 ACCOUNT_DEBUG("before _account_convert_column_to_account");
1922 _account_convert_column_to_account(hstmt, account_record);
1923 ACCOUNT_DEBUG("after _account_convert_column_to_account");
1924 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]);
1925 rc = _account_query_step(hstmt);
1928 ACCOUNT_DEBUG("account_record->id=[%d]", account_record->id);
1930 rc = _account_query_finalize(hstmt);
1931 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1933 ACCOUNT_DEBUG("before _account_query_capability_by_account_id");
1934 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, account_record->id, (void*)account_record);
1935 ACCOUNT_DEBUG("after _account_query_capability_by_account_id");
1937 ACCOUNT_DEBUG("before _account_query_custom_by_account_id");
1938 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, account_record->id, (void*)account_record);
1939 ACCOUNT_DEBUG("after _account_query_custom_by_account_id");
1942 error_code = _ACCOUNT_ERROR_NONE;
1945 if (hstmt != NULL) {
1946 rc = _account_query_finalize(hstmt);
1947 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1953 _remove_sensitive_info_from_non_owning_account(account_record, pid, uid);
1955 pthread_mutex_unlock(&account_mutex);
1956 ACCOUNT_DEBUG("_account_query_account_by_account_id end [%d]", error_code);
1960 GList* _account_query_account_by_user_name(int pid, uid_t uid, const char *user_name, int *error_code)
1962 *error_code = _ACCOUNT_ERROR_NONE;
1963 account_stmt hstmt = NULL;
1964 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1966 account_s *account_head = NULL;
1968 if (user_name == NULL)
1970 _ERR("USER NAME IS NULL");
1971 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
1975 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1977 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ?", ACCOUNT_TABLE);
1979 hstmt = _account_prepare_query(g_hAccountDB, query);
1981 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM)
1983 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1984 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
1988 int binding_count = 1;
1989 _account_query_bind_text(hstmt, binding_count++, user_name);
1991 rc = _account_query_step(hstmt);
1993 if (rc != SQLITE_ROW)
1995 _ERR("The record isn't found");
1996 *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
2002 account_head = (account_s*) malloc(sizeof(account_s));
2003 if (account_head == NULL) {
2004 ACCOUNT_FATAL("malloc Failed");
2005 if (hstmt != NULL) {
2006 rc = _account_query_finalize(hstmt);
2008 if (rc != _ACCOUNT_ERROR_NONE)
2010 _ERR("finalize error");
2016 *error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
2019 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2021 while (rc == SQLITE_ROW) {
2022 account_s* account_record = NULL;
2024 account_record = (account_s*) malloc(sizeof(account_s));
2026 if (account_record == NULL) {
2027 ACCOUNT_FATAL("malloc Failed");
2030 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2032 _account_convert_column_to_account(hstmt, account_record);
2034 account_head->account_list = g_list_append(account_head->account_list, account_record);
2036 rc = _account_query_step(hstmt);
2040 rc = _account_query_finalize(hstmt);
2042 if (rc != _ACCOUNT_ERROR_NONE)
2044 _ERR("finalize error");
2054 tmp = g_list_length(account_head->account_list);
2056 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2057 // account_h account;
2058 // account = (account_h)iter->data;
2060 account_s *testaccount = (account_s*)iter->data;
2062 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, testaccount->id, (void*)testaccount);
2063 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, testaccount->id, (void*)testaccount);
2067 *error_code = _ACCOUNT_ERROR_NONE;
2070 if (hstmt != NULL) {
2071 rc = _account_query_finalize(hstmt);
2072 if (rc != _ACCOUNT_ERROR_NONE)
2074 _ERR("finalize error");
2080 pthread_mutex_unlock(&account_mutex);
2083 _remove_sensitive_info_from_non_owning_account_list(account_head->account_list, pid, uid);
2084 GList* result = account_head->account_list;
2085 _ACCOUNT_FREE(account_head);
2092 _account_query_account_by_capability(int pid, uid_t uid, const char* capability_type, const int capability_value, int *error_code)
2094 *error_code = _ACCOUNT_ERROR_NONE;
2095 account_stmt hstmt = NULL;
2096 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2099 ACCOUNT_RETURN_VAL((capability_type != NULL), {*error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("capability_type IS NULL"));
2101 if ((capability_value < 0) || (capability_value >= _ACCOUNT_CAPABILITY_STATE_MAX)) {
2102 ACCOUNT_SLOGE("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
2103 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
2107 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
2109 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2111 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=? AND value=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
2113 hstmt = _account_prepare_query(g_hAccountDB, query);
2115 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
2116 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2117 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
2121 int binding_count = 1;
2122 _account_query_bind_text(hstmt, binding_count++, capability_type);
2123 _account_query_bind_int(hstmt, binding_count++, capability_value);
2125 rc = _account_query_step(hstmt);
2127 account_s* account_head = NULL;
2129 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2133 account_head = (account_s*) malloc(sizeof(account_s));
2134 if (account_head == NULL) {
2135 ACCOUNT_FATAL("malloc Failed");
2136 if (hstmt != NULL) {
2137 rc = _account_query_finalize(hstmt);
2138 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
2141 *error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
2144 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2146 while (rc == SQLITE_ROW) {
2147 account_s* account_record = NULL;
2149 account_record = (account_s*) malloc(sizeof(account_s));
2151 if (account_record == NULL) {
2152 ACCOUNT_FATAL("malloc Failed");
2155 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2157 _account_convert_column_to_account(hstmt, account_record);
2159 account_head->account_list = g_list_append(account_head->account_list, account_record);
2161 rc = _account_query_step(hstmt);
2165 rc = _account_query_finalize(hstmt);
2166 ACCOUNT_CATCH_ERROR_P((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2172 tmp = g_list_length(account_head->account_list);
2174 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2175 // account_h account = NULL;
2176 // account = (account_h)iter->data;
2177 account_s* testaccount = (account_s*)iter->data;
2179 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, testaccount->id, (void*)testaccount);
2180 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, testaccount->id, (void*)testaccount);
2185 *error_code = _ACCOUNT_ERROR_NONE;
2188 if (hstmt != NULL) {
2189 rc = _account_query_finalize(hstmt);
2190 if ( rc != _ACCOUNT_ERROR_NONE ) {
2192 _ERR("finalize error");
2197 if( *error_code != _ACCOUNT_ERROR_NONE && account_head ) {
2198 _account_glist_account_free(account_head->account_list);
2199 _ACCOUNT_FREE(account_head);
2200 account_head = NULL;
2203 pthread_mutex_unlock(&account_mutex);
2207 _remove_sensitive_info_from_non_owning_account_list(account_head->account_list, pid, uid);
2208 GList* result = account_head->account_list;
2209 _ACCOUNT_FREE(account_head);
2215 GList* _account_query_account_by_capability_type(int pid, uid_t uid, const char* capability_type, int *error_code)
2217 *error_code = _ACCOUNT_ERROR_NONE;
2218 account_stmt hstmt = NULL;
2219 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2222 ACCOUNT_RETURN_VAL((capability_type != NULL), {*error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("capability_type IS NULL"));
2223 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;},
2224 NULL, ("The database isn't connected."));
2226 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2228 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
2230 hstmt = _account_prepare_query(g_hAccountDB, query);
2232 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
2233 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2234 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
2238 int binding_count = 1;
2239 _account_query_bind_text(hstmt, binding_count++, capability_type);
2241 rc = _account_query_step(hstmt);
2243 account_s* account_head = NULL;
2245 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2249 account_head = (account_s*) malloc(sizeof(account_s));
2250 if (account_head == NULL) {
2251 ACCOUNT_FATAL("malloc Failed");
2252 if (hstmt != NULL) {
2253 rc = _account_query_finalize(hstmt);
2254 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
2257 *error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
2260 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2262 while (rc == SQLITE_ROW) {
2263 account_s* account_record = NULL;
2265 account_record = (account_s*) malloc(sizeof(account_s));
2267 if (account_record == NULL) {
2268 ACCOUNT_FATAL("malloc Failed");
2271 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2273 _account_convert_column_to_account(hstmt, account_record);
2275 account_head->account_list = g_list_append(account_head->account_list, account_record);
2277 rc = _account_query_step(hstmt);
2281 rc = _account_query_finalize(hstmt);
2282 ACCOUNT_CATCH_ERROR_P((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2288 tmp = g_list_length(account_head->account_list);
2290 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2291 account_s* testaccount = (account_s*)iter->data;
2293 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, testaccount->id, (void*)testaccount);
2294 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, testaccount->id, (void*)testaccount);
2298 *error_code = _ACCOUNT_ERROR_NONE;
2303 rc = _account_query_finalize(hstmt);
2304 if (rc != _ACCOUNT_ERROR_NONE) {
2306 _ERR("finalize error");
2311 if( (*error_code != _ACCOUNT_ERROR_NONE) && account_head ) {
2312 _account_glist_account_free(account_head->account_list);
2313 _ACCOUNT_FREE(account_head);
2314 account_head = NULL;
2317 pthread_mutex_unlock(&account_mutex);
2321 _remove_sensitive_info_from_non_owning_account_list(account_head->account_list, pid, uid);
2322 GList* result = account_head->account_list;
2323 _ACCOUNT_FREE(account_head);
2329 GList* account_server_query_account_by_package_name(const char* package_name, int *error_code, int pid, uid_t uid)
2331 _INFO("account_server_query_account_by_package_name start");
2333 GList * account_list = NULL;
2334 *error_code = _ACCOUNT_ERROR_NONE;
2336 ACCOUNT_RETURN_VAL((package_name != NULL), {*error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("PACKAGE NAME IS NULL"));
2337 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
2339 account_list = _account_query_account_by_package_name(g_hAccountDB, package_name, error_code, pid, uid);
2341 _INFO("account_server_query_account_by_package_name end");
2342 return account_list;
2345 int account_server_delete_account_by_package_name(const char* package_name, bool permission, int pid, uid_t uid)
2347 _INFO("account_db_delete_account_by_package_name");
2349 int error_code = _ACCOUNT_ERROR_NONE;
2351 ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
2352 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2354 error_code = _account_delete_account_by_package_name(g_hAccountDB, package_name, permission, pid, uid);
2356 _INFO("account_server_delete_account_by_package_name end");
2360 int _account_delete(int pid, uid_t uid, int account_id)
2362 int error_code = _ACCOUNT_ERROR_NONE;
2363 account_stmt hstmt = NULL;
2364 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2366 int ret_transaction = 0;
2367 bool is_success = FALSE;
2369 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2372 /* Check requested ID to delete */
2373 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id=%d", ACCOUNT_TABLE, account_id);
2375 count = _account_get_record_count(g_hAccountDB, query);
2377 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
2378 pthread_mutex_unlock(&account_mutex);
2379 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2380 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2384 ACCOUNT_ERROR("account id(%d) is not exist. count(%d)\n", account_id, count);
2385 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
2388 /* Check permission of requested appid */
2389 char* current_appid = NULL;
2390 char *package_name = NULL;
2392 current_appid = _account_get_current_appid(pid, uid);
2394 error_code = _account_get_package_name_from_account_id(account_id, &package_name);
2396 if(error_code != _ACCOUNT_ERROR_NONE){
2397 ACCOUNT_ERROR("No package name with account_id\n");
2398 _ACCOUNT_FREE(current_appid);
2399 _ACCOUNT_FREE(package_name);
2400 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
2402 ACCOUNT_DEBUG( "DELETE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name);
2404 error_code = _account_check_appid_group_with_package_name(current_appid, package_name, uid);
2406 _ACCOUNT_FREE(current_appid);
2407 _ACCOUNT_FREE(package_name);
2409 if(error_code != _ACCOUNT_ERROR_NONE){
2410 ACCOUNT_ERROR("No permission to delete\n");
2411 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2414 /* transaction control required*/
2415 ret_transaction = _account_begin_transaction(g_hAccountDB);
2417 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
2418 pthread_mutex_unlock(&account_mutex);
2419 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2420 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2423 if( ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY ){
2424 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg(g_hAccountDB));
2425 pthread_mutex_unlock(&account_mutex);
2426 return _ACCOUNT_ERROR_DATABASE_BUSY;
2429 if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2430 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
2431 pthread_mutex_unlock(&account_mutex);
2432 return ret_transaction;
2435 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2436 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
2438 hstmt = _account_prepare_query(g_hAccountDB, query);
2440 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
2441 pthread_mutex_unlock(&account_mutex);
2442 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2443 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2446 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2447 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2449 rc = _account_query_step(hstmt);
2450 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2452 rc = _account_query_finalize(hstmt);
2454 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2457 ACCOUNT_MEMSET(query, 0, sizeof(query));
2459 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
2461 hstmt = _account_prepare_query(g_hAccountDB, query);
2462 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2463 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2465 rc = _account_query_step(hstmt);
2466 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
2468 rc = _account_query_finalize(hstmt);
2469 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2472 /* delete custom data */
2473 ACCOUNT_MEMSET(query, 0, sizeof(query));
2475 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
2477 hstmt = _account_prepare_query(g_hAccountDB, query);
2479 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2480 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2482 rc = _account_query_step(hstmt);
2483 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
2485 rc = _account_query_finalize(hstmt);
2486 ACCOUNT_CATCH_ERROR(rc == _ACCOUNT_ERROR_NONE, {}, rc, ("finalize error", account_id, rc));
2492 if (hstmt != NULL) {
2493 rc = _account_query_finalize(hstmt);
2494 if(rc != _ACCOUNT_ERROR_NONE ){
2495 ACCOUNT_ERROR("rc (%d)", rc);
2502 ret_transaction = _account_end_transaction(g_hAccountDB, is_success);
2504 if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2505 ACCOUNT_ERROR("account_delete:_account_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
2507 if (is_success == true) {
2509 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_DELETE, account_id);
2510 _account_insert_delete_update_notification_send(buf);
2514 pthread_mutex_unlock(&account_mutex);
2520 static int _account_query_account_by_username_and_package(const char* username, const char* package_name, account_s *account)
2524 int error_code = _ACCOUNT_ERROR_NONE;
2525 account_stmt hstmt = NULL;
2526 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2528 int binding_count = 1;
2530 ACCOUNT_RETURN_VAL((username != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("username IS NULL"));
2531 ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name IS NULL"));
2532 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2533 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2535 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2537 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
2538 hstmt = _account_prepare_query(g_hAccountDB, query);
2540 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
2541 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2542 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2545 _account_query_bind_text(hstmt, binding_count++, username);
2546 _account_query_bind_text(hstmt, binding_count++, package_name);
2548 rc = _account_query_step(hstmt);
2549 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2551 account_s *account_record = account;
2553 while (rc == SQLITE_ROW) {
2554 _account_convert_column_to_account(hstmt, account_record);
2555 rc = _account_query_step(hstmt);
2558 rc = _account_query_finalize(hstmt);
2559 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2560 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, account_record->id, (void*)account_record);
2561 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, account_record->id, (void*)account_record);
2564 error_code = _ACCOUNT_ERROR_NONE;
2567 if (hstmt != NULL) {
2568 rc = _account_query_finalize(hstmt);
2569 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2573 pthread_mutex_unlock(&account_mutex);
2577 int _account_create(account_s **account)
2580 ACCOUNT_SLOGE("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__);
2581 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2584 account_s *data = (account_s*)malloc(sizeof(account_s));
2587 ACCOUNT_FATAL("Memory Allocation Failed");
2588 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
2590 ACCOUNT_MEMSET(data, 0, sizeof(account_s));
2592 /*Setting account as visible by default*/
2593 // data->secret = _ACCOUNT_SECRECY_VISIBLE;
2595 /*Setting account as not supporting sync by default*/
2596 // data->sync_support = _ACCOUNT_SYNC_NOT_SUPPORT;
2600 return _ACCOUNT_ERROR_NONE;
2603 int _account_destroy(account_s *account)
2605 account_s *data = account;
2607 ACCOUNT_RETURN_VAL((data != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Account handle is null!"));
2609 _account_free_account_with_items(data);
2611 return _ACCOUNT_ERROR_NONE;
2614 int _account_get_account_id(account_s* account, int *account_id)
2617 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2620 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2623 *account_id = account->id;
2625 return _ACCOUNT_ERROR_NONE;
2628 int _account_delete_from_db_by_user_name(int pid, uid_t uid, const char *user_name, const char *package_name)
2630 _INFO("[%s][%s]", user_name, package_name);
2632 int error_code = _ACCOUNT_ERROR_NONE;
2633 account_stmt hstmt = NULL;
2634 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2636 int ret_transaction = 0;
2637 bool is_success = FALSE;
2638 account_s *account = NULL;
2639 int binding_count = 1;
2640 int account_id = -1;
2642 ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is null!"));
2643 ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
2644 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2646 /* Check permission of requested appid */
2647 char* current_appid = NULL;
2648 char* package_name_temp = NULL;
2650 current_appid = _account_get_current_appid(pid, uid);
2652 package_name_temp = _account_dup_text(package_name);
2654 ACCOUNT_DEBUG( "DELETE:user_name[%s],current_appid[%s], package_name[%s]", user_name, current_appid, package_name_temp);
2656 error_code = _account_check_appid_group_with_package_name(current_appid, package_name_temp, uid);
2658 _ACCOUNT_FREE(current_appid);
2659 _ACCOUNT_FREE(package_name_temp);
2661 if(error_code != _ACCOUNT_ERROR_NONE){
2662 ACCOUNT_ERROR("No permission to delete\n");
2663 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2666 rc = _account_create(&account);
2667 rc = _account_query_account_by_username_and_package(user_name, package_name, account);
2671 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM )
2673 _account_destroy(account);
2674 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2675 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2679 account_s* account_data = (account_s*)account;
2681 rc = _account_get_account_id(account_data, &account_id);
2683 rc = _account_destroy(account);
2685 /* transaction control required*/
2686 ret_transaction = _account_begin_transaction(g_hAccountDB);
2688 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM )
2690 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2691 pthread_mutex_unlock(&account_mutex);
2692 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2696 if( ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY )
2698 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg(g_hAccountDB));
2699 pthread_mutex_unlock(&account_mutex);
2700 return _ACCOUNT_ERROR_DATABASE_BUSY;
2702 else if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2703 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
2704 pthread_mutex_unlock(&account_mutex);
2705 return ret_transaction;
2708 /* delete custom data */
2709 ACCOUNT_MEMSET(query, 0, sizeof(query));
2710 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = ?", ACCOUNT_CUSTOM_TABLE);
2712 hstmt = _account_prepare_query(g_hAccountDB, query);
2714 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
2715 _account_end_transaction(g_hAccountDB, FALSE);
2716 pthread_mutex_unlock(&account_mutex);
2717 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2718 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2721 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2722 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2724 _account_query_bind_int(hstmt, binding_count++, account_id);
2726 rc = _account_query_step(hstmt);
2727 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2729 rc = _account_query_finalize(hstmt);
2730 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2733 /* delete capability */
2734 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", CAPABILITY_TABLE);
2736 hstmt = _account_prepare_query(g_hAccountDB, query);
2738 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2739 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2742 _account_query_bind_text(hstmt, binding_count++, user_name);
2743 _account_query_bind_text(hstmt, binding_count++, package_name);
2745 rc = _account_query_step(hstmt);
2746 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2748 rc = _account_query_finalize(hstmt);
2749 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2752 ACCOUNT_MEMSET(query, 0, sizeof(query));
2755 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
2757 hstmt = _account_prepare_query(g_hAccountDB, query);
2758 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2759 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2763 _account_query_bind_text(hstmt, binding_count++, user_name);
2764 _account_query_bind_text(hstmt, binding_count++, package_name);
2766 rc = _account_query_step(hstmt);
2767 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));
2769 rc = _account_query_finalize(hstmt);
2770 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2776 if (hstmt != NULL) {
2777 rc = _account_query_finalize(hstmt);
2778 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2782 ret_transaction = _account_end_transaction(g_hAccountDB, is_success);
2784 if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2785 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
2787 if (is_success == true) {
2789 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_DELETE, account_id);
2790 _account_insert_delete_update_notification_send(buf);
2794 pthread_mutex_unlock(&account_mutex);
2800 int _account_get_total_count_from_db(gboolean include_hidden, int *count)
2803 ACCOUNT_SLOGE("(%s)-(%d) count is NULL.\n", __FUNCTION__, __LINE__);
2804 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2808 ACCOUNT_ERROR("DB is not opened\n");
2809 return _ACCOUNT_ERROR_DB_NOT_OPENED;
2812 char query[1024] = {0, };
2813 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2817 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s", ACCOUNT_TABLE);
2821 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s where secret = %d", ACCOUNT_TABLE, _ACCOUNT_SECRECY_VISIBLE);
2824 *count = _account_get_record_count(g_hAccountDB, query);
2826 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
2827 pthread_mutex_unlock(&account_mutex);
2828 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2829 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2834 account_stmt pStmt = NULL;
2836 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
2837 if (SQLITE_OK != rc) {
2838 ACCOUNT_SLOGE("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg(g_hAccountDB));
2839 sqlite3_finalize(pStmt);
2840 return _ACCOUNT_ERROR_DB_FAILED;
2843 rc = sqlite3_step(pStmt);
2844 if (SQLITE_ROW != rc) {
2845 ACCOUNT_ERROR("[ERROR] sqlite3_step() failed\n");
2846 sqlite3_finalize(pStmt);
2847 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
2850 ncount = sqlite3_column_int(pStmt, 0);
2854 sqlite3_finalize(pStmt);
2857 ACCOUNT_ERROR("[ERROR] Number of account : %d, End", ncount);
2858 return _ACCOUNT_ERROR_DB_FAILED;
2861 return _ACCOUNT_ERROR_NONE;
2865 int account_server_query_app_id_exist(const char* app_id)
2867 _INFO("account_server_query_app_id_exist start app_id=[%s]", app_id);
2868 int ret = _ACCOUNT_ERROR_NONE;
2870 ret = _account_type_query_app_id_exist_from_all_db(g_hAccountDB, g_hAccountGlobalDB, app_id);
2872 _INFO("account_server_query_app_id_exist end error_code=[%d]", ret);
2876 int account_server_insert_account_type_to_user_db(account_type_s *account_type, int *account_type_id, uid_t uid)
2878 ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE HANDLE IS NULL"));
2879 ACCOUNT_RETURN_VAL((account_type->app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID OF ACCOUNT TYPE IS NULL"));
2880 ACCOUNT_RETURN_VAL((account_type_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE ID IS NULL"));
2882 _INFO("account_server_insert_account_type_to_user_db start uid=[%d]", uid);
2883 int ret = _ACCOUNT_ERROR_NONE;
2885 if (_account_type_check_duplicated(g_hAccountDB, account_type->app_id) ||
2886 _account_type_check_duplicated(g_hAccountGlobalDB, account_type->app_id)) {
2887 *account_type_id = -1;
2888 return _ACCOUNT_ERROR_DUPLICATED;
2891 ret = _account_type_insert_to_db(g_hAccountDB, account_type, account_type_id);
2892 _INFO("account_server_insert_account_type_to_user_db end error_code=[%d]", ret);
2896 int account_server_delete_account_type_by_app_id_from_user_db(const char * app_id)
2898 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID OF ACCOUNT TYPE IS NULL"));
2900 _INFO("account_server_delete_account_type_by_app_id_from_user_db start");
2901 int ret = _ACCOUNT_ERROR_NONE;
2903 ret = _account_type_delete_by_app_id(g_hAccountDB, app_id);
2904 _INFO("account_server_delete_account_type_by_app_id_from_user_db end error_code=[%d]", ret);
2908 GSList* _account_type_query_provider_feature_by_app_id_from_global_db(const char* app_id, int *error_code)
2910 _INFO("_account_type_query_provider_feature_by_app_id_in_global_db app_id=%s", app_id);
2911 account_stmt hstmt = NULL;
2912 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2913 int rc = 0, binding_count = 1;
2914 GSList* feature_list = NULL;
2916 ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
2917 ACCOUNT_RETURN_VAL((error_code != NULL), {_ERR("error_code pointer is NULL");}, NULL, (""));
2918 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {*error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; _ERR("The database isn't connected.");}, NULL, ("The database isn't connected."));
2920 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2922 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
2923 _INFO("account query=[%s]", query);
2925 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
2927 if( _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM ){
2928 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2929 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
2933 _INFO("before _account_query_bind_text");
2934 _account_query_bind_text(hstmt, binding_count++, app_id);
2936 rc = _account_query_step(hstmt);
2938 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"));
2940 provider_feature_s* feature_record = NULL;
2942 while (rc == SQLITE_ROW) {
2944 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
2946 if (feature_record == NULL) {
2947 ACCOUNT_FATAL("malloc Failed");
2951 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
2953 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
2955 _INFO("Adding account feature_list");
2956 feature_list = g_slist_append(feature_list, feature_record);
2958 rc = _account_query_step(hstmt);
2961 *error_code = _ACCOUNT_ERROR_NONE;
2964 if (hstmt != NULL) {
2965 rc = _account_query_finalize(hstmt);
2966 if (rc != _ACCOUNT_ERROR_NONE) {
2968 _ERR("global db fianlize error");
2972 if (*error_code != _ACCOUNT_ERROR_NONE) {
2973 _account_type_gslist_feature_free(feature_list);
2976 _INFO("Returning account feature_list from global db");
2977 return feature_list;
2980 GSList* _account_type_query_provider_feature_by_app_id(const char* app_id, int *error_code)
2982 _INFO("_account_type_query_provider_feature_by_app_id app_id=%s", app_id);
2983 account_stmt hstmt = NULL;
2984 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2985 int rc = 0, binding_count = 1;
2986 GSList* feature_list = NULL;
2988 ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
2989 ACCOUNT_RETURN_VAL((error_code != NULL), {_ERR("error_code pointer is NULL");}, NULL, (""));
2990 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
2992 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2994 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
2995 _INFO("account query=[%s]", query);
2997 hstmt = _account_prepare_query(g_hAccountDB, query);
2999 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
3000 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3001 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3005 _account_query_bind_text(hstmt, binding_count++, app_id);
3007 rc = _account_query_step(hstmt);
3009 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"));
3011 provider_feature_s* feature_record = NULL;
3013 while (rc == SQLITE_ROW) {
3015 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
3017 if (feature_record == NULL) {
3018 ACCOUNT_FATAL("malloc Failed");
3022 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
3024 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
3026 _INFO("Adding account feature_list");
3027 feature_list = g_slist_append(feature_list, feature_record);
3029 rc = _account_query_step(hstmt);
3032 *error_code = _ACCOUNT_ERROR_NONE;
3034 rc = _account_query_finalize(hstmt);
3035 ACCOUNT_CATCH_ERROR_P((rc == _ACCOUNT_ERROR_NONE), {*error_code = rc;}, rc, ("account finalize error"));
3039 if (hstmt != NULL) {
3040 rc = _account_query_finalize(hstmt);
3041 if (rc != _ACCOUNT_ERROR_NONE) {
3043 _ERR("account fianlize error");
3047 _INFO("*error_code=[%d]", *error_code);
3049 if (*error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
3050 feature_list = _account_type_query_provider_feature_by_app_id_from_global_db(app_id, error_code);
3053 if (*error_code != _ACCOUNT_ERROR_NONE)
3054 _account_type_gslist_feature_free(feature_list);
3056 _INFO("Returning account feature_list");
3057 return feature_list;
3060 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 )
3062 int error_code = _ACCOUNT_ERROR_NONE;
3063 account_stmt hstmt = NULL;
3064 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3065 int rc = 0, binding_count = 1;
3067 _INFO("_account_type_query_provider_feature_cb_by_app_id_in_global_db start app_id=%s", app_id);
3068 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3069 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3070 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3072 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3074 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
3075 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3077 if( _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM ){
3078 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3079 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_PERMISSION_DENIED, ("global db permission denied.\n"));
3082 _account_query_bind_text(hstmt, binding_count++, app_id);
3084 rc = _account_query_step(hstmt);
3085 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"));
3087 provider_feature_s* feature_record = NULL;
3089 while (rc == SQLITE_ROW) {
3090 bool cb_ret = FALSE;
3091 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
3093 if (feature_record == NULL) {
3094 ACCOUNT_FATAL("malloc Failed");
3098 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
3100 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
3102 cb_ret = callback(feature_record->app_id, feature_record->key, user_data);
3104 _account_type_free_feature_with_items(feature_record);
3106 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returns FALSE, its iteration is stopped!!!!\n"));
3108 rc = _account_query_step(hstmt);
3111 error_code = _ACCOUNT_ERROR_NONE;
3114 if (hstmt != NULL) {
3115 rc = _account_query_finalize(hstmt);
3116 if (rc != _ACCOUNT_ERROR_NONE) {
3118 _ERR("global db finalize error[%d]", rc);
3123 _INFO("_account_type_query_provider_feature_cb_by_app_id_in_global_db end. error_code=[%d]", error_code);
3127 int _account_type_query_provider_feature_cb_by_app_id(account_type_provider_feature_cb callback, const char* app_id, void *user_data )
3129 int error_code = _ACCOUNT_ERROR_NONE;
3130 account_stmt hstmt = NULL;
3131 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3132 int rc = 0, binding_count = 1;
3134 _INFO("_account_type_query_provider_feature_cb_by_app_id start app_id=%s", app_id);
3135 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3136 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3137 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3139 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3141 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
3142 hstmt = _account_prepare_query(g_hAccountDB, query);
3144 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
3145 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3146 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3149 _account_query_bind_text(hstmt, binding_count++, app_id);
3151 rc = _account_query_step(hstmt);
3152 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found in user db.\n"));
3154 provider_feature_s* feature_record = NULL;
3156 while (rc == SQLITE_ROW) {
3157 bool cb_ret = FALSE;
3158 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
3160 if (feature_record == NULL) {
3161 ACCOUNT_FATAL("malloc Failed");
3165 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
3167 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
3169 cb_ret = callback(feature_record->app_id, feature_record->key, user_data);
3171 _account_type_free_feature_with_items(feature_record);
3173 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
3175 rc = _account_query_step(hstmt);
3178 rc = _account_query_finalize(hstmt);
3179 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3182 error_code = _ACCOUNT_ERROR_NONE;
3185 if (hstmt != NULL) {
3186 rc = _account_query_finalize(hstmt);
3187 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3191 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
3192 error_code = _account_type_query_provider_feature_cb_by_app_id_from_global_db(callback, app_id, user_data);
3195 _INFO("_account_type_query_provider_feature_cb_by_app_id end");
3199 int account_type_query_provider_feature_cb_by_app_id(account_type_provider_feature_cb callback, const char* app_id, void *user_data )
3201 int error_code = _ACCOUNT_ERROR_NONE;
3203 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3204 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3205 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3207 error_code = _account_type_query_provider_feature_cb_by_app_id(callback, app_id, user_data);
3209 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
3210 error_code = _account_type_query_provider_feature_cb_by_app_id_from_global_db(callback, app_id, user_data);
3216 bool _account_type_query_supported_feature_from_global_db(const char* app_id, const char* capability, int *error_code)
3218 _INFO("_account_type_query_supported_feature_in_global_db start");
3219 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3221 *error_code = _ACCOUNT_ERROR_NONE;
3223 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3224 int record_count = 0;
3226 if (app_id == NULL || capability == NULL)
3228 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
3232 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
3234 record_count = _account_get_record_count(g_hAccountGlobalDB, query);
3236 if( _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM ){
3237 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3238 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3241 if (record_count <= 0)
3243 *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
3247 _INFO("_account_type_query_supported_feature_in_global_db end");
3251 bool _account_type_query_supported_feature(const char* app_id, const char* capability, int *error_code)
3253 _INFO("_account_type_query_supported_feature start");
3255 *error_code = _ACCOUNT_ERROR_NONE;
3257 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3258 int record_count = 0;
3260 if (app_id == NULL || capability == NULL)
3262 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
3266 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
3268 record_count = _account_get_record_count(g_hAccountDB, query);
3270 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
3271 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3272 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3276 if (record_count <= 0)
3278 bool is_exist = false;
3279 is_exist = _account_type_query_supported_feature_from_global_db(app_id, capability, error_code);
3284 _INFO("_account_type_query_supported_feature end");
3289 static int _account_type_update_provider_feature(sqlite3 *account_db_handle, account_type_s *account_type, const char* app_id)
3292 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3293 account_stmt hstmt = NULL;
3295 ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
3297 if (g_slist_length( account_type->provider_feature_list)==0) {
3298 ACCOUNT_ERROR( "no feature\n");
3299 return _ACCOUNT_ERROR_NONE;
3302 ACCOUNT_DEBUG( "app id", app_id);
3304 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3306 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id=? ", PROVIDER_FEATURE_TABLE);
3307 hstmt = _account_prepare_query(account_db_handle, query);
3309 if( _account_db_err_code(account_db_handle) == SQLITE_PERM ){
3310 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(account_db_handle));
3311 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3315 _account_query_bind_text(hstmt, count++, app_id);
3316 rc = _account_query_step(hstmt);
3318 if (rc != SQLITE_DONE) {
3319 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3320 return _ACCOUNT_ERROR_DB_FAILED;
3322 rc = _account_query_finalize(hstmt);
3323 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3328 for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
3331 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3332 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
3333 "(?, ?) ", PROVIDER_FEATURE_TABLE);
3335 hstmt = _account_prepare_query(account_db_handle, query);
3337 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg(account_db_handle)));
3339 provider_feature_s* feature_data = NULL;
3340 feature_data = (provider_feature_s*)iter->data;
3342 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
3343 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3344 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
3345 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3347 rc = _account_query_step(hstmt);
3349 if (rc != SQLITE_DONE) {
3350 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3353 rc = _account_query_finalize(hstmt);
3354 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3358 return _ACCOUNT_ERROR_NONE;
3361 static int _account_type_update_label(sqlite3 *account_db_handle, account_type_s *account_type, const char* app_id)
3364 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3365 account_stmt hstmt = NULL;
3367 ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
3369 if (g_slist_length( account_type->label_list)==0) {
3370 return _ACCOUNT_ERROR_NONE;
3373 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3375 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId=? ", LABEL_TABLE);
3376 hstmt = _account_prepare_query(account_db_handle, query);
3378 if( _account_db_err_code(account_db_handle) == SQLITE_PERM ){
3379 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(account_db_handle));
3380 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3384 _account_query_bind_text(hstmt, count++, app_id);
3385 rc = _account_query_step(hstmt);
3387 if (rc != SQLITE_DONE) {
3388 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3389 return _ACCOUNT_ERROR_DB_FAILED;
3391 rc = _account_query_finalize(hstmt);
3392 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3397 for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
3400 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3401 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
3402 "(?, ?, ?) ", LABEL_TABLE);
3404 hstmt = _account_prepare_query(account_db_handle, query);
3406 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg(account_db_handle)));
3408 label_s* label_data = NULL;
3409 label_data = (label_s*)iter->data;
3411 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
3412 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3413 ret = _account_query_bind_text(hstmt, count++, label_data->label);
3414 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3415 ret = _account_query_bind_text(hstmt, count++, label_data->locale);
3416 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3418 rc = _account_query_step(hstmt);
3420 if (rc != SQLITE_DONE) {
3421 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3424 rc = _account_query_finalize(hstmt);
3425 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3429 return _ACCOUNT_ERROR_NONE;
3433 static int _account_type_update_account(sqlite3 *account_db_handle, account_type_s *account_type, const char* app_id)
3435 int rc = 0, binding_count =1;
3436 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3437 int error_code = _ACCOUNT_ERROR_NONE;
3438 account_stmt hstmt = NULL;
3440 if (!account_type->app_id) {
3441 ACCOUNT_ERROR("app id is mandetory field, it can not be NULL!!!!\n");
3442 return _ACCOUNT_ERROR_INVALID_PARAMETER;
3445 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3446 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET AppId=?, ServiceProviderId=?, IconPath=?, "
3447 "SmallIconPath=?, MultipleAccountSupport=? WHERE AppId=? ", ACCOUNT_TYPE_TABLE);
3449 hstmt = _account_prepare_query(account_db_handle, query);
3451 if( _account_db_err_code(account_db_handle) == SQLITE_PERM ){
3452 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(account_db_handle));
3453 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3454 } else if (_account_db_err_code(account_db_handle) == SQLITE_BUSY){
3455 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg(account_db_handle));
3456 return _ACCOUNT_ERROR_DATABASE_BUSY;
3459 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg(account_db_handle)));
3461 binding_count = _account_type_convert_account_to_sql(account_type, hstmt, query);
3462 _account_query_bind_text(hstmt, binding_count++, app_id);
3464 rc = _account_query_step(hstmt);
3465 if (rc != SQLITE_DONE) {
3466 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3469 rc = _account_query_finalize(hstmt);
3470 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3474 error_code = _account_type_update_label(account_db_handle, account_type, app_id);
3475 /* update provider feature */
3476 error_code = _account_type_update_provider_feature(account_db_handle, account_type, app_id);
3481 int _account_type_update_to_db_by_app_id(account_type_s* account_type, const char* app_id)
3483 int error_code = _ACCOUNT_ERROR_NONE;
3485 ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
3486 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3487 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3489 account_type_s* data = account_type;
3491 pthread_mutex_lock(&account_mutex);
3493 error_code = _account_type_update_account(g_hAccountDB, data, app_id);
3495 pthread_mutex_unlock(&account_mutex);
3500 GSList* _account_type_get_label_list_by_app_id_from_global_db(const char* app_id, int *error_code )
3502 *error_code = _ACCOUNT_ERROR_NONE;
3503 account_stmt hstmt = NULL;
3504 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3505 int rc = 0, binding_count = 1;
3506 GSList* label_list = NULL;
3508 ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
3509 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3511 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3513 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3514 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3516 if( _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM ){
3517 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3518 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3523 _account_query_bind_text(hstmt, binding_count++, app_id);
3525 rc = _account_query_step(hstmt);
3526 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"));
3528 label_s* label_record = NULL;
3530 while (rc == SQLITE_ROW) {
3531 label_record = (label_s*) malloc(sizeof(label_s));
3533 if (label_record == NULL) {
3534 ACCOUNT_FATAL("malloc Failed");
3538 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3540 _account_type_convert_column_to_label(hstmt, label_record);
3542 _INFO("Adding account label_list");
3543 label_list = g_slist_append (label_list, label_record);
3545 rc = _account_query_step(hstmt);
3548 *error_code = _ACCOUNT_ERROR_NONE;
3551 if (hstmt != NULL) {
3552 rc = _account_query_finalize(hstmt);
3553 if (rc != _ACCOUNT_ERROR_NONE) {
3554 _ERR("global db finalize error[%d]", rc);
3559 _INFO("Returning account global label_list");
3563 GSList* _account_type_get_label_list_by_app_id(const char* app_id, int *error_code )
3565 *error_code = _ACCOUNT_ERROR_NONE;
3566 account_stmt hstmt = NULL;
3567 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3568 int rc = 0, binding_count = 1;
3569 GSList* label_list = NULL;
3571 ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
3572 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
3574 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3576 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3577 hstmt = _account_prepare_query(g_hAccountDB, query);
3579 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
3580 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3581 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3585 _account_query_bind_text(hstmt, binding_count++, app_id);
3587 rc = _account_query_step(hstmt);
3588 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3590 label_s* label_record = NULL;
3592 while (rc == SQLITE_ROW) {
3593 label_record = (label_s*) malloc(sizeof(label_s));
3595 if (label_record == NULL) {
3596 ACCOUNT_FATAL("malloc Failed");
3600 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3602 _account_type_convert_column_to_label(hstmt, label_record);
3604 _INFO("Adding account label_list");
3605 label_list = g_slist_append (label_list, label_record);
3607 rc = _account_query_step(hstmt);
3610 rc = _account_query_finalize(hstmt);
3611 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
3614 *error_code = _ACCOUNT_ERROR_NONE;
3617 if (hstmt != NULL) {
3618 rc = _account_query_finalize(hstmt);
3619 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
3623 if (*error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
3624 label_list = _account_type_get_label_list_by_app_id_from_global_db(app_id, error_code);
3627 _INFO("Returning account label_list");
3631 int _account_type_query_label_by_app_id_from_global_db(account_type_label_cb callback, const char* app_id, void *user_data )
3633 int error_code = _ACCOUNT_ERROR_NONE;
3634 account_stmt hstmt = NULL;
3635 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3636 int rc = 0, binding_count = 1;
3638 _INFO("account_type_query_label_by_app_id_from_global_db start");
3640 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3641 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3642 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3644 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3646 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3647 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3649 if( _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM ){
3650 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3651 error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3655 _account_query_bind_text(hstmt, binding_count++, app_id);
3657 rc = _account_query_step(hstmt);
3658 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3660 label_s* label_record = NULL;
3662 while (rc == SQLITE_ROW) {
3663 bool cb_ret = FALSE;
3664 label_record = (label_s*) malloc(sizeof(label_s));
3666 if (label_record == NULL) {
3667 ACCOUNT_FATAL("malloc Failed");
3671 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3673 _account_type_convert_column_to_label(hstmt, label_record);
3675 cb_ret = callback(label_record->app_id, label_record->label , label_record->locale, user_data);
3677 _account_type_free_label_with_items(label_record);
3679 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
3681 rc = _account_query_step(hstmt);
3684 error_code = _ACCOUNT_ERROR_NONE;
3687 if (hstmt != NULL) {
3688 rc = _account_query_finalize(hstmt);
3689 if (rc != _ACCOUNT_ERROR_NONE) {
3690 _ERR("global db finalize error[%d]", rc);
3695 _INFO("account_type_query_label_by_app_id_from_global_db end [%d]", error_code);
3699 int _account_type_query_label_by_app_id(account_type_label_cb callback, const char* app_id, void *user_data )
3701 int error_code = _ACCOUNT_ERROR_NONE;
3702 account_stmt hstmt = NULL;
3703 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3704 int rc = 0, binding_count = 1;
3706 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3707 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3708 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3710 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3712 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3713 hstmt = _account_prepare_query(g_hAccountDB, query);
3715 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
3716 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3717 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3720 _account_query_bind_text(hstmt, binding_count++, app_id);
3722 rc = _account_query_step(hstmt);
3723 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3725 label_s* label_record = NULL;
3727 while (rc == SQLITE_ROW) {
3728 bool cb_ret = FALSE;
3729 label_record = (label_s*) malloc(sizeof(label_s));
3731 if (label_record == NULL) {
3732 ACCOUNT_FATAL("malloc Failed");
3736 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3738 _account_type_convert_column_to_label(hstmt, label_record);
3740 cb_ret = callback(label_record->app_id, label_record->label , label_record->locale, user_data);
3742 _account_type_free_label_with_items(label_record);
3744 // ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
3745 if(cb_ret == TRUE) {
3746 _INFO("Callback func returs FALSE, its iteration is stopped!!!!\n");
3750 rc = _account_query_step(hstmt);
3753 rc = _account_query_finalize(hstmt);
3754 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3757 error_code = _ACCOUNT_ERROR_NONE;
3760 if (hstmt != NULL) {
3761 rc = _account_query_finalize(hstmt);
3762 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3766 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
3767 error_code = account_type_query_label_by_app_id_from_global_db(callback, app_id, user_data);
3773 int account_type_query_label_by_app_id(account_type_label_cb callback, const char* app_id, void *user_data )
3775 int error_code = _ACCOUNT_ERROR_NONE;
3777 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3778 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3779 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3781 error_code = _account_type_query_label_by_app_id(callback, app_id, user_data);
3783 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
3784 error_code = _account_type_query_label_by_app_id_from_global_db(callback, app_id, user_data);
3790 bool _account_get_label_text_cb(char* app_id, char* label, char* locale, void *user_data)
3792 account_type_s *data = (account_type_s*)user_data;
3794 label_s *label_data = (label_s*)malloc(sizeof(label_s));
3796 if (label_data == NULL) {
3797 ACCOUNT_FATAL("_account_get_label_text_cb : MALLOC FAIL\n");
3800 ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
3802 label_data->app_id = _account_dup_text(app_id);
3803 label_data->label = _account_dup_text(label);
3804 label_data->locale = _account_dup_text(locale);
3806 data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
3811 bool _account_get_provider_feature_cb(char* app_id, char* key, void* user_data)
3813 account_type_s *data = (account_type_s*)user_data;
3815 provider_feature_s *feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
3817 if (feature_data == NULL) {
3818 ACCOUNT_FATAL("_account_get_provider_feature_cb : MALLOC FAIL\n");
3821 ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
3823 feature_data->app_id = _account_dup_text(app_id);
3824 feature_data->key = _account_dup_text(key);
3826 data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
3831 int _account_type_query_by_app_id_from_global_db(const char* app_id, account_type_s** account_type_record)
3833 _INFO("_account_type_query_by_app_id_from_global_db start");
3835 int error_code = _ACCOUNT_ERROR_NONE;
3836 account_stmt hstmt = NULL;
3837 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3838 int rc = 0, binding_count = 1;
3840 ACCOUNT_RETURN_VAL((app_id != 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3841 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3842 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3844 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3846 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
3847 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3849 if( _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM ){
3850 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3851 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3854 _account_query_bind_text(hstmt, binding_count++, app_id);
3856 rc = _account_query_step(hstmt);
3857 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3859 *account_type_record = create_empty_account_type_instance();
3861 while (rc == SQLITE_ROW) {
3862 _account_type_convert_column_to_account_type(hstmt, *account_type_record);
3863 rc = _account_query_step(hstmt);
3866 rc = _account_query_finalize(hstmt);
3867 ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {_ERR("global db finalize error rc=[%d]", rc);}, rc, ("finalize error"));
3868 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb, app_id, (void*)(*account_type_record));
3869 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, app_id,(void*)(*account_type_record));
3872 error_code = _ACCOUNT_ERROR_NONE;
3875 if (hstmt != NULL) {
3876 rc = _account_query_finalize(hstmt);
3877 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3881 _INFO("_account_type_query_by_app_id_from_global_db end [%d]", error_code);
3885 int _account_type_query_by_app_id(const char* app_id, account_type_s** account_type_record)
3887 _INFO("_account_type_query_by_app_id start");
3889 int error_code = _ACCOUNT_ERROR_NONE;
3890 account_stmt hstmt = NULL;
3891 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3892 int rc = 0, binding_count = 1;
3894 ACCOUNT_RETURN_VAL((app_id != 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3895 ACCOUNT_RETURN_VAL((account_type_record != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("account type record(account_type_s**) is NULL"));
3896 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3898 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3900 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
3901 hstmt = _account_prepare_query(g_hAccountDB, query);
3903 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
3904 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3905 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3908 _account_query_bind_text(hstmt, binding_count++, app_id);
3910 rc = _account_query_step(hstmt);
3911 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3913 *account_type_record = create_empty_account_type_instance();
3914 if (*account_type_record == NULL) {
3915 _ERR("Out of Memory");
3916 error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
3920 while (rc == SQLITE_ROW) {
3921 _account_type_convert_column_to_account_type(hstmt, *account_type_record);
3922 rc = _account_query_step(hstmt);
3925 rc = _account_query_finalize(hstmt);
3926 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3927 _account_type_query_label_by_app_id(_account_get_label_text_cb, app_id, (void*)(*account_type_record));
3928 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, app_id,(void*)(*account_type_record));
3931 error_code = _ACCOUNT_ERROR_NONE;
3934 if (hstmt != NULL) {
3935 rc = _account_query_finalize(hstmt);
3936 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3940 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
3941 error_code = _account_type_query_by_app_id_from_global_db(app_id, account_type_record);
3944 _INFO("_account_type_query_by_app_id end [%d]", error_code);
3948 int _account_type_query_by_provider_feature_from_global_db(const char* key, GSList **account_type_list_all)
3950 int error_code = _ACCOUNT_ERROR_NONE;
3951 account_stmt hstmt = NULL;
3952 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3954 GSList *account_type_list = NULL;
3956 _INFO("_account_type_query_by_provider_feature_from_global_db start key=%s", key);
3959 ACCOUNT_ERROR("capability_type IS NULL.");
3960 error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
3964 if(g_hAccountGlobalDB == NULL)
3966 ACCOUNT_ERROR("The database isn't connected.");
3967 error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;
3971 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3973 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
3975 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3977 if( _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM )
3979 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3980 error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3984 int binding_count = 1;
3985 _account_query_bind_text(hstmt, binding_count++, key);
3987 rc = _account_query_step(hstmt);
3989 account_type_s *account_type_record = NULL;
3991 if(rc != SQLITE_ROW)
3993 ACCOUNT_ERROR("The record isn't found. rc=[%d]", rc);
3994 error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
3998 while(rc == SQLITE_ROW) {
3999 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4001 if (account_type_record == NULL) {
4002 ACCOUNT_FATAL("malloc Failed");
4006 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4007 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4008 account_type_list = g_slist_append(account_type_list, account_type_record);
4009 rc = _account_query_step(hstmt);
4012 rc = _account_query_finalize(hstmt);
4013 if (rc != _ACCOUNT_ERROR_NONE )
4015 _account_type_gslist_account_type_free(account_type_list);
4016 ACCOUNT_ERROR("finalize error(%s)", rc);
4024 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4025 account_type_s *account_type = NULL;
4026 account_type = (account_type_s*)iter->data;
4027 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
4028 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
4029 _INFO("add label & provider_feature");
4032 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4034 account_type_s *account_type = NULL;
4035 account_type = (account_type_s*)iter->data;
4036 *account_type_list_all = g_slist_append(*account_type_list_all, account_type);
4037 _INFO("add account_type");
4040 error_code = _ACCOUNT_ERROR_NONE;
4043 if (hstmt != NULL) {
4044 rc = _account_query_finalize(hstmt);
4045 if (rc != _ACCOUNT_ERROR_NONE)
4047 ACCOUNT_ERROR("finalize error(%s)", rc);
4053 _INFO("_account_type_query_by_provider_feature_from_global_db end. error_code=[%d]", error_code);
4057 GSList* _account_type_query_by_provider_feature(const char* key, int *error_code)
4059 *error_code = _ACCOUNT_ERROR_NONE;
4060 account_stmt hstmt = NULL;
4061 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4063 GSList *account_type_list = NULL;
4065 _INFO("account_type_query_by_provider_feature start key=%s", key);
4068 ACCOUNT_ERROR("capability_type IS NULL.");
4069 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
4073 if(g_hAccountDB == NULL)
4075 ACCOUNT_ERROR("The database isn't connected.");
4076 *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;
4080 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4082 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
4084 hstmt = _account_prepare_query(g_hAccountDB, query);
4086 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM )
4088 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4089 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
4093 int binding_count = 1;
4094 _account_query_bind_text(hstmt, binding_count++, key);
4096 rc = _account_query_step(hstmt);
4098 account_type_s *account_type_record = NULL;
4100 if(rc != SQLITE_ROW)
4102 ACCOUNT_ERROR("The record isn't found. rc=[%d]", rc);
4103 *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4107 while(rc == SQLITE_ROW) {
4108 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4110 if (account_type_record == NULL) {
4111 ACCOUNT_FATAL("malloc Failed");
4115 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4116 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4117 account_type_list = g_slist_append(account_type_list, account_type_record);
4118 rc = _account_query_step(hstmt);
4121 rc = _account_query_finalize(hstmt);
4122 if (rc != _ACCOUNT_ERROR_NONE )
4124 _account_type_gslist_account_type_free(account_type_list);
4125 ACCOUNT_ERROR("finalize error(%s)", rc);
4133 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4134 account_type_s *account_type = NULL;
4135 account_type = (account_type_s*)iter->data;
4136 _account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
4137 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
4140 *error_code = _ACCOUNT_ERROR_NONE;
4143 if (hstmt != NULL) {
4144 rc = _account_query_finalize(hstmt);
4145 if (rc != _ACCOUNT_ERROR_NONE)
4153 if (*error_code == _ACCOUNT_ERROR_NONE || *error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4154 rc = _account_type_query_by_provider_feature_from_global_db(key, &account_type_list);
4155 if (rc != _ACCOUNT_ERROR_NONE && rc != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4156 ACCOUNT_ERROR( "_account_type_query_by_provider_feature_from_global_db fail=[%d]", rc);
4157 _account_type_gslist_account_type_free(account_type_list);
4160 if (rc == _ACCOUNT_ERROR_NONE)
4164 _INFO("account_type_query_by_provider_feature end");
4165 return account_type_list;
4168 int _account_type_query_all_from_global_db(GSList **account_type_list_all)
4170 account_stmt hstmt = NULL;
4171 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4172 int rc = _ACCOUNT_ERROR_NONE;
4173 int error_code = _ACCOUNT_ERROR_NONE;
4174 GSList *account_type_list = NULL;
4176 _INFO("_account_type_query_all_in_global_db start");
4177 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, NULL, ("The database isn't connected."));
4179 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4181 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
4182 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
4184 rc = _account_query_step(hstmt);
4186 if( _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM ){
4187 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
4188 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4191 account_type_s *account_type_record = NULL;
4193 if (rc != SQLITE_ROW)
4195 _INFO("[_ACCOUNT_ERROR_RECORD_NOT_FOUND]The record isn't found.");
4196 error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4200 while(rc == SQLITE_ROW) {
4201 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4203 if (account_type_record == NULL) {
4204 ACCOUNT_FATAL("malloc Failed");
4208 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4209 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4210 account_type_list = g_slist_append(account_type_list, account_type_record);
4211 rc = _account_query_step(hstmt);
4214 rc = _account_query_finalize(hstmt);
4215 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4220 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4221 account_type_s *account_type = NULL;
4222 account_type = (account_type_s*)iter->data;
4223 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
4224 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
4227 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4228 account_type_s *account_type = NULL;
4229 account_type = (account_type_s*)iter->data;
4230 *account_type_list_all = g_slist_append(*account_type_list_all, account_type);
4233 error_code = _ACCOUNT_ERROR_NONE;
4237 rc = _account_query_finalize(hstmt);
4238 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list);}, rc, ("finalize error"));
4242 _INFO("_account_type_query_all_in_global_db end");
4246 GSList* _account_type_query_all(void)
4248 account_stmt hstmt = NULL;
4249 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4251 int error_code = _ACCOUNT_ERROR_NONE;
4252 GSList *account_type_list = NULL;
4254 _INFO("_account_type_query_all start");
4255 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected."));
4257 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4259 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
4260 hstmt = _account_prepare_query(g_hAccountDB, query);
4262 rc = _account_query_step(hstmt);
4264 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
4265 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4269 account_type_s *account_type_record = NULL;
4271 if (rc != SQLITE_ROW)
4273 _INFO("[_ACCOUNT_ERROR_RECORD_NOT_FOUND]The record isn't found.");
4274 error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4278 while(rc == SQLITE_ROW) {
4279 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4281 if (account_type_record == NULL) {
4282 ACCOUNT_FATAL("malloc Failed");
4286 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4287 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4288 account_type_list = g_slist_append(account_type_list, account_type_record);
4289 rc = _account_query_step(hstmt);
4292 rc = _account_query_finalize(hstmt);
4293 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, NULL, ("finalize error"));
4298 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4299 account_type_s *account_type = NULL;
4300 account_type = (account_type_s*)iter->data;
4301 _account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
4302 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
4305 error_code = _ACCOUNT_ERROR_NONE;
4309 rc = _account_query_finalize(hstmt);
4310 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list);}, NULL, ("finalize error"));
4314 if (error_code == _ACCOUNT_ERROR_NONE || error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4315 error_code = _account_type_query_all_from_global_db(&account_type_list);
4316 if (rc != _ACCOUNT_ERROR_NONE && rc != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4317 ACCOUNT_ERROR( "_account_type_query_all_from_global_db fail=[%d]", rc);
4318 _account_type_gslist_account_type_free(account_type_list);
4323 _INFO("_account_type_query_all end");
4324 return account_type_list;
4327 // output parameter label must be free
4328 int _account_type_query_label_by_locale_from_global_db(const char* app_id, const char* locale, char **label)
4330 int error_code = _ACCOUNT_ERROR_NONE;
4331 account_stmt hstmt = NULL;
4332 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4333 int rc = 0, binding_count = 1;
4334 char* converted_locale = NULL;
4336 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
4337 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4338 ACCOUNT_RETURN_VAL((label != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
4339 ACCOUNT_RETURN_VAL((locale != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("locale char is null"));
4340 //Making label newly created
4342 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4344 converted_locale = _account_dup_text(locale);
4345 gchar** tokens = g_strsplit(converted_locale, "-", 2);
4347 if(tokens != NULL) {
4348 if((char*)(tokens[1]) != NULL) {
4349 char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1]));
4350 if(upper_token != NULL) {
4351 _ACCOUNT_FREE(converted_locale);
4352 converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token);
4354 _ACCOUNT_FREE(upper_token);
4359 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, converted_locale);
4360 _ACCOUNT_FREE(converted_locale);
4362 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
4364 if( _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM ){
4365 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
4366 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4369 _account_query_bind_text(hstmt, binding_count++, app_id);
4371 rc = _account_query_step(hstmt);
4372 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4374 label_s* label_record = NULL;
4376 while (rc == SQLITE_ROW) {
4377 label_record = (label_s*) malloc(sizeof(label_s));
4379 if (label_record == NULL) {
4380 ACCOUNT_FATAL("malloc Failed");
4384 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
4386 _account_type_convert_column_to_label(hstmt,label_record);
4388 _ACCOUNT_FREE(*label);
4389 //Making label newly created
4390 *label = _account_dup_text(label_record->label);
4392 _account_type_free_label_with_items(label_record);
4394 rc = _account_query_step(hstmt);
4397 rc = _account_query_finalize(hstmt);
4398 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4401 error_code = _ACCOUNT_ERROR_NONE;
4404 if (hstmt != NULL) {
4405 rc = _account_query_finalize(hstmt);
4406 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4410 _INFO("_account_type_query_label_by_locale_from_global_db() end : error_code = %d", error_code);
4414 // output parameter label must be free
4415 int _account_type_query_label_by_locale(const char* app_id, const char* locale, char **label)
4417 int error_code = _ACCOUNT_ERROR_NONE;
4418 account_stmt hstmt = NULL;
4419 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4420 int rc = 0, binding_count = 1;
4421 char* converted_locale = NULL;
4423 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
4424 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4425 ACCOUNT_RETURN_VAL((label != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
4426 ACCOUNT_RETURN_VAL((locale != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("locale char is null"));
4427 //Making label newly created
4429 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4431 converted_locale = _account_dup_text(locale);
4432 gchar** tokens = g_strsplit(converted_locale, "-", 2);
4434 if(tokens != NULL) {
4435 if((char*)(tokens[1]) != NULL) {
4436 char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1]));
4437 if(upper_token != NULL) {
4438 _ACCOUNT_FREE(converted_locale);
4439 converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token);
4441 _ACCOUNT_FREE(upper_token);
4446 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, converted_locale);
4447 _ACCOUNT_FREE(converted_locale);
4449 hstmt = _account_prepare_query(g_hAccountDB, query);
4451 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
4452 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4453 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4456 _account_query_bind_text(hstmt, binding_count++, app_id);
4458 rc = _account_query_step(hstmt);
4459 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4461 label_s* label_record = NULL;
4463 while (rc == SQLITE_ROW) {
4464 label_record = (label_s*) malloc(sizeof(label_s));
4466 if (label_record == NULL) {
4467 ACCOUNT_FATAL("malloc Failed");
4471 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
4473 _account_type_convert_column_to_label(hstmt,label_record);
4475 _ACCOUNT_FREE(*label);
4476 //Making label newly created
4477 *label = _account_dup_text(label_record->label);
4479 _account_type_free_label_with_items(label_record);
4481 rc = _account_query_step(hstmt);
4484 rc = _account_query_finalize(hstmt);
4485 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4488 error_code = _ACCOUNT_ERROR_NONE;
4491 if (hstmt != NULL) {
4492 rc = _account_query_finalize(hstmt);
4493 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4497 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4498 error_code = _account_type_query_label_by_locale_from_global_db(app_id, locale, label);
4501 _INFO("_account_type_query_label_by_locale() end : error_code = %d", error_code);
4505 static int _account_insert_custom(account_s *account, int account_id)
4507 _INFO("_account_insert_custom start");
4510 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4511 account_stmt hstmt = NULL;
4513 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4515 if (g_slist_length( account->custom_list)==0) {
4516 ACCOUNT_DEBUG( "_account_insert_custom, no custom data\n");
4517 return _ACCOUNT_ERROR_NONE;
4520 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
4522 rc = _account_get_record_count(g_hAccountDB, query);
4524 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
4525 ACCOUNT_ERROR( "Access failed(%d, %s)", _account_db_err_msg(g_hAccountDB));
4526 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4530 ACCOUNT_SLOGE( "_account_insert_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
4531 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4538 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
4541 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4542 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (AccountId, AppId, Key, Value) VALUES "
4543 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
4545 hstmt = _account_prepare_query(g_hAccountDB, query);
4547 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
4548 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4549 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4552 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
4554 account_custom_s* custom_data = NULL;
4555 custom_data = (account_custom_s*)iter->data;
4557 ret = _account_query_bind_int(hstmt, count++, account_id);
4558 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
4559 ret = _account_query_bind_text(hstmt, count++, account->package_name);
4560 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4561 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
4562 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4563 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
4564 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4566 rc = _account_query_step(hstmt);
4568 if (rc != SQLITE_DONE) {
4569 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4573 rc = _account_query_finalize(hstmt);
4574 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4579 _INFO("_account_insert_custom end");
4580 return _ACCOUNT_ERROR_NONE;
4583 static int _account_update_custom(account_s *account, int account_id)
4586 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4587 account_stmt hstmt = NULL;
4589 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4591 if (g_slist_length( account->custom_list)==0) {
4592 ACCOUNT_DEBUG( "_account_update_custom, no custom data\n");
4593 return _ACCOUNT_ERROR_NONE;
4596 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
4598 rc = _account_get_record_count(g_hAccountDB, query);
4600 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
4601 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4602 pthread_mutex_unlock(&account_mutex);
4603 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4604 } else if( _account_db_err_code(g_hAccountDB) == SQLITE_BUSY ){
4605 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg(g_hAccountDB));
4606 pthread_mutex_unlock(&account_mutex);
4607 return _ACCOUNT_ERROR_DATABASE_BUSY;
4611 ACCOUNT_SLOGE( "_account_update_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
4612 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4615 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4617 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId=? ", ACCOUNT_CUSTOM_TABLE);
4618 hstmt = _account_prepare_query(g_hAccountDB, query);
4620 _account_query_bind_int(hstmt, count++, (int)account_id);
4621 rc = _account_query_step(hstmt);
4623 if (rc == SQLITE_BUSY) {
4624 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4625 return _ACCOUNT_ERROR_DATABASE_BUSY;
4626 } else if (rc != SQLITE_DONE) {
4627 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4628 return _ACCOUNT_ERROR_DB_FAILED;
4631 rc = _account_query_finalize(hstmt);
4632 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4637 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
4640 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4641 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AccountId, AppId, Key, Value) VALUES "
4642 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
4644 hstmt = _account_prepare_query(g_hAccountDB, query);
4646 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
4647 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4648 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4651 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
4653 account_custom_s* custom_data = NULL;
4654 custom_data = (account_custom_s*)iter->data;
4656 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
4657 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
4658 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
4659 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4660 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
4661 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4662 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
4663 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4665 rc = _account_query_step(hstmt);
4667 if (rc != SQLITE_DONE) {
4668 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4672 rc = _account_query_finalize(hstmt);
4673 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4678 return _ACCOUNT_ERROR_NONE;