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 static pthread_mutex_t account_mutex = PTHREAD_MUTEX_INITIALIZER;
64 //static char *_account_dup_text(const char *text_data);
65 static int _account_insert_custom(account_s *account, int account_id);
66 static int _account_update_custom(account_s *account, int account_id);
67 static int _account_type_update_provider_feature(sqlite3 * account_db_handle, account_type_s *account_type, const char* app_id);
69 static void _account_insert_delete_update_notification_send(char *noti_name)
72 _ERR("Noti Name is NULL!!!!!!\n");
76 _INFO("noti_type = %s", noti_name);
78 if (vconf_set_str(VCONFKEY_ACCOUNT_MSG_STR, noti_name) != 0) {
79 _ERR("Vconf MSG Str set FAILED !!!!!!\n");;
83 int _account_get_current_appid_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
87 GSList** appid_list = (GSList**)user_data;
90 pkgmgr_ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
92 if (pkgmgr_ret != PMINFO_R_OK)
93 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_appid(%d)", pkgmgr_ret);
95 item = _account_dup_text(appid);
96 *appid_list = g_slist_append(*appid_list, item);
101 static inline int __read_proc(const char *path, char *buf, int size)
105 if (buf == NULL || path == NULL) {
106 ACCOUNT_ERROR("path and buffer is mandatory\n");
110 fd = open(path, O_RDONLY);
112 ACCOUNT_ERROR("fd open error(%d)\n", fd);
116 ret = read(fd, buf, size - 1);
118 ACCOUNT_ERROR("fd read error(%d)\n", fd);
130 char *_account_get_proc_cmdline_bypid(int pid)
135 ACCOUNT_SNPRINTF(buf, sizeof(buf), "/proc/%d/cmdline", pid);
136 ret = __read_proc(buf, buf, sizeof(buf));
138 ACCOUNT_DEBUG("No proc directory (%d)\n", pid);
146 static int _account_execute_query_from_global_db(const char *query)
149 char* pszErrorMsg = NULL;
152 ACCOUNT_ERROR("NULL query\n");
153 return _ACCOUNT_ERROR_QUERY_SYNTAX_ERROR;
156 if (!g_hAccountGlobalDB) {
157 ACCOUNT_ERROR("Global DB is not opened\n");
158 return _ACCOUNT_ERROR_DB_NOT_OPENED;
161 rc = sqlite3_exec(g_hAccountGlobalDB, query, NULL, NULL, &pszErrorMsg);
162 if (SQLITE_OK != rc) {
163 ACCOUNT_ERROR("sqlite3_exec rc(%d) query(%s) failed(%s).", rc, query, pszErrorMsg);
164 sqlite3_free(pszErrorMsg);
172 static int _account_begin_transaction_from_global_db(void)
174 ACCOUNT_DEBUG("_account_begin_transaction start");
177 ret = _account_execute_query_from_global_db("BEGIN IMMEDIATE TRANSACTION");
179 if (ret == SQLITE_BUSY) {
180 ACCOUNT_ERROR(" sqlite3 busy = %d", ret);
181 return _ACCOUNT_ERROR_DATABASE_BUSY;
182 } else if (ret != SQLITE_OK) {
183 ACCOUNT_ERROR("_account_svc_begin_transaction_in_global_db fail :: %d", ret);
184 return _ACCOUNT_ERROR_DB_FAILED;
187 ACCOUNT_DEBUG("_account_begin_transaction_in_global_db end");
188 return _ACCOUNT_ERROR_NONE;
191 static int _account_end_transaction_from_global_db(bool is_success)
193 ACCOUNT_DEBUG("_account_end_transaction_in_global_db start");
197 if (is_success == true) {
198 ret = _account_execute_query_from_global_db("COMMIT TRANSACTION");
199 ACCOUNT_DEBUG("_account_end_transaction_in_global_db COMMIT");
201 ret = _account_execute_query_from_global_db("ROLLBACK TRANSACTION");
202 ACCOUNT_DEBUG("_account_end_transaction ROLLBACK");
205 if (ret == SQLITE_PERM) {
206 ACCOUNT_ERROR("Account permission denied :: %d", ret);
207 return _ACCOUNT_ERROR_PERMISSION_DENIED;
210 if (ret == SQLITE_BUSY) {
211 ACCOUNT_DEBUG(" sqlite3 busy = %d", ret);
212 return _ACCOUNT_ERROR_DATABASE_BUSY;
215 if (ret != SQLITE_OK) {
216 ACCOUNT_ERROR("_account_svc_end_transaction_in_global_db fail :: %d", ret);
217 return _ACCOUNT_ERROR_DB_FAILED;
220 ACCOUNT_DEBUG("_account_end_transaction_in_global_db end");
221 return _ACCOUNT_ERROR_NONE;
225 int _account_global_db_open(void)
229 char account_db_path[256] = {0, };
231 _INFO("start _account_global_db_open()");
233 ACCOUNT_MEMSET(account_db_path, 0x00, sizeof(account_db_path));
234 ACCOUNT_GET_GLOBAL_DB_PATH(account_db_path, sizeof(account_db_path));
236 if (g_hAccountGlobalDB) {
237 _ERR("Account database is using in another app. %x", g_hAccountDB);
238 return _ACCOUNT_ERROR_DATABASE_BUSY;
241 ret = _account_db_handle_close(g_hAccountGlobalDB2);
242 if (ret != _ACCOUNT_ERROR_NONE)
243 ACCOUNT_DEBUG("db_util_close(g_hAccountGlobalDB2) fail ret = %d", ret);
245 ACCOUNT_DEBUG("before _account_global_db_open()");
246 // if (mode == ACCOUNT_DB_OPEN_READWRITE)
247 // rc = db_util_open(account_db_path, &g_hAccountDB, DB_UTIL_REGISTER_HOOK_METHOD);
248 // else if (mode == ACCOUNT_DB_OPEN_READONLY)
249 rc = db_util_open_with_options(account_db_path, &g_hAccountGlobalDB, SQLITE_OPEN_READONLY, NULL);
251 // return _ACCOUNT_ERROR_DB_NOT_OPENED;
252 ACCOUNT_DEBUG("after _account_global_db_open() sqlite_rc = %d", rc);
254 if (rc == SQLITE_PERM || _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
255 ACCOUNT_ERROR("Account permission denied");
256 return _ACCOUNT_ERROR_PERMISSION_DENIED;
259 if (rc == SQLITE_BUSY) {
260 ACCOUNT_ERROR("busy handler fail.");
261 return _ACCOUNT_ERROR_DATABASE_BUSY;
264 if (rc != SQLITE_OK) {
265 ACCOUNT_ERROR("The database isn't connected.");
266 return _ACCOUNT_ERROR_DB_NOT_OPENED;
269 _INFO("end _account_global_db_open()");
270 return _ACCOUNT_ERROR_NONE;
273 int _account_global_db_close(void)
275 ACCOUNT_DEBUG("start account_global_db_close()");
278 ret = _account_db_handle_close(g_hAccountGlobalDB2);
279 if (ret != _ACCOUNT_ERROR_NONE)
280 ACCOUNT_DEBUG("db_util_close(g_hAccountGlobalDB2) fail ret = %d", ret);
282 ret = _account_db_handle_close(g_hAccountGlobalDB);
283 if (ret != _ACCOUNT_ERROR_NONE) {
284 ACCOUNT_ERROR("db_util_close(g_hAccountGlobalDB) fail ret = %d", ret);
285 g_hAccountGlobalDB2 = g_hAccountGlobalDB;
287 g_hAccountGlobalDB = NULL;
292 static bool _account_check_add_more_account(const char* app_id)
294 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
297 ACCOUNT_RETURN_VAL((app_id != 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
298 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
300 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
302 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s' and MultipleAccountSupport = 1", ACCOUNT_TYPE_TABLE, app_id);
303 rc = _account_get_record_count(g_hAccountDB, query);
305 /* multiple account support case (User DB & global DB) */
306 if (rc > 0 || _account_get_record_count(g_hAccountGlobalDB, query) > 0) {
307 ACCOUNT_SLOGD("app id (%s) supports multiple account. rc(%d)\n", app_id, rc);
311 /* multiple account not support case */
312 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
313 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE package_name = '%s'", ACCOUNT_TABLE, app_id);
314 rc = _account_get_record_count(g_hAccountDB, query);
317 ACCOUNT_SLOGD("app id (%s) supports single account. and there is no account of the app id\n", app_id);
325 int _account_db_open(int mode, int pid, uid_t uid)
329 char account_db_dir[256] = {0, };
330 char account_db_path[256] = {0, };
332 _INFO("start _account_db_open() pid [%d]. uid [%d]", pid, uid);
334 ACCOUNT_MEMSET(account_db_dir, 0x00, sizeof(account_db_dir));
335 ACCOUNT_MEMSET(account_db_path, 0x00, sizeof(account_db_path));
337 ACCOUNT_GET_USER_DB_PATH(account_db_path, sizeof(account_db_path), uid);
340 _ERR("Account database is using in another app. %x", g_hAccountDB);
341 return _ACCOUNT_ERROR_DATABASE_BUSY;
344 ret = _account_db_handle_close(g_hAccountDB2);
345 if (ret != _ACCOUNT_ERROR_NONE)
346 ACCOUNT_DEBUG("db_util_close(g_hAccountDB2) fail ret = %d", ret);
348 ACCOUNT_GET_USER_DB_DIR(account_db_dir, sizeof(account_db_dir), uid);
350 if (mkdir(account_db_dir, 0777) != 0)
351 ACCOUNT_DEBUG("\"%s\" is already exist directory", account_db_dir);
353 ACCOUNT_DEBUG("before db_util_open()");
354 // if (mode == ACCOUNT_DB_OPEN_READWRITE)
355 rc = db_util_open(account_db_path, &g_hAccountDB, DB_UTIL_REGISTER_HOOK_METHOD);
356 // else if (mode == ACCOUNT_DB_OPEN_READONLY)
357 // rc = db_util_open_with_options(account_db_path, &g_hAccountDB, SQLITE_OPEN_READONLY, NULL);
359 // return _ACCOUNT_ERROR_DB_NOT_OPENED;
360 ACCOUNT_DEBUG("after db_util_open() sqlite_rc = %d", rc);
362 if (rc == SQLITE_PERM || _account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
363 ACCOUNT_ERROR("Account permission denied");
364 return _ACCOUNT_ERROR_PERMISSION_DENIED;
367 if (rc == SQLITE_BUSY) {
368 ACCOUNT_ERROR("busy handler fail.");
369 return _ACCOUNT_ERROR_DATABASE_BUSY;
372 if (rc != SQLITE_OK) {
373 ACCOUNT_ERROR("The database isn't connected.");
374 return _ACCOUNT_ERROR_DB_NOT_OPENED;
377 rc = _account_check_is_all_table_exists(g_hAccountDB);
380 _ERR("_account_check_is_all_table_exists rc=[%d]", rc);
382 } else if (rc == ACCOUNT_TABLE_TOTAL_COUNT) {
385 int ret = _account_create_all_tables(g_hAccountDB);
386 if (ret != _ACCOUNT_ERROR_NONE) {
387 _ERR("_account_create_all_tables fail ret=[%d]", ret);
392 _INFO("end _account_db_open()");
393 return _ACCOUNT_ERROR_NONE;
396 int _account_db_close(void)
398 ACCOUNT_DEBUG("start db_util_close()");
401 ret = _account_db_handle_close(g_hAccountDB2);
402 if (ret != _ACCOUNT_ERROR_NONE)
403 ACCOUNT_DEBUG("db_util_close(g_hAccountDB2) fail ret = %d", ret);
405 ret = _account_db_handle_close(g_hAccountDB);
406 if (ret != _ACCOUNT_ERROR_NONE) {
407 ACCOUNT_ERROR("db_util_close(g_hAccountDB) fail ret = %d", ret);
408 g_hAccountDB2 = g_hAccountDB;
415 static int _account_execute_insert_query(account_s *account)
417 _INFO("_account_execute_insert_query start");
420 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
421 int error_code = _ACCOUNT_ERROR_NONE;
422 account_stmt hstmt = NULL;
424 /* check whether app id exist in account type db */
426 if (!account->user_name && !account->display_name && !account->email_address) {
428 ACCOUNT_ERROR("Mandetory fields is NULL. At least one field is required among username, display name, email address\n");
429 return _ACCOUNT_ERROR_INVALID_PARAMETER;
433 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
434 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (user_name, email_address , display_name , icon_path , source , package_name , "
435 "access_token , domain_name , auth_type , secret , sync_support , txt_custom0, txt_custom1, txt_custom2, txt_custom3, txt_custom4, "
436 "int_custom0, int_custom1, int_custom2, int_custom3, int_custom4, txt_custom0 ) values " // to do urusa
437 "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", ACCOUNT_TABLE);
439 hstmt = _account_prepare_query(g_hAccountDB, query);
440 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
443 _account_convert_account_to_sql(account, hstmt, query);
446 rc = _account_query_step(hstmt);
447 if (rc != SQLITE_DONE) {
449 ACCOUNT_ERROR("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
451 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM)
452 error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
454 error_code = _ACCOUNT_ERROR_DB_FAILED;
458 rc = _account_query_finalize(hstmt);
459 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
462 _INFO("_account_execute_insert_query end");
466 static int _account_insert_capability(account_s *account, int account_id)
468 _INFO("_account_insert_capability start");
470 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
471 account_stmt hstmt = NULL;
473 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
475 if (g_slist_length(account->capablity_list) == 0) {
476 ACCOUNT_DEBUG("_account_insert_capability, no capability\n");
477 return _ACCOUNT_ERROR_NONE;
480 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
482 _INFO("_account_insert_capability _account_get_record_count [%s]", query);
483 rc = _account_get_record_count(g_hAccountDB, query);
485 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
486 _ERR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
487 return _ACCOUNT_ERROR_PERMISSION_DENIED;
491 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
497 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
501 account_capability_s* cap_data = NULL;
502 cap_data = (account_capability_s*)iter->data;
504 _INFO("cap_data->type = %s, cap_data->value = %d \n", cap_data->type, cap_data->value);
506 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
507 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
508 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
509 hstmt = _account_prepare_query(g_hAccountDB, query);
511 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
513 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
514 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
515 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
516 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
517 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
518 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
519 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
520 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
521 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
522 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
524 rc = _account_query_step(hstmt);
525 _INFO("_account_insert_capability _account_query_step[%d]", rc);
527 if (rc != SQLITE_DONE) {
528 _ERR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
532 rc = _account_query_finalize(hstmt);
533 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
538 _INFO("_account_insert_capability end");
539 return _ACCOUNT_ERROR_NONE;
542 static int _account_update_capability(account_s *account, int account_id)
545 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
546 account_stmt hstmt = NULL;
548 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
550 if (g_slist_length(account->capablity_list) == 0) {
551 ACCOUNT_ERROR("_account_update_capability, no capability\n");
552 return _ACCOUNT_ERROR_NONE;
555 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
557 rc = _account_get_record_count(g_hAccountDB, query);
560 ACCOUNT_SLOGI("_account_update_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
561 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
564 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
566 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id=? ", CAPABILITY_TABLE);
567 hstmt = _account_prepare_query(g_hAccountDB, query);
569 _account_query_bind_int(hstmt, count++, (int)account_id);
570 rc = _account_query_step(hstmt);
572 if (rc != SQLITE_DONE) {
573 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
574 if (_account_query_finalize(hstmt) != _ACCOUNT_ERROR_NONE)
575 _ERR("finalize error - account_query_step() failed");
576 return _ACCOUNT_ERROR_DB_FAILED;
578 rc = _account_query_finalize(hstmt);
579 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
584 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
587 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
588 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
589 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
591 hstmt = _account_prepare_query(g_hAccountDB, query);
593 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
595 account_capability_s* cap_data = NULL;
596 cap_data = (account_capability_s*)iter->data;
598 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
599 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
600 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
601 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
602 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
603 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
604 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
605 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
606 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
607 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
609 rc = _account_query_step(hstmt);
611 if (rc != SQLITE_DONE) {
612 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
616 rc = _account_query_finalize(hstmt);
617 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
622 return _ACCOUNT_ERROR_NONE;
625 static int _account_update_capability_by_user_name(account_s *account, const char *user_name, const char *package_name)
628 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
629 account_stmt hstmt = NULL;
631 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
633 if (g_slist_length(account->capablity_list) == 0) {
634 ACCOUNT_ERROR("_account_update_capability_by_user_name, no capability\n");
635 return _ACCOUNT_ERROR_NONE;
638 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where package_name= '%s' and user_name='%s'", ACCOUNT_TABLE, package_name, user_name);
640 rc = _account_get_record_count(g_hAccountDB, query);
643 ACCOUNT_SLOGI("_account_update_capability_by_user_name : related account item is not existed rc=%d , %s ", rc, _account_db_err_msg(g_hAccountDB));
644 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
647 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
649 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name=? and user_name=? ", CAPABILITY_TABLE);
650 hstmt = _account_prepare_query(g_hAccountDB, query);
652 _account_query_bind_text(hstmt, count++, (char*)account->package_name);
653 _account_query_bind_text(hstmt, count++, (char*)account->user_name);
654 rc = _account_query_step(hstmt);
655 if (rc != SQLITE_DONE) {
656 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
657 if (_account_query_finalize(hstmt) != _ACCOUNT_ERROR_NONE)
658 _ERR("finalize error - account_query_step() failed");
659 return _ACCOUNT_ERROR_DB_FAILED;
662 rc = _account_query_finalize(hstmt);
663 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
668 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
671 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
672 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
673 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
675 hstmt = _account_prepare_query(g_hAccountDB, query);
677 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
679 account_capability_s* cap_data = NULL;
680 cap_data = (account_capability_s*)iter->data;
682 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
683 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
684 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
685 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
686 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
687 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
688 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
689 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
690 ret = _account_query_bind_int(hstmt, count++, (int)account->id);
691 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
693 rc = _account_query_step(hstmt);
695 if (rc != SQLITE_DONE) {
696 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
700 rc = _account_query_finalize(hstmt);
701 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
706 return _ACCOUNT_ERROR_NONE;
710 bool _account_add_capability_to_account_cb(const char* capability_type, int capability_value, account_s *account)
712 account_capability_s *cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
714 if (cap_data == NULL)
716 ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
718 cap_data->type = _account_dup_text(capability_type);
719 cap_data->value = capability_value;
720 _INFO("cap_data->type = %s, cap_data->value = %d", cap_data->type, cap_data->value);
722 account->capablity_list = g_slist_append(account->capablity_list, (gpointer)cap_data);
728 bool _account_add_custom_to_account_cb(const char* key, const char* value, account_s *account)
730 account_custom_s *custom_data = (account_custom_s*)malloc(sizeof(account_custom_s));
732 if (custom_data == NULL) {
733 ACCOUNT_DEBUG("_account_add_custom_to_account_cb :: malloc fail\n");
736 ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s));
738 custom_data->account_id = account->id;
739 custom_data->app_id = _account_dup_text(account->package_name);
740 custom_data->key = _account_dup_text(key);
741 custom_data->value = _account_dup_text(value);
742 _INFO("custom_data->key = %s, custom_data->value = %s", custom_data->key, custom_data->value);
744 account->custom_list = g_slist_append(account->custom_list, (gpointer)custom_data);
749 static void _account_compare_new_and_old_account_properties(account_s** new_account, account_s* old_account)
751 if ((*new_account) == NULL || old_account == NULL) {
752 _ERR("invalid input params");
757 (*new_account)->id = old_account->id;
760 if (!(*new_account)->user_name) {
761 if (old_account->user_name)
762 (*new_account)->user_name = _account_dup_text(old_account->user_name);
766 if (!(*new_account)->display_name) {
767 if (old_account->display_name)
768 (*new_account)->display_name = _account_dup_text(old_account->display_name);
772 if (!(*new_account)->email_address) {
773 if (old_account->email_address)
774 (*new_account)->email_address = _account_dup_text(old_account->email_address);
778 if (!(*new_account)->domain_name) {
779 if (old_account->domain_name)
780 (*new_account)->domain_name = _account_dup_text(old_account->domain_name);
784 if (!(*new_account)->icon_path) {
785 if (old_account->icon_path)
786 (*new_account)->icon_path = _account_dup_text(old_account->icon_path);
790 if (!(*new_account)->source) {
791 if (old_account->source)
792 (*new_account)->source = _account_dup_text(old_account->source);
795 _ACCOUNT_FREE((*new_account)->package_name);
796 (*new_account)->package_name = _account_dup_text(old_account->package_name);
799 if (!(*new_account)->access_token) {
800 if (old_account->access_token)
801 (*new_account)->access_token = _account_dup_text(old_account->access_token);
805 if ((*new_account)->auth_type == _ACCOUNT_AUTH_TYPE_INVALID)
806 (*new_account)->auth_type = old_account->auth_type;
809 if ((*new_account)->secret == _ACCOUNT_SECRECY_INVALID)
810 (*new_account)->secret = old_account->secret;
813 if ((*new_account)->sync_support == _ACCOUNT_SYNC_INVALID)
814 (*new_account)->sync_support = old_account->sync_support;
818 for (i = 0; i < USER_TXT_CNT; i++) {
819 if (!(*new_account)->user_data_txt[i]) {
820 if (old_account->user_data_txt[i])
821 (*new_account)->user_data_txt[i] = _account_dup_text(old_account->user_data_txt[i]);
826 for (i = 0; i < USER_INT_CNT; i++) {
827 if ((*new_account)->user_data_int[i] == 0)
828 (*new_account)->user_data_int[i] = old_account->user_data_int[i];
835 static int _account_compare_old_record_by_user_name(account_s *new_account, const char* user_name, const char* package_name)
837 int error_code = _ACCOUNT_ERROR_NONE;
838 account_stmt hstmt = NULL;
839 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
841 account_s *old_account = NULL;
843 ACCOUNT_RETURN_VAL((new_account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
844 ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
845 ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
846 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
848 old_account = (account_s*)calloc(1, sizeof(account_s));
850 ACCOUNT_FATAL("Memory alloc fail\n");
851 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
854 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
856 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = '%s' and package_name='%s'", ACCOUNT_TABLE, user_name, package_name);
857 hstmt = _account_prepare_query(g_hAccountDB, query);
859 rc = _account_query_step(hstmt);
860 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
862 while (rc == SQLITE_ROW) {
863 _account_convert_column_to_account(hstmt, old_account);
864 rc = _account_query_step(hstmt);
867 rc = _account_query_finalize(hstmt);
868 ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
872 error_code = _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, old_account->id, (void*)old_account, false);
873 ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
876 error_code = _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, old_account->id, (void*)old_account, false);
877 ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
879 _account_compare_new_and_old_account_properties(&new_account, old_account);
883 _account_free_account_with_items(old_account);
886 rc = _account_query_finalize(hstmt);
887 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
891 return _ACCOUNT_ERROR_NONE;
894 static int _account_update_account_by_user_name(int pid, uid_t uid, account_s *account, const char *user_name, const char *package_name)
896 int rc = 0, binding_count = 0, count = 0;
897 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
898 int error_code = _ACCOUNT_ERROR_NONE;
899 account_stmt hstmt = NULL;
901 ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is NULL.\n"));
902 ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is NULL.\n"));
904 char* current_appid = NULL;
905 char* verified_appid = NULL;
907 current_appid = _account_get_current_appid(pid, uid);
908 error_code = _account_get_represented_appid_from_db(g_hAccountDB, g_hAccountGlobalDB, current_appid, uid, &verified_appid);
910 _ACCOUNT_FREE(current_appid);
911 _ACCOUNT_FREE(verified_appid);
913 if (error_code != _ACCOUNT_ERROR_NONE) {
914 ACCOUNT_ERROR("No permission to update\n");
915 return _ACCOUNT_ERROR_PERMISSION_DENIED;
918 error_code = encrypt_access_token(account);
919 if (error_code != _ACCOUNT_ERROR_NONE) {
920 _ERR("_encrypt_access_token error");
924 _account_compare_old_record_by_user_name(account, user_name, package_name);
926 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
927 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
928 return _ACCOUNT_ERROR_PERMISSION_DENIED;
931 if (!account->package_name) {
932 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
933 return _ACCOUNT_ERROR_INVALID_PARAMETER;
936 if (!account->user_name && !account->display_name && !account->email_address) {
937 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
938 return _ACCOUNT_ERROR_INVALID_PARAMETER;
941 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE user_name='%s' and package_name='%s'"
942 , ACCOUNT_TABLE, user_name, package_name);
944 count = _account_get_record_count(g_hAccountDB, query);
946 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
947 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
948 return _ACCOUNT_ERROR_PERMISSION_DENIED;
952 ACCOUNT_SLOGI("_account_update_account_by_user_name : The account not exist!, count = %d, user_name=%s, package_name=%s\n",
953 count, user_name, package_name);
954 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
957 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
959 //TODO: Is it required to update id ? As of now I can only think of falied rollback cases (between account and gSSO DB)
960 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
961 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
962 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
963 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE user_name=? and package_name=? ", ACCOUNT_TABLE);
965 hstmt = _account_prepare_query(g_hAccountDB, query);
966 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
967 _account_end_transaction(g_hAccountDB, FALSE);
968 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
969 return _ACCOUNT_ERROR_PERMISSION_DENIED;
971 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
973 binding_count = _account_convert_account_to_sql(account, hstmt, query);
975 _account_query_bind_text(hstmt, binding_count++, user_name);
976 _account_query_bind_text(hstmt, binding_count++, package_name);
978 rc = _account_query_step(hstmt);
979 if (rc != SQLITE_DONE)
980 ACCOUNT_ERROR("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
982 rc = _account_query_finalize(hstmt);
983 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
986 /*update capability*/
987 error_code = _account_update_capability_by_user_name(account, user_name, package_name);
990 error_code = _account_update_custom(account, account->id);
995 int _account_insert_to_db(account_s* account, int pid, uid_t uid, int *account_id)
998 int error_code = _ACCOUNT_ERROR_NONE;
999 int ret_transaction = 0;
1001 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1002 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1003 ACCOUNT_RETURN_VAL((account_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID POINTER IS NULL"));
1005 if (!account->user_name && !account->display_name && !account->email_address) {
1006 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1007 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1010 account_s *data = (account_s*)account;
1011 ACCOUNT_SLOGD("(%s)-(%d) account_insert_to_db: begin_transaction.\n", __FUNCTION__, __LINE__);
1013 pthread_mutex_lock(&account_mutex);
1015 /* transaction control required*/
1016 ret_transaction = _account_begin_transaction(g_hAccountDB);
1018 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1019 pthread_mutex_unlock(&account_mutex);
1020 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1021 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1024 if (ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY) {
1025 ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction);
1026 pthread_mutex_unlock(&account_mutex);
1027 return _ACCOUNT_ERROR_DATABASE_BUSY;
1028 } else if (ret_transaction != _ACCOUNT_ERROR_NONE) {
1029 ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction);
1030 pthread_mutex_unlock(&account_mutex);
1031 return ret_transaction;
1034 *account_id = _account_get_next_sequence(g_hAccountDB, ACCOUNT_TABLE);
1035 data->id = *account_id;
1038 appid = _account_get_current_appid(pid, uid);
1042 // API caller cannot be recognized
1043 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1044 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1045 pthread_mutex_unlock(&account_mutex);
1046 return _ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
1050 char* verified_appid = NULL;
1051 error_code = _account_get_represented_appid_from_db(g_hAccountDB, g_hAccountGlobalDB, appid, uid, &verified_appid);//FIX
1052 _ACCOUNT_FREE(appid);
1053 if (error_code != _ACCOUNT_ERROR_NONE) {
1054 _ERR("error_code = %d", error_code);
1055 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1056 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1057 _ACCOUNT_FREE(verified_appid);
1058 pthread_mutex_unlock(&account_mutex);
1062 if (verified_appid) {
1064 error_code = _account_check_duplicated(g_hAccountDB, data, verified_appid, uid);
1065 if (error_code != _ACCOUNT_ERROR_NONE) {
1067 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1068 ACCOUNT_DEBUG("_account_check_duplicated(), rollback insert query(%x)!!!!\n", ret_transaction);
1070 pthread_mutex_unlock(&account_mutex);
1073 if (!_account_check_add_more_account(verified_appid)) {
1074 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1075 ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
1076 pthread_mutex_unlock(&account_mutex);
1077 _ACCOUNT_FREE(verified_appid);
1078 return _ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
1081 _ACCOUNT_FREE(data->package_name);
1082 data->package_name = _account_dup_text(verified_appid);
1083 _ACCOUNT_FREE(verified_appid);
1086 if (!_account_check_add_more_account(data->package_name)) {
1088 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1089 ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
1090 pthread_mutex_unlock(&account_mutex);
1091 return _ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
1094 error_code = encrypt_access_token(data);
1095 if (error_code != _ACCOUNT_ERROR_NONE) {
1096 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1097 ACCOUNT_ERROR("encrypt_access_token fail, rollback insert query(%x)!!!!\n", ret_transaction);
1099 pthread_mutex_unlock(&account_mutex);
1103 error_code = _account_execute_insert_query(data);
1105 if (error_code != _ACCOUNT_ERROR_NONE) {
1107 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1108 ACCOUNT_ERROR("INSERT account fail, rollback insert query(%x)!!!!\n", ret_transaction);
1110 pthread_mutex_unlock(&account_mutex);
1115 error_code = _account_insert_capability(data, *account_id);
1116 if (error_code != _ACCOUNT_ERROR_NONE) {
1118 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1119 ACCOUNT_ERROR("INSERT capability fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1121 pthread_mutex_unlock(&account_mutex);
1126 error_code = _account_insert_custom(data, *account_id);
1127 if (error_code != _ACCOUNT_ERROR_NONE) {
1128 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1129 ACCOUNT_ERROR("INSERT custom fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1131 pthread_mutex_unlock(&account_mutex);
1137 pthread_mutex_unlock(&account_mutex);
1138 _account_end_transaction(g_hAccountDB, TRUE);
1139 ACCOUNT_SLOGD("(%s)-(%d) account _end_transaction.\n", __FUNCTION__, __LINE__);
1141 char buf[64] = {0,};
1142 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_INSERT, *account_id);
1143 _account_insert_delete_update_notification_send(buf);
1144 _INFO("account _notification_send end.");
1146 return _ACCOUNT_ERROR_NONE;
1149 GSList* _account_get_capability_list_by_account_id(int account_id, int *error_code)
1151 *error_code = _ACCOUNT_ERROR_NONE;
1152 account_stmt hstmt = NULL;
1153 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1155 GSList* capability_list = NULL;
1157 ACCOUNT_RETURN_VAL((account_id > 0), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("ACCOUNT INDEX IS LESS THAN 0"));
1158 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), { *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED; }, NULL, ("The database isn't connected."));
1160 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1162 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
1163 hstmt = _account_prepare_query(g_hAccountDB, query);
1165 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1166 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1167 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
1171 rc = _account_query_step(hstmt);
1172 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1174 account_capability_s* capability_record = NULL;
1176 while (rc == SQLITE_ROW) {
1177 capability_record = (account_capability_s*) malloc(sizeof(account_capability_s));
1179 if (capability_record == NULL) {
1180 ACCOUNT_FATAL("malloc Failed");
1184 ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s));
1186 _account_convert_column_to_capability(hstmt, capability_record);
1188 //cb_ret = callback(capability_record->type, capability_record->value, user_data);
1190 //_account_free_capability_items(capability_record);
1191 //_ACCOUNT_FREE(capability_record);
1193 //ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
1195 capability_list = g_slist_append(capability_list, capability_record);
1196 rc = _account_query_step(hstmt);
1199 rc = _account_query_finalize(hstmt);
1200 if (rc != _ACCOUNT_ERROR_NONE) {
1201 _ERR("finalize error");
1202 _account_gslist_capability_free(capability_list);
1209 *error_code = _ACCOUNT_ERROR_NONE;
1212 if (hstmt != NULL) {
1213 rc = _account_query_finalize(hstmt);
1214 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), { *error_code = rc; }, NULL, ("finalize error"));
1218 pthread_mutex_unlock(&account_mutex);
1220 return capability_list;
1223 static int _account_compare_old_record(account_s *new_account, int account_id)
1225 int error_code = _ACCOUNT_ERROR_NONE;
1226 account_stmt hstmt = NULL;
1227 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1229 account_s *old_account = NULL;
1231 ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1232 ACCOUNT_RETURN_VAL((new_account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
1233 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1235 old_account = (account_s*)calloc(1, sizeof(account_s));
1236 if (old_account == NULL) {
1237 _ERR("Out of Memory");
1238 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
1241 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1243 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
1244 hstmt = _account_prepare_query(g_hAccountDB, query);
1246 rc = _account_query_step(hstmt);
1247 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1249 while (rc == SQLITE_ROW) {
1250 _account_convert_column_to_account(hstmt, old_account);
1251 rc = _account_query_step(hstmt);
1254 rc = _account_query_finalize(hstmt);
1255 ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1259 error_code = _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, old_account->id, (void*)old_account, false);
1260 ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
1263 error_code = _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, old_account->id, (void*)old_account, false);
1264 ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
1266 _account_compare_new_and_old_account_properties(&new_account, old_account);
1270 _account_free_account_with_items(old_account);
1272 if (hstmt != NULL) {
1273 rc = _account_query_finalize(hstmt);
1274 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1278 return _ACCOUNT_ERROR_NONE;
1281 static int _account_get_package_name_from_account_id(int account_id, char **package_name)
1283 int error_code = _ACCOUNT_ERROR_NONE;
1284 account_stmt hstmt = NULL;
1285 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1287 account_s *old_account = NULL;
1289 ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1290 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1292 old_account = (account_s*)calloc(1, sizeof(account_s));
1293 if (old_account == NULL) {
1294 _ERR("Out Of memory");
1295 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
1298 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1300 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
1301 hstmt = _account_prepare_query(g_hAccountDB, query);
1303 rc = _account_query_step(hstmt);
1304 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1306 while (rc == SQLITE_ROW) {
1307 _account_convert_column_to_account(hstmt, old_account);
1308 rc = _account_query_step(hstmt);
1311 rc = _account_query_finalize(hstmt);
1312 ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1315 // get package name.
1316 *package_name = _account_dup_text(old_account->package_name);
1320 _account_free_account_with_items(old_account);
1322 if (hstmt != NULL) {
1323 rc = _account_query_finalize(hstmt);
1324 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1332 static int _account_update_account(int pid, uid_t uid, account_s *account, int account_id)
1334 int rc = 0, binding_count = 0;
1335 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1336 int error_code = _ACCOUNT_ERROR_NONE, count = 0, ret_transaction = 0;
1337 account_stmt hstmt = NULL;
1339 if (!account->package_name) {
1340 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
1341 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1344 /* Check permission of requested appid */
1345 char* current_appid = NULL;
1346 char *package_name = NULL;
1348 current_appid = _account_get_current_appid(pid, uid);
1349 error_code = _account_get_package_name_from_account_id(account_id, &package_name);
1351 if (error_code != _ACCOUNT_ERROR_NONE || package_name == NULL) {
1352 ACCOUNT_ERROR("No package name with account_id\n");
1353 _ACCOUNT_FREE(current_appid);
1354 _ACCOUNT_FREE(package_name);
1355 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1358 error_code = _account_check_appid_group_with_package_name(current_appid, package_name, uid);
1359 ACCOUNT_DEBUG("UPDATE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name); // TODO: remove the log later.
1361 _ACCOUNT_FREE(current_appid);
1362 _ACCOUNT_FREE(package_name);
1364 if (error_code != _ACCOUNT_ERROR_NONE) {
1365 ACCOUNT_ERROR("No permission to update\n");
1366 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1369 error_code = encrypt_access_token(account);
1370 if (error_code != _ACCOUNT_ERROR_NONE) {
1371 _ERR("_encrypt_access_token error");
1375 error_code = _account_compare_old_record(account, account_id);
1376 if (error_code != _ACCOUNT_ERROR_NONE) {
1377 ACCOUNT_ERROR("_account_compare_old_record fail\n");
1381 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1382 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1383 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1384 } else if (_account_db_err_code(g_hAccountDB) == SQLITE_BUSY) {
1385 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
1386 return _ACCOUNT_ERROR_DATABASE_BUSY;
1389 if (!account->user_name && !account->display_name && !account->email_address) {
1390 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1391 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1394 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1396 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
1398 count = _account_get_record_count(g_hAccountDB, query);
1400 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
1401 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1404 /* transaction control required*/
1405 ret_transaction = _account_begin_transaction(g_hAccountDB);
1406 if (ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY) {
1407 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
1408 pthread_mutex_unlock(&account_mutex);
1409 return _ACCOUNT_ERROR_DATABASE_BUSY;
1412 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1413 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
1414 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
1415 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
1416 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
1418 hstmt = _account_prepare_query(g_hAccountDB, query);
1420 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1421 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1422 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1423 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1426 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)));
1428 binding_count = _account_convert_account_to_sql(account, hstmt, query);
1429 _account_query_bind_int(hstmt, binding_count++, account_id);
1431 rc = _account_query_step(hstmt);
1432 if (rc != SQLITE_DONE)
1433 ACCOUNT_SLOGE("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
1435 rc = _account_query_finalize(hstmt);
1436 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1439 _INFO("update query=%s", query);
1441 /*update capability*/
1442 error_code = _account_update_capability(account, account_id);
1443 if (error_code != _ACCOUNT_ERROR_NONE && error_code != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
1444 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1445 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1450 error_code = _account_update_custom(account, account_id);
1451 if (error_code != _ACCOUNT_ERROR_NONE && error_code != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
1452 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1453 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1457 ret_transaction = _account_end_transaction(g_hAccountDB, TRUE);
1459 _INFO("update end");
1465 static int _account_update_account_ex(account_s *account, int account_id)
1467 int rc = 0, binding_count = 0;
1468 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1469 int error_code = _ACCOUNT_ERROR_NONE, count = 0, ret_transaction = 0;
1470 account_stmt hstmt = NULL;
1472 if (!account->package_name) {
1473 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
1474 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1477 error_code = encrypt_access_token(account);
1478 if (error_code != _ACCOUNT_ERROR_NONE) {
1479 _ERR("_encrypt_access_token error");
1483 error_code = _account_compare_old_record(account, account_id);
1484 if (error_code != _ACCOUNT_ERROR_NONE) {
1485 ACCOUNT_ERROR("_account_compare_old_record fail\n");
1489 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1490 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1491 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1494 if (!account->user_name && !account->display_name && !account->email_address) {
1495 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1496 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1499 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1501 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
1503 count = _account_get_record_count(g_hAccountDB, query);
1505 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
1506 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1509 /* transaction control required*/
1510 ret_transaction = _account_begin_transaction(g_hAccountDB);
1511 if (ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY) {
1512 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
1513 pthread_mutex_unlock(&account_mutex);
1514 return _ACCOUNT_ERROR_DATABASE_BUSY;
1517 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1518 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
1519 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
1520 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
1521 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
1523 hstmt = _account_prepare_query(g_hAccountDB, query);
1525 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1526 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1527 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1528 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1531 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)));
1533 _INFO("account_update_to_db_by_id_ex_p : before convert() : account_id[%d], user_name=%s", account->id, account->user_name);
1534 binding_count = _account_convert_account_to_sql(account, hstmt, query);
1535 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], user_name=%s", account->id, account->user_name);
1536 _INFO("account_update_to_db_by_id_ex_p : before bind()");
1537 rc = _account_query_bind_int(hstmt, binding_count++, account_id);
1538 _INFO("account_update_to_db_by_id_ex_p : after bind() : ret = %d", rc);
1540 rc = _account_query_step(hstmt);
1541 if (rc != SQLITE_DONE)
1542 ACCOUNT_SLOGE("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
1543 _INFO("account_update_to_db_by_id_ex_p : after query_step() : ret = %d", rc);
1545 rc = _account_query_finalize(hstmt);
1546 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1548 _INFO("account_update_to_db_by_id_ex_p : after query_filnalize() : ret = %d", rc);
1550 _INFO("account_update_to_db_by_id_ex_p : before update_capability()");
1551 /*update capability*/
1552 error_code = _account_update_capability(account, account_id);
1553 if (error_code != _ACCOUNT_ERROR_NONE && error_code != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
1554 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1555 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1558 _INFO("account_update_to_db_by_id_ex_p : after update_capability()");
1560 _INFO("account_update_to_db_by_id_ex_p : before update_custom()");
1562 error_code = _account_update_custom(account, account_id);
1563 if (error_code != _ACCOUNT_ERROR_NONE && error_code != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
1564 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1565 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1568 _INFO("account_update_to_db_by_id_ex_p : after update_custom()");
1570 ret_transaction = _account_end_transaction(g_hAccountDB, TRUE);
1576 int _account_update_to_db_by_id(int pid, uid_t uid, account_s* account, int account_id)
1578 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
1579 ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
1580 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1581 int error_code = _ACCOUNT_ERROR_NONE;
1582 account_s* data = (account_s*)account;
1584 pthread_mutex_lock(&account_mutex);
1586 error_code = _account_update_account(pid, uid, data, account_id);
1588 if (error_code != _ACCOUNT_ERROR_NONE) {
1589 pthread_mutex_unlock(&account_mutex);
1593 pthread_mutex_unlock(&account_mutex);
1595 char buf[64] = {0,};
1596 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_UPDATE, account_id);
1597 _account_insert_delete_update_notification_send(buf);
1599 return _ACCOUNT_ERROR_NONE;
1602 int _account_update_to_db_by_id_ex(account_s* account, int account_id)
1604 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
1605 ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
1606 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1607 int error_code = _ACCOUNT_ERROR_NONE;
1608 account_s* data = account;
1610 pthread_mutex_lock(&account_mutex);
1612 _INFO("before update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name);
1613 error_code = _account_update_account_ex(data, account_id);
1614 _INFO("after update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name);
1616 if (error_code != _ACCOUNT_ERROR_NONE) {
1617 pthread_mutex_unlock(&account_mutex);
1621 pthread_mutex_unlock(&account_mutex);
1623 char buf[64] = {0,};
1624 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_UPDATE, account_id);
1625 _account_insert_delete_update_notification_send(buf);
1627 return _ACCOUNT_ERROR_NONE;
1630 int _account_update_to_db_by_user_name(int pid, uid_t uid, account_s* account, const char *user_name, const char *package_name)
1632 ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
1633 ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
1634 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1636 int error_code = _ACCOUNT_ERROR_NONE;
1637 account_s *data = (account_s*)account;
1639 pthread_mutex_lock(&account_mutex);
1641 error_code = _account_update_account_by_user_name(pid, uid, data, user_name, package_name);
1643 pthread_mutex_unlock(&account_mutex);
1645 char buf[64] = {0,};
1646 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_UPDATE, data->id);
1647 _account_insert_delete_update_notification_send(buf);
1652 GSList* _account_db_query_all(int pid, uid_t uid)
1654 //int error_code = _ACCOUNT_ERROR_NONE;
1655 account_stmt hstmt = NULL;
1656 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1658 GSList *account_list = NULL;
1660 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected."));
1662 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1664 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TABLE);
1665 hstmt = _account_prepare_query(g_hAccountDB, query);
1667 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1668 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1672 rc = _account_query_step(hstmt);
1674 account_s *account_record = NULL;
1676 if (rc != SQLITE_ROW) {
1677 _ERR("The record isn't found");
1681 while (rc == SQLITE_ROW) {
1682 account_record = (account_s*) malloc(sizeof(account_s));
1684 if (account_record == NULL) {
1685 ACCOUNT_FATAL("malloc Failed");
1689 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
1690 _account_convert_column_to_account(hstmt, account_record);
1691 account_list = g_slist_append(account_list, account_record);
1692 rc = _account_query_step(hstmt);
1695 rc = _account_query_finalize(hstmt);
1696 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_gslist_account_free(account_list); }, NULL, ("finalize error"));
1701 for (iter = account_list; iter != NULL; iter = g_slist_next(iter)) {
1702 account_s *account = NULL;
1703 account = (account_s*)iter->data;
1704 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, account->id, (void*)account, false);
1705 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, account->id, (void*)account, false);
1709 if (hstmt != NULL) {
1710 rc = _account_query_finalize(hstmt);
1711 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_gslist_account_free(account_list); }, NULL, ("finalize error"));
1716 _remove_sensitive_info_from_non_owning_account_slist(account_list, pid, uid);
1718 return account_list;
1721 int _account_update_sync_status_by_id(uid_t uid, int account_db_id, const int sync_status)
1723 int error_code = _ACCOUNT_ERROR_NONE;
1724 account_stmt hstmt = NULL;
1725 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1729 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1730 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1731 if ((sync_status < 0) || (sync_status >= _ACCOUNT_SYNC_MAX)) {
1732 ACCOUNT_SLOGE("(%s)-(%d) sync_status is less than 0 or more than enum max.\n", __FUNCTION__, __LINE__);
1733 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1736 pthread_mutex_lock(&account_mutex);
1738 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1740 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_db_id);
1742 rc = _account_get_record_count(g_hAccountDB, query);
1744 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1745 pthread_mutex_unlock(&account_mutex);
1746 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1747 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1751 ACCOUNT_SLOGE("account_update_sync_status_by_id : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
1752 pthread_mutex_unlock(&account_mutex);
1753 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1756 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1758 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET sync_support=? WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
1759 hstmt = _account_prepare_query(g_hAccountDB, query);
1761 _account_query_bind_int(hstmt, count, sync_status);
1763 rc = _account_query_step(hstmt);
1765 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1766 pthread_mutex_unlock(&account_mutex);
1767 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1768 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1771 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_DB_FAILED,
1772 ("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB)));
1774 rc = _account_query_finalize(hstmt);
1775 if (rc != _ACCOUNT_ERROR_NONE) {
1776 ACCOUNT_ERROR("_account_query_finalize error");
1777 pthread_mutex_unlock(&account_mutex);
1781 char buf[64] = {0,};
1782 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_SYNC_UPDATE, account_db_id);
1783 _account_insert_delete_update_notification_send(buf);
1786 error_code = _ACCOUNT_ERROR_NONE;
1789 if (hstmt != NULL) {
1790 rc = _account_query_finalize(hstmt);
1791 pthread_mutex_unlock(&account_mutex);
1792 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1796 pthread_mutex_unlock(&account_mutex);
1801 int _account_query_account_by_account_id(int pid, uid_t uid, int account_db_id, account_s *account_record, bool query_del_acc)
1803 _INFO("_account_query_account_by_account_id() start, account_db_id=[%d], query_del_acc=[%d]", account_db_id, query_del_acc);
1805 int error_code = _ACCOUNT_ERROR_NONE;
1806 account_stmt hstmt = NULL;
1807 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1810 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1811 ACCOUNT_RETURN_VAL(account_record != NULL, {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
1812 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1814 ACCOUNT_DEBUG("starting db operations");
1816 /* prepare query for account-table */
1817 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1819 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
1821 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", DELETED_ACCOUNT_TABLE, account_db_id);
1823 _INFO("after _account_prepare_query, rc=[%d]", rc);
1825 hstmt = _account_prepare_query(g_hAccountDB, query);
1827 rc = _account_db_err_code(g_hAccountDB);
1828 if (rc == SQLITE_PERM) {
1829 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1830 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1833 ACCOUNT_DEBUG("before _account_query_step");
1835 rc = _account_query_step(hstmt);
1837 ACCOUNT_DEBUG("after _account_query_step returned [%d]", rc);
1839 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1841 while (rc == SQLITE_ROW) {
1842 ACCOUNT_DEBUG("before _account_convert_column_to_account");
1843 _account_convert_column_to_account(hstmt, account_record);
1844 ACCOUNT_DEBUG("after _account_convert_column_to_account");
1845 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]);
1846 rc = _account_query_step(hstmt);
1849 ACCOUNT_DEBUG("account_record->id=[%d]", account_record->id);
1851 rc = _account_query_finalize(hstmt);
1852 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1854 ACCOUNT_DEBUG("before _account_query_capability_by_account_id");
1855 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, account_record->id, (void*)account_record, query_del_acc);
1856 ACCOUNT_DEBUG("after _account_query_capability_by_account_id");
1858 ACCOUNT_DEBUG("before _account_query_custom_by_account_id");
1859 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, account_record->id, (void*)account_record, query_del_acc);
1860 ACCOUNT_DEBUG("after _account_query_custom_by_account_id");
1863 error_code = _ACCOUNT_ERROR_NONE;
1866 if (hstmt != NULL) {
1867 rc = _account_query_finalize(hstmt);
1868 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1873 _remove_sensitive_info_from_non_owning_account(account_record, pid, uid);
1875 pthread_mutex_unlock(&account_mutex);
1876 ACCOUNT_DEBUG("_account_query_account_by_account_id end [%d]", error_code);
1881 GList* _account_query_account_by_user_name(int pid, uid_t uid, const char *user_name, int *error_code)
1883 *error_code = _ACCOUNT_ERROR_NONE;
1884 account_stmt hstmt = NULL;
1885 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1887 account_s *account_head = NULL;
1889 if (user_name == NULL) {
1890 _ERR("USER NAME IS NULL");
1891 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
1895 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1897 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ?", ACCOUNT_TABLE);
1899 hstmt = _account_prepare_query(g_hAccountDB, query);
1901 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1902 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1903 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
1907 int binding_count = 1;
1908 _account_query_bind_text(hstmt, binding_count++, user_name);
1910 rc = _account_query_step(hstmt);
1912 if (rc != SQLITE_ROW) {
1913 _ERR("The record isn't found");
1914 *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1920 account_head = (account_s*) malloc(sizeof(account_s));
1921 if (account_head == NULL) {
1922 ACCOUNT_FATAL("malloc Failed");
1923 if (hstmt != NULL) {
1924 rc = _account_query_finalize(hstmt);
1925 if (rc != _ACCOUNT_ERROR_NONE) {
1926 _ERR("finalize error");
1932 *error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
1935 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
1937 while (rc == SQLITE_ROW) {
1938 account_s* account_record = NULL;
1940 account_record = (account_s*) malloc(sizeof(account_s));
1942 if (account_record == NULL) {
1943 ACCOUNT_FATAL("malloc Failed");
1946 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
1948 _account_convert_column_to_account(hstmt, account_record);
1950 account_head->account_list = g_list_append(account_head->account_list, account_record);
1952 rc = _account_query_step(hstmt);
1956 rc = _account_query_finalize(hstmt);
1958 if (rc != _ACCOUNT_ERROR_NONE) {
1959 _ERR("finalize error");
1968 tmp = g_list_length(account_head->account_list);
1970 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
1971 //account_h account;
1972 //account = (account_h)iter->data;
1974 account_s *testaccount = (account_s*)iter->data;
1976 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, testaccount->id, (void*)testaccount, false);
1977 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, testaccount->id, (void*)testaccount, false);
1980 *error_code = _ACCOUNT_ERROR_NONE;
1983 if (hstmt != NULL) {
1984 rc = _account_query_finalize(hstmt);
1985 if (rc != _ACCOUNT_ERROR_NONE) {
1986 _ERR("finalize error");
1992 pthread_mutex_unlock(&account_mutex);
1995 _remove_sensitive_info_from_non_owning_account_list(account_head->account_list, pid, uid);
1996 GList* result = account_head->account_list;
1997 _ACCOUNT_FREE(account_head);
2005 _account_query_account_by_capability(int pid, uid_t uid, const char* capability_type, const int capability_value, int *error_code)
2007 *error_code = _ACCOUNT_ERROR_NONE;
2008 account_stmt hstmt = NULL;
2009 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2012 ACCOUNT_RETURN_VAL((capability_type != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("capability_type IS NULL"));
2014 if ((capability_value < 0) || (capability_value >= _ACCOUNT_CAPABILITY_STATE_MAX)) {
2015 ACCOUNT_SLOGE("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
2016 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
2020 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), { *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED; }, NULL, ("The database isn't connected."));
2022 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2024 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=? AND value=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
2026 hstmt = _account_prepare_query(g_hAccountDB, query);
2028 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2029 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2030 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
2034 int binding_count = 1;
2035 _account_query_bind_text(hstmt, binding_count++, capability_type);
2036 _account_query_bind_int(hstmt, binding_count++, capability_value);
2038 rc = _account_query_step(hstmt);
2040 account_s* account_head = NULL;
2042 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2046 account_head = (account_s*) malloc(sizeof(account_s));
2047 if (account_head == NULL) {
2048 ACCOUNT_FATAL("malloc Failed");
2049 if (hstmt != NULL) {
2050 rc = _account_query_finalize(hstmt);
2051 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), { *error_code = rc; }, NULL, ("finalize error"));
2054 *error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
2057 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2059 while (rc == SQLITE_ROW) {
2060 account_s* account_record = NULL;
2062 account_record = (account_s*) malloc(sizeof(account_s));
2064 if (account_record == NULL) {
2065 ACCOUNT_FATAL("malloc Failed");
2068 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2070 _account_convert_column_to_account(hstmt, account_record);
2072 account_head->account_list = g_list_append(account_head->account_list, account_record);
2074 rc = _account_query_step(hstmt);
2078 rc = _account_query_finalize(hstmt);
2079 ACCOUNT_CATCH_ERROR_P((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2085 tmp = g_list_length(account_head->account_list);
2087 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2088 //account_h account = NULL;
2089 //account = (account_h)iter->data;
2090 account_s* testaccount = (account_s*)iter->data;
2092 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, testaccount->id, (void*)testaccount, false);
2093 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, testaccount->id, (void*)testaccount, false);
2097 *error_code = _ACCOUNT_ERROR_NONE;
2100 if (hstmt != NULL) {
2101 rc = _account_query_finalize(hstmt);
2102 if (rc != _ACCOUNT_ERROR_NONE) {
2104 _ERR("finalize error");
2109 if (*error_code != _ACCOUNT_ERROR_NONE && account_head) {
2110 _account_glist_account_free(account_head->account_list);
2111 _ACCOUNT_FREE(account_head);
2112 account_head = NULL;
2115 pthread_mutex_unlock(&account_mutex);
2118 _remove_sensitive_info_from_non_owning_account_list(account_head->account_list, pid, uid);
2119 GList* result = account_head->account_list;
2120 _ACCOUNT_FREE(account_head);
2127 GList* _account_query_account_by_capability_type(int pid, uid_t uid, const char* capability_type, int *error_code)
2129 *error_code = _ACCOUNT_ERROR_NONE;
2130 account_stmt hstmt = NULL;
2131 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2134 ACCOUNT_RETURN_VAL((capability_type != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("capability_type IS NULL"));
2135 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), { *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED; },
2136 NULL, ("The database isn't connected."));
2138 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2140 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
2142 hstmt = _account_prepare_query(g_hAccountDB, query);
2144 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2145 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2146 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
2150 int binding_count = 1;
2151 _account_query_bind_text(hstmt, binding_count++, capability_type);
2153 rc = _account_query_step(hstmt);
2155 account_s* account_head = NULL;
2157 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2161 account_head = (account_s*) malloc(sizeof(account_s));
2162 if (account_head == NULL) {
2163 ACCOUNT_FATAL("malloc Failed");
2164 if (hstmt != NULL) {
2165 rc = _account_query_finalize(hstmt);
2166 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), { *error_code = rc; }, NULL, ("finalize error"));
2169 *error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
2172 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2174 while (rc == SQLITE_ROW) {
2175 account_s* account_record = NULL;
2177 account_record = (account_s*) malloc(sizeof(account_s));
2179 if (account_record == NULL) {
2180 ACCOUNT_FATAL("malloc Failed");
2183 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2185 _account_convert_column_to_account(hstmt, account_record);
2187 account_head->account_list = g_list_append(account_head->account_list, account_record);
2189 rc = _account_query_step(hstmt);
2193 rc = _account_query_finalize(hstmt);
2194 ACCOUNT_CATCH_ERROR_P((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2200 tmp = g_list_length(account_head->account_list);
2202 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2203 account_s* testaccount = (account_s*)iter->data;
2205 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, testaccount->id, (void*)testaccount, false);
2206 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, testaccount->id, (void*)testaccount, false);
2210 *error_code = _ACCOUNT_ERROR_NONE;
2213 if (hstmt != NULL) {
2214 rc = _account_query_finalize(hstmt);
2215 if (rc != _ACCOUNT_ERROR_NONE) {
2217 _ERR("finalize error");
2222 if ((*error_code != _ACCOUNT_ERROR_NONE) && account_head) {
2223 _account_glist_account_free(account_head->account_list);
2224 _ACCOUNT_FREE(account_head);
2225 account_head = NULL;
2228 pthread_mutex_unlock(&account_mutex);
2231 _remove_sensitive_info_from_non_owning_account_list(account_head->account_list, pid, uid);
2232 GList* result = account_head->account_list;
2233 _ACCOUNT_FREE(account_head);
2240 GList* account_server_query_account_by_package_name(const char* package_name, int *error_code, int pid, uid_t uid)
2242 _INFO("account_server_query_account_by_package_name start");
2244 GList * account_list = NULL;
2245 *error_code = _ACCOUNT_ERROR_NONE;
2247 ACCOUNT_RETURN_VAL((package_name != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("PACKAGE NAME IS NULL"));
2248 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), { *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED; }, NULL, ("The database isn't connected."));
2250 account_list = _account_query_account_by_package_name(g_hAccountDB, package_name, error_code, pid, uid);
2252 _INFO("account_server_query_account_by_package_name end");
2254 return account_list;
2257 int account_server_delete_account_by_package_name(const char* package_name, bool permission, int pid, uid_t uid)
2259 _INFO("account_db_delete_account_by_package_name");
2261 int error_code = _ACCOUNT_ERROR_NONE;
2263 ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
2264 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2266 error_code = _account_delete_account_by_package_name(g_hAccountDB, package_name, permission, pid, uid);
2268 _INFO("account_server_delete_account_by_package_name end");
2273 int _account_delete(int pid, uid_t uid, int account_id)
2275 int error_code = _ACCOUNT_ERROR_NONE;
2276 account_stmt hstmt = NULL;
2277 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2279 int ret_transaction = 0;
2280 bool is_success = FALSE;
2282 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2285 /* Check requested ID to delete */
2286 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id=%d", ACCOUNT_TABLE, account_id);
2288 count = _account_get_record_count(g_hAccountDB, query);
2290 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2291 pthread_mutex_unlock(&account_mutex);
2292 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2293 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2297 ACCOUNT_ERROR("account id(%d) is not exist. count(%d)\n", account_id, count);
2298 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
2301 /* Check permission of requested appid */
2302 char* current_appid = NULL;
2303 char *package_name = NULL;
2305 current_appid = _account_get_current_appid(pid, uid);
2307 error_code = _account_get_package_name_from_account_id(account_id, &package_name);
2309 if (error_code != _ACCOUNT_ERROR_NONE) {
2310 ACCOUNT_ERROR("No package name with account_id\n");
2311 _ACCOUNT_FREE(current_appid);
2312 _ACCOUNT_FREE(package_name);
2313 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
2315 ACCOUNT_DEBUG("DELETE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name);
2317 error_code = _account_check_appid_group_with_package_name(current_appid, package_name, uid);
2319 _ACCOUNT_FREE(current_appid);
2320 _ACCOUNT_FREE(package_name);
2322 if (error_code != _ACCOUNT_ERROR_NONE) {
2323 ACCOUNT_ERROR("No permission to delete\n");
2324 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2327 /* transaction control required*/
2328 ret_transaction = _account_begin_transaction(g_hAccountDB);
2330 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2331 pthread_mutex_unlock(&account_mutex);
2332 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2333 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2336 if (ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY) {
2337 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
2338 pthread_mutex_unlock(&account_mutex);
2339 return _ACCOUNT_ERROR_DATABASE_BUSY;
2342 if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2343 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
2344 pthread_mutex_unlock(&account_mutex);
2345 return ret_transaction;
2348 /* capability table */
2349 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2350 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
2352 hstmt = _account_prepare_query(g_hAccountDB, query);
2354 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2355 pthread_mutex_unlock(&account_mutex);
2356 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2357 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2360 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2361 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2363 rc = _account_query_step(hstmt);
2364 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2366 rc = _account_query_finalize(hstmt);
2368 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2372 ACCOUNT_MEMSET(query, 0, sizeof(query));
2373 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
2375 hstmt = _account_prepare_query(g_hAccountDB, query);
2376 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2377 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2379 rc = _account_query_step(hstmt);
2380 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
2382 rc = _account_query_finalize(hstmt);
2383 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2386 /* delete custom data */
2387 ACCOUNT_MEMSET(query, 0, sizeof(query));
2388 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
2390 hstmt = _account_prepare_query(g_hAccountDB, query);
2392 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2393 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2395 rc = _account_query_step(hstmt);
2396 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
2398 rc = _account_query_finalize(hstmt);
2399 ACCOUNT_CATCH_ERROR(rc == _ACCOUNT_ERROR_NONE, {}, rc, ("finalize error", account_id, rc));
2405 if (hstmt != NULL) {
2406 rc = _account_query_finalize(hstmt);
2407 if (rc != _ACCOUNT_ERROR_NONE) {
2408 ACCOUNT_ERROR("rc (%d)", rc);
2415 ret_transaction = _account_end_transaction(g_hAccountDB, is_success);
2417 if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2418 ACCOUNT_ERROR("account_delete:_account_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
2420 if (is_success == true) {
2421 char buf[64] = {0,};
2422 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_DELETE, account_id);
2423 _account_insert_delete_update_notification_send(buf);
2427 pthread_mutex_unlock(&account_mutex);
2432 static int _account_query_account_by_username_and_package(const char* username, const char* package_name, account_s *account)
2436 int error_code = _ACCOUNT_ERROR_NONE;
2437 account_stmt hstmt = NULL;
2438 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2440 int binding_count = 1;
2442 ACCOUNT_RETURN_VAL((username != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("username IS NULL"));
2443 ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name IS NULL"));
2444 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2445 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2447 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2449 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
2450 hstmt = _account_prepare_query(g_hAccountDB, query);
2452 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2453 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2454 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2457 _account_query_bind_text(hstmt, binding_count++, username);
2458 _account_query_bind_text(hstmt, binding_count++, package_name);
2460 rc = _account_query_step(hstmt);
2461 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2463 account_s *account_record = account;
2465 while (rc == SQLITE_ROW) {
2466 _account_convert_column_to_account(hstmt, account_record);
2467 rc = _account_query_step(hstmt);
2470 rc = _account_query_finalize(hstmt);
2471 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2472 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, account_record->id, (void*)account_record, false);
2473 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, account_record->id, (void*)account_record, false);
2476 error_code = _ACCOUNT_ERROR_NONE;
2479 if (hstmt != NULL) {
2480 rc = _account_query_finalize(hstmt);
2481 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2485 pthread_mutex_unlock(&account_mutex);
2490 int _account_create(account_s **account)
2493 ACCOUNT_SLOGE("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__);
2494 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2497 account_s *data = (account_s*)malloc(sizeof(account_s));
2500 ACCOUNT_FATAL("Memory Allocation Failed");
2501 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
2503 ACCOUNT_MEMSET(data, 0, sizeof(account_s));
2505 /*Setting account as visible by default*/
2506 //data->secret = _ACCOUNT_SECRECY_VISIBLE;
2508 /*Setting account as not supporting sync by default*/
2509 //data->sync_support = _ACCOUNT_SYNC_NOT_SUPPORT;
2513 return _ACCOUNT_ERROR_NONE;
2516 int _account_destroy(account_s *account)
2518 account_s *data = account;
2520 ACCOUNT_RETURN_VAL((data != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Account handle is null!"));
2522 _account_free_account_with_items(data);
2524 return _ACCOUNT_ERROR_NONE;
2527 int _account_get_account_id(account_s* account, int *account_id)
2530 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2533 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2535 *account_id = account->id;
2537 return _ACCOUNT_ERROR_NONE;
2540 int _account_delete_from_db_by_user_name(int pid, uid_t uid, const char *user_name, const char *package_name)
2542 _INFO("[%s][%s]", user_name, package_name);
2544 int error_code = _ACCOUNT_ERROR_NONE;
2545 account_stmt hstmt = NULL;
2546 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2548 int ret_transaction = 0;
2549 bool is_success = FALSE;
2550 account_s *account = NULL;
2551 int binding_count = 1;
2552 int account_id = -1;
2554 ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is null!"));
2555 ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
2556 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2558 /* Check permission of requested appid */
2559 char* current_appid = NULL;
2560 char* package_name_temp = NULL;
2562 current_appid = _account_get_current_appid(pid, uid);
2564 package_name_temp = _account_dup_text(package_name);
2566 ACCOUNT_DEBUG("DELETE:user_name[%s],current_appid[%s], package_name[%s]", user_name, current_appid, package_name_temp);
2568 error_code = _account_check_appid_group_with_package_name(current_appid, package_name_temp, uid);
2570 _ACCOUNT_FREE(current_appid);
2571 _ACCOUNT_FREE(package_name_temp);
2573 if (error_code != _ACCOUNT_ERROR_NONE) {
2574 ACCOUNT_ERROR("No permission to delete\n");
2575 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2578 rc = _account_create(&account);
2579 rc = _account_query_account_by_username_and_package(user_name, package_name, account);
2583 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2584 _account_destroy(account);
2585 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2586 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2590 account_s* account_data = (account_s*)account;
2592 rc = _account_get_account_id(account_data, &account_id);
2594 rc = _account_destroy(account);
2596 /* transaction control required*/
2597 ret_transaction = _account_begin_transaction(g_hAccountDB);
2599 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2600 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2601 pthread_mutex_unlock(&account_mutex);
2602 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2606 if (ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY) {
2607 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
2608 pthread_mutex_unlock(&account_mutex);
2609 return _ACCOUNT_ERROR_DATABASE_BUSY;
2610 } else if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2611 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
2612 pthread_mutex_unlock(&account_mutex);
2613 return ret_transaction;
2616 /* delete custom data */
2617 ACCOUNT_MEMSET(query, 0, sizeof(query));
2618 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = ?", ACCOUNT_CUSTOM_TABLE);
2620 hstmt = _account_prepare_query(g_hAccountDB, query);
2622 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2623 _account_end_transaction(g_hAccountDB, FALSE);
2624 pthread_mutex_unlock(&account_mutex);
2625 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2626 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2629 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2630 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2632 _account_query_bind_int(hstmt, binding_count++, account_id);
2634 rc = _account_query_step(hstmt);
2635 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2637 rc = _account_query_finalize(hstmt);
2638 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2641 /* delete capability */
2642 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", CAPABILITY_TABLE);
2644 hstmt = _account_prepare_query(g_hAccountDB, query);
2646 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2647 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2650 _account_query_bind_text(hstmt, binding_count++, user_name);
2651 _account_query_bind_text(hstmt, binding_count++, package_name);
2653 rc = _account_query_step(hstmt);
2654 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2656 rc = _account_query_finalize(hstmt);
2657 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2660 ACCOUNT_MEMSET(query, 0, sizeof(query));
2663 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
2665 hstmt = _account_prepare_query(g_hAccountDB, query);
2666 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2667 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2671 _account_query_bind_text(hstmt, binding_count++, user_name);
2672 _account_query_bind_text(hstmt, binding_count++, package_name);
2674 rc = _account_query_step(hstmt);
2675 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));
2677 rc = _account_query_finalize(hstmt);
2678 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2684 if (hstmt != NULL) {
2685 rc = _account_query_finalize(hstmt);
2686 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2690 ret_transaction = _account_end_transaction(g_hAccountDB, is_success);
2692 if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2693 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
2695 if (is_success == true) {
2696 char buf[64] = {0,};
2697 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_DELETE, account_id);
2698 _account_insert_delete_update_notification_send(buf);
2702 pthread_mutex_unlock(&account_mutex);
2708 int _account_get_total_count_from_db(gboolean include_hidden, int *count)
2711 ACCOUNT_SLOGE("(%s)-(%d) count is NULL.\n", __FUNCTION__, __LINE__);
2712 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2715 if (!g_hAccountDB) {
2716 ACCOUNT_ERROR("DB is not opened\n");
2717 return _ACCOUNT_ERROR_DB_NOT_OPENED;
2720 char query[1024] = {0, };
2721 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2724 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s", ACCOUNT_TABLE);
2726 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s where secret = %d", ACCOUNT_TABLE, _ACCOUNT_SECRECY_VISIBLE);
2728 *count = _account_get_record_count(g_hAccountDB, query);
2730 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2731 pthread_mutex_unlock(&account_mutex);
2732 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2733 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2738 account_stmt pStmt = NULL;
2740 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
2741 if (SQLITE_OK != rc) {
2742 ACCOUNT_SLOGE("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg(g_hAccountDB));
2743 sqlite3_finalize(pStmt);
2744 return _ACCOUNT_ERROR_DB_FAILED;
2747 rc = sqlite3_step(pStmt);
2748 if (SQLITE_ROW != rc) {
2749 ACCOUNT_ERROR("[ERROR] sqlite3_step() failed\n");
2750 sqlite3_finalize(pStmt);
2751 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
2754 ncount = sqlite3_column_int(pStmt, 0);
2758 sqlite3_finalize(pStmt);
2761 ACCOUNT_ERROR("[ERROR] Number of account : %d, End", ncount);
2762 return _ACCOUNT_ERROR_DB_FAILED;
2765 return _ACCOUNT_ERROR_NONE;
2769 int account_server_query_app_id_exist(const char* app_id)
2771 _INFO("account_server_query_app_id_exist start app_id=[%s]", app_id);
2772 int ret = _ACCOUNT_ERROR_NONE;
2774 ret = _account_type_query_app_id_exist_from_all_db(g_hAccountDB, g_hAccountGlobalDB, app_id);
2776 _INFO("account_server_query_app_id_exist end error_code=[%d]", ret);
2781 int account_server_insert_account_type_to_user_db(account_type_s *account_type, int *account_type_id, uid_t uid)
2783 ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE HANDLE IS NULL"));
2784 ACCOUNT_RETURN_VAL((account_type->app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID OF ACCOUNT TYPE IS NULL"));
2785 ACCOUNT_RETURN_VAL((account_type_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE ID IS NULL"));
2787 _INFO("account_server_insert_account_type_to_user_db start uid=[%d]", uid);
2788 int ret = _ACCOUNT_ERROR_NONE;
2790 if (_account_type_check_duplicated(g_hAccountDB, account_type->app_id) ||
2791 _account_type_check_duplicated(g_hAccountGlobalDB, account_type->app_id)) {
2792 *account_type_id = -1;
2793 return _ACCOUNT_ERROR_DUPLICATED;
2796 ret = _account_type_insert_to_db(g_hAccountDB, account_type, account_type_id);
2797 _INFO("account_server_insert_account_type_to_user_db end error_code=[%d]", ret);
2802 int account_server_delete_account_type_by_app_id_from_user_db(const char * app_id)
2804 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID OF ACCOUNT TYPE IS NULL"));
2806 _INFO("account_server_delete_account_type_by_app_id_from_user_db start");
2807 int ret = _ACCOUNT_ERROR_NONE;
2809 ret = _account_type_delete_by_app_id(g_hAccountDB, app_id);
2810 _INFO("account_server_delete_account_type_by_app_id_from_user_db end error_code=[%d]", ret);
2815 GSList* _account_type_query_provider_feature_by_app_id_from_global_db(const char* app_id, int *error_code)
2817 _INFO("_account_type_query_provider_feature_by_app_id_in_global_db app_id=%s", app_id);
2818 account_stmt hstmt = NULL;
2819 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2820 int rc = 0, binding_count = 1;
2821 GSList* feature_list = NULL;
2823 ACCOUNT_RETURN_VAL((app_id != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("APP ID IS NULL"));
2824 ACCOUNT_RETURN_VAL((error_code != NULL), {_ERR("error_code pointer is NULL"); }, NULL, (""));
2825 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; _ERR("The database isn't connected."); }, NULL, ("The database isn't connected."));
2827 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2829 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
2830 _INFO("account query=[%s]", query);
2832 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
2834 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
2835 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2836 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
2840 _INFO("before _account_query_bind_text");
2841 _account_query_bind_text(hstmt, binding_count++, app_id);
2843 rc = _account_query_step(hstmt);
2845 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"));
2847 provider_feature_s* feature_record = NULL;
2849 while (rc == SQLITE_ROW) {
2851 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
2853 if (feature_record == NULL) {
2854 ACCOUNT_FATAL("malloc Failed");
2858 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
2860 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
2862 _INFO("Adding account feature_list");
2863 feature_list = g_slist_append(feature_list, feature_record);
2865 rc = _account_query_step(hstmt);
2868 *error_code = _ACCOUNT_ERROR_NONE;
2871 if (hstmt != NULL) {
2872 rc = _account_query_finalize(hstmt);
2873 if (rc != _ACCOUNT_ERROR_NONE) {
2875 _ERR("global db fianlize error");
2879 if (*error_code != _ACCOUNT_ERROR_NONE)
2880 _account_type_gslist_feature_free(feature_list);
2882 _INFO("Returning account feature_list from global db");
2884 return feature_list;
2887 GSList* _account_type_query_provider_feature_by_app_id(const char* app_id, int *error_code)
2889 _INFO("_account_type_query_provider_feature_by_app_id app_id=%s", app_id);
2890 account_stmt hstmt = NULL;
2891 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2892 int rc = 0, binding_count = 1;
2893 GSList* feature_list = NULL;
2895 ACCOUNT_RETURN_VAL((app_id != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("APP ID IS NULL"));
2896 ACCOUNT_RETURN_VAL((error_code != NULL), {_ERR("error_code pointer is NULL"); }, NULL, (""));
2897 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), { *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED; }, NULL, ("The database isn't connected."));
2899 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2901 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
2902 _INFO("account query=[%s]", query);
2904 hstmt = _account_prepare_query(g_hAccountDB, query);
2906 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2907 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2908 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
2912 _account_query_bind_text(hstmt, binding_count++, app_id);
2914 rc = _account_query_step(hstmt);
2916 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, { *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
2917 _ERR("The record isn't found from user db. rc=[%d]", rc); },
2918 _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2920 provider_feature_s* feature_record = NULL;
2922 while (rc == SQLITE_ROW) {
2924 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
2926 if (feature_record == NULL) {
2927 ACCOUNT_FATAL("malloc Failed");
2931 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
2933 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
2935 _INFO("Adding account feature_list");
2936 feature_list = g_slist_append(feature_list, feature_record);
2938 rc = _account_query_step(hstmt);
2941 *error_code = _ACCOUNT_ERROR_NONE;
2943 rc = _account_query_finalize(hstmt);
2944 ACCOUNT_CATCH_ERROR_P((rc == _ACCOUNT_ERROR_NONE), { *error_code = rc; }, rc, ("account finalize error"));
2948 if (hstmt != NULL) {
2949 rc = _account_query_finalize(hstmt);
2950 if (rc != _ACCOUNT_ERROR_NONE) {
2952 _ERR("account fianlize error");
2956 _INFO("*error_code=[%d]", *error_code);
2958 if (*error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
2959 feature_list = _account_type_query_provider_feature_by_app_id_from_global_db(app_id, error_code);
2961 if (*error_code != _ACCOUNT_ERROR_NONE) {
2962 _account_type_gslist_feature_free(feature_list);
2966 _INFO("Returning account feature_list");
2968 return feature_list;
2971 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)
2973 int error_code = _ACCOUNT_ERROR_NONE;
2974 account_stmt hstmt = NULL;
2975 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2976 int rc = 0, binding_count = 1;
2978 _INFO("_account_type_query_provider_feature_cb_by_app_id_in_global_db start app_id=%s", app_id);
2979 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
2980 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
2981 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2983 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2985 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
2986 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
2988 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
2989 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
2990 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_PERMISSION_DENIED, ("global db permission denied.\n"));
2993 _account_query_bind_text(hstmt, binding_count++, app_id);
2995 rc = _account_query_step(hstmt);
2996 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"));
2998 provider_feature_s* feature_record = NULL;
3000 while (rc == SQLITE_ROW) {
3001 bool cb_ret = FALSE;
3002 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
3004 if (feature_record == NULL) {
3005 ACCOUNT_FATAL("malloc Failed");
3009 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
3011 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
3013 cb_ret = callback(feature_record->app_id, feature_record->key, user_data);
3015 _account_type_free_feature_with_items(feature_record);
3017 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returns FALSE, its iteration is stopped!!!!\n"));
3019 rc = _account_query_step(hstmt);
3022 error_code = _ACCOUNT_ERROR_NONE;
3025 if (hstmt != NULL) {
3026 rc = _account_query_finalize(hstmt);
3027 if (rc != _ACCOUNT_ERROR_NONE) {
3029 _ERR("global db finalize error[%d]", rc);
3034 _INFO("_account_type_query_provider_feature_cb_by_app_id_in_global_db end. error_code=[%d]", error_code);
3039 int _account_type_query_provider_feature_cb_by_app_id(account_type_provider_feature_cb callback, const char* app_id, void *user_data)
3041 int error_code = _ACCOUNT_ERROR_NONE;
3042 account_stmt hstmt = NULL;
3043 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3044 int rc = 0, binding_count = 1;
3046 _INFO("_account_type_query_provider_feature_cb_by_app_id start app_id=%s", app_id);
3047 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3048 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3049 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3051 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3053 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
3054 hstmt = _account_prepare_query(g_hAccountDB, query);
3056 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
3057 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3058 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3061 _account_query_bind_text(hstmt, binding_count++, app_id);
3063 rc = _account_query_step(hstmt);
3064 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found in user db.\n"));
3066 provider_feature_s* feature_record = NULL;
3068 while (rc == SQLITE_ROW) {
3069 bool cb_ret = FALSE;
3070 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
3072 if (feature_record == NULL) {
3073 ACCOUNT_FATAL("malloc Failed");
3077 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
3079 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
3081 cb_ret = callback(feature_record->app_id, feature_record->key, user_data);
3083 _account_type_free_feature_with_items(feature_record);
3085 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
3087 rc = _account_query_step(hstmt);
3090 rc = _account_query_finalize(hstmt);
3091 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3094 error_code = _ACCOUNT_ERROR_NONE;
3097 if (hstmt != NULL) {
3098 rc = _account_query_finalize(hstmt);
3099 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3103 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3104 error_code = _account_type_query_provider_feature_cb_by_app_id_from_global_db(callback, app_id, user_data);
3106 _INFO("_account_type_query_provider_feature_cb_by_app_id end");
3111 int account_type_query_provider_feature_cb_by_app_id(account_type_provider_feature_cb callback, const char* app_id, void *user_data)
3113 int error_code = _ACCOUNT_ERROR_NONE;
3115 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3116 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3117 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3119 error_code = _account_type_query_provider_feature_cb_by_app_id(callback, app_id, user_data);
3121 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3122 error_code = _account_type_query_provider_feature_cb_by_app_id_from_global_db(callback, app_id, user_data);
3127 bool _account_type_query_supported_feature_from_global_db(const char* app_id, const char* capability, int *error_code)
3129 _INFO("_account_type_query_supported_feature_in_global_db start");
3130 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3132 *error_code = _ACCOUNT_ERROR_NONE;
3134 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3135 int record_count = 0;
3137 if (app_id == NULL || capability == NULL) {
3138 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
3142 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
3144 record_count = _account_get_record_count(g_hAccountGlobalDB, query);
3146 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3147 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3148 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3151 if (record_count <= 0) {
3152 *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
3156 _INFO("_account_type_query_supported_feature_in_global_db end");
3161 bool _account_type_query_supported_feature(const char* app_id, const char* capability, int *error_code)
3163 _INFO("_account_type_query_supported_feature start");
3165 *error_code = _ACCOUNT_ERROR_NONE;
3167 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3168 int record_count = 0;
3170 if (app_id == NULL || capability == NULL) {
3171 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
3175 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
3177 record_count = _account_get_record_count(g_hAccountDB, query);
3179 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
3180 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3181 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3185 if (record_count <= 0) {
3186 bool is_exist = false;
3187 is_exist = _account_type_query_supported_feature_from_global_db(app_id, capability, error_code);
3192 _INFO("_account_type_query_supported_feature end");
3198 static int _account_type_update_provider_feature(sqlite3 *account_db_handle, account_type_s *account_type, const char* app_id)
3201 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3202 account_stmt hstmt = NULL;
3204 ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
3206 if (g_slist_length(account_type->provider_feature_list) == 0) {
3207 ACCOUNT_ERROR("no feature\n");
3208 return _ACCOUNT_ERROR_NONE;
3211 ACCOUNT_DEBUG("app id", app_id);
3213 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3215 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id=? ", PROVIDER_FEATURE_TABLE);
3216 hstmt = _account_prepare_query(account_db_handle, query);
3218 if (_account_db_err_code(account_db_handle) == SQLITE_PERM) {
3219 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(account_db_handle));
3220 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3224 _account_query_bind_text(hstmt, count++, app_id);
3225 rc = _account_query_step(hstmt);
3227 if (rc != SQLITE_DONE) {
3228 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3229 if (_account_query_finalize(hstmt) != _ACCOUNT_ERROR_NONE)
3230 _ERR("finalize error - account_query_step() failed");
3231 return _ACCOUNT_ERROR_DB_FAILED;
3233 rc = _account_query_finalize(hstmt);
3234 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3239 for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
3242 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3243 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
3244 "(?, ?) ", PROVIDER_FEATURE_TABLE);
3246 hstmt = _account_prepare_query(account_db_handle, query);
3248 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg(account_db_handle)));
3250 provider_feature_s* feature_data = NULL;
3251 feature_data = (provider_feature_s*)iter->data;
3253 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
3254 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3255 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
3256 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3258 rc = _account_query_step(hstmt);
3260 if (rc != SQLITE_DONE) {
3261 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3264 rc = _account_query_finalize(hstmt);
3265 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3269 return _ACCOUNT_ERROR_NONE;
3272 static int _account_type_update_label(sqlite3 *account_db_handle, account_type_s *account_type, const char* app_id)
3275 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3276 account_stmt hstmt = NULL;
3278 ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
3280 if (g_slist_length(account_type->label_list) == 0)
3281 return _ACCOUNT_ERROR_NONE;
3283 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3285 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId=? ", LABEL_TABLE);
3286 hstmt = _account_prepare_query(account_db_handle, query);
3288 if (_account_db_err_code(account_db_handle) == SQLITE_PERM) {
3289 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(account_db_handle));
3290 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3294 _account_query_bind_text(hstmt, count++, app_id);
3295 rc = _account_query_step(hstmt);
3297 if (rc != SQLITE_DONE) {
3298 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3299 if (_account_query_finalize(hstmt) != _ACCOUNT_ERROR_NONE)
3300 _ERR("finalize error - account_query_step() failed");
3301 return _ACCOUNT_ERROR_DB_FAILED;
3303 rc = _account_query_finalize(hstmt);
3304 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3309 for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
3312 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3313 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
3314 "(?, ?, ?) ", LABEL_TABLE);
3316 hstmt = _account_prepare_query(account_db_handle, query);
3318 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg(account_db_handle)));
3320 label_s* label_data = NULL;
3321 label_data = (label_s*)iter->data;
3323 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
3324 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3325 ret = _account_query_bind_text(hstmt, count++, label_data->label);
3326 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3327 ret = _account_query_bind_text(hstmt, count++, label_data->locale);
3328 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3330 rc = _account_query_step(hstmt);
3332 if (rc != SQLITE_DONE) {
3333 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3336 rc = _account_query_finalize(hstmt);
3337 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3341 return _ACCOUNT_ERROR_NONE;
3345 static int _account_type_update_account(sqlite3 *account_db_handle, account_type_s *account_type, const char* app_id)
3347 int rc = 0, binding_count = 1;
3348 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3349 int error_code = _ACCOUNT_ERROR_NONE;
3350 account_stmt hstmt = NULL;
3352 if (!account_type->app_id) {
3353 ACCOUNT_ERROR("app id is mandetory field, it can not be NULL!!!!\n");
3354 return _ACCOUNT_ERROR_INVALID_PARAMETER;
3357 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3358 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET AppId=?, ServiceProviderId=?, IconPath=?, "
3359 "SmallIconPath=?, MultipleAccountSupport=? WHERE AppId=? ", ACCOUNT_TYPE_TABLE);
3361 hstmt = _account_prepare_query(account_db_handle, query);
3363 if (_account_db_err_code(account_db_handle) == SQLITE_PERM) {
3364 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(account_db_handle));
3365 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3366 } else if (_account_db_err_code(account_db_handle) == SQLITE_BUSY) {
3367 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(account_db_handle));
3368 return _ACCOUNT_ERROR_DATABASE_BUSY;
3371 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg(account_db_handle)));
3373 binding_count = _account_type_convert_account_to_sql(account_type, hstmt, query);
3374 _account_query_bind_text(hstmt, binding_count++, app_id);
3376 rc = _account_query_step(hstmt);
3377 if (rc != SQLITE_DONE)
3378 ACCOUNT_ERROR("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3380 rc = _account_query_finalize(hstmt);
3381 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3385 error_code = _account_type_update_label(account_db_handle, account_type, app_id);
3386 /* update provider feature */
3387 error_code = _account_type_update_provider_feature(account_db_handle, account_type, app_id);
3392 int _account_type_update_to_db_by_app_id(account_type_s* account_type, const char* app_id)
3394 int error_code = _ACCOUNT_ERROR_NONE;
3396 ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
3397 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3398 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3400 account_type_s* data = account_type;
3402 pthread_mutex_lock(&account_mutex);
3404 error_code = _account_type_update_account(g_hAccountDB, data, app_id);
3406 pthread_mutex_unlock(&account_mutex);
3411 GSList* _account_type_get_label_list_by_app_id_from_global_db(const char* app_id, int *error_code)
3413 *error_code = _ACCOUNT_ERROR_NONE;
3414 account_stmt hstmt = NULL;
3415 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3416 int rc = 0, binding_count = 1;
3417 GSList* label_list = NULL;
3419 ACCOUNT_RETURN_VAL((app_id != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("APP ID IS NULL"));
3420 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3422 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3424 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3425 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3427 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3428 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3429 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3434 _account_query_bind_text(hstmt, binding_count++, app_id);
3436 rc = _account_query_step(hstmt);
3437 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"));
3439 label_s* label_record = NULL;
3441 while (rc == SQLITE_ROW) {
3442 label_record = (label_s*) malloc(sizeof(label_s));
3444 if (label_record == NULL) {
3445 ACCOUNT_FATAL("malloc Failed");
3449 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3451 _account_type_convert_column_to_label(hstmt, label_record);
3453 _INFO("Adding account label_list");
3454 label_list = g_slist_append(label_list, label_record);
3456 rc = _account_query_step(hstmt);
3459 *error_code = _ACCOUNT_ERROR_NONE;
3462 if (hstmt != NULL) {
3463 rc = _account_query_finalize(hstmt);
3464 if (rc != _ACCOUNT_ERROR_NONE)
3465 _ERR("global db finalize error[%d]", rc);
3469 _INFO("Returning account global label_list");
3474 GSList* _account_type_get_label_list_by_app_id(const char* app_id, int *error_code)
3476 *error_code = _ACCOUNT_ERROR_NONE;
3477 account_stmt hstmt = NULL;
3478 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3479 int rc = 0, binding_count = 1;
3480 GSList* label_list = NULL;
3482 ACCOUNT_RETURN_VAL((app_id != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("APP ID IS NULL"));
3483 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), { *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED; }, NULL, ("The database isn't connected."));
3485 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3487 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3488 hstmt = _account_prepare_query(g_hAccountDB, query);
3490 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
3491 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3492 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3496 _account_query_bind_text(hstmt, binding_count++, app_id);
3498 rc = _account_query_step(hstmt);
3499 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3501 label_s* label_record = NULL;
3503 while (rc == SQLITE_ROW) {
3504 label_record = (label_s*) malloc(sizeof(label_s));
3506 if (label_record == NULL) {
3507 ACCOUNT_FATAL("malloc Failed");
3511 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3513 _account_type_convert_column_to_label(hstmt, label_record);
3515 _INFO("Adding account label_list");
3516 label_list = g_slist_append(label_list, label_record);
3518 rc = _account_query_step(hstmt);
3521 rc = _account_query_finalize(hstmt);
3522 if (rc != _ACCOUNT_ERROR_NONE) {
3523 _account_type_gslist_label_free(label_list);
3525 _ERR("finalize error");
3530 *error_code = _ACCOUNT_ERROR_NONE;
3533 if (hstmt != NULL) {
3534 rc = _account_query_finalize(hstmt);
3535 if (rc != _ACCOUNT_ERROR_NONE) {
3536 _account_type_gslist_label_free(label_list);
3538 _ERR("finalize error");
3544 if (*error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3545 label_list = _account_type_get_label_list_by_app_id_from_global_db(app_id, error_code);
3547 _INFO("Returning account label_list");
3552 int _account_type_query_label_by_app_id_from_global_db(account_type_label_cb callback, const char* app_id, void *user_data)
3554 int error_code = _ACCOUNT_ERROR_NONE;
3555 account_stmt hstmt = NULL;
3556 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3557 int rc = 0, binding_count = 1;
3559 _INFO("account_type_query_label_by_app_id_from_global_db start");
3561 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3562 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3563 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3565 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3567 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3568 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3570 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3571 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3572 error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3576 _account_query_bind_text(hstmt, binding_count++, app_id);
3578 rc = _account_query_step(hstmt);
3579 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3581 label_s* label_record = NULL;
3583 while (rc == SQLITE_ROW) {
3584 bool cb_ret = FALSE;
3585 label_record = (label_s*) malloc(sizeof(label_s));
3587 if (label_record == NULL) {
3588 ACCOUNT_FATAL("malloc Failed");
3592 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3594 _account_type_convert_column_to_label(hstmt, label_record);
3596 cb_ret = callback(label_record->app_id, label_record->label , label_record->locale, user_data);
3598 _account_type_free_label_with_items(label_record);
3600 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
3602 rc = _account_query_step(hstmt);
3605 error_code = _ACCOUNT_ERROR_NONE;
3608 if (hstmt != NULL) {
3609 rc = _account_query_finalize(hstmt);
3610 if (rc != _ACCOUNT_ERROR_NONE)
3611 _ERR("global db finalize error[%d]", rc);
3615 _INFO("account_type_query_label_by_app_id_from_global_db end [%d]", error_code);
3620 int _account_type_query_label_by_app_id(account_type_label_cb callback, const char* app_id, void *user_data)
3622 int error_code = _ACCOUNT_ERROR_NONE;
3623 account_stmt hstmt = NULL;
3624 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3625 int rc = 0, binding_count = 1;
3627 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3628 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3629 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3631 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3633 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3634 hstmt = _account_prepare_query(g_hAccountDB, query);
3636 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
3637 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3638 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3641 _account_query_bind_text(hstmt, binding_count++, app_id);
3643 rc = _account_query_step(hstmt);
3644 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3646 label_s* label_record = NULL;
3648 while (rc == SQLITE_ROW) {
3649 bool cb_ret = FALSE;
3650 label_record = (label_s*) malloc(sizeof(label_s));
3652 if (label_record == NULL) {
3653 ACCOUNT_FATAL("malloc Failed");
3657 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3659 _account_type_convert_column_to_label(hstmt, label_record);
3661 cb_ret = callback(label_record->app_id, label_record->label , label_record->locale, user_data);
3663 _account_type_free_label_with_items(label_record);
3665 //ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
3666 if (cb_ret != TRUE) {
3667 _INFO("Callback func returs FALSE, its iteration is stopped!!!!\n");
3671 rc = _account_query_step(hstmt);
3674 rc = _account_query_finalize(hstmt);
3675 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3678 error_code = _ACCOUNT_ERROR_NONE;
3681 if (hstmt != NULL) {
3682 rc = _account_query_finalize(hstmt);
3683 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3687 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
3688 error_code = account_type_query_label_by_app_id_from_global_db(callback, app_id, user_data);
3694 int account_type_query_label_by_app_id(account_type_label_cb callback, const char* app_id, void *user_data)
3696 int error_code = _ACCOUNT_ERROR_NONE;
3698 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3699 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3700 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3702 error_code = _account_type_query_label_by_app_id(callback, app_id, user_data);
3704 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3705 error_code = _account_type_query_label_by_app_id_from_global_db(callback, app_id, user_data);
3710 bool _account_get_label_text_cb(char* app_id, char* label, char* locale, void *user_data)
3712 account_type_s *data = (account_type_s*)user_data;
3714 label_s *label_data = (label_s*)malloc(sizeof(label_s));
3716 if (label_data == NULL) {
3717 ACCOUNT_FATAL("_account_get_label_text_cb : MALLOC FAIL\n");
3720 ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
3722 label_data->app_id = _account_dup_text(app_id);
3723 label_data->label = _account_dup_text(label);
3724 label_data->locale = _account_dup_text(locale);
3726 data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
3731 bool _account_get_provider_feature_cb(char* app_id, char* key, void* user_data)
3733 account_type_s *data = (account_type_s*)user_data;
3735 provider_feature_s *feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
3737 if (feature_data == NULL) {
3738 ACCOUNT_FATAL("_account_get_provider_feature_cb : MALLOC FAIL\n");
3741 ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
3743 feature_data->app_id = _account_dup_text(app_id);
3744 feature_data->key = _account_dup_text(key);
3746 data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
3751 int _account_type_query_by_app_id_from_global_db(const char* app_id, account_type_s** account_type_record)
3753 _INFO("_account_type_query_by_app_id_from_global_db start");
3755 int error_code = _ACCOUNT_ERROR_NONE;
3756 account_stmt hstmt = NULL;
3757 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3758 int rc = 0, binding_count = 1;
3760 ACCOUNT_RETURN_VAL((app_id != 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3761 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3762 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3764 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3766 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
3767 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3769 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3770 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3771 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3774 _account_query_bind_text(hstmt, binding_count++, app_id);
3776 rc = _account_query_step(hstmt);
3777 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3779 *account_type_record = create_empty_account_type_instance();
3781 while (rc == SQLITE_ROW) {
3782 _account_type_convert_column_to_account_type(hstmt, *account_type_record);
3783 rc = _account_query_step(hstmt);
3786 rc = _account_query_finalize(hstmt);
3787 ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {_ERR("global db finalize error rc=[%d]", rc); }, rc, ("finalize error"));
3788 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb, app_id, (void*)(*account_type_record));
3789 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, app_id, (void*)(*account_type_record));
3792 error_code = _ACCOUNT_ERROR_NONE;
3795 if (hstmt != NULL) {
3796 rc = _account_query_finalize(hstmt);
3797 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3801 _INFO("_account_type_query_by_app_id_from_global_db end [%d]", error_code);
3806 int _account_type_query_by_app_id(const char* app_id, account_type_s** account_type_record)
3808 _INFO("_account_type_query_by_app_id start");
3810 int error_code = _ACCOUNT_ERROR_NONE;
3811 account_stmt hstmt = NULL;
3812 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3813 int rc = 0, binding_count = 1;
3815 ACCOUNT_RETURN_VAL((app_id != 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3816 ACCOUNT_RETURN_VAL((account_type_record != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("account type record(account_type_s**) is NULL"));
3817 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3819 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3821 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
3822 hstmt = _account_prepare_query(g_hAccountDB, query);
3824 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
3825 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3826 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3829 _account_query_bind_text(hstmt, binding_count++, app_id);
3831 rc = _account_query_step(hstmt);
3832 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3834 *account_type_record = create_empty_account_type_instance();
3835 if (*account_type_record == NULL) {
3836 _ERR("Out of Memory");
3837 error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
3841 while (rc == SQLITE_ROW) {
3842 _account_type_convert_column_to_account_type(hstmt, *account_type_record);
3843 rc = _account_query_step(hstmt);
3846 rc = _account_query_finalize(hstmt);
3847 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3848 _account_type_query_label_by_app_id(_account_get_label_text_cb, app_id, (void*)(*account_type_record));
3849 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, app_id, (void*)(*account_type_record));
3852 error_code = _ACCOUNT_ERROR_NONE;
3855 if (hstmt != NULL) {
3856 rc = _account_query_finalize(hstmt);
3857 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3861 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3862 error_code = _account_type_query_by_app_id_from_global_db(app_id, account_type_record);
3864 _INFO("_account_type_query_by_app_id end [%d]", error_code);
3869 int _account_type_query_by_provider_feature_from_global_db(const char* key, GSList **account_type_list_all)
3871 int error_code = _ACCOUNT_ERROR_NONE;
3872 account_stmt hstmt = NULL;
3873 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3875 GSList *account_type_list = NULL;
3877 _INFO("_account_type_query_by_provider_feature_from_global_db start key=%s", key);
3879 ACCOUNT_ERROR("capability_type IS NULL.");
3880 error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
3884 if (g_hAccountGlobalDB == NULL) {
3885 ACCOUNT_ERROR("The database isn't connected.");
3886 error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;
3890 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3892 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
3894 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3896 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3897 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3898 error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3902 int binding_count = 1;
3903 _account_query_bind_text(hstmt, binding_count++, key);
3905 rc = _account_query_step(hstmt);
3907 account_type_s *account_type_record = NULL;
3909 if (rc != SQLITE_ROW) {
3910 ACCOUNT_ERROR("The record isn't found. rc=[%d]", rc);
3911 error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
3915 while (rc == SQLITE_ROW) {
3916 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
3918 if (account_type_record == NULL) {
3919 ACCOUNT_FATAL("malloc Failed");
3923 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
3924 _account_type_convert_column_to_account_type(hstmt, account_type_record);
3925 account_type_list = g_slist_append(account_type_list, account_type_record);
3926 rc = _account_query_step(hstmt);
3929 rc = _account_query_finalize(hstmt);
3930 if (rc != _ACCOUNT_ERROR_NONE) {
3931 _account_type_gslist_account_type_free(account_type_list);
3932 ACCOUNT_ERROR("finalize error(%s)", rc);
3940 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
3941 account_type_s *account_type = NULL;
3942 account_type = (account_type_s*)iter->data;
3943 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb, account_type->app_id, (void*)account_type);
3944 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, account_type->app_id, (void*)account_type);
3945 _INFO("add label & provider_feature");
3948 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
3950 account_type_s *account_type = NULL;
3951 account_type = (account_type_s*)iter->data;
3952 *account_type_list_all = g_slist_append(*account_type_list_all, account_type);
3953 _INFO("add account_type");
3955 g_slist_free(account_type_list);
3957 error_code = _ACCOUNT_ERROR_NONE;
3960 if (hstmt != NULL) {
3961 rc = _account_query_finalize(hstmt);
3962 if (rc != _ACCOUNT_ERROR_NONE) {
3963 ACCOUNT_ERROR("finalize error(%s)", rc);
3969 _INFO("_account_type_query_by_provider_feature_from_global_db end. error_code=[%d]", error_code);
3974 GSList* _account_type_query_by_provider_feature(const char* key, int *error_code)
3976 *error_code = _ACCOUNT_ERROR_NONE;
3977 account_stmt hstmt = NULL;
3978 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3980 GSList *account_type_list = NULL;
3982 _INFO("account_type_query_by_provider_feature start key=%s", key);
3984 ACCOUNT_ERROR("capability_type IS NULL.");
3985 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
3989 if (g_hAccountDB == NULL) {
3990 ACCOUNT_ERROR("The database isn't connected.");
3991 *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;
3995 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3997 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
3999 hstmt = _account_prepare_query(g_hAccountDB, query);
4001 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4002 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4003 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
4007 int binding_count = 1;
4008 _account_query_bind_text(hstmt, binding_count++, key);
4010 rc = _account_query_step(hstmt);
4012 account_type_s *account_type_record = NULL;
4014 if (rc != SQLITE_ROW) {
4015 ACCOUNT_ERROR("The record isn't found. rc=[%d]", rc);
4016 *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4020 while (rc == SQLITE_ROW) {
4021 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4023 if (account_type_record == NULL) {
4024 ACCOUNT_FATAL("malloc Failed");
4028 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4029 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4030 account_type_list = g_slist_append(account_type_list, account_type_record);
4031 rc = _account_query_step(hstmt);
4034 rc = _account_query_finalize(hstmt);
4035 if (rc != _ACCOUNT_ERROR_NONE) {
4036 _account_type_gslist_account_type_free(account_type_list);
4037 ACCOUNT_ERROR("finalize error(%s)", rc);
4045 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4046 account_type_s *account_type = NULL;
4047 account_type = (account_type_s*)iter->data;
4048 _account_type_query_label_by_app_id(_account_get_label_text_cb, account_type->app_id, (void*)account_type);
4049 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, account_type->app_id, (void*)account_type);
4052 *error_code = _ACCOUNT_ERROR_NONE;
4055 if (hstmt != NULL) {
4056 rc = _account_query_finalize(hstmt);
4057 if (rc != _ACCOUNT_ERROR_NONE) {
4064 if (*error_code == _ACCOUNT_ERROR_NONE || *error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4065 rc = _account_type_query_by_provider_feature_from_global_db(key, &account_type_list);
4066 if (rc != _ACCOUNT_ERROR_NONE && rc != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4067 ACCOUNT_ERROR("_account_type_query_by_provider_feature_from_global_db fail=[%d]", rc);
4068 _account_type_gslist_account_type_free(account_type_list);
4071 if (rc == _ACCOUNT_ERROR_NONE)
4075 _INFO("account_type_query_by_provider_feature end");
4077 return account_type_list;
4080 int _account_type_query_all_from_global_db(GSList **account_type_list_all)
4082 account_stmt hstmt = NULL;
4083 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4084 int rc = _ACCOUNT_ERROR_NONE;
4085 int error_code = _ACCOUNT_ERROR_NONE;
4086 GSList *account_type_list = NULL;
4088 _INFO("_account_type_query_all_in_global_db start");
4089 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, NULL, ("The database isn't connected."));
4091 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4093 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
4094 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
4096 rc = _account_query_step(hstmt);
4098 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
4099 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
4100 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4103 account_type_s *account_type_record = NULL;
4105 if (rc != SQLITE_ROW) {
4106 _INFO("[_ACCOUNT_ERROR_RECORD_NOT_FOUND]The record isn't found.");
4107 error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4111 while (rc == SQLITE_ROW) {
4112 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4114 if (account_type_record == NULL) {
4115 ACCOUNT_FATAL("malloc Failed");
4119 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4120 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4121 account_type_list = g_slist_append(account_type_list, account_type_record);
4122 rc = _account_query_step(hstmt);
4125 rc = _account_query_finalize(hstmt);
4126 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list); }, rc, ("finalize error"));
4131 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4132 account_type_s *account_type = NULL;
4133 account_type = (account_type_s*)iter->data;
4134 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb, account_type->app_id, (void*)account_type);
4135 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, account_type->app_id, (void*)account_type);
4138 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4139 account_type_s *account_type = NULL;
4140 account_type = (account_type_s*)iter->data;
4141 *account_type_list_all = g_slist_append(*account_type_list_all, account_type);
4143 g_slist_free(account_type_list);
4145 error_code = _ACCOUNT_ERROR_NONE;
4148 if (hstmt != NULL) {
4149 rc = _account_query_finalize(hstmt);
4150 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list); }, rc, ("finalize error"));
4154 _INFO("_account_type_query_all_in_global_db end");
4159 GSList* _account_type_query_all(void)
4161 account_stmt hstmt = NULL;
4162 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4164 int error_code = _ACCOUNT_ERROR_NONE;
4165 GSList *account_type_list = NULL;
4167 _INFO("_account_type_query_all start");
4168 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected."));
4170 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4172 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
4173 hstmt = _account_prepare_query(g_hAccountDB, query);
4175 rc = _account_query_step(hstmt);
4177 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4178 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4182 account_type_s *account_type_record = NULL;
4184 if (rc != SQLITE_ROW) {
4185 _INFO("[_ACCOUNT_ERROR_RECORD_NOT_FOUND]The record isn't found.");
4186 error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4190 while (rc == SQLITE_ROW) {
4191 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4193 if (account_type_record == NULL) {
4194 ACCOUNT_FATAL("malloc Failed");
4198 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4199 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4200 account_type_list = g_slist_append(account_type_list, account_type_record);
4201 rc = _account_query_step(hstmt);
4204 rc = _account_query_finalize(hstmt);
4205 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list); }, NULL, ("finalize error"));
4210 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4211 account_type_s *account_type = NULL;
4212 account_type = (account_type_s*)iter->data;
4213 _account_type_query_label_by_app_id(_account_get_label_text_cb, account_type->app_id, (void*)account_type);
4214 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, account_type->app_id, (void*)account_type);
4217 error_code = _ACCOUNT_ERROR_NONE;
4220 if (hstmt != NULL) {
4221 rc = _account_query_finalize(hstmt);
4222 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list); }, NULL, ("finalize error"));
4226 if (error_code == _ACCOUNT_ERROR_NONE || error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4227 error_code = _account_type_query_all_from_global_db(&account_type_list);
4228 if (rc != _ACCOUNT_ERROR_NONE && rc != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4229 ACCOUNT_ERROR("_account_type_query_all_from_global_db fail=[%d]", rc);
4230 _account_type_gslist_account_type_free(account_type_list);
4235 _INFO("_account_type_query_all end");
4237 return account_type_list;
4240 // output parameter label must be free
4241 int _account_type_query_label_by_locale_from_global_db(const char* app_id, const char* locale, char **label)
4243 int error_code = _ACCOUNT_ERROR_NONE;
4244 account_stmt hstmt = NULL;
4245 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4246 int rc = 0, binding_count = 1;
4247 char* converted_locale = NULL;
4249 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
4250 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4251 ACCOUNT_RETURN_VAL((label != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
4252 ACCOUNT_RETURN_VAL((locale != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("locale char is null"));
4253 //Making label newly created
4255 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4257 converted_locale = _account_dup_text(locale);
4258 gchar** tokens = g_strsplit(converted_locale, "-", 2);
4260 if (tokens != NULL) {
4261 if ((char*)(tokens[1]) != NULL) {
4262 char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1]));
4263 if (upper_token != NULL) {
4264 _ACCOUNT_FREE(converted_locale);
4265 converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token);
4267 _ACCOUNT_FREE(upper_token);
4272 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, converted_locale);
4273 _ACCOUNT_FREE(converted_locale);
4275 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
4277 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
4278 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
4279 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4282 _account_query_bind_text(hstmt, binding_count++, app_id);
4284 rc = _account_query_step(hstmt);
4285 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4287 label_s* label_record = NULL;
4289 while (rc == SQLITE_ROW) {
4290 label_record = (label_s*) malloc(sizeof(label_s));
4292 if (label_record == NULL) {
4293 ACCOUNT_FATAL("malloc Failed");
4297 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
4299 _account_type_convert_column_to_label(hstmt, label_record);
4301 _ACCOUNT_FREE(*label);
4302 //Making label newly created
4303 *label = _account_dup_text(label_record->label);
4305 _account_type_free_label_with_items(label_record);
4307 rc = _account_query_step(hstmt);
4310 rc = _account_query_finalize(hstmt);
4311 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4314 error_code = _ACCOUNT_ERROR_NONE;
4317 if (hstmt != NULL) {
4318 rc = _account_query_finalize(hstmt);
4319 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4323 _INFO("_account_type_query_label_by_locale_from_global_db() end : error_code = %d", error_code);
4328 // output parameter label must be free
4329 int _account_type_query_label_by_locale(const char* app_id, const char* locale, char **label)
4331 int error_code = _ACCOUNT_ERROR_NONE;
4332 account_stmt hstmt = NULL;
4333 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4334 int rc = 0, binding_count = 1;
4335 char* converted_locale = NULL;
4337 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
4338 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4339 ACCOUNT_RETURN_VAL((label != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
4340 ACCOUNT_RETURN_VAL((locale != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("locale char is null"));
4341 //Making label newly created
4343 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4345 converted_locale = _account_dup_text(locale);
4346 gchar** tokens = g_strsplit(converted_locale, "-", 2);
4348 if (tokens != NULL) {
4349 if ((char*)(tokens[1]) != NULL) {
4350 char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1]));
4351 if (upper_token != NULL) {
4352 _ACCOUNT_FREE(converted_locale);
4353 converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token);
4355 _ACCOUNT_FREE(upper_token);
4360 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, converted_locale);
4361 _ACCOUNT_FREE(converted_locale);
4363 hstmt = _account_prepare_query(g_hAccountDB, query);
4365 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4366 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4367 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4370 _account_query_bind_text(hstmt, binding_count++, app_id);
4372 rc = _account_query_step(hstmt);
4373 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4375 label_s* label_record = NULL;
4377 while (rc == SQLITE_ROW) {
4378 label_record = (label_s*) malloc(sizeof(label_s));
4380 if (label_record == NULL) {
4381 ACCOUNT_FATAL("malloc Failed");
4385 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
4387 _account_type_convert_column_to_label(hstmt, label_record);
4389 _ACCOUNT_FREE(*label);
4390 //Making label newly created
4391 *label = _account_dup_text(label_record->label);
4393 _account_type_free_label_with_items(label_record);
4395 rc = _account_query_step(hstmt);
4398 rc = _account_query_finalize(hstmt);
4399 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4402 error_code = _ACCOUNT_ERROR_NONE;
4405 if (hstmt != NULL) {
4406 rc = _account_query_finalize(hstmt);
4407 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4411 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
4412 error_code = _account_type_query_label_by_locale_from_global_db(app_id, locale, label);
4414 _INFO("_account_type_query_label_by_locale() end : error_code = %d", error_code);
4419 static int _account_insert_custom(account_s *account, int account_id)
4421 _INFO("_account_insert_custom start");
4424 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4425 account_stmt hstmt = NULL;
4427 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4429 if (g_slist_length(account->custom_list) == 0) {
4430 ACCOUNT_DEBUG("_account_insert_custom, no custom data\n");
4431 return _ACCOUNT_ERROR_NONE;
4434 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
4436 rc = _account_get_record_count(g_hAccountDB, query);
4438 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4439 ACCOUNT_ERROR("Access failed(%d, %s)", _account_db_err_msg(g_hAccountDB));
4440 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4444 ACCOUNT_SLOGE("_account_insert_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
4445 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4452 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
4455 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4456 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (AccountId, AppId, Key, Value) VALUES "
4457 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
4459 hstmt = _account_prepare_query(g_hAccountDB, query);
4461 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4462 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4463 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4466 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
4468 account_custom_s* custom_data = NULL;
4469 custom_data = (account_custom_s*)iter->data;
4471 ret = _account_query_bind_int(hstmt, count++, account_id);
4472 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
4473 ret = _account_query_bind_text(hstmt, count++, account->package_name);
4474 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4475 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
4476 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4477 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
4478 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4480 rc = _account_query_step(hstmt);
4482 if (rc != SQLITE_DONE) {
4483 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4487 rc = _account_query_finalize(hstmt);
4488 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4493 _INFO("_account_insert_custom end");
4495 return _ACCOUNT_ERROR_NONE;
4498 static int _account_update_custom(account_s *account, int account_id)
4501 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4502 account_stmt hstmt = NULL;
4504 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4506 if (g_slist_length(account->custom_list) == 0) {
4507 ACCOUNT_DEBUG("_account_update_custom, no custom data\n");
4508 return _ACCOUNT_ERROR_NONE;
4511 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
4513 rc = _account_get_record_count(g_hAccountDB, query);
4515 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4516 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4517 pthread_mutex_unlock(&account_mutex);
4518 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4519 } else if (_account_db_err_code(g_hAccountDB) == SQLITE_BUSY) {
4520 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
4521 pthread_mutex_unlock(&account_mutex);
4522 return _ACCOUNT_ERROR_DATABASE_BUSY;
4526 ACCOUNT_SLOGE("_account_update_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
4527 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4530 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4532 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId=? ", ACCOUNT_CUSTOM_TABLE);
4533 hstmt = _account_prepare_query(g_hAccountDB, query);
4535 _account_query_bind_int(hstmt, count++, (int)account_id);
4536 rc = _account_query_step(hstmt);
4538 if (rc == SQLITE_BUSY) {
4539 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4540 if (_account_query_finalize(hstmt) != _ACCOUNT_ERROR_NONE)
4541 _ERR("finalize error - account_query_step() failed");
4542 return _ACCOUNT_ERROR_DATABASE_BUSY;
4543 } else if (rc != SQLITE_DONE) {
4544 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4545 if (_account_query_finalize(hstmt) != _ACCOUNT_ERROR_NONE)
4546 _ERR("finalize error - account_query_step() failed");
4547 return _ACCOUNT_ERROR_DB_FAILED;
4550 rc = _account_query_finalize(hstmt);
4551 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4556 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
4559 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4560 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AccountId, AppId, Key, Value) VALUES "
4561 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
4563 hstmt = _account_prepare_query(g_hAccountDB, query);
4565 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4566 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4567 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4570 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
4572 account_custom_s* custom_data = NULL;
4573 custom_data = (account_custom_s*)iter->data;
4575 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
4576 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
4577 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
4578 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4579 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
4580 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4581 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
4582 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4584 rc = _account_query_step(hstmt);
4586 if (rc != SQLITE_DONE) {
4587 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4591 rc = _account_query_finalize(hstmt);
4592 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4597 return _ACCOUNT_ERROR_NONE;