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; _ERR("The record isn't found from user db. rc=[%d]", rc); }, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2971 provider_feature_s* feature_record = NULL;
2973 while (rc == SQLITE_ROW) {
2975 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
2977 if (feature_record == NULL) {
2978 ACCOUNT_FATAL("malloc Failed");
2982 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
2984 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
2986 _INFO("Adding account feature_list");
2987 feature_list = g_slist_append(feature_list, feature_record);
2989 rc = _account_query_step(hstmt);
2992 *error_code = _ACCOUNT_ERROR_NONE;
2994 rc = _account_query_finalize(hstmt);
2995 ACCOUNT_CATCH_ERROR_P((rc == _ACCOUNT_ERROR_NONE), { *error_code = rc; }, rc, ("account finalize error"));
2999 if (hstmt != NULL) {
3000 rc = _account_query_finalize(hstmt);
3001 if (rc != _ACCOUNT_ERROR_NONE) {
3003 _ERR("account fianlize error");
3007 _INFO("*error_code=[%d]", *error_code);
3009 if (*error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3010 feature_list = _account_type_query_provider_feature_by_app_id_from_global_db(app_id, error_code);
3012 if (*error_code != _ACCOUNT_ERROR_NONE)
3013 _account_type_gslist_feature_free(feature_list);
3015 _INFO("Returning account feature_list");
3017 return feature_list;
3020 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)
3022 int error_code = _ACCOUNT_ERROR_NONE;
3023 account_stmt hstmt = NULL;
3024 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3025 int rc = 0, binding_count = 1;
3027 _INFO("_account_type_query_provider_feature_cb_by_app_id_in_global_db start app_id=%s", app_id);
3028 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3029 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3030 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3032 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3034 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
3035 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3037 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3038 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3039 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_PERMISSION_DENIED, ("global db permission denied.\n"));
3042 _account_query_bind_text(hstmt, binding_count++, app_id);
3044 rc = _account_query_step(hstmt);
3045 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"));
3047 provider_feature_s* feature_record = NULL;
3049 while (rc == SQLITE_ROW) {
3050 bool cb_ret = FALSE;
3051 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
3053 if (feature_record == NULL) {
3054 ACCOUNT_FATAL("malloc Failed");
3058 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
3060 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
3062 cb_ret = callback(feature_record->app_id, feature_record->key, user_data);
3064 _account_type_free_feature_with_items(feature_record);
3066 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returns FALSE, its iteration is stopped!!!!\n"));
3068 rc = _account_query_step(hstmt);
3071 error_code = _ACCOUNT_ERROR_NONE;
3074 if (hstmt != NULL) {
3075 rc = _account_query_finalize(hstmt);
3076 if (rc != _ACCOUNT_ERROR_NONE) {
3078 _ERR("global db finalize error[%d]", rc);
3083 _INFO("_account_type_query_provider_feature_cb_by_app_id_in_global_db end. error_code=[%d]", error_code);
3088 int _account_type_query_provider_feature_cb_by_app_id(account_type_provider_feature_cb callback, const char* app_id, void *user_data)
3090 int error_code = _ACCOUNT_ERROR_NONE;
3091 account_stmt hstmt = NULL;
3092 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3093 int rc = 0, binding_count = 1;
3095 _INFO("_account_type_query_provider_feature_cb_by_app_id start app_id=%s", app_id);
3096 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3097 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3098 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3100 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3102 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
3103 hstmt = _account_prepare_query(g_hAccountDB, query);
3105 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
3106 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3107 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3110 _account_query_bind_text(hstmt, binding_count++, app_id);
3112 rc = _account_query_step(hstmt);
3113 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found in user db.\n"));
3115 provider_feature_s* feature_record = NULL;
3117 while (rc == SQLITE_ROW) {
3118 bool cb_ret = FALSE;
3119 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
3121 if (feature_record == NULL) {
3122 ACCOUNT_FATAL("malloc Failed");
3126 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
3128 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
3130 cb_ret = callback(feature_record->app_id, feature_record->key, user_data);
3132 _account_type_free_feature_with_items(feature_record);
3134 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
3136 rc = _account_query_step(hstmt);
3139 rc = _account_query_finalize(hstmt);
3140 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3143 error_code = _ACCOUNT_ERROR_NONE;
3146 if (hstmt != NULL) {
3147 rc = _account_query_finalize(hstmt);
3148 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3152 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3153 error_code = _account_type_query_provider_feature_cb_by_app_id_from_global_db(callback, app_id, user_data);
3155 _INFO("_account_type_query_provider_feature_cb_by_app_id end");
3160 int account_type_query_provider_feature_cb_by_app_id(account_type_provider_feature_cb callback, const char* app_id, void *user_data)
3162 int error_code = _ACCOUNT_ERROR_NONE;
3164 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3165 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3166 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3168 error_code = _account_type_query_provider_feature_cb_by_app_id(callback, app_id, user_data);
3170 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3171 error_code = _account_type_query_provider_feature_cb_by_app_id_from_global_db(callback, app_id, user_data);
3176 bool _account_type_query_supported_feature_from_global_db(const char* app_id, const char* capability, int *error_code)
3178 _INFO("_account_type_query_supported_feature_in_global_db start");
3179 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3181 *error_code = _ACCOUNT_ERROR_NONE;
3183 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3184 int record_count = 0;
3186 if (app_id == NULL || capability == NULL) {
3187 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
3191 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
3193 record_count = _account_get_record_count(g_hAccountGlobalDB, query);
3195 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3196 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3197 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3200 if (record_count <= 0) {
3201 *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
3205 _INFO("_account_type_query_supported_feature_in_global_db end");
3210 bool _account_type_query_supported_feature(const char* app_id, const char* capability, int *error_code)
3212 _INFO("_account_type_query_supported_feature start");
3214 *error_code = _ACCOUNT_ERROR_NONE;
3216 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3217 int record_count = 0;
3219 if (app_id == NULL || capability == NULL) {
3220 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
3224 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
3226 record_count = _account_get_record_count(g_hAccountDB, query);
3228 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
3229 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3230 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3234 if (record_count <= 0) {
3235 bool is_exist = false;
3236 is_exist = _account_type_query_supported_feature_from_global_db(app_id, capability, error_code);
3241 _INFO("_account_type_query_supported_feature end");
3247 static int _account_type_update_provider_feature(sqlite3 *account_db_handle, account_type_s *account_type, const char* app_id)
3250 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3251 account_stmt hstmt = NULL;
3253 ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
3255 if (g_slist_length(account_type->provider_feature_list) == 0) {
3256 ACCOUNT_ERROR("no feature\n");
3257 return _ACCOUNT_ERROR_NONE;
3260 ACCOUNT_DEBUG("app id", app_id);
3262 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3264 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id=? ", PROVIDER_FEATURE_TABLE);
3265 hstmt = _account_prepare_query(account_db_handle, query);
3267 if (_account_db_err_code(account_db_handle) == SQLITE_PERM) {
3268 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(account_db_handle));
3269 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3273 _account_query_bind_text(hstmt, count++, app_id);
3274 rc = _account_query_step(hstmt);
3276 if (rc != SQLITE_DONE) {
3277 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3278 return _ACCOUNT_ERROR_DB_FAILED;
3280 rc = _account_query_finalize(hstmt);
3281 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3286 for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
3289 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3290 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
3291 "(?, ?) ", PROVIDER_FEATURE_TABLE);
3293 hstmt = _account_prepare_query(account_db_handle, query);
3295 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg(account_db_handle)));
3297 provider_feature_s* feature_data = NULL;
3298 feature_data = (provider_feature_s*)iter->data;
3300 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
3301 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3302 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
3303 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3305 rc = _account_query_step(hstmt);
3307 if (rc != SQLITE_DONE) {
3308 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3311 rc = _account_query_finalize(hstmt);
3312 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3316 return _ACCOUNT_ERROR_NONE;
3319 static int _account_type_update_label(sqlite3 *account_db_handle, account_type_s *account_type, const char* app_id)
3322 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3323 account_stmt hstmt = NULL;
3325 ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
3327 if (g_slist_length(account_type->label_list) == 0)
3328 return _ACCOUNT_ERROR_NONE;
3330 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3332 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId=? ", LABEL_TABLE);
3333 hstmt = _account_prepare_query(account_db_handle, query);
3335 if (_account_db_err_code(account_db_handle) == SQLITE_PERM) {
3336 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(account_db_handle));
3337 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3341 _account_query_bind_text(hstmt, count++, app_id);
3342 rc = _account_query_step(hstmt);
3344 if (rc != SQLITE_DONE) {
3345 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3346 return _ACCOUNT_ERROR_DB_FAILED;
3348 rc = _account_query_finalize(hstmt);
3349 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3354 for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
3357 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3358 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
3359 "(?, ?, ?) ", LABEL_TABLE);
3361 hstmt = _account_prepare_query(account_db_handle, query);
3363 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg(account_db_handle)));
3365 label_s* label_data = NULL;
3366 label_data = (label_s*)iter->data;
3368 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
3369 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3370 ret = _account_query_bind_text(hstmt, count++, label_data->label);
3371 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3372 ret = _account_query_bind_text(hstmt, count++, label_data->locale);
3373 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3375 rc = _account_query_step(hstmt);
3377 if (rc != SQLITE_DONE) {
3378 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3381 rc = _account_query_finalize(hstmt);
3382 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3386 return _ACCOUNT_ERROR_NONE;
3390 static int _account_type_update_account(sqlite3 *account_db_handle, account_type_s *account_type, const char* app_id)
3392 int rc = 0, binding_count = 1;
3393 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3394 int error_code = _ACCOUNT_ERROR_NONE;
3395 account_stmt hstmt = NULL;
3397 if (!account_type->app_id) {
3398 ACCOUNT_ERROR("app id is mandetory field, it can not be NULL!!!!\n");
3399 return _ACCOUNT_ERROR_INVALID_PARAMETER;
3402 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3403 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET AppId=?, ServiceProviderId=?, IconPath=?, "
3404 "SmallIconPath=?, MultipleAccountSupport=? WHERE AppId=? ", ACCOUNT_TYPE_TABLE);
3406 hstmt = _account_prepare_query(account_db_handle, query);
3408 if (_account_db_err_code(account_db_handle) == SQLITE_PERM) {
3409 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(account_db_handle));
3410 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3411 } else if (_account_db_err_code(account_db_handle) == SQLITE_BUSY) {
3412 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(account_db_handle));
3413 return _ACCOUNT_ERROR_DATABASE_BUSY;
3416 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg(account_db_handle)));
3418 binding_count = _account_type_convert_account_to_sql(account_type, hstmt, query);
3419 _account_query_bind_text(hstmt, binding_count++, app_id);
3421 rc = _account_query_step(hstmt);
3422 if (rc != SQLITE_DONE)
3423 ACCOUNT_ERROR("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3425 rc = _account_query_finalize(hstmt);
3426 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3430 error_code = _account_type_update_label(account_db_handle, account_type, app_id);
3431 /* update provider feature */
3432 error_code = _account_type_update_provider_feature(account_db_handle, account_type, app_id);
3437 int _account_type_update_to_db_by_app_id(account_type_s* account_type, const char* app_id)
3439 int error_code = _ACCOUNT_ERROR_NONE;
3441 ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
3442 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3443 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3445 account_type_s* data = account_type;
3447 pthread_mutex_lock(&account_mutex);
3449 error_code = _account_type_update_account(g_hAccountDB, data, app_id);
3451 pthread_mutex_unlock(&account_mutex);
3456 GSList* _account_type_get_label_list_by_app_id_from_global_db(const char* app_id, int *error_code)
3458 *error_code = _ACCOUNT_ERROR_NONE;
3459 account_stmt hstmt = NULL;
3460 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3461 int rc = 0, binding_count = 1;
3462 GSList* label_list = NULL;
3464 ACCOUNT_RETURN_VAL((app_id != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("APP ID IS NULL"));
3465 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3467 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3469 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3470 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3472 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3473 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3474 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3479 _account_query_bind_text(hstmt, binding_count++, app_id);
3481 rc = _account_query_step(hstmt);
3482 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"));
3484 label_s* label_record = NULL;
3486 while (rc == SQLITE_ROW) {
3487 label_record = (label_s*) malloc(sizeof(label_s));
3489 if (label_record == NULL) {
3490 ACCOUNT_FATAL("malloc Failed");
3494 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3496 _account_type_convert_column_to_label(hstmt, label_record);
3498 _INFO("Adding account label_list");
3499 label_list = g_slist_append(label_list, label_record);
3501 rc = _account_query_step(hstmt);
3504 *error_code = _ACCOUNT_ERROR_NONE;
3507 if (hstmt != NULL) {
3508 rc = _account_query_finalize(hstmt);
3509 if (rc != _ACCOUNT_ERROR_NONE)
3510 _ERR("global db finalize error[%d]", rc);
3514 _INFO("Returning account global label_list");
3519 GSList* _account_type_get_label_list_by_app_id(const char* app_id, int *error_code)
3521 *error_code = _ACCOUNT_ERROR_NONE;
3522 account_stmt hstmt = NULL;
3523 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3524 int rc = 0, binding_count = 1;
3525 GSList* label_list = NULL;
3527 ACCOUNT_RETURN_VAL((app_id != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("APP ID IS NULL"));
3528 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), { *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED; }, NULL, ("The database isn't connected."));
3530 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3532 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3533 hstmt = _account_prepare_query(g_hAccountDB, query);
3535 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
3536 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3537 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3541 _account_query_bind_text(hstmt, binding_count++, app_id);
3543 rc = _account_query_step(hstmt);
3544 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3546 label_s* label_record = NULL;
3548 while (rc == SQLITE_ROW) {
3549 label_record = (label_s*) malloc(sizeof(label_s));
3551 if (label_record == NULL) {
3552 ACCOUNT_FATAL("malloc Failed");
3556 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3558 _account_type_convert_column_to_label(hstmt, label_record);
3560 _INFO("Adding account label_list");
3561 label_list = g_slist_append(label_list, label_record);
3563 rc = _account_query_step(hstmt);
3566 rc = _account_query_finalize(hstmt);
3567 if (rc != _ACCOUNT_ERROR_NONE) {
3568 _account_type_gslist_label_free(label_list);
3570 _ERR("finalize error");
3575 *error_code = _ACCOUNT_ERROR_NONE;
3578 if (hstmt != NULL) {
3579 rc = _account_query_finalize(hstmt);
3580 if (rc != _ACCOUNT_ERROR_NONE) {
3581 _account_type_gslist_label_free(label_list);
3583 _ERR("finalize error");
3589 if (*error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3590 label_list = _account_type_get_label_list_by_app_id_from_global_db(app_id, error_code);
3592 _INFO("Returning account label_list");
3597 int _account_type_query_label_by_app_id_from_global_db(account_type_label_cb callback, const char* app_id, void *user_data)
3599 int error_code = _ACCOUNT_ERROR_NONE;
3600 account_stmt hstmt = NULL;
3601 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3602 int rc = 0, binding_count = 1;
3604 _INFO("account_type_query_label_by_app_id_from_global_db start");
3606 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3607 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3608 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3610 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3612 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3613 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3615 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3616 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3617 error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3621 _account_query_bind_text(hstmt, binding_count++, app_id);
3623 rc = _account_query_step(hstmt);
3624 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3626 label_s* label_record = NULL;
3628 while (rc == SQLITE_ROW) {
3629 bool cb_ret = FALSE;
3630 label_record = (label_s*) malloc(sizeof(label_s));
3632 if (label_record == NULL) {
3633 ACCOUNT_FATAL("malloc Failed");
3637 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3639 _account_type_convert_column_to_label(hstmt, label_record);
3641 cb_ret = callback(label_record->app_id, label_record->label , label_record->locale, user_data);
3643 _account_type_free_label_with_items(label_record);
3645 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
3647 rc = _account_query_step(hstmt);
3650 error_code = _ACCOUNT_ERROR_NONE;
3653 if (hstmt != NULL) {
3654 rc = _account_query_finalize(hstmt);
3655 if (rc != _ACCOUNT_ERROR_NONE)
3656 _ERR("global db finalize error[%d]", rc);
3660 _INFO("account_type_query_label_by_app_id_from_global_db end [%d]", error_code);
3665 int _account_type_query_label_by_app_id(account_type_label_cb callback, const char* app_id, void *user_data)
3667 int error_code = _ACCOUNT_ERROR_NONE;
3668 account_stmt hstmt = NULL;
3669 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3670 int rc = 0, binding_count = 1;
3672 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3673 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3674 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3676 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3678 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3679 hstmt = _account_prepare_query(g_hAccountDB, query);
3681 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
3682 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3683 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3686 _account_query_bind_text(hstmt, binding_count++, app_id);
3688 rc = _account_query_step(hstmt);
3689 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3691 label_s* label_record = NULL;
3693 while (rc == SQLITE_ROW) {
3694 bool cb_ret = FALSE;
3695 label_record = (label_s*) malloc(sizeof(label_s));
3697 if (label_record == NULL) {
3698 ACCOUNT_FATAL("malloc Failed");
3702 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3704 _account_type_convert_column_to_label(hstmt, label_record);
3706 cb_ret = callback(label_record->app_id, label_record->label , label_record->locale, user_data);
3708 _account_type_free_label_with_items(label_record);
3710 //ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
3711 if (cb_ret != TRUE) {
3712 _INFO("Callback func returs FALSE, its iteration is stopped!!!!\n");
3716 rc = _account_query_step(hstmt);
3719 rc = _account_query_finalize(hstmt);
3720 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3723 error_code = _ACCOUNT_ERROR_NONE;
3726 if (hstmt != NULL) {
3727 rc = _account_query_finalize(hstmt);
3728 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3732 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
3733 error_code = account_type_query_label_by_app_id_from_global_db(callback, app_id, user_data);
3739 int account_type_query_label_by_app_id(account_type_label_cb callback, const char* app_id, void *user_data)
3741 int error_code = _ACCOUNT_ERROR_NONE;
3743 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3744 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3745 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3747 error_code = _account_type_query_label_by_app_id(callback, app_id, user_data);
3749 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3750 error_code = _account_type_query_label_by_app_id_from_global_db(callback, app_id, user_data);
3755 bool _account_get_label_text_cb(char* app_id, char* label, char* locale, void *user_data)
3757 account_type_s *data = (account_type_s*)user_data;
3759 label_s *label_data = (label_s*)malloc(sizeof(label_s));
3761 if (label_data == NULL) {
3762 ACCOUNT_FATAL("_account_get_label_text_cb : MALLOC FAIL\n");
3765 ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
3767 label_data->app_id = _account_dup_text(app_id);
3768 label_data->label = _account_dup_text(label);
3769 label_data->locale = _account_dup_text(locale);
3771 data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
3776 bool _account_get_provider_feature_cb(char* app_id, char* key, void* user_data)
3778 account_type_s *data = (account_type_s*)user_data;
3780 provider_feature_s *feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
3782 if (feature_data == NULL) {
3783 ACCOUNT_FATAL("_account_get_provider_feature_cb : MALLOC FAIL\n");
3786 ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
3788 feature_data->app_id = _account_dup_text(app_id);
3789 feature_data->key = _account_dup_text(key);
3791 data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
3796 int _account_type_query_by_app_id_from_global_db(const char* app_id, account_type_s** account_type_record)
3798 _INFO("_account_type_query_by_app_id_from_global_db start");
3800 int error_code = _ACCOUNT_ERROR_NONE;
3801 account_stmt hstmt = NULL;
3802 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3803 int rc = 0, binding_count = 1;
3805 ACCOUNT_RETURN_VAL((app_id != 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3806 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3807 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3809 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3811 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
3812 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3814 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3815 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3816 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3819 _account_query_bind_text(hstmt, binding_count++, app_id);
3821 rc = _account_query_step(hstmt);
3822 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3824 *account_type_record = create_empty_account_type_instance();
3826 while (rc == SQLITE_ROW) {
3827 _account_type_convert_column_to_account_type(hstmt, *account_type_record);
3828 rc = _account_query_step(hstmt);
3831 rc = _account_query_finalize(hstmt);
3832 ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {_ERR("global db finalize error rc=[%d]", rc); }, rc, ("finalize error"));
3833 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb, app_id, (void*)(*account_type_record));
3834 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, app_id, (void*)(*account_type_record));
3837 error_code = _ACCOUNT_ERROR_NONE;
3840 if (hstmt != NULL) {
3841 rc = _account_query_finalize(hstmt);
3842 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3846 _INFO("_account_type_query_by_app_id_from_global_db end [%d]", error_code);
3851 int _account_type_query_by_app_id(const char* app_id, account_type_s** account_type_record)
3853 _INFO("_account_type_query_by_app_id start");
3855 int error_code = _ACCOUNT_ERROR_NONE;
3856 account_stmt hstmt = NULL;
3857 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3858 int rc = 0, binding_count = 1;
3860 ACCOUNT_RETURN_VAL((app_id != 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3861 ACCOUNT_RETURN_VAL((account_type_record != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("account type record(account_type_s**) is NULL"));
3862 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3864 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3866 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
3867 hstmt = _account_prepare_query(g_hAccountDB, query);
3869 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
3870 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3871 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3874 _account_query_bind_text(hstmt, binding_count++, app_id);
3876 rc = _account_query_step(hstmt);
3877 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3879 *account_type_record = create_empty_account_type_instance();
3880 if (*account_type_record == NULL) {
3881 _ERR("Out of Memory");
3882 error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
3886 while (rc == SQLITE_ROW) {
3887 _account_type_convert_column_to_account_type(hstmt, *account_type_record);
3888 rc = _account_query_step(hstmt);
3891 rc = _account_query_finalize(hstmt);
3892 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3893 _account_type_query_label_by_app_id(_account_get_label_text_cb, app_id, (void*)(*account_type_record));
3894 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, app_id, (void*)(*account_type_record));
3897 error_code = _ACCOUNT_ERROR_NONE;
3900 if (hstmt != NULL) {
3901 rc = _account_query_finalize(hstmt);
3902 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3906 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3907 error_code = _account_type_query_by_app_id_from_global_db(app_id, account_type_record);
3909 _INFO("_account_type_query_by_app_id end [%d]", error_code);
3914 int _account_type_query_by_provider_feature_from_global_db(const char* key, GSList **account_type_list_all)
3916 int error_code = _ACCOUNT_ERROR_NONE;
3917 account_stmt hstmt = NULL;
3918 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3920 GSList *account_type_list = NULL;
3922 _INFO("_account_type_query_by_provider_feature_from_global_db start key=%s", key);
3924 ACCOUNT_ERROR("capability_type IS NULL.");
3925 error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
3929 if (g_hAccountGlobalDB == NULL) {
3930 ACCOUNT_ERROR("The database isn't connected.");
3931 error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;
3935 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3937 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
3939 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3941 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3942 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3943 error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3947 int binding_count = 1;
3948 _account_query_bind_text(hstmt, binding_count++, key);
3950 rc = _account_query_step(hstmt);
3952 account_type_s *account_type_record = NULL;
3954 if (rc != SQLITE_ROW) {
3955 ACCOUNT_ERROR("The record isn't found. rc=[%d]", rc);
3956 error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
3960 while (rc == SQLITE_ROW) {
3961 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
3963 if (account_type_record == NULL) {
3964 ACCOUNT_FATAL("malloc Failed");
3968 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
3969 _account_type_convert_column_to_account_type(hstmt, account_type_record);
3970 account_type_list = g_slist_append(account_type_list, account_type_record);
3971 rc = _account_query_step(hstmt);
3974 rc = _account_query_finalize(hstmt);
3975 if (rc != _ACCOUNT_ERROR_NONE) {
3976 _account_type_gslist_account_type_free(account_type_list);
3977 ACCOUNT_ERROR("finalize error(%s)", rc);
3985 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
3986 account_type_s *account_type = NULL;
3987 account_type = (account_type_s*)iter->data;
3988 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb, account_type->app_id, (void*)account_type);
3989 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, account_type->app_id, (void*)account_type);
3990 _INFO("add label & provider_feature");
3993 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
3995 account_type_s *account_type = NULL;
3996 account_type = (account_type_s*)iter->data;
3997 *account_type_list_all = g_slist_append(*account_type_list_all, account_type);
3998 _INFO("add account_type");
4000 g_slist_free(account_type_list);
4002 error_code = _ACCOUNT_ERROR_NONE;
4005 if (hstmt != NULL) {
4006 rc = _account_query_finalize(hstmt);
4007 if (rc != _ACCOUNT_ERROR_NONE) {
4008 ACCOUNT_ERROR("finalize error(%s)", rc);
4014 _INFO("_account_type_query_by_provider_feature_from_global_db end. error_code=[%d]", error_code);
4019 GSList* _account_type_query_by_provider_feature(const char* key, int *error_code)
4021 *error_code = _ACCOUNT_ERROR_NONE;
4022 account_stmt hstmt = NULL;
4023 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4025 GSList *account_type_list = NULL;
4027 _INFO("account_type_query_by_provider_feature start key=%s", key);
4029 ACCOUNT_ERROR("capability_type IS NULL.");
4030 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
4034 if (g_hAccountDB == NULL) {
4035 ACCOUNT_ERROR("The database isn't connected.");
4036 *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;
4040 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4042 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
4044 hstmt = _account_prepare_query(g_hAccountDB, query);
4046 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4047 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4048 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
4052 int binding_count = 1;
4053 _account_query_bind_text(hstmt, binding_count++, key);
4055 rc = _account_query_step(hstmt);
4057 account_type_s *account_type_record = NULL;
4059 if (rc != SQLITE_ROW) {
4060 ACCOUNT_ERROR("The record isn't found. rc=[%d]", rc);
4061 *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4065 while (rc == SQLITE_ROW) {
4066 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4068 if (account_type_record == NULL) {
4069 ACCOUNT_FATAL("malloc Failed");
4073 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4074 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4075 account_type_list = g_slist_append(account_type_list, account_type_record);
4076 rc = _account_query_step(hstmt);
4079 rc = _account_query_finalize(hstmt);
4080 if (rc != _ACCOUNT_ERROR_NONE) {
4081 _account_type_gslist_account_type_free(account_type_list);
4082 ACCOUNT_ERROR("finalize error(%s)", rc);
4090 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4091 account_type_s *account_type = NULL;
4092 account_type = (account_type_s*)iter->data;
4093 _account_type_query_label_by_app_id(_account_get_label_text_cb, account_type->app_id, (void*)account_type);
4094 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, account_type->app_id, (void*)account_type);
4097 *error_code = _ACCOUNT_ERROR_NONE;
4100 if (hstmt != NULL) {
4101 rc = _account_query_finalize(hstmt);
4102 if (rc != _ACCOUNT_ERROR_NONE) {
4109 if (*error_code == _ACCOUNT_ERROR_NONE || *error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4110 rc = _account_type_query_by_provider_feature_from_global_db(key, &account_type_list);
4111 if (rc != _ACCOUNT_ERROR_NONE && rc != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4112 ACCOUNT_ERROR("_account_type_query_by_provider_feature_from_global_db fail=[%d]", rc);
4113 _account_type_gslist_account_type_free(account_type_list);
4116 if (rc == _ACCOUNT_ERROR_NONE)
4120 _INFO("account_type_query_by_provider_feature end");
4122 return account_type_list;
4125 int _account_type_query_all_from_global_db(GSList **account_type_list_all)
4127 account_stmt hstmt = NULL;
4128 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4129 int rc = _ACCOUNT_ERROR_NONE;
4130 int error_code = _ACCOUNT_ERROR_NONE;
4131 GSList *account_type_list = NULL;
4133 _INFO("_account_type_query_all_in_global_db start");
4134 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, NULL, ("The database isn't connected."));
4136 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4138 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
4139 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
4141 rc = _account_query_step(hstmt);
4143 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
4144 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
4145 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4148 account_type_s *account_type_record = NULL;
4150 if (rc != SQLITE_ROW) {
4151 _INFO("[_ACCOUNT_ERROR_RECORD_NOT_FOUND]The record isn't found.");
4152 error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4156 while (rc == SQLITE_ROW) {
4157 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4159 if (account_type_record == NULL) {
4160 ACCOUNT_FATAL("malloc Failed");
4164 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4165 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4166 account_type_list = g_slist_append(account_type_list, account_type_record);
4167 rc = _account_query_step(hstmt);
4170 rc = _account_query_finalize(hstmt);
4171 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list); }, rc, ("finalize error"));
4176 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4177 account_type_s *account_type = NULL;
4178 account_type = (account_type_s*)iter->data;
4179 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb, account_type->app_id, (void*)account_type);
4180 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, account_type->app_id, (void*)account_type);
4183 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4184 account_type_s *account_type = NULL;
4185 account_type = (account_type_s*)iter->data;
4186 *account_type_list_all = g_slist_append(*account_type_list_all, account_type);
4188 g_slist_free(account_type_list);
4190 error_code = _ACCOUNT_ERROR_NONE;
4193 if (hstmt != NULL) {
4194 rc = _account_query_finalize(hstmt);
4195 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list); }, rc, ("finalize error"));
4199 _INFO("_account_type_query_all_in_global_db end");
4204 GSList* _account_type_query_all(void)
4206 account_stmt hstmt = NULL;
4207 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4209 int error_code = _ACCOUNT_ERROR_NONE;
4210 GSList *account_type_list = NULL;
4212 _INFO("_account_type_query_all start");
4213 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected."));
4215 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4217 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
4218 hstmt = _account_prepare_query(g_hAccountDB, query);
4220 rc = _account_query_step(hstmt);
4222 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4223 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4227 account_type_s *account_type_record = NULL;
4229 if (rc != SQLITE_ROW) {
4230 _INFO("[_ACCOUNT_ERROR_RECORD_NOT_FOUND]The record isn't found.");
4231 error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4235 while (rc == SQLITE_ROW) {
4236 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4238 if (account_type_record == NULL) {
4239 ACCOUNT_FATAL("malloc Failed");
4243 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4244 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4245 account_type_list = g_slist_append(account_type_list, account_type_record);
4246 rc = _account_query_step(hstmt);
4249 rc = _account_query_finalize(hstmt);
4250 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list); }, NULL, ("finalize error"));
4255 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4256 account_type_s *account_type = NULL;
4257 account_type = (account_type_s*)iter->data;
4258 _account_type_query_label_by_app_id(_account_get_label_text_cb, account_type->app_id, (void*)account_type);
4259 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, account_type->app_id, (void*)account_type);
4262 error_code = _ACCOUNT_ERROR_NONE;
4265 if (hstmt != NULL) {
4266 rc = _account_query_finalize(hstmt);
4267 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list); }, NULL, ("finalize error"));
4271 if (error_code == _ACCOUNT_ERROR_NONE || error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4272 error_code = _account_type_query_all_from_global_db(&account_type_list);
4273 if (rc != _ACCOUNT_ERROR_NONE && rc != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4274 ACCOUNT_ERROR("_account_type_query_all_from_global_db fail=[%d]", rc);
4275 _account_type_gslist_account_type_free(account_type_list);
4280 _INFO("_account_type_query_all end");
4282 return account_type_list;
4285 // output parameter label must be free
4286 int _account_type_query_label_by_locale_from_global_db(const char* app_id, const char* locale, char **label)
4288 int error_code = _ACCOUNT_ERROR_NONE;
4289 account_stmt hstmt = NULL;
4290 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4291 int rc = 0, binding_count = 1;
4292 char* converted_locale = NULL;
4294 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
4295 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4296 ACCOUNT_RETURN_VAL((label != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
4297 ACCOUNT_RETURN_VAL((locale != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("locale char is null"));
4298 //Making label newly created
4300 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4302 converted_locale = _account_dup_text(locale);
4303 gchar** tokens = g_strsplit(converted_locale, "-", 2);
4305 if (tokens != NULL) {
4306 if ((char*)(tokens[1]) != NULL) {
4307 char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1]));
4308 if (upper_token != NULL) {
4309 _ACCOUNT_FREE(converted_locale);
4310 converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token);
4312 _ACCOUNT_FREE(upper_token);
4317 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, converted_locale);
4318 _ACCOUNT_FREE(converted_locale);
4320 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
4322 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
4323 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
4324 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4327 _account_query_bind_text(hstmt, binding_count++, app_id);
4329 rc = _account_query_step(hstmt);
4330 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4332 label_s* label_record = NULL;
4334 while (rc == SQLITE_ROW) {
4335 label_record = (label_s*) malloc(sizeof(label_s));
4337 if (label_record == NULL) {
4338 ACCOUNT_FATAL("malloc Failed");
4342 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
4344 _account_type_convert_column_to_label(hstmt, label_record);
4346 _ACCOUNT_FREE(*label);
4347 //Making label newly created
4348 *label = _account_dup_text(label_record->label);
4350 _account_type_free_label_with_items(label_record);
4352 rc = _account_query_step(hstmt);
4355 rc = _account_query_finalize(hstmt);
4356 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4359 error_code = _ACCOUNT_ERROR_NONE;
4362 if (hstmt != NULL) {
4363 rc = _account_query_finalize(hstmt);
4364 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4368 _INFO("_account_type_query_label_by_locale_from_global_db() end : error_code = %d", error_code);
4373 // output parameter label must be free
4374 int _account_type_query_label_by_locale(const char* app_id, const char* locale, char **label)
4376 int error_code = _ACCOUNT_ERROR_NONE;
4377 account_stmt hstmt = NULL;
4378 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4379 int rc = 0, binding_count = 1;
4380 char* converted_locale = NULL;
4382 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
4383 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4384 ACCOUNT_RETURN_VAL((label != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
4385 ACCOUNT_RETURN_VAL((locale != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("locale char is null"));
4386 //Making label newly created
4388 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4390 converted_locale = _account_dup_text(locale);
4391 gchar** tokens = g_strsplit(converted_locale, "-", 2);
4393 if (tokens != NULL) {
4394 if ((char*)(tokens[1]) != NULL) {
4395 char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1]));
4396 if (upper_token != NULL) {
4397 _ACCOUNT_FREE(converted_locale);
4398 converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token);
4400 _ACCOUNT_FREE(upper_token);
4405 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, converted_locale);
4406 _ACCOUNT_FREE(converted_locale);
4408 hstmt = _account_prepare_query(g_hAccountDB, query);
4410 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4411 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4412 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4415 _account_query_bind_text(hstmt, binding_count++, app_id);
4417 rc = _account_query_step(hstmt);
4418 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4420 label_s* label_record = NULL;
4422 while (rc == SQLITE_ROW) {
4423 label_record = (label_s*) malloc(sizeof(label_s));
4425 if (label_record == NULL) {
4426 ACCOUNT_FATAL("malloc Failed");
4430 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
4432 _account_type_convert_column_to_label(hstmt, label_record);
4434 _ACCOUNT_FREE(*label);
4435 //Making label newly created
4436 *label = _account_dup_text(label_record->label);
4438 _account_type_free_label_with_items(label_record);
4440 rc = _account_query_step(hstmt);
4443 rc = _account_query_finalize(hstmt);
4444 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4447 error_code = _ACCOUNT_ERROR_NONE;
4450 if (hstmt != NULL) {
4451 rc = _account_query_finalize(hstmt);
4452 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4456 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
4457 error_code = _account_type_query_label_by_locale_from_global_db(app_id, locale, label);
4459 _INFO("_account_type_query_label_by_locale() end : error_code = %d", error_code);
4464 static int _account_insert_custom(account_s *account, int account_id)
4466 _INFO("_account_insert_custom start");
4469 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4470 account_stmt hstmt = NULL;
4472 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4474 if (g_slist_length(account->custom_list) == 0) {
4475 ACCOUNT_DEBUG("_account_insert_custom, no custom data\n");
4476 return _ACCOUNT_ERROR_NONE;
4479 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
4481 rc = _account_get_record_count(g_hAccountDB, query);
4483 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4484 ACCOUNT_ERROR("Access failed(%d, %s)", _account_db_err_msg(g_hAccountDB));
4485 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4489 ACCOUNT_SLOGE("_account_insert_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
4490 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4497 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
4500 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4501 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (AccountId, AppId, Key, Value) VALUES "
4502 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
4504 hstmt = _account_prepare_query(g_hAccountDB, query);
4506 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4507 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4508 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4511 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
4513 account_custom_s* custom_data = NULL;
4514 custom_data = (account_custom_s*)iter->data;
4516 ret = _account_query_bind_int(hstmt, count++, account_id);
4517 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
4518 ret = _account_query_bind_text(hstmt, count++, account->package_name);
4519 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4520 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
4521 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4522 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
4523 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4525 rc = _account_query_step(hstmt);
4527 if (rc != SQLITE_DONE) {
4528 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4532 rc = _account_query_finalize(hstmt);
4533 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4538 _INFO("_account_insert_custom end");
4540 return _ACCOUNT_ERROR_NONE;
4543 static int _account_update_custom(account_s *account, int account_id)
4546 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4547 account_stmt hstmt = NULL;
4549 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4551 if (g_slist_length(account->custom_list) == 0) {
4552 ACCOUNT_DEBUG("_account_update_custom, no custom data\n");
4553 return _ACCOUNT_ERROR_NONE;
4556 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
4558 rc = _account_get_record_count(g_hAccountDB, query);
4560 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4561 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4562 pthread_mutex_unlock(&account_mutex);
4563 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4564 } else if (_account_db_err_code(g_hAccountDB) == SQLITE_BUSY) {
4565 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
4566 pthread_mutex_unlock(&account_mutex);
4567 return _ACCOUNT_ERROR_DATABASE_BUSY;
4571 ACCOUNT_SLOGE("_account_update_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
4572 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4575 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4577 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId=? ", ACCOUNT_CUSTOM_TABLE);
4578 hstmt = _account_prepare_query(g_hAccountDB, query);
4580 _account_query_bind_int(hstmt, count++, (int)account_id);
4581 rc = _account_query_step(hstmt);
4583 if (rc == SQLITE_BUSY) {
4584 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4585 return _ACCOUNT_ERROR_DATABASE_BUSY;
4586 } else if (rc != SQLITE_DONE) {
4587 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4588 return _ACCOUNT_ERROR_DB_FAILED;
4591 rc = _account_query_finalize(hstmt);
4592 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4597 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
4600 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4601 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AccountId, AppId, Key, Value) VALUES "
4602 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
4604 hstmt = _account_prepare_query(g_hAccountDB, query);
4606 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4607 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4608 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4611 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
4613 account_custom_s* custom_data = NULL;
4614 custom_data = (account_custom_s*)iter->data;
4616 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
4617 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
4618 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
4619 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4620 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
4621 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4622 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
4623 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4625 rc = _account_query_step(hstmt);
4627 if (rc != SQLITE_DONE) {
4628 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4632 rc = _account_query_finalize(hstmt);
4633 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4638 return _ACCOUNT_ERROR_NONE;