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 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), { *error_code = rc; }, NULL, ("finalize error"));
1190 *error_code = _ACCOUNT_ERROR_NONE;
1193 if (hstmt != NULL) {
1194 rc = _account_query_finalize(hstmt);
1195 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), { *error_code = rc; }, NULL, ("finalize error"));
1199 pthread_mutex_unlock(&account_mutex);
1201 return capability_list;
1204 static int _account_compare_old_record(account_s *new_account, int account_id)
1206 int error_code = _ACCOUNT_ERROR_NONE;
1207 account_stmt hstmt = NULL;
1208 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1210 account_s *old_account = NULL;
1212 ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1213 ACCOUNT_RETURN_VAL((new_account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
1214 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1216 old_account = (account_s*)calloc(1, sizeof(account_s));
1217 if (old_account == NULL) {
1218 _ERR("Out of Memory");
1219 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
1222 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1224 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
1225 hstmt = _account_prepare_query(g_hAccountDB, query);
1227 rc = _account_query_step(hstmt);
1228 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1230 while (rc == SQLITE_ROW) {
1231 _account_convert_column_to_account(hstmt, old_account);
1232 rc = _account_query_step(hstmt);
1235 rc = _account_query_finalize(hstmt);
1236 ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1240 error_code = _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, old_account->id, (void*)old_account);
1241 ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
1244 error_code = _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, old_account->id, (void*)old_account);
1245 ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
1249 new_account->id = old_account->id;
1252 if (!new_account->user_name) {
1253 if (old_account->user_name)
1254 new_account->user_name = _account_dup_text(old_account->user_name);
1258 if (!new_account->display_name) {
1259 if (old_account->display_name)
1260 new_account->display_name = _account_dup_text(old_account->display_name);
1264 if (!new_account->email_address) {
1265 if (old_account->email_address)
1266 new_account->email_address = _account_dup_text(old_account->email_address);
1270 if (!new_account->domain_name) {
1271 if (old_account->domain_name)
1272 new_account->domain_name = _account_dup_text(old_account->domain_name);
1276 if (!new_account->icon_path) {
1277 if (old_account->icon_path)
1278 new_account->icon_path = _account_dup_text(old_account->icon_path);
1282 if (!new_account->source) {
1283 if (old_account->source)
1284 new_account->source = _account_dup_text(old_account->source);
1287 _ACCOUNT_FREE(new_account->package_name);
1288 new_account->package_name = _account_dup_text(old_account->package_name);
1291 if (!new_account->access_token) {
1292 if (old_account->access_token)
1293 new_account->access_token = _account_dup_text(old_account->access_token);
1298 for (i = 0; i < USER_TXT_CNT; i++) {
1299 if (!new_account->user_data_txt[i]) {
1300 if (old_account->user_data_txt[i])
1301 new_account->user_data_txt[i] = _account_dup_text(old_account->user_data_txt[i]);
1306 if (new_account->auth_type == _ACCOUNT_AUTH_TYPE_INVALID)
1307 new_account->auth_type = old_account->auth_type;
1310 if (new_account->secret == _ACCOUNT_SECRECY_INVALID)
1311 new_account->secret = old_account->secret;
1314 if (new_account->sync_support == _ACCOUNT_SYNC_INVALID)
1315 new_account->sync_support = old_account->sync_support;
1318 for (i = 0; i < USER_INT_CNT; i++) {
1319 if (new_account->user_data_int[i] == 0)
1320 new_account->user_data_int[i] = old_account->user_data_int[i];
1325 _account_free_account_with_items(old_account);
1327 if (hstmt != NULL) {
1328 rc = _account_query_finalize(hstmt);
1329 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1333 return _ACCOUNT_ERROR_NONE;
1336 static int _account_get_package_name_from_account_id(int account_id, char **package_name)
1338 int error_code = _ACCOUNT_ERROR_NONE;
1339 account_stmt hstmt = NULL;
1340 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1342 account_s *old_account = NULL;
1344 ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1345 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1347 old_account = (account_s*)calloc(1, sizeof(account_s));
1348 if (old_account == NULL) {
1349 _ERR("Out Of memory");
1350 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
1353 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1355 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
1356 hstmt = _account_prepare_query(g_hAccountDB, query);
1358 rc = _account_query_step(hstmt);
1359 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1361 while (rc == SQLITE_ROW) {
1362 _account_convert_column_to_account(hstmt, old_account);
1363 rc = _account_query_step(hstmt);
1366 rc = _account_query_finalize(hstmt);
1367 ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1370 // get package name.
1371 *package_name = _account_dup_text(old_account->package_name);
1375 _account_free_account_with_items(old_account);
1377 if (hstmt != NULL) {
1378 rc = _account_query_finalize(hstmt);
1379 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1387 static int _account_update_account(int pid, uid_t uid, account_s *account, int account_id)
1389 int rc = 0, binding_count = 0;
1390 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1391 int error_code = _ACCOUNT_ERROR_NONE, count = 0, ret_transaction = 0;
1392 account_stmt hstmt = NULL;
1394 if (!account->package_name) {
1395 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
1396 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1399 /* Check permission of requested appid */
1400 char* current_appid = NULL;
1401 char *package_name = NULL;
1403 current_appid = _account_get_current_appid(pid, uid);
1404 error_code = _account_get_package_name_from_account_id(account_id, &package_name);
1406 if (error_code != _ACCOUNT_ERROR_NONE || package_name == NULL) {
1407 ACCOUNT_ERROR("No package name with account_id\n");
1408 _ACCOUNT_FREE(current_appid);
1409 _ACCOUNT_FREE(package_name);
1410 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1413 error_code = _account_check_appid_group_with_package_name(current_appid, package_name, uid);
1414 ACCOUNT_DEBUG("UPDATE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name); // TODO: remove the log later.
1416 _ACCOUNT_FREE(current_appid);
1417 _ACCOUNT_FREE(package_name);
1419 if (error_code != _ACCOUNT_ERROR_NONE) {
1420 ACCOUNT_ERROR("No permission to update\n");
1421 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1424 error_code = encrypt_access_token(account);
1425 if (error_code != _ACCOUNT_ERROR_NONE) {
1426 _ERR("_encrypt_access_token error");
1430 error_code = _account_compare_old_record(account, account_id);
1431 if (error_code != _ACCOUNT_ERROR_NONE) {
1432 ACCOUNT_ERROR("_account_compare_old_record fail\n");
1436 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1437 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1438 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1439 } else if (_account_db_err_code(g_hAccountDB) == SQLITE_BUSY) {
1440 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
1441 return _ACCOUNT_ERROR_DATABASE_BUSY;
1444 if (!account->user_name && !account->display_name && !account->email_address) {
1445 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1446 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1449 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1451 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
1453 count = _account_get_record_count(g_hAccountDB, query);
1455 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
1456 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1459 /* transaction control required*/
1460 ret_transaction = _account_begin_transaction(g_hAccountDB);
1461 if (ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY) {
1462 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
1463 pthread_mutex_unlock(&account_mutex);
1464 return _ACCOUNT_ERROR_DATABASE_BUSY;
1467 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1468 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
1469 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
1470 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
1471 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
1473 hstmt = _account_prepare_query(g_hAccountDB, query);
1475 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1476 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1477 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1478 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1481 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)));
1483 binding_count = _account_convert_account_to_sql(account, hstmt, query);
1484 _account_query_bind_int(hstmt, binding_count++, account_id);
1486 rc = _account_query_step(hstmt);
1487 if (rc != SQLITE_DONE)
1488 ACCOUNT_SLOGE("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
1490 rc = _account_query_finalize(hstmt);
1491 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1494 _INFO("update query=%s", query);
1496 /*update capability*/
1497 error_code = _account_update_capability(account, account_id);
1498 if (error_code != _ACCOUNT_ERROR_NONE && error_code != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
1499 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1500 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1505 error_code = _account_update_custom(account, account_id);
1506 if (error_code != _ACCOUNT_ERROR_NONE && error_code != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
1507 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1508 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1512 ret_transaction = _account_end_transaction(g_hAccountDB, TRUE);
1514 _INFO("update end");
1520 static int _account_update_account_ex(account_s *account, int account_id)
1522 int rc = 0, binding_count = 0;
1523 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1524 int error_code = _ACCOUNT_ERROR_NONE, count = 0, ret_transaction = 0;
1525 account_stmt hstmt = NULL;
1527 if (!account->package_name) {
1528 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
1529 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1532 error_code = encrypt_access_token(account);
1533 if (error_code != _ACCOUNT_ERROR_NONE) {
1534 _ERR("_encrypt_access_token error");
1538 error_code = _account_compare_old_record(account, account_id);
1539 if (error_code != _ACCOUNT_ERROR_NONE) {
1540 ACCOUNT_ERROR("_account_compare_old_record fail\n");
1544 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1545 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1546 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1549 if (!account->user_name && !account->display_name && !account->email_address) {
1550 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1551 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1554 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1556 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
1558 count = _account_get_record_count(g_hAccountDB, query);
1560 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
1561 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1564 /* transaction control required*/
1565 ret_transaction = _account_begin_transaction(g_hAccountDB);
1566 if (ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY) {
1567 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
1568 pthread_mutex_unlock(&account_mutex);
1569 return _ACCOUNT_ERROR_DATABASE_BUSY;
1572 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1573 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
1574 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
1575 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
1576 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
1578 hstmt = _account_prepare_query(g_hAccountDB, query);
1580 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1581 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1582 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1583 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1586 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)));
1588 _INFO("account_update_to_db_by_id_ex_p : before convert() : account_id[%d], user_name=%s", account->id, account->user_name);
1589 binding_count = _account_convert_account_to_sql(account, hstmt, query);
1590 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], user_name=%s", account->id, account->user_name);
1591 _INFO("account_update_to_db_by_id_ex_p : before bind()");
1592 rc = _account_query_bind_int(hstmt, binding_count++, account_id);
1593 _INFO("account_update_to_db_by_id_ex_p : after bind() : ret = %d", rc);
1595 rc = _account_query_step(hstmt);
1596 if (rc != SQLITE_DONE)
1597 ACCOUNT_SLOGE("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
1598 _INFO("account_update_to_db_by_id_ex_p : after query_step() : ret = %d", rc);
1600 rc = _account_query_finalize(hstmt);
1601 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1603 _INFO("account_update_to_db_by_id_ex_p : after query_filnalize() : ret = %d", rc);
1605 _INFO("account_update_to_db_by_id_ex_p : before update_capability()");
1606 /*update capability*/
1607 error_code = _account_update_capability(account, account_id);
1608 if (error_code != _ACCOUNT_ERROR_NONE && error_code != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
1609 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1610 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1613 _INFO("account_update_to_db_by_id_ex_p : after update_capability()");
1615 _INFO("account_update_to_db_by_id_ex_p : before update_custom()");
1617 error_code = _account_update_custom(account, account_id);
1618 if (error_code != _ACCOUNT_ERROR_NONE && error_code != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
1619 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1620 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1623 _INFO("account_update_to_db_by_id_ex_p : after update_custom()");
1625 ret_transaction = _account_end_transaction(g_hAccountDB, TRUE);
1631 int _account_update_to_db_by_id(int pid, uid_t uid, account_s* account, int account_id)
1633 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
1634 ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
1635 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1636 int error_code = _ACCOUNT_ERROR_NONE;
1637 account_s* data = (account_s*)account;
1639 pthread_mutex_lock(&account_mutex);
1641 error_code = _account_update_account(pid, uid, data, account_id);
1643 if (error_code != _ACCOUNT_ERROR_NONE) {
1644 pthread_mutex_unlock(&account_mutex);
1648 pthread_mutex_unlock(&account_mutex);
1650 char buf[64] = {0,};
1651 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_UPDATE, account_id);
1652 _account_insert_delete_update_notification_send(buf);
1654 return _ACCOUNT_ERROR_NONE;
1657 int _account_update_to_db_by_id_ex(account_s* account, int account_id)
1659 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
1660 ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
1661 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1662 int error_code = _ACCOUNT_ERROR_NONE;
1663 account_s* data = account;
1665 pthread_mutex_lock(&account_mutex);
1667 _INFO("before update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name);
1668 error_code = _account_update_account_ex(data, account_id);
1669 _INFO("after update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name);
1671 if (error_code != _ACCOUNT_ERROR_NONE) {
1672 pthread_mutex_unlock(&account_mutex);
1676 pthread_mutex_unlock(&account_mutex);
1678 char buf[64] = {0,};
1679 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_UPDATE, account_id);
1680 _account_insert_delete_update_notification_send(buf);
1682 return _ACCOUNT_ERROR_NONE;
1685 int _account_update_to_db_by_user_name(int pid, uid_t uid, account_s* account, const char *user_name, const char *package_name)
1687 ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
1688 ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
1689 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1691 int error_code = _ACCOUNT_ERROR_NONE;
1692 account_s *data = (account_s*)account;
1694 pthread_mutex_lock(&account_mutex);
1696 error_code = _account_update_account_by_user_name(pid, uid, data, user_name, package_name);
1698 pthread_mutex_unlock(&account_mutex);
1700 char buf[64] = {0,};
1701 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_UPDATE, data->id);
1702 _account_insert_delete_update_notification_send(buf);
1707 GSList* _account_db_query_all(int pid, uid_t uid)
1709 //int error_code = _ACCOUNT_ERROR_NONE;
1710 account_stmt hstmt = NULL;
1711 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1713 GSList *account_list = NULL;
1715 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected."));
1717 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1719 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TABLE);
1720 hstmt = _account_prepare_query(g_hAccountDB, query);
1722 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1723 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1727 rc = _account_query_step(hstmt);
1729 account_s *account_record = NULL;
1731 if (rc != SQLITE_ROW) {
1732 _ERR("The record isn't found");
1736 while (rc == SQLITE_ROW) {
1737 account_record = (account_s*) malloc(sizeof(account_s));
1739 if (account_record == NULL) {
1740 ACCOUNT_FATAL("malloc Failed");
1744 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
1745 _account_convert_column_to_account(hstmt, account_record);
1746 account_list = g_slist_append(account_list, account_record);
1747 rc = _account_query_step(hstmt);
1750 rc = _account_query_finalize(hstmt);
1751 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, NULL, ("finalize error"));
1756 for (iter = account_list; iter != NULL; iter = g_slist_next(iter)) {
1757 account_s *account = NULL;
1758 account = (account_s*)iter->data;
1759 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, account->id, (void*)account);
1760 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, account->id, (void*)account);
1764 if (hstmt != NULL) {
1765 rc = _account_query_finalize(hstmt);
1766 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_gslist_account_free(account_list); }, NULL, ("finalize error"));
1771 _remove_sensitive_info_from_non_owning_account_slist(account_list, pid, uid);
1773 return account_list;
1776 int _account_update_sync_status_by_id(uid_t uid, int account_db_id, const int sync_status)
1778 int error_code = _ACCOUNT_ERROR_NONE;
1779 account_stmt hstmt = NULL;
1780 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1784 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1785 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1786 if ((sync_status < 0) || (sync_status >= _ACCOUNT_SYNC_MAX)) {
1787 ACCOUNT_SLOGE("(%s)-(%d) sync_status is less than 0 or more than enum max.\n", __FUNCTION__, __LINE__);
1788 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1791 pthread_mutex_lock(&account_mutex);
1793 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1795 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_db_id);
1797 rc = _account_get_record_count(g_hAccountDB, query);
1799 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1800 pthread_mutex_unlock(&account_mutex);
1801 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1802 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1806 ACCOUNT_SLOGE("account_update_sync_status_by_id : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
1807 pthread_mutex_unlock(&account_mutex);
1808 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1811 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1813 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET sync_support=? WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
1814 hstmt = _account_prepare_query(g_hAccountDB, query);
1816 _account_query_bind_int(hstmt, count, sync_status);
1818 rc = _account_query_step(hstmt);
1820 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1821 pthread_mutex_unlock(&account_mutex);
1822 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1823 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1826 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_DB_FAILED,
1827 ("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB)));
1829 rc = _account_query_finalize(hstmt);
1830 if (rc != _ACCOUNT_ERROR_NONE) {
1831 ACCOUNT_ERROR("_account_query_finalize error");
1832 pthread_mutex_unlock(&account_mutex);
1836 char buf[64] = {0,};
1837 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_SYNC_UPDATE, account_db_id);
1838 _account_insert_delete_update_notification_send(buf);
1841 error_code = _ACCOUNT_ERROR_NONE;
1844 if (hstmt != NULL) {
1845 rc = _account_query_finalize(hstmt);
1846 pthread_mutex_unlock(&account_mutex);
1847 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1851 pthread_mutex_unlock(&account_mutex);
1856 int _account_query_account_by_account_id(int pid, uid_t uid, int account_db_id, account_s *account_record)
1858 _INFO("_account_query_account_by_account_id() start, account_db_id=[%d]", account_db_id);
1860 int error_code = _ACCOUNT_ERROR_NONE;
1861 account_stmt hstmt = NULL;
1862 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1865 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1866 ACCOUNT_RETURN_VAL(account_record != NULL, {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
1867 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1869 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1871 ACCOUNT_DEBUG("starting db operations");
1873 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
1874 hstmt = _account_prepare_query(g_hAccountDB, query);
1875 rc = _account_db_err_code(g_hAccountDB);
1876 _INFO("after _account_prepare_query, rc=[%d]", rc);
1878 if (rc == SQLITE_PERM) {
1879 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1880 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1883 ACCOUNT_DEBUG("before _account_query_step");
1884 rc = _account_query_step(hstmt);
1885 ACCOUNT_DEBUG("after _account_query_step returned [%d]", rc);
1886 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1888 while (rc == SQLITE_ROW) {
1889 ACCOUNT_DEBUG("before _account_convert_column_to_account");
1890 _account_convert_column_to_account(hstmt, account_record);
1891 ACCOUNT_DEBUG("after _account_convert_column_to_account");
1892 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]);
1893 rc = _account_query_step(hstmt);
1896 ACCOUNT_DEBUG("account_record->id=[%d]", account_record->id);
1898 rc = _account_query_finalize(hstmt);
1899 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1901 ACCOUNT_DEBUG("before _account_query_capability_by_account_id");
1902 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, account_record->id, (void*)account_record);
1903 ACCOUNT_DEBUG("after _account_query_capability_by_account_id");
1905 ACCOUNT_DEBUG("before _account_query_custom_by_account_id");
1906 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, account_record->id, (void*)account_record);
1907 ACCOUNT_DEBUG("after _account_query_custom_by_account_id");
1910 error_code = _ACCOUNT_ERROR_NONE;
1913 if (hstmt != NULL) {
1914 rc = _account_query_finalize(hstmt);
1915 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1920 _remove_sensitive_info_from_non_owning_account(account_record, pid, uid);
1922 pthread_mutex_unlock(&account_mutex);
1923 ACCOUNT_DEBUG("_account_query_account_by_account_id end [%d]", error_code);
1928 GList* _account_query_account_by_user_name(int pid, uid_t uid, const char *user_name, int *error_code)
1930 *error_code = _ACCOUNT_ERROR_NONE;
1931 account_stmt hstmt = NULL;
1932 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1934 account_s *account_head = NULL;
1936 if (user_name == NULL) {
1937 _ERR("USER NAME IS NULL");
1938 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
1942 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1944 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ?", ACCOUNT_TABLE);
1946 hstmt = _account_prepare_query(g_hAccountDB, query);
1948 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
1949 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1950 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
1954 int binding_count = 1;
1955 _account_query_bind_text(hstmt, binding_count++, user_name);
1957 rc = _account_query_step(hstmt);
1959 if (rc != SQLITE_ROW) {
1960 _ERR("The record isn't found");
1961 *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1967 account_head = (account_s*) malloc(sizeof(account_s));
1968 if (account_head == NULL) {
1969 ACCOUNT_FATAL("malloc Failed");
1970 if (hstmt != NULL) {
1971 rc = _account_query_finalize(hstmt);
1972 if (rc != _ACCOUNT_ERROR_NONE) {
1973 _ERR("finalize error");
1979 *error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
1982 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
1984 while (rc == SQLITE_ROW) {
1985 account_s* account_record = NULL;
1987 account_record = (account_s*) malloc(sizeof(account_s));
1989 if (account_record == NULL) {
1990 ACCOUNT_FATAL("malloc Failed");
1993 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
1995 _account_convert_column_to_account(hstmt, account_record);
1997 account_head->account_list = g_list_append(account_head->account_list, account_record);
1999 rc = _account_query_step(hstmt);
2003 rc = _account_query_finalize(hstmt);
2005 if (rc != _ACCOUNT_ERROR_NONE) {
2006 _ERR("finalize error");
2015 tmp = g_list_length(account_head->account_list);
2017 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2018 //account_h account;
2019 //account = (account_h)iter->data;
2021 account_s *testaccount = (account_s*)iter->data;
2023 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, testaccount->id, (void*)testaccount);
2024 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, testaccount->id, (void*)testaccount);
2027 *error_code = _ACCOUNT_ERROR_NONE;
2030 if (hstmt != NULL) {
2031 rc = _account_query_finalize(hstmt);
2032 if (rc != _ACCOUNT_ERROR_NONE) {
2033 _ERR("finalize error");
2039 pthread_mutex_unlock(&account_mutex);
2042 _remove_sensitive_info_from_non_owning_account_list(account_head->account_list, pid, uid);
2043 GList* result = account_head->account_list;
2044 _ACCOUNT_FREE(account_head);
2052 _account_query_account_by_capability(int pid, uid_t uid, const char* capability_type, const int capability_value, int *error_code)
2054 *error_code = _ACCOUNT_ERROR_NONE;
2055 account_stmt hstmt = NULL;
2056 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2059 ACCOUNT_RETURN_VAL((capability_type != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("capability_type IS NULL"));
2061 if ((capability_value < 0) || (capability_value >= _ACCOUNT_CAPABILITY_STATE_MAX)) {
2062 ACCOUNT_SLOGE("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
2063 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
2067 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), { *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED; }, NULL, ("The database isn't connected."));
2069 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2071 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=? AND value=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
2073 hstmt = _account_prepare_query(g_hAccountDB, query);
2075 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2076 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2077 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
2081 int binding_count = 1;
2082 _account_query_bind_text(hstmt, binding_count++, capability_type);
2083 _account_query_bind_int(hstmt, binding_count++, capability_value);
2085 rc = _account_query_step(hstmt);
2087 account_s* account_head = NULL;
2089 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2093 account_head = (account_s*) malloc(sizeof(account_s));
2094 if (account_head == NULL) {
2095 ACCOUNT_FATAL("malloc Failed");
2096 if (hstmt != NULL) {
2097 rc = _account_query_finalize(hstmt);
2098 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), { *error_code = rc; }, NULL, ("finalize error"));
2101 *error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
2104 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2106 while (rc == SQLITE_ROW) {
2107 account_s* account_record = NULL;
2109 account_record = (account_s*) malloc(sizeof(account_s));
2111 if (account_record == NULL) {
2112 ACCOUNT_FATAL("malloc Failed");
2115 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2117 _account_convert_column_to_account(hstmt, account_record);
2119 account_head->account_list = g_list_append(account_head->account_list, account_record);
2121 rc = _account_query_step(hstmt);
2125 rc = _account_query_finalize(hstmt);
2126 ACCOUNT_CATCH_ERROR_P((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2132 tmp = g_list_length(account_head->account_list);
2134 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2135 //account_h account = NULL;
2136 //account = (account_h)iter->data;
2137 account_s* testaccount = (account_s*)iter->data;
2139 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, testaccount->id, (void*)testaccount);
2140 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, testaccount->id, (void*)testaccount);
2144 *error_code = _ACCOUNT_ERROR_NONE;
2147 if (hstmt != NULL) {
2148 rc = _account_query_finalize(hstmt);
2149 if (rc != _ACCOUNT_ERROR_NONE) {
2151 _ERR("finalize error");
2156 if (*error_code != _ACCOUNT_ERROR_NONE && account_head) {
2157 _account_glist_account_free(account_head->account_list);
2158 _ACCOUNT_FREE(account_head);
2159 account_head = NULL;
2162 pthread_mutex_unlock(&account_mutex);
2165 _remove_sensitive_info_from_non_owning_account_list(account_head->account_list, pid, uid);
2166 GList* result = account_head->account_list;
2167 _ACCOUNT_FREE(account_head);
2174 GList* _account_query_account_by_capability_type(int pid, uid_t uid, const char* capability_type, int *error_code)
2176 *error_code = _ACCOUNT_ERROR_NONE;
2177 account_stmt hstmt = NULL;
2178 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2181 ACCOUNT_RETURN_VAL((capability_type != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("capability_type IS NULL"));
2182 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), { *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED; },
2183 NULL, ("The database isn't connected."));
2185 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2187 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
2189 hstmt = _account_prepare_query(g_hAccountDB, query);
2191 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2192 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2193 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
2197 int binding_count = 1;
2198 _account_query_bind_text(hstmt, binding_count++, capability_type);
2200 rc = _account_query_step(hstmt);
2202 account_s* account_head = NULL;
2204 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2208 account_head = (account_s*) malloc(sizeof(account_s));
2209 if (account_head == NULL) {
2210 ACCOUNT_FATAL("malloc Failed");
2211 if (hstmt != NULL) {
2212 rc = _account_query_finalize(hstmt);
2213 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), { *error_code = rc; }, NULL, ("finalize error"));
2216 *error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
2219 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2221 while (rc == SQLITE_ROW) {
2222 account_s* account_record = NULL;
2224 account_record = (account_s*) malloc(sizeof(account_s));
2226 if (account_record == NULL) {
2227 ACCOUNT_FATAL("malloc Failed");
2230 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2232 _account_convert_column_to_account(hstmt, account_record);
2234 account_head->account_list = g_list_append(account_head->account_list, account_record);
2236 rc = _account_query_step(hstmt);
2240 rc = _account_query_finalize(hstmt);
2241 ACCOUNT_CATCH_ERROR_P((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2247 tmp = g_list_length(account_head->account_list);
2249 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2250 account_s* testaccount = (account_s*)iter->data;
2252 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, testaccount->id, (void*)testaccount);
2253 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, testaccount->id, (void*)testaccount);
2257 *error_code = _ACCOUNT_ERROR_NONE;
2260 if (hstmt != NULL) {
2261 rc = _account_query_finalize(hstmt);
2262 if (rc != _ACCOUNT_ERROR_NONE) {
2264 _ERR("finalize error");
2269 if ((*error_code != _ACCOUNT_ERROR_NONE) && account_head) {
2270 _account_glist_account_free(account_head->account_list);
2271 _ACCOUNT_FREE(account_head);
2272 account_head = NULL;
2275 pthread_mutex_unlock(&account_mutex);
2278 _remove_sensitive_info_from_non_owning_account_list(account_head->account_list, pid, uid);
2279 GList* result = account_head->account_list;
2280 _ACCOUNT_FREE(account_head);
2287 GList* account_server_query_account_by_package_name(const char* package_name, int *error_code, int pid, uid_t uid)
2289 _INFO("account_server_query_account_by_package_name start");
2291 GList * account_list = NULL;
2292 *error_code = _ACCOUNT_ERROR_NONE;
2294 ACCOUNT_RETURN_VAL((package_name != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("PACKAGE NAME IS NULL"));
2295 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), { *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED; }, NULL, ("The database isn't connected."));
2297 account_list = _account_query_account_by_package_name(g_hAccountDB, package_name, error_code, pid, uid);
2299 _INFO("account_server_query_account_by_package_name end");
2301 return account_list;
2304 int account_server_delete_account_by_package_name(const char* package_name, bool permission, int pid, uid_t uid)
2306 _INFO("account_db_delete_account_by_package_name");
2308 int error_code = _ACCOUNT_ERROR_NONE;
2310 ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
2311 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2313 error_code = _account_delete_account_by_package_name(g_hAccountDB, package_name, permission, pid, uid);
2315 _INFO("account_server_delete_account_by_package_name end");
2320 int _account_delete(int pid, uid_t uid, int account_id)
2322 int error_code = _ACCOUNT_ERROR_NONE;
2323 account_stmt hstmt = NULL;
2324 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2326 int ret_transaction = 0;
2327 bool is_success = FALSE;
2329 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2332 /* Check requested ID to delete */
2333 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id=%d", ACCOUNT_TABLE, account_id);
2335 count = _account_get_record_count(g_hAccountDB, query);
2337 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2338 pthread_mutex_unlock(&account_mutex);
2339 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2340 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2344 ACCOUNT_ERROR("account id(%d) is not exist. count(%d)\n", account_id, count);
2345 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
2348 /* Check permission of requested appid */
2349 char* current_appid = NULL;
2350 char *package_name = NULL;
2352 current_appid = _account_get_current_appid(pid, uid);
2354 error_code = _account_get_package_name_from_account_id(account_id, &package_name);
2356 if (error_code != _ACCOUNT_ERROR_NONE) {
2357 ACCOUNT_ERROR("No package name with account_id\n");
2358 _ACCOUNT_FREE(current_appid);
2359 _ACCOUNT_FREE(package_name);
2360 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
2362 ACCOUNT_DEBUG("DELETE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name);
2364 error_code = _account_check_appid_group_with_package_name(current_appid, package_name, uid);
2366 _ACCOUNT_FREE(current_appid);
2367 _ACCOUNT_FREE(package_name);
2369 if (error_code != _ACCOUNT_ERROR_NONE) {
2370 ACCOUNT_ERROR("No permission to delete\n");
2371 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2374 /* transaction control required*/
2375 ret_transaction = _account_begin_transaction(g_hAccountDB);
2377 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2378 pthread_mutex_unlock(&account_mutex);
2379 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2380 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2383 if (ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY) {
2384 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
2385 pthread_mutex_unlock(&account_mutex);
2386 return _ACCOUNT_ERROR_DATABASE_BUSY;
2389 if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2390 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
2391 pthread_mutex_unlock(&account_mutex);
2392 return ret_transaction;
2395 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2396 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
2398 hstmt = _account_prepare_query(g_hAccountDB, query);
2400 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2401 pthread_mutex_unlock(&account_mutex);
2402 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2403 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2406 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2407 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2409 rc = _account_query_step(hstmt);
2410 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2412 rc = _account_query_finalize(hstmt);
2414 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2417 ACCOUNT_MEMSET(query, 0, sizeof(query));
2419 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
2421 hstmt = _account_prepare_query(g_hAccountDB, query);
2422 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2423 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2425 rc = _account_query_step(hstmt);
2426 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
2428 rc = _account_query_finalize(hstmt);
2429 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2432 /* delete custom data */
2433 ACCOUNT_MEMSET(query, 0, sizeof(query));
2435 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
2437 hstmt = _account_prepare_query(g_hAccountDB, query);
2439 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2440 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2442 rc = _account_query_step(hstmt);
2443 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
2445 rc = _account_query_finalize(hstmt);
2446 ACCOUNT_CATCH_ERROR(rc == _ACCOUNT_ERROR_NONE, {}, rc, ("finalize error", account_id, rc));
2452 if (hstmt != NULL) {
2453 rc = _account_query_finalize(hstmt);
2454 if (rc != _ACCOUNT_ERROR_NONE) {
2455 ACCOUNT_ERROR("rc (%d)", rc);
2462 ret_transaction = _account_end_transaction(g_hAccountDB, is_success);
2464 if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2465 ACCOUNT_ERROR("account_delete:_account_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
2467 if (is_success == true) {
2468 char buf[64] = {0,};
2469 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_DELETE, account_id);
2470 _account_insert_delete_update_notification_send(buf);
2474 pthread_mutex_unlock(&account_mutex);
2479 static int _account_query_account_by_username_and_package(const char* username, const char* package_name, account_s *account)
2483 int error_code = _ACCOUNT_ERROR_NONE;
2484 account_stmt hstmt = NULL;
2485 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2487 int binding_count = 1;
2489 ACCOUNT_RETURN_VAL((username != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("username IS NULL"));
2490 ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name IS NULL"));
2491 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2492 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2494 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2496 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
2497 hstmt = _account_prepare_query(g_hAccountDB, query);
2499 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2500 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2501 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2504 _account_query_bind_text(hstmt, binding_count++, username);
2505 _account_query_bind_text(hstmt, binding_count++, package_name);
2507 rc = _account_query_step(hstmt);
2508 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2510 account_s *account_record = account;
2512 while (rc == SQLITE_ROW) {
2513 _account_convert_column_to_account(hstmt, account_record);
2514 rc = _account_query_step(hstmt);
2517 rc = _account_query_finalize(hstmt);
2518 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2519 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, account_record->id, (void*)account_record);
2520 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, account_record->id, (void*)account_record);
2523 error_code = _ACCOUNT_ERROR_NONE;
2526 if (hstmt != NULL) {
2527 rc = _account_query_finalize(hstmt);
2528 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2532 pthread_mutex_unlock(&account_mutex);
2537 int _account_create(account_s **account)
2540 ACCOUNT_SLOGE("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__);
2541 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2544 account_s *data = (account_s*)malloc(sizeof(account_s));
2547 ACCOUNT_FATAL("Memory Allocation Failed");
2548 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
2550 ACCOUNT_MEMSET(data, 0, sizeof(account_s));
2552 /*Setting account as visible by default*/
2553 //data->secret = _ACCOUNT_SECRECY_VISIBLE;
2555 /*Setting account as not supporting sync by default*/
2556 //data->sync_support = _ACCOUNT_SYNC_NOT_SUPPORT;
2560 return _ACCOUNT_ERROR_NONE;
2563 int _account_destroy(account_s *account)
2565 account_s *data = account;
2567 ACCOUNT_RETURN_VAL((data != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Account handle is null!"));
2569 _account_free_account_with_items(data);
2571 return _ACCOUNT_ERROR_NONE;
2574 int _account_get_account_id(account_s* account, int *account_id)
2577 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2580 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2582 *account_id = account->id;
2584 return _ACCOUNT_ERROR_NONE;
2587 int _account_delete_from_db_by_user_name(int pid, uid_t uid, const char *user_name, const char *package_name)
2589 _INFO("[%s][%s]", user_name, package_name);
2591 int error_code = _ACCOUNT_ERROR_NONE;
2592 account_stmt hstmt = NULL;
2593 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2595 int ret_transaction = 0;
2596 bool is_success = FALSE;
2597 account_s *account = NULL;
2598 int binding_count = 1;
2599 int account_id = -1;
2601 ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is null!"));
2602 ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
2603 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2605 /* Check permission of requested appid */
2606 char* current_appid = NULL;
2607 char* package_name_temp = NULL;
2609 current_appid = _account_get_current_appid(pid, uid);
2611 package_name_temp = _account_dup_text(package_name);
2613 ACCOUNT_DEBUG("DELETE:user_name[%s],current_appid[%s], package_name[%s]", user_name, current_appid, package_name_temp);
2615 error_code = _account_check_appid_group_with_package_name(current_appid, package_name_temp, uid);
2617 _ACCOUNT_FREE(current_appid);
2618 _ACCOUNT_FREE(package_name_temp);
2620 if (error_code != _ACCOUNT_ERROR_NONE) {
2621 ACCOUNT_ERROR("No permission to delete\n");
2622 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2625 rc = _account_create(&account);
2626 rc = _account_query_account_by_username_and_package(user_name, package_name, account);
2630 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2631 _account_destroy(account);
2632 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2633 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2637 account_s* account_data = (account_s*)account;
2639 rc = _account_get_account_id(account_data, &account_id);
2641 rc = _account_destroy(account);
2643 /* transaction control required*/
2644 ret_transaction = _account_begin_transaction(g_hAccountDB);
2646 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2647 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2648 pthread_mutex_unlock(&account_mutex);
2649 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2653 if (ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY) {
2654 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
2655 pthread_mutex_unlock(&account_mutex);
2656 return _ACCOUNT_ERROR_DATABASE_BUSY;
2657 } else if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2658 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
2659 pthread_mutex_unlock(&account_mutex);
2660 return ret_transaction;
2663 /* delete custom data */
2664 ACCOUNT_MEMSET(query, 0, sizeof(query));
2665 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = ?", ACCOUNT_CUSTOM_TABLE);
2667 hstmt = _account_prepare_query(g_hAccountDB, query);
2669 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2670 _account_end_transaction(g_hAccountDB, FALSE);
2671 pthread_mutex_unlock(&account_mutex);
2672 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2673 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2676 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2677 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2679 _account_query_bind_int(hstmt, binding_count++, account_id);
2681 rc = _account_query_step(hstmt);
2682 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2684 rc = _account_query_finalize(hstmt);
2685 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2688 /* delete capability */
2689 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", CAPABILITY_TABLE);
2691 hstmt = _account_prepare_query(g_hAccountDB, query);
2693 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2694 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2697 _account_query_bind_text(hstmt, binding_count++, user_name);
2698 _account_query_bind_text(hstmt, binding_count++, package_name);
2700 rc = _account_query_step(hstmt);
2701 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2703 rc = _account_query_finalize(hstmt);
2704 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2707 ACCOUNT_MEMSET(query, 0, sizeof(query));
2710 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
2712 hstmt = _account_prepare_query(g_hAccountDB, query);
2713 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2714 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2718 _account_query_bind_text(hstmt, binding_count++, user_name);
2719 _account_query_bind_text(hstmt, binding_count++, package_name);
2721 rc = _account_query_step(hstmt);
2722 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));
2724 rc = _account_query_finalize(hstmt);
2725 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2731 if (hstmt != NULL) {
2732 rc = _account_query_finalize(hstmt);
2733 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2737 ret_transaction = _account_end_transaction(g_hAccountDB, is_success);
2739 if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2740 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
2742 if (is_success == true) {
2743 char buf[64] = {0,};
2744 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_DELETE, account_id);
2745 _account_insert_delete_update_notification_send(buf);
2749 pthread_mutex_unlock(&account_mutex);
2755 int _account_get_total_count_from_db(gboolean include_hidden, int *count)
2758 ACCOUNT_SLOGE("(%s)-(%d) count is NULL.\n", __FUNCTION__, __LINE__);
2759 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2762 if (!g_hAccountDB) {
2763 ACCOUNT_ERROR("DB is not opened\n");
2764 return _ACCOUNT_ERROR_DB_NOT_OPENED;
2767 char query[1024] = {0, };
2768 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2771 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s", ACCOUNT_TABLE);
2773 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s where secret = %d", ACCOUNT_TABLE, _ACCOUNT_SECRECY_VISIBLE);
2775 *count = _account_get_record_count(g_hAccountDB, query);
2777 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2778 pthread_mutex_unlock(&account_mutex);
2779 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2780 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2785 account_stmt pStmt = NULL;
2787 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
2788 if (SQLITE_OK != rc) {
2789 ACCOUNT_SLOGE("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg(g_hAccountDB));
2790 sqlite3_finalize(pStmt);
2791 return _ACCOUNT_ERROR_DB_FAILED;
2794 rc = sqlite3_step(pStmt);
2795 if (SQLITE_ROW != rc) {
2796 ACCOUNT_ERROR("[ERROR] sqlite3_step() failed\n");
2797 sqlite3_finalize(pStmt);
2798 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
2801 ncount = sqlite3_column_int(pStmt, 0);
2805 sqlite3_finalize(pStmt);
2808 ACCOUNT_ERROR("[ERROR] Number of account : %d, End", ncount);
2809 return _ACCOUNT_ERROR_DB_FAILED;
2812 return _ACCOUNT_ERROR_NONE;
2816 int account_server_query_app_id_exist(const char* app_id)
2818 _INFO("account_server_query_app_id_exist start app_id=[%s]", app_id);
2819 int ret = _ACCOUNT_ERROR_NONE;
2821 ret = _account_type_query_app_id_exist_from_all_db(g_hAccountDB, g_hAccountGlobalDB, app_id);
2823 _INFO("account_server_query_app_id_exist end error_code=[%d]", ret);
2828 int account_server_insert_account_type_to_user_db(account_type_s *account_type, int *account_type_id, uid_t uid)
2830 ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE HANDLE IS NULL"));
2831 ACCOUNT_RETURN_VAL((account_type->app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID OF ACCOUNT TYPE IS NULL"));
2832 ACCOUNT_RETURN_VAL((account_type_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE ID IS NULL"));
2834 _INFO("account_server_insert_account_type_to_user_db start uid=[%d]", uid);
2835 int ret = _ACCOUNT_ERROR_NONE;
2837 if (_account_type_check_duplicated(g_hAccountDB, account_type->app_id) ||
2838 _account_type_check_duplicated(g_hAccountGlobalDB, account_type->app_id)) {
2839 *account_type_id = -1;
2840 return _ACCOUNT_ERROR_DUPLICATED;
2843 ret = _account_type_insert_to_db(g_hAccountDB, account_type, account_type_id);
2844 _INFO("account_server_insert_account_type_to_user_db end error_code=[%d]", ret);
2849 int account_server_delete_account_type_by_app_id_from_user_db(const char * app_id)
2851 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID OF ACCOUNT TYPE IS NULL"));
2853 _INFO("account_server_delete_account_type_by_app_id_from_user_db start");
2854 int ret = _ACCOUNT_ERROR_NONE;
2856 ret = _account_type_delete_by_app_id(g_hAccountDB, app_id);
2857 _INFO("account_server_delete_account_type_by_app_id_from_user_db end error_code=[%d]", ret);
2862 GSList* _account_type_query_provider_feature_by_app_id_from_global_db(const char* app_id, int *error_code)
2864 _INFO("_account_type_query_provider_feature_by_app_id_in_global_db app_id=%s", app_id);
2865 account_stmt hstmt = NULL;
2866 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2867 int rc = 0, binding_count = 1;
2868 GSList* feature_list = NULL;
2870 ACCOUNT_RETURN_VAL((app_id != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("APP ID IS NULL"));
2871 ACCOUNT_RETURN_VAL((error_code != NULL), {_ERR("error_code pointer is NULL"); }, NULL, (""));
2872 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; _ERR("The database isn't connected."); }, NULL, ("The database isn't connected."));
2874 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2876 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
2877 _INFO("account query=[%s]", query);
2879 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
2881 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
2882 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2883 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
2887 _INFO("before _account_query_bind_text");
2888 _account_query_bind_text(hstmt, binding_count++, app_id);
2890 rc = _account_query_step(hstmt);
2892 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"));
2894 provider_feature_s* feature_record = NULL;
2896 while (rc == SQLITE_ROW) {
2898 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
2900 if (feature_record == NULL) {
2901 ACCOUNT_FATAL("malloc Failed");
2905 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
2907 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
2909 _INFO("Adding account feature_list");
2910 feature_list = g_slist_append(feature_list, feature_record);
2912 rc = _account_query_step(hstmt);
2915 *error_code = _ACCOUNT_ERROR_NONE;
2918 if (hstmt != NULL) {
2919 rc = _account_query_finalize(hstmt);
2920 if (rc != _ACCOUNT_ERROR_NONE) {
2922 _ERR("global db fianlize error");
2926 if (*error_code != _ACCOUNT_ERROR_NONE)
2927 _account_type_gslist_feature_free(feature_list);
2929 _INFO("Returning account feature_list from global db");
2931 return feature_list;
2934 GSList* _account_type_query_provider_feature_by_app_id(const char* app_id, int *error_code)
2936 _INFO("_account_type_query_provider_feature_by_app_id app_id=%s", app_id);
2937 account_stmt hstmt = NULL;
2938 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2939 int rc = 0, binding_count = 1;
2940 GSList* feature_list = NULL;
2942 ACCOUNT_RETURN_VAL((app_id != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("APP ID IS NULL"));
2943 ACCOUNT_RETURN_VAL((error_code != NULL), {_ERR("error_code pointer is NULL"); }, NULL, (""));
2944 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), { *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED; }, NULL, ("The database isn't connected."));
2946 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2948 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
2949 _INFO("account query=[%s]", query);
2951 hstmt = _account_prepare_query(g_hAccountDB, query);
2953 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
2954 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2955 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
2959 _account_query_bind_text(hstmt, binding_count++, app_id);
2961 rc = _account_query_step(hstmt);
2963 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"));
2965 provider_feature_s* feature_record = NULL;
2967 while (rc == SQLITE_ROW) {
2969 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
2971 if (feature_record == NULL) {
2972 ACCOUNT_FATAL("malloc Failed");
2976 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
2978 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
2980 _INFO("Adding account feature_list");
2981 feature_list = g_slist_append(feature_list, feature_record);
2983 rc = _account_query_step(hstmt);
2986 *error_code = _ACCOUNT_ERROR_NONE;
2988 rc = _account_query_finalize(hstmt);
2989 ACCOUNT_CATCH_ERROR_P((rc == _ACCOUNT_ERROR_NONE), { *error_code = rc; }, rc, ("account finalize error"));
2993 if (hstmt != NULL) {
2994 rc = _account_query_finalize(hstmt);
2995 if (rc != _ACCOUNT_ERROR_NONE) {
2997 _ERR("account fianlize error");
3001 _INFO("*error_code=[%d]", *error_code);
3003 if (*error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3004 feature_list = _account_type_query_provider_feature_by_app_id_from_global_db(app_id, error_code);
3006 if (*error_code != _ACCOUNT_ERROR_NONE)
3007 _account_type_gslist_feature_free(feature_list);
3009 _INFO("Returning account feature_list");
3011 return feature_list;
3014 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)
3016 int error_code = _ACCOUNT_ERROR_NONE;
3017 account_stmt hstmt = NULL;
3018 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3019 int rc = 0, binding_count = 1;
3021 _INFO("_account_type_query_provider_feature_cb_by_app_id_in_global_db start app_id=%s", app_id);
3022 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3023 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3024 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3026 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3028 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
3029 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3031 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3032 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3033 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_PERMISSION_DENIED, ("global db permission denied.\n"));
3036 _account_query_bind_text(hstmt, binding_count++, app_id);
3038 rc = _account_query_step(hstmt);
3039 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"));
3041 provider_feature_s* feature_record = NULL;
3043 while (rc == SQLITE_ROW) {
3044 bool cb_ret = FALSE;
3045 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
3047 if (feature_record == NULL) {
3048 ACCOUNT_FATAL("malloc Failed");
3052 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
3054 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
3056 cb_ret = callback(feature_record->app_id, feature_record->key, user_data);
3058 _account_type_free_feature_with_items(feature_record);
3060 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returns FALSE, its iteration is stopped!!!!\n"));
3062 rc = _account_query_step(hstmt);
3065 error_code = _ACCOUNT_ERROR_NONE;
3068 if (hstmt != NULL) {
3069 rc = _account_query_finalize(hstmt);
3070 if (rc != _ACCOUNT_ERROR_NONE) {
3072 _ERR("global db finalize error[%d]", rc);
3077 _INFO("_account_type_query_provider_feature_cb_by_app_id_in_global_db end. error_code=[%d]", error_code);
3082 int _account_type_query_provider_feature_cb_by_app_id(account_type_provider_feature_cb callback, const char* app_id, void *user_data)
3084 int error_code = _ACCOUNT_ERROR_NONE;
3085 account_stmt hstmt = NULL;
3086 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3087 int rc = 0, binding_count = 1;
3089 _INFO("_account_type_query_provider_feature_cb_by_app_id start app_id=%s", app_id);
3090 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3091 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3092 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3094 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3096 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
3097 hstmt = _account_prepare_query(g_hAccountDB, query);
3099 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
3100 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3101 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3104 _account_query_bind_text(hstmt, binding_count++, app_id);
3106 rc = _account_query_step(hstmt);
3107 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found in user db.\n"));
3109 provider_feature_s* feature_record = NULL;
3111 while (rc == SQLITE_ROW) {
3112 bool cb_ret = FALSE;
3113 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
3115 if (feature_record == NULL) {
3116 ACCOUNT_FATAL("malloc Failed");
3120 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
3122 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
3124 cb_ret = callback(feature_record->app_id, feature_record->key, user_data);
3126 _account_type_free_feature_with_items(feature_record);
3128 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
3130 rc = _account_query_step(hstmt);
3133 rc = _account_query_finalize(hstmt);
3134 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3137 error_code = _ACCOUNT_ERROR_NONE;
3140 if (hstmt != NULL) {
3141 rc = _account_query_finalize(hstmt);
3142 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3146 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3147 error_code = _account_type_query_provider_feature_cb_by_app_id_from_global_db(callback, app_id, user_data);
3149 _INFO("_account_type_query_provider_feature_cb_by_app_id end");
3154 int account_type_query_provider_feature_cb_by_app_id(account_type_provider_feature_cb callback, const char* app_id, void *user_data)
3156 int error_code = _ACCOUNT_ERROR_NONE;
3158 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3159 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3160 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3162 error_code = _account_type_query_provider_feature_cb_by_app_id(callback, app_id, user_data);
3164 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3165 error_code = _account_type_query_provider_feature_cb_by_app_id_from_global_db(callback, app_id, user_data);
3170 bool _account_type_query_supported_feature_from_global_db(const char* app_id, const char* capability, int *error_code)
3172 _INFO("_account_type_query_supported_feature_in_global_db start");
3173 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3175 *error_code = _ACCOUNT_ERROR_NONE;
3177 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3178 int record_count = 0;
3180 if (app_id == NULL || capability == NULL) {
3181 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
3185 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
3187 record_count = _account_get_record_count(g_hAccountGlobalDB, query);
3189 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3190 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3191 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3194 if (record_count <= 0) {
3195 *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
3199 _INFO("_account_type_query_supported_feature_in_global_db end");
3204 bool _account_type_query_supported_feature(const char* app_id, const char* capability, int *error_code)
3206 _INFO("_account_type_query_supported_feature start");
3208 *error_code = _ACCOUNT_ERROR_NONE;
3210 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3211 int record_count = 0;
3213 if (app_id == NULL || capability == NULL) {
3214 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
3218 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
3220 record_count = _account_get_record_count(g_hAccountDB, query);
3222 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
3223 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3224 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3228 if (record_count <= 0) {
3229 bool is_exist = false;
3230 is_exist = _account_type_query_supported_feature_from_global_db(app_id, capability, error_code);
3235 _INFO("_account_type_query_supported_feature end");
3241 static int _account_type_update_provider_feature(sqlite3 *account_db_handle, account_type_s *account_type, const char* app_id)
3244 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3245 account_stmt hstmt = NULL;
3247 ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
3249 if (g_slist_length(account_type->provider_feature_list) == 0) {
3250 ACCOUNT_ERROR("no feature\n");
3251 return _ACCOUNT_ERROR_NONE;
3254 ACCOUNT_DEBUG("app id", app_id);
3256 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3258 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id=? ", PROVIDER_FEATURE_TABLE);
3259 hstmt = _account_prepare_query(account_db_handle, query);
3261 if (_account_db_err_code(account_db_handle) == SQLITE_PERM) {
3262 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(account_db_handle));
3263 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3267 _account_query_bind_text(hstmt, count++, app_id);
3268 rc = _account_query_step(hstmt);
3270 if (rc != SQLITE_DONE) {
3271 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3272 return _ACCOUNT_ERROR_DB_FAILED;
3274 rc = _account_query_finalize(hstmt);
3275 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3280 for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
3283 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3284 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
3285 "(?, ?) ", PROVIDER_FEATURE_TABLE);
3287 hstmt = _account_prepare_query(account_db_handle, query);
3289 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg(account_db_handle)));
3291 provider_feature_s* feature_data = NULL;
3292 feature_data = (provider_feature_s*)iter->data;
3294 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
3295 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3296 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
3297 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3299 rc = _account_query_step(hstmt);
3301 if (rc != SQLITE_DONE) {
3302 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3305 rc = _account_query_finalize(hstmt);
3306 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3310 return _ACCOUNT_ERROR_NONE;
3313 static int _account_type_update_label(sqlite3 *account_db_handle, account_type_s *account_type, const char* app_id)
3316 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3317 account_stmt hstmt = NULL;
3319 ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
3321 if (g_slist_length(account_type->label_list) == 0)
3322 return _ACCOUNT_ERROR_NONE;
3324 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3326 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId=? ", LABEL_TABLE);
3327 hstmt = _account_prepare_query(account_db_handle, query);
3329 if (_account_db_err_code(account_db_handle) == SQLITE_PERM) {
3330 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(account_db_handle));
3331 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3335 _account_query_bind_text(hstmt, count++, app_id);
3336 rc = _account_query_step(hstmt);
3338 if (rc != SQLITE_DONE) {
3339 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3340 return _ACCOUNT_ERROR_DB_FAILED;
3342 rc = _account_query_finalize(hstmt);
3343 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3348 for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
3351 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3352 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
3353 "(?, ?, ?) ", LABEL_TABLE);
3355 hstmt = _account_prepare_query(account_db_handle, query);
3357 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg(account_db_handle)));
3359 label_s* label_data = NULL;
3360 label_data = (label_s*)iter->data;
3362 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
3363 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3364 ret = _account_query_bind_text(hstmt, count++, label_data->label);
3365 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3366 ret = _account_query_bind_text(hstmt, count++, label_data->locale);
3367 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3369 rc = _account_query_step(hstmt);
3371 if (rc != SQLITE_DONE) {
3372 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3375 rc = _account_query_finalize(hstmt);
3376 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3380 return _ACCOUNT_ERROR_NONE;
3384 static int _account_type_update_account(sqlite3 *account_db_handle, account_type_s *account_type, const char* app_id)
3386 int rc = 0, binding_count = 1;
3387 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3388 int error_code = _ACCOUNT_ERROR_NONE;
3389 account_stmt hstmt = NULL;
3391 if (!account_type->app_id) {
3392 ACCOUNT_ERROR("app id is mandetory field, it can not be NULL!!!!\n");
3393 return _ACCOUNT_ERROR_INVALID_PARAMETER;
3396 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3397 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET AppId=?, ServiceProviderId=?, IconPath=?, "
3398 "SmallIconPath=?, MultipleAccountSupport=? WHERE AppId=? ", ACCOUNT_TYPE_TABLE);
3400 hstmt = _account_prepare_query(account_db_handle, query);
3402 if (_account_db_err_code(account_db_handle) == SQLITE_PERM) {
3403 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(account_db_handle));
3404 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3405 } else if (_account_db_err_code(account_db_handle) == SQLITE_BUSY) {
3406 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(account_db_handle));
3407 return _ACCOUNT_ERROR_DATABASE_BUSY;
3410 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg(account_db_handle)));
3412 binding_count = _account_type_convert_account_to_sql(account_type, hstmt, query);
3413 _account_query_bind_text(hstmt, binding_count++, app_id);
3415 rc = _account_query_step(hstmt);
3416 if (rc != SQLITE_DONE)
3417 ACCOUNT_ERROR("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3419 rc = _account_query_finalize(hstmt);
3420 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3424 error_code = _account_type_update_label(account_db_handle, account_type, app_id);
3425 /* update provider feature */
3426 error_code = _account_type_update_provider_feature(account_db_handle, account_type, app_id);
3431 int _account_type_update_to_db_by_app_id(account_type_s* account_type, const char* app_id)
3433 int error_code = _ACCOUNT_ERROR_NONE;
3435 ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
3436 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3437 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3439 account_type_s* data = account_type;
3441 pthread_mutex_lock(&account_mutex);
3443 error_code = _account_type_update_account(g_hAccountDB, data, app_id);
3445 pthread_mutex_unlock(&account_mutex);
3450 GSList* _account_type_get_label_list_by_app_id_from_global_db(const char* app_id, int *error_code)
3452 *error_code = _ACCOUNT_ERROR_NONE;
3453 account_stmt hstmt = NULL;
3454 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3455 int rc = 0, binding_count = 1;
3456 GSList* label_list = NULL;
3458 ACCOUNT_RETURN_VAL((app_id != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("APP ID IS NULL"));
3459 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), { *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED; }, NULL, ("The database isn't connected."));
3461 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3463 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3464 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3466 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3467 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3468 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3473 _account_query_bind_text(hstmt, binding_count++, app_id);
3475 rc = _account_query_step(hstmt);
3476 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"));
3478 label_s* label_record = NULL;
3480 while (rc == SQLITE_ROW) {
3481 label_record = (label_s*) malloc(sizeof(label_s));
3483 if (label_record == NULL) {
3484 ACCOUNT_FATAL("malloc Failed");
3488 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3490 _account_type_convert_column_to_label(hstmt, label_record);
3492 _INFO("Adding account label_list");
3493 label_list = g_slist_append(label_list, label_record);
3495 rc = _account_query_step(hstmt);
3498 *error_code = _ACCOUNT_ERROR_NONE;
3501 if (hstmt != NULL) {
3502 rc = _account_query_finalize(hstmt);
3503 if (rc != _ACCOUNT_ERROR_NONE)
3504 _ERR("global db finalize error[%d]", rc);
3508 _INFO("Returning account global label_list");
3513 GSList* _account_type_get_label_list_by_app_id(const char* app_id, int *error_code)
3515 *error_code = _ACCOUNT_ERROR_NONE;
3516 account_stmt hstmt = NULL;
3517 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3518 int rc = 0, binding_count = 1;
3519 GSList* label_list = NULL;
3521 ACCOUNT_RETURN_VAL((app_id != NULL), { *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; }, NULL, ("APP ID IS NULL"));
3522 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), { *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED; }, NULL, ("The database isn't connected."));
3524 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3526 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3527 hstmt = _account_prepare_query(g_hAccountDB, query);
3529 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
3530 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3531 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3535 _account_query_bind_text(hstmt, binding_count++, app_id);
3537 rc = _account_query_step(hstmt);
3538 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3540 label_s* label_record = NULL;
3542 while (rc == SQLITE_ROW) {
3543 label_record = (label_s*) malloc(sizeof(label_s));
3545 if (label_record == NULL) {
3546 ACCOUNT_FATAL("malloc Failed");
3550 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3552 _account_type_convert_column_to_label(hstmt, label_record);
3554 _INFO("Adding account label_list");
3555 label_list = g_slist_append(label_list, label_record);
3557 rc = _account_query_step(hstmt);
3560 rc = _account_query_finalize(hstmt);
3561 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), { *error_code = rc; }, NULL, ("finalize error"));
3564 *error_code = _ACCOUNT_ERROR_NONE;
3567 if (hstmt != NULL) {
3568 rc = _account_query_finalize(hstmt);
3569 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), { *error_code = rc; }, NULL, ("finalize error"));
3573 if (*error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3574 label_list = _account_type_get_label_list_by_app_id_from_global_db(app_id, error_code);
3576 _INFO("Returning account label_list");
3581 int _account_type_query_label_by_app_id_from_global_db(account_type_label_cb callback, const char* app_id, void *user_data)
3583 int error_code = _ACCOUNT_ERROR_NONE;
3584 account_stmt hstmt = NULL;
3585 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3586 int rc = 0, binding_count = 1;
3588 _INFO("account_type_query_label_by_app_id_from_global_db start");
3590 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3591 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3592 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3594 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3596 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3597 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3599 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3600 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3601 error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3605 _account_query_bind_text(hstmt, binding_count++, app_id);
3607 rc = _account_query_step(hstmt);
3608 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3610 label_s* label_record = NULL;
3612 while (rc == SQLITE_ROW) {
3613 bool cb_ret = FALSE;
3614 label_record = (label_s*) malloc(sizeof(label_s));
3616 if (label_record == NULL) {
3617 ACCOUNT_FATAL("malloc Failed");
3621 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3623 _account_type_convert_column_to_label(hstmt, label_record);
3625 cb_ret = callback(label_record->app_id, label_record->label , label_record->locale, user_data);
3627 _account_type_free_label_with_items(label_record);
3629 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
3631 rc = _account_query_step(hstmt);
3634 error_code = _ACCOUNT_ERROR_NONE;
3637 if (hstmt != NULL) {
3638 rc = _account_query_finalize(hstmt);
3639 if (rc != _ACCOUNT_ERROR_NONE)
3640 _ERR("global db finalize error[%d]", rc);
3644 _INFO("account_type_query_label_by_app_id_from_global_db end [%d]", error_code);
3649 int _account_type_query_label_by_app_id(account_type_label_cb callback, const char* app_id, void *user_data)
3651 int error_code = _ACCOUNT_ERROR_NONE;
3652 account_stmt hstmt = NULL;
3653 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3654 int rc = 0, binding_count = 1;
3656 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3657 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3658 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3660 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3662 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3663 hstmt = _account_prepare_query(g_hAccountDB, query);
3665 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
3666 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3667 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3670 _account_query_bind_text(hstmt, binding_count++, app_id);
3672 rc = _account_query_step(hstmt);
3673 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3675 label_s* label_record = NULL;
3677 while (rc == SQLITE_ROW) {
3678 bool cb_ret = FALSE;
3679 label_record = (label_s*) malloc(sizeof(label_s));
3681 if (label_record == NULL) {
3682 ACCOUNT_FATAL("malloc Failed");
3686 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3688 _account_type_convert_column_to_label(hstmt, label_record);
3690 cb_ret = callback(label_record->app_id, label_record->label , label_record->locale, user_data);
3692 _account_type_free_label_with_items(label_record);
3694 //ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
3695 if (cb_ret != TRUE) {
3696 _INFO("Callback func returs FALSE, its iteration is stopped!!!!\n");
3700 rc = _account_query_step(hstmt);
3703 rc = _account_query_finalize(hstmt);
3704 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3707 error_code = _ACCOUNT_ERROR_NONE;
3710 if (hstmt != NULL) {
3711 rc = _account_query_finalize(hstmt);
3712 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3716 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
3717 error_code = account_type_query_label_by_app_id_from_global_db(callback, app_id, user_data);
3723 int account_type_query_label_by_app_id(account_type_label_cb callback, const char* app_id, void *user_data)
3725 int error_code = _ACCOUNT_ERROR_NONE;
3727 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3728 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3729 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3731 error_code = _account_type_query_label_by_app_id(callback, app_id, user_data);
3733 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3734 error_code = _account_type_query_label_by_app_id_from_global_db(callback, app_id, user_data);
3739 bool _account_get_label_text_cb(char* app_id, char* label, char* locale, void *user_data)
3741 account_type_s *data = (account_type_s*)user_data;
3743 label_s *label_data = (label_s*)malloc(sizeof(label_s));
3745 if (label_data == NULL) {
3746 ACCOUNT_FATAL("_account_get_label_text_cb : MALLOC FAIL\n");
3749 ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
3751 label_data->app_id = _account_dup_text(app_id);
3752 label_data->label = _account_dup_text(label);
3753 label_data->locale = _account_dup_text(locale);
3755 data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
3760 bool _account_get_provider_feature_cb(char* app_id, char* key, void* user_data)
3762 account_type_s *data = (account_type_s*)user_data;
3764 provider_feature_s *feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
3766 if (feature_data == NULL) {
3767 ACCOUNT_FATAL("_account_get_provider_feature_cb : MALLOC FAIL\n");
3770 ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
3772 feature_data->app_id = _account_dup_text(app_id);
3773 feature_data->key = _account_dup_text(key);
3775 data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
3780 int _account_type_query_by_app_id_from_global_db(const char* app_id, account_type_s** account_type_record)
3782 _INFO("_account_type_query_by_app_id_from_global_db start");
3784 int error_code = _ACCOUNT_ERROR_NONE;
3785 account_stmt hstmt = NULL;
3786 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3787 int rc = 0, binding_count = 1;
3789 ACCOUNT_RETURN_VAL((app_id != 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3790 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3791 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3793 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3795 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
3796 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3798 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3799 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3800 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3803 _account_query_bind_text(hstmt, binding_count++, app_id);
3805 rc = _account_query_step(hstmt);
3806 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3808 *account_type_record = create_empty_account_type_instance();
3810 while (rc == SQLITE_ROW) {
3811 _account_type_convert_column_to_account_type(hstmt, *account_type_record);
3812 rc = _account_query_step(hstmt);
3815 rc = _account_query_finalize(hstmt);
3816 ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {_ERR("global db finalize error rc=[%d]", rc); }, rc, ("finalize error"));
3817 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb, app_id, (void*)(*account_type_record));
3818 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, app_id, (void*)(*account_type_record));
3821 error_code = _ACCOUNT_ERROR_NONE;
3824 if (hstmt != NULL) {
3825 rc = _account_query_finalize(hstmt);
3826 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3830 _INFO("_account_type_query_by_app_id_from_global_db end [%d]", error_code);
3835 int _account_type_query_by_app_id(const char* app_id, account_type_s** account_type_record)
3837 _INFO("_account_type_query_by_app_id start");
3839 int error_code = _ACCOUNT_ERROR_NONE;
3840 account_stmt hstmt = NULL;
3841 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3842 int rc = 0, binding_count = 1;
3844 ACCOUNT_RETURN_VAL((app_id != 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3845 ACCOUNT_RETURN_VAL((account_type_record != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("account type record(account_type_s**) is NULL"));
3846 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3848 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3850 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
3851 hstmt = _account_prepare_query(g_hAccountDB, query);
3853 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
3854 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3855 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3858 _account_query_bind_text(hstmt, binding_count++, app_id);
3860 rc = _account_query_step(hstmt);
3861 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3863 *account_type_record = create_empty_account_type_instance();
3864 if (*account_type_record == NULL) {
3865 _ERR("Out of Memory");
3866 error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
3870 while (rc == SQLITE_ROW) {
3871 _account_type_convert_column_to_account_type(hstmt, *account_type_record);
3872 rc = _account_query_step(hstmt);
3875 rc = _account_query_finalize(hstmt);
3876 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3877 _account_type_query_label_by_app_id(_account_get_label_text_cb, app_id, (void*)(*account_type_record));
3878 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, app_id, (void*)(*account_type_record));
3881 error_code = _ACCOUNT_ERROR_NONE;
3884 if (hstmt != NULL) {
3885 rc = _account_query_finalize(hstmt);
3886 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3890 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
3891 error_code = _account_type_query_by_app_id_from_global_db(app_id, account_type_record);
3893 _INFO("_account_type_query_by_app_id end [%d]", error_code);
3898 int _account_type_query_by_provider_feature_from_global_db(const char* key, GSList **account_type_list_all)
3900 int error_code = _ACCOUNT_ERROR_NONE;
3901 account_stmt hstmt = NULL;
3902 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3904 GSList *account_type_list = NULL;
3906 _INFO("_account_type_query_by_provider_feature_from_global_db start key=%s", key);
3908 ACCOUNT_ERROR("capability_type IS NULL.");
3909 error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
3913 if (g_hAccountGlobalDB == NULL) {
3914 ACCOUNT_ERROR("The database isn't connected.");
3915 error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;
3919 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3921 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
3923 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3925 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
3926 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3927 error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3931 int binding_count = 1;
3932 _account_query_bind_text(hstmt, binding_count++, key);
3934 rc = _account_query_step(hstmt);
3936 account_type_s *account_type_record = NULL;
3938 if (rc != SQLITE_ROW) {
3939 ACCOUNT_ERROR("The record isn't found. rc=[%d]", rc);
3940 error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
3944 while (rc == SQLITE_ROW) {
3945 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
3947 if (account_type_record == NULL) {
3948 ACCOUNT_FATAL("malloc Failed");
3952 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
3953 _account_type_convert_column_to_account_type(hstmt, account_type_record);
3954 account_type_list = g_slist_append(account_type_list, account_type_record);
3955 rc = _account_query_step(hstmt);
3958 rc = _account_query_finalize(hstmt);
3959 if (rc != _ACCOUNT_ERROR_NONE) {
3960 _account_type_gslist_account_type_free(account_type_list);
3961 ACCOUNT_ERROR("finalize error(%s)", rc);
3969 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
3970 account_type_s *account_type = NULL;
3971 account_type = (account_type_s*)iter->data;
3972 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb, account_type->app_id, (void*)account_type);
3973 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, account_type->app_id, (void*)account_type);
3974 _INFO("add label & provider_feature");
3977 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
3979 account_type_s *account_type = NULL;
3980 account_type = (account_type_s*)iter->data;
3981 *account_type_list_all = g_slist_append(*account_type_list_all, account_type);
3982 _INFO("add account_type");
3984 g_slist_free(account_type_list);
3986 error_code = _ACCOUNT_ERROR_NONE;
3989 if (hstmt != NULL) {
3990 rc = _account_query_finalize(hstmt);
3991 if (rc != _ACCOUNT_ERROR_NONE) {
3992 ACCOUNT_ERROR("finalize error(%s)", rc);
3998 _INFO("_account_type_query_by_provider_feature_from_global_db end. error_code=[%d]", error_code);
4003 GSList* _account_type_query_by_provider_feature(const char* key, int *error_code)
4005 *error_code = _ACCOUNT_ERROR_NONE;
4006 account_stmt hstmt = NULL;
4007 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4009 GSList *account_type_list = NULL;
4011 _INFO("account_type_query_by_provider_feature start key=%s", key);
4013 ACCOUNT_ERROR("capability_type IS NULL.");
4014 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
4018 if (g_hAccountDB == NULL) {
4019 ACCOUNT_ERROR("The database isn't connected.");
4020 *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;
4024 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4026 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
4028 hstmt = _account_prepare_query(g_hAccountDB, query);
4030 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4031 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4032 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
4036 int binding_count = 1;
4037 _account_query_bind_text(hstmt, binding_count++, key);
4039 rc = _account_query_step(hstmt);
4041 account_type_s *account_type_record = NULL;
4043 if (rc != SQLITE_ROW) {
4044 ACCOUNT_ERROR("The record isn't found. rc=[%d]", rc);
4045 *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4049 while (rc == SQLITE_ROW) {
4050 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4052 if (account_type_record == NULL) {
4053 ACCOUNT_FATAL("malloc Failed");
4057 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4058 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4059 account_type_list = g_slist_append(account_type_list, account_type_record);
4060 rc = _account_query_step(hstmt);
4063 rc = _account_query_finalize(hstmt);
4064 if (rc != _ACCOUNT_ERROR_NONE) {
4065 _account_type_gslist_account_type_free(account_type_list);
4066 ACCOUNT_ERROR("finalize error(%s)", rc);
4074 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4075 account_type_s *account_type = NULL;
4076 account_type = (account_type_s*)iter->data;
4077 _account_type_query_label_by_app_id(_account_get_label_text_cb, account_type->app_id, (void*)account_type);
4078 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, account_type->app_id, (void*)account_type);
4081 *error_code = _ACCOUNT_ERROR_NONE;
4084 if (hstmt != NULL) {
4085 rc = _account_query_finalize(hstmt);
4086 if (rc != _ACCOUNT_ERROR_NONE) {
4093 if (*error_code == _ACCOUNT_ERROR_NONE || *error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4094 rc = _account_type_query_by_provider_feature_from_global_db(key, &account_type_list);
4095 if (rc != _ACCOUNT_ERROR_NONE && rc != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4096 ACCOUNT_ERROR("_account_type_query_by_provider_feature_from_global_db fail=[%d]", rc);
4097 _account_type_gslist_account_type_free(account_type_list);
4100 if (rc == _ACCOUNT_ERROR_NONE)
4104 _INFO("account_type_query_by_provider_feature end");
4106 return account_type_list;
4109 int _account_type_query_all_from_global_db(GSList **account_type_list_all)
4111 account_stmt hstmt = NULL;
4112 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4113 int rc = _ACCOUNT_ERROR_NONE;
4114 int error_code = _ACCOUNT_ERROR_NONE;
4115 GSList *account_type_list = NULL;
4117 _INFO("_account_type_query_all_in_global_db start");
4118 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4120 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4122 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
4123 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
4125 rc = _account_query_step(hstmt);
4127 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
4128 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
4129 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4132 account_type_s *account_type_record = NULL;
4134 if (rc != SQLITE_ROW) {
4135 _INFO("[_ACCOUNT_ERROR_RECORD_NOT_FOUND]The record isn't found.");
4136 error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4140 while (rc == SQLITE_ROW) {
4141 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4143 if (account_type_record == NULL) {
4144 ACCOUNT_FATAL("malloc Failed");
4148 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4149 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4150 account_type_list = g_slist_append(account_type_list, account_type_record);
4151 rc = _account_query_step(hstmt);
4154 rc = _account_query_finalize(hstmt);
4155 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4160 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4161 account_type_s *account_type = NULL;
4162 account_type = (account_type_s*)iter->data;
4163 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb, account_type->app_id, (void*)account_type);
4164 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, account_type->app_id, (void*)account_type);
4167 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4168 account_type_s *account_type = NULL;
4169 account_type = (account_type_s*)iter->data;
4170 *account_type_list_all = g_slist_append(*account_type_list_all, account_type);
4172 g_slist_free(account_type_list);
4174 error_code = _ACCOUNT_ERROR_NONE;
4177 if (hstmt != NULL) {
4178 rc = _account_query_finalize(hstmt);
4179 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list); }, rc, ("finalize error"));
4183 _INFO("_account_type_query_all_in_global_db end");
4188 GSList* _account_type_query_all(void)
4190 account_stmt hstmt = NULL;
4191 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4193 int error_code = _ACCOUNT_ERROR_NONE;
4194 GSList *account_type_list = NULL;
4196 _INFO("_account_type_query_all start");
4197 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected."));
4199 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4201 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
4202 hstmt = _account_prepare_query(g_hAccountDB, query);
4204 rc = _account_query_step(hstmt);
4206 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4207 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4211 account_type_s *account_type_record = NULL;
4213 if (rc != SQLITE_ROW) {
4214 _INFO("[_ACCOUNT_ERROR_RECORD_NOT_FOUND]The record isn't found.");
4215 error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4219 while (rc == SQLITE_ROW) {
4220 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4222 if (account_type_record == NULL) {
4223 ACCOUNT_FATAL("malloc Failed");
4227 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4228 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4229 account_type_list = g_slist_append(account_type_list, account_type_record);
4230 rc = _account_query_step(hstmt);
4233 rc = _account_query_finalize(hstmt);
4234 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, NULL, ("finalize error"));
4239 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4240 account_type_s *account_type = NULL;
4241 account_type = (account_type_s*)iter->data;
4242 _account_type_query_label_by_app_id(_account_get_label_text_cb, account_type->app_id, (void*)account_type);
4243 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, account_type->app_id, (void*)account_type);
4246 error_code = _ACCOUNT_ERROR_NONE;
4249 if (hstmt != NULL) {
4250 rc = _account_query_finalize(hstmt);
4251 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list); }, NULL, ("finalize error"));
4255 if (error_code == _ACCOUNT_ERROR_NONE || error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4256 error_code = _account_type_query_all_from_global_db(&account_type_list);
4257 if (rc != _ACCOUNT_ERROR_NONE && rc != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4258 ACCOUNT_ERROR("_account_type_query_all_from_global_db fail=[%d]", rc);
4259 _account_type_gslist_account_type_free(account_type_list);
4264 _INFO("_account_type_query_all end");
4266 return account_type_list;
4269 // output parameter label must be free
4270 int _account_type_query_label_by_locale_from_global_db(const char* app_id, const char* locale, char **label)
4272 int error_code = _ACCOUNT_ERROR_NONE;
4273 account_stmt hstmt = NULL;
4274 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4275 int rc = 0, binding_count = 1;
4276 char* converted_locale = NULL;
4278 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
4279 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4280 ACCOUNT_RETURN_VAL((label != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
4281 ACCOUNT_RETURN_VAL((locale != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("locale char is null"));
4282 //Making label newly created
4284 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4286 converted_locale = _account_dup_text(locale);
4287 gchar** tokens = g_strsplit(converted_locale, "-", 2);
4289 if (tokens != NULL) {
4290 if ((char*)(tokens[1]) != NULL) {
4291 char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1]));
4292 if (upper_token != NULL) {
4293 _ACCOUNT_FREE(converted_locale);
4294 converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token);
4296 _ACCOUNT_FREE(upper_token);
4301 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, converted_locale);
4302 _ACCOUNT_FREE(converted_locale);
4304 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
4306 if (_account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM) {
4307 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
4308 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4311 _account_query_bind_text(hstmt, binding_count++, app_id);
4313 rc = _account_query_step(hstmt);
4314 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4316 label_s* label_record = NULL;
4318 while (rc == SQLITE_ROW) {
4319 label_record = (label_s*) malloc(sizeof(label_s));
4321 if (label_record == NULL) {
4322 ACCOUNT_FATAL("malloc Failed");
4326 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
4328 _account_type_convert_column_to_label(hstmt, label_record);
4330 _ACCOUNT_FREE(*label);
4331 //Making label newly created
4332 *label = _account_dup_text(label_record->label);
4334 _account_type_free_label_with_items(label_record);
4336 rc = _account_query_step(hstmt);
4339 rc = _account_query_finalize(hstmt);
4340 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4343 error_code = _ACCOUNT_ERROR_NONE;
4346 if (hstmt != NULL) {
4347 rc = _account_query_finalize(hstmt);
4348 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4352 _INFO("_account_type_query_label_by_locale_from_global_db() end : error_code = %d", error_code);
4357 // output parameter label must be free
4358 int _account_type_query_label_by_locale(const char* app_id, const char* locale, char **label)
4360 int error_code = _ACCOUNT_ERROR_NONE;
4361 account_stmt hstmt = NULL;
4362 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4363 int rc = 0, binding_count = 1;
4364 char* converted_locale = NULL;
4366 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
4367 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4368 ACCOUNT_RETURN_VAL((label != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
4369 ACCOUNT_RETURN_VAL((locale != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("locale char is null"));
4370 //Making label newly created
4372 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4374 converted_locale = _account_dup_text(locale);
4375 gchar** tokens = g_strsplit(converted_locale, "-", 2);
4377 if (tokens != NULL) {
4378 if ((char*)(tokens[1]) != NULL) {
4379 char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1]));
4380 if (upper_token != NULL) {
4381 _ACCOUNT_FREE(converted_locale);
4382 converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token);
4384 _ACCOUNT_FREE(upper_token);
4389 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, converted_locale);
4390 _ACCOUNT_FREE(converted_locale);
4392 hstmt = _account_prepare_query(g_hAccountDB, query);
4394 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4395 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4396 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4399 _account_query_bind_text(hstmt, binding_count++, app_id);
4401 rc = _account_query_step(hstmt);
4402 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4404 label_s* label_record = NULL;
4406 while (rc == SQLITE_ROW) {
4407 label_record = (label_s*) malloc(sizeof(label_s));
4409 if (label_record == NULL) {
4410 ACCOUNT_FATAL("malloc Failed");
4414 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
4416 _account_type_convert_column_to_label(hstmt, label_record);
4418 _ACCOUNT_FREE(*label);
4419 //Making label newly created
4420 *label = _account_dup_text(label_record->label);
4422 _account_type_free_label_with_items(label_record);
4424 rc = _account_query_step(hstmt);
4427 rc = _account_query_finalize(hstmt);
4428 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4431 error_code = _ACCOUNT_ERROR_NONE;
4434 if (hstmt != NULL) {
4435 rc = _account_query_finalize(hstmt);
4436 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4440 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND)
4441 error_code = _account_type_query_label_by_locale_from_global_db(app_id, locale, label);
4443 _INFO("_account_type_query_label_by_locale() end : error_code = %d", error_code);
4448 static int _account_insert_custom(account_s *account, int account_id)
4450 _INFO("_account_insert_custom start");
4453 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4454 account_stmt hstmt = NULL;
4456 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4458 if (g_slist_length(account->custom_list) == 0) {
4459 ACCOUNT_DEBUG("_account_insert_custom, no custom data\n");
4460 return _ACCOUNT_ERROR_NONE;
4463 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
4465 rc = _account_get_record_count(g_hAccountDB, query);
4467 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4468 ACCOUNT_ERROR("Access failed(%d, %s)", _account_db_err_msg(g_hAccountDB));
4469 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4473 ACCOUNT_SLOGE("_account_insert_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
4474 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4481 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
4484 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4485 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (AccountId, AppId, Key, Value) VALUES "
4486 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
4488 hstmt = _account_prepare_query(g_hAccountDB, query);
4490 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4491 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4492 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4495 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
4497 account_custom_s* custom_data = NULL;
4498 custom_data = (account_custom_s*)iter->data;
4500 ret = _account_query_bind_int(hstmt, count++, account_id);
4501 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
4502 ret = _account_query_bind_text(hstmt, count++, account->package_name);
4503 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4504 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
4505 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4506 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
4507 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4509 rc = _account_query_step(hstmt);
4511 if (rc != SQLITE_DONE) {
4512 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4516 rc = _account_query_finalize(hstmt);
4517 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4522 _INFO("_account_insert_custom end");
4524 return _ACCOUNT_ERROR_NONE;
4527 static int _account_update_custom(account_s *account, int account_id)
4530 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4531 account_stmt hstmt = NULL;
4533 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4535 if (g_slist_length(account->custom_list) == 0) {
4536 ACCOUNT_DEBUG("_account_update_custom, no custom data\n");
4537 return _ACCOUNT_ERROR_NONE;
4540 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
4542 rc = _account_get_record_count(g_hAccountDB, query);
4544 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4545 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4546 pthread_mutex_unlock(&account_mutex);
4547 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4548 } else if (_account_db_err_code(g_hAccountDB) == SQLITE_BUSY) {
4549 ACCOUNT_ERROR("database busy(%s)", _account_db_err_msg(g_hAccountDB));
4550 pthread_mutex_unlock(&account_mutex);
4551 return _ACCOUNT_ERROR_DATABASE_BUSY;
4555 ACCOUNT_SLOGE("_account_update_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
4556 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4559 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4561 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId=? ", ACCOUNT_CUSTOM_TABLE);
4562 hstmt = _account_prepare_query(g_hAccountDB, query);
4564 _account_query_bind_int(hstmt, count++, (int)account_id);
4565 rc = _account_query_step(hstmt);
4567 if (rc == SQLITE_BUSY) {
4568 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4569 return _ACCOUNT_ERROR_DATABASE_BUSY;
4570 } else if (rc != SQLITE_DONE) {
4571 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4572 return _ACCOUNT_ERROR_DB_FAILED;
4575 rc = _account_query_finalize(hstmt);
4576 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4581 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
4584 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4585 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AccountId, AppId, Key, Value) VALUES "
4586 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
4588 hstmt = _account_prepare_query(g_hAccountDB, query);
4590 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM) {
4591 ACCOUNT_ERROR("Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4592 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4595 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
4597 account_custom_s* custom_data = NULL;
4598 custom_data = (account_custom_s*)iter->data;
4600 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
4601 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
4602 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
4603 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4604 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
4605 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4606 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
4607 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4609 rc = _account_query_step(hstmt);
4611 if (rc != SQLITE_DONE) {
4612 ACCOUNT_ERROR("_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4616 rc = _account_query_finalize(hstmt);
4617 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4622 return _ACCOUNT_ERROR_NONE;