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);
99 item = _account_dup_text(appid);
100 *appid_list = g_slist_append(*appid_list, item);
105 static inline int __read_proc(const char *path, char *buf, int size)
109 if (buf == NULL || path == NULL) {
110 ACCOUNT_ERROR("path and buffer is mandatory\n");
114 fd = open(path, O_RDONLY);
116 ACCOUNT_ERROR("fd open error(%d)\n", fd);
120 ret = read(fd, buf, size - 1);
122 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);
150 static int _account_execute_query_from_global_db(const char *query)
153 char* pszErrorMsg = NULL;
156 ACCOUNT_ERROR("NULL query\n");
157 return _ACCOUNT_ERROR_QUERY_SYNTAX_ERROR;
160 if(!g_hAccountGlobalDB){
161 ACCOUNT_ERROR("Global DB is not opened\n");
162 return _ACCOUNT_ERROR_DB_NOT_OPENED;
165 rc = sqlite3_exec(g_hAccountGlobalDB, query, NULL, NULL, &pszErrorMsg);
166 if (SQLITE_OK != rc) {
167 ACCOUNT_ERROR("sqlite3_exec rc(%d) query(%s) failed(%s).", rc, query, pszErrorMsg);
168 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 )
411 ACCOUNT_ERROR("db_util_close(g_hAccountDB) fail ret = %d", ret);
412 g_hAccountDB2 = g_hAccountDB;
419 static int _account_execute_insert_query(account_s *account)
421 _INFO("_account_execute_insert_query start");
424 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
425 int error_code = _ACCOUNT_ERROR_NONE;
426 account_stmt hstmt = NULL;
428 /* check whether app id exist in account type db */
430 if (!account->user_name && !account->display_name && !account->email_address) {
432 ACCOUNT_ERROR("Mandetory fields is NULL. At least one field is required among username, display name, email address\n");
433 return _ACCOUNT_ERROR_INVALID_PARAMETER;
437 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
438 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (user_name, email_address , display_name , icon_path , source , package_name , "
439 "access_token , domain_name , auth_type , secret , sync_support , txt_custom0, txt_custom1, txt_custom2, txt_custom3, txt_custom4, "
440 "int_custom0, int_custom1, int_custom2, int_custom3, int_custom4, txt_custom0 ) values " // to do urusa
441 "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", ACCOUNT_TABLE);
443 hstmt = _account_prepare_query(g_hAccountDB, query);
444 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
447 _account_convert_account_to_sql(account, hstmt, query);
450 rc = _account_query_step(hstmt);
451 if (rc != SQLITE_DONE) {
453 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
455 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM )
456 error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
458 error_code = _ACCOUNT_ERROR_DB_FAILED;
462 rc = _account_query_finalize(hstmt);
463 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
466 _INFO("_account_execute_insert_query end");
470 static int _account_insert_capability(account_s *account, int account_id)
472 _INFO("_account_insert_capability start");
474 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
475 account_stmt hstmt = NULL;
477 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
479 if (g_slist_length( account->capablity_list)==0) {
480 ACCOUNT_DEBUG( "_account_insert_capability, no capability\n");
481 return _ACCOUNT_ERROR_NONE;
484 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
486 _INFO("_account_insert_capability _account_get_record_count [%s]", query);
487 rc = _account_get_record_count(g_hAccountDB, query);
489 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
490 _ERR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
491 return _ACCOUNT_ERROR_PERMISSION_DENIED;
494 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
501 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
505 account_capability_s* cap_data = NULL;
506 cap_data = (account_capability_s*)iter->data;
508 _INFO("cap_data->type = %s, cap_data->value = %d \n", cap_data->type, cap_data->value);
510 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
511 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
512 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
513 hstmt = _account_prepare_query(g_hAccountDB, query);
515 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
517 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
518 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
519 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
520 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
521 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
522 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
523 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
524 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
525 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
526 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
528 rc = _account_query_step(hstmt);
529 _INFO("_account_insert_capability _account_query_step[%d]", rc);
531 if (rc != SQLITE_DONE) {
532 _ERR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
536 rc = _account_query_finalize(hstmt);
537 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
542 _INFO("_account_insert_capability end");
543 return _ACCOUNT_ERROR_NONE;
546 static int _account_update_capability(account_s *account, int account_id)
549 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
550 account_stmt hstmt = NULL;
552 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
554 if (g_slist_length( account->capablity_list)==0) {
555 ACCOUNT_ERROR( "_account_update_capability, no capability\n");
556 return _ACCOUNT_ERROR_NONE;
559 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
561 rc = _account_get_record_count(g_hAccountDB, query);
564 ACCOUNT_SLOGI( "_account_update_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
565 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
568 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
570 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id=? ", CAPABILITY_TABLE);
571 hstmt = _account_prepare_query(g_hAccountDB, query);
573 _account_query_bind_int(hstmt, count++, (int)account_id);
574 rc = _account_query_step(hstmt);
576 if (rc != SQLITE_DONE) {
577 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
578 return _ACCOUNT_ERROR_DB_FAILED;
580 rc = _account_query_finalize(hstmt);
581 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
586 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
589 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
590 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
591 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
593 hstmt = _account_prepare_query(g_hAccountDB, query);
595 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
597 account_capability_s* cap_data = NULL;
598 cap_data = (account_capability_s*)iter->data;
600 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
601 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
602 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
603 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
604 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
605 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
606 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
607 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
608 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
609 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
611 rc = _account_query_step(hstmt);
613 if (rc != SQLITE_DONE) {
614 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
618 rc = _account_query_finalize(hstmt);
619 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
624 return _ACCOUNT_ERROR_NONE;
627 static int _account_update_capability_by_user_name(account_s *account, const char *user_name, const char *package_name )
630 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
631 account_stmt hstmt = NULL;
633 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
635 if (g_slist_length( account->capablity_list)==0) {
636 ACCOUNT_ERROR( "_account_update_capability_by_user_name, no capability\n");
637 return _ACCOUNT_ERROR_NONE;
640 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where package_name= '%s' and user_name='%s'", ACCOUNT_TABLE, package_name, user_name);
642 rc = _account_get_record_count(g_hAccountDB, query);
645 ACCOUNT_SLOGI( "_account_update_capability_by_user_name : related account item is not existed rc=%d , %s ", rc, _account_db_err_msg(g_hAccountDB));
646 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
649 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
651 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name=? and user_name=? ", CAPABILITY_TABLE);
652 hstmt = _account_prepare_query(g_hAccountDB, query);
654 _account_query_bind_text(hstmt, count++, (char*)account->package_name);
655 _account_query_bind_text(hstmt, count++, (char*)account->user_name);
656 rc = _account_query_step(hstmt);
657 if (rc != SQLITE_DONE) {
658 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
659 return _ACCOUNT_ERROR_DB_FAILED;
662 rc = _account_query_finalize(hstmt);
663 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
668 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
671 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
672 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
673 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
675 hstmt = _account_prepare_query(g_hAccountDB, query);
677 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
679 account_capability_s* cap_data = NULL;
680 cap_data = (account_capability_s*)iter->data;
682 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
683 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
684 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
685 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
686 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
687 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
688 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
689 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
690 ret = _account_query_bind_int(hstmt, count++, (int)account->id);
691 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
693 rc = _account_query_step(hstmt);
695 if (rc != SQLITE_DONE) {
696 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
700 rc = _account_query_finalize(hstmt);
701 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
706 return _ACCOUNT_ERROR_NONE;
710 bool _account_add_capability_to_account_cb(const char* capability_type, int capability_value, account_s *account)
712 account_capability_s *cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
714 if (cap_data == NULL)
716 ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
718 cap_data->type = _account_dup_text(capability_type);
719 cap_data->value = capability_value;
720 _INFO("cap_data->type = %s, cap_data->value = %d", cap_data->type, cap_data->value);
722 account->capablity_list = g_slist_append(account->capablity_list, (gpointer)cap_data);
728 bool _account_add_custom_to_account_cb(const char* key, const char* value, account_s *account)
730 account_custom_s *custom_data = (account_custom_s*)malloc(sizeof(account_custom_s));
732 if (custom_data == NULL) {
733 ACCOUNT_DEBUG("_account_add_custom_to_account_cb :: malloc fail\n");
736 ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s));
738 custom_data->account_id = account->id;
739 custom_data->app_id = _account_dup_text(account->package_name);
740 custom_data->key = _account_dup_text(key);
741 custom_data->value = _account_dup_text(value);
742 _INFO("custom_data->key = %s, custom_data->value = %s", custom_data->key, custom_data->value);
744 account->custom_list = g_slist_append(account->custom_list, (gpointer)custom_data);
750 static int _account_compare_old_record_by_user_name(account_s *new_account, const char* user_name, const char* package_name)
752 int error_code = _ACCOUNT_ERROR_NONE;
753 account_stmt hstmt = NULL;
754 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
756 account_s *old_account = NULL;
758 ACCOUNT_RETURN_VAL((new_account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
759 ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
760 ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
761 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
763 old_account = (account_s*)calloc(1, sizeof(account_s));
765 ACCOUNT_FATAL("Memory alloc fail\n");
766 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
769 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
771 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = '%s' and package_name='%s'", ACCOUNT_TABLE, user_name, package_name);
772 hstmt = _account_prepare_query(g_hAccountDB, query);
774 rc = _account_query_step(hstmt);
775 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
777 while (rc == SQLITE_ROW) {
778 _account_convert_column_to_account(hstmt, old_account);
779 rc = _account_query_step(hstmt);
782 rc = _account_query_finalize(hstmt);
783 ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
787 error_code = _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, old_account->id, (void*)old_account);
788 ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
791 error_code = _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, old_account->id, (void*)old_account);
792 ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
795 new_account->id = old_account->id;
798 if(!new_account->user_name) {
799 if(old_account->user_name)
800 new_account->user_name = _account_dup_text(old_account->user_name);
804 if(!new_account->display_name) {
805 if(old_account->display_name)
806 new_account->display_name = _account_dup_text(old_account->display_name);
810 if(!new_account->email_address) {
811 if(old_account->email_address)
812 new_account->email_address = _account_dup_text(old_account->email_address);
816 if(!new_account->domain_name) {
817 if(old_account->domain_name)
818 new_account->domain_name = _account_dup_text(old_account->domain_name);
822 if(!new_account->icon_path) {
823 if(old_account->icon_path)
824 new_account->icon_path = _account_dup_text(old_account->icon_path);
828 if(!new_account->source) {
829 if(old_account->source)
830 new_account->source = _account_dup_text(old_account->source);
833 _ACCOUNT_FREE(new_account->package_name);
834 new_account->package_name = _account_dup_text(old_account->package_name);
837 if(!new_account->access_token) {
838 if(old_account->access_token)
839 new_account->access_token = _account_dup_text(old_account->access_token);
843 if(new_account->auth_type == _ACCOUNT_AUTH_TYPE_INVALID) {
844 new_account->auth_type = old_account->auth_type;
848 if(new_account->secret== _ACCOUNT_SECRECY_INVALID) {
849 new_account->secret = old_account->secret;
853 if(new_account->sync_support == _ACCOUNT_SYNC_INVALID) {
854 new_account->sync_support = old_account->sync_support;
859 for(i=0;i<USER_TXT_CNT;i++) {
860 if(!new_account->user_data_txt[i]) {
861 if(old_account->user_data_txt[i])
862 new_account->user_data_txt[i] = _account_dup_text(old_account->user_data_txt[i]);
867 for(i=0;i<USER_INT_CNT;i++) {
868 if(new_account->user_data_int[i] == 0) {
869 new_account->user_data_int[i] = old_account->user_data_int[i];
879 _account_free_account_with_items(old_account);
883 rc = _account_query_finalize(hstmt);
884 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
888 return _ACCOUNT_ERROR_NONE;
893 static int _account_update_account_by_user_name(int pid, uid_t uid, account_s *account, const char *user_name, const char *package_name)
895 int rc = 0, binding_count = 0, count = 0;
896 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
897 int error_code = _ACCOUNT_ERROR_NONE;
898 account_stmt hstmt = NULL;
900 ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is NULL.\n"));
901 ACCOUNT_RETURN_VAL((package_name!= NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is NULL.\n"));
903 char* current_appid = NULL;
904 char* verified_appid = NULL;
906 current_appid = _account_get_current_appid(pid, uid);
907 error_code = _account_get_represented_appid_from_db(g_hAccountDB, g_hAccountGlobalDB, current_appid, uid, &verified_appid);
909 _ACCOUNT_FREE(current_appid);
910 _ACCOUNT_FREE(verified_appid);
912 if(error_code != _ACCOUNT_ERROR_NONE){
913 ACCOUNT_ERROR("No permission to update\n");
914 return _ACCOUNT_ERROR_PERMISSION_DENIED;
917 error_code = encrypt_access_token(account);
918 if (error_code != _ACCOUNT_ERROR_NONE)
920 _ERR("_encrypt_access_token error");
924 _account_compare_old_record_by_user_name(account, user_name, package_name);
926 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
927 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
928 return _ACCOUNT_ERROR_PERMISSION_DENIED;
931 if (!account->package_name) {
932 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
933 return _ACCOUNT_ERROR_INVALID_PARAMETER;
936 if (!account->user_name && !account->display_name && !account->email_address) {
937 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
938 return _ACCOUNT_ERROR_INVALID_PARAMETER;
941 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE user_name='%s' and package_name='%s'"
942 , ACCOUNT_TABLE, user_name, package_name);
944 count = _account_get_record_count(g_hAccountDB, query);
946 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
947 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
948 return _ACCOUNT_ERROR_PERMISSION_DENIED;
952 ACCOUNT_SLOGI("_account_update_account_by_user_name : The account not exist!, count = %d, user_name=%s, package_name=%s\n",
953 count, user_name, package_name);
954 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
957 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
959 //TODO: Is it required to update id ? As of now I can only think of falied rollback cases (between account and gSSO DB)
960 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
961 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
962 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
963 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE user_name=? and package_name=? ", ACCOUNT_TABLE);
965 hstmt = _account_prepare_query(g_hAccountDB, query);
966 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
967 _account_end_transaction(g_hAccountDB, FALSE);
968 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
969 return _ACCOUNT_ERROR_PERMISSION_DENIED;
971 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
973 binding_count = _account_convert_account_to_sql(account, hstmt, query);
975 _account_query_bind_text(hstmt, binding_count++, user_name);
976 _account_query_bind_text(hstmt, binding_count++, package_name);
977 rc = _account_query_step(hstmt);
978 if (rc != SQLITE_DONE) {
979 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
981 rc = _account_query_finalize(hstmt);
982 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
985 /*update capability*/
986 error_code = _account_update_capability_by_user_name(account, user_name, package_name);
989 error_code = _account_update_custom(account, account->id);
994 int _account_insert_to_db(account_s* account, int pid, uid_t uid, int *account_id)
997 int error_code = _ACCOUNT_ERROR_NONE;
998 int ret_transaction = 0;
1000 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1001 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1002 ACCOUNT_RETURN_VAL((account_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID POINTER IS NULL"));
1004 if (!account->user_name && !account->display_name && !account->email_address) {
1005 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1006 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1009 account_s *data = (account_s*)account;
1010 ACCOUNT_SLOGD("(%s)-(%d) account_insert_to_db: begin_transaction.\n", __FUNCTION__, __LINE__);
1012 pthread_mutex_lock(&account_mutex);
1014 /* transaction control required*/
1015 ret_transaction = _account_begin_transaction(g_hAccountDB);
1017 if(_account_db_err_code(g_hAccountDB) == SQLITE_PERM){
1018 pthread_mutex_unlock(&account_mutex);
1019 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1020 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1023 if (ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY) {
1024 ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction);
1025 pthread_mutex_unlock(&account_mutex);
1026 return _ACCOUNT_ERROR_DATABASE_BUSY;
1027 }else if (ret_transaction != _ACCOUNT_ERROR_NONE) {
1028 ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction);
1029 pthread_mutex_unlock(&account_mutex);
1030 return ret_transaction;
1033 *account_id = _account_get_next_sequence(g_hAccountDB, ACCOUNT_TABLE);
1034 data->id = *account_id;
1037 appid = _account_get_current_appid(pid, uid);
1042 // API caller cannot be recognized
1043 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1044 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1045 pthread_mutex_unlock(&account_mutex);
1046 return _ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
1050 char* verified_appid = NULL;
1051 error_code = _account_get_represented_appid_from_db(g_hAccountDB, g_hAccountGlobalDB, appid, uid, &verified_appid);//FIX
1052 _ACCOUNT_FREE(appid);
1053 if(error_code != _ACCOUNT_ERROR_NONE)
1055 _ERR("error_code = %d", error_code);
1056 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1057 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1058 _ACCOUNT_FREE(verified_appid);
1059 pthread_mutex_unlock(&account_mutex);
1066 error_code = _account_check_duplicated(g_hAccountDB, data, verified_appid, uid);
1067 if (error_code != _ACCOUNT_ERROR_NONE) {
1069 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1070 ACCOUNT_DEBUG("_account_check_duplicated(), rollback insert query(%x)!!!!\n", ret_transaction);
1072 pthread_mutex_unlock(&account_mutex);
1075 if(!_account_check_add_more_account(verified_appid)) {
1076 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1077 ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
1078 pthread_mutex_unlock(&account_mutex);
1079 _ACCOUNT_FREE(verified_appid);
1080 return _ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
1083 _ACCOUNT_FREE(data->package_name);
1084 data->package_name = _account_dup_text(verified_appid);
1085 _ACCOUNT_FREE(verified_appid);
1088 if(!_account_check_add_more_account(data->package_name))
1091 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1092 ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
1093 pthread_mutex_unlock(&account_mutex);
1094 return _ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
1097 error_code = encrypt_access_token(data);
1098 if (error_code != _ACCOUNT_ERROR_NONE)
1100 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1101 ACCOUNT_ERROR("encrypt_access_token fail, rollback insert query(%x)!!!!\n", ret_transaction);
1103 pthread_mutex_unlock(&account_mutex);
1107 error_code = _account_execute_insert_query(data);
1109 if (error_code != _ACCOUNT_ERROR_NONE)
1112 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1113 ACCOUNT_ERROR("INSERT account fail, rollback insert query(%x)!!!!\n", ret_transaction);
1115 pthread_mutex_unlock(&account_mutex);
1120 error_code = _account_insert_capability(data, *account_id);
1121 if (error_code != _ACCOUNT_ERROR_NONE)
1124 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1125 ACCOUNT_ERROR("INSERT capability fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1127 pthread_mutex_unlock(&account_mutex);
1132 error_code = _account_insert_custom(data, *account_id);
1133 if (error_code != _ACCOUNT_ERROR_NONE)
1135 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1136 ACCOUNT_ERROR("INSERT custom fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1138 pthread_mutex_unlock(&account_mutex);
1144 pthread_mutex_unlock(&account_mutex);
1145 _account_end_transaction(g_hAccountDB, TRUE);
1146 ACCOUNT_SLOGD("(%s)-(%d) account _end_transaction.\n", __FUNCTION__, __LINE__);
1149 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_INSERT, *account_id);
1150 _account_insert_delete_update_notification_send(buf);
1151 _INFO("account _notification_send end.");
1153 return _ACCOUNT_ERROR_NONE;
1157 GSList* _account_get_capability_list_by_account_id(int account_id, int *error_code)
1159 *error_code = _ACCOUNT_ERROR_NONE;
1160 account_stmt hstmt = NULL;
1161 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1163 GSList* capability_list = NULL;
1165 ACCOUNT_RETURN_VAL((account_id > 0), {*error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("ACCOUNT INDEX IS LESS THAN 0"));
1166 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
1168 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1170 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
1171 hstmt = _account_prepare_query(g_hAccountDB, query);
1173 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
1174 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1175 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
1179 rc = _account_query_step(hstmt);
1180 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1182 account_capability_s* capability_record = NULL;
1184 while (rc == SQLITE_ROW) {
1185 capability_record = (account_capability_s*) malloc(sizeof(account_capability_s));
1187 if (capability_record == NULL) {
1188 ACCOUNT_FATAL("malloc Failed");
1192 ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s));
1194 _account_convert_column_to_capability(hstmt, capability_record);
1196 //cb_ret = callback(capability_record->type, capability_record->value, user_data);
1198 //_account_free_capability_items(capability_record);
1199 //_ACCOUNT_FREE(capability_record);
1201 //ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
1203 capability_list = g_slist_append(capability_list, capability_record);
1204 rc = _account_query_step(hstmt);
1207 rc = _account_query_finalize(hstmt);
1208 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
1211 *error_code = _ACCOUNT_ERROR_NONE;
1216 rc = _account_query_finalize(hstmt);
1217 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
1221 pthread_mutex_unlock(&account_mutex);
1222 return capability_list;
1225 static int _account_compare_old_record(account_s *new_account, int account_id)
1227 int error_code = _ACCOUNT_ERROR_NONE;
1228 account_stmt hstmt = NULL;
1229 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1231 account_s *old_account = NULL;
1233 ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1234 ACCOUNT_RETURN_VAL((new_account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
1235 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1237 old_account = (account_s*)calloc(1, sizeof(account_s));
1238 if (old_account == NULL) {
1239 _ERR("Out of Memory");
1240 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
1243 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1245 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
1246 hstmt = _account_prepare_query(g_hAccountDB, query);
1248 rc = _account_query_step(hstmt);
1249 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1251 while (rc == SQLITE_ROW) {
1252 _account_convert_column_to_account(hstmt, old_account);
1253 rc = _account_query_step(hstmt);
1256 rc = _account_query_finalize(hstmt);
1257 ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1261 error_code = _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, old_account->id, (void*)old_account);
1262 ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
1265 error_code = _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, old_account->id, (void*)old_account);
1266 ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
1270 new_account->id = old_account->id;
1273 if(!new_account->user_name) {
1274 if(old_account->user_name)
1275 new_account->user_name = _account_dup_text(old_account->user_name);
1279 if(!new_account->display_name) {
1280 if(old_account->display_name)
1281 new_account->display_name = _account_dup_text(old_account->display_name);
1285 if(!new_account->email_address) {
1286 if(old_account->email_address)
1287 new_account->email_address = _account_dup_text(old_account->email_address);
1291 if(!new_account->domain_name) {
1292 if(old_account->domain_name)
1293 new_account->domain_name = _account_dup_text(old_account->domain_name);
1297 if(!new_account->icon_path) {
1298 if(old_account->icon_path)
1299 new_account->icon_path = _account_dup_text(old_account->icon_path);
1303 if(!new_account->source) {
1304 if(old_account->source)
1305 new_account->source = _account_dup_text(old_account->source);
1308 _ACCOUNT_FREE(new_account->package_name);
1309 new_account->package_name = _account_dup_text(old_account->package_name);
1312 if(!new_account->access_token) {
1313 if(old_account->access_token)
1314 new_account->access_token = _account_dup_text(old_account->access_token);
1319 for(i=0;i<USER_TXT_CNT;i++) {
1320 if(!new_account->user_data_txt[i]) {
1321 if(old_account->user_data_txt[i])
1322 new_account->user_data_txt[i] = _account_dup_text(old_account->user_data_txt[i]);
1327 if(new_account->auth_type == _ACCOUNT_AUTH_TYPE_INVALID) {
1328 new_account->auth_type = old_account->auth_type;
1332 if(new_account->secret== _ACCOUNT_SECRECY_INVALID) {
1333 new_account->secret = old_account->secret;
1337 if(new_account->sync_support == _ACCOUNT_SYNC_INVALID) {
1338 new_account->sync_support = old_account->sync_support;
1342 for(i=0;i<USER_INT_CNT;i++) {
1343 if(new_account->user_data_int[i] == 0) {
1344 new_account->user_data_int[i] = old_account->user_data_int[i];
1350 // user custom table
1354 _account_free_account_with_items(old_account);
1356 if (hstmt != NULL) {
1357 rc = _account_query_finalize(hstmt);
1358 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1362 return _ACCOUNT_ERROR_NONE;
1365 static int _account_get_package_name_from_account_id(int account_id, char **package_name)
1367 int error_code = _ACCOUNT_ERROR_NONE;
1368 account_stmt hstmt = NULL;
1369 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1371 account_s *old_account = NULL;
1373 ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1374 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1376 old_account = (account_s*)calloc(1, sizeof(account_s));
1377 if (old_account == NULL) {
1378 _ERR("Out Of memory");
1379 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
1382 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1384 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
1385 hstmt = _account_prepare_query(g_hAccountDB, query);
1387 rc = _account_query_step(hstmt);
1388 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1390 while (rc == SQLITE_ROW) {
1391 _account_convert_column_to_account(hstmt, old_account);
1392 rc = _account_query_step(hstmt);
1395 rc = _account_query_finalize(hstmt);
1396 ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1399 // get package name.
1400 *package_name = _account_dup_text(old_account->package_name);
1405 _account_free_account_with_items(old_account);
1408 if (hstmt != NULL) {
1409 rc = _account_query_finalize(hstmt);
1410 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1418 static int _account_update_account(int pid, uid_t uid, account_s *account, int account_id)
1420 int rc = 0, binding_count =0;
1421 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1422 int error_code = _ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0;
1423 account_stmt hstmt = NULL;
1425 if (!account->package_name) {
1426 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
1427 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1430 /* Check permission of requested appid */
1431 char* current_appid = NULL;
1432 char *package_name = NULL;
1434 current_appid = _account_get_current_appid(pid, uid);
1435 error_code = _account_get_package_name_from_account_id(account_id, &package_name);
1437 if(error_code != _ACCOUNT_ERROR_NONE || package_name == NULL){
1438 ACCOUNT_ERROR("No package name with account_id\n");
1439 _ACCOUNT_FREE(current_appid);
1440 _ACCOUNT_FREE(package_name);
1441 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1444 error_code = _account_check_appid_group_with_package_name(current_appid, package_name, uid);
1445 ACCOUNT_DEBUG( "UPDATE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name); // TODO: remove the log later.
1447 _ACCOUNT_FREE(current_appid);
1448 _ACCOUNT_FREE(package_name);
1450 if(error_code != _ACCOUNT_ERROR_NONE){
1451 ACCOUNT_ERROR("No permission to update\n");
1452 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1455 error_code = encrypt_access_token(account);
1456 if (error_code != _ACCOUNT_ERROR_NONE)
1458 _ERR("_encrypt_access_token error");
1462 error_code = _account_compare_old_record(account, account_id);
1463 if (error_code != _ACCOUNT_ERROR_NONE) {
1464 ACCOUNT_ERROR("_account_compare_old_record fail\n");
1468 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
1469 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1470 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1471 } else if( _account_db_err_code(g_hAccountDB) == SQLITE_BUSY ){
1472 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg(g_hAccountDB));
1473 return _ACCOUNT_ERROR_DATABASE_BUSY;
1476 if (!account->user_name && !account->display_name && !account->email_address) {
1477 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1478 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1481 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1483 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
1485 count = _account_get_record_count(g_hAccountDB, query);
1487 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
1488 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1491 /* transaction control required*/
1492 ret_transaction = _account_begin_transaction(g_hAccountDB);
1493 if( ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY ){
1494 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg(g_hAccountDB));
1495 pthread_mutex_unlock(&account_mutex);
1496 return _ACCOUNT_ERROR_DATABASE_BUSY;
1499 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1500 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
1501 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
1502 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
1503 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
1505 hstmt = _account_prepare_query(g_hAccountDB, query);
1507 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
1508 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1509 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1510 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1513 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)));
1515 binding_count = _account_convert_account_to_sql(account, hstmt, query);
1516 _account_query_bind_int(hstmt, binding_count++, account_id);
1518 rc = _account_query_step(hstmt);
1519 if (rc != SQLITE_DONE) {
1520 ACCOUNT_SLOGE( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
1523 rc = _account_query_finalize(hstmt);
1524 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1527 _INFO("update query=%s", query);
1529 /*update capability*/
1530 error_code = _account_update_capability(account, account_id);
1531 if(error_code != _ACCOUNT_ERROR_NONE && error_code!= _ACCOUNT_ERROR_RECORD_NOT_FOUND){
1532 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1533 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1538 error_code = _account_update_custom(account, account_id);
1539 if(error_code != _ACCOUNT_ERROR_NONE && error_code!= _ACCOUNT_ERROR_RECORD_NOT_FOUND){
1540 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1541 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1545 ret_transaction = _account_end_transaction(g_hAccountDB, TRUE);
1547 _INFO("update end");
1552 static int _account_update_account_ex(account_s *account, int account_id)
1554 int rc = 0, binding_count =0;
1555 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1556 int error_code = _ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0;
1557 account_stmt hstmt = NULL;
1559 if (!account->package_name) {
1560 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
1561 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1564 error_code = encrypt_access_token(account);
1565 if (error_code != _ACCOUNT_ERROR_NONE)
1567 _ERR("_encrypt_access_token error");
1571 error_code = _account_compare_old_record(account, account_id);
1572 if (error_code != _ACCOUNT_ERROR_NONE) {
1573 ACCOUNT_ERROR("_account_compare_old_record fail\n");
1577 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
1578 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1579 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1582 if (!account->user_name && !account->display_name && !account->email_address) {
1583 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1584 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1587 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1589 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
1591 count = _account_get_record_count(g_hAccountDB, query);
1593 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
1594 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1597 /* transaction control required*/
1598 ret_transaction = _account_begin_transaction(g_hAccountDB);
1599 if( ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY ){
1600 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg(g_hAccountDB));
1601 pthread_mutex_unlock(&account_mutex);
1602 return _ACCOUNT_ERROR_DATABASE_BUSY;
1605 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1606 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
1607 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
1608 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
1609 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
1611 hstmt = _account_prepare_query(g_hAccountDB, query);
1613 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
1614 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1615 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1616 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1619 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)));
1621 _INFO("account_update_to_db_by_id_ex_p : before convert() : account_id[%d], user_name=%s", account->id, account->user_name);
1622 binding_count = _account_convert_account_to_sql(account, hstmt, query);
1623 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], user_name=%s", account->id, account->user_name);
1624 _INFO("account_update_to_db_by_id_ex_p : before bind()");
1625 rc = _account_query_bind_int(hstmt, binding_count++, account_id);
1626 _INFO("account_update_to_db_by_id_ex_p : after bind() : ret = %d", rc);
1628 rc = _account_query_step(hstmt);
1629 if (rc != SQLITE_DONE) {
1630 ACCOUNT_SLOGE( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
1632 _INFO("account_update_to_db_by_id_ex_p : after query_step() : ret = %d", rc);
1634 rc = _account_query_finalize(hstmt);
1635 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1637 _INFO("account_update_to_db_by_id_ex_p : after query_filnalize() : ret = %d", rc);
1639 _INFO("account_update_to_db_by_id_ex_p : before update_capability()");
1640 /*update capability*/
1641 error_code = _account_update_capability(account, account_id);
1642 if(error_code != _ACCOUNT_ERROR_NONE && error_code!= _ACCOUNT_ERROR_RECORD_NOT_FOUND){
1643 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1644 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1647 _INFO("account_update_to_db_by_id_ex_p : after update_capability()");
1649 _INFO("account_update_to_db_by_id_ex_p : before update_custom()");
1651 error_code = _account_update_custom(account, account_id);
1652 if(error_code != _ACCOUNT_ERROR_NONE && error_code!= _ACCOUNT_ERROR_RECORD_NOT_FOUND){
1653 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1654 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1657 _INFO("account_update_to_db_by_id_ex_p : after update_custom()");
1659 ret_transaction = _account_end_transaction(g_hAccountDB, TRUE);
1665 int _account_update_to_db_by_id(int pid, uid_t uid, account_s* account, int account_id)
1667 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
1668 ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
1669 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1670 int error_code = _ACCOUNT_ERROR_NONE;
1671 account_s* data = (account_s*)account;
1673 pthread_mutex_lock(&account_mutex);
1675 error_code = _account_update_account(pid, uid, data, account_id);
1677 if(error_code != _ACCOUNT_ERROR_NONE) {
1678 pthread_mutex_unlock(&account_mutex);
1682 pthread_mutex_unlock(&account_mutex);
1685 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_UPDATE, account_id);
1686 _account_insert_delete_update_notification_send(buf);
1688 return _ACCOUNT_ERROR_NONE;
1691 int _account_update_to_db_by_id_ex(account_s* account, int account_id)
1693 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
1694 ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
1695 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1696 int error_code = _ACCOUNT_ERROR_NONE;
1697 account_s* data = account;
1699 pthread_mutex_lock(&account_mutex);
1701 _INFO("before update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name);
1702 error_code = _account_update_account_ex(data, account_id);
1703 _INFO("after update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name);
1705 if(error_code != _ACCOUNT_ERROR_NONE) {
1706 pthread_mutex_unlock(&account_mutex);
1710 pthread_mutex_unlock(&account_mutex);
1713 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_UPDATE, account_id);
1714 _account_insert_delete_update_notification_send(buf);
1716 return _ACCOUNT_ERROR_NONE;
1720 int _account_update_to_db_by_user_name(int pid, uid_t uid, account_s* account, const char *user_name, const char *package_name)
1722 ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
1723 ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
1724 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1726 int error_code = _ACCOUNT_ERROR_NONE;
1727 account_s *data = (account_s*)account;
1729 pthread_mutex_lock(&account_mutex);
1731 error_code = _account_update_account_by_user_name(pid, uid, data, user_name, package_name);
1733 pthread_mutex_unlock(&account_mutex);
1736 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_UPDATE, data->id);
1737 _account_insert_delete_update_notification_send(buf);
1742 GSList* _account_db_query_all(int pid, uid_t uid)
1744 //int error_code = _ACCOUNT_ERROR_NONE;
1745 account_stmt hstmt = NULL;
1746 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1748 GSList *account_list = NULL;
1750 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected."));
1752 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1754 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TABLE);
1755 hstmt = _account_prepare_query(g_hAccountDB, query);
1757 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
1758 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1762 rc = _account_query_step(hstmt);
1764 account_s *account_record = NULL;
1766 if (rc != SQLITE_ROW)
1768 _ERR("The record isn't found");
1772 while(rc == SQLITE_ROW) {
1773 account_record = (account_s*) malloc(sizeof(account_s));
1775 if (account_record == NULL) {
1776 ACCOUNT_FATAL("malloc Failed");
1780 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
1781 _account_convert_column_to_account(hstmt, account_record);
1782 account_list = g_slist_append(account_list, account_record);
1783 rc = _account_query_step(hstmt);
1786 rc = _account_query_finalize(hstmt);
1787 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, NULL, ("finalize error"));
1792 for (iter = account_list; iter != NULL; iter = g_slist_next(iter)) {
1793 account_s *account = NULL;
1794 account = (account_s*)iter->data;
1795 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, account->id, (void*)account);
1796 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, account->id, (void*)account);
1800 if (hstmt != NULL) {
1801 rc = _account_query_finalize(hstmt);
1802 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_gslist_account_free(account_list);}, NULL, ("finalize error"));
1807 _remove_sensitive_info_from_non_owning_account_slist(account_list, pid, uid);
1809 return account_list;
1812 int _account_update_sync_status_by_id(uid_t uid, int account_db_id, const int sync_status)
1814 int error_code = _ACCOUNT_ERROR_NONE;
1815 account_stmt hstmt = NULL;
1816 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1820 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1821 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1822 if ( (sync_status < 0) || (sync_status >= _ACCOUNT_SYNC_MAX)) {
1823 ACCOUNT_SLOGE("(%s)-(%d) sync_status is less than 0 or more than enum max.\n", __FUNCTION__, __LINE__);
1824 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1827 pthread_mutex_lock(&account_mutex);
1829 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1831 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_db_id);
1833 rc = _account_get_record_count(g_hAccountDB, query);
1835 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
1836 pthread_mutex_unlock(&account_mutex);
1837 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1838 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1842 ACCOUNT_SLOGE( "account_update_sync_status_by_id : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
1843 pthread_mutex_unlock(&account_mutex);
1844 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1847 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1849 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET sync_support=? WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
1850 hstmt = _account_prepare_query(g_hAccountDB, query);
1852 _account_query_bind_int(hstmt, count, sync_status);
1854 rc = _account_query_step(hstmt);
1856 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
1857 pthread_mutex_unlock(&account_mutex);
1858 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1859 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1862 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_DB_FAILED,
1863 ("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB)));
1865 rc = _account_query_finalize(hstmt);
1866 if (rc != _ACCOUNT_ERROR_NONE) {
1867 ACCOUNT_ERROR("_account_query_finalize error");
1868 pthread_mutex_unlock(&account_mutex);
1872 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_SYNC_UPDATE, account_db_id);
1873 _account_insert_delete_update_notification_send(buf);
1876 error_code = _ACCOUNT_ERROR_NONE;
1879 if (hstmt != NULL) {
1880 rc = _account_query_finalize(hstmt);
1881 pthread_mutex_unlock(&account_mutex);
1882 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1886 pthread_mutex_unlock(&account_mutex);
1890 int _account_query_account_by_account_id(int pid, uid_t uid, int account_db_id, account_s *account_record)
1892 _INFO("_account_query_account_by_account_id() start, account_db_id=[%d]", account_db_id);
1894 int error_code = _ACCOUNT_ERROR_NONE;
1895 account_stmt hstmt = NULL;
1896 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1899 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1900 ACCOUNT_RETURN_VAL(account_record != NULL, {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
1901 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1903 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1905 ACCOUNT_DEBUG("starting db operations");
1907 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
1908 hstmt = _account_prepare_query(g_hAccountDB, query);
1909 rc = _account_db_err_code(g_hAccountDB);
1910 _INFO("after _account_prepare_query, rc=[%d]", rc);
1912 if( rc == SQLITE_PERM ){
1913 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1914 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1917 ACCOUNT_DEBUG("before _account_query_step");
1918 rc = _account_query_step(hstmt);
1919 ACCOUNT_DEBUG("after _account_query_step returned [%d]", rc);
1920 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1922 while (rc == SQLITE_ROW) {
1923 ACCOUNT_DEBUG("before _account_convert_column_to_account");
1924 _account_convert_column_to_account(hstmt, account_record);
1925 ACCOUNT_DEBUG("after _account_convert_column_to_account");
1926 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]);
1927 rc = _account_query_step(hstmt);
1930 ACCOUNT_DEBUG("account_record->id=[%d]", account_record->id);
1932 rc = _account_query_finalize(hstmt);
1933 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1935 ACCOUNT_DEBUG("before _account_query_capability_by_account_id");
1936 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, account_record->id, (void*)account_record);
1937 ACCOUNT_DEBUG("after _account_query_capability_by_account_id");
1939 ACCOUNT_DEBUG("before _account_query_custom_by_account_id");
1940 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, account_record->id, (void*)account_record);
1941 ACCOUNT_DEBUG("after _account_query_custom_by_account_id");
1944 error_code = _ACCOUNT_ERROR_NONE;
1947 if (hstmt != NULL) {
1948 rc = _account_query_finalize(hstmt);
1949 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1955 _remove_sensitive_info_from_non_owning_account(account_record, pid, uid);
1957 pthread_mutex_unlock(&account_mutex);
1958 ACCOUNT_DEBUG("_account_query_account_by_account_id end [%d]", error_code);
1962 GList* _account_query_account_by_user_name(int pid, uid_t uid, const char *user_name, int *error_code)
1964 *error_code = _ACCOUNT_ERROR_NONE;
1965 account_stmt hstmt = NULL;
1966 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1968 account_s *account_head = NULL;
1970 if (user_name == NULL)
1972 _ERR("USER NAME IS NULL");
1973 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
1977 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1979 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ?", ACCOUNT_TABLE);
1981 hstmt = _account_prepare_query(g_hAccountDB, query);
1983 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM)
1985 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1986 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
1990 int binding_count = 1;
1991 _account_query_bind_text(hstmt, binding_count++, user_name);
1993 rc = _account_query_step(hstmt);
1995 if (rc != SQLITE_ROW)
1997 _ERR("The record isn't found");
1998 *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
2004 account_head = (account_s*) malloc(sizeof(account_s));
2005 if (account_head == NULL) {
2006 ACCOUNT_FATAL("malloc Failed");
2007 if (hstmt != NULL) {
2008 rc = _account_query_finalize(hstmt);
2010 if (rc != _ACCOUNT_ERROR_NONE)
2012 _ERR("finalize error");
2018 *error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
2021 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2023 while (rc == SQLITE_ROW) {
2024 account_s* account_record = NULL;
2026 account_record = (account_s*) malloc(sizeof(account_s));
2028 if (account_record == NULL) {
2029 ACCOUNT_FATAL("malloc Failed");
2032 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2034 _account_convert_column_to_account(hstmt, account_record);
2036 account_head->account_list = g_list_append(account_head->account_list, account_record);
2038 rc = _account_query_step(hstmt);
2042 rc = _account_query_finalize(hstmt);
2044 if (rc != _ACCOUNT_ERROR_NONE)
2046 _ERR("finalize error");
2056 tmp = g_list_length(account_head->account_list);
2058 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2059 // account_h account;
2060 // account = (account_h)iter->data;
2062 account_s *testaccount = (account_s*)iter->data;
2064 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, testaccount->id, (void*)testaccount);
2065 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, testaccount->id, (void*)testaccount);
2069 *error_code = _ACCOUNT_ERROR_NONE;
2072 if (hstmt != NULL) {
2073 rc = _account_query_finalize(hstmt);
2074 if (rc != _ACCOUNT_ERROR_NONE)
2076 _ERR("finalize error");
2082 pthread_mutex_unlock(&account_mutex);
2085 _remove_sensitive_info_from_non_owning_account_list(account_head->account_list, pid, uid);
2086 GList* result = account_head->account_list;
2087 _ACCOUNT_FREE(account_head);
2094 _account_query_account_by_capability(int pid, uid_t uid, const char* capability_type, const int capability_value, int *error_code)
2096 *error_code = _ACCOUNT_ERROR_NONE;
2097 account_stmt hstmt = NULL;
2098 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2101 ACCOUNT_RETURN_VAL((capability_type != NULL), {*error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("capability_type IS NULL"));
2103 if ((capability_value < 0) || (capability_value >= _ACCOUNT_CAPABILITY_STATE_MAX)) {
2104 ACCOUNT_SLOGE("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
2105 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
2109 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
2111 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2113 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=? AND value=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
2115 hstmt = _account_prepare_query(g_hAccountDB, query);
2117 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
2118 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2119 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
2123 int binding_count = 1;
2124 _account_query_bind_text(hstmt, binding_count++, capability_type);
2125 _account_query_bind_int(hstmt, binding_count++, capability_value);
2127 rc = _account_query_step(hstmt);
2129 account_s* account_head = NULL;
2131 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2135 account_head = (account_s*) malloc(sizeof(account_s));
2136 if (account_head == NULL) {
2137 ACCOUNT_FATAL("malloc Failed");
2138 if (hstmt != NULL) {
2139 rc = _account_query_finalize(hstmt);
2140 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
2143 *error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
2146 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2148 while (rc == SQLITE_ROW) {
2149 account_s* account_record = NULL;
2151 account_record = (account_s*) malloc(sizeof(account_s));
2153 if (account_record == NULL) {
2154 ACCOUNT_FATAL("malloc Failed");
2157 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2159 _account_convert_column_to_account(hstmt, account_record);
2161 account_head->account_list = g_list_append(account_head->account_list, account_record);
2163 rc = _account_query_step(hstmt);
2167 rc = _account_query_finalize(hstmt);
2168 ACCOUNT_CATCH_ERROR_P((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2174 tmp = g_list_length(account_head->account_list);
2176 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2177 // account_h account = NULL;
2178 // account = (account_h)iter->data;
2179 account_s* testaccount = (account_s*)iter->data;
2181 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, testaccount->id, (void*)testaccount);
2182 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, testaccount->id, (void*)testaccount);
2187 *error_code = _ACCOUNT_ERROR_NONE;
2190 if (hstmt != NULL) {
2191 rc = _account_query_finalize(hstmt);
2192 if ( rc != _ACCOUNT_ERROR_NONE ) {
2194 _ERR("finalize error");
2199 if( *error_code != _ACCOUNT_ERROR_NONE && account_head ) {
2200 _account_glist_account_free(account_head->account_list);
2201 _ACCOUNT_FREE(account_head);
2202 account_head = NULL;
2205 pthread_mutex_unlock(&account_mutex);
2209 _remove_sensitive_info_from_non_owning_account_list(account_head->account_list, pid, uid);
2210 GList* result = account_head->account_list;
2211 _ACCOUNT_FREE(account_head);
2217 GList* _account_query_account_by_capability_type(int pid, uid_t uid, const char* capability_type, int *error_code)
2219 *error_code = _ACCOUNT_ERROR_NONE;
2220 account_stmt hstmt = NULL;
2221 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2224 ACCOUNT_RETURN_VAL((capability_type != NULL), {*error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("capability_type IS NULL"));
2225 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;},
2226 NULL, ("The database isn't connected."));
2228 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2230 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
2232 hstmt = _account_prepare_query(g_hAccountDB, query);
2234 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
2235 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2236 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
2240 int binding_count = 1;
2241 _account_query_bind_text(hstmt, binding_count++, capability_type);
2243 rc = _account_query_step(hstmt);
2245 account_s* account_head = NULL;
2247 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2251 account_head = (account_s*) malloc(sizeof(account_s));
2252 if (account_head == NULL) {
2253 ACCOUNT_FATAL("malloc Failed");
2254 if (hstmt != NULL) {
2255 rc = _account_query_finalize(hstmt);
2256 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
2259 *error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
2262 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2264 while (rc == SQLITE_ROW) {
2265 account_s* account_record = NULL;
2267 account_record = (account_s*) malloc(sizeof(account_s));
2269 if (account_record == NULL) {
2270 ACCOUNT_FATAL("malloc Failed");
2273 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2275 _account_convert_column_to_account(hstmt, account_record);
2277 account_head->account_list = g_list_append(account_head->account_list, account_record);
2279 rc = _account_query_step(hstmt);
2283 rc = _account_query_finalize(hstmt);
2284 ACCOUNT_CATCH_ERROR_P((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2290 tmp = g_list_length(account_head->account_list);
2292 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2293 account_s* testaccount = (account_s*)iter->data;
2295 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, testaccount->id, (void*)testaccount);
2296 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, testaccount->id, (void*)testaccount);
2300 *error_code = _ACCOUNT_ERROR_NONE;
2305 rc = _account_query_finalize(hstmt);
2306 if (rc != _ACCOUNT_ERROR_NONE) {
2308 _ERR("finalize error");
2313 if( (*error_code != _ACCOUNT_ERROR_NONE) && account_head ) {
2314 _account_glist_account_free(account_head->account_list);
2315 _ACCOUNT_FREE(account_head);
2316 account_head = NULL;
2319 pthread_mutex_unlock(&account_mutex);
2323 _remove_sensitive_info_from_non_owning_account_list(account_head->account_list, pid, uid);
2324 GList* result = account_head->account_list;
2325 _ACCOUNT_FREE(account_head);
2331 GList* account_server_query_account_by_package_name(const char* package_name, int *error_code, int pid, uid_t uid)
2333 _INFO("account_server_query_account_by_package_name start");
2335 GList * account_list = NULL;
2336 *error_code = _ACCOUNT_ERROR_NONE;
2338 ACCOUNT_RETURN_VAL((package_name != NULL), {*error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("PACKAGE NAME IS NULL"));
2339 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
2341 account_list = _account_query_account_by_package_name(g_hAccountDB, package_name, error_code, pid, uid);
2343 _INFO("account_server_query_account_by_package_name end");
2344 return account_list;
2347 int account_server_delete_account_by_package_name(const char* package_name, bool permission, int pid, uid_t uid)
2349 _INFO("account_db_delete_account_by_package_name");
2351 int error_code = _ACCOUNT_ERROR_NONE;
2353 ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
2354 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2356 error_code = _account_delete_account_by_package_name(g_hAccountDB, package_name, permission, pid, uid);
2358 _INFO("account_server_delete_account_by_package_name end");
2362 int _account_delete(int pid, uid_t uid, int account_id)
2364 int error_code = _ACCOUNT_ERROR_NONE;
2365 account_stmt hstmt = NULL;
2366 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2368 int ret_transaction = 0;
2369 bool is_success = FALSE;
2371 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2374 /* Check requested ID to delete */
2375 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id=%d", ACCOUNT_TABLE, account_id);
2377 count = _account_get_record_count(g_hAccountDB, query);
2379 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
2380 pthread_mutex_unlock(&account_mutex);
2381 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2382 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2386 ACCOUNT_ERROR("account id(%d) is not exist. count(%d)\n", account_id, count);
2387 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
2390 /* Check permission of requested appid */
2391 char* current_appid = NULL;
2392 char *package_name = NULL;
2394 current_appid = _account_get_current_appid(pid, uid);
2396 error_code = _account_get_package_name_from_account_id(account_id, &package_name);
2398 if(error_code != _ACCOUNT_ERROR_NONE){
2399 ACCOUNT_ERROR("No package name with account_id\n");
2400 _ACCOUNT_FREE(current_appid);
2401 _ACCOUNT_FREE(package_name);
2402 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
2404 ACCOUNT_DEBUG( "DELETE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name);
2406 error_code = _account_check_appid_group_with_package_name(current_appid, package_name, uid);
2408 _ACCOUNT_FREE(current_appid);
2409 _ACCOUNT_FREE(package_name);
2411 if(error_code != _ACCOUNT_ERROR_NONE){
2412 ACCOUNT_ERROR("No permission to delete\n");
2413 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2416 /* transaction control required*/
2417 ret_transaction = _account_begin_transaction(g_hAccountDB);
2419 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
2420 pthread_mutex_unlock(&account_mutex);
2421 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2422 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2425 if( ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY ){
2426 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg(g_hAccountDB));
2427 pthread_mutex_unlock(&account_mutex);
2428 return _ACCOUNT_ERROR_DATABASE_BUSY;
2431 if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2432 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
2433 pthread_mutex_unlock(&account_mutex);
2434 return ret_transaction;
2437 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2438 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
2440 hstmt = _account_prepare_query(g_hAccountDB, query);
2442 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
2443 pthread_mutex_unlock(&account_mutex);
2444 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2445 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2448 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2449 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2451 rc = _account_query_step(hstmt);
2452 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2454 rc = _account_query_finalize(hstmt);
2456 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2459 ACCOUNT_MEMSET(query, 0, sizeof(query));
2461 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
2463 hstmt = _account_prepare_query(g_hAccountDB, query);
2464 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2465 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2467 rc = _account_query_step(hstmt);
2468 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
2470 rc = _account_query_finalize(hstmt);
2471 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2474 /* delete custom data */
2475 ACCOUNT_MEMSET(query, 0, sizeof(query));
2477 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
2479 hstmt = _account_prepare_query(g_hAccountDB, query);
2481 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2482 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2484 rc = _account_query_step(hstmt);
2485 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
2487 rc = _account_query_finalize(hstmt);
2488 ACCOUNT_CATCH_ERROR(rc == _ACCOUNT_ERROR_NONE, {}, rc, ("finalize error", account_id, rc));
2494 if (hstmt != NULL) {
2495 rc = _account_query_finalize(hstmt);
2496 if(rc != _ACCOUNT_ERROR_NONE ){
2497 ACCOUNT_ERROR("rc (%d)", rc);
2504 ret_transaction = _account_end_transaction(g_hAccountDB, is_success);
2506 if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2507 ACCOUNT_ERROR("account_delete:_account_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
2509 if (is_success == true) {
2511 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_DELETE, account_id);
2512 _account_insert_delete_update_notification_send(buf);
2516 pthread_mutex_unlock(&account_mutex);
2522 static int _account_query_account_by_username_and_package(const char* username, const char* package_name, account_s *account)
2526 int error_code = _ACCOUNT_ERROR_NONE;
2527 account_stmt hstmt = NULL;
2528 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2530 int binding_count = 1;
2532 ACCOUNT_RETURN_VAL((username != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("username IS NULL"));
2533 ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name IS NULL"));
2534 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2535 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2537 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2539 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
2540 hstmt = _account_prepare_query(g_hAccountDB, query);
2542 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
2543 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2544 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2547 _account_query_bind_text(hstmt, binding_count++, username);
2548 _account_query_bind_text(hstmt, binding_count++, package_name);
2550 rc = _account_query_step(hstmt);
2551 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2553 account_s *account_record = account;
2555 while (rc == SQLITE_ROW) {
2556 _account_convert_column_to_account(hstmt, account_record);
2557 rc = _account_query_step(hstmt);
2560 rc = _account_query_finalize(hstmt);
2561 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2562 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, account_record->id, (void*)account_record);
2563 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, account_record->id, (void*)account_record);
2566 error_code = _ACCOUNT_ERROR_NONE;
2569 if (hstmt != NULL) {
2570 rc = _account_query_finalize(hstmt);
2571 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2575 pthread_mutex_unlock(&account_mutex);
2579 int _account_create(account_s **account)
2582 ACCOUNT_SLOGE("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__);
2583 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2586 account_s *data = (account_s*)malloc(sizeof(account_s));
2589 ACCOUNT_FATAL("Memory Allocation Failed");
2590 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
2592 ACCOUNT_MEMSET(data, 0, sizeof(account_s));
2594 /*Setting account as visible by default*/
2595 // data->secret = _ACCOUNT_SECRECY_VISIBLE;
2597 /*Setting account as not supporting sync by default*/
2598 // data->sync_support = _ACCOUNT_SYNC_NOT_SUPPORT;
2602 return _ACCOUNT_ERROR_NONE;
2605 int _account_destroy(account_s *account)
2607 account_s *data = account;
2609 ACCOUNT_RETURN_VAL((data != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Account handle is null!"));
2611 _account_free_account_with_items(data);
2613 return _ACCOUNT_ERROR_NONE;
2616 int _account_get_account_id(account_s* account, int *account_id)
2619 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2622 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2625 *account_id = account->id;
2627 return _ACCOUNT_ERROR_NONE;
2630 int _account_delete_from_db_by_user_name(int pid, uid_t uid, const char *user_name, const char *package_name)
2632 _INFO("[%s][%s]", user_name, package_name);
2634 int error_code = _ACCOUNT_ERROR_NONE;
2635 account_stmt hstmt = NULL;
2636 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2638 int ret_transaction = 0;
2639 bool is_success = FALSE;
2640 account_s *account = NULL;
2641 int binding_count = 1;
2642 int account_id = -1;
2644 ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is null!"));
2645 ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
2646 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2648 /* Check permission of requested appid */
2649 char* current_appid = NULL;
2650 char* package_name_temp = NULL;
2652 current_appid = _account_get_current_appid(pid, uid);
2654 package_name_temp = _account_dup_text(package_name);
2656 ACCOUNT_DEBUG( "DELETE:user_name[%s],current_appid[%s], package_name[%s]", user_name, current_appid, package_name_temp);
2658 error_code = _account_check_appid_group_with_package_name(current_appid, package_name_temp, uid);
2660 _ACCOUNT_FREE(current_appid);
2661 _ACCOUNT_FREE(package_name_temp);
2663 if(error_code != _ACCOUNT_ERROR_NONE){
2664 ACCOUNT_ERROR("No permission to delete\n");
2665 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2668 rc = _account_create(&account);
2669 rc = _account_query_account_by_username_and_package(user_name, package_name, account);
2673 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM )
2675 _account_destroy(account);
2676 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2677 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2681 account_s* account_data = (account_s*)account;
2683 rc = _account_get_account_id(account_data, &account_id);
2685 rc = _account_destroy(account);
2687 /* transaction control required*/
2688 ret_transaction = _account_begin_transaction(g_hAccountDB);
2690 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM )
2692 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2693 pthread_mutex_unlock(&account_mutex);
2694 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2698 if( ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY )
2700 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg(g_hAccountDB));
2701 pthread_mutex_unlock(&account_mutex);
2702 return _ACCOUNT_ERROR_DATABASE_BUSY;
2704 else if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2705 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
2706 pthread_mutex_unlock(&account_mutex);
2707 return ret_transaction;
2710 /* delete custom data */
2711 ACCOUNT_MEMSET(query, 0, sizeof(query));
2712 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = ?", ACCOUNT_CUSTOM_TABLE);
2714 hstmt = _account_prepare_query(g_hAccountDB, query);
2716 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
2717 _account_end_transaction(g_hAccountDB, FALSE);
2718 pthread_mutex_unlock(&account_mutex);
2719 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2720 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2723 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2724 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2726 _account_query_bind_int(hstmt, binding_count++, account_id);
2728 rc = _account_query_step(hstmt);
2729 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2731 rc = _account_query_finalize(hstmt);
2732 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2735 /* delete capability */
2736 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", CAPABILITY_TABLE);
2738 hstmt = _account_prepare_query(g_hAccountDB, query);
2740 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2741 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2744 _account_query_bind_text(hstmt, binding_count++, user_name);
2745 _account_query_bind_text(hstmt, binding_count++, package_name);
2747 rc = _account_query_step(hstmt);
2748 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2750 rc = _account_query_finalize(hstmt);
2751 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2754 ACCOUNT_MEMSET(query, 0, sizeof(query));
2757 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
2759 hstmt = _account_prepare_query(g_hAccountDB, query);
2760 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2761 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2765 _account_query_bind_text(hstmt, binding_count++, user_name);
2766 _account_query_bind_text(hstmt, binding_count++, package_name);
2768 rc = _account_query_step(hstmt);
2769 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));
2771 rc = _account_query_finalize(hstmt);
2772 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2778 if (hstmt != NULL) {
2779 rc = _account_query_finalize(hstmt);
2780 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2784 ret_transaction = _account_end_transaction(g_hAccountDB, is_success);
2786 if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2787 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
2789 if (is_success == true) {
2791 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_DELETE, account_id);
2792 _account_insert_delete_update_notification_send(buf);
2796 pthread_mutex_unlock(&account_mutex);
2802 int _account_get_total_count_from_db(gboolean include_hidden, int *count)
2805 ACCOUNT_SLOGE("(%s)-(%d) count is NULL.\n", __FUNCTION__, __LINE__);
2806 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2810 ACCOUNT_ERROR("DB is not opened\n");
2811 return _ACCOUNT_ERROR_DB_NOT_OPENED;
2814 char query[1024] = {0, };
2815 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2819 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s", ACCOUNT_TABLE);
2823 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s where secret = %d", ACCOUNT_TABLE, _ACCOUNT_SECRECY_VISIBLE);
2826 *count = _account_get_record_count(g_hAccountDB, query);
2828 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
2829 pthread_mutex_unlock(&account_mutex);
2830 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2831 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2836 account_stmt pStmt = NULL;
2838 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
2839 if (SQLITE_OK != rc) {
2840 ACCOUNT_SLOGE("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg(g_hAccountDB));
2841 sqlite3_finalize(pStmt);
2842 return _ACCOUNT_ERROR_DB_FAILED;
2845 rc = sqlite3_step(pStmt);
2846 if (SQLITE_ROW != rc) {
2847 ACCOUNT_ERROR("[ERROR] sqlite3_step() failed\n");
2848 sqlite3_finalize(pStmt);
2849 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
2852 ncount = sqlite3_column_int(pStmt, 0);
2856 sqlite3_finalize(pStmt);
2859 ACCOUNT_ERROR("[ERROR] Number of account : %d, End", ncount);
2860 return _ACCOUNT_ERROR_DB_FAILED;
2863 return _ACCOUNT_ERROR_NONE;
2867 int account_server_query_app_id_exist(const char* app_id)
2869 _INFO("account_server_query_app_id_exist start app_id=[%s]", app_id);
2870 int ret = _ACCOUNT_ERROR_NONE;
2872 ret = _account_type_query_app_id_exist_from_all_db(g_hAccountDB, g_hAccountGlobalDB, app_id);
2874 _INFO("account_server_query_app_id_exist end error_code=[%d]", ret);
2878 int account_server_insert_account_type_to_user_db(account_type_s *account_type, int *account_type_id, uid_t uid)
2880 ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE HANDLE IS NULL"));
2881 ACCOUNT_RETURN_VAL((account_type->app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID OF ACCOUNT TYPE IS NULL"));
2882 ACCOUNT_RETURN_VAL((account_type_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE ID IS NULL"));
2884 _INFO("account_server_insert_account_type_to_user_db start uid=[%d]", uid);
2885 int ret = _ACCOUNT_ERROR_NONE;
2887 if (_account_type_check_duplicated(g_hAccountDB, account_type->app_id) ||
2888 _account_type_check_duplicated(g_hAccountGlobalDB, account_type->app_id)) {
2889 *account_type_id = -1;
2890 return _ACCOUNT_ERROR_DUPLICATED;
2893 ret = _account_type_insert_to_db(g_hAccountDB, account_type, account_type_id);
2894 _INFO("account_server_insert_account_type_to_user_db end error_code=[%d]", ret);
2898 int account_server_delete_account_type_by_app_id_from_user_db(const char * app_id)
2900 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID OF ACCOUNT TYPE IS NULL"));
2902 _INFO("account_server_delete_account_type_by_app_id_from_user_db start");
2903 int ret = _ACCOUNT_ERROR_NONE;
2905 ret = _account_type_delete_by_app_id(g_hAccountDB, app_id);
2906 _INFO("account_server_delete_account_type_by_app_id_from_user_db end error_code=[%d]", ret);
2910 GSList* _account_type_query_provider_feature_by_app_id_from_global_db(const char* app_id, int *error_code)
2912 _INFO("_account_type_query_provider_feature_by_app_id_in_global_db app_id=%s", app_id);
2913 account_stmt hstmt = NULL;
2914 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2915 int rc = 0, binding_count = 1;
2916 GSList* feature_list = NULL;
2918 ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
2919 ACCOUNT_RETURN_VAL((error_code != NULL), {_ERR("error_code pointer is NULL");}, NULL, (""));
2920 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {*error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; _ERR("The database isn't connected.");}, NULL, ("The database isn't connected."));
2922 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2924 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
2925 _INFO("account query=[%s]", query);
2927 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
2929 if( _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM ){
2930 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2931 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
2935 _INFO("before _account_query_bind_text");
2936 _account_query_bind_text(hstmt, binding_count++, app_id);
2938 rc = _account_query_step(hstmt);
2940 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"));
2942 provider_feature_s* feature_record = NULL;
2944 while (rc == SQLITE_ROW) {
2946 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
2948 if (feature_record == NULL) {
2949 ACCOUNT_FATAL("malloc Failed");
2953 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
2955 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
2957 _INFO("Adding account feature_list");
2958 feature_list = g_slist_append(feature_list, feature_record);
2960 rc = _account_query_step(hstmt);
2963 *error_code = _ACCOUNT_ERROR_NONE;
2966 if (hstmt != NULL) {
2967 rc = _account_query_finalize(hstmt);
2968 if (rc != _ACCOUNT_ERROR_NONE) {
2970 _ERR("global db fianlize error");
2974 if (*error_code != _ACCOUNT_ERROR_NONE) {
2975 _account_type_gslist_feature_free(feature_list);
2978 _INFO("Returning account feature_list from global db");
2979 return feature_list;
2982 GSList* _account_type_query_provider_feature_by_app_id(const char* app_id, int *error_code)
2984 _INFO("_account_type_query_provider_feature_by_app_id app_id=%s", app_id);
2985 account_stmt hstmt = NULL;
2986 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2987 int rc = 0, binding_count = 1;
2988 GSList* feature_list = NULL;
2990 ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
2991 ACCOUNT_RETURN_VAL((error_code != NULL), {_ERR("error_code pointer is NULL");}, NULL, (""));
2992 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
2994 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2996 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
2997 _INFO("account query=[%s]", query);
2999 hstmt = _account_prepare_query(g_hAccountDB, query);
3001 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
3002 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3003 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3007 _account_query_bind_text(hstmt, binding_count++, app_id);
3009 rc = _account_query_step(hstmt);
3011 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"));
3013 provider_feature_s* feature_record = NULL;
3015 while (rc == SQLITE_ROW) {
3017 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
3019 if (feature_record == NULL) {
3020 ACCOUNT_FATAL("malloc Failed");
3024 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
3026 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
3028 _INFO("Adding account feature_list");
3029 feature_list = g_slist_append(feature_list, feature_record);
3031 rc = _account_query_step(hstmt);
3034 *error_code = _ACCOUNT_ERROR_NONE;
3036 rc = _account_query_finalize(hstmt);
3037 ACCOUNT_CATCH_ERROR_P((rc == _ACCOUNT_ERROR_NONE), {*error_code = rc;}, rc, ("account finalize error"));
3041 if (hstmt != NULL) {
3042 rc = _account_query_finalize(hstmt);
3043 if (rc != _ACCOUNT_ERROR_NONE) {
3045 _ERR("account fianlize error");
3049 _INFO("*error_code=[%d]", *error_code);
3051 if (*error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
3052 feature_list = _account_type_query_provider_feature_by_app_id_from_global_db(app_id, error_code);
3055 if (*error_code != _ACCOUNT_ERROR_NONE)
3056 _account_type_gslist_feature_free(feature_list);
3058 _INFO("Returning account feature_list");
3059 return feature_list;
3062 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 )
3064 int error_code = _ACCOUNT_ERROR_NONE;
3065 account_stmt hstmt = NULL;
3066 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3067 int rc = 0, binding_count = 1;
3069 _INFO("_account_type_query_provider_feature_cb_by_app_id_in_global_db start app_id=%s", app_id);
3070 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3071 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3072 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3074 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3076 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
3077 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3079 if( _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM ){
3080 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3081 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_PERMISSION_DENIED, ("global db permission denied.\n"));
3084 _account_query_bind_text(hstmt, binding_count++, app_id);
3086 rc = _account_query_step(hstmt);
3087 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"));
3089 provider_feature_s* feature_record = NULL;
3091 while (rc == SQLITE_ROW) {
3092 bool cb_ret = FALSE;
3093 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
3095 if (feature_record == NULL) {
3096 ACCOUNT_FATAL("malloc Failed");
3100 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
3102 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
3104 cb_ret = callback(feature_record->app_id, feature_record->key, user_data);
3106 _account_type_free_feature_with_items(feature_record);
3108 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returns FALSE, its iteration is stopped!!!!\n"));
3110 rc = _account_query_step(hstmt);
3113 error_code = _ACCOUNT_ERROR_NONE;
3116 if (hstmt != NULL) {
3117 rc = _account_query_finalize(hstmt);
3118 if (rc != _ACCOUNT_ERROR_NONE) {
3120 _ERR("global db finalize error[%d]", rc);
3125 _INFO("_account_type_query_provider_feature_cb_by_app_id_in_global_db end. error_code=[%d]", error_code);
3129 int _account_type_query_provider_feature_cb_by_app_id(account_type_provider_feature_cb callback, const char* app_id, void *user_data )
3131 int error_code = _ACCOUNT_ERROR_NONE;
3132 account_stmt hstmt = NULL;
3133 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3134 int rc = 0, binding_count = 1;
3136 _INFO("_account_type_query_provider_feature_cb_by_app_id start app_id=%s", app_id);
3137 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3138 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3139 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3141 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3143 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
3144 hstmt = _account_prepare_query(g_hAccountDB, query);
3146 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
3147 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3148 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3151 _account_query_bind_text(hstmt, binding_count++, app_id);
3153 rc = _account_query_step(hstmt);
3154 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found in user db.\n"));
3156 provider_feature_s* feature_record = NULL;
3158 while (rc == SQLITE_ROW) {
3159 bool cb_ret = FALSE;
3160 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
3162 if (feature_record == NULL) {
3163 ACCOUNT_FATAL("malloc Failed");
3167 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
3169 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
3171 cb_ret = callback(feature_record->app_id, feature_record->key, user_data);
3173 _account_type_free_feature_with_items(feature_record);
3175 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
3177 rc = _account_query_step(hstmt);
3180 rc = _account_query_finalize(hstmt);
3181 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3184 error_code = _ACCOUNT_ERROR_NONE;
3187 if (hstmt != NULL) {
3188 rc = _account_query_finalize(hstmt);
3189 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3193 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
3194 error_code = _account_type_query_provider_feature_cb_by_app_id_from_global_db(callback, app_id, user_data);
3197 _INFO("_account_type_query_provider_feature_cb_by_app_id end");
3201 int account_type_query_provider_feature_cb_by_app_id(account_type_provider_feature_cb callback, const char* app_id, void *user_data )
3203 int error_code = _ACCOUNT_ERROR_NONE;
3205 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3206 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3207 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3209 error_code = _account_type_query_provider_feature_cb_by_app_id(callback, app_id, user_data);
3211 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
3212 error_code = _account_type_query_provider_feature_cb_by_app_id_from_global_db(callback, app_id, user_data);
3218 bool _account_type_query_supported_feature_from_global_db(const char* app_id, const char* capability, int *error_code)
3220 _INFO("_account_type_query_supported_feature_in_global_db start");
3221 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3223 *error_code = _ACCOUNT_ERROR_NONE;
3225 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3226 int record_count = 0;
3228 if (app_id == NULL || capability == NULL)
3230 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
3234 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
3236 record_count = _account_get_record_count(g_hAccountGlobalDB, query);
3238 if( _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM ){
3239 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3240 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3243 if (record_count <= 0)
3245 *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
3249 _INFO("_account_type_query_supported_feature_in_global_db end");
3253 bool _account_type_query_supported_feature(const char* app_id, const char* capability, int *error_code)
3255 _INFO("_account_type_query_supported_feature start");
3257 *error_code = _ACCOUNT_ERROR_NONE;
3259 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3260 int record_count = 0;
3262 if (app_id == NULL || capability == NULL)
3264 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
3268 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
3270 record_count = _account_get_record_count(g_hAccountDB, query);
3272 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
3273 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3274 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3278 if (record_count <= 0)
3280 bool is_exist = false;
3281 is_exist = _account_type_query_supported_feature_from_global_db(app_id, capability, error_code);
3286 _INFO("_account_type_query_supported_feature end");
3291 static int _account_type_update_provider_feature(sqlite3 *account_db_handle, account_type_s *account_type, const char* app_id)
3294 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3295 account_stmt hstmt = NULL;
3297 ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
3299 if (g_slist_length( account_type->provider_feature_list)==0) {
3300 ACCOUNT_ERROR( "no feature\n");
3301 return _ACCOUNT_ERROR_NONE;
3304 ACCOUNT_DEBUG( "app id", app_id);
3306 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3308 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id=? ", PROVIDER_FEATURE_TABLE);
3309 hstmt = _account_prepare_query(account_db_handle, query);
3311 if( _account_db_err_code(account_db_handle) == SQLITE_PERM ){
3312 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(account_db_handle));
3313 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3317 _account_query_bind_text(hstmt, count++, app_id);
3318 rc = _account_query_step(hstmt);
3320 if (rc != SQLITE_DONE) {
3321 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3322 return _ACCOUNT_ERROR_DB_FAILED;
3324 rc = _account_query_finalize(hstmt);
3325 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3330 for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
3333 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3334 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
3335 "(?, ?) ", PROVIDER_FEATURE_TABLE);
3337 hstmt = _account_prepare_query(account_db_handle, query);
3339 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg(account_db_handle)));
3341 provider_feature_s* feature_data = NULL;
3342 feature_data = (provider_feature_s*)iter->data;
3344 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
3345 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3346 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
3347 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3349 rc = _account_query_step(hstmt);
3351 if (rc != SQLITE_DONE) {
3352 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3355 rc = _account_query_finalize(hstmt);
3356 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3360 return _ACCOUNT_ERROR_NONE;
3363 static int _account_type_update_label(sqlite3 *account_db_handle, account_type_s *account_type, const char* app_id)
3366 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3367 account_stmt hstmt = NULL;
3369 ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
3371 if (g_slist_length( account_type->label_list)==0) {
3372 return _ACCOUNT_ERROR_NONE;
3375 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3377 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId=? ", LABEL_TABLE);
3378 hstmt = _account_prepare_query(account_db_handle, query);
3380 if( _account_db_err_code(account_db_handle) == SQLITE_PERM ){
3381 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(account_db_handle));
3382 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3386 _account_query_bind_text(hstmt, count++, app_id);
3387 rc = _account_query_step(hstmt);
3389 if (rc != SQLITE_DONE) {
3390 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3391 return _ACCOUNT_ERROR_DB_FAILED;
3393 rc = _account_query_finalize(hstmt);
3394 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3399 for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
3402 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3403 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
3404 "(?, ?, ?) ", LABEL_TABLE);
3406 hstmt = _account_prepare_query(account_db_handle, query);
3408 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg(account_db_handle)));
3410 label_s* label_data = NULL;
3411 label_data = (label_s*)iter->data;
3413 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
3414 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3415 ret = _account_query_bind_text(hstmt, count++, label_data->label);
3416 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3417 ret = _account_query_bind_text(hstmt, count++, label_data->locale);
3418 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3420 rc = _account_query_step(hstmt);
3422 if (rc != SQLITE_DONE) {
3423 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3426 rc = _account_query_finalize(hstmt);
3427 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3431 return _ACCOUNT_ERROR_NONE;
3435 static int _account_type_update_account(sqlite3 *account_db_handle, account_type_s *account_type, const char* app_id)
3437 int rc = 0, binding_count =1;
3438 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3439 int error_code = _ACCOUNT_ERROR_NONE;
3440 account_stmt hstmt = NULL;
3442 if (!account_type->app_id) {
3443 ACCOUNT_ERROR("app id is mandetory field, it can not be NULL!!!!\n");
3444 return _ACCOUNT_ERROR_INVALID_PARAMETER;
3447 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3448 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET AppId=?, ServiceProviderId=?, IconPath=?, "
3449 "SmallIconPath=?, MultipleAccountSupport=? WHERE AppId=? ", ACCOUNT_TYPE_TABLE);
3451 hstmt = _account_prepare_query(account_db_handle, query);
3453 if( _account_db_err_code(account_db_handle) == SQLITE_PERM ){
3454 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(account_db_handle));
3455 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3456 } else if (_account_db_err_code(account_db_handle) == SQLITE_BUSY){
3457 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg(account_db_handle));
3458 return _ACCOUNT_ERROR_DATABASE_BUSY;
3461 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg(account_db_handle)));
3463 binding_count = _account_type_convert_account_to_sql(account_type, hstmt, query);
3464 _account_query_bind_text(hstmt, binding_count++, app_id);
3466 rc = _account_query_step(hstmt);
3467 if (rc != SQLITE_DONE) {
3468 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3471 rc = _account_query_finalize(hstmt);
3472 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3476 error_code = _account_type_update_label(account_db_handle, account_type, app_id);
3477 /* update provider feature */
3478 error_code = _account_type_update_provider_feature(account_db_handle, account_type, app_id);
3483 int _account_type_update_to_db_by_app_id(account_type_s* account_type, const char* app_id)
3485 int error_code = _ACCOUNT_ERROR_NONE;
3487 ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
3488 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3489 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3491 account_type_s* data = account_type;
3493 pthread_mutex_lock(&account_mutex);
3495 error_code = _account_type_update_account(g_hAccountDB, data, app_id);
3497 pthread_mutex_unlock(&account_mutex);
3502 GSList* _account_type_get_label_list_by_app_id_from_global_db(const char* app_id, int *error_code )
3504 *error_code = _ACCOUNT_ERROR_NONE;
3505 account_stmt hstmt = NULL;
3506 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3507 int rc = 0, binding_count = 1;
3508 GSList* label_list = NULL;
3510 ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
3511 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3513 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3515 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3516 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3518 if( _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM ){
3519 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3520 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3525 _account_query_bind_text(hstmt, binding_count++, app_id);
3527 rc = _account_query_step(hstmt);
3528 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"));
3530 label_s* label_record = NULL;
3532 while (rc == SQLITE_ROW) {
3533 label_record = (label_s*) malloc(sizeof(label_s));
3535 if (label_record == NULL) {
3536 ACCOUNT_FATAL("malloc Failed");
3540 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3542 _account_type_convert_column_to_label(hstmt, label_record);
3544 _INFO("Adding account label_list");
3545 label_list = g_slist_append (label_list, label_record);
3547 rc = _account_query_step(hstmt);
3550 *error_code = _ACCOUNT_ERROR_NONE;
3553 if (hstmt != NULL) {
3554 rc = _account_query_finalize(hstmt);
3555 if (rc != _ACCOUNT_ERROR_NONE) {
3556 _ERR("global db finalize error[%d]", rc);
3561 _INFO("Returning account global label_list");
3565 GSList* _account_type_get_label_list_by_app_id(const char* app_id, int *error_code )
3567 *error_code = _ACCOUNT_ERROR_NONE;
3568 account_stmt hstmt = NULL;
3569 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3570 int rc = 0, binding_count = 1;
3571 GSList* label_list = NULL;
3573 ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
3574 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
3576 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3578 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3579 hstmt = _account_prepare_query(g_hAccountDB, query);
3581 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
3582 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3583 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3587 _account_query_bind_text(hstmt, binding_count++, app_id);
3589 rc = _account_query_step(hstmt);
3590 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3592 label_s* label_record = NULL;
3594 while (rc == SQLITE_ROW) {
3595 label_record = (label_s*) malloc(sizeof(label_s));
3597 if (label_record == NULL) {
3598 ACCOUNT_FATAL("malloc Failed");
3602 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3604 _account_type_convert_column_to_label(hstmt, label_record);
3606 _INFO("Adding account label_list");
3607 label_list = g_slist_append (label_list, label_record);
3609 rc = _account_query_step(hstmt);
3612 rc = _account_query_finalize(hstmt);
3613 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
3616 *error_code = _ACCOUNT_ERROR_NONE;
3619 if (hstmt != NULL) {
3620 rc = _account_query_finalize(hstmt);
3621 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
3625 if (*error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
3626 label_list = _account_type_get_label_list_by_app_id_from_global_db(app_id, error_code);
3629 _INFO("Returning account label_list");
3633 int _account_type_query_label_by_app_id_from_global_db(account_type_label_cb callback, const char* app_id, void *user_data )
3635 int error_code = _ACCOUNT_ERROR_NONE;
3636 account_stmt hstmt = NULL;
3637 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3638 int rc = 0, binding_count = 1;
3640 _INFO("account_type_query_label_by_app_id_from_global_db start");
3642 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3643 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3644 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3646 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3648 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3649 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3651 if( _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM ){
3652 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3653 error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3657 _account_query_bind_text(hstmt, binding_count++, app_id);
3659 rc = _account_query_step(hstmt);
3660 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3662 label_s* label_record = NULL;
3664 while (rc == SQLITE_ROW) {
3665 bool cb_ret = FALSE;
3666 label_record = (label_s*) malloc(sizeof(label_s));
3668 if (label_record == NULL) {
3669 ACCOUNT_FATAL("malloc Failed");
3673 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3675 _account_type_convert_column_to_label(hstmt, label_record);
3677 cb_ret = callback(label_record->app_id, label_record->label , label_record->locale, user_data);
3679 _account_type_free_label_with_items(label_record);
3681 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
3683 rc = _account_query_step(hstmt);
3686 error_code = _ACCOUNT_ERROR_NONE;
3689 if (hstmt != NULL) {
3690 rc = _account_query_finalize(hstmt);
3691 if (rc != _ACCOUNT_ERROR_NONE) {
3692 _ERR("global db finalize error[%d]", rc);
3697 _INFO("account_type_query_label_by_app_id_from_global_db end [%d]", error_code);
3701 int _account_type_query_label_by_app_id(account_type_label_cb callback, const char* app_id, void *user_data )
3703 int error_code = _ACCOUNT_ERROR_NONE;
3704 account_stmt hstmt = NULL;
3705 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3706 int rc = 0, binding_count = 1;
3708 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3709 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3710 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3712 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3714 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3715 hstmt = _account_prepare_query(g_hAccountDB, query);
3717 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
3718 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3719 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3722 _account_query_bind_text(hstmt, binding_count++, app_id);
3724 rc = _account_query_step(hstmt);
3725 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3727 label_s* label_record = NULL;
3729 while (rc == SQLITE_ROW) {
3730 bool cb_ret = FALSE;
3731 label_record = (label_s*) malloc(sizeof(label_s));
3733 if (label_record == NULL) {
3734 ACCOUNT_FATAL("malloc Failed");
3738 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3740 _account_type_convert_column_to_label(hstmt, label_record);
3742 cb_ret = callback(label_record->app_id, label_record->label , label_record->locale, user_data);
3744 _account_type_free_label_with_items(label_record);
3746 // ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
3747 if(cb_ret != TRUE) {
3748 _INFO("Callback func returs FALSE, its iteration is stopped!!!!\n");
3752 rc = _account_query_step(hstmt);
3755 rc = _account_query_finalize(hstmt);
3756 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3759 error_code = _ACCOUNT_ERROR_NONE;
3762 if (hstmt != NULL) {
3763 rc = _account_query_finalize(hstmt);
3764 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3768 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
3769 error_code = account_type_query_label_by_app_id_from_global_db(callback, app_id, user_data);
3775 int account_type_query_label_by_app_id(account_type_label_cb callback, const char* app_id, void *user_data )
3777 int error_code = _ACCOUNT_ERROR_NONE;
3779 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3780 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3781 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3783 error_code = _account_type_query_label_by_app_id(callback, app_id, user_data);
3785 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
3786 error_code = _account_type_query_label_by_app_id_from_global_db(callback, app_id, user_data);
3792 bool _account_get_label_text_cb(char* app_id, char* label, char* locale, void *user_data)
3794 account_type_s *data = (account_type_s*)user_data;
3796 label_s *label_data = (label_s*)malloc(sizeof(label_s));
3798 if (label_data == NULL) {
3799 ACCOUNT_FATAL("_account_get_label_text_cb : MALLOC FAIL\n");
3802 ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
3804 label_data->app_id = _account_dup_text(app_id);
3805 label_data->label = _account_dup_text(label);
3806 label_data->locale = _account_dup_text(locale);
3808 data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
3813 bool _account_get_provider_feature_cb(char* app_id, char* key, void* user_data)
3815 account_type_s *data = (account_type_s*)user_data;
3817 provider_feature_s *feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
3819 if (feature_data == NULL) {
3820 ACCOUNT_FATAL("_account_get_provider_feature_cb : MALLOC FAIL\n");
3823 ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
3825 feature_data->app_id = _account_dup_text(app_id);
3826 feature_data->key = _account_dup_text(key);
3828 data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
3833 int _account_type_query_by_app_id_from_global_db(const char* app_id, account_type_s** account_type_record)
3835 _INFO("_account_type_query_by_app_id_from_global_db start");
3837 int error_code = _ACCOUNT_ERROR_NONE;
3838 account_stmt hstmt = NULL;
3839 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3840 int rc = 0, binding_count = 1;
3842 ACCOUNT_RETURN_VAL((app_id != 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3843 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3844 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3846 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3848 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
3849 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3851 if( _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM ){
3852 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3853 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3856 _account_query_bind_text(hstmt, binding_count++, app_id);
3858 rc = _account_query_step(hstmt);
3859 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3861 *account_type_record = create_empty_account_type_instance();
3863 while (rc == SQLITE_ROW) {
3864 _account_type_convert_column_to_account_type(hstmt, *account_type_record);
3865 rc = _account_query_step(hstmt);
3868 rc = _account_query_finalize(hstmt);
3869 ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {_ERR("global db finalize error rc=[%d]", rc);}, rc, ("finalize error"));
3870 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb, app_id, (void*)(*account_type_record));
3871 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, app_id,(void*)(*account_type_record));
3874 error_code = _ACCOUNT_ERROR_NONE;
3877 if (hstmt != NULL) {
3878 rc = _account_query_finalize(hstmt);
3879 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3883 _INFO("_account_type_query_by_app_id_from_global_db end [%d]", error_code);
3887 int _account_type_query_by_app_id(const char* app_id, account_type_s** account_type_record)
3889 _INFO("_account_type_query_by_app_id start");
3891 int error_code = _ACCOUNT_ERROR_NONE;
3892 account_stmt hstmt = NULL;
3893 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3894 int rc = 0, binding_count = 1;
3896 ACCOUNT_RETURN_VAL((app_id != 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3897 ACCOUNT_RETURN_VAL((account_type_record != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("account type record(account_type_s**) is NULL"));
3898 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3900 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3902 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
3903 hstmt = _account_prepare_query(g_hAccountDB, query);
3905 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
3906 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3907 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3910 _account_query_bind_text(hstmt, binding_count++, app_id);
3912 rc = _account_query_step(hstmt);
3913 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3915 *account_type_record = create_empty_account_type_instance();
3916 if (*account_type_record == NULL) {
3917 _ERR("Out of Memory");
3918 error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
3922 while (rc == SQLITE_ROW) {
3923 _account_type_convert_column_to_account_type(hstmt, *account_type_record);
3924 rc = _account_query_step(hstmt);
3927 rc = _account_query_finalize(hstmt);
3928 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3929 _account_type_query_label_by_app_id(_account_get_label_text_cb, app_id, (void*)(*account_type_record));
3930 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, app_id,(void*)(*account_type_record));
3933 error_code = _ACCOUNT_ERROR_NONE;
3936 if (hstmt != NULL) {
3937 rc = _account_query_finalize(hstmt);
3938 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3942 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
3943 error_code = _account_type_query_by_app_id_from_global_db(app_id, account_type_record);
3946 _INFO("_account_type_query_by_app_id end [%d]", error_code);
3950 int _account_type_query_by_provider_feature_from_global_db(const char* key, GSList **account_type_list_all)
3952 int error_code = _ACCOUNT_ERROR_NONE;
3953 account_stmt hstmt = NULL;
3954 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3956 GSList *account_type_list = NULL;
3958 _INFO("_account_type_query_by_provider_feature_from_global_db start key=%s", key);
3961 ACCOUNT_ERROR("capability_type IS NULL.");
3962 error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
3966 if(g_hAccountGlobalDB == NULL)
3968 ACCOUNT_ERROR("The database isn't connected.");
3969 error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;
3973 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3975 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
3977 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3979 if( _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM )
3981 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3982 error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3986 int binding_count = 1;
3987 _account_query_bind_text(hstmt, binding_count++, key);
3989 rc = _account_query_step(hstmt);
3991 account_type_s *account_type_record = NULL;
3993 if(rc != SQLITE_ROW)
3995 ACCOUNT_ERROR("The record isn't found. rc=[%d]", rc);
3996 error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4000 while(rc == SQLITE_ROW) {
4001 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4003 if (account_type_record == NULL) {
4004 ACCOUNT_FATAL("malloc Failed");
4008 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4009 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4010 account_type_list = g_slist_append(account_type_list, account_type_record);
4011 rc = _account_query_step(hstmt);
4014 rc = _account_query_finalize(hstmt);
4015 if (rc != _ACCOUNT_ERROR_NONE )
4017 _account_type_gslist_account_type_free(account_type_list);
4018 ACCOUNT_ERROR("finalize error(%s)", rc);
4026 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4027 account_type_s *account_type = NULL;
4028 account_type = (account_type_s*)iter->data;
4029 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
4030 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
4031 _INFO("add label & provider_feature");
4034 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4036 account_type_s *account_type = NULL;
4037 account_type = (account_type_s*)iter->data;
4038 *account_type_list_all = g_slist_append(*account_type_list_all, account_type);
4039 _INFO("add account_type");
4041 g_slist_free(account_type_list);
4043 error_code = _ACCOUNT_ERROR_NONE;
4046 if (hstmt != NULL) {
4047 rc = _account_query_finalize(hstmt);
4048 if (rc != _ACCOUNT_ERROR_NONE)
4050 ACCOUNT_ERROR("finalize error(%s)", rc);
4056 _INFO("_account_type_query_by_provider_feature_from_global_db end. error_code=[%d]", error_code);
4060 GSList* _account_type_query_by_provider_feature(const char* key, int *error_code)
4062 *error_code = _ACCOUNT_ERROR_NONE;
4063 account_stmt hstmt = NULL;
4064 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4066 GSList *account_type_list = NULL;
4068 _INFO("account_type_query_by_provider_feature start key=%s", key);
4071 ACCOUNT_ERROR("capability_type IS NULL.");
4072 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
4076 if(g_hAccountDB == NULL)
4078 ACCOUNT_ERROR("The database isn't connected.");
4079 *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;
4083 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4085 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
4087 hstmt = _account_prepare_query(g_hAccountDB, query);
4089 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM )
4091 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4092 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
4096 int binding_count = 1;
4097 _account_query_bind_text(hstmt, binding_count++, key);
4099 rc = _account_query_step(hstmt);
4101 account_type_s *account_type_record = NULL;
4103 if(rc != SQLITE_ROW)
4105 ACCOUNT_ERROR("The record isn't found. rc=[%d]", rc);
4106 *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4110 while(rc == SQLITE_ROW) {
4111 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4113 if (account_type_record == NULL) {
4114 ACCOUNT_FATAL("malloc Failed");
4118 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4119 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4120 account_type_list = g_slist_append(account_type_list, account_type_record);
4121 rc = _account_query_step(hstmt);
4124 rc = _account_query_finalize(hstmt);
4125 if (rc != _ACCOUNT_ERROR_NONE )
4127 _account_type_gslist_account_type_free(account_type_list);
4128 ACCOUNT_ERROR("finalize error(%s)", rc);
4136 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4137 account_type_s *account_type = NULL;
4138 account_type = (account_type_s*)iter->data;
4139 _account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
4140 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
4143 *error_code = _ACCOUNT_ERROR_NONE;
4146 if (hstmt != NULL) {
4147 rc = _account_query_finalize(hstmt);
4148 if (rc != _ACCOUNT_ERROR_NONE)
4156 if (*error_code == _ACCOUNT_ERROR_NONE || *error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4157 rc = _account_type_query_by_provider_feature_from_global_db(key, &account_type_list);
4158 if (rc != _ACCOUNT_ERROR_NONE && rc != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4159 ACCOUNT_ERROR( "_account_type_query_by_provider_feature_from_global_db fail=[%d]", rc);
4160 _account_type_gslist_account_type_free(account_type_list);
4163 if (rc == _ACCOUNT_ERROR_NONE)
4167 _INFO("account_type_query_by_provider_feature end");
4168 return account_type_list;
4171 int _account_type_query_all_from_global_db(GSList **account_type_list_all)
4173 account_stmt hstmt = NULL;
4174 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4175 int rc = _ACCOUNT_ERROR_NONE;
4176 int error_code = _ACCOUNT_ERROR_NONE;
4177 GSList *account_type_list = NULL;
4179 _INFO("_account_type_query_all_in_global_db start");
4180 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, NULL, ("The database isn't connected."));
4182 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4184 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
4185 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
4187 rc = _account_query_step(hstmt);
4189 if( _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM ){
4190 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
4191 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4194 account_type_s *account_type_record = NULL;
4196 if (rc != SQLITE_ROW)
4198 _INFO("[_ACCOUNT_ERROR_RECORD_NOT_FOUND]The record isn't found.");
4199 error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4203 while(rc == SQLITE_ROW) {
4204 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4206 if (account_type_record == NULL) {
4207 ACCOUNT_FATAL("malloc Failed");
4211 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4212 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4213 account_type_list = g_slist_append(account_type_list, account_type_record);
4214 rc = _account_query_step(hstmt);
4217 rc = _account_query_finalize(hstmt);
4218 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4223 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4224 account_type_s *account_type = NULL;
4225 account_type = (account_type_s*)iter->data;
4226 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
4227 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
4230 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4231 account_type_s *account_type = NULL;
4232 account_type = (account_type_s*)iter->data;
4233 *account_type_list_all = g_slist_append(*account_type_list_all, account_type);
4235 g_slist_free(account_type_list);
4237 error_code = _ACCOUNT_ERROR_NONE;
4241 rc = _account_query_finalize(hstmt);
4242 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list);}, rc, ("finalize error"));
4246 _INFO("_account_type_query_all_in_global_db end");
4250 GSList* _account_type_query_all(void)
4252 account_stmt hstmt = NULL;
4253 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4255 int error_code = _ACCOUNT_ERROR_NONE;
4256 GSList *account_type_list = NULL;
4258 _INFO("_account_type_query_all start");
4259 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected."));
4261 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4263 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
4264 hstmt = _account_prepare_query(g_hAccountDB, query);
4266 rc = _account_query_step(hstmt);
4268 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
4269 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4273 account_type_s *account_type_record = NULL;
4275 if (rc != SQLITE_ROW)
4277 _INFO("[_ACCOUNT_ERROR_RECORD_NOT_FOUND]The record isn't found.");
4278 error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4282 while(rc == SQLITE_ROW) {
4283 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4285 if (account_type_record == NULL) {
4286 ACCOUNT_FATAL("malloc Failed");
4290 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4291 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4292 account_type_list = g_slist_append(account_type_list, account_type_record);
4293 rc = _account_query_step(hstmt);
4296 rc = _account_query_finalize(hstmt);
4297 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, NULL, ("finalize error"));
4302 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4303 account_type_s *account_type = NULL;
4304 account_type = (account_type_s*)iter->data;
4305 _account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
4306 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
4309 error_code = _ACCOUNT_ERROR_NONE;
4313 rc = _account_query_finalize(hstmt);
4314 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list);}, NULL, ("finalize error"));
4318 if (error_code == _ACCOUNT_ERROR_NONE || error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4319 error_code = _account_type_query_all_from_global_db(&account_type_list);
4320 if (rc != _ACCOUNT_ERROR_NONE && rc != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4321 ACCOUNT_ERROR( "_account_type_query_all_from_global_db fail=[%d]", rc);
4322 _account_type_gslist_account_type_free(account_type_list);
4327 _INFO("_account_type_query_all end");
4328 return account_type_list;
4331 // output parameter label must be free
4332 int _account_type_query_label_by_locale_from_global_db(const char* app_id, const char* locale, char **label)
4334 int error_code = _ACCOUNT_ERROR_NONE;
4335 account_stmt hstmt = NULL;
4336 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4337 int rc = 0, binding_count = 1;
4338 char* converted_locale = NULL;
4340 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
4341 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4342 ACCOUNT_RETURN_VAL((label != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
4343 ACCOUNT_RETURN_VAL((locale != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("locale char is null"));
4344 //Making label newly created
4346 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4348 converted_locale = _account_dup_text(locale);
4349 gchar** tokens = g_strsplit(converted_locale, "-", 2);
4351 if(tokens != NULL) {
4352 if((char*)(tokens[1]) != NULL) {
4353 char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1]));
4354 if(upper_token != NULL) {
4355 _ACCOUNT_FREE(converted_locale);
4356 converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token);
4358 _ACCOUNT_FREE(upper_token);
4363 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, converted_locale);
4364 _ACCOUNT_FREE(converted_locale);
4366 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
4368 if( _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM ){
4369 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
4370 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4373 _account_query_bind_text(hstmt, binding_count++, app_id);
4375 rc = _account_query_step(hstmt);
4376 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4378 label_s* label_record = NULL;
4380 while (rc == SQLITE_ROW) {
4381 label_record = (label_s*) malloc(sizeof(label_s));
4383 if (label_record == NULL) {
4384 ACCOUNT_FATAL("malloc Failed");
4388 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
4390 _account_type_convert_column_to_label(hstmt,label_record);
4392 _ACCOUNT_FREE(*label);
4393 //Making label newly created
4394 *label = _account_dup_text(label_record->label);
4396 _account_type_free_label_with_items(label_record);
4398 rc = _account_query_step(hstmt);
4401 rc = _account_query_finalize(hstmt);
4402 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4405 error_code = _ACCOUNT_ERROR_NONE;
4408 if (hstmt != NULL) {
4409 rc = _account_query_finalize(hstmt);
4410 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4414 _INFO("_account_type_query_label_by_locale_from_global_db() end : error_code = %d", error_code);
4418 // output parameter label must be free
4419 int _account_type_query_label_by_locale(const char* app_id, const char* locale, char **label)
4421 int error_code = _ACCOUNT_ERROR_NONE;
4422 account_stmt hstmt = NULL;
4423 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4424 int rc = 0, binding_count = 1;
4425 char* converted_locale = NULL;
4427 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
4428 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4429 ACCOUNT_RETURN_VAL((label != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
4430 ACCOUNT_RETURN_VAL((locale != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("locale char is null"));
4431 //Making label newly created
4433 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4435 converted_locale = _account_dup_text(locale);
4436 gchar** tokens = g_strsplit(converted_locale, "-", 2);
4438 if(tokens != NULL) {
4439 if((char*)(tokens[1]) != NULL) {
4440 char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1]));
4441 if(upper_token != NULL) {
4442 _ACCOUNT_FREE(converted_locale);
4443 converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token);
4445 _ACCOUNT_FREE(upper_token);
4450 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, converted_locale);
4451 _ACCOUNT_FREE(converted_locale);
4453 hstmt = _account_prepare_query(g_hAccountDB, query);
4455 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
4456 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4457 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4460 _account_query_bind_text(hstmt, binding_count++, app_id);
4462 rc = _account_query_step(hstmt);
4463 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4465 label_s* label_record = NULL;
4467 while (rc == SQLITE_ROW) {
4468 label_record = (label_s*) malloc(sizeof(label_s));
4470 if (label_record == NULL) {
4471 ACCOUNT_FATAL("malloc Failed");
4475 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
4477 _account_type_convert_column_to_label(hstmt,label_record);
4479 _ACCOUNT_FREE(*label);
4480 //Making label newly created
4481 *label = _account_dup_text(label_record->label);
4483 _account_type_free_label_with_items(label_record);
4485 rc = _account_query_step(hstmt);
4488 rc = _account_query_finalize(hstmt);
4489 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4492 error_code = _ACCOUNT_ERROR_NONE;
4495 if (hstmt != NULL) {
4496 rc = _account_query_finalize(hstmt);
4497 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4501 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4502 error_code = _account_type_query_label_by_locale_from_global_db(app_id, locale, label);
4505 _INFO("_account_type_query_label_by_locale() end : error_code = %d", error_code);
4509 static int _account_insert_custom(account_s *account, int account_id)
4511 _INFO("_account_insert_custom start");
4514 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4515 account_stmt hstmt = NULL;
4517 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4519 if (g_slist_length( account->custom_list)==0) {
4520 ACCOUNT_DEBUG( "_account_insert_custom, no custom data\n");
4521 return _ACCOUNT_ERROR_NONE;
4524 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
4526 rc = _account_get_record_count(g_hAccountDB, query);
4528 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
4529 ACCOUNT_ERROR( "Access failed(%d, %s)", _account_db_err_msg(g_hAccountDB));
4530 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4534 ACCOUNT_SLOGE( "_account_insert_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
4535 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4542 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
4545 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4546 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (AccountId, AppId, Key, Value) VALUES "
4547 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
4549 hstmt = _account_prepare_query(g_hAccountDB, query);
4551 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
4552 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4553 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4556 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
4558 account_custom_s* custom_data = NULL;
4559 custom_data = (account_custom_s*)iter->data;
4561 ret = _account_query_bind_int(hstmt, count++, account_id);
4562 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
4563 ret = _account_query_bind_text(hstmt, count++, account->package_name);
4564 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4565 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
4566 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4567 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
4568 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4570 rc = _account_query_step(hstmt);
4572 if (rc != SQLITE_DONE) {
4573 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4577 rc = _account_query_finalize(hstmt);
4578 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4583 _INFO("_account_insert_custom end");
4584 return _ACCOUNT_ERROR_NONE;
4587 static int _account_update_custom(account_s *account, int account_id)
4590 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4591 account_stmt hstmt = NULL;
4593 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4595 if (g_slist_length( account->custom_list)==0) {
4596 ACCOUNT_DEBUG( "_account_update_custom, no custom data\n");
4597 return _ACCOUNT_ERROR_NONE;
4600 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
4602 rc = _account_get_record_count(g_hAccountDB, query);
4604 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
4605 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4606 pthread_mutex_unlock(&account_mutex);
4607 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4608 } else if( _account_db_err_code(g_hAccountDB) == SQLITE_BUSY ){
4609 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg(g_hAccountDB));
4610 pthread_mutex_unlock(&account_mutex);
4611 return _ACCOUNT_ERROR_DATABASE_BUSY;
4615 ACCOUNT_SLOGE( "_account_update_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
4616 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4619 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4621 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId=? ", ACCOUNT_CUSTOM_TABLE);
4622 hstmt = _account_prepare_query(g_hAccountDB, query);
4624 _account_query_bind_int(hstmt, count++, (int)account_id);
4625 rc = _account_query_step(hstmt);
4627 if (rc == SQLITE_BUSY) {
4628 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4629 return _ACCOUNT_ERROR_DATABASE_BUSY;
4630 } else if (rc != SQLITE_DONE) {
4631 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4632 return _ACCOUNT_ERROR_DB_FAILED;
4635 rc = _account_query_finalize(hstmt);
4636 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4641 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
4644 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4645 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AccountId, AppId, Key, Value) VALUES "
4646 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
4648 hstmt = _account_prepare_query(g_hAccountDB, query);
4650 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
4651 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4652 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4655 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
4657 account_custom_s* custom_data = NULL;
4658 custom_data = (account_custom_s*)iter->data;
4660 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
4661 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
4662 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
4663 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4664 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
4665 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4666 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
4667 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4669 rc = _account_query_step(hstmt);
4671 if (rc != SQLITE_DONE) {
4672 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4676 rc = _account_query_finalize(hstmt);
4677 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4682 return _ACCOUNT_ERROR_NONE;