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.
28 #include <pkgmgr-info.h>
31 #include <tzplatform_config.h>
34 #include <account_ipc_marshal.h>
35 #include <account_free.h>
36 #include <account-private.h>
38 #include <account-error.h>
39 #include "account-server-db.h"
41 typedef sqlite3_stmt* account_stmt;
43 #define TEST_APP_ID "org.tizen.MyAccountCoreTest"
44 #define EAS_CMDLINE "/usr/bin/eas-engine"
45 #define EMAIL_SERVICE_CMDLINE "/usr/bin/email-service"
46 #define IMS_ENGINE_CMDLINE "/usr/bin/ims-srv"
47 #define IMS_AGENT_CMDLINE "/usr/bin/ims-agent"
48 #define MDM_SERVER_CMDLINE "/usr/bin/mdm-server"
50 #define RCS_APPID "com.samsung.rcs-im"
51 #define IMS_SERVICE_APPID "ims-service"
52 #define ACTIVESYNC_APPID "eas-ui"
53 #define EMAIL_APPID "email-setting-efl"
54 #define SYNCHRONISE_APPID "setting-synchronise-efl"
55 #define DS_AGENT_CMDLINE "/usr/bin/oma-ds-agent"
57 #define FACEBOOK_SDK_APPID "com.samsung.facebook-service"
58 #define FACEBOOK_APPID "com.samsung.facebook"
60 #define EASYSIGNUP_CMDLINE "/usr/bin/esu-agent"
61 #define EASYSIGNUP_APPID "com.samsung.esu-agent"
63 #define ACCOUNT_DB_OPEN_READONLY 0
64 #define ACCOUNT_DB_OPEN_READWRITE 1
68 #define _TIZEN_PUBLIC_
69 #ifndef _TIZEN_PUBLIC_
70 //#include <csc-feature.h>
74 static sqlite3* g_hAccountDB = NULL;
75 static sqlite3* g_hAccountDB2 = NULL;
76 pthread_mutex_t account_mutex = PTHREAD_MUTEX_INITIALIZER;
78 static char *_account_get_text(const char *text_data);
79 static const char *_account_query_table_column_text(account_stmt pStmt, int pos);
80 static int _account_insert_custom(account_s *account, int account_id);
81 static int _account_update_custom(account_s *account, int account_id);
82 static int _account_query_custom_by_account_id(account_custom_cb callback, int account_id, void *user_data );
83 static int _account_type_update_provider_feature(account_type_s *account_type, const char* app_id);
85 int _account_query_capability_by_account_id(capability_cb callback, int account_id, void *user_data );
87 static void _account_insert_delete_update_notification_send(char *noti_name)
90 _ERR("Noti Name is NULL!!!!!!\n");
94 _INFO("noti_type = %s", noti_name);
96 if (vconf_set_str(VCONFKEY_ACCOUNT_MSG_STR, noti_name) != 0) {
97 _ERR("Vconf MSG Str set FAILED !!!!!!\n");;
101 int _account_get_current_appid_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
105 GSList** appid_list = (GSList**)user_data;
108 pkgmgr_ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
110 if( pkgmgr_ret != PMINFO_R_OK ){
111 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_appid(%d)", pkgmgr_ret);
114 item = _account_get_text(appid);
115 *appid_list = g_slist_append(*appid_list, item);
120 static inline int __read_proc(const char *path, char *buf, int size)
124 if (buf == NULL || path == NULL) {
125 ACCOUNT_ERROR("path and buffer is mandatory\n");
129 fd = open(path, O_RDONLY);
131 ACCOUNT_ERROR("fd open error(%d)\n", fd);
135 ret = read(fd, buf, size - 1);
137 ACCOUNT_ERROR("fd read error(%d)\n", fd);
148 char *_account_get_proc_cmdline_bypid(int pid)
153 ACCOUNT_SNPRINTF(buf, sizeof(buf), "/proc/%d/cmdline", pid);
154 ret = __read_proc(buf, buf, sizeof(buf));
156 ACCOUNT_DEBUG("No proc directory (%d)\n", pid);
164 static char* _account_get_current_appid(int pid)
166 _INFO("getting caller appid with pid=[%d]", pid);
169 char appid[128]={0,};
170 char* appid_ret = NULL;
172 ret = aul_app_get_appid_bypid(pid, appid, sizeof(appid));
175 ACCOUNT_ERROR("fail to get current appid ret=[%d], appid=%s\n", ret, appid);
180 /* SLP platform core exception */
181 if(strlen(appid) == 0){
183 char* cmdline = NULL;
184 cmdline = _account_get_proc_cmdline_bypid(pid);
185 ACCOUNT_SLOGD("cmdline (%s)!!!!!!\n", cmdline);
186 if(!g_strcmp0(cmdline, EAS_CMDLINE)) {
187 appid_ret = _account_get_text(ACTIVESYNC_APPID);
188 _ACCOUNT_FREE(cmdline);
190 } else if (!g_strcmp0(cmdline, EMAIL_SERVICE_CMDLINE) || !g_strcmp0(cmdline, MDM_SERVER_CMDLINE)) {
191 appid_ret = _account_get_text(EMAIL_APPID);
192 _ACCOUNT_FREE(cmdline);
194 } else if (!g_strcmp0(cmdline, IMS_ENGINE_CMDLINE) || !g_strcmp0(cmdline, IMS_AGENT_CMDLINE)) {
195 if(_account_type_query_app_id_exist(RCS_APPID)==ACCOUNT_ERROR_NONE){
196 appid_ret = _account_get_text(RCS_APPID);
197 } else if(_account_type_query_app_id_exist(IMS_SERVICE_APPID)==ACCOUNT_ERROR_NONE){
198 appid_ret = _account_get_text(IMS_SERVICE_APPID);
200 appid_ret = _account_get_text(RCS_APPID);
202 _ACCOUNT_FREE(cmdline);
204 } else if (!g_strcmp0(cmdline, DS_AGENT_CMDLINE)) {
205 appid_ret = _account_get_text(SYNCHRONISE_APPID);
206 _ACCOUNT_FREE(cmdline);
208 } else if (!g_strcmp0(cmdline, EASYSIGNUP_CMDLINE)) {
209 appid_ret = _account_get_text(EASYSIGNUP_APPID);
210 _ACCOUNT_FREE(cmdline);
213 ACCOUNT_DEBUG("No app id\n");
214 _ACCOUNT_FREE(cmdline);
220 /* temporary exception */
221 if(!g_strcmp0(appid, "com.samsung.gallery")){
222 appid_ret = _account_get_text("com.samsung.facebook");
223 } else if(!g_strcmp0(appid, FACEBOOK_SDK_APPID)){
224 appid_ret = _account_get_text(FACEBOOK_APPID);
226 appid_ret = _account_get_text(appid);
232 static int _account_check_account_type_with_appid_group(int uid, const char* appid, char** verified_appid)
234 int error_code = ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
235 pkgmgrinfo_appinfo_h ahandle=NULL;
236 pkgmgrinfo_pkginfo_h phandle=NULL;
237 char* package_id = NULL;
238 GSList* appid_list = NULL;
242 ACCOUNT_ERROR("input param is null\n");
243 return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
247 ACCOUNT_ERROR("output param is null\n");
248 return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
251 if(!strcmp(appid, "com.samsung.setting")){
252 ACCOUNT_DEBUG("Setting exception\n");
253 *verified_appid = _account_get_text("com.samsung.setting");
254 return ACCOUNT_ERROR_NONE;
257 if(!strcmp(appid, "com.samsung.samsung-account-front")){
258 ACCOUNT_DEBUG("Setting exception\n");
259 *verified_appid = _account_get_text("com.samsung.samsung-account-front");
260 return ACCOUNT_ERROR_NONE;
263 if(!strcmp(appid, IMS_SERVICE_APPID) || !strcmp(appid, RCS_APPID)){
264 ACCOUNT_DEBUG("ims service exception\n");
265 *verified_appid = _account_get_text(appid);
266 return ACCOUNT_ERROR_NONE;
269 if(!strcmp(appid, EASYSIGNUP_APPID)){
270 ACCOUNT_DEBUG("easysignup exception\n");
271 *verified_appid = _account_get_text(appid);
272 return ACCOUNT_ERROR_NONE;
274 /* Get app id family which is stored in account database */
277 if (uid == OWNER_ROOT || uid == GLOBAL_USER) {
278 pkgmgr_ret = pkgmgrinfo_appinfo_get_appinfo(appid, &ahandle);
280 pkgmgr_ret = pkgmgrinfo_appinfo_get_usr_appinfo(appid, uid, &ahandle);
282 if( pkgmgr_ret != PMINFO_R_OK ){
283 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_appinfo(%d)", pkgmgr_ret);
286 pkgmgr_ret = pkgmgrinfo_appinfo_get_pkgid(ahandle, &package_id);
287 if( pkgmgr_ret != PMINFO_R_OK ){
288 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_pkgid(%d)", pkgmgr_ret);
291 if (uid == OWNER_ROOT || uid == GLOBAL_USER) {
292 pkgmgr_ret = pkgmgrinfo_pkginfo_get_pkginfo(package_id, &phandle);
294 pkgmgr_ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(package_id, uid, &phandle);
296 if( pkgmgr_ret != PMINFO_R_OK ){
297 ACCOUNT_DEBUG("pkgmgrinfo_pkginfo_get_pkginfo(%d)", pkgmgr_ret);
300 if (uid == OWNER_ROOT || uid == GLOBAL_USER) {
301 pkgmgr_ret = pkgmgrinfo_appinfo_get_list(phandle, PMINFO_ALL_APP, _account_get_current_appid_cb, (void *)&appid_list);
303 pkgmgr_ret = pkgmgrinfo_appinfo_get_usr_list(phandle, PMINFO_ALL_APP, _account_get_current_appid_cb, (void *)&appid_list, uid);
305 if( pkgmgr_ret != PMINFO_R_OK ){
306 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_list(%d)", pkgmgr_ret);
309 /* Compare current app id with the stored app id family */
310 for(iter=appid_list;iter!=NULL;iter=g_slist_next(iter)){
311 char* tmp = (char*)iter->data;
313 if(_account_type_query_app_id_exist(tmp) == ACCOUNT_ERROR_NONE) {
314 *verified_appid = _account_get_text(tmp);
315 error_code = ACCOUNT_ERROR_NONE;
319 ACCOUNT_SLOGD("not matched owner group app id(%s), current appid(%s)\n", tmp, appid);
325 g_slist_free(appid_list);
326 pkgmgr_ret = pkgmgrinfo_pkginfo_destroy_pkginfo(phandle);
327 if( pkgmgr_ret != PMINFO_R_OK ){
328 ACCOUNT_DEBUG("pkgmgrinfo_pkginfo_destroy_pkginfo(%d)", pkgmgr_ret);
331 pkgmgr_ret = pkgmgrinfo_appinfo_destroy_appinfo(ahandle);
332 if( pkgmgr_ret != PMINFO_R_OK ){
333 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_destroy_appinfo(%d)", pkgmgr_ret);
339 static int _account_check_appid_group_with_package_name(int uid, const char* appid, char* package_name)
341 int error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
342 pkgmgrinfo_appinfo_h ahandle=NULL;
343 pkgmgrinfo_pkginfo_h phandle=NULL;
344 char* package_id = NULL;
345 GSList* appid_list = NULL;
349 ACCOUNT_ERROR("input param -appid is null\n");
350 return ACCOUNT_ERROR_INVALID_PARAMETER;
354 ACCOUNT_ERROR("input param - package name is null\n");
355 return ACCOUNT_ERROR_INVALID_PARAMETER;
358 /* ims-service Exception */
359 if ( strcmp(appid, IMS_SERVICE_APPID) == 0 && strcmp(package_name, IMS_SERVICE_APPID) == 0 ) {
360 ACCOUNT_DEBUG("ims exception."); // TODO: NEED TO REMOVE, debug log.
361 return ACCOUNT_ERROR_NONE;
364 /* easysignup Exception */
365 if ( strcmp(appid, EASYSIGNUP_APPID) == 0 && strcmp(package_name, EASYSIGNUP_APPID) == 0 ) {
366 ACCOUNT_DEBUG("easysignup exception."); // TODO: NEED TO REMOVE, debug log.
367 return ACCOUNT_ERROR_NONE;
369 /* Get app id family which is stored in account database */
371 if (uid == OWNER_ROOT || uid == GLOBAL_USER) {
372 pkgmgr_ret = pkgmgrinfo_appinfo_get_appinfo(appid, &ahandle);
374 pkgmgr_ret = pkgmgrinfo_appinfo_get_usr_appinfo(appid, uid, &ahandle);
376 if( pkgmgr_ret != PMINFO_R_OK ){
377 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_appinfo(%d)", pkgmgr_ret);
380 pkgmgr_ret = pkgmgrinfo_appinfo_get_pkgid(ahandle, &package_id);
381 if( pkgmgr_ret != PMINFO_R_OK ){
382 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_pkgid(%d)", pkgmgr_ret);
385 if (uid == OWNER_ROOT || uid == GLOBAL_USER) {
386 pkgmgr_ret = pkgmgrinfo_pkginfo_get_pkginfo(package_id, &phandle);
388 pkgmgr_ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(package_id, uid, &phandle);
390 if( pkgmgr_ret != PMINFO_R_OK ){
391 ACCOUNT_DEBUG("pkgmgrinfo_pkginfo_get_pkginfo(%d)", pkgmgr_ret);
394 if (uid == OWNER_ROOT || uid == GLOBAL_USER) {
395 pkgmgr_ret = pkgmgrinfo_appinfo_get_list(phandle, PMINFO_ALL_APP, _account_get_current_appid_cb, (void *)&appid_list);
397 pkgmgr_ret = pkgmgrinfo_appinfo_get_usr_list(phandle, PMINFO_ALL_APP, _account_get_current_appid_cb, (void *)&appid_list, uid);
399 if( pkgmgr_ret != PMINFO_R_OK ){
400 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_list(%d)", pkgmgr_ret);
403 /* Compare current app id with the stored app id family */
404 for(iter=appid_list;iter!=NULL;iter=g_slist_next(iter)){
405 char* tmp = (char*)iter->data;
407 //ACCOUNT_ERROR("tmp(%s)package_name(%s)\n\n", tmp, package_name); // TODO: NEED TO REMOVE, debug log.
408 if( strcmp(tmp, package_name) == 0) {
409 error_code = ACCOUNT_ERROR_NONE;
412 } else if ( strcmp(tmp, "com.samsung.samsung-account-front") == 0 &&
413 strcmp(package_name, "com.samsung.samsungaccount") == 0 ) {
414 /* Samung Account Exception */
415 error_code = ACCOUNT_ERROR_NONE;
419 ACCOUNT_SLOGD("not matched owner group app id(%s), current appid(%s)\n", tmp, appid);
425 g_slist_free(appid_list);
426 pkgmgr_ret = pkgmgrinfo_pkginfo_destroy_pkginfo(phandle);
427 if( pkgmgr_ret != PMINFO_R_OK ){
428 ACCOUNT_DEBUG("pkgmgrinfo_pkginfo_destroy_pkginfo(%d)", pkgmgr_ret);
431 pkgmgr_ret = pkgmgrinfo_appinfo_destroy_appinfo(ahandle);
432 if( pkgmgr_ret != PMINFO_R_OK ){
433 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_destroy_appinfo(%d)", pkgmgr_ret);
439 static int _remove_sensitive_info_from_non_owning_account(int caller_pid, account_s *account)
444 return ACCOUNT_ERROR_INVALID_PARAMETER;
447 if (account->package_name)
449 char *caller_package_name = _account_get_current_appid(caller_pid);
450 if (caller_package_name == NULL)
452 _ERR("Could not get caller app id, so removing sensitive info from account id [%d]", account->id);
453 return ACCOUNT_ERROR_INVALID_PARAMETER;
456 if (g_strcmp0(caller_package_name, account->package_name) != 0)
458 // packages dont match, so remove sensitive info
459 _INFO("Removing sensitive info from account id [%d]", account->id);
460 free (account->access_token);
461 account->access_token = NULL;
464 _ACCOUNT_FREE(caller_package_name);
465 return ACCOUNT_ERROR_NONE;
467 return ACCOUNT_ERROR_INVALID_PARAMETER;
470 static int _remove_sensitive_info_from_non_owning_account_list(int caller_pid, GList *account_list)
472 int return_code = ACCOUNT_ERROR_NONE;
474 if (account_list == NULL)
477 return ACCOUNT_ERROR_INVALID_PARAMETER;
480 GList *list_iter = NULL;
481 for (list_iter = account_list; list_iter != NULL; list_iter = g_list_next(list_iter))
483 account_s *account = (account_s *) list_iter->data;
484 int ret = _remove_sensitive_info_from_non_owning_account(caller_pid, account);
485 if( ret != ACCOUNT_ERROR_NONE)
491 static int _remove_sensitive_info_from_non_owning_account_slist(int caller_pid, GSList *account_list)
493 int return_code = ACCOUNT_ERROR_NONE;
495 if (account_list == NULL)
498 return ACCOUNT_ERROR_INVALID_PARAMETER;
501 GSList *list_iter = NULL;
502 for (list_iter = account_list; list_iter != NULL; list_iter = g_slist_next(list_iter))
504 account_s *account = (account_s *) list_iter->data;
505 int ret = _remove_sensitive_info_from_non_owning_account(caller_pid, account);
506 if( ret != ACCOUNT_ERROR_NONE)
512 static const char *_account_db_err_msg()
514 return sqlite3_errmsg(g_hAccountDB);
517 static int _account_db_err_code()
519 return sqlite3_errcode(g_hAccountDB);
522 static int _account_get_record_count(char* query)
524 _INFO("_account_get_record_count");
528 account_stmt pStmt = NULL;
531 _ERR("NULL query\n");
532 return ACCOUNT_ERROR_QUERY_SYNTAX_ERROR;
536 _ERR("DB is not opened\n");
537 return ACCOUNT_ERROR_DB_NOT_OPENED;
540 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
542 if (SQLITE_BUSY == rc){
543 _ERR("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg());
544 sqlite3_finalize(pStmt);
545 return ACCOUNT_ERROR_DATABASE_BUSY;
546 } else if (SQLITE_OK != rc) {
547 _ERR("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg());
548 sqlite3_finalize(pStmt);
549 return ACCOUNT_ERROR_DB_FAILED;
552 rc = sqlite3_step(pStmt);
553 if (SQLITE_BUSY == rc) {
554 _ERR("sqlite3_step() failed(%d, %s).", rc, _account_db_err_msg());
555 sqlite3_finalize(pStmt);
556 return ACCOUNT_ERROR_DATABASE_BUSY;
557 } else if (SQLITE_ROW != rc) {
558 _ERR("sqlite3_step() failed(%d, %s).", rc, _account_db_err_msg());
559 sqlite3_finalize(pStmt);
560 return ACCOUNT_ERROR_DB_FAILED;
563 ncount = sqlite3_column_int(pStmt, 0);
565 _INFO("account record count [%d]", ncount);
566 sqlite3_finalize(pStmt);
571 static int _account_execute_query(const char *query)
574 char* pszErrorMsg = NULL;
577 ACCOUNT_ERROR("NULL query\n");
578 return ACCOUNT_ERROR_QUERY_SYNTAX_ERROR;
582 ACCOUNT_ERROR("DB is not opened\n");
583 return ACCOUNT_ERROR_DB_NOT_OPENED;
586 rc = sqlite3_exec(g_hAccountDB, query, NULL, NULL, &pszErrorMsg);
587 if (SQLITE_OK != rc) {
588 ACCOUNT_ERROR("sqlite3_exec rc(%d) query(%s) failed(%s).", rc, query, pszErrorMsg);
589 sqlite3_free(pszErrorMsg);
595 static int _account_begin_transaction(void)
597 ACCOUNT_DEBUG("_account_begin_transaction start");
600 ret = _account_execute_query("BEGIN IMMEDIATE TRANSACTION");
602 if (ret == SQLITE_BUSY){
603 ACCOUNT_ERROR(" sqlite3 busy = %d", ret);
604 return ACCOUNT_ERROR_DATABASE_BUSY;
605 } else if(ret != SQLITE_OK) {
606 ACCOUNT_ERROR("_account_svc_begin_transaction fail :: %d", ret);
607 return ACCOUNT_ERROR_DB_FAILED;
610 ACCOUNT_DEBUG("_account_begin_transaction end");
611 return ACCOUNT_ERROR_NONE;
614 static int _account_end_transaction(bool is_success)
616 ACCOUNT_DEBUG("_account_end_transaction start");
620 if (is_success == true) {
621 ret = _account_execute_query("COMMIT TRANSACTION");
622 ACCOUNT_DEBUG("_account_end_transaction COMMIT");
624 ret = _account_execute_query("ROLLBACK TRANSACTION");
625 ACCOUNT_DEBUG("_account_end_transaction ROLLBACK");
628 if(ret == SQLITE_PERM){
629 ACCOUNT_ERROR("Account permission denied :: %d", ret);
630 return ACCOUNT_ERROR_PERMISSION_DENIED;
633 if (ret == SQLITE_BUSY){
634 ACCOUNT_DEBUG(" sqlite3 busy = %d", ret);
635 return ACCOUNT_ERROR_DATABASE_BUSY;
638 if (ret != SQLITE_OK) {
639 ACCOUNT_ERROR("_account_svc_end_transaction fail :: %d", ret);
640 return ACCOUNT_ERROR_DB_FAILED;
643 ACCOUNT_DEBUG("_account_end_transaction end");
644 return ACCOUNT_ERROR_NONE;
647 static bool _account_check_add_more_account(const char* app_id)
649 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
652 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
653 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
655 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
657 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s' and MultipleAccountSupport = 1", ACCOUNT_TYPE_TABLE, app_id);
658 rc = _account_get_record_count(query);
660 /* multiple account support case */
662 ACCOUNT_SLOGD("app id (%s) supports multiple account. rc(%d)\n", app_id, rc);
666 /* multiple account not support case */
667 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
668 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE package_name = '%s'", ACCOUNT_TABLE, app_id);
669 rc = _account_get_record_count(query);
672 ACCOUNT_SLOGD("app id (%s) supports single account. and there is no account of the app id\n", app_id);
679 //TODO: Need to enable creating db on the first connect for
680 //a) multi-user cases
681 //b) to ensure db exist in every connect call
683 //static int _account_create_all_tables(void)
686 // int error_code = ACCOUNT_ERROR_NONE;
687 // char query[ACCOUNT_SQL_LEN_MAX] = {0, };
689 // ACCOUNT_DEBUG("create all table - BEGIN");
690 // ACCOUNT_MEMSET(query, 0, sizeof(query));
692 // /*Create the account table*/
693 // ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_TABLE);
694 // rc = _account_get_record_count(query);
696 // rc = _account_execute_query(ACCOUNT_SCHEMA);
697 // if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY;
698 // 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()));
700 //#ifndef _TIZEN_PUBLIC_
701 // if (CSC_FEATURE_BOOL_TRUE == csc_feature_get_bool(CSC_FEATURE_DEF_BOOL_CONTACTS_DOCOMO_SOCIAL_PHONEBOOK)) {
702 // /* NTT docomo specific area */
703 // rc = _account_execute_query(DOCOMO_DEFAULT_VAL_INSERT_QUERY);
704 // if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY;
705 // ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", DOCOMO_DEFAULT_VAL_INSERT_QUERY, rc, _account_db_err_msg()));
706 // /* END of NTT docomo specific area */
711 // /*Create capability table*/
712 // ACCOUNT_MEMSET(query, 0, sizeof(query));
713 // ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", CAPABILITY_TABLE);
714 // rc = _account_get_record_count(query);
716 // rc = _account_execute_query(CAPABILITY_SCHEMA);
717 // if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY;
718 // 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()));
721 // /* Create account custom table */
722 // ACCOUNT_MEMSET(query, 0, sizeof(query));
723 // ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_CUSTOM_TABLE);
724 // rc = _account_get_record_count(query);
726 // rc = _account_execute_query(ACCOUNT_CUSTOM_SCHEMA);
727 // if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY;
728 // ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
731 // /* Create account type table */
732 // ACCOUNT_MEMSET(query, 0, sizeof(query));
733 // ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_TYPE_TABLE);
734 // rc = _account_get_record_count(query);
736 // rc = _account_execute_query(ACCOUNT_TYPE_SCHEMA);
737 // if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY;
738 // 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()));
741 // /* Create label table */
742 // ACCOUNT_MEMSET(query, 0, sizeof(query));
743 // ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", LABEL_TABLE);
744 // rc = _account_get_record_count(query);
746 // rc = _account_execute_query(LABEL_SCHEMA);
747 // if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY;
748 // 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()));
751 // /* Create account feature table */
752 // ACCOUNT_MEMSET(query, 0, sizeof(query));
753 // ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", PROVIDER_FEATURE_TABLE);
754 // rc = _account_get_record_count(query);
756 // rc = _account_execute_query(PROVIDER_FEATURE_SCHEMA);
757 // if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY;
758 // 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()));
761 // ACCOUNT_DEBUG("create all table - END");
762 // return error_code;
765 //static int _account_check_is_all_table_exists()
768 // char query[ACCOUNT_SQL_LEN_MAX] = {0,};
769 // ACCOUNT_MEMSET(query, 0, sizeof(query));
771 // ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s', '%s', '%s', '%s', '%s', '%s')",
772 // ACCOUNT_TABLE, CAPABILITY_TABLE, ACCOUNT_CUSTOM_TABLE, ACCOUNT_TYPE_TABLE, LABEL_TABLE, PROVIDER_FEATURE_TABLE);
773 // rc = _account_get_record_count(query);
775 // if (rc != ACCOUNT_TABLE_TOTAL_COUNT) {
776 // ACCOUNT_ERROR("Table count is not matched rc=%d\n", rc);
782 int _account_db_handle_close(sqlite3* hDB)
785 int ret = ACCOUNT_ERROR_NONE;
788 rc = db_util_close(hDB);
789 if( rc == SQLITE_OK )
790 ret = ACCOUNT_ERROR_NONE;
791 else if( rc == SQLITE_PERM )
792 ret = ACCOUNT_ERROR_PERMISSION_DENIED;
793 else if ( rc == SQLITE_BUSY )
794 ret = ACCOUNT_ERROR_DATABASE_BUSY;
796 ret = ACCOUNT_ERROR_DB_FAILED;
801 int _account_db_open(int mode, int pid)
805 char account_db_path[256] = {0, };
807 _INFO( "start _account_db_open()");
809 ACCOUNT_MEMSET(account_db_path, 0x00, sizeof(account_db_path));
810 ACCOUNT_SNPRINTF(account_db_path, sizeof(account_db_path), "%s", ACCOUNT_DB_PATH);
813 _ERR( "Account database is using in another app. %x", g_hAccountDB );
814 return ACCOUNT_ERROR_DATABASE_BUSY;
817 ret = _account_db_handle_close(g_hAccountDB2);
818 if( ret != ACCOUNT_ERROR_NONE )
819 ACCOUNT_DEBUG( "db_util_close(g_hAccountDB2) fail ret = %d", ret);
821 ACCOUNT_DEBUG( "before db_util_open()");
822 if(mode == ACCOUNT_DB_OPEN_READWRITE)
823 rc = db_util_open(account_db_path, &g_hAccountDB, DB_UTIL_REGISTER_HOOK_METHOD);
824 else if(mode == ACCOUNT_DB_OPEN_READONLY)
825 rc = db_util_open_with_options(account_db_path, &g_hAccountDB, SQLITE_OPEN_READONLY, NULL);
827 return ACCOUNT_ERROR_DB_NOT_OPENED;
828 ACCOUNT_DEBUG( "after db_util_open() sqlite_rc = %d", rc);
830 if( rc == SQLITE_PERM || _account_db_err_code() == SQLITE_PERM ) {
831 ACCOUNT_ERROR( "Account permission denied");
832 return ACCOUNT_ERROR_PERMISSION_DENIED;
835 if( rc == SQLITE_BUSY ) {
836 ACCOUNT_ERROR( "busy handler fail.");
837 return ACCOUNT_ERROR_DATABASE_BUSY;
840 if( rc != SQLITE_OK ) {
841 ACCOUNT_ERROR( "The database isn't connected." );
842 return ACCOUNT_ERROR_DB_NOT_OPENED;
845 _INFO( "end _account_db_open()");
846 return ACCOUNT_ERROR_NONE;
849 int _account_db_close(void)
851 ACCOUNT_DEBUG( "start db_util_close()");
854 ret = _account_db_handle_close(g_hAccountDB2);
855 if( ret != ACCOUNT_ERROR_NONE )
856 ACCOUNT_DEBUG( "db_util_close(g_hAccountDB2) fail ret = %d", ret);
858 ret = _account_db_handle_close(g_hAccountDB);
859 if( ret != ACCOUNT_ERROR_NONE )
861 ACCOUNT_ERROR( "db_util_close(g_hAccountDB) fail ret = %d", ret);
862 g_hAccountDB2 = g_hAccountDB;
869 static int _account_check_duplicated(account_s *data, const char* verified_appid)
871 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
875 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
877 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')"
878 , ACCOUNT_TABLE, verified_appid, data->user_name, data->display_name, data->email_address);
880 count = _account_get_record_count(query);
883 return ACCOUNT_ERROR_NONE;
886 //check whether duplicated account or not.
888 //2. check display_name
889 //3. check email_address
890 GList* account_list_temp = _account_query_account_by_package_name(getpid(), verified_appid, &ret);
891 if (account_list_temp == NULL)
893 _ERR("_account_query_account_by_package_name returned NULL");
894 return ACCOUNT_ERROR_DB_FAILED;
897 if( _account_db_err_code() == SQLITE_PERM ){
898 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
899 return ACCOUNT_ERROR_PERMISSION_DENIED;
902 if(ret != ACCOUNT_ERROR_NONE){
906 account_list_temp = g_list_first(account_list_temp);
907 _INFO("account_list_temp length=[%d]",g_list_length(account_list_temp));
910 for (iter = account_list_temp; iter != NULL; iter = g_list_next(iter))
912 _INFO("iterating account_list_temp");
913 account_s *account = NULL;
914 _INFO("Before iter->data");
915 account = (account_s*)iter->data;
916 _INFO("After iter->data");
919 if(account->user_name!=NULL && data->user_name!=NULL && strcmp(account->user_name, data->user_name)==0)
921 _INFO("duplicated account(s) exist!, same user_name=%s", data->user_name);
922 return ACCOUNT_ERROR_DUPLICATED;
924 //when user_name is not NULL and display_name is same.
925 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)
927 _INFO("duplicated account(s) exist!, same display_name=%s", data->display_name);
928 return ACCOUNT_ERROR_DUPLICATED;
930 //when user_name and display_name are not NULL and email_address is same.
931 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)
933 _INFO("duplicated account(s) exist!, same email_address=%s", data->email_address);
934 return ACCOUNT_ERROR_DUPLICATED;
939 return ACCOUNT_ERROR_NONE;
942 static int _account_get_next_sequence(const char *pszName)
945 account_stmt pStmt = NULL;
947 char szQuery[ACCOUNT_SQL_LEN_MAX] = {0,};
949 ACCOUNT_MEMSET(szQuery, 0x00, sizeof(szQuery));
950 ACCOUNT_SNPRINTF(szQuery, sizeof(szQuery), "SELECT max(seq) FROM %s where name = '%s' ", ACCOUNT_SQLITE_SEQ, pszName);
951 rc = sqlite3_prepare_v2(g_hAccountDB, szQuery, strlen(szQuery), &pStmt, NULL);
952 if (SQLITE_OK != rc) {
953 ACCOUNT_SLOGE("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg());
954 sqlite3_finalize(pStmt);
955 return ACCOUNT_ERROR_DB_FAILED;
958 rc = sqlite3_step(pStmt);
959 max_seq = sqlite3_column_int(pStmt, 0);
962 /*Finalize Statement*/
963 rc = sqlite3_finalize(pStmt);
969 static account_stmt _account_prepare_query(char *query)
972 account_stmt pStmt = NULL;
974 ACCOUNT_RETURN_VAL((query != NULL), {}, NULL, ("query is NULL"));
976 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
978 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, NULL, ("sqlite3_prepare_v2(%s) failed(%s).", query, _account_db_err_msg()));
983 static int _account_query_bind_int(account_stmt pStmt, int pos, int num)
986 ACCOUNT_ERROR("statement is null");
991 ACCOUNT_ERROR("invalid pos");
995 return sqlite3_bind_int(pStmt, pos, num);
998 static int _account_query_bind_text(account_stmt pStmt, int pos, const char *str)
1000 _INFO("_account_query_bind_text");
1004 _ERR("statement is null");
1010 _INFO("sqlite3_bind_text");
1011 return sqlite3_bind_text(pStmt, pos, (const char*)str, strlen(str), SQLITE_STATIC);
1015 _INFO("sqlite3_bind_null");
1016 return sqlite3_bind_null(pStmt, pos);
1020 static int _account_convert_account_to_sql(account_s *account, account_stmt hstmt, char *sql_value)
1026 /*Caution : Keep insert query orders.*/
1029 _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1030 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], user_name=%s", account->id, account->user_name);
1032 /* 2. email address*/
1033 _account_query_bind_text(hstmt, count++, (char*)account->email_address);
1034 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], email_address=%s", account->id, account->email_address);
1036 /* 3. display name*/
1037 _account_query_bind_text(hstmt, count++, (char*)account->display_name);
1038 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], display_name=%s", account->id, account->display_name);
1041 _account_query_bind_text(hstmt, count++, (char*)account->icon_path);
1042 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], icon_path=%s", account->id, account->icon_path);
1045 _account_query_bind_text(hstmt, count++, (char*)account->source);
1046 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], source=%s", account->id, account->source);
1048 /* 6. package name*/
1049 _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1050 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], package_name=%s", account->id, account->package_name);
1052 /* 7. access token*/
1053 _account_query_bind_text(hstmt, count++, (char*)account->access_token);
1054 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], access_token=%s", account->id, account->access_token);
1057 _account_query_bind_text(hstmt, count++, (char*)account->domain_name);
1058 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], domain_name=%s", account->id, account->domain_name);
1061 _account_query_bind_int(hstmt, count++, account->auth_type);
1062 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], auth_type=%d", account->id, account->auth_type);
1065 _account_query_bind_int(hstmt, count++, account->secret);
1066 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], secret=%d", account->id, account->secret);
1068 /* 11. sync_support */
1069 _account_query_bind_int(hstmt, count++, account->sync_support);
1070 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], sync_support=%d", account->id, account->sync_support);
1075 for(i=0; i< USER_TXT_CNT; i++)
1076 _account_query_bind_text(hstmt, count++, (char*)account->user_data_txt[i]);
1078 /* 13. user integer */
1079 for(i=0; i< USER_INT_CNT; i++)
1081 _account_query_bind_int(hstmt, count++, account->user_data_int[i]);
1082 _INFO("convert user_data_int : marshal_user_int data_int[%d]=%d", i, account->user_data_int[i]);
1090 static int _account_query_finalize(account_stmt pStmt)
1095 ACCOUNT_ERROR( "pStmt is NULL");
1096 return ACCOUNT_ERROR_INVALID_PARAMETER;
1099 rc = sqlite3_finalize(pStmt);
1100 if (rc == SQLITE_BUSY){
1101 ACCOUNT_ERROR(" sqlite3 busy = %d", rc);
1102 return ACCOUNT_ERROR_DATABASE_BUSY;
1103 } else if (rc != SQLITE_OK) {
1104 ACCOUNT_ERROR( "sqlite3_finalize fail, rc : %d, db_error : %s\n", rc, _account_db_err_msg());
1105 return ACCOUNT_ERROR_DB_FAILED;
1108 return ACCOUNT_ERROR_NONE;
1111 static int _account_query_step(account_stmt pStmt)
1114 ACCOUNT_ERROR( "pStmt is NULL");
1118 return sqlite3_step(pStmt);
1121 static int _account_execute_insert_query(account_s *account)
1123 _INFO("_account_execute_insert_query start");
1126 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1127 int error_code = ACCOUNT_ERROR_NONE;
1128 account_stmt hstmt = NULL;
1130 /* check whether app id exist in account type db */
1132 if (!account->user_name && !account->display_name && !account->email_address) {
1134 ACCOUNT_ERROR("Mandetory fields is NULL. At least one field is required among username, display name, email address\n");
1135 return ACCOUNT_ERROR_INVALID_PARAMETER;
1139 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1140 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (user_name, email_address , display_name , icon_path , source , package_name , "
1141 "access_token , domain_name , auth_type , secret , sync_support , txt_custom0, txt_custom1, txt_custom2, txt_custom3, txt_custom4, "
1142 "int_custom0, int_custom1, int_custom2, int_custom3, int_custom4, txt_custom0 ) values " // to do urusa
1143 "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", ACCOUNT_TABLE);
1145 hstmt = _account_prepare_query(query);
1146 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
1149 _account_convert_account_to_sql(account, hstmt, query);
1152 rc = _account_query_step(hstmt);
1153 if (rc != SQLITE_DONE) {
1155 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1157 if( _account_db_err_code() == SQLITE_PERM )
1158 error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
1160 error_code = ACCOUNT_ERROR_DB_FAILED;
1164 rc = _account_query_finalize(hstmt);
1165 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1168 _INFO("_account_execute_insert_query end");
1172 static int _account_insert_capability(account_s *account, int account_id)
1174 _INFO("_account_insert_capability start");
1176 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1177 account_stmt hstmt = NULL;
1179 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1181 if (g_slist_length( account->capablity_list)==0) {
1182 ACCOUNT_DEBUG( "_account_insert_capability, no capability\n");
1183 return ACCOUNT_ERROR_NONE;
1186 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
1188 _INFO("_account_insert_capability _account_get_record_count [%s]", query);
1189 rc = _account_get_record_count(query);
1191 if( _account_db_err_code() == SQLITE_PERM ){
1192 _ERR( "Access failed(%s)", _account_db_err_msg());
1193 return ACCOUNT_ERROR_PERMISSION_DENIED;
1196 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1203 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1207 account_capability_s* cap_data = NULL;
1208 cap_data = (account_capability_s*)iter->data;
1210 _INFO("cap_data->type = %s, cap_data->value = %d \n", cap_data->type, cap_data->value);
1212 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1213 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
1214 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
1215 hstmt = _account_prepare_query(query);
1217 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
1219 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
1220 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1221 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
1222 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1223 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1224 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1225 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1226 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1227 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
1228 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1230 rc = _account_query_step(hstmt);
1231 _INFO("_account_insert_capability _account_query_step[%d]", rc);
1233 if (rc != SQLITE_DONE) {
1234 _ERR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1238 rc = _account_query_finalize(hstmt);
1239 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1244 _INFO("_account_insert_capability end");
1245 return ACCOUNT_ERROR_NONE;
1248 static int _account_update_capability(account_s *account, int account_id)
1251 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1252 account_stmt hstmt = NULL;
1254 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1256 if (g_slist_length( account->capablity_list)==0) {
1257 ACCOUNT_ERROR( "_account_update_capability, no capability\n");
1258 return ACCOUNT_ERROR_NONE;
1261 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
1263 rc = _account_get_record_count(query);
1266 ACCOUNT_SLOGI( "_account_update_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
1267 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1270 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1272 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id=? ", CAPABILITY_TABLE);
1273 hstmt = _account_prepare_query(query);
1275 _account_query_bind_int(hstmt, count++, (int)account_id);
1276 rc = _account_query_step(hstmt);
1278 if (rc != SQLITE_DONE) {
1279 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1280 return ACCOUNT_ERROR_DB_FAILED;
1282 rc = _account_query_finalize(hstmt);
1283 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1288 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1291 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1292 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
1293 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
1295 hstmt = _account_prepare_query(query);
1297 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
1299 account_capability_s* cap_data = NULL;
1300 cap_data = (account_capability_s*)iter->data;
1302 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
1303 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1304 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
1305 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1306 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1307 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1308 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1309 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1310 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
1311 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1313 rc = _account_query_step(hstmt);
1315 if (rc != SQLITE_DONE) {
1316 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1320 rc = _account_query_finalize(hstmt);
1321 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1326 return ACCOUNT_ERROR_NONE;
1329 static int _account_update_capability_by_user_name(account_s *account, const char *user_name, const char *package_name )
1332 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1333 account_stmt hstmt = NULL;
1335 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1337 if (g_slist_length( account->capablity_list)==0) {
1338 ACCOUNT_ERROR( "_account_update_capability_by_user_name, no capability\n");
1339 return ACCOUNT_ERROR_NONE;
1342 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where package_name= '%s' and user_name='%s'", ACCOUNT_TABLE, package_name, user_name);
1344 rc = _account_get_record_count(query);
1347 ACCOUNT_SLOGI( "_account_update_capability_by_user_name : related account item is not existed rc=%d , %s ", rc, _account_db_err_msg());
1348 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1351 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1353 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name=? and user_name=? ", CAPABILITY_TABLE);
1354 hstmt = _account_prepare_query(query);
1356 _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1357 _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1358 rc = _account_query_step(hstmt);
1359 if (rc != SQLITE_DONE) {
1360 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1361 return ACCOUNT_ERROR_DB_FAILED;
1364 rc = _account_query_finalize(hstmt);
1365 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1370 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1373 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1374 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
1375 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
1377 hstmt = _account_prepare_query(query);
1379 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
1381 account_capability_s* cap_data = NULL;
1382 cap_data = (account_capability_s*)iter->data;
1384 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
1385 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1386 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
1387 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1388 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1389 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1390 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1391 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1392 ret = _account_query_bind_int(hstmt, count++, (int)account->id);
1393 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1395 rc = _account_query_step(hstmt);
1397 if (rc != SQLITE_DONE) {
1398 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1402 rc = _account_query_finalize(hstmt);
1403 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1408 return ACCOUNT_ERROR_NONE;
1411 static int _account_query_table_column_int(account_stmt pStmt, int pos)
1414 ACCOUNT_ERROR("statement is null");
1419 ACCOUNT_ERROR("invalid pos");
1423 return sqlite3_column_int(pStmt, pos);
1426 static const char *_account_query_table_column_text(account_stmt pStmt, int pos)
1429 ACCOUNT_ERROR("statement is null");
1434 ACCOUNT_ERROR("invalid pos");
1438 return (const char*)sqlite3_column_text(pStmt, pos);
1441 static void _account_db_data_to_text(const char *textbuf, char **output)
1443 if (textbuf && strlen(textbuf)>0) {
1448 *output = strdup(textbuf);
1452 static void _account_convert_column_to_account(account_stmt hstmt, account_s *account_record)
1454 const char *textbuf = NULL;
1456 account_record->id = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_ID);
1457 ACCOUNT_DEBUG("account_record->id =[%d]", account_record->id);
1459 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_NAME);
1460 _account_db_data_to_text(textbuf, &(account_record->user_name));
1462 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_EMAIL_ADDRESS);
1463 _account_db_data_to_text(textbuf, &(account_record->email_address));
1465 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DISPLAY_NAME);
1466 _account_db_data_to_text(textbuf, &(account_record->display_name));
1468 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ICON_PATH);
1469 _account_db_data_to_text(textbuf, &(account_record->icon_path));
1471 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_SOURCE);
1472 _account_db_data_to_text(textbuf, &(account_record->source));
1474 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_PACKAGE_NAME);
1475 _account_db_data_to_text(textbuf, &(account_record->package_name));
1477 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ACCESS_TOKEN);
1478 _account_db_data_to_text(textbuf, &(account_record->access_token));
1480 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DOMAIN_NAME);
1481 _account_db_data_to_text(textbuf, &(account_record->domain_name));
1483 account_record->auth_type = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_AUTH_TYPE);
1485 account_record->secret = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SECRET);
1487 account_record->sync_support = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SYNC_SUPPORT);
1489 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_0);
1490 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[0]));
1492 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_1);
1493 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[1]));
1495 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_2);
1496 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[2]));
1498 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_3);
1499 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[3]));
1501 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_4);
1502 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[4]));
1504 account_record->user_data_int[0] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_0);
1505 account_record->user_data_int[1] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_1);
1506 account_record->user_data_int[2] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_2);
1507 account_record->user_data_int[3] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_3);
1508 account_record->user_data_int[4] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_4);
1511 static void _account_convert_column_to_capability(account_stmt hstmt, account_capability_s *capability_record)
1513 const char *textbuf = NULL;
1515 _INFO("start _account_convert_column_to_capability()");
1516 capability_record->id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ID);
1518 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_KEY);
1519 _account_db_data_to_text(textbuf, &(capability_record->type));
1521 capability_record->value = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_VALUE);
1523 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_PACKAGE_NAME);
1524 _account_db_data_to_text(textbuf, &(capability_record->package_name));
1526 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_USER_NAME);
1527 _account_db_data_to_text(textbuf, &(capability_record->user_name));
1529 capability_record->account_id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ACCOUNT_ID);
1530 _INFO("type = %s, value = %d", capability_record->type, capability_record->value);
1531 _INFO("end _account_convert_column_to_capability()");
1534 static void _account_convert_column_to_custom(account_stmt hstmt, account_custom_s *custom_record)
1536 _INFO("start _account_convert_column_to_custom()");
1537 const char *textbuf = NULL;
1539 custom_record->account_id = _account_query_table_column_int(hstmt, ACCOUNT_CUSTOM_FIELD_ACCOUNT_ID);
1541 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_APP_ID);
1542 _account_db_data_to_text(textbuf, &(custom_record->app_id));
1544 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_KEY);
1545 _account_db_data_to_text(textbuf, &(custom_record->key));
1547 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_VALUE);
1548 _account_db_data_to_text(textbuf, &(custom_record->value));
1549 _INFO("key = %s, value = %s", custom_record->key, custom_record->value);
1550 _INFO("end _account_convert_column_to_custom()");
1553 bool _account_get_capability_text_cb(const char* capability_type, account_capability_state_e capability_value, void *user_data)
1555 account_s *data = (account_s*)user_data;
1557 account_capability_s *cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
1559 if (cap_data == NULL)
1561 ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
1563 cap_data->type = _account_get_text(capability_type);
1564 cap_data->value = capability_value;
1565 _INFO("cap_data->type = %s, cap_data->value = %d", cap_data->type, cap_data->value);
1567 data->capablity_list = g_slist_append(data->capablity_list, (gpointer)cap_data);
1573 bool _account_get_custom_text_cb(char* key, char* value, void *user_data)
1575 account_s *data = (account_s*)user_data;
1577 account_custom_s *custom_data = (account_custom_s*)malloc(sizeof(account_custom_s));
1579 if (custom_data == NULL) {
1580 ACCOUNT_DEBUG("_account_get_custom_text_cb :: malloc fail\n");
1583 ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s));
1585 custom_data->account_id = data->id;
1586 custom_data->app_id = _account_get_text(data->package_name);
1587 custom_data->key = _account_get_text(key);
1588 custom_data->value = _account_get_text(value);
1589 _INFO("custom_data->key = %s, custom_data->value = %s", custom_data->key, custom_data->value);
1591 data->custom_list = g_slist_append(data->custom_list, (gpointer)custom_data);
1597 static char *_account_get_text(const char *text_data)
1599 char *text_value = NULL;
1601 if (text_data != NULL) {
1602 text_value = strdup(text_data);
1607 static int _account_compare_old_record_by_user_name(account_s *new_account, const char* user_name, const char* package_name)
1609 int error_code = ACCOUNT_ERROR_NONE;
1610 account_stmt hstmt = NULL;
1611 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1613 account_s *old_account = NULL;
1615 ACCOUNT_RETURN_VAL((new_account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
1616 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
1617 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
1618 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1620 old_account = (account_s*)calloc(1, sizeof(account_s));
1622 ACCOUNT_FATAL("Memory alloc fail\n");
1623 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1626 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1628 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = '%s' and package_name='%s'", ACCOUNT_TABLE, user_name, package_name);
1629 hstmt = _account_prepare_query(query);
1631 rc = _account_query_step(hstmt);
1632 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1634 while (rc == SQLITE_ROW) {
1635 _account_convert_column_to_account(hstmt, old_account);
1636 rc = _account_query_step(hstmt);
1639 rc = _account_query_finalize(hstmt);
1640 ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1644 error_code = _account_query_capability_by_account_id(_account_get_capability_text_cb, old_account->id, (void*)old_account);
1645 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
1648 error_code = _account_query_custom_by_account_id(_account_get_custom_text_cb, old_account->id, (void*)old_account);
1649 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
1652 new_account->id = old_account->id;
1655 if(!new_account->user_name) {
1656 if(old_account->user_name)
1657 new_account->user_name = _account_get_text(old_account->user_name);
1661 if(!new_account->display_name) {
1662 if(old_account->display_name)
1663 new_account->display_name = _account_get_text(old_account->display_name);
1667 if(!new_account->email_address) {
1668 if(old_account->email_address)
1669 new_account->email_address = _account_get_text(old_account->email_address);
1673 if(!new_account->domain_name) {
1674 if(old_account->domain_name)
1675 new_account->domain_name = _account_get_text(old_account->domain_name);
1679 if(!new_account->icon_path) {
1680 if(old_account->icon_path)
1681 new_account->icon_path = _account_get_text(old_account->icon_path);
1685 if(!new_account->source) {
1686 if(old_account->source)
1687 new_account->source = _account_get_text(old_account->source);
1690 _ACCOUNT_FREE(new_account->package_name);
1691 new_account->package_name = _account_get_text(old_account->package_name);
1694 if(!new_account->access_token) {
1695 if(old_account->access_token)
1696 new_account->access_token = _account_get_text(old_account->access_token);
1700 if(new_account->auth_type == ACCOUNT_AUTH_TYPE_INVALID) {
1701 new_account->auth_type = old_account->auth_type;
1705 if(new_account->secret== ACCOUNT_SECRECY_INVALID) {
1706 new_account->secret = old_account->secret;
1710 if(new_account->sync_support == ACCOUNT_SYNC_INVALID) {
1711 new_account->sync_support = old_account->sync_support;
1716 for(i=0;i<USER_TXT_CNT;i++) {
1717 if(!new_account->user_data_txt[i]) {
1718 if(old_account->user_data_txt[i])
1719 new_account->user_data_txt[i] = _account_get_text(old_account->user_data_txt[i]);
1724 for(i=0;i<USER_INT_CNT;i++) {
1725 if(new_account->user_data_int[i] == 0) {
1726 new_account->user_data_int[i] = old_account->user_data_int[i];
1732 // user custom table
1736 _account_free_account_with_items(old_account);
1739 if (hstmt != NULL) {
1740 rc = _account_query_finalize(hstmt);
1741 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1745 return ACCOUNT_ERROR_NONE;
1750 static int _account_update_account_by_user_name(int pid, int uid, account_s *account, const char *user_name, const char *package_name)
1752 int rc = 0, binding_count = 0, count = 0;
1753 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1754 int error_code = ACCOUNT_ERROR_NONE;
1755 account_stmt hstmt = NULL;
1757 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is NULL.\n"));
1758 ACCOUNT_RETURN_VAL((package_name!= NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is NULL.\n"));
1760 char* current_appid = NULL;
1761 char* verified_appid = NULL;
1763 current_appid = _account_get_current_appid(pid);
1764 error_code = _account_check_account_type_with_appid_group(uid, current_appid, &verified_appid);
1766 _ACCOUNT_FREE(current_appid);
1767 _ACCOUNT_FREE(verified_appid);
1769 if(error_code != ACCOUNT_ERROR_NONE){
1770 ACCOUNT_ERROR("No permission to update\n");
1771 return ACCOUNT_ERROR_PERMISSION_DENIED;
1774 _account_compare_old_record_by_user_name(account, user_name, package_name);
1776 if( _account_db_err_code() == SQLITE_PERM ){
1777 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
1778 return ACCOUNT_ERROR_PERMISSION_DENIED;
1781 if (!account->package_name) {
1782 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
1783 return ACCOUNT_ERROR_INVALID_PARAMETER;
1786 if (!account->user_name && !account->display_name && !account->email_address) {
1787 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1788 return ACCOUNT_ERROR_INVALID_PARAMETER;
1791 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE user_name='%s' and package_name='%s'"
1792 , ACCOUNT_TABLE, user_name, package_name);
1794 count = _account_get_record_count(query);
1796 if( _account_db_err_code() == SQLITE_PERM ){
1797 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
1798 return ACCOUNT_ERROR_PERMISSION_DENIED;
1802 ACCOUNT_SLOGI("_account_update_account_by_user_name : The account not exist!, count = %d, user_name=%s, package_name=%s\n",
1803 count, user_name, package_name);
1804 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1807 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1809 //TODO: Is it required to update id ? As of now I can only think of falied rollback cases (between account and gSSO DB)
1810 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
1811 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
1812 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
1813 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE user_name=? and package_name=? ", ACCOUNT_TABLE);
1815 hstmt = _account_prepare_query(query);
1816 if( _account_db_err_code() == SQLITE_PERM ){
1817 _account_end_transaction(FALSE);
1818 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
1819 return ACCOUNT_ERROR_PERMISSION_DENIED;
1821 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg()));
1823 binding_count = _account_convert_account_to_sql(account, hstmt, query);
1825 _account_query_bind_text(hstmt, binding_count++, user_name);
1826 _account_query_bind_text(hstmt, binding_count++, package_name);
1827 rc = _account_query_step(hstmt);
1828 if (rc != SQLITE_DONE) {
1829 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1831 rc = _account_query_finalize(hstmt);
1832 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1835 /*update capability*/
1836 error_code = _account_update_capability_by_user_name(account, user_name, package_name);
1839 error_code = _account_update_custom(account, account->id);
1844 int _account_insert_to_db(account_s* account, int pid, int uid, int *account_id)
1847 int error_code = ACCOUNT_ERROR_NONE;
1848 int ret_transaction = 0;
1850 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1851 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1852 ACCOUNT_RETURN_VAL((account_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID POINTER IS NULL"));
1854 if (!account->user_name && !account->display_name && !account->email_address) {
1855 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1856 return ACCOUNT_ERROR_INVALID_PARAMETER;
1859 account_s *data = (account_s*)account;
1860 ACCOUNT_SLOGD("(%s)-(%d) account_insert_to_db: begin_transaction.\n", __FUNCTION__, __LINE__);
1862 pthread_mutex_lock(&account_mutex);
1864 /* transaction control required*/
1865 ret_transaction = _account_begin_transaction();
1867 if(_account_db_err_code() == SQLITE_PERM){
1868 pthread_mutex_unlock(&account_mutex);
1869 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
1870 return ACCOUNT_ERROR_PERMISSION_DENIED;
1873 if (ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY) {
1874 ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction);
1875 pthread_mutex_unlock(&account_mutex);
1876 return ACCOUNT_ERROR_DATABASE_BUSY;
1877 }else if (ret_transaction != ACCOUNT_ERROR_NONE) {
1878 ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction);
1879 pthread_mutex_unlock(&account_mutex);
1880 return ret_transaction;
1883 *account_id = _account_get_next_sequence(ACCOUNT_TABLE);
1884 data->id = *account_id;
1887 appid = _account_get_current_appid(pid);
1892 // API caller cannot be recognized
1893 ret_transaction = _account_end_transaction(FALSE);
1894 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1895 pthread_mutex_unlock(&account_mutex);
1896 return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
1900 char* verified_appid = NULL;
1901 error_code = _account_check_account_type_with_appid_group(uid, appid, &verified_appid);//FIX
1902 _ACCOUNT_FREE(appid);
1903 if(error_code != ACCOUNT_ERROR_NONE)
1905 _ERR("error_code = %d", error_code);
1906 ret_transaction = _account_end_transaction(FALSE);
1907 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1908 _ACCOUNT_FREE(verified_appid);
1909 pthread_mutex_unlock(&account_mutex);
1916 error_code = _account_check_duplicated(data, verified_appid);
1917 if (error_code != ACCOUNT_ERROR_NONE) {
1919 ret_transaction = _account_end_transaction(FALSE);
1920 ACCOUNT_DEBUG("_account_check_duplicated(), rollback insert query(%x)!!!!\n", ret_transaction);
1922 pthread_mutex_unlock(&account_mutex);
1925 if(!_account_check_add_more_account(verified_appid)) {
1926 ret_transaction = _account_end_transaction(FALSE);
1927 ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
1928 pthread_mutex_unlock(&account_mutex);
1929 _ACCOUNT_FREE(verified_appid);
1930 return ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
1933 _ACCOUNT_FREE(data->package_name);
1934 data->package_name = _account_get_text(verified_appid);
1935 _ACCOUNT_FREE(verified_appid);
1938 if(!_account_check_add_more_account(data->package_name))
1941 ret_transaction = _account_end_transaction(FALSE);
1942 ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
1943 pthread_mutex_unlock(&account_mutex);
1944 return ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
1947 error_code = _account_execute_insert_query(data);
1949 if (error_code != ACCOUNT_ERROR_NONE)
1952 ret_transaction = _account_end_transaction(FALSE);
1953 ACCOUNT_ERROR("INSERT account fail, rollback insert query(%x)!!!!\n", ret_transaction);
1955 pthread_mutex_unlock(&account_mutex);
1960 error_code = _account_insert_capability(data, *account_id);
1961 if (error_code != ACCOUNT_ERROR_NONE)
1964 ret_transaction = _account_end_transaction(FALSE);
1965 ACCOUNT_ERROR("INSERT capability fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1967 pthread_mutex_unlock(&account_mutex);
1972 error_code = _account_insert_custom(data, *account_id);
1973 if (error_code != ACCOUNT_ERROR_NONE)
1975 ret_transaction = _account_end_transaction(FALSE);
1976 ACCOUNT_ERROR("INSERT custom fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1978 pthread_mutex_unlock(&account_mutex);
1984 pthread_mutex_unlock(&account_mutex);
1985 _account_end_transaction(TRUE);
1986 ACCOUNT_SLOGD("(%s)-(%d) account _end_transaction.\n", __FUNCTION__, __LINE__);
1989 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_INSERT, *account_id);
1990 _account_insert_delete_update_notification_send(buf);
1991 _INFO("account _notification_send end.");
1993 return ACCOUNT_ERROR_NONE;
1997 int _account_query_capability_by_account_id(capability_cb callback, int account_id, void *user_data )
1999 int error_code = ACCOUNT_ERROR_NONE;
2000 account_stmt hstmt = NULL;
2001 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2004 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2005 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
2006 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2008 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2010 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
2011 hstmt = _account_prepare_query(query);
2013 if( _account_db_err_code() == SQLITE_PERM ){
2014 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2015 return ACCOUNT_ERROR_PERMISSION_DENIED;
2018 rc = _account_query_step(hstmt);
2019 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2021 account_capability_s* capability_record = NULL;
2023 while (rc == SQLITE_ROW) {
2024 bool cb_ret = FALSE;
2025 capability_record = (account_capability_s*) malloc(sizeof(account_capability_s));
2027 if (capability_record == NULL) {
2028 ACCOUNT_FATAL("malloc Failed");
2032 ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s));
2034 _account_convert_column_to_capability(hstmt, capability_record);
2036 cb_ret = callback(capability_record->type, capability_record->value, user_data);
2038 _account_free_capability_with_items(capability_record);
2040 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
2042 rc = _account_query_step(hstmt);
2045 rc = _account_query_finalize(hstmt);
2046 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2049 error_code = ACCOUNT_ERROR_NONE;
2052 if (hstmt != NULL) {
2053 rc = _account_query_finalize(hstmt);
2054 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2058 pthread_mutex_unlock(&account_mutex);
2062 GSList* _account_get_capability_list_by_account_id(int account_id, int *error_code)
2064 *error_code = ACCOUNT_ERROR_NONE;
2065 account_stmt hstmt = NULL;
2066 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2068 GSList* capability_list = NULL;
2070 ACCOUNT_RETURN_VAL((account_id > 0), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("ACCOUNT INDEX IS LESS THAN 0"));
2071 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
2073 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2075 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
2076 hstmt = _account_prepare_query(query);
2078 if( _account_db_err_code() == SQLITE_PERM ){
2079 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2080 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
2084 rc = _account_query_step(hstmt);
2085 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2087 account_capability_s* capability_record = NULL;
2089 while (rc == SQLITE_ROW) {
2090 capability_record = (account_capability_s*) malloc(sizeof(account_capability_s));
2092 if (capability_record == NULL) {
2093 ACCOUNT_FATAL("malloc Failed");
2097 ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s));
2099 _account_convert_column_to_capability(hstmt, capability_record);
2101 //cb_ret = callback(capability_record->type, capability_record->value, user_data);
2103 //_account_free_capability_items(capability_record);
2104 //_ACCOUNT_FREE(capability_record);
2106 //ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
2108 capability_list = g_slist_append(capability_list, capability_record);
2109 rc = _account_query_step(hstmt);
2112 rc = _account_query_finalize(hstmt);
2113 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
2116 *error_code = ACCOUNT_ERROR_NONE;
2121 rc = _account_query_finalize(hstmt);
2122 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
2126 pthread_mutex_unlock(&account_mutex);
2127 return capability_list;
2130 static int _account_compare_old_record(account_s *new_account, int account_id)
2132 int error_code = ACCOUNT_ERROR_NONE;
2133 account_stmt hstmt = NULL;
2134 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2136 account_s *old_account = NULL;
2138 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2139 ACCOUNT_RETURN_VAL((new_account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2140 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2142 old_account = (account_s*)calloc(1, sizeof(account_s));
2143 if (old_account == NULL) {
2144 _ERR("Out of Memory");
2145 return ACCOUNT_ERROR_OUT_OF_MEMORY;
2148 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2150 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
2151 hstmt = _account_prepare_query(query);
2153 rc = _account_query_step(hstmt);
2154 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2156 while (rc == SQLITE_ROW) {
2157 _account_convert_column_to_account(hstmt, old_account);
2158 rc = _account_query_step(hstmt);
2161 rc = _account_query_finalize(hstmt);
2162 ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2166 error_code = _account_query_capability_by_account_id(_account_get_capability_text_cb, old_account->id, (void*)old_account);
2167 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
2170 error_code = _account_query_custom_by_account_id(_account_get_custom_text_cb, old_account->id, (void*)old_account);
2171 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
2175 new_account->id = old_account->id;
2178 if(!new_account->user_name) {
2179 if(old_account->user_name)
2180 new_account->user_name = _account_get_text(old_account->user_name);
2184 if(!new_account->display_name) {
2185 if(old_account->display_name)
2186 new_account->display_name = _account_get_text(old_account->display_name);
2190 if(!new_account->email_address) {
2191 if(old_account->email_address)
2192 new_account->email_address = _account_get_text(old_account->email_address);
2196 if(!new_account->domain_name) {
2197 if(old_account->domain_name)
2198 new_account->domain_name = _account_get_text(old_account->domain_name);
2202 if(!new_account->icon_path) {
2203 if(old_account->icon_path)
2204 new_account->icon_path = _account_get_text(old_account->icon_path);
2208 if(!new_account->source) {
2209 if(old_account->source)
2210 new_account->source = _account_get_text(old_account->source);
2213 _ACCOUNT_FREE(new_account->package_name);
2214 new_account->package_name = _account_get_text(old_account->package_name);
2217 if(!new_account->access_token) {
2218 if(old_account->access_token)
2219 new_account->access_token = _account_get_text(old_account->access_token);
2224 for(i=0;i<USER_TXT_CNT;i++) {
2225 if(!new_account->user_data_txt[i]) {
2226 if(old_account->user_data_txt[i])
2227 new_account->user_data_txt[i] = _account_get_text(old_account->user_data_txt[i]);
2232 if(new_account->auth_type == ACCOUNT_AUTH_TYPE_INVALID) {
2233 new_account->auth_type = old_account->auth_type;
2237 if(new_account->secret== ACCOUNT_SECRECY_INVALID) {
2238 new_account->secret = old_account->secret;
2242 if(new_account->sync_support == ACCOUNT_SYNC_INVALID) {
2243 new_account->sync_support = old_account->sync_support;
2247 for(i=0;i<USER_INT_CNT;i++) {
2248 if(new_account->user_data_int[i] == 0) {
2249 new_account->user_data_int[i] = old_account->user_data_int[i];
2255 // user custom table
2259 _account_free_account_with_items(old_account);
2261 if (hstmt != NULL) {
2262 rc = _account_query_finalize(hstmt);
2263 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2267 return ACCOUNT_ERROR_NONE;
2270 static int _account_get_package_name_from_account_id(int account_id, char **package_name)
2272 int error_code = ACCOUNT_ERROR_NONE;
2273 account_stmt hstmt = NULL;
2274 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2276 account_s *old_account = NULL;
2278 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2279 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2281 old_account = (account_s*)calloc(1, sizeof(account_s));
2282 if (old_account == NULL) {
2283 _ERR("Out Of memory");
2284 return ACCOUNT_ERROR_OUT_OF_MEMORY;
2287 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2289 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
2290 hstmt = _account_prepare_query(query);
2292 rc = _account_query_step(hstmt);
2293 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2295 while (rc == SQLITE_ROW) {
2296 _account_convert_column_to_account(hstmt, old_account);
2297 rc = _account_query_step(hstmt);
2300 rc = _account_query_finalize(hstmt);
2301 ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2304 // get package name.
2305 *package_name = _account_get_text(old_account->package_name);
2310 _account_free_account_with_items(old_account);
2313 if (hstmt != NULL) {
2314 rc = _account_query_finalize(hstmt);
2315 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2323 static int _account_update_account(int pid, int uid, account_s *account, int account_id)
2325 int rc = 0, binding_count =0;
2326 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2327 int error_code = ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0;
2328 account_stmt hstmt = NULL;
2330 if (!account->package_name) {
2331 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
2332 return ACCOUNT_ERROR_INVALID_PARAMETER;
2335 /* Check permission of requested appid */
2336 char* current_appid = NULL;
2337 char *package_name = NULL;
2339 current_appid = _account_get_current_appid(pid);
2340 error_code = _account_get_package_name_from_account_id(account_id, &package_name);
2342 if(error_code != ACCOUNT_ERROR_NONE || package_name == NULL){
2343 ACCOUNT_ERROR("No package name with account_id\n");
2344 _ACCOUNT_FREE(current_appid);
2345 _ACCOUNT_FREE(package_name);
2346 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2349 error_code = _account_check_appid_group_with_package_name(uid, current_appid, package_name);
2350 ACCOUNT_DEBUG( "UPDATE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name); // TODO: remove the log later.
2352 _ACCOUNT_FREE(current_appid);
2353 _ACCOUNT_FREE(package_name);
2355 if(error_code != ACCOUNT_ERROR_NONE){
2356 ACCOUNT_ERROR("No permission to update\n");
2357 return ACCOUNT_ERROR_PERMISSION_DENIED;
2360 error_code = _account_compare_old_record(account, account_id);
2361 if (error_code != ACCOUNT_ERROR_NONE) {
2362 ACCOUNT_ERROR("_account_compare_old_record fail\n");
2366 if( _account_db_err_code() == SQLITE_PERM ){
2367 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2368 return ACCOUNT_ERROR_PERMISSION_DENIED;
2369 } else if( _account_db_err_code() == SQLITE_BUSY ){
2370 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
2371 return ACCOUNT_ERROR_DATABASE_BUSY;
2374 if (!account->user_name && !account->display_name && !account->email_address) {
2375 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
2376 return ACCOUNT_ERROR_INVALID_PARAMETER;
2379 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2381 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
2383 count = _account_get_record_count(query);
2385 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
2386 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2389 /* transaction control required*/
2390 ret_transaction = _account_begin_transaction();
2391 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
2392 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
2393 pthread_mutex_unlock(&account_mutex);
2394 return ACCOUNT_ERROR_DATABASE_BUSY;
2397 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2398 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
2399 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
2400 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
2401 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
2403 hstmt = _account_prepare_query(query);
2405 if( _account_db_err_code() == SQLITE_PERM ){
2406 ret_transaction = _account_end_transaction(FALSE);
2407 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2408 return ACCOUNT_ERROR_PERMISSION_DENIED;
2411 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)));
2413 binding_count = _account_convert_account_to_sql(account, hstmt, query);
2414 _account_query_bind_int(hstmt, binding_count++, account_id);
2416 rc = _account_query_step(hstmt);
2417 if (rc != SQLITE_DONE) {
2418 ACCOUNT_SLOGE( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
2421 rc = _account_query_finalize(hstmt);
2422 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2425 _INFO("update query=%s", query);
2427 /*update capability*/
2428 error_code = _account_update_capability(account, account_id);
2429 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2430 ret_transaction = _account_end_transaction(FALSE);
2431 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2436 error_code = _account_update_custom(account, account_id);
2437 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2438 ret_transaction = _account_end_transaction(FALSE);
2439 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2443 ret_transaction = _account_end_transaction(TRUE);
2445 _INFO("update end");
2450 static int _account_update_account_ex(account_s *account, int account_id)
2452 int rc = 0, binding_count =0;
2453 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2454 int error_code = ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0;
2455 account_stmt hstmt = NULL;
2457 if (!account->package_name) {
2458 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
2459 return ACCOUNT_ERROR_INVALID_PARAMETER;
2462 error_code = _account_compare_old_record(account, account_id);
2463 if (error_code != ACCOUNT_ERROR_NONE) {
2464 ACCOUNT_ERROR("_account_compare_old_record fail\n");
2468 if( _account_db_err_code() == SQLITE_PERM ){
2469 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2470 return ACCOUNT_ERROR_PERMISSION_DENIED;
2473 if (!account->user_name && !account->display_name && !account->email_address) {
2474 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
2475 return ACCOUNT_ERROR_INVALID_PARAMETER;
2478 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2480 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
2482 count = _account_get_record_count(query);
2484 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
2485 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2488 /* transaction control required*/
2489 ret_transaction = _account_begin_transaction();
2490 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
2491 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
2492 pthread_mutex_unlock(&account_mutex);
2493 return ACCOUNT_ERROR_DATABASE_BUSY;
2496 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2497 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
2498 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
2499 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
2500 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
2502 hstmt = _account_prepare_query(query);
2504 if( _account_db_err_code() == SQLITE_PERM ){
2505 ret_transaction = _account_end_transaction(FALSE);
2506 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2507 return ACCOUNT_ERROR_PERMISSION_DENIED;
2510 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)));
2512 _INFO("account_update_to_db_by_id_ex_p : before convert() : account_id[%d], user_name=%s", account->id, account->user_name);
2513 binding_count = _account_convert_account_to_sql(account, hstmt, query);
2514 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], user_name=%s", account->id, account->user_name);
2515 _INFO("account_update_to_db_by_id_ex_p : before bind()");
2516 rc = _account_query_bind_int(hstmt, binding_count++, account_id);
2517 _INFO("account_update_to_db_by_id_ex_p : after bind() : ret = %d", rc);
2519 rc = _account_query_step(hstmt);
2520 if (rc != SQLITE_DONE) {
2521 ACCOUNT_SLOGE( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
2523 _INFO("account_update_to_db_by_id_ex_p : after query_step() : ret = %d", rc);
2525 rc = _account_query_finalize(hstmt);
2526 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2528 _INFO("account_update_to_db_by_id_ex_p : after query_filnalize() : ret = %d", rc);
2530 _INFO("account_update_to_db_by_id_ex_p : before update_capability()");
2531 /*update capability*/
2532 error_code = _account_update_capability(account, account_id);
2533 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2534 ret_transaction = _account_end_transaction(FALSE);
2535 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2538 _INFO("account_update_to_db_by_id_ex_p : after update_capability()");
2540 _INFO("account_update_to_db_by_id_ex_p : before update_custom()");
2542 error_code = _account_update_custom(account, account_id);
2543 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2544 ret_transaction = _account_end_transaction(FALSE);
2545 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2548 _INFO("account_update_to_db_by_id_ex_p : after update_custom()");
2550 ret_transaction = _account_end_transaction(TRUE);
2556 int _account_update_to_db_by_id(int pid, int uid, account_s* account, int account_id)
2558 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
2559 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
2560 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2561 int error_code = ACCOUNT_ERROR_NONE;
2562 account_s* data = (account_s*)account;
2564 pthread_mutex_lock(&account_mutex);
2566 error_code = _account_update_account(pid, uid, data, account_id);
2568 if(error_code != ACCOUNT_ERROR_NONE) {
2569 pthread_mutex_unlock(&account_mutex);
2573 pthread_mutex_unlock(&account_mutex);
2576 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, account_id);
2577 _account_insert_delete_update_notification_send(buf);
2579 return ACCOUNT_ERROR_NONE;
2582 int _account_update_to_db_by_id_ex(account_s* account, int account_id)
2584 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
2585 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
2586 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2587 int error_code = ACCOUNT_ERROR_NONE;
2588 account_s* data = account;
2590 pthread_mutex_lock(&account_mutex);
2592 _INFO("before update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name);
2593 error_code = _account_update_account_ex(data, account_id);
2594 _INFO("after update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name);
2596 if(error_code != ACCOUNT_ERROR_NONE) {
2597 pthread_mutex_unlock(&account_mutex);
2601 pthread_mutex_unlock(&account_mutex);
2604 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, account_id);
2605 _account_insert_delete_update_notification_send(buf);
2607 return ACCOUNT_ERROR_NONE;
2611 int _account_update_to_db_by_user_name(int pid, int uid, account_s* account, const char *user_name, const char *package_name)
2613 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
2614 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
2615 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2617 int error_code = ACCOUNT_ERROR_NONE;
2618 account_s *data = (account_s*)account;
2620 pthread_mutex_lock(&account_mutex);
2622 error_code = _account_update_account_by_user_name(pid, uid, data, user_name, package_name);
2624 pthread_mutex_unlock(&account_mutex);
2627 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, data->id);
2628 _account_insert_delete_update_notification_send(buf);
2633 GSList* _account_db_query_all(int pid)
2635 //int error_code = ACCOUNT_ERROR_NONE;
2636 account_stmt hstmt = NULL;
2637 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2639 GSList *account_list = NULL;
2641 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected."));
2643 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2645 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TABLE);
2646 hstmt = _account_prepare_query(query);
2648 if( _account_db_err_code() == SQLITE_PERM ){
2649 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2653 rc = _account_query_step(hstmt);
2655 account_s *account_record = NULL;
2657 if (rc != SQLITE_ROW)
2659 _ERR("The record isn't found");
2663 while(rc == SQLITE_ROW) {
2664 account_record = (account_s*) malloc(sizeof(account_s));
2666 if (account_record == NULL) {
2667 ACCOUNT_FATAL("malloc Failed");
2671 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2672 _account_convert_column_to_account(hstmt, account_record);
2673 account_list = g_slist_append(account_list, account_record);
2674 rc = _account_query_step(hstmt);
2677 rc = _account_query_finalize(hstmt);
2678 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, NULL, ("finalize error"));
2683 for (iter = account_list; iter != NULL; iter = g_slist_next(iter)) {
2684 account_s *account = NULL;
2685 account = (account_s*)iter->data;
2686 _account_query_capability_by_account_id(_account_get_capability_text_cb, account->id, (void*)account);
2687 _account_query_custom_by_account_id(_account_get_custom_text_cb, account->id, (void*)account);
2691 if (hstmt != NULL) {
2692 rc = _account_query_finalize(hstmt);
2693 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {_account_gslist_account_free(account_list);}, NULL, ("finalize error"));
2698 _remove_sensitive_info_from_non_owning_account_slist(pid, account_list);
2700 return account_list;
2703 int _account_update_sync_status_by_id(int uid, int account_db_id, const int sync_status)
2705 int error_code = ACCOUNT_ERROR_NONE;
2706 account_stmt hstmt = NULL;
2707 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2711 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2712 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2713 if ( (sync_status < 0) || (sync_status > ACCOUNT_SYNC_STATUS_RUNNING)) {
2714 ACCOUNT_SLOGE("(%s)-(%d) sync_status is less than 0 or more than enum max.\n", __FUNCTION__, __LINE__);
2715 return ACCOUNT_ERROR_INVALID_PARAMETER;
2718 pthread_mutex_lock(&account_mutex);
2720 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2722 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_db_id);
2724 rc = _account_get_record_count(query);
2726 if( _account_db_err_code() == SQLITE_PERM ){
2727 pthread_mutex_unlock(&account_mutex);
2728 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2729 return ACCOUNT_ERROR_PERMISSION_DENIED;
2733 ACCOUNT_SLOGE( "account_update_sync_status_by_id : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
2734 pthread_mutex_unlock(&account_mutex);
2735 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2738 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2740 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET sync_support=? WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
2741 hstmt = _account_prepare_query(query);
2743 _account_query_bind_int(hstmt, count, sync_status);
2745 rc = _account_query_step(hstmt);
2747 if( _account_db_err_code() == SQLITE_PERM ){
2748 pthread_mutex_unlock(&account_mutex);
2749 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2750 return ACCOUNT_ERROR_PERMISSION_DENIED;
2753 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_DB_FAILED,
2754 ("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg()));
2756 rc = _account_query_finalize(hstmt);
2757 if (rc != ACCOUNT_ERROR_NONE) {
2758 ACCOUNT_ERROR("_account_query_finalize error");
2759 pthread_mutex_unlock(&account_mutex);
2763 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_SYNC_UPDATE, account_db_id);
2764 _account_insert_delete_update_notification_send(buf);
2767 error_code = ACCOUNT_ERROR_NONE;
2770 if (hstmt != NULL) {
2771 rc = _account_query_finalize(hstmt);
2772 pthread_mutex_unlock(&account_mutex);
2773 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2777 pthread_mutex_unlock(&account_mutex);
2781 int _account_query_account_by_account_id(int pid, int account_db_id, account_s *account_record)
2783 _INFO("_account_query_account_by_account_id() start, account_db_id=[%d]", account_db_id);
2785 int error_code = ACCOUNT_ERROR_NONE;
2786 account_stmt hstmt = NULL;
2787 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2790 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2791 ACCOUNT_RETURN_VAL(account_record != NULL, {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2792 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2794 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2796 ACCOUNT_DEBUG("starting db operations");
2798 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
2799 hstmt = _account_prepare_query(query);
2800 rc = _account_db_err_code();
2801 _INFO("after _account_prepare_query, rc=[%d]", rc);
2803 if( rc == SQLITE_PERM ){
2804 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2805 return ACCOUNT_ERROR_PERMISSION_DENIED;
2808 ACCOUNT_DEBUG("before _account_query_step");
2809 rc = _account_query_step(hstmt);
2810 ACCOUNT_DEBUG("after _account_query_step returned [%d]", rc);
2811 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2813 while (rc == SQLITE_ROW) {
2814 ACCOUNT_DEBUG("before _account_convert_column_to_account");
2815 _account_convert_column_to_account(hstmt, account_record);
2816 ACCOUNT_DEBUG("after _account_convert_column_to_account");
2817 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]);
2818 rc = _account_query_step(hstmt);
2821 ACCOUNT_DEBUG("account_record->id=[%d]", account_record->id);
2823 rc = _account_query_finalize(hstmt);
2824 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2826 ACCOUNT_DEBUG("before _account_query_capability_by_account_id");
2827 _account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record);
2828 ACCOUNT_DEBUG("after _account_query_capability_by_account_id");
2830 ACCOUNT_DEBUG("before _account_query_custom_by_account_id");
2831 _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record);
2832 ACCOUNT_DEBUG("after _account_query_custom_by_account_id");
2835 error_code = ACCOUNT_ERROR_NONE;
2838 if (hstmt != NULL) {
2839 rc = _account_query_finalize(hstmt);
2840 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2846 _remove_sensitive_info_from_non_owning_account(pid, account_record);
2848 pthread_mutex_unlock(&account_mutex);
2849 ACCOUNT_DEBUG("_account_query_account_by_account_id end [%d]", error_code);
2853 GList* _account_query_account_by_user_name(int pid, const char *user_name, int *error_code)
2855 *error_code = ACCOUNT_ERROR_NONE;
2856 account_stmt hstmt = NULL;
2857 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2859 account_s *account_head = NULL;
2861 if (user_name == NULL)
2863 _ERR("USER NAME IS NULL");
2864 *error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
2868 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2870 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ?", ACCOUNT_TABLE);
2872 hstmt = _account_prepare_query(query);
2874 if (_account_db_err_code() == SQLITE_PERM)
2876 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2877 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
2881 int binding_count = 1;
2882 _account_query_bind_text(hstmt, binding_count++, user_name);
2884 rc = _account_query_step(hstmt);
2886 if (rc != SQLITE_ROW)
2888 _ERR("The record isn't found");
2889 *error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
2895 account_head = (account_s*) malloc(sizeof(account_s));
2896 if (account_head == NULL) {
2897 ACCOUNT_FATAL("malloc Failed");
2898 if (hstmt != NULL) {
2899 rc = _account_query_finalize(hstmt);
2901 if (rc != ACCOUNT_ERROR_NONE)
2903 _ERR("finalize error");
2909 *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
2912 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2914 while (rc == SQLITE_ROW) {
2915 account_s* account_record = NULL;
2917 account_record = (account_s*) malloc(sizeof(account_s));
2919 if (account_record == NULL) {
2920 ACCOUNT_FATAL("malloc Failed");
2923 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2925 _account_convert_column_to_account(hstmt, account_record);
2927 account_head->account_list = g_list_append(account_head->account_list, account_record);
2929 rc = _account_query_step(hstmt);
2933 rc = _account_query_finalize(hstmt);
2935 if (rc != ACCOUNT_ERROR_NONE)
2937 _ERR("finalize error");
2947 tmp = g_list_length(account_head->account_list);
2949 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2951 account = (account_h)iter->data;
2953 account_s *testaccount = (account_s*)account;
2955 _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
2956 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
2960 *error_code = ACCOUNT_ERROR_NONE;
2963 if (hstmt != NULL) {
2964 rc = _account_query_finalize(hstmt);
2965 if (rc != ACCOUNT_ERROR_NONE)
2967 _ERR("finalize error");
2973 pthread_mutex_unlock(&account_mutex);
2976 _remove_sensitive_info_from_non_owning_account_list(pid, account_head->account_list);
2977 GList* result = account_head->account_list;
2978 _ACCOUNT_FREE(account_head);
2985 _account_query_account_by_capability(int pid, const char* capability_type, const int capability_value, int *error_code)
2987 *error_code = ACCOUNT_ERROR_NONE;
2988 account_stmt hstmt = NULL;
2989 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2992 ACCOUNT_RETURN_VAL((capability_type != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("capability_type IS NULL"));
2994 if ((capability_value < 0) || (capability_value > ACCOUNT_CAPABILITY_ENABLED)) {
2995 ACCOUNT_SLOGE("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
2996 *error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
3000 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
3002 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3004 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=? AND value=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
3006 hstmt = _account_prepare_query(query);
3008 if( _account_db_err_code() == SQLITE_PERM ){
3009 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3010 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
3014 int binding_count = 1;
3015 _account_query_bind_text(hstmt, binding_count++, capability_type);
3016 _account_query_bind_int(hstmt, binding_count++, capability_value);
3018 rc = _account_query_step(hstmt);
3020 account_s* account_head = NULL;
3022 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3026 account_head = (account_s*) malloc(sizeof(account_s));
3027 if (account_head == NULL) {
3028 ACCOUNT_FATAL("malloc Failed");
3029 if (hstmt != NULL) {
3030 rc = _account_query_finalize(hstmt);
3031 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
3034 *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
3037 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3039 while (rc == SQLITE_ROW) {
3040 account_s* account_record = NULL;
3042 account_record = (account_s*) malloc(sizeof(account_s));
3044 if (account_record == NULL) {
3045 ACCOUNT_FATAL("malloc Failed");
3048 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3050 _account_convert_column_to_account(hstmt, account_record);
3052 account_head->account_list = g_list_append(account_head->account_list, account_record);
3054 rc = _account_query_step(hstmt);
3058 rc = _account_query_finalize(hstmt);
3059 ACCOUNT_CATCH_ERROR_P((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3065 tmp = g_list_length(account_head->account_list);
3067 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3068 account_h account = NULL;
3069 account = (account_h)iter->data;
3070 account_s* testaccount = (account_s*)account;
3072 _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3073 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3078 *error_code = ACCOUNT_ERROR_NONE;
3081 if (hstmt != NULL) {
3082 rc = _account_query_finalize(hstmt);
3083 if ( rc != ACCOUNT_ERROR_NONE ) {
3085 _ERR("finalize error");
3090 if( *error_code != ACCOUNT_ERROR_NONE && account_head ) {
3091 _account_glist_account_free(account_head->account_list);
3092 _ACCOUNT_FREE(account_head);
3093 account_head = NULL;
3096 pthread_mutex_unlock(&account_mutex);
3100 _remove_sensitive_info_from_non_owning_account_list(pid, account_head->account_list);
3101 GList* result = account_head->account_list;
3102 _ACCOUNT_FREE(account_head);
3108 GList* _account_query_account_by_capability_type(int pid, const char* capability_type, int *error_code)
3110 *error_code = ACCOUNT_ERROR_NONE;
3111 account_stmt hstmt = NULL;
3112 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3115 ACCOUNT_RETURN_VAL((capability_type != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("capability_type IS NULL"));
3116 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;},
3117 NULL, ("The database isn't connected."));
3119 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3121 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
3123 hstmt = _account_prepare_query(query);
3125 if( _account_db_err_code() == SQLITE_PERM ){
3126 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3127 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
3131 int binding_count = 1;
3132 _account_query_bind_text(hstmt, binding_count++, capability_type);
3134 rc = _account_query_step(hstmt);
3136 account_s* account_head = NULL;
3138 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3142 account_head = (account_s*) malloc(sizeof(account_s));
3143 if (account_head == NULL) {
3144 ACCOUNT_FATAL("malloc Failed");
3145 if (hstmt != NULL) {
3146 rc = _account_query_finalize(hstmt);
3147 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
3150 *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
3153 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3155 while (rc == SQLITE_ROW) {
3156 account_s* account_record = NULL;
3158 account_record = (account_s*) malloc(sizeof(account_s));
3160 if (account_record == NULL) {
3161 ACCOUNT_FATAL("malloc Failed");
3164 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3166 _account_convert_column_to_account(hstmt, account_record);
3168 account_head->account_list = g_list_append(account_head->account_list, account_record);
3170 rc = _account_query_step(hstmt);
3174 rc = _account_query_finalize(hstmt);
3175 ACCOUNT_CATCH_ERROR_P((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3181 tmp = g_list_length(account_head->account_list);
3183 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3184 account_s* testaccount = (account_s*)iter->data;
3186 _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3187 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3191 *error_code = ACCOUNT_ERROR_NONE;
3196 rc = _account_query_finalize(hstmt);
3197 if (rc != ACCOUNT_ERROR_NONE) {
3199 _ERR("finalize error");
3204 if( (*error_code != ACCOUNT_ERROR_NONE) && account_head ) {
3205 _account_glist_account_free(account_head->account_list);
3206 _ACCOUNT_FREE(account_head);
3207 account_head = NULL;
3210 pthread_mutex_unlock(&account_mutex);
3214 _remove_sensitive_info_from_non_owning_account_list(pid, account_head->account_list);
3215 GList* result = account_head->account_list;
3216 _ACCOUNT_FREE(account_head);
3222 GList* _account_query_account_by_package_name(int pid,const char* package_name, int *error_code)
3224 _INFO("_account_query_account_by_package_name");
3226 *error_code = ACCOUNT_ERROR_NONE;
3227 account_stmt hstmt = NULL;
3228 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3231 ACCOUNT_RETURN_VAL((package_name != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("PACKAGE NAME IS NULL"));
3232 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
3234 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3236 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE package_name=?", ACCOUNT_TABLE);
3238 hstmt = _account_prepare_query(query);
3240 if( _account_db_err_code() == SQLITE_PERM ){
3241 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3242 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
3246 int binding_count = 1;
3247 _account_query_bind_text(hstmt, binding_count++, package_name);
3249 rc = _account_query_step(hstmt);
3251 account_s* account_head = NULL;
3253 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.(%s)\n", package_name));
3257 account_head = (account_s*) malloc(sizeof(account_s));
3258 if (account_head == NULL) {
3259 ACCOUNT_FATAL("malloc Failed");
3260 if (hstmt != NULL) {
3261 rc = _account_query_finalize(hstmt);
3262 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
3265 *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
3268 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3270 while (rc == SQLITE_ROW) {
3271 account_s* account_record = NULL;
3273 account_record = (account_s*) malloc(sizeof(account_s));
3275 if (account_record == NULL) {
3276 ACCOUNT_FATAL("malloc Failed");
3279 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3281 _account_convert_column_to_account(hstmt, account_record);
3283 _INFO("Adding account_list");
3284 account_head->account_list = g_list_append(account_head->account_list, account_record);
3286 rc = _account_query_step(hstmt);
3290 rc = _account_query_finalize(hstmt);
3291 ACCOUNT_CATCH_ERROR_P((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3296 tmp = g_list_length(account_head->account_list);
3298 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3299 account_s* testaccount = (account_s*)iter->data;
3301 _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3302 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3305 *error_code = ACCOUNT_ERROR_NONE;
3310 rc = _account_query_finalize(hstmt);
3311 if (rc != ACCOUNT_ERROR_NONE) {
3313 _ERR("finalize error");
3318 pthread_mutex_unlock(&account_mutex);
3320 if( (*error_code != ACCOUNT_ERROR_NONE) && account_head ) {
3321 _account_glist_account_free(account_head->account_list);
3322 _ACCOUNT_FREE(account_head);
3323 account_head = NULL;
3326 if ((*error_code == ACCOUNT_ERROR_NONE) && account_head != NULL)
3328 _INFO("Returning account_list");
3329 _remove_sensitive_info_from_non_owning_account_list(pid,account_head->account_list);
3330 GList* result = account_head->account_list;
3331 _ACCOUNT_FREE(account_head);
3337 int _account_delete(int pid, int uid, int account_id)
3339 int error_code = ACCOUNT_ERROR_NONE;
3340 account_stmt hstmt = NULL;
3341 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3343 int ret_transaction = 0;
3344 bool is_success = FALSE;
3346 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3349 /* Check requested ID to delete */
3350 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id=%d", ACCOUNT_TABLE, account_id);
3352 count = _account_get_record_count(query);
3354 if( _account_db_err_code() == SQLITE_PERM ){
3355 pthread_mutex_unlock(&account_mutex);
3356 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3357 return ACCOUNT_ERROR_PERMISSION_DENIED;
3361 ACCOUNT_ERROR("account id(%d) is not exist. count(%d)\n", account_id, count);
3362 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3365 /* Check permission of requested appid */
3366 char* current_appid = NULL;
3367 char *package_name = NULL;
3369 current_appid = _account_get_current_appid(pid);
3371 error_code = _account_get_package_name_from_account_id(account_id, &package_name);
3373 if(error_code != ACCOUNT_ERROR_NONE){
3374 ACCOUNT_ERROR("No package name with account_id\n");
3375 _ACCOUNT_FREE(current_appid);
3376 _ACCOUNT_FREE(package_name);
3377 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3379 ACCOUNT_DEBUG( "DELETE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name);
3381 error_code = _account_check_appid_group_with_package_name(uid, current_appid, package_name);
3383 _ACCOUNT_FREE(current_appid);
3384 _ACCOUNT_FREE(package_name);
3386 if(error_code != ACCOUNT_ERROR_NONE){
3387 ACCOUNT_ERROR("No permission to delete\n");
3388 return ACCOUNT_ERROR_PERMISSION_DENIED;
3391 /* transaction control required*/
3392 ret_transaction = _account_begin_transaction();
3394 if( _account_db_err_code() == SQLITE_PERM ){
3395 pthread_mutex_unlock(&account_mutex);
3396 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3397 return ACCOUNT_ERROR_PERMISSION_DENIED;
3400 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
3401 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
3402 pthread_mutex_unlock(&account_mutex);
3403 return ACCOUNT_ERROR_DATABASE_BUSY;
3406 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3407 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3408 pthread_mutex_unlock(&account_mutex);
3409 return ret_transaction;
3412 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3413 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
3415 hstmt = _account_prepare_query(query);
3417 if( _account_db_err_code() == SQLITE_PERM ){
3418 pthread_mutex_unlock(&account_mutex);
3419 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3420 return ACCOUNT_ERROR_PERMISSION_DENIED;
3423 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3424 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3426 rc = _account_query_step(hstmt);
3427 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3429 rc = _account_query_finalize(hstmt);
3431 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3434 ACCOUNT_MEMSET(query, 0, sizeof(query));
3436 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
3438 hstmt = _account_prepare_query(query);
3439 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3440 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3442 rc = _account_query_step(hstmt);
3443 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
3445 rc = _account_query_finalize(hstmt);
3446 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3449 /* delete custom data */
3450 ACCOUNT_MEMSET(query, 0, sizeof(query));
3452 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
3454 hstmt = _account_prepare_query(query);
3456 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3457 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3459 rc = _account_query_step(hstmt);
3460 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
3462 rc = _account_query_finalize(hstmt);
3463 ACCOUNT_CATCH_ERROR(rc == ACCOUNT_ERROR_NONE, {}, rc, ("finalize error", account_id, rc));
3469 if (hstmt != NULL) {
3470 rc = _account_query_finalize(hstmt);
3471 if(rc != ACCOUNT_ERROR_NONE ){
3472 ACCOUNT_ERROR("rc (%d)", rc);
3479 ret_transaction = _account_end_transaction(is_success);
3481 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3482 ACCOUNT_ERROR("account_delete:_account_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3484 if (is_success == true) {
3486 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, account_id);
3487 _account_insert_delete_update_notification_send(buf);
3491 pthread_mutex_unlock(&account_mutex);
3497 static int _account_query_account_by_username_and_package(const char* username, const char* package_name, account_h *account)
3501 int error_code = ACCOUNT_ERROR_NONE;
3502 account_stmt hstmt = NULL;
3503 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3505 int binding_count = 1;
3507 ACCOUNT_RETURN_VAL((username != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("username IS NULL"));
3508 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name IS NULL"));
3509 ACCOUNT_RETURN_VAL((*account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
3510 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3512 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3514 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
3515 hstmt = _account_prepare_query(query);
3517 if( _account_db_err_code() == SQLITE_PERM ){
3518 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3519 return ACCOUNT_ERROR_PERMISSION_DENIED;
3522 _account_query_bind_text(hstmt, binding_count++, username);
3523 _account_query_bind_text(hstmt, binding_count++, package_name);
3525 rc = _account_query_step(hstmt);
3526 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3528 account_s *account_record = (account_s *)(*account);
3530 while (rc == SQLITE_ROW) {
3531 _account_convert_column_to_account(hstmt, account_record);
3532 rc = _account_query_step(hstmt);
3535 rc = _account_query_finalize(hstmt);
3536 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3537 _account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record);
3538 _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record);
3541 error_code = ACCOUNT_ERROR_NONE;
3544 if (hstmt != NULL) {
3545 rc = _account_query_finalize(hstmt);
3546 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3550 pthread_mutex_unlock(&account_mutex);
3554 int _account_create(account_h *account)
3557 ACCOUNT_SLOGE("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__);
3558 return ACCOUNT_ERROR_INVALID_PARAMETER;
3561 account_s *data = (account_s*)malloc(sizeof(account_s));
3564 ACCOUNT_FATAL("Memory Allocation Failed");
3565 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3567 ACCOUNT_MEMSET(data, 0, sizeof(account_s));
3569 /*Setting account as visible by default*/
3570 data->secret = ACCOUNT_SECRECY_VISIBLE;
3572 /*Setting account as not supporting sync by default*/
3573 data->sync_support = ACCOUNT_SYNC_NOT_SUPPORT;
3575 *account = (account_h)data;
3577 return ACCOUNT_ERROR_NONE;
3580 int _account_destroy(account_h account)
3582 account_s *data = (account_s*)account;
3584 ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account handle is null!"));
3586 _account_free_account_with_items(data);
3588 return ACCOUNT_ERROR_NONE;
3591 int _account_get_account_id(account_s* account, int *account_id)
3594 return ACCOUNT_ERROR_INVALID_PARAMETER;
3597 return ACCOUNT_ERROR_INVALID_PARAMETER;
3600 *account_id = account->id;
3602 return ACCOUNT_ERROR_NONE;
3605 int _account_delete_from_db_by_user_name(int pid, int uid, const char *user_name, const char *package_name)
3607 _INFO("[%s][%s]", user_name, package_name);
3609 int error_code = ACCOUNT_ERROR_NONE;
3610 account_stmt hstmt = NULL;
3611 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3613 int ret_transaction = 0;
3614 bool is_success = FALSE;
3615 account_h account = NULL;
3616 int binding_count = 1;
3617 int account_id = -1;
3619 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is null!"));
3620 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
3621 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3623 /* Check permission of requested appid */
3624 char* current_appid = NULL;
3625 char* package_name_temp = NULL;
3627 current_appid = _account_get_current_appid(pid);
3629 package_name_temp = _account_get_text(package_name);
3631 ACCOUNT_DEBUG( "DELETE:user_name[%s],current_appid[%s], package_name[%s]", user_name, current_appid, package_name_temp);
3633 error_code = _account_check_appid_group_with_package_name(uid, current_appid, package_name_temp);
3635 _ACCOUNT_FREE(current_appid);
3636 _ACCOUNT_FREE(package_name_temp);
3638 if(error_code != ACCOUNT_ERROR_NONE){
3639 ACCOUNT_ERROR("No permission to delete\n");
3640 return ACCOUNT_ERROR_PERMISSION_DENIED;
3643 rc = _account_create(&account);
3644 rc = _account_query_account_by_username_and_package(user_name, package_name, &account);
3648 if( _account_db_err_code() == SQLITE_PERM )
3650 _account_destroy(account);
3651 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3652 return ACCOUNT_ERROR_PERMISSION_DENIED;
3656 account_s* account_data = (account_s*)account;
3658 rc = _account_get_account_id(account_data, &account_id);
3660 rc = _account_destroy(account);
3662 /* transaction control required*/
3663 ret_transaction = _account_begin_transaction();
3665 if( _account_db_err_code() == SQLITE_PERM )
3667 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3668 pthread_mutex_unlock(&account_mutex);
3669 return ACCOUNT_ERROR_PERMISSION_DENIED;
3673 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY )
3675 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
3676 pthread_mutex_unlock(&account_mutex);
3677 return ACCOUNT_ERROR_DATABASE_BUSY;
3679 else if (ret_transaction != ACCOUNT_ERROR_NONE) {
3680 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3681 pthread_mutex_unlock(&account_mutex);
3682 return ret_transaction;
3685 /* delete custom data */
3686 ACCOUNT_MEMSET(query, 0, sizeof(query));
3687 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = ?", ACCOUNT_CUSTOM_TABLE);
3689 hstmt = _account_prepare_query(query);
3691 if( _account_db_err_code() == SQLITE_PERM ){
3692 _account_end_transaction(FALSE);
3693 pthread_mutex_unlock(&account_mutex);
3694 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3695 return ACCOUNT_ERROR_PERMISSION_DENIED;
3698 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3699 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3701 _account_query_bind_int(hstmt, binding_count++, account_id);
3703 rc = _account_query_step(hstmt);
3704 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3706 rc = _account_query_finalize(hstmt);
3707 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3710 /* delete capability */
3711 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", CAPABILITY_TABLE);
3713 hstmt = _account_prepare_query(query);
3715 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3716 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3719 _account_query_bind_text(hstmt, binding_count++, user_name);
3720 _account_query_bind_text(hstmt, binding_count++, package_name);
3722 rc = _account_query_step(hstmt);
3723 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3725 rc = _account_query_finalize(hstmt);
3726 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3729 ACCOUNT_MEMSET(query, 0, sizeof(query));
3732 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
3734 hstmt = _account_prepare_query(query);
3735 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3736 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3740 _account_query_bind_text(hstmt, binding_count++, user_name);
3741 _account_query_bind_text(hstmt, binding_count++, package_name);
3743 rc = _account_query_step(hstmt);
3744 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));
3746 rc = _account_query_finalize(hstmt);
3747 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3753 if (hstmt != NULL) {
3754 rc = _account_query_finalize(hstmt);
3755 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3759 ret_transaction = _account_end_transaction(is_success);
3761 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3762 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3764 if (is_success == true) {
3766 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, account_id);
3767 _account_insert_delete_update_notification_send(buf);
3771 pthread_mutex_unlock(&account_mutex);
3776 int _account_delete_from_db_by_package_name(int pid, int uid, const char *package_name, gboolean permission)
3778 _INFO("_account_delete_from_db_by_package_name");
3779 int error_code = ACCOUNT_ERROR_NONE;
3780 account_stmt hstmt = NULL;
3781 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3783 int ret_transaction = 0;
3784 bool is_success = FALSE;
3785 int binding_count = 1;
3786 GSList *account_id_list = NULL;
3789 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
3790 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3792 // It only needs list of ids, does not need to query sensitive info. So sending 0
3793 GList* account_list_temp = _account_query_account_by_package_name(getpid(), package_name, &ret);
3794 if( _account_db_err_code() == SQLITE_PERM ){
3795 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3796 return ACCOUNT_ERROR_PERMISSION_DENIED;
3799 if(ret != ACCOUNT_ERROR_NONE){
3800 _ERR("_account_query_account_by_package_name failed ret=[%d]", ret);
3804 /* Check permission of requested appid */
3806 char* current_appid = NULL;
3807 char* package_name_temp = NULL;
3809 current_appid = _account_get_current_appid(pid);
3811 package_name_temp = _account_get_text(package_name);
3813 ACCOUNT_DEBUG( "DELETE: current_appid[%s], package_name[%s]", current_appid, package_name_temp);
3815 error_code = _account_check_appid_group_with_package_name(uid, current_appid, package_name_temp);
3817 _ACCOUNT_FREE(current_appid);
3818 _ACCOUNT_FREE(package_name_temp);
3820 if(error_code != ACCOUNT_ERROR_NONE){
3821 ACCOUNT_ERROR("No permission to delete\n");
3822 _account_glist_account_free(account_list_temp);
3823 return ACCOUNT_ERROR_PERMISSION_DENIED;
3827 GList *account_list = g_list_first(account_list_temp);
3828 _INFO("account_list_temp length=[%d]",g_list_length(account_list));
3831 for (iter = account_list; iter != NULL; iter = g_list_next(iter))
3833 _INFO("iterating account_list");
3834 account_s *account = NULL;
3835 _INFO("Before iter->data");
3836 account = (account_s*)iter->data;
3837 _INFO("After iter->data");
3838 if (account != NULL)
3840 char id[256] = {0, };
3842 ACCOUNT_MEMSET(id, 0, 256);
3844 ACCOUNT_SNPRINTF(id, 256, "%d", account->id);
3846 _INFO("Adding account id [%s]", id);
3847 account_id_list = g_slist_append(account_id_list, g_strdup(id));
3851 _account_glist_account_free(account_list_temp);
3853 /* transaction control required*/
3854 ret_transaction = _account_begin_transaction();
3856 if( _account_db_err_code() == SQLITE_PERM ){
3857 pthread_mutex_unlock(&account_mutex);
3858 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3859 return ACCOUNT_ERROR_PERMISSION_DENIED;
3862 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
3863 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
3864 pthread_mutex_unlock(&account_mutex);
3865 return ACCOUNT_ERROR_DATABASE_BUSY;
3866 }else if (ret_transaction != ACCOUNT_ERROR_NONE) {
3867 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3868 pthread_mutex_unlock(&account_mutex);
3869 return ret_transaction;
3872 /* delete custom table */
3873 ACCOUNT_MEMSET(query, 0, sizeof(query));
3874 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", ACCOUNT_CUSTOM_TABLE);
3876 hstmt = _account_prepare_query(query);
3878 if( _account_db_err_code() == SQLITE_PERM ){
3879 _account_end_transaction(FALSE);
3880 pthread_mutex_unlock(&account_mutex);
3881 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3882 return ACCOUNT_ERROR_PERMISSION_DENIED;
3885 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3886 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3889 _account_query_bind_text(hstmt, binding_count++, package_name);
3891 rc = _account_query_step(hstmt);
3892 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3894 rc = _account_query_finalize(hstmt);
3895 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3898 /* delete capability table */
3899 ACCOUNT_MEMSET(query, 0, sizeof(query));
3900 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", CAPABILITY_TABLE);
3902 hstmt = _account_prepare_query(query);
3904 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3905 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3908 _account_query_bind_text(hstmt, binding_count++, package_name);
3910 rc = _account_query_step(hstmt);
3911 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3913 rc = _account_query_finalize(hstmt);
3914 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3917 /* delete account table */
3918 ACCOUNT_MEMSET(query, 0, sizeof(query));
3920 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", ACCOUNT_TABLE);
3922 hstmt = _account_prepare_query(query);
3923 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3924 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3927 _account_query_bind_text(hstmt, binding_count++, package_name);
3929 rc = _account_query_step(hstmt);
3930 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));
3932 rc = _account_query_finalize(hstmt);
3933 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3939 if (hstmt != NULL) {
3940 rc = _account_query_finalize(hstmt);
3941 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3945 ret_transaction = _account_end_transaction(is_success);
3947 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3948 ACCOUNT_ERROR("account_delete:_account_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3950 if (is_success == true) {
3951 GSList* gs_iter = NULL;
3952 for (gs_iter = account_id_list; gs_iter != NULL; gs_iter = g_slist_next(gs_iter)) {
3953 char* p_tmpid = NULL;
3954 p_tmpid = (char*)gs_iter->data;
3956 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%s", ACCOUNT_NOTI_NAME_DELETE, p_tmpid);
3957 ACCOUNT_SLOGD("%s", buf);
3958 _account_insert_delete_update_notification_send(buf);
3959 _ACCOUNT_FREE(p_tmpid);
3961 g_slist_free(account_id_list);
3965 pthread_mutex_unlock(&account_mutex);
3967 _INFO("_account_delete_from_db_by_package_name end");
3971 int _account_get_total_count_from_db(gboolean include_hidden, int *count)
3974 ACCOUNT_SLOGE("(%s)-(%d) count is NULL.\n", __FUNCTION__, __LINE__);
3975 return ACCOUNT_ERROR_INVALID_PARAMETER;
3979 ACCOUNT_ERROR("DB is not opened\n");
3980 return ACCOUNT_ERROR_DB_NOT_OPENED;
3983 char query[1024] = {0, };
3984 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3988 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s", ACCOUNT_TABLE);
3992 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s where secret = %d", ACCOUNT_TABLE, ACCOUNT_SECRECY_VISIBLE);
3995 *count = _account_get_record_count(query);
3997 if( _account_db_err_code() == SQLITE_PERM ){
3998 pthread_mutex_unlock(&account_mutex);
3999 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4000 return ACCOUNT_ERROR_PERMISSION_DENIED;
4005 account_stmt pStmt = NULL;
4007 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
4008 if (SQLITE_OK != rc) {
4009 ACCOUNT_SLOGE("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg());
4010 sqlite3_finalize(pStmt);
4011 return ACCOUNT_ERROR_DB_FAILED;
4014 rc = sqlite3_step(pStmt);
4015 if (SQLITE_ROW != rc) {
4016 ACCOUNT_ERROR("[ERROR] sqlite3_step() failed\n");
4017 sqlite3_finalize(pStmt);
4018 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4021 ncount = sqlite3_column_int(pStmt, 0);
4025 sqlite3_finalize(pStmt);
4028 ACCOUNT_ERROR("[ERROR] Number of account : %d, End", ncount);
4029 return ACCOUNT_ERROR_DB_FAILED;
4032 return ACCOUNT_ERROR_NONE;
4035 int account_type_create(account_type_h *account_type)
4037 if (!account_type) {
4038 ACCOUNT_SLOGE("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
4039 return ACCOUNT_ERROR_INVALID_PARAMETER;
4042 account_type_s *data = (account_type_s*)malloc(sizeof(account_type_s));
4045 ACCOUNT_ERROR("Memory Allocation Failed");
4046 return ACCOUNT_ERROR_OUT_OF_MEMORY;
4049 ACCOUNT_MEMSET(data, 0, sizeof(account_type_s));
4051 *account_type = (account_type_h)data;
4053 return ACCOUNT_ERROR_NONE;
4056 int account_type_destroy(account_type_h account_type)
4058 account_type_s *data = (account_type_s*)account_type;
4060 ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account type handle is null!"));
4062 _account_type_free_account_type_with_items(data);
4064 return ACCOUNT_ERROR_NONE;
4067 //app_id mandatory field
4068 int account_type_set_app_id(account_type_h account_type, const char *app_id)
4070 if (!account_type) {
4071 ACCOUNT_SLOGE("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
4072 return ACCOUNT_ERROR_INVALID_PARAMETER;
4076 ACCOUNT_SLOGE("(%s)-(%d) app_id is NULL.\n", __FUNCTION__, __LINE__);
4077 return ACCOUNT_ERROR_INVALID_PARAMETER;
4080 account_type_s *data = (account_type_s*)account_type;
4082 _ACCOUNT_FREE(data->app_id);
4083 data->app_id = _account_get_text(app_id);
4085 return ACCOUNT_ERROR_NONE;
4088 //service_provider_id mandatory field
4089 int account_type_set_service_provider_id(account_type_h account_type, const char *service_provider_id)
4091 if (!account_type) {
4092 return ACCOUNT_ERROR_INVALID_PARAMETER;
4095 if (!service_provider_id) {
4096 return ACCOUNT_ERROR_INVALID_PARAMETER;
4099 account_type_s *data = (account_type_s*)account_type;
4101 _ACCOUNT_FREE(data->service_provider_id);
4102 data->service_provider_id = _account_get_text(service_provider_id);
4104 return ACCOUNT_ERROR_NONE;
4107 int account_type_set_icon_path(account_type_h account_type, const char *icon_path)
4109 if (!account_type) {
4110 return ACCOUNT_ERROR_INVALID_PARAMETER;
4114 return ACCOUNT_ERROR_INVALID_PARAMETER;
4117 account_type_s *data = (account_type_s*)account_type;
4119 _ACCOUNT_FREE(data->icon_path);
4120 data->icon_path = _account_get_text(icon_path);
4122 return ACCOUNT_ERROR_NONE;
4125 int account_type_set_small_icon_path(account_type_h account_type, const char *small_icon_path)
4127 if (!account_type) {
4128 return ACCOUNT_ERROR_INVALID_PARAMETER;
4131 if (!small_icon_path) {
4132 return ACCOUNT_ERROR_INVALID_PARAMETER;
4135 account_type_s *data = (account_type_s*)account_type;
4137 _ACCOUNT_FREE(data->small_icon_path);
4138 data->small_icon_path = _account_get_text(small_icon_path);
4140 return ACCOUNT_ERROR_NONE;
4143 int account_type_set_multiple_account_support(account_type_h account_type, const bool multiple_account_support)
4145 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__));
4147 account_type_s *data = (account_type_s*)account_type;
4149 data->multiple_account_support = multiple_account_support;
4151 return ACCOUNT_ERROR_NONE;
4155 int account_type_set_label(account_type_h account_type, const char* label, const char* locale)
4157 if (!account_type) {
4158 ACCOUNT_SLOGE("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
4159 return ACCOUNT_ERROR_INVALID_PARAMETER;
4162 if(!label || !locale) {
4163 return ACCOUNT_ERROR_INVALID_PARAMETER;
4166 account_type_s *data = (account_type_s*)account_type;
4167 label_s *label_data = (label_s*)malloc(sizeof(label_s));
4169 if (label_data == NULL) {
4170 return ACCOUNT_ERROR_OUT_OF_MEMORY;
4172 ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
4174 label_data->label = _account_get_text(label);
4175 label_data->locale = _account_get_text(locale);
4177 data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
4179 return ACCOUNT_ERROR_NONE;
4182 int account_type_get_app_id(account_type_h account_type, char **app_id)
4184 if (!account_type) {
4185 return ACCOUNT_ERROR_INVALID_PARAMETER;
4189 return ACCOUNT_ERROR_INVALID_PARAMETER;
4192 account_type_s *data = (account_type_s*)account_type;
4195 *app_id = _account_get_text(data->app_id);
4197 return ACCOUNT_ERROR_NONE;
4200 int account_type_get_service_provider_id(account_type_h account_type, char **service_provider_id)
4202 if (!account_type) {
4203 return ACCOUNT_ERROR_INVALID_PARAMETER;
4206 if (!service_provider_id) {
4207 return ACCOUNT_ERROR_INVALID_PARAMETER;
4210 account_type_s *data = (account_type_s*)account_type;
4212 (*service_provider_id) = NULL;
4213 *service_provider_id = _account_get_text(data->service_provider_id);
4215 return ACCOUNT_ERROR_NONE;
4218 int account_type_get_icon_path(account_type_h account_type, char **icon_path)
4220 if (!account_type) {
4221 return ACCOUNT_ERROR_INVALID_PARAMETER;
4225 return ACCOUNT_ERROR_INVALID_PARAMETER;
4228 account_type_s *data = (account_type_s*)account_type;
4230 (*icon_path) = NULL;
4231 *icon_path = _account_get_text(data->icon_path);
4233 return ACCOUNT_ERROR_NONE;
4236 int account_type_get_small_icon_path(account_type_h account_type, char **small_icon_path)
4238 if (!account_type) {
4239 return ACCOUNT_ERROR_INVALID_PARAMETER;
4242 if (!small_icon_path) {
4243 return ACCOUNT_ERROR_INVALID_PARAMETER;
4246 account_type_s *data = (account_type_s*)account_type;
4248 (*small_icon_path) = NULL;
4249 *small_icon_path = _account_get_text(data->small_icon_path);
4251 return ACCOUNT_ERROR_NONE;
4254 int account_type_get_multiple_account_support(account_type_h account_type, int *multiple_account_support)
4256 if (!account_type) {
4257 return ACCOUNT_ERROR_INVALID_PARAMETER;
4259 if (!multiple_account_support) {
4260 return ACCOUNT_ERROR_INVALID_PARAMETER;
4263 account_type_s *data = (account_type_s*)account_type;
4265 *multiple_account_support = data->multiple_account_support;
4267 return ACCOUNT_ERROR_NONE;
4270 int account_type_get_label_by_locale(account_type_h account_type, const char* locale, char** label)
4272 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4273 ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID PARAMETER"));
4276 account_type_s *data = (account_type_s*)account_type;
4278 for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) {
4279 label_s *label_data = NULL;
4281 label_data = (label_s*)iter->data;
4285 if(!strcmp(locale, label_data->locale)) {
4286 *label = _account_get_text(label_data->label);
4287 return ACCOUNT_ERROR_NONE;
4291 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4294 int account_type_get_label(account_type_h account_type, account_label_cb callback, void *user_data)
4296 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4297 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4300 account_type_s *data = (account_type_s*)account_type;
4302 for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) {
4303 label_s *label_data = NULL;
4305 label_data = (label_s*)iter->data;
4307 if(callback(label_data->app_id, label_data->label, label_data->locale, user_data)!=TRUE) {
4308 ACCOUNT_DEBUG("Callback func returs FALSE, its iteration is stopped!!!!\n");
4309 return ACCOUNT_ERROR_NONE;
4313 return ACCOUNT_ERROR_NONE;
4316 static gboolean _account_type_check_duplicated(account_type_s *data)
4318 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4321 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4323 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId='%s'"
4324 , ACCOUNT_TYPE_TABLE, data->app_id);
4326 count = _account_get_record_count(query);
4334 static int _account_type_convert_account_to_sql(account_type_s *account_type, account_stmt hstmt, char *sql_value)
4340 /*Caution : Keep insert query orders.*/
4343 _account_query_bind_text(hstmt, count++, (char*)account_type->app_id);
4345 /* 2. service provider id*/
4346 _account_query_bind_text(hstmt, count++, (char*)account_type->service_provider_id);
4349 _account_query_bind_text(hstmt, count++, (char*)account_type->icon_path);
4351 /* 4. small icon path*/
4352 _account_query_bind_text(hstmt, count++, (char*)account_type->small_icon_path);
4354 /* 5. multiple accont support*/
4355 _account_query_bind_int(hstmt, count++, account_type->multiple_account_support);
4363 static int _account_type_execute_insert_query(account_type_s *account_type)
4368 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4369 int error_code = ACCOUNT_ERROR_NONE;
4370 account_stmt hstmt = NULL;
4372 /* check mandatory field */
4373 // app id & service provider id
4374 if (!account_type->app_id) {
4375 return ACCOUNT_ERROR_INVALID_PARAMETER;
4378 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4379 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s( AppId, ServiceProviderId , IconPath , SmallIconPath , MultipleAccountSupport ) values "
4380 "(?, ?, ?, ?, ?)", ACCOUNT_TYPE_TABLE);
4383 hstmt = _account_prepare_query(query);
4386 if( _account_db_err_code() == SQLITE_PERM ){
4387 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4388 return ACCOUNT_ERROR_PERMISSION_DENIED;
4389 } else if( _account_db_err_code() == SQLITE_BUSY ){
4390 ACCOUNT_ERROR( "Database Busy(%s)", _account_db_err_msg());
4391 return ACCOUNT_ERROR_DATABASE_BUSY;
4394 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4397 _account_type_convert_account_to_sql(account_type, hstmt, query);
4400 rc = _account_query_step(hstmt);
4401 if (rc == SQLITE_BUSY) {
4402 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4403 error_code = ACCOUNT_ERROR_DATABASE_BUSY;
4404 } else if (rc != SQLITE_DONE) {
4405 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4406 error_code = ACCOUNT_ERROR_DB_FAILED;
4410 rc = _account_query_finalize(hstmt);
4411 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4418 static int _account_type_insert_label(account_type_s *account_type)
4421 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4422 account_stmt hstmt = NULL;
4424 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4426 if (g_slist_length( account_type->label_list)==0) {
4427 ACCOUNT_ERROR( "_account_type_insert_label, no label\n");
4428 return ACCOUNT_ERROR_NONE;
4431 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId = '%s'", ACCOUNT_TYPE_TABLE, account_type->app_id);
4433 rc = _account_get_record_count(query);
4435 if( _account_db_err_code() == SQLITE_PERM ){
4436 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4437 return ACCOUNT_ERROR_PERMISSION_DENIED;
4441 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4447 for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
4450 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4451 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
4452 "(?, ?, ?) ", LABEL_TABLE);
4454 hstmt = _account_prepare_query(query);
4456 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4458 label_s* label_data = NULL;
4459 label_data = (label_s*)iter->data;
4461 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4462 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4463 ret = _account_query_bind_text(hstmt, count++, label_data->label);
4464 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4465 ret = _account_query_bind_text(hstmt, count++, (char*)label_data->locale);
4466 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4468 rc = _account_query_step(hstmt);
4470 if (rc != SQLITE_DONE) {
4471 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4475 rc = _account_query_finalize(hstmt);
4476 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4481 return ACCOUNT_ERROR_NONE;
4484 static void _account_type_convert_column_to_provider_feature(account_stmt hstmt, provider_feature_s *feature_record)
4486 const char *textbuf = NULL;
4488 textbuf = _account_query_table_column_text(hstmt, PROVIDER_FEATURE_FIELD_APP_ID);
4489 _account_db_data_to_text(textbuf, &(feature_record->app_id));
4491 textbuf = _account_query_table_column_text(hstmt, PROVIDER_FEATURE_FIELD_KEY);
4492 _account_db_data_to_text(textbuf, &(feature_record->key));
4496 GSList* _account_type_query_provider_feature_by_app_id(const char* app_id, int *error_code)
4498 _INFO("_account_type_query_provider_feature_by_app_id");
4499 *error_code = ACCOUNT_ERROR_NONE;
4500 account_stmt hstmt = NULL;
4501 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4502 int rc = 0, binding_count = 1;
4503 GSList* feature_list = NULL;
4505 ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
4506 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
4508 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4510 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
4511 _INFO("account query=[%s]", query);
4513 hstmt = _account_prepare_query(query);
4515 if( _account_db_err_code() == SQLITE_PERM ){
4516 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4517 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
4521 _account_query_bind_text(hstmt, binding_count++, app_id);
4523 rc = _account_query_step(hstmt);
4524 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {*error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;}, NULL, ("The record isn't found.\n"));
4526 provider_feature_s* feature_record = NULL;
4528 while (rc == SQLITE_ROW) {
4530 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
4532 if (feature_record == NULL) {
4533 ACCOUNT_FATAL("malloc Failed");
4537 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
4539 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
4541 _INFO("Adding account feature_list");
4542 feature_list = g_slist_append(feature_list, feature_record);
4544 rc = _account_query_step(hstmt);
4547 rc = _account_query_finalize(hstmt);
4548 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("account finalize error"));
4551 *error_code = ACCOUNT_ERROR_NONE;
4554 if (hstmt != NULL) {
4555 rc = _account_query_finalize(hstmt);
4556 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("account finalize error"));
4560 pthread_mutex_unlock(&account_mutex);
4562 _INFO("Returning account feature_list");
4563 return feature_list;
4566 int account_type_query_provider_feature_by_app_id(provider_feature_cb callback, const char* app_id, void *user_data )
4568 int error_code = ACCOUNT_ERROR_NONE;
4569 account_stmt hstmt = NULL;
4570 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4571 int rc = 0, binding_count = 1;
4573 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4574 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4575 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4577 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4579 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
4580 hstmt = _account_prepare_query(query);
4582 if( _account_db_err_code() == SQLITE_PERM ){
4583 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4584 return ACCOUNT_ERROR_PERMISSION_DENIED;
4587 _account_query_bind_text(hstmt, binding_count++, app_id);
4589 rc = _account_query_step(hstmt);
4590 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4592 provider_feature_s* feature_record = NULL;
4594 while (rc == SQLITE_ROW) {
4595 bool cb_ret = FALSE;
4596 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
4598 if (feature_record == NULL) {
4599 ACCOUNT_FATAL("malloc Failed");
4603 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
4605 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
4607 cb_ret = callback(feature_record->app_id, feature_record->key, user_data);
4609 _account_type_free_feature_with_items(feature_record);
4611 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
4613 rc = _account_query_step(hstmt);
4616 rc = _account_query_finalize(hstmt);
4617 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4620 error_code = ACCOUNT_ERROR_NONE;
4623 if (hstmt != NULL) {
4624 rc = _account_query_finalize(hstmt);
4625 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4629 pthread_mutex_unlock(&account_mutex);
4633 bool _account_type_query_supported_feature(const char* app_id, const char* capability, int *error_code)
4635 _INFO("_account_type_query_supported_feature start");
4637 *error_code = ACCOUNT_ERROR_NONE;
4639 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4640 int record_count = 0;
4642 if (app_id == NULL || capability == NULL)
4644 *error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
4648 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
4650 record_count = _account_get_record_count(query);
4652 if( _account_db_err_code() == SQLITE_PERM ){
4653 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4654 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
4658 if (record_count <= 0)
4660 *error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
4664 _INFO("_account_type_query_supported_feature end");
4670 int account_type_get_provider_feature_all(account_type_h account_type, provider_feature_cb callback, void* user_data)
4672 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4673 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4676 account_type_s *data = (account_type_s*)account_type;
4678 for (iter = data->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4679 provider_feature_s *feature_data = NULL;
4681 feature_data = (provider_feature_s*)iter->data;
4683 if(callback(feature_data->app_id, feature_data->key, user_data)!=TRUE) {
4684 ACCOUNT_DEBUG("Callback func returs FALSE, its iteration is stopped!!!!\n");
4685 return ACCOUNT_ERROR_NONE;
4689 return ACCOUNT_ERROR_NONE;
4692 int account_type_set_provider_feature(account_type_h account_type, const char* provider_feature)
4694 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("account type handle is null"));
4695 ACCOUNT_RETURN_VAL((provider_feature != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("provider_feature is null"));
4697 account_type_s *data = (account_type_s*)account_type;
4699 GSList *iter = NULL;
4700 bool b_is_new = TRUE;
4702 for(iter = data->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4703 provider_feature_s *feature_data = NULL;
4704 feature_data = (provider_feature_s*)iter->data;
4706 if(!strcmp(feature_data->key, provider_feature)) {
4713 provider_feature_s* feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
4715 if (feature_data == NULL)
4716 return ACCOUNT_ERROR_OUT_OF_MEMORY;
4717 ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
4719 feature_data->key = _account_get_text(provider_feature);
4720 data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
4723 return ACCOUNT_ERROR_NONE;
4726 static int _account_type_insert_provider_feature(account_type_s *account_type, const char* app_id)
4729 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4730 account_stmt hstmt = NULL;
4732 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4733 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4735 if (g_slist_length( account_type->provider_feature_list)==0) {
4736 ACCOUNT_ERROR( "no capability\n");
4737 return ACCOUNT_ERROR_NONE;
4740 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId='%s'", ACCOUNT_TYPE_TABLE, app_id);
4742 rc = _account_get_record_count(query);
4744 if( _account_db_err_code() == SQLITE_PERM ){
4745 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4746 return ACCOUNT_ERROR_PERMISSION_DENIED;
4750 ACCOUNT_SLOGI( "related account type item is not existed rc=%d , %s", rc, _account_db_err_msg());
4751 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4758 for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4761 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4762 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
4763 "(?, ?) ", PROVIDER_FEATURE_TABLE);
4765 hstmt = _account_prepare_query(query);
4767 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4769 provider_feature_s* feature_data = NULL;
4770 feature_data = (provider_feature_s*)iter->data;
4772 ret = _account_query_bind_text(hstmt, count++, app_id);
4773 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4774 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
4775 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
4777 rc = _account_query_step(hstmt);
4779 if (rc != SQLITE_DONE) {
4780 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4784 rc = _account_query_finalize(hstmt);
4785 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4790 return ACCOUNT_ERROR_NONE;
4793 int _account_type_insert_to_db(account_type_s* account_type, int* account_type_id)
4797 int error_code = ACCOUNT_ERROR_NONE, ret_transaction = 0;
4799 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4800 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE HANDLE IS NULL"));
4801 ACCOUNT_RETURN_VAL((account_type_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE ID POINTER IS NULL"));
4803 account_type_s *data = (account_type_s*)account_type;
4805 pthread_mutex_lock(&account_mutex);
4808 /* transaction control required*/
4809 ret_transaction = _account_begin_transaction();
4813 if( _account_db_err_code() == SQLITE_PERM ){
4814 pthread_mutex_unlock(&account_mutex);
4815 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4816 return ACCOUNT_ERROR_PERMISSION_DENIED;
4820 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
4821 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
4822 pthread_mutex_unlock(&account_mutex);
4823 return ACCOUNT_ERROR_DATABASE_BUSY;
4824 } else if (ret_transaction != ACCOUNT_ERROR_NONE) {
4825 ACCOUNT_ERROR("_account_begin_transaction fail %d\n", ret_transaction);
4826 pthread_mutex_unlock(&account_mutex);
4827 return ret_transaction;
4831 if (_account_type_check_duplicated(data)) {
4833 ret_transaction = _account_end_transaction(FALSE);
4834 ACCOUNT_ERROR("Duplicated, rollback insert query(%x)!!!!\n", ret_transaction);
4835 *account_type_id = -1;
4836 pthread_mutex_unlock(&account_mutex);
4837 return ACCOUNT_ERROR_DUPLICATED;
4840 *account_type_id = _account_get_next_sequence(ACCOUNT_TYPE_TABLE);
4842 error_code = _account_type_execute_insert_query(data);
4844 if (error_code != ACCOUNT_ERROR_NONE){
4845 error_code = ACCOUNT_ERROR_DUPLICATED;
4846 ret_transaction = _account_end_transaction(FALSE);
4847 ACCOUNT_ERROR("Insert fail, rollback insert query(%x)!!!!\n", ret_transaction);
4848 *account_type_id = -1;
4849 pthread_mutex_unlock(&account_mutex);
4855 error_code = _account_type_insert_provider_feature(data, data->app_id);
4856 if(error_code != ACCOUNT_ERROR_NONE) {
4858 ret_transaction = _account_end_transaction(FALSE);
4859 ACCOUNT_ERROR("Insert provider feature fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction);
4860 pthread_mutex_unlock(&account_mutex);
4864 error_code = _account_type_insert_label(data);
4865 if(error_code != ACCOUNT_ERROR_NONE) {
4867 ret_transaction = _account_end_transaction(FALSE);
4868 ACCOUNT_ERROR("Insert label fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction);
4869 pthread_mutex_unlock(&account_mutex);
4873 ret_transaction = _account_end_transaction(TRUE);
4875 pthread_mutex_unlock(&account_mutex);
4878 return ACCOUNT_ERROR_NONE;
4881 static int _account_type_update_provider_feature(account_type_s *account_type, const char* app_id)
4884 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4885 account_stmt hstmt = NULL;
4887 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4889 if (g_slist_length( account_type->provider_feature_list)==0) {
4890 ACCOUNT_ERROR( "no feature\n");
4891 return ACCOUNT_ERROR_NONE;
4894 ACCOUNT_DEBUG( "app id", app_id);
4896 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4898 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id=? ", PROVIDER_FEATURE_TABLE);
4899 hstmt = _account_prepare_query(query);
4901 if( _account_db_err_code() == SQLITE_PERM ){
4902 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4903 return ACCOUNT_ERROR_PERMISSION_DENIED;
4907 _account_query_bind_text(hstmt, count++, app_id);
4908 rc = _account_query_step(hstmt);
4910 if (rc != SQLITE_DONE) {
4911 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4912 return ACCOUNT_ERROR_DB_FAILED;
4914 rc = _account_query_finalize(hstmt);
4915 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4920 for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4923 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4924 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
4925 "(?, ?) ", PROVIDER_FEATURE_TABLE);
4927 hstmt = _account_prepare_query(query);
4929 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4931 provider_feature_s* feature_data = NULL;
4932 feature_data = (provider_feature_s*)iter->data;
4934 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4935 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4936 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
4937 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4939 rc = _account_query_step(hstmt);
4941 if (rc != SQLITE_DONE) {
4942 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4945 rc = _account_query_finalize(hstmt);
4946 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4950 return ACCOUNT_ERROR_NONE;
4953 static int _account_type_update_label(account_type_s *account_type, const char* app_id)
4956 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4957 account_stmt hstmt = NULL;
4959 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4961 if (g_slist_length( account_type->label_list)==0) {
4962 return ACCOUNT_ERROR_NONE;
4965 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4967 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId=? ", LABEL_TABLE);
4968 hstmt = _account_prepare_query(query);
4970 if( _account_db_err_code() == SQLITE_PERM ){
4971 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4972 return ACCOUNT_ERROR_PERMISSION_DENIED;
4976 _account_query_bind_text(hstmt, count++, app_id);
4977 rc = _account_query_step(hstmt);
4979 if (rc != SQLITE_DONE) {
4980 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4981 return ACCOUNT_ERROR_DB_FAILED;
4983 rc = _account_query_finalize(hstmt);
4984 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4989 for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
4992 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4993 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
4994 "(?, ?, ?) ", LABEL_TABLE);
4996 hstmt = _account_prepare_query(query);
4998 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
5000 label_s* label_data = NULL;
5001 label_data = (label_s*)iter->data;
5003 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
5004 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5005 ret = _account_query_bind_text(hstmt, count++, label_data->label);
5006 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5007 ret = _account_query_bind_text(hstmt, count++, label_data->locale);
5008 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5010 rc = _account_query_step(hstmt);
5012 if (rc != SQLITE_DONE) {
5013 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5016 rc = _account_query_finalize(hstmt);
5017 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5021 return ACCOUNT_ERROR_NONE;
5025 static int _account_type_update_account(account_type_s *account_type, const char* app_id)
5027 int rc = 0, binding_count =1;
5028 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5029 int error_code = ACCOUNT_ERROR_NONE;
5030 account_stmt hstmt = NULL;
5032 if (!account_type->app_id) {
5033 ACCOUNT_ERROR("app id is mandetory field, it can not be NULL!!!!\n");
5034 return ACCOUNT_ERROR_INVALID_PARAMETER;
5037 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5038 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET AppId=?, ServiceProviderId=?, IconPath=?, "
5039 "SmallIconPath=?, MultipleAccountSupport=? WHERE AppId=? ", ACCOUNT_TYPE_TABLE);
5041 hstmt = _account_prepare_query(query);
5043 if( _account_db_err_code() == SQLITE_PERM ){
5044 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5045 return ACCOUNT_ERROR_PERMISSION_DENIED;
5046 } else if (_account_db_err_code() == SQLITE_BUSY){
5047 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
5048 return ACCOUNT_ERROR_DATABASE_BUSY;
5051 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg()));
5053 binding_count = _account_type_convert_account_to_sql(account_type, hstmt, query);
5054 _account_query_bind_text(hstmt, binding_count++, app_id);
5056 rc = _account_query_step(hstmt);
5057 if (rc != SQLITE_DONE) {
5058 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5061 rc = _account_query_finalize(hstmt);
5062 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5066 error_code = _account_type_update_label(account_type, app_id);
5067 /* update provider feature */
5068 error_code = _account_type_update_provider_feature(account_type, app_id);
5073 int _account_type_update_to_db_by_app_id(account_type_s* account_type, const char* app_id)
5075 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
5076 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5077 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5079 int error_code = ACCOUNT_ERROR_NONE;
5080 account_type_s* data = account_type;
5082 pthread_mutex_lock(&account_mutex);
5084 error_code = _account_type_update_account(data, app_id);
5086 pthread_mutex_unlock(&account_mutex);
5091 int _account_type_delete_by_app_id(const char* app_id)
5093 int error_code = ACCOUNT_ERROR_NONE;
5094 account_stmt hstmt = NULL;
5095 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5096 int rc = 0, count = -1;
5097 int ret_transaction = 0;
5098 int binding_count = 1;
5099 bool is_success = FALSE;
5101 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5102 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("The database isn't connected."));
5104 /* Check requested ID to delete */
5105 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
5107 count = _account_get_record_count(query);
5109 if( _account_db_err_code() == SQLITE_PERM ){
5110 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5111 return ACCOUNT_ERROR_PERMISSION_DENIED;
5115 ACCOUNT_SLOGE("app id(%s) is not exist. count(%d)\n", app_id, count);
5116 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5119 /* transaction control required*/
5120 ret_transaction = _account_begin_transaction();
5122 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
5123 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
5124 pthread_mutex_unlock(&account_mutex);
5125 return ACCOUNT_ERROR_DATABASE_BUSY;
5126 }else if (ret_transaction != ACCOUNT_ERROR_NONE) {
5127 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
5128 pthread_mutex_unlock(&account_mutex);
5129 return ret_transaction;
5132 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", LABEL_TABLE);
5134 hstmt = _account_prepare_query(query);
5136 if( _account_db_err_code() == SQLITE_PERM ){
5137 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5138 pthread_mutex_unlock(&account_mutex);
5139 return ACCOUNT_ERROR_PERMISSION_DENIED;
5142 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
5143 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
5145 _account_query_bind_text(hstmt, binding_count++, app_id);
5147 rc = _account_query_step(hstmt);
5148 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5150 rc = _account_query_finalize(hstmt);
5151 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5155 ACCOUNT_MEMSET(query, 0, sizeof(query));
5157 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id = ? ", PROVIDER_FEATURE_TABLE);
5159 hstmt = _account_prepare_query(query);
5160 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
5161 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
5163 _account_query_bind_text(hstmt, binding_count++, app_id);
5165 rc = _account_query_step(hstmt);
5166 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
5168 rc = _account_query_finalize(hstmt);
5169 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5175 ACCOUNT_MEMSET(query, 0, sizeof(query));
5177 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ? ", ACCOUNT_TYPE_TABLE);
5179 hstmt = _account_prepare_query(query);
5180 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
5181 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
5183 _account_query_bind_text(hstmt, binding_count++, app_id);
5185 rc = _account_query_step(hstmt);
5186 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
5188 rc = _account_query_finalize(hstmt);
5189 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5195 if (hstmt != NULL) {
5196 rc = _account_query_finalize(hstmt);
5197 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5201 ret_transaction = _account_end_transaction(is_success);
5203 if (ret_transaction != ACCOUNT_ERROR_NONE) {
5204 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
5207 pthread_mutex_unlock(&account_mutex);
5212 static void _account_type_convert_column_to_account_type(account_stmt hstmt, account_type_s *account_type_record)
5214 const char *textbuf = NULL;
5216 account_type_record->id = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_ID);
5218 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_APP_ID);
5219 _account_db_data_to_text(textbuf, &(account_type_record->app_id));
5221 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SERVICE_PROVIDER_ID);
5222 _account_db_data_to_text(textbuf, &(account_type_record->service_provider_id));
5224 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_ICON_PATH);
5225 _account_db_data_to_text(textbuf, &(account_type_record->icon_path));
5227 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SMALL_ICON_PATH);
5228 _account_db_data_to_text(textbuf, &(account_type_record->small_icon_path));
5230 account_type_record->multiple_account_support = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_MULTIPLE_ACCOUNT_SUPPORT);
5234 static void _account_type_convert_column_to_label(account_stmt hstmt, label_s *label_record)
5236 const char *textbuf = NULL;
5238 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_APP_ID);
5239 _account_db_data_to_text(textbuf, &(label_record->app_id));
5241 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LABEL);
5242 _account_db_data_to_text(textbuf, &(label_record->label));
5244 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LOCALE);
5245 _account_db_data_to_text(textbuf, &(label_record->locale));
5249 GSList* _account_type_get_label_list_by_app_id(const char* app_id, int *error_code )
5251 *error_code = ACCOUNT_ERROR_NONE;
5252 account_stmt hstmt = NULL;
5253 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5254 int rc = 0, binding_count = 1;
5255 GSList* label_list = NULL;
5257 ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
5258 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
5260 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5262 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
5263 hstmt = _account_prepare_query(query);
5265 if( _account_db_err_code() == SQLITE_PERM ){
5266 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5267 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
5271 _account_query_bind_text(hstmt, binding_count++, app_id);
5273 rc = _account_query_step(hstmt);
5274 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5276 label_s* label_record = NULL;
5278 while (rc == SQLITE_ROW) {
5279 label_record = (label_s*) malloc(sizeof(label_s));
5281 if (label_record == NULL) {
5282 ACCOUNT_FATAL("malloc Failed");
5286 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
5288 _account_type_convert_column_to_label(hstmt, label_record);
5290 _INFO("Adding account label_list");
5291 label_list = g_slist_append (label_list, label_record);
5293 rc = _account_query_step(hstmt);
5296 rc = _account_query_finalize(hstmt);
5297 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
5300 *error_code = ACCOUNT_ERROR_NONE;
5303 if (hstmt != NULL) {
5304 rc = _account_query_finalize(hstmt);
5305 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
5309 pthread_mutex_unlock(&account_mutex);
5310 _INFO("Returning account label_list");
5314 int account_type_query_label_by_app_id(account_label_cb callback, const char* app_id, void *user_data )
5316 int error_code = ACCOUNT_ERROR_NONE;
5317 account_stmt hstmt = NULL;
5318 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5319 int rc = 0, binding_count = 1;
5321 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5322 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
5323 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5325 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5327 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
5328 hstmt = _account_prepare_query(query);
5330 if( _account_db_err_code() == SQLITE_PERM ){
5331 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5332 return ACCOUNT_ERROR_PERMISSION_DENIED;
5335 _account_query_bind_text(hstmt, binding_count++, app_id);
5337 rc = _account_query_step(hstmt);
5338 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5340 label_s* label_record = NULL;
5342 while (rc == SQLITE_ROW) {
5343 bool cb_ret = FALSE;
5344 label_record = (label_s*) malloc(sizeof(label_s));
5346 if (label_record == NULL) {
5347 ACCOUNT_FATAL("malloc Failed");
5351 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
5353 _account_type_convert_column_to_label(hstmt, label_record);
5355 cb_ret = callback(label_record->app_id, label_record->label , label_record->locale, user_data);
5357 _account_type_free_label_with_items(label_record);
5359 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
5361 rc = _account_query_step(hstmt);
5364 rc = _account_query_finalize(hstmt);
5365 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5368 error_code = ACCOUNT_ERROR_NONE;
5371 if (hstmt != NULL) {
5372 rc = _account_query_finalize(hstmt);
5373 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5377 pthread_mutex_unlock(&account_mutex);
5381 int _account_type_label_get_app_id(label_h label, char **app_id)
5384 return ACCOUNT_ERROR_INVALID_PARAMETER;
5388 return ACCOUNT_ERROR_INVALID_PARAMETER;
5391 label_s *data = (label_s*)label;
5395 *app_id = _account_get_text(data->app_id);
5397 return ACCOUNT_ERROR_NONE;
5400 bool _account_get_label_text_cb(char* app_id, char* label, char* locale, void *user_data)
5402 account_type_s *data = (account_type_s*)user_data;
5404 label_s *label_data = (label_s*)malloc(sizeof(label_s));
5406 if (label_data == NULL) {
5407 ACCOUNT_FATAL("_account_get_label_text_cb : MALLOC FAIL\n");
5410 ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
5412 label_data->app_id = _account_get_text(app_id);
5413 label_data->label = _account_get_text(label);
5414 label_data->locale = _account_get_text(locale);
5416 data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
5421 bool _account_get_provider_feature_cb(char* app_id, char* key, void* user_data)
5423 account_type_s *data = (account_type_s*)user_data;
5425 provider_feature_s *feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
5427 if (feature_data == NULL) {
5428 ACCOUNT_FATAL("_account_get_provider_feature_cb : MALLOC FAIL\n");
5431 ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
5433 feature_data->app_id = _account_get_text(app_id);
5434 feature_data->key = _account_get_text(key);
5436 data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
5441 int _account_type_query_by_app_id(const char* app_id, account_type_s** account_type_record)
5443 _INFO("_account_type_query_by_app_id start");
5445 int error_code = ACCOUNT_ERROR_NONE;
5446 account_stmt hstmt = NULL;
5447 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5448 int rc = 0, binding_count = 1;
5450 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5451 ACCOUNT_RETURN_VAL((account_type_record != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("account type record(account_type_s**) is NULL"));
5452 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5454 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5456 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
5457 hstmt = _account_prepare_query(query);
5459 if( _account_db_err_code() == SQLITE_PERM ){
5460 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5461 return ACCOUNT_ERROR_PERMISSION_DENIED;
5464 _account_query_bind_text(hstmt, binding_count++, app_id);
5466 rc = _account_query_step(hstmt);
5467 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5469 *account_type_record = create_empty_account_type_instance();
5470 if (*account_type_record == NULL) {
5471 _ERR("Out of Memory");
5472 error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
5476 while (rc == SQLITE_ROW) {
5477 _account_type_convert_column_to_account_type(hstmt, *account_type_record);
5478 rc = _account_query_step(hstmt);
5481 rc = _account_query_finalize(hstmt);
5482 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5483 account_type_query_label_by_app_id(_account_get_label_text_cb, app_id, (void*)(*account_type_record));
5484 account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, app_id,(void*)(*account_type_record));
5487 error_code = ACCOUNT_ERROR_NONE;
5490 if (hstmt != NULL) {
5491 rc = _account_query_finalize(hstmt);
5492 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5496 pthread_mutex_unlock(&account_mutex);
5497 _INFO("_account_type_query_by_app_id end [%d]", error_code);
5501 int _account_type_query_app_id_exist(const char* app_id)
5503 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5506 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5507 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5509 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5511 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
5512 rc = _account_get_record_count(query);
5514 if( _account_db_err_code() == SQLITE_PERM ){
5515 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5516 return ACCOUNT_ERROR_PERMISSION_DENIED;
5520 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5523 return ACCOUNT_ERROR_NONE;
5526 GSList* _account_type_query_by_provider_feature(const char* key, int *error_code)
5528 *error_code = ACCOUNT_ERROR_NONE;
5529 account_stmt hstmt = NULL;
5530 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5532 GSList *account_type_list = NULL;
5536 ACCOUNT_ERROR("capability_type IS NULL.");
5537 *error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
5541 if(g_hAccountDB == NULL)
5543 ACCOUNT_ERROR("The database isn't connected.");
5544 *error_code = ACCOUNT_ERROR_DB_NOT_OPENED;
5548 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5550 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
5552 hstmt = _account_prepare_query(query);
5554 if( _account_db_err_code() == SQLITE_PERM )
5556 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5557 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
5561 int binding_count = 1;
5562 _account_query_bind_text(hstmt, binding_count++, key);
5564 rc = _account_query_step(hstmt);
5566 account_type_s *account_type_record = NULL;
5568 if(rc != SQLITE_ROW)
5570 ACCOUNT_ERROR("The record isn't found.");
5571 *error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
5575 while(rc == SQLITE_ROW) {
5576 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
5578 if (account_type_record == NULL) {
5579 ACCOUNT_FATAL("malloc Failed");
5583 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
5584 _account_type_convert_column_to_account_type(hstmt, account_type_record);
5585 account_type_list = g_slist_append(account_type_list, account_type_record);
5586 rc = _account_query_step(hstmt);
5589 rc = _account_query_finalize(hstmt);
5590 if (rc != ACCOUNT_ERROR_NONE )
5592 _account_type_gslist_account_type_free(account_type_list);
5593 ACCOUNT_ERROR("finalize error(%s)", rc);
5601 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
5602 account_type_s *account_type = NULL;
5603 account_type = (account_type_s*)iter->data;
5604 account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
5605 account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
5608 *error_code = ACCOUNT_ERROR_NONE;
5611 if (hstmt != NULL) {
5612 rc = _account_query_finalize(hstmt);
5613 if (rc != ACCOUNT_ERROR_NONE)
5621 return account_type_list;
5625 GSList* _account_type_query_all(void)
5627 account_stmt hstmt = NULL;
5628 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5630 GSList *account_type_list = NULL;
5632 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected."));
5634 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5636 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
5637 hstmt = _account_prepare_query(query);
5639 rc = _account_query_step(hstmt);
5641 if( _account_db_err_code() == SQLITE_PERM ){
5642 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5646 account_type_s *account_type_record = NULL;
5648 if (rc != SQLITE_ROW)
5650 _INFO("[ACCOUNT_ERROR_RECORD_NOT_FOUND]The record isn't found.");
5654 while(rc == SQLITE_ROW) {
5655 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
5657 if (account_type_record == NULL) {
5658 ACCOUNT_FATAL("malloc Failed");
5662 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
5663 _account_type_convert_column_to_account_type(hstmt, account_type_record);
5664 account_type_list = g_slist_append(account_type_list, account_type_record);
5665 rc = _account_query_step(hstmt);
5668 rc = _account_query_finalize(hstmt);
5669 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, NULL, ("finalize error"));
5674 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
5675 account_type_s *account_type = NULL;
5676 account_type = (account_type_s*)iter->data;
5677 account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
5678 account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
5684 rc = _account_query_finalize(hstmt);
5685 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list);}, NULL, ("finalize error"));
5689 return account_type_list;
5692 // output parameter label must be free
5693 int _account_type_query_label_by_locale(const char* app_id, const char* locale, char **label)
5695 int error_code = ACCOUNT_ERROR_NONE;
5696 account_stmt hstmt = NULL;
5697 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5698 int rc = 0, binding_count = 1;
5699 char* converted_locale = NULL;
5701 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
5702 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5703 ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
5704 ACCOUNT_RETURN_VAL((locale != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("locale char is null"));
5705 //Making label newly created
5707 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5709 converted_locale = _account_get_text(locale);
5710 gchar** tokens = g_strsplit(converted_locale, "-", 2);
5712 if(tokens != NULL) {
5713 if((char*)(tokens[1]) != NULL) {
5714 char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1]));
5715 if(upper_token != NULL) {
5716 _ACCOUNT_FREE(converted_locale);
5717 converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token);
5719 _ACCOUNT_FREE(upper_token);
5724 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, converted_locale);
5725 _ACCOUNT_FREE(converted_locale);
5727 hstmt = _account_prepare_query(query);
5729 if( _account_db_err_code() == SQLITE_PERM ){
5730 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5731 return ACCOUNT_ERROR_PERMISSION_DENIED;
5734 _account_query_bind_text(hstmt, binding_count++, app_id);
5736 rc = _account_query_step(hstmt);
5737 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5739 label_s* label_record = NULL;
5741 while (rc == SQLITE_ROW) {
5742 label_record = (label_s*) malloc(sizeof(label_s));
5744 if (label_record == NULL) {
5745 ACCOUNT_FATAL("malloc Failed");
5749 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
5751 _account_type_convert_column_to_label(hstmt,label_record);
5753 _ACCOUNT_FREE(*label);
5754 //Making label newly created
5755 *label = _account_get_text(label_record->label);
5757 _account_type_free_label_with_items(label_record);
5759 rc = _account_query_step(hstmt);
5762 rc = _account_query_finalize(hstmt);
5763 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5766 error_code = ACCOUNT_ERROR_NONE;
5769 if (hstmt != NULL) {
5770 rc = _account_query_finalize(hstmt);
5771 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5775 pthread_mutex_unlock(&account_mutex);
5776 _INFO("_account_type_query_label_by_locale() end : error_code = %d", error_code);
5780 static int _account_insert_custom(account_s *account, int account_id)
5782 _INFO("_account_insert_custom start");
5785 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5786 account_stmt hstmt = NULL;
5788 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
5790 if (g_slist_length( account->custom_list)==0) {
5791 ACCOUNT_DEBUG( "_account_insert_custom, no custom data\n");
5792 return ACCOUNT_ERROR_NONE;
5795 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
5797 rc = _account_get_record_count(query);
5799 if( _account_db_err_code() == SQLITE_PERM ){
5800 ACCOUNT_ERROR( "Access failed(%d, %s)", _account_db_err_msg());
5801 return ACCOUNT_ERROR_PERMISSION_DENIED;
5805 ACCOUNT_SLOGE( "_account_insert_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
5806 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5813 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
5816 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5817 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (AccountId, AppId, Key, Value) VALUES "
5818 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
5820 hstmt = _account_prepare_query(query);
5822 if( _account_db_err_code() == SQLITE_PERM ){
5823 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5824 return ACCOUNT_ERROR_PERMISSION_DENIED;
5827 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
5829 account_custom_s* custom_data = NULL;
5830 custom_data = (account_custom_s*)iter->data;
5832 ret = _account_query_bind_int(hstmt, count++, account_id);
5833 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
5834 ret = _account_query_bind_text(hstmt, count++, account->package_name);
5835 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5836 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
5837 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5838 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
5839 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5841 rc = _account_query_step(hstmt);
5843 if (rc != SQLITE_DONE) {
5844 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5848 rc = _account_query_finalize(hstmt);
5849 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5854 _INFO("_account_insert_custom end");
5855 return ACCOUNT_ERROR_NONE;
5858 static int _account_update_custom(account_s *account, int account_id)
5861 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5862 account_stmt hstmt = NULL;
5864 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
5866 if (g_slist_length( account->custom_list)==0) {
5867 ACCOUNT_DEBUG( "_account_update_custom, no custom data\n");
5868 return ACCOUNT_ERROR_NONE;
5871 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
5873 rc = _account_get_record_count(query);
5875 if( _account_db_err_code() == SQLITE_PERM ){
5876 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5877 pthread_mutex_unlock(&account_mutex);
5878 return ACCOUNT_ERROR_PERMISSION_DENIED;
5879 } else if( _account_db_err_code() == SQLITE_BUSY ){
5880 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
5881 pthread_mutex_unlock(&account_mutex);
5882 return ACCOUNT_ERROR_DATABASE_BUSY;
5886 ACCOUNT_SLOGE( "_account_update_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
5887 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5890 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5892 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId=? ", ACCOUNT_CUSTOM_TABLE);
5893 hstmt = _account_prepare_query(query);
5895 _account_query_bind_int(hstmt, count++, (int)account_id);
5896 rc = _account_query_step(hstmt);
5898 if (rc == SQLITE_BUSY) {
5899 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5900 return ACCOUNT_ERROR_DATABASE_BUSY;
5901 } else if (rc != SQLITE_DONE) {
5902 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5903 return ACCOUNT_ERROR_DB_FAILED;
5906 rc = _account_query_finalize(hstmt);
5907 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5912 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
5915 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5916 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AccountId, AppId, Key, Value) VALUES "
5917 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
5919 hstmt = _account_prepare_query(query);
5921 if( _account_db_err_code() == SQLITE_PERM ){
5922 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5923 return ACCOUNT_ERROR_PERMISSION_DENIED;
5926 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
5928 account_custom_s* custom_data = NULL;
5929 custom_data = (account_custom_s*)iter->data;
5931 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
5932 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
5933 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
5934 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5935 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
5936 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5937 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
5938 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5940 rc = _account_query_step(hstmt);
5942 if (rc != SQLITE_DONE) {
5943 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5947 rc = _account_query_finalize(hstmt);
5948 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5953 return ACCOUNT_ERROR_NONE;
5956 static int _account_query_custom_by_account_id(account_custom_cb callback, int account_id, void *user_data )
5958 int error_code = ACCOUNT_ERROR_NONE;
5959 account_stmt hstmt = NULL;
5960 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5963 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
5964 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
5965 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5967 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5969 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
5970 hstmt = _account_prepare_query(query);
5972 if( _account_db_err_code() == SQLITE_PERM ){
5973 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5974 return ACCOUNT_ERROR_PERMISSION_DENIED;
5977 rc = _account_query_step(hstmt);
5979 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5981 account_custom_s* custom_record = NULL;
5983 while (rc == SQLITE_ROW) {
5984 bool cb_ret = FALSE;
5985 custom_record = (account_custom_s*) malloc(sizeof(account_custom_s));
5987 if (custom_record == NULL) {
5988 ACCOUNT_FATAL("malloc Failed");
5992 ACCOUNT_MEMSET(custom_record, 0x00, sizeof(account_custom_s));
5994 _account_convert_column_to_custom(hstmt, custom_record);
5996 cb_ret = callback(custom_record->key, custom_record->value, user_data);
5998 _account_free_custom_with_items(custom_record);
6000 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
6002 rc = _account_query_step(hstmt);
6005 rc = _account_query_finalize(hstmt);
6006 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
6009 error_code = ACCOUNT_ERROR_NONE;
6012 if (hstmt != NULL) {
6013 rc = _account_query_finalize(hstmt);
6014 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
6018 pthread_mutex_unlock(&account_mutex);