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>
33 #include <tzplatform_config.h>
36 #include <account_ipc_marshal.h>
37 #include <account_free.h>
38 #include <account-private.h>
40 #include <account-error.h>
41 #include "account-server-db.h"
43 typedef sqlite3_stmt* account_stmt;
45 #define TEST_APP_ID "org.tizen.MyAccountCoreTest"
46 #define EAS_CMDLINE "/usr/bin/eas-engine"
47 #define EMAIL_SERVICE_CMDLINE "/usr/bin/email-service"
48 #define IMS_ENGINE_CMDLINE "/usr/bin/ims-srv"
49 #define IMS_AGENT_CMDLINE "/usr/bin/ims-agent"
50 #define MDM_SERVER_CMDLINE "/usr/bin/mdm-server"
52 #define RCS_APPID "com.samsung.rcs-im"
53 #define IMS_SERVICE_APPID "ims-service"
54 #define ACTIVESYNC_APPID "eas-ui"
55 #define EMAIL_APPID "email-setting-efl"
56 #define SYNCHRONISE_APPID "setting-synchronise-efl"
57 #define DS_AGENT_CMDLINE "/usr/bin/oma-ds-agent"
59 #define FACEBOOK_SDK_APPID "com.samsung.facebook-service"
60 #define FACEBOOK_APPID "com.samsung.facebook"
62 #define EASYSIGNUP_CMDLINE "/usr/bin/esu-agent"
63 #define EASYSIGNUP_APPID "com.samsung.esu-agent"
65 #define ACCOUNT_DB_OPEN_READONLY 0
66 #define ACCOUNT_DB_OPEN_READWRITE 1
70 #define _TIZEN_PUBLIC_
71 #ifndef _TIZEN_PUBLIC_
75 static sqlite3* g_hAccountDB = NULL;
76 static sqlite3* g_hAccountDB2 = NULL;
77 static sqlite3* g_hAccountGlobalDB = NULL;
78 static sqlite3* g_hAccountGlobalDB2 = NULL;
79 pthread_mutex_t account_mutex = PTHREAD_MUTEX_INITIALIZER;
80 pthread_mutex_t account_global_mutex = PTHREAD_MUTEX_INITIALIZER;
82 int _account_db_handle_close(sqlite3* hDB);
83 static char *_account_get_text(const char *text_data);
84 static const char *_account_query_table_column_text(account_stmt pStmt, int pos);
85 static int _account_insert_custom(account_s *account, int account_id);
86 static int _account_update_custom(account_s *account, int account_id);
87 static int _account_query_custom_by_account_id(account_custom_cb callback, int account_id, void *user_data );
88 static int _account_type_update_provider_feature(account_type_s *account_type, const char* app_id);
90 int _account_query_capability_by_account_id(capability_cb callback, int account_id, void *user_data );
92 static void _account_insert_delete_update_notification_send(char *noti_name)
95 _ERR("Noti Name is NULL!!!!!!\n");
99 _INFO("noti_type = %s", noti_name);
101 if (vconf_set_str(VCONFKEY_ACCOUNT_MSG_STR, noti_name) != 0) {
102 _ERR("Vconf MSG Str set FAILED !!!!!!\n");;
106 int _account_get_current_appid_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
110 GSList** appid_list = (GSList**)user_data;
113 pkgmgr_ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
115 if( pkgmgr_ret != PMINFO_R_OK ){
116 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_appid(%d)", pkgmgr_ret);
119 item = _account_get_text(appid);
120 *appid_list = g_slist_append(*appid_list, item);
125 static inline int __read_proc(const char *path, char *buf, int size)
129 if (buf == NULL || path == NULL) {
130 ACCOUNT_ERROR("path and buffer is mandatory\n");
134 fd = open(path, O_RDONLY);
136 ACCOUNT_ERROR("fd open error(%d)\n", fd);
140 ret = read(fd, buf, size - 1);
142 ACCOUNT_ERROR("fd read error(%d)\n", fd);
153 char *_account_get_proc_cmdline_bypid(int pid)
158 ACCOUNT_SNPRINTF(buf, sizeof(buf), "/proc/%d/cmdline", pid);
159 ret = __read_proc(buf, buf, sizeof(buf));
161 ACCOUNT_DEBUG("No proc directory (%d)\n", pid);
169 static char* _account_get_current_appid(int pid)
171 _INFO("getting caller appid with pid=[%d]", pid);
174 char appid[128]={0,};
175 char* appid_ret = NULL;
177 ret = aul_app_get_appid_bypid(pid, appid, sizeof(appid));
180 ACCOUNT_ERROR("fail to get current appid ret=[%d], appid=%s\n", ret, appid);
185 /* SLP platform core exception */
186 if(strlen(appid) == 0){
188 char* cmdline = NULL;
189 cmdline = _account_get_proc_cmdline_bypid(pid);
190 ACCOUNT_SLOGD("cmdline (%s)!!!!!!\n", cmdline);
191 if(!g_strcmp0(cmdline, EAS_CMDLINE)) {
192 appid_ret = _account_get_text(ACTIVESYNC_APPID);
193 _ACCOUNT_FREE(cmdline);
195 } else if (!g_strcmp0(cmdline, EMAIL_SERVICE_CMDLINE) || !g_strcmp0(cmdline, MDM_SERVER_CMDLINE)) {
196 appid_ret = _account_get_text(EMAIL_APPID);
197 _ACCOUNT_FREE(cmdline);
199 } else if (!g_strcmp0(cmdline, IMS_ENGINE_CMDLINE) || !g_strcmp0(cmdline, IMS_AGENT_CMDLINE)) {
200 if(_account_type_query_app_id_exist_from_all_db(RCS_APPID) == ACCOUNT_ERROR_NONE) {
201 appid_ret = _account_get_text(RCS_APPID);
202 } else if(_account_type_query_app_id_exist_from_all_db(IMS_SERVICE_APPID) == ACCOUNT_ERROR_NONE) {
203 appid_ret = _account_get_text(IMS_SERVICE_APPID);
205 appid_ret = _account_get_text(RCS_APPID);
207 _ACCOUNT_FREE(cmdline);
209 } else if (!g_strcmp0(cmdline, DS_AGENT_CMDLINE)) {
210 appid_ret = _account_get_text(SYNCHRONISE_APPID);
211 _ACCOUNT_FREE(cmdline);
213 } else if (!g_strcmp0(cmdline, EASYSIGNUP_CMDLINE)) {
214 appid_ret = _account_get_text(EASYSIGNUP_APPID);
215 _ACCOUNT_FREE(cmdline);
218 ACCOUNT_DEBUG("No app id\n");
219 _ACCOUNT_FREE(cmdline);
225 /* temporary exception */
226 if(!g_strcmp0(appid, "com.samsung.gallery")){
227 appid_ret = _account_get_text("com.samsung.facebook");
228 } else if(!g_strcmp0(appid, FACEBOOK_SDK_APPID)){
229 appid_ret = _account_get_text(FACEBOOK_APPID);
231 appid_ret = _account_get_text(appid);
237 static const char *_account_db_err_msg_from_global_db()
239 return sqlite3_errmsg(g_hAccountGlobalDB);
242 static int _account_db_err_code_from_global_db()
244 return sqlite3_errcode(g_hAccountGlobalDB);
247 static int _account_get_record_count_from_global_db(const char *query)
249 _INFO("_account_get_record_count_in_global_db");
253 account_stmt pStmt = NULL;
256 _ERR("NULL query\n");
257 return ACCOUNT_ERROR_QUERY_SYNTAX_ERROR;
260 if(!g_hAccountGlobalDB) {
261 _ERR("DB is not opened\n");
262 return ACCOUNT_ERROR_DB_NOT_OPENED;
265 rc = sqlite3_prepare_v2(g_hAccountGlobalDB, query, strlen(query), &pStmt, NULL);
267 if (SQLITE_BUSY == rc){
268 _ERR("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg_from_global_db());
269 sqlite3_finalize(pStmt);
270 return ACCOUNT_ERROR_DATABASE_BUSY;
271 } else if (SQLITE_OK != rc) {
272 _ERR("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg_from_global_db());
273 sqlite3_finalize(pStmt);
274 return ACCOUNT_ERROR_DB_FAILED;
277 rc = sqlite3_step(pStmt);
278 if (SQLITE_BUSY == rc) {
279 _ERR("sqlite3_step() failed(%d, %s).", rc, _account_db_err_msg_from_global_db());
280 sqlite3_finalize(pStmt);
281 return ACCOUNT_ERROR_DATABASE_BUSY;
282 } else if (SQLITE_ROW != rc) {
283 _ERR("sqlite3_step() failed(%d, %s).", rc, _account_db_err_msg_from_global_db());
284 sqlite3_finalize(pStmt);
285 return ACCOUNT_ERROR_DB_FAILED;
288 ncount = sqlite3_column_int(pStmt, 0);
290 _INFO("account record count [%d]", ncount);
291 sqlite3_finalize(pStmt);
296 static int _account_execute_query_from_global_db(const char *query)
299 char* pszErrorMsg = NULL;
302 ACCOUNT_ERROR("NULL query\n");
303 return ACCOUNT_ERROR_QUERY_SYNTAX_ERROR;
306 if(!g_hAccountGlobalDB){
307 ACCOUNT_ERROR("Global DB is not opened\n");
308 return ACCOUNT_ERROR_DB_NOT_OPENED;
311 rc = sqlite3_exec(g_hAccountGlobalDB, query, NULL, NULL, &pszErrorMsg);
312 if (SQLITE_OK != rc) {
313 ACCOUNT_ERROR("sqlite3_exec rc(%d) query(%s) failed(%s).", rc, query, pszErrorMsg);
314 sqlite3_free(pszErrorMsg);
321 static int _account_begin_transaction_from_global_db(void)
323 ACCOUNT_DEBUG("_account_begin_transaction start");
326 ret = _account_execute_query_from_global_db("BEGIN IMMEDIATE TRANSACTION");
328 if (ret == SQLITE_BUSY){
329 ACCOUNT_ERROR(" sqlite3 busy = %d", ret);
330 return ACCOUNT_ERROR_DATABASE_BUSY;
331 } else if(ret != SQLITE_OK) {
332 ACCOUNT_ERROR("_account_svc_begin_transaction_in_global_db fail :: %d", ret);
333 return ACCOUNT_ERROR_DB_FAILED;
336 ACCOUNT_DEBUG("_account_begin_transaction_in_global_db end");
337 return ACCOUNT_ERROR_NONE;
340 static int _account_end_transaction_from_global_db(bool is_success)
342 ACCOUNT_DEBUG("_account_end_transaction_in_global_db start");
346 if (is_success == true) {
347 ret = _account_execute_query_from_global_db("COMMIT TRANSACTION");
348 ACCOUNT_DEBUG("_account_end_transaction_in_global_db COMMIT");
350 ret = _account_execute_query_from_global_db("ROLLBACK TRANSACTION");
351 ACCOUNT_DEBUG("_account_end_transaction ROLLBACK");
354 if(ret == SQLITE_PERM) {
355 ACCOUNT_ERROR("Account permission denied :: %d", ret);
356 return ACCOUNT_ERROR_PERMISSION_DENIED;
359 if (ret == SQLITE_BUSY){
360 ACCOUNT_DEBUG(" sqlite3 busy = %d", ret);
361 return ACCOUNT_ERROR_DATABASE_BUSY;
364 if (ret != SQLITE_OK) {
365 ACCOUNT_ERROR("_account_svc_end_transaction_in_global_db fail :: %d", ret);
366 return ACCOUNT_ERROR_DB_FAILED;
369 ACCOUNT_DEBUG("_account_end_transaction_in_global_db end");
370 return ACCOUNT_ERROR_NONE;
373 int _account_type_query_app_id_exist_from_global_db(const char *app_id)
375 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
378 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
379 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
381 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
383 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
385 pthread_mutex_lock(&account_global_mutex);
387 ret_transaction = _account_begin_transaction_from_global_db();
389 if(_account_db_err_code() == SQLITE_PERM){
390 pthread_mutex_unlock(&account_global_mutex);
391 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg_global());
392 return ACCOUNT_ERROR_PERMISSION_DENIED;
395 if (ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY) {
396 ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction);
397 pthread_mutex_unlock(&account_global_mutex);
398 return ACCOUNT_ERROR_DATABASE_BUSY;
399 }else if (ret_transaction != ACCOUNT_ERROR_NONE) {
400 ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction);
401 pthread_mutex_unlock(&account_global_mutex);
402 return ret_transaction;
405 rc = _account_get_record_count_from_global_db(query);
407 if( _account_db_err_code_from_global_db() == SQLITE_PERM ){
408 _ERR( "Global DB access failed(%s)", _account_db_err_msg_from_global_db());
410 return ACCOUNT_ERROR_PERMISSION_DENIED;
414 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
417 return ACCOUNT_ERROR_NONE;
420 int _account_global_db_open(void)
424 char account_db_path[256] = {0, };
426 _INFO( "start _account_global_db_open()");
428 ACCOUNT_MEMSET(account_db_path, 0x00, sizeof(account_db_path));
429 ACCOUNT_GET_GLOBAL_DB_PATH(account_db_path, sizeof(account_db_path));
431 if( g_hAccountGlobalDB ) {
432 _ERR( "Account database is using in another app. %x", g_hAccountDB );
433 return ACCOUNT_ERROR_DATABASE_BUSY;
436 ret = _account_db_handle_close(g_hAccountGlobalDB2);
437 if( ret != ACCOUNT_ERROR_NONE )
438 ACCOUNT_DEBUG( "db_util_close(g_hAccountGlobalDB2) fail ret = %d", ret);
440 ACCOUNT_DEBUG( "before db_util_open()");
441 // if(mode == ACCOUNT_DB_OPEN_READWRITE)
442 // rc = db_util_open(account_db_path, &g_hAccountDB, DB_UTIL_REGISTER_HOOK_METHOD);
443 // else if(mode == ACCOUNT_DB_OPEN_READONLY)
444 rc = db_util_open_with_options(account_db_path, &g_hAccountGlobalDB, SQLITE_OPEN_READONLY, NULL);
446 // return ACCOUNT_ERROR_DB_NOT_OPENED;
447 ACCOUNT_DEBUG( "after db_util_open() sqlite_rc = %d", rc);
449 if( rc == SQLITE_PERM || _account_db_err_code_from_global_db() == SQLITE_PERM ) {
450 ACCOUNT_ERROR( "Account permission denied");
451 return ACCOUNT_ERROR_PERMISSION_DENIED;
454 if( rc == SQLITE_BUSY ) {
455 ACCOUNT_ERROR( "busy handler fail.");
456 return ACCOUNT_ERROR_DATABASE_BUSY;
459 if( rc != SQLITE_OK ) {
460 ACCOUNT_ERROR( "The database isn't connected." );
461 return ACCOUNT_ERROR_DB_NOT_OPENED;
464 _INFO( "end _account_global_db_open()");
465 return ACCOUNT_ERROR_NONE;
468 int _account_global_db_close(void)
470 ACCOUNT_DEBUG( "start account_global_db_close()");
473 ret = _account_db_handle_close(g_hAccountGlobalDB2);
474 if( ret != ACCOUNT_ERROR_NONE )
475 ACCOUNT_DEBUG( "db_util_close(g_hAccountGlobalDB2) fail ret = %d", ret);
477 ret = _account_db_handle_close(g_hAccountGlobalDB);
478 if( ret != ACCOUNT_ERROR_NONE )
480 ACCOUNT_ERROR( "db_util_close(g_hAccountGlobalDB) fail ret = %d", ret);
481 g_hAccountGlobalDB2 = g_hAccountGlobalDB;
483 g_hAccountGlobalDB = NULL;
488 static int _account_check_account_type_with_appid_group(int uid, const char* appid, char** verified_appid)
490 int error_code = ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
491 pkgmgrinfo_appinfo_h ahandle=NULL;
492 pkgmgrinfo_pkginfo_h phandle=NULL;
493 char* package_id = NULL;
494 GSList* appid_list = NULL;
498 ACCOUNT_ERROR("input param is null\n");
499 return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
503 ACCOUNT_ERROR("output param is null\n");
504 return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
507 if(!strcmp(appid, "com.samsung.setting")){
508 ACCOUNT_DEBUG("Setting exception\n");
509 *verified_appid = _account_get_text("com.samsung.setting");
510 return ACCOUNT_ERROR_NONE;
513 if(!strcmp(appid, "com.samsung.samsung-account-front")){
514 ACCOUNT_DEBUG("Setting exception\n");
515 *verified_appid = _account_get_text("com.samsung.samsung-account-front");
516 return ACCOUNT_ERROR_NONE;
519 if(!strcmp(appid, IMS_SERVICE_APPID) || !strcmp(appid, RCS_APPID)){
520 ACCOUNT_DEBUG("ims service exception\n");
521 *verified_appid = _account_get_text(appid);
522 return ACCOUNT_ERROR_NONE;
525 if(!strcmp(appid, EASYSIGNUP_APPID)){
526 ACCOUNT_DEBUG("easysignup exception\n");
527 *verified_appid = _account_get_text(appid);
528 return ACCOUNT_ERROR_NONE;
530 /* Get app id family which is stored in account database */
533 if (uid == OWNER_ROOT || uid == GLOBAL_USER) {
534 pkgmgr_ret = pkgmgrinfo_appinfo_get_appinfo(appid, &ahandle);
536 pkgmgr_ret = pkgmgrinfo_appinfo_get_usr_appinfo(appid, uid, &ahandle);
538 if( pkgmgr_ret != PMINFO_R_OK ){
539 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_appinfo(%d)", pkgmgr_ret);
542 pkgmgr_ret = pkgmgrinfo_appinfo_get_pkgid(ahandle, &package_id);
543 if( pkgmgr_ret != PMINFO_R_OK ){
544 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_pkgid(%d)", pkgmgr_ret);
547 if (uid == OWNER_ROOT || uid == GLOBAL_USER) {
548 pkgmgr_ret = pkgmgrinfo_pkginfo_get_pkginfo(package_id, &phandle);
550 pkgmgr_ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(package_id, uid, &phandle);
552 if( pkgmgr_ret != PMINFO_R_OK ){
553 ACCOUNT_DEBUG("pkgmgrinfo_pkginfo_get_pkginfo(%d)", pkgmgr_ret);
556 if (uid == OWNER_ROOT || uid == GLOBAL_USER) {
557 pkgmgr_ret = pkgmgrinfo_appinfo_get_list(phandle, PMINFO_ALL_APP, _account_get_current_appid_cb, (void *)&appid_list);
559 pkgmgr_ret = pkgmgrinfo_appinfo_get_usr_list(phandle, PMINFO_ALL_APP, _account_get_current_appid_cb, (void *)&appid_list, uid);
561 if( pkgmgr_ret != PMINFO_R_OK ){
562 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_list(%d)", pkgmgr_ret);
565 /* Compare current app id with the stored app id family */
566 for(iter=appid_list;iter!=NULL;iter=g_slist_next(iter)){
567 char* tmp = (char*)iter->data;
569 if(_account_type_query_app_id_exist_from_all_db(tmp) == ACCOUNT_ERROR_NONE) {
570 *verified_appid = _account_get_text(tmp);
571 error_code = ACCOUNT_ERROR_NONE;
575 ACCOUNT_SLOGD("not matched owner group app id(%s), current appid(%s)\n", tmp, appid);
581 g_slist_free(appid_list);
582 pkgmgr_ret = pkgmgrinfo_pkginfo_destroy_pkginfo(phandle);
583 if( pkgmgr_ret != PMINFO_R_OK ){
584 ACCOUNT_DEBUG("pkgmgrinfo_pkginfo_destroy_pkginfo(%d)", pkgmgr_ret);
587 pkgmgr_ret = pkgmgrinfo_appinfo_destroy_appinfo(ahandle);
588 if( pkgmgr_ret != PMINFO_R_OK ){
589 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_destroy_appinfo(%d)", pkgmgr_ret);
595 static int _account_check_appid_group_with_package_name(int uid, const char* appid, char* package_name)
597 int error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
598 pkgmgrinfo_appinfo_h ahandle=NULL;
599 pkgmgrinfo_pkginfo_h phandle=NULL;
600 char* package_id = NULL;
601 GSList* appid_list = NULL;
605 ACCOUNT_ERROR("input param -appid is null\n");
606 return ACCOUNT_ERROR_INVALID_PARAMETER;
610 ACCOUNT_ERROR("input param - package name is null\n");
611 return ACCOUNT_ERROR_INVALID_PARAMETER;
614 /* ims-service Exception */
615 if ( strcmp(appid, IMS_SERVICE_APPID) == 0 && strcmp(package_name, IMS_SERVICE_APPID) == 0 ) {
616 ACCOUNT_DEBUG("ims exception."); // TODO: NEED TO REMOVE, debug log.
617 return ACCOUNT_ERROR_NONE;
620 /* easysignup Exception */
621 if ( strcmp(appid, EASYSIGNUP_APPID) == 0 && strcmp(package_name, EASYSIGNUP_APPID) == 0 ) {
622 ACCOUNT_DEBUG("easysignup exception."); // TODO: NEED TO REMOVE, debug log.
623 return ACCOUNT_ERROR_NONE;
625 /* Get app id family which is stored in account database */
627 if (uid == OWNER_ROOT || uid == GLOBAL_USER) {
628 pkgmgr_ret = pkgmgrinfo_appinfo_get_appinfo(appid, &ahandle);
630 pkgmgr_ret = pkgmgrinfo_appinfo_get_usr_appinfo(appid, uid, &ahandle);
632 if( pkgmgr_ret != PMINFO_R_OK ){
633 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_appinfo(%d)", pkgmgr_ret);
636 pkgmgr_ret = pkgmgrinfo_appinfo_get_pkgid(ahandle, &package_id);
637 if( pkgmgr_ret != PMINFO_R_OK ){
638 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_pkgid(%d)", pkgmgr_ret);
641 if (uid == OWNER_ROOT || uid == GLOBAL_USER) {
642 pkgmgr_ret = pkgmgrinfo_pkginfo_get_pkginfo(package_id, &phandle);
644 pkgmgr_ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(package_id, uid, &phandle);
646 if( pkgmgr_ret != PMINFO_R_OK ){
647 ACCOUNT_DEBUG("pkgmgrinfo_pkginfo_get_pkginfo(%d)", pkgmgr_ret);
650 if (uid == OWNER_ROOT || uid == GLOBAL_USER) {
651 pkgmgr_ret = pkgmgrinfo_appinfo_get_list(phandle, PMINFO_ALL_APP, _account_get_current_appid_cb, (void *)&appid_list);
653 pkgmgr_ret = pkgmgrinfo_appinfo_get_usr_list(phandle, PMINFO_ALL_APP, _account_get_current_appid_cb, (void *)&appid_list, uid);
655 if( pkgmgr_ret != PMINFO_R_OK ){
656 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_list(%d)", pkgmgr_ret);
659 /* Compare current app id with the stored app id family */
660 for(iter=appid_list;iter!=NULL;iter=g_slist_next(iter)){
661 char* tmp = (char*)iter->data;
663 //ACCOUNT_ERROR("tmp(%s)package_name(%s)\n\n", tmp, package_name); // TODO: NEED TO REMOVE, debug log.
664 if( strcmp(tmp, package_name) == 0) {
665 error_code = ACCOUNT_ERROR_NONE;
668 } else if ( strcmp(tmp, "com.samsung.samsung-account-front") == 0 &&
669 strcmp(package_name, "com.samsung.samsungaccount") == 0 ) {
670 /* Samung Account Exception */
671 error_code = ACCOUNT_ERROR_NONE;
675 ACCOUNT_SLOGD("not matched owner group app id(%s), current appid(%s)\n", tmp, appid);
681 g_slist_free(appid_list);
682 pkgmgr_ret = pkgmgrinfo_pkginfo_destroy_pkginfo(phandle);
683 if( pkgmgr_ret != PMINFO_R_OK ){
684 ACCOUNT_DEBUG("pkgmgrinfo_pkginfo_destroy_pkginfo(%d)", pkgmgr_ret);
687 pkgmgr_ret = pkgmgrinfo_appinfo_destroy_appinfo(ahandle);
688 if( pkgmgr_ret != PMINFO_R_OK ){
689 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_destroy_appinfo(%d)", pkgmgr_ret);
695 static int _remove_sensitive_info_from_non_owning_account(int caller_pid, account_s *account)
700 return ACCOUNT_ERROR_INVALID_PARAMETER;
703 if (account->package_name)
705 char *caller_package_name = _account_get_current_appid(caller_pid);
706 if (caller_package_name == NULL)
708 _ERR("Could not get caller app id, so removing sensitive info from account id [%d]", account->id);
709 return ACCOUNT_ERROR_INVALID_PARAMETER;
712 if (g_strcmp0(caller_package_name, account->package_name) != 0)
714 // packages dont match, so remove sensitive info
715 _INFO("Removing sensitive info from account id [%d]", account->id);
716 free (account->access_token);
717 account->access_token = NULL;
720 _ACCOUNT_FREE(caller_package_name);
721 return ACCOUNT_ERROR_NONE;
723 return ACCOUNT_ERROR_INVALID_PARAMETER;
726 static int _remove_sensitive_info_from_non_owning_account_list(int caller_pid, GList *account_list)
728 int return_code = ACCOUNT_ERROR_NONE;
730 if (account_list == NULL)
733 return ACCOUNT_ERROR_INVALID_PARAMETER;
736 GList *list_iter = NULL;
737 for (list_iter = account_list; list_iter != NULL; list_iter = g_list_next(list_iter))
739 account_s *account = (account_s *) list_iter->data;
740 int ret = _remove_sensitive_info_from_non_owning_account(caller_pid, account);
741 if( ret != ACCOUNT_ERROR_NONE)
747 static int _remove_sensitive_info_from_non_owning_account_slist(int caller_pid, GSList *account_list)
749 int return_code = ACCOUNT_ERROR_NONE;
751 if (account_list == NULL)
754 return ACCOUNT_ERROR_INVALID_PARAMETER;
757 GSList *list_iter = NULL;
758 for (list_iter = account_list; list_iter != NULL; list_iter = g_slist_next(list_iter))
760 account_s *account = (account_s *) list_iter->data;
761 int ret = _remove_sensitive_info_from_non_owning_account(caller_pid, account);
762 if( ret != ACCOUNT_ERROR_NONE)
768 static const char *_account_db_err_msg()
770 return sqlite3_errmsg(g_hAccountDB);
773 static int _account_db_err_code()
775 return sqlite3_errcode(g_hAccountDB);
778 static int _account_get_record_count(char* query)
780 _INFO("_account_get_record_count");
784 account_stmt pStmt = NULL;
787 _ERR("NULL query\n");
788 return ACCOUNT_ERROR_QUERY_SYNTAX_ERROR;
792 _ERR("DB is not opened\n");
793 return ACCOUNT_ERROR_DB_NOT_OPENED;
796 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
798 if (SQLITE_BUSY == rc){
799 _ERR("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg());
800 sqlite3_finalize(pStmt);
801 return ACCOUNT_ERROR_DATABASE_BUSY;
802 } else if (SQLITE_OK != rc) {
803 _ERR("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg());
804 sqlite3_finalize(pStmt);
805 return ACCOUNT_ERROR_DB_FAILED;
808 rc = sqlite3_step(pStmt);
809 if (SQLITE_BUSY == rc) {
810 _ERR("sqlite3_step() failed(%d, %s).", rc, _account_db_err_msg());
811 sqlite3_finalize(pStmt);
812 return ACCOUNT_ERROR_DATABASE_BUSY;
813 } else if (SQLITE_ROW != rc) {
814 _ERR("sqlite3_step() failed(%d, %s).", rc, _account_db_err_msg());
815 sqlite3_finalize(pStmt);
816 return ACCOUNT_ERROR_DB_FAILED;
819 ncount = sqlite3_column_int(pStmt, 0);
821 _INFO("account record count [%d]", ncount);
822 sqlite3_finalize(pStmt);
827 static int _account_execute_query(const char *query)
830 char* pszErrorMsg = NULL;
833 ACCOUNT_ERROR("NULL query\n");
834 return ACCOUNT_ERROR_QUERY_SYNTAX_ERROR;
838 ACCOUNT_ERROR("DB is not opened\n");
839 return ACCOUNT_ERROR_DB_NOT_OPENED;
842 rc = sqlite3_exec(g_hAccountDB, query, NULL, NULL, &pszErrorMsg);
843 if (SQLITE_OK != rc) {
844 ACCOUNT_ERROR("sqlite3_exec rc(%d) query(%s) failed(%s).", rc, query, pszErrorMsg);
845 sqlite3_free(pszErrorMsg);
851 static int _account_begin_transaction(void)
853 ACCOUNT_DEBUG("_account_begin_transaction start");
856 ret = _account_execute_query("BEGIN IMMEDIATE TRANSACTION");
858 if (ret == SQLITE_BUSY){
859 ACCOUNT_ERROR(" sqlite3 busy = %d", ret);
860 return ACCOUNT_ERROR_DATABASE_BUSY;
861 } else if(ret != SQLITE_OK) {
862 ACCOUNT_ERROR("_account_svc_begin_transaction fail :: %d", ret);
863 return ACCOUNT_ERROR_DB_FAILED;
866 ACCOUNT_DEBUG("_account_begin_transaction end");
867 return ACCOUNT_ERROR_NONE;
870 static int _account_end_transaction(bool is_success)
872 ACCOUNT_DEBUG("_account_end_transaction start");
876 if (is_success == true) {
877 ret = _account_execute_query("COMMIT TRANSACTION");
878 ACCOUNT_DEBUG("_account_end_transaction COMMIT");
880 ret = _account_execute_query("ROLLBACK TRANSACTION");
881 ACCOUNT_DEBUG("_account_end_transaction ROLLBACK");
884 if(ret == SQLITE_PERM){
885 ACCOUNT_ERROR("Account permission denied :: %d", ret);
886 return ACCOUNT_ERROR_PERMISSION_DENIED;
889 if (ret == SQLITE_BUSY){
890 ACCOUNT_DEBUG(" sqlite3 busy = %d", ret);
891 return ACCOUNT_ERROR_DATABASE_BUSY;
894 if (ret != SQLITE_OK) {
895 ACCOUNT_ERROR("_account_svc_end_transaction fail :: %d", ret);
896 return ACCOUNT_ERROR_DB_FAILED;
899 ACCOUNT_DEBUG("_account_end_transaction end");
900 return ACCOUNT_ERROR_NONE;
903 static bool _account_check_add_more_account(const char* app_id)
905 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
908 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
909 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
911 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
913 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s' and MultipleAccountSupport = 1", ACCOUNT_TYPE_TABLE, app_id);
914 rc = _account_get_record_count(query);
916 /* multiple account support case */
918 ACCOUNT_SLOGD("app id (%s) supports multiple account. rc(%d)\n", app_id, rc);
922 /* multiple account not support case */
923 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
924 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE package_name = '%s'", ACCOUNT_TABLE, app_id);
925 rc = _account_get_record_count(query);
928 ACCOUNT_SLOGD("app id (%s) supports single account. and there is no account of the app id\n", app_id);
935 //TODO: Need to enable creating db on the first connect for
936 //a) multi-user cases
937 //b) to ensure db exist in every connect call
939 static int _account_create_all_tables(void)
942 int error_code = ACCOUNT_ERROR_NONE;
943 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
945 _INFO("create all table - BEGIN");
946 ACCOUNT_MEMSET(query, 0, sizeof(query));
948 /*Create the account table*/
949 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_TABLE);
950 rc = _account_get_record_count(query);
952 rc = _account_execute_query(ACCOUNT_SCHEMA);
953 if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY;
954 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", ACCOUNT_SCHEMA, rc, _account_db_err_msg()));
958 /*Create capability table*/
959 ACCOUNT_MEMSET(query, 0, sizeof(query));
960 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", CAPABILITY_TABLE);
961 rc = _account_get_record_count(query);
963 rc = _account_execute_query(CAPABILITY_SCHEMA);
964 if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY;
965 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", CAPABILITY_SCHEMA, rc, _account_db_err_msg()));
968 /* Create account custom table */
969 ACCOUNT_MEMSET(query, 0, sizeof(query));
970 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_CUSTOM_TABLE);
971 rc = _account_get_record_count(query);
973 rc = _account_execute_query(ACCOUNT_CUSTOM_SCHEMA);
974 if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY;
975 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
978 /* Create account type table */
979 ACCOUNT_MEMSET(query, 0, sizeof(query));
980 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_TYPE_TABLE);
981 rc = _account_get_record_count(query);
983 rc = _account_execute_query(ACCOUNT_TYPE_SCHEMA);
984 if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY;
985 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", ACCOUNT_TYPE_SCHEMA, rc, _account_db_err_msg()));
988 /* Create label table */
989 ACCOUNT_MEMSET(query, 0, sizeof(query));
990 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", LABEL_TABLE);
991 rc = _account_get_record_count(query);
993 rc = _account_execute_query(LABEL_SCHEMA);
994 if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY;
995 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", LABEL_SCHEMA, rc, _account_db_err_msg()));
998 /* Create account feature table */
999 ACCOUNT_MEMSET(query, 0, sizeof(query));
1000 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", PROVIDER_FEATURE_TABLE);
1001 rc = _account_get_record_count(query);
1003 rc = _account_execute_query(PROVIDER_FEATURE_SCHEMA);
1004 if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY;
1005 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", PROVIDER_FEATURE_SCHEMA, rc, _account_db_err_msg()));
1008 _INFO("create all table - END");
1012 static int _account_check_is_all_table_exists()
1015 char query[ACCOUNT_SQL_LEN_MAX] = {0,};
1016 ACCOUNT_MEMSET(query, 0, sizeof(query));
1018 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s', '%s', '%s', '%s', '%s', '%s')",
1019 ACCOUNT_TABLE, CAPABILITY_TABLE, ACCOUNT_CUSTOM_TABLE, ACCOUNT_TYPE_TABLE, LABEL_TABLE, PROVIDER_FEATURE_TABLE);
1020 rc = _account_get_record_count(query);
1022 if (rc != ACCOUNT_TABLE_TOTAL_COUNT) {
1023 ACCOUNT_ERROR("Table count is not matched rc=%d\n", rc);
1029 int _account_db_handle_close(sqlite3* hDB)
1032 int ret = ACCOUNT_ERROR_NONE;
1035 rc = db_util_close(hDB);
1036 if( rc == SQLITE_OK )
1037 ret = ACCOUNT_ERROR_NONE;
1038 else if( rc == SQLITE_PERM )
1039 ret = ACCOUNT_ERROR_PERMISSION_DENIED;
1040 else if ( rc == SQLITE_BUSY )
1041 ret = ACCOUNT_ERROR_DATABASE_BUSY;
1043 ret = ACCOUNT_ERROR_DB_FAILED;
1048 int _account_db_open(int mode, int pid, int uid)
1052 char account_db_dir[256] = {0, };
1053 char account_db_path[256] = {0, };
1055 _INFO( "start _account_db_open()");
1057 ACCOUNT_MEMSET(account_db_dir, 0x00, sizeof(account_db_dir));
1058 ACCOUNT_MEMSET(account_db_path, 0x00, sizeof(account_db_path));
1060 ACCOUNT_GET_USER_DB_PATH(account_db_path, sizeof(account_db_path), uid);
1062 if( g_hAccountDB ) {
1063 _ERR( "Account database is using in another app. %x", g_hAccountDB );
1064 return ACCOUNT_ERROR_DATABASE_BUSY;
1067 ret = _account_db_handle_close(g_hAccountDB2);
1068 if( ret != ACCOUNT_ERROR_NONE )
1069 ACCOUNT_DEBUG( "db_util_close(g_hAccountDB2) fail ret = %d", ret);
1071 ACCOUNT_GET_USER_DB_DIR(account_db_dir, sizeof(account_db_dir), uid);
1072 if (-1 == access (account_db_dir, F_OK)) {
1073 mkdir(account_db_dir, 644);
1076 ACCOUNT_DEBUG( "before db_util_open()");
1077 // if(mode == ACCOUNT_DB_OPEN_READWRITE)
1078 rc = db_util_open(account_db_path, &g_hAccountDB, DB_UTIL_REGISTER_HOOK_METHOD);
1079 // else if(mode == ACCOUNT_DB_OPEN_READONLY)
1080 // rc = db_util_open_with_options(account_db_path, &g_hAccountDB, SQLITE_OPEN_READONLY, NULL);
1082 // return ACCOUNT_ERROR_DB_NOT_OPENED;
1083 ACCOUNT_DEBUG( "after db_util_open() sqlite_rc = %d", rc);
1085 if( rc == SQLITE_PERM || _account_db_err_code() == SQLITE_PERM ) {
1086 ACCOUNT_ERROR( "Account permission denied");
1087 return ACCOUNT_ERROR_PERMISSION_DENIED;
1090 if( rc == SQLITE_BUSY ) {
1091 ACCOUNT_ERROR( "busy handler fail.");
1092 return ACCOUNT_ERROR_DATABASE_BUSY;
1095 if( rc != SQLITE_OK ) {
1096 ACCOUNT_ERROR( "The database isn't connected." );
1097 return ACCOUNT_ERROR_DB_NOT_OPENED;
1100 rc = _account_check_is_all_table_exists();
1103 _ERR("_account_check_is_all_table_exists rc=[%d]", rc);
1105 } else if (rc == ACCOUNT_TABLE_TOTAL_COUNT) {
1108 int ret = _account_create_all_tables();
1109 if (ret != ACCOUNT_ERROR_NONE) {
1110 _ERR("_account_create_all_tables fail ret=[%d]", ret);
1115 _INFO( "end _account_db_open()");
1116 return ACCOUNT_ERROR_NONE;
1119 int _account_db_close(void)
1121 ACCOUNT_DEBUG( "start db_util_close()");
1124 ret = _account_db_handle_close(g_hAccountDB2);
1125 if( ret != ACCOUNT_ERROR_NONE )
1126 ACCOUNT_DEBUG( "db_util_close(g_hAccountDB2) fail ret = %d", ret);
1128 ret = _account_db_handle_close(g_hAccountDB);
1129 if( ret != ACCOUNT_ERROR_NONE )
1131 ACCOUNT_ERROR( "db_util_close(g_hAccountDB) fail ret = %d", ret);
1132 g_hAccountDB2 = g_hAccountDB;
1134 g_hAccountDB = NULL;
1139 static int _account_check_duplicated(account_s *data, const char* verified_appid)
1141 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1145 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1147 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s where package_name='%s' and (user_name='%s' or display_name='%s' or email_address='%s')"
1148 , ACCOUNT_TABLE, verified_appid, data->user_name, data->display_name, data->email_address);
1150 count = _account_get_record_count(query);
1153 return ACCOUNT_ERROR_NONE;
1156 //check whether duplicated account or not.
1157 //1. check user_name
1158 //2. check display_name
1159 //3. check email_address
1160 GList* account_list_temp = _account_query_account_by_package_name(getpid(), verified_appid, &ret);
1161 if (account_list_temp == NULL)
1163 _ERR("_account_query_account_by_package_name returned NULL");
1164 return ACCOUNT_ERROR_DB_FAILED;
1167 if( _account_db_err_code() == SQLITE_PERM ){
1168 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
1169 return ACCOUNT_ERROR_PERMISSION_DENIED;
1172 if(ret != ACCOUNT_ERROR_NONE){
1176 account_list_temp = g_list_first(account_list_temp);
1177 _INFO("account_list_temp length=[%d]",g_list_length(account_list_temp));
1180 for (iter = account_list_temp; iter != NULL; iter = g_list_next(iter))
1182 _INFO("iterating account_list_temp");
1183 account_s *account = NULL;
1184 _INFO("Before iter->data");
1185 account = (account_s*)iter->data;
1186 _INFO("After iter->data");
1187 if (account != NULL)
1189 if(account->user_name!=NULL && data->user_name!=NULL && strcmp(account->user_name, data->user_name)==0)
1191 _INFO("duplicated account(s) exist!, same user_name=%s", data->user_name);
1192 return ACCOUNT_ERROR_DUPLICATED;
1194 //when user_name is not NULL and display_name is same.
1195 if(account->user_name==NULL && data->user_name==NULL && account->display_name!=NULL && data->display_name!=NULL && strcmp(account->display_name, data->display_name)==0)
1197 _INFO("duplicated account(s) exist!, same display_name=%s", data->display_name);
1198 return ACCOUNT_ERROR_DUPLICATED;
1200 //when user_name and display_name are not NULL and email_address is same.
1201 if(account->user_name==NULL && data->user_name==NULL && account->display_name==NULL && data->display_name==NULL && account->email_address!=NULL && data->email_address!=NULL && strcmp(account->email_address, data->email_address)==0)
1203 _INFO("duplicated account(s) exist!, same email_address=%s", data->email_address);
1204 return ACCOUNT_ERROR_DUPLICATED;
1209 return ACCOUNT_ERROR_NONE;
1212 static int _account_get_next_sequence(const char *pszName)
1215 account_stmt pStmt = NULL;
1217 char szQuery[ACCOUNT_SQL_LEN_MAX] = {0,};
1219 ACCOUNT_MEMSET(szQuery, 0x00, sizeof(szQuery));
1220 ACCOUNT_SNPRINTF(szQuery, sizeof(szQuery), "SELECT max(seq) FROM %s where name = '%s' ", ACCOUNT_SQLITE_SEQ, pszName);
1221 rc = sqlite3_prepare_v2(g_hAccountDB, szQuery, strlen(szQuery), &pStmt, NULL);
1222 if (SQLITE_OK != rc) {
1223 ACCOUNT_SLOGE("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg());
1224 sqlite3_finalize(pStmt);
1225 return ACCOUNT_ERROR_DB_FAILED;
1228 rc = sqlite3_step(pStmt);
1229 max_seq = sqlite3_column_int(pStmt, 0);
1232 /*Finalize Statement*/
1233 rc = sqlite3_finalize(pStmt);
1239 static account_stmt _account_prepare_query(char *query)
1242 account_stmt pStmt = NULL;
1244 ACCOUNT_RETURN_VAL((query != NULL), {}, NULL, ("query is NULL"));
1246 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
1248 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, NULL, ("sqlite3_prepare_v2(%s) failed(%s).", query, _account_db_err_msg()));
1253 static account_stmt _account_prepare_query_from_global_db(char *query)
1256 account_stmt pStmt = NULL;
1258 ACCOUNT_RETURN_VAL((query != NULL), {}, NULL, ("query is NULL"));
1260 rc = sqlite3_prepare_v2(g_hAccountGlobalDB, query, strlen(query), &pStmt, NULL);
1262 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, NULL, ("sqlite3_prepare_v2(%s) failed(%s).", query, _account_db_err_msg_from_global_db()));
1267 static int _account_query_bind_int(account_stmt pStmt, int pos, int num)
1270 ACCOUNT_ERROR("statement is null");
1275 ACCOUNT_ERROR("invalid pos");
1279 return sqlite3_bind_int(pStmt, pos, num);
1282 static int _account_query_bind_text(account_stmt pStmt, int pos, const char *str)
1284 _INFO("_account_query_bind_text");
1288 _ERR("statement is null");
1294 _INFO("sqlite3_bind_text");
1295 return sqlite3_bind_text(pStmt, pos, (const char*)str, strlen(str), SQLITE_STATIC);
1299 _INFO("sqlite3_bind_null");
1300 return sqlite3_bind_null(pStmt, pos);
1304 static int _account_convert_account_to_sql(account_s *account, account_stmt hstmt, char *sql_value)
1310 /*Caution : Keep insert query orders.*/
1313 _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1314 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], user_name=%s", account->id, account->user_name);
1316 /* 2. email address*/
1317 _account_query_bind_text(hstmt, count++, (char*)account->email_address);
1318 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], email_address=%s", account->id, account->email_address);
1320 /* 3. display name*/
1321 _account_query_bind_text(hstmt, count++, (char*)account->display_name);
1322 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], display_name=%s", account->id, account->display_name);
1325 _account_query_bind_text(hstmt, count++, (char*)account->icon_path);
1326 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], icon_path=%s", account->id, account->icon_path);
1329 _account_query_bind_text(hstmt, count++, (char*)account->source);
1330 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], source=%s", account->id, account->source);
1332 /* 6. package name*/
1333 _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1334 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], package_name=%s", account->id, account->package_name);
1336 /* 7. access token*/
1337 _account_query_bind_text(hstmt, count++, (char*)account->access_token);
1338 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], access_token=%s", account->id, account->access_token);
1341 _account_query_bind_text(hstmt, count++, (char*)account->domain_name);
1342 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], domain_name=%s", account->id, account->domain_name);
1345 _account_query_bind_int(hstmt, count++, account->auth_type);
1346 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], auth_type=%d", account->id, account->auth_type);
1349 _account_query_bind_int(hstmt, count++, account->secret);
1350 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], secret=%d", account->id, account->secret);
1352 /* 11. sync_support */
1353 _account_query_bind_int(hstmt, count++, account->sync_support);
1354 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], sync_support=%d", account->id, account->sync_support);
1359 for(i=0; i< USER_TXT_CNT; i++)
1360 _account_query_bind_text(hstmt, count++, (char*)account->user_data_txt[i]);
1362 /* 13. user integer */
1363 for(i=0; i< USER_INT_CNT; i++)
1365 _account_query_bind_int(hstmt, count++, account->user_data_int[i]);
1366 _INFO("convert user_data_int : marshal_user_int data_int[%d]=%d", i, account->user_data_int[i]);
1374 static int _account_query_finalize(account_stmt pStmt)
1379 ACCOUNT_ERROR( "pStmt is NULL");
1380 return ACCOUNT_ERROR_INVALID_PARAMETER;
1383 rc = sqlite3_finalize(pStmt);
1384 if (rc == SQLITE_BUSY){
1385 ACCOUNT_ERROR(" sqlite3 busy = %d", rc);
1386 return ACCOUNT_ERROR_DATABASE_BUSY;
1387 } else if (rc != SQLITE_OK) {
1388 ACCOUNT_ERROR( "sqlite3_finalize fail, rc : %d, db_error : %s\n", rc, _account_db_err_msg());
1389 return ACCOUNT_ERROR_DB_FAILED;
1392 return ACCOUNT_ERROR_NONE;
1395 static int _account_query_finalize_from_global_db(account_stmt pStmt)
1400 ACCOUNT_ERROR( "pStmt is NULL");
1401 return ACCOUNT_ERROR_INVALID_PARAMETER;
1404 rc = sqlite3_finalize(pStmt);
1405 if (rc == SQLITE_BUSY){
1406 ACCOUNT_ERROR(" sqlite3 busy = %d", rc);
1407 return ACCOUNT_ERROR_DATABASE_BUSY;
1408 } else if (rc != SQLITE_OK) {
1409 ACCOUNT_ERROR( "sqlite3_finalize fail, rc : %d, db_error : %s\n", rc, _account_db_err_msg_from_global_db());
1410 return ACCOUNT_ERROR_DB_FAILED;
1413 return ACCOUNT_ERROR_NONE;
1416 static int _account_query_step(account_stmt pStmt)
1419 ACCOUNT_ERROR( "pStmt is NULL");
1423 return sqlite3_step(pStmt);
1426 static int _account_execute_insert_query(account_s *account)
1428 _INFO("_account_execute_insert_query start");
1431 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1432 int error_code = ACCOUNT_ERROR_NONE;
1433 account_stmt hstmt = NULL;
1435 /* check whether app id exist in account type db */
1437 if (!account->user_name && !account->display_name && !account->email_address) {
1439 ACCOUNT_ERROR("Mandetory fields is NULL. At least one field is required among username, display name, email address\n");
1440 return ACCOUNT_ERROR_INVALID_PARAMETER;
1444 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1445 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (user_name, email_address , display_name , icon_path , source , package_name , "
1446 "access_token , domain_name , auth_type , secret , sync_support , txt_custom0, txt_custom1, txt_custom2, txt_custom3, txt_custom4, "
1447 "int_custom0, int_custom1, int_custom2, int_custom3, int_custom4, txt_custom0 ) values " // to do urusa
1448 "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", ACCOUNT_TABLE);
1450 hstmt = _account_prepare_query(query);
1451 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
1454 _account_convert_account_to_sql(account, hstmt, query);
1457 rc = _account_query_step(hstmt);
1458 if (rc != SQLITE_DONE) {
1460 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1462 if( _account_db_err_code() == SQLITE_PERM )
1463 error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
1465 error_code = ACCOUNT_ERROR_DB_FAILED;
1469 rc = _account_query_finalize(hstmt);
1470 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1473 _INFO("_account_execute_insert_query end");
1477 static int _account_insert_capability(account_s *account, int account_id)
1479 _INFO("_account_insert_capability start");
1481 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1482 account_stmt hstmt = NULL;
1484 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1486 if (g_slist_length( account->capablity_list)==0) {
1487 ACCOUNT_DEBUG( "_account_insert_capability, no capability\n");
1488 return ACCOUNT_ERROR_NONE;
1491 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
1493 _INFO("_account_insert_capability _account_get_record_count [%s]", query);
1494 rc = _account_get_record_count(query);
1496 if( _account_db_err_code() == SQLITE_PERM ){
1497 _ERR( "Access failed(%s)", _account_db_err_msg());
1498 return ACCOUNT_ERROR_PERMISSION_DENIED;
1501 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1508 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1512 account_capability_s* cap_data = NULL;
1513 cap_data = (account_capability_s*)iter->data;
1515 _INFO("cap_data->type = %s, cap_data->value = %d \n", cap_data->type, cap_data->value);
1517 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1518 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
1519 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
1520 hstmt = _account_prepare_query(query);
1522 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
1524 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
1525 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1526 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
1527 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1528 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1529 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1530 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1531 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1532 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
1533 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1535 rc = _account_query_step(hstmt);
1536 _INFO("_account_insert_capability _account_query_step[%d]", rc);
1538 if (rc != SQLITE_DONE) {
1539 _ERR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1543 rc = _account_query_finalize(hstmt);
1544 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1549 _INFO("_account_insert_capability end");
1550 return ACCOUNT_ERROR_NONE;
1553 static int _account_update_capability(account_s *account, int account_id)
1556 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1557 account_stmt hstmt = NULL;
1559 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1561 if (g_slist_length( account->capablity_list)==0) {
1562 ACCOUNT_ERROR( "_account_update_capability, no capability\n");
1563 return ACCOUNT_ERROR_NONE;
1566 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
1568 rc = _account_get_record_count(query);
1571 ACCOUNT_SLOGI( "_account_update_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
1572 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1575 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1577 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id=? ", CAPABILITY_TABLE);
1578 hstmt = _account_prepare_query(query);
1580 _account_query_bind_int(hstmt, count++, (int)account_id);
1581 rc = _account_query_step(hstmt);
1583 if (rc != SQLITE_DONE) {
1584 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1585 return ACCOUNT_ERROR_DB_FAILED;
1587 rc = _account_query_finalize(hstmt);
1588 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1593 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1596 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1597 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
1598 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
1600 hstmt = _account_prepare_query(query);
1602 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
1604 account_capability_s* cap_data = NULL;
1605 cap_data = (account_capability_s*)iter->data;
1607 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
1608 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1609 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
1610 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1611 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1612 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1613 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1614 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1615 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
1616 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1618 rc = _account_query_step(hstmt);
1620 if (rc != SQLITE_DONE) {
1621 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1625 rc = _account_query_finalize(hstmt);
1626 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1631 return ACCOUNT_ERROR_NONE;
1634 static int _account_update_capability_by_user_name(account_s *account, const char *user_name, const char *package_name )
1637 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1638 account_stmt hstmt = NULL;
1640 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1642 if (g_slist_length( account->capablity_list)==0) {
1643 ACCOUNT_ERROR( "_account_update_capability_by_user_name, no capability\n");
1644 return ACCOUNT_ERROR_NONE;
1647 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where package_name= '%s' and user_name='%s'", ACCOUNT_TABLE, package_name, user_name);
1649 rc = _account_get_record_count(query);
1652 ACCOUNT_SLOGI( "_account_update_capability_by_user_name : related account item is not existed rc=%d , %s ", rc, _account_db_err_msg());
1653 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1656 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1658 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name=? and user_name=? ", CAPABILITY_TABLE);
1659 hstmt = _account_prepare_query(query);
1661 _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1662 _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1663 rc = _account_query_step(hstmt);
1664 if (rc != SQLITE_DONE) {
1665 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1666 return ACCOUNT_ERROR_DB_FAILED;
1669 rc = _account_query_finalize(hstmt);
1670 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1675 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1678 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1679 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
1680 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
1682 hstmt = _account_prepare_query(query);
1684 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
1686 account_capability_s* cap_data = NULL;
1687 cap_data = (account_capability_s*)iter->data;
1689 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
1690 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1691 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
1692 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1693 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1694 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1695 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1696 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1697 ret = _account_query_bind_int(hstmt, count++, (int)account->id);
1698 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1700 rc = _account_query_step(hstmt);
1702 if (rc != SQLITE_DONE) {
1703 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1707 rc = _account_query_finalize(hstmt);
1708 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1713 return ACCOUNT_ERROR_NONE;
1716 static int _account_query_table_column_int(account_stmt pStmt, int pos)
1719 ACCOUNT_ERROR("statement is null");
1724 ACCOUNT_ERROR("invalid pos");
1728 return sqlite3_column_int(pStmt, pos);
1731 static const char *_account_query_table_column_text(account_stmt pStmt, int pos)
1734 ACCOUNT_ERROR("statement is null");
1739 ACCOUNT_ERROR("invalid pos");
1743 return (const char*)sqlite3_column_text(pStmt, pos);
1746 static void _account_db_data_to_text(const char *textbuf, char **output)
1748 if (textbuf && strlen(textbuf)>0) {
1753 *output = strdup(textbuf);
1757 static void _account_convert_column_to_account(account_stmt hstmt, account_s *account_record)
1759 const char *textbuf = NULL;
1761 account_record->id = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_ID);
1762 ACCOUNT_DEBUG("account_record->id =[%d]", account_record->id);
1764 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_NAME);
1765 _account_db_data_to_text(textbuf, &(account_record->user_name));
1767 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_EMAIL_ADDRESS);
1768 _account_db_data_to_text(textbuf, &(account_record->email_address));
1770 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DISPLAY_NAME);
1771 _account_db_data_to_text(textbuf, &(account_record->display_name));
1773 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ICON_PATH);
1774 _account_db_data_to_text(textbuf, &(account_record->icon_path));
1776 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_SOURCE);
1777 _account_db_data_to_text(textbuf, &(account_record->source));
1779 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_PACKAGE_NAME);
1780 _account_db_data_to_text(textbuf, &(account_record->package_name));
1782 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ACCESS_TOKEN);
1783 _account_db_data_to_text(textbuf, &(account_record->access_token));
1785 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DOMAIN_NAME);
1786 _account_db_data_to_text(textbuf, &(account_record->domain_name));
1788 account_record->auth_type = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_AUTH_TYPE);
1790 account_record->secret = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SECRET);
1792 account_record->sync_support = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SYNC_SUPPORT);
1794 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_0);
1795 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[0]));
1797 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_1);
1798 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[1]));
1800 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_2);
1801 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[2]));
1803 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_3);
1804 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[3]));
1806 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_4);
1807 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[4]));
1809 account_record->user_data_int[0] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_0);
1810 account_record->user_data_int[1] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_1);
1811 account_record->user_data_int[2] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_2);
1812 account_record->user_data_int[3] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_3);
1813 account_record->user_data_int[4] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_4);
1816 static void _account_convert_column_to_capability(account_stmt hstmt, account_capability_s *capability_record)
1818 const char *textbuf = NULL;
1820 _INFO("start _account_convert_column_to_capability()");
1821 capability_record->id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ID);
1823 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_KEY);
1824 _account_db_data_to_text(textbuf, &(capability_record->type));
1826 capability_record->value = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_VALUE);
1828 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_PACKAGE_NAME);
1829 _account_db_data_to_text(textbuf, &(capability_record->package_name));
1831 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_USER_NAME);
1832 _account_db_data_to_text(textbuf, &(capability_record->user_name));
1834 capability_record->account_id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ACCOUNT_ID);
1835 _INFO("type = %s, value = %d", capability_record->type, capability_record->value);
1836 _INFO("end _account_convert_column_to_capability()");
1839 static void _account_convert_column_to_custom(account_stmt hstmt, account_custom_s *custom_record)
1841 _INFO("start _account_convert_column_to_custom()");
1842 const char *textbuf = NULL;
1844 custom_record->account_id = _account_query_table_column_int(hstmt, ACCOUNT_CUSTOM_FIELD_ACCOUNT_ID);
1846 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_APP_ID);
1847 _account_db_data_to_text(textbuf, &(custom_record->app_id));
1849 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_KEY);
1850 _account_db_data_to_text(textbuf, &(custom_record->key));
1852 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_VALUE);
1853 _account_db_data_to_text(textbuf, &(custom_record->value));
1854 _INFO("key = %s, value = %s", custom_record->key, custom_record->value);
1855 _INFO("end _account_convert_column_to_custom()");
1858 bool _account_get_capability_text_cb(const char* capability_type, account_capability_state_e capability_value, void *user_data)
1860 account_s *data = (account_s*)user_data;
1862 account_capability_s *cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
1864 if (cap_data == NULL)
1866 ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
1868 cap_data->type = _account_get_text(capability_type);
1869 cap_data->value = capability_value;
1870 _INFO("cap_data->type = %s, cap_data->value = %d", cap_data->type, cap_data->value);
1872 data->capablity_list = g_slist_append(data->capablity_list, (gpointer)cap_data);
1878 bool _account_get_custom_text_cb(char* key, char* value, void *user_data)
1880 account_s *data = (account_s*)user_data;
1882 account_custom_s *custom_data = (account_custom_s*)malloc(sizeof(account_custom_s));
1884 if (custom_data == NULL) {
1885 ACCOUNT_DEBUG("_account_get_custom_text_cb :: malloc fail\n");
1888 ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s));
1890 custom_data->account_id = data->id;
1891 custom_data->app_id = _account_get_text(data->package_name);
1892 custom_data->key = _account_get_text(key);
1893 custom_data->value = _account_get_text(value);
1894 _INFO("custom_data->key = %s, custom_data->value = %s", custom_data->key, custom_data->value);
1896 data->custom_list = g_slist_append(data->custom_list, (gpointer)custom_data);
1902 static char *_account_get_text(const char *text_data)
1904 char *text_value = NULL;
1906 if (text_data != NULL) {
1907 text_value = strdup(text_data);
1912 static int _account_compare_old_record_by_user_name(account_s *new_account, const char* user_name, const char* package_name)
1914 int error_code = ACCOUNT_ERROR_NONE;
1915 account_stmt hstmt = NULL;
1916 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1918 account_s *old_account = NULL;
1920 ACCOUNT_RETURN_VAL((new_account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
1921 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
1922 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
1923 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1925 old_account = (account_s*)calloc(1, sizeof(account_s));
1927 ACCOUNT_FATAL("Memory alloc fail\n");
1928 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1931 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1933 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = '%s' and package_name='%s'", ACCOUNT_TABLE, user_name, package_name);
1934 hstmt = _account_prepare_query(query);
1936 rc = _account_query_step(hstmt);
1937 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1939 while (rc == SQLITE_ROW) {
1940 _account_convert_column_to_account(hstmt, old_account);
1941 rc = _account_query_step(hstmt);
1944 rc = _account_query_finalize(hstmt);
1945 ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1949 error_code = _account_query_capability_by_account_id(_account_get_capability_text_cb, old_account->id, (void*)old_account);
1950 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
1953 error_code = _account_query_custom_by_account_id(_account_get_custom_text_cb, old_account->id, (void*)old_account);
1954 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
1957 new_account->id = old_account->id;
1960 if(!new_account->user_name) {
1961 if(old_account->user_name)
1962 new_account->user_name = _account_get_text(old_account->user_name);
1966 if(!new_account->display_name) {
1967 if(old_account->display_name)
1968 new_account->display_name = _account_get_text(old_account->display_name);
1972 if(!new_account->email_address) {
1973 if(old_account->email_address)
1974 new_account->email_address = _account_get_text(old_account->email_address);
1978 if(!new_account->domain_name) {
1979 if(old_account->domain_name)
1980 new_account->domain_name = _account_get_text(old_account->domain_name);
1984 if(!new_account->icon_path) {
1985 if(old_account->icon_path)
1986 new_account->icon_path = _account_get_text(old_account->icon_path);
1990 if(!new_account->source) {
1991 if(old_account->source)
1992 new_account->source = _account_get_text(old_account->source);
1995 _ACCOUNT_FREE(new_account->package_name);
1996 new_account->package_name = _account_get_text(old_account->package_name);
1999 if(!new_account->access_token) {
2000 if(old_account->access_token)
2001 new_account->access_token = _account_get_text(old_account->access_token);
2005 if(new_account->auth_type == ACCOUNT_AUTH_TYPE_INVALID) {
2006 new_account->auth_type = old_account->auth_type;
2010 if(new_account->secret== ACCOUNT_SECRECY_INVALID) {
2011 new_account->secret = old_account->secret;
2015 if(new_account->sync_support == ACCOUNT_SYNC_INVALID) {
2016 new_account->sync_support = old_account->sync_support;
2021 for(i=0;i<USER_TXT_CNT;i++) {
2022 if(!new_account->user_data_txt[i]) {
2023 if(old_account->user_data_txt[i])
2024 new_account->user_data_txt[i] = _account_get_text(old_account->user_data_txt[i]);
2029 for(i=0;i<USER_INT_CNT;i++) {
2030 if(new_account->user_data_int[i] == 0) {
2031 new_account->user_data_int[i] = old_account->user_data_int[i];
2037 // user custom table
2041 _account_free_account_with_items(old_account);
2044 if (hstmt != NULL) {
2045 rc = _account_query_finalize(hstmt);
2046 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2050 return ACCOUNT_ERROR_NONE;
2055 static int _account_update_account_by_user_name(int pid, int uid, account_s *account, const char *user_name, const char *package_name)
2057 int rc = 0, binding_count = 0, count = 0;
2058 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2059 int error_code = ACCOUNT_ERROR_NONE;
2060 account_stmt hstmt = NULL;
2062 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is NULL.\n"));
2063 ACCOUNT_RETURN_VAL((package_name!= NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is NULL.\n"));
2065 char* current_appid = NULL;
2066 char* verified_appid = NULL;
2068 current_appid = _account_get_current_appid(pid);
2069 error_code = _account_check_account_type_with_appid_group(uid, current_appid, &verified_appid);
2071 _ACCOUNT_FREE(current_appid);
2072 _ACCOUNT_FREE(verified_appid);
2074 if(error_code != ACCOUNT_ERROR_NONE){
2075 ACCOUNT_ERROR("No permission to update\n");
2076 return ACCOUNT_ERROR_PERMISSION_DENIED;
2079 _account_compare_old_record_by_user_name(account, user_name, package_name);
2081 if( _account_db_err_code() == SQLITE_PERM ){
2082 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2083 return ACCOUNT_ERROR_PERMISSION_DENIED;
2086 if (!account->package_name) {
2087 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
2088 return ACCOUNT_ERROR_INVALID_PARAMETER;
2091 if (!account->user_name && !account->display_name && !account->email_address) {
2092 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
2093 return ACCOUNT_ERROR_INVALID_PARAMETER;
2096 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE user_name='%s' and package_name='%s'"
2097 , ACCOUNT_TABLE, user_name, package_name);
2099 count = _account_get_record_count(query);
2101 if( _account_db_err_code() == SQLITE_PERM ){
2102 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2103 return ACCOUNT_ERROR_PERMISSION_DENIED;
2107 ACCOUNT_SLOGI("_account_update_account_by_user_name : The account not exist!, count = %d, user_name=%s, package_name=%s\n",
2108 count, user_name, package_name);
2109 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2112 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2114 //TODO: Is it required to update id ? As of now I can only think of falied rollback cases (between account and gSSO DB)
2115 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
2116 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
2117 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
2118 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE user_name=? and package_name=? ", ACCOUNT_TABLE);
2120 hstmt = _account_prepare_query(query);
2121 if( _account_db_err_code() == SQLITE_PERM ){
2122 _account_end_transaction(FALSE);
2123 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2124 return ACCOUNT_ERROR_PERMISSION_DENIED;
2126 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg()));
2128 binding_count = _account_convert_account_to_sql(account, hstmt, query);
2130 _account_query_bind_text(hstmt, binding_count++, user_name);
2131 _account_query_bind_text(hstmt, binding_count++, package_name);
2132 rc = _account_query_step(hstmt);
2133 if (rc != SQLITE_DONE) {
2134 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
2136 rc = _account_query_finalize(hstmt);
2137 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2140 /*update capability*/
2141 error_code = _account_update_capability_by_user_name(account, user_name, package_name);
2144 error_code = _account_update_custom(account, account->id);
2149 int _account_insert_to_db(account_s* account, int pid, int uid, int *account_id)
2152 int error_code = ACCOUNT_ERROR_NONE;
2153 int ret_transaction = 0;
2155 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2156 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
2157 ACCOUNT_RETURN_VAL((account_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID POINTER IS NULL"));
2159 if (!account->user_name && !account->display_name && !account->email_address) {
2160 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
2161 return ACCOUNT_ERROR_INVALID_PARAMETER;
2164 account_s *data = (account_s*)account;
2165 ACCOUNT_SLOGD("(%s)-(%d) account_insert_to_db: begin_transaction.\n", __FUNCTION__, __LINE__);
2167 pthread_mutex_lock(&account_mutex);
2169 /* transaction control required*/
2170 ret_transaction = _account_begin_transaction();
2172 if(_account_db_err_code() == SQLITE_PERM){
2173 pthread_mutex_unlock(&account_mutex);
2174 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2175 return ACCOUNT_ERROR_PERMISSION_DENIED;
2178 if (ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY) {
2179 ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction);
2180 pthread_mutex_unlock(&account_mutex);
2181 return ACCOUNT_ERROR_DATABASE_BUSY;
2182 }else if (ret_transaction != ACCOUNT_ERROR_NONE) {
2183 ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction);
2184 pthread_mutex_unlock(&account_mutex);
2185 return ret_transaction;
2188 *account_id = _account_get_next_sequence(ACCOUNT_TABLE);
2189 data->id = *account_id;
2192 appid = _account_get_current_appid(pid);
2197 // API caller cannot be recognized
2198 ret_transaction = _account_end_transaction(FALSE);
2199 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
2200 pthread_mutex_unlock(&account_mutex);
2201 return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
2205 char* verified_appid = NULL;
2206 error_code = _account_check_account_type_with_appid_group(uid, appid, &verified_appid);//FIX
2207 _ACCOUNT_FREE(appid);
2208 if(error_code != ACCOUNT_ERROR_NONE)
2210 _ERR("error_code = %d", error_code);
2211 ret_transaction = _account_end_transaction(FALSE);
2212 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
2213 _ACCOUNT_FREE(verified_appid);
2214 pthread_mutex_unlock(&account_mutex);
2221 error_code = _account_check_duplicated(data, verified_appid);
2222 if (error_code != ACCOUNT_ERROR_NONE) {
2224 ret_transaction = _account_end_transaction(FALSE);
2225 ACCOUNT_DEBUG("_account_check_duplicated(), rollback insert query(%x)!!!!\n", ret_transaction);
2227 pthread_mutex_unlock(&account_mutex);
2230 if(!_account_check_add_more_account(verified_appid)) {
2231 ret_transaction = _account_end_transaction(FALSE);
2232 ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
2233 pthread_mutex_unlock(&account_mutex);
2234 _ACCOUNT_FREE(verified_appid);
2235 return ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
2238 _ACCOUNT_FREE(data->package_name);
2239 data->package_name = _account_get_text(verified_appid);
2240 _ACCOUNT_FREE(verified_appid);
2243 if(!_account_check_add_more_account(data->package_name))
2246 ret_transaction = _account_end_transaction(FALSE);
2247 ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
2248 pthread_mutex_unlock(&account_mutex);
2249 return ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
2252 error_code = _account_execute_insert_query(data);
2254 if (error_code != ACCOUNT_ERROR_NONE)
2257 ret_transaction = _account_end_transaction(FALSE);
2258 ACCOUNT_ERROR("INSERT account fail, rollback insert query(%x)!!!!\n", ret_transaction);
2260 pthread_mutex_unlock(&account_mutex);
2265 error_code = _account_insert_capability(data, *account_id);
2266 if (error_code != ACCOUNT_ERROR_NONE)
2269 ret_transaction = _account_end_transaction(FALSE);
2270 ACCOUNT_ERROR("INSERT capability fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
2272 pthread_mutex_unlock(&account_mutex);
2277 error_code = _account_insert_custom(data, *account_id);
2278 if (error_code != ACCOUNT_ERROR_NONE)
2280 ret_transaction = _account_end_transaction(FALSE);
2281 ACCOUNT_ERROR("INSERT custom fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
2283 pthread_mutex_unlock(&account_mutex);
2289 pthread_mutex_unlock(&account_mutex);
2290 _account_end_transaction(TRUE);
2291 ACCOUNT_SLOGD("(%s)-(%d) account _end_transaction.\n", __FUNCTION__, __LINE__);
2294 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_INSERT, *account_id);
2295 _account_insert_delete_update_notification_send(buf);
2296 _INFO("account _notification_send end.");
2298 return ACCOUNT_ERROR_NONE;
2302 int _account_query_capability_by_account_id(capability_cb callback, int account_id, void *user_data )
2304 int error_code = ACCOUNT_ERROR_NONE;
2305 account_stmt hstmt = NULL;
2306 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2309 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2310 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
2311 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2313 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2315 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
2316 hstmt = _account_prepare_query(query);
2318 if( _account_db_err_code() == SQLITE_PERM ){
2319 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2320 return ACCOUNT_ERROR_PERMISSION_DENIED;
2323 rc = _account_query_step(hstmt);
2324 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2326 account_capability_s* capability_record = NULL;
2328 while (rc == SQLITE_ROW) {
2329 bool cb_ret = FALSE;
2330 capability_record = (account_capability_s*) malloc(sizeof(account_capability_s));
2332 if (capability_record == NULL) {
2333 ACCOUNT_FATAL("malloc Failed");
2337 ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s));
2339 _account_convert_column_to_capability(hstmt, capability_record);
2341 cb_ret = callback(capability_record->type, capability_record->value, user_data);
2343 _account_free_capability_with_items(capability_record);
2345 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
2347 rc = _account_query_step(hstmt);
2350 rc = _account_query_finalize(hstmt);
2351 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2354 error_code = ACCOUNT_ERROR_NONE;
2357 if (hstmt != NULL) {
2358 rc = _account_query_finalize(hstmt);
2359 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2363 pthread_mutex_unlock(&account_mutex);
2367 GSList* _account_get_capability_list_by_account_id(int account_id, int *error_code)
2369 *error_code = ACCOUNT_ERROR_NONE;
2370 account_stmt hstmt = NULL;
2371 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2373 GSList* capability_list = NULL;
2375 ACCOUNT_RETURN_VAL((account_id > 0), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("ACCOUNT INDEX IS LESS THAN 0"));
2376 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
2378 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2380 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
2381 hstmt = _account_prepare_query(query);
2383 if( _account_db_err_code() == SQLITE_PERM ){
2384 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2385 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
2389 rc = _account_query_step(hstmt);
2390 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2392 account_capability_s* capability_record = NULL;
2394 while (rc == SQLITE_ROW) {
2395 capability_record = (account_capability_s*) malloc(sizeof(account_capability_s));
2397 if (capability_record == NULL) {
2398 ACCOUNT_FATAL("malloc Failed");
2402 ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s));
2404 _account_convert_column_to_capability(hstmt, capability_record);
2406 //cb_ret = callback(capability_record->type, capability_record->value, user_data);
2408 //_account_free_capability_items(capability_record);
2409 //_ACCOUNT_FREE(capability_record);
2411 //ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
2413 capability_list = g_slist_append(capability_list, capability_record);
2414 rc = _account_query_step(hstmt);
2417 rc = _account_query_finalize(hstmt);
2418 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
2421 *error_code = ACCOUNT_ERROR_NONE;
2426 rc = _account_query_finalize(hstmt);
2427 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
2431 pthread_mutex_unlock(&account_mutex);
2432 return capability_list;
2435 static int _account_compare_old_record(account_s *new_account, int account_id)
2437 int error_code = ACCOUNT_ERROR_NONE;
2438 account_stmt hstmt = NULL;
2439 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2441 account_s *old_account = NULL;
2443 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2444 ACCOUNT_RETURN_VAL((new_account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2445 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2447 old_account = (account_s*)calloc(1, sizeof(account_s));
2448 if (old_account == NULL) {
2449 _ERR("Out of Memory");
2450 return ACCOUNT_ERROR_OUT_OF_MEMORY;
2453 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2455 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
2456 hstmt = _account_prepare_query(query);
2458 rc = _account_query_step(hstmt);
2459 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2461 while (rc == SQLITE_ROW) {
2462 _account_convert_column_to_account(hstmt, old_account);
2463 rc = _account_query_step(hstmt);
2466 rc = _account_query_finalize(hstmt);
2467 ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2471 error_code = _account_query_capability_by_account_id(_account_get_capability_text_cb, old_account->id, (void*)old_account);
2472 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
2475 error_code = _account_query_custom_by_account_id(_account_get_custom_text_cb, old_account->id, (void*)old_account);
2476 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
2480 new_account->id = old_account->id;
2483 if(!new_account->user_name) {
2484 if(old_account->user_name)
2485 new_account->user_name = _account_get_text(old_account->user_name);
2489 if(!new_account->display_name) {
2490 if(old_account->display_name)
2491 new_account->display_name = _account_get_text(old_account->display_name);
2495 if(!new_account->email_address) {
2496 if(old_account->email_address)
2497 new_account->email_address = _account_get_text(old_account->email_address);
2501 if(!new_account->domain_name) {
2502 if(old_account->domain_name)
2503 new_account->domain_name = _account_get_text(old_account->domain_name);
2507 if(!new_account->icon_path) {
2508 if(old_account->icon_path)
2509 new_account->icon_path = _account_get_text(old_account->icon_path);
2513 if(!new_account->source) {
2514 if(old_account->source)
2515 new_account->source = _account_get_text(old_account->source);
2518 _ACCOUNT_FREE(new_account->package_name);
2519 new_account->package_name = _account_get_text(old_account->package_name);
2522 if(!new_account->access_token) {
2523 if(old_account->access_token)
2524 new_account->access_token = _account_get_text(old_account->access_token);
2529 for(i=0;i<USER_TXT_CNT;i++) {
2530 if(!new_account->user_data_txt[i]) {
2531 if(old_account->user_data_txt[i])
2532 new_account->user_data_txt[i] = _account_get_text(old_account->user_data_txt[i]);
2537 if(new_account->auth_type == ACCOUNT_AUTH_TYPE_INVALID) {
2538 new_account->auth_type = old_account->auth_type;
2542 if(new_account->secret== ACCOUNT_SECRECY_INVALID) {
2543 new_account->secret = old_account->secret;
2547 if(new_account->sync_support == ACCOUNT_SYNC_INVALID) {
2548 new_account->sync_support = old_account->sync_support;
2552 for(i=0;i<USER_INT_CNT;i++) {
2553 if(new_account->user_data_int[i] == 0) {
2554 new_account->user_data_int[i] = old_account->user_data_int[i];
2560 // user custom table
2564 _account_free_account_with_items(old_account);
2566 if (hstmt != NULL) {
2567 rc = _account_query_finalize(hstmt);
2568 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2572 return ACCOUNT_ERROR_NONE;
2575 static int _account_get_package_name_from_account_id(int account_id, char **package_name)
2577 int error_code = ACCOUNT_ERROR_NONE;
2578 account_stmt hstmt = NULL;
2579 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2581 account_s *old_account = NULL;
2583 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2584 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2586 old_account = (account_s*)calloc(1, sizeof(account_s));
2587 if (old_account == NULL) {
2588 _ERR("Out Of memory");
2589 return ACCOUNT_ERROR_OUT_OF_MEMORY;
2592 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2594 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
2595 hstmt = _account_prepare_query(query);
2597 rc = _account_query_step(hstmt);
2598 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2600 while (rc == SQLITE_ROW) {
2601 _account_convert_column_to_account(hstmt, old_account);
2602 rc = _account_query_step(hstmt);
2605 rc = _account_query_finalize(hstmt);
2606 ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2609 // get package name.
2610 *package_name = _account_get_text(old_account->package_name);
2615 _account_free_account_with_items(old_account);
2618 if (hstmt != NULL) {
2619 rc = _account_query_finalize(hstmt);
2620 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2628 static int _account_update_account(int pid, int uid, account_s *account, int account_id)
2630 int rc = 0, binding_count =0;
2631 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2632 int error_code = ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0;
2633 account_stmt hstmt = NULL;
2635 if (!account->package_name) {
2636 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
2637 return ACCOUNT_ERROR_INVALID_PARAMETER;
2640 /* Check permission of requested appid */
2641 char* current_appid = NULL;
2642 char *package_name = NULL;
2644 current_appid = _account_get_current_appid(pid);
2645 error_code = _account_get_package_name_from_account_id(account_id, &package_name);
2647 if(error_code != ACCOUNT_ERROR_NONE || package_name == NULL){
2648 ACCOUNT_ERROR("No package name with account_id\n");
2649 _ACCOUNT_FREE(current_appid);
2650 _ACCOUNT_FREE(package_name);
2651 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2654 error_code = _account_check_appid_group_with_package_name(uid, current_appid, package_name);
2655 ACCOUNT_DEBUG( "UPDATE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name); // TODO: remove the log later.
2657 _ACCOUNT_FREE(current_appid);
2658 _ACCOUNT_FREE(package_name);
2660 if(error_code != ACCOUNT_ERROR_NONE){
2661 ACCOUNT_ERROR("No permission to update\n");
2662 return ACCOUNT_ERROR_PERMISSION_DENIED;
2665 error_code = _account_compare_old_record(account, account_id);
2666 if (error_code != ACCOUNT_ERROR_NONE) {
2667 ACCOUNT_ERROR("_account_compare_old_record fail\n");
2671 if( _account_db_err_code() == SQLITE_PERM ){
2672 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2673 return ACCOUNT_ERROR_PERMISSION_DENIED;
2674 } else if( _account_db_err_code() == SQLITE_BUSY ){
2675 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
2676 return ACCOUNT_ERROR_DATABASE_BUSY;
2679 if (!account->user_name && !account->display_name && !account->email_address) {
2680 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
2681 return ACCOUNT_ERROR_INVALID_PARAMETER;
2684 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2686 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
2688 count = _account_get_record_count(query);
2690 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
2691 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2694 /* transaction control required*/
2695 ret_transaction = _account_begin_transaction();
2696 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
2697 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
2698 pthread_mutex_unlock(&account_mutex);
2699 return ACCOUNT_ERROR_DATABASE_BUSY;
2702 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2703 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
2704 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
2705 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
2706 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
2708 hstmt = _account_prepare_query(query);
2710 if( _account_db_err_code() == SQLITE_PERM ){
2711 ret_transaction = _account_end_transaction(FALSE);
2712 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2713 return ACCOUNT_ERROR_PERMISSION_DENIED;
2716 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s)(%x).\n", _account_db_err_msg(), _account_end_transaction(FALSE)));
2718 binding_count = _account_convert_account_to_sql(account, hstmt, query);
2719 _account_query_bind_int(hstmt, binding_count++, account_id);
2721 rc = _account_query_step(hstmt);
2722 if (rc != SQLITE_DONE) {
2723 ACCOUNT_SLOGE( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
2726 rc = _account_query_finalize(hstmt);
2727 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2730 _INFO("update query=%s", query);
2732 /*update capability*/
2733 error_code = _account_update_capability(account, account_id);
2734 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2735 ret_transaction = _account_end_transaction(FALSE);
2736 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2741 error_code = _account_update_custom(account, account_id);
2742 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2743 ret_transaction = _account_end_transaction(FALSE);
2744 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2748 ret_transaction = _account_end_transaction(TRUE);
2750 _INFO("update end");
2755 static int _account_update_account_ex(account_s *account, int account_id)
2757 int rc = 0, binding_count =0;
2758 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2759 int error_code = ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0;
2760 account_stmt hstmt = NULL;
2762 if (!account->package_name) {
2763 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
2764 return ACCOUNT_ERROR_INVALID_PARAMETER;
2767 error_code = _account_compare_old_record(account, account_id);
2768 if (error_code != ACCOUNT_ERROR_NONE) {
2769 ACCOUNT_ERROR("_account_compare_old_record fail\n");
2773 if( _account_db_err_code() == SQLITE_PERM ){
2774 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2775 return ACCOUNT_ERROR_PERMISSION_DENIED;
2778 if (!account->user_name && !account->display_name && !account->email_address) {
2779 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
2780 return ACCOUNT_ERROR_INVALID_PARAMETER;
2783 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2785 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
2787 count = _account_get_record_count(query);
2789 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
2790 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2793 /* transaction control required*/
2794 ret_transaction = _account_begin_transaction();
2795 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
2796 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
2797 pthread_mutex_unlock(&account_mutex);
2798 return ACCOUNT_ERROR_DATABASE_BUSY;
2801 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2802 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
2803 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
2804 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
2805 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
2807 hstmt = _account_prepare_query(query);
2809 if( _account_db_err_code() == SQLITE_PERM ){
2810 ret_transaction = _account_end_transaction(FALSE);
2811 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2812 return ACCOUNT_ERROR_PERMISSION_DENIED;
2815 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s)(%x).\n", _account_db_err_msg(), _account_end_transaction(FALSE)));
2817 _INFO("account_update_to_db_by_id_ex_p : before convert() : account_id[%d], user_name=%s", account->id, account->user_name);
2818 binding_count = _account_convert_account_to_sql(account, hstmt, query);
2819 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], user_name=%s", account->id, account->user_name);
2820 _INFO("account_update_to_db_by_id_ex_p : before bind()");
2821 rc = _account_query_bind_int(hstmt, binding_count++, account_id);
2822 _INFO("account_update_to_db_by_id_ex_p : after bind() : ret = %d", rc);
2824 rc = _account_query_step(hstmt);
2825 if (rc != SQLITE_DONE) {
2826 ACCOUNT_SLOGE( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
2828 _INFO("account_update_to_db_by_id_ex_p : after query_step() : ret = %d", rc);
2830 rc = _account_query_finalize(hstmt);
2831 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2833 _INFO("account_update_to_db_by_id_ex_p : after query_filnalize() : ret = %d", rc);
2835 _INFO("account_update_to_db_by_id_ex_p : before update_capability()");
2836 /*update capability*/
2837 error_code = _account_update_capability(account, account_id);
2838 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2839 ret_transaction = _account_end_transaction(FALSE);
2840 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2843 _INFO("account_update_to_db_by_id_ex_p : after update_capability()");
2845 _INFO("account_update_to_db_by_id_ex_p : before update_custom()");
2847 error_code = _account_update_custom(account, account_id);
2848 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2849 ret_transaction = _account_end_transaction(FALSE);
2850 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2853 _INFO("account_update_to_db_by_id_ex_p : after update_custom()");
2855 ret_transaction = _account_end_transaction(TRUE);
2861 int _account_update_to_db_by_id(int pid, int uid, account_s* account, int account_id)
2863 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
2864 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
2865 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2866 int error_code = ACCOUNT_ERROR_NONE;
2867 account_s* data = (account_s*)account;
2869 pthread_mutex_lock(&account_mutex);
2871 error_code = _account_update_account(pid, uid, data, account_id);
2873 if(error_code != ACCOUNT_ERROR_NONE) {
2874 pthread_mutex_unlock(&account_mutex);
2878 pthread_mutex_unlock(&account_mutex);
2881 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, account_id);
2882 _account_insert_delete_update_notification_send(buf);
2884 return ACCOUNT_ERROR_NONE;
2887 int _account_update_to_db_by_id_ex(account_s* account, int account_id)
2889 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
2890 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
2891 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2892 int error_code = ACCOUNT_ERROR_NONE;
2893 account_s* data = account;
2895 pthread_mutex_lock(&account_mutex);
2897 _INFO("before update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name);
2898 error_code = _account_update_account_ex(data, account_id);
2899 _INFO("after update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name);
2901 if(error_code != ACCOUNT_ERROR_NONE) {
2902 pthread_mutex_unlock(&account_mutex);
2906 pthread_mutex_unlock(&account_mutex);
2909 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, account_id);
2910 _account_insert_delete_update_notification_send(buf);
2912 return ACCOUNT_ERROR_NONE;
2916 int _account_update_to_db_by_user_name(int pid, int uid, account_s* account, const char *user_name, const char *package_name)
2918 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
2919 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
2920 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2922 int error_code = ACCOUNT_ERROR_NONE;
2923 account_s *data = (account_s*)account;
2925 pthread_mutex_lock(&account_mutex);
2927 error_code = _account_update_account_by_user_name(pid, uid, data, user_name, package_name);
2929 pthread_mutex_unlock(&account_mutex);
2932 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, data->id);
2933 _account_insert_delete_update_notification_send(buf);
2938 GSList* _account_db_query_all(int pid)
2940 //int error_code = ACCOUNT_ERROR_NONE;
2941 account_stmt hstmt = NULL;
2942 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2944 GSList *account_list = NULL;
2946 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected."));
2948 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2950 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TABLE);
2951 hstmt = _account_prepare_query(query);
2953 if( _account_db_err_code() == SQLITE_PERM ){
2954 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2958 rc = _account_query_step(hstmt);
2960 account_s *account_record = NULL;
2962 if (rc != SQLITE_ROW)
2964 _ERR("The record isn't found");
2968 while(rc == SQLITE_ROW) {
2969 account_record = (account_s*) malloc(sizeof(account_s));
2971 if (account_record == NULL) {
2972 ACCOUNT_FATAL("malloc Failed");
2976 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2977 _account_convert_column_to_account(hstmt, account_record);
2978 account_list = g_slist_append(account_list, account_record);
2979 rc = _account_query_step(hstmt);
2982 rc = _account_query_finalize(hstmt);
2983 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, NULL, ("finalize error"));
2988 for (iter = account_list; iter != NULL; iter = g_slist_next(iter)) {
2989 account_s *account = NULL;
2990 account = (account_s*)iter->data;
2991 _account_query_capability_by_account_id(_account_get_capability_text_cb, account->id, (void*)account);
2992 _account_query_custom_by_account_id(_account_get_custom_text_cb, account->id, (void*)account);
2996 if (hstmt != NULL) {
2997 rc = _account_query_finalize(hstmt);
2998 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {_account_gslist_account_free(account_list);}, NULL, ("finalize error"));
3003 _remove_sensitive_info_from_non_owning_account_slist(pid, account_list);
3005 return account_list;
3008 int _account_update_sync_status_by_id(int uid, int account_db_id, const int sync_status)
3010 int error_code = ACCOUNT_ERROR_NONE;
3011 account_stmt hstmt = NULL;
3012 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3016 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
3017 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3018 if ( (sync_status < 0) || (sync_status > ACCOUNT_SYNC_STATUS_RUNNING)) {
3019 ACCOUNT_SLOGE("(%s)-(%d) sync_status is less than 0 or more than enum max.\n", __FUNCTION__, __LINE__);
3020 return ACCOUNT_ERROR_INVALID_PARAMETER;
3023 pthread_mutex_lock(&account_mutex);
3025 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3027 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_db_id);
3029 rc = _account_get_record_count(query);
3031 if( _account_db_err_code() == SQLITE_PERM ){
3032 pthread_mutex_unlock(&account_mutex);
3033 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3034 return ACCOUNT_ERROR_PERMISSION_DENIED;
3038 ACCOUNT_SLOGE( "account_update_sync_status_by_id : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
3039 pthread_mutex_unlock(&account_mutex);
3040 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3043 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3045 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET sync_support=? WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
3046 hstmt = _account_prepare_query(query);
3048 _account_query_bind_int(hstmt, count, sync_status);
3050 rc = _account_query_step(hstmt);
3052 if( _account_db_err_code() == SQLITE_PERM ){
3053 pthread_mutex_unlock(&account_mutex);
3054 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3055 return ACCOUNT_ERROR_PERMISSION_DENIED;
3058 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_DB_FAILED,
3059 ("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg()));
3061 rc = _account_query_finalize(hstmt);
3062 if (rc != ACCOUNT_ERROR_NONE) {
3063 ACCOUNT_ERROR("_account_query_finalize error");
3064 pthread_mutex_unlock(&account_mutex);
3068 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_SYNC_UPDATE, account_db_id);
3069 _account_insert_delete_update_notification_send(buf);
3072 error_code = ACCOUNT_ERROR_NONE;
3075 if (hstmt != NULL) {
3076 rc = _account_query_finalize(hstmt);
3077 pthread_mutex_unlock(&account_mutex);
3078 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3082 pthread_mutex_unlock(&account_mutex);
3086 int _account_query_account_by_account_id(int pid, int account_db_id, account_s *account_record)
3088 _INFO("_account_query_account_by_account_id() start, account_db_id=[%d]", account_db_id);
3090 int error_code = ACCOUNT_ERROR_NONE;
3091 account_stmt hstmt = NULL;
3092 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3095 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
3096 ACCOUNT_RETURN_VAL(account_record != NULL, {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
3097 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3099 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3101 ACCOUNT_DEBUG("starting db operations");
3103 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
3104 hstmt = _account_prepare_query(query);
3105 rc = _account_db_err_code();
3106 _INFO("after _account_prepare_query, rc=[%d]", rc);
3108 if( rc == SQLITE_PERM ){
3109 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3110 return ACCOUNT_ERROR_PERMISSION_DENIED;
3113 ACCOUNT_DEBUG("before _account_query_step");
3114 rc = _account_query_step(hstmt);
3115 ACCOUNT_DEBUG("after _account_query_step returned [%d]", rc);
3116 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3118 while (rc == SQLITE_ROW) {
3119 ACCOUNT_DEBUG("before _account_convert_column_to_account");
3120 _account_convert_column_to_account(hstmt, account_record);
3121 ACCOUNT_DEBUG("after _account_convert_column_to_account");
3122 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]);
3123 rc = _account_query_step(hstmt);
3126 ACCOUNT_DEBUG("account_record->id=[%d]", account_record->id);
3128 rc = _account_query_finalize(hstmt);
3129 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3131 ACCOUNT_DEBUG("before _account_query_capability_by_account_id");
3132 _account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record);
3133 ACCOUNT_DEBUG("after _account_query_capability_by_account_id");
3135 ACCOUNT_DEBUG("before _account_query_custom_by_account_id");
3136 _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record);
3137 ACCOUNT_DEBUG("after _account_query_custom_by_account_id");
3140 error_code = ACCOUNT_ERROR_NONE;
3143 if (hstmt != NULL) {
3144 rc = _account_query_finalize(hstmt);
3145 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3151 _remove_sensitive_info_from_non_owning_account(pid, account_record);
3153 pthread_mutex_unlock(&account_mutex);
3154 ACCOUNT_DEBUG("_account_query_account_by_account_id end [%d]", error_code);
3158 GList* _account_query_account_by_user_name(int pid, const char *user_name, int *error_code)
3160 *error_code = ACCOUNT_ERROR_NONE;
3161 account_stmt hstmt = NULL;
3162 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3164 account_s *account_head = NULL;
3166 if (user_name == NULL)
3168 _ERR("USER NAME IS NULL");
3169 *error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
3173 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3175 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ?", ACCOUNT_TABLE);
3177 hstmt = _account_prepare_query(query);
3179 if (_account_db_err_code() == SQLITE_PERM)
3181 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3182 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
3186 int binding_count = 1;
3187 _account_query_bind_text(hstmt, binding_count++, user_name);
3189 rc = _account_query_step(hstmt);
3191 if (rc != SQLITE_ROW)
3193 _ERR("The record isn't found");
3194 *error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
3200 account_head = (account_s*) malloc(sizeof(account_s));
3201 if (account_head == NULL) {
3202 ACCOUNT_FATAL("malloc Failed");
3203 if (hstmt != NULL) {
3204 rc = _account_query_finalize(hstmt);
3206 if (rc != ACCOUNT_ERROR_NONE)
3208 _ERR("finalize error");
3214 *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
3217 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3219 while (rc == SQLITE_ROW) {
3220 account_s* account_record = NULL;
3222 account_record = (account_s*) malloc(sizeof(account_s));
3224 if (account_record == NULL) {
3225 ACCOUNT_FATAL("malloc Failed");
3228 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3230 _account_convert_column_to_account(hstmt, account_record);
3232 account_head->account_list = g_list_append(account_head->account_list, account_record);
3234 rc = _account_query_step(hstmt);
3238 rc = _account_query_finalize(hstmt);
3240 if (rc != ACCOUNT_ERROR_NONE)
3242 _ERR("finalize error");
3252 tmp = g_list_length(account_head->account_list);
3254 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3256 account = (account_h)iter->data;
3258 account_s *testaccount = (account_s*)account;
3260 _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3261 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3265 *error_code = ACCOUNT_ERROR_NONE;
3268 if (hstmt != NULL) {
3269 rc = _account_query_finalize(hstmt);
3270 if (rc != ACCOUNT_ERROR_NONE)
3272 _ERR("finalize error");
3278 pthread_mutex_unlock(&account_mutex);
3281 _remove_sensitive_info_from_non_owning_account_list(pid, account_head->account_list);
3282 GList* result = account_head->account_list;
3283 _ACCOUNT_FREE(account_head);
3290 _account_query_account_by_capability(int pid, const char* capability_type, const int capability_value, int *error_code)
3292 *error_code = ACCOUNT_ERROR_NONE;
3293 account_stmt hstmt = NULL;
3294 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3297 ACCOUNT_RETURN_VAL((capability_type != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("capability_type IS NULL"));
3299 if ((capability_value < 0) || (capability_value > ACCOUNT_CAPABILITY_ENABLED)) {
3300 ACCOUNT_SLOGE("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
3301 *error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
3305 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
3307 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3309 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=? AND value=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
3311 hstmt = _account_prepare_query(query);
3313 if( _account_db_err_code() == SQLITE_PERM ){
3314 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3315 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
3319 int binding_count = 1;
3320 _account_query_bind_text(hstmt, binding_count++, capability_type);
3321 _account_query_bind_int(hstmt, binding_count++, capability_value);
3323 rc = _account_query_step(hstmt);
3325 account_s* account_head = NULL;
3327 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3331 account_head = (account_s*) malloc(sizeof(account_s));
3332 if (account_head == NULL) {
3333 ACCOUNT_FATAL("malloc Failed");
3334 if (hstmt != NULL) {
3335 rc = _account_query_finalize(hstmt);
3336 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
3339 *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
3342 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3344 while (rc == SQLITE_ROW) {
3345 account_s* account_record = NULL;
3347 account_record = (account_s*) malloc(sizeof(account_s));
3349 if (account_record == NULL) {
3350 ACCOUNT_FATAL("malloc Failed");
3353 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3355 _account_convert_column_to_account(hstmt, account_record);
3357 account_head->account_list = g_list_append(account_head->account_list, account_record);
3359 rc = _account_query_step(hstmt);
3363 rc = _account_query_finalize(hstmt);
3364 ACCOUNT_CATCH_ERROR_P((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3370 tmp = g_list_length(account_head->account_list);
3372 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3373 account_h account = NULL;
3374 account = (account_h)iter->data;
3375 account_s* testaccount = (account_s*)account;
3377 _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3378 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3383 *error_code = ACCOUNT_ERROR_NONE;
3386 if (hstmt != NULL) {
3387 rc = _account_query_finalize(hstmt);
3388 if ( rc != ACCOUNT_ERROR_NONE ) {
3390 _ERR("finalize error");
3395 if( *error_code != ACCOUNT_ERROR_NONE && account_head ) {
3396 _account_glist_account_free(account_head->account_list);
3397 _ACCOUNT_FREE(account_head);
3398 account_head = NULL;
3401 pthread_mutex_unlock(&account_mutex);
3405 _remove_sensitive_info_from_non_owning_account_list(pid, account_head->account_list);
3406 GList* result = account_head->account_list;
3407 _ACCOUNT_FREE(account_head);
3413 GList* _account_query_account_by_capability_type(int pid, const char* capability_type, int *error_code)
3415 *error_code = ACCOUNT_ERROR_NONE;
3416 account_stmt hstmt = NULL;
3417 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3420 ACCOUNT_RETURN_VAL((capability_type != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("capability_type IS NULL"));
3421 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;},
3422 NULL, ("The database isn't connected."));
3424 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3426 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
3428 hstmt = _account_prepare_query(query);
3430 if( _account_db_err_code() == SQLITE_PERM ){
3431 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3432 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
3436 int binding_count = 1;
3437 _account_query_bind_text(hstmt, binding_count++, capability_type);
3439 rc = _account_query_step(hstmt);
3441 account_s* account_head = NULL;
3443 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3447 account_head = (account_s*) malloc(sizeof(account_s));
3448 if (account_head == NULL) {
3449 ACCOUNT_FATAL("malloc Failed");
3450 if (hstmt != NULL) {
3451 rc = _account_query_finalize(hstmt);
3452 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
3455 *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
3458 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3460 while (rc == SQLITE_ROW) {
3461 account_s* account_record = NULL;
3463 account_record = (account_s*) malloc(sizeof(account_s));
3465 if (account_record == NULL) {
3466 ACCOUNT_FATAL("malloc Failed");
3469 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3471 _account_convert_column_to_account(hstmt, account_record);
3473 account_head->account_list = g_list_append(account_head->account_list, account_record);
3475 rc = _account_query_step(hstmt);
3479 rc = _account_query_finalize(hstmt);
3480 ACCOUNT_CATCH_ERROR_P((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3486 tmp = g_list_length(account_head->account_list);
3488 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3489 account_s* testaccount = (account_s*)iter->data;
3491 _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3492 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3496 *error_code = ACCOUNT_ERROR_NONE;
3501 rc = _account_query_finalize(hstmt);
3502 if (rc != ACCOUNT_ERROR_NONE) {
3504 _ERR("finalize error");
3509 if( (*error_code != ACCOUNT_ERROR_NONE) && account_head ) {
3510 _account_glist_account_free(account_head->account_list);
3511 _ACCOUNT_FREE(account_head);
3512 account_head = NULL;
3515 pthread_mutex_unlock(&account_mutex);
3519 _remove_sensitive_info_from_non_owning_account_list(pid, account_head->account_list);
3520 GList* result = account_head->account_list;
3521 _ACCOUNT_FREE(account_head);
3527 GList* _account_query_account_by_package_name(int pid,const char* package_name, int *error_code)
3529 _INFO("_account_query_account_by_package_name");
3531 *error_code = ACCOUNT_ERROR_NONE;
3532 account_stmt hstmt = NULL;
3533 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3536 ACCOUNT_RETURN_VAL((package_name != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("PACKAGE NAME IS NULL"));
3537 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
3539 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3541 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE package_name=?", ACCOUNT_TABLE);
3543 hstmt = _account_prepare_query(query);
3545 if( _account_db_err_code() == SQLITE_PERM ){
3546 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3547 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
3551 int binding_count = 1;
3552 _account_query_bind_text(hstmt, binding_count++, package_name);
3554 rc = _account_query_step(hstmt);
3556 account_s* account_head = NULL;
3558 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.(%s)\n", package_name));
3562 account_head = (account_s*) malloc(sizeof(account_s));
3563 if (account_head == NULL) {
3564 ACCOUNT_FATAL("malloc Failed");
3565 if (hstmt != NULL) {
3566 rc = _account_query_finalize(hstmt);
3567 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
3570 *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
3573 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3575 while (rc == SQLITE_ROW) {
3576 account_s* account_record = NULL;
3578 account_record = (account_s*) malloc(sizeof(account_s));
3580 if (account_record == NULL) {
3581 ACCOUNT_FATAL("malloc Failed");
3584 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3586 _account_convert_column_to_account(hstmt, account_record);
3588 _INFO("Adding account_list");
3589 account_head->account_list = g_list_append(account_head->account_list, account_record);
3591 rc = _account_query_step(hstmt);
3595 rc = _account_query_finalize(hstmt);
3596 ACCOUNT_CATCH_ERROR_P((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3601 tmp = g_list_length(account_head->account_list);
3603 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3604 account_s* testaccount = (account_s*)iter->data;
3606 _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3607 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3610 *error_code = ACCOUNT_ERROR_NONE;
3615 rc = _account_query_finalize(hstmt);
3616 if (rc != ACCOUNT_ERROR_NONE) {
3618 _ERR("finalize error");
3623 pthread_mutex_unlock(&account_mutex);
3625 if( (*error_code != ACCOUNT_ERROR_NONE) && account_head ) {
3626 _account_glist_account_free(account_head->account_list);
3627 _ACCOUNT_FREE(account_head);
3628 account_head = NULL;
3631 if ((*error_code == ACCOUNT_ERROR_NONE) && account_head != NULL)
3633 _INFO("Returning account_list");
3634 _remove_sensitive_info_from_non_owning_account_list(pid,account_head->account_list);
3635 GList* result = account_head->account_list;
3636 _ACCOUNT_FREE(account_head);
3642 int _account_delete(int pid, int uid, int account_id)
3644 int error_code = ACCOUNT_ERROR_NONE;
3645 account_stmt hstmt = NULL;
3646 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3648 int ret_transaction = 0;
3649 bool is_success = FALSE;
3651 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3654 /* Check requested ID to delete */
3655 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id=%d", ACCOUNT_TABLE, account_id);
3657 count = _account_get_record_count(query);
3659 if( _account_db_err_code() == SQLITE_PERM ){
3660 pthread_mutex_unlock(&account_mutex);
3661 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3662 return ACCOUNT_ERROR_PERMISSION_DENIED;
3666 ACCOUNT_ERROR("account id(%d) is not exist. count(%d)\n", account_id, count);
3667 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3670 /* Check permission of requested appid */
3671 char* current_appid = NULL;
3672 char *package_name = NULL;
3674 current_appid = _account_get_current_appid(pid);
3676 error_code = _account_get_package_name_from_account_id(account_id, &package_name);
3678 if(error_code != ACCOUNT_ERROR_NONE){
3679 ACCOUNT_ERROR("No package name with account_id\n");
3680 _ACCOUNT_FREE(current_appid);
3681 _ACCOUNT_FREE(package_name);
3682 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3684 ACCOUNT_DEBUG( "DELETE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name);
3686 error_code = _account_check_appid_group_with_package_name(uid, current_appid, package_name);
3688 _ACCOUNT_FREE(current_appid);
3689 _ACCOUNT_FREE(package_name);
3691 if(error_code != ACCOUNT_ERROR_NONE){
3692 ACCOUNT_ERROR("No permission to delete\n");
3693 return ACCOUNT_ERROR_PERMISSION_DENIED;
3696 /* transaction control required*/
3697 ret_transaction = _account_begin_transaction();
3699 if( _account_db_err_code() == SQLITE_PERM ){
3700 pthread_mutex_unlock(&account_mutex);
3701 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3702 return ACCOUNT_ERROR_PERMISSION_DENIED;
3705 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
3706 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
3707 pthread_mutex_unlock(&account_mutex);
3708 return ACCOUNT_ERROR_DATABASE_BUSY;
3711 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3712 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3713 pthread_mutex_unlock(&account_mutex);
3714 return ret_transaction;
3717 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3718 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
3720 hstmt = _account_prepare_query(query);
3722 if( _account_db_err_code() == SQLITE_PERM ){
3723 pthread_mutex_unlock(&account_mutex);
3724 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3725 return ACCOUNT_ERROR_PERMISSION_DENIED;
3728 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3729 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3731 rc = _account_query_step(hstmt);
3732 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3734 rc = _account_query_finalize(hstmt);
3736 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3739 ACCOUNT_MEMSET(query, 0, sizeof(query));
3741 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
3743 hstmt = _account_prepare_query(query);
3744 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3745 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3747 rc = _account_query_step(hstmt);
3748 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
3750 rc = _account_query_finalize(hstmt);
3751 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3754 /* delete custom data */
3755 ACCOUNT_MEMSET(query, 0, sizeof(query));
3757 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
3759 hstmt = _account_prepare_query(query);
3761 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3762 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3764 rc = _account_query_step(hstmt);
3765 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
3767 rc = _account_query_finalize(hstmt);
3768 ACCOUNT_CATCH_ERROR(rc == ACCOUNT_ERROR_NONE, {}, rc, ("finalize error", account_id, rc));
3774 if (hstmt != NULL) {
3775 rc = _account_query_finalize(hstmt);
3776 if(rc != ACCOUNT_ERROR_NONE ){
3777 ACCOUNT_ERROR("rc (%d)", rc);
3784 ret_transaction = _account_end_transaction(is_success);
3786 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3787 ACCOUNT_ERROR("account_delete:_account_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3789 if (is_success == true) {
3791 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, account_id);
3792 _account_insert_delete_update_notification_send(buf);
3796 pthread_mutex_unlock(&account_mutex);
3802 static int _account_query_account_by_username_and_package(const char* username, const char* package_name, account_h *account)
3806 int error_code = ACCOUNT_ERROR_NONE;
3807 account_stmt hstmt = NULL;
3808 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3810 int binding_count = 1;
3812 ACCOUNT_RETURN_VAL((username != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("username IS NULL"));
3813 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name IS NULL"));
3814 ACCOUNT_RETURN_VAL((*account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
3815 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3817 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3819 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
3820 hstmt = _account_prepare_query(query);
3822 if( _account_db_err_code() == SQLITE_PERM ){
3823 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3824 return ACCOUNT_ERROR_PERMISSION_DENIED;
3827 _account_query_bind_text(hstmt, binding_count++, username);
3828 _account_query_bind_text(hstmt, binding_count++, package_name);
3830 rc = _account_query_step(hstmt);
3831 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3833 account_s *account_record = (account_s *)(*account);
3835 while (rc == SQLITE_ROW) {
3836 _account_convert_column_to_account(hstmt, account_record);
3837 rc = _account_query_step(hstmt);
3840 rc = _account_query_finalize(hstmt);
3841 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3842 _account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record);
3843 _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record);
3846 error_code = ACCOUNT_ERROR_NONE;
3849 if (hstmt != NULL) {
3850 rc = _account_query_finalize(hstmt);
3851 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3855 pthread_mutex_unlock(&account_mutex);
3859 int _account_create(account_h *account)
3862 ACCOUNT_SLOGE("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__);
3863 return ACCOUNT_ERROR_INVALID_PARAMETER;
3866 account_s *data = (account_s*)malloc(sizeof(account_s));
3869 ACCOUNT_FATAL("Memory Allocation Failed");
3870 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3872 ACCOUNT_MEMSET(data, 0, sizeof(account_s));
3874 /*Setting account as visible by default*/
3875 data->secret = ACCOUNT_SECRECY_VISIBLE;
3877 /*Setting account as not supporting sync by default*/
3878 data->sync_support = ACCOUNT_SYNC_NOT_SUPPORT;
3880 *account = (account_h)data;
3882 return ACCOUNT_ERROR_NONE;
3885 int _account_destroy(account_h account)
3887 account_s *data = (account_s*)account;
3889 ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account handle is null!"));
3891 _account_free_account_with_items(data);
3893 return ACCOUNT_ERROR_NONE;
3896 int _account_get_account_id(account_s* account, int *account_id)
3899 return ACCOUNT_ERROR_INVALID_PARAMETER;
3902 return ACCOUNT_ERROR_INVALID_PARAMETER;
3905 *account_id = account->id;
3907 return ACCOUNT_ERROR_NONE;
3910 int _account_delete_from_db_by_user_name(int pid, int uid, const char *user_name, const char *package_name)
3912 _INFO("[%s][%s]", user_name, package_name);
3914 int error_code = ACCOUNT_ERROR_NONE;
3915 account_stmt hstmt = NULL;
3916 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3918 int ret_transaction = 0;
3919 bool is_success = FALSE;
3920 account_h account = NULL;
3921 int binding_count = 1;
3922 int account_id = -1;
3924 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is null!"));
3925 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
3926 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3928 /* Check permission of requested appid */
3929 char* current_appid = NULL;
3930 char* package_name_temp = NULL;
3932 current_appid = _account_get_current_appid(pid);
3934 package_name_temp = _account_get_text(package_name);
3936 ACCOUNT_DEBUG( "DELETE:user_name[%s],current_appid[%s], package_name[%s]", user_name, current_appid, package_name_temp);
3938 error_code = _account_check_appid_group_with_package_name(uid, current_appid, package_name_temp);
3940 _ACCOUNT_FREE(current_appid);
3941 _ACCOUNT_FREE(package_name_temp);
3943 if(error_code != ACCOUNT_ERROR_NONE){
3944 ACCOUNT_ERROR("No permission to delete\n");
3945 return ACCOUNT_ERROR_PERMISSION_DENIED;
3948 rc = _account_create(&account);
3949 rc = _account_query_account_by_username_and_package(user_name, package_name, &account);
3953 if( _account_db_err_code() == SQLITE_PERM )
3955 _account_destroy(account);
3956 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3957 return ACCOUNT_ERROR_PERMISSION_DENIED;
3961 account_s* account_data = (account_s*)account;
3963 rc = _account_get_account_id(account_data, &account_id);
3965 rc = _account_destroy(account);
3967 /* transaction control required*/
3968 ret_transaction = _account_begin_transaction();
3970 if( _account_db_err_code() == SQLITE_PERM )
3972 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3973 pthread_mutex_unlock(&account_mutex);
3974 return ACCOUNT_ERROR_PERMISSION_DENIED;
3978 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY )
3980 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
3981 pthread_mutex_unlock(&account_mutex);
3982 return ACCOUNT_ERROR_DATABASE_BUSY;
3984 else if (ret_transaction != ACCOUNT_ERROR_NONE) {
3985 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3986 pthread_mutex_unlock(&account_mutex);
3987 return ret_transaction;
3990 /* delete custom data */
3991 ACCOUNT_MEMSET(query, 0, sizeof(query));
3992 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = ?", ACCOUNT_CUSTOM_TABLE);
3994 hstmt = _account_prepare_query(query);
3996 if( _account_db_err_code() == SQLITE_PERM ){
3997 _account_end_transaction(FALSE);
3998 pthread_mutex_unlock(&account_mutex);
3999 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4000 return ACCOUNT_ERROR_PERMISSION_DENIED;
4003 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4004 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4006 _account_query_bind_int(hstmt, binding_count++, account_id);
4008 rc = _account_query_step(hstmt);
4009 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4011 rc = _account_query_finalize(hstmt);
4012 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4015 /* delete capability */
4016 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", CAPABILITY_TABLE);
4018 hstmt = _account_prepare_query(query);
4020 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4021 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4024 _account_query_bind_text(hstmt, binding_count++, user_name);
4025 _account_query_bind_text(hstmt, binding_count++, package_name);
4027 rc = _account_query_step(hstmt);
4028 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4030 rc = _account_query_finalize(hstmt);
4031 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4034 ACCOUNT_MEMSET(query, 0, sizeof(query));
4037 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
4039 hstmt = _account_prepare_query(query);
4040 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4041 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4045 _account_query_bind_text(hstmt, binding_count++, user_name);
4046 _account_query_bind_text(hstmt, binding_count++, package_name);
4048 rc = _account_query_step(hstmt);
4049 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));
4051 rc = _account_query_finalize(hstmt);
4052 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4058 if (hstmt != NULL) {
4059 rc = _account_query_finalize(hstmt);
4060 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4064 ret_transaction = _account_end_transaction(is_success);
4066 if (ret_transaction != ACCOUNT_ERROR_NONE) {
4067 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
4069 if (is_success == true) {
4071 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, account_id);
4072 _account_insert_delete_update_notification_send(buf);
4076 pthread_mutex_unlock(&account_mutex);
4081 int _account_delete_from_db_by_package_name(int pid, int uid, const char *package_name, gboolean permission)
4083 _INFO("_account_delete_from_db_by_package_name");
4084 int error_code = ACCOUNT_ERROR_NONE;
4085 account_stmt hstmt = NULL;
4086 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4088 int ret_transaction = 0;
4089 bool is_success = FALSE;
4090 int binding_count = 1;
4091 GSList *account_id_list = NULL;
4094 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
4095 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4097 // It only needs list of ids, does not need to query sensitive info. So sending 0
4098 GList* account_list_temp = _account_query_account_by_package_name(getpid(), package_name, &ret);
4099 if( _account_db_err_code() == SQLITE_PERM ){
4100 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4101 return ACCOUNT_ERROR_PERMISSION_DENIED;
4104 if(ret != ACCOUNT_ERROR_NONE){
4105 _ERR("_account_query_account_by_package_name failed ret=[%d]", ret);
4109 /* Check permission of requested appid */
4111 char* current_appid = NULL;
4112 char* package_name_temp = NULL;
4114 current_appid = _account_get_current_appid(pid);
4116 package_name_temp = _account_get_text(package_name);
4118 ACCOUNT_DEBUG( "DELETE: current_appid[%s], package_name[%s]", current_appid, package_name_temp);
4120 error_code = _account_check_appid_group_with_package_name(uid, current_appid, package_name_temp);
4122 _ACCOUNT_FREE(current_appid);
4123 _ACCOUNT_FREE(package_name_temp);
4125 if(error_code != ACCOUNT_ERROR_NONE){
4126 ACCOUNT_ERROR("No permission to delete\n");
4127 _account_glist_account_free(account_list_temp);
4128 return ACCOUNT_ERROR_PERMISSION_DENIED;
4132 GList *account_list = g_list_first(account_list_temp);
4133 _INFO("account_list_temp length=[%d]",g_list_length(account_list));
4136 for (iter = account_list; iter != NULL; iter = g_list_next(iter))
4138 _INFO("iterating account_list");
4139 account_s *account = NULL;
4140 _INFO("Before iter->data");
4141 account = (account_s*)iter->data;
4142 _INFO("After iter->data");
4143 if (account != NULL)
4145 char id[256] = {0, };
4147 ACCOUNT_MEMSET(id, 0, 256);
4149 ACCOUNT_SNPRINTF(id, 256, "%d", account->id);
4151 _INFO("Adding account id [%s]", id);
4152 account_id_list = g_slist_append(account_id_list, g_strdup(id));
4156 _account_glist_account_free(account_list_temp);
4158 /* transaction control required*/
4159 ret_transaction = _account_begin_transaction();
4161 if( _account_db_err_code() == SQLITE_PERM ){
4162 pthread_mutex_unlock(&account_mutex);
4163 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4164 return ACCOUNT_ERROR_PERMISSION_DENIED;
4167 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
4168 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
4169 pthread_mutex_unlock(&account_mutex);
4170 return ACCOUNT_ERROR_DATABASE_BUSY;
4171 }else if (ret_transaction != ACCOUNT_ERROR_NONE) {
4172 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
4173 pthread_mutex_unlock(&account_mutex);
4174 return ret_transaction;
4177 /* delete custom table */
4178 ACCOUNT_MEMSET(query, 0, sizeof(query));
4179 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", ACCOUNT_CUSTOM_TABLE);
4181 hstmt = _account_prepare_query(query);
4183 if( _account_db_err_code() == SQLITE_PERM ){
4184 _account_end_transaction(FALSE);
4185 pthread_mutex_unlock(&account_mutex);
4186 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4187 return ACCOUNT_ERROR_PERMISSION_DENIED;
4190 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4191 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4194 _account_query_bind_text(hstmt, binding_count++, package_name);
4196 rc = _account_query_step(hstmt);
4197 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4199 rc = _account_query_finalize(hstmt);
4200 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4203 /* delete capability table */
4204 ACCOUNT_MEMSET(query, 0, sizeof(query));
4205 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", CAPABILITY_TABLE);
4207 hstmt = _account_prepare_query(query);
4209 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4210 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4213 _account_query_bind_text(hstmt, binding_count++, package_name);
4215 rc = _account_query_step(hstmt);
4216 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4218 rc = _account_query_finalize(hstmt);
4219 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4222 /* delete account table */
4223 ACCOUNT_MEMSET(query, 0, sizeof(query));
4225 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", ACCOUNT_TABLE);
4227 hstmt = _account_prepare_query(query);
4228 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4229 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4232 _account_query_bind_text(hstmt, binding_count++, package_name);
4234 rc = _account_query_step(hstmt);
4235 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. package_name=%s, rc=%d\n", package_name, rc));
4237 rc = _account_query_finalize(hstmt);
4238 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4244 if (hstmt != NULL) {
4245 rc = _account_query_finalize(hstmt);
4246 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4250 ret_transaction = _account_end_transaction(is_success);
4252 if (ret_transaction != ACCOUNT_ERROR_NONE) {
4253 ACCOUNT_ERROR("account_delete:_account_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
4255 if (is_success == true) {
4256 GSList* gs_iter = NULL;
4257 for (gs_iter = account_id_list; gs_iter != NULL; gs_iter = g_slist_next(gs_iter)) {
4258 char* p_tmpid = NULL;
4259 p_tmpid = (char*)gs_iter->data;
4261 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%s", ACCOUNT_NOTI_NAME_DELETE, p_tmpid);
4262 ACCOUNT_SLOGD("%s", buf);
4263 _account_insert_delete_update_notification_send(buf);
4264 _ACCOUNT_FREE(p_tmpid);
4266 g_slist_free(account_id_list);
4270 pthread_mutex_unlock(&account_mutex);
4272 _INFO("_account_delete_from_db_by_package_name end");
4276 int _account_get_total_count_from_db(gboolean include_hidden, int *count)
4279 ACCOUNT_SLOGE("(%s)-(%d) count is NULL.\n", __FUNCTION__, __LINE__);
4280 return ACCOUNT_ERROR_INVALID_PARAMETER;
4284 ACCOUNT_ERROR("DB is not opened\n");
4285 return ACCOUNT_ERROR_DB_NOT_OPENED;
4288 char query[1024] = {0, };
4289 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4293 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s", ACCOUNT_TABLE);
4297 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s where secret = %d", ACCOUNT_TABLE, ACCOUNT_SECRECY_VISIBLE);
4300 *count = _account_get_record_count(query);
4302 if( _account_db_err_code() == SQLITE_PERM ){
4303 pthread_mutex_unlock(&account_mutex);
4304 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4305 return ACCOUNT_ERROR_PERMISSION_DENIED;
4310 account_stmt pStmt = NULL;
4312 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
4313 if (SQLITE_OK != rc) {
4314 ACCOUNT_SLOGE("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg());
4315 sqlite3_finalize(pStmt);
4316 return ACCOUNT_ERROR_DB_FAILED;
4319 rc = sqlite3_step(pStmt);
4320 if (SQLITE_ROW != rc) {
4321 ACCOUNT_ERROR("[ERROR] sqlite3_step() failed\n");
4322 sqlite3_finalize(pStmt);
4323 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4326 ncount = sqlite3_column_int(pStmt, 0);
4330 sqlite3_finalize(pStmt);
4333 ACCOUNT_ERROR("[ERROR] Number of account : %d, End", ncount);
4334 return ACCOUNT_ERROR_DB_FAILED;
4337 return ACCOUNT_ERROR_NONE;
4340 int account_type_create(account_type_h *account_type)
4342 if (!account_type) {
4343 ACCOUNT_SLOGE("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
4344 return ACCOUNT_ERROR_INVALID_PARAMETER;
4347 account_type_s *data = (account_type_s*)malloc(sizeof(account_type_s));
4350 ACCOUNT_ERROR("Memory Allocation Failed");
4351 return ACCOUNT_ERROR_OUT_OF_MEMORY;
4354 ACCOUNT_MEMSET(data, 0, sizeof(account_type_s));
4356 *account_type = (account_type_h)data;
4358 return ACCOUNT_ERROR_NONE;
4361 int account_type_destroy(account_type_h account_type)
4363 account_type_s *data = (account_type_s*)account_type;
4365 ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account type handle is null!"));
4367 _account_type_free_account_type_with_items(data);
4369 return ACCOUNT_ERROR_NONE;
4372 //app_id mandatory field
4373 int account_type_set_app_id(account_type_h account_type, const char *app_id)
4375 if (!account_type) {
4376 ACCOUNT_SLOGE("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
4377 return ACCOUNT_ERROR_INVALID_PARAMETER;
4381 ACCOUNT_SLOGE("(%s)-(%d) app_id is NULL.\n", __FUNCTION__, __LINE__);
4382 return ACCOUNT_ERROR_INVALID_PARAMETER;
4385 account_type_s *data = (account_type_s*)account_type;
4387 _ACCOUNT_FREE(data->app_id);
4388 data->app_id = _account_get_text(app_id);
4390 return ACCOUNT_ERROR_NONE;
4393 //service_provider_id mandatory field
4394 int account_type_set_service_provider_id(account_type_h account_type, const char *service_provider_id)
4396 if (!account_type) {
4397 return ACCOUNT_ERROR_INVALID_PARAMETER;
4400 if (!service_provider_id) {
4401 return ACCOUNT_ERROR_INVALID_PARAMETER;
4404 account_type_s *data = (account_type_s*)account_type;
4406 _ACCOUNT_FREE(data->service_provider_id);
4407 data->service_provider_id = _account_get_text(service_provider_id);
4409 return ACCOUNT_ERROR_NONE;
4412 int account_type_set_icon_path(account_type_h account_type, const char *icon_path)
4414 if (!account_type) {
4415 return ACCOUNT_ERROR_INVALID_PARAMETER;
4419 return ACCOUNT_ERROR_INVALID_PARAMETER;
4422 account_type_s *data = (account_type_s*)account_type;
4424 _ACCOUNT_FREE(data->icon_path);
4425 data->icon_path = _account_get_text(icon_path);
4427 return ACCOUNT_ERROR_NONE;
4430 int account_type_set_small_icon_path(account_type_h account_type, const char *small_icon_path)
4432 if (!account_type) {
4433 return ACCOUNT_ERROR_INVALID_PARAMETER;
4436 if (!small_icon_path) {
4437 return ACCOUNT_ERROR_INVALID_PARAMETER;
4440 account_type_s *data = (account_type_s*)account_type;
4442 _ACCOUNT_FREE(data->small_icon_path);
4443 data->small_icon_path = _account_get_text(small_icon_path);
4445 return ACCOUNT_ERROR_NONE;
4448 int account_type_set_multiple_account_support(account_type_h account_type, const bool multiple_account_support)
4450 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__));
4452 account_type_s *data = (account_type_s*)account_type;
4454 data->multiple_account_support = multiple_account_support;
4456 return ACCOUNT_ERROR_NONE;
4460 int account_type_set_label(account_type_h account_type, const char* label, const char* locale)
4462 if (!account_type) {
4463 ACCOUNT_SLOGE("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
4464 return ACCOUNT_ERROR_INVALID_PARAMETER;
4467 if(!label || !locale) {
4468 return ACCOUNT_ERROR_INVALID_PARAMETER;
4471 account_type_s *data = (account_type_s*)account_type;
4472 label_s *label_data = (label_s*)malloc(sizeof(label_s));
4474 if (label_data == NULL) {
4475 return ACCOUNT_ERROR_OUT_OF_MEMORY;
4477 ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
4479 label_data->label = _account_get_text(label);
4480 label_data->locale = _account_get_text(locale);
4482 data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
4484 return ACCOUNT_ERROR_NONE;
4487 int account_type_get_app_id(account_type_h account_type, char **app_id)
4489 if (!account_type) {
4490 return ACCOUNT_ERROR_INVALID_PARAMETER;
4494 return ACCOUNT_ERROR_INVALID_PARAMETER;
4497 account_type_s *data = (account_type_s*)account_type;
4500 *app_id = _account_get_text(data->app_id);
4502 return ACCOUNT_ERROR_NONE;
4505 int account_type_get_service_provider_id(account_type_h account_type, char **service_provider_id)
4507 if (!account_type) {
4508 return ACCOUNT_ERROR_INVALID_PARAMETER;
4511 if (!service_provider_id) {
4512 return ACCOUNT_ERROR_INVALID_PARAMETER;
4515 account_type_s *data = (account_type_s*)account_type;
4517 (*service_provider_id) = NULL;
4518 *service_provider_id = _account_get_text(data->service_provider_id);
4520 return ACCOUNT_ERROR_NONE;
4523 int account_type_get_icon_path(account_type_h account_type, char **icon_path)
4525 if (!account_type) {
4526 return ACCOUNT_ERROR_INVALID_PARAMETER;
4530 return ACCOUNT_ERROR_INVALID_PARAMETER;
4533 account_type_s *data = (account_type_s*)account_type;
4535 (*icon_path) = NULL;
4536 *icon_path = _account_get_text(data->icon_path);
4538 return ACCOUNT_ERROR_NONE;
4541 int account_type_get_small_icon_path(account_type_h account_type, char **small_icon_path)
4543 if (!account_type) {
4544 return ACCOUNT_ERROR_INVALID_PARAMETER;
4547 if (!small_icon_path) {
4548 return ACCOUNT_ERROR_INVALID_PARAMETER;
4551 account_type_s *data = (account_type_s*)account_type;
4553 (*small_icon_path) = NULL;
4554 *small_icon_path = _account_get_text(data->small_icon_path);
4556 return ACCOUNT_ERROR_NONE;
4559 int account_type_get_multiple_account_support(account_type_h account_type, int *multiple_account_support)
4561 if (!account_type) {
4562 return ACCOUNT_ERROR_INVALID_PARAMETER;
4564 if (!multiple_account_support) {
4565 return ACCOUNT_ERROR_INVALID_PARAMETER;
4568 account_type_s *data = (account_type_s*)account_type;
4570 *multiple_account_support = data->multiple_account_support;
4572 return ACCOUNT_ERROR_NONE;
4575 int account_type_get_label_by_locale(account_type_h account_type, const char* locale, char** label)
4577 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4578 ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID PARAMETER"));
4581 account_type_s *data = (account_type_s*)account_type;
4583 for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) {
4584 label_s *label_data = NULL;
4586 label_data = (label_s*)iter->data;
4590 if(!strcmp(locale, label_data->locale)) {
4591 *label = _account_get_text(label_data->label);
4592 return ACCOUNT_ERROR_NONE;
4596 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4599 int account_type_get_label(account_type_h account_type, account_label_cb callback, void *user_data)
4601 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4602 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4605 account_type_s *data = (account_type_s*)account_type;
4607 for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) {
4608 label_s *label_data = NULL;
4610 label_data = (label_s*)iter->data;
4612 if(callback(label_data->app_id, label_data->label, label_data->locale, user_data)!=TRUE) {
4613 ACCOUNT_DEBUG("Callback func returs FALSE, its iteration is stopped!!!!\n");
4614 return ACCOUNT_ERROR_NONE;
4618 return ACCOUNT_ERROR_NONE;
4621 static gboolean _account_type_check_duplicated(account_type_s *data)
4623 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4626 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4628 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId='%s'"
4629 , ACCOUNT_TYPE_TABLE, data->app_id);
4631 count = _account_get_record_count(query);
4639 static int _account_type_convert_account_to_sql(account_type_s *account_type, account_stmt hstmt, char *sql_value)
4645 /*Caution : Keep insert query orders.*/
4648 _account_query_bind_text(hstmt, count++, (char*)account_type->app_id);
4650 /* 2. service provider id*/
4651 _account_query_bind_text(hstmt, count++, (char*)account_type->service_provider_id);
4654 _account_query_bind_text(hstmt, count++, (char*)account_type->icon_path);
4656 /* 4. small icon path*/
4657 _account_query_bind_text(hstmt, count++, (char*)account_type->small_icon_path);
4659 /* 5. multiple accont support*/
4660 _account_query_bind_int(hstmt, count++, account_type->multiple_account_support);
4668 static int _account_type_execute_insert_query(account_type_s *account_type)
4673 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4674 int error_code = ACCOUNT_ERROR_NONE;
4675 account_stmt hstmt = NULL;
4677 /* check mandatory field */
4678 // app id & service provider id
4679 if (!account_type->app_id) {
4680 return ACCOUNT_ERROR_INVALID_PARAMETER;
4683 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4684 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s( AppId, ServiceProviderId , IconPath , SmallIconPath , MultipleAccountSupport ) values "
4685 "(?, ?, ?, ?, ?)", ACCOUNT_TYPE_TABLE);
4688 hstmt = _account_prepare_query(query);
4691 if( _account_db_err_code() == SQLITE_PERM ){
4692 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4693 return ACCOUNT_ERROR_PERMISSION_DENIED;
4694 } else if( _account_db_err_code() == SQLITE_BUSY ){
4695 ACCOUNT_ERROR( "Database Busy(%s)", _account_db_err_msg());
4696 return ACCOUNT_ERROR_DATABASE_BUSY;
4699 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4702 _account_type_convert_account_to_sql(account_type, hstmt, query);
4705 rc = _account_query_step(hstmt);
4706 if (rc == SQLITE_BUSY) {
4707 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4708 error_code = ACCOUNT_ERROR_DATABASE_BUSY;
4709 } else if (rc != SQLITE_DONE) {
4710 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4711 error_code = ACCOUNT_ERROR_DB_FAILED;
4715 rc = _account_query_finalize(hstmt);
4716 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4723 static int _account_type_insert_label(account_type_s *account_type)
4726 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4727 account_stmt hstmt = NULL;
4729 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4731 if (g_slist_length( account_type->label_list)==0) {
4732 ACCOUNT_ERROR( "_account_type_insert_label, no label\n");
4733 return ACCOUNT_ERROR_NONE;
4736 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId = '%s'", ACCOUNT_TYPE_TABLE, account_type->app_id);
4738 rc = _account_get_record_count(query);
4740 if( _account_db_err_code() == SQLITE_PERM ){
4741 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4742 return ACCOUNT_ERROR_PERMISSION_DENIED;
4746 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4752 for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
4755 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4756 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
4757 "(?, ?, ?) ", LABEL_TABLE);
4759 hstmt = _account_prepare_query(query);
4761 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4763 label_s* label_data = NULL;
4764 label_data = (label_s*)iter->data;
4766 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4767 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4768 ret = _account_query_bind_text(hstmt, count++, label_data->label);
4769 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4770 ret = _account_query_bind_text(hstmt, count++, (char*)label_data->locale);
4771 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4773 rc = _account_query_step(hstmt);
4775 if (rc != SQLITE_DONE) {
4776 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4780 rc = _account_query_finalize(hstmt);
4781 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4786 return ACCOUNT_ERROR_NONE;
4789 static void _account_type_convert_column_to_provider_feature(account_stmt hstmt, provider_feature_s *feature_record)
4791 const char *textbuf = NULL;
4793 textbuf = _account_query_table_column_text(hstmt, PROVIDER_FEATURE_FIELD_APP_ID);
4794 _account_db_data_to_text(textbuf, &(feature_record->app_id));
4796 textbuf = _account_query_table_column_text(hstmt, PROVIDER_FEATURE_FIELD_KEY);
4797 _account_db_data_to_text(textbuf, &(feature_record->key));
4801 GSList* _account_type_query_provider_feature_by_app_id_from_global_db(const char* app_id, int *error_code)
4803 _INFO("_account_type_query_provider_feature_by_app_id_in_global_db app_id=%s", app_id);
4804 account_stmt hstmt = NULL;
4805 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4806 int rc = 0, binding_count = 1;
4807 GSList* feature_list = NULL;
4809 ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
4810 ACCOUNT_RETURN_VAL((error_code != NULL), {_ERR("error_code pointer is NULL");}, NULL, (""));
4811 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER; _ERR("The database isn't connected.");}, NULL, ("The database isn't connected."));
4813 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4815 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
4816 _INFO("account query=[%s]", query);
4818 hstmt = _account_prepare_query_from_global_db(query);
4820 if( _account_db_err_code_from_global_db() == SQLITE_PERM ){
4821 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4822 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
4826 _INFO("before _account_query_bind_text");
4827 _account_query_bind_text(hstmt, binding_count++, app_id);
4829 rc = _account_query_step(hstmt);
4831 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"));
4833 provider_feature_s* feature_record = NULL;
4835 while (rc == SQLITE_ROW) {
4837 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
4839 if (feature_record == NULL) {
4840 ACCOUNT_FATAL("malloc Failed");
4844 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
4846 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
4848 _INFO("Adding account feature_list");
4849 feature_list = g_slist_append(feature_list, feature_record);
4851 rc = _account_query_step(hstmt);
4854 *error_code = ACCOUNT_ERROR_NONE;
4857 if (hstmt != NULL) {
4858 rc = _account_query_finalize_from_global_db(hstmt);
4859 if (rc != ACCOUNT_ERROR_NONE) {
4861 _ERR("global db fianlize error");
4865 if (*error_code != ACCOUNT_ERROR_NONE) {
4866 _account_type_gslist_feature_free(feature_list);
4869 _INFO("Returning account feature_list from global db");
4870 return feature_list;
4873 GSList* _account_type_query_provider_feature_by_app_id(const char* app_id, int *error_code)
4875 _INFO("_account_type_query_provider_feature_by_app_id app_id=%s", app_id);
4876 account_stmt hstmt = NULL;
4877 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4878 int rc = 0, binding_count = 1;
4879 GSList* feature_list = NULL;
4881 ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
4882 ACCOUNT_RETURN_VAL((error_code != NULL), {_ERR("error_code pointer is NULL");}, NULL, (""));
4883 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
4885 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4887 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
4888 _INFO("account query=[%s]", query);
4890 hstmt = _account_prepare_query(query);
4892 if( _account_db_err_code() == SQLITE_PERM ){
4893 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4894 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
4898 _account_query_bind_text(hstmt, binding_count++, app_id);
4900 rc = _account_query_step(hstmt);
4902 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"));
4904 provider_feature_s* feature_record = NULL;
4906 while (rc == SQLITE_ROW) {
4908 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
4910 if (feature_record == NULL) {
4911 ACCOUNT_FATAL("malloc Failed");
4915 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
4917 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
4919 _INFO("Adding account feature_list");
4920 feature_list = g_slist_append(feature_list, feature_record);
4922 rc = _account_query_step(hstmt);
4925 *error_code = ACCOUNT_ERROR_NONE;
4927 rc = _account_query_finalize(hstmt);
4928 ACCOUNT_CATCH_ERROR_P((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, rc, ("account finalize error"));
4932 if (hstmt != NULL) {
4933 rc = _account_query_finalize(hstmt);
4934 if (rc != ACCOUNT_ERROR_NONE) {
4936 _ERR("account fianlize error");
4940 _INFO("*error_code=[%d]", *error_code);
4942 if (*error_code == ACCOUNT_ERROR_RECORD_NOT_FOUND) {
4943 feature_list = _account_type_query_provider_feature_by_app_id_from_global_db(app_id, error_code);
4946 if (*error_code != ACCOUNT_ERROR_NONE)
4947 _account_type_gslist_feature_free(feature_list);
4949 _INFO("Returning account feature_list");
4950 return feature_list;
4953 int _account_type_query_provider_feature_cb_by_app_id_from_global_db(provider_feature_cb callback, const char* app_id, void *user_data )
4955 int error_code = ACCOUNT_ERROR_NONE;
4956 account_stmt hstmt = NULL;
4957 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4958 int rc = 0, binding_count = 1;
4960 _INFO("_account_type_query_provider_feature_cb_by_app_id_in_global_db start app_id=%s", app_id);
4961 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4962 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4963 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4965 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4967 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
4968 hstmt = _account_prepare_query_from_global_db(query);
4970 if( _account_db_err_code_from_global_db() == SQLITE_PERM ){
4971 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg_from_global_db());
4972 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_PERMISSION_DENIED, ("global db permission denied.\n"));
4975 _account_query_bind_text(hstmt, binding_count++, app_id);
4977 rc = _account_query_step(hstmt);
4978 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"));
4980 provider_feature_s* feature_record = NULL;
4982 while (rc == SQLITE_ROW) {
4983 bool cb_ret = FALSE;
4984 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
4986 if (feature_record == NULL) {
4987 ACCOUNT_FATAL("malloc Failed");
4991 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
4993 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
4995 cb_ret = callback(feature_record->app_id, feature_record->key, user_data);
4997 _account_type_free_feature_with_items(feature_record);
4999 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returns FALSE, its iteration is stopped!!!!\n"));
5001 rc = _account_query_step(hstmt);
5004 error_code = ACCOUNT_ERROR_NONE;
5007 if (hstmt != NULL) {
5008 rc = _account_query_finalize_from_global_db(hstmt);
5009 if (rc != ACCOUNT_ERROR_NONE) {
5011 _ERR("global db finalize error[%d]", rc);
5016 _INFO("_account_type_query_provider_feature_cb_by_app_id_in_global_db end. error_code=[%d]", error_code);
5020 int _account_type_query_provider_feature_cb_by_app_id(provider_feature_cb callback, const char* app_id, void *user_data )
5022 int error_code = ACCOUNT_ERROR_NONE;
5023 account_stmt hstmt = NULL;
5024 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5025 int rc = 0, binding_count = 1;
5027 _INFO("_account_type_query_provider_feature_cb_by_app_id start app_id=%s", app_id);
5028 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5029 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
5030 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5032 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5034 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
5035 hstmt = _account_prepare_query(query);
5037 if( _account_db_err_code() == SQLITE_PERM ){
5038 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5039 return ACCOUNT_ERROR_PERMISSION_DENIED;
5042 _account_query_bind_text(hstmt, binding_count++, app_id);
5044 rc = _account_query_step(hstmt);
5045 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found in user db.\n"));
5047 provider_feature_s* feature_record = NULL;
5049 while (rc == SQLITE_ROW) {
5050 bool cb_ret = FALSE;
5051 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
5053 if (feature_record == NULL) {
5054 ACCOUNT_FATAL("malloc Failed");
5058 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
5060 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
5062 cb_ret = callback(feature_record->app_id, feature_record->key, user_data);
5064 _account_type_free_feature_with_items(feature_record);
5066 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
5068 rc = _account_query_step(hstmt);
5071 rc = _account_query_finalize(hstmt);
5072 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5075 error_code = ACCOUNT_ERROR_NONE;
5078 if (hstmt != NULL) {
5079 rc = _account_query_finalize(hstmt);
5080 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5084 if (error_code == ACCOUNT_ERROR_RECORD_NOT_FOUND) {
5085 error_code = _account_type_query_provider_feature_cb_by_app_id_from_global_db(callback, app_id, user_data);
5088 _INFO("_account_type_query_provider_feature_cb_by_app_id end");
5092 int account_type_query_provider_feature_cb_by_app_id(provider_feature_cb callback, const char* app_id, void *user_data )
5094 int error_code = ACCOUNT_ERROR_NONE;
5096 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5097 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
5098 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5100 error_code = _account_type_query_provider_feature_cb_by_app_id(callback, app_id, user_data);
5102 if (error_code == ACCOUNT_ERROR_RECORD_NOT_FOUND) {
5103 error_code = _account_type_query_provider_feature_cb_by_app_id_from_global_db(callback, app_id, user_data);
5109 bool _account_type_query_supported_feature_from_global_db(const char* app_id, const char* capability, int *error_code)
5111 _INFO("_account_type_query_supported_feature_in_global_db start");
5112 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5114 *error_code = ACCOUNT_ERROR_NONE;
5116 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5117 int record_count = 0;
5119 if (app_id == NULL || capability == NULL)
5121 *error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
5125 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
5127 record_count = _account_get_record_count_from_global_db(query);
5129 if( _account_db_err_code_from_global_db() == SQLITE_PERM ){
5130 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg_from_global_db());
5131 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
5134 if (record_count <= 0)
5136 *error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
5140 _INFO("_account_type_query_supported_feature_in_global_db end");
5144 bool _account_type_query_supported_feature(const char* app_id, const char* capability, int *error_code)
5146 _INFO("_account_type_query_supported_feature start");
5148 *error_code = ACCOUNT_ERROR_NONE;
5150 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5151 int record_count = 0;
5153 if (app_id == NULL || capability == NULL)
5155 *error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
5159 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
5161 record_count = _account_get_record_count(query);
5163 if( _account_db_err_code() == SQLITE_PERM ){
5164 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5165 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
5169 if (record_count <= 0)
5171 bool is_exist = false;
5172 is_exist = _account_type_query_supported_feature_from_global_db(app_id, capability, error_code);
5177 _INFO("_account_type_query_supported_feature end");
5181 static int _account_type_insert_provider_feature(account_type_s *account_type, const char* app_id)
5184 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5185 account_stmt hstmt = NULL;
5187 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
5188 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5190 if (g_slist_length( account_type->provider_feature_list)==0) {
5191 ACCOUNT_ERROR( "no capability\n");
5192 return ACCOUNT_ERROR_NONE;
5195 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId='%s'", ACCOUNT_TYPE_TABLE, app_id);
5197 rc = _account_get_record_count(query);
5199 if( _account_db_err_code() == SQLITE_PERM ){
5200 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5201 return ACCOUNT_ERROR_PERMISSION_DENIED;
5205 ACCOUNT_SLOGI( "related account type item is not existed rc=%d , %s", rc, _account_db_err_msg());
5206 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5213 for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
5216 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5217 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
5218 "(?, ?) ", PROVIDER_FEATURE_TABLE);
5220 hstmt = _account_prepare_query(query);
5222 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
5224 provider_feature_s* feature_data = NULL;
5225 feature_data = (provider_feature_s*)iter->data;
5227 ret = _account_query_bind_text(hstmt, count++, app_id);
5228 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5229 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
5230 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
5232 rc = _account_query_step(hstmt);
5234 if (rc != SQLITE_DONE) {
5235 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5239 rc = _account_query_finalize(hstmt);
5240 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5245 return ACCOUNT_ERROR_NONE;
5248 int _account_type_insert_to_db(account_type_s* account_type, int* account_type_id)
5252 int error_code = ACCOUNT_ERROR_NONE, ret_transaction = 0;
5254 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5255 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE HANDLE IS NULL"));
5256 ACCOUNT_RETURN_VAL((account_type_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE ID POINTER IS NULL"));
5258 account_type_s *data = (account_type_s*)account_type;
5260 pthread_mutex_lock(&account_mutex);
5263 /* transaction control required*/
5264 ret_transaction = _account_begin_transaction();
5268 if( _account_db_err_code() == SQLITE_PERM ){
5269 pthread_mutex_unlock(&account_mutex);
5270 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5271 return ACCOUNT_ERROR_PERMISSION_DENIED;
5275 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
5276 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
5277 pthread_mutex_unlock(&account_mutex);
5278 return ACCOUNT_ERROR_DATABASE_BUSY;
5279 } else if (ret_transaction != ACCOUNT_ERROR_NONE) {
5280 ACCOUNT_ERROR("_account_begin_transaction fail %d\n", ret_transaction);
5281 pthread_mutex_unlock(&account_mutex);
5282 return ret_transaction;
5286 if (_account_type_check_duplicated(data)) {
5288 ret_transaction = _account_end_transaction(FALSE);
5289 ACCOUNT_ERROR("Duplicated, rollback insert query(%x)!!!!\n", ret_transaction);
5290 *account_type_id = -1;
5291 pthread_mutex_unlock(&account_mutex);
5292 return ACCOUNT_ERROR_DUPLICATED;
5295 *account_type_id = _account_get_next_sequence(ACCOUNT_TYPE_TABLE);
5297 error_code = _account_type_execute_insert_query(data);
5299 if (error_code != ACCOUNT_ERROR_NONE){
5300 error_code = ACCOUNT_ERROR_DUPLICATED;
5301 ret_transaction = _account_end_transaction(FALSE);
5302 ACCOUNT_ERROR("Insert fail, rollback insert query(%x)!!!!\n", ret_transaction);
5303 *account_type_id = -1;
5304 pthread_mutex_unlock(&account_mutex);
5310 error_code = _account_type_insert_provider_feature(data, data->app_id);
5311 if(error_code != ACCOUNT_ERROR_NONE) {
5313 ret_transaction = _account_end_transaction(FALSE);
5314 ACCOUNT_ERROR("Insert provider feature fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction);
5315 pthread_mutex_unlock(&account_mutex);
5319 error_code = _account_type_insert_label(data);
5320 if(error_code != ACCOUNT_ERROR_NONE) {
5322 ret_transaction = _account_end_transaction(FALSE);
5323 ACCOUNT_ERROR("Insert label fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction);
5324 pthread_mutex_unlock(&account_mutex);
5328 ret_transaction = _account_end_transaction(TRUE);
5330 pthread_mutex_unlock(&account_mutex);
5333 return ACCOUNT_ERROR_NONE;
5336 static int _account_type_update_provider_feature(account_type_s *account_type, const char* app_id)
5339 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5340 account_stmt hstmt = NULL;
5342 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
5344 if (g_slist_length( account_type->provider_feature_list)==0) {
5345 ACCOUNT_ERROR( "no feature\n");
5346 return ACCOUNT_ERROR_NONE;
5349 ACCOUNT_DEBUG( "app id", app_id);
5351 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5353 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id=? ", PROVIDER_FEATURE_TABLE);
5354 hstmt = _account_prepare_query(query);
5356 if( _account_db_err_code() == SQLITE_PERM ){
5357 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5358 return ACCOUNT_ERROR_PERMISSION_DENIED;
5362 _account_query_bind_text(hstmt, count++, app_id);
5363 rc = _account_query_step(hstmt);
5365 if (rc != SQLITE_DONE) {
5366 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5367 return ACCOUNT_ERROR_DB_FAILED;
5369 rc = _account_query_finalize(hstmt);
5370 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5375 for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
5378 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5379 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
5380 "(?, ?) ", PROVIDER_FEATURE_TABLE);
5382 hstmt = _account_prepare_query(query);
5384 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
5386 provider_feature_s* feature_data = NULL;
5387 feature_data = (provider_feature_s*)iter->data;
5389 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
5390 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5391 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
5392 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5394 rc = _account_query_step(hstmt);
5396 if (rc != SQLITE_DONE) {
5397 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5400 rc = _account_query_finalize(hstmt);
5401 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5405 return ACCOUNT_ERROR_NONE;
5408 static int _account_type_update_label(account_type_s *account_type, const char* app_id)
5411 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5412 account_stmt hstmt = NULL;
5414 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
5416 if (g_slist_length( account_type->label_list)==0) {
5417 return ACCOUNT_ERROR_NONE;
5420 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5422 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId=? ", LABEL_TABLE);
5423 hstmt = _account_prepare_query(query);
5425 if( _account_db_err_code() == SQLITE_PERM ){
5426 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5427 return ACCOUNT_ERROR_PERMISSION_DENIED;
5431 _account_query_bind_text(hstmt, count++, app_id);
5432 rc = _account_query_step(hstmt);
5434 if (rc != SQLITE_DONE) {
5435 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5436 return ACCOUNT_ERROR_DB_FAILED;
5438 rc = _account_query_finalize(hstmt);
5439 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5444 for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
5447 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5448 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
5449 "(?, ?, ?) ", LABEL_TABLE);
5451 hstmt = _account_prepare_query(query);
5453 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
5455 label_s* label_data = NULL;
5456 label_data = (label_s*)iter->data;
5458 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
5459 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5460 ret = _account_query_bind_text(hstmt, count++, label_data->label);
5461 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5462 ret = _account_query_bind_text(hstmt, count++, label_data->locale);
5463 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5465 rc = _account_query_step(hstmt);
5467 if (rc != SQLITE_DONE) {
5468 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5471 rc = _account_query_finalize(hstmt);
5472 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5476 return ACCOUNT_ERROR_NONE;
5480 static int _account_type_update_account(account_type_s *account_type, const char* app_id)
5482 int rc = 0, binding_count =1;
5483 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5484 int error_code = ACCOUNT_ERROR_NONE;
5485 account_stmt hstmt = NULL;
5487 if (!account_type->app_id) {
5488 ACCOUNT_ERROR("app id is mandetory field, it can not be NULL!!!!\n");
5489 return ACCOUNT_ERROR_INVALID_PARAMETER;
5492 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5493 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET AppId=?, ServiceProviderId=?, IconPath=?, "
5494 "SmallIconPath=?, MultipleAccountSupport=? WHERE AppId=? ", ACCOUNT_TYPE_TABLE);
5496 hstmt = _account_prepare_query(query);
5498 if( _account_db_err_code() == SQLITE_PERM ){
5499 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5500 return ACCOUNT_ERROR_PERMISSION_DENIED;
5501 } else if (_account_db_err_code() == SQLITE_BUSY){
5502 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
5503 return ACCOUNT_ERROR_DATABASE_BUSY;
5506 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg()));
5508 binding_count = _account_type_convert_account_to_sql(account_type, hstmt, query);
5509 _account_query_bind_text(hstmt, binding_count++, app_id);
5511 rc = _account_query_step(hstmt);
5512 if (rc != SQLITE_DONE) {
5513 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5516 rc = _account_query_finalize(hstmt);
5517 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5521 error_code = _account_type_update_label(account_type, app_id);
5522 /* update provider feature */
5523 error_code = _account_type_update_provider_feature(account_type, app_id);
5528 int _account_type_update_to_db_by_app_id(account_type_s* account_type, const char* app_id)
5530 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
5531 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5532 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5534 int error_code = ACCOUNT_ERROR_NONE;
5535 account_type_s* data = account_type;
5537 pthread_mutex_lock(&account_mutex);
5539 error_code = _account_type_update_account(data, app_id);
5541 pthread_mutex_unlock(&account_mutex);
5546 int _account_type_delete_by_app_id(const char* app_id)
5548 int error_code = ACCOUNT_ERROR_NONE;
5549 account_stmt hstmt = NULL;
5550 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5551 int rc = 0, count = -1;
5552 int ret_transaction = 0;
5553 int binding_count = 1;
5554 bool is_success = FALSE;
5556 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5557 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("The database isn't connected."));
5559 /* Check requested ID to delete */
5560 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
5562 count = _account_get_record_count(query);
5564 if( _account_db_err_code() == SQLITE_PERM ){
5565 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5566 return ACCOUNT_ERROR_PERMISSION_DENIED;
5570 ACCOUNT_SLOGE("app id(%s) is not exist. count(%d)\n", app_id, count);
5571 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5574 /* transaction control required*/
5575 ret_transaction = _account_begin_transaction();
5577 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
5578 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
5579 pthread_mutex_unlock(&account_mutex);
5580 return ACCOUNT_ERROR_DATABASE_BUSY;
5581 }else if (ret_transaction != ACCOUNT_ERROR_NONE) {
5582 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
5583 pthread_mutex_unlock(&account_mutex);
5584 return ret_transaction;
5587 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", LABEL_TABLE);
5589 hstmt = _account_prepare_query(query);
5591 if( _account_db_err_code() == SQLITE_PERM ){
5592 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5593 pthread_mutex_unlock(&account_mutex);
5594 return ACCOUNT_ERROR_PERMISSION_DENIED;
5597 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
5598 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
5600 _account_query_bind_text(hstmt, binding_count++, app_id);
5602 rc = _account_query_step(hstmt);
5603 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5605 rc = _account_query_finalize(hstmt);
5606 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5610 ACCOUNT_MEMSET(query, 0, sizeof(query));
5612 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id = ? ", PROVIDER_FEATURE_TABLE);
5614 hstmt = _account_prepare_query(query);
5615 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
5616 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
5618 _account_query_bind_text(hstmt, binding_count++, app_id);
5620 rc = _account_query_step(hstmt);
5621 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
5623 rc = _account_query_finalize(hstmt);
5624 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5630 ACCOUNT_MEMSET(query, 0, sizeof(query));
5632 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ? ", ACCOUNT_TYPE_TABLE);
5634 hstmt = _account_prepare_query(query);
5635 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
5636 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
5638 _account_query_bind_text(hstmt, binding_count++, app_id);
5640 rc = _account_query_step(hstmt);
5641 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
5643 rc = _account_query_finalize(hstmt);
5644 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5650 if (hstmt != NULL) {
5651 rc = _account_query_finalize(hstmt);
5652 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5656 ret_transaction = _account_end_transaction(is_success);
5658 if (ret_transaction != ACCOUNT_ERROR_NONE) {
5659 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
5662 pthread_mutex_unlock(&account_mutex);
5667 static void _account_type_convert_column_to_account_type(account_stmt hstmt, account_type_s *account_type_record)
5669 const char *textbuf = NULL;
5671 account_type_record->id = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_ID);
5673 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_APP_ID);
5674 _account_db_data_to_text(textbuf, &(account_type_record->app_id));
5676 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SERVICE_PROVIDER_ID);
5677 _account_db_data_to_text(textbuf, &(account_type_record->service_provider_id));
5679 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_ICON_PATH);
5680 _account_db_data_to_text(textbuf, &(account_type_record->icon_path));
5682 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SMALL_ICON_PATH);
5683 _account_db_data_to_text(textbuf, &(account_type_record->small_icon_path));
5685 account_type_record->multiple_account_support = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_MULTIPLE_ACCOUNT_SUPPORT);
5689 static void _account_type_convert_column_to_label(account_stmt hstmt, label_s *label_record)
5691 const char *textbuf = NULL;
5693 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_APP_ID);
5694 _account_db_data_to_text(textbuf, &(label_record->app_id));
5696 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LABEL);
5697 _account_db_data_to_text(textbuf, &(label_record->label));
5699 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LOCALE);
5700 _account_db_data_to_text(textbuf, &(label_record->locale));
5704 GSList* _account_type_get_label_list_by_app_id_from_global_db(const char* app_id, int *error_code )
5706 *error_code = ACCOUNT_ERROR_NONE;
5707 account_stmt hstmt = NULL;
5708 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5709 int rc = 0, binding_count = 1;
5710 GSList* label_list = NULL;
5712 ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
5713 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5715 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5717 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
5718 hstmt = _account_prepare_query_from_global_db(query);
5720 if( _account_db_err_code_from_global_db() == SQLITE_PERM ){
5721 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg_from_global_db());
5722 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
5727 _account_query_bind_text(hstmt, binding_count++, app_id);
5729 rc = _account_query_step(hstmt);
5730 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"));
5732 label_s* label_record = NULL;
5734 while (rc == SQLITE_ROW) {
5735 label_record = (label_s*) malloc(sizeof(label_s));
5737 if (label_record == NULL) {
5738 ACCOUNT_FATAL("malloc Failed");
5742 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
5744 _account_type_convert_column_to_label(hstmt, label_record);
5746 _INFO("Adding account label_list");
5747 label_list = g_slist_append (label_list, label_record);
5749 rc = _account_query_step(hstmt);
5752 *error_code = ACCOUNT_ERROR_NONE;
5755 if (hstmt != NULL) {
5756 rc = _account_query_finalize_from_global_db(hstmt);
5757 if (rc != ACCOUNT_ERROR_NONE) {
5758 _ERR("global db finalize error[%d]", rc);
5763 _INFO("Returning account global label_list");
5767 GSList* _account_type_get_label_list_by_app_id(const char* app_id, int *error_code )
5769 *error_code = ACCOUNT_ERROR_NONE;
5770 account_stmt hstmt = NULL;
5771 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5772 int rc = 0, binding_count = 1;
5773 GSList* label_list = NULL;
5775 ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
5776 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
5778 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5780 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
5781 hstmt = _account_prepare_query(query);
5783 if( _account_db_err_code() == SQLITE_PERM ){
5784 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5785 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
5789 _account_query_bind_text(hstmt, binding_count++, app_id);
5791 rc = _account_query_step(hstmt);
5792 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5794 label_s* label_record = NULL;
5796 while (rc == SQLITE_ROW) {
5797 label_record = (label_s*) malloc(sizeof(label_s));
5799 if (label_record == NULL) {
5800 ACCOUNT_FATAL("malloc Failed");
5804 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
5806 _account_type_convert_column_to_label(hstmt, label_record);
5808 _INFO("Adding account label_list");
5809 label_list = g_slist_append (label_list, label_record);
5811 rc = _account_query_step(hstmt);
5814 rc = _account_query_finalize(hstmt);
5815 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
5818 *error_code = ACCOUNT_ERROR_NONE;
5821 if (hstmt != NULL) {
5822 rc = _account_query_finalize(hstmt);
5823 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
5827 if (*error_code == ACCOUNT_ERROR_RECORD_NOT_FOUND) {
5828 label_list = _account_type_get_label_list_by_app_id_from_global_db(app_id, error_code);
5831 _INFO("Returning account label_list");
5835 int _account_type_query_label_by_app_id_from_global_db(account_label_cb callback, const char* app_id, void *user_data )
5837 int error_code = ACCOUNT_ERROR_NONE;
5838 account_stmt hstmt = NULL;
5839 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5840 int rc = 0, binding_count = 1;
5842 _INFO("account_type_query_label_by_app_id_from_global_db start");
5844 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5845 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
5846 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5848 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5850 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
5851 hstmt = _account_prepare_query_from_global_db(query);
5853 if( _account_db_err_code_from_global_db() == SQLITE_PERM ){
5854 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg_from_global_db());
5855 error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
5859 _account_query_bind_text(hstmt, binding_count++, app_id);
5861 rc = _account_query_step(hstmt);
5862 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5864 label_s* label_record = NULL;
5866 while (rc == SQLITE_ROW) {
5867 bool cb_ret = FALSE;
5868 label_record = (label_s*) malloc(sizeof(label_s));
5870 if (label_record == NULL) {
5871 ACCOUNT_FATAL("malloc Failed");
5875 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
5877 _account_type_convert_column_to_label(hstmt, label_record);
5879 cb_ret = callback(label_record->app_id, label_record->label , label_record->locale, user_data);
5881 _account_type_free_label_with_items(label_record);
5883 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
5885 rc = _account_query_step(hstmt);
5888 error_code = ACCOUNT_ERROR_NONE;
5891 if (hstmt != NULL) {
5892 rc = _account_query_finalize_from_global_db(hstmt);
5893 if (rc != ACCOUNT_ERROR_NONE) {
5894 _ERR("global db finalize error[%d]", rc);
5899 _INFO("account_type_query_label_by_app_id_from_global_db end [%d]", error_code);
5903 int _account_type_query_label_by_app_id(account_label_cb callback, const char* app_id, void *user_data )
5905 int error_code = ACCOUNT_ERROR_NONE;
5906 account_stmt hstmt = NULL;
5907 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5908 int rc = 0, binding_count = 1;
5910 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5911 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
5912 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5914 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5916 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
5917 hstmt = _account_prepare_query(query);
5919 if( _account_db_err_code() == SQLITE_PERM ){
5920 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5921 return ACCOUNT_ERROR_PERMISSION_DENIED;
5924 _account_query_bind_text(hstmt, binding_count++, app_id);
5926 rc = _account_query_step(hstmt);
5927 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5929 label_s* label_record = NULL;
5931 while (rc == SQLITE_ROW) {
5932 bool cb_ret = FALSE;
5933 label_record = (label_s*) malloc(sizeof(label_s));
5935 if (label_record == NULL) {
5936 ACCOUNT_FATAL("malloc Failed");
5940 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
5942 _account_type_convert_column_to_label(hstmt, label_record);
5944 cb_ret = callback(label_record->app_id, label_record->label , label_record->locale, user_data);
5946 _account_type_free_label_with_items(label_record);
5948 // ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
5949 if(cb_ret == TRUE) {
5950 _INFO("Callback func returs FALSE, its iteration is stopped!!!!\n");
5954 rc = _account_query_step(hstmt);
5957 rc = _account_query_finalize(hstmt);
5958 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5961 error_code = ACCOUNT_ERROR_NONE;
5964 if (hstmt != NULL) {
5965 rc = _account_query_finalize(hstmt);
5966 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5970 if (error_code == ACCOUNT_ERROR_RECORD_NOT_FOUND) {
5971 error_code = account_type_query_label_by_app_id_from_global_db(callback, app_id, user_data);
5977 int account_type_query_label_by_app_id(account_label_cb callback, const char* app_id, void *user_data )
5979 int error_code = ACCOUNT_ERROR_NONE;
5981 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5982 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
5983 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5985 error_code = _account_type_query_label_by_app_id(callback, app_id, user_data);
5987 if (error_code == ACCOUNT_ERROR_RECORD_NOT_FOUND) {
5988 error_code = _account_type_query_label_by_app_id_from_global_db(callback, app_id, user_data);
5994 int _account_type_label_get_app_id(label_h label, char **app_id)
5997 return ACCOUNT_ERROR_INVALID_PARAMETER;
6001 return ACCOUNT_ERROR_INVALID_PARAMETER;
6004 label_s *data = (label_s*)label;
6008 *app_id = _account_get_text(data->app_id);
6010 return ACCOUNT_ERROR_NONE;
6013 bool _account_get_label_text_cb(char* app_id, char* label, char* locale, void *user_data)
6015 account_type_s *data = (account_type_s*)user_data;
6017 label_s *label_data = (label_s*)malloc(sizeof(label_s));
6019 if (label_data == NULL) {
6020 ACCOUNT_FATAL("_account_get_label_text_cb : MALLOC FAIL\n");
6023 ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
6025 label_data->app_id = _account_get_text(app_id);
6026 label_data->label = _account_get_text(label);
6027 label_data->locale = _account_get_text(locale);
6029 data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
6034 bool _account_get_provider_feature_cb(char* app_id, char* key, void* user_data)
6036 account_type_s *data = (account_type_s*)user_data;
6038 provider_feature_s *feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
6040 if (feature_data == NULL) {
6041 ACCOUNT_FATAL("_account_get_provider_feature_cb : MALLOC FAIL\n");
6044 ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
6046 feature_data->app_id = _account_get_text(app_id);
6047 feature_data->key = _account_get_text(key);
6049 data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
6054 int _account_type_query_by_app_id_from_global_db(const char* app_id, account_type_s** account_type_record)
6056 _INFO("_account_type_query_by_app_id_from_global_db start");
6058 int error_code = ACCOUNT_ERROR_NONE;
6059 account_stmt hstmt = NULL;
6060 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
6061 int rc = 0, binding_count = 1;
6063 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
6064 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
6065 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
6067 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
6069 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
6070 hstmt = _account_prepare_query_from_global_db(query);
6072 if( _account_db_err_code_from_global_db() == SQLITE_PERM ){
6073 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg_from_global_db());
6074 return ACCOUNT_ERROR_PERMISSION_DENIED;
6077 _account_query_bind_text(hstmt, binding_count++, app_id);
6079 rc = _account_query_step(hstmt);
6080 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
6082 *account_type_record = create_empty_account_type_instance();
6084 while (rc == SQLITE_ROW) {
6085 _account_type_convert_column_to_account_type(hstmt, *account_type_record);
6086 rc = _account_query_step(hstmt);
6089 rc = _account_query_finalize_from_global_db(hstmt);
6090 ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {_ERR("global db finalize error rc=[%d]", rc);}, rc, ("finalize error"));
6091 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb, app_id, (void*)(*account_type_record));
6092 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, app_id,(void*)(*account_type_record));
6095 error_code = ACCOUNT_ERROR_NONE;
6098 if (hstmt != NULL) {
6099 rc = _account_query_finalize(hstmt);
6100 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
6104 _INFO("_account_type_query_by_app_id_from_global_db end [%d]", error_code);
6108 int _account_type_query_by_app_id(const char* app_id, account_type_s** account_type_record)
6110 _INFO("_account_type_query_by_app_id start");
6112 int error_code = ACCOUNT_ERROR_NONE;
6113 account_stmt hstmt = NULL;
6114 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
6115 int rc = 0, binding_count = 1;
6117 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
6118 ACCOUNT_RETURN_VAL((account_type_record != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("account type record(account_type_s**) is NULL"));
6119 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
6121 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
6123 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
6124 hstmt = _account_prepare_query(query);
6126 if( _account_db_err_code() == SQLITE_PERM ){
6127 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
6128 return ACCOUNT_ERROR_PERMISSION_DENIED;
6131 _account_query_bind_text(hstmt, binding_count++, app_id);
6133 rc = _account_query_step(hstmt);
6134 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
6136 *account_type_record = create_empty_account_type_instance();
6137 if (*account_type_record == NULL) {
6138 _ERR("Out of Memory");
6139 error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
6143 while (rc == SQLITE_ROW) {
6144 _account_type_convert_column_to_account_type(hstmt, *account_type_record);
6145 rc = _account_query_step(hstmt);
6148 rc = _account_query_finalize(hstmt);
6149 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
6150 _account_type_query_label_by_app_id(_account_get_label_text_cb, app_id, (void*)(*account_type_record));
6151 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, app_id,(void*)(*account_type_record));
6154 error_code = ACCOUNT_ERROR_NONE;
6157 if (hstmt != NULL) {
6158 rc = _account_query_finalize(hstmt);
6159 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
6163 if (error_code == ACCOUNT_ERROR_RECORD_NOT_FOUND) {
6164 error_code = _account_type_query_by_app_id_from_global_db(app_id, account_type_record);
6167 _INFO("_account_type_query_by_app_id end [%d]", error_code);
6171 int _account_type_query_app_id_exist(const char* app_id)
6173 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
6176 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
6177 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
6179 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
6181 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
6182 rc = _account_get_record_count(query);
6184 if( _account_db_err_code() == SQLITE_PERM ){
6185 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
6186 return ACCOUNT_ERROR_PERMISSION_DENIED;
6190 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
6193 return ACCOUNT_ERROR_NONE;
6196 int _account_type_query_app_id_exist_from_all_db(const char *app_id)
6198 _INFO("_account_type_query_app_id_exist_from_all_db start app_id=%s", app_id);
6199 int return_code = ACCOUNT_ERROR_NONE;
6201 return_code = _account_type_query_app_id_exist(app_id);
6203 if (return_code == ACCOUNT_ERROR_RECORD_NOT_FOUND) {
6204 return_code = _account_type_query_app_id_exist_from_global_db(app_id);
6208 _INFO("_account_type_query_app_id_exist_from_all_db end");
6212 int _account_type_query_by_provider_feature_from_global_db(const char* key, GSList **account_type_list_all)
6214 int error_code = ACCOUNT_ERROR_NONE;
6215 account_stmt hstmt = NULL;
6216 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
6218 GSList *account_type_list = NULL;
6220 _INFO("_account_type_query_by_provider_feature_from_global_db start key=%s", key);
6223 ACCOUNT_ERROR("capability_type IS NULL.");
6224 error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
6228 if(g_hAccountGlobalDB == NULL)
6230 ACCOUNT_ERROR("The database isn't connected.");
6231 error_code = ACCOUNT_ERROR_DB_NOT_OPENED;
6235 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
6237 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
6239 hstmt = _account_prepare_query_from_global_db(query);
6241 if( _account_db_err_code_from_global_db() == SQLITE_PERM )
6243 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg_from_global_db());
6244 error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
6248 int binding_count = 1;
6249 _account_query_bind_text(hstmt, binding_count++, key);
6251 rc = _account_query_step(hstmt);
6253 account_type_s *account_type_record = NULL;
6255 if(rc != SQLITE_ROW)
6257 ACCOUNT_ERROR("The record isn't found. rc=[%d]", rc);
6258 error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
6262 while(rc == SQLITE_ROW) {
6263 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
6265 if (account_type_record == NULL) {
6266 ACCOUNT_FATAL("malloc Failed");
6270 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
6271 _account_type_convert_column_to_account_type(hstmt, account_type_record);
6272 account_type_list = g_slist_append(account_type_list, account_type_record);
6273 rc = _account_query_step(hstmt);
6276 rc = _account_query_finalize_from_global_db(hstmt);
6277 if (rc != ACCOUNT_ERROR_NONE )
6279 _account_type_gslist_account_type_free(account_type_list);
6280 ACCOUNT_ERROR("finalize error(%s)", rc);
6288 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
6289 account_type_s *account_type = NULL;
6290 account_type = (account_type_s*)iter->data;
6291 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
6292 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
6293 _INFO("add label & provider_feature");
6296 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
6298 account_type_s *account_type = NULL;
6299 account_type = (account_type_s*)iter->data;
6300 *account_type_list_all = g_slist_append(*account_type_list_all, account_type);
6301 _INFO("add account_type");
6304 error_code = ACCOUNT_ERROR_NONE;
6307 if (hstmt != NULL) {
6308 rc = _account_query_finalize_from_global_db(hstmt);
6309 if (rc != ACCOUNT_ERROR_NONE)
6311 ACCOUNT_ERROR("finalize error(%s)", rc);
6317 _INFO("_account_type_query_by_provider_feature_from_global_db end. error_code=[%d]", error_code);
6321 GSList* _account_type_query_by_provider_feature(const char* key, int *error_code)
6323 *error_code = ACCOUNT_ERROR_NONE;
6324 account_stmt hstmt = NULL;
6325 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
6327 GSList *account_type_list = NULL;
6329 _INFO("account_type_query_by_provider_feature start key=%s", key);
6332 ACCOUNT_ERROR("capability_type IS NULL.");
6333 *error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
6337 if(g_hAccountDB == NULL)
6339 ACCOUNT_ERROR("The database isn't connected.");
6340 *error_code = ACCOUNT_ERROR_DB_NOT_OPENED;
6344 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
6346 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
6348 hstmt = _account_prepare_query(query);
6350 if( _account_db_err_code() == SQLITE_PERM )
6352 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
6353 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
6357 int binding_count = 1;
6358 _account_query_bind_text(hstmt, binding_count++, key);
6360 rc = _account_query_step(hstmt);
6362 account_type_s *account_type_record = NULL;
6364 if(rc != SQLITE_ROW)
6366 ACCOUNT_ERROR("The record isn't found. rc=[%d]", rc);
6367 *error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
6371 while(rc == SQLITE_ROW) {
6372 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
6374 if (account_type_record == NULL) {
6375 ACCOUNT_FATAL("malloc Failed");
6379 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
6380 _account_type_convert_column_to_account_type(hstmt, account_type_record);
6381 account_type_list = g_slist_append(account_type_list, account_type_record);
6382 rc = _account_query_step(hstmt);
6385 rc = _account_query_finalize(hstmt);
6386 if (rc != ACCOUNT_ERROR_NONE )
6388 _account_type_gslist_account_type_free(account_type_list);
6389 ACCOUNT_ERROR("finalize error(%s)", rc);
6397 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
6398 account_type_s *account_type = NULL;
6399 account_type = (account_type_s*)iter->data;
6400 _account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
6401 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
6404 *error_code = ACCOUNT_ERROR_NONE;
6407 if (hstmt != NULL) {
6408 rc = _account_query_finalize(hstmt);
6409 if (rc != ACCOUNT_ERROR_NONE)
6417 if (*error_code == ACCOUNT_ERROR_NONE || *error_code == ACCOUNT_ERROR_RECORD_NOT_FOUND) {
6418 rc = _account_type_query_by_provider_feature_from_global_db(key, &account_type_list);
6419 if (rc != ACCOUNT_ERROR_NONE && rc != ACCOUNT_ERROR_RECORD_NOT_FOUND) {
6420 ACCOUNT_ERROR( "_account_type_query_by_provider_feature_from_global_db fail=[%d]", rc);
6421 _account_type_gslist_account_type_free(account_type_list);
6424 if (rc == ACCOUNT_ERROR_NONE)
6428 _INFO("account_type_query_by_provider_feature end");
6429 return account_type_list;
6432 int _account_type_query_all_from_global_db(GSList **account_type_list_all)
6434 account_stmt hstmt = NULL;
6435 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
6436 int rc = ACCOUNT_ERROR_NONE;
6437 int error_code = ACCOUNT_ERROR_NONE;
6438 GSList *account_type_list = NULL;
6440 _INFO("_account_type_query_all_in_global_db start");
6441 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, NULL, ("The database isn't connected."));
6443 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
6445 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
6446 hstmt = _account_prepare_query_from_global_db(query);
6448 rc = _account_query_step(hstmt);
6450 if( _account_db_err_code_from_global_db() == SQLITE_PERM ){
6451 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg_from_global_db());
6452 return ACCOUNT_ERROR_PERMISSION_DENIED;
6455 account_type_s *account_type_record = NULL;
6457 if (rc != SQLITE_ROW)
6459 _INFO("[ACCOUNT_ERROR_RECORD_NOT_FOUND]The record isn't found.");
6460 error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
6464 while(rc == SQLITE_ROW) {
6465 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
6467 if (account_type_record == NULL) {
6468 ACCOUNT_FATAL("malloc Failed");
6472 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
6473 _account_type_convert_column_to_account_type(hstmt, account_type_record);
6474 account_type_list = g_slist_append(account_type_list, account_type_record);
6475 rc = _account_query_step(hstmt);
6478 rc = _account_query_finalize_from_global_db(hstmt);
6479 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
6484 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
6485 account_type_s *account_type = NULL;
6486 account_type = (account_type_s*)iter->data;
6487 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
6488 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
6491 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
6492 account_type_s *account_type = NULL;
6493 account_type = (account_type_s*)iter->data;
6494 *account_type_list_all = g_slist_append(*account_type_list_all, account_type);
6497 error_code = ACCOUNT_ERROR_NONE;
6501 rc = _account_query_finalize_from_global_db(hstmt);
6502 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list);}, rc, ("finalize error"));
6506 _INFO("_account_type_query_all_in_global_db end");
6510 GSList* _account_type_query_all(void)
6512 account_stmt hstmt = NULL;
6513 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
6515 int error_code = ACCOUNT_ERROR_NONE;
6516 GSList *account_type_list = NULL;
6518 _INFO("_account_type_query_all start");
6519 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected."));
6521 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
6523 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
6524 hstmt = _account_prepare_query(query);
6526 rc = _account_query_step(hstmt);
6528 if( _account_db_err_code() == SQLITE_PERM ){
6529 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
6533 account_type_s *account_type_record = NULL;
6535 if (rc != SQLITE_ROW)
6537 _INFO("[ACCOUNT_ERROR_RECORD_NOT_FOUND]The record isn't found.");
6538 error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
6542 while(rc == SQLITE_ROW) {
6543 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
6545 if (account_type_record == NULL) {
6546 ACCOUNT_FATAL("malloc Failed");
6550 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
6551 _account_type_convert_column_to_account_type(hstmt, account_type_record);
6552 account_type_list = g_slist_append(account_type_list, account_type_record);
6553 rc = _account_query_step(hstmt);
6556 rc = _account_query_finalize(hstmt);
6557 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, NULL, ("finalize error"));
6562 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
6563 account_type_s *account_type = NULL;
6564 account_type = (account_type_s*)iter->data;
6565 _account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
6566 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
6569 error_code = ACCOUNT_ERROR_NONE;
6573 rc = _account_query_finalize(hstmt);
6574 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list);}, NULL, ("finalize error"));
6578 if (error_code == ACCOUNT_ERROR_NONE || error_code == ACCOUNT_ERROR_RECORD_NOT_FOUND) {
6579 error_code = _account_type_query_all_from_global_db(&account_type_list);
6580 if (rc != ACCOUNT_ERROR_NONE && rc != ACCOUNT_ERROR_RECORD_NOT_FOUND) {
6581 ACCOUNT_ERROR( "_account_type_query_all_from_global_db fail=[%d]", rc);
6582 _account_type_gslist_account_type_free(account_type_list);
6587 _INFO("_account_type_query_all end");
6588 return account_type_list;
6591 // output parameter label must be free
6592 int _account_type_query_label_by_locale_from_global_db(const char* app_id, const char* locale, char **label)
6594 int error_code = ACCOUNT_ERROR_NONE;
6595 account_stmt hstmt = NULL;
6596 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
6597 int rc = 0, binding_count = 1;
6598 char* converted_locale = NULL;
6600 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
6601 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
6602 ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
6603 ACCOUNT_RETURN_VAL((locale != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("locale char is null"));
6604 //Making label newly created
6606 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
6608 converted_locale = _account_get_text(locale);
6609 gchar** tokens = g_strsplit(converted_locale, "-", 2);
6611 if(tokens != NULL) {
6612 if((char*)(tokens[1]) != NULL) {
6613 char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1]));
6614 if(upper_token != NULL) {
6615 _ACCOUNT_FREE(converted_locale);
6616 converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token);
6618 _ACCOUNT_FREE(upper_token);
6623 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, converted_locale);
6624 _ACCOUNT_FREE(converted_locale);
6626 hstmt = _account_prepare_query_from_global_db(query);
6628 if( _account_db_err_code_from_global_db() == SQLITE_PERM ){
6629 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg_from_global_db());
6630 return ACCOUNT_ERROR_PERMISSION_DENIED;
6633 _account_query_bind_text(hstmt, binding_count++, app_id);
6635 rc = _account_query_step(hstmt);
6636 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
6638 label_s* label_record = NULL;
6640 while (rc == SQLITE_ROW) {
6641 label_record = (label_s*) malloc(sizeof(label_s));
6643 if (label_record == NULL) {
6644 ACCOUNT_FATAL("malloc Failed");
6648 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
6650 _account_type_convert_column_to_label(hstmt,label_record);
6652 _ACCOUNT_FREE(*label);
6653 //Making label newly created
6654 *label = _account_get_text(label_record->label);
6656 _account_type_free_label_with_items(label_record);
6658 rc = _account_query_step(hstmt);
6661 rc = _account_query_finalize_from_global_db(hstmt);
6662 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
6665 error_code = ACCOUNT_ERROR_NONE;
6668 if (hstmt != NULL) {
6669 rc = _account_query_finalize_from_global_db(hstmt);
6670 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
6674 _INFO("_account_type_query_label_by_locale_from_global_db() end : error_code = %d", error_code);
6678 // output parameter label must be free
6679 int _account_type_query_label_by_locale(const char* app_id, const char* locale, char **label)
6681 int error_code = ACCOUNT_ERROR_NONE;
6682 account_stmt hstmt = NULL;
6683 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
6684 int rc = 0, binding_count = 1;
6685 char* converted_locale = NULL;
6687 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
6688 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
6689 ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
6690 ACCOUNT_RETURN_VAL((locale != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("locale char is null"));
6691 //Making label newly created
6693 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
6695 converted_locale = _account_get_text(locale);
6696 gchar** tokens = g_strsplit(converted_locale, "-", 2);
6698 if(tokens != NULL) {
6699 if((char*)(tokens[1]) != NULL) {
6700 char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1]));
6701 if(upper_token != NULL) {
6702 _ACCOUNT_FREE(converted_locale);
6703 converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token);
6705 _ACCOUNT_FREE(upper_token);
6710 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, converted_locale);
6711 _ACCOUNT_FREE(converted_locale);
6713 hstmt = _account_prepare_query(query);
6715 if( _account_db_err_code() == SQLITE_PERM ){
6716 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
6717 return ACCOUNT_ERROR_PERMISSION_DENIED;
6720 _account_query_bind_text(hstmt, binding_count++, app_id);
6722 rc = _account_query_step(hstmt);
6723 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
6725 label_s* label_record = NULL;
6727 while (rc == SQLITE_ROW) {
6728 label_record = (label_s*) malloc(sizeof(label_s));
6730 if (label_record == NULL) {
6731 ACCOUNT_FATAL("malloc Failed");
6735 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
6737 _account_type_convert_column_to_label(hstmt,label_record);
6739 _ACCOUNT_FREE(*label);
6740 //Making label newly created
6741 *label = _account_get_text(label_record->label);
6743 _account_type_free_label_with_items(label_record);
6745 rc = _account_query_step(hstmt);
6748 rc = _account_query_finalize(hstmt);
6749 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
6752 error_code = ACCOUNT_ERROR_NONE;
6755 if (hstmt != NULL) {
6756 rc = _account_query_finalize(hstmt);
6757 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
6761 if (error_code == ACCOUNT_ERROR_RECORD_NOT_FOUND) {
6762 error_code = _account_type_query_label_by_locale_from_global_db(app_id, locale, label);
6765 _INFO("_account_type_query_label_by_locale() end : error_code = %d", error_code);
6769 static int _account_insert_custom(account_s *account, int account_id)
6771 _INFO("_account_insert_custom start");
6774 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
6775 account_stmt hstmt = NULL;
6777 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
6779 if (g_slist_length( account->custom_list)==0) {
6780 ACCOUNT_DEBUG( "_account_insert_custom, no custom data\n");
6781 return ACCOUNT_ERROR_NONE;
6784 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
6786 rc = _account_get_record_count(query);
6788 if( _account_db_err_code() == SQLITE_PERM ){
6789 ACCOUNT_ERROR( "Access failed(%d, %s)", _account_db_err_msg());
6790 return ACCOUNT_ERROR_PERMISSION_DENIED;
6794 ACCOUNT_SLOGE( "_account_insert_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
6795 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
6802 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
6805 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
6806 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (AccountId, AppId, Key, Value) VALUES "
6807 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
6809 hstmt = _account_prepare_query(query);
6811 if( _account_db_err_code() == SQLITE_PERM ){
6812 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
6813 return ACCOUNT_ERROR_PERMISSION_DENIED;
6816 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
6818 account_custom_s* custom_data = NULL;
6819 custom_data = (account_custom_s*)iter->data;
6821 ret = _account_query_bind_int(hstmt, count++, account_id);
6822 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
6823 ret = _account_query_bind_text(hstmt, count++, account->package_name);
6824 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
6825 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
6826 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
6827 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
6828 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
6830 rc = _account_query_step(hstmt);
6832 if (rc != SQLITE_DONE) {
6833 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
6837 rc = _account_query_finalize(hstmt);
6838 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
6843 _INFO("_account_insert_custom end");
6844 return ACCOUNT_ERROR_NONE;
6847 static int _account_update_custom(account_s *account, int account_id)
6850 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
6851 account_stmt hstmt = NULL;
6853 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
6855 if (g_slist_length( account->custom_list)==0) {
6856 ACCOUNT_DEBUG( "_account_update_custom, no custom data\n");
6857 return ACCOUNT_ERROR_NONE;
6860 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
6862 rc = _account_get_record_count(query);
6864 if( _account_db_err_code() == SQLITE_PERM ){
6865 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
6866 pthread_mutex_unlock(&account_mutex);
6867 return ACCOUNT_ERROR_PERMISSION_DENIED;
6868 } else if( _account_db_err_code() == SQLITE_BUSY ){
6869 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
6870 pthread_mutex_unlock(&account_mutex);
6871 return ACCOUNT_ERROR_DATABASE_BUSY;
6875 ACCOUNT_SLOGE( "_account_update_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
6876 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
6879 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
6881 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId=? ", ACCOUNT_CUSTOM_TABLE);
6882 hstmt = _account_prepare_query(query);
6884 _account_query_bind_int(hstmt, count++, (int)account_id);
6885 rc = _account_query_step(hstmt);
6887 if (rc == SQLITE_BUSY) {
6888 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
6889 return ACCOUNT_ERROR_DATABASE_BUSY;
6890 } else if (rc != SQLITE_DONE) {
6891 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
6892 return ACCOUNT_ERROR_DB_FAILED;
6895 rc = _account_query_finalize(hstmt);
6896 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
6901 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
6904 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
6905 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AccountId, AppId, Key, Value) VALUES "
6906 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
6908 hstmt = _account_prepare_query(query);
6910 if( _account_db_err_code() == SQLITE_PERM ){
6911 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
6912 return ACCOUNT_ERROR_PERMISSION_DENIED;
6915 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
6917 account_custom_s* custom_data = NULL;
6918 custom_data = (account_custom_s*)iter->data;
6920 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
6921 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
6922 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
6923 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
6924 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
6925 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
6926 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
6927 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
6929 rc = _account_query_step(hstmt);
6931 if (rc != SQLITE_DONE) {
6932 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
6936 rc = _account_query_finalize(hstmt);
6937 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
6942 return ACCOUNT_ERROR_NONE;
6945 static int _account_query_custom_by_account_id(account_custom_cb callback, int account_id, void *user_data )
6947 int error_code = ACCOUNT_ERROR_NONE;
6948 account_stmt hstmt = NULL;
6949 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
6952 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
6953 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
6954 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
6956 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
6958 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
6959 hstmt = _account_prepare_query(query);
6961 if( _account_db_err_code() == SQLITE_PERM ){
6962 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
6963 return ACCOUNT_ERROR_PERMISSION_DENIED;
6966 rc = _account_query_step(hstmt);
6968 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
6970 account_custom_s* custom_record = NULL;
6972 while (rc == SQLITE_ROW) {
6973 bool cb_ret = FALSE;
6974 custom_record = (account_custom_s*) malloc(sizeof(account_custom_s));
6976 if (custom_record == NULL) {
6977 ACCOUNT_FATAL("malloc Failed");
6981 ACCOUNT_MEMSET(custom_record, 0x00, sizeof(account_custom_s));
6983 _account_convert_column_to_custom(hstmt, custom_record);
6985 cb_ret = callback(custom_record->key, custom_record->value, user_data);
6987 _account_free_custom_with_items(custom_record);
6989 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
6991 rc = _account_query_step(hstmt);
6994 rc = _account_query_finalize(hstmt);
6995 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
6998 error_code = ACCOUNT_ERROR_NONE;
7001 if (hstmt != NULL) {
7002 rc = _account_query_finalize(hstmt);
7003 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
7007 pthread_mutex_unlock(&account_mutex);