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-private.h>
37 #include <account-error.h>
38 #include "account-server-private.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_items(old_account);
1737 _ACCOUNT_FREE(old_account);
1740 if (hstmt != NULL) {
1741 rc = _account_query_finalize(hstmt);
1742 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1746 return ACCOUNT_ERROR_NONE;
1751 static int _account_update_account_by_user_name(int pid, int uid, account_s *account, const char *user_name, const char *package_name)
1753 int rc = 0, binding_count = 0, count = 0;
1754 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1755 int error_code = ACCOUNT_ERROR_NONE;
1756 account_stmt hstmt = NULL;
1758 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is NULL.\n"));
1759 ACCOUNT_RETURN_VAL((package_name!= NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is NULL.\n"));
1761 char* current_appid = NULL;
1762 char* verified_appid = NULL;
1764 current_appid = _account_get_current_appid(pid);
1765 error_code = _account_check_account_type_with_appid_group(uid, current_appid, &verified_appid);
1767 _ACCOUNT_FREE(current_appid);
1768 _ACCOUNT_FREE(verified_appid);
1770 if(error_code != ACCOUNT_ERROR_NONE){
1771 ACCOUNT_ERROR("No permission to update\n");
1772 return ACCOUNT_ERROR_PERMISSION_DENIED;
1775 _account_compare_old_record_by_user_name(account, user_name, package_name);
1777 if( _account_db_err_code() == SQLITE_PERM ){
1778 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
1779 return ACCOUNT_ERROR_PERMISSION_DENIED;
1782 if (!account->package_name) {
1783 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
1784 return ACCOUNT_ERROR_INVALID_PARAMETER;
1787 if (!account->user_name && !account->display_name && !account->email_address) {
1788 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1789 return ACCOUNT_ERROR_INVALID_PARAMETER;
1792 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE user_name='%s' and package_name='%s'"
1793 , ACCOUNT_TABLE, user_name, package_name);
1795 count = _account_get_record_count(query);
1797 if( _account_db_err_code() == SQLITE_PERM ){
1798 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
1799 return ACCOUNT_ERROR_PERMISSION_DENIED;
1803 ACCOUNT_SLOGI("_account_update_account_by_user_name : The account not exist!, count = %d, user_name=%s, package_name=%s\n",
1804 count, user_name, package_name);
1805 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1808 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1810 //TODO: Is it required to update id ? As of now I can only think of falied rollback cases (between account and gSSO DB)
1811 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
1812 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
1813 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
1814 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE user_name=? and package_name=? ", ACCOUNT_TABLE);
1816 hstmt = _account_prepare_query(query);
1817 if( _account_db_err_code() == SQLITE_PERM ){
1818 _account_end_transaction(FALSE);
1819 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
1820 return ACCOUNT_ERROR_PERMISSION_DENIED;
1822 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg()));
1824 binding_count = _account_convert_account_to_sql(account, hstmt, query);
1826 _account_query_bind_text(hstmt, binding_count++, user_name);
1827 _account_query_bind_text(hstmt, binding_count++, package_name);
1828 rc = _account_query_step(hstmt);
1829 if (rc != SQLITE_DONE) {
1830 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1832 rc = _account_query_finalize(hstmt);
1833 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1836 /*update capability*/
1837 error_code = _account_update_capability_by_user_name(account, user_name, package_name);
1840 error_code = _account_update_custom(account, account->id);
1845 int _account_insert_to_db(account_s* account, int pid, int uid, int *account_id)
1848 int error_code = ACCOUNT_ERROR_NONE;
1849 int ret_transaction = 0;
1851 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1852 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1853 ACCOUNT_RETURN_VAL((account_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID POINTER IS NULL"));
1855 if (!account->user_name && !account->display_name && !account->email_address) {
1856 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1857 return ACCOUNT_ERROR_INVALID_PARAMETER;
1860 account_s *data = (account_s*)account;
1861 ACCOUNT_SLOGD("(%s)-(%d) account_insert_to_db: begin_transaction.\n", __FUNCTION__, __LINE__);
1863 pthread_mutex_lock(&account_mutex);
1865 /* transaction control required*/
1866 ret_transaction = _account_begin_transaction();
1868 if(_account_db_err_code() == SQLITE_PERM){
1869 pthread_mutex_unlock(&account_mutex);
1870 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
1871 return ACCOUNT_ERROR_PERMISSION_DENIED;
1874 if (ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY) {
1875 ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction);
1876 pthread_mutex_unlock(&account_mutex);
1877 return ACCOUNT_ERROR_DATABASE_BUSY;
1878 }else if (ret_transaction != ACCOUNT_ERROR_NONE) {
1879 ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction);
1880 pthread_mutex_unlock(&account_mutex);
1881 return ret_transaction;
1884 *account_id = _account_get_next_sequence(ACCOUNT_TABLE);
1885 data->id = *account_id;
1888 appid = _account_get_current_appid(pid);
1893 // API caller cannot be recognized
1894 ret_transaction = _account_end_transaction(FALSE);
1895 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1896 pthread_mutex_unlock(&account_mutex);
1897 return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
1901 char* verified_appid = NULL;
1902 error_code = _account_check_account_type_with_appid_group(uid, appid, &verified_appid);//FIX
1903 _ACCOUNT_FREE(appid);
1904 if(error_code != ACCOUNT_ERROR_NONE)
1906 _ERR("error_code = %d", error_code);
1907 ret_transaction = _account_end_transaction(FALSE);
1908 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1909 _ACCOUNT_FREE(verified_appid);
1910 pthread_mutex_unlock(&account_mutex);
1917 error_code = _account_check_duplicated(data, verified_appid);
1918 if (error_code != ACCOUNT_ERROR_NONE) {
1920 ret_transaction = _account_end_transaction(FALSE);
1921 ACCOUNT_DEBUG("_account_check_duplicated(), rollback insert query(%x)!!!!\n", ret_transaction);
1923 pthread_mutex_unlock(&account_mutex);
1926 if(!_account_check_add_more_account(verified_appid)) {
1927 ret_transaction = _account_end_transaction(FALSE);
1928 ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
1929 pthread_mutex_unlock(&account_mutex);
1930 _ACCOUNT_FREE(verified_appid);
1931 return ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
1934 _ACCOUNT_FREE(data->package_name);
1935 data->package_name = _account_get_text(verified_appid);
1936 _ACCOUNT_FREE(verified_appid);
1939 if(!_account_check_add_more_account(data->package_name))
1942 ret_transaction = _account_end_transaction(FALSE);
1943 ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
1944 pthread_mutex_unlock(&account_mutex);
1945 return ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
1948 error_code = _account_execute_insert_query(data);
1950 if (error_code != ACCOUNT_ERROR_NONE)
1953 ret_transaction = _account_end_transaction(FALSE);
1954 ACCOUNT_ERROR("INSERT account fail, rollback insert query(%x)!!!!\n", ret_transaction);
1956 pthread_mutex_unlock(&account_mutex);
1961 error_code = _account_insert_capability(data, *account_id);
1962 if (error_code != ACCOUNT_ERROR_NONE)
1965 ret_transaction = _account_end_transaction(FALSE);
1966 ACCOUNT_ERROR("INSERT capability fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1968 pthread_mutex_unlock(&account_mutex);
1973 error_code = _account_insert_custom(data, *account_id);
1974 if (error_code != ACCOUNT_ERROR_NONE)
1976 ret_transaction = _account_end_transaction(FALSE);
1977 ACCOUNT_ERROR("INSERT custom fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1979 pthread_mutex_unlock(&account_mutex);
1985 pthread_mutex_unlock(&account_mutex);
1986 _account_end_transaction(TRUE);
1987 ACCOUNT_SLOGD("(%s)-(%d) account _end_transaction.\n", __FUNCTION__, __LINE__);
1990 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_INSERT, *account_id);
1991 _account_insert_delete_update_notification_send(buf);
1992 _INFO("account _notification_send end.");
1994 return ACCOUNT_ERROR_NONE;
1998 int _account_query_capability_by_account_id(capability_cb callback, int account_id, void *user_data )
2000 int error_code = ACCOUNT_ERROR_NONE;
2001 account_stmt hstmt = NULL;
2002 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2005 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2006 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
2007 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2009 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2011 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
2012 hstmt = _account_prepare_query(query);
2014 if( _account_db_err_code() == SQLITE_PERM ){
2015 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2016 return ACCOUNT_ERROR_PERMISSION_DENIED;
2019 rc = _account_query_step(hstmt);
2020 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2022 account_capability_s* capability_record = NULL;
2024 while (rc == SQLITE_ROW) {
2025 bool cb_ret = FALSE;
2026 capability_record = (account_capability_s*) malloc(sizeof(account_capability_s));
2028 if (capability_record == NULL) {
2029 ACCOUNT_FATAL("malloc Failed");
2033 ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s));
2035 _account_convert_column_to_capability(hstmt, capability_record);
2037 cb_ret = callback(capability_record->type, capability_record->value, user_data);
2039 _account_free_capability_items(capability_record);
2040 _ACCOUNT_FREE(capability_record);
2042 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
2044 rc = _account_query_step(hstmt);
2047 rc = _account_query_finalize(hstmt);
2048 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2051 error_code = ACCOUNT_ERROR_NONE;
2054 if (hstmt != NULL) {
2055 rc = _account_query_finalize(hstmt);
2056 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2060 pthread_mutex_unlock(&account_mutex);
2064 GSList* _account_get_capability_list_by_account_id(int account_id, int *error_code)
2066 *error_code = ACCOUNT_ERROR_NONE;
2067 account_stmt hstmt = NULL;
2068 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2070 GSList* capability_list = NULL;
2072 ACCOUNT_RETURN_VAL((account_id > 0), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("ACCOUNT INDEX IS LESS THAN 0"));
2073 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
2075 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2077 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
2078 hstmt = _account_prepare_query(query);
2080 if( _account_db_err_code() == SQLITE_PERM ){
2081 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2082 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
2086 rc = _account_query_step(hstmt);
2087 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2089 account_capability_s* capability_record = NULL;
2091 while (rc == SQLITE_ROW) {
2092 capability_record = (account_capability_s*) malloc(sizeof(account_capability_s));
2094 if (capability_record == NULL) {
2095 ACCOUNT_FATAL("malloc Failed");
2099 ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s));
2101 _account_convert_column_to_capability(hstmt, capability_record);
2103 //cb_ret = callback(capability_record->type, capability_record->value, user_data);
2105 //_account_free_capability_items(capability_record);
2106 //_ACCOUNT_FREE(capability_record);
2108 //ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
2110 capability_list = g_slist_append(capability_list, capability_record);
2111 rc = _account_query_step(hstmt);
2114 rc = _account_query_finalize(hstmt);
2115 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
2118 *error_code = ACCOUNT_ERROR_NONE;
2123 rc = _account_query_finalize(hstmt);
2124 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
2128 pthread_mutex_unlock(&account_mutex);
2129 return capability_list;
2132 static int _account_compare_old_record(account_s *new_account, int account_id)
2134 int error_code = ACCOUNT_ERROR_NONE;
2135 account_stmt hstmt = NULL;
2136 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2138 account_s *old_account = NULL;
2140 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2141 ACCOUNT_RETURN_VAL((new_account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2142 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2144 old_account = (account_s*)calloc(1, sizeof(account_s));
2146 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2148 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
2149 hstmt = _account_prepare_query(query);
2151 rc = _account_query_step(hstmt);
2152 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2154 while (rc == SQLITE_ROW) {
2155 _account_convert_column_to_account(hstmt, old_account);
2156 rc = _account_query_step(hstmt);
2159 rc = _account_query_finalize(hstmt);
2160 ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2164 error_code = _account_query_capability_by_account_id(_account_get_capability_text_cb, old_account->id, (void*)old_account);
2165 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
2168 error_code = _account_query_custom_by_account_id(_account_get_custom_text_cb, old_account->id, (void*)old_account);
2169 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
2173 new_account->id = old_account->id;
2176 if(!new_account->user_name) {
2177 if(old_account->user_name)
2178 new_account->user_name = _account_get_text(old_account->user_name);
2182 if(!new_account->display_name) {
2183 if(old_account->display_name)
2184 new_account->display_name = _account_get_text(old_account->display_name);
2188 if(!new_account->email_address) {
2189 if(old_account->email_address)
2190 new_account->email_address = _account_get_text(old_account->email_address);
2194 if(!new_account->domain_name) {
2195 if(old_account->domain_name)
2196 new_account->domain_name = _account_get_text(old_account->domain_name);
2200 if(!new_account->icon_path) {
2201 if(old_account->icon_path)
2202 new_account->icon_path = _account_get_text(old_account->icon_path);
2206 if(!new_account->source) {
2207 if(old_account->source)
2208 new_account->source = _account_get_text(old_account->source);
2211 _ACCOUNT_FREE(new_account->package_name);
2212 new_account->package_name = _account_get_text(old_account->package_name);
2215 if(!new_account->access_token) {
2216 if(old_account->access_token)
2217 new_account->access_token = _account_get_text(old_account->access_token);
2222 for(i=0;i<USER_TXT_CNT;i++) {
2223 if(!new_account->user_data_txt[i]) {
2224 if(old_account->user_data_txt[i])
2225 new_account->user_data_txt[i] = _account_get_text(old_account->user_data_txt[i]);
2230 if(new_account->auth_type == ACCOUNT_AUTH_TYPE_INVALID) {
2231 new_account->auth_type = old_account->auth_type;
2235 if(new_account->secret== ACCOUNT_SECRECY_INVALID) {
2236 new_account->secret = old_account->secret;
2240 if(new_account->sync_support == ACCOUNT_SYNC_INVALID) {
2241 new_account->sync_support = old_account->sync_support;
2245 for(i=0;i<USER_INT_CNT;i++) {
2246 if(new_account->user_data_int[i] == 0) {
2247 new_account->user_data_int[i] = old_account->user_data_int[i];
2253 // user custom table
2257 _account_free_account_items(old_account);
2258 _ACCOUNT_FREE(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));
2283 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2285 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
2286 hstmt = _account_prepare_query(query);
2288 rc = _account_query_step(hstmt);
2289 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2291 while (rc == SQLITE_ROW) {
2292 _account_convert_column_to_account(hstmt, old_account);
2293 rc = _account_query_step(hstmt);
2296 rc = _account_query_finalize(hstmt);
2297 ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2300 // get package name.
2301 *package_name = _account_get_text(old_account->package_name);
2306 _account_free_account_items(old_account);
2307 _ACCOUNT_FREE(old_account);
2310 if (hstmt != NULL) {
2311 rc = _account_query_finalize(hstmt);
2312 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2320 static int _account_update_account(int pid, int uid, account_s *account, int account_id)
2322 int rc = 0, binding_count =0;
2323 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2324 int error_code = ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0;
2325 account_stmt hstmt = NULL;
2327 if (!account->package_name) {
2328 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
2329 return ACCOUNT_ERROR_INVALID_PARAMETER;
2332 /* Check permission of requested appid */
2333 char* current_appid = NULL;
2334 char *package_name = NULL;
2336 current_appid = _account_get_current_appid(pid);
2337 error_code = _account_get_package_name_from_account_id(account_id, &package_name);
2339 if(error_code != ACCOUNT_ERROR_NONE || package_name == NULL){
2340 ACCOUNT_ERROR("No package name with account_id\n");
2341 _ACCOUNT_FREE(current_appid);
2342 _ACCOUNT_FREE(package_name);
2343 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2346 error_code = _account_check_appid_group_with_package_name(uid, current_appid, package_name);
2347 ACCOUNT_DEBUG( "UPDATE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name); // TODO: remove the log later.
2349 _ACCOUNT_FREE(current_appid);
2350 _ACCOUNT_FREE(package_name);
2352 if(error_code != ACCOUNT_ERROR_NONE){
2353 ACCOUNT_ERROR("No permission to update\n");
2354 return ACCOUNT_ERROR_PERMISSION_DENIED;
2357 _account_compare_old_record(account, account_id);
2359 if( _account_db_err_code() == SQLITE_PERM ){
2360 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2361 return ACCOUNT_ERROR_PERMISSION_DENIED;
2362 } else if( _account_db_err_code() == SQLITE_BUSY ){
2363 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
2364 return ACCOUNT_ERROR_DATABASE_BUSY;
2367 if (!account->user_name && !account->display_name && !account->email_address) {
2368 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
2369 return ACCOUNT_ERROR_INVALID_PARAMETER;
2372 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2374 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
2376 count = _account_get_record_count(query);
2378 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
2379 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2382 /* transaction control required*/
2383 ret_transaction = _account_begin_transaction();
2384 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
2385 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
2386 pthread_mutex_unlock(&account_mutex);
2387 return ACCOUNT_ERROR_DATABASE_BUSY;
2390 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2391 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
2392 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
2393 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
2394 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
2396 hstmt = _account_prepare_query(query);
2398 if( _account_db_err_code() == SQLITE_PERM ){
2399 ret_transaction = _account_end_transaction(FALSE);
2400 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2401 return ACCOUNT_ERROR_PERMISSION_DENIED;
2404 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)));
2406 binding_count = _account_convert_account_to_sql(account, hstmt, query);
2407 _account_query_bind_int(hstmt, binding_count++, account_id);
2409 rc = _account_query_step(hstmt);
2410 if (rc != SQLITE_DONE) {
2411 ACCOUNT_SLOGE( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
2414 rc = _account_query_finalize(hstmt);
2415 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2418 _INFO("update query=%s", query);
2420 /*update capability*/
2421 error_code = _account_update_capability(account, account_id);
2422 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2423 ret_transaction = _account_end_transaction(FALSE);
2424 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2429 error_code = _account_update_custom(account, account_id);
2430 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2431 ret_transaction = _account_end_transaction(FALSE);
2432 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2436 ret_transaction = _account_end_transaction(TRUE);
2438 _INFO("update end");
2443 static int _account_update_account_ex(account_s *account, int account_id)
2445 int rc = 0, binding_count =0;
2446 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2447 int error_code = ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0;
2448 account_stmt hstmt = NULL;
2450 if (!account->package_name) {
2451 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
2452 return ACCOUNT_ERROR_INVALID_PARAMETER;
2455 _account_compare_old_record(account, account_id);
2457 if( _account_db_err_code() == SQLITE_PERM ){
2458 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2459 return ACCOUNT_ERROR_PERMISSION_DENIED;
2462 if (!account->user_name && !account->display_name && !account->email_address) {
2463 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
2464 return ACCOUNT_ERROR_INVALID_PARAMETER;
2467 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2469 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
2471 count = _account_get_record_count(query);
2473 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
2474 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2477 /* transaction control required*/
2478 ret_transaction = _account_begin_transaction();
2479 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
2480 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
2481 pthread_mutex_unlock(&account_mutex);
2482 return ACCOUNT_ERROR_DATABASE_BUSY;
2485 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2486 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
2487 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
2488 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
2489 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
2491 hstmt = _account_prepare_query(query);
2493 if( _account_db_err_code() == SQLITE_PERM ){
2494 ret_transaction = _account_end_transaction(FALSE);
2495 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2496 return ACCOUNT_ERROR_PERMISSION_DENIED;
2499 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)));
2501 _INFO("account_update_to_db_by_id_ex_p : before convert() : account_id[%d], user_name=%s", account->id, account->user_name);
2502 binding_count = _account_convert_account_to_sql(account, hstmt, query);
2503 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], user_name=%s", account->id, account->user_name);
2504 _INFO("account_update_to_db_by_id_ex_p : before bind()");
2505 rc = _account_query_bind_int(hstmt, binding_count++, account_id);
2506 _INFO("account_update_to_db_by_id_ex_p : after bind() : ret = %d", rc);
2508 rc = _account_query_step(hstmt);
2509 if (rc != SQLITE_DONE) {
2510 ACCOUNT_SLOGE( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
2512 _INFO("account_update_to_db_by_id_ex_p : after query_step() : ret = %d", rc);
2514 rc = _account_query_finalize(hstmt);
2515 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2517 _INFO("account_update_to_db_by_id_ex_p : after query_filnalize() : ret = %d", rc);
2519 _INFO("account_update_to_db_by_id_ex_p : before update_capability()");
2520 /*update capability*/
2521 error_code = _account_update_capability(account, account_id);
2522 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2523 ret_transaction = _account_end_transaction(FALSE);
2524 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2527 _INFO("account_update_to_db_by_id_ex_p : after update_capability()");
2529 _INFO("account_update_to_db_by_id_ex_p : before update_custom()");
2531 error_code = _account_update_custom(account, account_id);
2532 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2533 ret_transaction = _account_end_transaction(FALSE);
2534 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2537 _INFO("account_update_to_db_by_id_ex_p : after update_custom()");
2539 ret_transaction = _account_end_transaction(TRUE);
2545 int _account_update_to_db_by_id(int pid, int uid, account_s* account, int account_id)
2547 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
2548 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
2549 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2550 int error_code = ACCOUNT_ERROR_NONE;
2551 account_s* data = (account_s*)account;
2553 pthread_mutex_lock(&account_mutex);
2555 error_code = _account_update_account(pid, uid, data, account_id);
2557 if(error_code != ACCOUNT_ERROR_NONE) {
2558 pthread_mutex_unlock(&account_mutex);
2562 pthread_mutex_unlock(&account_mutex);
2565 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, account_id);
2566 _account_insert_delete_update_notification_send(buf);
2568 return ACCOUNT_ERROR_NONE;
2571 int _account_update_to_db_by_id_ex(account_s* account, int account_id)
2573 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
2574 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
2575 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2576 int error_code = ACCOUNT_ERROR_NONE;
2577 account_s* data = account;
2579 pthread_mutex_lock(&account_mutex);
2581 _INFO("before update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name);
2582 error_code = _account_update_account_ex(data, account_id);
2583 _INFO("after update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name);
2585 if(error_code != ACCOUNT_ERROR_NONE) {
2586 pthread_mutex_unlock(&account_mutex);
2590 pthread_mutex_unlock(&account_mutex);
2593 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, account_id);
2594 _account_insert_delete_update_notification_send(buf);
2596 return ACCOUNT_ERROR_NONE;
2600 int _account_update_to_db_by_user_name(int pid, int uid, account_s* account, const char *user_name, const char *package_name)
2602 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
2603 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
2604 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2606 int error_code = ACCOUNT_ERROR_NONE;
2607 account_s *data = (account_s*)account;
2609 pthread_mutex_lock(&account_mutex);
2611 error_code = _account_update_account_by_user_name(pid, uid, data, user_name, package_name);
2613 pthread_mutex_unlock(&account_mutex);
2616 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, data->id);
2617 _account_insert_delete_update_notification_send(buf);
2622 GSList* _account_db_query_all(int pid)
2624 //int error_code = ACCOUNT_ERROR_NONE;
2625 account_stmt hstmt = NULL;
2626 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2628 GSList *account_list = NULL;
2630 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected."));
2632 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2634 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TABLE);
2635 hstmt = _account_prepare_query(query);
2637 if( _account_db_err_code() == SQLITE_PERM ){
2638 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2642 rc = _account_query_step(hstmt);
2644 account_s *account_record = NULL;
2646 if (rc != SQLITE_ROW)
2648 _ERR("The record isn't found");
2652 while(rc == SQLITE_ROW) {
2653 account_record = (account_s*) malloc(sizeof(account_s));
2655 if (account_record == NULL) {
2656 ACCOUNT_FATAL("malloc Failed");
2660 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2661 _account_convert_column_to_account(hstmt, account_record);
2662 account_list = g_slist_append(account_list, account_record);
2663 rc = _account_query_step(hstmt);
2666 rc = _account_query_finalize(hstmt);
2667 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, NULL, ("finalize error"));
2672 for (iter = account_list; iter != NULL; iter = g_slist_next(iter)) {
2673 account_s *account = NULL;
2674 account = (account_s*)iter->data;
2675 _account_query_capability_by_account_id(_account_get_capability_text_cb, account->id, (void*)account);
2676 _account_query_custom_by_account_id(_account_get_custom_text_cb, account->id, (void*)account);
2680 if (hstmt != NULL) {
2681 rc = _account_query_finalize(hstmt);
2682 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {_account_gslist_free(account_list);}, NULL, ("finalize error"));
2687 _remove_sensitive_info_from_non_owning_account_slist(pid, account_list);
2689 return account_list;
2692 int _account_update_sync_status_by_id(int uid, int account_db_id, const int sync_status)
2694 int error_code = ACCOUNT_ERROR_NONE;
2695 account_stmt hstmt = NULL;
2696 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2700 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2701 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2702 if ( (sync_status < 0) || (sync_status > ACCOUNT_SYNC_STATUS_RUNNING)) {
2703 ACCOUNT_SLOGE("(%s)-(%d) sync_status is less than 0 or more than enum max.\n", __FUNCTION__, __LINE__);
2704 return ACCOUNT_ERROR_INVALID_PARAMETER;
2707 pthread_mutex_lock(&account_mutex);
2709 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2711 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_db_id);
2713 rc = _account_get_record_count(query);
2715 if( _account_db_err_code() == SQLITE_PERM ){
2716 pthread_mutex_unlock(&account_mutex);
2717 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2718 return ACCOUNT_ERROR_PERMISSION_DENIED;
2722 ACCOUNT_SLOGE( "account_update_sync_status_by_id : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
2723 pthread_mutex_unlock(&account_mutex);
2724 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2727 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2729 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET sync_support=? WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
2730 hstmt = _account_prepare_query(query);
2732 _account_query_bind_int(hstmt, count, sync_status);
2734 rc = _account_query_step(hstmt);
2736 if( _account_db_err_code() == SQLITE_PERM ){
2737 pthread_mutex_unlock(&account_mutex);
2738 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2739 return ACCOUNT_ERROR_PERMISSION_DENIED;
2742 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_DB_FAILED,
2743 ("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg()));
2745 rc = _account_query_finalize(hstmt);
2746 if (rc != ACCOUNT_ERROR_NONE) {
2747 ACCOUNT_ERROR("_account_query_finalize error");
2748 pthread_mutex_unlock(&account_mutex);
2752 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_SYNC_UPDATE, account_db_id);
2753 _account_insert_delete_update_notification_send(buf);
2756 error_code = ACCOUNT_ERROR_NONE;
2759 if (hstmt != NULL) {
2760 rc = _account_query_finalize(hstmt);
2761 pthread_mutex_unlock(&account_mutex);
2762 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2766 pthread_mutex_unlock(&account_mutex);
2770 int _account_query_account_by_account_id(int pid, int account_db_id, account_s *account_record)
2772 _INFO("_account_query_account_by_account_id() start, account_db_id=[%d]", account_db_id);
2774 int error_code = ACCOUNT_ERROR_NONE;
2775 account_stmt hstmt = NULL;
2776 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2779 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2780 ACCOUNT_RETURN_VAL(account_record != NULL, {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2781 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2783 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2785 ACCOUNT_DEBUG("starting db operations");
2787 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
2788 hstmt = _account_prepare_query(query);
2789 rc = _account_db_err_code();
2790 _INFO("after _account_prepare_query, rc=[%d]", rc);
2792 if( rc == SQLITE_PERM ){
2793 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2794 return ACCOUNT_ERROR_PERMISSION_DENIED;
2797 ACCOUNT_DEBUG("before _account_query_step");
2798 rc = _account_query_step(hstmt);
2799 ACCOUNT_DEBUG("after _account_query_step returned [%d]", rc);
2800 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2802 while (rc == SQLITE_ROW) {
2803 ACCOUNT_DEBUG("before _account_convert_column_to_account");
2804 _account_convert_column_to_account(hstmt, account_record);
2805 ACCOUNT_DEBUG("after _account_convert_column_to_account");
2806 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]);
2807 rc = _account_query_step(hstmt);
2810 ACCOUNT_DEBUG("account_record->id=[%d]", account_record->id);
2812 rc = _account_query_finalize(hstmt);
2813 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2815 ACCOUNT_DEBUG("before _account_query_capability_by_account_id");
2816 _account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record);
2817 ACCOUNT_DEBUG("after _account_query_capability_by_account_id");
2819 ACCOUNT_DEBUG("before _account_query_custom_by_account_id");
2820 _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record);
2821 ACCOUNT_DEBUG("after _account_query_custom_by_account_id");
2824 error_code = ACCOUNT_ERROR_NONE;
2827 if (hstmt != NULL) {
2828 rc = _account_query_finalize(hstmt);
2829 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2835 _remove_sensitive_info_from_non_owning_account(pid, account_record);
2837 pthread_mutex_unlock(&account_mutex);
2838 ACCOUNT_DEBUG("_account_query_account_by_account_id end [%d]", error_code);
2842 GList* _account_query_account_by_user_name(int pid, const char *user_name, int *error_code)
2844 *error_code = ACCOUNT_ERROR_NONE;
2845 account_stmt hstmt = NULL;
2846 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2848 account_s *account_head = NULL;
2850 if (user_name == NULL)
2852 _ERR("USER NAME IS NULL");
2853 *error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
2857 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2859 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ?", ACCOUNT_TABLE);
2861 hstmt = _account_prepare_query(query);
2863 if (_account_db_err_code() == SQLITE_PERM)
2865 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2866 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
2870 int binding_count = 1;
2871 _account_query_bind_text(hstmt, binding_count++, user_name);
2873 rc = _account_query_step(hstmt);
2875 if (rc != SQLITE_ROW)
2877 _ERR("The record isn't found");
2878 *error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
2884 account_head = (account_s*) malloc(sizeof(account_s));
2885 if (account_head == NULL) {
2886 ACCOUNT_FATAL("malloc Failed");
2887 if (hstmt != NULL) {
2888 rc = _account_query_finalize(hstmt);
2890 if (rc != ACCOUNT_ERROR_NONE)
2892 _ERR("finalize error");
2898 *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
2901 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2903 while (rc == SQLITE_ROW) {
2904 account_s* account_record = NULL;
2906 account_record = (account_s*) malloc(sizeof(account_s));
2908 if (account_record == NULL) {
2909 ACCOUNT_FATAL("malloc Failed");
2912 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2914 _account_convert_column_to_account(hstmt, account_record);
2916 account_head->account_list = g_list_append(account_head->account_list, account_record);
2918 rc = _account_query_step(hstmt);
2922 rc = _account_query_finalize(hstmt);
2924 if (rc != ACCOUNT_ERROR_NONE)
2926 _ERR("finalize error");
2936 tmp = g_list_length(account_head->account_list);
2938 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2940 account = (account_h)iter->data;
2942 account_s *testaccount = (account_s*)account;
2944 _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
2945 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
2949 *error_code = ACCOUNT_ERROR_NONE;
2952 if (hstmt != NULL) {
2953 rc = _account_query_finalize(hstmt);
2954 if (rc != ACCOUNT_ERROR_NONE)
2956 _ERR("finalize error");
2962 pthread_mutex_unlock(&account_mutex);
2965 _remove_sensitive_info_from_non_owning_account_list(pid, account_head->account_list);
2966 GList* result = account_head->account_list;
2967 _ACCOUNT_FREE(account_head);
2974 _account_query_account_by_capability(int pid, const char* capability_type, const int capability_value, int *error_code)
2976 *error_code = ACCOUNT_ERROR_NONE;
2977 account_stmt hstmt = NULL;
2978 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2981 ACCOUNT_RETURN_VAL((capability_type != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("capability_type IS NULL"));
2983 if ((capability_value < 0) || (capability_value > ACCOUNT_CAPABILITY_ENABLED)) {
2984 ACCOUNT_SLOGE("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
2985 *error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
2989 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
2991 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2993 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=? AND value=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
2995 hstmt = _account_prepare_query(query);
2997 if( _account_db_err_code() == SQLITE_PERM ){
2998 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2999 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
3003 int binding_count = 1;
3004 _account_query_bind_text(hstmt, binding_count++, capability_type);
3005 _account_query_bind_int(hstmt, binding_count++, capability_value);
3007 rc = _account_query_step(hstmt);
3009 account_s* account_head = NULL;
3011 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3015 account_head = (account_s*) malloc(sizeof(account_s));
3016 if (account_head == NULL) {
3017 ACCOUNT_FATAL("malloc Failed");
3018 if (hstmt != NULL) {
3019 rc = _account_query_finalize(hstmt);
3020 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
3023 *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
3026 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3028 while (rc == SQLITE_ROW) {
3029 account_s* account_record = NULL;
3031 account_record = (account_s*) malloc(sizeof(account_s));
3033 if (account_record == NULL) {
3034 ACCOUNT_FATAL("malloc Failed");
3037 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3039 _account_convert_column_to_account(hstmt, account_record);
3041 account_head->account_list = g_list_append(account_head->account_list, account_record);
3043 rc = _account_query_step(hstmt);
3047 rc = _account_query_finalize(hstmt);
3048 ACCOUNT_CATCH_ERROR_P((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3054 tmp = g_list_length(account_head->account_list);
3056 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3057 account_h account = NULL;
3058 account = (account_h)iter->data;
3059 account_s* testaccount = (account_s*)account;
3061 _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3062 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3067 *error_code = ACCOUNT_ERROR_NONE;
3070 if (hstmt != NULL) {
3071 rc = _account_query_finalize(hstmt);
3072 if ( rc != ACCOUNT_ERROR_NONE ) {
3074 _ERR("finalize error");
3079 if( *error_code != ACCOUNT_ERROR_NONE && account_head ) {
3080 _account_glist_free(account_head->account_list);
3081 _ACCOUNT_FREE(account_head);
3082 account_head = NULL;
3085 pthread_mutex_unlock(&account_mutex);
3089 _remove_sensitive_info_from_non_owning_account_list(pid, account_head->account_list);
3090 GList* result = account_head->account_list;
3091 _ACCOUNT_FREE(account_head);
3097 GList* _account_query_account_by_capability_type(int pid, const char* capability_type, int *error_code)
3099 *error_code = ACCOUNT_ERROR_NONE;
3100 account_stmt hstmt = NULL;
3101 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3104 ACCOUNT_RETURN_VAL((capability_type != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("capability_type IS NULL"));
3105 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;},
3106 NULL, ("The database isn't connected."));
3108 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3110 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
3112 hstmt = _account_prepare_query(query);
3114 if( _account_db_err_code() == SQLITE_PERM ){
3115 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3116 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
3120 int binding_count = 1;
3121 _account_query_bind_text(hstmt, binding_count++, capability_type);
3123 rc = _account_query_step(hstmt);
3125 account_s* account_head = NULL;
3127 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3131 account_head = (account_s*) malloc(sizeof(account_s));
3132 if (account_head == NULL) {
3133 ACCOUNT_FATAL("malloc Failed");
3134 if (hstmt != NULL) {
3135 rc = _account_query_finalize(hstmt);
3136 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
3139 *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
3142 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3144 while (rc == SQLITE_ROW) {
3145 account_s* account_record = NULL;
3147 account_record = (account_s*) malloc(sizeof(account_s));
3149 if (account_record == NULL) {
3150 ACCOUNT_FATAL("malloc Failed");
3153 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3155 _account_convert_column_to_account(hstmt, account_record);
3157 account_head->account_list = g_list_append(account_head->account_list, account_record);
3159 rc = _account_query_step(hstmt);
3163 rc = _account_query_finalize(hstmt);
3164 ACCOUNT_CATCH_ERROR_P((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3170 tmp = g_list_length(account_head->account_list);
3172 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3173 account_s* testaccount = (account_s*)iter->data;
3175 _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3176 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3180 *error_code = ACCOUNT_ERROR_NONE;
3185 rc = _account_query_finalize(hstmt);
3186 if (rc != ACCOUNT_ERROR_NONE) {
3188 _ERR("finalize error");
3193 if( (*error_code != ACCOUNT_ERROR_NONE) && account_head ) {
3194 _account_glist_free(account_head->account_list);
3195 _ACCOUNT_FREE(account_head);
3196 account_head = NULL;
3199 pthread_mutex_unlock(&account_mutex);
3203 _remove_sensitive_info_from_non_owning_account_list(pid, account_head->account_list);
3204 GList* result = account_head->account_list;
3205 _ACCOUNT_FREE(account_head);
3211 GList* _account_query_account_by_package_name(int pid,const char* package_name, int *error_code)
3213 _INFO("_account_query_account_by_package_name");
3215 *error_code = ACCOUNT_ERROR_NONE;
3216 account_stmt hstmt = NULL;
3217 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3220 ACCOUNT_RETURN_VAL((package_name != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("PACKAGE NAME IS NULL"));
3221 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
3223 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3225 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE package_name=?", ACCOUNT_TABLE);
3227 hstmt = _account_prepare_query(query);
3229 if( _account_db_err_code() == SQLITE_PERM ){
3230 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3231 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
3235 int binding_count = 1;
3236 _account_query_bind_text(hstmt, binding_count++, package_name);
3238 rc = _account_query_step(hstmt);
3240 account_s* account_head = NULL;
3242 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.(%s)\n", package_name));
3246 account_head = (account_s*) malloc(sizeof(account_s));
3247 if (account_head == NULL) {
3248 ACCOUNT_FATAL("malloc Failed");
3249 if (hstmt != NULL) {
3250 rc = _account_query_finalize(hstmt);
3251 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
3254 *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
3257 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3259 while (rc == SQLITE_ROW) {
3260 account_s* account_record = NULL;
3262 account_record = (account_s*) malloc(sizeof(account_s));
3264 if (account_record == NULL) {
3265 ACCOUNT_FATAL("malloc Failed");
3268 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3270 _account_convert_column_to_account(hstmt, account_record);
3272 _INFO("Adding account_list");
3273 account_head->account_list = g_list_append(account_head->account_list, account_record);
3275 rc = _account_query_step(hstmt);
3279 rc = _account_query_finalize(hstmt);
3280 ACCOUNT_CATCH_ERROR_P((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3285 tmp = g_list_length(account_head->account_list);
3287 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3288 account_s* testaccount = (account_s*)iter->data;
3290 _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3291 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3294 *error_code = ACCOUNT_ERROR_NONE;
3299 rc = _account_query_finalize(hstmt);
3300 if (rc != ACCOUNT_ERROR_NONE) {
3302 _ERR("finalize error");
3307 pthread_mutex_unlock(&account_mutex);
3309 if( (*error_code != ACCOUNT_ERROR_NONE) && account_head ) {
3310 _account_glist_free(account_head->account_list);
3311 _ACCOUNT_FREE(account_head);
3312 account_head = NULL;
3315 if ((*error_code == ACCOUNT_ERROR_NONE) && account_head != NULL)
3317 _INFO("Returning account_list");
3318 _remove_sensitive_info_from_non_owning_account_list(pid,account_head->account_list);
3319 GList* result = account_head->account_list;
3320 _ACCOUNT_FREE(account_head);
3326 int _account_delete(int pid, int uid, int account_id)
3328 int error_code = ACCOUNT_ERROR_NONE;
3329 account_stmt hstmt = NULL;
3330 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3332 int ret_transaction = 0;
3333 bool is_success = FALSE;
3335 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3338 /* Check requested ID to delete */
3339 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id=%d", ACCOUNT_TABLE, account_id);
3341 count = _account_get_record_count(query);
3343 if( _account_db_err_code() == SQLITE_PERM ){
3344 pthread_mutex_unlock(&account_mutex);
3345 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3346 return ACCOUNT_ERROR_PERMISSION_DENIED;
3350 ACCOUNT_ERROR("account id(%d) is not exist. count(%d)\n", account_id, count);
3351 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3354 /* Check permission of requested appid */
3355 char* current_appid = NULL;
3356 char *package_name = NULL;
3358 current_appid = _account_get_current_appid(pid);
3360 error_code = _account_get_package_name_from_account_id(account_id, &package_name);
3362 if(error_code != ACCOUNT_ERROR_NONE){
3363 ACCOUNT_ERROR("No package name with account_id\n");
3364 _ACCOUNT_FREE(current_appid);
3365 _ACCOUNT_FREE(package_name);
3366 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3368 ACCOUNT_DEBUG( "DELETE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name);
3370 error_code = _account_check_appid_group_with_package_name(uid, current_appid, package_name);
3372 _ACCOUNT_FREE(current_appid);
3373 _ACCOUNT_FREE(package_name);
3375 if(error_code != ACCOUNT_ERROR_NONE){
3376 ACCOUNT_ERROR("No permission to delete\n");
3377 return ACCOUNT_ERROR_PERMISSION_DENIED;
3380 /* transaction control required*/
3381 ret_transaction = _account_begin_transaction();
3383 if( _account_db_err_code() == SQLITE_PERM ){
3384 pthread_mutex_unlock(&account_mutex);
3385 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3386 return ACCOUNT_ERROR_PERMISSION_DENIED;
3389 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
3390 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
3391 pthread_mutex_unlock(&account_mutex);
3392 return ACCOUNT_ERROR_DATABASE_BUSY;
3395 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3396 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3397 pthread_mutex_unlock(&account_mutex);
3398 return ret_transaction;
3401 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3402 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
3404 hstmt = _account_prepare_query(query);
3406 if( _account_db_err_code() == SQLITE_PERM ){
3407 pthread_mutex_unlock(&account_mutex);
3408 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3409 return ACCOUNT_ERROR_PERMISSION_DENIED;
3412 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3413 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3415 rc = _account_query_step(hstmt);
3416 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3418 rc = _account_query_finalize(hstmt);
3420 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3423 ACCOUNT_MEMSET(query, 0, sizeof(query));
3425 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
3427 hstmt = _account_prepare_query(query);
3428 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3429 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3431 rc = _account_query_step(hstmt);
3432 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
3434 rc = _account_query_finalize(hstmt);
3435 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3438 /* delete custom data */
3439 ACCOUNT_MEMSET(query, 0, sizeof(query));
3441 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
3443 hstmt = _account_prepare_query(query);
3445 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3446 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3448 rc = _account_query_step(hstmt);
3449 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
3451 rc = _account_query_finalize(hstmt);
3452 ACCOUNT_CATCH_ERROR(rc == ACCOUNT_ERROR_NONE, {}, rc, ("finalize error", account_id, rc));
3458 if (hstmt != NULL) {
3459 rc = _account_query_finalize(hstmt);
3460 if(rc != ACCOUNT_ERROR_NONE ){
3461 ACCOUNT_ERROR("rc (%d)", rc);
3468 ret_transaction = _account_end_transaction(is_success);
3470 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3471 ACCOUNT_ERROR("account_delete:_account_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3473 if (is_success == true) {
3475 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, account_id);
3476 _account_insert_delete_update_notification_send(buf);
3480 pthread_mutex_unlock(&account_mutex);
3486 static int _account_query_account_by_username_and_package(const char* username, const char* package_name, account_h *account)
3490 int error_code = ACCOUNT_ERROR_NONE;
3491 account_stmt hstmt = NULL;
3492 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3494 int binding_count = 1;
3496 ACCOUNT_RETURN_VAL((username != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("username IS NULL"));
3497 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name IS NULL"));
3498 ACCOUNT_RETURN_VAL((*account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
3499 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3501 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3503 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
3504 hstmt = _account_prepare_query(query);
3506 if( _account_db_err_code() == SQLITE_PERM ){
3507 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3508 return ACCOUNT_ERROR_PERMISSION_DENIED;
3511 _account_query_bind_text(hstmt, binding_count++, username);
3512 _account_query_bind_text(hstmt, binding_count++, package_name);
3514 rc = _account_query_step(hstmt);
3515 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3517 account_s *account_record = (account_s *)(*account);
3519 while (rc == SQLITE_ROW) {
3520 _account_convert_column_to_account(hstmt, account_record);
3521 rc = _account_query_step(hstmt);
3524 rc = _account_query_finalize(hstmt);
3525 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3526 _account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record);
3527 _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record);
3530 error_code = ACCOUNT_ERROR_NONE;
3533 if (hstmt != NULL) {
3534 rc = _account_query_finalize(hstmt);
3535 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3539 pthread_mutex_unlock(&account_mutex);
3543 int _account_create(account_h *account)
3546 ACCOUNT_SLOGE("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__);
3547 return ACCOUNT_ERROR_INVALID_PARAMETER;
3550 account_s *data = (account_s*)malloc(sizeof(account_s));
3553 ACCOUNT_FATAL("Memory Allocation Failed");
3554 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3556 ACCOUNT_MEMSET(data, 0, sizeof(account_s));
3558 /*Setting account as visible by default*/
3559 data->secret = ACCOUNT_SECRECY_VISIBLE;
3561 /*Setting account as not supporting sync by default*/
3562 data->sync_support = ACCOUNT_SYNC_NOT_SUPPORT;
3564 *account = (account_h)data;
3566 return ACCOUNT_ERROR_NONE;
3569 int _account_destroy(account_h account)
3571 account_s *data = (account_s*)account;
3573 ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account handle is null!"));
3575 _account_free_account_items(data);
3576 _ACCOUNT_FREE(data);
3578 return ACCOUNT_ERROR_NONE;
3581 int _account_get_account_id(account_s* account, int *account_id)
3584 return ACCOUNT_ERROR_INVALID_PARAMETER;
3587 return ACCOUNT_ERROR_INVALID_PARAMETER;
3590 *account_id = account->id;
3592 return ACCOUNT_ERROR_NONE;
3595 int _account_delete_from_db_by_user_name(int pid, int uid, const char *user_name, const char *package_name)
3597 _INFO("[%s][%s]", user_name, package_name);
3599 int error_code = ACCOUNT_ERROR_NONE;
3600 account_stmt hstmt = NULL;
3601 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3603 int ret_transaction = 0;
3604 bool is_success = FALSE;
3605 account_h account = NULL;
3606 int binding_count = 1;
3607 int account_id = -1;
3609 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is null!"));
3610 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
3611 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3613 /* Check permission of requested appid */
3614 char* current_appid = NULL;
3615 char* package_name_temp = NULL;
3617 current_appid = _account_get_current_appid(pid);
3619 package_name_temp = _account_get_text(package_name);
3621 ACCOUNT_DEBUG( "DELETE:user_name[%s],current_appid[%s], package_name[%s]", user_name, current_appid, package_name_temp);
3623 error_code = _account_check_appid_group_with_package_name(uid, current_appid, package_name_temp);
3625 _ACCOUNT_FREE(current_appid);
3626 _ACCOUNT_FREE(package_name_temp);
3628 if(error_code != ACCOUNT_ERROR_NONE){
3629 ACCOUNT_ERROR("No permission to delete\n");
3630 return ACCOUNT_ERROR_PERMISSION_DENIED;
3633 rc = _account_create(&account);
3634 rc = _account_query_account_by_username_and_package(user_name, package_name, &account);
3638 if( _account_db_err_code() == SQLITE_PERM )
3640 _account_destroy(account);
3641 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3642 return ACCOUNT_ERROR_PERMISSION_DENIED;
3646 account_s* account_data = (account_s*)account;
3648 rc = _account_get_account_id(account_data, &account_id);
3650 rc = _account_destroy(account);
3652 /* transaction control required*/
3653 ret_transaction = _account_begin_transaction();
3655 if( _account_db_err_code() == SQLITE_PERM )
3657 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3658 pthread_mutex_unlock(&account_mutex);
3659 return ACCOUNT_ERROR_PERMISSION_DENIED;
3663 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY )
3665 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
3666 pthread_mutex_unlock(&account_mutex);
3667 return ACCOUNT_ERROR_DATABASE_BUSY;
3669 else if (ret_transaction != ACCOUNT_ERROR_NONE) {
3670 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3671 pthread_mutex_unlock(&account_mutex);
3672 return ret_transaction;
3675 /* delete custom data */
3676 ACCOUNT_MEMSET(query, 0, sizeof(query));
3677 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = ?", ACCOUNT_CUSTOM_TABLE);
3679 hstmt = _account_prepare_query(query);
3681 if( _account_db_err_code() == SQLITE_PERM ){
3682 _account_end_transaction(FALSE);
3683 pthread_mutex_unlock(&account_mutex);
3684 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3685 return ACCOUNT_ERROR_PERMISSION_DENIED;
3688 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3689 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3691 _account_query_bind_int(hstmt, binding_count++, account_id);
3693 rc = _account_query_step(hstmt);
3694 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3696 rc = _account_query_finalize(hstmt);
3697 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3700 /* delete capability */
3701 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", CAPABILITY_TABLE);
3703 hstmt = _account_prepare_query(query);
3705 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3706 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3709 _account_query_bind_text(hstmt, binding_count++, user_name);
3710 _account_query_bind_text(hstmt, binding_count++, package_name);
3712 rc = _account_query_step(hstmt);
3713 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3715 rc = _account_query_finalize(hstmt);
3716 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3719 ACCOUNT_MEMSET(query, 0, sizeof(query));
3722 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
3724 hstmt = _account_prepare_query(query);
3725 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3726 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3730 _account_query_bind_text(hstmt, binding_count++, user_name);
3731 _account_query_bind_text(hstmt, binding_count++, package_name);
3733 rc = _account_query_step(hstmt);
3734 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));
3736 rc = _account_query_finalize(hstmt);
3737 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3743 if (hstmt != NULL) {
3744 rc = _account_query_finalize(hstmt);
3745 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3749 ret_transaction = _account_end_transaction(is_success);
3751 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3752 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3754 if (is_success == true) {
3756 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, account_id);
3757 _account_insert_delete_update_notification_send(buf);
3761 pthread_mutex_unlock(&account_mutex);
3766 int _account_delete_from_db_by_package_name(int pid, int uid, const char *package_name, gboolean permission)
3768 _INFO("_account_delete_from_db_by_package_name");
3769 int error_code = ACCOUNT_ERROR_NONE;
3770 account_stmt hstmt = NULL;
3771 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3773 int ret_transaction = 0;
3774 bool is_success = FALSE;
3775 int binding_count = 1;
3776 GSList *account_id_list = NULL;
3779 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
3780 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3782 // It only needs list of ids, does not need to query sensitive info. So sending 0
3783 GList* account_list_temp = _account_query_account_by_package_name(getpid(), package_name, &ret);
3784 if( _account_db_err_code() == SQLITE_PERM ){
3785 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3786 return ACCOUNT_ERROR_PERMISSION_DENIED;
3789 if(ret != ACCOUNT_ERROR_NONE){
3790 _ERR("_account_query_account_by_package_name failed ret=[%d]", ret);
3794 /* Check permission of requested appid */
3796 char* current_appid = NULL;
3797 char* package_name_temp = NULL;
3799 current_appid = _account_get_current_appid(pid);
3801 package_name_temp = _account_get_text(package_name);
3803 ACCOUNT_DEBUG( "DELETE: current_appid[%s], package_name[%s]", current_appid, package_name_temp);
3805 error_code = _account_check_appid_group_with_package_name(uid, current_appid, package_name_temp);
3807 _ACCOUNT_FREE(current_appid);
3808 _ACCOUNT_FREE(package_name_temp);
3810 if(error_code != ACCOUNT_ERROR_NONE){
3811 ACCOUNT_ERROR("No permission to delete\n");
3812 _account_glist_free(account_list_temp);
3813 return ACCOUNT_ERROR_PERMISSION_DENIED;
3817 GList *account_list = g_list_first(account_list_temp);
3818 _INFO("account_list_temp length=[%d]",g_list_length(account_list));
3821 for (iter = account_list; iter != NULL; iter = g_list_next(iter))
3823 _INFO("iterating account_list");
3824 account_s *account = NULL;
3825 _INFO("Before iter->data");
3826 account = (account_s*)iter->data;
3827 _INFO("After iter->data");
3828 if (account != NULL)
3830 char id[256] = {0, };
3832 ACCOUNT_MEMSET(id, 0, 256);
3834 ACCOUNT_SNPRINTF(id, 256, "%d", account->id);
3836 _INFO("Adding account id [%s]", id);
3837 account_id_list = g_slist_append(account_id_list, g_strdup(id));
3841 _account_glist_free(account_list_temp);
3843 /* transaction control required*/
3844 ret_transaction = _account_begin_transaction();
3846 if( _account_db_err_code() == SQLITE_PERM ){
3847 pthread_mutex_unlock(&account_mutex);
3848 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3849 return ACCOUNT_ERROR_PERMISSION_DENIED;
3852 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
3853 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
3854 pthread_mutex_unlock(&account_mutex);
3855 return ACCOUNT_ERROR_DATABASE_BUSY;
3856 }else if (ret_transaction != ACCOUNT_ERROR_NONE) {
3857 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3858 pthread_mutex_unlock(&account_mutex);
3859 return ret_transaction;
3862 /* delete custom table */
3863 ACCOUNT_MEMSET(query, 0, sizeof(query));
3864 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", ACCOUNT_CUSTOM_TABLE);
3866 hstmt = _account_prepare_query(query);
3868 if( _account_db_err_code() == SQLITE_PERM ){
3869 _account_end_transaction(FALSE);
3870 pthread_mutex_unlock(&account_mutex);
3871 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3872 return ACCOUNT_ERROR_PERMISSION_DENIED;
3875 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3876 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3879 _account_query_bind_text(hstmt, binding_count++, package_name);
3881 rc = _account_query_step(hstmt);
3882 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3884 rc = _account_query_finalize(hstmt);
3885 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3888 /* delete capability table */
3889 ACCOUNT_MEMSET(query, 0, sizeof(query));
3890 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", CAPABILITY_TABLE);
3892 hstmt = _account_prepare_query(query);
3894 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3895 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3898 _account_query_bind_text(hstmt, binding_count++, package_name);
3900 rc = _account_query_step(hstmt);
3901 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3903 rc = _account_query_finalize(hstmt);
3904 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3907 /* delete account table */
3908 ACCOUNT_MEMSET(query, 0, sizeof(query));
3910 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", ACCOUNT_TABLE);
3912 hstmt = _account_prepare_query(query);
3913 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3914 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3917 _account_query_bind_text(hstmt, binding_count++, package_name);
3919 rc = _account_query_step(hstmt);
3920 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));
3922 rc = _account_query_finalize(hstmt);
3923 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3929 if (hstmt != NULL) {
3930 rc = _account_query_finalize(hstmt);
3931 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3935 ret_transaction = _account_end_transaction(is_success);
3937 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3938 ACCOUNT_ERROR("account_delete:_account_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3940 if (is_success == true) {
3941 GSList* gs_iter = NULL;
3942 for (gs_iter = account_id_list; gs_iter != NULL; gs_iter = g_slist_next(gs_iter)) {
3943 char* p_tmpid = NULL;
3944 p_tmpid = (char*)gs_iter->data;
3946 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%s", ACCOUNT_NOTI_NAME_DELETE, p_tmpid);
3947 ACCOUNT_SLOGD("%s", buf);
3948 _account_insert_delete_update_notification_send(buf);
3949 _ACCOUNT_FREE(p_tmpid);
3951 g_slist_free(account_id_list);
3955 pthread_mutex_unlock(&account_mutex);
3957 _INFO("_account_delete_from_db_by_package_name end");
3961 int _account_get_total_count_from_db(gboolean include_hidden, int *count)
3964 ACCOUNT_SLOGE("(%s)-(%d) count is NULL.\n", __FUNCTION__, __LINE__);
3965 return ACCOUNT_ERROR_INVALID_PARAMETER;
3969 ACCOUNT_ERROR("DB is not opened\n");
3970 return ACCOUNT_ERROR_DB_NOT_OPENED;
3973 char query[1024] = {0, };
3974 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3978 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s", ACCOUNT_TABLE);
3982 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s where secret = %d", ACCOUNT_TABLE, ACCOUNT_SECRECY_VISIBLE);
3985 *count = _account_get_record_count(query);
3987 if( _account_db_err_code() == SQLITE_PERM ){
3988 pthread_mutex_unlock(&account_mutex);
3989 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3990 return ACCOUNT_ERROR_PERMISSION_DENIED;
3995 account_stmt pStmt = NULL;
3997 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
3998 if (SQLITE_OK != rc) {
3999 ACCOUNT_SLOGE("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg());
4000 sqlite3_finalize(pStmt);
4001 return ACCOUNT_ERROR_DB_FAILED;
4004 rc = sqlite3_step(pStmt);
4005 if (SQLITE_ROW != rc) {
4006 ACCOUNT_ERROR("[ERROR] sqlite3_step() failed\n");
4007 sqlite3_finalize(pStmt);
4008 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4011 ncount = sqlite3_column_int(pStmt, 0);
4015 sqlite3_finalize(pStmt);
4018 ACCOUNT_ERROR("[ERROR] Number of account : %d, End", ncount);
4019 return ACCOUNT_ERROR_DB_FAILED;
4022 return ACCOUNT_ERROR_NONE;
4025 int account_type_create(account_type_h *account_type)
4027 if (!account_type) {
4028 ACCOUNT_SLOGE("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
4029 return ACCOUNT_ERROR_INVALID_PARAMETER;
4032 account_type_s *data = (account_type_s*)malloc(sizeof(account_type_s));
4035 ACCOUNT_ERROR("Memory Allocation Failed");
4036 return ACCOUNT_ERROR_OUT_OF_MEMORY;
4039 ACCOUNT_MEMSET(data, 0, sizeof(account_type_s));
4041 *account_type = (account_type_h)data;
4043 return ACCOUNT_ERROR_NONE;
4046 int account_type_destroy(account_type_h account_type)
4048 account_type_s *data = (account_type_s*)account_type;
4050 ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account type handle is null!"));
4052 _account_type_free_account_type_items(data);
4053 _ACCOUNT_FREE(data);
4055 return ACCOUNT_ERROR_NONE;
4058 //app_id mandatory field
4059 int account_type_set_app_id(account_type_h account_type, const char *app_id)
4061 if (!account_type) {
4062 ACCOUNT_SLOGE("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
4063 return ACCOUNT_ERROR_INVALID_PARAMETER;
4067 ACCOUNT_SLOGE("(%s)-(%d) app_id is NULL.\n", __FUNCTION__, __LINE__);
4068 return ACCOUNT_ERROR_INVALID_PARAMETER;
4071 account_type_s *data = (account_type_s*)account_type;
4073 _ACCOUNT_FREE(data->app_id);
4074 data->app_id = _account_get_text(app_id);
4076 return ACCOUNT_ERROR_NONE;
4079 //service_provider_id mandatory field
4080 int account_type_set_service_provider_id(account_type_h account_type, const char *service_provider_id)
4082 if (!account_type) {
4083 return ACCOUNT_ERROR_INVALID_PARAMETER;
4086 if (!service_provider_id) {
4087 return ACCOUNT_ERROR_INVALID_PARAMETER;
4090 account_type_s *data = (account_type_s*)account_type;
4092 _ACCOUNT_FREE(data->service_provider_id);
4093 data->service_provider_id = _account_get_text(service_provider_id);
4095 return ACCOUNT_ERROR_NONE;
4098 int account_type_set_icon_path(account_type_h account_type, const char *icon_path)
4100 if (!account_type) {
4101 return ACCOUNT_ERROR_INVALID_PARAMETER;
4105 return ACCOUNT_ERROR_INVALID_PARAMETER;
4108 account_type_s *data = (account_type_s*)account_type;
4110 _ACCOUNT_FREE(data->icon_path);
4111 data->icon_path = _account_get_text(icon_path);
4113 return ACCOUNT_ERROR_NONE;
4116 int account_type_set_small_icon_path(account_type_h account_type, const char *small_icon_path)
4118 if (!account_type) {
4119 return ACCOUNT_ERROR_INVALID_PARAMETER;
4122 if (!small_icon_path) {
4123 return ACCOUNT_ERROR_INVALID_PARAMETER;
4126 account_type_s *data = (account_type_s*)account_type;
4128 _ACCOUNT_FREE(data->small_icon_path);
4129 data->small_icon_path = _account_get_text(small_icon_path);
4131 return ACCOUNT_ERROR_NONE;
4134 int account_type_set_multiple_account_support(account_type_h account_type, const bool multiple_account_support)
4136 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__));
4138 account_type_s *data = (account_type_s*)account_type;
4140 data->multiple_account_support = multiple_account_support;
4142 return ACCOUNT_ERROR_NONE;
4146 int account_type_set_label(account_type_h account_type, const char* label, const char* locale)
4148 if (!account_type) {
4149 ACCOUNT_SLOGE("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
4150 return ACCOUNT_ERROR_INVALID_PARAMETER;
4153 if(!label || !locale) {
4154 return ACCOUNT_ERROR_INVALID_PARAMETER;
4157 account_type_s *data = (account_type_s*)account_type;
4158 label_s *label_data = (label_s*)malloc(sizeof(label_s));
4160 if (label_data == NULL) {
4161 return ACCOUNT_ERROR_OUT_OF_MEMORY;
4163 ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
4165 label_data->label = _account_get_text(label);
4166 label_data->locale = _account_get_text(locale);
4168 data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
4170 return ACCOUNT_ERROR_NONE;
4173 int account_type_get_app_id(account_type_h account_type, char **app_id)
4175 if (!account_type) {
4176 return ACCOUNT_ERROR_INVALID_PARAMETER;
4180 return ACCOUNT_ERROR_INVALID_PARAMETER;
4183 account_type_s *data = (account_type_s*)account_type;
4186 *app_id = _account_get_text(data->app_id);
4188 return ACCOUNT_ERROR_NONE;
4191 int account_type_get_service_provider_id(account_type_h account_type, char **service_provider_id)
4193 if (!account_type) {
4194 return ACCOUNT_ERROR_INVALID_PARAMETER;
4197 if (!service_provider_id) {
4198 return ACCOUNT_ERROR_INVALID_PARAMETER;
4201 account_type_s *data = (account_type_s*)account_type;
4203 (*service_provider_id) = NULL;
4204 *service_provider_id = _account_get_text(data->service_provider_id);
4206 return ACCOUNT_ERROR_NONE;
4209 int account_type_get_icon_path(account_type_h account_type, char **icon_path)
4211 if (!account_type) {
4212 return ACCOUNT_ERROR_INVALID_PARAMETER;
4216 return ACCOUNT_ERROR_INVALID_PARAMETER;
4219 account_type_s *data = (account_type_s*)account_type;
4221 (*icon_path) = NULL;
4222 *icon_path = _account_get_text(data->icon_path);
4224 return ACCOUNT_ERROR_NONE;
4227 int account_type_get_small_icon_path(account_type_h account_type, char **small_icon_path)
4229 if (!account_type) {
4230 return ACCOUNT_ERROR_INVALID_PARAMETER;
4233 if (!small_icon_path) {
4234 return ACCOUNT_ERROR_INVALID_PARAMETER;
4237 account_type_s *data = (account_type_s*)account_type;
4239 (*small_icon_path) = NULL;
4240 *small_icon_path = _account_get_text(data->small_icon_path);
4242 return ACCOUNT_ERROR_NONE;
4245 int account_type_get_multiple_account_support(account_type_h account_type, int *multiple_account_support)
4247 if (!account_type) {
4248 return ACCOUNT_ERROR_INVALID_PARAMETER;
4250 if (!multiple_account_support) {
4251 return ACCOUNT_ERROR_INVALID_PARAMETER;
4254 account_type_s *data = (account_type_s*)account_type;
4256 *multiple_account_support = data->multiple_account_support;
4258 return ACCOUNT_ERROR_NONE;
4261 int account_type_get_label_by_locale(account_type_h account_type, const char* locale, char** label)
4263 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4264 ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID PARAMETER"));
4267 account_type_s *data = (account_type_s*)account_type;
4269 for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) {
4270 label_s *label_data = NULL;
4272 label_data = (label_s*)iter->data;
4276 if(!strcmp(locale, label_data->locale)) {
4277 *label = _account_get_text(label_data->label);
4278 return ACCOUNT_ERROR_NONE;
4282 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4285 int account_type_get_label(account_type_h account_type, account_label_cb callback, void *user_data)
4287 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4288 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4291 account_type_s *data = (account_type_s*)account_type;
4293 for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) {
4294 label_s *label_data = NULL;
4296 label_data = (label_s*)iter->data;
4298 if(callback(label_data->app_id, label_data->label, label_data->locale, user_data)!=TRUE) {
4299 ACCOUNT_DEBUG("Callback func returs FALSE, its iteration is stopped!!!!\n");
4300 return ACCOUNT_ERROR_NONE;
4304 return ACCOUNT_ERROR_NONE;
4307 static gboolean _account_type_check_duplicated(account_type_s *data)
4309 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4312 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4314 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId='%s'"
4315 , ACCOUNT_TYPE_TABLE, data->app_id);
4317 count = _account_get_record_count(query);
4325 static int _account_type_convert_account_to_sql(account_type_s *account_type, account_stmt hstmt, char *sql_value)
4331 /*Caution : Keep insert query orders.*/
4334 _account_query_bind_text(hstmt, count++, (char*)account_type->app_id);
4336 /* 2. service provider id*/
4337 _account_query_bind_text(hstmt, count++, (char*)account_type->service_provider_id);
4340 _account_query_bind_text(hstmt, count++, (char*)account_type->icon_path);
4342 /* 4. small icon path*/
4343 _account_query_bind_text(hstmt, count++, (char*)account_type->small_icon_path);
4345 /* 5. multiple accont support*/
4346 _account_query_bind_int(hstmt, count++, account_type->multiple_account_support);
4354 static int _account_type_execute_insert_query(account_type_s *account_type)
4359 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4360 int error_code = ACCOUNT_ERROR_NONE;
4361 account_stmt hstmt = NULL;
4363 /* check mandatory field */
4364 // app id & service provider id
4365 if (!account_type->app_id) {
4366 return ACCOUNT_ERROR_INVALID_PARAMETER;
4369 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4370 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s( AppId, ServiceProviderId , IconPath , SmallIconPath , MultipleAccountSupport ) values "
4371 "(?, ?, ?, ?, ?)", ACCOUNT_TYPE_TABLE);
4374 hstmt = _account_prepare_query(query);
4377 if( _account_db_err_code() == SQLITE_PERM ){
4378 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4379 return ACCOUNT_ERROR_PERMISSION_DENIED;
4380 } else if( _account_db_err_code() == SQLITE_BUSY ){
4381 ACCOUNT_ERROR( "Database Busy(%s)", _account_db_err_msg());
4382 return ACCOUNT_ERROR_DATABASE_BUSY;
4385 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4388 _account_type_convert_account_to_sql(account_type, hstmt, query);
4391 rc = _account_query_step(hstmt);
4392 if (rc == SQLITE_BUSY) {
4393 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4394 error_code = ACCOUNT_ERROR_DATABASE_BUSY;
4395 } else if (rc != SQLITE_DONE) {
4396 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4397 error_code = ACCOUNT_ERROR_DB_FAILED;
4401 rc = _account_query_finalize(hstmt);
4402 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4409 static int _account_type_insert_label(account_type_s *account_type)
4412 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4413 account_stmt hstmt = NULL;
4415 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4417 if (g_slist_length( account_type->label_list)==0) {
4418 ACCOUNT_ERROR( "_account_type_insert_label, no label\n");
4419 return ACCOUNT_ERROR_NONE;
4422 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId = '%s'", ACCOUNT_TYPE_TABLE, account_type->app_id);
4424 rc = _account_get_record_count(query);
4426 if( _account_db_err_code() == SQLITE_PERM ){
4427 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4428 return ACCOUNT_ERROR_PERMISSION_DENIED;
4432 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4438 for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
4441 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4442 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
4443 "(?, ?, ?) ", LABEL_TABLE);
4445 hstmt = _account_prepare_query(query);
4447 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4449 label_s* label_data = NULL;
4450 label_data = (label_s*)iter->data;
4452 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4453 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4454 ret = _account_query_bind_text(hstmt, count++, label_data->label);
4455 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4456 ret = _account_query_bind_text(hstmt, count++, (char*)label_data->locale);
4457 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4459 rc = _account_query_step(hstmt);
4461 if (rc != SQLITE_DONE) {
4462 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4466 rc = _account_query_finalize(hstmt);
4467 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4472 return ACCOUNT_ERROR_NONE;
4475 static void _account_type_convert_column_to_provider_feature(account_stmt hstmt, provider_feature_s *feature_record)
4477 const char *textbuf = NULL;
4479 textbuf = _account_query_table_column_text(hstmt, PROVIDER_FEATURE_FIELD_APP_ID);
4480 _account_db_data_to_text(textbuf, &(feature_record->app_id));
4482 textbuf = _account_query_table_column_text(hstmt, PROVIDER_FEATURE_FIELD_KEY);
4483 _account_db_data_to_text(textbuf, &(feature_record->key));
4487 GSList* _account_type_query_provider_feature_by_app_id(const char* app_id, int *error_code)
4489 _INFO("_account_type_query_provider_feature_by_app_id");
4490 *error_code = ACCOUNT_ERROR_NONE;
4491 account_stmt hstmt = NULL;
4492 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4493 int rc = 0, binding_count = 1;
4494 GSList* feature_list = NULL;
4496 ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
4497 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
4499 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4501 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
4502 _INFO("account query=[%s]", query);
4504 hstmt = _account_prepare_query(query);
4506 if( _account_db_err_code() == SQLITE_PERM ){
4507 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4508 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
4512 _account_query_bind_text(hstmt, binding_count++, app_id);
4514 rc = _account_query_step(hstmt);
4515 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {*error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;}, NULL, ("The record isn't found.\n"));
4517 provider_feature_s* feature_record = NULL;
4519 while (rc == SQLITE_ROW) {
4521 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
4523 if (feature_record == NULL) {
4524 ACCOUNT_FATAL("malloc Failed");
4528 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
4530 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
4532 _INFO("Adding account feature_list");
4533 feature_list = g_slist_append(feature_list, feature_record);
4535 rc = _account_query_step(hstmt);
4538 rc = _account_query_finalize(hstmt);
4539 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("account finalize error"));
4542 *error_code = ACCOUNT_ERROR_NONE;
4545 if (hstmt != NULL) {
4546 rc = _account_query_finalize(hstmt);
4547 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("account finalize error"));
4551 pthread_mutex_unlock(&account_mutex);
4553 _INFO("Returning account feature_list");
4554 return feature_list;
4557 int account_type_query_provider_feature_by_app_id(provider_feature_cb callback, const char* app_id, void *user_data )
4559 int error_code = ACCOUNT_ERROR_NONE;
4560 account_stmt hstmt = NULL;
4561 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4562 int rc = 0, binding_count = 1;
4564 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4565 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4566 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4568 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4570 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
4571 hstmt = _account_prepare_query(query);
4573 if( _account_db_err_code() == SQLITE_PERM ){
4574 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4575 return ACCOUNT_ERROR_PERMISSION_DENIED;
4578 _account_query_bind_text(hstmt, binding_count++, app_id);
4580 rc = _account_query_step(hstmt);
4581 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4583 provider_feature_s* feature_record = NULL;
4585 while (rc == SQLITE_ROW) {
4586 bool cb_ret = FALSE;
4587 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
4589 if (feature_record == NULL) {
4590 ACCOUNT_FATAL("malloc Failed");
4594 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
4596 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
4598 cb_ret = callback(feature_record->app_id, feature_record->key, user_data);
4600 _account_type_free_feature_items(feature_record);
4601 _ACCOUNT_FREE(feature_record);
4603 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
4605 rc = _account_query_step(hstmt);
4608 rc = _account_query_finalize(hstmt);
4609 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4612 error_code = ACCOUNT_ERROR_NONE;
4615 if (hstmt != NULL) {
4616 rc = _account_query_finalize(hstmt);
4617 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4621 pthread_mutex_unlock(&account_mutex);
4625 bool _account_type_query_supported_feature(const char* app_id, const char* capability, int *error_code)
4627 _INFO("_account_type_query_supported_feature start");
4629 *error_code = ACCOUNT_ERROR_NONE;
4631 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4632 int record_count = 0;
4634 if (app_id == NULL || capability == NULL)
4636 *error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
4640 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
4642 record_count = _account_get_record_count(query);
4644 if( _account_db_err_code() == SQLITE_PERM ){
4645 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4646 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
4650 if (record_count <= 0)
4652 *error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
4656 _INFO("_account_type_query_supported_feature end");
4662 int account_type_get_provider_feature_all(account_type_h account_type, provider_feature_cb callback, void* user_data)
4664 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4665 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4668 account_type_s *data = (account_type_s*)account_type;
4670 for (iter = data->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4671 provider_feature_s *feature_data = NULL;
4673 feature_data = (provider_feature_s*)iter->data;
4675 if(callback(feature_data->app_id, feature_data->key, user_data)!=TRUE) {
4676 ACCOUNT_DEBUG("Callback func returs FALSE, its iteration is stopped!!!!\n");
4677 return ACCOUNT_ERROR_NONE;
4681 return ACCOUNT_ERROR_NONE;
4684 int account_type_set_provider_feature(account_type_h account_type, const char* provider_feature)
4686 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("account type handle is null"));
4687 ACCOUNT_RETURN_VAL((provider_feature != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("provider_feature is null"));
4689 account_type_s *data = (account_type_s*)account_type;
4691 GSList *iter = NULL;
4692 bool b_is_new = TRUE;
4694 for(iter = data->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4695 provider_feature_s *feature_data = NULL;
4696 feature_data = (provider_feature_s*)iter->data;
4698 if(!strcmp(feature_data->key, provider_feature)) {
4705 provider_feature_s* feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
4707 if (feature_data == NULL)
4708 return ACCOUNT_ERROR_OUT_OF_MEMORY;
4709 ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
4711 feature_data->key = _account_get_text(provider_feature);
4712 data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
4715 return ACCOUNT_ERROR_NONE;
4718 static int _account_type_insert_provider_feature(account_type_s *account_type, const char* app_id)
4721 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4722 account_stmt hstmt = NULL;
4724 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4725 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4727 if (g_slist_length( account_type->provider_feature_list)==0) {
4728 ACCOUNT_ERROR( "no capability\n");
4729 return ACCOUNT_ERROR_NONE;
4732 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId='%s'", ACCOUNT_TYPE_TABLE, app_id);
4734 rc = _account_get_record_count(query);
4736 if( _account_db_err_code() == SQLITE_PERM ){
4737 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4738 return ACCOUNT_ERROR_PERMISSION_DENIED;
4742 ACCOUNT_SLOGI( "related account type item is not existed rc=%d , %s", rc, _account_db_err_msg());
4743 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4750 for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4753 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4754 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
4755 "(?, ?) ", PROVIDER_FEATURE_TABLE);
4757 hstmt = _account_prepare_query(query);
4759 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4761 provider_feature_s* feature_data = NULL;
4762 feature_data = (provider_feature_s*)iter->data;
4764 ret = _account_query_bind_text(hstmt, count++, app_id);
4765 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4766 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
4767 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
4769 rc = _account_query_step(hstmt);
4771 if (rc != SQLITE_DONE) {
4772 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4776 rc = _account_query_finalize(hstmt);
4777 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4782 return ACCOUNT_ERROR_NONE;
4785 int _account_type_insert_to_db(account_type_s* account_type, int* account_type_id)
4789 int error_code = ACCOUNT_ERROR_NONE, ret_transaction = 0;
4791 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4792 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE HANDLE IS NULL"));
4793 ACCOUNT_RETURN_VAL((account_type_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE ID POINTER IS NULL"));
4795 account_type_s *data = (account_type_s*)account_type;
4797 pthread_mutex_lock(&account_mutex);
4800 /* transaction control required*/
4801 ret_transaction = _account_begin_transaction();
4805 if( _account_db_err_code() == SQLITE_PERM ){
4806 pthread_mutex_unlock(&account_mutex);
4807 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4808 return ACCOUNT_ERROR_PERMISSION_DENIED;
4812 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
4813 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
4814 pthread_mutex_unlock(&account_mutex);
4815 return ACCOUNT_ERROR_DATABASE_BUSY;
4816 } else if (ret_transaction != ACCOUNT_ERROR_NONE) {
4817 ACCOUNT_ERROR("_account_begin_transaction fail %d\n", ret_transaction);
4818 pthread_mutex_unlock(&account_mutex);
4819 return ret_transaction;
4823 if (_account_type_check_duplicated(data)) {
4825 ret_transaction = _account_end_transaction(FALSE);
4826 ACCOUNT_ERROR("Duplicated, rollback insert query(%x)!!!!\n", ret_transaction);
4827 *account_type_id = -1;
4828 pthread_mutex_unlock(&account_mutex);
4829 return ACCOUNT_ERROR_DUPLICATED;
4832 *account_type_id = _account_get_next_sequence(ACCOUNT_TYPE_TABLE);
4834 error_code = _account_type_execute_insert_query(data);
4836 if (error_code != ACCOUNT_ERROR_NONE){
4837 error_code = ACCOUNT_ERROR_DUPLICATED;
4838 ret_transaction = _account_end_transaction(FALSE);
4839 ACCOUNT_ERROR("Insert fail, rollback insert query(%x)!!!!\n", ret_transaction);
4840 *account_type_id = -1;
4841 pthread_mutex_unlock(&account_mutex);
4847 error_code = _account_type_insert_provider_feature(data, data->app_id);
4848 if(error_code != ACCOUNT_ERROR_NONE) {
4850 ret_transaction = _account_end_transaction(FALSE);
4851 ACCOUNT_ERROR("Insert provider feature fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction);
4852 pthread_mutex_unlock(&account_mutex);
4856 error_code = _account_type_insert_label(data);
4857 if(error_code != ACCOUNT_ERROR_NONE) {
4859 ret_transaction = _account_end_transaction(FALSE);
4860 ACCOUNT_ERROR("Insert label fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction);
4861 pthread_mutex_unlock(&account_mutex);
4865 ret_transaction = _account_end_transaction(TRUE);
4867 pthread_mutex_unlock(&account_mutex);
4870 return ACCOUNT_ERROR_NONE;
4873 static int _account_type_update_provider_feature(account_type_s *account_type, const char* app_id)
4876 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4877 account_stmt hstmt = NULL;
4879 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4881 if (g_slist_length( account_type->provider_feature_list)==0) {
4882 ACCOUNT_ERROR( "no feature\n");
4883 return ACCOUNT_ERROR_NONE;
4886 ACCOUNT_DEBUG( "app id", app_id);
4888 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4890 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id=? ", PROVIDER_FEATURE_TABLE);
4891 hstmt = _account_prepare_query(query);
4893 if( _account_db_err_code() == SQLITE_PERM ){
4894 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4895 return ACCOUNT_ERROR_PERMISSION_DENIED;
4899 _account_query_bind_text(hstmt, count++, app_id);
4900 rc = _account_query_step(hstmt);
4902 if (rc != SQLITE_DONE) {
4903 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4904 return ACCOUNT_ERROR_DB_FAILED;
4906 rc = _account_query_finalize(hstmt);
4907 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4912 for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4915 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4916 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
4917 "(?, ?) ", PROVIDER_FEATURE_TABLE);
4919 hstmt = _account_prepare_query(query);
4921 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4923 provider_feature_s* feature_data = NULL;
4924 feature_data = (provider_feature_s*)iter->data;
4926 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4927 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4928 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
4929 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4931 rc = _account_query_step(hstmt);
4933 if (rc != SQLITE_DONE) {
4934 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4937 rc = _account_query_finalize(hstmt);
4938 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4942 return ACCOUNT_ERROR_NONE;
4945 static int _account_type_update_label(account_type_s *account_type, const char* app_id)
4948 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4949 account_stmt hstmt = NULL;
4951 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4953 if (g_slist_length( account_type->label_list)==0) {
4954 return ACCOUNT_ERROR_NONE;
4957 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4959 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId=? ", LABEL_TABLE);
4960 hstmt = _account_prepare_query(query);
4962 if( _account_db_err_code() == SQLITE_PERM ){
4963 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4964 return ACCOUNT_ERROR_PERMISSION_DENIED;
4968 _account_query_bind_text(hstmt, count++, app_id);
4969 rc = _account_query_step(hstmt);
4971 if (rc != SQLITE_DONE) {
4972 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4973 return ACCOUNT_ERROR_DB_FAILED;
4975 rc = _account_query_finalize(hstmt);
4976 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4981 for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
4984 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4985 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
4986 "(?, ?, ?) ", LABEL_TABLE);
4988 hstmt = _account_prepare_query(query);
4990 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4992 label_s* label_data = NULL;
4993 label_data = (label_s*)iter->data;
4995 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4996 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4997 ret = _account_query_bind_text(hstmt, count++, label_data->label);
4998 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4999 ret = _account_query_bind_text(hstmt, count++, label_data->locale);
5000 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5002 rc = _account_query_step(hstmt);
5004 if (rc != SQLITE_DONE) {
5005 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5008 rc = _account_query_finalize(hstmt);
5009 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5013 return ACCOUNT_ERROR_NONE;
5017 static int _account_type_update_account(account_type_s *account_type, const char* app_id)
5019 int rc = 0, binding_count =1;
5020 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5021 int error_code = ACCOUNT_ERROR_NONE;
5022 account_stmt hstmt = NULL;
5024 if (!account_type->app_id) {
5025 ACCOUNT_ERROR("app id is mandetory field, it can not be NULL!!!!\n");
5026 return ACCOUNT_ERROR_INVALID_PARAMETER;
5029 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5030 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET AppId=?, ServiceProviderId=?, IconPath=?, "
5031 "SmallIconPath=?, MultipleAccountSupport=? WHERE AppId=? ", ACCOUNT_TYPE_TABLE);
5033 hstmt = _account_prepare_query(query);
5035 if( _account_db_err_code() == SQLITE_PERM ){
5036 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5037 return ACCOUNT_ERROR_PERMISSION_DENIED;
5038 } else if (_account_db_err_code() == SQLITE_BUSY){
5039 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
5040 return ACCOUNT_ERROR_DATABASE_BUSY;
5043 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg()));
5045 binding_count = _account_type_convert_account_to_sql(account_type, hstmt, query);
5046 _account_query_bind_text(hstmt, binding_count++, app_id);
5048 rc = _account_query_step(hstmt);
5049 if (rc != SQLITE_DONE) {
5050 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5053 rc = _account_query_finalize(hstmt);
5054 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5058 error_code = _account_type_update_label(account_type, app_id);
5059 /* update provider feature */
5060 error_code = _account_type_update_provider_feature(account_type, app_id);
5065 int _account_type_update_to_db_by_app_id(account_type_s* account_type, const char* app_id)
5067 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
5068 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5069 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5071 int error_code = ACCOUNT_ERROR_NONE;
5072 account_type_s* data = account_type;
5074 pthread_mutex_lock(&account_mutex);
5076 error_code = _account_type_update_account(data, app_id);
5078 pthread_mutex_unlock(&account_mutex);
5083 int _account_type_delete_by_app_id(const char* app_id)
5085 int error_code = ACCOUNT_ERROR_NONE;
5086 account_stmt hstmt = NULL;
5087 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5088 int rc = 0, count = -1;
5089 int ret_transaction = 0;
5090 int binding_count = 1;
5091 bool is_success = FALSE;
5093 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5094 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("The database isn't connected."));
5096 /* Check requested ID to delete */
5097 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
5099 count = _account_get_record_count(query);
5101 if( _account_db_err_code() == SQLITE_PERM ){
5102 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5103 return ACCOUNT_ERROR_PERMISSION_DENIED;
5107 ACCOUNT_SLOGE("app id(%s) is not exist. count(%d)\n", app_id, count);
5108 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5111 /* transaction control required*/
5112 ret_transaction = _account_begin_transaction();
5114 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
5115 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
5116 pthread_mutex_unlock(&account_mutex);
5117 return ACCOUNT_ERROR_DATABASE_BUSY;
5118 }else if (ret_transaction != ACCOUNT_ERROR_NONE) {
5119 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
5120 pthread_mutex_unlock(&account_mutex);
5121 return ret_transaction;
5124 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", LABEL_TABLE);
5126 hstmt = _account_prepare_query(query);
5128 if( _account_db_err_code() == SQLITE_PERM ){
5129 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5130 pthread_mutex_unlock(&account_mutex);
5131 return ACCOUNT_ERROR_PERMISSION_DENIED;
5134 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
5135 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
5137 _account_query_bind_text(hstmt, binding_count++, app_id);
5139 rc = _account_query_step(hstmt);
5140 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5142 rc = _account_query_finalize(hstmt);
5143 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5147 ACCOUNT_MEMSET(query, 0, sizeof(query));
5149 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id = ? ", PROVIDER_FEATURE_TABLE);
5151 hstmt = _account_prepare_query(query);
5152 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
5153 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
5155 _account_query_bind_text(hstmt, binding_count++, app_id);
5157 rc = _account_query_step(hstmt);
5158 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
5160 rc = _account_query_finalize(hstmt);
5161 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5167 ACCOUNT_MEMSET(query, 0, sizeof(query));
5169 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ? ", ACCOUNT_TYPE_TABLE);
5171 hstmt = _account_prepare_query(query);
5172 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
5173 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
5175 _account_query_bind_text(hstmt, binding_count++, app_id);
5177 rc = _account_query_step(hstmt);
5178 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
5180 rc = _account_query_finalize(hstmt);
5181 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5187 if (hstmt != NULL) {
5188 rc = _account_query_finalize(hstmt);
5189 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5193 ret_transaction = _account_end_transaction(is_success);
5195 if (ret_transaction != ACCOUNT_ERROR_NONE) {
5196 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
5199 pthread_mutex_unlock(&account_mutex);
5204 static void _account_type_convert_column_to_account_type(account_stmt hstmt, account_type_s *account_type_record)
5206 const char *textbuf = NULL;
5208 account_type_record->id = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_ID);
5210 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_APP_ID);
5211 _account_db_data_to_text(textbuf, &(account_type_record->app_id));
5213 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SERVICE_PROVIDER_ID);
5214 _account_db_data_to_text(textbuf, &(account_type_record->service_provider_id));
5216 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_ICON_PATH);
5217 _account_db_data_to_text(textbuf, &(account_type_record->icon_path));
5219 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SMALL_ICON_PATH);
5220 _account_db_data_to_text(textbuf, &(account_type_record->small_icon_path));
5222 account_type_record->multiple_account_support = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_MULTIPLE_ACCOUNT_SUPPORT);
5226 static void _account_type_convert_column_to_label(account_stmt hstmt, label_s *label_record)
5228 const char *textbuf = NULL;
5230 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_APP_ID);
5231 _account_db_data_to_text(textbuf, &(label_record->app_id));
5233 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LABEL);
5234 _account_db_data_to_text(textbuf, &(label_record->label));
5236 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LOCALE);
5237 _account_db_data_to_text(textbuf, &(label_record->locale));
5241 GSList* _account_type_get_label_list_by_app_id(const char* app_id, int *error_code )
5243 *error_code = ACCOUNT_ERROR_NONE;
5244 account_stmt hstmt = NULL;
5245 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5246 int rc = 0, binding_count = 1;
5247 GSList* label_list = NULL;
5249 ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
5250 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
5252 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5254 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
5255 hstmt = _account_prepare_query(query);
5257 if( _account_db_err_code() == SQLITE_PERM ){
5258 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5259 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
5263 _account_query_bind_text(hstmt, binding_count++, app_id);
5265 rc = _account_query_step(hstmt);
5266 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5268 label_s* label_record = NULL;
5270 while (rc == SQLITE_ROW) {
5271 label_record = (label_s*) malloc(sizeof(label_s));
5273 if (label_record == NULL) {
5274 ACCOUNT_FATAL("malloc Failed");
5278 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
5280 _account_type_convert_column_to_label(hstmt, label_record);
5282 _INFO("Adding account label_list");
5283 label_list = g_slist_append (label_list, label_record);
5285 rc = _account_query_step(hstmt);
5288 rc = _account_query_finalize(hstmt);
5289 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
5292 *error_code = ACCOUNT_ERROR_NONE;
5295 if (hstmt != NULL) {
5296 rc = _account_query_finalize(hstmt);
5297 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
5301 pthread_mutex_unlock(&account_mutex);
5302 _INFO("Returning account label_list");
5306 int account_type_query_label_by_app_id(account_label_cb callback, const char* app_id, void *user_data )
5308 int error_code = ACCOUNT_ERROR_NONE;
5309 account_stmt hstmt = NULL;
5310 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5311 int rc = 0, binding_count = 1;
5313 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5314 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
5315 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5317 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5319 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
5320 hstmt = _account_prepare_query(query);
5322 if( _account_db_err_code() == SQLITE_PERM ){
5323 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5324 return ACCOUNT_ERROR_PERMISSION_DENIED;
5327 _account_query_bind_text(hstmt, binding_count++, app_id);
5329 rc = _account_query_step(hstmt);
5330 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5332 label_s* label_record = NULL;
5334 while (rc == SQLITE_ROW) {
5335 bool cb_ret = FALSE;
5336 label_record = (label_s*) malloc(sizeof(label_s));
5338 if (label_record == NULL) {
5339 ACCOUNT_FATAL("malloc Failed");
5343 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
5345 _account_type_convert_column_to_label(hstmt, label_record);
5347 cb_ret = callback(label_record->app_id, label_record->label , label_record->locale, user_data);
5349 _account_type_free_label_items(label_record);
5350 _ACCOUNT_FREE(label_record);
5352 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
5354 rc = _account_query_step(hstmt);
5357 rc = _account_query_finalize(hstmt);
5358 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5361 error_code = ACCOUNT_ERROR_NONE;
5364 if (hstmt != NULL) {
5365 rc = _account_query_finalize(hstmt);
5366 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5370 pthread_mutex_unlock(&account_mutex);
5374 int _account_type_label_get_app_id(label_h label, char **app_id)
5377 return ACCOUNT_ERROR_INVALID_PARAMETER;
5381 return ACCOUNT_ERROR_INVALID_PARAMETER;
5384 label_s *data = (label_s*)label;
5388 *app_id = _account_get_text(data->app_id);
5390 return ACCOUNT_ERROR_NONE;
5393 bool _account_get_label_text_cb(char* app_id, char* label, char* locale, void *user_data)
5395 account_type_s *data = (account_type_s*)user_data;
5397 label_s *label_data = (label_s*)malloc(sizeof(label_s));
5399 if (label_data == NULL) {
5400 ACCOUNT_FATAL("_account_get_label_text_cb : MALLOC FAIL\n");
5403 ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
5405 label_data->app_id = _account_get_text(app_id);
5406 label_data->label = _account_get_text(label);
5407 label_data->locale = _account_get_text(locale);
5409 data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
5414 bool _account_get_provider_feature_cb(char* app_id, char* key, void* user_data)
5416 account_type_s *data = (account_type_s*)user_data;
5418 provider_feature_s *feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
5420 if (feature_data == NULL) {
5421 ACCOUNT_FATAL("_account_get_provider_feature_cb : MALLOC FAIL\n");
5424 ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
5426 feature_data->app_id = _account_get_text(app_id);
5427 feature_data->key = _account_get_text(key);
5429 data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
5434 int _account_type_query_by_app_id(const char* app_id, account_type_s** account_type_record)
5436 _INFO("_account_type_query_by_app_id start");
5438 int error_code = ACCOUNT_ERROR_NONE;
5439 account_stmt hstmt = NULL;
5440 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5441 int rc = 0, binding_count = 1;
5443 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5444 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5446 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5448 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
5449 hstmt = _account_prepare_query(query);
5451 if( _account_db_err_code() == SQLITE_PERM ){
5452 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5453 return ACCOUNT_ERROR_PERMISSION_DENIED;
5456 _account_query_bind_text(hstmt, binding_count++, app_id);
5458 rc = _account_query_step(hstmt);
5459 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5461 *account_type_record = create_empty_account_type_instance();
5463 while (rc == SQLITE_ROW) {
5464 _account_type_convert_column_to_account_type(hstmt, *account_type_record);
5465 rc = _account_query_step(hstmt);
5468 rc = _account_query_finalize(hstmt);
5469 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5470 account_type_query_label_by_app_id(_account_get_label_text_cb, app_id, (void*)(*account_type_record));
5471 account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, app_id,(void*)(*account_type_record));
5474 error_code = ACCOUNT_ERROR_NONE;
5477 if (hstmt != NULL) {
5478 rc = _account_query_finalize(hstmt);
5479 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5483 pthread_mutex_unlock(&account_mutex);
5484 _INFO("_account_type_query_by_app_id end [%d]", error_code);
5488 int _account_type_query_app_id_exist(const char* app_id)
5490 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5493 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5494 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5496 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5498 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
5499 rc = _account_get_record_count(query);
5501 if( _account_db_err_code() == SQLITE_PERM ){
5502 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5503 return ACCOUNT_ERROR_PERMISSION_DENIED;
5507 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5510 return ACCOUNT_ERROR_NONE;
5513 GSList* _account_type_query_by_provider_feature(const char* key, int *error_code)
5515 *error_code = ACCOUNT_ERROR_NONE;
5516 account_stmt hstmt = NULL;
5517 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5519 GSList *account_type_list = NULL;
5523 ACCOUNT_ERROR("capability_type IS NULL.");
5524 *error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
5528 if(g_hAccountDB == NULL)
5530 ACCOUNT_ERROR("The database isn't connected.");
5531 *error_code = ACCOUNT_ERROR_DB_NOT_OPENED;
5535 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5537 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
5539 hstmt = _account_prepare_query(query);
5541 if( _account_db_err_code() == SQLITE_PERM )
5543 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5544 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
5548 int binding_count = 1;
5549 _account_query_bind_text(hstmt, binding_count++, key);
5551 rc = _account_query_step(hstmt);
5553 account_type_s *account_type_record = NULL;
5555 if(rc != SQLITE_ROW)
5557 ACCOUNT_ERROR("The record isn't found.");
5558 *error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
5562 while(rc == SQLITE_ROW) {
5563 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
5565 if (account_type_record == NULL) {
5566 ACCOUNT_FATAL("malloc Failed");
5570 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
5571 _account_type_convert_column_to_account_type(hstmt, account_type_record);
5572 account_type_list = g_slist_append(account_type_list, account_type_record);
5573 rc = _account_query_step(hstmt);
5576 rc = _account_query_finalize(hstmt);
5577 if (rc != ACCOUNT_ERROR_NONE )
5579 _account_type_gslist_free(account_type_list);
5580 ACCOUNT_ERROR("finalize error(%s)", rc);
5588 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
5589 account_type_s *account_type = NULL;
5590 account_type = (account_type_s*)iter->data;
5591 account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
5592 account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
5595 *error_code = ACCOUNT_ERROR_NONE;
5598 if (hstmt != NULL) {
5599 rc = _account_query_finalize(hstmt);
5600 if (rc != ACCOUNT_ERROR_NONE)
5608 return account_type_list;
5612 GSList* _account_type_query_all(void)
5614 account_stmt hstmt = NULL;
5615 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5617 GSList *account_type_list = NULL;
5619 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected."));
5621 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5623 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
5624 hstmt = _account_prepare_query(query);
5626 rc = _account_query_step(hstmt);
5628 if( _account_db_err_code() == SQLITE_PERM ){
5629 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5633 account_type_s *account_type_record = NULL;
5635 if (rc != SQLITE_ROW)
5637 _INFO("[ACCOUNT_ERROR_RECORD_NOT_FOUND]The record isn't found.");
5641 while(rc == SQLITE_ROW) {
5642 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
5644 if (account_type_record == NULL) {
5645 ACCOUNT_FATAL("malloc Failed");
5649 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
5650 _account_type_convert_column_to_account_type(hstmt, account_type_record);
5651 account_type_list = g_slist_append(account_type_list, account_type_record);
5652 rc = _account_query_step(hstmt);
5655 rc = _account_query_finalize(hstmt);
5656 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, NULL, ("finalize error"));
5661 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
5662 account_type_s *account_type = NULL;
5663 account_type = (account_type_s*)iter->data;
5664 account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
5665 account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
5671 rc = _account_query_finalize(hstmt);
5672 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {_account_type_gslist_free(account_type_list);}, NULL, ("finalize error"));
5676 return account_type_list;
5679 // output parameter label must be free
5680 int _account_type_query_label_by_locale(const char* app_id, const char* locale, char **label)
5682 int error_code = ACCOUNT_ERROR_NONE;
5683 account_stmt hstmt = NULL;
5684 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5685 int rc = 0, binding_count = 1;
5686 char* converted_locale = NULL;
5688 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
5689 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5690 ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
5691 ACCOUNT_RETURN_VAL((locale != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("locale char is null"));
5692 //Making label newly created
5694 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5696 converted_locale = _account_get_text(locale);
5697 gchar** tokens = g_strsplit(converted_locale, "-", 2);
5699 if(tokens != NULL) {
5700 if((char*)(tokens[1]) != NULL) {
5701 char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1]));
5702 if(upper_token != NULL) {
5703 _ACCOUNT_FREE(converted_locale);
5704 converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token);
5706 _ACCOUNT_FREE(upper_token);
5711 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, converted_locale);
5712 _ACCOUNT_FREE(converted_locale);
5714 hstmt = _account_prepare_query(query);
5716 if( _account_db_err_code() == SQLITE_PERM ){
5717 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5718 return ACCOUNT_ERROR_PERMISSION_DENIED;
5721 _account_query_bind_text(hstmt, binding_count++, app_id);
5723 rc = _account_query_step(hstmt);
5724 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5726 label_s* label_record = NULL;
5728 while (rc == SQLITE_ROW) {
5729 label_record = (label_s*) malloc(sizeof(label_s));
5731 if (label_record == NULL) {
5732 ACCOUNT_FATAL("malloc Failed");
5736 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
5738 _account_type_convert_column_to_label(hstmt,label_record);
5740 _ACCOUNT_FREE(*label);
5741 //Making label newly created
5742 *label = _account_get_text(label_record->label);
5744 _account_type_free_label_items(label_record);
5745 _ACCOUNT_FREE(label_record);
5747 rc = _account_query_step(hstmt);
5750 rc = _account_query_finalize(hstmt);
5751 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5754 error_code = ACCOUNT_ERROR_NONE;
5757 if (hstmt != NULL) {
5758 rc = _account_query_finalize(hstmt);
5759 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5763 pthread_mutex_unlock(&account_mutex);
5764 _INFO("_account_type_query_label_by_locale() end : error_code = %d", error_code);
5768 static int _account_insert_custom(account_s *account, int account_id)
5770 _INFO("_account_insert_custom start");
5773 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5774 account_stmt hstmt = NULL;
5776 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
5778 if (g_slist_length( account->custom_list)==0) {
5779 ACCOUNT_DEBUG( "_account_insert_custom, no custom data\n");
5780 return ACCOUNT_ERROR_NONE;
5783 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
5785 rc = _account_get_record_count(query);
5787 if( _account_db_err_code() == SQLITE_PERM ){
5788 ACCOUNT_ERROR( "Access failed(%d, %s)", _account_db_err_msg());
5789 return ACCOUNT_ERROR_PERMISSION_DENIED;
5793 ACCOUNT_SLOGE( "_account_insert_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
5794 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5801 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
5804 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5805 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (AccountId, AppId, Key, Value) VALUES "
5806 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
5808 hstmt = _account_prepare_query(query);
5810 if( _account_db_err_code() == SQLITE_PERM ){
5811 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5812 return ACCOUNT_ERROR_PERMISSION_DENIED;
5815 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
5817 account_custom_s* custom_data = NULL;
5818 custom_data = (account_custom_s*)iter->data;
5820 ret = _account_query_bind_int(hstmt, count++, account_id);
5821 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
5822 ret = _account_query_bind_text(hstmt, count++, account->package_name);
5823 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5824 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
5825 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5826 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
5827 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5829 rc = _account_query_step(hstmt);
5831 if (rc != SQLITE_DONE) {
5832 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5836 rc = _account_query_finalize(hstmt);
5837 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5842 _INFO("_account_insert_custom end");
5843 return ACCOUNT_ERROR_NONE;
5846 static int _account_update_custom(account_s *account, int account_id)
5849 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5850 account_stmt hstmt = NULL;
5852 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
5854 if (g_slist_length( account->custom_list)==0) {
5855 ACCOUNT_DEBUG( "_account_update_custom, no custom data\n");
5856 return ACCOUNT_ERROR_NONE;
5859 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
5861 rc = _account_get_record_count(query);
5863 if( _account_db_err_code() == SQLITE_PERM ){
5864 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5865 pthread_mutex_unlock(&account_mutex);
5866 return ACCOUNT_ERROR_PERMISSION_DENIED;
5867 } else if( _account_db_err_code() == SQLITE_BUSY ){
5868 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
5869 pthread_mutex_unlock(&account_mutex);
5870 return ACCOUNT_ERROR_DATABASE_BUSY;
5874 ACCOUNT_SLOGE( "_account_update_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
5875 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5878 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5880 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId=? ", ACCOUNT_CUSTOM_TABLE);
5881 hstmt = _account_prepare_query(query);
5883 _account_query_bind_int(hstmt, count++, (int)account_id);
5884 rc = _account_query_step(hstmt);
5886 if (rc == SQLITE_BUSY) {
5887 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5888 return ACCOUNT_ERROR_DATABASE_BUSY;
5889 } else if (rc != SQLITE_DONE) {
5890 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5891 return ACCOUNT_ERROR_DB_FAILED;
5894 rc = _account_query_finalize(hstmt);
5895 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5900 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
5903 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5904 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AccountId, AppId, Key, Value) VALUES "
5905 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
5907 hstmt = _account_prepare_query(query);
5909 if( _account_db_err_code() == SQLITE_PERM ){
5910 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5911 return ACCOUNT_ERROR_PERMISSION_DENIED;
5914 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
5916 account_custom_s* custom_data = NULL;
5917 custom_data = (account_custom_s*)iter->data;
5919 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
5920 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
5921 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
5922 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5923 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
5924 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5925 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
5926 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5928 rc = _account_query_step(hstmt);
5930 if (rc != SQLITE_DONE) {
5931 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5935 rc = _account_query_finalize(hstmt);
5936 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5941 return ACCOUNT_ERROR_NONE;
5944 static int _account_query_custom_by_account_id(account_custom_cb callback, int account_id, void *user_data )
5946 int error_code = ACCOUNT_ERROR_NONE;
5947 account_stmt hstmt = NULL;
5948 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5951 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
5952 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
5953 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5955 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5957 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
5958 hstmt = _account_prepare_query(query);
5960 if( _account_db_err_code() == SQLITE_PERM ){
5961 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5962 return ACCOUNT_ERROR_PERMISSION_DENIED;
5965 rc = _account_query_step(hstmt);
5967 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5969 account_custom_s* custom_record = NULL;
5971 while (rc == SQLITE_ROW) {
5972 bool cb_ret = FALSE;
5973 custom_record = (account_custom_s*) malloc(sizeof(account_custom_s));
5975 if (custom_record == NULL) {
5976 ACCOUNT_FATAL("malloc Failed");
5980 ACCOUNT_MEMSET(custom_record, 0x00, sizeof(account_custom_s));
5982 _account_convert_column_to_custom(hstmt, custom_record);
5984 cb_ret = callback(custom_record->key, custom_record->value, user_data);
5986 _account_custom_item_free(custom_record);
5987 _ACCOUNT_FREE(custom_record);
5989 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
5991 rc = _account_query_step(hstmt);
5994 rc = _account_query_finalize(hstmt);
5995 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5998 error_code = ACCOUNT_ERROR_NONE;
6001 if (hstmt != NULL) {
6002 rc = _account_query_finalize(hstmt);
6003 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
6007 pthread_mutex_unlock(&account_mutex);