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 #define EMAIL_SERVICE_CMDLINE "/usr/bin/email-service"
46 #define EMAIL_APPID "email-setting-efl"
48 #define ACCOUNT_DB_OPEN_READONLY 0
49 #define ACCOUNT_DB_OPEN_READWRITE 1
53 #define _TIZEN_PUBLIC_
54 #ifndef _TIZEN_PUBLIC_
58 static sqlite3* g_hAccountDB = NULL;
59 static sqlite3* g_hAccountDB2 = NULL;
60 static sqlite3* g_hAccountGlobalDB = NULL;
61 static sqlite3* g_hAccountGlobalDB2 = NULL;
62 pthread_mutex_t account_mutex = PTHREAD_MUTEX_INITIALIZER;
63 pthread_mutex_t account_global_mutex = PTHREAD_MUTEX_INITIALIZER;
65 //static char *_account_dup_text(const char *text_data);
66 static int _account_insert_custom(account_s *account, int account_id);
67 static int _account_update_custom(account_s *account, int account_id);
68 static int _account_type_update_provider_feature(sqlite3 * account_db_handle, account_type_s *account_type, const char* app_id);
70 static void _account_insert_delete_update_notification_send(char *noti_name)
73 _ERR("Noti Name is NULL!!!!!!\n");
77 _INFO("noti_type = %s", noti_name);
79 if (vconf_set_str(VCONFKEY_ACCOUNT_MSG_STR, noti_name) != 0) {
80 _ERR("Vconf MSG Str set FAILED !!!!!!\n");;
84 int _account_get_current_appid_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
88 GSList** appid_list = (GSList**)user_data;
91 pkgmgr_ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
93 if (pkgmgr_ret != PMINFO_R_OK)
94 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_appid(%d)", pkgmgr_ret);
96 item = _account_dup_text(appid);
97 *appid_list = g_slist_append(*appid_list, item);
102 static inline int __read_proc(const char *path, char *buf, int size)
106 if (buf == NULL || path == NULL) {
107 ACCOUNT_ERROR("path and buffer is mandatory\n");
111 fd = open(path, O_RDONLY);
113 ACCOUNT_ERROR("fd open error(%d)\n", fd);
117 ret = read(fd, buf, size - 1);
119 ACCOUNT_ERROR("fd read error(%d)\n", fd);
131 char *_account_get_proc_cmdline_bypid(int pid)
136 ACCOUNT_SNPRINTF(buf, sizeof(buf), "/proc/%d/cmdline", pid);
137 ret = __read_proc(buf, buf, sizeof(buf));
139 ACCOUNT_DEBUG("No proc directory (%d)\n", pid);
147 static int _account_execute_query_from_global_db(const char *query)
150 char* pszErrorMsg = NULL;
153 ACCOUNT_ERROR("NULL query\n");
154 return _ACCOUNT_ERROR_QUERY_SYNTAX_ERROR;
157 if (!g_hAccountGlobalDB) {
158 ACCOUNT_ERROR("Global DB is not opened\n");
159 return _ACCOUNT_ERROR_DB_NOT_OPENED;
162 rc = sqlite3_exec(g_hAccountGlobalDB, query, NULL, NULL, &pszErrorMsg);
163 if (SQLITE_OK != rc) {
164 ACCOUNT_ERROR("sqlite3_exec rc(%d) query(%s) failed(%s).", rc, query, pszErrorMsg);
165 sqlite3_free(pszErrorMsg);
173 static int _account_begin_transaction_from_global_db(void)
175 ACCOUNT_DEBUG("_account_begin_transaction start");
178 ret = _account_execute_query_from_global_db("BEGIN IMMEDIATE TRANSACTION");
180 if (ret == SQLITE_BUSY) {
181 ACCOUNT_ERROR(" sqlite3 busy = %d", ret);
182 return _ACCOUNT_ERROR_DATABASE_BUSY;
183 } else if (ret != SQLITE_OK) {
184 ACCOUNT_ERROR("_account_svc_begin_transaction_in_global_db fail :: %d", ret);
185 return _ACCOUNT_ERROR_DB_FAILED;
188 ACCOUNT_DEBUG("_account_begin_transaction_in_global_db end");
189 return _ACCOUNT_ERROR_NONE;
192 static int _account_end_transaction_from_global_db(bool is_success)
194 ACCOUNT_DEBUG("_account_end_transaction_in_global_db start");
198 if (is_success == true) {
199 ret = _account_execute_query_from_global_db("COMMIT TRANSACTION");
200 ACCOUNT_DEBUG("_account_end_transaction_in_global_db COMMIT");
202 ret = _account_execute_query_from_global_db("ROLLBACK TRANSACTION");
203 ACCOUNT_DEBUG("_account_end_transaction ROLLBACK");
206 if (ret == SQLITE_PERM) {
207 ACCOUNT_ERROR("Account permission denied :: %d", ret);
208 return _ACCOUNT_ERROR_PERMISSION_DENIED;
211 if (ret == SQLITE_BUSY) {
212 ACCOUNT_DEBUG(" sqlite3 busy = %d", ret);
213 return _ACCOUNT_ERROR_DATABASE_BUSY;
216 if (ret != SQLITE_OK) {
217 ACCOUNT_ERROR("_account_svc_end_transaction_in_global_db fail :: %d", ret);
218 return _ACCOUNT_ERROR_DB_FAILED;
221 ACCOUNT_DEBUG("_account_end_transaction_in_global_db end");
222 return _ACCOUNT_ERROR_NONE;
226 int _account_global_db_open(void)
230 char account_db_path[256] = {0, };
232 _INFO("start _account_global_db_open()");
234 ACCOUNT_MEMSET(account_db_path, 0x00, sizeof(account_db_path));
235 ACCOUNT_GET_GLOBAL_DB_PATH(account_db_path, sizeof(account_db_path));
237 if (g_hAccountGlobalDB) {
238 _ERR("Account database is using in another app. %x", g_hAccountDB);
239 return _ACCOUNT_ERROR_DATABASE_BUSY;
242 ret = _account_db_handle_close(g_hAccountGlobalDB2);
243 if (ret != _ACCOUNT_ERROR_NONE)
244 ACCOUNT_DEBUG("db_util_close(g_hAccountGlobalDB2) fail ret = %d", ret);
246 ACCOUNT_DEBUG("before _account_global_db_open()");
247 // if (mode == ACCOUNT_DB_OPEN_READWRITE)
248 // rc = db_util_open(account_db_path, &g_hAccountDB, DB_UTIL_REGISTER_HOOK_METHOD);
249 // else if (mode == ACCOUNT_DB_OPEN_READONLY)
250 rc = db_util_open_with_options(account_db_path, &g_hAccountGlobalDB, SQLITE_OPEN_READONLY, NULL);
252 // return _ACCOUNT_ERROR_DB_NOT_OPENED;
253 ACCOUNT_DEBUG("after _account_global_db_open() sqlite_rc = %d", rc);
255 if (rc == SQLITE_PERM || _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
256 ACCOUNT_ERROR("Account permission denied");
257 return _ACCOUNT_ERROR_PERMISSION_DENIED;
260 if (rc == SQLITE_BUSY) {
261 ACCOUNT_ERROR("busy handler fail.");
262 return _ACCOUNT_ERROR_DATABASE_BUSY;
265 if (rc != SQLITE_OK) {
266 ACCOUNT_ERROR("The database isn't connected.");
267 return _ACCOUNT_ERROR_DB_NOT_OPENED;
270 _INFO("end _account_global_db_open()");
271 return _ACCOUNT_ERROR_NONE;
274 int _account_global_db_close(void)
276 ACCOUNT_DEBUG("start account_global_db_close()");
279 ret = _account_db_handle_close(g_hAccountGlobalDB2);
280 if (ret != _ACCOUNT_ERROR_NONE)
281 ACCOUNT_DEBUG("db_util_close(g_hAccountGlobalDB2) fail ret = %d", ret);
283 ret = _account_db_handle_close(g_hAccountGlobalDB);
284 if (ret != _ACCOUNT_ERROR_NONE) {
285 ACCOUNT_ERROR("db_util_close(g_hAccountGlobalDB) fail ret = %d", ret);
286 g_hAccountGlobalDB2 = g_hAccountGlobalDB;
288 g_hAccountGlobalDB = NULL;
293 static bool _account_check_add_more_account(const char* app_id)
295 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
298 ACCOUNT_RETURN_VAL((app_id != 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
299 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
301 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
303 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s' and MultipleAccountSupport = 1", ACCOUNT_TYPE_TABLE, app_id);
304 rc = _account_get_record_count(g_hAccountDB, query);
306 /* multiple account support case (User DB & global DB) */
307 if (rc > 0 || _account_get_record_count(g_hAccountGlobalDB, query) > 0) {
308 ACCOUNT_SLOGD("app id (%s) supports multiple account. rc(%d)\n", app_id, rc);
312 /* multiple account not support case */
313 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
314 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE package_name = '%s'", ACCOUNT_TABLE, app_id);
315 rc = _account_get_record_count(g_hAccountDB, query);
318 ACCOUNT_SLOGD("app id (%s) supports single account. and there is no account of the app id\n", app_id);
326 int _account_db_open(int mode, int pid, uid_t uid)
330 char account_db_dir[256] = {0, };
331 char account_db_path[256] = {0, };
333 _INFO("start _account_db_open() pid [%d]. uid [%d]", pid, uid);
335 ACCOUNT_MEMSET(account_db_dir, 0x00, sizeof(account_db_dir));
336 ACCOUNT_MEMSET(account_db_path, 0x00, sizeof(account_db_path));
338 ACCOUNT_GET_USER_DB_PATH(account_db_path, sizeof(account_db_path), uid);
341 _ERR("Account database is using in another app. %x", g_hAccountDB);
342 return _ACCOUNT_ERROR_DATABASE_BUSY;
345 ret = _account_db_handle_close(g_hAccountDB2);
346 if (ret != _ACCOUNT_ERROR_NONE)
347 ACCOUNT_DEBUG("db_util_close(g_hAccountDB2) fail ret = %d", ret);
349 ACCOUNT_GET_USER_DB_DIR(account_db_dir, sizeof(account_db_dir), uid);
351 if (mkdir(account_db_dir, 0777) != 0)
352 ACCOUNT_DEBUG("\"%s\" is already exist directory", account_db_dir);
354 ACCOUNT_DEBUG("before db_util_open()");
355 // if (mode == ACCOUNT_DB_OPEN_READWRITE)
356 rc = db_util_open(account_db_path, &g_hAccountDB, DB_UTIL_REGISTER_HOOK_METHOD);
357 // else if (mode == ACCOUNT_DB_OPEN_READONLY)
358 // rc = db_util_open_with_options(account_db_path, &g_hAccountDB, SQLITE_OPEN_READONLY, NULL);
360 // return _ACCOUNT_ERROR_DB_NOT_OPENED;
361 ACCOUNT_DEBUG("after db_util_open() sqlite_rc = %d", rc);
363 if (rc == SQLITE_PERM || _account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
364 ACCOUNT_ERROR("Account permission denied");
365 return _ACCOUNT_ERROR_PERMISSION_DENIED;
368 if (rc == SQLITE_BUSY) {
369 ACCOUNT_ERROR("busy handler fail.");
370 return _ACCOUNT_ERROR_DATABASE_BUSY;
373 if (rc != SQLITE_OK) {
374 ACCOUNT_ERROR("The database isn't connected.");
375 return _ACCOUNT_ERROR_DB_NOT_OPENED;
378 rc = _account_check_is_all_table_exists(g_hAccountDB);
381 _ERR("_account_check_is_all_table_exists rc=[%d]", rc);
383 } else if (rc == ACCOUNT_TABLE_TOTAL_COUNT) {
386 int ret = _account_create_all_tables(g_hAccountDB);
387 if (ret != _ACCOUNT_ERROR_NONE) {
388 _ERR("_account_create_all_tables fail ret=[%d]", ret);
393 _INFO("end _account_db_open()");
394 return _ACCOUNT_ERROR_NONE;
397 int _account_db_close(void)
399 ACCOUNT_DEBUG("start db_util_close()");
402 ret = _account_db_handle_close(g_hAccountDB2);
403 if (ret != _ACCOUNT_ERROR_NONE)
404 ACCOUNT_DEBUG("db_util_close(g_hAccountDB2) fail ret = %d", ret);
406 ret = _account_db_handle_close(g_hAccountDB);
407 if (ret != _ACCOUNT_ERROR_NONE) {
408 ACCOUNT_ERROR("db_util_close(g_hAccountDB) fail ret = %d", ret);
409 g_hAccountDB2 = g_hAccountDB;
416 static int _account_execute_insert_query(account_s *account)
418 _INFO("_account_execute_insert_query start");
421 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
422 int error_code = _ACCOUNT_ERROR_NONE;
423 account_stmt hstmt = NULL;
425 /* check whether app id exist in account type db */
427 if (!account->user_name && !account->display_name && !account->email_address) {
429 ACCOUNT_ERROR("Mandetory fields is NULL. At least one field is required among username, display name, email address\n");
430 return _ACCOUNT_ERROR_INVALID_PARAMETER;
434 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
435 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (user_name, email_address , display_name , icon_path , source , package_name , "
436 "access_token , domain_name , auth_type , secret , sync_support , txt_custom0, txt_custom1, txt_custom2, txt_custom3, txt_custom4, "
437 "int_custom0, int_custom1, int_custom2, int_custom3, int_custom4, txt_custom0 ) values " // to do urusa
438 "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", ACCOUNT_TABLE);
440 hstmt = _account_prepare_query(g_hAccountDB, query);
441 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
444 _account_convert_account_to_sql(account, hstmt, query);
447 rc = _account_query_step(hstmt);
448 if (rc != SQLITE_DONE) {
450 ACCOUNT_ERROR("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
452 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM)
453 error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
455 error_code = _ACCOUNT_ERROR_DB_FAILED;
459 rc = _account_query_finalize(hstmt);
460 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
463 _INFO("_account_execute_insert_query end");
467 static int _account_insert_capability(account_s *account, int account_id)
469 _INFO("_account_insert_capability start");
471 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
472 account_stmt hstmt = NULL;
474 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
476 if (g_slist_length(account->capablity_list) == 0) {
477 ACCOUNT_DEBUG("_account_insert_capability, no capability\n");
478 return _ACCOUNT_ERROR_NONE;
481 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
483 _INFO("_account_insert_capability _account_get_record_count [%s]", query);
484 rc = _account_get_record_count(g_hAccountDB, query);
486 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
487 _ERR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
488 return _ACCOUNT_ERROR_PERMISSION_DENIED;
492 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
498 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
502 account_capability_s* cap_data = NULL;
503 cap_data = (account_capability_s*)iter->data;
505 _INFO("cap_data->type = %s, cap_data->value = %d \n", cap_data->type, cap_data->value);
507 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
508 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
509 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
510 hstmt = _account_prepare_query(g_hAccountDB, query);
512 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
514 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
515 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
516 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
517 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
518 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
519 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
520 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
521 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
522 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
523 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
525 rc = _account_query_step(hstmt);
526 _INFO("_account_insert_capability _account_query_step[%d]", rc);
528 if (rc != SQLITE_DONE) {
529 _ERR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
533 rc = _account_query_finalize(hstmt);
534 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
539 _INFO("_account_insert_capability end");
540 return _ACCOUNT_ERROR_NONE;
543 static int _account_update_capability(account_s *account, int account_id)
546 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
547 account_stmt hstmt = NULL;
549 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
551 if (g_slist_length(account->capablity_list) == 0) {
552 ACCOUNT_ERROR("_account_update_capability, no capability\n");
553 return _ACCOUNT_ERROR_NONE;
556 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
558 rc = _account_get_record_count(g_hAccountDB, query);
561 ACCOUNT_SLOGI("_account_update_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
562 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
565 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
567 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id=? ", CAPABILITY_TABLE);
568 hstmt = _account_prepare_query(g_hAccountDB, query);
570 _account_query_bind_int(hstmt, count++, (int)account_id);
571 rc = _account_query_step(hstmt);
573 if (rc != SQLITE_DONE) {
574 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
575 return _ACCOUNT_ERROR_DB_FAILED;
577 rc = _account_query_finalize(hstmt);
578 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
583 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
586 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
587 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
588 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
590 hstmt = _account_prepare_query(g_hAccountDB, query);
592 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
594 account_capability_s* cap_data = NULL;
595 cap_data = (account_capability_s*)iter->data;
597 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
598 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
599 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
600 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
601 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
602 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
603 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
604 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
605 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
606 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
608 rc = _account_query_step(hstmt);
610 if (rc != SQLITE_DONE) {
611 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
615 rc = _account_query_finalize(hstmt);
616 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
621 return _ACCOUNT_ERROR_NONE;
624 static int _account_update_capability_by_user_name(account_s *account, const char *user_name, const char *package_name)
627 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
628 account_stmt hstmt = NULL;
630 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
632 if (g_slist_length(account->capablity_list) == 0) {
633 ACCOUNT_ERROR("_account_update_capability_by_user_name, no capability\n");
634 return _ACCOUNT_ERROR_NONE;
637 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where package_name= '%s' and user_name='%s'", ACCOUNT_TABLE, package_name, user_name);
639 rc = _account_get_record_count(g_hAccountDB, query);
642 ACCOUNT_SLOGI("_account_update_capability_by_user_name : related account item is not existed rc=%d , %s ", rc, _account_db_err_msg(g_hAccountDB));
643 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
646 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
648 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name=? and user_name=? ", CAPABILITY_TABLE);
649 hstmt = _account_prepare_query(g_hAccountDB, query);
651 _account_query_bind_text(hstmt, count++, (char*)account->package_name);
652 _account_query_bind_text(hstmt, count++, (char*)account->user_name);
653 rc = _account_query_step(hstmt);
654 if (rc != SQLITE_DONE) {
655 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
656 return _ACCOUNT_ERROR_DB_FAILED;
659 rc = _account_query_finalize(hstmt);
660 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
665 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
668 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
669 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
670 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
672 hstmt = _account_prepare_query(g_hAccountDB, query);
674 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
676 account_capability_s* cap_data = NULL;
677 cap_data = (account_capability_s*)iter->data;
679 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
680 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
681 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
682 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
683 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
684 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
685 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
686 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
687 ret = _account_query_bind_int(hstmt, count++, (int)account->id);
688 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
690 rc = _account_query_step(hstmt);
692 if (rc != SQLITE_DONE) {
693 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
697 rc = _account_query_finalize(hstmt);
698 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
703 return _ACCOUNT_ERROR_NONE;
707 bool _account_add_capability_to_account_cb(const char* capability_type, int capability_value, account_s *account)
709 account_capability_s *cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
711 if (cap_data == NULL)
713 ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
715 cap_data->type = _account_dup_text(capability_type);
716 cap_data->value = capability_value;
717 _INFO("cap_data->type = %s, cap_data->value = %d", cap_data->type, cap_data->value);
719 account->capablity_list = g_slist_append(account->capablity_list, (gpointer)cap_data);
725 bool _account_add_custom_to_account_cb(const char* key, const char* value, account_s *account)
727 account_custom_s *custom_data = (account_custom_s*)malloc(sizeof(account_custom_s));
729 if (custom_data == NULL) {
730 ACCOUNT_DEBUG("_account_add_custom_to_account_cb :: malloc fail\n");
733 ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s));
735 custom_data->account_id = account->id;
736 custom_data->app_id = _account_dup_text(account->package_name);
737 custom_data->key = _account_dup_text(key);
738 custom_data->value = _account_dup_text(value);
739 _INFO("custom_data->key = %s, custom_data->value = %s", custom_data->key, custom_data->value);
741 account->custom_list = g_slist_append(account->custom_list, (gpointer)custom_data);
747 static int _account_compare_old_record_by_user_name(account_s *new_account, const char* user_name, const char* package_name)
749 int error_code = _ACCOUNT_ERROR_NONE;
750 account_stmt hstmt = NULL;
751 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
753 account_s *old_account = NULL;
755 ACCOUNT_RETURN_VAL((new_account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
756 ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
757 ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
758 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
760 old_account = (account_s*)calloc(1, sizeof(account_s));
762 ACCOUNT_FATAL("Memory alloc fail\n");
763 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
766 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
768 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = '%s' and package_name='%s'", ACCOUNT_TABLE, user_name, package_name);
769 hstmt = _account_prepare_query(g_hAccountDB, query);
771 rc = _account_query_step(hstmt);
772 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
774 while (rc == SQLITE_ROW) {
775 _account_convert_column_to_account(hstmt, old_account);
776 rc = _account_query_step(hstmt);
779 rc = _account_query_finalize(hstmt);
780 ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
784 error_code = _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, old_account->id, (void*)old_account, false);
785 ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
788 error_code = _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, old_account->id, (void*)old_account, false);
789 ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
792 new_account->id = old_account->id;
795 if (!new_account->user_name) {
796 if (old_account->user_name)
797 new_account->user_name = _account_dup_text(old_account->user_name);
801 if (!new_account->display_name) {
802 if (old_account->display_name)
803 new_account->display_name = _account_dup_text(old_account->display_name);
807 if (!new_account->email_address) {
808 if (old_account->email_address)
809 new_account->email_address = _account_dup_text(old_account->email_address);
813 if (!new_account->domain_name) {
814 if (old_account->domain_name)
815 new_account->domain_name = _account_dup_text(old_account->domain_name);
819 if (!new_account->icon_path) {
820 if (old_account->icon_path)
821 new_account->icon_path = _account_dup_text(old_account->icon_path);
825 if (!new_account->source) {
826 if (old_account->source)
827 new_account->source = _account_dup_text(old_account->source);
830 _ACCOUNT_FREE(new_account->package_name);
831 new_account->package_name = _account_dup_text(old_account->package_name);
834 if (!new_account->access_token) {
835 if (old_account->access_token)
836 new_account->access_token = _account_dup_text(old_account->access_token);
840 if (new_account->auth_type == _ACCOUNT_AUTH_TYPE_INVALID)
841 new_account->auth_type = old_account->auth_type;
844 if (new_account->secret == _ACCOUNT_SECRECY_INVALID)
845 new_account->secret = old_account->secret;
848 if (new_account->sync_support == _ACCOUNT_SYNC_INVALID)
849 new_account->sync_support = old_account->sync_support;
853 for (i = 0; i < USER_TXT_CNT; i++) {
854 if (!new_account->user_data_txt[i]) {
855 if (old_account->user_data_txt[i])
856 new_account->user_data_txt[i] = _account_dup_text(old_account->user_data_txt[i]);
861 for (i = 0; i < USER_INT_CNT; i++) {
862 if (new_account->user_data_int[i] == 0)
863 new_account->user_data_int[i] = old_account->user_data_int[i];
868 _account_free_account_with_items(old_account);
871 rc = _account_query_finalize(hstmt);
872 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
876 return _ACCOUNT_ERROR_NONE;
879 static int _account_update_account_by_user_name(int pid, uid_t uid, account_s *account, const char *user_name, const char *package_name)
881 int rc = 0, binding_count = 0, count = 0;
882 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
883 int error_code = _ACCOUNT_ERROR_NONE;
884 account_stmt hstmt = NULL;
886 ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is NULL.\n"));
887 ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is NULL.\n"));
889 char* current_appid = NULL;
890 char* verified_appid = NULL;
892 current_appid = _account_get_current_appid(pid, uid);
893 error_code = _account_get_represented_appid_from_db(g_hAccountDB, g_hAccountGlobalDB, current_appid, uid, &verified_appid);
895 _ACCOUNT_FREE(current_appid);
896 _ACCOUNT_FREE(verified_appid);
898 if (error_code != _ACCOUNT_ERROR_NONE) {
899 ACCOUNT_ERROR("No permission to update\n");
900 return _ACCOUNT_ERROR_PERMISSION_DENIED;
903 error_code = encrypt_access_token(account);
904 if (error_code != _ACCOUNT_ERROR_NONE) {
905 _ERR("_encrypt_access_token error");
909 _account_compare_old_record_by_user_name(account, user_name, package_name);
911 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
912 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
913 return _ACCOUNT_ERROR_PERMISSION_DENIED;
916 if (!account->package_name) {
917 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
918 return _ACCOUNT_ERROR_INVALID_PARAMETER;
921 if (!account->user_name && !account->display_name && !account->email_address) {
922 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
923 return _ACCOUNT_ERROR_INVALID_PARAMETER;
926 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE user_name='%s' and package_name='%s'"
927 , ACCOUNT_TABLE, user_name, package_name);
929 count = _account_get_record_count(g_hAccountDB, query);
931 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
932 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
933 return _ACCOUNT_ERROR_PERMISSION_DENIED;
937 ACCOUNT_SLOGI("_account_update_account_by_user_name : The account not exist!, count = %d, user_name=%s, package_name=%s\n",
938 count, user_name, package_name);
939 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
942 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
944 //TODO: Is it required to update id ? As of now I can only think of falied rollback cases (between account and gSSO DB)
945 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
946 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
947 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
948 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE user_name=? and package_name=? ", ACCOUNT_TABLE);
950 hstmt = _account_prepare_query(g_hAccountDB, query);
951 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
952 _account_end_transaction(g_hAccountDB, FALSE);
953 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
954 return _ACCOUNT_ERROR_PERMISSION_DENIED;
956 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
958 binding_count = _account_convert_account_to_sql(account, hstmt, query);
960 _account_query_bind_text(hstmt, binding_count++, user_name);
961 _account_query_bind_text(hstmt, binding_count++, package_name);
963 rc = _account_query_step(hstmt);
964 if (rc != SQLITE_DONE)
965 ACCOUNT_ERROR("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
967 rc = _account_query_finalize(hstmt);
968 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
971 /*update capability*/
972 error_code = _account_update_capability_by_user_name(account, user_name, package_name);
975 error_code = _account_update_custom(account, account->id);
980 int _account_insert_to_db(account_s* account, int pid, uid_t uid, int *account_id)
983 int error_code = _ACCOUNT_ERROR_NONE;
984 int ret_transaction = 0;
986 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
987 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
988 ACCOUNT_RETURN_VAL((account_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID POINTER IS NULL"));
990 if (!account->user_name && !account->display_name && !account->email_address) {
991 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
992 return _ACCOUNT_ERROR_INVALID_PARAMETER;
995 account_s *data = (account_s*)account;
996 ACCOUNT_SLOGD("(%s)-(%d) account_insert_to_db: begin_transaction.\n", __FUNCTION__, __LINE__);
998 pthread_mutex_lock(&account_mutex);
1000 /* transaction control required*/
1001 ret_transaction = _account_begin_transaction(g_hAccountDB);
1003 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1004 pthread_mutex_unlock(&account_mutex);
1005 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1006 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1009 if (ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY) {
1010 ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction);
1011 pthread_mutex_unlock(&account_mutex);
1012 return _ACCOUNT_ERROR_DATABASE_BUSY;
1013 } else if (ret_transaction != _ACCOUNT_ERROR_NONE) {
1014 ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction);
1015 pthread_mutex_unlock(&account_mutex);
1016 return ret_transaction;
1019 *account_id = _account_get_next_sequence(g_hAccountDB, ACCOUNT_TABLE);
1020 data->id = *account_id;
1023 appid = _account_get_current_appid(pid, uid);
1027 // API caller cannot be recognized
1028 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1029 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1030 pthread_mutex_unlock(&account_mutex);
1031 return _ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
1035 char* verified_appid = NULL;
1036 error_code = _account_get_represented_appid_from_db(g_hAccountDB, g_hAccountGlobalDB, appid, uid, &verified_appid);//FIX
1037 _ACCOUNT_FREE(appid);
1038 if (error_code != _ACCOUNT_ERROR_NONE) {
1039 _ERR("error_code = %d", error_code);
1040 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1041 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1042 _ACCOUNT_FREE(verified_appid);
1043 pthread_mutex_unlock(&account_mutex);
1047 if (verified_appid) {
1049 error_code = _account_check_duplicated(g_hAccountDB, data, verified_appid, uid);
1050 if (error_code != _ACCOUNT_ERROR_NONE) {
1052 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1053 ACCOUNT_DEBUG("_account_check_duplicated(), rollback insert query(%x)!!!!\n", ret_transaction);
1055 pthread_mutex_unlock(&account_mutex);
1058 if (!_account_check_add_more_account(verified_appid)) {
1059 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1060 ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
1061 pthread_mutex_unlock(&account_mutex);
1062 _ACCOUNT_FREE(verified_appid);
1063 return _ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
1066 _ACCOUNT_FREE(data->package_name);
1067 data->package_name = _account_dup_text(verified_appid);
1068 _ACCOUNT_FREE(verified_appid);
1071 if (!_account_check_add_more_account(data->package_name)) {
1073 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1074 ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
1075 pthread_mutex_unlock(&account_mutex);
1076 return _ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
1079 error_code = encrypt_access_token(data);
1080 if (error_code != _ACCOUNT_ERROR_NONE) {
1081 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1082 ACCOUNT_ERROR("encrypt_access_token fail, rollback insert query(%x)!!!!\n", ret_transaction);
1084 pthread_mutex_unlock(&account_mutex);
1088 error_code = _account_execute_insert_query(data);
1090 if (error_code != _ACCOUNT_ERROR_NONE) {
1092 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1093 ACCOUNT_ERROR("INSERT account fail, rollback insert query(%x)!!!!\n", ret_transaction);
1095 pthread_mutex_unlock(&account_mutex);
1100 error_code = _account_insert_capability(data, *account_id);
1101 if (error_code != _ACCOUNT_ERROR_NONE) {
1103 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1104 ACCOUNT_ERROR("INSERT capability fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1106 pthread_mutex_unlock(&account_mutex);
1111 error_code = _account_insert_custom(data, *account_id);
1112 if (error_code != _ACCOUNT_ERROR_NONE) {
1113 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1114 ACCOUNT_ERROR("INSERT custom fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1116 pthread_mutex_unlock(&account_mutex);
1122 pthread_mutex_unlock(&account_mutex);
1123 _account_end_transaction(g_hAccountDB, TRUE);
1124 ACCOUNT_SLOGD("(%s)-(%d) account _end_transaction.\n", __FUNCTION__, __LINE__);
1126 char buf[64] = {0,};
1127 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_INSERT, *account_id);
1128 _account_insert_delete_update_notification_send(buf);
1129 _INFO("account _notification_send end.");
1131 return _ACCOUNT_ERROR_NONE;
1134 GSList* _account_get_capability_list_by_account_id(int account_id, int *error_code)
1136 *error_code = _ACCOUNT_ERROR_NONE;
1137 account_stmt hstmt = NULL;
1138 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1140 GSList* capability_list = NULL;
1142 ACCOUNT_RETURN_VAL((account_id > 0), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("ACCOUNT INDEX IS LESS THAN 0"));
1143 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), { *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED; }, NULL, ("The database isn't connected."));
1145 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1147 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
1148 hstmt = _account_prepare_query(g_hAccountDB, query);
1150 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1151 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1152 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
1156 rc = _account_query_step(hstmt);
1157 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1159 account_capability_s* capability_record = NULL;
1161 while (rc == SQLITE_ROW) {
1162 capability_record = (account_capability_s*) malloc(sizeof(account_capability_s));
1164 if (capability_record == NULL) {
1165 ACCOUNT_FATAL("malloc Failed");
1169 ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s));
1171 _account_convert_column_to_capability(hstmt, capability_record);
1173 //cb_ret = callback(capability_record->type, capability_record->value, user_data);
1175 //_account_free_capability_items(capability_record);
1176 //_ACCOUNT_FREE(capability_record);
1178 //ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
1180 capability_list = g_slist_append(capability_list, capability_record);
1181 rc = _account_query_step(hstmt);
1184 rc = _account_query_finalize(hstmt);
1185 if (rc != _ACCOUNT_ERROR_NONE) {
1186 _ERR("finalize error");
1187 _account_gslist_capability_free(capability_list);
1194 *error_code = _ACCOUNT_ERROR_NONE;
1197 if (hstmt != NULL) {
1198 rc = _account_query_finalize(hstmt);
1199 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), { *error_code = rc; }, NULL, ("finalize error"));
1203 pthread_mutex_unlock(&account_mutex);
1205 return capability_list;
1208 static int _account_compare_old_record(account_s *new_account, int account_id)
1210 int error_code = _ACCOUNT_ERROR_NONE;
1211 account_stmt hstmt = NULL;
1212 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1214 account_s *old_account = NULL;
1216 ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1217 ACCOUNT_RETURN_VAL((new_account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
1218 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1220 old_account = (account_s*)calloc(1, sizeof(account_s));
1221 if (old_account == NULL) {
1222 _ERR("Out of Memory");
1223 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
1226 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1228 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
1229 hstmt = _account_prepare_query(g_hAccountDB, query);
1231 rc = _account_query_step(hstmt);
1232 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1234 while (rc == SQLITE_ROW) {
1235 _account_convert_column_to_account(hstmt, old_account);
1236 rc = _account_query_step(hstmt);
1239 rc = _account_query_finalize(hstmt);
1240 ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1244 error_code = _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, old_account->id, (void*)old_account, false);
1245 ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
1248 error_code = _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, old_account->id, (void*)old_account, false);
1249 ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
1253 new_account->id = old_account->id;
1256 if (!new_account->user_name) {
1257 if (old_account->user_name)
1258 new_account->user_name = _account_dup_text(old_account->user_name);
1262 if (!new_account->display_name) {
1263 if (old_account->display_name)
1264 new_account->display_name = _account_dup_text(old_account->display_name);
1268 if (!new_account->email_address) {
1269 if (old_account->email_address)
1270 new_account->email_address = _account_dup_text(old_account->email_address);
1274 if (!new_account->domain_name) {
1275 if (old_account->domain_name)
1276 new_account->domain_name = _account_dup_text(old_account->domain_name);
1280 if (!new_account->icon_path) {
1281 if (old_account->icon_path)
1282 new_account->icon_path = _account_dup_text(old_account->icon_path);
1286 if (!new_account->source) {
1287 if (old_account->source)
1288 new_account->source = _account_dup_text(old_account->source);
1291 _ACCOUNT_FREE(new_account->package_name);
1292 new_account->package_name = _account_dup_text(old_account->package_name);
1295 if (!new_account->access_token) {
1296 if (old_account->access_token)
1297 new_account->access_token = _account_dup_text(old_account->access_token);
1302 for (i = 0; i < USER_TXT_CNT; i++) {
1303 if (!new_account->user_data_txt[i]) {
1304 if (old_account->user_data_txt[i])
1305 new_account->user_data_txt[i] = _account_dup_text(old_account->user_data_txt[i]);
1310 if (new_account->auth_type == _ACCOUNT_AUTH_TYPE_INVALID)
1311 new_account->auth_type = old_account->auth_type;
1314 if (new_account->secret == _ACCOUNT_SECRECY_INVALID)
1315 new_account->secret = old_account->secret;
1318 if (new_account->sync_support == _ACCOUNT_SYNC_INVALID)
1319 new_account->sync_support = old_account->sync_support;
1322 for (i = 0; i < USER_INT_CNT; i++) {
1323 if (new_account->user_data_int[i] == 0)
1324 new_account->user_data_int[i] = old_account->user_data_int[i];
1329 _account_free_account_with_items(old_account);
1331 if (hstmt != NULL) {
1332 rc = _account_query_finalize(hstmt);
1333 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1337 return _ACCOUNT_ERROR_NONE;
1340 static int _account_get_package_name_from_account_id(int account_id, char **package_name)
1342 int error_code = _ACCOUNT_ERROR_NONE;
1343 account_stmt hstmt = NULL;
1344 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1346 account_s *old_account = NULL;
1348 ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1349 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1351 old_account = (account_s*)calloc(1, sizeof(account_s));
1352 if (old_account == NULL) {
1353 _ERR("Out Of memory");
1354 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
1357 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1359 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
1360 hstmt = _account_prepare_query(g_hAccountDB, query);
1362 rc = _account_query_step(hstmt);
1363 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1365 while (rc == SQLITE_ROW) {
1366 _account_convert_column_to_account(hstmt, old_account);
1367 rc = _account_query_step(hstmt);
1370 rc = _account_query_finalize(hstmt);
1371 ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1374 // get package name.
1375 *package_name = _account_dup_text(old_account->package_name);
1379 _account_free_account_with_items(old_account);
1381 if (hstmt != NULL) {
1382 rc = _account_query_finalize(hstmt);
1383 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1391 static int _account_update_account(int pid, uid_t uid, account_s *account, int account_id)
1393 int rc = 0, binding_count = 0;
1394 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1395 int error_code = _ACCOUNT_ERROR_NONE, count = 0, ret_transaction = 0;
1396 account_stmt hstmt = NULL;
1398 if (!account->package_name) {
1399 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
1400 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1403 /* Check permission of requested appid */
1404 char* current_appid = NULL;
1405 char *package_name = NULL;
1407 current_appid = _account_get_current_appid(pid, uid);
1408 error_code = _account_get_package_name_from_account_id(account_id, &package_name);
1410 if (error_code != _ACCOUNT_ERROR_NONE || package_name == NULL) {
1411 ACCOUNT_ERROR("No package name with account_id\n");
1412 _ACCOUNT_FREE(current_appid);
1413 _ACCOUNT_FREE(package_name);
1414 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1417 error_code = _account_check_appid_group_with_package_name(current_appid, package_name, uid);
1418 ACCOUNT_DEBUG("UPDATE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name); // TODO: remove the log later.
1420 _ACCOUNT_FREE(current_appid);
1421 _ACCOUNT_FREE(package_name);
1423 if (error_code != _ACCOUNT_ERROR_NONE) {
1424 ACCOUNT_ERROR("No permission to update\n");
1425 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1428 error_code = encrypt_access_token(account);
1429 if (error_code != _ACCOUNT_ERROR_NONE) {
1430 _ERR("_encrypt_access_token error");
1434 error_code = _account_compare_old_record(account, account_id);
1435 if (error_code != _ACCOUNT_ERROR_NONE) {
1436 ACCOUNT_ERROR("_account_compare_old_record fail\n");
1440 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1441 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1442 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1443 } else if (_account_db_err_code(g_hAccountDB) == SQLITE_BUSY) {
1444 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
1445 return _ACCOUNT_ERROR_DATABASE_BUSY;
1448 if (!account->user_name && !account->display_name && !account->email_address) {
1449 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1450 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1453 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1455 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
1457 count = _account_get_record_count(g_hAccountDB, query);
1459 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
1460 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1463 /* transaction control required*/
1464 ret_transaction = _account_begin_transaction(g_hAccountDB);
1465 if (ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY) {
1466 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
1467 pthread_mutex_unlock(&account_mutex);
1468 return _ACCOUNT_ERROR_DATABASE_BUSY;
1471 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1472 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
1473 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
1474 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
1475 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
1477 hstmt = _account_prepare_query(g_hAccountDB, query);
1479 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1480 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1481 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1482 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1485 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)));
1487 binding_count = _account_convert_account_to_sql(account, hstmt, query);
1488 _account_query_bind_int(hstmt, binding_count++, account_id);
1490 rc = _account_query_step(hstmt);
1491 if (rc != SQLITE_DONE)
1492 ACCOUNT_SLOGE("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
1494 rc = _account_query_finalize(hstmt);
1495 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1498 _INFO("update query=%s", query);
1500 /*update capability*/
1501 error_code = _account_update_capability(account, account_id);
1502 if (error_code != _ACCOUNT_ERROR_NONE && error_code != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
1503 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1504 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1509 error_code = _account_update_custom(account, account_id);
1510 if (error_code != _ACCOUNT_ERROR_NONE && error_code != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
1511 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1512 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1516 ret_transaction = _account_end_transaction(g_hAccountDB, TRUE);
1518 _INFO("update end");
1524 static int _account_update_account_ex(account_s *account, int account_id)
1526 int rc = 0, binding_count = 0;
1527 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1528 int error_code = _ACCOUNT_ERROR_NONE, count = 0, ret_transaction = 0;
1529 account_stmt hstmt = NULL;
1531 if (!account->package_name) {
1532 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
1533 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1536 error_code = encrypt_access_token(account);
1537 if (error_code != _ACCOUNT_ERROR_NONE) {
1538 _ERR("_encrypt_access_token error");
1542 error_code = _account_compare_old_record(account, account_id);
1543 if (error_code != _ACCOUNT_ERROR_NONE) {
1544 ACCOUNT_ERROR("_account_compare_old_record fail\n");
1548 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1549 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1550 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1553 if (!account->user_name && !account->display_name && !account->email_address) {
1554 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1555 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1558 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1560 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
1562 count = _account_get_record_count(g_hAccountDB, query);
1564 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
1565 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1568 /* transaction control required*/
1569 ret_transaction = _account_begin_transaction(g_hAccountDB);
1570 if (ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY) {
1571 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
1572 pthread_mutex_unlock(&account_mutex);
1573 return _ACCOUNT_ERROR_DATABASE_BUSY;
1576 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1577 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
1578 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
1579 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
1580 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
1582 hstmt = _account_prepare_query(g_hAccountDB, query);
1584 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1585 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1586 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1587 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1590 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)));
1592 _INFO("account_update_to_db_by_id_ex_p : before convert() : account_id[%d], user_name=%s", account->id, account->user_name);
1593 binding_count = _account_convert_account_to_sql(account, hstmt, query);
1594 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], user_name=%s", account->id, account->user_name);
1595 _INFO("account_update_to_db_by_id_ex_p : before bind()");
1596 rc = _account_query_bind_int(hstmt, binding_count++, account_id);
1597 _INFO("account_update_to_db_by_id_ex_p : after bind() : ret = %d", rc);
1599 rc = _account_query_step(hstmt);
1600 if (rc != SQLITE_DONE)
1601 ACCOUNT_SLOGE("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
1602 _INFO("account_update_to_db_by_id_ex_p : after query_step() : ret = %d", rc);
1604 rc = _account_query_finalize(hstmt);
1605 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1607 _INFO("account_update_to_db_by_id_ex_p : after query_filnalize() : ret = %d", rc);
1609 _INFO("account_update_to_db_by_id_ex_p : before update_capability()");
1610 /*update capability*/
1611 error_code = _account_update_capability(account, account_id);
1612 if (error_code != _ACCOUNT_ERROR_NONE && error_code != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
1613 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1614 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1617 _INFO("account_update_to_db_by_id_ex_p : after update_capability()");
1619 _INFO("account_update_to_db_by_id_ex_p : before update_custom()");
1621 error_code = _account_update_custom(account, account_id);
1622 if (error_code != _ACCOUNT_ERROR_NONE && error_code != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
1623 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1624 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1627 _INFO("account_update_to_db_by_id_ex_p : after update_custom()");
1629 ret_transaction = _account_end_transaction(g_hAccountDB, TRUE);
1635 int _account_update_to_db_by_id(int pid, uid_t uid, account_s* account, int account_id)
1637 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
1638 ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
1639 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1640 int error_code = _ACCOUNT_ERROR_NONE;
1641 account_s* data = (account_s*)account;
1643 pthread_mutex_lock(&account_mutex);
1645 error_code = _account_update_account(pid, uid, data, account_id);
1647 if (error_code != _ACCOUNT_ERROR_NONE) {
1648 pthread_mutex_unlock(&account_mutex);
1652 pthread_mutex_unlock(&account_mutex);
1654 char buf[64] = {0,};
1655 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_UPDATE, account_id);
1656 _account_insert_delete_update_notification_send(buf);
1658 return _ACCOUNT_ERROR_NONE;
1661 int _account_update_to_db_by_id_ex(account_s* account, int account_id)
1663 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
1664 ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
1665 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1666 int error_code = _ACCOUNT_ERROR_NONE;
1667 account_s* data = account;
1669 pthread_mutex_lock(&account_mutex);
1671 _INFO("before update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name);
1672 error_code = _account_update_account_ex(data, account_id);
1673 _INFO("after update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name);
1675 if (error_code != _ACCOUNT_ERROR_NONE) {
1676 pthread_mutex_unlock(&account_mutex);
1680 pthread_mutex_unlock(&account_mutex);
1682 char buf[64] = {0,};
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_user_name(int pid, uid_t uid, account_s* account, const char *user_name, const char *package_name)
1691 ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
1692 ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
1693 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1695 int error_code = _ACCOUNT_ERROR_NONE;
1696 account_s *data = (account_s*)account;
1698 pthread_mutex_lock(&account_mutex);
1700 error_code = _account_update_account_by_user_name(pid, uid, data, user_name, package_name);
1702 pthread_mutex_unlock(&account_mutex);
1704 char buf[64] = {0,};
1705 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_UPDATE, data->id);
1706 _account_insert_delete_update_notification_send(buf);
1711 GSList* _account_db_query_all(int pid, uid_t uid)
1713 //int error_code = _ACCOUNT_ERROR_NONE;
1714 account_stmt hstmt = NULL;
1715 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1717 GSList *account_list = NULL;
1719 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected."));
1721 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1723 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TABLE);
1724 hstmt = _account_prepare_query(g_hAccountDB, query);
1726 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1727 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1731 rc = _account_query_step(hstmt);
1733 account_s *account_record = NULL;
1735 if (rc != SQLITE_ROW) {
1736 _ERR("The record isn't found");
1740 while (rc == SQLITE_ROW) {
1741 account_record = (account_s*) malloc(sizeof(account_s));
1743 if (account_record == NULL) {
1744 ACCOUNT_FATAL("malloc Failed");
1748 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
1749 _account_convert_column_to_account(hstmt, account_record);
1750 account_list = g_slist_append(account_list, account_record);
1751 rc = _account_query_step(hstmt);
1754 rc = _account_query_finalize(hstmt);
1755 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_gslist_account_free(account_list); }, NULL, ("finalize error"));
1760 for (iter = account_list; iter != NULL; iter = g_slist_next(iter)) {
1761 account_s *account = NULL;
1762 account = (account_s*)iter->data;
1763 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, account->id, (void*)account, false);
1764 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, account->id, (void*)account, false);
1768 if (hstmt != NULL) {
1769 rc = _account_query_finalize(hstmt);
1770 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_gslist_account_free(account_list); }, NULL, ("finalize error"));
1775 _remove_sensitive_info_from_non_owning_account_slist(account_list, pid, uid);
1777 return account_list;
1780 int _account_update_sync_status_by_id(uid_t uid, int account_db_id, const int sync_status)
1782 int error_code = _ACCOUNT_ERROR_NONE;
1783 account_stmt hstmt = NULL;
1784 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1788 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1789 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1790 if ((sync_status < 0) || (sync_status >= _ACCOUNT_SYNC_MAX)) {
1791 ACCOUNT_SLOGE("(%s)-(%d) sync_status is less than 0 or more than enum max.\n", __FUNCTION__, __LINE__);
1792 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1795 pthread_mutex_lock(&account_mutex);
1797 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1799 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_db_id);
1801 rc = _account_get_record_count(g_hAccountDB, query);
1803 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1804 pthread_mutex_unlock(&account_mutex);
1805 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1806 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1810 ACCOUNT_SLOGE("account_update_sync_status_by_id : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
1811 pthread_mutex_unlock(&account_mutex);
1812 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1815 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1817 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET sync_support=? WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
1818 hstmt = _account_prepare_query(g_hAccountDB, query);
1820 _account_query_bind_int(hstmt, count, sync_status);
1822 rc = _account_query_step(hstmt);
1824 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1825 pthread_mutex_unlock(&account_mutex);
1826 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1827 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1830 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_DB_FAILED,
1831 ("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB)));
1833 rc = _account_query_finalize(hstmt);
1834 if (rc != _ACCOUNT_ERROR_NONE) {
1835 ACCOUNT_ERROR("_account_query_finalize error");
1836 pthread_mutex_unlock(&account_mutex);
1840 char buf[64] = {0,};
1841 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_SYNC_UPDATE, account_db_id);
1842 _account_insert_delete_update_notification_send(buf);
1845 error_code = _ACCOUNT_ERROR_NONE;
1848 if (hstmt != NULL) {
1849 rc = _account_query_finalize(hstmt);
1850 pthread_mutex_unlock(&account_mutex);
1851 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1855 pthread_mutex_unlock(&account_mutex);
1860 int account_server_empty_deleted_account_info_tables(const char* del_account_table, const char* del_capability_table,
1861 const char* del_account_custom_table)
1863 _INFO("account_server_empty_deleted_account_info_tables start");
1865 account_stmt hstmt = NULL;
1866 int error_code = _ACCOUNT_ERROR_NONE;
1868 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1870 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1872 rc = _account_begin_transaction(g_hAccountDB);
1873 _INFO("after _account_begin_trasaction");
1875 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1876 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1877 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1880 if (rc == _ACCOUNT_ERROR_DATABASE_BUSY) {
1881 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
1882 return _ACCOUNT_ERROR_DATABASE_BUSY;
1883 } else if (rc != _ACCOUNT_ERROR_NONE) {
1884 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", rc);
1888 /*clear deleted account table*/
1889 ACCOUNT_MEMSET(query, 0, sizeof(query));
1890 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s", del_account_table);
1892 _INFO("clearing deleted-account-table - query[%s]", query);
1894 hstmt = _account_prepare_query(g_hAccountDB, query);
1895 if ((_account_db_err_code(g_hAccountDB) == SQLITE_PERM)) {
1896 _account_end_transaction(g_hAccountDB, false);
1897 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1898 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1901 if (hstmt == NULL) {
1902 _ERR("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB));
1903 error_code = _ACCOUNT_ERROR_DB_FAILED;
1907 rc = _account_query_step(hstmt);
1908 if (rc != SQLITE_DONE) {
1909 _ERR("The record isn't found");
1910 error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1914 rc = _account_query_finalize(hstmt);
1915 if (rc != _ACCOUNT_ERROR_NONE) {
1916 _ERR("finalize error");
1921 /*clear deleted capability table*/
1922 ACCOUNT_MEMSET(query, 0, sizeof(query));
1923 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s", del_capability_table);
1925 _INFO("clearing deleted-capability-table - query[%s]", query);
1927 hstmt = _account_prepare_query(g_hAccountDB, query);
1928 if (hstmt == NULL) {
1929 _ERR("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB));
1930 error_code = _ACCOUNT_ERROR_DB_FAILED;
1934 rc = _account_query_step(hstmt);
1935 if (rc != SQLITE_DONE) {
1936 _ERR("The record isn't found");
1937 error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1941 rc = _account_query_finalize(hstmt);
1942 if (rc != _ACCOUNT_ERROR_NONE) {
1943 _ERR("finalize error");
1948 /*clear deleted account_custom table*/
1949 ACCOUNT_MEMSET(query, 0, sizeof(query));
1950 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s", del_account_custom_table);
1952 _INFO("clearing deleted-account-custom-table - query[%s]", query);
1954 hstmt = _account_prepare_query(g_hAccountDB, query);
1955 if (hstmt == NULL) {
1956 _ERR("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB));
1957 error_code = _ACCOUNT_ERROR_DB_FAILED;
1961 rc = _account_query_step(hstmt);
1962 if (rc != SQLITE_DONE) {
1963 _ERR("The record isn't found");
1964 error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1968 rc = _account_query_finalize(hstmt);
1969 if (rc != _ACCOUNT_ERROR_NONE) {
1970 _ERR("finalize error");
1975 rc = _account_end_transaction(g_hAccountDB, true);
1976 if (rc != _ACCOUNT_ERROR_NONE)
1977 _ERR("clear deleted account tables - fail [%d]", rc);
1980 _INFO("account_server_empty_deleted_account_info_tables end");
1985 int _account_query_account_by_account_id(int pid, uid_t uid, int account_db_id, account_s *account_record, bool *IsDeletedAccount)
1987 _INFO("_account_query_account_by_account_id() start, account_db_id=[%d]", account_db_id);
1989 int error_code = _ACCOUNT_ERROR_NONE;
1990 account_stmt hstmt = NULL;
1991 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1994 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1995 ACCOUNT_RETURN_VAL(account_record != NULL, {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
1996 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1998 ACCOUNT_DEBUG("starting db operations");
2000 /* prepare query for account-table */
2001 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2002 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
2004 _INFO("after _account_prepare_query, rc=[%d]", rc);
2006 hstmt = _account_prepare_query(g_hAccountDB, query);
2008 rc = _account_db_err_code(g_hAccountDB);
2009 if (rc == SQLITE_PERM) {
2010 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2011 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2014 ACCOUNT_DEBUG("before _account_query_step");
2016 rc = _account_query_step(hstmt);
2018 ACCOUNT_DEBUG("after _account_query_step returned [%d]", rc);
2020 /*querying information from deleted_account_table*/
2021 if (rc != SQLITE_ROW) {
2023 /*free previous hstmt*/
2024 if (hstmt != NULL) {
2025 rc = _account_query_finalize(hstmt);
2026 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2030 ACCOUNT_DEBUG("checking account info in deleted_account_table");
2032 /* query from deleted-account-info table */
2033 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2034 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", DELETED_ACCOUNT_TABLE, account_db_id);
2036 hstmt = _account_prepare_query(g_hAccountDB, query);
2038 rc = _account_db_err_code(g_hAccountDB);
2039 if (rc == SQLITE_PERM) {
2040 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2041 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2044 rc = _account_query_step(hstmt);
2045 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2047 ACCOUNT_DEBUG("getting account info from deleted_account_table");
2048 *IsDeletedAccount = true;
2051 while (rc == SQLITE_ROW) {
2052 ACCOUNT_DEBUG("before _account_convert_column_to_account");
2053 _account_convert_column_to_account(hstmt, account_record);
2054 ACCOUNT_DEBUG("after _account_convert_column_to_account");
2055 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]);
2056 rc = _account_query_step(hstmt);
2059 ACCOUNT_DEBUG("account_record->id=[%d]", account_record->id);
2061 rc = _account_query_finalize(hstmt);
2062 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2064 ACCOUNT_DEBUG("before _account_query_capability_by_account_id");
2065 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, account_record->id, (void*)account_record, *IsDeletedAccount);
2066 ACCOUNT_DEBUG("after _account_query_capability_by_account_id");
2068 ACCOUNT_DEBUG("before _account_query_custom_by_account_id");
2069 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, account_record->id, (void*)account_record, *IsDeletedAccount);
2070 ACCOUNT_DEBUG("after _account_query_custom_by_account_id");
2073 error_code = _ACCOUNT_ERROR_NONE;
2076 if (hstmt != NULL) {
2077 rc = _account_query_finalize(hstmt);
2078 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2083 _remove_sensitive_info_from_non_owning_account(account_record, pid, uid);
2085 pthread_mutex_unlock(&account_mutex);
2086 ACCOUNT_DEBUG("_account_query_account_by_account_id end [%d]", error_code);
2091 GList* _account_query_account_by_user_name(int pid, uid_t uid, const char *user_name, int *error_code)
2093 *error_code = _ACCOUNT_ERROR_NONE;
2094 account_stmt hstmt = NULL;
2095 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2097 account_s *account_head = NULL;
2099 if (user_name == NULL) {
2100 _ERR("USER NAME IS NULL");
2101 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
2105 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2107 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ?", ACCOUNT_TABLE);
2109 hstmt = _account_prepare_query(g_hAccountDB, query);
2111 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2112 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2113 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
2117 int binding_count = 1;
2118 _account_query_bind_text(hstmt, binding_count++, user_name);
2120 rc = _account_query_step(hstmt);
2122 if (rc != SQLITE_ROW) {
2123 _ERR("The record isn't found");
2124 *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
2130 account_head = (account_s*) malloc(sizeof(account_s));
2131 if (account_head == NULL) {
2132 ACCOUNT_FATAL("malloc Failed");
2133 if (hstmt != NULL) {
2134 rc = _account_query_finalize(hstmt);
2135 if (rc != _ACCOUNT_ERROR_NONE) {
2136 _ERR("finalize error");
2142 *error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
2145 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2147 while (rc == SQLITE_ROW) {
2148 account_s* account_record = NULL;
2150 account_record = (account_s*) malloc(sizeof(account_s));
2152 if (account_record == NULL) {
2153 ACCOUNT_FATAL("malloc Failed");
2156 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2158 _account_convert_column_to_account(hstmt, account_record);
2160 account_head->account_list = g_list_append(account_head->account_list, account_record);
2162 rc = _account_query_step(hstmt);
2166 rc = _account_query_finalize(hstmt);
2168 if (rc != _ACCOUNT_ERROR_NONE) {
2169 _ERR("finalize error");
2178 tmp = g_list_length(account_head->account_list);
2180 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2181 //account_h account;
2182 //account = (account_h)iter->data;
2184 account_s *testaccount = (account_s*)iter->data;
2186 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, testaccount->id, (void*)testaccount, false);
2187 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, testaccount->id, (void*)testaccount, false);
2190 *error_code = _ACCOUNT_ERROR_NONE;
2193 if (hstmt != NULL) {
2194 rc = _account_query_finalize(hstmt);
2195 if (rc != _ACCOUNT_ERROR_NONE) {
2196 _ERR("finalize error");
2202 pthread_mutex_unlock(&account_mutex);
2205 _remove_sensitive_info_from_non_owning_account_list(account_head->account_list, pid, uid);
2206 GList* result = account_head->account_list;
2207 _ACCOUNT_FREE(account_head);
2215 _account_query_account_by_capability(int pid, uid_t uid, const char* capability_type, const int capability_value, 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"));
2224 if ((capability_value < 0) || (capability_value >= _ACCOUNT_CAPABILITY_STATE_MAX)) {
2225 ACCOUNT_SLOGE("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
2226 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
2230 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), { *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED; }, NULL, ("The database isn't connected."));
2232 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2234 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=? AND value=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
2236 hstmt = _account_prepare_query(g_hAccountDB, query);
2238 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2239 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2240 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
2244 int binding_count = 1;
2245 _account_query_bind_text(hstmt, binding_count++, capability_type);
2246 _account_query_bind_int(hstmt, binding_count++, capability_value);
2248 rc = _account_query_step(hstmt);
2250 account_s* account_head = NULL;
2252 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2256 account_head = (account_s*) malloc(sizeof(account_s));
2257 if (account_head == NULL) {
2258 ACCOUNT_FATAL("malloc Failed");
2259 if (hstmt != NULL) {
2260 rc = _account_query_finalize(hstmt);
2261 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), { *error_code = rc; }, NULL, ("finalize error"));
2264 *error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
2267 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2269 while (rc == SQLITE_ROW) {
2270 account_s* account_record = NULL;
2272 account_record = (account_s*) malloc(sizeof(account_s));
2274 if (account_record == NULL) {
2275 ACCOUNT_FATAL("malloc Failed");
2278 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2280 _account_convert_column_to_account(hstmt, account_record);
2282 account_head->account_list = g_list_append(account_head->account_list, account_record);
2284 rc = _account_query_step(hstmt);
2288 rc = _account_query_finalize(hstmt);
2289 ACCOUNT_CATCH_ERROR_P((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2295 tmp = g_list_length(account_head->account_list);
2297 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2298 //account_h account = NULL;
2299 //account = (account_h)iter->data;
2300 account_s* testaccount = (account_s*)iter->data;
2302 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, testaccount->id, (void*)testaccount, false);
2303 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, testaccount->id, (void*)testaccount, false);
2307 *error_code = _ACCOUNT_ERROR_NONE;
2310 if (hstmt != NULL) {
2311 rc = _account_query_finalize(hstmt);
2312 if (rc != _ACCOUNT_ERROR_NONE) {
2314 _ERR("finalize error");
2319 if (*error_code != _ACCOUNT_ERROR_NONE && account_head) {
2320 _account_glist_account_free(account_head->account_list);
2321 _ACCOUNT_FREE(account_head);
2322 account_head = NULL;
2325 pthread_mutex_unlock(&account_mutex);
2328 _remove_sensitive_info_from_non_owning_account_list(account_head->account_list, pid, uid);
2329 GList* result = account_head->account_list;
2330 _ACCOUNT_FREE(account_head);
2337 GList* _account_query_account_by_capability_type(int pid, uid_t uid, const char* capability_type, int *error_code)
2339 *error_code = _ACCOUNT_ERROR_NONE;
2340 account_stmt hstmt = NULL;
2341 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2344 ACCOUNT_RETURN_VAL((capability_type != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("capability_type IS NULL"));
2345 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), { *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED; },
2346 NULL, ("The database isn't connected."));
2348 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2350 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
2352 hstmt = _account_prepare_query(g_hAccountDB, query);
2354 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2355 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2356 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
2360 int binding_count = 1;
2361 _account_query_bind_text(hstmt, binding_count++, capability_type);
2363 rc = _account_query_step(hstmt);
2365 account_s* account_head = NULL;
2367 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2371 account_head = (account_s*) malloc(sizeof(account_s));
2372 if (account_head == NULL) {
2373 ACCOUNT_FATAL("malloc Failed");
2374 if (hstmt != NULL) {
2375 rc = _account_query_finalize(hstmt);
2376 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), { *error_code = rc; }, NULL, ("finalize error"));
2379 *error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
2382 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2384 while (rc == SQLITE_ROW) {
2385 account_s* account_record = NULL;
2387 account_record = (account_s*) malloc(sizeof(account_s));
2389 if (account_record == NULL) {
2390 ACCOUNT_FATAL("malloc Failed");
2393 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2395 _account_convert_column_to_account(hstmt, account_record);
2397 account_head->account_list = g_list_append(account_head->account_list, account_record);
2399 rc = _account_query_step(hstmt);
2403 rc = _account_query_finalize(hstmt);
2404 ACCOUNT_CATCH_ERROR_P((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2410 tmp = g_list_length(account_head->account_list);
2412 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2413 account_s* testaccount = (account_s*)iter->data;
2415 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, testaccount->id, (void*)testaccount, false);
2416 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, testaccount->id, (void*)testaccount, false);
2420 *error_code = _ACCOUNT_ERROR_NONE;
2423 if (hstmt != NULL) {
2424 rc = _account_query_finalize(hstmt);
2425 if (rc != _ACCOUNT_ERROR_NONE) {
2427 _ERR("finalize error");
2432 if ((*error_code != _ACCOUNT_ERROR_NONE) && account_head) {
2433 _account_glist_account_free(account_head->account_list);
2434 _ACCOUNT_FREE(account_head);
2435 account_head = NULL;
2438 pthread_mutex_unlock(&account_mutex);
2441 _remove_sensitive_info_from_non_owning_account_list(account_head->account_list, pid, uid);
2442 GList* result = account_head->account_list;
2443 _ACCOUNT_FREE(account_head);
2450 GList* account_server_query_account_by_package_name(const char* package_name, int *error_code, int pid, uid_t uid)
2452 _INFO("account_server_query_account_by_package_name start");
2454 GList * account_list = NULL;
2455 *error_code = _ACCOUNT_ERROR_NONE;
2457 ACCOUNT_RETURN_VAL((package_name != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("PACKAGE NAME IS NULL"));
2458 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), { *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED; }, NULL, ("The database isn't connected."));
2460 account_list = _account_query_account_by_package_name(g_hAccountDB, package_name, error_code, pid, uid);
2462 _INFO("account_server_query_account_by_package_name end");
2464 return account_list;
2467 int account_server_delete_account_by_package_name(const char* package_name, bool permission, int pid, uid_t uid)
2469 _INFO("account_db_delete_account_by_package_name");
2471 int error_code = _ACCOUNT_ERROR_NONE;
2473 ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
2474 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2476 error_code = _account_delete_account_by_package_name(g_hAccountDB, package_name, permission, pid, uid);
2478 _INFO("account_server_delete_account_by_package_name end");
2483 int _account_delete(int pid, uid_t uid, int account_id)
2485 int error_code = _ACCOUNT_ERROR_NONE;
2486 account_stmt hstmt = NULL;
2487 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2489 int ret_transaction = 0;
2490 bool is_success = FALSE;
2492 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2495 /* Check requested ID to delete */
2496 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id=%d", ACCOUNT_TABLE, account_id);
2498 count = _account_get_record_count(g_hAccountDB, query);
2500 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2501 pthread_mutex_unlock(&account_mutex);
2502 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2503 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2507 ACCOUNT_ERROR("account id(%d) is not exist. count(%d)\n", account_id, count);
2508 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
2511 /* Check permission of requested appid */
2512 char* current_appid = NULL;
2513 char *package_name = NULL;
2515 current_appid = _account_get_current_appid(pid, uid);
2517 error_code = _account_get_package_name_from_account_id(account_id, &package_name);
2519 if (error_code != _ACCOUNT_ERROR_NONE) {
2520 ACCOUNT_ERROR("No package name with account_id\n");
2521 _ACCOUNT_FREE(current_appid);
2522 _ACCOUNT_FREE(package_name);
2523 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
2525 ACCOUNT_DEBUG("DELETE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name);
2527 error_code = _account_check_appid_group_with_package_name(current_appid, package_name, uid);
2529 _ACCOUNT_FREE(current_appid);
2530 _ACCOUNT_FREE(package_name);
2532 if (error_code != _ACCOUNT_ERROR_NONE) {
2533 ACCOUNT_ERROR("No permission to delete\n");
2534 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2537 /* transaction control required*/
2538 ret_transaction = _account_begin_transaction(g_hAccountDB);
2540 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2541 pthread_mutex_unlock(&account_mutex);
2542 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2543 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2546 if (ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY) {
2547 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
2548 pthread_mutex_unlock(&account_mutex);
2549 return _ACCOUNT_ERROR_DATABASE_BUSY;
2552 if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2553 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
2554 pthread_mutex_unlock(&account_mutex);
2555 return ret_transaction;
2558 /* capability table */
2559 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2560 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
2562 hstmt = _account_prepare_query(g_hAccountDB, query);
2564 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2565 pthread_mutex_unlock(&account_mutex);
2566 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2567 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2570 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2571 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2573 rc = _account_query_step(hstmt);
2574 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2576 rc = _account_query_finalize(hstmt);
2578 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2582 ACCOUNT_MEMSET(query, 0, sizeof(query));
2583 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
2585 hstmt = _account_prepare_query(g_hAccountDB, query);
2586 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2587 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2589 rc = _account_query_step(hstmt);
2590 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
2592 rc = _account_query_finalize(hstmt);
2593 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2596 /* delete custom data */
2597 ACCOUNT_MEMSET(query, 0, sizeof(query));
2598 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
2600 hstmt = _account_prepare_query(g_hAccountDB, query);
2602 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2603 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2605 rc = _account_query_step(hstmt);
2606 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
2608 rc = _account_query_finalize(hstmt);
2609 ACCOUNT_CATCH_ERROR(rc == _ACCOUNT_ERROR_NONE, {}, rc, ("finalize error", account_id, rc));
2615 if (hstmt != NULL) {
2616 rc = _account_query_finalize(hstmt);
2617 if (rc != _ACCOUNT_ERROR_NONE) {
2618 ACCOUNT_ERROR("rc (%d)", rc);
2625 ret_transaction = _account_end_transaction(g_hAccountDB, is_success);
2627 if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2628 ACCOUNT_ERROR("account_delete:_account_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
2630 if (is_success == true) {
2631 char buf[64] = {0,};
2632 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_DELETE, account_id);
2633 _account_insert_delete_update_notification_send(buf);
2637 pthread_mutex_unlock(&account_mutex);
2642 static int _account_query_account_by_username_and_package(const char* username, const char* package_name, account_s *account)
2646 int error_code = _ACCOUNT_ERROR_NONE;
2647 account_stmt hstmt = NULL;
2648 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2650 int binding_count = 1;
2652 ACCOUNT_RETURN_VAL((username != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("username IS NULL"));
2653 ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name IS NULL"));
2654 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2655 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2657 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2659 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
2660 hstmt = _account_prepare_query(g_hAccountDB, query);
2662 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2663 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2664 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2667 _account_query_bind_text(hstmt, binding_count++, username);
2668 _account_query_bind_text(hstmt, binding_count++, package_name);
2670 rc = _account_query_step(hstmt);
2671 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2673 account_s *account_record = account;
2675 while (rc == SQLITE_ROW) {
2676 _account_convert_column_to_account(hstmt, account_record);
2677 rc = _account_query_step(hstmt);
2680 rc = _account_query_finalize(hstmt);
2681 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2682 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, account_record->id, (void*)account_record, false);
2683 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, account_record->id, (void*)account_record, false);
2686 error_code = _ACCOUNT_ERROR_NONE;
2689 if (hstmt != NULL) {
2690 rc = _account_query_finalize(hstmt);
2691 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2695 pthread_mutex_unlock(&account_mutex);
2700 int _account_create(account_s **account)
2703 ACCOUNT_SLOGE("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__);
2704 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2707 account_s *data = (account_s*)malloc(sizeof(account_s));
2710 ACCOUNT_FATAL("Memory Allocation Failed");
2711 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
2713 ACCOUNT_MEMSET(data, 0, sizeof(account_s));
2715 /*Setting account as visible by default*/
2716 //data->secret = _ACCOUNT_SECRECY_VISIBLE;
2718 /*Setting account as not supporting sync by default*/
2719 //data->sync_support = _ACCOUNT_SYNC_NOT_SUPPORT;
2723 return _ACCOUNT_ERROR_NONE;
2726 int _account_destroy(account_s *account)
2728 account_s *data = account;
2730 ACCOUNT_RETURN_VAL((data != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Account handle is null!"));
2732 _account_free_account_with_items(data);
2734 return _ACCOUNT_ERROR_NONE;
2737 int _account_get_account_id(account_s* account, int *account_id)
2740 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2743 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2745 *account_id = account->id;
2747 return _ACCOUNT_ERROR_NONE;
2750 int _account_delete_from_db_by_user_name(int pid, uid_t uid, const char *user_name, const char *package_name)
2752 _INFO("[%s][%s]", user_name, package_name);
2754 int error_code = _ACCOUNT_ERROR_NONE;
2755 account_stmt hstmt = NULL;
2756 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2758 int ret_transaction = 0;
2759 bool is_success = FALSE;
2760 account_s *account = NULL;
2761 int binding_count = 1;
2762 int account_id = -1;
2764 ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is null!"));
2765 ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
2766 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2768 /* Check permission of requested appid */
2769 char* current_appid = NULL;
2770 char* package_name_temp = NULL;
2772 current_appid = _account_get_current_appid(pid, uid);
2774 package_name_temp = _account_dup_text(package_name);
2776 ACCOUNT_DEBUG("DELETE:user_name[%s],current_appid[%s], package_name[%s]", user_name, current_appid, package_name_temp);
2778 error_code = _account_check_appid_group_with_package_name(current_appid, package_name_temp, uid);
2780 _ACCOUNT_FREE(current_appid);
2781 _ACCOUNT_FREE(package_name_temp);
2783 if (error_code != _ACCOUNT_ERROR_NONE) {
2784 ACCOUNT_ERROR("No permission to delete\n");
2785 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2788 rc = _account_create(&account);
2789 rc = _account_query_account_by_username_and_package(user_name, package_name, account);
2793 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2794 _account_destroy(account);
2795 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2796 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2800 account_s* account_data = (account_s*)account;
2802 rc = _account_get_account_id(account_data, &account_id);
2804 rc = _account_destroy(account);
2806 /* transaction control required*/
2807 ret_transaction = _account_begin_transaction(g_hAccountDB);
2809 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2810 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2811 pthread_mutex_unlock(&account_mutex);
2812 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2816 if (ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY) {
2817 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
2818 pthread_mutex_unlock(&account_mutex);
2819 return _ACCOUNT_ERROR_DATABASE_BUSY;
2820 } else if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2821 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
2822 pthread_mutex_unlock(&account_mutex);
2823 return ret_transaction;
2826 /* delete custom data */
2827 ACCOUNT_MEMSET(query, 0, sizeof(query));
2828 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = ?", ACCOUNT_CUSTOM_TABLE);
2830 hstmt = _account_prepare_query(g_hAccountDB, query);
2832 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2833 _account_end_transaction(g_hAccountDB, FALSE);
2834 pthread_mutex_unlock(&account_mutex);
2835 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2836 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2839 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2840 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2842 _account_query_bind_int(hstmt, binding_count++, account_id);
2844 rc = _account_query_step(hstmt);
2845 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2847 rc = _account_query_finalize(hstmt);
2848 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2851 /* delete capability */
2852 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", CAPABILITY_TABLE);
2854 hstmt = _account_prepare_query(g_hAccountDB, query);
2856 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2857 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2860 _account_query_bind_text(hstmt, binding_count++, user_name);
2861 _account_query_bind_text(hstmt, binding_count++, package_name);
2863 rc = _account_query_step(hstmt);
2864 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2866 rc = _account_query_finalize(hstmt);
2867 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2870 ACCOUNT_MEMSET(query, 0, sizeof(query));
2873 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
2875 hstmt = _account_prepare_query(g_hAccountDB, query);
2876 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2877 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2881 _account_query_bind_text(hstmt, binding_count++, user_name);
2882 _account_query_bind_text(hstmt, binding_count++, package_name);
2884 rc = _account_query_step(hstmt);
2885 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));
2887 rc = _account_query_finalize(hstmt);
2888 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2894 if (hstmt != NULL) {
2895 rc = _account_query_finalize(hstmt);
2896 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2900 ret_transaction = _account_end_transaction(g_hAccountDB, is_success);
2902 if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2903 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
2905 if (is_success == true) {
2906 char buf[64] = {0,};
2907 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_DELETE, account_id);
2908 _account_insert_delete_update_notification_send(buf);
2912 pthread_mutex_unlock(&account_mutex);
2918 int _account_get_total_count_from_db(gboolean include_hidden, int *count)
2921 ACCOUNT_SLOGE("(%s)-(%d) count is NULL.\n", __FUNCTION__, __LINE__);
2922 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2925 if (!g_hAccountDB) {
2926 ACCOUNT_ERROR("DB is not opened\n");
2927 return _ACCOUNT_ERROR_DB_NOT_OPENED;
2930 char query[1024] = {0, };
2931 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2934 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s", ACCOUNT_TABLE);
2936 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s where secret = %d", ACCOUNT_TABLE, _ACCOUNT_SECRECY_VISIBLE);
2938 *count = _account_get_record_count(g_hAccountDB, query);
2940 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2941 pthread_mutex_unlock(&account_mutex);
2942 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2943 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2948 account_stmt pStmt = NULL;
2950 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
2951 if (SQLITE_OK != rc) {
2952 ACCOUNT_SLOGE("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg(g_hAccountDB));
2953 sqlite3_finalize(pStmt);
2954 return _ACCOUNT_ERROR_DB_FAILED;
2957 rc = sqlite3_step(pStmt);
2958 if (SQLITE_ROW != rc) {
2959 ACCOUNT_ERROR("[ERROR] sqlite3_step() failed\n");
2960 sqlite3_finalize(pStmt);
2961 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
2964 ncount = sqlite3_column_int(pStmt, 0);
2968 sqlite3_finalize(pStmt);
2971 ACCOUNT_ERROR("[ERROR] Number of account : %d, End", ncount);
2972 return _ACCOUNT_ERROR_DB_FAILED;
2975 return _ACCOUNT_ERROR_NONE;
2979 int account_server_query_app_id_exist(const char* app_id)
2981 _INFO("account_server_query_app_id_exist start app_id=[%s]", app_id);
2982 int ret = _ACCOUNT_ERROR_NONE;
2984 ret = _account_type_query_app_id_exist_from_all_db(g_hAccountDB, g_hAccountGlobalDB, app_id);
2986 _INFO("account_server_query_app_id_exist end error_code=[%d]", ret);
2991 int account_server_insert_account_type_to_user_db(account_type_s *account_type, int *account_type_id, uid_t uid)
2993 ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE HANDLE IS NULL"));
2994 ACCOUNT_RETURN_VAL((account_type->app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID OF ACCOUNT TYPE IS NULL"));
2995 ACCOUNT_RETURN_VAL((account_type_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE ID IS NULL"));
2997 _INFO("account_server_insert_account_type_to_user_db start uid=[%d]", uid);
2998 int ret = _ACCOUNT_ERROR_NONE;
3000 if (_account_type_check_duplicated(g_hAccountDB, account_type->app_id) ||
3001 _account_type_check_duplicated(g_hAccountGlobalDB, account_type->app_id)) {
3002 *account_type_id = -1;
3003 return _ACCOUNT_ERROR_DUPLICATED;
3006 ret = _account_type_insert_to_db(g_hAccountDB, account_type, account_type_id);
3007 _INFO("account_server_insert_account_type_to_user_db end error_code=[%d]", ret);
3012 int account_server_delete_account_type_by_app_id_from_user_db(const char * app_id)
3014 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID OF ACCOUNT TYPE IS NULL"));
3016 _INFO("account_server_delete_account_type_by_app_id_from_user_db start");
3017 int ret = _ACCOUNT_ERROR_NONE;
3019 ret = _account_type_delete_by_app_id(g_hAccountDB, app_id);
3020 _INFO("account_server_delete_account_type_by_app_id_from_user_db end error_code=[%d]", ret);
3025 GSList* _account_type_query_provider_feature_by_app_id_from_global_db(const char* app_id, int *error_code)
3027 _INFO("_account_type_query_provider_feature_by_app_id_in_global_db app_id=%s", app_id);
3028 account_stmt hstmt = NULL;
3029 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3030 int rc = 0, binding_count = 1;
3031 GSList* feature_list = NULL;
3033 ACCOUNT_RETURN_VAL((app_id != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("APP ID IS NULL"));
3034 ACCOUNT_RETURN_VAL((error_code != NULL), {_ERR("error_code pointer is NULL"); }, NULL, (""));
3035 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; _ERR("The database isn't connected."); }, NULL, ("The database isn't connected."));
3037 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3039 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
3040 _INFO("account query=[%s]", query);
3042 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3044 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3045 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3046 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3050 _INFO("before _account_query_bind_text");
3051 _account_query_bind_text(hstmt, binding_count++, app_id);
3053 rc = _account_query_step(hstmt);
3055 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"));
3057 provider_feature_s* feature_record = NULL;
3059 while (rc == SQLITE_ROW) {
3061 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
3063 if (feature_record == NULL) {
3064 ACCOUNT_FATAL("malloc Failed");
3068 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
3070 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
3072 _INFO("Adding account feature_list");
3073 feature_list = g_slist_append(feature_list, feature_record);
3075 rc = _account_query_step(hstmt);
3078 *error_code = _ACCOUNT_ERROR_NONE;
3081 if (hstmt != NULL) {
3082 rc = _account_query_finalize(hstmt);
3083 if (rc != _ACCOUNT_ERROR_NONE) {
3085 _ERR("global db fianlize error");
3089 if (*error_code != _ACCOUNT_ERROR_NONE)
3090 _account_type_gslist_feature_free(feature_list);
3092 _INFO("Returning account feature_list from global db");
3094 return feature_list;
3097 GSList* _account_type_query_provider_feature_by_app_id(const char* app_id, int *error_code)
3099 _INFO("_account_type_query_provider_feature_by_app_id app_id=%s", app_id);
3100 account_stmt hstmt = NULL;
3101 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3102 int rc = 0, binding_count = 1;
3103 GSList* feature_list = NULL;
3105 ACCOUNT_RETURN_VAL((app_id != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("APP ID IS NULL"));
3106 ACCOUNT_RETURN_VAL((error_code != NULL), {_ERR("error_code pointer is NULL"); }, NULL, (""));
3107 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), { *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED; }, NULL, ("The database isn't connected."));
3109 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3111 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
3112 _INFO("account query=[%s]", query);
3114 hstmt = _account_prepare_query(g_hAccountDB, query);
3116 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
3117 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3118 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3122 _account_query_bind_text(hstmt, binding_count++, app_id);
3124 rc = _account_query_step(hstmt);
3126 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, { *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
3127 _ERR("The record isn't found from user db. rc=[%d]", rc); },
3128 _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3130 provider_feature_s* feature_record = NULL;
3132 while (rc == SQLITE_ROW) {
3134 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
3136 if (feature_record == NULL) {
3137 ACCOUNT_FATAL("malloc Failed");
3141 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
3143 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
3145 _INFO("Adding account feature_list");
3146 feature_list = g_slist_append(feature_list, feature_record);
3148 rc = _account_query_step(hstmt);
3151 *error_code = _ACCOUNT_ERROR_NONE;
3153 rc = _account_query_finalize(hstmt);
3154 ACCOUNT_CATCH_ERROR_P((rc == _ACCOUNT_ERROR_NONE), { *error_code = rc; }, rc, ("account finalize error"));
3158 if (hstmt != NULL) {
3159 rc = _account_query_finalize(hstmt);
3160 if (rc != _ACCOUNT_ERROR_NONE) {
3162 _ERR("account fianlize error");
3166 _INFO("*error_code=[%d]", *error_code);
3168 if (*error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3169 feature_list = _account_type_query_provider_feature_by_app_id_from_global_db(app_id, error_code);
3171 if (*error_code != _ACCOUNT_ERROR_NONE) {
3172 _account_type_gslist_feature_free(feature_list);
3176 _INFO("Returning account feature_list");
3178 return feature_list;
3181 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)
3183 int error_code = _ACCOUNT_ERROR_NONE;
3184 account_stmt hstmt = NULL;
3185 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3186 int rc = 0, binding_count = 1;
3188 _INFO("_account_type_query_provider_feature_cb_by_app_id_in_global_db start app_id=%s", app_id);
3189 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3190 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3191 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3193 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3195 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
3196 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3198 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3199 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3200 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_PERMISSION_DENIED, ("global db permission denied.\n"));
3203 _account_query_bind_text(hstmt, binding_count++, app_id);
3205 rc = _account_query_step(hstmt);
3206 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"));
3208 provider_feature_s* feature_record = NULL;
3210 while (rc == SQLITE_ROW) {
3211 bool cb_ret = FALSE;
3212 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
3214 if (feature_record == NULL) {
3215 ACCOUNT_FATAL("malloc Failed");
3219 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
3221 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
3223 cb_ret = callback(feature_record->app_id, feature_record->key, user_data);
3225 _account_type_free_feature_with_items(feature_record);
3227 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returns FALSE, its iteration is stopped!!!!\n"));
3229 rc = _account_query_step(hstmt);
3232 error_code = _ACCOUNT_ERROR_NONE;
3235 if (hstmt != NULL) {
3236 rc = _account_query_finalize(hstmt);
3237 if (rc != _ACCOUNT_ERROR_NONE) {
3239 _ERR("global db finalize error[%d]", rc);
3244 _INFO("_account_type_query_provider_feature_cb_by_app_id_in_global_db end. error_code=[%d]", error_code);
3249 int _account_type_query_provider_feature_cb_by_app_id(account_type_provider_feature_cb callback, const char* app_id, void *user_data)
3251 int error_code = _ACCOUNT_ERROR_NONE;
3252 account_stmt hstmt = NULL;
3253 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3254 int rc = 0, binding_count = 1;
3256 _INFO("_account_type_query_provider_feature_cb_by_app_id start app_id=%s", app_id);
3257 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3258 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3259 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3261 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3263 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
3264 hstmt = _account_prepare_query(g_hAccountDB, query);
3266 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
3267 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3268 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3271 _account_query_bind_text(hstmt, binding_count++, app_id);
3273 rc = _account_query_step(hstmt);
3274 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found in user db.\n"));
3276 provider_feature_s* feature_record = NULL;
3278 while (rc == SQLITE_ROW) {
3279 bool cb_ret = FALSE;
3280 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
3282 if (feature_record == NULL) {
3283 ACCOUNT_FATAL("malloc Failed");
3287 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
3289 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
3291 cb_ret = callback(feature_record->app_id, feature_record->key, user_data);
3293 _account_type_free_feature_with_items(feature_record);
3295 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
3297 rc = _account_query_step(hstmt);
3300 rc = _account_query_finalize(hstmt);
3301 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3304 error_code = _ACCOUNT_ERROR_NONE;
3307 if (hstmt != NULL) {
3308 rc = _account_query_finalize(hstmt);
3309 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3313 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3314 error_code = _account_type_query_provider_feature_cb_by_app_id_from_global_db(callback, app_id, user_data);
3316 _INFO("_account_type_query_provider_feature_cb_by_app_id end");
3321 int account_type_query_provider_feature_cb_by_app_id(account_type_provider_feature_cb callback, const char* app_id, void *user_data)
3323 int error_code = _ACCOUNT_ERROR_NONE;
3325 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3326 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3327 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3329 error_code = _account_type_query_provider_feature_cb_by_app_id(callback, app_id, user_data);
3331 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3332 error_code = _account_type_query_provider_feature_cb_by_app_id_from_global_db(callback, app_id, user_data);
3337 bool _account_type_query_supported_feature_from_global_db(const char* app_id, const char* capability, int *error_code)
3339 _INFO("_account_type_query_supported_feature_in_global_db start");
3340 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3342 *error_code = _ACCOUNT_ERROR_NONE;
3344 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3345 int record_count = 0;
3347 if (app_id == NULL || capability == NULL) {
3348 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
3352 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
3354 record_count = _account_get_record_count(g_hAccountGlobalDB, query);
3356 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3357 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3358 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3361 if (record_count <= 0) {
3362 *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
3366 _INFO("_account_type_query_supported_feature_in_global_db end");
3371 bool _account_type_query_supported_feature(const char* app_id, const char* capability, int *error_code)
3373 _INFO("_account_type_query_supported_feature start");
3375 *error_code = _ACCOUNT_ERROR_NONE;
3377 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3378 int record_count = 0;
3380 if (app_id == NULL || capability == NULL) {
3381 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
3385 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
3387 record_count = _account_get_record_count(g_hAccountDB, query);
3389 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
3390 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3391 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3395 if (record_count <= 0) {
3396 bool is_exist = false;
3397 is_exist = _account_type_query_supported_feature_from_global_db(app_id, capability, error_code);
3402 _INFO("_account_type_query_supported_feature end");
3408 static int _account_type_update_provider_feature(sqlite3 *account_db_handle, account_type_s *account_type, const char* app_id)
3411 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3412 account_stmt hstmt = NULL;
3414 ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
3416 if (g_slist_length(account_type->provider_feature_list) == 0) {
3417 ACCOUNT_ERROR("no feature\n");
3418 return _ACCOUNT_ERROR_NONE;
3421 ACCOUNT_DEBUG("app id", app_id);
3423 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3425 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id=? ", PROVIDER_FEATURE_TABLE);
3426 hstmt = _account_prepare_query(account_db_handle, query);
3428 if (_account_db_err_code(account_db_handle) == SQLITE_PERM) {
3429 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(account_db_handle));
3430 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3434 _account_query_bind_text(hstmt, count++, app_id);
3435 rc = _account_query_step(hstmt);
3437 if (rc != SQLITE_DONE) {
3438 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3439 return _ACCOUNT_ERROR_DB_FAILED;
3441 rc = _account_query_finalize(hstmt);
3442 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3447 for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
3450 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3451 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
3452 "(?, ?) ", PROVIDER_FEATURE_TABLE);
3454 hstmt = _account_prepare_query(account_db_handle, query);
3456 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg(account_db_handle)));
3458 provider_feature_s* feature_data = NULL;
3459 feature_data = (provider_feature_s*)iter->data;
3461 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
3462 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3463 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
3464 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3466 rc = _account_query_step(hstmt);
3468 if (rc != SQLITE_DONE) {
3469 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3472 rc = _account_query_finalize(hstmt);
3473 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3477 return _ACCOUNT_ERROR_NONE;
3480 static int _account_type_update_label(sqlite3 *account_db_handle, account_type_s *account_type, const char* app_id)
3483 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3484 account_stmt hstmt = NULL;
3486 ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
3488 if (g_slist_length(account_type->label_list) == 0)
3489 return _ACCOUNT_ERROR_NONE;
3491 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3493 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId=? ", LABEL_TABLE);
3494 hstmt = _account_prepare_query(account_db_handle, query);
3496 if (_account_db_err_code(account_db_handle) == SQLITE_PERM) {
3497 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(account_db_handle));
3498 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3502 _account_query_bind_text(hstmt, count++, app_id);
3503 rc = _account_query_step(hstmt);
3505 if (rc != SQLITE_DONE) {
3506 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3507 return _ACCOUNT_ERROR_DB_FAILED;
3509 rc = _account_query_finalize(hstmt);
3510 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3515 for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
3518 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3519 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
3520 "(?, ?, ?) ", LABEL_TABLE);
3522 hstmt = _account_prepare_query(account_db_handle, query);
3524 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg(account_db_handle)));
3526 label_s* label_data = NULL;
3527 label_data = (label_s*)iter->data;
3529 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
3530 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3531 ret = _account_query_bind_text(hstmt, count++, label_data->label);
3532 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3533 ret = _account_query_bind_text(hstmt, count++, label_data->locale);
3534 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3536 rc = _account_query_step(hstmt);
3538 if (rc != SQLITE_DONE) {
3539 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3542 rc = _account_query_finalize(hstmt);
3543 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3547 return _ACCOUNT_ERROR_NONE;
3551 static int _account_type_update_account(sqlite3 *account_db_handle, account_type_s *account_type, const char* app_id)
3553 int rc = 0, binding_count = 1;
3554 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3555 int error_code = _ACCOUNT_ERROR_NONE;
3556 account_stmt hstmt = NULL;
3558 if (!account_type->app_id) {
3559 ACCOUNT_ERROR("app id is mandetory field, it can not be NULL!!!!\n");
3560 return _ACCOUNT_ERROR_INVALID_PARAMETER;
3563 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3564 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET AppId=?, ServiceProviderId=?, IconPath=?, "
3565 "SmallIconPath=?, MultipleAccountSupport=? WHERE AppId=? ", ACCOUNT_TYPE_TABLE);
3567 hstmt = _account_prepare_query(account_db_handle, query);
3569 if (_account_db_err_code(account_db_handle) == SQLITE_PERM) {
3570 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(account_db_handle));
3571 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3572 } else if (_account_db_err_code(account_db_handle) == SQLITE_BUSY) {
3573 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(account_db_handle));
3574 return _ACCOUNT_ERROR_DATABASE_BUSY;
3577 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg(account_db_handle)));
3579 binding_count = _account_type_convert_account_to_sql(account_type, hstmt, query);
3580 _account_query_bind_text(hstmt, binding_count++, app_id);
3582 rc = _account_query_step(hstmt);
3583 if (rc != SQLITE_DONE)
3584 ACCOUNT_ERROR("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3586 rc = _account_query_finalize(hstmt);
3587 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3591 error_code = _account_type_update_label(account_db_handle, account_type, app_id);
3592 /* update provider feature */
3593 error_code = _account_type_update_provider_feature(account_db_handle, account_type, app_id);
3598 int _account_type_update_to_db_by_app_id(account_type_s* account_type, const char* app_id)
3600 int error_code = _ACCOUNT_ERROR_NONE;
3602 ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
3603 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3604 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3606 account_type_s* data = account_type;
3608 pthread_mutex_lock(&account_mutex);
3610 error_code = _account_type_update_account(g_hAccountDB, data, app_id);
3612 pthread_mutex_unlock(&account_mutex);
3617 GSList* _account_type_get_label_list_by_app_id_from_global_db(const char* app_id, int *error_code)
3619 *error_code = _ACCOUNT_ERROR_NONE;
3620 account_stmt hstmt = NULL;
3621 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3622 int rc = 0, binding_count = 1;
3623 GSList* label_list = NULL;
3625 ACCOUNT_RETURN_VAL((app_id != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("APP ID IS NULL"));
3626 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3628 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3630 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3631 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3633 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3634 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3635 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3640 _account_query_bind_text(hstmt, binding_count++, app_id);
3642 rc = _account_query_step(hstmt);
3643 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"));
3645 label_s* label_record = NULL;
3647 while (rc == SQLITE_ROW) {
3648 label_record = (label_s*) malloc(sizeof(label_s));
3650 if (label_record == NULL) {
3651 ACCOUNT_FATAL("malloc Failed");
3655 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3657 _account_type_convert_column_to_label(hstmt, label_record);
3659 _INFO("Adding account label_list");
3660 label_list = g_slist_append(label_list, label_record);
3662 rc = _account_query_step(hstmt);
3665 *error_code = _ACCOUNT_ERROR_NONE;
3668 if (hstmt != NULL) {
3669 rc = _account_query_finalize(hstmt);
3670 if (rc != _ACCOUNT_ERROR_NONE)
3671 _ERR("global db finalize error[%d]", rc);
3675 _INFO("Returning account global label_list");
3680 GSList* _account_type_get_label_list_by_app_id(const char* app_id, int *error_code)
3682 *error_code = _ACCOUNT_ERROR_NONE;
3683 account_stmt hstmt = NULL;
3684 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3685 int rc = 0, binding_count = 1;
3686 GSList* label_list = NULL;
3688 ACCOUNT_RETURN_VAL((app_id != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("APP ID IS NULL"));
3689 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), { *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED; }, NULL, ("The database isn't connected."));
3691 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3693 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3694 hstmt = _account_prepare_query(g_hAccountDB, query);
3696 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
3697 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3698 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3702 _account_query_bind_text(hstmt, binding_count++, app_id);
3704 rc = _account_query_step(hstmt);
3705 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3707 label_s* label_record = NULL;
3709 while (rc == SQLITE_ROW) {
3710 label_record = (label_s*) malloc(sizeof(label_s));
3712 if (label_record == NULL) {
3713 ACCOUNT_FATAL("malloc Failed");
3717 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3719 _account_type_convert_column_to_label(hstmt, label_record);
3721 _INFO("Adding account label_list");
3722 label_list = g_slist_append(label_list, label_record);
3724 rc = _account_query_step(hstmt);
3727 rc = _account_query_finalize(hstmt);
3728 if (rc != _ACCOUNT_ERROR_NONE) {
3729 _account_type_gslist_label_free(label_list);
3731 _ERR("finalize error");
3736 *error_code = _ACCOUNT_ERROR_NONE;
3739 if (hstmt != NULL) {
3740 rc = _account_query_finalize(hstmt);
3741 if (rc != _ACCOUNT_ERROR_NONE) {
3742 _account_type_gslist_label_free(label_list);
3744 _ERR("finalize error");
3750 if (*error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3751 label_list = _account_type_get_label_list_by_app_id_from_global_db(app_id, error_code);
3753 _INFO("Returning account label_list");
3758 int _account_type_query_label_by_app_id_from_global_db(account_type_label_cb callback, const char* app_id, void *user_data)
3760 int error_code = _ACCOUNT_ERROR_NONE;
3761 account_stmt hstmt = NULL;
3762 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3763 int rc = 0, binding_count = 1;
3765 _INFO("account_type_query_label_by_app_id_from_global_db start");
3767 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3768 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3769 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3771 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3773 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3774 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3776 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3777 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3778 error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3782 _account_query_bind_text(hstmt, binding_count++, app_id);
3784 rc = _account_query_step(hstmt);
3785 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3787 label_s* label_record = NULL;
3789 while (rc == SQLITE_ROW) {
3790 bool cb_ret = FALSE;
3791 label_record = (label_s*) malloc(sizeof(label_s));
3793 if (label_record == NULL) {
3794 ACCOUNT_FATAL("malloc Failed");
3798 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3800 _account_type_convert_column_to_label(hstmt, label_record);
3802 cb_ret = callback(label_record->app_id, label_record->label , label_record->locale, user_data);
3804 _account_type_free_label_with_items(label_record);
3806 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
3808 rc = _account_query_step(hstmt);
3811 error_code = _ACCOUNT_ERROR_NONE;
3814 if (hstmt != NULL) {
3815 rc = _account_query_finalize(hstmt);
3816 if (rc != _ACCOUNT_ERROR_NONE)
3817 _ERR("global db finalize error[%d]", rc);
3821 _INFO("account_type_query_label_by_app_id_from_global_db end [%d]", error_code);
3826 int _account_type_query_label_by_app_id(account_type_label_cb callback, const char* app_id, void *user_data)
3828 int error_code = _ACCOUNT_ERROR_NONE;
3829 account_stmt hstmt = NULL;
3830 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3831 int rc = 0, binding_count = 1;
3833 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3834 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3835 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3837 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3839 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3840 hstmt = _account_prepare_query(g_hAccountDB, query);
3842 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
3843 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3844 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3847 _account_query_bind_text(hstmt, binding_count++, app_id);
3849 rc = _account_query_step(hstmt);
3850 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3852 label_s* label_record = NULL;
3854 while (rc == SQLITE_ROW) {
3855 bool cb_ret = FALSE;
3856 label_record = (label_s*) malloc(sizeof(label_s));
3858 if (label_record == NULL) {
3859 ACCOUNT_FATAL("malloc Failed");
3863 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3865 _account_type_convert_column_to_label(hstmt, label_record);
3867 cb_ret = callback(label_record->app_id, label_record->label , label_record->locale, user_data);
3869 _account_type_free_label_with_items(label_record);
3871 //ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
3872 if (cb_ret != TRUE) {
3873 _INFO("Callback func returs FALSE, its iteration is stopped!!!!\n");
3877 rc = _account_query_step(hstmt);
3880 rc = _account_query_finalize(hstmt);
3881 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3884 error_code = _ACCOUNT_ERROR_NONE;
3887 if (hstmt != NULL) {
3888 rc = _account_query_finalize(hstmt);
3889 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3893 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
3894 error_code = account_type_query_label_by_app_id_from_global_db(callback, app_id, user_data);
3900 int account_type_query_label_by_app_id(account_type_label_cb callback, const char* app_id, void *user_data)
3902 int error_code = _ACCOUNT_ERROR_NONE;
3904 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3905 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3906 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3908 error_code = _account_type_query_label_by_app_id(callback, app_id, user_data);
3910 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3911 error_code = _account_type_query_label_by_app_id_from_global_db(callback, app_id, user_data);
3916 bool _account_get_label_text_cb(char* app_id, char* label, char* locale, void *user_data)
3918 account_type_s *data = (account_type_s*)user_data;
3920 label_s *label_data = (label_s*)malloc(sizeof(label_s));
3922 if (label_data == NULL) {
3923 ACCOUNT_FATAL("_account_get_label_text_cb : MALLOC FAIL\n");
3926 ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
3928 label_data->app_id = _account_dup_text(app_id);
3929 label_data->label = _account_dup_text(label);
3930 label_data->locale = _account_dup_text(locale);
3932 data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
3937 bool _account_get_provider_feature_cb(char* app_id, char* key, void* user_data)
3939 account_type_s *data = (account_type_s*)user_data;
3941 provider_feature_s *feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
3943 if (feature_data == NULL) {
3944 ACCOUNT_FATAL("_account_get_provider_feature_cb : MALLOC FAIL\n");
3947 ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
3949 feature_data->app_id = _account_dup_text(app_id);
3950 feature_data->key = _account_dup_text(key);
3952 data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
3957 int _account_type_query_by_app_id_from_global_db(const char* app_id, account_type_s** account_type_record)
3959 _INFO("_account_type_query_by_app_id_from_global_db start");
3961 int error_code = _ACCOUNT_ERROR_NONE;
3962 account_stmt hstmt = NULL;
3963 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3964 int rc = 0, binding_count = 1;
3966 ACCOUNT_RETURN_VAL((app_id != 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3967 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3968 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3970 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3972 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
3973 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3975 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3976 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3977 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3980 _account_query_bind_text(hstmt, binding_count++, app_id);
3982 rc = _account_query_step(hstmt);
3983 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3985 *account_type_record = create_empty_account_type_instance();
3987 while (rc == SQLITE_ROW) {
3988 _account_type_convert_column_to_account_type(hstmt, *account_type_record);
3989 rc = _account_query_step(hstmt);
3992 rc = _account_query_finalize(hstmt);
3993 ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {_ERR("global db finalize error rc=[%d]", rc); }, rc, ("finalize error"));
3994 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb, app_id, (void*)(*account_type_record));
3995 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, app_id, (void*)(*account_type_record));
3998 error_code = _ACCOUNT_ERROR_NONE;
4001 if (hstmt != NULL) {
4002 rc = _account_query_finalize(hstmt);
4003 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4007 _INFO("_account_type_query_by_app_id_from_global_db end [%d]", error_code);
4012 int _account_type_query_by_app_id(const char* app_id, account_type_s** account_type_record)
4014 _INFO("_account_type_query_by_app_id start");
4016 int error_code = _ACCOUNT_ERROR_NONE;
4017 account_stmt hstmt = NULL;
4018 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4019 int rc = 0, binding_count = 1;
4021 ACCOUNT_RETURN_VAL((app_id != 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4022 ACCOUNT_RETURN_VAL((account_type_record != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("account type record(account_type_s**) is NULL"));
4023 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4025 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4027 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
4028 hstmt = _account_prepare_query(g_hAccountDB, query);
4030 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4031 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4032 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4035 _account_query_bind_text(hstmt, binding_count++, app_id);
4037 rc = _account_query_step(hstmt);
4038 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4040 *account_type_record = create_empty_account_type_instance();
4041 if (*account_type_record == NULL) {
4042 _ERR("Out of Memory");
4043 error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
4047 while (rc == SQLITE_ROW) {
4048 _account_type_convert_column_to_account_type(hstmt, *account_type_record);
4049 rc = _account_query_step(hstmt);
4052 rc = _account_query_finalize(hstmt);
4053 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4054 _account_type_query_label_by_app_id(_account_get_label_text_cb, app_id, (void*)(*account_type_record));
4055 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, app_id, (void*)(*account_type_record));
4058 error_code = _ACCOUNT_ERROR_NONE;
4061 if (hstmt != NULL) {
4062 rc = _account_query_finalize(hstmt);
4063 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4067 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
4068 error_code = _account_type_query_by_app_id_from_global_db(app_id, account_type_record);
4070 _INFO("_account_type_query_by_app_id end [%d]", error_code);
4075 int _account_type_query_by_provider_feature_from_global_db(const char* key, GSList **account_type_list_all)
4077 int error_code = _ACCOUNT_ERROR_NONE;
4078 account_stmt hstmt = NULL;
4079 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4081 GSList *account_type_list = NULL;
4083 _INFO("_account_type_query_by_provider_feature_from_global_db start key=%s", key);
4085 ACCOUNT_ERROR("capability_type IS NULL.");
4086 error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
4090 if (g_hAccountGlobalDB == NULL) {
4091 ACCOUNT_ERROR("The database isn't connected.");
4092 error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;
4096 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4098 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
4100 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
4102 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
4103 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
4104 error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
4108 int binding_count = 1;
4109 _account_query_bind_text(hstmt, binding_count++, key);
4111 rc = _account_query_step(hstmt);
4113 account_type_s *account_type_record = NULL;
4115 if (rc != SQLITE_ROW) {
4116 ACCOUNT_ERROR("The record isn't found. rc=[%d]", rc);
4117 error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4121 while (rc == SQLITE_ROW) {
4122 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4124 if (account_type_record == NULL) {
4125 ACCOUNT_FATAL("malloc Failed");
4129 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4130 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4131 account_type_list = g_slist_append(account_type_list, account_type_record);
4132 rc = _account_query_step(hstmt);
4135 rc = _account_query_finalize(hstmt);
4136 if (rc != _ACCOUNT_ERROR_NONE) {
4137 _account_type_gslist_account_type_free(account_type_list);
4138 ACCOUNT_ERROR("finalize error(%s)", rc);
4146 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4147 account_type_s *account_type = NULL;
4148 account_type = (account_type_s*)iter->data;
4149 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb, account_type->app_id, (void*)account_type);
4150 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, account_type->app_id, (void*)account_type);
4151 _INFO("add label & provider_feature");
4154 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4156 account_type_s *account_type = NULL;
4157 account_type = (account_type_s*)iter->data;
4158 *account_type_list_all = g_slist_append(*account_type_list_all, account_type);
4159 _INFO("add account_type");
4161 g_slist_free(account_type_list);
4163 error_code = _ACCOUNT_ERROR_NONE;
4166 if (hstmt != NULL) {
4167 rc = _account_query_finalize(hstmt);
4168 if (rc != _ACCOUNT_ERROR_NONE) {
4169 ACCOUNT_ERROR("finalize error(%s)", rc);
4175 _INFO("_account_type_query_by_provider_feature_from_global_db end. error_code=[%d]", error_code);
4180 GSList* _account_type_query_by_provider_feature(const char* key, int *error_code)
4182 *error_code = _ACCOUNT_ERROR_NONE;
4183 account_stmt hstmt = NULL;
4184 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4186 GSList *account_type_list = NULL;
4188 _INFO("account_type_query_by_provider_feature start key=%s", key);
4190 ACCOUNT_ERROR("capability_type IS NULL.");
4191 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
4195 if (g_hAccountDB == NULL) {
4196 ACCOUNT_ERROR("The database isn't connected.");
4197 *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;
4201 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4203 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
4205 hstmt = _account_prepare_query(g_hAccountDB, query);
4207 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4208 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4209 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
4213 int binding_count = 1;
4214 _account_query_bind_text(hstmt, binding_count++, key);
4216 rc = _account_query_step(hstmt);
4218 account_type_s *account_type_record = NULL;
4220 if (rc != SQLITE_ROW) {
4221 ACCOUNT_ERROR("The record isn't found. rc=[%d]", rc);
4222 *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4226 while (rc == SQLITE_ROW) {
4227 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4229 if (account_type_record == NULL) {
4230 ACCOUNT_FATAL("malloc Failed");
4234 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4235 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4236 account_type_list = g_slist_append(account_type_list, account_type_record);
4237 rc = _account_query_step(hstmt);
4240 rc = _account_query_finalize(hstmt);
4241 if (rc != _ACCOUNT_ERROR_NONE) {
4242 _account_type_gslist_account_type_free(account_type_list);
4243 ACCOUNT_ERROR("finalize error(%s)", rc);
4251 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4252 account_type_s *account_type = NULL;
4253 account_type = (account_type_s*)iter->data;
4254 _account_type_query_label_by_app_id(_account_get_label_text_cb, account_type->app_id, (void*)account_type);
4255 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, account_type->app_id, (void*)account_type);
4258 *error_code = _ACCOUNT_ERROR_NONE;
4261 if (hstmt != NULL) {
4262 rc = _account_query_finalize(hstmt);
4263 if (rc != _ACCOUNT_ERROR_NONE) {
4270 if (*error_code == _ACCOUNT_ERROR_NONE || *error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4271 rc = _account_type_query_by_provider_feature_from_global_db(key, &account_type_list);
4272 if (rc != _ACCOUNT_ERROR_NONE && rc != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4273 ACCOUNT_ERROR("_account_type_query_by_provider_feature_from_global_db fail=[%d]", rc);
4274 _account_type_gslist_account_type_free(account_type_list);
4277 if (rc == _ACCOUNT_ERROR_NONE)
4281 _INFO("account_type_query_by_provider_feature end");
4283 return account_type_list;
4286 int _account_type_query_all_from_global_db(GSList **account_type_list_all)
4288 account_stmt hstmt = NULL;
4289 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4290 int rc = _ACCOUNT_ERROR_NONE;
4291 int error_code = _ACCOUNT_ERROR_NONE;
4292 GSList *account_type_list = NULL;
4294 _INFO("_account_type_query_all_in_global_db start");
4295 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, NULL, ("The database isn't connected."));
4297 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4299 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
4300 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
4302 rc = _account_query_step(hstmt);
4304 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
4305 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
4306 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4309 account_type_s *account_type_record = NULL;
4311 if (rc != SQLITE_ROW) {
4312 _INFO("[_ACCOUNT_ERROR_RECORD_NOT_FOUND]The record isn't found.");
4313 error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4317 while (rc == SQLITE_ROW) {
4318 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4320 if (account_type_record == NULL) {
4321 ACCOUNT_FATAL("malloc Failed");
4325 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4326 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4327 account_type_list = g_slist_append(account_type_list, account_type_record);
4328 rc = _account_query_step(hstmt);
4331 rc = _account_query_finalize(hstmt);
4332 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list); }, rc, ("finalize error"));
4337 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4338 account_type_s *account_type = NULL;
4339 account_type = (account_type_s*)iter->data;
4340 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb, account_type->app_id, (void*)account_type);
4341 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, account_type->app_id, (void*)account_type);
4344 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4345 account_type_s *account_type = NULL;
4346 account_type = (account_type_s*)iter->data;
4347 *account_type_list_all = g_slist_append(*account_type_list_all, account_type);
4349 g_slist_free(account_type_list);
4351 error_code = _ACCOUNT_ERROR_NONE;
4354 if (hstmt != NULL) {
4355 rc = _account_query_finalize(hstmt);
4356 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list); }, rc, ("finalize error"));
4360 _INFO("_account_type_query_all_in_global_db end");
4365 GSList* _account_type_query_all(void)
4367 account_stmt hstmt = NULL;
4368 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4370 int error_code = _ACCOUNT_ERROR_NONE;
4371 GSList *account_type_list = NULL;
4373 _INFO("_account_type_query_all start");
4374 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected."));
4376 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4378 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
4379 hstmt = _account_prepare_query(g_hAccountDB, query);
4381 rc = _account_query_step(hstmt);
4383 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4384 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4388 account_type_s *account_type_record = NULL;
4390 if (rc != SQLITE_ROW) {
4391 _INFO("[_ACCOUNT_ERROR_RECORD_NOT_FOUND]The record isn't found.");
4392 error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4396 while (rc == SQLITE_ROW) {
4397 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4399 if (account_type_record == NULL) {
4400 ACCOUNT_FATAL("malloc Failed");
4404 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4405 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4406 account_type_list = g_slist_append(account_type_list, account_type_record);
4407 rc = _account_query_step(hstmt);
4410 rc = _account_query_finalize(hstmt);
4411 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list); }, NULL, ("finalize error"));
4416 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4417 account_type_s *account_type = NULL;
4418 account_type = (account_type_s*)iter->data;
4419 _account_type_query_label_by_app_id(_account_get_label_text_cb, account_type->app_id, (void*)account_type);
4420 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, account_type->app_id, (void*)account_type);
4423 error_code = _ACCOUNT_ERROR_NONE;
4426 if (hstmt != NULL) {
4427 rc = _account_query_finalize(hstmt);
4428 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list); }, NULL, ("finalize error"));
4432 if (error_code == _ACCOUNT_ERROR_NONE || error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4433 error_code = _account_type_query_all_from_global_db(&account_type_list);
4434 if (rc != _ACCOUNT_ERROR_NONE && rc != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4435 ACCOUNT_ERROR("_account_type_query_all_from_global_db fail=[%d]", rc);
4436 _account_type_gslist_account_type_free(account_type_list);
4441 _INFO("_account_type_query_all end");
4443 return account_type_list;
4446 // output parameter label must be free
4447 int _account_type_query_label_by_locale_from_global_db(const char* app_id, const char* locale, char **label)
4449 int error_code = _ACCOUNT_ERROR_NONE;
4450 account_stmt hstmt = NULL;
4451 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4452 int rc = 0, binding_count = 1;
4453 char* converted_locale = NULL;
4455 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
4456 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4457 ACCOUNT_RETURN_VAL((label != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
4458 ACCOUNT_RETURN_VAL((locale != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("locale char is null"));
4459 //Making label newly created
4461 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4463 converted_locale = _account_dup_text(locale);
4464 gchar** tokens = g_strsplit(converted_locale, "-", 2);
4466 if (tokens != NULL) {
4467 if ((char*)(tokens[1]) != NULL) {
4468 char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1]));
4469 if (upper_token != NULL) {
4470 _ACCOUNT_FREE(converted_locale);
4471 converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token);
4473 _ACCOUNT_FREE(upper_token);
4478 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, converted_locale);
4479 _ACCOUNT_FREE(converted_locale);
4481 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
4483 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
4484 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
4485 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4488 _account_query_bind_text(hstmt, binding_count++, app_id);
4490 rc = _account_query_step(hstmt);
4491 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4493 label_s* label_record = NULL;
4495 while (rc == SQLITE_ROW) {
4496 label_record = (label_s*) malloc(sizeof(label_s));
4498 if (label_record == NULL) {
4499 ACCOUNT_FATAL("malloc Failed");
4503 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
4505 _account_type_convert_column_to_label(hstmt, label_record);
4507 _ACCOUNT_FREE(*label);
4508 //Making label newly created
4509 *label = _account_dup_text(label_record->label);
4511 _account_type_free_label_with_items(label_record);
4513 rc = _account_query_step(hstmt);
4516 rc = _account_query_finalize(hstmt);
4517 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4520 error_code = _ACCOUNT_ERROR_NONE;
4523 if (hstmt != NULL) {
4524 rc = _account_query_finalize(hstmt);
4525 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4529 _INFO("_account_type_query_label_by_locale_from_global_db() end : error_code = %d", error_code);
4534 // output parameter label must be free
4535 int _account_type_query_label_by_locale(const char* app_id, const char* locale, char **label)
4537 int error_code = _ACCOUNT_ERROR_NONE;
4538 account_stmt hstmt = NULL;
4539 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4540 int rc = 0, binding_count = 1;
4541 char* converted_locale = NULL;
4543 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
4544 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4545 ACCOUNT_RETURN_VAL((label != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
4546 ACCOUNT_RETURN_VAL((locale != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("locale char is null"));
4547 //Making label newly created
4549 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4551 converted_locale = _account_dup_text(locale);
4552 gchar** tokens = g_strsplit(converted_locale, "-", 2);
4554 if (tokens != NULL) {
4555 if ((char*)(tokens[1]) != NULL) {
4556 char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1]));
4557 if (upper_token != NULL) {
4558 _ACCOUNT_FREE(converted_locale);
4559 converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token);
4561 _ACCOUNT_FREE(upper_token);
4566 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, converted_locale);
4567 _ACCOUNT_FREE(converted_locale);
4569 hstmt = _account_prepare_query(g_hAccountDB, query);
4571 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4572 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4573 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4576 _account_query_bind_text(hstmt, binding_count++, app_id);
4578 rc = _account_query_step(hstmt);
4579 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4581 label_s* label_record = NULL;
4583 while (rc == SQLITE_ROW) {
4584 label_record = (label_s*) malloc(sizeof(label_s));
4586 if (label_record == NULL) {
4587 ACCOUNT_FATAL("malloc Failed");
4591 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
4593 _account_type_convert_column_to_label(hstmt, label_record);
4595 _ACCOUNT_FREE(*label);
4596 //Making label newly created
4597 *label = _account_dup_text(label_record->label);
4599 _account_type_free_label_with_items(label_record);
4601 rc = _account_query_step(hstmt);
4604 rc = _account_query_finalize(hstmt);
4605 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4608 error_code = _ACCOUNT_ERROR_NONE;
4611 if (hstmt != NULL) {
4612 rc = _account_query_finalize(hstmt);
4613 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4617 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
4618 error_code = _account_type_query_label_by_locale_from_global_db(app_id, locale, label);
4620 _INFO("_account_type_query_label_by_locale() end : error_code = %d", error_code);
4625 static int _account_insert_custom(account_s *account, int account_id)
4627 _INFO("_account_insert_custom start");
4630 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4631 account_stmt hstmt = NULL;
4633 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4635 if (g_slist_length(account->custom_list) == 0) {
4636 ACCOUNT_DEBUG("_account_insert_custom, no custom data\n");
4637 return _ACCOUNT_ERROR_NONE;
4640 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
4642 rc = _account_get_record_count(g_hAccountDB, query);
4644 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4645 ACCOUNT_ERROR("Access failed(%d, %s)", _account_db_err_msg(g_hAccountDB));
4646 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4650 ACCOUNT_SLOGE("_account_insert_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
4651 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4658 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
4661 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4662 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (AccountId, AppId, Key, Value) VALUES "
4663 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
4665 hstmt = _account_prepare_query(g_hAccountDB, query);
4667 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4668 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4669 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4672 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
4674 account_custom_s* custom_data = NULL;
4675 custom_data = (account_custom_s*)iter->data;
4677 ret = _account_query_bind_int(hstmt, count++, account_id);
4678 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
4679 ret = _account_query_bind_text(hstmt, count++, account->package_name);
4680 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4681 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
4682 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4683 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
4684 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4686 rc = _account_query_step(hstmt);
4688 if (rc != SQLITE_DONE) {
4689 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4693 rc = _account_query_finalize(hstmt);
4694 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4699 _INFO("_account_insert_custom end");
4701 return _ACCOUNT_ERROR_NONE;
4704 static int _account_update_custom(account_s *account, int account_id)
4707 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4708 account_stmt hstmt = NULL;
4710 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4712 if (g_slist_length(account->custom_list) == 0) {
4713 ACCOUNT_DEBUG("_account_update_custom, no custom data\n");
4714 return _ACCOUNT_ERROR_NONE;
4717 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
4719 rc = _account_get_record_count(g_hAccountDB, query);
4721 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4722 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4723 pthread_mutex_unlock(&account_mutex);
4724 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4725 } else if (_account_db_err_code(g_hAccountDB) == SQLITE_BUSY) {
4726 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
4727 pthread_mutex_unlock(&account_mutex);
4728 return _ACCOUNT_ERROR_DATABASE_BUSY;
4732 ACCOUNT_SLOGE("_account_update_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
4733 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4736 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4738 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId=? ", ACCOUNT_CUSTOM_TABLE);
4739 hstmt = _account_prepare_query(g_hAccountDB, query);
4741 _account_query_bind_int(hstmt, count++, (int)account_id);
4742 rc = _account_query_step(hstmt);
4744 if (rc == SQLITE_BUSY) {
4745 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4746 return _ACCOUNT_ERROR_DATABASE_BUSY;
4747 } else if (rc != SQLITE_DONE) {
4748 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4749 return _ACCOUNT_ERROR_DB_FAILED;
4752 rc = _account_query_finalize(hstmt);
4753 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4758 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
4761 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4762 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AccountId, AppId, Key, Value) VALUES "
4763 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
4765 hstmt = _account_prepare_query(g_hAccountDB, query);
4767 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4768 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4769 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4772 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
4774 account_custom_s* custom_data = NULL;
4775 custom_data = (account_custom_s*)iter->data;
4777 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
4778 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
4779 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
4780 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4781 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
4782 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4783 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
4784 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4786 rc = _account_query_step(hstmt);
4788 if (rc != SQLITE_DONE) {
4789 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4793 rc = _account_query_finalize(hstmt);
4794 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4799 return _ACCOUNT_ERROR_NONE;