3 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
31 #include <pkgmgr-info.h>
32 //#include <tzplatform_config.h>
35 #include <account_ipc_marshal.h>
36 #include <account_free.h>
37 #include <account-private.h>
38 #include <account_db_helper.h>
39 #include <account_crypto_service.h>
40 #include <account_err.h>
41 #include "account_type.h"
42 #include "account-server-db.h"
44 //typedef sqlite3_stmt* account_stmt;
46 #define EMAIL_SERVICE_CMDLINE "/usr/bin/email-service"
48 #define EMAIL_APPID "email-setting-efl"
50 #define ACCOUNT_DB_OPEN_READONLY 0
51 #define ACCOUNT_DB_OPEN_READWRITE 1
55 #define _TIZEN_PUBLIC_
56 #ifndef _TIZEN_PUBLIC_
60 static sqlite3* g_hAccountDB = NULL;
61 static sqlite3* g_hAccountDB2 = NULL;
62 static sqlite3* g_hAccountGlobalDB = NULL;
63 static sqlite3* g_hAccountGlobalDB2 = NULL;
64 pthread_mutex_t account_mutex = PTHREAD_MUTEX_INITIALIZER;
65 pthread_mutex_t account_global_mutex = PTHREAD_MUTEX_INITIALIZER;
67 //static char *_account_dup_text(const char *text_data);
68 static int _account_insert_custom(account_s *account, int account_id);
69 static int _account_update_custom(account_s *account, int account_id);
70 static int _account_type_update_provider_feature(sqlite3 * account_db_handle, account_type_s *account_type, const char* app_id);
72 static void _account_insert_delete_update_notification_send(char *noti_name)
75 _ERR("Noti Name is NULL!!!!!!\n");
79 _INFO("noti_type = %s", noti_name);
81 if (vconf_set_str(VCONFKEY_ACCOUNT_MSG_STR, noti_name) != 0) {
82 _ERR("Vconf MSG Str set FAILED !!!!!!\n");;
86 int _account_get_current_appid_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
90 GSList** appid_list = (GSList**)user_data;
93 pkgmgr_ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
95 if (pkgmgr_ret != PMINFO_R_OK)
96 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_appid(%d)", pkgmgr_ret);
98 item = _account_dup_text(appid);
99 *appid_list = g_slist_append(*appid_list, item);
104 static inline int __read_proc(const char *path, char *buf, int size)
108 if (buf == NULL || path == NULL) {
109 ACCOUNT_ERROR("path and buffer is mandatory\n");
113 fd = open(path, O_RDONLY);
115 ACCOUNT_ERROR("fd open error(%d)\n", fd);
119 ret = read(fd, buf, size - 1);
121 ACCOUNT_ERROR("fd read error(%d)\n", fd);
133 char *_account_get_proc_cmdline_bypid(int pid)
138 ACCOUNT_SNPRINTF(buf, sizeof(buf), "/proc/%d/cmdline", pid);
139 ret = __read_proc(buf, buf, sizeof(buf));
141 ACCOUNT_DEBUG("No proc directory (%d)\n", pid);
149 static int _account_execute_query_from_global_db(const char *query)
152 char* pszErrorMsg = NULL;
155 ACCOUNT_ERROR("NULL query\n");
156 return _ACCOUNT_ERROR_QUERY_SYNTAX_ERROR;
159 if (!g_hAccountGlobalDB) {
160 ACCOUNT_ERROR("Global DB is not opened\n");
161 return _ACCOUNT_ERROR_DB_NOT_OPENED;
164 rc = sqlite3_exec(g_hAccountGlobalDB, query, NULL, NULL, &pszErrorMsg);
165 if (SQLITE_OK != rc) {
166 ACCOUNT_ERROR("sqlite3_exec rc(%d) query(%s) failed(%s).", rc, query, pszErrorMsg);
167 sqlite3_free(pszErrorMsg);
175 static int _account_begin_transaction_from_global_db(void)
177 ACCOUNT_DEBUG("_account_begin_transaction start");
180 ret = _account_execute_query_from_global_db("BEGIN IMMEDIATE TRANSACTION");
182 if (ret == SQLITE_BUSY) {
183 ACCOUNT_ERROR(" sqlite3 busy = %d", ret);
184 return _ACCOUNT_ERROR_DATABASE_BUSY;
185 } else if (ret != SQLITE_OK) {
186 ACCOUNT_ERROR("_account_svc_begin_transaction_in_global_db fail :: %d", ret);
187 return _ACCOUNT_ERROR_DB_FAILED;
190 ACCOUNT_DEBUG("_account_begin_transaction_in_global_db end");
191 return _ACCOUNT_ERROR_NONE;
194 static int _account_end_transaction_from_global_db(bool is_success)
196 ACCOUNT_DEBUG("_account_end_transaction_in_global_db start");
200 if (is_success == true) {
201 ret = _account_execute_query_from_global_db("COMMIT TRANSACTION");
202 ACCOUNT_DEBUG("_account_end_transaction_in_global_db COMMIT");
204 ret = _account_execute_query_from_global_db("ROLLBACK TRANSACTION");
205 ACCOUNT_DEBUG("_account_end_transaction ROLLBACK");
208 if (ret == SQLITE_PERM) {
209 ACCOUNT_ERROR("Account permission denied :: %d", ret);
210 return _ACCOUNT_ERROR_PERMISSION_DENIED;
213 if (ret == SQLITE_BUSY) {
214 ACCOUNT_DEBUG(" sqlite3 busy = %d", ret);
215 return _ACCOUNT_ERROR_DATABASE_BUSY;
218 if (ret != SQLITE_OK) {
219 ACCOUNT_ERROR("_account_svc_end_transaction_in_global_db fail :: %d", ret);
220 return _ACCOUNT_ERROR_DB_FAILED;
223 ACCOUNT_DEBUG("_account_end_transaction_in_global_db end");
224 return _ACCOUNT_ERROR_NONE;
228 int _account_global_db_open(void)
232 char account_db_path[256] = {0, };
234 _INFO("start _account_global_db_open()");
236 ACCOUNT_MEMSET(account_db_path, 0x00, sizeof(account_db_path));
237 ACCOUNT_GET_GLOBAL_DB_PATH(account_db_path, sizeof(account_db_path));
239 if (g_hAccountGlobalDB) {
240 _ERR("Account database is using in another app. %x", g_hAccountDB);
241 return _ACCOUNT_ERROR_DATABASE_BUSY;
244 ret = _account_db_handle_close(g_hAccountGlobalDB2);
245 if (ret != _ACCOUNT_ERROR_NONE)
246 ACCOUNT_DEBUG("db_util_close(g_hAccountGlobalDB2) fail ret = %d", ret);
248 ACCOUNT_DEBUG("before _account_global_db_open()");
249 // if (mode == ACCOUNT_DB_OPEN_READWRITE)
250 // rc = db_util_open(account_db_path, &g_hAccountDB, DB_UTIL_REGISTER_HOOK_METHOD);
251 // else if (mode == ACCOUNT_DB_OPEN_READONLY)
252 rc = db_util_open_with_options(account_db_path, &g_hAccountGlobalDB, SQLITE_OPEN_READONLY, NULL);
254 // return _ACCOUNT_ERROR_DB_NOT_OPENED;
255 ACCOUNT_DEBUG("after _account_global_db_open() sqlite_rc = %d", rc);
257 if (rc == SQLITE_PERM || _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
258 ACCOUNT_ERROR("Account permission denied");
259 return _ACCOUNT_ERROR_PERMISSION_DENIED;
262 if (rc == SQLITE_BUSY) {
263 ACCOUNT_ERROR("busy handler fail.");
264 return _ACCOUNT_ERROR_DATABASE_BUSY;
267 if (rc != SQLITE_OK) {
268 ACCOUNT_ERROR("The database isn't connected.");
269 return _ACCOUNT_ERROR_DB_NOT_OPENED;
272 _INFO("end _account_global_db_open()");
273 return _ACCOUNT_ERROR_NONE;
276 int _account_global_db_close(void)
278 ACCOUNT_DEBUG("start account_global_db_close()");
281 ret = _account_db_handle_close(g_hAccountGlobalDB2);
282 if (ret != _ACCOUNT_ERROR_NONE)
283 ACCOUNT_DEBUG("db_util_close(g_hAccountGlobalDB2) fail ret = %d", ret);
285 ret = _account_db_handle_close(g_hAccountGlobalDB);
286 if (ret != _ACCOUNT_ERROR_NONE) {
287 ACCOUNT_ERROR("db_util_close(g_hAccountGlobalDB) fail ret = %d", ret);
288 g_hAccountGlobalDB2 = g_hAccountGlobalDB;
290 g_hAccountGlobalDB = NULL;
295 static bool _account_check_add_more_account(const char* app_id)
297 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
300 ACCOUNT_RETURN_VAL((app_id != 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
301 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
303 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
305 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s' and MultipleAccountSupport = 1", ACCOUNT_TYPE_TABLE, app_id);
306 rc = _account_get_record_count(g_hAccountDB, query);
308 /* multiple account support case (User DB & global DB) */
309 if (rc > 0 || _account_get_record_count(g_hAccountGlobalDB, query) > 0) {
310 ACCOUNT_SLOGD("app id (%s) supports multiple account. rc(%d)\n", app_id, rc);
314 /* multiple account not support case */
315 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
316 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE package_name = '%s'", ACCOUNT_TABLE, app_id);
317 rc = _account_get_record_count(g_hAccountDB, query);
320 ACCOUNT_SLOGD("app id (%s) supports single account. and there is no account of the app id\n", app_id);
328 int _account_db_open(int mode, int pid, uid_t uid)
332 char account_db_dir[256] = {0, };
333 char account_db_path[256] = {0, };
335 _INFO("start _account_db_open() pid [%d]. uid [%d]", pid, uid);
337 ACCOUNT_MEMSET(account_db_dir, 0x00, sizeof(account_db_dir));
338 ACCOUNT_MEMSET(account_db_path, 0x00, sizeof(account_db_path));
340 ACCOUNT_GET_USER_DB_PATH(account_db_path, sizeof(account_db_path), uid);
343 _ERR("Account database is using in another app. %x", g_hAccountDB);
344 return _ACCOUNT_ERROR_DATABASE_BUSY;
347 ret = _account_db_handle_close(g_hAccountDB2);
348 if (ret != _ACCOUNT_ERROR_NONE)
349 ACCOUNT_DEBUG("db_util_close(g_hAccountDB2) fail ret = %d", ret);
351 ACCOUNT_GET_USER_DB_DIR(account_db_dir, sizeof(account_db_dir), uid);
353 if (mkdir(account_db_dir, 0777) != 0)
354 ACCOUNT_DEBUG("\"%s\" is already exist directory", account_db_dir);
356 ACCOUNT_DEBUG("before db_util_open()");
357 // if (mode == ACCOUNT_DB_OPEN_READWRITE)
358 rc = db_util_open(account_db_path, &g_hAccountDB, DB_UTIL_REGISTER_HOOK_METHOD);
359 // else if (mode == ACCOUNT_DB_OPEN_READONLY)
360 // rc = db_util_open_with_options(account_db_path, &g_hAccountDB, SQLITE_OPEN_READONLY, NULL);
362 // return _ACCOUNT_ERROR_DB_NOT_OPENED;
363 ACCOUNT_DEBUG("after db_util_open() sqlite_rc = %d", rc);
365 if (rc == SQLITE_PERM || _account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
366 ACCOUNT_ERROR("Account permission denied");
367 return _ACCOUNT_ERROR_PERMISSION_DENIED;
370 if (rc == SQLITE_BUSY) {
371 ACCOUNT_ERROR("busy handler fail.");
372 return _ACCOUNT_ERROR_DATABASE_BUSY;
375 if (rc != SQLITE_OK) {
376 ACCOUNT_ERROR("The database isn't connected.");
377 return _ACCOUNT_ERROR_DB_NOT_OPENED;
380 rc = _account_check_is_all_table_exists(g_hAccountDB);
383 _ERR("_account_check_is_all_table_exists rc=[%d]", rc);
385 } else if (rc == ACCOUNT_TABLE_TOTAL_COUNT) {
388 int ret = _account_create_all_tables(g_hAccountDB);
389 if (ret != _ACCOUNT_ERROR_NONE) {
390 _ERR("_account_create_all_tables fail ret=[%d]", ret);
395 _INFO("end _account_db_open()");
396 return _ACCOUNT_ERROR_NONE;
399 int _account_db_close(void)
401 ACCOUNT_DEBUG("start db_util_close()");
404 ret = _account_db_handle_close(g_hAccountDB2);
405 if (ret != _ACCOUNT_ERROR_NONE)
406 ACCOUNT_DEBUG("db_util_close(g_hAccountDB2) fail ret = %d", ret);
408 ret = _account_db_handle_close(g_hAccountDB);
409 if (ret != _ACCOUNT_ERROR_NONE) {
410 ACCOUNT_ERROR("db_util_close(g_hAccountDB) fail ret = %d", ret);
411 g_hAccountDB2 = g_hAccountDB;
418 static int _account_execute_insert_query(account_s *account)
420 _INFO("_account_execute_insert_query start");
423 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
424 int error_code = _ACCOUNT_ERROR_NONE;
425 account_stmt hstmt = NULL;
427 /* check whether app id exist in account type db */
429 if (!account->user_name && !account->display_name && !account->email_address) {
431 ACCOUNT_ERROR("Mandetory fields is NULL. At least one field is required among username, display name, email address\n");
432 return _ACCOUNT_ERROR_INVALID_PARAMETER;
436 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
437 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (user_name, email_address , display_name , icon_path , source , package_name , "
438 "access_token , domain_name , auth_type , secret , sync_support , txt_custom0, txt_custom1, txt_custom2, txt_custom3, txt_custom4, "
439 "int_custom0, int_custom1, int_custom2, int_custom3, int_custom4, txt_custom0 ) values " // to do urusa
440 "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", ACCOUNT_TABLE);
442 hstmt = _account_prepare_query(g_hAccountDB, query);
443 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
446 _account_convert_account_to_sql(account, hstmt, query);
449 rc = _account_query_step(hstmt);
450 if (rc != SQLITE_DONE) {
452 ACCOUNT_ERROR("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
454 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM)
455 error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
457 error_code = _ACCOUNT_ERROR_DB_FAILED;
461 rc = _account_query_finalize(hstmt);
462 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
465 _INFO("_account_execute_insert_query end");
469 static int _account_insert_capability(account_s *account, int account_id)
471 _INFO("_account_insert_capability start");
473 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
474 account_stmt hstmt = NULL;
476 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
478 if (g_slist_length(account->capablity_list) == 0) {
479 ACCOUNT_DEBUG("_account_insert_capability, no capability\n");
480 return _ACCOUNT_ERROR_NONE;
483 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
485 _INFO("_account_insert_capability _account_get_record_count [%s]", query);
486 rc = _account_get_record_count(g_hAccountDB, query);
488 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
489 _ERR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
490 return _ACCOUNT_ERROR_PERMISSION_DENIED;
494 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
500 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
504 account_capability_s* cap_data = NULL;
505 cap_data = (account_capability_s*)iter->data;
507 _INFO("cap_data->type = %s, cap_data->value = %d \n", cap_data->type, cap_data->value);
509 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
510 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
511 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
512 hstmt = _account_prepare_query(g_hAccountDB, query);
514 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
516 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
517 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
518 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
519 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
520 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
521 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
522 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
523 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
524 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
525 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
527 rc = _account_query_step(hstmt);
528 _INFO("_account_insert_capability _account_query_step[%d]", rc);
530 if (rc != SQLITE_DONE) {
531 _ERR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
535 rc = _account_query_finalize(hstmt);
536 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
541 _INFO("_account_insert_capability end");
542 return _ACCOUNT_ERROR_NONE;
545 static int _account_update_capability(account_s *account, int account_id)
548 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
549 account_stmt hstmt = NULL;
551 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
553 if (g_slist_length(account->capablity_list) == 0) {
554 ACCOUNT_ERROR("_account_update_capability, no capability\n");
555 return _ACCOUNT_ERROR_NONE;
558 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
560 rc = _account_get_record_count(g_hAccountDB, query);
563 ACCOUNT_SLOGI("_account_update_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
564 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
567 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
569 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id=? ", CAPABILITY_TABLE);
570 hstmt = _account_prepare_query(g_hAccountDB, query);
572 _account_query_bind_int(hstmt, count++, (int)account_id);
573 rc = _account_query_step(hstmt);
575 if (rc != SQLITE_DONE) {
576 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
577 return _ACCOUNT_ERROR_DB_FAILED;
579 rc = _account_query_finalize(hstmt);
580 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
585 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
588 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
589 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
590 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
592 hstmt = _account_prepare_query(g_hAccountDB, query);
594 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
596 account_capability_s* cap_data = NULL;
597 cap_data = (account_capability_s*)iter->data;
599 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
600 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
601 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
602 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
603 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
604 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
605 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
606 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
607 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
608 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
610 rc = _account_query_step(hstmt);
612 if (rc != SQLITE_DONE) {
613 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
617 rc = _account_query_finalize(hstmt);
618 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
623 return _ACCOUNT_ERROR_NONE;
626 static int _account_update_capability_by_user_name(account_s *account, const char *user_name, const char *package_name)
629 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
630 account_stmt hstmt = NULL;
632 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
634 if (g_slist_length(account->capablity_list) == 0) {
635 ACCOUNT_ERROR("_account_update_capability_by_user_name, no capability\n");
636 return _ACCOUNT_ERROR_NONE;
639 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where package_name= '%s' and user_name='%s'", ACCOUNT_TABLE, package_name, user_name);
641 rc = _account_get_record_count(g_hAccountDB, query);
644 ACCOUNT_SLOGI("_account_update_capability_by_user_name : related account item is not existed rc=%d , %s ", rc, _account_db_err_msg(g_hAccountDB));
645 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
648 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
650 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name=? and user_name=? ", CAPABILITY_TABLE);
651 hstmt = _account_prepare_query(g_hAccountDB, query);
653 _account_query_bind_text(hstmt, count++, (char*)account->package_name);
654 _account_query_bind_text(hstmt, count++, (char*)account->user_name);
655 rc = _account_query_step(hstmt);
656 if (rc != SQLITE_DONE) {
657 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
658 return _ACCOUNT_ERROR_DB_FAILED;
661 rc = _account_query_finalize(hstmt);
662 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
667 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
670 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
671 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
672 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
674 hstmt = _account_prepare_query(g_hAccountDB, query);
676 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
678 account_capability_s* cap_data = NULL;
679 cap_data = (account_capability_s*)iter->data;
681 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
682 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
683 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
684 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
685 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
686 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
687 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
688 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
689 ret = _account_query_bind_int(hstmt, count++, (int)account->id);
690 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
692 rc = _account_query_step(hstmt);
694 if (rc != SQLITE_DONE) {
695 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
699 rc = _account_query_finalize(hstmt);
700 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
705 return _ACCOUNT_ERROR_NONE;
709 bool _account_add_capability_to_account_cb(const char* capability_type, int capability_value, account_s *account)
711 account_capability_s *cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
713 if (cap_data == NULL)
715 ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
717 cap_data->type = _account_dup_text(capability_type);
718 cap_data->value = capability_value;
719 _INFO("cap_data->type = %s, cap_data->value = %d", cap_data->type, cap_data->value);
721 account->capablity_list = g_slist_append(account->capablity_list, (gpointer)cap_data);
727 bool _account_add_custom_to_account_cb(const char* key, const char* value, account_s *account)
729 account_custom_s *custom_data = (account_custom_s*)malloc(sizeof(account_custom_s));
731 if (custom_data == NULL) {
732 ACCOUNT_DEBUG("_account_add_custom_to_account_cb :: malloc fail\n");
735 ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s));
737 custom_data->account_id = account->id;
738 custom_data->app_id = _account_dup_text(account->package_name);
739 custom_data->key = _account_dup_text(key);
740 custom_data->value = _account_dup_text(value);
741 _INFO("custom_data->key = %s, custom_data->value = %s", custom_data->key, custom_data->value);
743 account->custom_list = g_slist_append(account->custom_list, (gpointer)custom_data);
749 static int _account_compare_old_record_by_user_name(account_s *new_account, const char* user_name, const char* package_name)
751 int error_code = _ACCOUNT_ERROR_NONE;
752 account_stmt hstmt = NULL;
753 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
755 account_s *old_account = NULL;
757 ACCOUNT_RETURN_VAL((new_account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
758 ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
759 ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
760 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
762 old_account = (account_s*)calloc(1, sizeof(account_s));
764 ACCOUNT_FATAL("Memory alloc fail\n");
765 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
768 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
770 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = '%s' and package_name='%s'", ACCOUNT_TABLE, user_name, package_name);
771 hstmt = _account_prepare_query(g_hAccountDB, query);
773 rc = _account_query_step(hstmt);
774 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
776 while (rc == SQLITE_ROW) {
777 _account_convert_column_to_account(hstmt, old_account);
778 rc = _account_query_step(hstmt);
781 rc = _account_query_finalize(hstmt);
782 ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
786 error_code = _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, old_account->id, (void*)old_account);
787 ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
790 error_code = _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, old_account->id, (void*)old_account);
791 ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
794 new_account->id = old_account->id;
797 if (!new_account->user_name) {
798 if (old_account->user_name)
799 new_account->user_name = _account_dup_text(old_account->user_name);
803 if (!new_account->display_name) {
804 if (old_account->display_name)
805 new_account->display_name = _account_dup_text(old_account->display_name);
809 if (!new_account->email_address) {
810 if (old_account->email_address)
811 new_account->email_address = _account_dup_text(old_account->email_address);
815 if (!new_account->domain_name) {
816 if (old_account->domain_name)
817 new_account->domain_name = _account_dup_text(old_account->domain_name);
821 if (!new_account->icon_path) {
822 if (old_account->icon_path)
823 new_account->icon_path = _account_dup_text(old_account->icon_path);
827 if (!new_account->source) {
828 if (old_account->source)
829 new_account->source = _account_dup_text(old_account->source);
832 _ACCOUNT_FREE(new_account->package_name);
833 new_account->package_name = _account_dup_text(old_account->package_name);
836 if (!new_account->access_token) {
837 if (old_account->access_token)
838 new_account->access_token = _account_dup_text(old_account->access_token);
842 if (new_account->auth_type == _ACCOUNT_AUTH_TYPE_INVALID)
843 new_account->auth_type = old_account->auth_type;
846 if (new_account->secret == _ACCOUNT_SECRECY_INVALID)
847 new_account->secret = old_account->secret;
850 if (new_account->sync_support == _ACCOUNT_SYNC_INVALID)
851 new_account->sync_support = old_account->sync_support;
855 for (i = 0; i < USER_TXT_CNT; i++) {
856 if (!new_account->user_data_txt[i]) {
857 if (old_account->user_data_txt[i])
858 new_account->user_data_txt[i] = _account_dup_text(old_account->user_data_txt[i]);
863 for (i = 0; i < USER_INT_CNT; i++) {
864 if (new_account->user_data_int[i] == 0)
865 new_account->user_data_int[i] = old_account->user_data_int[i];
870 _account_free_account_with_items(old_account);
873 rc = _account_query_finalize(hstmt);
874 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
878 return _ACCOUNT_ERROR_NONE;
881 static int _account_update_account_by_user_name(int pid, uid_t uid, account_s *account, const char *user_name, const char *package_name)
883 int rc = 0, binding_count = 0, count = 0;
884 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
885 int error_code = _ACCOUNT_ERROR_NONE;
886 account_stmt hstmt = NULL;
888 ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is NULL.\n"));
889 ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is NULL.\n"));
891 char* current_appid = NULL;
892 char* verified_appid = NULL;
894 current_appid = _account_get_current_appid(pid, uid);
895 error_code = _account_get_represented_appid_from_db(g_hAccountDB, g_hAccountGlobalDB, current_appid, uid, &verified_appid);
897 _ACCOUNT_FREE(current_appid);
898 _ACCOUNT_FREE(verified_appid);
900 if (error_code != _ACCOUNT_ERROR_NONE) {
901 ACCOUNT_ERROR("No permission to update\n");
902 return _ACCOUNT_ERROR_PERMISSION_DENIED;
905 error_code = encrypt_access_token(account);
906 if (error_code != _ACCOUNT_ERROR_NONE) {
907 _ERR("_encrypt_access_token error");
911 _account_compare_old_record_by_user_name(account, user_name, package_name);
913 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
914 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
915 return _ACCOUNT_ERROR_PERMISSION_DENIED;
918 if (!account->package_name) {
919 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
920 return _ACCOUNT_ERROR_INVALID_PARAMETER;
923 if (!account->user_name && !account->display_name && !account->email_address) {
924 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
925 return _ACCOUNT_ERROR_INVALID_PARAMETER;
928 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE user_name='%s' and package_name='%s'"
929 , ACCOUNT_TABLE, user_name, package_name);
931 count = _account_get_record_count(g_hAccountDB, query);
933 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
934 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
935 return _ACCOUNT_ERROR_PERMISSION_DENIED;
939 ACCOUNT_SLOGI("_account_update_account_by_user_name : The account not exist!, count = %d, user_name=%s, package_name=%s\n",
940 count, user_name, package_name);
941 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
944 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
946 //TODO: Is it required to update id ? As of now I can only think of falied rollback cases (between account and gSSO DB)
947 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
948 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
949 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
950 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE user_name=? and package_name=? ", ACCOUNT_TABLE);
952 hstmt = _account_prepare_query(g_hAccountDB, query);
953 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
954 _account_end_transaction(g_hAccountDB, FALSE);
955 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
956 return _ACCOUNT_ERROR_PERMISSION_DENIED;
958 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
960 binding_count = _account_convert_account_to_sql(account, hstmt, query);
962 _account_query_bind_text(hstmt, binding_count++, user_name);
963 _account_query_bind_text(hstmt, binding_count++, package_name);
965 rc = _account_query_step(hstmt);
966 if (rc != SQLITE_DONE)
967 ACCOUNT_ERROR("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
969 rc = _account_query_finalize(hstmt);
970 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
973 /*update capability*/
974 error_code = _account_update_capability_by_user_name(account, user_name, package_name);
977 error_code = _account_update_custom(account, account->id);
982 int _account_insert_to_db(account_s* account, int pid, uid_t uid, int *account_id)
985 int error_code = _ACCOUNT_ERROR_NONE;
986 int ret_transaction = 0;
988 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
989 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
990 ACCOUNT_RETURN_VAL((account_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID POINTER IS NULL"));
992 if (!account->user_name && !account->display_name && !account->email_address) {
993 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
994 return _ACCOUNT_ERROR_INVALID_PARAMETER;
997 account_s *data = (account_s*)account;
998 ACCOUNT_SLOGD("(%s)-(%d) account_insert_to_db: begin_transaction.\n", __FUNCTION__, __LINE__);
1000 pthread_mutex_lock(&account_mutex);
1002 /* transaction control required*/
1003 ret_transaction = _account_begin_transaction(g_hAccountDB);
1005 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1006 pthread_mutex_unlock(&account_mutex);
1007 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1008 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1011 if (ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY) {
1012 ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction);
1013 pthread_mutex_unlock(&account_mutex);
1014 return _ACCOUNT_ERROR_DATABASE_BUSY;
1015 } else if (ret_transaction != _ACCOUNT_ERROR_NONE) {
1016 ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction);
1017 pthread_mutex_unlock(&account_mutex);
1018 return ret_transaction;
1021 *account_id = _account_get_next_sequence(g_hAccountDB, ACCOUNT_TABLE);
1022 data->id = *account_id;
1025 appid = _account_get_current_appid(pid, uid);
1029 // API caller cannot be recognized
1030 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1031 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1032 pthread_mutex_unlock(&account_mutex);
1033 return _ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
1037 char* verified_appid = NULL;
1038 error_code = _account_get_represented_appid_from_db(g_hAccountDB, g_hAccountGlobalDB, appid, uid, &verified_appid);//FIX
1039 _ACCOUNT_FREE(appid);
1040 if (error_code != _ACCOUNT_ERROR_NONE) {
1041 _ERR("error_code = %d", error_code);
1042 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1043 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1044 _ACCOUNT_FREE(verified_appid);
1045 pthread_mutex_unlock(&account_mutex);
1049 if (verified_appid) {
1051 error_code = _account_check_duplicated(g_hAccountDB, data, verified_appid, uid);
1052 if (error_code != _ACCOUNT_ERROR_NONE) {
1054 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1055 ACCOUNT_DEBUG("_account_check_duplicated(), rollback insert query(%x)!!!!\n", ret_transaction);
1057 pthread_mutex_unlock(&account_mutex);
1060 if (!_account_check_add_more_account(verified_appid)) {
1061 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1062 ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
1063 pthread_mutex_unlock(&account_mutex);
1064 _ACCOUNT_FREE(verified_appid);
1065 return _ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
1068 _ACCOUNT_FREE(data->package_name);
1069 data->package_name = _account_dup_text(verified_appid);
1070 _ACCOUNT_FREE(verified_appid);
1073 if (!_account_check_add_more_account(data->package_name)) {
1075 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1076 ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
1077 pthread_mutex_unlock(&account_mutex);
1078 return _ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
1081 error_code = encrypt_access_token(data);
1082 if (error_code != _ACCOUNT_ERROR_NONE) {
1083 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1084 ACCOUNT_ERROR("encrypt_access_token fail, rollback insert query(%x)!!!!\n", ret_transaction);
1086 pthread_mutex_unlock(&account_mutex);
1090 error_code = _account_execute_insert_query(data);
1092 if (error_code != _ACCOUNT_ERROR_NONE) {
1094 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1095 ACCOUNT_ERROR("INSERT account fail, rollback insert query(%x)!!!!\n", ret_transaction);
1097 pthread_mutex_unlock(&account_mutex);
1102 error_code = _account_insert_capability(data, *account_id);
1103 if (error_code != _ACCOUNT_ERROR_NONE) {
1105 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1106 ACCOUNT_ERROR("INSERT capability fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1108 pthread_mutex_unlock(&account_mutex);
1113 error_code = _account_insert_custom(data, *account_id);
1114 if (error_code != _ACCOUNT_ERROR_NONE) {
1115 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1116 ACCOUNT_ERROR("INSERT custom fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1118 pthread_mutex_unlock(&account_mutex);
1124 pthread_mutex_unlock(&account_mutex);
1125 _account_end_transaction(g_hAccountDB, TRUE);
1126 ACCOUNT_SLOGD("(%s)-(%d) account _end_transaction.\n", __FUNCTION__, __LINE__);
1128 char buf[64] = {0,};
1129 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_INSERT, *account_id);
1130 _account_insert_delete_update_notification_send(buf);
1131 _INFO("account _notification_send end.");
1133 return _ACCOUNT_ERROR_NONE;
1136 GSList* _account_get_capability_list_by_account_id(int account_id, int *error_code)
1138 *error_code = _ACCOUNT_ERROR_NONE;
1139 account_stmt hstmt = NULL;
1140 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1142 GSList* capability_list = NULL;
1144 ACCOUNT_RETURN_VAL((account_id > 0), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("ACCOUNT INDEX IS LESS THAN 0"));
1145 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), { *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED; }, NULL, ("The database isn't connected."));
1147 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1149 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
1150 hstmt = _account_prepare_query(g_hAccountDB, query);
1152 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1153 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1154 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
1158 rc = _account_query_step(hstmt);
1159 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1161 account_capability_s* capability_record = NULL;
1163 while (rc == SQLITE_ROW) {
1164 capability_record = (account_capability_s*) malloc(sizeof(account_capability_s));
1166 if (capability_record == NULL) {
1167 ACCOUNT_FATAL("malloc Failed");
1171 ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s));
1173 _account_convert_column_to_capability(hstmt, capability_record);
1175 //cb_ret = callback(capability_record->type, capability_record->value, user_data);
1177 //_account_free_capability_items(capability_record);
1178 //_ACCOUNT_FREE(capability_record);
1180 //ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
1182 capability_list = g_slist_append(capability_list, capability_record);
1183 rc = _account_query_step(hstmt);
1186 rc = _account_query_finalize(hstmt);
1187 if (rc != _ACCOUNT_ERROR_NONE) {
1188 _ERR("finalize error");
1189 _account_gslist_capability_free(capability_list);
1196 *error_code = _ACCOUNT_ERROR_NONE;
1199 if (hstmt != NULL) {
1200 rc = _account_query_finalize(hstmt);
1201 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), { *error_code = rc; }, NULL, ("finalize error"));
1205 pthread_mutex_unlock(&account_mutex);
1207 return capability_list;
1210 static int _account_compare_old_record(account_s *new_account, int account_id)
1212 int error_code = _ACCOUNT_ERROR_NONE;
1213 account_stmt hstmt = NULL;
1214 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1216 account_s *old_account = NULL;
1218 ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1219 ACCOUNT_RETURN_VAL((new_account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
1220 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1222 old_account = (account_s*)calloc(1, sizeof(account_s));
1223 if (old_account == NULL) {
1224 _ERR("Out of Memory");
1225 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
1228 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1230 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
1231 hstmt = _account_prepare_query(g_hAccountDB, query);
1233 rc = _account_query_step(hstmt);
1234 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1236 while (rc == SQLITE_ROW) {
1237 _account_convert_column_to_account(hstmt, old_account);
1238 rc = _account_query_step(hstmt);
1241 rc = _account_query_finalize(hstmt);
1242 ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1246 error_code = _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, old_account->id, (void*)old_account);
1247 ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
1250 error_code = _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, old_account->id, (void*)old_account);
1251 ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
1255 new_account->id = old_account->id;
1258 if (!new_account->user_name) {
1259 if (old_account->user_name)
1260 new_account->user_name = _account_dup_text(old_account->user_name);
1264 if (!new_account->display_name) {
1265 if (old_account->display_name)
1266 new_account->display_name = _account_dup_text(old_account->display_name);
1270 if (!new_account->email_address) {
1271 if (old_account->email_address)
1272 new_account->email_address = _account_dup_text(old_account->email_address);
1276 if (!new_account->domain_name) {
1277 if (old_account->domain_name)
1278 new_account->domain_name = _account_dup_text(old_account->domain_name);
1282 if (!new_account->icon_path) {
1283 if (old_account->icon_path)
1284 new_account->icon_path = _account_dup_text(old_account->icon_path);
1288 if (!new_account->source) {
1289 if (old_account->source)
1290 new_account->source = _account_dup_text(old_account->source);
1293 _ACCOUNT_FREE(new_account->package_name);
1294 new_account->package_name = _account_dup_text(old_account->package_name);
1297 if (!new_account->access_token) {
1298 if (old_account->access_token)
1299 new_account->access_token = _account_dup_text(old_account->access_token);
1304 for (i = 0; i < USER_TXT_CNT; i++) {
1305 if (!new_account->user_data_txt[i]) {
1306 if (old_account->user_data_txt[i])
1307 new_account->user_data_txt[i] = _account_dup_text(old_account->user_data_txt[i]);
1312 if (new_account->auth_type == _ACCOUNT_AUTH_TYPE_INVALID)
1313 new_account->auth_type = old_account->auth_type;
1316 if (new_account->secret == _ACCOUNT_SECRECY_INVALID)
1317 new_account->secret = old_account->secret;
1320 if (new_account->sync_support == _ACCOUNT_SYNC_INVALID)
1321 new_account->sync_support = old_account->sync_support;
1324 for (i = 0; i < USER_INT_CNT; i++) {
1325 if (new_account->user_data_int[i] == 0)
1326 new_account->user_data_int[i] = old_account->user_data_int[i];
1331 _account_free_account_with_items(old_account);
1333 if (hstmt != NULL) {
1334 rc = _account_query_finalize(hstmt);
1335 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1339 return _ACCOUNT_ERROR_NONE;
1342 static int _account_get_package_name_from_account_id(int account_id, char **package_name)
1344 int error_code = _ACCOUNT_ERROR_NONE;
1345 account_stmt hstmt = NULL;
1346 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1348 account_s *old_account = NULL;
1350 ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1351 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1353 old_account = (account_s*)calloc(1, sizeof(account_s));
1354 if (old_account == NULL) {
1355 _ERR("Out Of memory");
1356 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
1359 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1361 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
1362 hstmt = _account_prepare_query(g_hAccountDB, query);
1364 rc = _account_query_step(hstmt);
1365 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1367 while (rc == SQLITE_ROW) {
1368 _account_convert_column_to_account(hstmt, old_account);
1369 rc = _account_query_step(hstmt);
1372 rc = _account_query_finalize(hstmt);
1373 ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1376 // get package name.
1377 *package_name = _account_dup_text(old_account->package_name);
1381 _account_free_account_with_items(old_account);
1383 if (hstmt != NULL) {
1384 rc = _account_query_finalize(hstmt);
1385 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1393 static int _account_update_account(int pid, uid_t uid, account_s *account, int account_id)
1395 int rc = 0, binding_count = 0;
1396 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1397 int error_code = _ACCOUNT_ERROR_NONE, count = 0, ret_transaction = 0;
1398 account_stmt hstmt = NULL;
1400 if (!account->package_name) {
1401 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
1402 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1405 /* Check permission of requested appid */
1406 char* current_appid = NULL;
1407 char *package_name = NULL;
1409 current_appid = _account_get_current_appid(pid, uid);
1410 error_code = _account_get_package_name_from_account_id(account_id, &package_name);
1412 if (error_code != _ACCOUNT_ERROR_NONE || package_name == NULL) {
1413 ACCOUNT_ERROR("No package name with account_id\n");
1414 _ACCOUNT_FREE(current_appid);
1415 _ACCOUNT_FREE(package_name);
1416 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1419 error_code = _account_check_appid_group_with_package_name(current_appid, package_name, uid);
1420 ACCOUNT_DEBUG("UPDATE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name); // TODO: remove the log later.
1422 _ACCOUNT_FREE(current_appid);
1423 _ACCOUNT_FREE(package_name);
1425 if (error_code != _ACCOUNT_ERROR_NONE) {
1426 ACCOUNT_ERROR("No permission to update\n");
1427 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1430 error_code = encrypt_access_token(account);
1431 if (error_code != _ACCOUNT_ERROR_NONE) {
1432 _ERR("_encrypt_access_token error");
1436 error_code = _account_compare_old_record(account, account_id);
1437 if (error_code != _ACCOUNT_ERROR_NONE) {
1438 ACCOUNT_ERROR("_account_compare_old_record fail\n");
1442 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1443 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1444 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1445 } else if (_account_db_err_code(g_hAccountDB) == SQLITE_BUSY) {
1446 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
1447 return _ACCOUNT_ERROR_DATABASE_BUSY;
1450 if (!account->user_name && !account->display_name && !account->email_address) {
1451 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1452 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1455 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1457 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
1459 count = _account_get_record_count(g_hAccountDB, query);
1461 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
1462 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1465 /* transaction control required*/
1466 ret_transaction = _account_begin_transaction(g_hAccountDB);
1467 if (ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY) {
1468 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
1469 pthread_mutex_unlock(&account_mutex);
1470 return _ACCOUNT_ERROR_DATABASE_BUSY;
1473 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1474 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
1475 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
1476 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
1477 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
1479 hstmt = _account_prepare_query(g_hAccountDB, query);
1481 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1482 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1483 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1484 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1487 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)));
1489 binding_count = _account_convert_account_to_sql(account, hstmt, query);
1490 _account_query_bind_int(hstmt, binding_count++, account_id);
1492 rc = _account_query_step(hstmt);
1493 if (rc != SQLITE_DONE)
1494 ACCOUNT_SLOGE("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
1496 rc = _account_query_finalize(hstmt);
1497 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1500 _INFO("update query=%s", query);
1502 /*update capability*/
1503 error_code = _account_update_capability(account, account_id);
1504 if (error_code != _ACCOUNT_ERROR_NONE && error_code != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
1505 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1506 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1511 error_code = _account_update_custom(account, account_id);
1512 if (error_code != _ACCOUNT_ERROR_NONE && error_code != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
1513 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1514 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1518 ret_transaction = _account_end_transaction(g_hAccountDB, TRUE);
1520 _INFO("update end");
1526 static int _account_update_account_ex(account_s *account, int account_id)
1528 int rc = 0, binding_count = 0;
1529 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1530 int error_code = _ACCOUNT_ERROR_NONE, count = 0, ret_transaction = 0;
1531 account_stmt hstmt = NULL;
1533 if (!account->package_name) {
1534 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
1535 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1538 error_code = encrypt_access_token(account);
1539 if (error_code != _ACCOUNT_ERROR_NONE) {
1540 _ERR("_encrypt_access_token error");
1544 error_code = _account_compare_old_record(account, account_id);
1545 if (error_code != _ACCOUNT_ERROR_NONE) {
1546 ACCOUNT_ERROR("_account_compare_old_record fail\n");
1550 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1551 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1552 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1555 if (!account->user_name && !account->display_name && !account->email_address) {
1556 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1557 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1560 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1562 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
1564 count = _account_get_record_count(g_hAccountDB, query);
1566 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
1567 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1570 /* transaction control required*/
1571 ret_transaction = _account_begin_transaction(g_hAccountDB);
1572 if (ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY) {
1573 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
1574 pthread_mutex_unlock(&account_mutex);
1575 return _ACCOUNT_ERROR_DATABASE_BUSY;
1578 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1579 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
1580 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
1581 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
1582 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
1584 hstmt = _account_prepare_query(g_hAccountDB, query);
1586 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1587 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1588 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1589 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1592 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)));
1594 _INFO("account_update_to_db_by_id_ex_p : before convert() : account_id[%d], user_name=%s", account->id, account->user_name);
1595 binding_count = _account_convert_account_to_sql(account, hstmt, query);
1596 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], user_name=%s", account->id, account->user_name);
1597 _INFO("account_update_to_db_by_id_ex_p : before bind()");
1598 rc = _account_query_bind_int(hstmt, binding_count++, account_id);
1599 _INFO("account_update_to_db_by_id_ex_p : after bind() : ret = %d", rc);
1601 rc = _account_query_step(hstmt);
1602 if (rc != SQLITE_DONE)
1603 ACCOUNT_SLOGE("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
1604 _INFO("account_update_to_db_by_id_ex_p : after query_step() : ret = %d", rc);
1606 rc = _account_query_finalize(hstmt);
1607 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1609 _INFO("account_update_to_db_by_id_ex_p : after query_filnalize() : ret = %d", rc);
1611 _INFO("account_update_to_db_by_id_ex_p : before update_capability()");
1612 /*update capability*/
1613 error_code = _account_update_capability(account, account_id);
1614 if (error_code != _ACCOUNT_ERROR_NONE && error_code != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
1615 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1616 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1619 _INFO("account_update_to_db_by_id_ex_p : after update_capability()");
1621 _INFO("account_update_to_db_by_id_ex_p : before update_custom()");
1623 error_code = _account_update_custom(account, account_id);
1624 if (error_code != _ACCOUNT_ERROR_NONE && error_code != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
1625 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1626 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1629 _INFO("account_update_to_db_by_id_ex_p : after update_custom()");
1631 ret_transaction = _account_end_transaction(g_hAccountDB, TRUE);
1637 int _account_update_to_db_by_id(int pid, uid_t uid, account_s* account, int account_id)
1639 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
1640 ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
1641 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1642 int error_code = _ACCOUNT_ERROR_NONE;
1643 account_s* data = (account_s*)account;
1645 pthread_mutex_lock(&account_mutex);
1647 error_code = _account_update_account(pid, uid, data, account_id);
1649 if (error_code != _ACCOUNT_ERROR_NONE) {
1650 pthread_mutex_unlock(&account_mutex);
1654 pthread_mutex_unlock(&account_mutex);
1656 char buf[64] = {0,};
1657 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_UPDATE, account_id);
1658 _account_insert_delete_update_notification_send(buf);
1660 return _ACCOUNT_ERROR_NONE;
1663 int _account_update_to_db_by_id_ex(account_s* account, int account_id)
1665 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
1666 ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
1667 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1668 int error_code = _ACCOUNT_ERROR_NONE;
1669 account_s* data = account;
1671 pthread_mutex_lock(&account_mutex);
1673 _INFO("before update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name);
1674 error_code = _account_update_account_ex(data, account_id);
1675 _INFO("after update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name);
1677 if (error_code != _ACCOUNT_ERROR_NONE) {
1678 pthread_mutex_unlock(&account_mutex);
1682 pthread_mutex_unlock(&account_mutex);
1684 char buf[64] = {0,};
1685 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_UPDATE, account_id);
1686 _account_insert_delete_update_notification_send(buf);
1688 return _ACCOUNT_ERROR_NONE;
1691 int _account_update_to_db_by_user_name(int pid, uid_t uid, account_s* account, const char *user_name, const char *package_name)
1693 ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
1694 ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
1695 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1697 int error_code = _ACCOUNT_ERROR_NONE;
1698 account_s *data = (account_s*)account;
1700 pthread_mutex_lock(&account_mutex);
1702 error_code = _account_update_account_by_user_name(pid, uid, data, user_name, package_name);
1704 pthread_mutex_unlock(&account_mutex);
1706 char buf[64] = {0,};
1707 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_UPDATE, data->id);
1708 _account_insert_delete_update_notification_send(buf);
1713 GSList* _account_db_query_all(int pid, uid_t uid)
1715 //int error_code = _ACCOUNT_ERROR_NONE;
1716 account_stmt hstmt = NULL;
1717 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1719 GSList *account_list = NULL;
1721 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected."));
1723 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1725 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TABLE);
1726 hstmt = _account_prepare_query(g_hAccountDB, query);
1728 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1729 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1733 rc = _account_query_step(hstmt);
1735 account_s *account_record = NULL;
1737 if (rc != SQLITE_ROW) {
1738 _ERR("The record isn't found");
1742 while (rc == SQLITE_ROW) {
1743 account_record = (account_s*) malloc(sizeof(account_s));
1745 if (account_record == NULL) {
1746 ACCOUNT_FATAL("malloc Failed");
1750 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
1751 _account_convert_column_to_account(hstmt, account_record);
1752 account_list = g_slist_append(account_list, account_record);
1753 rc = _account_query_step(hstmt);
1756 rc = _account_query_finalize(hstmt);
1757 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_gslist_account_free(account_list); }, NULL, ("finalize error"));
1762 for (iter = account_list; iter != NULL; iter = g_slist_next(iter)) {
1763 account_s *account = NULL;
1764 account = (account_s*)iter->data;
1765 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, account->id, (void*)account);
1766 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, account->id, (void*)account);
1770 if (hstmt != NULL) {
1771 rc = _account_query_finalize(hstmt);
1772 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_gslist_account_free(account_list); }, NULL, ("finalize error"));
1777 _remove_sensitive_info_from_non_owning_account_slist(account_list, pid, uid);
1779 return account_list;
1782 int _account_update_sync_status_by_id(uid_t uid, int account_db_id, const int sync_status)
1784 int error_code = _ACCOUNT_ERROR_NONE;
1785 account_stmt hstmt = NULL;
1786 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1790 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1791 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1792 if ((sync_status < 0) || (sync_status >= _ACCOUNT_SYNC_MAX)) {
1793 ACCOUNT_SLOGE("(%s)-(%d) sync_status is less than 0 or more than enum max.\n", __FUNCTION__, __LINE__);
1794 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1797 pthread_mutex_lock(&account_mutex);
1799 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1801 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_db_id);
1803 rc = _account_get_record_count(g_hAccountDB, query);
1805 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1806 pthread_mutex_unlock(&account_mutex);
1807 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1808 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1812 ACCOUNT_SLOGE("account_update_sync_status_by_id : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
1813 pthread_mutex_unlock(&account_mutex);
1814 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1817 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1819 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET sync_support=? WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
1820 hstmt = _account_prepare_query(g_hAccountDB, query);
1822 _account_query_bind_int(hstmt, count, sync_status);
1824 rc = _account_query_step(hstmt);
1826 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1827 pthread_mutex_unlock(&account_mutex);
1828 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1829 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1832 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_DB_FAILED,
1833 ("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB)));
1835 rc = _account_query_finalize(hstmt);
1836 if (rc != _ACCOUNT_ERROR_NONE) {
1837 ACCOUNT_ERROR("_account_query_finalize error");
1838 pthread_mutex_unlock(&account_mutex);
1842 char buf[64] = {0,};
1843 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_SYNC_UPDATE, account_db_id);
1844 _account_insert_delete_update_notification_send(buf);
1847 error_code = _ACCOUNT_ERROR_NONE;
1850 if (hstmt != NULL) {
1851 rc = _account_query_finalize(hstmt);
1852 pthread_mutex_unlock(&account_mutex);
1853 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1857 pthread_mutex_unlock(&account_mutex);
1862 int _account_query_account_by_account_id(int pid, uid_t uid, int account_db_id, account_s *account_record)
1864 _INFO("_account_query_account_by_account_id() start, account_db_id=[%d]", account_db_id);
1866 int error_code = _ACCOUNT_ERROR_NONE;
1867 account_stmt hstmt = NULL;
1868 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1871 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1872 ACCOUNT_RETURN_VAL(account_record != NULL, {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
1873 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1875 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1877 ACCOUNT_DEBUG("starting db operations");
1879 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
1880 hstmt = _account_prepare_query(g_hAccountDB, query);
1881 rc = _account_db_err_code(g_hAccountDB);
1882 _INFO("after _account_prepare_query, rc=[%d]", rc);
1884 if (rc == SQLITE_PERM) {
1885 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1886 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1889 ACCOUNT_DEBUG("before _account_query_step");
1890 rc = _account_query_step(hstmt);
1891 ACCOUNT_DEBUG("after _account_query_step returned [%d]", rc);
1892 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1894 while (rc == SQLITE_ROW) {
1895 ACCOUNT_DEBUG("before _account_convert_column_to_account");
1896 _account_convert_column_to_account(hstmt, account_record);
1897 ACCOUNT_DEBUG("after _account_convert_column_to_account");
1898 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]);
1899 rc = _account_query_step(hstmt);
1902 ACCOUNT_DEBUG("account_record->id=[%d]", account_record->id);
1904 rc = _account_query_finalize(hstmt);
1905 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1907 ACCOUNT_DEBUG("before _account_query_capability_by_account_id");
1908 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, account_record->id, (void*)account_record);
1909 ACCOUNT_DEBUG("after _account_query_capability_by_account_id");
1911 ACCOUNT_DEBUG("before _account_query_custom_by_account_id");
1912 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, account_record->id, (void*)account_record);
1913 ACCOUNT_DEBUG("after _account_query_custom_by_account_id");
1916 error_code = _ACCOUNT_ERROR_NONE;
1919 if (hstmt != NULL) {
1920 rc = _account_query_finalize(hstmt);
1921 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1926 _remove_sensitive_info_from_non_owning_account(account_record, pid, uid);
1928 pthread_mutex_unlock(&account_mutex);
1929 ACCOUNT_DEBUG("_account_query_account_by_account_id end [%d]", error_code);
1934 GList* _account_query_account_by_user_name(int pid, uid_t uid, const char *user_name, int *error_code)
1936 *error_code = _ACCOUNT_ERROR_NONE;
1937 account_stmt hstmt = NULL;
1938 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1940 account_s *account_head = NULL;
1942 if (user_name == NULL) {
1943 _ERR("USER NAME IS NULL");
1944 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
1948 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1950 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ?", ACCOUNT_TABLE);
1952 hstmt = _account_prepare_query(g_hAccountDB, query);
1954 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1955 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1956 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
1960 int binding_count = 1;
1961 _account_query_bind_text(hstmt, binding_count++, user_name);
1963 rc = _account_query_step(hstmt);
1965 if (rc != SQLITE_ROW) {
1966 _ERR("The record isn't found");
1967 *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1973 account_head = (account_s*) malloc(sizeof(account_s));
1974 if (account_head == NULL) {
1975 ACCOUNT_FATAL("malloc Failed");
1976 if (hstmt != NULL) {
1977 rc = _account_query_finalize(hstmt);
1978 if (rc != _ACCOUNT_ERROR_NONE) {
1979 _ERR("finalize error");
1985 *error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
1988 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
1990 while (rc == SQLITE_ROW) {
1991 account_s* account_record = NULL;
1993 account_record = (account_s*) malloc(sizeof(account_s));
1995 if (account_record == NULL) {
1996 ACCOUNT_FATAL("malloc Failed");
1999 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2001 _account_convert_column_to_account(hstmt, account_record);
2003 account_head->account_list = g_list_append(account_head->account_list, account_record);
2005 rc = _account_query_step(hstmt);
2009 rc = _account_query_finalize(hstmt);
2011 if (rc != _ACCOUNT_ERROR_NONE) {
2012 _ERR("finalize error");
2021 tmp = g_list_length(account_head->account_list);
2023 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2024 //account_h account;
2025 //account = (account_h)iter->data;
2027 account_s *testaccount = (account_s*)iter->data;
2029 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, testaccount->id, (void*)testaccount);
2030 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, testaccount->id, (void*)testaccount);
2033 *error_code = _ACCOUNT_ERROR_NONE;
2036 if (hstmt != NULL) {
2037 rc = _account_query_finalize(hstmt);
2038 if (rc != _ACCOUNT_ERROR_NONE) {
2039 _ERR("finalize error");
2045 pthread_mutex_unlock(&account_mutex);
2048 _remove_sensitive_info_from_non_owning_account_list(account_head->account_list, pid, uid);
2049 GList* result = account_head->account_list;
2050 _ACCOUNT_FREE(account_head);
2058 _account_query_account_by_capability(int pid, uid_t uid, const char* capability_type, const int capability_value, int *error_code)
2060 *error_code = _ACCOUNT_ERROR_NONE;
2061 account_stmt hstmt = NULL;
2062 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2065 ACCOUNT_RETURN_VAL((capability_type != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("capability_type IS NULL"));
2067 if ((capability_value < 0) || (capability_value >= _ACCOUNT_CAPABILITY_STATE_MAX)) {
2068 ACCOUNT_SLOGE("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
2069 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
2073 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), { *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED; }, NULL, ("The database isn't connected."));
2075 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2077 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=? AND value=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
2079 hstmt = _account_prepare_query(g_hAccountDB, query);
2081 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2082 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2083 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
2087 int binding_count = 1;
2088 _account_query_bind_text(hstmt, binding_count++, capability_type);
2089 _account_query_bind_int(hstmt, binding_count++, capability_value);
2091 rc = _account_query_step(hstmt);
2093 account_s* account_head = NULL;
2095 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2099 account_head = (account_s*) malloc(sizeof(account_s));
2100 if (account_head == NULL) {
2101 ACCOUNT_FATAL("malloc Failed");
2102 if (hstmt != NULL) {
2103 rc = _account_query_finalize(hstmt);
2104 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), { *error_code = rc; }, NULL, ("finalize error"));
2107 *error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
2110 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2112 while (rc == SQLITE_ROW) {
2113 account_s* account_record = NULL;
2115 account_record = (account_s*) malloc(sizeof(account_s));
2117 if (account_record == NULL) {
2118 ACCOUNT_FATAL("malloc Failed");
2121 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2123 _account_convert_column_to_account(hstmt, account_record);
2125 account_head->account_list = g_list_append(account_head->account_list, account_record);
2127 rc = _account_query_step(hstmt);
2131 rc = _account_query_finalize(hstmt);
2132 ACCOUNT_CATCH_ERROR_P((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2138 tmp = g_list_length(account_head->account_list);
2140 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2141 //account_h account = NULL;
2142 //account = (account_h)iter->data;
2143 account_s* testaccount = (account_s*)iter->data;
2145 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, testaccount->id, (void*)testaccount);
2146 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, testaccount->id, (void*)testaccount);
2150 *error_code = _ACCOUNT_ERROR_NONE;
2153 if (hstmt != NULL) {
2154 rc = _account_query_finalize(hstmt);
2155 if (rc != _ACCOUNT_ERROR_NONE) {
2157 _ERR("finalize error");
2162 if (*error_code != _ACCOUNT_ERROR_NONE && account_head) {
2163 _account_glist_account_free(account_head->account_list);
2164 _ACCOUNT_FREE(account_head);
2165 account_head = NULL;
2168 pthread_mutex_unlock(&account_mutex);
2171 _remove_sensitive_info_from_non_owning_account_list(account_head->account_list, pid, uid);
2172 GList* result = account_head->account_list;
2173 _ACCOUNT_FREE(account_head);
2180 GList* _account_query_account_by_capability_type(int pid, uid_t uid, const char* capability_type, int *error_code)
2182 *error_code = _ACCOUNT_ERROR_NONE;
2183 account_stmt hstmt = NULL;
2184 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2187 ACCOUNT_RETURN_VAL((capability_type != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("capability_type IS NULL"));
2188 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), { *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED; },
2189 NULL, ("The database isn't connected."));
2191 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2193 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
2195 hstmt = _account_prepare_query(g_hAccountDB, query);
2197 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2198 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2199 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
2203 int binding_count = 1;
2204 _account_query_bind_text(hstmt, binding_count++, capability_type);
2206 rc = _account_query_step(hstmt);
2208 account_s* account_head = NULL;
2210 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2214 account_head = (account_s*) malloc(sizeof(account_s));
2215 if (account_head == NULL) {
2216 ACCOUNT_FATAL("malloc Failed");
2217 if (hstmt != NULL) {
2218 rc = _account_query_finalize(hstmt);
2219 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), { *error_code = rc; }, NULL, ("finalize error"));
2222 *error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
2225 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2227 while (rc == SQLITE_ROW) {
2228 account_s* account_record = NULL;
2230 account_record = (account_s*) malloc(sizeof(account_s));
2232 if (account_record == NULL) {
2233 ACCOUNT_FATAL("malloc Failed");
2236 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2238 _account_convert_column_to_account(hstmt, account_record);
2240 account_head->account_list = g_list_append(account_head->account_list, account_record);
2242 rc = _account_query_step(hstmt);
2246 rc = _account_query_finalize(hstmt);
2247 ACCOUNT_CATCH_ERROR_P((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2253 tmp = g_list_length(account_head->account_list);
2255 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2256 account_s* testaccount = (account_s*)iter->data;
2258 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, testaccount->id, (void*)testaccount);
2259 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, testaccount->id, (void*)testaccount);
2263 *error_code = _ACCOUNT_ERROR_NONE;
2266 if (hstmt != NULL) {
2267 rc = _account_query_finalize(hstmt);
2268 if (rc != _ACCOUNT_ERROR_NONE) {
2270 _ERR("finalize error");
2275 if ((*error_code != _ACCOUNT_ERROR_NONE) && account_head) {
2276 _account_glist_account_free(account_head->account_list);
2277 _ACCOUNT_FREE(account_head);
2278 account_head = NULL;
2281 pthread_mutex_unlock(&account_mutex);
2284 _remove_sensitive_info_from_non_owning_account_list(account_head->account_list, pid, uid);
2285 GList* result = account_head->account_list;
2286 _ACCOUNT_FREE(account_head);
2293 GList* account_server_query_account_by_package_name(const char* package_name, int *error_code, int pid, uid_t uid)
2295 _INFO("account_server_query_account_by_package_name start");
2297 GList * account_list = NULL;
2298 *error_code = _ACCOUNT_ERROR_NONE;
2300 ACCOUNT_RETURN_VAL((package_name != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("PACKAGE NAME IS NULL"));
2301 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), { *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED; }, NULL, ("The database isn't connected."));
2303 account_list = _account_query_account_by_package_name(g_hAccountDB, package_name, error_code, pid, uid);
2305 _INFO("account_server_query_account_by_package_name end");
2307 return account_list;
2310 int account_server_delete_account_by_package_name(const char* package_name, bool permission, int pid, uid_t uid)
2312 _INFO("account_db_delete_account_by_package_name");
2314 int error_code = _ACCOUNT_ERROR_NONE;
2316 ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
2317 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2319 error_code = _account_delete_account_by_package_name(g_hAccountDB, package_name, permission, pid, uid);
2321 _INFO("account_server_delete_account_by_package_name end");
2326 int _account_delete(int pid, uid_t uid, int account_id)
2328 int error_code = _ACCOUNT_ERROR_NONE;
2329 account_stmt hstmt = NULL;
2330 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2332 int ret_transaction = 0;
2333 bool is_success = FALSE;
2335 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2338 /* Check requested ID to delete */
2339 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id=%d", ACCOUNT_TABLE, account_id);
2341 count = _account_get_record_count(g_hAccountDB, query);
2343 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2344 pthread_mutex_unlock(&account_mutex);
2345 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2346 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2350 ACCOUNT_ERROR("account id(%d) is not exist. count(%d)\n", account_id, count);
2351 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
2354 /* Check permission of requested appid */
2355 char* current_appid = NULL;
2356 char *package_name = NULL;
2358 current_appid = _account_get_current_appid(pid, uid);
2360 error_code = _account_get_package_name_from_account_id(account_id, &package_name);
2362 if (error_code != _ACCOUNT_ERROR_NONE) {
2363 ACCOUNT_ERROR("No package name with account_id\n");
2364 _ACCOUNT_FREE(current_appid);
2365 _ACCOUNT_FREE(package_name);
2366 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
2368 ACCOUNT_DEBUG("DELETE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name);
2370 error_code = _account_check_appid_group_with_package_name(current_appid, package_name, uid);
2372 _ACCOUNT_FREE(current_appid);
2373 _ACCOUNT_FREE(package_name);
2375 if (error_code != _ACCOUNT_ERROR_NONE) {
2376 ACCOUNT_ERROR("No permission to delete\n");
2377 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2380 /* transaction control required*/
2381 ret_transaction = _account_begin_transaction(g_hAccountDB);
2383 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2384 pthread_mutex_unlock(&account_mutex);
2385 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2386 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2389 if (ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY) {
2390 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
2391 pthread_mutex_unlock(&account_mutex);
2392 return _ACCOUNT_ERROR_DATABASE_BUSY;
2395 if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2396 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
2397 pthread_mutex_unlock(&account_mutex);
2398 return ret_transaction;
2401 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2402 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
2404 hstmt = _account_prepare_query(g_hAccountDB, query);
2406 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2407 pthread_mutex_unlock(&account_mutex);
2408 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2409 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2412 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2413 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2415 rc = _account_query_step(hstmt);
2416 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2418 rc = _account_query_finalize(hstmt);
2420 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2423 ACCOUNT_MEMSET(query, 0, sizeof(query));
2425 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
2427 hstmt = _account_prepare_query(g_hAccountDB, query);
2428 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2429 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2431 rc = _account_query_step(hstmt);
2432 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
2434 rc = _account_query_finalize(hstmt);
2435 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2438 /* delete custom data */
2439 ACCOUNT_MEMSET(query, 0, sizeof(query));
2441 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
2443 hstmt = _account_prepare_query(g_hAccountDB, query);
2445 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2446 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2448 rc = _account_query_step(hstmt);
2449 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
2451 rc = _account_query_finalize(hstmt);
2452 ACCOUNT_CATCH_ERROR(rc == _ACCOUNT_ERROR_NONE, {}, rc, ("finalize error", account_id, rc));
2458 if (hstmt != NULL) {
2459 rc = _account_query_finalize(hstmt);
2460 if (rc != _ACCOUNT_ERROR_NONE) {
2461 ACCOUNT_ERROR("rc (%d)", rc);
2468 ret_transaction = _account_end_transaction(g_hAccountDB, is_success);
2470 if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2471 ACCOUNT_ERROR("account_delete:_account_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
2473 if (is_success == true) {
2474 char buf[64] = {0,};
2475 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_DELETE, account_id);
2476 _account_insert_delete_update_notification_send(buf);
2480 pthread_mutex_unlock(&account_mutex);
2485 static int _account_query_account_by_username_and_package(const char* username, const char* package_name, account_s *account)
2489 int error_code = _ACCOUNT_ERROR_NONE;
2490 account_stmt hstmt = NULL;
2491 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2493 int binding_count = 1;
2495 ACCOUNT_RETURN_VAL((username != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("username IS NULL"));
2496 ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name IS NULL"));
2497 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2498 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2500 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2502 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
2503 hstmt = _account_prepare_query(g_hAccountDB, query);
2505 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2506 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2507 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2510 _account_query_bind_text(hstmt, binding_count++, username);
2511 _account_query_bind_text(hstmt, binding_count++, package_name);
2513 rc = _account_query_step(hstmt);
2514 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2516 account_s *account_record = account;
2518 while (rc == SQLITE_ROW) {
2519 _account_convert_column_to_account(hstmt, account_record);
2520 rc = _account_query_step(hstmt);
2523 rc = _account_query_finalize(hstmt);
2524 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2525 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, account_record->id, (void*)account_record);
2526 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, account_record->id, (void*)account_record);
2529 error_code = _ACCOUNT_ERROR_NONE;
2532 if (hstmt != NULL) {
2533 rc = _account_query_finalize(hstmt);
2534 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2538 pthread_mutex_unlock(&account_mutex);
2543 int _account_create(account_s **account)
2546 ACCOUNT_SLOGE("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__);
2547 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2550 account_s *data = (account_s*)malloc(sizeof(account_s));
2553 ACCOUNT_FATAL("Memory Allocation Failed");
2554 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
2556 ACCOUNT_MEMSET(data, 0, sizeof(account_s));
2558 /*Setting account as visible by default*/
2559 //data->secret = _ACCOUNT_SECRECY_VISIBLE;
2561 /*Setting account as not supporting sync by default*/
2562 //data->sync_support = _ACCOUNT_SYNC_NOT_SUPPORT;
2566 return _ACCOUNT_ERROR_NONE;
2569 int _account_destroy(account_s *account)
2571 account_s *data = account;
2573 ACCOUNT_RETURN_VAL((data != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Account handle is null!"));
2575 _account_free_account_with_items(data);
2577 return _ACCOUNT_ERROR_NONE;
2580 int _account_get_account_id(account_s* account, int *account_id)
2583 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2586 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2588 *account_id = account->id;
2590 return _ACCOUNT_ERROR_NONE;
2593 int _account_delete_from_db_by_user_name(int pid, uid_t uid, const char *user_name, const char *package_name)
2595 _INFO("[%s][%s]", user_name, package_name);
2597 int error_code = _ACCOUNT_ERROR_NONE;
2598 account_stmt hstmt = NULL;
2599 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2601 int ret_transaction = 0;
2602 bool is_success = FALSE;
2603 account_s *account = NULL;
2604 int binding_count = 1;
2605 int account_id = -1;
2607 ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is null!"));
2608 ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
2609 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2611 /* Check permission of requested appid */
2612 char* current_appid = NULL;
2613 char* package_name_temp = NULL;
2615 current_appid = _account_get_current_appid(pid, uid);
2617 package_name_temp = _account_dup_text(package_name);
2619 ACCOUNT_DEBUG("DELETE:user_name[%s],current_appid[%s], package_name[%s]", user_name, current_appid, package_name_temp);
2621 error_code = _account_check_appid_group_with_package_name(current_appid, package_name_temp, uid);
2623 _ACCOUNT_FREE(current_appid);
2624 _ACCOUNT_FREE(package_name_temp);
2626 if (error_code != _ACCOUNT_ERROR_NONE) {
2627 ACCOUNT_ERROR("No permission to delete\n");
2628 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2631 rc = _account_create(&account);
2632 rc = _account_query_account_by_username_and_package(user_name, package_name, account);
2636 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2637 _account_destroy(account);
2638 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2639 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2643 account_s* account_data = (account_s*)account;
2645 rc = _account_get_account_id(account_data, &account_id);
2647 rc = _account_destroy(account);
2649 /* transaction control required*/
2650 ret_transaction = _account_begin_transaction(g_hAccountDB);
2652 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2653 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2654 pthread_mutex_unlock(&account_mutex);
2655 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2659 if (ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY) {
2660 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
2661 pthread_mutex_unlock(&account_mutex);
2662 return _ACCOUNT_ERROR_DATABASE_BUSY;
2663 } else if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2664 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
2665 pthread_mutex_unlock(&account_mutex);
2666 return ret_transaction;
2669 /* delete custom data */
2670 ACCOUNT_MEMSET(query, 0, sizeof(query));
2671 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = ?", ACCOUNT_CUSTOM_TABLE);
2673 hstmt = _account_prepare_query(g_hAccountDB, query);
2675 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2676 _account_end_transaction(g_hAccountDB, FALSE);
2677 pthread_mutex_unlock(&account_mutex);
2678 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2679 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2682 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2683 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2685 _account_query_bind_int(hstmt, binding_count++, account_id);
2687 rc = _account_query_step(hstmt);
2688 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2690 rc = _account_query_finalize(hstmt);
2691 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2694 /* delete capability */
2695 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", CAPABILITY_TABLE);
2697 hstmt = _account_prepare_query(g_hAccountDB, query);
2699 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2700 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2703 _account_query_bind_text(hstmt, binding_count++, user_name);
2704 _account_query_bind_text(hstmt, binding_count++, package_name);
2706 rc = _account_query_step(hstmt);
2707 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2709 rc = _account_query_finalize(hstmt);
2710 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2713 ACCOUNT_MEMSET(query, 0, sizeof(query));
2716 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
2718 hstmt = _account_prepare_query(g_hAccountDB, query);
2719 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2720 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2724 _account_query_bind_text(hstmt, binding_count++, user_name);
2725 _account_query_bind_text(hstmt, binding_count++, package_name);
2727 rc = _account_query_step(hstmt);
2728 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));
2730 rc = _account_query_finalize(hstmt);
2731 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2737 if (hstmt != NULL) {
2738 rc = _account_query_finalize(hstmt);
2739 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2743 ret_transaction = _account_end_transaction(g_hAccountDB, is_success);
2745 if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2746 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
2748 if (is_success == true) {
2749 char buf[64] = {0,};
2750 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_DELETE, account_id);
2751 _account_insert_delete_update_notification_send(buf);
2755 pthread_mutex_unlock(&account_mutex);
2761 int _account_get_total_count_from_db(gboolean include_hidden, int *count)
2764 ACCOUNT_SLOGE("(%s)-(%d) count is NULL.\n", __FUNCTION__, __LINE__);
2765 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2768 if (!g_hAccountDB) {
2769 ACCOUNT_ERROR("DB is not opened\n");
2770 return _ACCOUNT_ERROR_DB_NOT_OPENED;
2773 char query[1024] = {0, };
2774 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2777 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s", ACCOUNT_TABLE);
2779 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s where secret = %d", ACCOUNT_TABLE, _ACCOUNT_SECRECY_VISIBLE);
2781 *count = _account_get_record_count(g_hAccountDB, query);
2783 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2784 pthread_mutex_unlock(&account_mutex);
2785 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2786 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2791 account_stmt pStmt = NULL;
2793 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
2794 if (SQLITE_OK != rc) {
2795 ACCOUNT_SLOGE("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg(g_hAccountDB));
2796 sqlite3_finalize(pStmt);
2797 return _ACCOUNT_ERROR_DB_FAILED;
2800 rc = sqlite3_step(pStmt);
2801 if (SQLITE_ROW != rc) {
2802 ACCOUNT_ERROR("[ERROR] sqlite3_step() failed\n");
2803 sqlite3_finalize(pStmt);
2804 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
2807 ncount = sqlite3_column_int(pStmt, 0);
2811 sqlite3_finalize(pStmt);
2814 ACCOUNT_ERROR("[ERROR] Number of account : %d, End", ncount);
2815 return _ACCOUNT_ERROR_DB_FAILED;
2818 return _ACCOUNT_ERROR_NONE;
2822 int account_server_query_app_id_exist(const char* app_id)
2824 _INFO("account_server_query_app_id_exist start app_id=[%s]", app_id);
2825 int ret = _ACCOUNT_ERROR_NONE;
2827 ret = _account_type_query_app_id_exist_from_all_db(g_hAccountDB, g_hAccountGlobalDB, app_id);
2829 _INFO("account_server_query_app_id_exist end error_code=[%d]", ret);
2834 int account_server_insert_account_type_to_user_db(account_type_s *account_type, int *account_type_id, uid_t uid)
2836 ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE HANDLE IS NULL"));
2837 ACCOUNT_RETURN_VAL((account_type->app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID OF ACCOUNT TYPE IS NULL"));
2838 ACCOUNT_RETURN_VAL((account_type_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE ID IS NULL"));
2840 _INFO("account_server_insert_account_type_to_user_db start uid=[%d]", uid);
2841 int ret = _ACCOUNT_ERROR_NONE;
2843 if (_account_type_check_duplicated(g_hAccountDB, account_type->app_id) ||
2844 _account_type_check_duplicated(g_hAccountGlobalDB, account_type->app_id)) {
2845 *account_type_id = -1;
2846 return _ACCOUNT_ERROR_DUPLICATED;
2849 ret = _account_type_insert_to_db(g_hAccountDB, account_type, account_type_id);
2850 _INFO("account_server_insert_account_type_to_user_db end error_code=[%d]", ret);
2855 int account_server_delete_account_type_by_app_id_from_user_db(const char * app_id)
2857 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID OF ACCOUNT TYPE IS NULL"));
2859 _INFO("account_server_delete_account_type_by_app_id_from_user_db start");
2860 int ret = _ACCOUNT_ERROR_NONE;
2862 ret = _account_type_delete_by_app_id(g_hAccountDB, app_id);
2863 _INFO("account_server_delete_account_type_by_app_id_from_user_db end error_code=[%d]", ret);
2868 GSList* _account_type_query_provider_feature_by_app_id_from_global_db(const char* app_id, int *error_code)
2870 _INFO("_account_type_query_provider_feature_by_app_id_in_global_db app_id=%s", app_id);
2871 account_stmt hstmt = NULL;
2872 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2873 int rc = 0, binding_count = 1;
2874 GSList* feature_list = NULL;
2876 ACCOUNT_RETURN_VAL((app_id != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("APP ID IS NULL"));
2877 ACCOUNT_RETURN_VAL((error_code != NULL), {_ERR("error_code pointer is NULL"); }, NULL, (""));
2878 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; _ERR("The database isn't connected."); }, NULL, ("The database isn't connected."));
2880 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2882 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
2883 _INFO("account query=[%s]", query);
2885 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
2887 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
2888 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2889 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
2893 _INFO("before _account_query_bind_text");
2894 _account_query_bind_text(hstmt, binding_count++, app_id);
2896 rc = _account_query_step(hstmt);
2898 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"));
2900 provider_feature_s* feature_record = NULL;
2902 while (rc == SQLITE_ROW) {
2904 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
2906 if (feature_record == NULL) {
2907 ACCOUNT_FATAL("malloc Failed");
2911 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
2913 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
2915 _INFO("Adding account feature_list");
2916 feature_list = g_slist_append(feature_list, feature_record);
2918 rc = _account_query_step(hstmt);
2921 *error_code = _ACCOUNT_ERROR_NONE;
2924 if (hstmt != NULL) {
2925 rc = _account_query_finalize(hstmt);
2926 if (rc != _ACCOUNT_ERROR_NONE) {
2928 _ERR("global db fianlize error");
2932 if (*error_code != _ACCOUNT_ERROR_NONE)
2933 _account_type_gslist_feature_free(feature_list);
2935 _INFO("Returning account feature_list from global db");
2937 return feature_list;
2940 GSList* _account_type_query_provider_feature_by_app_id(const char* app_id, int *error_code)
2942 _INFO("_account_type_query_provider_feature_by_app_id app_id=%s", app_id);
2943 account_stmt hstmt = NULL;
2944 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2945 int rc = 0, binding_count = 1;
2946 GSList* feature_list = NULL;
2948 ACCOUNT_RETURN_VAL((app_id != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("APP ID IS NULL"));
2949 ACCOUNT_RETURN_VAL((error_code != NULL), {_ERR("error_code pointer is NULL"); }, NULL, (""));
2950 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), { *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED; }, NULL, ("The database isn't connected."));
2952 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2954 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
2955 _INFO("account query=[%s]", query);
2957 hstmt = _account_prepare_query(g_hAccountDB, query);
2959 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2960 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2961 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
2965 _account_query_bind_text(hstmt, binding_count++, app_id);
2967 rc = _account_query_step(hstmt);
2969 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, { *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
2970 _ERR("The record isn't found from user db. rc=[%d]", rc); },
2971 _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2973 provider_feature_s* feature_record = NULL;
2975 while (rc == SQLITE_ROW) {
2977 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
2979 if (feature_record == NULL) {
2980 ACCOUNT_FATAL("malloc Failed");
2984 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
2986 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
2988 _INFO("Adding account feature_list");
2989 feature_list = g_slist_append(feature_list, feature_record);
2991 rc = _account_query_step(hstmt);
2994 *error_code = _ACCOUNT_ERROR_NONE;
2996 rc = _account_query_finalize(hstmt);
2997 ACCOUNT_CATCH_ERROR_P((rc == _ACCOUNT_ERROR_NONE), { *error_code = rc; }, rc, ("account finalize error"));
3001 if (hstmt != NULL) {
3002 rc = _account_query_finalize(hstmt);
3003 if (rc != _ACCOUNT_ERROR_NONE) {
3005 _ERR("account fianlize error");
3009 _INFO("*error_code=[%d]", *error_code);
3011 if (*error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3012 feature_list = _account_type_query_provider_feature_by_app_id_from_global_db(app_id, error_code);
3014 if (*error_code != _ACCOUNT_ERROR_NONE) {
3015 _account_type_gslist_feature_free(feature_list);
3019 _INFO("Returning account feature_list");
3021 return feature_list;
3024 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)
3026 int error_code = _ACCOUNT_ERROR_NONE;
3027 account_stmt hstmt = NULL;
3028 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3029 int rc = 0, binding_count = 1;
3031 _INFO("_account_type_query_provider_feature_cb_by_app_id_in_global_db start app_id=%s", app_id);
3032 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3033 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3034 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3036 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3038 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
3039 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3041 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3042 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3043 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_PERMISSION_DENIED, ("global db permission denied.\n"));
3046 _account_query_bind_text(hstmt, binding_count++, app_id);
3048 rc = _account_query_step(hstmt);
3049 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"));
3051 provider_feature_s* feature_record = NULL;
3053 while (rc == SQLITE_ROW) {
3054 bool cb_ret = FALSE;
3055 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
3057 if (feature_record == NULL) {
3058 ACCOUNT_FATAL("malloc Failed");
3062 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
3064 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
3066 cb_ret = callback(feature_record->app_id, feature_record->key, user_data);
3068 _account_type_free_feature_with_items(feature_record);
3070 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returns FALSE, its iteration is stopped!!!!\n"));
3072 rc = _account_query_step(hstmt);
3075 error_code = _ACCOUNT_ERROR_NONE;
3078 if (hstmt != NULL) {
3079 rc = _account_query_finalize(hstmt);
3080 if (rc != _ACCOUNT_ERROR_NONE) {
3082 _ERR("global db finalize error[%d]", rc);
3087 _INFO("_account_type_query_provider_feature_cb_by_app_id_in_global_db end. error_code=[%d]", error_code);
3092 int _account_type_query_provider_feature_cb_by_app_id(account_type_provider_feature_cb callback, const char* app_id, void *user_data)
3094 int error_code = _ACCOUNT_ERROR_NONE;
3095 account_stmt hstmt = NULL;
3096 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3097 int rc = 0, binding_count = 1;
3099 _INFO("_account_type_query_provider_feature_cb_by_app_id start app_id=%s", app_id);
3100 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3101 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3102 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3104 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3106 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
3107 hstmt = _account_prepare_query(g_hAccountDB, query);
3109 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
3110 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3111 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3114 _account_query_bind_text(hstmt, binding_count++, app_id);
3116 rc = _account_query_step(hstmt);
3117 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found in user db.\n"));
3119 provider_feature_s* feature_record = NULL;
3121 while (rc == SQLITE_ROW) {
3122 bool cb_ret = FALSE;
3123 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
3125 if (feature_record == NULL) {
3126 ACCOUNT_FATAL("malloc Failed");
3130 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
3132 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
3134 cb_ret = callback(feature_record->app_id, feature_record->key, user_data);
3136 _account_type_free_feature_with_items(feature_record);
3138 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
3140 rc = _account_query_step(hstmt);
3143 rc = _account_query_finalize(hstmt);
3144 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3147 error_code = _ACCOUNT_ERROR_NONE;
3150 if (hstmt != NULL) {
3151 rc = _account_query_finalize(hstmt);
3152 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3156 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3157 error_code = _account_type_query_provider_feature_cb_by_app_id_from_global_db(callback, app_id, user_data);
3159 _INFO("_account_type_query_provider_feature_cb_by_app_id end");
3164 int account_type_query_provider_feature_cb_by_app_id(account_type_provider_feature_cb callback, const char* app_id, void *user_data)
3166 int error_code = _ACCOUNT_ERROR_NONE;
3168 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3169 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3170 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3172 error_code = _account_type_query_provider_feature_cb_by_app_id(callback, app_id, user_data);
3174 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3175 error_code = _account_type_query_provider_feature_cb_by_app_id_from_global_db(callback, app_id, user_data);
3180 bool _account_type_query_supported_feature_from_global_db(const char* app_id, const char* capability, int *error_code)
3182 _INFO("_account_type_query_supported_feature_in_global_db start");
3183 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3185 *error_code = _ACCOUNT_ERROR_NONE;
3187 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3188 int record_count = 0;
3190 if (app_id == NULL || capability == NULL) {
3191 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
3195 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
3197 record_count = _account_get_record_count(g_hAccountGlobalDB, query);
3199 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3200 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3201 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3204 if (record_count <= 0) {
3205 *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
3209 _INFO("_account_type_query_supported_feature_in_global_db end");
3214 bool _account_type_query_supported_feature(const char* app_id, const char* capability, int *error_code)
3216 _INFO("_account_type_query_supported_feature start");
3218 *error_code = _ACCOUNT_ERROR_NONE;
3220 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3221 int record_count = 0;
3223 if (app_id == NULL || capability == NULL) {
3224 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
3228 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
3230 record_count = _account_get_record_count(g_hAccountDB, query);
3232 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
3233 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3234 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3238 if (record_count <= 0) {
3239 bool is_exist = false;
3240 is_exist = _account_type_query_supported_feature_from_global_db(app_id, capability, error_code);
3245 _INFO("_account_type_query_supported_feature end");
3251 static int _account_type_update_provider_feature(sqlite3 *account_db_handle, account_type_s *account_type, const char* app_id)
3254 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3255 account_stmt hstmt = NULL;
3257 ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
3259 if (g_slist_length(account_type->provider_feature_list) == 0) {
3260 ACCOUNT_ERROR("no feature\n");
3261 return _ACCOUNT_ERROR_NONE;
3264 ACCOUNT_DEBUG("app id", app_id);
3266 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3268 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id=? ", PROVIDER_FEATURE_TABLE);
3269 hstmt = _account_prepare_query(account_db_handle, query);
3271 if (_account_db_err_code(account_db_handle) == SQLITE_PERM) {
3272 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(account_db_handle));
3273 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3277 _account_query_bind_text(hstmt, count++, app_id);
3278 rc = _account_query_step(hstmt);
3280 if (rc != SQLITE_DONE) {
3281 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3282 return _ACCOUNT_ERROR_DB_FAILED;
3284 rc = _account_query_finalize(hstmt);
3285 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3290 for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
3293 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3294 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
3295 "(?, ?) ", PROVIDER_FEATURE_TABLE);
3297 hstmt = _account_prepare_query(account_db_handle, query);
3299 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg(account_db_handle)));
3301 provider_feature_s* feature_data = NULL;
3302 feature_data = (provider_feature_s*)iter->data;
3304 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
3305 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3306 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
3307 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3309 rc = _account_query_step(hstmt);
3311 if (rc != SQLITE_DONE) {
3312 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3315 rc = _account_query_finalize(hstmt);
3316 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3320 return _ACCOUNT_ERROR_NONE;
3323 static int _account_type_update_label(sqlite3 *account_db_handle, account_type_s *account_type, const char* app_id)
3326 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3327 account_stmt hstmt = NULL;
3329 ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
3331 if (g_slist_length(account_type->label_list) == 0)
3332 return _ACCOUNT_ERROR_NONE;
3334 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3336 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId=? ", LABEL_TABLE);
3337 hstmt = _account_prepare_query(account_db_handle, query);
3339 if (_account_db_err_code(account_db_handle) == SQLITE_PERM) {
3340 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(account_db_handle));
3341 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3345 _account_query_bind_text(hstmt, count++, app_id);
3346 rc = _account_query_step(hstmt);
3348 if (rc != SQLITE_DONE) {
3349 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3350 return _ACCOUNT_ERROR_DB_FAILED;
3352 rc = _account_query_finalize(hstmt);
3353 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3358 for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
3361 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3362 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
3363 "(?, ?, ?) ", LABEL_TABLE);
3365 hstmt = _account_prepare_query(account_db_handle, query);
3367 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg(account_db_handle)));
3369 label_s* label_data = NULL;
3370 label_data = (label_s*)iter->data;
3372 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
3373 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3374 ret = _account_query_bind_text(hstmt, count++, label_data->label);
3375 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3376 ret = _account_query_bind_text(hstmt, count++, label_data->locale);
3377 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3379 rc = _account_query_step(hstmt);
3381 if (rc != SQLITE_DONE) {
3382 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3385 rc = _account_query_finalize(hstmt);
3386 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3390 return _ACCOUNT_ERROR_NONE;
3394 static int _account_type_update_account(sqlite3 *account_db_handle, account_type_s *account_type, const char* app_id)
3396 int rc = 0, binding_count = 1;
3397 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3398 int error_code = _ACCOUNT_ERROR_NONE;
3399 account_stmt hstmt = NULL;
3401 if (!account_type->app_id) {
3402 ACCOUNT_ERROR("app id is mandetory field, it can not be NULL!!!!\n");
3403 return _ACCOUNT_ERROR_INVALID_PARAMETER;
3406 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3407 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET AppId=?, ServiceProviderId=?, IconPath=?, "
3408 "SmallIconPath=?, MultipleAccountSupport=? WHERE AppId=? ", ACCOUNT_TYPE_TABLE);
3410 hstmt = _account_prepare_query(account_db_handle, query);
3412 if (_account_db_err_code(account_db_handle) == SQLITE_PERM) {
3413 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(account_db_handle));
3414 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3415 } else if (_account_db_err_code(account_db_handle) == SQLITE_BUSY) {
3416 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(account_db_handle));
3417 return _ACCOUNT_ERROR_DATABASE_BUSY;
3420 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg(account_db_handle)));
3422 binding_count = _account_type_convert_account_to_sql(account_type, hstmt, query);
3423 _account_query_bind_text(hstmt, binding_count++, app_id);
3425 rc = _account_query_step(hstmt);
3426 if (rc != SQLITE_DONE)
3427 ACCOUNT_ERROR("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3429 rc = _account_query_finalize(hstmt);
3430 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3434 error_code = _account_type_update_label(account_db_handle, account_type, app_id);
3435 /* update provider feature */
3436 error_code = _account_type_update_provider_feature(account_db_handle, account_type, app_id);
3441 int _account_type_update_to_db_by_app_id(account_type_s* account_type, const char* app_id)
3443 int error_code = _ACCOUNT_ERROR_NONE;
3445 ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
3446 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3447 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3449 account_type_s* data = account_type;
3451 pthread_mutex_lock(&account_mutex);
3453 error_code = _account_type_update_account(g_hAccountDB, data, app_id);
3455 pthread_mutex_unlock(&account_mutex);
3460 GSList* _account_type_get_label_list_by_app_id_from_global_db(const char* app_id, int *error_code)
3462 *error_code = _ACCOUNT_ERROR_NONE;
3463 account_stmt hstmt = NULL;
3464 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3465 int rc = 0, binding_count = 1;
3466 GSList* label_list = NULL;
3468 ACCOUNT_RETURN_VAL((app_id != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("APP ID IS NULL"));
3469 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3471 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3473 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3474 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3476 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3477 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3478 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3483 _account_query_bind_text(hstmt, binding_count++, app_id);
3485 rc = _account_query_step(hstmt);
3486 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"));
3488 label_s* label_record = NULL;
3490 while (rc == SQLITE_ROW) {
3491 label_record = (label_s*) malloc(sizeof(label_s));
3493 if (label_record == NULL) {
3494 ACCOUNT_FATAL("malloc Failed");
3498 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3500 _account_type_convert_column_to_label(hstmt, label_record);
3502 _INFO("Adding account label_list");
3503 label_list = g_slist_append(label_list, label_record);
3505 rc = _account_query_step(hstmt);
3508 *error_code = _ACCOUNT_ERROR_NONE;
3511 if (hstmt != NULL) {
3512 rc = _account_query_finalize(hstmt);
3513 if (rc != _ACCOUNT_ERROR_NONE)
3514 _ERR("global db finalize error[%d]", rc);
3518 _INFO("Returning account global label_list");
3523 GSList* _account_type_get_label_list_by_app_id(const char* app_id, int *error_code)
3525 *error_code = _ACCOUNT_ERROR_NONE;
3526 account_stmt hstmt = NULL;
3527 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3528 int rc = 0, binding_count = 1;
3529 GSList* label_list = NULL;
3531 ACCOUNT_RETURN_VAL((app_id != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("APP ID IS NULL"));
3532 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), { *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED; }, NULL, ("The database isn't connected."));
3534 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3536 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3537 hstmt = _account_prepare_query(g_hAccountDB, query);
3539 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
3540 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3541 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3545 _account_query_bind_text(hstmt, binding_count++, app_id);
3547 rc = _account_query_step(hstmt);
3548 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3550 label_s* label_record = NULL;
3552 while (rc == SQLITE_ROW) {
3553 label_record = (label_s*) malloc(sizeof(label_s));
3555 if (label_record == NULL) {
3556 ACCOUNT_FATAL("malloc Failed");
3560 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3562 _account_type_convert_column_to_label(hstmt, label_record);
3564 _INFO("Adding account label_list");
3565 label_list = g_slist_append(label_list, label_record);
3567 rc = _account_query_step(hstmt);
3570 rc = _account_query_finalize(hstmt);
3571 if (rc != _ACCOUNT_ERROR_NONE) {
3572 _account_type_gslist_label_free(label_list);
3574 _ERR("finalize error");
3579 *error_code = _ACCOUNT_ERROR_NONE;
3582 if (hstmt != NULL) {
3583 rc = _account_query_finalize(hstmt);
3584 if (rc != _ACCOUNT_ERROR_NONE) {
3585 _account_type_gslist_label_free(label_list);
3587 _ERR("finalize error");
3593 if (*error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3594 label_list = _account_type_get_label_list_by_app_id_from_global_db(app_id, error_code);
3596 _INFO("Returning account label_list");
3601 int _account_type_query_label_by_app_id_from_global_db(account_type_label_cb callback, const char* app_id, void *user_data)
3603 int error_code = _ACCOUNT_ERROR_NONE;
3604 account_stmt hstmt = NULL;
3605 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3606 int rc = 0, binding_count = 1;
3608 _INFO("account_type_query_label_by_app_id_from_global_db start");
3610 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3611 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3612 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3614 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3616 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3617 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3619 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3620 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3621 error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3625 _account_query_bind_text(hstmt, binding_count++, app_id);
3627 rc = _account_query_step(hstmt);
3628 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3630 label_s* label_record = NULL;
3632 while (rc == SQLITE_ROW) {
3633 bool cb_ret = FALSE;
3634 label_record = (label_s*) malloc(sizeof(label_s));
3636 if (label_record == NULL) {
3637 ACCOUNT_FATAL("malloc Failed");
3641 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3643 _account_type_convert_column_to_label(hstmt, label_record);
3645 cb_ret = callback(label_record->app_id, label_record->label , label_record->locale, user_data);
3647 _account_type_free_label_with_items(label_record);
3649 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
3651 rc = _account_query_step(hstmt);
3654 error_code = _ACCOUNT_ERROR_NONE;
3657 if (hstmt != NULL) {
3658 rc = _account_query_finalize(hstmt);
3659 if (rc != _ACCOUNT_ERROR_NONE)
3660 _ERR("global db finalize error[%d]", rc);
3664 _INFO("account_type_query_label_by_app_id_from_global_db end [%d]", error_code);
3669 int _account_type_query_label_by_app_id(account_type_label_cb callback, const char* app_id, void *user_data)
3671 int error_code = _ACCOUNT_ERROR_NONE;
3672 account_stmt hstmt = NULL;
3673 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3674 int rc = 0, binding_count = 1;
3676 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3677 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3678 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3680 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3682 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3683 hstmt = _account_prepare_query(g_hAccountDB, query);
3685 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
3686 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3687 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3690 _account_query_bind_text(hstmt, binding_count++, app_id);
3692 rc = _account_query_step(hstmt);
3693 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3695 label_s* label_record = NULL;
3697 while (rc == SQLITE_ROW) {
3698 bool cb_ret = FALSE;
3699 label_record = (label_s*) malloc(sizeof(label_s));
3701 if (label_record == NULL) {
3702 ACCOUNT_FATAL("malloc Failed");
3706 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3708 _account_type_convert_column_to_label(hstmt, label_record);
3710 cb_ret = callback(label_record->app_id, label_record->label , label_record->locale, user_data);
3712 _account_type_free_label_with_items(label_record);
3714 //ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
3715 if (cb_ret != TRUE) {
3716 _INFO("Callback func returs FALSE, its iteration is stopped!!!!\n");
3720 rc = _account_query_step(hstmt);
3723 rc = _account_query_finalize(hstmt);
3724 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3727 error_code = _ACCOUNT_ERROR_NONE;
3730 if (hstmt != NULL) {
3731 rc = _account_query_finalize(hstmt);
3732 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3736 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
3737 error_code = account_type_query_label_by_app_id_from_global_db(callback, app_id, user_data);
3743 int account_type_query_label_by_app_id(account_type_label_cb callback, const char* app_id, void *user_data)
3745 int error_code = _ACCOUNT_ERROR_NONE;
3747 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3748 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3749 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3751 error_code = _account_type_query_label_by_app_id(callback, app_id, user_data);
3753 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3754 error_code = _account_type_query_label_by_app_id_from_global_db(callback, app_id, user_data);
3759 bool _account_get_label_text_cb(char* app_id, char* label, char* locale, void *user_data)
3761 account_type_s *data = (account_type_s*)user_data;
3763 label_s *label_data = (label_s*)malloc(sizeof(label_s));
3765 if (label_data == NULL) {
3766 ACCOUNT_FATAL("_account_get_label_text_cb : MALLOC FAIL\n");
3769 ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
3771 label_data->app_id = _account_dup_text(app_id);
3772 label_data->label = _account_dup_text(label);
3773 label_data->locale = _account_dup_text(locale);
3775 data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
3780 bool _account_get_provider_feature_cb(char* app_id, char* key, void* user_data)
3782 account_type_s *data = (account_type_s*)user_data;
3784 provider_feature_s *feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
3786 if (feature_data == NULL) {
3787 ACCOUNT_FATAL("_account_get_provider_feature_cb : MALLOC FAIL\n");
3790 ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
3792 feature_data->app_id = _account_dup_text(app_id);
3793 feature_data->key = _account_dup_text(key);
3795 data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
3800 int _account_type_query_by_app_id_from_global_db(const char* app_id, account_type_s** account_type_record)
3802 _INFO("_account_type_query_by_app_id_from_global_db start");
3804 int error_code = _ACCOUNT_ERROR_NONE;
3805 account_stmt hstmt = NULL;
3806 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3807 int rc = 0, binding_count = 1;
3809 ACCOUNT_RETURN_VAL((app_id != 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3810 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3811 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3813 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3815 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
3816 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3818 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3819 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3820 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3823 _account_query_bind_text(hstmt, binding_count++, app_id);
3825 rc = _account_query_step(hstmt);
3826 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3828 *account_type_record = create_empty_account_type_instance();
3830 while (rc == SQLITE_ROW) {
3831 _account_type_convert_column_to_account_type(hstmt, *account_type_record);
3832 rc = _account_query_step(hstmt);
3835 rc = _account_query_finalize(hstmt);
3836 ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {_ERR("global db finalize error rc=[%d]", rc); }, rc, ("finalize error"));
3837 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb, app_id, (void*)(*account_type_record));
3838 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, app_id, (void*)(*account_type_record));
3841 error_code = _ACCOUNT_ERROR_NONE;
3844 if (hstmt != NULL) {
3845 rc = _account_query_finalize(hstmt);
3846 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3850 _INFO("_account_type_query_by_app_id_from_global_db end [%d]", error_code);
3855 int _account_type_query_by_app_id(const char* app_id, account_type_s** account_type_record)
3857 _INFO("_account_type_query_by_app_id start");
3859 int error_code = _ACCOUNT_ERROR_NONE;
3860 account_stmt hstmt = NULL;
3861 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3862 int rc = 0, binding_count = 1;
3864 ACCOUNT_RETURN_VAL((app_id != 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3865 ACCOUNT_RETURN_VAL((account_type_record != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("account type record(account_type_s**) is NULL"));
3866 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3868 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3870 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
3871 hstmt = _account_prepare_query(g_hAccountDB, query);
3873 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
3874 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3875 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3878 _account_query_bind_text(hstmt, binding_count++, app_id);
3880 rc = _account_query_step(hstmt);
3881 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3883 *account_type_record = create_empty_account_type_instance();
3884 if (*account_type_record == NULL) {
3885 _ERR("Out of Memory");
3886 error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
3890 while (rc == SQLITE_ROW) {
3891 _account_type_convert_column_to_account_type(hstmt, *account_type_record);
3892 rc = _account_query_step(hstmt);
3895 rc = _account_query_finalize(hstmt);
3896 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3897 _account_type_query_label_by_app_id(_account_get_label_text_cb, app_id, (void*)(*account_type_record));
3898 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, app_id, (void*)(*account_type_record));
3901 error_code = _ACCOUNT_ERROR_NONE;
3904 if (hstmt != NULL) {
3905 rc = _account_query_finalize(hstmt);
3906 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3910 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3911 error_code = _account_type_query_by_app_id_from_global_db(app_id, account_type_record);
3913 _INFO("_account_type_query_by_app_id end [%d]", error_code);
3918 int _account_type_query_by_provider_feature_from_global_db(const char* key, GSList **account_type_list_all)
3920 int error_code = _ACCOUNT_ERROR_NONE;
3921 account_stmt hstmt = NULL;
3922 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3924 GSList *account_type_list = NULL;
3926 _INFO("_account_type_query_by_provider_feature_from_global_db start key=%s", key);
3928 ACCOUNT_ERROR("capability_type IS NULL.");
3929 error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
3933 if (g_hAccountGlobalDB == NULL) {
3934 ACCOUNT_ERROR("The database isn't connected.");
3935 error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;
3939 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3941 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
3943 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3945 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3946 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3947 error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3951 int binding_count = 1;
3952 _account_query_bind_text(hstmt, binding_count++, key);
3954 rc = _account_query_step(hstmt);
3956 account_type_s *account_type_record = NULL;
3958 if (rc != SQLITE_ROW) {
3959 ACCOUNT_ERROR("The record isn't found. rc=[%d]", rc);
3960 error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
3964 while (rc == SQLITE_ROW) {
3965 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
3967 if (account_type_record == NULL) {
3968 ACCOUNT_FATAL("malloc Failed");
3972 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
3973 _account_type_convert_column_to_account_type(hstmt, account_type_record);
3974 account_type_list = g_slist_append(account_type_list, account_type_record);
3975 rc = _account_query_step(hstmt);
3978 rc = _account_query_finalize(hstmt);
3979 if (rc != _ACCOUNT_ERROR_NONE) {
3980 _account_type_gslist_account_type_free(account_type_list);
3981 ACCOUNT_ERROR("finalize error(%s)", rc);
3989 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
3990 account_type_s *account_type = NULL;
3991 account_type = (account_type_s*)iter->data;
3992 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb, account_type->app_id, (void*)account_type);
3993 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, account_type->app_id, (void*)account_type);
3994 _INFO("add label & provider_feature");
3997 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
3999 account_type_s *account_type = NULL;
4000 account_type = (account_type_s*)iter->data;
4001 *account_type_list_all = g_slist_append(*account_type_list_all, account_type);
4002 _INFO("add account_type");
4004 g_slist_free(account_type_list);
4006 error_code = _ACCOUNT_ERROR_NONE;
4009 if (hstmt != NULL) {
4010 rc = _account_query_finalize(hstmt);
4011 if (rc != _ACCOUNT_ERROR_NONE) {
4012 ACCOUNT_ERROR("finalize error(%s)", rc);
4018 _INFO("_account_type_query_by_provider_feature_from_global_db end. error_code=[%d]", error_code);
4023 GSList* _account_type_query_by_provider_feature(const char* key, int *error_code)
4025 *error_code = _ACCOUNT_ERROR_NONE;
4026 account_stmt hstmt = NULL;
4027 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4029 GSList *account_type_list = NULL;
4031 _INFO("account_type_query_by_provider_feature start key=%s", key);
4033 ACCOUNT_ERROR("capability_type IS NULL.");
4034 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
4038 if (g_hAccountDB == NULL) {
4039 ACCOUNT_ERROR("The database isn't connected.");
4040 *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;
4044 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4046 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
4048 hstmt = _account_prepare_query(g_hAccountDB, query);
4050 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4051 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4052 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
4056 int binding_count = 1;
4057 _account_query_bind_text(hstmt, binding_count++, key);
4059 rc = _account_query_step(hstmt);
4061 account_type_s *account_type_record = NULL;
4063 if (rc != SQLITE_ROW) {
4064 ACCOUNT_ERROR("The record isn't found. rc=[%d]", rc);
4065 *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4069 while (rc == SQLITE_ROW) {
4070 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4072 if (account_type_record == NULL) {
4073 ACCOUNT_FATAL("malloc Failed");
4077 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4078 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4079 account_type_list = g_slist_append(account_type_list, account_type_record);
4080 rc = _account_query_step(hstmt);
4083 rc = _account_query_finalize(hstmt);
4084 if (rc != _ACCOUNT_ERROR_NONE) {
4085 _account_type_gslist_account_type_free(account_type_list);
4086 ACCOUNT_ERROR("finalize error(%s)", rc);
4094 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4095 account_type_s *account_type = NULL;
4096 account_type = (account_type_s*)iter->data;
4097 _account_type_query_label_by_app_id(_account_get_label_text_cb, account_type->app_id, (void*)account_type);
4098 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, account_type->app_id, (void*)account_type);
4101 *error_code = _ACCOUNT_ERROR_NONE;
4104 if (hstmt != NULL) {
4105 rc = _account_query_finalize(hstmt);
4106 if (rc != _ACCOUNT_ERROR_NONE) {
4113 if (*error_code == _ACCOUNT_ERROR_NONE || *error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4114 rc = _account_type_query_by_provider_feature_from_global_db(key, &account_type_list);
4115 if (rc != _ACCOUNT_ERROR_NONE && rc != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4116 ACCOUNT_ERROR("_account_type_query_by_provider_feature_from_global_db fail=[%d]", rc);
4117 _account_type_gslist_account_type_free(account_type_list);
4120 if (rc == _ACCOUNT_ERROR_NONE)
4124 _INFO("account_type_query_by_provider_feature end");
4126 return account_type_list;
4129 int _account_type_query_all_from_global_db(GSList **account_type_list_all)
4131 account_stmt hstmt = NULL;
4132 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4133 int rc = _ACCOUNT_ERROR_NONE;
4134 int error_code = _ACCOUNT_ERROR_NONE;
4135 GSList *account_type_list = NULL;
4137 _INFO("_account_type_query_all_in_global_db start");
4138 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, NULL, ("The database isn't connected."));
4140 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4142 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
4143 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
4145 rc = _account_query_step(hstmt);
4147 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
4148 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
4149 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4152 account_type_s *account_type_record = NULL;
4154 if (rc != SQLITE_ROW) {
4155 _INFO("[_ACCOUNT_ERROR_RECORD_NOT_FOUND]The record isn't found.");
4156 error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4160 while (rc == SQLITE_ROW) {
4161 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4163 if (account_type_record == NULL) {
4164 ACCOUNT_FATAL("malloc Failed");
4168 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4169 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4170 account_type_list = g_slist_append(account_type_list, account_type_record);
4171 rc = _account_query_step(hstmt);
4174 rc = _account_query_finalize(hstmt);
4175 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list); }, rc, ("finalize error"));
4180 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4181 account_type_s *account_type = NULL;
4182 account_type = (account_type_s*)iter->data;
4183 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb, account_type->app_id, (void*)account_type);
4184 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, account_type->app_id, (void*)account_type);
4187 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4188 account_type_s *account_type = NULL;
4189 account_type = (account_type_s*)iter->data;
4190 *account_type_list_all = g_slist_append(*account_type_list_all, account_type);
4192 g_slist_free(account_type_list);
4194 error_code = _ACCOUNT_ERROR_NONE;
4197 if (hstmt != NULL) {
4198 rc = _account_query_finalize(hstmt);
4199 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list); }, rc, ("finalize error"));
4203 _INFO("_account_type_query_all_in_global_db end");
4208 GSList* _account_type_query_all(void)
4210 account_stmt hstmt = NULL;
4211 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4213 int error_code = _ACCOUNT_ERROR_NONE;
4214 GSList *account_type_list = NULL;
4216 _INFO("_account_type_query_all start");
4217 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected."));
4219 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4221 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
4222 hstmt = _account_prepare_query(g_hAccountDB, query);
4224 rc = _account_query_step(hstmt);
4226 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4227 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4231 account_type_s *account_type_record = NULL;
4233 if (rc != SQLITE_ROW) {
4234 _INFO("[_ACCOUNT_ERROR_RECORD_NOT_FOUND]The record isn't found.");
4235 error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4239 while (rc == SQLITE_ROW) {
4240 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4242 if (account_type_record == NULL) {
4243 ACCOUNT_FATAL("malloc Failed");
4247 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4248 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4249 account_type_list = g_slist_append(account_type_list, account_type_record);
4250 rc = _account_query_step(hstmt);
4253 rc = _account_query_finalize(hstmt);
4254 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list); }, NULL, ("finalize error"));
4259 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4260 account_type_s *account_type = NULL;
4261 account_type = (account_type_s*)iter->data;
4262 _account_type_query_label_by_app_id(_account_get_label_text_cb, account_type->app_id, (void*)account_type);
4263 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, account_type->app_id, (void*)account_type);
4266 error_code = _ACCOUNT_ERROR_NONE;
4269 if (hstmt != NULL) {
4270 rc = _account_query_finalize(hstmt);
4271 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list); }, NULL, ("finalize error"));
4275 if (error_code == _ACCOUNT_ERROR_NONE || error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4276 error_code = _account_type_query_all_from_global_db(&account_type_list);
4277 if (rc != _ACCOUNT_ERROR_NONE && rc != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4278 ACCOUNT_ERROR("_account_type_query_all_from_global_db fail=[%d]", rc);
4279 _account_type_gslist_account_type_free(account_type_list);
4284 _INFO("_account_type_query_all end");
4286 return account_type_list;
4289 // output parameter label must be free
4290 int _account_type_query_label_by_locale_from_global_db(const char* app_id, const char* locale, char **label)
4292 int error_code = _ACCOUNT_ERROR_NONE;
4293 account_stmt hstmt = NULL;
4294 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4295 int rc = 0, binding_count = 1;
4296 char* converted_locale = NULL;
4298 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
4299 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4300 ACCOUNT_RETURN_VAL((label != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
4301 ACCOUNT_RETURN_VAL((locale != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("locale char is null"));
4302 //Making label newly created
4304 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4306 converted_locale = _account_dup_text(locale);
4307 gchar** tokens = g_strsplit(converted_locale, "-", 2);
4309 if (tokens != NULL) {
4310 if ((char*)(tokens[1]) != NULL) {
4311 char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1]));
4312 if (upper_token != NULL) {
4313 _ACCOUNT_FREE(converted_locale);
4314 converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token);
4316 _ACCOUNT_FREE(upper_token);
4321 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, converted_locale);
4322 _ACCOUNT_FREE(converted_locale);
4324 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
4326 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
4327 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
4328 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4331 _account_query_bind_text(hstmt, binding_count++, app_id);
4333 rc = _account_query_step(hstmt);
4334 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4336 label_s* label_record = NULL;
4338 while (rc == SQLITE_ROW) {
4339 label_record = (label_s*) malloc(sizeof(label_s));
4341 if (label_record == NULL) {
4342 ACCOUNT_FATAL("malloc Failed");
4346 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
4348 _account_type_convert_column_to_label(hstmt, label_record);
4350 _ACCOUNT_FREE(*label);
4351 //Making label newly created
4352 *label = _account_dup_text(label_record->label);
4354 _account_type_free_label_with_items(label_record);
4356 rc = _account_query_step(hstmt);
4359 rc = _account_query_finalize(hstmt);
4360 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4363 error_code = _ACCOUNT_ERROR_NONE;
4366 if (hstmt != NULL) {
4367 rc = _account_query_finalize(hstmt);
4368 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4372 _INFO("_account_type_query_label_by_locale_from_global_db() end : error_code = %d", error_code);
4377 // output parameter label must be free
4378 int _account_type_query_label_by_locale(const char* app_id, const char* locale, char **label)
4380 int error_code = _ACCOUNT_ERROR_NONE;
4381 account_stmt hstmt = NULL;
4382 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4383 int rc = 0, binding_count = 1;
4384 char* converted_locale = NULL;
4386 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
4387 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4388 ACCOUNT_RETURN_VAL((label != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
4389 ACCOUNT_RETURN_VAL((locale != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("locale char is null"));
4390 //Making label newly created
4392 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4394 converted_locale = _account_dup_text(locale);
4395 gchar** tokens = g_strsplit(converted_locale, "-", 2);
4397 if (tokens != NULL) {
4398 if ((char*)(tokens[1]) != NULL) {
4399 char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1]));
4400 if (upper_token != NULL) {
4401 _ACCOUNT_FREE(converted_locale);
4402 converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token);
4404 _ACCOUNT_FREE(upper_token);
4409 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, converted_locale);
4410 _ACCOUNT_FREE(converted_locale);
4412 hstmt = _account_prepare_query(g_hAccountDB, query);
4414 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4415 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4416 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4419 _account_query_bind_text(hstmt, binding_count++, app_id);
4421 rc = _account_query_step(hstmt);
4422 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4424 label_s* label_record = NULL;
4426 while (rc == SQLITE_ROW) {
4427 label_record = (label_s*) malloc(sizeof(label_s));
4429 if (label_record == NULL) {
4430 ACCOUNT_FATAL("malloc Failed");
4434 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
4436 _account_type_convert_column_to_label(hstmt, label_record);
4438 _ACCOUNT_FREE(*label);
4439 //Making label newly created
4440 *label = _account_dup_text(label_record->label);
4442 _account_type_free_label_with_items(label_record);
4444 rc = _account_query_step(hstmt);
4447 rc = _account_query_finalize(hstmt);
4448 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4451 error_code = _ACCOUNT_ERROR_NONE;
4454 if (hstmt != NULL) {
4455 rc = _account_query_finalize(hstmt);
4456 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4460 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
4461 error_code = _account_type_query_label_by_locale_from_global_db(app_id, locale, label);
4463 _INFO("_account_type_query_label_by_locale() end : error_code = %d", error_code);
4468 static int _account_insert_custom(account_s *account, int account_id)
4470 _INFO("_account_insert_custom start");
4473 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4474 account_stmt hstmt = NULL;
4476 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4478 if (g_slist_length(account->custom_list) == 0) {
4479 ACCOUNT_DEBUG("_account_insert_custom, no custom data\n");
4480 return _ACCOUNT_ERROR_NONE;
4483 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
4485 rc = _account_get_record_count(g_hAccountDB, query);
4487 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4488 ACCOUNT_ERROR("Access failed(%d, %s)", _account_db_err_msg(g_hAccountDB));
4489 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4493 ACCOUNT_SLOGE("_account_insert_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
4494 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4501 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
4504 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4505 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (AccountId, AppId, Key, Value) VALUES "
4506 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
4508 hstmt = _account_prepare_query(g_hAccountDB, query);
4510 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4511 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4512 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4515 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
4517 account_custom_s* custom_data = NULL;
4518 custom_data = (account_custom_s*)iter->data;
4520 ret = _account_query_bind_int(hstmt, count++, account_id);
4521 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
4522 ret = _account_query_bind_text(hstmt, count++, account->package_name);
4523 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4524 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
4525 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4526 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
4527 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4529 rc = _account_query_step(hstmt);
4531 if (rc != SQLITE_DONE) {
4532 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4536 rc = _account_query_finalize(hstmt);
4537 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4542 _INFO("_account_insert_custom end");
4544 return _ACCOUNT_ERROR_NONE;
4547 static int _account_update_custom(account_s *account, int account_id)
4550 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4551 account_stmt hstmt = NULL;
4553 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4555 if (g_slist_length(account->custom_list) == 0) {
4556 ACCOUNT_DEBUG("_account_update_custom, no custom data\n");
4557 return _ACCOUNT_ERROR_NONE;
4560 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
4562 rc = _account_get_record_count(g_hAccountDB, query);
4564 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4565 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4566 pthread_mutex_unlock(&account_mutex);
4567 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4568 } else if (_account_db_err_code(g_hAccountDB) == SQLITE_BUSY) {
4569 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
4570 pthread_mutex_unlock(&account_mutex);
4571 return _ACCOUNT_ERROR_DATABASE_BUSY;
4575 ACCOUNT_SLOGE("_account_update_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
4576 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4579 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4581 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId=? ", ACCOUNT_CUSTOM_TABLE);
4582 hstmt = _account_prepare_query(g_hAccountDB, query);
4584 _account_query_bind_int(hstmt, count++, (int)account_id);
4585 rc = _account_query_step(hstmt);
4587 if (rc == SQLITE_BUSY) {
4588 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4589 return _ACCOUNT_ERROR_DATABASE_BUSY;
4590 } else if (rc != SQLITE_DONE) {
4591 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4592 return _ACCOUNT_ERROR_DB_FAILED;
4595 rc = _account_query_finalize(hstmt);
4596 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4601 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
4604 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4605 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AccountId, AppId, Key, Value) VALUES "
4606 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
4608 hstmt = _account_prepare_query(g_hAccountDB, query);
4610 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4611 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4612 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4615 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
4617 account_custom_s* custom_data = NULL;
4618 custom_data = (account_custom_s*)iter->data;
4620 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
4621 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
4622 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
4623 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4624 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
4625 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4626 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
4627 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4629 rc = _account_query_step(hstmt);
4631 if (rc != SQLITE_DONE) {
4632 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4636 rc = _account_query_finalize(hstmt);
4637 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4642 return _ACCOUNT_ERROR_NONE;