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 db_util_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 db_util_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 )
288 ACCOUNT_ERROR( "db_util_close(g_hAccountGlobalDB) fail ret = %d", ret);
289 g_hAccountGlobalDB2 = g_hAccountGlobalDB;
291 g_hAccountGlobalDB = NULL;
296 static bool _account_check_add_more_account(const char* app_id)
298 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
301 ACCOUNT_RETURN_VAL((app_id != 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
302 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
304 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
306 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s' and MultipleAccountSupport = 1", ACCOUNT_TYPE_TABLE, app_id);
307 rc = _account_get_record_count(g_hAccountDB, query);
309 /* multiple account support case (User DB & global DB) */
310 if(rc > 0 || _account_get_record_count(g_hAccountGlobalDB, query) > 0) {
311 ACCOUNT_SLOGD("app id (%s) supports multiple account. rc(%d)\n", app_id, rc);
315 /* multiple account not support case */
316 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
317 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE package_name = '%s'", ACCOUNT_TABLE, app_id);
318 rc = _account_get_record_count(g_hAccountDB, query);
321 ACCOUNT_SLOGD("app id (%s) supports single account. and there is no account of the app id\n", app_id);
329 int _account_db_open(int mode, int pid, uid_t uid)
333 char account_db_dir[256] = {0, };
334 char account_db_path[256] = {0, };
336 _INFO( "start _account_db_open()");
338 ACCOUNT_MEMSET(account_db_dir, 0x00, sizeof(account_db_dir));
339 ACCOUNT_MEMSET(account_db_path, 0x00, sizeof(account_db_path));
341 ACCOUNT_GET_USER_DB_PATH(account_db_path, sizeof(account_db_path), uid);
344 _ERR( "Account database is using in another app. %x", g_hAccountDB );
345 return _ACCOUNT_ERROR_DATABASE_BUSY;
348 ret = _account_db_handle_close(g_hAccountDB2);
349 if( ret != _ACCOUNT_ERROR_NONE )
350 ACCOUNT_DEBUG( "db_util_close(g_hAccountDB2) fail ret = %d", ret);
352 ACCOUNT_GET_USER_DB_DIR(account_db_dir, sizeof(account_db_dir), uid);
354 if (mkdir(account_db_dir, 0777) != 0)
355 ACCOUNT_DEBUG("mkdir \"%s\" fail", account_db_dir);
358 ACCOUNT_DEBUG( "before db_util_open()");
359 // if(mode == ACCOUNT_DB_OPEN_READWRITE)
360 rc = db_util_open(account_db_path, &g_hAccountDB, DB_UTIL_REGISTER_HOOK_METHOD);
361 // else if(mode == ACCOUNT_DB_OPEN_READONLY)
362 // rc = db_util_open_with_options(account_db_path, &g_hAccountDB, SQLITE_OPEN_READONLY, NULL);
364 // return _ACCOUNT_ERROR_DB_NOT_OPENED;
365 ACCOUNT_DEBUG( "after db_util_open() sqlite_rc = %d", rc);
367 if( rc == SQLITE_PERM || _account_db_err_code(g_hAccountDB) == SQLITE_PERM ) {
368 ACCOUNT_ERROR( "Account permission denied");
369 return _ACCOUNT_ERROR_PERMISSION_DENIED;
372 if( rc == SQLITE_BUSY ) {
373 ACCOUNT_ERROR( "busy handler fail.");
374 return _ACCOUNT_ERROR_DATABASE_BUSY;
377 if( rc != SQLITE_OK ) {
378 ACCOUNT_ERROR( "The database isn't connected." );
379 return _ACCOUNT_ERROR_DB_NOT_OPENED;
382 rc = _account_check_is_all_table_exists(g_hAccountDB);
385 _ERR("_account_check_is_all_table_exists rc=[%d]", rc);
387 } else if (rc == ACCOUNT_TABLE_TOTAL_COUNT) {
390 int ret = _account_create_all_tables(g_hAccountDB);
391 if (ret != _ACCOUNT_ERROR_NONE) {
392 _ERR("_account_create_all_tables fail ret=[%d]", ret);
397 _INFO( "end _account_db_open()");
398 return _ACCOUNT_ERROR_NONE;
401 int _account_db_close(void)
403 ACCOUNT_DEBUG( "start db_util_close()");
406 ret = _account_db_handle_close(g_hAccountDB2);
407 if( ret != _ACCOUNT_ERROR_NONE )
408 ACCOUNT_DEBUG( "db_util_close(g_hAccountDB2) fail ret = %d", ret);
410 ret = _account_db_handle_close(g_hAccountDB);
411 if( ret != _ACCOUNT_ERROR_NONE )
413 ACCOUNT_ERROR( "db_util_close(g_hAccountDB) fail ret = %d", ret);
414 g_hAccountDB2 = g_hAccountDB;
421 static int _account_execute_insert_query(account_s *account)
423 _INFO("_account_execute_insert_query start");
426 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
427 int error_code = _ACCOUNT_ERROR_NONE;
428 account_stmt hstmt = NULL;
430 /* check whether app id exist in account type db */
432 if (!account->user_name && !account->display_name && !account->email_address) {
434 ACCOUNT_ERROR("Mandetory fields is NULL. At least one field is required among username, display name, email address\n");
435 return _ACCOUNT_ERROR_INVALID_PARAMETER;
439 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
440 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (user_name, email_address , display_name , icon_path , source , package_name , "
441 "access_token , domain_name , auth_type , secret , sync_support , txt_custom0, txt_custom1, txt_custom2, txt_custom3, txt_custom4, "
442 "int_custom0, int_custom1, int_custom2, int_custom3, int_custom4, txt_custom0 ) values " // to do urusa
443 "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", ACCOUNT_TABLE);
445 hstmt = _account_prepare_query(g_hAccountDB, query);
446 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
449 _account_convert_account_to_sql(account, hstmt, query);
452 rc = _account_query_step(hstmt);
453 if (rc != SQLITE_DONE) {
455 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
457 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM )
458 error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
460 error_code = _ACCOUNT_ERROR_DB_FAILED;
464 rc = _account_query_finalize(hstmt);
465 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
468 _INFO("_account_execute_insert_query end");
472 static int _account_insert_capability(account_s *account, int account_id)
474 _INFO("_account_insert_capability start");
476 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
477 account_stmt hstmt = NULL;
479 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
481 if (g_slist_length( account->capablity_list)==0) {
482 ACCOUNT_DEBUG( "_account_insert_capability, no capability\n");
483 return _ACCOUNT_ERROR_NONE;
486 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
488 _INFO("_account_insert_capability _account_get_record_count [%s]", query);
489 rc = _account_get_record_count(g_hAccountDB, query);
491 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
492 _ERR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
493 return _ACCOUNT_ERROR_PERMISSION_DENIED;
496 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
503 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
507 account_capability_s* cap_data = NULL;
508 cap_data = (account_capability_s*)iter->data;
510 _INFO("cap_data->type = %s, cap_data->value = %d \n", cap_data->type, cap_data->value);
512 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
513 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
514 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
515 hstmt = _account_prepare_query(g_hAccountDB, query);
517 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
519 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
520 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
521 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
522 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
523 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
524 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
525 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
526 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
527 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
528 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
530 rc = _account_query_step(hstmt);
531 _INFO("_account_insert_capability _account_query_step[%d]", rc);
533 if (rc != SQLITE_DONE) {
534 _ERR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
538 rc = _account_query_finalize(hstmt);
539 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
544 _INFO("_account_insert_capability end");
545 return _ACCOUNT_ERROR_NONE;
548 static int _account_update_capability(account_s *account, int account_id)
551 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
552 account_stmt hstmt = NULL;
554 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
556 if (g_slist_length( account->capablity_list)==0) {
557 ACCOUNT_ERROR( "_account_update_capability, no capability\n");
558 return _ACCOUNT_ERROR_NONE;
561 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
563 rc = _account_get_record_count(g_hAccountDB, query);
566 ACCOUNT_SLOGI( "_account_update_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
567 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
570 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
572 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id=? ", CAPABILITY_TABLE);
573 hstmt = _account_prepare_query(g_hAccountDB, query);
575 _account_query_bind_int(hstmt, count++, (int)account_id);
576 rc = _account_query_step(hstmt);
578 if (rc != SQLITE_DONE) {
579 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
580 return _ACCOUNT_ERROR_DB_FAILED;
582 rc = _account_query_finalize(hstmt);
583 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
588 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
591 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
592 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
593 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
595 hstmt = _account_prepare_query(g_hAccountDB, query);
597 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
599 account_capability_s* cap_data = NULL;
600 cap_data = (account_capability_s*)iter->data;
602 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
603 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
604 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
605 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
606 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
607 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
608 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
609 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
610 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
611 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
613 rc = _account_query_step(hstmt);
615 if (rc != SQLITE_DONE) {
616 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
620 rc = _account_query_finalize(hstmt);
621 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
626 return _ACCOUNT_ERROR_NONE;
629 static int _account_update_capability_by_user_name(account_s *account, const char *user_name, const char *package_name )
632 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
633 account_stmt hstmt = NULL;
635 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
637 if (g_slist_length( account->capablity_list)==0) {
638 ACCOUNT_ERROR( "_account_update_capability_by_user_name, no capability\n");
639 return _ACCOUNT_ERROR_NONE;
642 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where package_name= '%s' and user_name='%s'", ACCOUNT_TABLE, package_name, user_name);
644 rc = _account_get_record_count(g_hAccountDB, query);
647 ACCOUNT_SLOGI( "_account_update_capability_by_user_name : related account item is not existed rc=%d , %s ", rc, _account_db_err_msg(g_hAccountDB));
648 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
651 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
653 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name=? and user_name=? ", CAPABILITY_TABLE);
654 hstmt = _account_prepare_query(g_hAccountDB, query);
656 _account_query_bind_text(hstmt, count++, (char*)account->package_name);
657 _account_query_bind_text(hstmt, count++, (char*)account->user_name);
658 rc = _account_query_step(hstmt);
659 if (rc != SQLITE_DONE) {
660 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
661 return _ACCOUNT_ERROR_DB_FAILED;
664 rc = _account_query_finalize(hstmt);
665 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
670 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
673 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
674 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
675 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
677 hstmt = _account_prepare_query(g_hAccountDB, query);
679 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
681 account_capability_s* cap_data = NULL;
682 cap_data = (account_capability_s*)iter->data;
684 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
685 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
686 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
687 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
688 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
689 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
690 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
691 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
692 ret = _account_query_bind_int(hstmt, count++, (int)account->id);
693 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
695 rc = _account_query_step(hstmt);
697 if (rc != SQLITE_DONE) {
698 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
702 rc = _account_query_finalize(hstmt);
703 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
708 return _ACCOUNT_ERROR_NONE;
712 bool _account_add_capability_to_account_cb(const char* capability_type, int capability_value, account_s *account)
714 account_capability_s *cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
716 if (cap_data == NULL)
718 ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
720 cap_data->type = _account_dup_text(capability_type);
721 cap_data->value = capability_value;
722 _INFO("cap_data->type = %s, cap_data->value = %d", cap_data->type, cap_data->value);
724 account->capablity_list = g_slist_append(account->capablity_list, (gpointer)cap_data);
730 bool _account_add_custom_to_account_cb(const char* key, const char* value, account_s *account)
732 account_custom_s *custom_data = (account_custom_s*)malloc(sizeof(account_custom_s));
734 if (custom_data == NULL) {
735 ACCOUNT_DEBUG("_account_add_custom_to_account_cb :: malloc fail\n");
738 ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s));
740 custom_data->account_id = account->id;
741 custom_data->app_id = _account_dup_text(account->package_name);
742 custom_data->key = _account_dup_text(key);
743 custom_data->value = _account_dup_text(value);
744 _INFO("custom_data->key = %s, custom_data->value = %s", custom_data->key, custom_data->value);
746 account->custom_list = g_slist_append(account->custom_list, (gpointer)custom_data);
752 static int _account_compare_old_record_by_user_name(account_s *new_account, const char* user_name, const char* package_name)
754 int error_code = _ACCOUNT_ERROR_NONE;
755 account_stmt hstmt = NULL;
756 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
758 account_s *old_account = NULL;
760 ACCOUNT_RETURN_VAL((new_account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
761 ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
762 ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
763 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
765 old_account = (account_s*)calloc(1, sizeof(account_s));
767 ACCOUNT_FATAL("Memory alloc fail\n");
768 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
771 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
773 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = '%s' and package_name='%s'", ACCOUNT_TABLE, user_name, package_name);
774 hstmt = _account_prepare_query(g_hAccountDB, query);
776 rc = _account_query_step(hstmt);
777 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
779 while (rc == SQLITE_ROW) {
780 _account_convert_column_to_account(hstmt, old_account);
781 rc = _account_query_step(hstmt);
784 rc = _account_query_finalize(hstmt);
785 ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
789 error_code = _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, old_account->id, (void*)old_account);
790 ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
793 error_code = _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, old_account->id, (void*)old_account);
794 ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
797 new_account->id = old_account->id;
800 if(!new_account->user_name) {
801 if(old_account->user_name)
802 new_account->user_name = _account_dup_text(old_account->user_name);
806 if(!new_account->display_name) {
807 if(old_account->display_name)
808 new_account->display_name = _account_dup_text(old_account->display_name);
812 if(!new_account->email_address) {
813 if(old_account->email_address)
814 new_account->email_address = _account_dup_text(old_account->email_address);
818 if(!new_account->domain_name) {
819 if(old_account->domain_name)
820 new_account->domain_name = _account_dup_text(old_account->domain_name);
824 if(!new_account->icon_path) {
825 if(old_account->icon_path)
826 new_account->icon_path = _account_dup_text(old_account->icon_path);
830 if(!new_account->source) {
831 if(old_account->source)
832 new_account->source = _account_dup_text(old_account->source);
835 _ACCOUNT_FREE(new_account->package_name);
836 new_account->package_name = _account_dup_text(old_account->package_name);
839 if(!new_account->access_token) {
840 if(old_account->access_token)
841 new_account->access_token = _account_dup_text(old_account->access_token);
845 if(new_account->auth_type == _ACCOUNT_AUTH_TYPE_INVALID) {
846 new_account->auth_type = old_account->auth_type;
850 if(new_account->secret== _ACCOUNT_SECRECY_INVALID) {
851 new_account->secret = old_account->secret;
855 if(new_account->sync_support == _ACCOUNT_SYNC_INVALID) {
856 new_account->sync_support = old_account->sync_support;
861 for(i=0;i<USER_TXT_CNT;i++) {
862 if(!new_account->user_data_txt[i]) {
863 if(old_account->user_data_txt[i])
864 new_account->user_data_txt[i] = _account_dup_text(old_account->user_data_txt[i]);
869 for(i=0;i<USER_INT_CNT;i++) {
870 if(new_account->user_data_int[i] == 0) {
871 new_account->user_data_int[i] = old_account->user_data_int[i];
881 _account_free_account_with_items(old_account);
885 rc = _account_query_finalize(hstmt);
886 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
890 return _ACCOUNT_ERROR_NONE;
895 static int _account_update_account_by_user_name(int pid, uid_t uid, account_s *account, const char *user_name, const char *package_name)
897 int rc = 0, binding_count = 0, count = 0;
898 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
899 int error_code = _ACCOUNT_ERROR_NONE;
900 account_stmt hstmt = NULL;
902 ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is NULL.\n"));
903 ACCOUNT_RETURN_VAL((package_name!= NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is NULL.\n"));
905 char* current_appid = NULL;
906 char* verified_appid = NULL;
908 current_appid = _account_get_current_appid(pid, uid);
909 error_code = _account_get_represented_appid_from_db(g_hAccountDB, g_hAccountGlobalDB, current_appid, uid, &verified_appid);
911 _ACCOUNT_FREE(current_appid);
912 _ACCOUNT_FREE(verified_appid);
914 if(error_code != _ACCOUNT_ERROR_NONE){
915 ACCOUNT_ERROR("No permission to update\n");
916 return _ACCOUNT_ERROR_PERMISSION_DENIED;
919 error_code = encrypt_access_token(account);
920 if (error_code != _ACCOUNT_ERROR_NONE)
922 _ERR("_encrypt_access_token error");
926 _account_compare_old_record_by_user_name(account, user_name, package_name);
928 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
929 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
930 return _ACCOUNT_ERROR_PERMISSION_DENIED;
933 if (!account->package_name) {
934 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
935 return _ACCOUNT_ERROR_INVALID_PARAMETER;
938 if (!account->user_name && !account->display_name && !account->email_address) {
939 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
940 return _ACCOUNT_ERROR_INVALID_PARAMETER;
943 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE user_name='%s' and package_name='%s'"
944 , ACCOUNT_TABLE, user_name, package_name);
946 count = _account_get_record_count(g_hAccountDB, query);
948 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
949 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
950 return _ACCOUNT_ERROR_PERMISSION_DENIED;
954 ACCOUNT_SLOGI("_account_update_account_by_user_name : The account not exist!, count = %d, user_name=%s, package_name=%s\n",
955 count, user_name, package_name);
956 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
959 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
961 //TODO: Is it required to update id ? As of now I can only think of falied rollback cases (between account and gSSO DB)
962 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
963 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
964 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
965 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE user_name=? and package_name=? ", ACCOUNT_TABLE);
967 hstmt = _account_prepare_query(g_hAccountDB, query);
968 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
969 _account_end_transaction(g_hAccountDB, FALSE);
970 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
971 return _ACCOUNT_ERROR_PERMISSION_DENIED;
973 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
975 binding_count = _account_convert_account_to_sql(account, hstmt, query);
977 _account_query_bind_text(hstmt, binding_count++, user_name);
978 _account_query_bind_text(hstmt, binding_count++, package_name);
979 rc = _account_query_step(hstmt);
980 if (rc != SQLITE_DONE) {
981 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
983 rc = _account_query_finalize(hstmt);
984 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
987 /*update capability*/
988 error_code = _account_update_capability_by_user_name(account, user_name, package_name);
991 error_code = _account_update_custom(account, account->id);
996 int _account_insert_to_db(account_s* account, int pid, uid_t uid, int *account_id)
999 int error_code = _ACCOUNT_ERROR_NONE;
1000 int ret_transaction = 0;
1002 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1003 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1004 ACCOUNT_RETURN_VAL((account_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID POINTER IS NULL"));
1006 if (!account->user_name && !account->display_name && !account->email_address) {
1007 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1008 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1011 account_s *data = (account_s*)account;
1012 ACCOUNT_SLOGD("(%s)-(%d) account_insert_to_db: begin_transaction.\n", __FUNCTION__, __LINE__);
1014 pthread_mutex_lock(&account_mutex);
1016 /* transaction control required*/
1017 ret_transaction = _account_begin_transaction(g_hAccountDB);
1019 if(_account_db_err_code(g_hAccountDB) == SQLITE_PERM){
1020 pthread_mutex_unlock(&account_mutex);
1021 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1022 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1025 if (ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY) {
1026 ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction);
1027 pthread_mutex_unlock(&account_mutex);
1028 return _ACCOUNT_ERROR_DATABASE_BUSY;
1029 }else if (ret_transaction != _ACCOUNT_ERROR_NONE) {
1030 ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction);
1031 pthread_mutex_unlock(&account_mutex);
1032 return ret_transaction;
1035 *account_id = _account_get_next_sequence(g_hAccountDB, ACCOUNT_TABLE);
1036 data->id = *account_id;
1039 appid = _account_get_current_appid(pid, uid);
1044 // API caller cannot be recognized
1045 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1046 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1047 pthread_mutex_unlock(&account_mutex);
1048 return _ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
1052 char* verified_appid = NULL;
1053 error_code = _account_get_represented_appid_from_db(g_hAccountDB, g_hAccountGlobalDB, appid, uid, &verified_appid);//FIX
1054 _ACCOUNT_FREE(appid);
1055 if(error_code != _ACCOUNT_ERROR_NONE)
1057 _ERR("error_code = %d", error_code);
1058 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1059 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1060 _ACCOUNT_FREE(verified_appid);
1061 pthread_mutex_unlock(&account_mutex);
1068 error_code = _account_check_duplicated(g_hAccountDB, data, verified_appid, uid);
1069 if (error_code != _ACCOUNT_ERROR_NONE) {
1071 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1072 ACCOUNT_DEBUG("_account_check_duplicated(), rollback insert query(%x)!!!!\n", ret_transaction);
1074 pthread_mutex_unlock(&account_mutex);
1077 if(!_account_check_add_more_account(verified_appid)) {
1078 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1079 ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
1080 pthread_mutex_unlock(&account_mutex);
1081 _ACCOUNT_FREE(verified_appid);
1082 return _ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
1085 _ACCOUNT_FREE(data->package_name);
1086 data->package_name = _account_dup_text(verified_appid);
1087 _ACCOUNT_FREE(verified_appid);
1090 if(!_account_check_add_more_account(data->package_name))
1093 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1094 ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
1095 pthread_mutex_unlock(&account_mutex);
1096 return _ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
1099 error_code = encrypt_access_token(data);
1100 if (error_code != _ACCOUNT_ERROR_NONE)
1102 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1103 ACCOUNT_ERROR("encrypt_access_token fail, rollback insert query(%x)!!!!\n", ret_transaction);
1105 pthread_mutex_unlock(&account_mutex);
1109 error_code = _account_execute_insert_query(data);
1111 if (error_code != _ACCOUNT_ERROR_NONE)
1114 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1115 ACCOUNT_ERROR("INSERT account fail, rollback insert query(%x)!!!!\n", ret_transaction);
1117 pthread_mutex_unlock(&account_mutex);
1122 error_code = _account_insert_capability(data, *account_id);
1123 if (error_code != _ACCOUNT_ERROR_NONE)
1126 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1127 ACCOUNT_ERROR("INSERT capability fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1129 pthread_mutex_unlock(&account_mutex);
1134 error_code = _account_insert_custom(data, *account_id);
1135 if (error_code != _ACCOUNT_ERROR_NONE)
1137 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1138 ACCOUNT_ERROR("INSERT custom fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1140 pthread_mutex_unlock(&account_mutex);
1146 pthread_mutex_unlock(&account_mutex);
1147 _account_end_transaction(g_hAccountDB, TRUE);
1148 ACCOUNT_SLOGD("(%s)-(%d) account _end_transaction.\n", __FUNCTION__, __LINE__);
1151 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_INSERT, *account_id);
1152 _account_insert_delete_update_notification_send(buf);
1153 _INFO("account _notification_send end.");
1155 return _ACCOUNT_ERROR_NONE;
1159 GSList* _account_get_capability_list_by_account_id(int account_id, int *error_code)
1161 *error_code = _ACCOUNT_ERROR_NONE;
1162 account_stmt hstmt = NULL;
1163 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1165 GSList* capability_list = NULL;
1167 ACCOUNT_RETURN_VAL((account_id > 0), {*error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("ACCOUNT INDEX IS LESS THAN 0"));
1168 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
1170 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1172 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
1173 hstmt = _account_prepare_query(g_hAccountDB, query);
1175 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
1176 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1177 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
1181 rc = _account_query_step(hstmt);
1182 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1184 account_capability_s* capability_record = NULL;
1186 while (rc == SQLITE_ROW) {
1187 capability_record = (account_capability_s*) malloc(sizeof(account_capability_s));
1189 if (capability_record == NULL) {
1190 ACCOUNT_FATAL("malloc Failed");
1194 ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s));
1196 _account_convert_column_to_capability(hstmt, capability_record);
1198 //cb_ret = callback(capability_record->type, capability_record->value, user_data);
1200 //_account_free_capability_items(capability_record);
1201 //_ACCOUNT_FREE(capability_record);
1203 //ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
1205 capability_list = g_slist_append(capability_list, capability_record);
1206 rc = _account_query_step(hstmt);
1209 rc = _account_query_finalize(hstmt);
1210 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
1213 *error_code = _ACCOUNT_ERROR_NONE;
1218 rc = _account_query_finalize(hstmt);
1219 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
1223 pthread_mutex_unlock(&account_mutex);
1224 return capability_list;
1227 static int _account_compare_old_record(account_s *new_account, int account_id)
1229 int error_code = _ACCOUNT_ERROR_NONE;
1230 account_stmt hstmt = NULL;
1231 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1233 account_s *old_account = NULL;
1235 ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1236 ACCOUNT_RETURN_VAL((new_account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
1237 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1239 old_account = (account_s*)calloc(1, sizeof(account_s));
1240 if (old_account == NULL) {
1241 _ERR("Out of Memory");
1242 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
1245 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1247 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
1248 hstmt = _account_prepare_query(g_hAccountDB, query);
1250 rc = _account_query_step(hstmt);
1251 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1253 while (rc == SQLITE_ROW) {
1254 _account_convert_column_to_account(hstmt, old_account);
1255 rc = _account_query_step(hstmt);
1258 rc = _account_query_finalize(hstmt);
1259 ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1263 error_code = _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, old_account->id, (void*)old_account);
1264 ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
1267 error_code = _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, old_account->id, (void*)old_account);
1268 ACCOUNT_CATCH_ERROR((error_code == _ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
1272 new_account->id = old_account->id;
1275 if(!new_account->user_name) {
1276 if(old_account->user_name)
1277 new_account->user_name = _account_dup_text(old_account->user_name);
1281 if(!new_account->display_name) {
1282 if(old_account->display_name)
1283 new_account->display_name = _account_dup_text(old_account->display_name);
1287 if(!new_account->email_address) {
1288 if(old_account->email_address)
1289 new_account->email_address = _account_dup_text(old_account->email_address);
1293 if(!new_account->domain_name) {
1294 if(old_account->domain_name)
1295 new_account->domain_name = _account_dup_text(old_account->domain_name);
1299 if(!new_account->icon_path) {
1300 if(old_account->icon_path)
1301 new_account->icon_path = _account_dup_text(old_account->icon_path);
1305 if(!new_account->source) {
1306 if(old_account->source)
1307 new_account->source = _account_dup_text(old_account->source);
1310 _ACCOUNT_FREE(new_account->package_name);
1311 new_account->package_name = _account_dup_text(old_account->package_name);
1314 if(!new_account->access_token) {
1315 if(old_account->access_token)
1316 new_account->access_token = _account_dup_text(old_account->access_token);
1321 for(i=0;i<USER_TXT_CNT;i++) {
1322 if(!new_account->user_data_txt[i]) {
1323 if(old_account->user_data_txt[i])
1324 new_account->user_data_txt[i] = _account_dup_text(old_account->user_data_txt[i]);
1329 if(new_account->auth_type == _ACCOUNT_AUTH_TYPE_INVALID) {
1330 new_account->auth_type = old_account->auth_type;
1334 if(new_account->secret== _ACCOUNT_SECRECY_INVALID) {
1335 new_account->secret = old_account->secret;
1339 if(new_account->sync_support == _ACCOUNT_SYNC_INVALID) {
1340 new_account->sync_support = old_account->sync_support;
1344 for(i=0;i<USER_INT_CNT;i++) {
1345 if(new_account->user_data_int[i] == 0) {
1346 new_account->user_data_int[i] = old_account->user_data_int[i];
1352 // user custom table
1356 _account_free_account_with_items(old_account);
1358 if (hstmt != NULL) {
1359 rc = _account_query_finalize(hstmt);
1360 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1364 return _ACCOUNT_ERROR_NONE;
1367 static int _account_get_package_name_from_account_id(int account_id, char **package_name)
1369 int error_code = _ACCOUNT_ERROR_NONE;
1370 account_stmt hstmt = NULL;
1371 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1373 account_s *old_account = NULL;
1375 ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1376 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1378 old_account = (account_s*)calloc(1, sizeof(account_s));
1379 if (old_account == NULL) {
1380 _ERR("Out Of memory");
1381 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
1384 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1386 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
1387 hstmt = _account_prepare_query(g_hAccountDB, query);
1389 rc = _account_query_step(hstmt);
1390 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1392 while (rc == SQLITE_ROW) {
1393 _account_convert_column_to_account(hstmt, old_account);
1394 rc = _account_query_step(hstmt);
1397 rc = _account_query_finalize(hstmt);
1398 ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1401 // get package name.
1402 *package_name = _account_dup_text(old_account->package_name);
1407 _account_free_account_with_items(old_account);
1410 if (hstmt != NULL) {
1411 rc = _account_query_finalize(hstmt);
1412 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1420 static int _account_update_account(int pid, uid_t uid, account_s *account, int account_id)
1422 int rc = 0, binding_count =0;
1423 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1424 int error_code = _ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0;
1425 account_stmt hstmt = NULL;
1427 if (!account->package_name) {
1428 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
1429 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1432 /* Check permission of requested appid */
1433 char* current_appid = NULL;
1434 char *package_name = NULL;
1436 current_appid = _account_get_current_appid(pid, uid);
1437 error_code = _account_get_package_name_from_account_id(account_id, &package_name);
1439 if(error_code != _ACCOUNT_ERROR_NONE || package_name == NULL){
1440 ACCOUNT_ERROR("No package name with account_id\n");
1441 _ACCOUNT_FREE(current_appid);
1442 _ACCOUNT_FREE(package_name);
1443 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1446 error_code = _account_check_appid_group_with_package_name(current_appid, package_name, uid);
1447 ACCOUNT_DEBUG( "UPDATE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name); // TODO: remove the log later.
1449 _ACCOUNT_FREE(current_appid);
1450 _ACCOUNT_FREE(package_name);
1452 if(error_code != _ACCOUNT_ERROR_NONE){
1453 ACCOUNT_ERROR("No permission to update\n");
1454 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1457 error_code = encrypt_access_token(account);
1458 if (error_code != _ACCOUNT_ERROR_NONE)
1460 _ERR("_encrypt_access_token error");
1464 error_code = _account_compare_old_record(account, account_id);
1465 if (error_code != _ACCOUNT_ERROR_NONE) {
1466 ACCOUNT_ERROR("_account_compare_old_record fail\n");
1470 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
1471 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1472 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1473 } else if( _account_db_err_code(g_hAccountDB) == SQLITE_BUSY ){
1474 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg(g_hAccountDB));
1475 return _ACCOUNT_ERROR_DATABASE_BUSY;
1478 if (!account->user_name && !account->display_name && !account->email_address) {
1479 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1480 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1483 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1485 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
1487 count = _account_get_record_count(g_hAccountDB, query);
1489 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
1490 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1493 /* transaction control required*/
1494 ret_transaction = _account_begin_transaction(g_hAccountDB);
1495 if( ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY ){
1496 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg(g_hAccountDB));
1497 pthread_mutex_unlock(&account_mutex);
1498 return _ACCOUNT_ERROR_DATABASE_BUSY;
1501 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1502 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
1503 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
1504 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
1505 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
1507 hstmt = _account_prepare_query(g_hAccountDB, query);
1509 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
1510 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1511 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1512 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1515 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)));
1517 binding_count = _account_convert_account_to_sql(account, hstmt, query);
1518 _account_query_bind_int(hstmt, binding_count++, account_id);
1520 rc = _account_query_step(hstmt);
1521 if (rc != SQLITE_DONE) {
1522 ACCOUNT_SLOGE( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
1525 rc = _account_query_finalize(hstmt);
1526 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1529 _INFO("update query=%s", query);
1531 /*update capability*/
1532 error_code = _account_update_capability(account, account_id);
1533 if(error_code != _ACCOUNT_ERROR_NONE && error_code!= _ACCOUNT_ERROR_RECORD_NOT_FOUND){
1534 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1535 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1540 error_code = _account_update_custom(account, account_id);
1541 if(error_code != _ACCOUNT_ERROR_NONE && error_code!= _ACCOUNT_ERROR_RECORD_NOT_FOUND){
1542 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1543 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1547 ret_transaction = _account_end_transaction(g_hAccountDB, TRUE);
1549 _INFO("update end");
1554 static int _account_update_account_ex(account_s *account, int account_id)
1556 int rc = 0, binding_count =0;
1557 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1558 int error_code = _ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0;
1559 account_stmt hstmt = NULL;
1561 if (!account->package_name) {
1562 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
1563 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1566 error_code = encrypt_access_token(account);
1567 if (error_code != _ACCOUNT_ERROR_NONE)
1569 _ERR("_encrypt_access_token error");
1573 error_code = _account_compare_old_record(account, account_id);
1574 if (error_code != _ACCOUNT_ERROR_NONE) {
1575 ACCOUNT_ERROR("_account_compare_old_record fail\n");
1579 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
1580 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1581 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1584 if (!account->user_name && !account->display_name && !account->email_address) {
1585 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1586 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1589 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1591 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
1593 count = _account_get_record_count(g_hAccountDB, query);
1595 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
1596 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1599 /* transaction control required*/
1600 ret_transaction = _account_begin_transaction(g_hAccountDB);
1601 if( ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY ){
1602 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg(g_hAccountDB));
1603 pthread_mutex_unlock(&account_mutex);
1604 return _ACCOUNT_ERROR_DATABASE_BUSY;
1607 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1608 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
1609 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
1610 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
1611 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
1613 hstmt = _account_prepare_query(g_hAccountDB, query);
1615 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
1616 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1617 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1618 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1621 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)));
1623 _INFO("account_update_to_db_by_id_ex_p : before convert() : account_id[%d], user_name=%s", account->id, account->user_name);
1624 binding_count = _account_convert_account_to_sql(account, hstmt, query);
1625 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], user_name=%s", account->id, account->user_name);
1626 _INFO("account_update_to_db_by_id_ex_p : before bind()");
1627 rc = _account_query_bind_int(hstmt, binding_count++, account_id);
1628 _INFO("account_update_to_db_by_id_ex_p : after bind() : ret = %d", rc);
1630 rc = _account_query_step(hstmt);
1631 if (rc != SQLITE_DONE) {
1632 ACCOUNT_SLOGE( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
1634 _INFO("account_update_to_db_by_id_ex_p : after query_step() : ret = %d", rc);
1636 rc = _account_query_finalize(hstmt);
1637 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1639 _INFO("account_update_to_db_by_id_ex_p : after query_filnalize() : ret = %d", rc);
1641 _INFO("account_update_to_db_by_id_ex_p : before update_capability()");
1642 /*update capability*/
1643 error_code = _account_update_capability(account, account_id);
1644 if(error_code != _ACCOUNT_ERROR_NONE && error_code!= _ACCOUNT_ERROR_RECORD_NOT_FOUND){
1645 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1646 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1649 _INFO("account_update_to_db_by_id_ex_p : after update_capability()");
1651 _INFO("account_update_to_db_by_id_ex_p : before update_custom()");
1653 error_code = _account_update_custom(account, account_id);
1654 if(error_code != _ACCOUNT_ERROR_NONE && error_code!= _ACCOUNT_ERROR_RECORD_NOT_FOUND){
1655 ret_transaction = _account_end_transaction(g_hAccountDB, FALSE);
1656 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
1659 _INFO("account_update_to_db_by_id_ex_p : after update_custom()");
1661 ret_transaction = _account_end_transaction(g_hAccountDB, TRUE);
1667 int _account_update_to_db_by_id(int pid, uid_t uid, account_s* account, int account_id)
1669 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
1670 ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
1671 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1672 int error_code = _ACCOUNT_ERROR_NONE;
1673 account_s* data = (account_s*)account;
1675 pthread_mutex_lock(&account_mutex);
1677 error_code = _account_update_account(pid, uid, data, account_id);
1679 if(error_code != _ACCOUNT_ERROR_NONE) {
1680 pthread_mutex_unlock(&account_mutex);
1684 pthread_mutex_unlock(&account_mutex);
1687 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_UPDATE, account_id);
1688 _account_insert_delete_update_notification_send(buf);
1690 return _ACCOUNT_ERROR_NONE;
1693 int _account_update_to_db_by_id_ex(account_s* account, int account_id)
1695 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
1696 ACCOUNT_RETURN_VAL((account_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
1697 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1698 int error_code = _ACCOUNT_ERROR_NONE;
1699 account_s* data = account;
1701 pthread_mutex_lock(&account_mutex);
1703 _INFO("before update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name);
1704 error_code = _account_update_account_ex(data, account_id);
1705 _INFO("after update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name);
1707 if(error_code != _ACCOUNT_ERROR_NONE) {
1708 pthread_mutex_unlock(&account_mutex);
1712 pthread_mutex_unlock(&account_mutex);
1715 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_UPDATE, account_id);
1716 _account_insert_delete_update_notification_send(buf);
1718 return _ACCOUNT_ERROR_NONE;
1722 int _account_update_to_db_by_user_name(int pid, uid_t uid, account_s* account, const char *user_name, const char *package_name)
1724 ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
1725 ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
1726 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1728 int error_code = _ACCOUNT_ERROR_NONE;
1729 account_s *data = (account_s*)account;
1731 pthread_mutex_lock(&account_mutex);
1733 error_code = _account_update_account_by_user_name(pid, uid, data, user_name, package_name);
1735 pthread_mutex_unlock(&account_mutex);
1738 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_UPDATE, data->id);
1739 _account_insert_delete_update_notification_send(buf);
1744 GSList* _account_db_query_all(int pid, uid_t uid)
1746 //int error_code = _ACCOUNT_ERROR_NONE;
1747 account_stmt hstmt = NULL;
1748 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1750 GSList *account_list = NULL;
1752 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected."));
1754 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1756 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TABLE);
1757 hstmt = _account_prepare_query(g_hAccountDB, query);
1759 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
1760 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1764 rc = _account_query_step(hstmt);
1766 account_s *account_record = NULL;
1768 if (rc != SQLITE_ROW)
1770 _ERR("The record isn't found");
1774 while(rc == SQLITE_ROW) {
1775 account_record = (account_s*) malloc(sizeof(account_s));
1777 if (account_record == NULL) {
1778 ACCOUNT_FATAL("malloc Failed");
1782 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
1783 _account_convert_column_to_account(hstmt, account_record);
1784 account_list = g_slist_append(account_list, account_record);
1785 rc = _account_query_step(hstmt);
1788 rc = _account_query_finalize(hstmt);
1789 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, NULL, ("finalize error"));
1794 for (iter = account_list; iter != NULL; iter = g_slist_next(iter)) {
1795 account_s *account = NULL;
1796 account = (account_s*)iter->data;
1797 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, account->id, (void*)account);
1798 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, account->id, (void*)account);
1802 if (hstmt != NULL) {
1803 rc = _account_query_finalize(hstmt);
1804 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_gslist_account_free(account_list);}, NULL, ("finalize error"));
1809 _remove_sensitive_info_from_non_owning_account_slist(account_list, pid, uid);
1811 return account_list;
1814 int _account_update_sync_status_by_id(uid_t uid, int account_db_id, const int sync_status)
1816 int error_code = _ACCOUNT_ERROR_NONE;
1817 account_stmt hstmt = NULL;
1818 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1822 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1823 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1824 if ( (sync_status < 0) || (sync_status >= _ACCOUNT_SYNC_MAX)) {
1825 ACCOUNT_SLOGE("(%s)-(%d) sync_status is less than 0 or more than enum max.\n", __FUNCTION__, __LINE__);
1826 return _ACCOUNT_ERROR_INVALID_PARAMETER;
1829 pthread_mutex_lock(&account_mutex);
1831 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1833 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_db_id);
1835 rc = _account_get_record_count(g_hAccountDB, query);
1837 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
1838 pthread_mutex_unlock(&account_mutex);
1839 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1840 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1844 ACCOUNT_SLOGE( "account_update_sync_status_by_id : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
1845 pthread_mutex_unlock(&account_mutex);
1846 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
1849 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1851 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET sync_support=? WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
1852 hstmt = _account_prepare_query(g_hAccountDB, query);
1854 _account_query_bind_int(hstmt, count, sync_status);
1856 rc = _account_query_step(hstmt);
1858 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
1859 pthread_mutex_unlock(&account_mutex);
1860 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1861 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1864 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_DB_FAILED,
1865 ("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB)));
1867 rc = _account_query_finalize(hstmt);
1868 if (rc != _ACCOUNT_ERROR_NONE) {
1869 ACCOUNT_ERROR("_account_query_finalize error");
1870 pthread_mutex_unlock(&account_mutex);
1874 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_SYNC_UPDATE, account_db_id);
1875 _account_insert_delete_update_notification_send(buf);
1878 error_code = _ACCOUNT_ERROR_NONE;
1881 if (hstmt != NULL) {
1882 rc = _account_query_finalize(hstmt);
1883 pthread_mutex_unlock(&account_mutex);
1884 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1888 pthread_mutex_unlock(&account_mutex);
1892 int _account_query_account_by_account_id(int pid, uid_t uid, int account_db_id, account_s *account_record)
1894 _INFO("_account_query_account_by_account_id() start, account_db_id=[%d]", account_db_id);
1896 int error_code = _ACCOUNT_ERROR_NONE;
1897 account_stmt hstmt = NULL;
1898 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1901 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1902 ACCOUNT_RETURN_VAL(account_record != NULL, {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
1903 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1905 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1907 ACCOUNT_DEBUG("starting db operations");
1909 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
1910 hstmt = _account_prepare_query(g_hAccountDB, query);
1911 rc = _account_db_err_code(g_hAccountDB);
1912 _INFO("after _account_prepare_query, rc=[%d]", rc);
1914 if( rc == SQLITE_PERM ){
1915 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1916 return _ACCOUNT_ERROR_PERMISSION_DENIED;
1919 ACCOUNT_DEBUG("before _account_query_step");
1920 rc = _account_query_step(hstmt);
1921 ACCOUNT_DEBUG("after _account_query_step returned [%d]", rc);
1922 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1924 while (rc == SQLITE_ROW) {
1925 ACCOUNT_DEBUG("before _account_convert_column_to_account");
1926 _account_convert_column_to_account(hstmt, account_record);
1927 ACCOUNT_DEBUG("after _account_convert_column_to_account");
1928 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]);
1929 rc = _account_query_step(hstmt);
1932 ACCOUNT_DEBUG("account_record->id=[%d]", account_record->id);
1934 rc = _account_query_finalize(hstmt);
1935 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1937 ACCOUNT_DEBUG("before _account_query_capability_by_account_id");
1938 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, account_record->id, (void*)account_record);
1939 ACCOUNT_DEBUG("after _account_query_capability_by_account_id");
1941 ACCOUNT_DEBUG("before _account_query_custom_by_account_id");
1942 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, account_record->id, (void*)account_record);
1943 ACCOUNT_DEBUG("after _account_query_custom_by_account_id");
1946 error_code = _ACCOUNT_ERROR_NONE;
1949 if (hstmt != NULL) {
1950 rc = _account_query_finalize(hstmt);
1951 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1957 _remove_sensitive_info_from_non_owning_account(account_record, pid, uid);
1959 pthread_mutex_unlock(&account_mutex);
1960 ACCOUNT_DEBUG("_account_query_account_by_account_id end [%d]", error_code);
1964 GList* _account_query_account_by_user_name(int pid, uid_t uid, const char *user_name, int *error_code)
1966 *error_code = _ACCOUNT_ERROR_NONE;
1967 account_stmt hstmt = NULL;
1968 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1970 account_s *account_head = NULL;
1972 if (user_name == NULL)
1974 _ERR("USER NAME IS NULL");
1975 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
1979 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1981 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ?", ACCOUNT_TABLE);
1983 hstmt = _account_prepare_query(g_hAccountDB, query);
1985 if (_account_db_err_code(g_hAccountDB) == SQLITE_PERM)
1987 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
1988 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
1992 int binding_count = 1;
1993 _account_query_bind_text(hstmt, binding_count++, user_name);
1995 rc = _account_query_step(hstmt);
1997 if (rc != SQLITE_ROW)
1999 _ERR("The record isn't found");
2000 *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
2006 account_head = (account_s*) malloc(sizeof(account_s));
2007 if (account_head == NULL) {
2008 ACCOUNT_FATAL("malloc Failed");
2009 if (hstmt != NULL) {
2010 rc = _account_query_finalize(hstmt);
2012 if (rc != _ACCOUNT_ERROR_NONE)
2014 _ERR("finalize error");
2020 *error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
2023 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2025 while (rc == SQLITE_ROW) {
2026 account_s* account_record = NULL;
2028 account_record = (account_s*) malloc(sizeof(account_s));
2030 if (account_record == NULL) {
2031 ACCOUNT_FATAL("malloc Failed");
2034 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2036 _account_convert_column_to_account(hstmt, account_record);
2038 account_head->account_list = g_list_append(account_head->account_list, account_record);
2040 rc = _account_query_step(hstmt);
2044 rc = _account_query_finalize(hstmt);
2046 if (rc != _ACCOUNT_ERROR_NONE)
2048 _ERR("finalize error");
2058 tmp = g_list_length(account_head->account_list);
2060 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2061 // account_h account;
2062 // account = (account_h)iter->data;
2064 account_s *testaccount = (account_s*)iter->data;
2066 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, testaccount->id, (void*)testaccount);
2067 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, testaccount->id, (void*)testaccount);
2071 *error_code = _ACCOUNT_ERROR_NONE;
2074 if (hstmt != NULL) {
2075 rc = _account_query_finalize(hstmt);
2076 if (rc != _ACCOUNT_ERROR_NONE)
2078 _ERR("finalize error");
2084 pthread_mutex_unlock(&account_mutex);
2087 _remove_sensitive_info_from_non_owning_account_list(account_head->account_list, pid, uid);
2088 GList* result = account_head->account_list;
2089 _ACCOUNT_FREE(account_head);
2096 _account_query_account_by_capability(int pid, uid_t uid, const char* capability_type, const int capability_value, int *error_code)
2098 *error_code = _ACCOUNT_ERROR_NONE;
2099 account_stmt hstmt = NULL;
2100 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2103 ACCOUNT_RETURN_VAL((capability_type != NULL), {*error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("capability_type IS NULL"));
2105 if ((capability_value < 0) || (capability_value >= _ACCOUNT_CAPABILITY_STATE_MAX)) {
2106 ACCOUNT_SLOGE("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
2107 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
2111 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
2113 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2115 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=? AND value=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
2117 hstmt = _account_prepare_query(g_hAccountDB, query);
2119 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
2120 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2121 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
2125 int binding_count = 1;
2126 _account_query_bind_text(hstmt, binding_count++, capability_type);
2127 _account_query_bind_int(hstmt, binding_count++, capability_value);
2129 rc = _account_query_step(hstmt);
2131 account_s* account_head = NULL;
2133 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2137 account_head = (account_s*) malloc(sizeof(account_s));
2138 if (account_head == NULL) {
2139 ACCOUNT_FATAL("malloc Failed");
2140 if (hstmt != NULL) {
2141 rc = _account_query_finalize(hstmt);
2142 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
2145 *error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
2148 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2150 while (rc == SQLITE_ROW) {
2151 account_s* account_record = NULL;
2153 account_record = (account_s*) malloc(sizeof(account_s));
2155 if (account_record == NULL) {
2156 ACCOUNT_FATAL("malloc Failed");
2159 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2161 _account_convert_column_to_account(hstmt, account_record);
2163 account_head->account_list = g_list_append(account_head->account_list, account_record);
2165 rc = _account_query_step(hstmt);
2169 rc = _account_query_finalize(hstmt);
2170 ACCOUNT_CATCH_ERROR_P((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2176 tmp = g_list_length(account_head->account_list);
2178 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2179 // account_h account = NULL;
2180 // account = (account_h)iter->data;
2181 account_s* testaccount = (account_s*)iter->data;
2183 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, testaccount->id, (void*)testaccount);
2184 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, testaccount->id, (void*)testaccount);
2189 *error_code = _ACCOUNT_ERROR_NONE;
2192 if (hstmt != NULL) {
2193 rc = _account_query_finalize(hstmt);
2194 if ( rc != _ACCOUNT_ERROR_NONE ) {
2196 _ERR("finalize error");
2201 if( *error_code != _ACCOUNT_ERROR_NONE && account_head ) {
2202 _account_glist_account_free(account_head->account_list);
2203 _ACCOUNT_FREE(account_head);
2204 account_head = NULL;
2207 pthread_mutex_unlock(&account_mutex);
2211 _remove_sensitive_info_from_non_owning_account_list(account_head->account_list, pid, uid);
2212 GList* result = account_head->account_list;
2213 _ACCOUNT_FREE(account_head);
2219 GList* _account_query_account_by_capability_type(int pid, uid_t uid, const char* capability_type, int *error_code)
2221 *error_code = _ACCOUNT_ERROR_NONE;
2222 account_stmt hstmt = NULL;
2223 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2226 ACCOUNT_RETURN_VAL((capability_type != NULL), {*error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("capability_type IS NULL"));
2227 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;},
2228 NULL, ("The database isn't connected."));
2230 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2232 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
2234 hstmt = _account_prepare_query(g_hAccountDB, query);
2236 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
2237 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2238 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
2242 int binding_count = 1;
2243 _account_query_bind_text(hstmt, binding_count++, capability_type);
2245 rc = _account_query_step(hstmt);
2247 account_s* account_head = NULL;
2249 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2253 account_head = (account_s*) malloc(sizeof(account_s));
2254 if (account_head == NULL) {
2255 ACCOUNT_FATAL("malloc Failed");
2256 if (hstmt != NULL) {
2257 rc = _account_query_finalize(hstmt);
2258 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
2261 *error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
2264 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2266 while (rc == SQLITE_ROW) {
2267 account_s* account_record = NULL;
2269 account_record = (account_s*) malloc(sizeof(account_s));
2271 if (account_record == NULL) {
2272 ACCOUNT_FATAL("malloc Failed");
2275 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2277 _account_convert_column_to_account(hstmt, account_record);
2279 account_head->account_list = g_list_append(account_head->account_list, account_record);
2281 rc = _account_query_step(hstmt);
2285 rc = _account_query_finalize(hstmt);
2286 ACCOUNT_CATCH_ERROR_P((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2292 tmp = g_list_length(account_head->account_list);
2294 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2295 account_s* testaccount = (account_s*)iter->data;
2297 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, testaccount->id, (void*)testaccount);
2298 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, testaccount->id, (void*)testaccount);
2302 *error_code = _ACCOUNT_ERROR_NONE;
2307 rc = _account_query_finalize(hstmt);
2308 if (rc != _ACCOUNT_ERROR_NONE) {
2310 _ERR("finalize error");
2315 if( (*error_code != _ACCOUNT_ERROR_NONE) && account_head ) {
2316 _account_glist_account_free(account_head->account_list);
2317 _ACCOUNT_FREE(account_head);
2318 account_head = NULL;
2321 pthread_mutex_unlock(&account_mutex);
2325 _remove_sensitive_info_from_non_owning_account_list(account_head->account_list, pid, uid);
2326 GList* result = account_head->account_list;
2327 _ACCOUNT_FREE(account_head);
2333 GList* account_server_query_account_by_package_name(const char* package_name, int *error_code, int pid, uid_t uid)
2335 _INFO("account_server_query_account_by_package_name start");
2337 GList * account_list = NULL;
2338 *error_code = _ACCOUNT_ERROR_NONE;
2340 ACCOUNT_RETURN_VAL((package_name != NULL), {*error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("PACKAGE NAME IS NULL"));
2341 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
2343 account_list = _account_query_account_by_package_name(g_hAccountDB, package_name, error_code, pid, uid);
2345 _INFO("account_server_query_account_by_package_name end");
2346 return account_list;
2349 int account_server_delete_account_by_package_name(const char* package_name, bool permission, int pid, uid_t uid)
2351 _INFO("account_db_delete_account_by_package_name");
2353 int error_code = _ACCOUNT_ERROR_NONE;
2355 ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
2356 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2358 error_code = _account_delete_account_by_package_name(g_hAccountDB, package_name, permission, pid, uid);
2360 _INFO("account_server_delete_account_by_package_name end");
2364 int _account_delete(int pid, uid_t uid, int account_id)
2366 int error_code = _ACCOUNT_ERROR_NONE;
2367 account_stmt hstmt = NULL;
2368 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2370 int ret_transaction = 0;
2371 bool is_success = FALSE;
2373 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2376 /* Check requested ID to delete */
2377 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id=%d", ACCOUNT_TABLE, account_id);
2379 count = _account_get_record_count(g_hAccountDB, query);
2381 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
2382 pthread_mutex_unlock(&account_mutex);
2383 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2384 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2388 ACCOUNT_ERROR("account id(%d) is not exist. count(%d)\n", account_id, count);
2389 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
2392 /* Check permission of requested appid */
2393 char* current_appid = NULL;
2394 char *package_name = NULL;
2396 current_appid = _account_get_current_appid(pid, uid);
2398 error_code = _account_get_package_name_from_account_id(account_id, &package_name);
2400 if(error_code != _ACCOUNT_ERROR_NONE){
2401 ACCOUNT_ERROR("No package name with account_id\n");
2402 _ACCOUNT_FREE(current_appid);
2403 _ACCOUNT_FREE(package_name);
2404 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
2406 ACCOUNT_DEBUG( "DELETE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name);
2408 error_code = _account_check_appid_group_with_package_name(current_appid, package_name, uid);
2410 _ACCOUNT_FREE(current_appid);
2411 _ACCOUNT_FREE(package_name);
2413 if(error_code != _ACCOUNT_ERROR_NONE){
2414 ACCOUNT_ERROR("No permission to delete\n");
2415 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2418 /* transaction control required*/
2419 ret_transaction = _account_begin_transaction(g_hAccountDB);
2421 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
2422 pthread_mutex_unlock(&account_mutex);
2423 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2424 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2427 if( ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY ){
2428 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg(g_hAccountDB));
2429 pthread_mutex_unlock(&account_mutex);
2430 return _ACCOUNT_ERROR_DATABASE_BUSY;
2433 if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2434 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
2435 pthread_mutex_unlock(&account_mutex);
2436 return ret_transaction;
2439 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2440 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
2442 hstmt = _account_prepare_query(g_hAccountDB, query);
2444 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
2445 pthread_mutex_unlock(&account_mutex);
2446 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2447 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2450 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2451 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2453 rc = _account_query_step(hstmt);
2454 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2456 rc = _account_query_finalize(hstmt);
2458 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2461 ACCOUNT_MEMSET(query, 0, sizeof(query));
2463 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
2465 hstmt = _account_prepare_query(g_hAccountDB, query);
2466 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2467 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2469 rc = _account_query_step(hstmt);
2470 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
2472 rc = _account_query_finalize(hstmt);
2473 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2476 /* delete custom data */
2477 ACCOUNT_MEMSET(query, 0, sizeof(query));
2479 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
2481 hstmt = _account_prepare_query(g_hAccountDB, query);
2483 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2484 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2486 rc = _account_query_step(hstmt);
2487 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
2489 rc = _account_query_finalize(hstmt);
2490 ACCOUNT_CATCH_ERROR(rc == _ACCOUNT_ERROR_NONE, {}, rc, ("finalize error", account_id, rc));
2496 if (hstmt != NULL) {
2497 rc = _account_query_finalize(hstmt);
2498 if(rc != _ACCOUNT_ERROR_NONE ){
2499 ACCOUNT_ERROR("rc (%d)", rc);
2506 ret_transaction = _account_end_transaction(g_hAccountDB, is_success);
2508 if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2509 ACCOUNT_ERROR("account_delete:_account_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
2511 if (is_success == true) {
2513 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_DELETE, account_id);
2514 _account_insert_delete_update_notification_send(buf);
2518 pthread_mutex_unlock(&account_mutex);
2524 static int _account_query_account_by_username_and_package(const char* username, const char* package_name, account_s *account)
2528 int error_code = _ACCOUNT_ERROR_NONE;
2529 account_stmt hstmt = NULL;
2530 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2532 int binding_count = 1;
2534 ACCOUNT_RETURN_VAL((username != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("username IS NULL"));
2535 ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name IS NULL"));
2536 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2537 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2539 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2541 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
2542 hstmt = _account_prepare_query(g_hAccountDB, query);
2544 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
2545 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2546 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2549 _account_query_bind_text(hstmt, binding_count++, username);
2550 _account_query_bind_text(hstmt, binding_count++, package_name);
2552 rc = _account_query_step(hstmt);
2553 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2555 account_s *account_record = account;
2557 while (rc == SQLITE_ROW) {
2558 _account_convert_column_to_account(hstmt, account_record);
2559 rc = _account_query_step(hstmt);
2562 rc = _account_query_finalize(hstmt);
2563 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2564 _account_query_capability_by_account_id(g_hAccountDB, _account_add_capability_to_account_cb, account_record->id, (void*)account_record);
2565 _account_query_custom_by_account_id(g_hAccountDB, _account_add_custom_to_account_cb, account_record->id, (void*)account_record);
2568 error_code = _ACCOUNT_ERROR_NONE;
2571 if (hstmt != NULL) {
2572 rc = _account_query_finalize(hstmt);
2573 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2577 pthread_mutex_unlock(&account_mutex);
2581 int _account_create(account_s **account)
2584 ACCOUNT_SLOGE("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__);
2585 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2588 account_s *data = (account_s*)malloc(sizeof(account_s));
2591 ACCOUNT_FATAL("Memory Allocation Failed");
2592 return _ACCOUNT_ERROR_OUT_OF_MEMORY;
2594 ACCOUNT_MEMSET(data, 0, sizeof(account_s));
2596 /*Setting account as visible by default*/
2597 // data->secret = _ACCOUNT_SECRECY_VISIBLE;
2599 /*Setting account as not supporting sync by default*/
2600 // data->sync_support = _ACCOUNT_SYNC_NOT_SUPPORT;
2604 return _ACCOUNT_ERROR_NONE;
2607 int _account_destroy(account_s *account)
2609 account_s *data = account;
2611 ACCOUNT_RETURN_VAL((data != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Account handle is null!"));
2613 _account_free_account_with_items(data);
2615 return _ACCOUNT_ERROR_NONE;
2618 int _account_get_account_id(account_s* account, int *account_id)
2621 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2624 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2627 *account_id = account->id;
2629 return _ACCOUNT_ERROR_NONE;
2632 int _account_delete_from_db_by_user_name(int pid, uid_t uid, const char *user_name, const char *package_name)
2634 _INFO("[%s][%s]", user_name, package_name);
2636 int error_code = _ACCOUNT_ERROR_NONE;
2637 account_stmt hstmt = NULL;
2638 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2640 int ret_transaction = 0;
2641 bool is_success = FALSE;
2642 account_s *account = NULL;
2643 int binding_count = 1;
2644 int account_id = -1;
2646 ACCOUNT_RETURN_VAL((user_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is null!"));
2647 ACCOUNT_RETURN_VAL((package_name != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
2648 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2650 /* Check permission of requested appid */
2651 char* current_appid = NULL;
2652 char* package_name_temp = NULL;
2654 current_appid = _account_get_current_appid(pid, uid);
2656 package_name_temp = _account_dup_text(package_name);
2658 ACCOUNT_DEBUG( "DELETE:user_name[%s],current_appid[%s], package_name[%s]", user_name, current_appid, package_name_temp);
2660 error_code = _account_check_appid_group_with_package_name(current_appid, package_name_temp, uid);
2662 _ACCOUNT_FREE(current_appid);
2663 _ACCOUNT_FREE(package_name_temp);
2665 if(error_code != _ACCOUNT_ERROR_NONE){
2666 ACCOUNT_ERROR("No permission to delete\n");
2667 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2670 rc = _account_create(&account);
2671 rc = _account_query_account_by_username_and_package(user_name, package_name, account);
2675 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM )
2677 _account_destroy(account);
2678 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2679 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2683 account_s* account_data = (account_s*)account;
2685 rc = _account_get_account_id(account_data, &account_id);
2687 rc = _account_destroy(account);
2689 /* transaction control required*/
2690 ret_transaction = _account_begin_transaction(g_hAccountDB);
2692 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM )
2694 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2695 pthread_mutex_unlock(&account_mutex);
2696 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2700 if( ret_transaction == _ACCOUNT_ERROR_DATABASE_BUSY )
2702 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg(g_hAccountDB));
2703 pthread_mutex_unlock(&account_mutex);
2704 return _ACCOUNT_ERROR_DATABASE_BUSY;
2706 else if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2707 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
2708 pthread_mutex_unlock(&account_mutex);
2709 return ret_transaction;
2712 /* delete custom data */
2713 ACCOUNT_MEMSET(query, 0, sizeof(query));
2714 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = ?", ACCOUNT_CUSTOM_TABLE);
2716 hstmt = _account_prepare_query(g_hAccountDB, query);
2718 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
2719 _account_end_transaction(g_hAccountDB, FALSE);
2720 pthread_mutex_unlock(&account_mutex);
2721 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2722 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2725 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2726 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2728 _account_query_bind_int(hstmt, binding_count++, account_id);
2730 rc = _account_query_step(hstmt);
2731 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2733 rc = _account_query_finalize(hstmt);
2734 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2737 /* delete capability */
2738 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", CAPABILITY_TABLE);
2740 hstmt = _account_prepare_query(g_hAccountDB, query);
2742 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2743 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2746 _account_query_bind_text(hstmt, binding_count++, user_name);
2747 _account_query_bind_text(hstmt, binding_count++, package_name);
2749 rc = _account_query_step(hstmt);
2750 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2752 rc = _account_query_finalize(hstmt);
2753 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2756 ACCOUNT_MEMSET(query, 0, sizeof(query));
2759 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
2761 hstmt = _account_prepare_query(g_hAccountDB, query);
2762 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, _ACCOUNT_ERROR_DB_FAILED,
2763 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg(g_hAccountDB)));
2767 _account_query_bind_text(hstmt, binding_count++, user_name);
2768 _account_query_bind_text(hstmt, binding_count++, package_name);
2770 rc = _account_query_step(hstmt);
2771 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));
2773 rc = _account_query_finalize(hstmt);
2774 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2780 if (hstmt != NULL) {
2781 rc = _account_query_finalize(hstmt);
2782 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2786 ret_transaction = _account_end_transaction(g_hAccountDB, is_success);
2788 if (ret_transaction != _ACCOUNT_ERROR_NONE) {
2789 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
2791 if (is_success == true) {
2793 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", _ACCOUNT_NOTI_NAME_DELETE, account_id);
2794 _account_insert_delete_update_notification_send(buf);
2798 pthread_mutex_unlock(&account_mutex);
2804 int _account_get_total_count_from_db(gboolean include_hidden, int *count)
2807 ACCOUNT_SLOGE("(%s)-(%d) count is NULL.\n", __FUNCTION__, __LINE__);
2808 return _ACCOUNT_ERROR_INVALID_PARAMETER;
2812 ACCOUNT_ERROR("DB is not opened\n");
2813 return _ACCOUNT_ERROR_DB_NOT_OPENED;
2816 char query[1024] = {0, };
2817 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2821 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s", ACCOUNT_TABLE);
2825 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s where secret = %d", ACCOUNT_TABLE, _ACCOUNT_SECRECY_VISIBLE);
2828 *count = _account_get_record_count(g_hAccountDB, query);
2830 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
2831 pthread_mutex_unlock(&account_mutex);
2832 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2833 return _ACCOUNT_ERROR_PERMISSION_DENIED;
2838 account_stmt pStmt = NULL;
2840 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
2841 if (SQLITE_OK != rc) {
2842 ACCOUNT_SLOGE("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg(g_hAccountDB));
2843 sqlite3_finalize(pStmt);
2844 return _ACCOUNT_ERROR_DB_FAILED;
2847 rc = sqlite3_step(pStmt);
2848 if (SQLITE_ROW != rc) {
2849 ACCOUNT_ERROR("[ERROR] sqlite3_step() failed\n");
2850 sqlite3_finalize(pStmt);
2851 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
2854 ncount = sqlite3_column_int(pStmt, 0);
2858 sqlite3_finalize(pStmt);
2861 ACCOUNT_ERROR("[ERROR] Number of account : %d, End", ncount);
2862 return _ACCOUNT_ERROR_DB_FAILED;
2865 return _ACCOUNT_ERROR_NONE;
2869 int account_server_query_app_id_exist(const char* app_id)
2871 _INFO("account_server_query_app_id_exist start app_id=[%s]", app_id);
2872 int ret = _ACCOUNT_ERROR_NONE;
2874 ret = _account_type_query_app_id_exist_from_all_db(g_hAccountDB, g_hAccountGlobalDB, app_id);
2876 _INFO("account_server_query_app_id_exist end error_code=[%d]", ret);
2880 int account_server_insert_account_type_to_user_db(account_type_s *account_type, int *account_type_id, uid_t uid)
2882 ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE HANDLE IS NULL"));
2883 ACCOUNT_RETURN_VAL((account_type->app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID OF ACCOUNT TYPE IS NULL"));
2884 ACCOUNT_RETURN_VAL((account_type_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE ID IS NULL"));
2886 _INFO("account_server_insert_account_type_to_user_db start uid=[%d]", uid);
2887 int ret = _ACCOUNT_ERROR_NONE;
2889 if (_account_type_check_duplicated(g_hAccountDB, account_type->app_id) ||
2890 _account_type_check_duplicated(g_hAccountGlobalDB, account_type->app_id)) {
2891 *account_type_id = -1;
2892 return _ACCOUNT_ERROR_DUPLICATED;
2895 ret = _account_type_insert_to_db(g_hAccountDB, account_type, account_type_id);
2896 _INFO("account_server_insert_account_type_to_user_db end error_code=[%d]", ret);
2900 int account_server_delete_account_type_by_app_id_from_user_db(const char * app_id)
2902 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID OF ACCOUNT TYPE IS NULL"));
2904 _INFO("account_server_delete_account_type_by_app_id_from_user_db start");
2905 int ret = _ACCOUNT_ERROR_NONE;
2907 ret = _account_type_delete_by_app_id(g_hAccountDB, app_id);
2908 _INFO("account_server_delete_account_type_by_app_id_from_user_db end error_code=[%d]", ret);
2912 GSList* _account_type_query_provider_feature_by_app_id_from_global_db(const char* app_id, int *error_code)
2914 _INFO("_account_type_query_provider_feature_by_app_id_in_global_db app_id=%s", app_id);
2915 account_stmt hstmt = NULL;
2916 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2917 int rc = 0, binding_count = 1;
2918 GSList* feature_list = NULL;
2920 ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
2921 ACCOUNT_RETURN_VAL((error_code != NULL), {_ERR("error_code pointer is NULL");}, NULL, (""));
2922 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {*error_code = _ACCOUNT_ERROR_INVALID_PARAMETER; _ERR("The database isn't connected.");}, NULL, ("The database isn't connected."));
2924 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2926 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
2927 _INFO("account query=[%s]", query);
2929 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
2931 if( _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM ){
2932 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
2933 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
2937 _INFO("before _account_query_bind_text");
2938 _account_query_bind_text(hstmt, binding_count++, app_id);
2940 rc = _account_query_step(hstmt);
2942 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"));
2944 provider_feature_s* feature_record = NULL;
2946 while (rc == SQLITE_ROW) {
2948 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
2950 if (feature_record == NULL) {
2951 ACCOUNT_FATAL("malloc Failed");
2955 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
2957 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
2959 _INFO("Adding account feature_list");
2960 feature_list = g_slist_append(feature_list, feature_record);
2962 rc = _account_query_step(hstmt);
2965 *error_code = _ACCOUNT_ERROR_NONE;
2968 if (hstmt != NULL) {
2969 rc = _account_query_finalize(hstmt);
2970 if (rc != _ACCOUNT_ERROR_NONE) {
2972 _ERR("global db fianlize error");
2976 if (*error_code != _ACCOUNT_ERROR_NONE) {
2977 _account_type_gslist_feature_free(feature_list);
2980 _INFO("Returning account feature_list from global db");
2981 return feature_list;
2984 GSList* _account_type_query_provider_feature_by_app_id(const char* app_id, int *error_code)
2986 _INFO("_account_type_query_provider_feature_by_app_id app_id=%s", app_id);
2987 account_stmt hstmt = NULL;
2988 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2989 int rc = 0, binding_count = 1;
2990 GSList* feature_list = NULL;
2992 ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
2993 ACCOUNT_RETURN_VAL((error_code != NULL), {_ERR("error_code pointer is NULL");}, NULL, (""));
2994 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
2996 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2998 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
2999 _INFO("account query=[%s]", query);
3001 hstmt = _account_prepare_query(g_hAccountDB, query);
3003 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
3004 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3005 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3009 _account_query_bind_text(hstmt, binding_count++, app_id);
3011 rc = _account_query_step(hstmt);
3013 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"));
3015 provider_feature_s* feature_record = NULL;
3017 while (rc == SQLITE_ROW) {
3019 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
3021 if (feature_record == NULL) {
3022 ACCOUNT_FATAL("malloc Failed");
3026 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
3028 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
3030 _INFO("Adding account feature_list");
3031 feature_list = g_slist_append(feature_list, feature_record);
3033 rc = _account_query_step(hstmt);
3036 *error_code = _ACCOUNT_ERROR_NONE;
3038 rc = _account_query_finalize(hstmt);
3039 ACCOUNT_CATCH_ERROR_P((rc == _ACCOUNT_ERROR_NONE), {*error_code = rc;}, rc, ("account finalize error"));
3043 if (hstmt != NULL) {
3044 rc = _account_query_finalize(hstmt);
3045 if (rc != _ACCOUNT_ERROR_NONE) {
3047 _ERR("account fianlize error");
3051 _INFO("*error_code=[%d]", *error_code);
3053 if (*error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
3054 feature_list = _account_type_query_provider_feature_by_app_id_from_global_db(app_id, error_code);
3057 if (*error_code != _ACCOUNT_ERROR_NONE)
3058 _account_type_gslist_feature_free(feature_list);
3060 _INFO("Returning account feature_list");
3061 return feature_list;
3064 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 )
3066 int error_code = _ACCOUNT_ERROR_NONE;
3067 account_stmt hstmt = NULL;
3068 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3069 int rc = 0, binding_count = 1;
3071 _INFO("_account_type_query_provider_feature_cb_by_app_id_in_global_db start app_id=%s", app_id);
3072 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3073 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3074 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3076 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3078 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
3079 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3081 if( _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM ){
3082 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3083 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_PERMISSION_DENIED, ("global db permission denied.\n"));
3086 _account_query_bind_text(hstmt, binding_count++, app_id);
3088 rc = _account_query_step(hstmt);
3089 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"));
3091 provider_feature_s* feature_record = NULL;
3093 while (rc == SQLITE_ROW) {
3094 bool cb_ret = FALSE;
3095 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
3097 if (feature_record == NULL) {
3098 ACCOUNT_FATAL("malloc Failed");
3102 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
3104 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
3106 cb_ret = callback(feature_record->app_id, feature_record->key, user_data);
3108 _account_type_free_feature_with_items(feature_record);
3110 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returns FALSE, its iteration is stopped!!!!\n"));
3112 rc = _account_query_step(hstmt);
3115 error_code = _ACCOUNT_ERROR_NONE;
3118 if (hstmt != NULL) {
3119 rc = _account_query_finalize(hstmt);
3120 if (rc != _ACCOUNT_ERROR_NONE) {
3122 _ERR("global db finalize error[%d]", rc);
3127 _INFO("_account_type_query_provider_feature_cb_by_app_id_in_global_db end. error_code=[%d]", error_code);
3131 int _account_type_query_provider_feature_cb_by_app_id(account_type_provider_feature_cb callback, const char* app_id, void *user_data )
3133 int error_code = _ACCOUNT_ERROR_NONE;
3134 account_stmt hstmt = NULL;
3135 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3136 int rc = 0, binding_count = 1;
3138 _INFO("_account_type_query_provider_feature_cb_by_app_id start app_id=%s", app_id);
3139 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3140 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3141 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3143 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3145 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
3146 hstmt = _account_prepare_query(g_hAccountDB, query);
3148 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
3149 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3150 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3153 _account_query_bind_text(hstmt, binding_count++, app_id);
3155 rc = _account_query_step(hstmt);
3156 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found in user db.\n"));
3158 provider_feature_s* feature_record = NULL;
3160 while (rc == SQLITE_ROW) {
3161 bool cb_ret = FALSE;
3162 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
3164 if (feature_record == NULL) {
3165 ACCOUNT_FATAL("malloc Failed");
3169 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
3171 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
3173 cb_ret = callback(feature_record->app_id, feature_record->key, user_data);
3175 _account_type_free_feature_with_items(feature_record);
3177 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
3179 rc = _account_query_step(hstmt);
3182 rc = _account_query_finalize(hstmt);
3183 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3186 error_code = _ACCOUNT_ERROR_NONE;
3189 if (hstmt != NULL) {
3190 rc = _account_query_finalize(hstmt);
3191 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3195 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
3196 error_code = _account_type_query_provider_feature_cb_by_app_id_from_global_db(callback, app_id, user_data);
3199 _INFO("_account_type_query_provider_feature_cb_by_app_id end");
3203 int account_type_query_provider_feature_cb_by_app_id(account_type_provider_feature_cb callback, const char* app_id, void *user_data )
3205 int error_code = _ACCOUNT_ERROR_NONE;
3207 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3208 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3209 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3211 error_code = _account_type_query_provider_feature_cb_by_app_id(callback, app_id, user_data);
3213 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
3214 error_code = _account_type_query_provider_feature_cb_by_app_id_from_global_db(callback, app_id, user_data);
3220 bool _account_type_query_supported_feature_from_global_db(const char* app_id, const char* capability, int *error_code)
3222 _INFO("_account_type_query_supported_feature_in_global_db start");
3223 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3225 *error_code = _ACCOUNT_ERROR_NONE;
3227 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3228 int record_count = 0;
3230 if (app_id == NULL || capability == NULL)
3232 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
3236 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
3238 record_count = _account_get_record_count(g_hAccountGlobalDB, query);
3240 if( _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM ){
3241 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3242 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3245 if (record_count <= 0)
3247 *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
3251 _INFO("_account_type_query_supported_feature_in_global_db end");
3255 bool _account_type_query_supported_feature(const char* app_id, const char* capability, int *error_code)
3257 _INFO("_account_type_query_supported_feature start");
3259 *error_code = _ACCOUNT_ERROR_NONE;
3261 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3262 int record_count = 0;
3264 if (app_id == NULL || capability == NULL)
3266 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
3270 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
3272 record_count = _account_get_record_count(g_hAccountDB, query);
3274 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
3275 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3276 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3280 if (record_count <= 0)
3282 bool is_exist = false;
3283 is_exist = _account_type_query_supported_feature_from_global_db(app_id, capability, error_code);
3288 _INFO("_account_type_query_supported_feature end");
3293 static int _account_type_update_provider_feature(sqlite3 *account_db_handle, account_type_s *account_type, const char* app_id)
3296 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3297 account_stmt hstmt = NULL;
3299 ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
3301 if (g_slist_length( account_type->provider_feature_list)==0) {
3302 ACCOUNT_ERROR( "no feature\n");
3303 return _ACCOUNT_ERROR_NONE;
3306 ACCOUNT_DEBUG( "app id", app_id);
3308 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3310 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id=? ", PROVIDER_FEATURE_TABLE);
3311 hstmt = _account_prepare_query(account_db_handle, query);
3313 if( _account_db_err_code(account_db_handle) == SQLITE_PERM ){
3314 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(account_db_handle));
3315 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3319 _account_query_bind_text(hstmt, count++, app_id);
3320 rc = _account_query_step(hstmt);
3322 if (rc != SQLITE_DONE) {
3323 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3324 return _ACCOUNT_ERROR_DB_FAILED;
3326 rc = _account_query_finalize(hstmt);
3327 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3332 for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
3335 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3336 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
3337 "(?, ?) ", PROVIDER_FEATURE_TABLE);
3339 hstmt = _account_prepare_query(account_db_handle, query);
3341 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg(account_db_handle)));
3343 provider_feature_s* feature_data = NULL;
3344 feature_data = (provider_feature_s*)iter->data;
3346 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
3347 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3348 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
3349 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3351 rc = _account_query_step(hstmt);
3353 if (rc != SQLITE_DONE) {
3354 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3357 rc = _account_query_finalize(hstmt);
3358 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3362 return _ACCOUNT_ERROR_NONE;
3365 static int _account_type_update_label(sqlite3 *account_db_handle, account_type_s *account_type, const char* app_id)
3368 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3369 account_stmt hstmt = NULL;
3371 ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
3373 if (g_slist_length( account_type->label_list)==0) {
3374 return _ACCOUNT_ERROR_NONE;
3377 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3379 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId=? ", LABEL_TABLE);
3380 hstmt = _account_prepare_query(account_db_handle, query);
3382 if( _account_db_err_code(account_db_handle) == SQLITE_PERM ){
3383 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(account_db_handle));
3384 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3388 _account_query_bind_text(hstmt, count++, app_id);
3389 rc = _account_query_step(hstmt);
3391 if (rc != SQLITE_DONE) {
3392 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3393 return _ACCOUNT_ERROR_DB_FAILED;
3395 rc = _account_query_finalize(hstmt);
3396 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3401 for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
3404 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3405 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
3406 "(?, ?, ?) ", LABEL_TABLE);
3408 hstmt = _account_prepare_query(account_db_handle, query);
3410 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg(account_db_handle)));
3412 label_s* label_data = NULL;
3413 label_data = (label_s*)iter->data;
3415 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
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->label);
3418 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3419 ret = _account_query_bind_text(hstmt, count++, label_data->locale);
3420 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3422 rc = _account_query_step(hstmt);
3424 if (rc != SQLITE_DONE) {
3425 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3428 rc = _account_query_finalize(hstmt);
3429 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3433 return _ACCOUNT_ERROR_NONE;
3437 static int _account_type_update_account(sqlite3 *account_db_handle, account_type_s *account_type, const char* app_id)
3439 int rc = 0, binding_count =1;
3440 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3441 int error_code = _ACCOUNT_ERROR_NONE;
3442 account_stmt hstmt = NULL;
3444 if (!account_type->app_id) {
3445 ACCOUNT_ERROR("app id is mandetory field, it can not be NULL!!!!\n");
3446 return _ACCOUNT_ERROR_INVALID_PARAMETER;
3449 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3450 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET AppId=?, ServiceProviderId=?, IconPath=?, "
3451 "SmallIconPath=?, MultipleAccountSupport=? WHERE AppId=? ", ACCOUNT_TYPE_TABLE);
3453 hstmt = _account_prepare_query(account_db_handle, query);
3455 if( _account_db_err_code(account_db_handle) == SQLITE_PERM ){
3456 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(account_db_handle));
3457 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3458 } else if (_account_db_err_code(account_db_handle) == SQLITE_BUSY){
3459 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg(account_db_handle));
3460 return _ACCOUNT_ERROR_DATABASE_BUSY;
3463 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg(account_db_handle)));
3465 binding_count = _account_type_convert_account_to_sql(account_type, hstmt, query);
3466 _account_query_bind_text(hstmt, binding_count++, app_id);
3468 rc = _account_query_step(hstmt);
3469 if (rc != SQLITE_DONE) {
3470 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg(account_db_handle));
3473 rc = _account_query_finalize(hstmt);
3474 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3478 error_code = _account_type_update_label(account_db_handle, account_type, app_id);
3479 /* update provider feature */
3480 error_code = _account_type_update_provider_feature(account_db_handle, account_type, app_id);
3485 int _account_type_update_to_db_by_app_id(account_type_s* account_type, const char* app_id)
3487 int error_code = _ACCOUNT_ERROR_NONE;
3489 ACCOUNT_RETURN_VAL((account_type != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
3490 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3491 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3493 account_type_s* data = account_type;
3495 pthread_mutex_lock(&account_mutex);
3497 error_code = _account_type_update_account(g_hAccountDB, data, app_id);
3499 pthread_mutex_unlock(&account_mutex);
3504 GSList* _account_type_get_label_list_by_app_id_from_global_db(const char* app_id, int *error_code )
3506 *error_code = _ACCOUNT_ERROR_NONE;
3507 account_stmt hstmt = NULL;
3508 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3509 int rc = 0, binding_count = 1;
3510 GSList* label_list = NULL;
3512 ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
3513 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3515 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3517 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3518 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3520 if( _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM ){
3521 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3522 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3527 _account_query_bind_text(hstmt, binding_count++, app_id);
3529 rc = _account_query_step(hstmt);
3530 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"));
3532 label_s* label_record = NULL;
3534 while (rc == SQLITE_ROW) {
3535 label_record = (label_s*) malloc(sizeof(label_s));
3537 if (label_record == NULL) {
3538 ACCOUNT_FATAL("malloc Failed");
3542 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3544 _account_type_convert_column_to_label(hstmt, label_record);
3546 _INFO("Adding account label_list");
3547 label_list = g_slist_append (label_list, label_record);
3549 rc = _account_query_step(hstmt);
3552 *error_code = _ACCOUNT_ERROR_NONE;
3555 if (hstmt != NULL) {
3556 rc = _account_query_finalize(hstmt);
3557 if (rc != _ACCOUNT_ERROR_NONE) {
3558 _ERR("global db finalize error[%d]", rc);
3563 _INFO("Returning account global label_list");
3567 GSList* _account_type_get_label_list_by_app_id(const char* app_id, int *error_code )
3569 *error_code = _ACCOUNT_ERROR_NONE;
3570 account_stmt hstmt = NULL;
3571 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3572 int rc = 0, binding_count = 1;
3573 GSList* label_list = NULL;
3575 ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
3576 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
3578 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3580 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3581 hstmt = _account_prepare_query(g_hAccountDB, query);
3583 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
3584 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3585 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3589 _account_query_bind_text(hstmt, binding_count++, app_id);
3591 rc = _account_query_step(hstmt);
3592 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3594 label_s* label_record = NULL;
3596 while (rc == SQLITE_ROW) {
3597 label_record = (label_s*) malloc(sizeof(label_s));
3599 if (label_record == NULL) {
3600 ACCOUNT_FATAL("malloc Failed");
3604 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3606 _account_type_convert_column_to_label(hstmt, label_record);
3608 _INFO("Adding account label_list");
3609 label_list = g_slist_append (label_list, label_record);
3611 rc = _account_query_step(hstmt);
3614 rc = _account_query_finalize(hstmt);
3615 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
3618 *error_code = _ACCOUNT_ERROR_NONE;
3621 if (hstmt != NULL) {
3622 rc = _account_query_finalize(hstmt);
3623 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
3627 if (*error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
3628 label_list = _account_type_get_label_list_by_app_id_from_global_db(app_id, error_code);
3631 _INFO("Returning account label_list");
3635 int _account_type_query_label_by_app_id_from_global_db(account_type_label_cb callback, const char* app_id, void *user_data )
3637 int error_code = _ACCOUNT_ERROR_NONE;
3638 account_stmt hstmt = NULL;
3639 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3640 int rc = 0, binding_count = 1;
3642 _INFO("account_type_query_label_by_app_id_from_global_db start");
3644 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3645 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3646 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3648 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3650 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3651 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3653 if( _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM ){
3654 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3655 error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3659 _account_query_bind_text(hstmt, binding_count++, app_id);
3661 rc = _account_query_step(hstmt);
3662 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3664 label_s* label_record = NULL;
3666 while (rc == SQLITE_ROW) {
3667 bool cb_ret = FALSE;
3668 label_record = (label_s*) malloc(sizeof(label_s));
3670 if (label_record == NULL) {
3671 ACCOUNT_FATAL("malloc Failed");
3675 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3677 _account_type_convert_column_to_label(hstmt, label_record);
3679 cb_ret = callback(label_record->app_id, label_record->label , label_record->locale, user_data);
3681 _account_type_free_label_with_items(label_record);
3683 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
3685 rc = _account_query_step(hstmt);
3688 error_code = _ACCOUNT_ERROR_NONE;
3691 if (hstmt != NULL) {
3692 rc = _account_query_finalize(hstmt);
3693 if (rc != _ACCOUNT_ERROR_NONE) {
3694 _ERR("global db finalize error[%d]", rc);
3699 _INFO("account_type_query_label_by_app_id_from_global_db end [%d]", error_code);
3703 int _account_type_query_label_by_app_id(account_type_label_cb callback, const char* app_id, void *user_data )
3705 int error_code = _ACCOUNT_ERROR_NONE;
3706 account_stmt hstmt = NULL;
3707 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3708 int rc = 0, binding_count = 1;
3710 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3711 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3712 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3714 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3716 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3717 hstmt = _account_prepare_query(g_hAccountDB, query);
3719 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
3720 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3721 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3724 _account_query_bind_text(hstmt, binding_count++, app_id);
3726 rc = _account_query_step(hstmt);
3727 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3729 label_s* label_record = NULL;
3731 while (rc == SQLITE_ROW) {
3732 bool cb_ret = FALSE;
3733 label_record = (label_s*) malloc(sizeof(label_s));
3735 if (label_record == NULL) {
3736 ACCOUNT_FATAL("malloc Failed");
3740 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3742 _account_type_convert_column_to_label(hstmt, label_record);
3744 cb_ret = callback(label_record->app_id, label_record->label , label_record->locale, user_data);
3746 _account_type_free_label_with_items(label_record);
3748 // ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, _ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
3749 if(cb_ret != TRUE) {
3750 _INFO("Callback func returs FALSE, its iteration is stopped!!!!\n");
3754 rc = _account_query_step(hstmt);
3757 rc = _account_query_finalize(hstmt);
3758 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3761 error_code = _ACCOUNT_ERROR_NONE;
3764 if (hstmt != NULL) {
3765 rc = _account_query_finalize(hstmt);
3766 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3770 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
3771 error_code = account_type_query_label_by_app_id_from_global_db(callback, app_id, user_data);
3777 int account_type_query_label_by_app_id(account_type_label_cb callback, const char* app_id, void *user_data )
3779 int error_code = _ACCOUNT_ERROR_NONE;
3781 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3782 ACCOUNT_RETURN_VAL((callback != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3783 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3785 error_code = _account_type_query_label_by_app_id(callback, app_id, user_data);
3787 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
3788 error_code = _account_type_query_label_by_app_id_from_global_db(callback, app_id, user_data);
3794 bool _account_get_label_text_cb(char* app_id, char* label, char* locale, void *user_data)
3796 account_type_s *data = (account_type_s*)user_data;
3798 label_s *label_data = (label_s*)malloc(sizeof(label_s));
3800 if (label_data == NULL) {
3801 ACCOUNT_FATAL("_account_get_label_text_cb : MALLOC FAIL\n");
3804 ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
3806 label_data->app_id = _account_dup_text(app_id);
3807 label_data->label = _account_dup_text(label);
3808 label_data->locale = _account_dup_text(locale);
3810 data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
3815 bool _account_get_provider_feature_cb(char* app_id, char* key, void* user_data)
3817 account_type_s *data = (account_type_s*)user_data;
3819 provider_feature_s *feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
3821 if (feature_data == NULL) {
3822 ACCOUNT_FATAL("_account_get_provider_feature_cb : MALLOC FAIL\n");
3825 ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
3827 feature_data->app_id = _account_dup_text(app_id);
3828 feature_data->key = _account_dup_text(key);
3830 data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
3835 int _account_type_query_by_app_id_from_global_db(const char* app_id, account_type_s** account_type_record)
3837 _INFO("_account_type_query_by_app_id_from_global_db start");
3839 int error_code = _ACCOUNT_ERROR_NONE;
3840 account_stmt hstmt = NULL;
3841 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3842 int rc = 0, binding_count = 1;
3844 ACCOUNT_RETURN_VAL((app_id != 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3845 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3846 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3848 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3850 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
3851 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3853 if( _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM ){
3854 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3855 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3858 _account_query_bind_text(hstmt, binding_count++, app_id);
3860 rc = _account_query_step(hstmt);
3861 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3863 *account_type_record = create_empty_account_type_instance();
3865 while (rc == SQLITE_ROW) {
3866 _account_type_convert_column_to_account_type(hstmt, *account_type_record);
3867 rc = _account_query_step(hstmt);
3870 rc = _account_query_finalize(hstmt);
3871 ACCOUNT_CATCH_ERROR((rc == _ACCOUNT_ERROR_NONE), {_ERR("global db finalize error rc=[%d]", rc);}, rc, ("finalize error"));
3872 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb, app_id, (void*)(*account_type_record));
3873 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, app_id,(void*)(*account_type_record));
3876 error_code = _ACCOUNT_ERROR_NONE;
3879 if (hstmt != NULL) {
3880 rc = _account_query_finalize(hstmt);
3881 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3885 _INFO("_account_type_query_by_app_id_from_global_db end [%d]", error_code);
3889 int _account_type_query_by_app_id(const char* app_id, account_type_s** account_type_record)
3891 _INFO("_account_type_query_by_app_id start");
3893 int error_code = _ACCOUNT_ERROR_NONE;
3894 account_stmt hstmt = NULL;
3895 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3896 int rc = 0, binding_count = 1;
3898 ACCOUNT_RETURN_VAL((app_id != 0), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3899 ACCOUNT_RETURN_VAL((account_type_record != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("account type record(account_type_s**) is NULL"));
3900 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3902 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3904 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
3905 hstmt = _account_prepare_query(g_hAccountDB, query);
3907 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
3908 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
3909 return _ACCOUNT_ERROR_PERMISSION_DENIED;
3912 _account_query_bind_text(hstmt, binding_count++, app_id);
3914 rc = _account_query_step(hstmt);
3915 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3917 *account_type_record = create_empty_account_type_instance();
3918 if (*account_type_record == NULL) {
3919 _ERR("Out of Memory");
3920 error_code = _ACCOUNT_ERROR_OUT_OF_MEMORY;
3924 while (rc == SQLITE_ROW) {
3925 _account_type_convert_column_to_account_type(hstmt, *account_type_record);
3926 rc = _account_query_step(hstmt);
3929 rc = _account_query_finalize(hstmt);
3930 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3931 _account_type_query_label_by_app_id(_account_get_label_text_cb, app_id, (void*)(*account_type_record));
3932 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, app_id,(void*)(*account_type_record));
3935 error_code = _ACCOUNT_ERROR_NONE;
3938 if (hstmt != NULL) {
3939 rc = _account_query_finalize(hstmt);
3940 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3944 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
3945 error_code = _account_type_query_by_app_id_from_global_db(app_id, account_type_record);
3948 _INFO("_account_type_query_by_app_id end [%d]", error_code);
3952 int _account_type_query_by_provider_feature_from_global_db(const char* key, GSList **account_type_list_all)
3954 int error_code = _ACCOUNT_ERROR_NONE;
3955 account_stmt hstmt = NULL;
3956 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3958 GSList *account_type_list = NULL;
3960 _INFO("_account_type_query_by_provider_feature_from_global_db start key=%s", key);
3963 ACCOUNT_ERROR("capability_type IS NULL.");
3964 error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
3968 if(g_hAccountGlobalDB == NULL)
3970 ACCOUNT_ERROR("The database isn't connected.");
3971 error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;
3975 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3977 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
3979 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
3981 if( _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM )
3983 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
3984 error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
3988 int binding_count = 1;
3989 _account_query_bind_text(hstmt, binding_count++, key);
3991 rc = _account_query_step(hstmt);
3993 account_type_s *account_type_record = NULL;
3995 if(rc != SQLITE_ROW)
3997 ACCOUNT_ERROR("The record isn't found. rc=[%d]", rc);
3998 error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4002 while(rc == SQLITE_ROW) {
4003 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4005 if (account_type_record == NULL) {
4006 ACCOUNT_FATAL("malloc Failed");
4010 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4011 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4012 account_type_list = g_slist_append(account_type_list, account_type_record);
4013 rc = _account_query_step(hstmt);
4016 rc = _account_query_finalize(hstmt);
4017 if (rc != _ACCOUNT_ERROR_NONE )
4019 _account_type_gslist_account_type_free(account_type_list);
4020 ACCOUNT_ERROR("finalize error(%s)", rc);
4028 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4029 account_type_s *account_type = NULL;
4030 account_type = (account_type_s*)iter->data;
4031 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
4032 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
4033 _INFO("add label & provider_feature");
4036 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4038 account_type_s *account_type = NULL;
4039 account_type = (account_type_s*)iter->data;
4040 *account_type_list_all = g_slist_append(*account_type_list_all, account_type);
4041 _INFO("add account_type");
4043 g_slist_free(account_type_list);
4045 error_code = _ACCOUNT_ERROR_NONE;
4048 if (hstmt != NULL) {
4049 rc = _account_query_finalize(hstmt);
4050 if (rc != _ACCOUNT_ERROR_NONE)
4052 ACCOUNT_ERROR("finalize error(%s)", rc);
4058 _INFO("_account_type_query_by_provider_feature_from_global_db end. error_code=[%d]", error_code);
4062 GSList* _account_type_query_by_provider_feature(const char* key, int *error_code)
4064 *error_code = _ACCOUNT_ERROR_NONE;
4065 account_stmt hstmt = NULL;
4066 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4068 GSList *account_type_list = NULL;
4070 _INFO("account_type_query_by_provider_feature start key=%s", key);
4073 ACCOUNT_ERROR("capability_type IS NULL.");
4074 *error_code = _ACCOUNT_ERROR_INVALID_PARAMETER;
4078 if(g_hAccountDB == NULL)
4080 ACCOUNT_ERROR("The database isn't connected.");
4081 *error_code = _ACCOUNT_ERROR_DB_NOT_OPENED;
4085 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4087 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
4089 hstmt = _account_prepare_query(g_hAccountDB, query);
4091 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM )
4093 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4094 *error_code = _ACCOUNT_ERROR_PERMISSION_DENIED;
4098 int binding_count = 1;
4099 _account_query_bind_text(hstmt, binding_count++, key);
4101 rc = _account_query_step(hstmt);
4103 account_type_s *account_type_record = NULL;
4105 if(rc != SQLITE_ROW)
4107 ACCOUNT_ERROR("The record isn't found. rc=[%d]", rc);
4108 *error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4112 while(rc == SQLITE_ROW) {
4113 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4115 if (account_type_record == NULL) {
4116 ACCOUNT_FATAL("malloc Failed");
4120 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4121 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4122 account_type_list = g_slist_append(account_type_list, account_type_record);
4123 rc = _account_query_step(hstmt);
4126 rc = _account_query_finalize(hstmt);
4127 if (rc != _ACCOUNT_ERROR_NONE )
4129 _account_type_gslist_account_type_free(account_type_list);
4130 ACCOUNT_ERROR("finalize error(%s)", rc);
4138 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4139 account_type_s *account_type = NULL;
4140 account_type = (account_type_s*)iter->data;
4141 _account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
4142 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
4145 *error_code = _ACCOUNT_ERROR_NONE;
4148 if (hstmt != NULL) {
4149 rc = _account_query_finalize(hstmt);
4150 if (rc != _ACCOUNT_ERROR_NONE)
4158 if (*error_code == _ACCOUNT_ERROR_NONE || *error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4159 rc = _account_type_query_by_provider_feature_from_global_db(key, &account_type_list);
4160 if (rc != _ACCOUNT_ERROR_NONE && rc != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4161 ACCOUNT_ERROR( "_account_type_query_by_provider_feature_from_global_db fail=[%d]", rc);
4162 _account_type_gslist_account_type_free(account_type_list);
4165 if (rc == _ACCOUNT_ERROR_NONE)
4169 _INFO("account_type_query_by_provider_feature end");
4170 return account_type_list;
4173 int _account_type_query_all_from_global_db(GSList **account_type_list_all)
4175 account_stmt hstmt = NULL;
4176 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4177 int rc = _ACCOUNT_ERROR_NONE;
4178 int error_code = _ACCOUNT_ERROR_NONE;
4179 GSList *account_type_list = NULL;
4181 _INFO("_account_type_query_all_in_global_db start");
4182 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, NULL, ("The database isn't connected."));
4184 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4186 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
4187 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
4189 rc = _account_query_step(hstmt);
4191 if( _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM ){
4192 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
4193 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4196 account_type_s *account_type_record = NULL;
4198 if (rc != SQLITE_ROW)
4200 _INFO("[_ACCOUNT_ERROR_RECORD_NOT_FOUND]The record isn't found.");
4201 error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4205 while(rc == SQLITE_ROW) {
4206 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4208 if (account_type_record == NULL) {
4209 ACCOUNT_FATAL("malloc Failed");
4213 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4214 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4215 account_type_list = g_slist_append(account_type_list, account_type_record);
4216 rc = _account_query_step(hstmt);
4219 rc = _account_query_finalize(hstmt);
4220 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4225 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4226 account_type_s *account_type = NULL;
4227 account_type = (account_type_s*)iter->data;
4228 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
4229 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
4232 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4233 account_type_s *account_type = NULL;
4234 account_type = (account_type_s*)iter->data;
4235 *account_type_list_all = g_slist_append(*account_type_list_all, account_type);
4237 g_slist_free(account_type_list);
4239 error_code = _ACCOUNT_ERROR_NONE;
4243 rc = _account_query_finalize(hstmt);
4244 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list);}, rc, ("finalize error"));
4248 _INFO("_account_type_query_all_in_global_db end");
4252 GSList* _account_type_query_all(void)
4254 account_stmt hstmt = NULL;
4255 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4257 int error_code = _ACCOUNT_ERROR_NONE;
4258 GSList *account_type_list = NULL;
4260 _INFO("_account_type_query_all start");
4261 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected."));
4263 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4265 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
4266 hstmt = _account_prepare_query(g_hAccountDB, query);
4268 rc = _account_query_step(hstmt);
4270 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
4271 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4275 account_type_s *account_type_record = NULL;
4277 if (rc != SQLITE_ROW)
4279 _INFO("[_ACCOUNT_ERROR_RECORD_NOT_FOUND]The record isn't found.");
4280 error_code = _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4284 while(rc == SQLITE_ROW) {
4285 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4287 if (account_type_record == NULL) {
4288 ACCOUNT_FATAL("malloc Failed");
4292 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4293 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4294 account_type_list = g_slist_append(account_type_list, account_type_record);
4295 rc = _account_query_step(hstmt);
4298 rc = _account_query_finalize(hstmt);
4299 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, NULL, ("finalize error"));
4304 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
4305 account_type_s *account_type = NULL;
4306 account_type = (account_type_s*)iter->data;
4307 _account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
4308 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
4311 error_code = _ACCOUNT_ERROR_NONE;
4315 rc = _account_query_finalize(hstmt);
4316 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list);}, NULL, ("finalize error"));
4320 if (error_code == _ACCOUNT_ERROR_NONE || error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4321 error_code = _account_type_query_all_from_global_db(&account_type_list);
4322 if (rc != _ACCOUNT_ERROR_NONE && rc != _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4323 ACCOUNT_ERROR( "_account_type_query_all_from_global_db fail=[%d]", rc);
4324 _account_type_gslist_account_type_free(account_type_list);
4329 _INFO("_account_type_query_all end");
4330 return account_type_list;
4333 // output parameter label must be free
4334 int _account_type_query_label_by_locale_from_global_db(const char* app_id, const char* locale, char **label)
4336 int error_code = _ACCOUNT_ERROR_NONE;
4337 account_stmt hstmt = NULL;
4338 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4339 int rc = 0, binding_count = 1;
4340 char* converted_locale = NULL;
4342 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
4343 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4344 ACCOUNT_RETURN_VAL((label != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
4345 ACCOUNT_RETURN_VAL((locale != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("locale char is null"));
4346 //Making label newly created
4348 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4350 converted_locale = _account_dup_text(locale);
4351 gchar** tokens = g_strsplit(converted_locale, "-", 2);
4353 if(tokens != NULL) {
4354 if((char*)(tokens[1]) != NULL) {
4355 char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1]));
4356 if(upper_token != NULL) {
4357 _ACCOUNT_FREE(converted_locale);
4358 converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token);
4360 _ACCOUNT_FREE(upper_token);
4365 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, converted_locale);
4366 _ACCOUNT_FREE(converted_locale);
4368 hstmt = _account_prepare_query(g_hAccountGlobalDB, query);
4370 if( _account_db_err_code(g_hAccountGlobalDB) == SQLITE_PERM ){
4371 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountGlobalDB));
4372 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4375 _account_query_bind_text(hstmt, binding_count++, app_id);
4377 rc = _account_query_step(hstmt);
4378 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4380 label_s* label_record = NULL;
4382 while (rc == SQLITE_ROW) {
4383 label_record = (label_s*) malloc(sizeof(label_s));
4385 if (label_record == NULL) {
4386 ACCOUNT_FATAL("malloc Failed");
4390 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
4392 _account_type_convert_column_to_label(hstmt,label_record);
4394 _ACCOUNT_FREE(*label);
4395 //Making label newly created
4396 *label = _account_dup_text(label_record->label);
4398 _account_type_free_label_with_items(label_record);
4400 rc = _account_query_step(hstmt);
4403 rc = _account_query_finalize(hstmt);
4404 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4407 error_code = _ACCOUNT_ERROR_NONE;
4410 if (hstmt != NULL) {
4411 rc = _account_query_finalize(hstmt);
4412 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4416 _INFO("_account_type_query_label_by_locale_from_global_db() end : error_code = %d", error_code);
4420 // output parameter label must be free
4421 int _account_type_query_label_by_locale(const char* app_id, const char* locale, char **label)
4423 int error_code = _ACCOUNT_ERROR_NONE;
4424 account_stmt hstmt = NULL;
4425 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4426 int rc = 0, binding_count = 1;
4427 char* converted_locale = NULL;
4429 ACCOUNT_RETURN_VAL((app_id != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
4430 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, _ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4431 ACCOUNT_RETURN_VAL((label != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
4432 ACCOUNT_RETURN_VAL((locale != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("locale char is null"));
4433 //Making label newly created
4435 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4437 converted_locale = _account_dup_text(locale);
4438 gchar** tokens = g_strsplit(converted_locale, "-", 2);
4440 if(tokens != NULL) {
4441 if((char*)(tokens[1]) != NULL) {
4442 char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1]));
4443 if(upper_token != NULL) {
4444 _ACCOUNT_FREE(converted_locale);
4445 converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token);
4447 _ACCOUNT_FREE(upper_token);
4452 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, converted_locale);
4453 _ACCOUNT_FREE(converted_locale);
4455 hstmt = _account_prepare_query(g_hAccountDB, query);
4457 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
4458 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4459 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4462 _account_query_bind_text(hstmt, binding_count++, app_id);
4464 rc = _account_query_step(hstmt);
4465 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, _ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4467 label_s* label_record = NULL;
4469 while (rc == SQLITE_ROW) {
4470 label_record = (label_s*) malloc(sizeof(label_s));
4472 if (label_record == NULL) {
4473 ACCOUNT_FATAL("malloc Failed");
4477 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
4479 _account_type_convert_column_to_label(hstmt,label_record);
4481 _ACCOUNT_FREE(*label);
4482 //Making label newly created
4483 *label = _account_dup_text(label_record->label);
4485 _account_type_free_label_with_items(label_record);
4487 rc = _account_query_step(hstmt);
4490 rc = _account_query_finalize(hstmt);
4491 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4494 error_code = _ACCOUNT_ERROR_NONE;
4497 if (hstmt != NULL) {
4498 rc = _account_query_finalize(hstmt);
4499 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4503 if (error_code == _ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4504 error_code = _account_type_query_label_by_locale_from_global_db(app_id, locale, label);
4507 _INFO("_account_type_query_label_by_locale() end : error_code = %d", error_code);
4511 static int _account_insert_custom(account_s *account, int account_id)
4513 _INFO("_account_insert_custom start");
4516 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4517 account_stmt hstmt = NULL;
4519 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4521 if (g_slist_length( account->custom_list)==0) {
4522 ACCOUNT_DEBUG( "_account_insert_custom, no custom data\n");
4523 return _ACCOUNT_ERROR_NONE;
4526 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
4528 rc = _account_get_record_count(g_hAccountDB, query);
4530 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
4531 ACCOUNT_ERROR( "Access failed(%d, %s)", _account_db_err_msg(g_hAccountDB));
4532 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4536 ACCOUNT_SLOGE( "_account_insert_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
4537 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4544 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
4547 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4548 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (AccountId, AppId, Key, Value) VALUES "
4549 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
4551 hstmt = _account_prepare_query(g_hAccountDB, query);
4553 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
4554 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4555 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4558 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
4560 account_custom_s* custom_data = NULL;
4561 custom_data = (account_custom_s*)iter->data;
4563 ret = _account_query_bind_int(hstmt, count++, account_id);
4564 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
4565 ret = _account_query_bind_text(hstmt, count++, account->package_name);
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->key);
4568 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4569 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
4570 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4572 rc = _account_query_step(hstmt);
4574 if (rc != SQLITE_DONE) {
4575 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4579 rc = _account_query_finalize(hstmt);
4580 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4585 _INFO("_account_insert_custom end");
4586 return _ACCOUNT_ERROR_NONE;
4589 static int _account_update_custom(account_s *account, int account_id)
4592 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4593 account_stmt hstmt = NULL;
4595 ACCOUNT_RETURN_VAL((account != NULL), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4597 if (g_slist_length( account->custom_list)==0) {
4598 ACCOUNT_DEBUG( "_account_update_custom, no custom data\n");
4599 return _ACCOUNT_ERROR_NONE;
4602 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
4604 rc = _account_get_record_count(g_hAccountDB, query);
4606 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
4607 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4608 pthread_mutex_unlock(&account_mutex);
4609 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4610 } else if( _account_db_err_code(g_hAccountDB) == SQLITE_BUSY ){
4611 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg(g_hAccountDB));
4612 pthread_mutex_unlock(&account_mutex);
4613 return _ACCOUNT_ERROR_DATABASE_BUSY;
4617 ACCOUNT_SLOGE( "_account_update_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg(g_hAccountDB));
4618 return _ACCOUNT_ERROR_RECORD_NOT_FOUND;
4621 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4623 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId=? ", ACCOUNT_CUSTOM_TABLE);
4624 hstmt = _account_prepare_query(g_hAccountDB, query);
4626 _account_query_bind_int(hstmt, count++, (int)account_id);
4627 rc = _account_query_step(hstmt);
4629 if (rc == SQLITE_BUSY) {
4630 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4631 return _ACCOUNT_ERROR_DATABASE_BUSY;
4632 } else if (rc != SQLITE_DONE) {
4633 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4634 return _ACCOUNT_ERROR_DB_FAILED;
4637 rc = _account_query_finalize(hstmt);
4638 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4643 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
4646 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4647 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AccountId, AppId, Key, Value) VALUES "
4648 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
4650 hstmt = _account_prepare_query(g_hAccountDB, query);
4652 if( _account_db_err_code(g_hAccountDB) == SQLITE_PERM ){
4653 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg(g_hAccountDB));
4654 return _ACCOUNT_ERROR_PERMISSION_DENIED;
4657 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, _ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query(g_hAccountDB, ) failed(%s).\n", _account_db_err_msg(g_hAccountDB)));
4659 account_custom_s* custom_data = NULL;
4660 custom_data = (account_custom_s*)iter->data;
4662 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
4663 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
4664 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
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->key);
4667 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4668 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
4669 ACCOUNT_RETURN_VAL((ret == _ACCOUNT_ERROR_NONE), {}, _ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4671 rc = _account_query_step(hstmt);
4673 if (rc != SQLITE_DONE) {
4674 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg(g_hAccountDB));
4678 rc = _account_query_finalize(hstmt);
4679 ACCOUNT_RETURN_VAL((rc == _ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4684 return _ACCOUNT_ERROR_NONE;